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

History | View | Annotate | Download (7.02 KB)

1 45 nbrodin
/* 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 21 nbrodin
package org.gvsig.sextante.app.algorithm.spatialjoin;
20
21 45 nbrodin
import java.util.HashMap;
22 39 nbrodin
23 60 nbrodin
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 63 nbrodin
import org.gvsig.sextante.app.algorithm.spatialjoin.SpatiallyIndexedSpatialJoinOperation;
31 60 nbrodin
import org.gvsig.sextante.app.extension.core.gvGeoAlgorithm;
32
import org.gvsig.sextante.app.extension.core.gvVectorLayer;
33
34 21 nbrodin
import es.unex.sextante.core.Sextante;
35 39 nbrodin
import es.unex.sextante.dataObjects.IVectorLayer;
36 21 nbrodin
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
37
import es.unex.sextante.exceptions.RepeatedParameterNameException;
38 39 nbrodin
import es.unex.sextante.outputs.OutputVectorLayer;
39 21 nbrodin
40 45 nbrodin
/**
41
 * Spatial join algorithm
42
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
43
 */
44 60 nbrodin
public class SpatialJoinAlgorithm extends gvGeoAlgorithm {
45 45 nbrodin
        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 39 nbrodin
55 45 nbrodin
        /*
56
         * (non-Javadoc)
57
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
58
         */
59 21 nbrodin
        public void defineCharacteristics(){
60 57 nbrodin
                setName(Sextante.getText("Spatialjoin"));
61 21 nbrodin
                setGroup(Sextante.getText("gvSIG_Algorithms"));
62 39 nbrodin
                setGeneratesUserDefinedRasterOutput(false);
63
64
                try {
65 45 nbrodin
                        m_Parameters.addInputVectorLayer(LAYER1,
66
                                                                                                Sextante.getText("Input_layer"),
67 39 nbrodin
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG,
68
                                                                                                true);
69 45 nbrodin
                        m_Parameters.addInputVectorLayer(LAYER2,
70
                                                                                                Sextante.getText( "Input_layer"),
71 39 nbrodin
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG,
72
                                                                                                true);
73 45 nbrodin
                        m_Parameters.addBoolean(SELECTED_GEOM, Sextante.getText("Selected_geometries"), false);
74
                        m_Parameters.addBoolean(NEAREST, Sextante.getText("use_the_nearest"), false);
75 60 nbrodin
                        m_Parameters.addString(FUNCTION_LIST, Sextante.getText("Function_list"));
76 39 nbrodin
                } catch (RepeatedParameterNameException e) {
77
                        Sextante.addErrorToLog(e);
78
                }
79
                addOutputVectorLayer(RESULT,
80 57 nbrodin
                                                                Sextante.getText("Spatialjoin"),
81 39 nbrodin
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
82 21 nbrodin
        }
83 39 nbrodin
84 45 nbrodin
        /*
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 60 nbrodin
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 63 nbrodin
                String indexName = "GEOMETRY";
109
110 60 nbrodin
                try {
111 63 nbrodin
                        indexName = lyr2.getFeatureStore().getDefaultFeatureType().getDefaultGeometryAttributeName();
112 60 nbrodin
                        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 63 nbrodin
                                SpatiallyIndexedSpatialJoinOperation operation = new SpatiallyIndexedSpatialJoinOperation(lyr2, indexName + "_idx");
139 60 nbrodin
                                operation.setProgressModel(this);
140
                                operation.computesGeometryOperation(lyr1.getFeatureStore(),
141
                                                outFeatStore,
142 63 nbrodin
                                                attrNames,
143 60 nbrodin
                                                selectedGeom,
144
                                                true);
145
                        }
146
                } catch (DataException e) {
147
                        Sextante.addErrorToLog(e);
148
                }
149
150 39 nbrodin
                return true;
151
        }
152 60 nbrodin
153 45 nbrodin
        /**
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 21 nbrodin
}