Statistics
| Revision:

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 @ 64

History | View | Annotate | Download (9.28 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.exception.DataException;
28
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
29
import org.gvsig.fmap.dal.feature.FeatureSet;
30
import org.gvsig.fmap.dal.feature.FeatureStore;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32
import org.gvsig.sextante.app.algorithm.base.core.GeometryOperation;
33
import org.gvsig.sextante.app.algorithm.dissolve.DissolveRule;
34
import org.gvsig.sextante.app.algorithm.dissolve.IDissolveRule;
35
import org.gvsig.sextante.app.algorithm.dissolve.Summary;
36
import org.gvsig.sextante.app.extension.core.gvGeoAlgorithm;
37
import org.gvsig.sextante.app.extension.core.gvVectorLayer;
38

    
39
import es.unex.sextante.core.Sextante;
40
import es.unex.sextante.dataObjects.IVectorLayer;
41
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
42
import es.unex.sextante.exceptions.RepeatedParameterNameException;
43
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
44
import es.unex.sextante.outputs.OutputVectorLayer;
45

    
46
/**
47
 * Spatial join algorithm
48
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
49
 */
50
public class SpatialJoinAlgorithm extends gvGeoAlgorithm {
51
        public static final String        RESULT            = "RESULT";
52
        public static final String        LAYER1            = "LAYER1";
53
        public static final String        LAYER2            = "LAYER2";
54
        public static final String        SELECTED_GEOM     = "SELECTED_GEOM";
55
        public static final String        NEAREST           = "NEAREST";
56
        public static final String        FUNCTION_LIST     = "FUNCTION_LIST";
57
        public static final String        Summary[]         = {"Min", "Max", "Sum", "Avg"};
58
        private boolean                   funcList[]        = new boolean[Summary.length];
59
        private HashMap<String, String>   funcMap           = new HashMap<String, String>();
60
        
61
        /*
62
         * (non-Javadoc)
63
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
64
         */
65
        public void defineCharacteristics(){
66
                setName(Sextante.getText("Spatialjoin"));
67
                setGroup(Sextante.getText("gvSIG_Algorithms"));
68
                setGeneratesUserDefinedRasterOutput(false);
69
                
70
                try {
71
                        m_Parameters.addInputVectorLayer(LAYER1, 
72
                                                                                                Sextante.getText("Input_layer"), 
73
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
74
                                                                                                true);
75
                        m_Parameters.addInputVectorLayer(LAYER2, 
76
                                                                                                Sextante.getText( "Input_layer"), 
77
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
78
                                                                                                true);
79
                        m_Parameters.addBoolean(SELECTED_GEOM, Sextante.getText("Selected_geometries"), false);
80
                        m_Parameters.addBoolean(NEAREST, Sextante.getText("use_the_nearest"), false);
81
                        m_Parameters.addString(FUNCTION_LIST, Sextante.getText("Function_list"));
82
                } catch (RepeatedParameterNameException e) {
83
                        Sextante.addErrorToLog(e);
84
                }
85
                addOutputVectorLayer(RESULT,
86
                                                                Sextante.getText("Spatialjoin"),
87
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
88
        }
89
        
90
        /*
91
         * (non-Javadoc)
92
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
93
         */
94
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
95
                IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1);
96
                IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2);
97
                boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
98
                boolean nearest = m_Parameters.getParameterValueAsBoolean(NEAREST);
99
                String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
100
                loadSummary(functionList);
101

    
102
                gvVectorLayer lyr1 = null;
103
                gvVectorLayer lyr2 = null;
104
                if(layer2 instanceof gvVectorLayer && layer1 instanceof gvVectorLayer) { 
105
                        lyr2 = ((gvVectorLayer)layer2);
106
                        lyr1 = ((gvVectorLayer)layer1);
107
                } else 
108
                        return false;
109

    
110
                DataManager dataManager = DALLocator.getDataManager();
111

    
112
                //Builds a new JSIRTree index. To do that we have to replace the default index and 
113
                //when the operation has finished then it restores the original default index 
114
                String indexName = "GEOMETRY";
115
                
116
                try {
117
                        indexName = lyr2.getFeatureStore().getDefaultFeatureType().getDefaultGeometryAttributeName();
118
                        FeatureAttributeDescriptor fat = lyr2.getFeatureStore().getDefaultFeatureType().getAttributeDescriptor(indexName);
119
                        String defaultIndex = dataManager.getDefaultFeatureIndexProviderName(fat.getDataType());
120
                        dataManager.setDefaultFeatureIndexProviderName(fat.getDataType(), "JSIRTree");
121
                        lyr2.getFeatureStore().createIndex(lyr2.getFeatureStore().getDefaultFeatureType(), indexName, indexName + "_idx");
122
                        dataManager.setDefaultFeatureIndexProviderName(fat.getDataType(), defaultIndex);
123
                } catch (DataException e) {
124
                        Sextante.addErrorToLog(e);
125
                }
126

    
127
                //Builds the output and computes the operation
128
                try {
129
                        FeatureSet features = null;
130
                        features = lyr1.getFeatureStore().getFeatureSet();
131
                        FeatureType featureType1 = features.getDefaultFeatureType();
132
                        features = lyr2.getFeatureStore().getFeatureSet();
133
                        FeatureType featureType2 = features.getDefaultFeatureType();
134
                        
135
                        GeometryOperation operation = null;
136
                        FeatureStore outFeatStore = null;
137
                        
138
                        if(nearest) {
139
                                outFeatStore = buildOutPutStoreFromUnion(featureType1, 
140
                                                featureType2, 
141
                                                lyr1.getShapeType(), 
142
                                                Sextante.getText("SpatialJoin"), 
143
                                                RESULT, 
144
                                                "DIST", 
145
                                                Double.class);
146

    
147
                                operation = new SpatiallyIndexedSpatialJoinOperation(lyr2, indexName + "_idx");
148
                        } else {
149
                                outFeatStore = buildSpatialJoinOutPutStore(featureType1,
150
                                                lyr1.getShapeType(), 
151
                                                Sextante.getText("SpatialJoin"), 
152
                                                RESULT);
153
                                IDissolveRule rule = new DissolveRule(0, funcMap);
154
                                Summary summary = new Summary(rule, outFeatStore.getDefaultFeatureType());
155
                                //summary.loadDefaultSummarizes(this.feature);
156
                                operation = new IntersectsSpatialJoinOperation(lyr2, indexName + "_idx", summary);
157
                        }
158
                        
159
                        operation.setProgressModel(this);
160
                        operation.computesGeometryOperation(lyr1.getFeatureStore(), 
161
                                        outFeatStore, 
162
                                        attrNames, 
163
                                        selectedGeom, 
164
                                        true);                
165
                } catch (DataException e) {
166
                        Sextante.addErrorToLog(e);
167
                }
168

    
169
                return true;
170
        }
171

    
172
        /**
173
         * Checks if the parameter is in Summary list
174
         * @param it
175
         * @return the position in the list
176
         */
177
        private int isInList(String it) {
178
                for (int i = 0; i < Summary.length; i++) {
179
                        if(Summary[i].compareTo(it) == 0)
180
                                return i;
181
                }
182
                return -1;
183
        }
184
        
185
        /**
186
         * Loads the list of functions to use
187
         * @param functionList
188
         */
189
        private void loadSummary(String functionList) {
190
                String[] attrList = functionList.split(";");
191
                for (int i = 0; i < attrList.length; i++) {
192
                        String[] func = attrList[i].split(",");
193
                        for (int j = 1; j < func.length; j++) {
194
                                int pos = isInList(func[j]);
195
                                if(pos != -1) {
196
                                        funcList[pos] = true;
197
                                        funcMap.put(Summary[pos], func[0]);
198
                                }
199
                        }
200
                }
201
        }
202
        
203
        /**
204
         * Builds the output FeatureStore 
205
         * @param featureType
206
         * @return FeatureStore
207
         */
208
        @SuppressWarnings("unchecked")
209
        protected FeatureStore buildSpatialJoinOutPutStore(FeatureType featureType1,
210
                                                                                        int shapeType,
211
                                                                                        String sextanteLayerName, 
212
                                                                                        String sextanteLayerLabel) {
213
                ArrayList<Class> typesList = new ArrayList<Class>();
214
                ArrayList<String> attr = new ArrayList<String>();
215
                
216
                Iterator it = featureType1.iterator();
217
                while( it.hasNext() ) {
218
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor)it.next();
219
                        if(attribute.getName().compareTo("GEOMETRY") != 0) {
220
                                attr.add(attribute.getName());
221
                                typesList.add(attribute.getObjectClass());
222
                        }
223
                }
224
                
225
                for (int i = 0; i < funcList.length; i++) {
226
                        if(funcList[i]) {
227
                                String fieldName = funcMap.get(Summary[i]);
228
                                if(fieldName.length() >= 6)
229
                                        fieldName = fieldName.substring(0, 7);
230
                                attr.add(fieldName + "_" + Summary[i]);
231
                                typesList.add(Double.class);
232
                        }
233
                }
234
                
235
                attr.add("NUM_RELA");
236
                typesList.add(Integer.class);
237
                
238
                attrNames = new String[attr.size()];
239
                attr.toArray(attrNames);
240
                Class[] types = new Class[typesList.size()];
241
                typesList.toArray(types);
242
                
243
                try {
244
                        IVectorLayer output = getNewVectorLayer(sextanteLayerLabel,
245
                                                                                                        sextanteLayerName,
246
                                                                                                        shapeType, types, attrNames);
247
                        return ((gvVectorLayer)output).getFeatureStore();
248
                } catch (UnsupportedOutputChannelException e) {
249
                        Sextante.addErrorToLog(e);
250
                }
251
                return null;
252
        }
253
        
254
}