root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / InPolygonSpatialJoinB / InPolygonSpatialJoinBAlgorithm.java @ 59
History | View | Annotate | Download (4.93 KB)
1 |
package es.unex.sextante.vectorTools.InPolygonSpatialJoinB; |
---|---|
2 |
|
3 |
import com.vividsolutions.jts.geom.Geometry; |
4 |
|
5 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
6 |
import es.unex.sextante.core.GeoAlgorithm; |
7 |
import es.unex.sextante.core.Sextante; |
8 |
import es.unex.sextante.dataObjects.IFeature; |
9 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
10 |
import es.unex.sextante.dataObjects.IVectorLayer; |
11 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
12 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
13 |
import es.unex.sextante.outputs.OutputVectorLayer; |
14 |
|
15 |
|
16 |
public class InPolygonSpatialJoinBAlgorithm |
17 |
extends
|
18 |
GeoAlgorithm { |
19 |
|
20 |
public static final String RESULT = "RESULT"; |
21 |
public static final String MAIN = "MAIN"; |
22 |
public static final String SECONDARY = "SECONDARY"; |
23 |
public static final String FIELD = "FIELD"; |
24 |
//public static final String FIELDNAME = "FIELDNAME";
|
25 |
|
26 |
|
27 |
private IVectorLayer m_MainLayer;
|
28 |
private IVectorLayer m_SecondaryLayer;
|
29 |
|
30 |
private IVectorLayer m_Output;
|
31 |
|
32 |
private NearestNeighbourFinder m_NNF;
|
33 |
private int m_iField; |
34 |
private Class m_Type; |
35 |
|
36 |
|
37 |
@Override
|
38 |
public void defineCharacteristics() { |
39 |
|
40 |
setName(Sextante.getText("InPolygonSpatialJoinB"));
|
41 |
setGroup(Sextante.getText("Tools_for_polygon_layers"));
|
42 |
setUserCanDefineAnalysisExtent(true);
|
43 |
|
44 |
try {
|
45 |
m_Parameters.addInputVectorLayer(MAIN, Sextante.getText("Polygons"), AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON, true); |
46 |
m_Parameters.addInputVectorLayer(SECONDARY, Sextante.getText("Secondary_layer"),
|
47 |
AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true);
|
48 |
m_Parameters.addTableField(FIELD, Sextante.getText("Field"), MAIN);
|
49 |
addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_POLYGON, SECONDARY);
|
50 |
} |
51 |
catch (final Exception e) { |
52 |
Sextante.addErrorToLog(e); |
53 |
} |
54 |
|
55 |
} |
56 |
|
57 |
|
58 |
@Override
|
59 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
60 |
|
61 |
int i;
|
62 |
int iShapeCount;
|
63 |
|
64 |
m_MainLayer = m_Parameters.getParameterValueAsVectorLayer(MAIN); |
65 |
m_SecondaryLayer = m_Parameters.getParameterValueAsVectorLayer(SECONDARY); |
66 |
|
67 |
if (!m_bIsAutoExtent) {
|
68 |
m_MainLayer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
69 |
m_SecondaryLayer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
70 |
} |
71 |
|
72 |
m_iField = m_Parameters.getParameterValueAsInt(FIELD); |
73 |
|
74 |
final String[] sFields = new String[m_SecondaryLayer.getFieldCount() + 1]; |
75 |
final Class[] types = new Class[m_SecondaryLayer.getFieldCount() + 1]; |
76 |
for (i = 0; i < m_SecondaryLayer.getFieldCount(); i++) { |
77 |
sFields[i] = m_SecondaryLayer.getFieldName(i); |
78 |
types[i] = m_SecondaryLayer.getFieldType(i); |
79 |
} |
80 |
sFields[m_SecondaryLayer.getFieldCount()] = m_MainLayer.getFieldName(m_iField); |
81 |
m_Type = m_MainLayer.getFieldType(m_iField); |
82 |
types[m_SecondaryLayer.getFieldCount()] = m_Type; |
83 |
|
84 |
m_Output = getNewVectorLayer(RESULT, m_SecondaryLayer.getName(), m_SecondaryLayer.getShapeType(), types, sFields); |
85 |
|
86 |
m_NNF = new NearestNeighbourFinder(m_MainLayer, this.m_Task); |
87 |
|
88 |
iShapeCount = m_SecondaryLayer.getShapesCount(); |
89 |
i = 0;
|
90 |
setProgressText(Sextante.getText("Processing"));
|
91 |
final IFeatureIterator iter = m_SecondaryLayer.iterator();
|
92 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
93 |
final IFeature feature = iter.next();
|
94 |
performSpatialJoin(feature); |
95 |
i++; |
96 |
} |
97 |
iter.close(); |
98 |
|
99 |
return !m_Task.isCanceled();
|
100 |
|
101 |
} |
102 |
|
103 |
|
104 |
private void performSpatialJoin(final IFeature feature) { |
105 |
|
106 |
final Geometry geom = feature.getGeometry();
|
107 |
final IFeature[] polygons = m_NNF.getClosestPoints(geom); |
108 |
|
109 |
for (final IFeature polygon : polygons) { |
110 |
if (polygon.getGeometry().contains(geom)) {
|
111 |
m_Output.addFeature(feature.getGeometry(), calculateRecord(polygon.getRecord().getValue(m_iField), |
112 |
feature.getRecord().getValues())); |
113 |
return;
|
114 |
} |
115 |
} |
116 |
|
117 |
Object value;
|
118 |
|
119 |
if (Integer.class.isAssignableFrom(m_Type)) { |
120 |
value = new Integer(-99999); |
121 |
} |
122 |
else if (Double.class.isAssignableFrom(m_Type)) { |
123 |
value = new Integer(-99999); |
124 |
|
125 |
} |
126 |
else {
|
127 |
value = "";
|
128 |
} |
129 |
|
130 |
m_Output.addFeature(feature.getGeometry(), calculateRecord(value, feature.getRecord().getValues())); |
131 |
|
132 |
} |
133 |
|
134 |
|
135 |
private Object[] calculateRecord(final Object value, |
136 |
final Object[] record) { |
137 |
|
138 |
final Object[] values = new Object[record.length + 1]; |
139 |
System.arraycopy(record, 0, values, 0, record.length); |
140 |
values[values.length - 1] = value;
|
141 |
return values;
|
142 |
|
143 |
} |
144 |
|
145 |
|
146 |
} |