gvsig-lrs / org.gvsig.lrs / trunk / org.gvsig.lrs / org.gvsig.lrs.swing / org.gvsig.lrs.swing.impl / src / main / java / org / gvsig / lrs / swing / impl / JLrsCalibrateRouteParamsController.java @ 9
History | View | Annotate | Download (31 KB)
1 | 9 | dmartinezizquierdo | /*
|
---|---|---|---|
2 | * Copyright 2015 DiSiD Technologies S.L.L. All rights reserved.
|
||
3 | *
|
||
4 | * Project : DiSiD org.gvsig.lrs.swing.impl
|
||
5 | * SVN Id : $Id$
|
||
6 | */
|
||
7 | package org.gvsig.lrs.swing.impl; |
||
8 | |||
9 | import java.awt.event.ActionEvent; |
||
10 | import java.awt.event.ActionListener; |
||
11 | import java.awt.event.ItemEvent; |
||
12 | import java.awt.event.ItemListener; |
||
13 | import java.io.File; |
||
14 | import java.util.Iterator; |
||
15 | import java.util.Locale; |
||
16 | |||
17 | import javax.swing.ComboBoxModel; |
||
18 | import javax.swing.DefaultComboBoxModel; |
||
19 | import javax.swing.JComponent; |
||
20 | import javax.swing.JOptionPane; |
||
21 | import javax.swing.ListCellRenderer; |
||
22 | |||
23 | import org.apache.commons.io.FileUtils; |
||
24 | import org.apache.commons.lang3.StringUtils; |
||
25 | import org.cresques.cts.IProjection; |
||
26 | import org.gvsig.fmap.dal.DALLocator; |
||
27 | import org.gvsig.fmap.dal.DataManager; |
||
28 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
29 | import org.gvsig.fmap.dal.exception.DataException; |
||
30 | import org.gvsig.fmap.dal.exception.ReadException; |
||
31 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
32 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
33 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
34 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
35 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
36 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
37 | import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
||
38 | import org.gvsig.fmap.geom.DataTypes; |
||
39 | import org.gvsig.fmap.geom.Geometry; |
||
40 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
41 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
42 | import org.gvsig.fmap.geom.GeometryException; |
||
43 | import org.gvsig.fmap.geom.GeometryLocator; |
||
44 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
45 | import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection; |
||
46 | import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor; |
||
47 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
48 | import org.gvsig.lrs.lib.api.DistanceUnits; |
||
49 | import org.gvsig.lrs.lib.api.LrsAlgorithmParams; |
||
50 | import org.gvsig.lrs.lib.api.LrsAlgorithmsLocator; |
||
51 | import org.gvsig.lrs.lib.api.LrsAlgorithmsManager; |
||
52 | import org.gvsig.lrs.lib.api.LrsCalibrateRouteAlgorithmParams; |
||
53 | import org.gvsig.lrs.lib.api.LrsMeasureCalculationMethods; |
||
54 | import org.gvsig.lrs.lib.api.exceptions.LrsGettingParametersException; |
||
55 | import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
||
56 | import org.gvsig.lrs.swing.api.JLrsAlgorithmParams; |
||
57 | import org.gvsig.tools.ToolsLocator; |
||
58 | import org.gvsig.tools.exception.BaseException; |
||
59 | import org.gvsig.tools.i18n.I18nManager; |
||
60 | import org.gvsig.tools.locator.LocatorException; |
||
61 | import org.gvsig.tools.swing.api.ActionListenerSupport; |
||
62 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
63 | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
||
64 | import org.gvsig.tools.visitor.VisitCanceledException; |
||
65 | import org.slf4j.Logger; |
||
66 | import org.slf4j.LoggerFactory; |
||
67 | |||
68 | public class JLrsCalibrateRouteParamsController extends JLrsCalibrateRouteParamsView implements JLrsAlgorithmParams{ |
||
69 | |||
70 | private static final Logger logger = LoggerFactory.getLogger(JLrsCalibrateRouteParamsController.class); |
||
71 | |||
72 | private boolean canceled; |
||
73 | private ActionListenerSupport listeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
||
74 | |||
75 | private LayerCollection layers;
|
||
76 | |||
77 | /**
|
||
78 | * @param layers
|
||
79 | * @throws LrsNeededParameterException
|
||
80 | *
|
||
81 | */
|
||
82 | public JLrsCalibrateRouteParamsController(LayerCollection layers, LrsAlgorithmParams params) throws LrsNeededParameterException { |
||
83 | super();
|
||
84 | this.layers = layers;
|
||
85 | initComponents(); |
||
86 | translate(); |
||
87 | setParams(params); |
||
88 | } |
||
89 | |||
90 | public void addActionListener(ActionListener listener) { |
||
91 | listeners.addActionListener(listener); |
||
92 | } |
||
93 | |||
94 | public void removeActionListener(ActionListener listener) { |
||
95 | listeners.removeActionListener(listener); |
||
96 | } |
||
97 | |||
98 | private void translate() { |
||
99 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
100 | |||
101 | lblInputLayer.setText(i18nManager.getTranslation(lblInputLayer.getText())); |
||
102 | lblIdRouteField.setText(i18nManager.getTranslation(lblIdRouteField.getText())); |
||
103 | lblCalibratePointLayer.setText(i18nManager.getTranslation(lblCalibratePointLayer.getText())); |
||
104 | lblCalibrationPointIdRouteField.setText(i18nManager.getTranslation(lblCalibrationPointIdRouteField.getText())); |
||
105 | lblFromMeasureField.setText(i18nManager.getTranslation(lblFromMeasureField.getText())); |
||
106 | lblOutputFile.setText(i18nManager.getTranslation(lblOutputFile.getText())); |
||
107 | lblMeasureCalcMethod.setText(i18nManager.getTranslation(lblMeasureCalcMethod.getText())); |
||
108 | lblSearchRadius.setText(i18nManager.getTranslation(lblSearchRadius.getText())); |
||
109 | lblMeasureUnits.setText(i18nManager.getTranslation(lblMeasureUnits.getText())); |
||
110 | chkInterpolateBetweenCalibrationPoints.setText(i18nManager.getTranslation(chkInterpolateBetweenCalibrationPoints.getText())); |
||
111 | chkExtrapolateBeforeCalibrationPoints.setText(i18nManager.getTranslation(chkExtrapolateBeforeCalibrationPoints.getText())); |
||
112 | chkExtrapolateAfterCalibrationPoints.setText(i18nManager.getTranslation(chkExtrapolateAfterCalibrationPoints.getText())); |
||
113 | chkIgnoreSpatialGaps.setText(i18nManager.getTranslation(chkIgnoreSpatialGaps.getText())); |
||
114 | chkIncludeAll.setText(i18nManager.getTranslation(chkIncludeAll.getText())); |
||
115 | btnBrowser.setText("\u2026");
|
||
116 | btnAccept.setText(i18nManager.getTranslation(btnAccept.getText())); |
||
117 | btnCancel.setText(i18nManager.getTranslation(btnCancel.getText())); |
||
118 | |||
119 | } |
||
120 | |||
121 | /**
|
||
122 | *
|
||
123 | */
|
||
124 | public void setLocate(Locale locale) { |
||
125 | Locale l = super.getLocale(); |
||
126 | if (!l.equals(locale)) {
|
||
127 | translate(); |
||
128 | } |
||
129 | super.setLocale(locale);
|
||
130 | } |
||
131 | |||
132 | /**
|
||
133 | * Validates the layers:
|
||
134 | * - At least 2 layers
|
||
135 | * - At least 1 curve layer with at least 1 non-geometrical field
|
||
136 | * - At least 1 point layer with at least 1 non-geometrical field and 1 numeric field
|
||
137 | * if they are not correct throws an exception
|
||
138 | * @throws LrsNeededParameterException
|
||
139 | */
|
||
140 | public static void validateLayers(LayerCollection layers)throws LrsNeededParameterException { |
||
141 | logger.debug("Validating layers for Calibrate Route");
|
||
142 | if (layers==null||layers.getLayersCount()<2){ |
||
143 | logger.error("Two or more layers are needed");
|
||
144 | throw new LrsNeededParameterException("two_or_more_layers_are_needed", null); |
||
145 | } |
||
146 | |||
147 | if (!correctLayersFound(layers)){
|
||
148 | logger.error("Impossible to find the layers needed");
|
||
149 | throw new LrsNeededParameterException("impossible_to_find_needed_layers",null); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /**
|
||
154 | * @throws LrsNeededParameterException
|
||
155 | */
|
||
156 | private void initComponents() throws LrsNeededParameterException { |
||
157 | |||
158 | validateLayers(layers); |
||
159 | |||
160 | // Modelos de los combos
|
||
161 | FLayersComboBoxModel inputLayerModel = new FLayersComboBoxModel(this.layers); |
||
162 | FeatureAttributeDescriptorsComboBoxModel idRouteFieldComboBoxModel = |
||
163 | new FeatureAttributeDescriptorsComboBoxModel();
|
||
164 | |||
165 | FLayersComboBoxModel calibratePointLayerModel = new FLayersComboBoxModel(this.layers); |
||
166 | FeatureAttributeDescriptorsComboBoxModel idCalibrationPointFieldComboBoxModel = |
||
167 | new FeatureAttributeDescriptorsComboBoxModel();
|
||
168 | FeatureAttributeDescriptorsComboBoxModel fromMeasureFieldComboBoxModel = |
||
169 | new FeatureAttributeDescriptorsComboBoxModel();
|
||
170 | |||
171 | |||
172 | // Renderer para los combos de attributeDescriptors
|
||
173 | ListCellRenderer featureAttributeDescriptorCellRenderer = new FLayersAttributeDescriptorCellRenderer(); |
||
174 | // asignaci?n de renderers y modelos
|
||
175 | cmbIdRouteField.setRenderer(featureAttributeDescriptorCellRenderer); |
||
176 | cmbCalibrationPointIdRouteField.setRenderer(featureAttributeDescriptorCellRenderer); |
||
177 | cmbFromMeasureField.setRenderer(featureAttributeDescriptorCellRenderer); |
||
178 | cmbIdRouteField.setModel(idRouteFieldComboBoxModel); |
||
179 | cmbCalibrationPointIdRouteField.setModel(idCalibrationPointFieldComboBoxModel); |
||
180 | cmbFromMeasureField.setModel(fromMeasureFieldComboBoxModel); |
||
181 | |||
182 | // renderer para el combo de capas
|
||
183 | ListCellRenderer layerNameCellRenderer=new FLayersLayerNameCellRenderer(); |
||
184 | cmbInputLayer.setRenderer(layerNameCellRenderer); |
||
185 | cmbCalibratePointLayer.setRenderer(layerNameCellRenderer); |
||
186 | // asignaci?n modelo al combo de capas
|
||
187 | cmbInputLayer.setModel(inputLayerModel); |
||
188 | cmbCalibratePointLayer.setModel(calibratePointLayerModel); |
||
189 | // listener del combo de capas para que actualice el resto de
|
||
190 | // componentes
|
||
191 | cmbInputLayer.addItemListener(new ItemListener() { |
||
192 | |||
193 | public void itemStateChanged(ItemEvent e) { |
||
194 | Object item = e.getItem();
|
||
195 | if (item instanceof FLyrVect) { |
||
196 | FLyrVect layer = (FLyrVect) item; |
||
197 | updateLayerSource(layer); |
||
198 | } |
||
199 | } |
||
200 | }); |
||
201 | cmbCalibratePointLayer.addItemListener(new ItemListener() { |
||
202 | |||
203 | public void itemStateChanged(ItemEvent e) { |
||
204 | Object item = e.getItem();
|
||
205 | if (item instanceof FLyrVect) { |
||
206 | FLyrVect layer = (FLyrVect) item; |
||
207 | updateLayerCalibratePoint(layer); |
||
208 | } |
||
209 | } |
||
210 | }); |
||
211 | |||
212 | ComboBoxModel<LrsMeasureCalculationMethods> measureCalculationMethodsModel =
|
||
213 | new DefaultComboBoxModel<LrsMeasureCalculationMethods>(LrsMeasureCalculationMethods.values()); |
||
214 | |||
215 | cmbMeasureCalcMethod.setModel(measureCalculationMethodsModel); |
||
216 | |||
217 | ComboBoxModel<DistanceUnits> measureUnitsModel =
|
||
218 | new DefaultComboBoxModel<DistanceUnits>(DistanceUnits.values()); |
||
219 | |||
220 | cmbMeasureUnits.setModel(measureUnitsModel); |
||
221 | |||
222 | this.btnBrowser.addActionListener(new ActionListener() { |
||
223 | |||
224 | public void actionPerformed(ActionEvent e) { |
||
225 | doSelectOuputFile(); |
||
226 | } |
||
227 | }); |
||
228 | |||
229 | this.btnAccept.addActionListener(new ActionListener() { |
||
230 | |||
231 | public void actionPerformed(ActionEvent e) { |
||
232 | canceled = false;
|
||
233 | doClose(); |
||
234 | listeners.fireActionEvent(new ActionEvent(JLrsCalibrateRouteParamsController.this, 0, "accept")); |
||
235 | |||
236 | } |
||
237 | }); |
||
238 | |||
239 | this.btnCancel.addActionListener(new ActionListener() { |
||
240 | |||
241 | public void actionPerformed(ActionEvent e) { |
||
242 | canceled = true;
|
||
243 | doClose(); |
||
244 | listeners.fireActionEvent(new ActionEvent(JLrsCalibrateRouteParamsController.this, 0, "cancel")); |
||
245 | } |
||
246 | }); |
||
247 | |||
248 | setVisibleAceptCancel(false);
|
||
249 | canceled = false;
|
||
250 | } |
||
251 | |||
252 | private void doSelectOuputFile() { |
||
253 | final I18nManager i18nManager = ToolsLocator.getI18nManager();
|
||
254 | ThreadSafeDialogsManager manager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
255 | File[] files = manager.showSaveFileDialog(i18nManager.getTranslation("output_file"), null); |
||
256 | if (files != null && files.length > 0) { |
||
257 | File file = files[0]; |
||
258 | String absolutePath = file.getAbsolutePath();
|
||
259 | if(!StringUtils.endsWithIgnoreCase(absolutePath, ".shp")){ |
||
260 | absolutePath = new StringBuilder().append(absolutePath).append(".shp").toString(); |
||
261 | } |
||
262 | File outputFile = FileUtils.getFile(absolutePath);
|
||
263 | if(outputFile.exists()) {
|
||
264 | int resp = JOptionPane.showOptionDialog( |
||
265 | this.asJComponent(),
|
||
266 | i18nManager.getTranslation("file_already_exists_do_you_want_overwrite_it"),
|
||
267 | i18nManager.getTranslation("output_file"),
|
||
268 | JOptionPane.OK_CANCEL_OPTION,
|
||
269 | JOptionPane.QUESTION_MESSAGE,
|
||
270 | null,
|
||
271 | null,
|
||
272 | JOptionPane.OK_OPTION);
|
||
273 | if (resp == JOptionPane.OK_OPTION){ |
||
274 | this.txtOutputFile.setText(absolutePath);
|
||
275 | } else {
|
||
276 | doSelectOuputFile(); |
||
277 | } |
||
278 | } else {
|
||
279 | this.txtOutputFile.setText(absolutePath);
|
||
280 | } |
||
281 | } |
||
282 | |||
283 | } |
||
284 | |||
285 | private void doClose() { |
||
286 | this.setVisible(false); |
||
287 | } |
||
288 | |||
289 | public boolean isCanceled() { |
||
290 | return this.canceled; |
||
291 | } |
||
292 | |||
293 | public void setVisibleAceptCancel(boolean visible) { |
||
294 | this.btnAccept.setVisible(visible);
|
||
295 | this.btnCancel.setVisible(visible);
|
||
296 | } |
||
297 | |||
298 | public boolean getVisibleAceptCancel() { |
||
299 | return this.btnAccept.isVisible(); |
||
300 | } |
||
301 | |||
302 | public JComponent asJComponent() { |
||
303 | return this; |
||
304 | } |
||
305 | |||
306 | public LrsAlgorithmParams getParams() throws LrsGettingParametersException, LrsNeededParameterException { |
||
307 | LrsAlgorithmsManager manager = LrsAlgorithmsLocator.getLrsAlgorithmsManager(); |
||
308 | LrsCalibrateRouteAlgorithmParams params = manager.createLrsCalibrateRouteAlgorithmParams(); |
||
309 | // Recorrer todo del interfaz y rellenar los parametros
|
||
310 | |||
311 | //cmbInputLayer
|
||
312 | if (!(this.cmbInputLayer.getSelectedItem() instanceof FLyrVect)){ |
||
313 | logger.error("Error getting selected item from cmbInputLayer");
|
||
314 | throw new LrsGettingParametersException("error_getting_selected_item_from_cmbInputLayer", null); |
||
315 | } |
||
316 | FLyrVect fLyrVect = (FLyrVect) this.cmbIdRouteField.getSelectedItem();
|
||
317 | if (!(fLyrVect.getFeatureStore() instanceof FeatureStore)){ |
||
318 | logger.error("Error getting selected item from cmbInputLayer");
|
||
319 | throw new LrsGettingParametersException("error_getting_selected_item_from_cmbInputLayer", null); |
||
320 | } |
||
321 | FeatureStore sourceFeatureStore = (FeatureStore)fLyrVect.getFeatureStore(); |
||
322 | if (sourceFeatureStore == null) { |
||
323 | throw new LrsNeededParameterException("need_to_select_a_source_layer", null); |
||
324 | } |
||
325 | params.setSourceFeatureStore(sourceFeatureStore); |
||
326 | |||
327 | //cmbIdRouteField
|
||
328 | if (!(this.cmbIdRouteField.getSelectedItem() instanceof FeatureAttributeDescriptor)){ |
||
329 | logger.error("Error getting selected item from cmbIdRouteField");
|
||
330 | throw new LrsGettingParametersException("error_getting_selected_item_from_cmbIdRouteField", null); |
||
331 | } |
||
332 | FeatureAttributeDescriptor idRouteField = (FeatureAttributeDescriptor) this.cmbIdRouteField.getSelectedItem();
|
||
333 | if (idRouteField == null) { |
||
334 | throw new LrsNeededParameterException("need_to_select_a_route_identifier_field", null); |
||
335 | } |
||
336 | params.setIdRouteField(idRouteField); |
||
337 | |||
338 | //cmbCalibratePointLayer
|
||
339 | if (!(this.cmbCalibratePointLayer.getSelectedItem() instanceof FLyrVect)){ |
||
340 | logger.error("Error getting selected item from cmbCalibratePointLayer");
|
||
341 | throw new LrsGettingParametersException("error_getting_selected_item_from_cmbCalibratePointLayer", null); |
||
342 | } |
||
343 | FLyrVect fLyrVectCalibratePoint = (FLyrVect) this.cmbCalibratePointLayer.getSelectedItem();
|
||
344 | if (!(fLyrVectCalibratePoint.getFeatureStore() instanceof FeatureStore)){ |
||
345 | logger.error("Error getting selected item from cmbCalibratePointLayer");
|
||
346 | throw new LrsGettingParametersException("error_getting_selected_item_from_cmbCalibratePointLayer", null); |
||
347 | } |
||
348 | FeatureStore calibratePointFeatureStore = (FeatureStore)fLyrVectCalibratePoint.getFeatureStore(); |
||
349 | if (calibratePointFeatureStore == null) { |
||
350 | throw new LrsNeededParameterException("need_to_select_a_calibrate_point_layer", null); |
||
351 | } |
||
352 | params.setCalibratePointFeatureStore(calibratePointFeatureStore); |
||
353 | |||
354 | //cmbCalibrationPointIdRouteField
|
||
355 | if (!(this.cmbCalibrationPointIdRouteField.getSelectedItem() instanceof FeatureAttributeDescriptor)){ |
||
356 | logger.error("Error getting selected item from cmbCalibrationPointIdRouteField");
|
||
357 | throw new LrsGettingParametersException("error_getting_selected_item_from_cmbCalibrationPointIdRouteField", null); |
||
358 | } |
||
359 | FeatureAttributeDescriptor idRouteFieldCalibratePoint = |
||
360 | (FeatureAttributeDescriptor) this.cmbCalibrationPointIdRouteField.getSelectedItem();
|
||
361 | if (idRouteFieldCalibratePoint == null) { |
||
362 | throw new LrsNeededParameterException("need_to_select_a_route_identifier_calibration_point_field", null); |
||
363 | } |
||
364 | params.setCalibratePointIdRouteField(idRouteFieldCalibratePoint); |
||
365 | |||
366 | //cmbFromMeasureField
|
||
367 | if (!(this.cmbFromMeasureField.getSelectedItem() instanceof FeatureAttributeDescriptor)){ |
||
368 | logger.error("Error getting selected item from cmbFromMeasureField");
|
||
369 | throw new LrsGettingParametersException("error_getting_selected_item_from_cmbFromMeasureField", null); |
||
370 | } |
||
371 | FeatureAttributeDescriptor fromMeasureField = |
||
372 | (FeatureAttributeDescriptor) this.cmbFromMeasureField.getSelectedItem();
|
||
373 | if (fromMeasureField == null) { |
||
374 | throw new LrsNeededParameterException("need_to_select_a_from_Measure_field", null); |
||
375 | } |
||
376 | params.setFromMeasureField(fromMeasureField); |
||
377 | |||
378 | //OutputStore
|
||
379 | String outputLayerPath = this.txtOutputFile.getText(); |
||
380 | if(StringUtils.isEmpty(outputLayerPath)){
|
||
381 | throw new LrsNeededParameterException("need_to_fill_the_target_path", null); |
||
382 | } |
||
383 | SHPNewStoreParameters sHPNewStoreParameters=new SHPNewStoreParameters();
|
||
384 | sHPNewStoreParameters.setFile(outputLayerPath); |
||
385 | params.setNewFeatureStoreParameters((NewFeatureStoreParameters)sHPNewStoreParameters); |
||
386 | |||
387 | |||
388 | LrsMeasureCalculationMethods measureCalculationMethods = (LrsMeasureCalculationMethods) this.cmbMeasureCalcMethod.getSelectedItem();
|
||
389 | params.setMeasureCalculationMethods(measureCalculationMethods); |
||
390 | DistanceUnits measureUnits = (DistanceUnits) this.cmbMeasureUnits.getSelectedItem();
|
||
391 | params.setMeasureUnits(measureUnits); |
||
392 | |||
393 | Double searchRadius = (Double) this.txtSearchRadius.getValue(); |
||
394 | params.setSearchRadius(searchRadius); |
||
395 | |||
396 | params.setInterpolateBetweenCalibrationPoints(this.chkInterpolateBetweenCalibrationPoints.isSelected());
|
||
397 | params.setExtrapolateBeforeCalibrationPoints(this.chkExtrapolateBeforeCalibrationPoints.isSelected());
|
||
398 | params.setExtrapolateAfterCalibrationPoints(this.chkExtrapolateAfterCalibrationPoints.isSelected());
|
||
399 | params.setIgnoreSpatialGaps(this.chkIgnoreSpatialGaps.isSelected());
|
||
400 | params.setIncludeAll(this.chkIncludeAll.isSelected());
|
||
401 | |||
402 | return params;
|
||
403 | } |
||
404 | |||
405 | public void setParams(LrsAlgorithmParams params) { |
||
406 | if (params == null) { |
||
407 | clear(); |
||
408 | return;
|
||
409 | } |
||
410 | // Rellena los campos con los valores de params
|
||
411 | LrsCalibrateRouteAlgorithmParams parameters = (LrsCalibrateRouteAlgorithmParams) params; |
||
412 | |||
413 | this.cmbMeasureCalcMethod.setSelectedItem(parameters.getMeasureCalculationMethods());
|
||
414 | this.cmbMeasureUnits.setSelectedItem(parameters.getMeasureUnits());
|
||
415 | |||
416 | FeatureStore sourceFeatureStore = parameters.getSourceFeatureStore(); |
||
417 | if(layers!=null && sourceFeatureStore!=null){ |
||
418 | FLayer layer = layers.getLayer(sourceFeatureStore.getName()); |
||
419 | this.cmbInputLayer.setSelectedItem(layer);
|
||
420 | } |
||
421 | FeatureStore calibratePointFeaturesStore=parameters.getCalibratePointFeatureStore(); |
||
422 | if(layers!=null && calibratePointFeaturesStore!=null){ |
||
423 | FLayer layer = layers.getLayer(calibratePointFeaturesStore.getName()); |
||
424 | this.cmbCalibratePointLayer.setSelectedItem(layer);
|
||
425 | } |
||
426 | NewFeatureStoreParameters newFeatureStoreParameters = parameters.getNewFeatureStoreParameters(); |
||
427 | if(newFeatureStoreParameters!=null && newFeatureStoreParameters instanceof SHPNewStoreParameters){ |
||
428 | this.txtOutputFile.setText(((SHPNewStoreParameters)newFeatureStoreParameters).getSHPFile().getAbsolutePath());
|
||
429 | } |
||
430 | |||
431 | this.cmbIdRouteField.setSelectedItem(parameters.getIdRouteField());
|
||
432 | this.cmbCalibrationPointIdRouteField.setSelectedItem(parameters.getCalibratePointIdRouteField());
|
||
433 | this.cmbFromMeasureField.setSelectedItem(parameters.getFromMeasureField());
|
||
434 | |||
435 | this.txtSearchRadius.setValue(parameters.getSearchRadius());
|
||
436 | this.chkInterpolateBetweenCalibrationPoints.setSelected(parameters.interpolateBetweenCalibrationPoints());
|
||
437 | this.chkExtrapolateBeforeCalibrationPoints.setSelected(parameters.extrapolateBeforeCalibrationPoints());
|
||
438 | this.chkExtrapolateAfterCalibrationPoints.setSelected(parameters.extrapolateAfterCalibrationPoints());
|
||
439 | this.chkIgnoreSpatialGaps.setSelected(parameters.ignoreSpatialGaps());
|
||
440 | this.chkIncludeAll.setSelected(parameters.includeAll());
|
||
441 | |||
442 | } |
||
443 | |||
444 | public void clear() { |
||
445 | this.txtOutputFile.setText(""); |
||
446 | this.txtSearchRadius.setValue(0.0); |
||
447 | |||
448 | this.cmbInputLayer.setSelectedIndex(0); |
||
449 | Object inputLayerSelected = this.cmbInputLayer.getSelectedItem(); |
||
450 | if (inputLayerSelected != null) { |
||
451 | this.updateLayerSource((FLyrVect) inputLayerSelected);
|
||
452 | } |
||
453 | this.cmbCalibratePointLayer.setSelectedIndex(0); |
||
454 | Object calibratePointLayerSelected = this.cmbCalibratePointLayer.getSelectedItem(); |
||
455 | if (calibratePointLayerSelected != null) { |
||
456 | this.updateLayerCalibratePoint((FLyrVect) calibratePointLayerSelected);
|
||
457 | } |
||
458 | this.cmbIdRouteField.setSelectedIndex(0); |
||
459 | this.cmbCalibrationPointIdRouteField.setSelectedIndex(0); |
||
460 | // this.cmbFromMeasureField.setSelectedIndex(0);
|
||
461 | this.cmbMeasureCalcMethod.setSelectedIndex(0); |
||
462 | this.cmbMeasureUnits.setSelectedIndex(0); |
||
463 | this.chkInterpolateBetweenCalibrationPoints.setSelected(false); |
||
464 | this.chkExtrapolateBeforeCalibrationPoints.setSelected(false); |
||
465 | this.chkExtrapolateAfterCalibrationPoints.setSelected(false); |
||
466 | this.chkIgnoreSpatialGaps.setSelected(false); |
||
467 | this.chkIncludeAll.setSelected(false); |
||
468 | |||
469 | } |
||
470 | |||
471 | private void updateLayerSource(FLyrVect layer) { |
||
472 | logger.info(new StringBuilder().append("Setting layer ").append(layer.getName()).toString()); |
||
473 | FeatureStore featureStore = layer.getFeatureStore(); |
||
474 | try {
|
||
475 | FeatureType type = featureStore.getDefaultFeatureType(); |
||
476 | // Se renuevan los combos de attributeDescriptors
|
||
477 | FeatureAttributeDescriptorsComboBoxModel idRouteFieldComboBoxModel = |
||
478 | new FeatureAttributeDescriptorsComboBoxModel();
|
||
479 | |||
480 | this.cmbIdRouteField.setModel(idRouteFieldComboBoxModel);
|
||
481 | |||
482 | FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
|
||
483 | for (int i = 0; i < attributeDescriptors.length; i++) { |
||
484 | FeatureAttributeDescriptor featureAttributeDescriptor = attributeDescriptors[i]; |
||
485 | if (featureAttributeDescriptor.getDataType().getType() != DataTypes.GEOMETRY) {
|
||
486 | logger.info(new StringBuilder().append("Adding field ") |
||
487 | .append(featureAttributeDescriptor.getName()).toString()); |
||
488 | |||
489 | JLrsCalibrateRouteParamsController.this.cmbIdRouteField.addItem(featureAttributeDescriptor); |
||
490 | } |
||
491 | } |
||
492 | this.cmbIdRouteField.invalidate();
|
||
493 | |||
494 | } catch (DataException e1) {
|
||
495 | logger.warn( |
||
496 | new StringBuilder().append("Error getting default feature type from layer ").append(layer.getName()) |
||
497 | .toString(), e1); |
||
498 | } |
||
499 | } |
||
500 | |||
501 | |||
502 | |||
503 | private void updateLayerCalibratePoint(FLyrVect layer) { |
||
504 | logger.info(new StringBuilder().append("Setting layer ").append(layer.getName()).toString()); |
||
505 | FeatureStore featureStore = layer.getFeatureStore(); |
||
506 | try {
|
||
507 | FeatureType type = featureStore.getDefaultFeatureType(); |
||
508 | // Se renuevan los combos de attributeDescriptors
|
||
509 | FeatureAttributeDescriptorsComboBoxModel calibrationPointIdRouteFieldComboBoxModel = |
||
510 | new FeatureAttributeDescriptorsComboBoxModel();
|
||
511 | FeatureAttributeDescriptorsComboBoxModel fromMeasureFieldComboBoxModel = |
||
512 | new FeatureAttributeDescriptorsComboBoxModel();
|
||
513 | |||
514 | this.cmbCalibrationPointIdRouteField.setModel(calibrationPointIdRouteFieldComboBoxModel);
|
||
515 | this.cmbFromMeasureField.setModel(fromMeasureFieldComboBoxModel);
|
||
516 | |||
517 | FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
|
||
518 | for (int i = 0; i < attributeDescriptors.length; i++) { |
||
519 | FeatureAttributeDescriptor featureAttributeDescriptor = attributeDescriptors[i]; |
||
520 | if (featureAttributeDescriptor.getDataType().getType() != DataTypes.GEOMETRY) {
|
||
521 | logger.info(new StringBuilder().append("Adding field ") |
||
522 | .append(featureAttributeDescriptor.getName()).toString()); |
||
523 | |||
524 | JLrsCalibrateRouteParamsController.this.cmbCalibrationPointIdRouteField.addItem(featureAttributeDescriptor); |
||
525 | if (featureAttributeDescriptor.getDataType().isNumeric()) {
|
||
526 | JLrsCalibrateRouteParamsController.this.cmbFromMeasureField.addItem(featureAttributeDescriptor); |
||
527 | } |
||
528 | } |
||
529 | } |
||
530 | this.cmbCalibrationPointIdRouteField.invalidate();
|
||
531 | this.cmbFromMeasureField.invalidate();
|
||
532 | |||
533 | } catch (DataException e1) {
|
||
534 | logger.warn( |
||
535 | new StringBuilder().append("Error getting default feature type from layer ").append(layer.getName()) |
||
536 | .toString(), e1); |
||
537 | } |
||
538 | } |
||
539 | |||
540 | |||
541 | protected FeatureStore getFeatureStore(String outputLayerPath, IProjection projection) throws Exception { |
||
542 | |||
543 | DataManager dataManager = DALLocator.getDataManager(); |
||
544 | DataStoreParameters dataStoreParams = dataManager.createStoreParameters("Shape");
|
||
545 | dataStoreParams.setDynValue("shpfile", outputLayerPath);
|
||
546 | dataStoreParams.setDynValue("CRS", projection);
|
||
547 | dataStoreParams.setDynValue("useNullGeometry", false); |
||
548 | dataStoreParams.validate(); |
||
549 | |||
550 | return (FeatureStore) dataManager.openStore("Shape", dataStoreParams); |
||
551 | } |
||
552 | |||
553 | |||
554 | /**
|
||
555 | * Tries to find the correct layers:
|
||
556 | * - At least 2 layers
|
||
557 | * - At least 1 curve layer with at least 1 non-geometrical field
|
||
558 | * - At least 1 point layer with at least 1 non-geometrical field and 1 numeric field
|
||
559 | * @param LayerCollection layers
|
||
560 | * @return true if finds the correct layers
|
||
561 | */
|
||
562 | private static boolean correctLayersFound(LayerCollection layers){ |
||
563 | boolean correctCurveLayerFound = false; |
||
564 | boolean correctPointLayerFound = false; |
||
565 | boolean layersFound=false; |
||
566 | |||
567 | for (int i = 0; i < layers.getLayersCount() && !layersFound; i++) { |
||
568 | FLayer layer = layers.getLayer(i); |
||
569 | if (layer instanceof FLyrVect) { |
||
570 | FLyrVect lyrVect = (FLyrVect) layer; |
||
571 | try {
|
||
572 | if (!correctCurveLayerFound) {
|
||
573 | logger.debug( |
||
574 | "Finding a curve layer with at least one non-geometrical field");
|
||
575 | correctCurveLayerFound = |
||
576 | correctCurveLayerFound(lyrVect); |
||
577 | } |
||
578 | if (!correctPointLayerFound) {
|
||
579 | logger.debug( |
||
580 | "Finding a point layer with at least one non-geometrical field and a numeric one");
|
||
581 | correctPointLayerFound=correctPointLayerFound(lyrVect); |
||
582 | } |
||
583 | } catch (BaseException e1) {
|
||
584 | logger.error("Can't get linear vector layers", e1);
|
||
585 | } |
||
586 | } |
||
587 | layersFound=correctCurveLayerFound && correctPointLayerFound; |
||
588 | } |
||
589 | return correctCurveLayerFound && correctPointLayerFound;
|
||
590 | } |
||
591 | |||
592 | /**
|
||
593 | * Tries to find one curve layer with at least one non-geometrical field
|
||
594 | * @param lyrVect
|
||
595 | * @return
|
||
596 | * @throws ReadException
|
||
597 | */
|
||
598 | private static boolean correctCurveLayerFound(FLyrVect lyrVect) throws ReadException{ |
||
599 | boolean correctCurveLayerFound = false; |
||
600 | if (lyrVect!=null&&(lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.CURVE) |
||
601 | || lyrVect.getGeometryType() |
||
602 | .isTypeOf(Geometry.TYPES.MULTICURVE))) { |
||
603 | |||
604 | FeatureStore featureStore = lyrVect.getFeatureStore(); |
||
605 | try {
|
||
606 | FeatureType type = featureStore.getDefaultFeatureType(); |
||
607 | |||
608 | FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
|
||
609 | for (int i = 0; i < attributeDescriptors.length; i++) { |
||
610 | FeatureAttributeDescriptor featureAttributeDescriptor = attributeDescriptors[i]; |
||
611 | if (featureAttributeDescriptor.getDataType().getType() != DataTypes.GEOMETRY) {
|
||
612 | logger.debug("Correct curve layer found");
|
||
613 | correctCurveLayerFound = true;
|
||
614 | } |
||
615 | } |
||
616 | } catch (DataException e1) {
|
||
617 | logger.warn( |
||
618 | new StringBuilder().append("Error getting default feature type from layer ").append(lyrVect.getName()) |
||
619 | .toString(), e1); |
||
620 | } |
||
621 | } |
||
622 | return correctCurveLayerFound;
|
||
623 | } |
||
624 | |||
625 | |||
626 | /**
|
||
627 | * Tries to find one point layer with at least 1 non-geometrical field and 1 numeric field
|
||
628 | * @param lyrVect
|
||
629 | * @return
|
||
630 | * @throws ReadException
|
||
631 | */
|
||
632 | private static boolean correctPointLayerFound(FLyrVect lyrVect) throws ReadException{ |
||
633 | boolean correctPointLayerFound=false; |
||
634 | if (lyrVect!=null&&(lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.POINT) |
||
635 | || lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.MULTIPOINT))) { |
||
636 | FeatureStore featureStore = lyrVect.getFeatureStore(); |
||
637 | try {
|
||
638 | FeatureType type = featureStore.getDefaultFeatureType(); |
||
639 | |||
640 | FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
|
||
641 | Boolean nonGeometricDataTypeFound=false; |
||
642 | Boolean numericDataTypeFound=false; |
||
643 | Boolean dataTypesFound=false; |
||
644 | Boolean validateNext=true; |
||
645 | for (int i = 0; i < attributeDescriptors.length&&!dataTypesFound; i++) { |
||
646 | FeatureAttributeDescriptor featureAttributeDescriptor = attributeDescriptors[i]; |
||
647 | validateNext=true;
|
||
648 | if (featureAttributeDescriptor.getDataType().isNumeric()&&!numericDataTypeFound) {
|
||
649 | numericDataTypeFound = true;
|
||
650 | validateNext=false;
|
||
651 | } |
||
652 | //This is done to avoid to validate twice the same numeric field
|
||
653 | if (validateNext){
|
||
654 | if (featureAttributeDescriptor.getDataType().getType() != DataTypes.GEOMETRY) {
|
||
655 | nonGeometricDataTypeFound = true;
|
||
656 | } |
||
657 | } |
||
658 | dataTypesFound=nonGeometricDataTypeFound&&numericDataTypeFound; |
||
659 | } |
||
660 | if(dataTypesFound){
|
||
661 | logger.debug("Correct point layer found");
|
||
662 | correctPointLayerFound=true;
|
||
663 | } |
||
664 | } catch (DataException e1) {
|
||
665 | logger.warn( |
||
666 | new StringBuilder().append("Error getting default feature type from layer ").append(lyrVect.getName()) |
||
667 | .toString(), e1); |
||
668 | } |
||
669 | } |
||
670 | return correctPointLayerFound;
|
||
671 | } |
||
672 | |||
673 | } |