Revision 739 org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.dissolve/src/main/java/org/gvsig/geoprocess/algorithm/dissolve/DissolveOperationFast.java
DissolveOperationFast.java | ||
---|---|---|
3 | 3 |
* |
4 | 4 |
* Copyright (C) 2007-2012 gvSIG Association. |
5 | 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.
|
|
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 | 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.
|
|
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 | 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. |
|
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. |
|
20 | 19 |
* |
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
23 | 22 |
*/ |
24 | 23 |
/* |
25 | 24 |
|
... | ... | |
41 | 40 |
* along with this program; if not, write to the Free Software |
42 | 41 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
43 | 42 |
*/ |
44 |
|
|
45 | 43 |
package org.gvsig.geoprocess.algorithm.dissolve; |
46 | 44 |
|
47 | 45 |
import java.util.ArrayList; |
... | ... | |
63 | 61 |
import org.slf4j.LoggerFactory; |
64 | 62 |
|
65 | 63 |
import com.vividsolutions.jts.geom.Geometry; |
64 |
|
|
66 | 65 |
/** |
67 | 66 |
* Dissolve operation |
67 |
* |
|
68 | 68 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a> |
69 | 69 |
*/ |
70 | 70 |
public class DissolveOperationFast extends GeometryOperation { |
71 |
private static Logger logger = LoggerFactory.getLogger(DissolveOperationFast.class.getName()); |
|
72 |
private EditableFeature lastEditFeature = null; |
|
73 |
private ArrayList<Element> featureList = null; |
|
74 |
private IDissolveRule rule = null; |
|
75 |
private Summary summary = null; |
|
76 |
|
|
77 |
class Element { |
|
78 |
public int id = -1; |
|
79 |
public Feature feat = null; |
|
80 |
public List<Element> overlapList = new ArrayList<Element>(); |
|
81 |
public boolean insertedToJoin = false; |
|
82 |
public Geometry jtsGeom = null; |
|
83 |
} |
|
84 |
|
|
85 |
class NodeTree { |
|
86 |
public Element element = null; |
|
87 |
public int pos = 0; |
|
88 |
public NodeTree parent = null; |
|
89 |
|
|
90 |
public NodeTree(Element node, NodeTree parent) { |
|
91 |
this.element = node; |
|
92 |
this.parent = parent; |
|
93 |
} |
|
94 |
|
|
95 |
public Element getNext() { |
|
96 |
if(pos < element.overlapList.size()) |
|
97 |
return element.overlapList.get(pos++); |
|
98 |
return null; |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
public DissolveOperationFast(IDissolveRule rule, AbstractSextanteGeoProcess p) { |
|
103 |
super(p); |
|
104 |
this.rule = rule; |
|
105 |
featureList = new ArrayList<Element>(); |
|
106 |
} |
|
107 | 71 |
|
108 |
/* |
|
109 |
* (non-Javadoc) |
|
110 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature) |
|
111 |
*/ |
|
112 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) { |
|
113 |
return null; |
|
114 |
} |
|
115 |
|
|
116 |
/* |
|
117 |
* (non-Javadoc) |
|
118 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature) |
|
119 |
*/ |
|
120 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
|
121 |
if(g == null) |
|
122 |
return; |
|
123 |
} |
|
124 |
|
|
125 |
/* |
|
126 |
* (non-Javadoc) |
|
127 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult() |
|
128 |
*/ |
|
129 |
public Object getResult() { |
|
130 |
return lastEditFeature; |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* Computes a complete operation over the input FeatureStore. The result of this operation |
|
135 |
* is stored in the output FeatureStore. |
|
136 |
* @param inFeatStore |
|
137 |
* Input FeatureStore |
|
138 |
* @param outFeatStore |
|
139 |
* Output FeatureStore |
|
140 |
* @param attrNames |
|
141 |
* List of attributes to build the output feature store |
|
142 |
* @param selectedGeom |
|
143 |
* If it is true only the selected geometries will be processed |
|
144 |
* @throws DataException |
|
145 |
*/ |
|
146 |
@SuppressWarnings({ "deprecation" }) |
|
147 |
public void computesGeometryOperation(FeatureStore inFeatStore, |
|
148 |
FeatureStore outFeatStore, |
|
149 |
String[] attrNames, |
|
150 |
boolean selectedGeomInput, |
|
151 |
boolean selectedGeomOutput, |
|
152 |
boolean closeOutStore) throws DataException { |
|
153 |
this.inFeatureStore = inFeatStore; |
|
154 |
FeatureSet featuresSet = null; |
|
155 |
featuresSet = inFeatStore.getFeatureSet(); |
|
156 |
|
|
157 |
setFeatureStore(outFeatStore, attrNames); |
|
158 |
DisposableIterator it = null; |
|
72 |
private static Logger logger = LoggerFactory.getLogger(DissolveOperationFast.class.getName()); |
|
73 |
private EditableFeature lastEditFeature = null; |
|
74 |
private ArrayList<Element> featureList = null; |
|
75 |
private IDissolveRule rule = null; |
|
76 |
private Summary summary = null; |
|
159 | 77 |
|
160 |
if(selectedGeomInput) { |
|
78 |
class Element { |
|
79 |
|
|
80 |
public int id = -1; |
|
81 |
public Feature feat = null; |
|
82 |
public List<Element> overlapList = new ArrayList<Element>(); |
|
83 |
public boolean insertedToJoin = false; |
|
84 |
public Geometry jtsGeom = null; |
|
85 |
} |
|
86 |
|
|
87 |
class NodeTree { |
|
88 |
|
|
89 |
public Element element = null; |
|
90 |
public int pos = 0; |
|
91 |
public NodeTree parent = null; |
|
92 |
|
|
93 |
public NodeTree(Element node, NodeTree parent) { |
|
94 |
this.element = node; |
|
95 |
this.parent = parent; |
|
96 |
} |
|
97 |
|
|
98 |
public Element getNext() { |
|
99 |
if (pos < element.overlapList.size()) { |
|
100 |
return element.overlapList.get(pos++); |
|
101 |
} |
|
102 |
return null; |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
public DissolveOperationFast(IDissolveRule rule, AbstractSextanteGeoProcess p) { |
|
107 |
super(p); |
|
108 |
this.rule = rule; |
|
109 |
featureList = new ArrayList<Element>(); |
|
110 |
} |
|
111 |
|
|
112 |
/* |
|
113 |
* (non-Javadoc) |
|
114 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature) |
|
115 |
*/ |
|
116 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) { |
|
117 |
return null; |
|
118 |
} |
|
119 |
|
|
120 |
/* |
|
121 |
* (non-Javadoc) |
|
122 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature) |
|
123 |
*/ |
|
124 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
|
125 |
if (g == null) { |
|
126 |
return; |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/* |
|
131 |
* (non-Javadoc) |
|
132 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult() |
|
133 |
*/ |
|
134 |
public Object getResult() { |
|
135 |
return lastEditFeature; |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Computes a complete operation over the input FeatureStore. The result of |
|
140 |
* this operation is stored in the output FeatureStore. |
|
141 |
* |
|
142 |
* @param inFeatStore Input FeatureStore |
|
143 |
* @param outFeatStore Output FeatureStore |
|
144 |
* @param attrNames List of attributes to build the output feature store |
|
145 |
* @param selectedGeom If it is true only the selected geometries will be |
|
146 |
* processed |
|
147 |
* @throws DataException |
|
148 |
*/ |
|
149 |
@SuppressWarnings({"deprecation"}) |
|
150 |
public void computesGeometryOperation(FeatureStore inFeatStore, |
|
151 |
FeatureStore outFeatStore, |
|
152 |
String[] attrNames, |
|
153 |
boolean selectedGeomInput, |
|
154 |
boolean selectedGeomOutput, |
|
155 |
boolean closeOutStore) throws DataException { |
|
156 |
this.inFeatureStore = inFeatStore; |
|
157 |
FeatureSet featuresSet = null; |
|
158 |
featuresSet = inFeatStore.getFeatureSet(); |
|
159 |
|
|
160 |
setFeatureStore(outFeatStore, attrNames); |
|
161 |
DisposableIterator it = null; |
|
162 |
|
|
163 |
if (selectedGeomInput) { |
|
161 | 164 |
FeatureSelection ds = inFeatStore.getFeatureSelection(); |
162 | 165 |
it = ds.iterator(); |
163 | 166 |
numberOfFeatures = (int) ds.getSelectedCount(); |
164 |
} else {
|
|
165 |
it = featuresSet.iterator();
|
|
166 |
numberOfFeatures = (int)featuresSet.getSize();
|
|
167 |
}
|
|
168 |
|
|
167 |
} else {
|
|
168 |
it = featuresSet.iterator();
|
|
169 |
numberOfFeatures = (int) featuresSet.getSize();
|
|
170 |
}
|
|
171 |
|
|
169 | 172 |
if (status != null && process != null) { |
170 | 173 |
status.setRangeOfValues(0, numberOfFeatures); |
171 | 174 |
process.setProgress(0, numberOfFeatures); |
172 | 175 |
} |
173 |
|
|
176 |
|
|
174 | 177 |
//Crear lista de elementos |
175 |
int iCount = 0;
|
|
176 |
while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
|
|
177 |
Feature feat = (Feature)it.next();
|
|
178 |
Element el = new Element();
|
|
179 |
el.feat = feat;
|
|
180 |
el.id = iCount;
|
|
181 |
featureList.add(el);
|
|
178 |
int iCount = 0;
|
|
179 |
while (it.hasNext() && !process.getTaskMonitor().isCanceled()) {
|
|
180 |
Feature feat = (Feature) it.next();
|
|
181 |
Element el = new Element();
|
|
182 |
el.feat = feat;
|
|
183 |
el.id = iCount;
|
|
184 |
featureList.add(el);
|
|
182 | 185 |
if (status != null && process != null) { |
183 | 186 |
status.setCurValue(iCount); |
184 | 187 |
process.setProgress(iCount, numberOfFeatures); |
185 | 188 |
} |
186 |
iCount ++;
|
|
187 |
}
|
|
188 |
it.dispose();
|
|
189 |
|
|
190 |
//Crear listas de solapes para cada feature
|
|
191 |
iCount = 0;
|
|
192 |
while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
|
|
193 |
Element elem1 = featureList.get(iCount);
|
|
194 |
org.gvsig.fmap.geom.Geometry geom1 = elem1.feat.getDefaultGeometry();
|
|
195 |
elem1.jtsGeom = GeometryUtil.geomToJTS(geom1);
|
|
196 |
|
|
197 |
if (status != null)
|
|
189 |
iCount++;
|
|
190 |
}
|
|
191 |
it.dispose();
|
|
192 |
|
|
193 |
//Crear listas de solapes para cada feature
|
|
194 |
iCount = 0;
|
|
195 |
while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
|
|
196 |
Element elem1 = featureList.get(iCount);
|
|
197 |
org.gvsig.fmap.geom.Geometry geom1 = elem1.feat.getDefaultGeometry();
|
|
198 |
elem1.jtsGeom = GeometryUtil.geomToJTS(geom1);
|
|
199 |
|
|
200 |
if (status != null) {
|
|
198 | 201 |
status.setCurValue(iCount); |
199 |
if(process != null) |
|
202 |
} |
|
203 |
if (process != null) { |
|
200 | 204 |
process.setProgress(iCount, numberOfFeatures); |
201 |
|
|
202 |
for (int i = iCount + 1; i < featureList.size(); i++) { |
|
203 |
Element elem2 = featureList.get(i); |
|
204 |
org.gvsig.fmap.geom.Geometry geom2 = elem2.feat.getDefaultGeometry(); |
|
205 |
elem2.jtsGeom = GeometryUtil.geomToJTS(geom2); |
|
206 |
if(rule.verifyIfDissolve(elem1.jtsGeom, elem2.jtsGeom, elem1.feat, elem2.feat)) { |
|
207 |
elem1.overlapList.add(elem2); |
|
208 |
elem2.overlapList.add(elem1); |
|
209 |
} |
|
210 |
} |
|
211 |
iCount ++; |
|
212 |
} |
|
213 |
|
|
205 |
} |
|
206 |
|
|
207 |
for (int i = iCount + 1; i < featureList.size(); i++) { |
|
208 |
Element elem2 = featureList.get(i); |
|
209 |
org.gvsig.fmap.geom.Geometry geom2 = elem2.feat.getDefaultGeometry(); |
|
210 |
elem2.jtsGeom = GeometryUtil.geomToJTS(geom2); |
|
211 |
if (rule.verifyIfDissolve(elem1.jtsGeom, elem2.jtsGeom, elem1.feat, elem2.feat)) { |
|
212 |
elem1.overlapList.add(elem2); |
|
213 |
elem2.overlapList.add(elem1); |
|
214 |
} |
|
215 |
} |
|
216 |
iCount++; |
|
217 |
} |
|
218 |
|
|
214 | 219 |
//Se calculan las listas de geometrias a unir |
215 |
//Para cada feature se obtiene su lista de elementos que solapan y para
|
|
216 |
//cada elemento que solapa se obtiene su lista. Finalmente todas se unen y
|
|
217 |
//y se hace un insert de una feature nueva
|
|
218 |
List<Geometry> listResult = new ArrayList<Geometry>();
|
|
219 |
iCount = 0;
|
|
220 |
int iFeat = 0;
|
|
221 |
summary = new Summary(rule, outFeatStore.getDefaultFeatureType());
|
|
222 |
while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
|
|
223 |
Element elem1 = featureList.get(iCount);
|
|
224 |
summary.loadDefaultSummarizes(elem1.feat);
|
|
225 |
if (status != null)
|
|
220 |
//Para cada feature se obtiene su lista de elementos que solapan y para
|
|
221 |
//cada elemento que solapa se obtiene su lista. Finalmente todas se unen y
|
|
222 |
//y se hace un insert de una feature nueva
|
|
223 |
List<Geometry> listResult = new ArrayList<Geometry>();
|
|
224 |
iCount = 0;
|
|
225 |
int iFeat = 0;
|
|
226 |
summary = new Summary(rule, outFeatStore.getDefaultFeatureType());
|
|
227 |
while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
|
|
228 |
Element elem1 = featureList.get(iCount);
|
|
229 |
summary.loadDefaultSummarizes(elem1.feat);
|
|
230 |
if (status != null) {
|
|
226 | 231 |
status.setCurValue(iCount); |
227 |
if(process != null) |
|
232 |
} |
|
233 |
if (process != null) { |
|
228 | 234 |
process.setProgress(iCount, numberOfFeatures); |
229 |
|
|
230 |
if(!elem1.insertedToJoin) { |
|
231 |
buildListToJoin(elem1, iFeat); |
|
232 |
iFeat ++; |
|
233 |
} |
|
234 |
|
|
235 |
/*if(!elem1.insertedToJoin) { |
|
236 |
elem1.insertedToJoin = true; |
|
237 |
listResult.clear(); |
|
238 |
//org.gvsig.fmap.geom.Geometry dalGeom = elem1.feat.getDefaultGeometry(); |
|
239 |
//Geometry jtsGeom = GeometryUtil.geomToJTS(dalGeom); |
|
240 |
listResult.add(elem1.jtsGeom); |
|
235 |
} |
|
236 |
|
|
237 |
if (!elem1.insertedToJoin) { |
|
238 |
buildListToJoin(elem1, iFeat); |
|
239 |
iFeat++; |
|
240 |
} |
|
241 |
|
|
242 |
/*if(!elem1.insertedToJoin) { |
|
243 |
elem1.insertedToJoin = true; |
|
244 |
listResult.clear(); |
|
245 |
//org.gvsig.fmap.geom.Geometry dalGeom = elem1.feat.getDefaultGeometry(); |
|
246 |
//Geometry jtsGeom = GeometryUtil.geomToJTS(dalGeom); |
|
247 |
listResult.add(elem1.jtsGeom); |
|
241 | 248 |
|
242 |
buildListToJoin(listResult, elem1.overlapList);
|
|
243 |
Geometry newGeom = computesUnion(listResult, elem1);//GeometryUtil.geometryUnion(listResult, elem1.feat.getDefaultGeometry().getGeometryType().getType());
|
|
244 |
try {
|
|
245 |
addFeatureToOutput(newGeom, elem1.feat, iFeat);
|
|
246 |
} catch (CreateGeometryException e) {
|
|
247 |
logger.info("Error a?adiendo geometr?a", e);
|
|
248 |
} catch (DataException e) {
|
|
249 |
logger.info("Error a?adiendo geometr?a", e);
|
|
250 |
}
|
|
251 |
iFeat ++;
|
|
252 |
}*/
|
|
253 |
iCount ++;
|
|
254 |
}
|
|
249 |
buildListToJoin(listResult, elem1.overlapList);
|
|
250 |
Geometry newGeom = computesUnion(listResult, elem1);//GeometryUtil.geometryUnion(listResult, elem1.feat.getDefaultGeometry().getGeometryType().getType());
|
|
251 |
try {
|
|
252 |
addFeatureToOutput(newGeom, elem1.feat, iFeat);
|
|
253 |
} catch (CreateGeometryException e) {
|
|
254 |
logger.info("Error a?adiendo geometr?a", e);
|
|
255 |
} catch (DataException e) {
|
|
256 |
logger.info("Error a?adiendo geometr?a", e);
|
|
257 |
}
|
|
258 |
iFeat ++;
|
|
259 |
}*/
|
|
260 |
iCount++;
|
|
261 |
}
|
|
255 | 262 |
|
256 |
if(closeOutStore && persister != null) |
|
257 |
persister.end(); |
|
258 |
} |
|
259 |
|
|
260 |
/** |
|
261 |
* Computes the union of the list of geometries |
|
262 |
* @param listResult |
|
263 |
* @param elem1 |
|
264 |
* @return |
|
265 |
*/ |
|
266 |
private Geometry computesUnion(List<Geometry> listResult, Element elem1) { |
|
267 |
int splitValue = 500; |
|
268 |
Geometry newGeom = null; |
|
269 |
if(listResult.size() > splitValue) { |
|
270 |
List<List<Geometry>> list = splitList(listResult, splitValue); |
|
271 |
List<Geometry> result = new ArrayList<Geometry>(); |
|
272 |
for (int i = 0; i < list.size(); i++) { |
|
273 |
Geometry aux = GeometryUtil.geometryUnion(list.get(i), elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
274 |
result.add(aux); |
|
275 |
} |
|
276 |
for (int i = 0; i < result.size(); i++) { |
|
277 |
newGeom = GeometryUtil.geometryUnion(result, elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
278 |
} |
|
279 |
} else { |
|
280 |
newGeom = GeometryUtil.geometryUnion(listResult, elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
281 |
} |
|
282 |
return newGeom; |
|
283 |
} |
|
284 |
|
|
285 |
private Geometry computesUnion3(List<Geometry> listResult) { |
|
286 |
Geometry newGeom = null; |
|
287 |
int iCount = 0; |
|
288 |
int max = listResult.size(); |
|
289 |
if(process != null) |
|
290 |
process.setName("Generating union"); |
|
291 |
while(listResult.size() > 1) { |
|
292 |
List<Geometry> list = new ArrayList<Geometry>(); |
|
293 |
if (status != null) |
|
263 |
if (closeOutStore && persister != null) { |
|
264 |
persister.end(); |
|
265 |
} |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Computes the union of the list of geometries |
|
270 |
* |
|
271 |
* @param listResult |
|
272 |
* @param elem1 |
|
273 |
* @return |
|
274 |
*/ |
|
275 |
private Geometry computesUnion(List<Geometry> listResult, Element elem1) { |
|
276 |
int splitValue = 500; |
|
277 |
Geometry newGeom = null; |
|
278 |
if (listResult.size() > splitValue) { |
|
279 |
List<List<Geometry>> list = splitList(listResult, splitValue); |
|
280 |
List<Geometry> result = new ArrayList<Geometry>(); |
|
281 |
for (int i = 0; i < list.size(); i++) { |
|
282 |
Geometry aux = GeometryUtil.geometryUnion(list.get(i), elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
283 |
result.add(aux); |
|
284 |
} |
|
285 |
for (int i = 0; i < result.size(); i++) { |
|
286 |
newGeom = GeometryUtil.geometryUnion(result, elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
287 |
} |
|
288 |
} else { |
|
289 |
newGeom = GeometryUtil.geometryUnion(listResult, elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
290 |
} |
|
291 |
return newGeom; |
|
292 |
} |
|
293 |
|
|
294 |
private Geometry computesUnion3(List<Geometry> listResult) { |
|
295 |
Geometry newGeom = null; |
|
296 |
int iCount = 0; |
|
297 |
int max = listResult.size(); |
|
298 |
if (process != null) { |
|
299 |
process.setName("Generating union"); |
|
300 |
} |
|
301 |
while (listResult.size() > 1) { |
|
302 |
List<Geometry> list = new ArrayList<Geometry>(); |
|
303 |
if (status != null) { |
|
294 | 304 |
status.setCurValue(iCount); |
295 |
if(process != null) |
|
305 |
} |
|
306 |
if (process != null) { |
|
296 | 307 |
process.setProgress(iCount, max); |
297 |
for (int i = 0; i < listResult.size(); i = i + 2) { |
|
298 |
if(i == (listResult.size() - 1)) |
|
299 |
list.add(listResult.get(i)); |
|
300 |
else { |
|
301 |
newGeom = JTSFacade.union(listResult.get(i), listResult.get(i + 1)); |
|
302 |
list.add(newGeom); |
|
303 |
} |
|
304 |
} |
|
305 |
listResult = list; |
|
306 |
} |
|
307 |
return newGeom; |
|
308 |
} |
|
309 |
|
|
310 |
/** |
|
311 |
* Splits the array of geometries to compute its union because JTS cannot support |
|
312 |
* a lot of geometries |
|
313 |
* @param list |
|
314 |
* @param n |
|
315 |
* @return |
|
316 |
*/ |
|
317 |
private List<List<Geometry>> splitList(List<Geometry> list, int n) { |
|
318 |
int elements = (int)(list.size() / n); |
|
319 |
List<List<Geometry>> l = new ArrayList<List<Geometry>>(); |
|
320 |
for (int i = 0; i < elements; i++) { |
|
321 |
l.add(list.subList((i * n), (i * n) + n)); |
|
322 |
} |
|
323 |
if(elements * n < list.size()) { |
|
324 |
l.add(list.subList((elements * n), list.size())); |
|
325 |
} |
|
326 |
return l; |
|
327 |
} |
|
328 |
|
|
329 |
/** |
|
330 |
* Adds a feature to the output |
|
331 |
* @param newGeom |
|
332 |
* @param feat |
|
333 |
* @param newFeatID |
|
334 |
* @throws DataException |
|
335 |
* @throws CreateGeometryException |
|
336 |
*/ |
|
337 |
private void addFeatureToOutput(Geometry newGeom, Feature feat, int newFeatID) throws DataException, CreateGeometryException { |
|
338 |
EditableFeature result = persister.getOutputFeatureStore().createNewFeature(); |
|
339 |
result.setDouble(0, newFeatID); |
|
340 |
result.set(1, feat.get(rule.getIndexField())); |
|
341 |
result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom)); |
|
342 |
summary.loadEditableFeature(result); |
|
343 |
lastEditFeature = persister.addFeature(result, result.getDefaultGeometry()); |
|
344 |
} |
|
345 |
|
|
346 |
/** |
|
347 |
* Builds the union of all lists |
|
348 |
* @param listResult |
|
349 |
* @param listToJoin |
|
350 |
*/ |
|
351 |
private void buildListToJoin(List<Geometry> listResult, List<Element> listToJoin) { |
|
352 |
for (int i = 0; i < listToJoin.size(); i++) { |
|
353 |
Element elem = listToJoin.get(i); |
|
354 |
if(!elem.insertedToJoin) { |
|
355 |
elem.insertedToJoin = true; |
|
356 |
buildListToJoin(listResult, elem.overlapList); |
|
357 |
summary.updateValues(elem.feat); |
|
308 |
} |
|
309 |
for (int i = 0; i < listResult.size(); i = i + 2) { |
|
310 |
if (i == (listResult.size() - 1)) { |
|
311 |
list.add(listResult.get(i)); |
|
312 |
} else { |
|
313 |
newGeom = JTSFacade.union(listResult.get(i), listResult.get(i + 1)); |
|
314 |
list.add(newGeom); |
|
315 |
} |
|
316 |
} |
|
317 |
listResult = list; |
|
318 |
} |
|
319 |
return newGeom; |
|
320 |
} |
|
321 |
|
|
322 |
/** |
|
323 |
* Splits the array of geometries to compute its union because JTS cannot |
|
324 |
* support a lot of geometries |
|
325 |
* |
|
326 |
* @param list |
|
327 |
* @param n |
|
328 |
* @return |
|
329 |
*/ |
|
330 |
private List<List<Geometry>> splitList(List<Geometry> list, int n) { |
|
331 |
int elements = (int) (list.size() / n); |
|
332 |
List<List<Geometry>> l = new ArrayList<List<Geometry>>(); |
|
333 |
for (int i = 0; i < elements; i++) { |
|
334 |
l.add(list.subList((i * n), (i * n) + n)); |
|
335 |
} |
|
336 |
if (elements * n < list.size()) { |
|
337 |
l.add(list.subList((elements * n), list.size())); |
|
338 |
} |
|
339 |
return l; |
|
340 |
} |
|
341 |
|
|
342 |
/** |
|
343 |
* Adds a feature to the output |
|
344 |
* |
|
345 |
* @param newGeom |
|
346 |
* @param feat |
|
347 |
* @param newFeatID |
|
348 |
* @throws DataException |
|
349 |
* @throws CreateGeometryException |
|
350 |
*/ |
|
351 |
private void addFeatureToOutput(Geometry newGeom, Feature feat, int newFeatID) throws DataException, CreateGeometryException { |
|
352 |
EditableFeature result = persister.getOutputFeatureStore().createNewFeature(); |
|
353 |
result.setDouble(0, newFeatID); |
|
354 |
result.set(1, feat.get(rule.getIndexField())); |
|
355 |
result.setGeometry("GEOMETRY", GeometryUtil.jtsToGeom(newGeom)); |
|
356 |
summary.loadEditableFeature(result); |
|
357 |
lastEditFeature = persister.addFeature(result, result.getDefaultGeometry()); |
|
358 |
} |
|
359 |
|
|
360 |
/** |
|
361 |
* Builds the union of all lists |
|
362 |
* |
|
363 |
* @param listResult |
|
364 |
* @param listToJoin |
|
365 |
*/ |
|
366 |
private void buildListToJoin(List<Geometry> listResult, List<Element> listToJoin) { |
|
367 |
for (int i = 0; i < listToJoin.size(); i++) { |
|
368 |
Element elem = listToJoin.get(i); |
|
369 |
if (!elem.insertedToJoin) { |
|
370 |
elem.insertedToJoin = true; |
|
371 |
buildListToJoin(listResult, elem.overlapList); |
|
372 |
summary.updateValues(elem.feat); |
|
358 | 373 |
//org.gvsig.fmap.geom.Geometry dalGeom = elem.feat.getDefaultGeometry(); |
359 |
//Geometry jtsGeom = GeometryUtil.geomToJTS(dalGeom); |
|
360 |
listResult.add(elem.jtsGeom); |
|
361 |
} |
|
362 |
} |
|
363 |
} |
|
364 |
|
|
365 |
/** |
|
366 |
* Builds the union of all lists |
|
367 |
* @param listResult |
|
368 |
* @param listToJoin |
|
369 |
*/ |
|
370 |
private void buildListToJoin(Element elem, int iFeat) { |
|
371 |
Geometry newGeom = null; |
|
372 |
|
|
373 |
if(elem.overlapList.size() == 0) { |
|
374 |
if(!elem.insertedToJoin) |
|
375 |
elem.insertedToJoin = true; |
|
376 |
try { |
|
377 |
addFeatureToOutput(elem.jtsGeom, elem.feat, iFeat); |
|
378 |
} catch (CreateGeometryException e) { |
|
379 |
logger.info("Error a?adiendo geometr?a", e); |
|
380 |
} catch (DataException e) { |
|
381 |
logger.info("Error a?adiendo geometr?a", e); |
|
382 |
} |
|
383 |
} else { |
|
384 |
List<Geometry> listResult = new ArrayList<Geometry>(); |
|
385 |
NodeTree subtree = new NodeTree(elem, null); |
|
374 |
//Geometry jtsGeom = GeometryUtil.geomToJTS(dalGeom); |
|
375 |
listResult.add(elem.jtsGeom); |
|
376 |
} |
|
377 |
} |
|
378 |
} |
|
379 |
|
|
380 |
/** |
|
381 |
* Builds the union of all lists |
|
382 |
* |
|
383 |
* @param listResult |
|
384 |
* @param listToJoin |
|
385 |
*/ |
|
386 |
private void buildListToJoin(Element elem, int iFeat) { |
|
387 |
Geometry newGeom = null; |
|
388 |
|
|
389 |
if (elem.overlapList.size() == 0) { |
|
390 |
if (!elem.insertedToJoin) { |
|
391 |
elem.insertedToJoin = true; |
|
392 |
} |
|
393 |
try { |
|
394 |
addFeatureToOutput(elem.jtsGeom, elem.feat, iFeat); |
|
395 |
} catch (CreateGeometryException e) { |
|
396 |
logger.info("Error a?adiendo geometr?a", e); |
|
397 |
} catch (DataException e) { |
|
398 |
logger.info("Error a?adiendo geometr?a", e); |
|
399 |
} |
|
400 |
} else { |
|
401 |
List<Geometry> listResult = new ArrayList<Geometry>(); |
|
402 |
NodeTree subtree = new NodeTree(elem, null); |
|
386 | 403 |
//Hacemos un recorrido en profundidad del ?rbol para a?adir |
387 |
//todos los elementos a la lista de geometrias a unir sin |
|
388 |
//repetir ninguna. |
|
389 |
while (subtree != null) { |
|
390 |
if(!subtree.element.insertedToJoin) { |
|
391 |
listResult.add(subtree.element.jtsGeom); |
|
392 |
summary.updateValues(subtree.element.feat); |
|
393 |
subtree.element.insertedToJoin = true; |
|
394 |
} |
|
395 |
|
|
396 |
boolean back = false; |
|
397 |
|
|
398 |
Element l = subtree.getNext(); |
|
399 |
if(l == null) |
|
400 |
back = true; |
|
401 |
|
|
402 |
while(!back && l.insertedToJoin) { |
|
403 |
l = subtree.getNext(); |
|
404 |
if(l == null) |
|
405 |
back = true; |
|
406 |
} |
|
407 |
|
|
408 |
if(back) { |
|
409 |
subtree = subtree.parent; |
|
410 |
continue; |
|
411 |
} |
|
412 |
subtree = new NodeTree(l, subtree); |
|
413 |
} |
|
414 |
newGeom = computesUnion3(listResult); |
|
415 |
|
|
416 |
try { |
|
417 |
addFeatureToOutput(newGeom, elem.feat, iFeat); |
|
418 |
} catch (DataException e) { |
|
419 |
logger.info("Imposible insertar en la tabla", e); |
|
420 |
} catch (CreateGeometryException e) { |
|
421 |
logger.info("Error a?adiendo geometr?a", e); |
|
422 |
} |
|
423 |
} |
|
424 |
|
|
425 |
} |
|
426 |
|
|
404 |
//todos los elementos a la lista de geometrias a unir sin |
|
405 |
//repetir ninguna. |
|
406 |
while (subtree != null) { |
|
407 |
if (!subtree.element.insertedToJoin) { |
|
408 |
listResult.add(subtree.element.jtsGeom); |
|
409 |
summary.updateValues(subtree.element.feat); |
|
410 |
subtree.element.insertedToJoin = true; |
|
411 |
} |
|
412 |
|
|
413 |
boolean back = false; |
|
414 |
|
|
415 |
Element l = subtree.getNext(); |
|
416 |
if (l == null) { |
|
417 |
back = true; |
|
418 |
} |
|
419 |
|
|
420 |
while (!back && l.insertedToJoin) { |
|
421 |
l = subtree.getNext(); |
|
422 |
if (l == null) { |
|
423 |
back = true; |
|
424 |
} |
|
425 |
} |
|
426 |
|
|
427 |
if (back) { |
|
428 |
subtree = subtree.parent; |
|
429 |
continue; |
|
430 |
} |
|
431 |
subtree = new NodeTree(l, subtree); |
|
432 |
} |
|
433 |
newGeom = computesUnion3(listResult); |
|
434 |
|
|
435 |
try { |
|
436 |
addFeatureToOutput(newGeom, elem.feat, iFeat); |
|
437 |
} catch (DataException e) { |
|
438 |
logger.info("Imposible insertar en la tabla", e); |
|
439 |
} catch (CreateGeometryException e) { |
|
440 |
logger.info("Error a?adiendo geometr?a", e); |
|
441 |
} |
|
442 |
} |
|
443 |
|
|
444 |
} |
|
445 |
|
|
427 | 446 |
} |
428 |
|
Also available in: Unified diff