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 @ 63
History | View | Annotate | Download (7.02 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.HashMap; |
22 |
|
23 |
import org.gvsig.fmap.dal.DALLocator; |
24 |
import org.gvsig.fmap.dal.DataManager; |
25 |
import org.gvsig.fmap.dal.exception.DataException; |
26 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
28 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
29 |
import org.gvsig.fmap.dal.feature.FeatureType; |
30 |
import org.gvsig.sextante.app.algorithm.spatialjoin.SpatiallyIndexedSpatialJoinOperation; |
31 |
import org.gvsig.sextante.app.extension.core.gvGeoAlgorithm; |
32 |
import org.gvsig.sextante.app.extension.core.gvVectorLayer; |
33 |
|
34 |
import es.unex.sextante.core.Sextante; |
35 |
import es.unex.sextante.dataObjects.IVectorLayer; |
36 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
37 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
38 |
import es.unex.sextante.outputs.OutputVectorLayer; |
39 |
|
40 |
/**
|
41 |
* Spatial join algorithm
|
42 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
43 |
*/
|
44 |
public class SpatialJoinAlgorithm extends gvGeoAlgorithm { |
45 |
public static final String RESULT = "RESULT"; |
46 |
public static final String LAYER1 = "LAYER1"; |
47 |
public static final String LAYER2 = "LAYER2"; |
48 |
public static final String SELECTED_GEOM = "SELECTED_GEOM"; |
49 |
public static final String NEAREST = "NEAREST"; |
50 |
public static final String FUNCTION_LIST = "FUNCTION_LIST"; |
51 |
public static final String Summary[] = {"Min", "Max", "Sum", "Avg"}; |
52 |
private boolean funcList[] = new boolean[Summary.length]; |
53 |
private HashMap<String, String> funcMap = new HashMap<String, String>(); |
54 |
|
55 |
/*
|
56 |
* (non-Javadoc)
|
57 |
* @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
58 |
*/
|
59 |
public void defineCharacteristics(){ |
60 |
setName(Sextante.getText("Spatialjoin"));
|
61 |
setGroup(Sextante.getText("gvSIG_Algorithms"));
|
62 |
setGeneratesUserDefinedRasterOutput(false);
|
63 |
|
64 |
try {
|
65 |
m_Parameters.addInputVectorLayer(LAYER1, |
66 |
Sextante.getText("Input_layer"),
|
67 |
IVectorLayer.SHAPE_TYPE_WRONG, |
68 |
true);
|
69 |
m_Parameters.addInputVectorLayer(LAYER2, |
70 |
Sextante.getText( "Input_layer"),
|
71 |
IVectorLayer.SHAPE_TYPE_WRONG, |
72 |
true);
|
73 |
m_Parameters.addBoolean(SELECTED_GEOM, Sextante.getText("Selected_geometries"), false); |
74 |
m_Parameters.addBoolean(NEAREST, Sextante.getText("use_the_nearest"), false); |
75 |
m_Parameters.addString(FUNCTION_LIST, Sextante.getText("Function_list"));
|
76 |
} catch (RepeatedParameterNameException e) {
|
77 |
Sextante.addErrorToLog(e); |
78 |
} |
79 |
addOutputVectorLayer(RESULT, |
80 |
Sextante.getText("Spatialjoin"),
|
81 |
OutputVectorLayer.SHAPE_TYPE_UNDEFINED); |
82 |
} |
83 |
|
84 |
/*
|
85 |
* (non-Javadoc)
|
86 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
87 |
*/
|
88 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
89 |
IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1); |
90 |
IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2); |
91 |
boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
|
92 |
boolean nearest = m_Parameters.getParameterValueAsBoolean(NEAREST);
|
93 |
String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
|
94 |
loadSummary(functionList); |
95 |
|
96 |
gvVectorLayer lyr1 = null;
|
97 |
gvVectorLayer lyr2 = null;
|
98 |
if(layer2 instanceof gvVectorLayer && layer1 instanceof gvVectorLayer) { |
99 |
lyr2 = ((gvVectorLayer)layer2); |
100 |
lyr1 = ((gvVectorLayer)layer1); |
101 |
} else
|
102 |
return false; |
103 |
|
104 |
DataManager dataManager = DALLocator.getDataManager(); |
105 |
|
106 |
//Builds a new JSIRTree index. To do that we have to replace the default index and
|
107 |
//when the operation has finished then it restores the original default index
|
108 |
String indexName = "GEOMETRY"; |
109 |
|
110 |
try {
|
111 |
indexName = lyr2.getFeatureStore().getDefaultFeatureType().getDefaultGeometryAttributeName(); |
112 |
FeatureAttributeDescriptor fat = lyr2.getFeatureStore().getDefaultFeatureType().getAttributeDescriptor(indexName); |
113 |
String defaultIndex = dataManager.getDefaultFeatureIndexProviderName(fat.getDataType());
|
114 |
dataManager.setDefaultFeatureIndexProviderName(fat.getDataType(), "JSIRTree");
|
115 |
lyr2.getFeatureStore().createIndex(lyr2.getFeatureStore().getDefaultFeatureType(), indexName, indexName + "_idx");
|
116 |
dataManager.setDefaultFeatureIndexProviderName(fat.getDataType(), defaultIndex); |
117 |
} catch (DataException e) {
|
118 |
Sextante.addErrorToLog(e); |
119 |
} |
120 |
|
121 |
//Builds the output and computes the operation
|
122 |
try {
|
123 |
if(nearest) {
|
124 |
FeatureSet features = null;
|
125 |
features = lyr1.getFeatureStore().getFeatureSet(); |
126 |
FeatureType featureType1 = features.getDefaultFeatureType(); |
127 |
features = lyr2.getFeatureStore().getFeatureSet(); |
128 |
FeatureType featureType2 = features.getDefaultFeatureType(); |
129 |
|
130 |
FeatureStore outFeatStore = buildOutPutStoreFromUnion(featureType1, |
131 |
featureType2, |
132 |
lyr1.getShapeType(), |
133 |
Sextante.getText("SpatialJoin"),
|
134 |
RESULT, |
135 |
"DIST",
|
136 |
Double.class);
|
137 |
|
138 |
SpatiallyIndexedSpatialJoinOperation operation = new SpatiallyIndexedSpatialJoinOperation(lyr2, indexName + "_idx"); |
139 |
operation.setProgressModel(this);
|
140 |
operation.computesGeometryOperation(lyr1.getFeatureStore(), |
141 |
outFeatStore, |
142 |
attrNames, |
143 |
selectedGeom, |
144 |
true);
|
145 |
} |
146 |
} catch (DataException e) {
|
147 |
Sextante.addErrorToLog(e); |
148 |
} |
149 |
|
150 |
return true; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Checks if the parameter is in Summary list
|
155 |
* @param it
|
156 |
* @return the position in the list
|
157 |
*/
|
158 |
private int isInList(String it) { |
159 |
for (int i = 0; i < Summary.length; i++) { |
160 |
if(Summary[i].compareTo(it) == 0) |
161 |
return i;
|
162 |
} |
163 |
return -1; |
164 |
} |
165 |
|
166 |
/**
|
167 |
* Loads the list of functions to use
|
168 |
* @param functionList
|
169 |
*/
|
170 |
private void loadSummary(String functionList) { |
171 |
String[] attrList = functionList.split(";"); |
172 |
for (int i = 0; i < attrList.length; i++) { |
173 |
String[] func = attrList[i].split(","); |
174 |
for (int j = 1; j < func.length; j++) { |
175 |
int pos = isInList(func[j]);
|
176 |
if(pos != -1) { |
177 |
funcList[pos] = true;
|
178 |
funcMap.put(Summary[pos], func[0]);
|
179 |
} |
180 |
} |
181 |
} |
182 |
} |
183 |
|
184 |
} |