Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.fusespatially / src / main / java / org / gvsig / geoprocess / algorithm / fusespatially / FuseSpatiallyOperation.java @ 347

History | View | Annotate | Download (6.79 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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25

26
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2010 Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 */
44

    
45
package org.gvsig.geoprocess.algorithm.fusespatially;
46

    
47
import java.util.ArrayList;
48
import java.util.List;
49
import java.util.Stack;
50

    
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.feature.EditableFeature;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.geom.GeometryLocator;
57
import org.gvsig.fmap.geom.GeometryManager;
58
import org.gvsig.fmap.geom.exception.CreateGeometryException;
59
import org.gvsig.geoprocess.algorithm.base.core.DALFeaturePersister;
60
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
61
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
62
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
63

    
64
import com.vividsolutions.jts.geom.Geometry;
65
/**
66
 * Fuse spatially operation
67
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
68
 * @deprecated Uses FuseSpatiallyOperationFast
69
 */
70
public class FuseSpatiallyOperation extends GeometryOperation {
71
        protected GeometryManager                geomManager           = GeometryLocator.getGeometryManager();
72
        private int                              id                    = 0;
73
        private List<Feature>                    featList              = null;
74
        private FeatureStore                     outFeatureStoreTable  = null;
75
        private String                           idField               = "FID";
76
        
77
        public FuseSpatiallyOperation(List<Feature> f, 
78
                        FeatureStore outFeatStoreTable, 
79
                        String[] fieldNames, 
80
                        String idField, 
81
                        AbstractSextanteGeoProcess p) throws DataException {
82
                super(p);
83
                this.featList = f;
84
                this.outFeatureStoreTable = outFeatStoreTable;
85
                this.idField = idField;
86
        }
87
        
88
        public FuseSpatiallyOperation(Stack<Feature> f, 
89
                        FeatureStore outFeatStoreTable, 
90
                        String[] fieldNames, 
91
                        AbstractSextanteGeoProcess p) throws DataException {
92
                super(p);
93
                this.featList = f;
94
                this.outFeatureStoreTable = outFeatStoreTable;
95
        }
96
        
97
        /*
98
         * (non-Javadoc)
99
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
100
         */
101
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
102
                try {
103
                        Geometry jtsInputGeom = GeometryUtil.geomToJTS(g);
104
                        insertInTable(feature);
105
                        
106
                        int cont = featList.size() - 1;
107
                        while(cont >= 0 && !process.getTaskMonitor().isCanceled()) {
108
                                Feature f = featList.get(cont);
109
                                g = f.getDefaultGeometry();
110
                                Geometry jtsGeom = GeometryUtil.geomToJTS(g); //Multigeometry support
111
                                if(jtsGeom.intersects(jtsInputGeom)) {
112
                                        jtsInputGeom = jtsInputGeom.union(jtsGeom);
113
                                        featList.remove(cont);
114
                                        process.setProgress(procesSize - featList.size(), procesSize);
115
                                        cont = featList.size();
116
                                        insertInTable(f);
117
                                }
118
                                cont --;
119
                        }
120

    
121
                        //Cuando no genera dos capas mete los campos de la feature de entrada en la de salida perdiendose el resto
122
                        if(outFeatureStoreTable == null) { 
123
                                String[] fieldNames = persister.getFieldNamesWithoutGeom();
124
                                ArrayList<String> fields = new ArrayList<String>();
125
                                ArrayList<Object> values = new ArrayList<Object>();
126
                                fields.add(idField);
127
                                values.add(id);
128
                                for (int j = 0; j < fieldNames.length; j++) {
129
                                        Object obj = feature.get(fieldNames[j]);
130
                                        if(obj != null && fieldNames[j].compareTo(idField) != 0) {
131
                                                fields.add(fieldNames[j]);
132
                                                values.add(obj);
133
                                        }
134
                                }
135
                                lastEditFeature = persister.addFeature(jtsInputGeom, fields, values);
136
                        } else
137
                                lastEditFeature = persister.addFeature(jtsInputGeom, idField, id);
138
                } catch (DataException e) {
139
                        return null;
140
                } catch (CreateGeometryException e) {
141
                        return null;
142
                }
143
                id++;
144
                return lastEditFeature;
145
        }
146
        
147
        private void insertInTable(Feature f) throws DataException {
148
                if(outFeatureStoreTable == null)
149
                        return;
150
                EditableFeature edFeat = outFeatureStoreTable.createNewFeature();
151
                edFeat.set(idField, id);
152
                FeatureAttributeDescriptor[] attrList = f.getType().getAttributeDescriptors();
153
                for (int j = 0; j < attrList.length; j++) {
154
                        if(attrList[j].getName().compareTo("GEOMETRY") != 0) {
155
                                edFeat.set(attrList[j].getName(), f.get(attrList[j].getName()));
156
                        }
157
                }
158
                outFeatureStoreTable.insert(edFeat);
159
        }
160
        
161
        /**
162
         * Removes duplicate fields
163
         * @param names
164
         * @return
165
         */
166
        public static String[] checkFields(String[] names) {
167
                if(names.length <= 1)
168
                        return names;
169
                int cont = 0;
170

    
171
                int i = 1;
172
                while(i < names.length) {
173
                        if(names[0].compareTo(names[i]) == 0) {
174
                                names[0] = "FID_" + cont;
175
                                i = 0;
176
                                cont ++;
177
                        }
178
                        i ++;
179
                }
180
                return names;
181
        }
182
        
183
        /*
184
         * (non-Javadoc)
185
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
186
         */
187
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
188
                if(g == null)
189
                        return;
190
        }
191
        
192
        /*
193
         * (non-Javadoc)
194
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
195
         */
196
        public Object getResult() {
197
                return lastEditFeature;
198
        }
199
        
200
        public DALFeaturePersister getWriter() {
201
                return persister;
202
        }
203
        
204
}
205