gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.base / src / main / java / org / gvsig / sextante / app / algorithm / base / core / ScalableUnionOperation.java @ 28
History | View | Annotate | Download (4.43 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2010 Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
*/
|
21 |
package org.gvsig.sextante.app.algorithm.base.core; |
22 |
|
23 |
import java.util.Iterator; |
24 |
import java.util.List; |
25 |
|
26 |
import org.gvsig.fmap.dal.exception.DataException; |
27 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
28 |
import org.gvsig.fmap.dal.feature.Feature; |
29 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
30 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
31 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
32 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
33 |
import org.gvsig.fmap.geom.util.Converter; |
34 |
import org.gvsig.sextante.app.algorithm.base.util.JTSFacade; |
35 |
import org.gvsig.sextante.app.algorithm.base.visitor.exception.VisitorException; |
36 |
import org.gvsig.sextante.app.extension.core.gvVectorLayer; |
37 |
|
38 |
import com.vividsolutions.jts.geom.Geometry; |
39 |
|
40 |
import es.unex.sextante.core.Sextante; |
41 |
import es.unex.sextante.dataObjects.IVectorLayer; |
42 |
import es.unex.sextante.exceptions.NullParameterValueException; |
43 |
import es.unex.sextante.exceptions.WrongParameterIDException; |
44 |
import es.unex.sextante.exceptions.WrongParameterTypeException; |
45 |
|
46 |
/**
|
47 |
* Union operation between two geometries.
|
48 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
49 |
*/
|
50 |
public class ScalableUnionOperation { |
51 |
//geometry type of the layer whose features we are going to fussion
|
52 |
//(polygon features are optimized in jts with buffer(0) trick, the
|
53 |
//nor the rest
|
54 |
private Geometry geometry = null; |
55 |
|
56 |
/**
|
57 |
* Returns FMap convex hull geometry.
|
58 |
* @return
|
59 |
*/
|
60 |
public org.gvsig.fmap.geom.Geometry getGeometry() {
|
61 |
try {
|
62 |
return Converter.jtsToGeometry(geometry);
|
63 |
} catch (CreateGeometryException e) {
|
64 |
return null; |
65 |
} |
66 |
} |
67 |
|
68 |
public org.gvsig.fmap.geom.Geometry invoke(org.gvsig.fmap.geom.Geometry g, int geometryType) { |
69 |
if(g == null) |
70 |
return null; |
71 |
|
72 |
if(g.getGeometryType().getType() != TYPES.SURFACE)
|
73 |
return null; |
74 |
|
75 |
Geometry actualGeometry = Converter.geometryToJts(g); |
76 |
if(geometry == null){ |
77 |
geometry = actualGeometry; |
78 |
}else{
|
79 |
Geometry[] geoms = new Geometry[2]; |
80 |
geoms[0] = geometry;
|
81 |
geoms[1] = actualGeometry;
|
82 |
|
83 |
geometry = JTSFacade.union(geoms, geometryType); |
84 |
} |
85 |
return getGeometry();
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* Computes union of all geometries of the clipping layer. The input resources won't
|
90 |
* be released.
|
91 |
*
|
92 |
* @return one geometry
|
93 |
* @throws NullParameterValueException
|
94 |
* @throws WrongParameterIDException
|
95 |
* @throws WrongParameterTypeException
|
96 |
* @throws com.iver.cit.gvsig.fmap.DriverException
|
97 |
* @throws ReadDriverException
|
98 |
* @throws VisitorException
|
99 |
* @throws ExpansionFileReadException
|
100 |
*/
|
101 |
@SuppressWarnings("unchecked") |
102 |
public static org.gvsig.fmap.geom.Geometry joinLayerGeometries(IVectorLayer input) throws WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
103 |
ScalableUnionOperation operation = new ScalableUnionOperation();
|
104 |
|
105 |
FeatureStore store = null;
|
106 |
|
107 |
if(input instanceof gvVectorLayer) |
108 |
store = ((gvVectorLayer)input).getFeatureStore(); |
109 |
else
|
110 |
return null; |
111 |
|
112 |
FeatureSet features = null;
|
113 |
try {
|
114 |
features = store.getFeatureSet(); |
115 |
DisposableIterator it = features.iterator(); |
116 |
while( it.hasNext() ) {
|
117 |
Feature feature = (Feature)it.next(); |
118 |
List geomList = feature.getGeometries();
|
119 |
if(geomList == null) { |
120 |
org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry(); |
121 |
if(geom != null) |
122 |
operation.invoke(geom, geom.getType()); |
123 |
continue;
|
124 |
} |
125 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
126 |
while(itGeom.hasNext()) {
|
127 |
org.gvsig.fmap.geom.Geometry g = itGeom.next(); |
128 |
operation.invoke(g, g.getType()); |
129 |
} |
130 |
} |
131 |
return operation.getGeometry();
|
132 |
} catch (DataException e) {
|
133 |
Sextante.addErrorToLog(e); |
134 |
} |
135 |
return null; |
136 |
} |
137 |
} |
138 |
|