svn-gvsig-desktop / tags / v1_0_2_RELEASE / extensions / extGeoprocessingExtensions / src / com / iver / cit / gvsig / geoprocess / impl / reproject / fmap / ReprojectGeoprocess.java @ 11432
History | View | Annotate | Download (9.42 KB)
1 | 6166 | azabala | /*
|
---|---|---|---|
2 | * Created on 03-jul-2006
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | *
|
||
6 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | *
|
||
18 | * You should have received a copy of the GNU General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | *
|
||
34 | * or
|
||
35 | *
|
||
36 | * IVER T.I. S.A
|
||
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | *
|
||
41 | * +34 963163400
|
||
42 | * dac@iver.es
|
||
43 | */
|
||
44 | /* CVS MESSAGES:
|
||
45 | *
|
||
46 | * $Id$
|
||
47 | * $Log$
|
||
48 | 8903 | luisw2 | * Revision 1.3.2.1 2006-11-22 07:38:09 luisw2
|
49 | * normalizaci?n de la gesti?n de CRSs
|
||
50 | *
|
||
51 | * Revision 1.3 2006/08/11 17:20:32 azabala
|
||
52 | 6166 | azabala | * *** empty log message ***
|
53 | *
|
||
54 | 6752 | azabala | * Revision 1.2 2006/07/04 16:43:18 azabala
|
55 | * *** empty log message ***
|
||
56 | *
|
||
57 | 6179 | azabala | * Revision 1.1 2006/07/03 20:28:38 azabala
|
58 | * *** empty log message ***
|
||
59 | 6166 | azabala | *
|
60 | 6179 | azabala | *
|
61 | 6166 | azabala | */
|
62 | package com.iver.cit.gvsig.geoprocess.impl.reproject.fmap; |
||
63 | |||
64 | import java.util.Map; |
||
65 | |||
66 | import org.cresques.cts.ICoordTrans; |
||
67 | import org.cresques.cts.IProjection; |
||
68 | import org.cresques.cts.gt2.CoordSys; |
||
69 | |||
70 | 6752 | azabala | |
71 | 6166 | azabala | import com.hardcode.gdbms.engine.data.driver.DriverException; |
72 | import com.hardcode.gdbms.engine.values.Value; |
||
73 | import com.hardcode.gdbms.engine.values.ValueFactory; |
||
74 | import com.iver.andami.PluginServices; |
||
75 | import com.iver.cit.gvsig.fmap.core.IFeature; |
||
76 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
77 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
78 | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
||
79 | import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
||
80 | import com.iver.cit.gvsig.fmap.edition.EditionException; |
||
81 | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
||
82 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
83 | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
||
84 | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
||
85 | import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
||
86 | import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData; |
||
87 | import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
||
88 | import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
||
89 | import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager; |
||
90 | import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
||
91 | import com.iver.cit.gvsig.geoprocess.core.fmap.AbstractGeoprocess; |
||
92 | import com.iver.cit.gvsig.geoprocess.core.fmap.DefinitionUtils; |
||
93 | import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory; |
||
94 | import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2; |
||
95 | import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException; |
||
96 | import com.iver.utiles.swing.threads.AbstractMonitorableTask; |
||
97 | import com.iver.utiles.swing.threads.IMonitorableTask; |
||
98 | |||
99 | public class ReprojectGeoprocess extends AbstractGeoprocess { |
||
100 | |||
101 | /**
|
||
102 | * Schema of the result layer
|
||
103 | */
|
||
104 | |||
105 | private ILayerDefinition resultLayerDefinition;
|
||
106 | |||
107 | /**
|
||
108 | * flag to only clip selection of input layer
|
||
109 | */
|
||
110 | private boolean onlyInputLayerSelection = false; |
||
111 | |||
112 | /**
|
||
113 | * Processes features (writing them)
|
||
114 | */
|
||
115 | FeaturePersisterProcessor2 processor; |
||
116 | |||
117 | /**
|
||
118 | * Target projection for features of the input layer
|
||
119 | */
|
||
120 | private IProjection targetProjection;
|
||
121 | |||
122 | ReprojectTask task = null;
|
||
123 | |||
124 | public ReprojectGeoprocess() {
|
||
125 | super();
|
||
126 | } |
||
127 | |||
128 | public void setFirstLayer(FLyrVect inputLayer){ |
||
129 | this.firstLayer = inputLayer;
|
||
130 | } |
||
131 | |||
132 | public void setParameters(Map params) throws GeoprocessException { |
||
133 | Boolean firstLayerSelection = (Boolean) params |
||
134 | .get("firstlayerselection");
|
||
135 | if (firstLayerSelection != null) |
||
136 | this.onlyInputLayerSelection = firstLayerSelection.booleanValue();
|
||
137 | this.targetProjection = (IProjection) params.get("targetProjection"); |
||
138 | |||
139 | } |
||
140 | |||
141 | public void checkPreconditions() throws GeoprocessException { |
||
142 | if (this.writer == null || this.schemaManager == null) { |
||
143 | throw new GeoprocessException( |
||
144 | "Operacion de reproyecci?n sin especificar capa de resultados");
|
||
145 | } |
||
146 | if (this.targetProjection == null) |
||
147 | throw new GeoprocessException( |
||
148 | "Geoproceso reproyecci?n sin proyecci?n destino");
|
||
149 | } |
||
150 | |||
151 | public void process() throws GeoprocessException { |
||
152 | try {
|
||
153 | createTask().run(); |
||
154 | } catch (Exception e) { |
||
155 | throw new GeoprocessException( |
||
156 | "Error durante ejecuci?n del geoproceso", e);
|
||
157 | } |
||
158 | |||
159 | } |
||
160 | |||
161 | public ILayerDefinition createLayerDefinition() {
|
||
162 | if (resultLayerDefinition == null) { |
||
163 | try {
|
||
164 | resultLayerDefinition = DefinitionUtils |
||
165 | .createLayerDefinition(super.firstLayer);
|
||
166 | } catch (DriverException e) {
|
||
167 | // TODO Auto-generated catch block
|
||
168 | e.printStackTrace(); |
||
169 | } catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
||
170 | // TODO Auto-generated catch block
|
||
171 | e.printStackTrace(); |
||
172 | } |
||
173 | } |
||
174 | return resultLayerDefinition;
|
||
175 | } |
||
176 | |||
177 | class ReprojectVisitor implements FeatureVisitor { |
||
178 | |||
179 | FeaturePersisterProcessor2 processor; |
||
180 | |||
181 | SelectableDataSource recordset; |
||
182 | |||
183 | ILayerDefinition layerDefinition; |
||
184 | |||
185 | FBitSet selection; |
||
186 | |||
187 | ICoordTrans ct; |
||
188 | |||
189 | ReprojectVisitor(ILayerDefinition layerDefinition, |
||
190 | FeaturePersisterProcessor2 processor, FBitSet selection, |
||
191 | ICoordTrans coordTrans) { |
||
192 | |||
193 | this.layerDefinition = layerDefinition;
|
||
194 | this.processor = processor;
|
||
195 | this.selection = selection;
|
||
196 | this.ct = coordTrans;
|
||
197 | } |
||
198 | |||
199 | public void visit(IGeometry g, int index) throws VisitException { |
||
200 | if (g == null) |
||
201 | return;
|
||
202 | if (selection != null) { |
||
203 | if (!selection.get(index))
|
||
204 | return;
|
||
205 | }// if
|
||
206 | g.reProject(ct); |
||
207 | IFeature feature = null;
|
||
208 | try {
|
||
209 | feature = createFeature(g, index); |
||
210 | } catch (DriverException e) {
|
||
211 | throw new VisitException("Error al crear al reproyectar "+index, e); |
||
212 | } |
||
213 | processor.processFeature(feature); |
||
214 | } |
||
215 | |||
216 | public String getProcessDescription() { |
||
217 | return ""; |
||
218 | } |
||
219 | |||
220 | public void stop(FLayer layer) { |
||
221 | processor.finish(); |
||
222 | } |
||
223 | |||
224 | public boolean start(FLayer layer) { |
||
225 | if (layer instanceof AlphanumericData |
||
226 | && layer instanceof VectorialData) {
|
||
227 | try {
|
||
228 | this.recordset = ((AlphanumericData) layer).getRecordset();
|
||
229 | this.processor.start();
|
||
230 | } catch (EditionException e) {
|
||
231 | return false; |
||
232 | } catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
||
233 | return false; |
||
234 | } |
||
235 | return true; |
||
236 | } |
||
237 | return false; |
||
238 | } |
||
239 | |||
240 | private IFeature createFeature(IGeometry geometry, int layerIndex) |
||
241 | throws DriverException,
|
||
242 | com.hardcode.gdbms.engine.data.driver.DriverException { |
||
243 | IFeature solution = null;
|
||
244 | FieldDescription[] fields = layerDefinition.getFieldsDesc();
|
||
245 | Value[] featureAttr = new Value[fields.length]; |
||
246 | int numFields = recordset.getFieldCount();
|
||
247 | for (int indexField = 0; indexField < numFields; indexField++) { |
||
248 | // for each field of firstRs
|
||
249 | String fieldName = recordset.getFieldName(indexField);
|
||
250 | for (int j = 0; j < fields.length; j++) { |
||
251 | if (fieldName.equalsIgnoreCase(fields[j].getFieldName())) {
|
||
252 | featureAttr[j] = recordset.getFieldValue(layerIndex, |
||
253 | indexField); |
||
254 | break;
|
||
255 | }// if
|
||
256 | }// for
|
||
257 | }// for
|
||
258 | // now we put null values
|
||
259 | for (int i = 0; i < featureAttr.length; i++) { |
||
260 | if (featureAttr[i] == null) |
||
261 | featureAttr[i] = ValueFactory.createNullValue(); |
||
262 | } |
||
263 | solution = FeatureFactory.createFeature(featureAttr, geometry); |
||
264 | return solution;
|
||
265 | } |
||
266 | } |
||
267 | |||
268 | class ReprojectTask extends AbstractMonitorableTask { |
||
269 | |||
270 | private ReprojectTask() {
|
||
271 | setInitialStep(0);
|
||
272 | try {
|
||
273 | if (onlyInputLayerSelection) {
|
||
274 | int numSelected = firstLayer.getRecordset().getSelection()
|
||
275 | .cardinality(); |
||
276 | setFinalStep(numSelected); |
||
277 | } else {
|
||
278 | int numShapes = firstLayer.getSource().getShapeCount();
|
||
279 | setFinalStep(numShapes); |
||
280 | }// else
|
||
281 | } catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
||
282 | // TODO Auto-generated catch block
|
||
283 | e.printStackTrace(); |
||
284 | } catch (DriverIOException e) {
|
||
285 | // TODO Auto-generated catch block
|
||
286 | e.printStackTrace(); |
||
287 | } |
||
288 | setDeterminatedProcess(true);
|
||
289 | setStatusMessage(PluginServices.getText(this,
|
||
290 | "XYShift._Progress_Message"));
|
||
291 | |||
292 | } |
||
293 | |||
294 | public void run() throws Exception { |
||
295 | processor = |
||
296 | new FeaturePersisterProcessor2(writer);
|
||
297 | 6179 | azabala | FBitSet selection = null;
|
298 | if(onlyInputLayerSelection)
|
||
299 | selection = firstLayer.getRecordset().getSelection(); |
||
300 | 6166 | azabala | IProjection from = firstLayer.getProjection(); |
301 | IProjection to = targetProjection; |
||
302 | 6752 | azabala | |
303 | // ICoordTrans ct = new CoordTrans((CoordSys) to, (CoordSys) from);
|
||
304 | 8903 | luisw2 | ICoordTrans ct = from.getCT(to); |
305 | 6166 | azabala | ReprojectVisitor visitor = new ReprojectVisitor(resultLayerDefinition,
|
306 | processor, selection, ct); |
||
307 | Strategy strategy = StrategyManager.getStrategy(firstLayer); |
||
308 | try {
|
||
309 | strategy.process(visitor, this);
|
||
310 | } catch (Exception e) { |
||
311 | e.printStackTrace(); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | // TODO INTERNACIONALIZAR LOS MENSAJES
|
||
316 | public String getNote() { |
||
317 | return "Reproyectando features..." + " " + getCurrentStep() + " " |
||
318 | + "de" + " " + getFinishStep(); |
||
319 | } |
||
320 | |||
321 | public void cancel() { |
||
322 | setCanceled(true);
|
||
323 | ReprojectGeoprocess.this.cancel(); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | public IMonitorableTask createTask() {
|
||
328 | 6179 | azabala | if(task == null) |
329 | 6166 | azabala | task = new ReprojectTask();
|
330 | return task;
|
||
331 | } |
||
332 | |||
333 | } |