gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.merge / src / main / java / org / gvsig / geoprocess / algorithm / merge / MergeAlgorithm.java @ 1261
History | View | Annotate | Download (8.13 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.merge; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
|
28 |
import javax.swing.JOptionPane; |
29 |
|
30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import org.gvsig.fmap.dal.feature.FeatureType; |
34 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
35 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
36 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
37 |
|
38 |
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput; |
39 |
import es.unex.sextante.core.Sextante; |
40 |
import es.unex.sextante.dataObjects.IVectorLayer; |
41 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
42 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
43 |
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel; |
44 |
import es.unex.sextante.outputs.OutputVectorLayer; |
45 |
|
46 |
/**
|
47 |
* Merge algorithm
|
48 |
*
|
49 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
50 |
*/
|
51 |
public class MergeAlgorithm 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 LAYERS = "LAYERS"; |
57 |
public static final String FIELDLAYER = "LAYER"; |
58 |
|
59 |
/*
|
60 |
* (non-Javadoc)
|
61 |
* @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
62 |
*/
|
63 |
public void defineCharacteristics() { |
64 |
setName(getTranslation("Merge"));
|
65 |
setGroup(getTranslation("basic_vect_algorithms"));
|
66 |
// setGeneratesUserDefinedRasterOutput(false);
|
67 |
|
68 |
try {
|
69 |
m_Parameters.addMultipleInput(LAYERS, |
70 |
getTranslation("Input_layers"),
|
71 |
AdditionalInfoMultipleInput.DATA_TYPE_VECTOR_ANY, |
72 |
true);
|
73 |
m_Parameters.addInputVectorLayer(FIELDLAYER, |
74 |
getTranslation("Fields"),
|
75 |
IVectorLayer.SHAPE_TYPE_WRONG, |
76 |
true);
|
77 |
|
78 |
addOutputVectorLayer(RESULT_POL, getTranslation("Merge_polygon"),
|
79 |
OutputVectorLayer.SHAPE_TYPE_POLYGON); |
80 |
addOutputVectorLayer(RESULT_LINE, getTranslation("Merge_line"),
|
81 |
OutputVectorLayer.SHAPE_TYPE_LINE); |
82 |
addOutputVectorLayer(RESULT_POINT, getTranslation("Merge_point"),
|
83 |
OutputVectorLayer.SHAPE_TYPE_POINT); |
84 |
|
85 |
} catch (RepeatedParameterNameException e) {
|
86 |
Sextante.addErrorToLog(e); |
87 |
} |
88 |
} |
89 |
|
90 |
/*
|
91 |
* (non-Javadoc)
|
92 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
93 |
*/
|
94 |
@SuppressWarnings("unchecked") |
95 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
96 |
if (existsOutPutFile(RESULT_POL, 0) |
97 |
|| existsOutPutFile(RESULT_LINE, 0)
|
98 |
|| existsOutPutFile(RESULT_POINT, 0)) {
|
99 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
100 |
} |
101 |
IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(FIELDLAYER); |
102 |
ArrayList<IVectorLayer> layers = m_Parameters.getParameterValueAsArrayList(LAYERS);
|
103 |
|
104 |
FeatureStore storeLayer = null;
|
105 |
if (layer instanceof FlyrVectIVectorLayer) { |
106 |
storeLayer = ((FlyrVectIVectorLayer) layer).getFeatureStore(); |
107 |
} else {
|
108 |
return false; |
109 |
} |
110 |
|
111 |
try {
|
112 |
//Gets the list of FeatureStore
|
113 |
ArrayList<FeatureStore> featureStoreList = new ArrayList<FeatureStore>(); |
114 |
for (int i = 0; i < layers.size(); i++) { |
115 |
IVectorLayer lyr = layers.get(i); |
116 |
if (lyr instanceof FlyrVectIVectorLayer && layer.getShapeType() == lyr.getShapeType()) { |
117 |
featureStoreList.add(((FlyrVectIVectorLayer) lyr).getFeatureStore()); |
118 |
} else {
|
119 |
JOptionPane.showMessageDialog(null, |
120 |
getTranslation("layers_type_are_different"),
|
121 |
"Error",
|
122 |
JOptionPane.WARNING_MESSAGE);
|
123 |
return false; |
124 |
} |
125 |
} |
126 |
|
127 |
//Builds the output FeatureStore
|
128 |
FeatureSet features = null;
|
129 |
features = storeLayer.getFeatureSet(); |
130 |
FeatureType featureType = features.getDefaultFeatureType(); |
131 |
/* FeatureStore outFeatStore =
|
132 |
buildOutPutStore(featureType, layer.getShapeType(),
|
133 |
getTranslation("Merge"), RESULT);
|
134 |
|
135 |
//Calls the operation
|
136 |
GeometryOperation operation = new MergeOperation(layer, this);
|
137 |
operation.setTaskStatus(getStatus());
|
138 |
operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, false, true);
|
139 |
*/
|
140 |
GeometryOperation operation = new MergeOperation(layer, this); |
141 |
operation.setTaskStatus(getStatus()); |
142 |
|
143 |
if (isPolygon(storeLayer) || isUndefined(storeLayer)) {
|
144 |
FeatureStore outFeatStore |
145 |
= buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON, |
146 |
getTranslation("Merge_polygon"), RESULT_POL);
|
147 |
|
148 |
operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, |
149 |
false, false, true); |
150 |
} else {
|
151 |
buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_POLYGON, |
152 |
getTranslation("Null_polygon"), RESULT_POL);
|
153 |
} |
154 |
|
155 |
if (isLine(storeLayer) || isUndefined(storeLayer)) {
|
156 |
FeatureStore outFeatStore |
157 |
= buildOutPutStore(featureType, IVectorLayer.SHAPE_TYPE_LINE, |
158 |
getTranslation("Merge_line"), RESULT_LINE);
|
159 |
|
160 |
operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, |
161 |
false, false, true); |
162 |
} else {
|
163 |
buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_LINE, |
164 |
getTranslation("Null_line"), RESULT_LINE);
|
165 |
} |
166 |
|
167 |
if (isPoint(storeLayer) || isUndefined(storeLayer)) {
|
168 |
FeatureStore outFeatStore |
169 |
= buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT, |
170 |
getTranslation("Merge_point"), RESULT_POINT);
|
171 |
|
172 |
operation.computesGeometryOperationInAList(featureStoreList, outFeatStore, attrNames, |
173 |
false, false, true); |
174 |
} else {
|
175 |
buildOutPutStore(featureType.getCopy(), IVectorLayer.SHAPE_TYPE_POINT, |
176 |
getTranslation("Null_point"), RESULT_POINT);
|
177 |
} |
178 |
|
179 |
} catch (DataException e) {
|
180 |
Sextante.addErrorToLog(e); |
181 |
return false; |
182 |
} |
183 |
|
184 |
if (getTaskMonitor().isCanceled()) {
|
185 |
return false; |
186 |
} |
187 |
|
188 |
return true; |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() { |
193 |
return MergeParametersPanel.class;
|
194 |
} |
195 |
|
196 |
} |