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 @ 560
History | View | Annotate | Download (6.08 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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.geoprocess.algorithm.convexhull; |
25 |
|
26 |
import java.util.Iterator; |
27 |
import java.util.List; |
28 |
|
29 |
import org.gvsig.fmap.dal.exception.DataException; |
30 |
import org.gvsig.fmap.dal.feature.Feature; |
31 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
33 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
35 |
import org.gvsig.fmap.geom.Geometry; |
36 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
37 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
38 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
39 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
40 |
import org.gvsig.tools.dispose.DisposableIterator; |
41 |
import org.slf4j.Logger; |
42 |
import org.slf4j.LoggerFactory; |
43 |
|
44 |
import es.unex.sextante.core.Sextante; |
45 |
import es.unex.sextante.dataObjects.IVectorLayer; |
46 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
47 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
48 |
import es.unex.sextante.outputs.OutputVectorLayer; |
49 |
|
50 |
/**
|
51 |
* Convex Hull Algorithm
|
52 |
*
|
53 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
54 |
*/
|
55 |
public class ConvexHullAlgorithm extends AbstractSextanteGeoProcess { |
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 |
ConvexHullOperation convexHullOperation = new ConvexHullOperation();
|
101 |
|
102 |
FeatureSet features = null;
|
103 |
try {
|
104 |
DisposableIterator it = null;
|
105 |
if (selectedGeom) {
|
106 |
features = store.getFeatureSet(); |
107 |
FeatureSelection ds = store.getFeatureSelection(); |
108 |
it = ds.iterator(); |
109 |
} else {
|
110 |
FeatureQuery query = getQueryFromAnalysisExtent(m_AnalysisExtent, store); |
111 |
features = store.getFeatureSet(query); |
112 |
it = features.iterator(); |
113 |
} |
114 |
|
115 |
int numberOfFeatures = (int) features.getSize(); |
116 |
int iCount = 0; |
117 |
|
118 |
while (it.hasNext()) {
|
119 |
Feature feature = (Feature) it.next(); |
120 |
List geomList = feature.getGeometries();
|
121 |
setProgress(iCount, numberOfFeatures); |
122 |
iCount++; |
123 |
if (geomList == null) { |
124 |
Geometry geom = feature.getDefaultGeometry(); |
125 |
if (geom != null) |
126 |
convexHullOperation.invoke(geom); |
127 |
continue;
|
128 |
} |
129 |
Iterator<Geometry> itGeom = geomList.iterator();
|
130 |
while (itGeom.hasNext()) {
|
131 |
Geometry g = itGeom.next(); |
132 |
convexHullOperation.invoke(g); |
133 |
} |
134 |
} |
135 |
Geometry g = convexHullOperation.getGeometry(); |
136 |
if (g == null) |
137 |
return false; |
138 |
String[] sNames = { "ID" }; |
139 |
Class[] types = { Integer.class }; |
140 |
IVectorLayer output = |
141 |
getNewVectorLayer(RESULT, getTranslation("ConvexHull"),
|
142 |
OutputVectorLayer.SHAPE_TYPE_POLYGON, types, sNames); |
143 |
|
144 |
com.vividsolutions.jts.geom.Geometry jtsGeom = |
145 |
GeometryUtil.geomToJTS(g); |
146 |
|
147 |
output.addFeature(jtsGeom, new Object[] { new Integer(0) }); |
148 |
it.dispose(); |
149 |
} catch (DataException e) {
|
150 |
log.error("", e);
|
151 |
} catch (CreateEnvelopeException e) {
|
152 |
log.error("Error creating envelope", e);
|
153 |
} |
154 |
return !m_Task.isCanceled();
|
155 |
} |
156 |
} |