gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.convexhull / src / main / java / org / gvsig / geoprocess / algorithm / convexhull / ConvexHullAlgorithm.java @ 741
History | View | Annotate | Download (6.2 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 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 |
*
|
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 |
*
|
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.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.geoprocess.algorithm.convexhull; |
24 |
|
25 |
import java.util.Iterator; |
26 |
import java.util.List; |
27 |
|
28 |
import org.gvsig.fmap.dal.exception.DataException; |
29 |
import org.gvsig.fmap.dal.feature.Feature; |
30 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
31 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
36 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
37 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
38 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
39 |
import org.gvsig.tools.dispose.DisposableIterator; |
40 |
import org.slf4j.Logger; |
41 |
import org.slf4j.LoggerFactory; |
42 |
|
43 |
import es.unex.sextante.core.Sextante; |
44 |
import es.unex.sextante.dataObjects.IVectorLayer; |
45 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
46 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
47 |
import es.unex.sextante.outputs.OutputVectorLayer; |
48 |
|
49 |
/**
|
50 |
* Convex Hull Algorithm
|
51 |
*
|
52 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
53 |
*/
|
54 |
public class ConvexHullAlgorithm extends AbstractSextanteGeoProcess { |
55 |
|
56 |
private static final Logger log = LoggerFactory.getLogger(ConvexHullAlgorithm.class); |
57 |
public static final String LAYER = "LAYER"; |
58 |
public static final String RESULT = "RESULT"; |
59 |
public static final String CHECK = "CHECK"; |
60 |
|
61 |
public void defineCharacteristics() { |
62 |
setName(getTranslation("ConvexHull"));
|
63 |
setGroup(getTranslation("basic_vect_algorithms"));
|
64 |
// setGeneratesUserDefinedRasterOutput(false);
|
65 |
try {
|
66 |
m_Parameters.addInputVectorLayer(LAYER, |
67 |
getTranslation("Input_layer"), IVectorLayer.SHAPE_TYPE_WRONG,
|
68 |
true);
|
69 |
addOutputVectorLayer(RESULT, getTranslation("ConvexHull"),
|
70 |
OutputVectorLayer.SHAPE_TYPE_POLYGON); |
71 |
m_Parameters.addBoolean(CHECK, |
72 |
getTranslation("Selected_geometries_convex_hull"), false); |
73 |
} catch (RepeatedParameterNameException e) {
|
74 |
Sextante.addErrorToLog(e); |
75 |
} |
76 |
} |
77 |
|
78 |
/*
|
79 |
* (non-Javadoc)
|
80 |
*
|
81 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
82 |
*/
|
83 |
@SuppressWarnings("unchecked") |
84 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
85 |
if (existsOutPutFile(ConvexHullAlgorithm.RESULT, 0)) { |
86 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
87 |
} |
88 |
|
89 |
IVectorLayer input = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
90 |
boolean selectedGeom
|
91 |
= m_Parameters.getParameter(CHECK).getParameterValueAsBoolean(); |
92 |
|
93 |
FeatureStore store = null;
|
94 |
|
95 |
if (input instanceof FlyrVectIVectorLayer) { |
96 |
store = ((FlyrVectIVectorLayer) input).getFeatureStore(); |
97 |
} else {
|
98 |
return false; |
99 |
} |
100 |
|
101 |
ConvexHullOperation convexHullOperation = new ConvexHullOperation();
|
102 |
|
103 |
FeatureSet features = null;
|
104 |
try {
|
105 |
Iterator it = null; |
106 |
if (selectedGeom) {
|
107 |
features = store.getFeatureSet(); |
108 |
FeatureSelection ds = store.getFeatureSelection(); |
109 |
it = ds.iterator(); |
110 |
} else {
|
111 |
FeatureQuery query = getQueryFromAnalysisExtent(m_AnalysisExtent, store); |
112 |
features = store.getFeatureSet(query); |
113 |
it = features.iterator(); |
114 |
} |
115 |
|
116 |
int numberOfFeatures = (int) features.getSize(); |
117 |
int iCount = 0; |
118 |
|
119 |
while (it.hasNext()) {
|
120 |
Feature feature = (Feature) it.next(); |
121 |
List geomList = feature.getGeometries();
|
122 |
setProgress(iCount, numberOfFeatures); |
123 |
iCount++; |
124 |
if (geomList == null) { |
125 |
Geometry geom = feature.getDefaultGeometry(); |
126 |
if (geom != null) { |
127 |
convexHullOperation.invoke(geom); |
128 |
} |
129 |
continue;
|
130 |
} |
131 |
Iterator<Geometry> itGeom = geomList.iterator();
|
132 |
while (itGeom.hasNext()) {
|
133 |
Geometry g = itGeom.next(); |
134 |
convexHullOperation.invoke(g); |
135 |
} |
136 |
} |
137 |
Geometry g = convexHullOperation.getGeometry(); |
138 |
if (g == null) { |
139 |
return false; |
140 |
} |
141 |
String[] sNames = {"ID"}; |
142 |
Class[] types = {Integer.class}; |
143 |
IVectorLayer output |
144 |
= getNewVectorLayer(RESULT, getTranslation("ConvexHull"),
|
145 |
OutputVectorLayer.SHAPE_TYPE_POLYGON, types, sNames); |
146 |
|
147 |
com.vividsolutions.jts.geom.Geometry jtsGeom |
148 |
= GeometryUtil.geomToJTS(g); |
149 |
|
150 |
output.addFeature(jtsGeom, new Object[]{new Integer(0)}); |
151 |
//it.dispose();
|
152 |
} catch (DataException e) {
|
153 |
log.error("", e);
|
154 |
} catch (CreateEnvelopeException e) {
|
155 |
log.error("Error creating envelope", e);
|
156 |
} |
157 |
return !m_Task.isCanceled();
|
158 |
} |
159 |
} |