gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.spatialjoin / src / main / java / org / gvsig / geoprocess / algorithm / spatialjoin / SpatialJoinAlgorithm.java @ 244
History | View | Annotate | Download (10.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007,2012 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*/
|
21 |
package org.gvsig.geoprocess.algorithm.spatialjoin; |
22 |
|
23 |
import java.util.ArrayList; |
24 |
import java.util.HashMap; |
25 |
import java.util.Iterator; |
26 |
|
27 |
import es.unex.sextante.core.Sextante; |
28 |
import es.unex.sextante.dataObjects.IVectorLayer; |
29 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
30 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
31 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
32 |
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel; |
33 |
import es.unex.sextante.outputs.OutputVectorLayer; |
34 |
|
35 |
import org.gvsig.fmap.dal.DALLocator; |
36 |
import org.gvsig.fmap.dal.DataManager; |
37 |
import org.gvsig.fmap.dal.DataTypes; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
40 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
41 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
42 |
import org.gvsig.fmap.dal.feature.FeatureType; |
43 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
44 |
import org.gvsig.geoprocess.algorithm.dissolve.DissolveRule; |
45 |
import org.gvsig.geoprocess.algorithm.dissolve.IDissolveRule; |
46 |
import org.gvsig.geoprocess.algorithm.dissolve.Summary; |
47 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
48 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
49 |
|
50 |
/**
|
51 |
* Spatial join algorithm
|
52 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
53 |
*/
|
54 |
public class SpatialJoinAlgorithm extends AbstractSextanteGeoProcess { |
55 |
|
56 |
public static final String NEW_FIELD = "NUM_RELA"; |
57 |
public static final String RESULT = "RESULT"; |
58 |
public static final String LAYER1 = "LAYER1"; |
59 |
public static final String LAYER2 = "LAYER2"; |
60 |
public static final String SELECTED_GEOM = "SELECTED_GEOM"; |
61 |
public static final String NEAREST = "NEAREST"; |
62 |
public static final String FUNCTION_LIST = "FUNCTION_LIST"; |
63 |
public static final String Summary[] = {"Min", "Max", "Sum", "Avg"}; |
64 |
private boolean funcList[] = new boolean[Summary.length]; |
65 |
private HashMap<String, String> funcMap = new HashMap<String, String>(); |
66 |
|
67 |
/*
|
68 |
* (non-Javadoc)
|
69 |
* @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
70 |
*/
|
71 |
public void defineCharacteristics(){ |
72 |
setName(getTranslation("Spatialjoin"));
|
73 |
setGroup(getTranslation("basic_vect_algorithms"));
|
74 |
// setGeneratesUserDefinedRasterOutput(false);
|
75 |
|
76 |
try {
|
77 |
m_Parameters.addInputVectorLayer(LAYER1, |
78 |
getTranslation("Input_layer"),
|
79 |
IVectorLayer.SHAPE_TYPE_WRONG, |
80 |
true);
|
81 |
m_Parameters.addInputVectorLayer(LAYER2, |
82 |
getTranslation("Input_layer"),
|
83 |
IVectorLayer.SHAPE_TYPE_WRONG, |
84 |
true);
|
85 |
m_Parameters.addBoolean(SELECTED_GEOM, |
86 |
getTranslation("Selected_geometries"), false); |
87 |
m_Parameters.addBoolean(NEAREST, getTranslation("use_the_nearest"),
|
88 |
false);
|
89 |
m_Parameters.addString(FUNCTION_LIST, |
90 |
getTranslation("Function_list"));
|
91 |
} catch (RepeatedParameterNameException e) {
|
92 |
Sextante.addErrorToLog(e); |
93 |
} |
94 |
addOutputVectorLayer(RESULT, |
95 |
getTranslation("Spatialjoin"),
|
96 |
OutputVectorLayer.SHAPE_TYPE_UNDEFINED); |
97 |
} |
98 |
|
99 |
/*
|
100 |
* (non-Javadoc)
|
101 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
102 |
*/
|
103 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
104 |
IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1); |
105 |
IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2); |
106 |
boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
|
107 |
boolean nearest = m_Parameters.getParameterValueAsBoolean(NEAREST);
|
108 |
String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
|
109 |
loadSummary(functionList); |
110 |
|
111 |
FlyrVectIVectorLayer lyr1 = null;
|
112 |
FlyrVectIVectorLayer lyr2 = null;
|
113 |
if(layer2 instanceof FlyrVectIVectorLayer && layer1 instanceof FlyrVectIVectorLayer) { |
114 |
lyr2 = ((FlyrVectIVectorLayer)layer2); |
115 |
lyr1 = ((FlyrVectIVectorLayer)layer1); |
116 |
} else
|
117 |
return false; |
118 |
|
119 |
DataManager dataManager = DALLocator.getDataManager(); |
120 |
|
121 |
//Builds a new JSIRTree index. To do that we have to replace the default index and
|
122 |
//when the operation has finished then it restores the original default index
|
123 |
String indexName = "GEOMETRY"; |
124 |
|
125 |
try {
|
126 |
indexName = lyr2.getFeatureStore().getDefaultFeatureType().getDefaultGeometryAttributeName(); |
127 |
FeatureAttributeDescriptor fat = lyr2.getFeatureStore().getDefaultFeatureType().getAttributeDescriptor(indexName); |
128 |
String defaultIndex = dataManager
|
129 |
.getDefaultFeatureIndexProviderName(fat.getDataType() |
130 |
.getType()); |
131 |
dataManager.setDefaultFeatureIndexProviderName(fat.getDataType() |
132 |
.getType(), "JSIRTree");
|
133 |
lyr2.getFeatureStore().createIndex(lyr2.getFeatureStore().getDefaultFeatureType(), indexName, indexName + "_idx");
|
134 |
dataManager.setDefaultFeatureIndexProviderName(fat.getDataType() |
135 |
.getType(), defaultIndex); |
136 |
} catch (DataException e) {
|
137 |
Sextante.addErrorToLog(e); |
138 |
} |
139 |
|
140 |
//Builds the output and computes the operation
|
141 |
try {
|
142 |
FeatureSet features = null;
|
143 |
features = lyr1.getFeatureStore().getFeatureSet(); |
144 |
FeatureType featureType1 = features.getDefaultFeatureType(); |
145 |
features = lyr2.getFeatureStore().getFeatureSet(); |
146 |
FeatureType featureType2 = features.getDefaultFeatureType(); |
147 |
|
148 |
GeometryOperation operation = null;
|
149 |
FeatureStore outFeatStore = null;
|
150 |
|
151 |
if (nearest) {
|
152 |
outFeatStore = |
153 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
154 |
lyr1.getShapeType(), getTranslation("SpatialJoin"),
|
155 |
RESULT, "DIST", Double.class); |
156 |
|
157 |
operation = |
158 |
new SpatiallyIndexedSpatialJoinOperation(lyr2, indexName
|
159 |
+ "_idx");
|
160 |
} else {
|
161 |
outFeatStore = |
162 |
buildSpatialJoinOutPutStore(featureType1, |
163 |
lyr1.getShapeType(), getTranslation("SpatialJoin"),
|
164 |
RESULT); |
165 |
IDissolveRule rule = new DissolveRule(0, funcMap); |
166 |
Summary summary = |
167 |
new Summary(rule, outFeatStore.getDefaultFeatureType());
|
168 |
operation = |
169 |
new IntersectsSpatialJoinOperation(lyr2,
|
170 |
indexName + "_idx", summary);
|
171 |
} |
172 |
|
173 |
operation.setTaskStatus(getStatus()); |
174 |
operation.computesGeometryOperation(lyr1.getFeatureStore(), |
175 |
outFeatStore, |
176 |
attrNames, |
177 |
selectedGeom, |
178 |
true);
|
179 |
} catch (DataException e) {
|
180 |
Sextante.addErrorToLog(e); |
181 |
} |
182 |
|
183 |
return true; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Checks if the parameter is in Summary list
|
188 |
* @param it
|
189 |
* @return the position in the list
|
190 |
*/
|
191 |
private int isInList(String it) { |
192 |
for (int i = 0; i < Summary.length; i++) { |
193 |
if(Summary[i].compareTo(it) == 0) |
194 |
return i;
|
195 |
} |
196 |
return -1; |
197 |
} |
198 |
|
199 |
/**
|
200 |
* Loads the list of functions to use
|
201 |
* @param functionList
|
202 |
*/
|
203 |
private void loadSummary(String functionList) { |
204 |
String[] attrList = functionList.split(";"); |
205 |
for (int i = 0; i < attrList.length; i++) { |
206 |
String[] func = attrList[i].split(","); |
207 |
for (int j = 1; j < func.length; j++) { |
208 |
int pos = isInList(func[j]);
|
209 |
if(pos != -1) { |
210 |
funcList[pos] = true;
|
211 |
funcMap.put(Summary[pos], func[0]);
|
212 |
} |
213 |
} |
214 |
} |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Builds the output FeatureStore
|
219 |
* @param featureType
|
220 |
* @return FeatureStore
|
221 |
*/
|
222 |
@SuppressWarnings("unchecked") |
223 |
protected FeatureStore buildSpatialJoinOutPutStore(FeatureType featureType1,
|
224 |
int shapeType,
|
225 |
String sextanteLayerName,
|
226 |
String sextanteLayerLabel) {
|
227 |
ArrayList<Class> typesList = new ArrayList<Class>(); |
228 |
ArrayList<String> attr = new ArrayList<String>(); |
229 |
|
230 |
Iterator it = featureType1.iterator();
|
231 |
while( it.hasNext() ) {
|
232 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor)it.next(); |
233 |
if (attribute.getDataType().getType() != DataTypes.GEOMETRY) {
|
234 |
attr.add(attribute.getName()); |
235 |
typesList.add(attribute.getObjectClass()); |
236 |
} |
237 |
} |
238 |
|
239 |
for (int i = 0; i < funcList.length; i++) { |
240 |
if(funcList[i]) {
|
241 |
String fieldName = funcMap.get(Summary[i]);
|
242 |
if(fieldName.length() >= 6) |
243 |
fieldName = fieldName.substring(0, 7); |
244 |
attr.add(fieldName + "_" + Summary[i]);
|
245 |
typesList.add(Double.class);
|
246 |
} |
247 |
} |
248 |
|
249 |
attr.add(NEW_FIELD); |
250 |
typesList.add(Integer.class);
|
251 |
|
252 |
attrNames = new String[attr.size()]; |
253 |
attr.toArray(attrNames); |
254 |
Class[] types = new Class[typesList.size()]; |
255 |
typesList.toArray(types); |
256 |
|
257 |
try {
|
258 |
IVectorLayer output = getNewVectorLayer(sextanteLayerLabel, |
259 |
sextanteLayerName, |
260 |
shapeType, types, attrNames); |
261 |
return ((FlyrVectIVectorLayer)output).getFeatureStore();
|
262 |
} catch (UnsupportedOutputChannelException e) {
|
263 |
Sextante.addErrorToLog(e); |
264 |
} catch (GeoAlgorithmExecutionException e) {
|
265 |
Sextante.addErrorToLog(e); |
266 |
} |
267 |
return null; |
268 |
} |
269 |
|
270 |
@Override
|
271 |
public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() { |
272 |
return SpatialJoinParametersPanel.class;
|
273 |
} |
274 |
} |