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 |
|
|
32 |
import java.io.File; |
import java.io.File; |
|
import java.io.FileNotFoundException; |
|
33 |
import java.io.IOException; |
import java.io.IOException; |
34 |
import java.net.URL; |
import java.net.URL; |
35 |
import java.util.Date; |
import java.util.Date; |
40 |
|
|
41 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
42 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
|
import org.geotools.data.store.EmptyFeatureCollection; |
|
43 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
44 |
|
import org.geotools.feature.NameImpl; |
45 |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
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; |
62 |
* This class enables a non Atlas context to use the Atlas LayerPanel |
* This class enables a non Atlas context to use the Atlas LayerPanel |
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 Tzeggai</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 ;-) |
80 |
*/ |
*/ |
81 |
private String id; |
final private String id; |
82 |
|
|
83 |
private Style style; |
private Style style; |
84 |
|
|
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> attMap; |
93 |
|
|
94 |
private Filter filter = Filter.INCLUDE; |
private Filter filter = Filter.INCLUDE; |
95 |
|
|
103 |
* @param sldFile |
* @param sldFile |
104 |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
105 |
* import and associate with this {@link StyledFS} |
* import and associate with this {@link StyledFS} |
106 |
|
* |
107 |
|
* @param id |
108 |
|
* <code>null</code> is allowed and will autogenerate an id |
109 |
*/ |
*/ |
110 |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
111 |
File sldFile) { |
File sldFile, String id) { |
112 |
|
|
113 |
this.fs = fs; |
this.fs = fs; |
114 |
id = StyledFS.class.getSimpleName() |
|
115 |
+ new Random(new Date().getTime()).nextInt(10000000); |
if (id == null) { |
116 |
|
this.id = StyledFS.class.getSimpleName() |
117 |
|
+ new Random(new Date().getTime()).nextInt(10000000); |
118 |
|
} else { |
119 |
|
this.id = id; |
120 |
|
} |
121 |
|
|
122 |
this.sldFile = sldFile; |
this.sldFile = sldFile; |
123 |
|
|
124 |
|
// datei existiert, dann lesen |
125 |
if (sldFile != null && sldFile.exists()) { |
if (sldFile != null && sldFile.exists()) { |
126 |
try { |
try { |
127 |
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"); |
|
128 |
} catch (Exception e) { |
} catch (Exception e) { |
129 |
LOGGER.warn("Reading SLD failed: " + sldFile, e); |
LOGGER.warn("Reading SLD failed: " + sldFile, e); |
130 |
|
style = null; |
131 |
} |
} |
132 |
} |
} |
133 |
|
|
134 |
title = new Translation(); |
title = new Translation(); |
135 |
desc = new Translation(); |
desc = new Translation(); |
136 |
|
|
142 |
} |
} |
143 |
|
|
144 |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) { |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) { |
145 |
this(fs, null); |
this(fs, (File) null, null); |
146 |
|
} |
147 |
|
|
148 |
|
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
149 |
|
String id) { |
150 |
|
this(fs, null, id); |
151 |
} |
} |
152 |
|
|
153 |
public void dispose() { |
public void dispose() { |
168 |
} |
} |
169 |
|
|
170 |
public CoordinateReferenceSystem getCrs() { |
public CoordinateReferenceSystem getCrs() { |
171 |
CoordinateReferenceSystem crs = fs.getSchema().getCoordinateReferenceSystem(); |
if (crs == null) { |
172 |
if (fs.getSchema().getCoordinateReferenceSystem() == null) { |
crs = fs.getSchema().getCoordinateReferenceSystem(); |
173 |
LOGGER.warn("Could not determine the CRS of "+getTitle()+". Using default "+GeoImportUtil.getDefaultCRS()); |
if (crs == null) { |
174 |
crs = GeoImportUtil.getDefaultCRS(); |
|
175 |
|
crs = fs.getSchema().getGeometryDescriptor() |
176 |
|
.getCoordinateReferenceSystem(); |
177 |
|
|
178 |
|
if (crs == null) { |
179 |
|
LOGGER.warn("Could not determine the CRS of " + getTitle() |
180 |
|
+ ". Using default " |
181 |
|
+ GeoImportUtil.getDefaultCRS()); |
182 |
|
crs = GeoImportUtil.getDefaultCRS(); |
183 |
|
} |
184 |
|
} |
185 |
} |
} |
186 |
return crs; |
return crs; |
187 |
} |
} |
272 |
/** |
/** |
273 |
* |
* |
274 |
*/ |
*/ |
275 |
public AttributeMetadataMap getAttributeMetaDataMap() { |
@Override |
276 |
if (map == null) { |
public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() { |
277 |
|
if (attMap == null) { |
278 |
map = new AttributeMetadataMap(); |
attMap = StyledLayerUtil |
279 |
|
.createDefaultAttributeMetadataMap(getSchema()); |
|
// Leaving out the first one, it will be the_geom |
|
|
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
|
|
AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i); |
|
|
|
|
|
AttributeMetadata attMetaData = new AttributeMetadata(attDesc.getName()); |
|
|
map.put(attDesc.getName(), attMetaData); |
|
|
} |
|
280 |
} |
} |
281 |
return map; |
return attMap; |
282 |
} |
} |
283 |
|
|
284 |
/** |
/** |
285 |
* @return The {@link File} where the SLD was loaded from or |
* @return The {@link File} where the SLD was loaded from or |
286 |
* <code>null</code> if there didn't exist a {@link File}. |
* <code>null</code> if there didn't exist a {@link File}. |
287 |
* |
* |
288 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
|
* Krüger</a> |
|
289 |
*/ |
*/ |
290 |
public File getSldFile() { |
public File getSldFile() { |
291 |
return sldFile; |
return sldFile; |
292 |
} |
} |
293 |
|
|
294 |
|
/** |
295 |
|
* Associates this .sld with the {@link FeatureSource}, but does not |
296 |
|
* automatically load the file. It must not even exist. |
297 |
|
* |
298 |
|
* @param sldFile |
299 |
|
*/ |
300 |
public void setSldFile(File sldFile) { |
public void setSldFile(File sldFile) { |
301 |
this.sldFile = sldFile; |
this.sldFile = sldFile; |
302 |
} |
} |
331 |
|
|
332 |
@Override |
@Override |
333 |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
334 |
final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection(); |
// final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = |
335 |
if (filter == Filter.EXCLUDE) |
// getFeatureCollection(); |
336 |
return new EmptyFeatureCollection(fc.getSchema()); |
// if (filter == Filter.EXCLUDE) |
337 |
if (filter == Filter.INCLUDE) |
// return new EmptyFeatureCollection(fc.getSchema()); |
338 |
return fc; |
// if (filter == Filter.INCLUDE) |
339 |
return fc.subCollection(filter); |
// return fc; |
340 |
|
// return fc.subCollection(filter); |
341 |
|
|
342 |
|
try { |
343 |
|
if (filter != Filter.INCLUDE) |
344 |
|
return getFeatureSource().getFeatures(filter); |
345 |
|
else |
346 |
|
return getFeatureSource().getFeatures(); |
347 |
|
} catch (IOException e) { |
348 |
|
throw new RuntimeException(e); |
349 |
|
} |
350 |
} |
} |
351 |
|
|
352 |
@Override |
@Override |
364 |
return getGeoObject().getSchema(); |
return getGeoObject().getSchema(); |
365 |
} |
} |
366 |
|
|
367 |
|
/** |
368 |
|
* Tries to load a style from the file denoted in {@link #getSldFile()}. If |
369 |
|
* the file doesn't exits, return <code>null</code>; |
370 |
|
* |
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 |
|
public void setTitle(String title) { |
388 |
|
setTitle(new Translation(title)); |
389 |
|
} |
390 |
|
|
391 |
|
public void setDesc(String desc) { |
392 |
|
setDesc(new Translation(desc)); |
393 |
|
} |
394 |
|
|
395 |
|
public void setCRS(CoordinateReferenceSystem crs2) { |
396 |
|
crs = crs2; |
397 |
|
} |
398 |
|
|
399 |
|
@Override |
400 |
|
public ReferencedEnvelope getReferencedEnvelope() { |
401 |
|
return new ReferencedEnvelope(getEnvelope(), getCrs()); |
402 |
|
} |
403 |
|
|
404 |
} |
} |