Revision 1132
org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/java/org/gvsig/geoprocess/algorithm/fusespatially/FuseSpatiallyOperationFast2.java | ||
---|---|---|
41 | 41 |
* along with this program; if not, write to the Free Software |
42 | 42 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
43 | 43 |
*/ |
44 |
|
|
45 | 44 |
package org.gvsig.geoprocess.algorithm.fusespatially; |
46 | 45 |
|
47 | 46 |
import java.util.ArrayList; |
... | ... | |
64 | 63 |
|
65 | 64 |
import com.vividsolutions.jts.geom.Geometry; |
66 | 65 |
import java.util.Iterator; |
66 |
|
|
67 | 67 |
/** |
68 | 68 |
* Fuse spatially operation |
69 |
* |
|
69 | 70 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a> |
70 | 71 |
*/ |
71 | 72 |
public class FuseSpatiallyOperationFast2 extends GeometryOperation { |
72 |
private static Logger logger = LoggerFactory.getLogger(FuseSpatiallyOperationFast2.class.getName()); |
|
73 |
private ArrayList<Element> featureList = null; |
|
74 |
private String nameIdField = null; |
|
75 |
private FeatureStore outFeatStoreTable = null; |
|
76 |
private NodeTree baseTree = null; |
|
77 | 73 |
|
78 |
class Element { |
|
79 |
public int id = -1; |
|
80 |
public Feature feat = null; |
|
81 |
public List<Element> overlapList = new ArrayList<Element>(); |
|
82 |
public boolean insertedToJoin = false; |
|
83 |
public Geometry jtsGeom = null; |
|
84 |
} |
|
74 |
private static Logger logger = LoggerFactory.getLogger(FuseSpatiallyOperationFast2.class.getName()); |
|
75 |
private ArrayList<Element> featureList = null; |
|
76 |
private String nameIdField = null; |
|
77 |
private FeatureStore outFeatStoreTable = null; |
|
78 |
private NodeTree baseTree = null; |
|
85 | 79 |
|
86 |
class NodeTree { |
|
87 |
public Element element = null; |
|
88 |
public int pos = 0; |
|
89 |
public NodeTree parent = null; |
|
80 |
class Element { |
|
90 | 81 |
|
91 |
public NodeTree(Element node, NodeTree parent) { |
|
92 |
this.element = node; |
|
93 |
this.parent = parent; |
|
94 |
} |
|
82 |
public int id = -1; |
|
83 |
public Feature feat = null; |
|
84 |
public List<Element> overlapList = new ArrayList<Element>(); |
|
85 |
public boolean insertedToJoin = false; |
|
86 |
public Geometry jtsGeom = null; |
|
87 |
} |
|
95 | 88 |
|
96 |
public Element getNext() { |
|
97 |
if(pos < element.overlapList.size()) |
|
98 |
return element.overlapList.get(pos++); |
|
99 |
return null; |
|
100 |
} |
|
101 |
} |
|
89 |
class NodeTree { |
|
102 | 90 |
|
103 |
public FuseSpatiallyOperationFast2(AbstractSextanteGeoProcess process) { |
|
104 |
super(process); |
|
105 |
featureList = new ArrayList<Element>(); |
|
106 |
} |
|
91 |
public Element element = null; |
|
92 |
public int pos = 0; |
|
93 |
public NodeTree parent = null; |
|
107 | 94 |
|
108 |
@Override |
|
109 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, |
|
110 |
Feature featureInput) { |
|
111 |
// TODO Auto-generated method stub |
|
112 |
return null; |
|
113 |
} |
|
95 |
public NodeTree(Element node, NodeTree parent) { |
|
96 |
this.element = node; |
|
97 |
this.parent = parent; |
|
98 |
} |
|
114 | 99 |
|
115 |
@Override |
|
116 |
public void invoke(org.gvsig.fmap.geom.Geometry g, |
|
117 |
EditableFeature featureInput) { |
|
118 |
// TODO Auto-generated method stub |
|
100 |
public Element getNext() { |
|
101 |
if (pos < element.overlapList.size()) { |
|
102 |
return element.overlapList.get(pos++); |
|
103 |
} |
|
104 |
return null; |
|
105 |
} |
|
106 |
} |
|
119 | 107 |
|
120 |
} |
|
108 |
public FuseSpatiallyOperationFast2(AbstractSextanteGeoProcess process) { |
|
109 |
super(process); |
|
110 |
featureList = new ArrayList<Element>(); |
|
111 |
} |
|
121 | 112 |
|
122 |
/** |
|
123 |
* Computes a complete operation over the input FeatureStore. The result of this operation |
|
124 |
* is stored in the output FeatureStore. This method will call once for each geometry. |
|
125 |
* @param inFeatStore |
|
126 |
* Input FeatureStore |
|
127 |
* @param outFeatStore |
|
128 |
* Output FeatureStore |
|
129 |
* @param outFeatStoreTable |
|
130 |
* @param attrNames |
|
131 |
* List of attributes to build the output feature store |
|
132 |
* @param attrNamesTable |
|
133 |
* @param selectedGeomInput |
|
134 |
* @param selectedGeomOutput |
|
135 |
* @param idField |
|
136 |
* @param selectedGeom |
|
137 |
* If it is true only the selected geometries will be processed |
|
138 |
* @throws DataException |
|
139 |
*/ |
|
140 |
@SuppressWarnings("deprecation") |
|
141 |
public void computesGeometryOperation(FeatureStore inFeatStore, |
|
142 |
FeatureStore outFeatStore, |
|
143 |
FeatureStore outFeatStoreTable, |
|
144 |
String[] attrNames, |
|
145 |
String[] attrNamesTable, |
|
146 |
boolean selectedGeomInput, |
|
147 |
boolean selectedGeomOutput, |
|
148 |
String idField) throws DataException { |
|
149 |
this.outFeatStoreTable = outFeatStoreTable; |
|
150 |
this.nameIdField = idField; |
|
151 |
this.inFeatureStore = inFeatStore; |
|
152 |
this.selectedGeomInput = selectedGeomInput; |
|
153 |
this.selectedGeomOverlay = selectedGeomOutput; |
|
154 |
FeatureSet featuresSet = null; |
|
155 |
featuresSet = inFeatStore.getFeatureSet(); |
|
113 |
@Override |
|
114 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, |
|
115 |
Feature featureInput) { |
|
116 |
// TODO Auto-generated method stub |
|
117 |
return null; |
|
118 |
} |
|
156 | 119 |
|
157 |
setFeatureStore(outFeatStore, attrNames); |
|
158 |
Iterator it = null; |
|
120 |
@Override |
|
121 |
public void invoke(org.gvsig.fmap.geom.Geometry g, |
|
122 |
EditableFeature featureInput) { |
|
123 |
// TODO Auto-generated method stub |
|
159 | 124 |
|
160 |
if(selectedGeomInput) { |
|
125 |
} |
|
126 |
|
|
127 |
/** |
|
128 |
* Computes a complete operation over the input FeatureStore. The result of |
|
129 |
* this operation is stored in the output FeatureStore. This method will |
|
130 |
* call once for each geometry. |
|
131 |
* |
|
132 |
* @param inFeatStore Input FeatureStore |
|
133 |
* @param outFeatStore Output FeatureStore |
|
134 |
* @param outFeatStoreTable |
|
135 |
* @param attrNames List of attributes to build the output feature store |
|
136 |
* @param attrNamesTable |
|
137 |
* @param selectedGeomInput |
|
138 |
* @param selectedGeomOutput |
|
139 |
* @param idField |
|
140 |
* @param selectedGeom If it is true only the selected geometries will be |
|
141 |
* processed |
|
142 |
* @throws DataException |
|
143 |
*/ |
|
144 |
@SuppressWarnings("deprecation") |
|
145 |
public void computesGeometryOperation(FeatureStore inFeatStore, |
|
146 |
FeatureStore outFeatStore, |
|
147 |
FeatureStore outFeatStoreTable, |
|
148 |
String[] attrNames, |
|
149 |
String[] attrNamesTable, |
|
150 |
boolean selectedGeomInput, |
|
151 |
boolean selectedGeomOutput, |
|
152 |
String idField) throws DataException { |
|
153 |
this.outFeatStoreTable = outFeatStoreTable; |
|
154 |
this.nameIdField = idField; |
|
155 |
this.inFeatureStore = inFeatStore; |
|
156 |
this.selectedGeomInput = selectedGeomInput; |
|
157 |
this.selectedGeomOverlay = selectedGeomOutput; |
|
158 |
FeatureSet featuresSet = null; |
|
159 |
featuresSet = inFeatStore.getFeatureSet(); |
|
160 |
|
|
161 |
setFeatureStore(outFeatStore, attrNames); |
|
162 |
Iterator it = null; |
|
163 |
|
|
164 |
if (selectedGeomInput) { |
|
161 | 165 |
FeatureSelection ds = inFeatStore.getFeatureSelection(); |
162 | 166 |
it = ds.iterator(); |
163 | 167 |
numberOfFeatures = (int) ds.getSelectedCount(); |
164 |
} else {
|
|
165 |
it = featuresSet.iterator();
|
|
166 |
numberOfFeatures = (int)featuresSet.getSize();
|
|
167 |
}
|
|
168 |
} else {
|
|
169 |
it = featuresSet.iterator();
|
|
170 |
numberOfFeatures = (int) featuresSet.getSize();
|
|
171 |
}
|
|
168 | 172 |
|
169 |
if (status != null) |
|
173 |
if (status != null) {
|
|
170 | 174 |
status.setRangeOfValues(0, numberOfFeatures); |
171 |
if(process != null) |
|
175 |
} |
|
176 |
if (process != null) { |
|
172 | 177 |
process.setProgress(0, numberOfFeatures); |
178 |
} |
|
173 | 179 |
|
174 | 180 |
//Crear lista de elementos |
175 |
int iCount = 0;
|
|
176 |
while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
|
|
177 |
Feature feat = ((Feature)it.next()).getCopy();
|
|
178 |
Element el = new Element();
|
|
179 |
el.feat = feat;
|
|
180 |
el.id = iCount;
|
|
181 |
featureList.add(el);
|
|
182 |
if (status != null && process != null) |
|
181 |
int iCount = 0;
|
|
182 |
while (it.hasNext() && !process.getTaskMonitor().isCanceled()) {
|
|
183 |
Feature feat = ((Feature) it.next()).getCopy();
|
|
184 |
Element el = new Element();
|
|
185 |
el.feat = feat;
|
|
186 |
el.id = iCount;
|
|
187 |
featureList.add(el);
|
|
188 |
if (status != null && process != null) {
|
|
183 | 189 |
status.setCurValue(iCount); |
184 |
if(process != null) |
|
190 |
} |
|
191 |
if (process != null) { |
|
185 | 192 |
process.setProgress(iCount, numberOfFeatures); |
186 |
iCount ++; |
|
187 |
} |
|
193 |
} |
|
194 |
iCount++; |
|
195 |
} |
|
188 | 196 |
// it.dispose(); |
189 | 197 |
|
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 |
if (status != null)
|
|
198 |
//Crear listas de solapes para cada feature
|
|
199 |
iCount = 0;
|
|
200 |
while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
|
|
201 |
Element elem1 = featureList.get(iCount);
|
|
202 |
org.gvsig.fmap.geom.Geometry geom1 = elem1.feat.getDefaultGeometry();
|
|
203 |
if (status != null) {
|
|
196 | 204 |
status.setCurValue(iCount); |
197 |
if(process != null) |
|
205 |
} |
|
206 |
if (process != null) { |
|
198 | 207 |
process.setProgress(iCount, numberOfFeatures); |
208 |
} |
|
199 | 209 |
|
200 |
for (int i = iCount + 1; i < featureList.size(); i++) {
|
|
201 |
Element elem2 = featureList.get(i);
|
|
202 |
org.gvsig.fmap.geom.Geometry geom2 = elem2.feat.getDefaultGeometry();
|
|
203 |
if(areBBoxesOverlaping(geom1, geom2)) {
|
|
204 |
if(elem1.jtsGeom == null) {
|
|
205 |
elem1.jtsGeom = GeometryUtil.geomToJTS(geom1);
|
|
206 |
}
|
|
207 |
elem2.jtsGeom = GeometryUtil.geomToJTS(geom2);
|
|
208 |
if(elem1.jtsGeom.intersects(elem2.jtsGeom)) {
|
|
209 |
elem1.overlapList.add(elem2);
|
|
210 |
elem2.overlapList.add(elem1);
|
|
211 |
}
|
|
212 |
}
|
|
213 |
}
|
|
214 |
iCount ++;
|
|
215 |
}
|
|
210 |
for (int i = iCount + 1; i < featureList.size(); i++) {
|
|
211 |
Element elem2 = featureList.get(i);
|
|
212 |
org.gvsig.fmap.geom.Geometry geom2 = elem2.feat.getDefaultGeometry();
|
|
213 |
if (areBBoxesOverlaping(geom1, geom2)) {
|
|
214 |
if (elem1.jtsGeom == null) {
|
|
215 |
elem1.jtsGeom = GeometryUtil.geomToJTS(geom1);
|
|
216 |
}
|
|
217 |
elem2.jtsGeom = GeometryUtil.geomToJTS(geom2);
|
|
218 |
if (elem1.jtsGeom.intersects(elem2.jtsGeom)) {
|
|
219 |
elem1.overlapList.add(elem2);
|
|
220 |
elem2.overlapList.add(elem1);
|
|
221 |
}
|
|
222 |
}
|
|
223 |
}
|
|
224 |
iCount++;
|
|
225 |
}
|
|
216 | 226 |
|
217 |
iCount = 0;
|
|
218 |
int iFeat = 0;
|
|
219 |
while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
|
|
220 |
Element elem1 = featureList.get(iCount);
|
|
221 |
if (status != null)
|
|
227 |
iCount = 0;
|
|
228 |
int iFeat = 0;
|
|
229 |
while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
|
|
230 |
Element elem1 = featureList.get(iCount);
|
|
231 |
if (status != null) {
|
|
222 | 232 |
status.setCurValue(iCount); |
223 |
if(process != null) |
|
233 |
} |
|
234 |
if (process != null) { |
|
224 | 235 |
process.setProgress(iCount, numberOfFeatures); |
236 |
} |
|
225 | 237 |
|
226 |
if(!elem1.insertedToJoin) {
|
|
227 |
buildListToJoin(elem1, iFeat);
|
|
228 |
iFeat ++;
|
|
229 |
}
|
|
238 |
if (!elem1.insertedToJoin) {
|
|
239 |
buildListToJoin(elem1, iFeat);
|
|
240 |
iFeat++;
|
|
241 |
}
|
|
230 | 242 |
|
231 |
iCount ++;
|
|
232 |
}
|
|
243 |
iCount++;
|
|
244 |
}
|
|
233 | 245 |
|
234 |
if(persister != null) |
|
235 |
persister.end(); |
|
246 |
if (persister != null) { |
|
247 |
persister.end(); |
|
248 |
} |
|
236 | 249 |
|
237 |
}
|
|
250 |
}
|
|
238 | 251 |
|
239 |
private boolean areBBoxesOverlaping(org.gvsig.fmap.geom.Geometry g1, org.gvsig.fmap.geom.Geometry g2) { |
|
240 |
if(g1.getEnvelope().getMaximum(0) < g2.getEnvelope().getMinimum(0)) |
|
241 |
return false; |
|
242 |
if(g1.getEnvelope().getMinimum(0) > g2.getEnvelope().getMaximum(0)) |
|
243 |
return false; |
|
244 |
if(g1.getEnvelope().getMaximum(1) < g2.getEnvelope().getMinimum(1)) |
|
245 |
return false; |
|
246 |
if(g1.getEnvelope().getMinimum(1) > g2.getEnvelope().getMaximum(1)) |
|
247 |
return false; |
|
248 |
return true; |
|
249 |
} |
|
252 |
private boolean areBBoxesOverlaping(org.gvsig.fmap.geom.Geometry g1, org.gvsig.fmap.geom.Geometry g2) { |
|
253 |
if (g1.getEnvelope().getMaximum(0) < g2.getEnvelope().getMinimum(0)) { |
|
254 |
return false; |
|
255 |
} |
|
256 |
if (g1.getEnvelope().getMinimum(0) > g2.getEnvelope().getMaximum(0)) { |
|
257 |
return false; |
|
258 |
} |
|
259 |
if (g1.getEnvelope().getMaximum(1) < g2.getEnvelope().getMinimum(1)) { |
|
260 |
return false; |
|
261 |
} |
|
262 |
if (g1.getEnvelope().getMinimum(1) > g2.getEnvelope().getMaximum(1)) { |
|
263 |
return false; |
|
264 |
} |
|
265 |
return true; |
|
266 |
} |
|
250 | 267 |
|
251 |
/** |
|
252 |
* Computes the union of the list of geometries |
|
253 |
* @param listResult |
|
254 |
* @param elem1 |
|
255 |
* @return |
|
256 |
*/ |
|
257 |
private Geometry computesUnion(List<Geometry> listResult, Element elem1) { |
|
258 |
int splitValue = 500; |
|
259 |
Geometry newGeom = null; |
|
260 |
if(listResult.size() > splitValue) { |
|
261 |
List<List<Geometry>> list = splitList(listResult, splitValue); |
|
262 |
List<Geometry> result = new ArrayList<Geometry>(); |
|
263 |
for (int i = 0; i < list.size(); i++) { |
|
264 |
Geometry aux = GeometryUtil.geometryUnion(list.get(i), elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
265 |
result.add(aux); |
|
266 |
} |
|
267 |
for (int i = 0; i < result.size(); i++) { |
|
268 |
newGeom = GeometryUtil.geometryUnion(result, elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
269 |
} |
|
270 |
} else { |
|
271 |
newGeom = GeometryUtil.geometryUnion(listResult, elem1.feat.getDefaultGeometry().getGeometryType().getType()); |
|
272 |
} |
|
273 |
return newGeom; |
|
274 |
} |
|
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 |
} |
|
275 | 293 |
|
276 |
private Geometry computesUnion2(List<Geometry> listResult, Element elem1) { |
|
277 |
Geometry newGeom = null; |
|
278 |
for (int i = 1; i < listResult.size(); i++) { |
|
279 |
if(i == 1) |
|
280 |
newGeom = JTSFacade.union(listResult.get(0), listResult.get(1)); |
|
281 |
else |
|
282 |
newGeom = JTSFacade.union(newGeom, listResult.get(i)); |
|
283 |
} |
|
284 |
return newGeom; |
|
285 |
} |
|
294 |
private Geometry computesUnion2(List<Geometry> listResult, Element elem1) { |
|
295 |
Geometry newGeom = null; |
|
296 |
for (int i = 1; i < listResult.size(); i++) { |
|
297 |
if (i == 1) { |
|
298 |
newGeom = JTSFacade.union(listResult.get(0), listResult.get(1)); |
|
299 |
} else { |
|
300 |
newGeom = JTSFacade.union(newGeom, listResult.get(i)); |
|
301 |
} |
|
302 |
} |
|
303 |
return newGeom; |
|
304 |
} |
|
286 | 305 |
|
287 |
private Geometry computesUnion3(List<Geometry> listResult) { |
|
288 |
Geometry newGeom = null; |
|
289 |
int iCount = 0; |
|
290 |
int max = listResult.size(); |
|
291 |
if(process != null) |
|
292 |
process.setName("Generating union"); |
|
293 |
while(listResult.size() > 1) { |
|
294 |
List<Geometry> list = new ArrayList<Geometry>(); |
|
295 |
if (status != null) |
|
306 |
private Geometry computesUnion3(List<Geometry> listResult) { |
|
307 |
Geometry newGeom = null; |
|
308 |
int iCount = 0; |
|
309 |
int max = listResult.size(); |
|
310 |
if (process != null) { |
|
311 |
process.setName("Generating union"); |
|
312 |
} |
|
313 |
while (listResult.size() > 1) { |
|
314 |
List<Geometry> list = new ArrayList<Geometry>(); |
|
315 |
if (status != null) { |
|
296 | 316 |
status.setCurValue(iCount); |
297 |
if(process != null) |
|
317 |
} |
|
318 |
if (process != null) { |
|
298 | 319 |
process.setProgress(iCount, max); |
299 |
for (int i = 0; i < listResult.size(); i = i + 2) { |
|
300 |
if(i == (listResult.size() - 1)) |
|
301 |
list.add(listResult.get(i)); |
|
302 |
else { |
|
303 |
newGeom = JTSFacade.union(listResult.get(i), listResult.get(i + 1)); |
|
304 |
list.add(newGeom); |
|
305 |
} |
|
306 |
} |
|
307 |
listResult = list; |
|
308 |
} |
|
309 |
return newGeom; |
|
310 |
} |
|
320 |
} |
|
321 |
for (int i = 0; i < listResult.size(); i = i + 2) { |
|
322 |
if (i == (listResult.size() - 1)) { |
|
323 |
list.add(listResult.get(i)); |
|
324 |
} else { |
|
325 |
newGeom = JTSFacade.union(listResult.get(i), listResult.get(i + 1)); |
|
326 |
list.add(newGeom); |
|
327 |
} |
|
328 |
} |
|
329 |
listResult = list; |
|
330 |
} |
|
331 |
return newGeom; |
|
332 |
} |
|
311 | 333 |
|
312 |
/** |
|
313 |
* Splits the array of geometries to compute its union because JTS cannot support |
|
314 |
* a lot of geometries |
|
315 |
* @param list |
|
316 |
* @param n |
|
317 |
* @return |
|
318 |
*/ |
|
319 |
private List<List<Geometry>> splitList(List<Geometry> list, int n) { |
|
320 |
int elements = (int)(list.size() / n); |
|
321 |
List<List<Geometry>> l = new ArrayList<List<Geometry>>(); |
|
322 |
for (int i = 0; i < elements; i++) { |
|
323 |
l.add(list.subList((i * n), (i * n) + n)); |
|
324 |
} |
|
325 |
if(elements * n < list.size()) { |
|
326 |
l.add(list.subList((elements * n), list.size())); |
|
327 |
} |
|
328 |
return l; |
|
329 |
} |
|
334 |
/** |
|
335 |
* Splits the array of geometries to compute its union because JTS cannot |
|
336 |
* support a lot of geometries |
|
337 |
* |
|
338 |
* @param list |
|
339 |
* @param n |
|
340 |
* @return |
|
341 |
*/ |
|
342 |
private List<List<Geometry>> splitList(List<Geometry> list, int n) { |
|
343 |
int elements = (int) (list.size() / n); |
|
344 |
List<List<Geometry>> l = new ArrayList<List<Geometry>>(); |
|
345 |
for (int i = 0; i < elements; i++) { |
|
346 |
l.add(list.subList((i * n), (i * n) + n)); |
|
347 |
} |
|
348 |
if (elements * n < list.size()) { |
|
349 |
l.add(list.subList((elements * n), list.size())); |
|
350 |
} |
|
351 |
return l; |
|
352 |
} |
|
330 | 353 |
|
331 |
/** |
|
332 |
* Builds the union of all lists |
|
333 |
* @param listResult |
|
334 |
* @param listToJoin |
|
335 |
*/ |
|
336 |
private void buildListToJoin(Element elem, int iFeat) { |
|
337 |
Geometry newGeom = null; |
|
354 |
/** |
|
355 |
* Builds the union of all lists |
|
356 |
* |
|
357 |
* @param listResult |
|
358 |
* @param listToJoin |
|
359 |
*/ |
|
360 |
private void buildListToJoin(Element elem, int iFeat) { |
|
361 |
Geometry newGeom = null; |
|
338 | 362 |
|
339 |
if(elem.overlapList.size() == 0) { |
|
340 |
if(!elem.insertedToJoin) |
|
341 |
elem.insertedToJoin = true; |
|
342 |
try { |
|
343 |
insertInTable(outFeatStoreTable, elem.feat, iFeat); |
|
344 |
addFeatureToOutput(elem.feat.getDefaultGeometry(), elem.feat, iFeat); |
|
345 |
} catch (DataException e) { |
|
346 |
logger.info("Imposible insertar en la tabla", e); |
|
347 |
} catch (CreateGeometryException e) { |
|
348 |
logger.info("Error a?adiendo geometr?a", e); |
|
349 |
} |
|
350 |
} else { |
|
351 |
List<Geometry> listResult = new ArrayList<Geometry>(); |
|
352 |
NodeTree subtree = new NodeTree(elem, null); |
|
353 |
//Hacemos un recorrido en profundidad del ?rbol para a?adir |
|
354 |
//todos los elementos a la lista de geometrias a unir sin |
|
355 |
//repetir ninguna. |
|
356 |
while (subtree != null) { |
|
357 |
if(!subtree.element.insertedToJoin) { |
|
358 |
listResult.add(subtree.element.jtsGeom); |
|
359 |
try { |
|
360 |
insertInTable(outFeatStoreTable, subtree.element.feat, iFeat); |
|
361 |
} catch (DataException e) { |
|
362 |
logger.info("Imposible insertar en la tabla", e); |
|
363 |
} |
|
364 |
subtree.element.insertedToJoin = true; |
|
365 |
} |
|
363 |
if (elem.overlapList.size() == 0) { |
|
364 |
if (!elem.insertedToJoin) { |
|
365 |
elem.insertedToJoin = true; |
|
366 |
} |
|
367 |
try { |
|
368 |
insertInTable(outFeatStoreTable, elem.feat, iFeat); |
|
369 |
addFeatureToOutput(elem.feat.getDefaultGeometry(), elem.feat, iFeat); |
|
370 |
} catch (DataException e) { |
|
371 |
logger.info("Imposible insertar en la tabla", e); |
|
372 |
} catch (CreateGeometryException e) { |
|
373 |
logger.info("Error a?adiendo geometr?a", e); |
|
374 |
} |
|
375 |
} else { |
|
376 |
List<Geometry> listResult = new ArrayList<Geometry>(); |
|
377 |
NodeTree subtree = new NodeTree(elem, null); |
|
378 |
//Hacemos un recorrido en profundidad del ?rbol para a?adir |
|
379 |
//todos los elementos a la lista de geometrias a unir sin |
|
380 |
//repetir ninguna. |
|
381 |
while (subtree != null) { |
|
382 |
if (!subtree.element.insertedToJoin) { |
|
383 |
listResult.add(subtree.element.jtsGeom); |
|
384 |
try { |
|
385 |
insertInTable(outFeatStoreTable, subtree.element.feat, iFeat); |
|
386 |
} catch (DataException e) { |
|
387 |
logger.info("Imposible insertar en la tabla", e); |
|
388 |
} |
|
389 |
subtree.element.insertedToJoin = true; |
|
390 |
} |
|
366 | 391 |
|
367 |
boolean back = false;
|
|
392 |
boolean back = false;
|
|
368 | 393 |
|
369 |
Element l = subtree.getNext(); |
|
370 |
if(l == null) |
|
371 |
back = true; |
|
394 |
Element l = subtree.getNext(); |
|
395 |
if (l == null) { |
|
396 |
back = true; |
|
397 |
} |
|
372 | 398 |
|
373 |
while(!back && l.insertedToJoin) { |
|
374 |
l = subtree.getNext(); |
|
375 |
if(l == null) |
|
376 |
back = true; |
|
377 |
} |
|
399 |
while (!back && l.insertedToJoin) { |
|
400 |
l = subtree.getNext(); |
|
401 |
if (l == null) { |
|
402 |
back = true; |
|
403 |
} |
|
404 |
} |
|
378 | 405 |
|
379 |
if(back) {
|
|
380 |
subtree = subtree.parent;
|
|
381 |
continue;
|
|
382 |
}
|
|
383 |
subtree = new NodeTree(l, subtree);
|
|
384 |
}
|
|
385 |
newGeom = computesUnion3(listResult);
|
|
406 |
if (back) {
|
|
407 |
subtree = subtree.parent;
|
|
408 |
continue;
|
|
409 |
}
|
|
410 |
subtree = new NodeTree(l, subtree);
|
|
411 |
}
|
|
412 |
newGeom = computesUnion3(listResult);
|
|
386 | 413 |
|
387 |
try {
|
|
388 |
addFeatureToOutput(newGeom, elem.feat, iFeat);
|
|
389 |
} catch (DataException e) {
|
|
390 |
logger.info("Imposible insertar en la tabla", e);
|
|
391 |
} catch (CreateGeometryException e) {
|
|
392 |
logger.info("Error a?adiendo geometr?a", e);
|
|
393 |
}
|
|
394 |
}
|
|
414 |
try {
|
|
415 |
addFeatureToOutput(newGeom, elem.feat, iFeat);
|
|
416 |
} catch (DataException e) {
|
|
417 |
logger.info("Imposible insertar en la tabla", e);
|
|
418 |
} catch (CreateGeometryException e) {
|
|
419 |
logger.info("Error a?adiendo geometr?a", e);
|
|
420 |
}
|
|
421 |
}
|
|
395 | 422 |
|
396 |
}
|
|
423 |
}
|
|
397 | 424 |
|
398 |
/** |
|
399 |
* Adds a feature to the output |
|
400 |
* @param newGeom |
|
401 |
* @param feat |
|
402 |
* @param newFeatID |
|
403 |
* @throws DataException |
|
404 |
* @throws CreateGeometryException |
|
405 |
*/ |
|
406 |
private void addFeatureToOutput(org.gvsig.fmap.geom.Geometry newGeom, |
|
407 |
Feature feat, |
|
408 |
int newFeatID) throws DataException, CreateGeometryException { |
|
409 |
//Si hay una tabla aparte esta capa solo lleva el id como referencia a la tabla de campos |
|
410 |
if(outFeatStoreTable != null) { |
|
411 |
lastEditFeature = persister.addFeature(newGeom, nameIdField, newFeatID); |
|
412 |
} else { |
|
413 |
//Si no hay tabla se ponen todos los campos de la tabla de entrada |
|
414 |
String[] fieldNames = persister.getFieldNamesWithoutGeom(); |
|
415 |
ArrayList<String> fields = new ArrayList<String>(); |
|
416 |
ArrayList<Object> values = new ArrayList<Object>(); |
|
417 |
fields.add(nameIdField); |
|
418 |
values.add(newFeatID); |
|
419 |
for (int j = 0; j < fieldNames.length; j++) { |
|
420 |
Object obj = feat.get(fieldNames[j]); |
|
421 |
if(obj != null && fieldNames[j].compareTo(nameIdField) != 0) { |
|
422 |
fields.add(fieldNames[j]); |
|
423 |
values.add(obj); |
|
424 |
} |
|
425 |
} |
|
426 |
lastEditFeature = persister.addFeature(newGeom, fields, values); |
|
427 |
} |
|
428 |
} |
|
425 |
/** |
|
426 |
* Adds a feature to the output |
|
427 |
* |
|
428 |
* @param newGeom |
|
429 |
* @param feat |
|
430 |
* @param newFeatID |
|
431 |
* @throws DataException |
|
432 |
* @throws CreateGeometryException |
|
433 |
*/ |
|
434 |
private void addFeatureToOutput(org.gvsig.fmap.geom.Geometry newGeom, |
|
435 |
Feature feat, |
|
436 |
int newFeatID) throws DataException, CreateGeometryException { |
|
437 |
//Si hay una tabla aparte esta capa solo lleva el id como referencia a la tabla de campos |
|
438 |
if (outFeatStoreTable != null) { |
|
439 |
lastEditFeature = persister.addFeature(newGeom, nameIdField, newFeatID); |
|
440 |
} else { |
|
441 |
//Si no hay tabla se ponen todos los campos de la tabla de entrada |
|
442 |
String[] fieldNames = persister.getFieldNamesWithoutGeom(); |
|
443 |
ArrayList<String> fields = new ArrayList<String>(); |
|
444 |
ArrayList<Object> values = new ArrayList<Object>(); |
|
445 |
fields.add(nameIdField); |
|
446 |
values.add(newFeatID); |
|
447 |
for (int j = 0; j < fieldNames.length; j++) { |
|
448 |
Object obj = feat.get(fieldNames[j]); |
|
449 |
if (obj != null && fieldNames[j].compareTo(nameIdField) != 0) { |
|
450 |
fields.add(fieldNames[j]); |
|
451 |
values.add(obj); |
|
452 |
} |
|
453 |
} |
|
454 |
lastEditFeature = persister.addFeature(newGeom, fields, values); |
|
455 |
} |
|
456 |
} |
|
429 | 457 |
|
430 |
/** |
|
431 |
* Adds a feature to the output |
|
432 |
* @param newGeom |
|
433 |
* @param feat |
|
434 |
* @param newFeatID |
|
435 |
* @throws DataException |
|
436 |
* @throws CreateGeometryException |
|
437 |
*/ |
|
438 |
private void addFeatureToOutput(Geometry newGeom, |
|
439 |
Feature feat, |
|
440 |
int newFeatID) throws DataException, CreateGeometryException { |
|
441 |
//Si hay una tabla aparte esta capa solo lleva el id como referencia a la tabla de campos |
|
442 |
if(outFeatStoreTable != null) { |
|
443 |
lastEditFeature = persister.addFeature(newGeom, nameIdField, newFeatID); |
|
444 |
} else { |
|
445 |
//Si no hay tabla se ponen todos los campos de la tabla de entrada |
|
446 |
String[] fieldNames = persister.getFieldNamesWithoutGeom(); |
|
447 |
ArrayList<String> fields = new ArrayList<String>(); |
|
448 |
ArrayList<Object> values = new ArrayList<Object>(); |
|
449 |
fields.add(nameIdField); |
|
450 |
values.add(newFeatID); |
|
451 |
for (int j = 0; j < fieldNames.length; j++) { |
|
452 |
Object obj = feat.get(fieldNames[j]); |
|
453 |
if(obj != null && fieldNames[j].compareTo(nameIdField) != 0) { |
|
454 |
fields.add(fieldNames[j]); |
|
455 |
values.add(obj); |
|
456 |
} |
|
457 |
} |
|
458 |
lastEditFeature = persister.addFeature(newGeom, fields, values); |
|
459 |
} |
|
460 |
} |
|
458 |
/** |
|
459 |
* Adds a feature to the output |
|
460 |
* |
|
461 |
* @param newGeom |
|
462 |
* @param feat |
|
463 |
* @param newFeatID |
|
464 |
* @throws DataException |
|
465 |
* @throws CreateGeometryException |
|
466 |
*/ |
|
467 |
private void addFeatureToOutput(Geometry newGeom, |
|
468 |
Feature feat, |
|
469 |
int newFeatID) throws DataException, CreateGeometryException { |
|
470 |
//Si hay una tabla aparte esta capa solo lleva el id como referencia a la tabla de campos |
|
471 |
if (outFeatStoreTable != null) { |
|
472 |
lastEditFeature = persister.addFeature(newGeom, nameIdField, newFeatID); |
|
473 |
} else { |
|
474 |
//Si no hay tabla se ponen todos los campos de la tabla de entrada |
|
475 |
String[] fieldNames = persister.getFieldNamesWithoutGeom(); |
|
476 |
ArrayList<String> fields = new ArrayList<String>(); |
|
477 |
ArrayList<Object> values = new ArrayList<Object>(); |
|
478 |
fields.add(nameIdField); |
|
479 |
values.add(newFeatID); |
|
480 |
for (int j = 0; j < fieldNames.length; j++) { |
|
481 |
Object obj = feat.get(fieldNames[j]); |
|
482 |
if (obj != null && fieldNames[j].compareTo(nameIdField) != 0) { |
|
483 |
fields.add(fieldNames[j]); |
|
484 |
values.add(obj); |
|
485 |
} |
|
486 |
} |
|
487 |
lastEditFeature = persister.addFeature(newGeom, fields, values); |
|
488 |
} |
|
489 |
} |
|
461 | 490 |
|
462 |
/** |
|
463 |
* Insert in the output table a new feature with the fields of the input feature. Moreover |
|
464 |
* exists a field that is an identifier which is a reference to the fusion layer. |
|
465 |
* @param outFeatureStoreTable |
|
466 |
* @param f |
|
467 |
* @throws DataException |
|
468 |
*/ |
|
469 |
private void insertInTable(FeatureStore outFeatureStoreTable, Feature f, int reference) throws DataException { |
|
470 |
if(outFeatureStoreTable == null) |
|
471 |
return; |
|
472 |
EditableFeature edFeat = outFeatureStoreTable.createNewFeature(); |
|
473 |
edFeat.set(nameIdField, reference); |
|
474 |
FeatureAttributeDescriptor[] attrList = f.getType().getAttributeDescriptors(); |
|
475 |
for (int j = 0; j < attrList.length; j++) { |
|
476 |
if(attrList[j].getName().compareTo("GEOMETRY") != 0) { |
|
477 |
edFeat.set(attrList[j].getName(), f.get(attrList[j].getName())); |
|
478 |
} |
|
479 |
} |
|
480 |
outFeatureStoreTable.insert(edFeat); |
|
481 |
} |
|
491 |
/** |
|
492 |
* Insert in the output table a new feature with the fields of the input |
|
493 |
* feature. Moreover exists a field that is an identifier which is a |
|
494 |
* reference to the fusion layer. |
|
495 |
* |
|
496 |
* @param outFeatureStoreTable |
|
497 |
* @param f |
|
498 |
* @throws DataException |
|
499 |
*/ |
|
500 |
private void insertInTable(FeatureStore outFeatureStoreTable, Feature f, int reference) throws DataException { |
|
501 |
if (outFeatureStoreTable == null) { |
|
502 |
return; |
|
503 |
} |
|
504 |
EditableFeature edFeat = outFeatureStoreTable.createNewFeature(); |
|
505 |
edFeat.set(nameIdField, reference); |
|
506 |
FeatureAttributeDescriptor[] attrList = f.getType().getAttributeDescriptors(); |
|
507 |
for (int j = 0; j < attrList.length; j++) { |
|
508 |
if (attrList[j].getName().compareTo("GEOMETRY") != 0) { |
|
509 |
edFeat.set(attrList[j].getName(), f.get(attrList[j].getName())); |
|
510 |
} |
|
511 |
} |
|
512 |
outFeatureStoreTable.insert(edFeat); |
|
513 |
} |
|
482 | 514 |
|
483 | 515 |
} |
484 |
|
Also available in: Unified diff