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