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 / JLrsUtilsController.java @ 16
History | View | Annotate | Download (16 KB)
1 |
/*
|
---|---|
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.io.File; |
10 |
import java.util.List; |
11 |
|
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.JOptionPane; |
14 |
import javax.swing.JTextField; |
15 |
|
16 |
import org.apache.commons.io.FileUtils; |
17 |
import org.apache.commons.lang3.StringUtils; |
18 |
import org.apache.commons.lang3.mutable.MutableBoolean; |
19 |
import org.cresques.cts.IProjection; |
20 |
import org.gvsig.fmap.dal.DALLocator; |
21 |
import org.gvsig.fmap.dal.DataManager; |
22 |
import org.gvsig.fmap.dal.DataServerExplorer; |
23 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
24 |
import org.gvsig.fmap.dal.exception.DataException; |
25 |
import org.gvsig.fmap.dal.exception.ReadException; |
26 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
28 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
29 |
import org.gvsig.fmap.dal.feature.FeatureType; |
30 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
31 |
import org.gvsig.fmap.dal.store.shp.SHPNewStoreParameters; |
32 |
import org.gvsig.fmap.geom.DataTypes; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
35 |
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection; |
36 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
37 |
import org.gvsig.lrs.lib.api.exceptions.LrsNeededParameterException; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.exception.BaseException; |
40 |
import org.gvsig.tools.i18n.I18nManager; |
41 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
42 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
43 |
import org.gvsig.tools.visitor.VisitCanceledException; |
44 |
import org.gvsig.tools.visitor.Visitor; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
public class JLrsUtilsController { |
49 |
|
50 |
private static final Logger logger = LoggerFactory.getLogger(JLrsUtilsController.class); |
51 |
/**
|
52 |
* Given a pathfile and a projection returns params to create a new feature store
|
53 |
* @param outputLayerPathFile
|
54 |
* @param projection
|
55 |
* @return
|
56 |
* @throws Exception
|
57 |
*/
|
58 |
static protected NewFeatureStoreParameters createFeatureStoreParams(String outputLayerPathFile,IProjection projection) throws Exception{ |
59 |
String filePath=""; |
60 |
if(outputLayerPathFile!=null){ |
61 |
filePath=outputLayerPathFile.substring(0,outputLayerPathFile.lastIndexOf(File.separator)); |
62 |
} |
63 |
DataManager dataManager = DALLocator.getDataManager(); |
64 |
DataServerExplorerParameters eparams = |
65 |
dataManager.createServerExplorerParameters("FilesystemExplorer");
|
66 |
eparams.setDynValue("initialpath", filePath);
|
67 |
DataServerExplorer serverExplorer = |
68 |
dataManager.openServerExplorer(eparams.getExplorerName(), eparams); |
69 |
|
70 |
SHPNewStoreParameters sparams = (SHPNewStoreParameters)serverExplorer.getAddParameters("Shape");
|
71 |
sparams.setDynValue("shpfile",outputLayerPathFile);
|
72 |
|
73 |
sparams.setCRS(projection); |
74 |
|
75 |
return sparams;
|
76 |
} |
77 |
|
78 |
|
79 |
/**
|
80 |
* Tries to find the correct layers:
|
81 |
* - At least 1 curve layer with at least 1 non-geometrical field
|
82 |
* @param LayerCollection layers
|
83 |
* @return true if finds the correct layers
|
84 |
*/
|
85 |
public static boolean validateOneCurveLyrOneNonGeomField(LayerCollection layers){ |
86 |
boolean correctCurveLayerFound = false; |
87 |
|
88 |
for (int i = 0; i < layers.getLayersCount() && !correctCurveLayerFound; i++) { |
89 |
FLayer layer = layers.getLayer(i); |
90 |
if (layer instanceof FLyrVect) { |
91 |
FLyrVect lyrVect = (FLyrVect) layer; |
92 |
try {
|
93 |
if (!correctCurveLayerFound) {
|
94 |
logger.debug( |
95 |
"Finding a curve layer with at least one non-geometrical field");
|
96 |
correctCurveLayerFound = |
97 |
CurveLayerFoundWithOneNonGeomField(lyrVect); |
98 |
} |
99 |
} catch (BaseException e1) {
|
100 |
logger.error("Can't get linear vector layers", e1);
|
101 |
} |
102 |
} |
103 |
} |
104 |
return correctCurveLayerFound;
|
105 |
} |
106 |
|
107 |
|
108 |
/**
|
109 |
* Tries to find the correct layers:
|
110 |
* - At least 2 layers
|
111 |
* - At least 1 curve layer with at least 1 non-geometrical field
|
112 |
* - At least 1 point layer with at least 1 non-geometrical field and 1 numeric field
|
113 |
* @param LayerCollection layers
|
114 |
* @return true if finds the correct layers
|
115 |
*/
|
116 |
public static boolean validatedCurveAndPointLayer(LayerCollection layers){ |
117 |
boolean correctCurveLayerFound = false; |
118 |
boolean correctPointLayerFound = false; |
119 |
boolean layersFound=false; |
120 |
|
121 |
for (int i = 0; i < layers.getLayersCount() && !layersFound; i++) { |
122 |
FLayer layer = layers.getLayer(i); |
123 |
if (layer instanceof FLyrVect) { |
124 |
FLyrVect lyrVect = (FLyrVect) layer; |
125 |
try {
|
126 |
if (!correctCurveLayerFound) {
|
127 |
logger.debug( |
128 |
"Finding a curve layer with at least one non-geometrical field");
|
129 |
correctCurveLayerFound = |
130 |
CurveLayerFoundWithOneNonGeomField(lyrVect); |
131 |
} |
132 |
if (!correctPointLayerFound) {
|
133 |
logger.debug( |
134 |
"Finding a point layer with at least one non-geometrical field and a numeric one");
|
135 |
correctPointLayerFound=isPointLayerWithNonGeomAndNumericFields(lyrVect); |
136 |
} |
137 |
} catch (BaseException e1) {
|
138 |
logger.error("Can't get linear vector layers", e1);
|
139 |
} |
140 |
} |
141 |
layersFound=correctCurveLayerFound && correctPointLayerFound; |
142 |
} |
143 |
return correctCurveLayerFound && correctPointLayerFound;
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Tries to find a curve M-layer with at least one non-geometrical field
|
148 |
*
|
149 |
* @param layers
|
150 |
* @return
|
151 |
*/
|
152 |
public static boolean validateMLayerWithNonGeomField(LayerCollection layers) { |
153 |
final MutableBoolean correctLayerFound=new MutableBoolean(false); |
154 |
if (layers != null) { |
155 |
try {
|
156 |
layers.accept(new Visitor() {
|
157 |
|
158 |
public void visit(Object obj) |
159 |
throws VisitCanceledException, BaseException {
|
160 |
FLayer layer = (FLayer) obj; |
161 |
if(!correctLayerFound.getValue()){
|
162 |
if (
|
163 |
isMlayerWithNonGeomField(layer)) { |
164 |
correctLayerFound.setValue(true);
|
165 |
throw new VisitCanceledException(); |
166 |
} |
167 |
} |
168 |
} |
169 |
}); |
170 |
} catch (VisitCanceledException e) {
|
171 |
return true; |
172 |
} |
173 |
catch (BaseException e) {
|
174 |
logger.warn("Error visiting layer");
|
175 |
} |
176 |
} |
177 |
return correctLayerFound.getValue();
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Tries to find one curve layer with at least one non-geometrical field
|
182 |
* @param lyrVect
|
183 |
* @return
|
184 |
* @throws ReadException
|
185 |
*/
|
186 |
private static boolean CurveLayerFoundWithOneNonGeomField(FLyrVect lyrVect) throws ReadException{ |
187 |
boolean correctCurveLayerFound = false; |
188 |
if (lyrVect!=null&&(lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.CURVE) |
189 |
|| lyrVect.getGeometryType() |
190 |
.isTypeOf(Geometry.TYPES.MULTICURVE))) { |
191 |
|
192 |
FeatureStore featureStore = lyrVect.getFeatureStore(); |
193 |
try {
|
194 |
FeatureType type = featureStore.getDefaultFeatureType(); |
195 |
|
196 |
FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
|
197 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
198 |
FeatureAttributeDescriptor featureAttributeDescriptor = attributeDescriptors[i]; |
199 |
if (featureAttributeDescriptor.getDataType().getType() != DataTypes.GEOMETRY) {
|
200 |
logger.debug("Correct curve layer found");
|
201 |
correctCurveLayerFound = true;
|
202 |
return correctCurveLayerFound;
|
203 |
} |
204 |
} |
205 |
} catch (DataException e1) {
|
206 |
logger.warn( |
207 |
new StringBuilder().append("Error getting default feature type from layer ").append(lyrVect.getName()) |
208 |
.toString(), e1); |
209 |
} |
210 |
} |
211 |
return correctCurveLayerFound;
|
212 |
} |
213 |
|
214 |
|
215 |
|
216 |
/**
|
217 |
* Tries to find one point layer with at least 1 non-geometrical field and 1 numeric field
|
218 |
* @param lyrVect
|
219 |
* @return
|
220 |
* @throws ReadException
|
221 |
*/
|
222 |
private static boolean isPointLayerWithNonGeomAndNumericFields(FLyrVect lyrVect) throws ReadException{ |
223 |
boolean correctPointLayerFound=false; |
224 |
if (lyrVect!=null&&(lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.POINT) |
225 |
|| lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.MULTIPOINT))) { |
226 |
FeatureStore featureStore = lyrVect.getFeatureStore(); |
227 |
try {
|
228 |
FeatureType type = featureStore.getDefaultFeatureType(); |
229 |
|
230 |
FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
|
231 |
Boolean nonGeometricDataTypeFound=false; |
232 |
Boolean numericDataTypeFound=false; |
233 |
Boolean dataTypesFound=false; |
234 |
Boolean validateNext=true; |
235 |
for (int i = 0; i < attributeDescriptors.length&&!dataTypesFound; i++) { |
236 |
FeatureAttributeDescriptor featureAttributeDescriptor = attributeDescriptors[i]; |
237 |
validateNext=true;
|
238 |
if (featureAttributeDescriptor.getDataType().isNumeric()&&!numericDataTypeFound) {
|
239 |
numericDataTypeFound = true;
|
240 |
validateNext=false;
|
241 |
} |
242 |
//This is done to avoid to validate twice the same numeric field
|
243 |
if (validateNext){
|
244 |
if (featureAttributeDescriptor.getDataType().getType() != DataTypes.GEOMETRY) {
|
245 |
nonGeometricDataTypeFound = true;
|
246 |
} |
247 |
} |
248 |
dataTypesFound=nonGeometricDataTypeFound&&numericDataTypeFound; |
249 |
} |
250 |
if(dataTypesFound){
|
251 |
logger.debug("Correct point layer found");
|
252 |
correctPointLayerFound=true;
|
253 |
} |
254 |
} catch (DataException e1) {
|
255 |
logger.warn( |
256 |
new StringBuilder().append("Error getting default feature type from layer ").append(lyrVect.getName()) |
257 |
.toString(), e1); |
258 |
} |
259 |
} |
260 |
return correctPointLayerFound;
|
261 |
} |
262 |
|
263 |
|
264 |
/**
|
265 |
* Verifies if it is a curve M-layer with at least one non-geometrical field
|
266 |
*
|
267 |
* @param FLayer
|
268 |
* @return
|
269 |
* @throws ReadException
|
270 |
*/
|
271 |
public static boolean isMlayerWithNonGeomField(FLayer layer) { |
272 |
boolean correctLayerFound = false; |
273 |
// Validates is FLyrVect
|
274 |
FLyrVect lyrVect = null;
|
275 |
if (layer instanceof FLyrVect) { |
276 |
lyrVect = (FLyrVect) layer; |
277 |
} else
|
278 |
return false; |
279 |
|
280 |
try {
|
281 |
if (lyrVect != null |
282 |
&& (lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.CURVE) |
283 |
|| lyrVect.getGeometryType() |
284 |
.isTypeOf(Geometry.TYPES.MULTICURVE))) { |
285 |
// TODO M-geometry commented to test button
|
286 |
// if (lyrVect.getGeometryType()
|
287 |
// .isSubTypeOf(Geometry.SUBTYPES.GEOM2DM) ||
|
288 |
// lyrVect.getGeometryType()
|
289 |
// .isSubTypeOf(Geometry.SUBTYPES.GEOM3DM)) {
|
290 |
FeatureStore featureStore = lyrVect.getFeatureStore(); |
291 |
try {
|
292 |
FeatureType type = featureStore.getDefaultFeatureType(); |
293 |
|
294 |
FeatureAttributeDescriptor[] attributeDescriptors =
|
295 |
type.getAttributeDescriptors(); |
296 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
297 |
FeatureAttributeDescriptor featureAttributeDescriptor = |
298 |
attributeDescriptors[i]; |
299 |
if (featureAttributeDescriptor.getDataType()
|
300 |
.getType() != DataTypes.GEOMETRY) { |
301 |
logger.debug("Correct curve layer found");
|
302 |
return true; |
303 |
} |
304 |
} |
305 |
// type.getDefaultGeometryAttribute().getGeomType().isSubTypeOf(Geometry.SUBTYPES.GEOM2DM)
|
306 |
|
307 |
} catch (DataException e1) {
|
308 |
logger.warn(new StringBuilder() |
309 |
.append( |
310 |
"Error getting default feature type from layer ")
|
311 |
.append(lyrVect.getName()).toString(), e1); |
312 |
} |
313 |
|
314 |
// }
|
315 |
} |
316 |
} catch (Exception e) { |
317 |
logger.error("Can't get linear vector layers", e);
|
318 |
} |
319 |
return correctLayerFound;
|
320 |
} |
321 |
|
322 |
|
323 |
/**
|
324 |
* Validates if there is a valid table:
|
325 |
* - at least two fields
|
326 |
* - one of them is numeric
|
327 |
* if can't find any throws an exception
|
328 |
*
|
329 |
* @throws LrsNeededParameterException
|
330 |
* @throws ReadException
|
331 |
*/
|
332 |
public static boolean validateStoreWithFieldsAndOneNumeric (List<FeatureStore> tables) { |
333 |
for (FeatureStore featureStore : tables) {
|
334 |
try {
|
335 |
FeatureType type = featureStore.getDefaultFeatureType(); |
336 |
|
337 |
FeatureAttributeDescriptor[] attributeDescriptors =
|
338 |
type.getAttributeDescriptors(); |
339 |
if (attributeDescriptors.length >= 2) { |
340 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
341 |
FeatureAttributeDescriptor featureAttributeDescriptor = |
342 |
attributeDescriptors[i]; |
343 |
if (featureAttributeDescriptor.getDataType()
|
344 |
.isNumeric()) { |
345 |
logger.debug("Correct table found");
|
346 |
return true; |
347 |
} |
348 |
} |
349 |
} |
350 |
} catch (DataException e1) {
|
351 |
logger.warn("Error getting default feature type");
|
352 |
} |
353 |
} |
354 |
return false; |
355 |
} |
356 |
|
357 |
/**
|
358 |
* Tries to find one curve M-layer with selected Features
|
359 |
*
|
360 |
* @param FLayer
|
361 |
* @return
|
362 |
* @throws ReadException
|
363 |
*/
|
364 |
public static boolean hasMLayersWithSelectedFeatures(FLayer layer) { |
365 |
// Validates is FLyrVect
|
366 |
FLyrVect lyrVect = null;
|
367 |
if (layer instanceof FLyrVect) { |
368 |
lyrVect = (FLyrVect) layer; |
369 |
} else
|
370 |
return false; |
371 |
|
372 |
try {
|
373 |
if (lyrVect != null && (lyrVect.getGeometryType() |
374 |
.isTypeOf(Geometry.TYPES.CURVE) |
375 |
|| lyrVect.getGeometryType().isTypeOf(Geometry.TYPES.MULTICURVE))) { |
376 |
//TODO M-geometry commented to test button
|
377 |
// if (lyrVect.getGeometryType()
|
378 |
// .isSubTypeOf(Geometry.SUBTYPES.GEOM2DM) ||
|
379 |
// lyrVect.getGeometryType()
|
380 |
// .isSubTypeOf(Geometry.SUBTYPES.GEOM3DM)) {
|
381 |
|
382 |
//At least a feature must be selected
|
383 |
return hasSelectedFeatures(lyrVect);
|
384 |
|
385 |
//}
|
386 |
} |
387 |
} catch (Exception e) { |
388 |
logger.error("Can't get linear vector layers", e);
|
389 |
} |
390 |
return false; |
391 |
} |
392 |
|
393 |
private static boolean hasSelectedFeatures(FLyrVect layer) { |
394 |
FeatureStore featureStore=layer.getFeatureStore(); |
395 |
if(featureStore!=null){ |
396 |
try {
|
397 |
FeatureSelection selectedFeatures= featureStore.getFeatureSelection(); |
398 |
if ((selectedFeatures.getSize() >0)) { |
399 |
return true; |
400 |
} |
401 |
} catch (DataException e) {
|
402 |
logger.warn("Can't get selected feature", e);
|
403 |
} |
404 |
} |
405 |
return false; |
406 |
} |
407 |
} |