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/DissolveOperation.java

View differences:

DissolveOperation.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;
......
62 60
import com.vividsolutions.jts.geom.Geometry;
63 61

  
64 62
import es.unex.sextante.core.Sextante;
63

  
65 64
/**
66 65
 * Dissolve operation
66
 *
67 67
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
68 68
 */
69 69
public class DissolveOperation extends GeometryOperation {
70
	private EditableFeature                  lastEditFeature   = null;
71
	private ArrayList<Geometry>              geometries        = new ArrayList<Geometry>();
72
	private IDissolveRule                    criteria          = null;
73
	private AtomicDissolveOperation          atomicOperation   = null;
74
	
75
	/**
76
	 * Cada elemento representa una feature del vectorial de entrada. Cuando se hace un dissolve de
77
	 * esa feature con otra su posici?n en el array se pone a false para no volver a procesarla.
78
	 */
70

  
71
    private EditableFeature lastEditFeature = null;
72
    private ArrayList<Geometry> geometries = new ArrayList<Geometry>();
73
    private IDissolveRule criteria = null;
74
    private AtomicDissolveOperation atomicOperation = null;
75

  
76
    /**
77
     * Cada elemento representa una feature del vectorial de entrada. Cuando se
78
     * hace un dissolve de esa feature con otra su posici?n en el array se pone
79
     * a false para no volver a procesarla.
80
     */
79 81
	//private boolean[]                        analizedFeats    = null;
80
	
81
	public DissolveOperation(IDissolveRule criteria, AbstractSextanteGeoProcess p) {
82
		super(p);
83
		this.criteria = criteria;
84
	}
85
	
86
	public AtomicDissolveOperation getAtomicDissolveOperation() {
87
		if(atomicOperation == null)
88
			atomicOperation = new AtomicDissolveOperation(process);
89
		return atomicOperation;
90
	}
82
    public DissolveOperation(IDissolveRule criteria, AbstractSextanteGeoProcess p) {
83
        super(p);
84
        this.criteria = criteria;
85
    }
91 86

  
92
	/*
93
	 * (non-Javadoc)
94
	 * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
95
	 */
96
	public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
97
		if(g == null)
98
			return lastEditFeature;
99
		getAtomicDissolveOperation().setFeature(feature);
100
		try {
101
			getAtomicDissolveOperation().computesOperation(selectedGeomInput);
102
			EditableFeature res = (EditableFeature)getAtomicDissolveOperation().getResult();
103
			lastEditFeature = persister.addFeature(res, res.getDefaultGeometry());				
104
		} catch (DataException e) {
105
			Sextante.addErrorToLog(e);
106
		} catch (CreateGeometryException e) {
107
			Sextante.addErrorToLog(e);
108
		}
109
		
110
		return lastEditFeature;
111
	}
112
	
113
	/*
114
	 * (non-Javadoc)
115
	 * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
116
	 */
117
	public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
118
		if(g == null)
119
			return;
120
	}
121
	
122
	/*
123
	 * (non-Javadoc)
124
	 * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
125
	 */
126
	public Object getResult() {
127
		return lastEditFeature;
128
	}
129
	
130
	/**
131
	 * Computes a complete operation over the input FeatureStore. The result of this operation
132
	 * is stored in the output FeatureStore. 
133
	 * @param inFeatStore
134
	 *        Input FeatureStore
135
	 * @param outFeatStore
136
	 *        Output FeatureStore
137
	 * @param attrNames
138
	 *        List of attributes to build the output feature store
139
	 * @param selectedGeom
140
	 *        If it is true only the selected geometries will be processed
141
	 * @throws DataException
142
	 */
143
	@SuppressWarnings({ "deprecation" })
144
	public void computesGeometryOperation(FeatureStore inFeatStore,
145
									FeatureStore outFeatStore,
146
									String[] attrNames,
147
									boolean selectedGeomInput,
148
									boolean selectedGeomOutput,
149
									boolean closeOutStore) throws DataException {
150
		this.inFeatureStore = inFeatStore;
151
		this.selectedGeomInput = selectedGeomInput;
152
		this.selectedGeomOverlay = selectedGeomOutput;
153
		FeatureSet featuresSet = null;
154
		featuresSet = inFeatStore.getFeatureSet();
155
		
156
		setFeatureStore(outFeatStore, attrNames);
157
		DisposableIterator it = null;
87
    public AtomicDissolveOperation getAtomicDissolveOperation() {
88
        if (atomicOperation == null) {
89
            atomicOperation = new AtomicDissolveOperation(process);
90
        }
91
        return atomicOperation;
92
    }
158 93

  
159
		if(selectedGeomInput) {
94
    /*
95
     * (non-Javadoc)
96
     * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
97
     */
98
    public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
99
        if (g == null) {
100
            return lastEditFeature;
101
        }
102
        getAtomicDissolveOperation().setFeature(feature);
103
        try {
104
            getAtomicDissolveOperation().computesOperation(selectedGeomInput);
105
            EditableFeature res = (EditableFeature) getAtomicDissolveOperation().getResult();
106
            lastEditFeature = persister.addFeature(res, res.getDefaultGeometry());
107
        } catch (DataException e) {
108
            Sextante.addErrorToLog(e);
109
        } catch (CreateGeometryException e) {
110
            Sextante.addErrorToLog(e);
111
        }
112

  
113
        return lastEditFeature;
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
    /*
127
     * (non-Javadoc)
128
     * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
129
     */
130
    public Object getResult() {
131
        return lastEditFeature;
132
    }
133

  
134
    /**
135
     * Computes a complete operation over the input FeatureStore. The result of
136
     * this operation is stored in the output FeatureStore.
137
     *
138
     * @param inFeatStore Input FeatureStore
139
     * @param outFeatStore Output FeatureStore
140
     * @param attrNames List of attributes to build the output feature store
141
     * @param selectedGeom If it is true only the selected geometries will be
142
     * processed
143
     * @throws DataException
144
     */
145
    @SuppressWarnings({"deprecation"})
146
    public void computesGeometryOperation(FeatureStore inFeatStore,
147
            FeatureStore outFeatStore,
148
            String[] attrNames,
149
            boolean selectedGeomInput,
150
            boolean selectedGeomOutput,
151
            boolean closeOutStore) throws DataException {
152
        this.inFeatureStore = inFeatStore;
153
        this.selectedGeomInput = selectedGeomInput;
154
        this.selectedGeomOverlay = selectedGeomOutput;
155
        FeatureSet featuresSet = null;
156
        featuresSet = inFeatStore.getFeatureSet();
157

  
158
        setFeatureStore(outFeatStore, attrNames);
159
        DisposableIterator it = null;
160

  
161
        if (selectedGeomInput) {
160 162
            FeatureSelection ds = inFeatStore.getFeatureSelection();
161 163
            it = ds.iterator();
162 164
            numberOfFeatures = (int) ds.getSelectedCount();
163
		} else {
164
			it = featuresSet.iterator();
165
			numberOfFeatures = (int)featuresSet.getSize();
166
		}
167
		
165
        } else {
166
            it = featuresSet.iterator();
167
            numberOfFeatures = (int) featuresSet.getSize();
168
        }
169

  
168 170
        if (status != null && process != null) {
169 171
            status.setRangeOfValues(0, numberOfFeatures);
170 172
            process.setProgress(0, numberOfFeatures);
171 173
        }
172
		
173
		ArrayList<Feature> featList = new ArrayList<Feature>();
174
		
175
		int iCount = 0;
176
		while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
177
			featList.add((Feature)it.next());
174

  
175
        ArrayList<Feature> featList = new ArrayList<Feature>();
176

  
177
        int iCount = 0;
178
        while (it.hasNext() && !process.getTaskMonitor().isCanceled()) {
179
            featList.add((Feature) it.next());
178 180
            if (status != null && process != null) {
179 181
                status.setCurValue(iCount);
180 182
                process.setProgress(iCount, numberOfFeatures);
181 183
            }
182
			iCount ++;
183
		}
184
		
185
		getAtomicDissolveOperation().setTaskStatus(status);
186
		getAtomicDissolveOperation().setGeoProcess(process, numberOfFeatures);
187
		getAtomicDissolveOperation().setCriteria(criteria);
188
		getAtomicDissolveOperation().setGeometryList(geometries);
189
		getAtomicDissolveOperation().setFeatureStore(persister.getOutputFeatureStore(), null);
190
		getAtomicDissolveOperation().setFeatureList(featList);
191
		
192
		iCount = 0;
193
		while(featList.size() > 0 && !process.getTaskMonitor().isCanceled()) {
194
			Feature feature = featList.remove(0);
184
            iCount++;
185
        }
195 186

  
196
			geometries.clear();
187
        getAtomicDissolveOperation().setTaskStatus(status);
188
        getAtomicDissolveOperation().setGeoProcess(process, numberOfFeatures);
189
        getAtomicDissolveOperation().setCriteria(criteria);
190
        getAtomicDissolveOperation().setGeometryList(geometries);
191
        getAtomicDissolveOperation().setFeatureStore(persister.getOutputFeatureStore(), null);
192
        getAtomicDissolveOperation().setFeatureList(featList);
197 193

  
198
			if (status != null && process != null) {
199
				status.setCurValue(numberOfFeatures - featList.size());
200
				process.setProgress(numberOfFeatures - featList.size(), numberOfFeatures);
201
			}
194
        iCount = 0;
195
        while (featList.size() > 0 && !process.getTaskMonitor().isCanceled()) {
196
            Feature feature = featList.remove(0);
202 197

  
203
			org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
204
			if(!(geom instanceof NullGeometry)) {
205
				geometries.add(GeometryUtil.geomToJTS(geom));
206
				invoke(geom, feature);
207
			}
208
			continue;
198
            geometries.clear();
209 199

  
210
		}
200
            if (status != null && process != null) {
201
                status.setCurValue(numberOfFeatures - featList.size());
202
                process.setProgress(numberOfFeatures - featList.size(), numberOfFeatures);
203
            }
211 204

  
212
		if(closeOutStore && persister != null)
213
			persister.end();
214
	}
215
	
216
	/**
217
	 * Computes a complete operation over the input FeatureStore. The result of this operation
218
	 * is stored in the output FeatureStore. 
219
	 * @param inFeatStore
220
	 *        Input FeatureStore
221
	 * @param outFeatStore
222
	 *        Output FeatureStore
223
	 * @param attrNames
224
	 *        List of attributes to build the output feature store
225
	 * @param selectedGeom
226
	 *        If it is true only the selected geometries will be processed
227
	 * @throws DataException
228
	 */
205
            org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
206
            if (!(geom instanceof NullGeometry)) {
207
                geometries.add(GeometryUtil.geomToJTS(geom));
208
                invoke(geom, feature);
209
            }
210
            continue;
211

  
212
        }
213

  
214
        if (closeOutStore && persister != null) {
215
            persister.end();
216
        }
217
    }
218

  
219
    /**
220
     * Computes a complete operation over the input FeatureStore. The result of
221
     * this operation is stored in the output FeatureStore.
222
     *
223
     * @param inFeatStore Input FeatureStore
224
     * @param outFeatStore Output FeatureStore
225
     * @param attrNames List of attributes to build the output feature store
226
     * @param selectedGeom If it is true only the selected geometries will be
227
     * processed
228
     * @throws DataException
229
     */
229 230
//	public void computesGeometryOperation(FeatureStore inFeatStore,
230 231
//									FeatureStore outFeatStore,
231 232
//									String[] attrNames,
......
302 303
//			persister.end();
303 304
//	}
304 305
}
305

  

Also available in: Unified diff