Revision 658 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
ScalableUnionOperation.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.geoprocess.algorithm.base.core; |
25 | 25 |
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.Date; |
|
26 | 28 |
import java.util.Iterator; |
27 | 29 |
import java.util.List; |
28 | 30 |
|
29 | 31 |
import com.vividsolutions.jts.geom.Geometry; |
30 | 32 |
|
33 |
import org.slf4j.Logger; |
|
34 |
import org.slf4j.LoggerFactory; |
|
35 |
|
|
31 | 36 |
import es.unex.sextante.core.Sextante; |
32 | 37 |
import es.unex.sextante.dataObjects.IVectorLayer; |
33 | 38 |
import es.unex.sextante.exceptions.NullParameterValueException; |
... | ... | |
52 | 57 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a> |
53 | 58 |
*/ |
54 | 59 |
public class ScalableUnionOperation extends GeometryOperation { |
60 |
private static final Logger LOG = LoggerFactory |
|
61 |
.getLogger(ScalableUnionOperation.class); |
|
55 | 62 |
//geometry type of the layer whose features we are going to fussion |
56 | 63 |
//(polygon features are optimized in jts with buffer(0) trick, the |
57 | 64 |
//nor the rest |
58 | 65 |
private Geometry geometry = null; |
59 |
|
|
66 |
|
|
60 | 67 |
public ScalableUnionOperation() { |
61 | 68 |
super(null); |
62 | 69 |
} |
63 |
|
|
70 |
|
|
64 | 71 |
public ScalableUnionOperation(AbstractSextanteGeoProcess p) { |
65 | 72 |
super(p); |
66 | 73 |
} |
67 |
|
|
74 |
|
|
68 | 75 |
/* |
69 | 76 |
* (non-Javadoc) |
70 | 77 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature) |
... | ... | |
72 | 79 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) { |
73 | 80 |
if(g == null) |
74 | 81 |
return null; |
75 |
|
|
82 |
|
|
76 | 83 |
if(g.getGeometryType().getType() != TYPES.SURFACE) |
77 | 84 |
return null; |
78 |
|
|
85 |
|
|
79 | 86 |
com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g); |
80 |
|
|
87 |
|
|
81 | 88 |
if(geometry == null){ |
82 | 89 |
geometry = actualGeometry; |
83 | 90 |
}else{ |
84 | 91 |
Geometry[] geoms = new Geometry[2]; |
85 | 92 |
geoms[0] = geometry; |
86 | 93 |
geoms[1] = actualGeometry; |
87 |
|
|
94 |
|
|
88 | 95 |
geometry = JTSFacade.union(geoms, g.getType()); |
89 | 96 |
} |
90 | 97 |
return null; |
... | ... | |
101 | 108 |
public void setFeatureStore(FeatureStore out, String[] attrNames) |
102 | 109 |
throws DataException { |
103 | 110 |
} |
104 |
|
|
111 |
|
|
105 | 112 |
/* |
106 | 113 |
* (non-Javadoc) |
107 | 114 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult() |
... | ... | |
109 | 116 |
public Object getResult() { |
110 | 117 |
return getGeometry(); |
111 | 118 |
} |
112 |
|
|
119 |
|
|
113 | 120 |
/** |
114 | 121 |
* Returns FMap convex hull geometry. |
115 | 122 |
* @return |
... | ... | |
117 | 124 |
private org.gvsig.fmap.geom.Geometry getGeometry() { |
118 | 125 |
return GeometryUtil.jtsToGeom(geometry); |
119 | 126 |
} |
120 |
|
|
127 |
|
|
121 | 128 |
/** |
122 | 129 |
* Joins two geometries |
123 | 130 |
* @param g |
... | ... | |
125 | 132 |
* @return |
126 | 133 |
*/ |
127 | 134 |
private void union(org.gvsig.fmap.geom.Geometry g, int geometryType) { |
135 |
Date time1 = new Date(); |
|
128 | 136 |
if(g == null) |
129 | 137 |
return; |
130 |
|
|
138 |
|
|
131 | 139 |
if(g.getGeometryType().getType() != TYPES.SURFACE) |
132 | 140 |
return; |
133 |
|
|
141 |
|
|
134 | 142 |
com.vividsolutions.jts.geom.Geometry actualGeometry = GeometryUtil.geomToJTS(g); |
135 |
|
|
143 |
|
|
136 | 144 |
if(geometry == null){ |
137 | 145 |
geometry = actualGeometry; |
138 | 146 |
}else{ |
139 | 147 |
Geometry[] geoms = new Geometry[2]; |
140 | 148 |
geoms[0] = geometry; |
141 | 149 |
geoms[1] = actualGeometry; |
142 |
|
|
150 |
|
|
143 | 151 |
geometry = JTSFacade.union(geoms, geometryType); |
144 | 152 |
} |
153 |
Date time2 = new Date(); |
|
154 |
LOG.info("scalableUnion time = "+(time2.getTime()-time1.getTime()+" ms")); |
|
155 |
|
|
145 | 156 |
} |
146 |
|
|
157 |
|
|
158 |
/** |
|
159 |
* Joins an array of surface geometries |
|
160 |
* @param geoms |
|
161 |
* @param geometryType |
|
162 |
* @return |
|
163 |
*/ |
|
164 |
private void union(org.gvsig.fmap.geom.Geometry[] geoms) { |
|
165 |
Date time1 = new Date(); |
|
166 |
if(geoms == null) |
|
167 |
return; |
|
168 |
|
|
169 |
ArrayList<Geometry> geometries = new ArrayList<Geometry>(); |
|
170 |
|
|
171 |
if(geometry != null){ |
|
172 |
geometries.add(geometry); |
|
173 |
} |
|
174 |
for (int i = 0; i < geoms.length; i++) { |
|
175 |
if(geoms[i].getGeometryType().getType() == TYPES.SURFACE) { |
|
176 |
geometries.add(GeometryUtil.geomToJTS(geoms[i])); |
|
177 |
} |
|
178 |
} |
|
179 |
geometry = JTSFacade.union((Geometry[])geometries.toArray(new Geometry[0]), TYPES.SURFACE); |
|
180 |
|
|
181 |
Date time2 = new Date(); |
|
182 |
LOG.info("scalableUnion time = "+(time2.getTime()-time1.getTime()+" ms")); |
|
183 |
|
|
184 |
} |
|
185 |
|
|
147 | 186 |
/** |
148 |
* Computes union of all geometries of the clipping layer. The input resources won't
|
|
187 |
* Computes union of all geometries of the clipping layer. The input resources won't |
|
149 | 188 |
* be released. |
150 | 189 |
* |
151 | 190 |
* @return one geometry |
152 |
* @throws NullParameterValueException
|
|
153 |
* @throws WrongParameterIDException
|
|
154 |
* @throws WrongParameterTypeException
|
|
191 |
* @throws NullParameterValueException |
|
192 |
* @throws WrongParameterIDException |
|
193 |
* @throws WrongParameterTypeException |
|
155 | 194 |
* @throws com.iver.cit.gvsig.fmap.DriverException |
156 | 195 |
* @throws ReadDriverException |
157 | 196 |
* @throws VisitorException |
... | ... | |
159 | 198 |
*/ |
160 | 199 |
public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
161 | 200 |
FeatureStore store = null; |
162 |
|
|
201 |
|
|
163 | 202 |
if(input instanceof FlyrVectIVectorLayer) |
164 | 203 |
store = ((FlyrVectIVectorLayer)input).getFeatureStore(); |
165 | 204 |
else |
166 | 205 |
return null; |
167 |
|
|
206 |
|
|
168 | 207 |
return joinLayerGeometries(store, selectedGeom); |
169 | 208 |
} |
170 |
|
|
209 |
|
|
171 | 210 |
/** |
172 |
* Computes union of all geometries of the clipping layer. The input resources won't
|
|
211 |
* Computes union of all geometries of the clipping layer. The input resources won't |
|
173 | 212 |
* be released. |
174 | 213 |
* |
175 | 214 |
* @return one geometry |
176 |
* @throws NullParameterValueException
|
|
177 |
* @throws WrongParameterIDException
|
|
178 |
* @throws WrongParameterTypeException
|
|
215 |
* @throws NullParameterValueException |
|
216 |
* @throws WrongParameterIDException |
|
217 |
* @throws WrongParameterTypeException |
|
179 | 218 |
* @throws com.iver.cit.gvsig.fmap.DriverException |
180 | 219 |
* @throws ReadDriverException |
181 | 220 |
* @throws VisitorException |
... | ... | |
184 | 223 |
@SuppressWarnings({ "unchecked", "deprecation" }) |
185 | 224 |
public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(FeatureStore store, boolean selectedGeom) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
186 | 225 |
ScalableUnionOperation operation = new ScalableUnionOperation(); |
187 |
|
|
226 |
|
|
188 | 227 |
FeatureSet features = null; |
189 | 228 |
try { |
190 | 229 |
if(selectedGeom) { |
... | ... | |
193 | 232 |
features = store.getFeatureSet(); |
194 | 233 |
} |
195 | 234 |
DisposableIterator it = features.iterator(); |
235 |
int i=0; |
|
236 |
List geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
|
237 |
List types = new ArrayList(); |
|
196 | 238 |
while( it.hasNext() ) { |
239 |
i++; |
|
197 | 240 |
Feature feature = (Feature)it.next(); |
198 | 241 |
List geomList = feature.getGeometries(); |
199 | 242 |
if(geomList == null) { |
200 | 243 |
org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry(); |
201 |
if(geom != null) |
|
202 |
operation.union(geom, geom.getType()); |
|
244 |
if(geom != null){ |
|
245 |
geometries.add(geom); |
|
246 |
types.add(geom.getType()); |
|
247 |
} |
|
203 | 248 |
continue; |
204 | 249 |
} |
205 | 250 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator(); |
206 | 251 |
while(itGeom.hasNext()) { |
207 | 252 |
org.gvsig.fmap.geom.Geometry g = itGeom.next(); |
208 |
operation.union(g, g.getType()); |
|
253 |
geometries.add(g); |
|
254 |
types.add(g.getType()); |
|
209 | 255 |
} |
210 | 256 |
} |
257 |
operation.union((org.gvsig.fmap.geom.Geometry[])geometries.toArray(new org.gvsig.fmap.geom.Geometry[0])); |
|
211 | 258 |
return operation.getGeometry(); |
212 | 259 |
} catch (DataException e) { |
213 | 260 |
Sextante.addErrorToLog(e); |
Also available in: Unified diff