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