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