gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.dissolve / src / main / java / org / gvsig / geoprocess / algorithm / dissolve / DissolveOperation.java @ 237
History | View | Annotate | Download (6.8 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007,2012 gvSIG Association.
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*/
|
21 |
/*
|
22 |
|
23 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
24 |
*
|
25 |
* Copyright (C) 2010 Generalitat Valenciana.
|
26 |
*
|
27 |
* This program is free software; you can redistribute it and/or
|
28 |
* modify it under the terms of the GNU General Public License
|
29 |
* as published by the Free Software Foundation; either version 2
|
30 |
* of the License, or (at your option) any later version.
|
31 |
*
|
32 |
* This program is distributed in the hope that it will be useful,
|
33 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35 |
* GNU General Public License for more details.
|
36 |
*
|
37 |
* You should have received a copy of the GNU General Public License
|
38 |
* along with this program; if not, write to the Free Software
|
39 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
40 |
*/
|
41 |
|
42 |
package org.gvsig.geoprocess.algorithm.dissolve; |
43 |
|
44 |
import java.util.ArrayList; |
45 |
import java.util.Iterator; |
46 |
import java.util.List; |
47 |
|
48 |
import com.vividsolutions.jts.geom.Geometry; |
49 |
|
50 |
import es.unex.sextante.core.Sextante; |
51 |
|
52 |
import org.gvsig.fmap.dal.exception.DataException; |
53 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
54 |
import org.gvsig.fmap.dal.feature.Feature; |
55 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
56 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
57 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
58 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
59 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
60 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
61 |
import org.gvsig.tools.dispose.DisposableIterator; |
62 |
/**
|
63 |
* Dissolve operation
|
64 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
65 |
*/
|
66 |
public class DissolveOperation extends GeometryOperation { |
67 |
private EditableFeature lastEditFeature = null; |
68 |
private ArrayList<Geometry> geometries = new ArrayList<Geometry>(); |
69 |
private IDissolveRule criteria = null; |
70 |
private AtomicDissolveOperation atomicOperation = new AtomicDissolveOperation(); |
71 |
/**
|
72 |
* Cada elemento representa una feature del vectorial de entrada. Cuando se hace un dissolve de
|
73 |
* esa feature con otra su posici?n en el array se pone a false para no volver a procesarla.
|
74 |
*/
|
75 |
private boolean[] analizedFeats = null; |
76 |
|
77 |
public DissolveOperation(IDissolveRule criteria) {
|
78 |
this.criteria = criteria;
|
79 |
} |
80 |
|
81 |
/*
|
82 |
* (non-Javadoc)
|
83 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
84 |
*/
|
85 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
|
86 |
if(g == null) |
87 |
return lastEditFeature;
|
88 |
|
89 |
atomicOperation.setCriteria(criteria); |
90 |
atomicOperation.setGeometryList(geometries); |
91 |
atomicOperation.setFeature(feature); |
92 |
atomicOperation.setFeatureStore(persister.getOutputFeatureStore(), null);
|
93 |
atomicOperation.setAnalizedFeatureList(analizedFeats); |
94 |
atomicOperation.setFeatureStore(inFeatureStore); |
95 |
|
96 |
try {
|
97 |
atomicOperation.computesOperation(selectedGeom); |
98 |
Feature feat = (Feature)atomicOperation.getResult(); |
99 |
lastEditFeature = persister.addFeature(feat, feat.getDefaultGeometry()); |
100 |
} catch (DataException e) {
|
101 |
Sextante.addErrorToLog(e); |
102 |
} catch (CreateGeometryException e) {
|
103 |
Sextante.addErrorToLog(e); |
104 |
} |
105 |
|
106 |
return lastEditFeature;
|
107 |
} |
108 |
|
109 |
/*
|
110 |
* (non-Javadoc)
|
111 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
112 |
*/
|
113 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
114 |
if(g == null) |
115 |
return;
|
116 |
} |
117 |
|
118 |
/*
|
119 |
* (non-Javadoc)
|
120 |
* @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
|
121 |
*/
|
122 |
public Object getResult() { |
123 |
return lastEditFeature;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Computes a complete operation over the input FeatureStore. The result of this operation
|
128 |
* is stored in the output FeatureStore.
|
129 |
* @param inFeatStore
|
130 |
* Input FeatureStore
|
131 |
* @param outFeatStore
|
132 |
* Output FeatureStore
|
133 |
* @param attrNames
|
134 |
* List of attributes to build the output feature store
|
135 |
* @param selectedGeom
|
136 |
* If it is true only the selected geometries will be processed
|
137 |
* @throws DataException
|
138 |
*/
|
139 |
@SuppressWarnings("unchecked") |
140 |
public void computesGeometryOperation(FeatureStore inFeatStore, |
141 |
FeatureStore outFeatStore, |
142 |
String[] attrNames, |
143 |
boolean selectedGeom,
|
144 |
boolean closeOutStore) throws DataException { |
145 |
this.inFeatureStore = inFeatStore;
|
146 |
this.selectedGeom = selectedGeom;
|
147 |
FeatureSet featuresSet = null;
|
148 |
featuresSet = inFeatStore.getFeatureSet(); |
149 |
|
150 |
setFeatureStore(outFeatStore, attrNames); |
151 |
DisposableIterator it = null;
|
152 |
|
153 |
if(selectedGeom) {
|
154 |
FeatureSelection ds = inFeatStore.getFeatureSelection(); |
155 |
it = ds.iterator(); |
156 |
numberOfFeatures = (int) ds.getSelectedCount();
|
157 |
} else {
|
158 |
it = featuresSet.iterator(); |
159 |
numberOfFeatures = (int)featuresSet.getSize();
|
160 |
} |
161 |
|
162 |
if(progressModel != null) |
163 |
progressModel.setTotalNumberOfSteps(numberOfFeatures); |
164 |
|
165 |
analizedFeats = new boolean[numberOfFeatures]; |
166 |
|
167 |
int iCount = 0; |
168 |
int featAnalized = 0; |
169 |
while( it.hasNext() ) {
|
170 |
Feature feature = (Feature)it.next(); |
171 |
|
172 |
if(analizedFeats[featAnalized]) {
|
173 |
featAnalized ++; |
174 |
continue;
|
175 |
} |
176 |
|
177 |
analizedFeats[featAnalized] = true;
|
178 |
List geomList = feature.getGeometries();
|
179 |
geometries.clear(); |
180 |
|
181 |
if(progressModel != null) |
182 |
progressModel.setProgress(iCount); |
183 |
iCount ++; |
184 |
|
185 |
if(geomList == null) { |
186 |
org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry(); |
187 |
geometries.add(GeometryUtil.geomToJTS(geom)); |
188 |
invoke(geom, feature); |
189 |
featAnalized ++; |
190 |
continue;
|
191 |
} |
192 |
|
193 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
194 |
while(itGeom.hasNext()) {
|
195 |
org.gvsig.fmap.geom.Geometry g = itGeom.next(); |
196 |
geometries.add(GeometryUtil.geomToJTS(g)); |
197 |
invoke(g, feature); |
198 |
} |
199 |
|
200 |
featAnalized ++; |
201 |
} |
202 |
|
203 |
if(closeOutStore && persister != null) |
204 |
persister.end(); |
205 |
} |
206 |
} |
207 |
|