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

History | View | Annotate | Download (6.56 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

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

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

    
160
                int i = 1;
161
                while(i < names.length) {
162
                        if(names[0].compareTo(names[i]) == 0) {
163
                                names[0] = "FID_" + cont;
164
                                i = 0;
165
                                cont ++;
166
                        }
167
                        i ++;
168
                }
169
                return names;
170
        }
171
        
172
        /*
173
         * (non-Javadoc)
174
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
175
         */
176
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
177
                if(g == null)
178
                        return;
179
        }
180
        
181
        /*
182
         * (non-Javadoc)
183
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
184
         */
185
        public Object getResult() {
186
                return lastEditFeature;
187
        }
188
        
189
        public DALFeaturePersister getWriter() {
190
                return persister;
191
        }
192
        
193
}
194