gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.clip / src / main / java / org / gvsig / geoprocess / algorithm / clip / ClipAlgorithm.java @ 335
History | View | Annotate | Download (6.54 KB)
1 | 237 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 175 | cordinyana | *
|
4 | 245 | cordinyana | * Copyright (C) 2007-2012 gvSIG Association.
|
5 | 175 | cordinyana | *
|
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 | 237 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 245 | cordinyana | *
|
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 | 175 | cordinyana | */
|
24 | package org.gvsig.geoprocess.algorithm.clip; |
||
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.algorithm.base.core.ScalableUnionOperation; |
||
31 | 218 | cordinyana | import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
32 | 225 | cordinyana | import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
33 | 175 | cordinyana | |
34 | 271 | nbrodin | import es.unex.sextante.core.Sextante; |
35 | import es.unex.sextante.dataObjects.IVectorLayer; |
||
36 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
37 | import es.unex.sextante.exceptions.RepeatedParameterNameException; |
||
38 | import es.unex.sextante.outputs.OutputVectorLayer; |
||
39 | |||
40 | 175 | cordinyana | /**
|
41 | * Clip algorithm
|
||
42 | * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
||
43 | */
|
||
44 | 191 | cordinyana | public class ClipAlgorithm extends AbstractSextanteGeoProcess { |
45 | |||
46 | 292 | nbrodin | public static final String RESULT_POL = "RESULT_POL"; |
47 | public static final String RESULT_LIN = "RESULT_LIN"; |
||
48 | public static final String RESULT_POINT = "RESULT_POINT"; |
||
49 | public static final String LAYER = "LAYER"; |
||
50 | public static final String CLIP = "CLIP"; |
||
51 | public static final String CHECK = "CHECK"; |
||
52 | 175 | cordinyana | |
53 | /*
|
||
54 | * (non-Javadoc)
|
||
55 | * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
||
56 | */
|
||
57 | public void defineCharacteristics(){ |
||
58 | 244 | cordinyana | setName(getTranslation("Clip"));
|
59 | setGroup(getTranslation("basic_vect_algorithms"));
|
||
60 | 175 | cordinyana | // setGeneratesUserDefinedRasterOutput(false);
|
61 | |||
62 | try {
|
||
63 | m_Parameters.addInputVectorLayer(LAYER, |
||
64 | 244 | cordinyana | getTranslation("Input_layer"),
|
65 | 175 | cordinyana | IVectorLayer.SHAPE_TYPE_WRONG, |
66 | true);
|
||
67 | m_Parameters.addInputVectorLayer(CLIP, |
||
68 | 244 | cordinyana | getTranslation("Clip_layer"),
|
69 | 253 | nbrodin | IVectorLayer.SHAPE_TYPE_POLYGON, |
70 | 175 | cordinyana | true);
|
71 | 244 | cordinyana | m_Parameters.addBoolean(CHECK, |
72 | 291 | jldominguez | getTranslation("Selected_geometries_input_layer"), false); |
73 | 175 | cordinyana | } catch (RepeatedParameterNameException e) {
|
74 | Sextante.addErrorToLog(e); |
||
75 | } |
||
76 | 292 | nbrodin | //addOutputVectorLayer(RESULT, getTranslation("Clip"),
|
77 | // OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
|
||
78 | |||
79 | addOutputVectorLayer(RESULT_POL, getTranslation("Clip_polygon"),
|
||
80 | OutputVectorLayer.SHAPE_TYPE_POLYGON); |
||
81 | addOutputVectorLayer(RESULT_LIN, getTranslation("Clip_line"),
|
||
82 | OutputVectorLayer.SHAPE_TYPE_LINE); |
||
83 | addOutputVectorLayer(RESULT_POINT, getTranslation("Clip_point"),
|
||
84 | OutputVectorLayer.SHAPE_TYPE_POINT); |
||
85 | 175 | cordinyana | } |
86 | |||
87 | /*
|
||
88 | * (non-Javadoc)
|
||
89 | * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
||
90 | */
|
||
91 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
92 | 292 | nbrodin | if(existsOutPutFile(ClipAlgorithm.RESULT_POL, 0) || |
93 | existsOutPutFile(ClipAlgorithm.RESULT_LIN, 0) ||
|
||
94 | existsOutPutFile(ClipAlgorithm.RESULT_POINT, 0)) {
|
||
95 | 271 | nbrodin | throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
96 | } |
||
97 | 175 | cordinyana | org.gvsig.fmap.geom.Geometry clippingGeometry = null;
|
98 | IVectorLayer clip = m_Parameters.getParameterValueAsVectorLayer(CLIP); |
||
99 | IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
||
100 | boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
|
||
101 | |||
102 | try {
|
||
103 | 335 | nbrodin | clippingGeometry = ScalableUnionOperation.joinLayerGeometries(clip, false);
|
104 | 175 | cordinyana | } catch (Exception e) { |
105 | Sextante.addErrorToLog(e); |
||
106 | return false; |
||
107 | } |
||
108 | |||
109 | FeatureStore storeLayer = null;
|
||
110 | 225 | cordinyana | if(layer instanceof FlyrVectIVectorLayer && clippingGeometry != null) |
111 | storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore(); |
||
112 | 175 | cordinyana | else
|
113 | return false; |
||
114 | |||
115 | try {
|
||
116 | FeatureSet features = null;
|
||
117 | features = storeLayer.getFeatureSet(); |
||
118 | FeatureType featureType = features.getDefaultFeatureType(); |
||
119 | |||
120 | 292 | nbrodin | if(isPolygon(storeLayer) || isUndefined(storeLayer)) {
|
121 | FeatureStore outFeatStore = |
||
122 | buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON, |
||
123 | getTranslation("Clip_polygon"), RESULT_POL);
|
||
124 | |||
125 | ClipOperation operation = new ClipOperation(clippingGeometry, this); |
||
126 | operation.setTaskStatus(getStatus()); |
||
127 | 335 | nbrodin | operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, |
128 | selectedGeom, false, true); |
||
129 | 317 | nbrodin | } else {
|
130 | buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON, |
||
131 | getTranslation("Null_polygon"), RESULT_POL);
|
||
132 | 292 | nbrodin | } |
133 | |||
134 | if(isLine(storeLayer) || isUndefined(storeLayer)) {
|
||
135 | FeatureStore outFeatStore = |
||
136 | buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE, |
||
137 | getTranslation("Clip_line"), RESULT_LIN);
|
||
138 | |||
139 | ClipOperation operation = new ClipOperation(clippingGeometry, this); |
||
140 | operation.setTaskStatus(getStatus()); |
||
141 | 335 | nbrodin | operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, |
142 | selectedGeom, false, true); |
||
143 | 317 | nbrodin | } else {
|
144 | buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE, |
||
145 | getTranslation("Null_line"), RESULT_LIN);
|
||
146 | 292 | nbrodin | } |
147 | |||
148 | if(isPoint(storeLayer) || isUndefined(storeLayer)) {
|
||
149 | FeatureStore outFeatStore = |
||
150 | buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POINT, |
||
151 | getTranslation("Clip_point"), RESULT_POINT);
|
||
152 | |||
153 | ClipOperation operation = new ClipOperation(clippingGeometry, this); |
||
154 | operation.setTaskStatus(getStatus()); |
||
155 | 335 | nbrodin | operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, |
156 | selectedGeom, false, true); |
||
157 | 317 | nbrodin | } else {
|
158 | buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POINT, |
||
159 | getTranslation("Null_point"), RESULT_POINT);
|
||
160 | 292 | nbrodin | } |
161 | 175 | cordinyana | } catch (DataException e) {
|
162 | Sextante.addErrorToLog(e); |
||
163 | return false; |
||
164 | } |
||
165 | |||
166 | return true; |
||
167 | } |
||
168 | |||
169 | } |