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

History | View | Annotate | Download (6.78 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
                        for (int i = featList.size() - 1; i >= 0; i--) {
96
                                Feature f = featList.get(i);
97
                                g = f.getDefaultGeometry();
98
                                Geometry jtsGeom = GeometryUtil.geomToJTS(g); //Multigeometry support
99
                                if(jtsGeom.intersects(jtsInputGeom)) {
100
                                        jtsInputGeom = jtsInputGeom.union(jtsGeom);
101
                                        featList.remove(i);
102
                                        insertInTable(f);
103
                                }
104
                        }
105
                        
106
                        /*int i = featList.size() - 1;
107
                        while(i >= 0) {
108
                                Feature f = featList.get(i);
109
                                geom = f.getDefaultGeometry();
110
                                Geometry jtsGeom = GeometryUtil.geomToJTS(geom); //Multigeometry support
111
                                if(jtsInputGeom.intersects(jtsGeom)) {
112
                                        jtsInputGeom = jtsInputGeom.union(jtsGeom);
113
                                        featList.remove(i);
114
                                        insertInTable(f);
115
                                        i = featList.size();
116
                                }
117
                                i --;
118
                        }*/
119

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

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