Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.base / src / main / java / org / gvsig / geoprocess / algorithm / base / core / ScalableUnionOperation.java @ 245

History | View | Annotate | Download (6.4 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
package org.gvsig.geoprocess.algorithm.base.core;
25

    
26
import java.util.Iterator;
27
import java.util.List;
28

    
29
import com.vividsolutions.jts.geom.Geometry;
30

    
31
import es.unex.sextante.core.Sextante;
32
import es.unex.sextante.dataObjects.IVectorLayer;
33
import es.unex.sextante.exceptions.NullParameterValueException;
34
import es.unex.sextante.exceptions.WrongParameterIDException;
35
import es.unex.sextante.exceptions.WrongParameterTypeException;
36

    
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.EditableFeature;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.geom.Geometry.TYPES;
43
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
44
import org.gvsig.geoprocess.algorithm.base.util.JTSFacade;
45
import org.gvsig.geoprocess.algorithm.base.visitor.exception.VisitorException;
46
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
47
import org.gvsig.tools.dispose.DisposableIterator;
48

    
49
/**
50
 * Joins all geometries in a layer
51
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52
 */
53
public class ScalableUnionOperation extends GeometryOperation {
54
        //geometry type of the layer whose features we are going to fussion
55
        //(polygon features are optimized in jts with buffer(0) trick, the
56
        //nor the rest
57
        private Geometry            geometry         = null;
58
        
59
        /*
60
         * (non-Javadoc)
61
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
62
         */
63
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
64
                if(g == null)
65
                        return null;
66
                
67
                if(g.getGeometryType().getType() != TYPES.SURFACE)
68
                        return null;
69
                
70
                com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g);
71
                
72
                if(geometry == null){
73
                        geometry = actualGeometry;
74
                }else{
75
                        Geometry[] geoms = new Geometry[2];
76
                        geoms[0] = geometry;
77
                        geoms[1] = actualGeometry;
78
                        
79
                        geometry = JTSFacade.union(geoms, g.getType());
80
                }
81
                return null;
82
        }
83

    
84
        /*
85
         * (non-Javadoc)
86
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
87
         */
88
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) {
89
                invoke(g, featureInput);
90
        }
91

    
92
        public void setFeatureStore(FeatureStore out, String[] attrNames)
93
                        throws DataException {
94
        }
95
        
96
        /*
97
         * (non-Javadoc)
98
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
99
         */
100
        public Object getResult() {
101
                return getGeometry();
102
        }
103
        
104
        /**
105
         * Returns FMap convex hull geometry.
106
         * @return
107
         */
108
        private org.gvsig.fmap.geom.Geometry getGeometry() {
109
                return GeometryUtil.jtsToGeom(geometry);
110
        }
111
        
112
        /**
113
         * Joins two geometries
114
         * @param g
115
         * @param geometryType
116
         * @return
117
         */
118
        private void union(org.gvsig.fmap.geom.Geometry g, int geometryType) {
119
                if(g == null)
120
                        return;
121
                
122
                if(g.getGeometryType().getType() != TYPES.SURFACE)
123
                        return;
124
                
125
                com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g);
126
                
127
                if(geometry == null){
128
                        geometry = actualGeometry;
129
                }else{
130
                        Geometry[] geoms = new Geometry[2];
131
                        geoms[0] = geometry;
132
                        geoms[1] = actualGeometry;
133
                        
134
                        geometry = JTSFacade.union(geoms, geometryType);
135
                }
136
        }
137
        
138
        /**
139
         * Computes union of all geometries of the clipping layer. The input resources won't 
140
         * be released.
141
         *
142
         * @return one geometry
143
         * @throws NullParameterValueException 
144
         * @throws WrongParameterIDException 
145
         * @throws WrongParameterTypeException 
146
         * @throws com.iver.cit.gvsig.fmap.DriverException
147
         * @throws ReadDriverException
148
         * @throws VisitorException
149
         * @throws ExpansionFileReadException
150
         */
151
        public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
152
                FeatureStore store = null;
153
                
154
                if(input instanceof FlyrVectIVectorLayer)
155
                        store = ((FlyrVectIVectorLayer)input).getFeatureStore();
156
                else
157
                        return null;
158
                
159
                return joinLayerGeometries(store);
160
        }
161
        
162
        /**
163
         * Computes union of all geometries of the clipping layer. The input resources won't 
164
         * be released.
165
         *
166
         * @return one geometry
167
         * @throws NullParameterValueException 
168
         * @throws WrongParameterIDException 
169
         * @throws WrongParameterTypeException 
170
         * @throws com.iver.cit.gvsig.fmap.DriverException
171
         * @throws ReadDriverException
172
         * @throws VisitorException
173
         * @throws ExpansionFileReadException
174
         */
175
        @SuppressWarnings("unchecked")
176
        public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(FeatureStore store) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
177
                ScalableUnionOperation operation = new ScalableUnionOperation();
178
                
179
                FeatureSet features = null;
180
                try {
181
                        features = store.getFeatureSet();
182
                        DisposableIterator it = features.iterator();
183
                        while( it.hasNext() ) {
184
                                Feature feature = (Feature)it.next();
185
                                List geomList = feature.getGeometries();
186
                                if(geomList == null) {
187
                                        org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
188
                                        if(geom != null)
189
                                                operation.union(geom, geom.getType());
190
                                        continue;
191
                                }
192
                                Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
193
                                while(itGeom.hasNext()) {
194
                                        org.gvsig.fmap.geom.Geometry g = itGeom.next();
195
                                        operation.union(g, g.getType());
196
                                }
197
                        }
198
                        return operation.getGeometry();
199
                } catch (DataException e) {
200
                        Sextante.addErrorToLog(e);
201
                }
202
                return null;
203
        }
204
}
205