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 @ 335
History | View | Annotate | Download (6.77 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.AbstractSextanteGeoProcess; |
47 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
48 |
import org.gvsig.tools.dispose.DisposableIterator; |
49 |
|
50 |
/**
|
51 |
* Joins all geometries in a layer
|
52 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
53 |
*/
|
54 |
public class ScalableUnionOperation extends GeometryOperation { |
55 |
//geometry type of the layer whose features we are going to fussion
|
56 |
//(polygon features are optimized in jts with buffer(0) trick, the
|
57 |
//nor the rest
|
58 |
private Geometry geometry = null; |
59 |
|
60 |
public ScalableUnionOperation() {
|
61 |
super(null); |
62 |
} |
63 |
|
64 |
public ScalableUnionOperation(AbstractSextanteGeoProcess p) {
|
65 |
super(p);
|
66 |
} |
67 |
|
68 |
/*
|
69 |
* (non-Javadoc)
|
70 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
71 |
*/
|
72 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
|
73 |
if(g == null) |
74 |
return null; |
75 |
|
76 |
if(g.getGeometryType().getType() != TYPES.SURFACE)
|
77 |
return null; |
78 |
|
79 |
com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g); |
80 |
|
81 |
if(geometry == null){ |
82 |
geometry = actualGeometry; |
83 |
}else{
|
84 |
Geometry[] geoms = new Geometry[2]; |
85 |
geoms[0] = geometry;
|
86 |
geoms[1] = actualGeometry;
|
87 |
|
88 |
geometry = JTSFacade.union(geoms, g.getType()); |
89 |
} |
90 |
return null; |
91 |
} |
92 |
|
93 |
/*
|
94 |
* (non-Javadoc)
|
95 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
96 |
*/
|
97 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
98 |
invoke(g, featureInput); |
99 |
} |
100 |
|
101 |
public void setFeatureStore(FeatureStore out, String[] attrNames) |
102 |
throws DataException {
|
103 |
} |
104 |
|
105 |
/*
|
106 |
* (non-Javadoc)
|
107 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
|
108 |
*/
|
109 |
public Object getResult() { |
110 |
return getGeometry();
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* Returns FMap convex hull geometry.
|
115 |
* @return
|
116 |
*/
|
117 |
private org.gvsig.fmap.geom.Geometry getGeometry() {
|
118 |
return GeometryUtil.jtsToGeom(geometry);
|
119 |
} |
120 |
|
121 |
/**
|
122 |
* Joins two geometries
|
123 |
* @param g
|
124 |
* @param geometryType
|
125 |
* @return
|
126 |
*/
|
127 |
private void union(org.gvsig.fmap.geom.Geometry g, int geometryType) { |
128 |
if(g == null) |
129 |
return;
|
130 |
|
131 |
if(g.getGeometryType().getType() != TYPES.SURFACE)
|
132 |
return;
|
133 |
|
134 |
com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g); |
135 |
|
136 |
if(geometry == null){ |
137 |
geometry = actualGeometry; |
138 |
}else{
|
139 |
Geometry[] geoms = new Geometry[2]; |
140 |
geoms[0] = geometry;
|
141 |
geoms[1] = actualGeometry;
|
142 |
|
143 |
geometry = JTSFacade.union(geoms, geometryType); |
144 |
} |
145 |
} |
146 |
|
147 |
/**
|
148 |
* Computes union of all geometries of the clipping layer. The input resources won't
|
149 |
* be released.
|
150 |
*
|
151 |
* @return one geometry
|
152 |
* @throws NullParameterValueException
|
153 |
* @throws WrongParameterIDException
|
154 |
* @throws WrongParameterTypeException
|
155 |
* @throws com.iver.cit.gvsig.fmap.DriverException
|
156 |
* @throws ReadDriverException
|
157 |
* @throws VisitorException
|
158 |
* @throws ExpansionFileReadException
|
159 |
*/
|
160 |
public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
161 |
FeatureStore store = null;
|
162 |
|
163 |
if(input instanceof FlyrVectIVectorLayer) |
164 |
store = ((FlyrVectIVectorLayer)input).getFeatureStore(); |
165 |
else
|
166 |
return null; |
167 |
|
168 |
return joinLayerGeometries(store, selectedGeom);
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* Computes union of all geometries of the clipping layer. The input resources won't
|
173 |
* be released.
|
174 |
*
|
175 |
* @return one geometry
|
176 |
* @throws NullParameterValueException
|
177 |
* @throws WrongParameterIDException
|
178 |
* @throws WrongParameterTypeException
|
179 |
* @throws com.iver.cit.gvsig.fmap.DriverException
|
180 |
* @throws ReadDriverException
|
181 |
* @throws VisitorException
|
182 |
* @throws ExpansionFileReadException
|
183 |
*/
|
184 |
@SuppressWarnings({ "unchecked", "deprecation" }) |
185 |
public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(FeatureStore store, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
186 |
ScalableUnionOperation operation = new ScalableUnionOperation();
|
187 |
|
188 |
FeatureSet features = null;
|
189 |
try {
|
190 |
if(selectedGeom) {
|
191 |
features = (FeatureSet)store.getSelection(); |
192 |
} else {
|
193 |
features = store.getFeatureSet(); |
194 |
} |
195 |
DisposableIterator it = features.iterator(); |
196 |
while( it.hasNext() ) {
|
197 |
Feature feature = (Feature)it.next(); |
198 |
List geomList = feature.getGeometries();
|
199 |
if(geomList == null) { |
200 |
org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry(); |
201 |
if(geom != null) |
202 |
operation.union(geom, geom.getType()); |
203 |
continue;
|
204 |
} |
205 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
206 |
while(itGeom.hasNext()) {
|
207 |
org.gvsig.fmap.geom.Geometry g = itGeom.next(); |
208 |
operation.union(g, g.getType()); |
209 |
} |
210 |
} |
211 |
return operation.getGeometry();
|
212 |
} catch (DataException e) {
|
213 |
Sextante.addErrorToLog(e); |
214 |
} |
215 |
return null; |
216 |
} |
217 |
} |
218 |
|