gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.intersection / src / main / java / org / gvsig / geoprocess / algorithm / intersection / IntersectionAlgorithm.java @ 335
History | View | Annotate | Download (8.02 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.intersection; |
25 |
|
26 |
import org.gvsig.fmap.dal.exception.DataException; |
27 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
28 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
29 |
import org.gvsig.fmap.dal.feature.FeatureType; |
30 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
31 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
32 |
|
33 |
import es.unex.sextante.core.Sextante; |
34 |
import es.unex.sextante.dataObjects.IVectorLayer; |
35 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
36 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
37 |
import es.unex.sextante.outputs.OutputVectorLayer; |
38 |
|
39 |
/**
|
40 |
* Intersection algorithm
|
41 |
* <UL>
|
42 |
* <LI>Pol-Pol: 3 layers (polygon, point, line)</LI>
|
43 |
* <LI>Pol-Line: 2 layers (point, line)</LI>
|
44 |
* <LI>Pol-Point: 1 layer (point)</LI>
|
45 |
* <LI>Line-Point: 1 layer (point)</LI>
|
46 |
* <LI>Line-Line: 2 layers (point, line)</LI>
|
47 |
* <LI>Point-Point: 1 layer (point)</LI>
|
48 |
* </UL>
|
49 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
50 |
*/
|
51 |
public class IntersectionAlgorithm extends AbstractSextanteGeoProcess { |
52 |
|
53 |
public static final String RESULT_POL = "RESULT_POL"; |
54 |
public static final String RESULT_POINT = "RESULT_POINT"; |
55 |
public static final String RESULT_LINE = "RESULT_LINE"; |
56 |
public static final String LAYER = "LAYER"; |
57 |
public static final String INTER = "INTER"; |
58 |
public static final String CHECK = "CHECK"; |
59 |
|
60 |
/*
|
61 |
* (non-Javadoc)
|
62 |
* @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
63 |
*/
|
64 |
public void defineCharacteristics() { |
65 |
setName(getTranslation("Intersection"));
|
66 |
setGroup(getTranslation("basic_vect_algorithms"));
|
67 |
// setGeneratesUserDefinedRasterOutput(false);
|
68 |
|
69 |
try {
|
70 |
m_Parameters.addInputVectorLayer(LAYER, |
71 |
getTranslation("Input_layer"),
|
72 |
IVectorLayer.SHAPE_TYPE_WRONG, |
73 |
true);
|
74 |
m_Parameters.addInputVectorLayer(INTER, |
75 |
getTranslation("Overlays_layer"),
|
76 |
IVectorLayer.SHAPE_TYPE_WRONG, |
77 |
true);
|
78 |
m_Parameters.addBoolean(CHECK, |
79 |
getTranslation("Selected_geometries"), false); |
80 |
} catch (RepeatedParameterNameException e) {
|
81 |
Sextante.addErrorToLog(e); |
82 |
} |
83 |
addOutputVectorLayer(RESULT_POL, getTranslation("Intersection_polygon"),
|
84 |
OutputVectorLayer.SHAPE_TYPE_POLYGON); |
85 |
addOutputVectorLayer(RESULT_LINE, getTranslation("Intersection_line"),
|
86 |
OutputVectorLayer.SHAPE_TYPE_LINE); |
87 |
addOutputVectorLayer(RESULT_POINT, getTranslation("Intersection_point"),
|
88 |
OutputVectorLayer.SHAPE_TYPE_POINT); |
89 |
} |
90 |
|
91 |
/*
|
92 |
* (non-Javadoc)
|
93 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
94 |
*/
|
95 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
96 |
if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) { |
97 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
98 |
} |
99 |
if(existsOutPutFile(IntersectionAlgorithm.RESULT_POINT, 0)) { |
100 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
101 |
} |
102 |
if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) { |
103 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
104 |
} |
105 |
IVectorLayer inter = m_Parameters.getParameterValueAsVectorLayer(INTER); |
106 |
IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
107 |
boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
|
108 |
|
109 |
try {
|
110 |
computesIntersection(layer, inter, layer.getShapeType(), selectedGeom); |
111 |
} catch (DataException e) {
|
112 |
Sextante.addErrorToLog(e); |
113 |
return false; |
114 |
} |
115 |
|
116 |
if(getTaskMonitor().isCanceled())
|
117 |
return false; |
118 |
return true; |
119 |
} |
120 |
|
121 |
/**
|
122 |
* Builds a layer with the intersection between the input layer and the templateGeometry
|
123 |
* @param layer
|
124 |
* Input layer
|
125 |
* @param templateGeometry
|
126 |
* @param shapeType
|
127 |
* Output shape type
|
128 |
* @param selectedGeom
|
129 |
* If it's true only selected geometries will be computed
|
130 |
* @throws GeoAlgorithmExecutionException
|
131 |
*/
|
132 |
private void computesIntersection(IVectorLayer layer, |
133 |
IVectorLayer overlay, |
134 |
int shapeType,
|
135 |
boolean selectedGeom) throws DataException, GeoAlgorithmExecutionException { |
136 |
FeatureStore storeLayer = null;
|
137 |
FeatureStore storeOverlay = null;
|
138 |
if(layer instanceof FlyrVectIVectorLayer && |
139 |
overlay instanceof FlyrVectIVectorLayer) {
|
140 |
storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore(); |
141 |
storeOverlay = ((FlyrVectIVectorLayer)overlay).getFeatureStore(); |
142 |
} else
|
143 |
return;
|
144 |
|
145 |
FeatureSet features1 = null;
|
146 |
features1 = storeLayer.getFeatureSet(); |
147 |
FeatureType featureType1 = features1.getDefaultFeatureType(); |
148 |
|
149 |
FeatureSet features2 = null;
|
150 |
features2 = storeOverlay.getFeatureSet(); |
151 |
FeatureType featureType2 = features2.getDefaultFeatureType(); |
152 |
|
153 |
FeatureStore outFeatStorePol = null;
|
154 |
FeatureStore outFeatStoreLine = null;
|
155 |
FeatureStore outFeatStorePoint = null;
|
156 |
|
157 |
IntersectionOperation operation = new IntersectionOperation(storeOverlay, this); |
158 |
operation.setTaskStatus(getStatus()); |
159 |
|
160 |
//La de puntos se genera siempre
|
161 |
outFeatStorePoint = |
162 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
163 |
IVectorLayer.SHAPE_TYPE_POINT, |
164 |
getTranslation("Intersection_point"), RESULT_POINT);
|
165 |
|
166 |
getStatus().setTitle("Point");
|
167 |
operation.computesGeometryOperation(storeLayer, outFeatStorePoint, attrNames, |
168 |
selectedGeom, false, true); |
169 |
|
170 |
//La de pol?gonos solo si es intersecci?n entre pol?gonos
|
171 |
if (isPolygon(storeLayer) && isPolygon(storeOverlay)) {
|
172 |
outFeatStorePol = |
173 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
174 |
IVectorLayer.SHAPE_TYPE_POLYGON, getTranslation("Intersection_polygon"), RESULT_POL);
|
175 |
getStatus().setTitle("Polygon");
|
176 |
operation.computesGeometryOperation(storeLayer, outFeatStorePol, attrNames, |
177 |
selectedGeom, false, true); |
178 |
} |
179 |
|
180 |
//La capa de l?neas se genera cuando ning?na de las dos es de puntos
|
181 |
if (!isPoint(storeLayer) && !isPoint(storeOverlay)) {
|
182 |
outFeatStoreLine = |
183 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
184 |
IVectorLayer.SHAPE_TYPE_LINE, getTranslation("Intersection_line"), RESULT_LINE);
|
185 |
getStatus().setTitle("Line");
|
186 |
operation.computesGeometryOperation(storeLayer, outFeatStoreLine, attrNames, |
187 |
selectedGeom, false, true); |
188 |
} |
189 |
|
190 |
if(outFeatStorePol == null) { |
191 |
getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"),
|
192 |
OutputVectorLayer.SHAPE_TYPE_POLYGON, new Class[]{Integer.class}, new String[]{""}); |
193 |
} |
194 |
|
195 |
if(outFeatStoreLine == null) { |
196 |
getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"),
|
197 |
OutputVectorLayer.SHAPE_TYPE_LINE, new Class[]{Integer.class}, new String[]{""}); |
198 |
} |
199 |
|
200 |
if(outFeatStorePoint == null) { |
201 |
getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"),
|
202 |
OutputVectorLayer.SHAPE_TYPE_POINT, new Class[]{Integer.class}, new String[]{""}); |
203 |
} |
204 |
} |
205 |
|
206 |
} |