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 @ 737
History | View | Annotate | Download (9.17 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 modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 2 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.geoprocess.algorithm.base.core; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.Date; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
|
30 |
import com.vividsolutions.jts.geom.Geometry; |
31 |
|
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
import es.unex.sextante.core.Sextante; |
36 |
import es.unex.sextante.dataObjects.IVectorLayer; |
37 |
import es.unex.sextante.exceptions.NullParameterValueException; |
38 |
import es.unex.sextante.exceptions.WrongParameterIDException; |
39 |
import es.unex.sextante.exceptions.WrongParameterTypeException; |
40 |
|
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
43 |
import org.gvsig.fmap.dal.feature.Feature; |
44 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
47 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
48 |
import org.gvsig.geoprocess.algorithm.base.util.JTSFacade; |
49 |
import org.gvsig.geoprocess.algorithm.base.visitor.exception.VisitorException; |
50 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
51 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
52 |
import org.gvsig.tools.dispose.DisposableIterator; |
53 |
|
54 |
/**
|
55 |
* Joins all geometries in a layer
|
56 |
*
|
57 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
58 |
*/
|
59 |
public class ScalableUnionOperation extends GeometryOperation { |
60 |
|
61 |
private static final Logger LOG = LoggerFactory |
62 |
.getLogger(ScalableUnionOperation.class); |
63 |
//geometry type of the layer whose features we are going to fussion
|
64 |
//(polygon features are optimized in jts with buffer(0) trick, the
|
65 |
//nor the rest
|
66 |
private Geometry geometry = null; |
67 |
|
68 |
public ScalableUnionOperation() {
|
69 |
super(null); |
70 |
} |
71 |
|
72 |
public ScalableUnionOperation(AbstractSextanteGeoProcess p) {
|
73 |
super(p);
|
74 |
} |
75 |
|
76 |
/*
|
77 |
* (non-Javadoc)
|
78 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
79 |
*/
|
80 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
|
81 |
if (g == null) { |
82 |
return null; |
83 |
} |
84 |
|
85 |
if (g.getGeometryType().getType() != TYPES.SURFACE) {
|
86 |
return null; |
87 |
} |
88 |
|
89 |
com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g); |
90 |
|
91 |
if (geometry == null) { |
92 |
geometry = actualGeometry; |
93 |
} else {
|
94 |
Geometry[] geoms = new Geometry[2]; |
95 |
geoms[0] = geometry;
|
96 |
geoms[1] = actualGeometry;
|
97 |
|
98 |
geometry = JTSFacade.union(geoms, g.getType()); |
99 |
} |
100 |
return null; |
101 |
} |
102 |
|
103 |
/*
|
104 |
* (non-Javadoc)
|
105 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
106 |
*/
|
107 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
108 |
invoke(g, featureInput); |
109 |
} |
110 |
|
111 |
public void setFeatureStore(FeatureStore out, String[] attrNames) |
112 |
throws DataException {
|
113 |
} |
114 |
|
115 |
/*
|
116 |
* (non-Javadoc)
|
117 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
|
118 |
*/
|
119 |
public Object getResult() { |
120 |
return getGeometry();
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* Returns FMap convex hull geometry.
|
125 |
*
|
126 |
* @return
|
127 |
*/
|
128 |
private org.gvsig.fmap.geom.Geometry getGeometry() {
|
129 |
return GeometryUtil.jtsToGeom(geometry);
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* Joins two geometries
|
134 |
*
|
135 |
* @param g
|
136 |
* @param geometryType
|
137 |
* @return
|
138 |
*/
|
139 |
private void union(org.gvsig.fmap.geom.Geometry g, int geometryType) { |
140 |
Date time1 = new Date(); |
141 |
if (g == null) { |
142 |
return;
|
143 |
} |
144 |
|
145 |
if (g.getGeometryType().getType() != TYPES.SURFACE) {
|
146 |
return;
|
147 |
} |
148 |
|
149 |
com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g); |
150 |
|
151 |
if (geometry == null) { |
152 |
geometry = actualGeometry; |
153 |
} else {
|
154 |
Geometry[] geoms = new Geometry[2]; |
155 |
geoms[0] = geometry;
|
156 |
geoms[1] = actualGeometry;
|
157 |
|
158 |
geometry = JTSFacade.union(geoms, geometryType); |
159 |
} |
160 |
Date time2 = new Date(); |
161 |
LOG.info("scalableUnion time = " + (time2.getTime() - time1.getTime() + " ms")); |
162 |
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Joins an array of surface geometries
|
167 |
*
|
168 |
* @param geoms
|
169 |
* @param geometryType
|
170 |
* @return
|
171 |
*/
|
172 |
private void union(org.gvsig.fmap.geom.Geometry[] geoms) { |
173 |
Date time1 = new Date(); |
174 |
if (geoms == null) { |
175 |
return;
|
176 |
} |
177 |
|
178 |
ArrayList<Geometry> geometries = new ArrayList<Geometry>(); |
179 |
|
180 |
if (geometry != null) { |
181 |
geometries.add(geometry); |
182 |
} |
183 |
for (int i = 0; i < geoms.length; i++) { |
184 |
if (geoms[i].getGeometryType().getType() == TYPES.SURFACE) {
|
185 |
geometries.add(GeometryUtil.geomToJTS(geoms[i])); |
186 |
} |
187 |
} |
188 |
geometry = JTSFacade.union((Geometry[]) geometries.toArray(new Geometry[0]), TYPES.SURFACE); |
189 |
|
190 |
Date time2 = new Date(); |
191 |
LOG.info("scalableUnion time = " + (time2.getTime() - time1.getTime() + " ms")); |
192 |
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* Computes union of all geometries of the clipping layer. The input
|
197 |
* resources won't be released.
|
198 |
*
|
199 |
* @return one geometry
|
200 |
* @throws NullParameterValueException
|
201 |
* @throws WrongParameterIDException
|
202 |
* @throws WrongParameterTypeException
|
203 |
* @throws com.iver.cit.gvsig.fmap.DriverException
|
204 |
* @throws ReadDriverException
|
205 |
* @throws VisitorException
|
206 |
* @throws ExpansionFileReadException
|
207 |
*/
|
208 |
public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
209 |
FeatureStore store = null;
|
210 |
|
211 |
if (input instanceof FlyrVectIVectorLayer) { |
212 |
store = ((FlyrVectIVectorLayer) input).getFeatureStore(); |
213 |
} else {
|
214 |
return null; |
215 |
} |
216 |
|
217 |
return joinLayerGeometries(store, selectedGeom);
|
218 |
} |
219 |
|
220 |
/**
|
221 |
* Computes union of all geometries of the clipping layer. The input
|
222 |
* resources won't be released.
|
223 |
*
|
224 |
* @return one geometry
|
225 |
* @throws NullParameterValueException
|
226 |
* @throws WrongParameterIDException
|
227 |
* @throws WrongParameterTypeException
|
228 |
* @throws com.iver.cit.gvsig.fmap.DriverException
|
229 |
* @throws ReadDriverException
|
230 |
* @throws VisitorException
|
231 |
* @throws ExpansionFileReadException
|
232 |
*/
|
233 |
@SuppressWarnings({"unchecked", "deprecation"}) |
234 |
public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(FeatureStore store, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
235 |
ScalableUnionOperation operation = new ScalableUnionOperation();
|
236 |
|
237 |
FeatureSet features = null;
|
238 |
try {
|
239 |
if (selectedGeom) {
|
240 |
features = (FeatureSet) store.getSelection(); |
241 |
} else {
|
242 |
features = store.getFeatureSet(); |
243 |
} |
244 |
DisposableIterator it = features.iterator(); |
245 |
int i = 0; |
246 |
List geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
247 |
List types = new ArrayList(); |
248 |
while (it.hasNext()) {
|
249 |
i++; |
250 |
Feature feature = (Feature) it.next(); |
251 |
List geomList = feature.getGeometries();
|
252 |
if (geomList == null) { |
253 |
org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry(); |
254 |
if (geom != null) { |
255 |
geometries.add(geom); |
256 |
types.add(geom.getType()); |
257 |
} |
258 |
continue;
|
259 |
} |
260 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
261 |
while (itGeom.hasNext()) {
|
262 |
org.gvsig.fmap.geom.Geometry g = itGeom.next(); |
263 |
geometries.add(g); |
264 |
types.add(g.getType()); |
265 |
} |
266 |
} |
267 |
operation.union((org.gvsig.fmap.geom.Geometry[]) geometries.toArray(new org.gvsig.fmap.geom.Geometry[0])); |
268 |
return operation.getGeometry();
|
269 |
} catch (DataException e) {
|
270 |
Sextante.addErrorToLog(e); |
271 |
} |
272 |
return null; |
273 |
} |
274 |
} |