25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.geotools; |
package skrueger.geotools; |
31 |
|
|
41 |
|
|
42 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
43 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
|
import org.geotools.data.store.EmptyFeatureCollection; |
|
44 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
45 |
|
import org.geotools.feature.NameImpl; |
46 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
47 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
48 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
52 |
|
|
53 |
import schmitzm.geotools.io.GeoImportUtil; |
import schmitzm.geotools.io.GeoImportUtil; |
54 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
55 |
import skrueger.AttributeMetaData; |
import skrueger.AttributeMetadataImpl; |
56 |
|
import skrueger.AttributeMetadataInterface; |
57 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
58 |
|
|
59 |
import com.vividsolutions.jts.geom.Envelope; |
import com.vividsolutions.jts.geom.Envelope; |
63 |
* {@link JPanel} as a {@link MapContextManagerInterface} |
* {@link JPanel} as a {@link MapContextManagerInterface} |
64 |
* |
* |
65 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
66 |
|
* |
67 |
|
* TODO Rename to StyledShapefile |
68 |
*/ |
*/ |
69 |
public class StyledFS implements StyledFeatureSourceInterface { |
public class StyledFS implements StyledFeatureSourceInterface { |
70 |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
71 |
|
|
72 |
private final FeatureSource<SimpleFeatureType, SimpleFeature> fs; |
private final FeatureSource<SimpleFeatureType, SimpleFeature> fs; |
73 |
|
|
74 |
|
/** Caching the CRS of the layer **/ |
75 |
|
CoordinateReferenceSystem crs = null; |
76 |
|
|
77 |
/** |
/** |
78 |
* A unique ID which identifies the Layer in the Atlas. It's more important |
* A unique ID which identifies the Layer in the Atlas. It's more important |
79 |
* than it should be ;-) |
* than it should be ;-) |
88 |
|
|
89 |
private File sldFile; |
private File sldFile; |
90 |
|
|
91 |
private AttributeMetadataMap map; |
/** A map of simple attribute names to their meta-data **/ |
92 |
|
private AttributeMetadataMap<AttributeMetadataImpl> map; |
93 |
|
|
94 |
private Filter filter = Filter.INCLUDE; |
private Filter filter = Filter.INCLUDE; |
95 |
|
|
108 |
File sldFile) { |
File sldFile) { |
109 |
|
|
110 |
this.fs = fs; |
this.fs = fs; |
111 |
|
|
112 |
id = StyledFS.class.getSimpleName() |
id = StyledFS.class.getSimpleName() |
113 |
+ new Random(new Date().getTime()).nextInt(10000000); |
+ new Random(new Date().getTime()).nextInt(10000000); |
114 |
|
|
115 |
this.sldFile = sldFile; |
this.sldFile = sldFile; |
116 |
|
|
117 |
|
// datei existiert, dann lesen |
118 |
if (sldFile != null && sldFile.exists()) { |
if (sldFile != null && sldFile.exists()) { |
119 |
try { |
try { |
120 |
style = StylingUtil.loadSLD(sldFile)[0]; |
style = StylingUtil.loadSLD(sldFile)[0]; |
|
} catch (FileNotFoundException e) { |
|
|
LOGGER |
|
|
.debug("The SLD file passed was empty. Leaving the Style untouched. (We are in the constructor.. so its null"); |
|
121 |
} catch (Exception e) { |
} catch (Exception e) { |
122 |
LOGGER.warn("Reading SLD failed: " + sldFile, e); |
LOGGER.warn("Reading SLD failed: " + sldFile, e); |
123 |
|
style = null; |
124 |
} |
} |
125 |
} |
} |
126 |
|
|
127 |
title = new Translation(); |
title = new Translation(); |
128 |
desc = new Translation(); |
desc = new Translation(); |
129 |
|
|
156 |
} |
} |
157 |
|
|
158 |
public CoordinateReferenceSystem getCrs() { |
public CoordinateReferenceSystem getCrs() { |
159 |
CoordinateReferenceSystem crs = fs.getSchema().getCoordinateReferenceSystem(); |
if (crs == null) { |
160 |
if (fs.getSchema().getCoordinateReferenceSystem() == null) { |
crs = fs.getSchema().getCoordinateReferenceSystem(); |
161 |
LOGGER.warn("Could not determine the CRS of "+getTitle()+". Using default "+GeoImportUtil.getDefaultCRS()); |
if (fs.getSchema().getCoordinateReferenceSystem() == null) { |
162 |
crs = GeoImportUtil.getDefaultCRS(); |
LOGGER.warn("Could not determine the CRS of " + getTitle() |
163 |
|
+ ". Using default " + GeoImportUtil.getDefaultCRS()); |
164 |
|
crs = GeoImportUtil.getDefaultCRS(); |
165 |
|
} |
166 |
} |
} |
167 |
return crs; |
return crs; |
168 |
} |
} |
253 |
/** |
/** |
254 |
* |
* |
255 |
*/ |
*/ |
256 |
public AttributeMetadataMap getAttributeMetaDataMap() { |
public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() { |
257 |
if (map == null) { |
if (map == null) { |
258 |
|
|
259 |
map = new AttributeMetadataMap(); |
map = new AttributeMetadataImplMap(); |
260 |
|
|
261 |
|
// // Leaving out the first one, it will be the_geom |
262 |
|
// for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
263 |
|
// AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
264 |
|
// |
265 |
|
// AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
266 |
|
// new NameImpl(attDesc |
267 |
|
// .getName().getNamespaceURI(), attDesc |
268 |
|
// .getName().getLocalPart()), map.getLanguages()); |
269 |
|
// map.put(attDesc.getName(), attMetaData); |
270 |
|
// } |
271 |
|
|
272 |
// Leaving out the first one, it will be the_geom |
// Leaving out the first one, it will be the_geom |
273 |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
274 |
AttributeDescriptor att = fs.getSchema().getDescriptor(i); |
AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
275 |
|
|
276 |
AttributeMetaData attMetaData = new AttributeMetaData(i, att |
// TODO AttributeMetadataAS would be nicer, which would not work |
277 |
.getLocalName()); |
// with Translations ;-) |
278 |
map.put(i, attMetaData); |
AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
279 |
|
new NameImpl(attDesc.getName().getNamespaceURI(), |
280 |
|
attDesc.getName().getLocalPart()), map |
281 |
|
.getLanguages()); |
282 |
|
if (String.class.isAssignableFrom(attDesc.getType() |
283 |
|
.getBinding())) { |
284 |
|
// For Strings we add the "" as NODATA values |
285 |
|
attMetaData.addNodataValue(""); |
286 |
|
} |
287 |
|
map.put(attDesc.getName(), attMetaData); |
288 |
} |
} |
289 |
} |
} |
290 |
return map; |
return map; |
335 |
|
|
336 |
@Override |
@Override |
337 |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
338 |
final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection(); |
// final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = |
339 |
if (filter == Filter.EXCLUDE) |
// getFeatureCollection(); |
340 |
return new EmptyFeatureCollection(fc.getSchema()); |
// if (filter == Filter.EXCLUDE) |
341 |
if (filter == Filter.INCLUDE) |
// return new EmptyFeatureCollection(fc.getSchema()); |
342 |
return fc; |
// if (filter == Filter.INCLUDE) |
343 |
return fc.subCollection(filter); |
// return fc; |
344 |
|
// return fc.subCollection(filter); |
345 |
|
|
346 |
|
try { |
347 |
|
return getFeatureSource().getFeatures(filter); |
348 |
|
} catch (IOException e) { |
349 |
|
throw new RuntimeException(e); |
350 |
|
} |
351 |
} |
} |
352 |
|
|
353 |
@Override |
@Override |
365 |
return getGeoObject().getSchema(); |
return getGeoObject().getSchema(); |
366 |
} |
} |
367 |
|
|
368 |
|
/** |
369 |
|
* Tries to load a style from the file denoted in {@link #getSldFile()}. If |
370 |
|
* the file doesn't exits, return <code>null</code>; |
371 |
|
* @return <code>true</code> is style was loaded |
372 |
|
*/ |
373 |
|
public boolean loadStyle() { |
374 |
|
if (getSldFile() == null) |
375 |
|
return false; |
376 |
|
|
377 |
|
try { |
378 |
|
Style[] loadSLD = StylingUtil.loadSLD(getSldFile()); |
379 |
|
setStyle(loadSLD[0]); |
380 |
|
return true; |
381 |
|
} catch (Exception e) { |
382 |
|
return false; |
383 |
|
} |
384 |
|
|
385 |
|
} |
386 |
|
|
387 |
} |
} |