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.IOException; |
import java.io.IOException; |
33 |
import java.net.URL; |
import java.net.URL; |
|
import java.util.HashMap; |
|
|
import java.util.Map; |
|
34 |
|
|
35 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
36 |
|
|
37 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
38 |
import org.geotools.data.collection.CollectionDataStore; |
import org.geotools.data.collection.CollectionDataStore; |
39 |
import org.geotools.feature.AttributeType; |
import org.geotools.data.store.EmptyFeatureCollection; |
40 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
41 |
|
import org.geotools.feature.NameImpl; |
42 |
|
import org.geotools.feature.collection.SubFeatureCollection; |
43 |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
44 |
|
import org.geotools.styling.Style; |
45 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
46 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
47 |
import org.geotools.styling.Style; |
import org.opengis.feature.type.AttributeDescriptor; |
48 |
|
import org.opengis.filter.Filter; |
49 |
|
|
50 |
import schmitzm.geotools.feature.FeatureUtil; |
import schmitzm.geotools.feature.FeatureUtil; |
51 |
import skrueger.AttributeMetaData; |
import schmitzm.geotools.feature.FeatureUtil.GeometryForm; |
52 |
|
import skrueger.AttributeMetadataImpl; |
53 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
54 |
|
|
55 |
/** |
/** |
56 |
* This class provides a simple implementation of {@link StyledLayerInterface} for |
* This class provides a simple implementation of {@link StyledLayerInterface} |
57 |
* {@link FeatureCollection}. The uncache functionality is not supported, |
* for {@link FeatureCollection}. The uncache functionality is not supported, |
58 |
* because this class bases on an existing {@link FeatureCollection} object in |
* because this class bases on an existing {@link FeatureCollection} object in |
59 |
* memory. |
* memory. |
60 |
* |
* |
62 |
* (University of Bonn/Germany) |
* (University of Bonn/Germany) |
63 |
* @version 1.0 |
* @version 1.0 |
64 |
*/ |
*/ |
65 |
public class StyledFeatureCollection extends |
public class StyledFeatureCollection |
66 |
AbstractStyledLayer<FeatureCollection<SimpleFeatureType, SimpleFeature> > implements |
extends |
67 |
StyledFeatureCollectionInterface { |
AbstractStyledLayer<FeatureCollection<SimpleFeatureType, SimpleFeature>> |
68 |
|
implements StyledFeatureCollectionInterface { |
69 |
|
|
70 |
/** Holds the meta data for displaying a legend. */ |
/** Holds the meta data for displaying a legend. */ |
71 |
protected Map<Integer, AttributeMetaData> attrMetaData = null; |
protected AttributeMetadataMap<AttributeMetadataImpl> attrMetaData = null; |
72 |
|
|
73 |
/** |
/** |
74 |
* We be filled with a "virtual" {@link FeatureSource} on demand. |
* We be filled with a "virtual" {@link FeatureSource} on demand. |
76 |
private FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null; |
private FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null; |
77 |
|
|
78 |
/** |
/** |
79 |
|
* A Filter that will be applied internally applied to this |
80 |
|
* FeatureCollection |
81 |
|
**/ |
82 |
|
private Filter filter = Filter.INCLUDE; |
83 |
|
|
84 |
|
/** |
85 |
* Creates a styled {@link FeatureCollection} with language-specific |
* Creates a styled {@link FeatureCollection} with language-specific |
86 |
* informations. |
* informations. |
87 |
* |
* |
104 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
105 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
106 |
*/ |
*/ |
107 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
108 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
109 |
Translation title, Translation desc, Translation keywords, |
Translation title, Translation desc, Translation keywords, |
110 |
Style style, Map<Integer, AttributeMetaData> attrMetaData, |
Style style, AttributeMetadataMap attrMetaData, ImageIcon icon) { |
111 |
ImageIcon icon) { |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
112 |
super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry() |
.getCoordinateReferenceSystem(), id, title, desc, keywords, |
113 |
.getCoordinateSystem(), id, title, desc, keywords, style, icon); |
style, icon); |
114 |
setAttributeMetaData(attrMetaData); |
setAttributeMetaData(attrMetaData); |
115 |
} |
} |
116 |
|
|
135 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
136 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
137 |
*/ |
*/ |
138 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
139 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
140 |
Translation title, Translation desc, Translation keywords, |
Translation title, Translation desc, Translation keywords, |
141 |
StyledLayerStyle<Map<Integer, AttributeMetaData>> style, |
StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) { |
142 |
ImageIcon icon) { |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
143 |
super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry() |
.getCoordinateReferenceSystem(), id, title, desc, keywords, |
|
.getCoordinateSystem(), id, title, desc, keywords, |
|
144 |
style != null ? style.getGeoObjectStyle() : null, icon); |
style != null ? style.getGeoObjectStyle() : null, icon); |
145 |
setAttributeMetaData(style != null ? style.getMetaData() : null); |
setAttributeMetaData(style != null ? style.getMetaData() : null); |
146 |
} |
} |
162 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
163 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
164 |
*/ |
*/ |
165 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
166 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
167 |
Translation title, Style style) { |
Translation title, Style style) { |
168 |
this(fc, id, title, null, null, style, null, null); |
this(fc, id, title, null, null, style, null, null); |
169 |
} |
} |
191 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
192 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
193 |
*/ |
*/ |
194 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
195 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
196 |
String title, String desc, String keywords, Style style, |
String title, String desc, String keywords, Style style, |
197 |
Map<Integer, AttributeMetaData> attrMetaData, ImageIcon icon) { |
AttributeMetadataMap attrMetaData, ImageIcon icon) { |
198 |
this(fc, id, (Translation) null, null, null, style, attrMetaData, icon); |
this(fc, id, (Translation) null, null, null, style, attrMetaData, icon); |
199 |
setTitle(title); |
setTitle(title); |
200 |
setDesc(desc); |
setDesc(desc); |
222 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
223 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
224 |
*/ |
*/ |
225 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
226 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
227 |
String title, String desc, String keywords, |
String title, String desc, String keywords, |
228 |
StyledLayerStyle<Map<Integer, AttributeMetaData>> style, |
StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) { |
|
ImageIcon icon) { |
|
229 |
this(fc, id, title, desc, keywords, style != null ? style |
this(fc, id, title, desc, keywords, style != null ? style |
230 |
.getGeoObjectStyle() : null, style != null ? style |
.getGeoObjectStyle() : null, style != null ? style |
231 |
.getMetaData() : null, icon); |
.getMetaData() : null, icon); |
248 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
249 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
250 |
*/ |
*/ |
251 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
252 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
253 |
String title, Style style) { |
String title, Style style) { |
254 |
this(fc, id, title, null, null, style, null, null); |
this(fc, id, title, null, null, style, null, null); |
255 |
} |
} |
271 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
272 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
273 |
*/ |
*/ |
274 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
275 |
String title, StyledLayerStyle<Map<Integer, AttributeMetaData>> style) { |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
276 |
|
String title, StyledLayerStyle<AttributeMetadataMap> style) { |
277 |
this(fc, id, title, null, null, style != null ? style |
this(fc, id, title, null, null, style != null ? style |
278 |
.getGeoObjectStyle() : null, style != null ? style |
.getGeoObjectStyle() : null, style != null ? style |
279 |
.getMetaData() : null, null); |
.getMetaData() : null, null); |
291 |
/** |
/** |
292 |
* Returns the meta data needed for displaying a legend. |
* Returns the meta data needed for displaying a legend. |
293 |
*/ |
*/ |
294 |
public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() { |
public AttributeMetadataMap getAttributeMetaDataMap() { |
295 |
return attrMetaData; |
return attrMetaData; |
296 |
} |
} |
297 |
|
|
303 |
* @param attrMetaData |
* @param attrMetaData |
304 |
* map of attribute meta data |
* map of attribute meta data |
305 |
*/ |
*/ |
306 |
public void setAttributeMetaData( |
public void setAttributeMetaData(AttributeMetadataMap attrMetaData) { |
|
Map<Integer, AttributeMetaData> attrMetaData) { |
|
307 |
this.attrMetaData = (attrMetaData != null) ? attrMetaData |
this.attrMetaData = (attrMetaData != null) ? attrMetaData |
308 |
: createDefaultAttributeMetaDataMap(geoObject); |
: createDefaultAttributeMetaDataMap(geoObject); |
309 |
} |
} |
315 |
* @param fc |
* @param fc |
316 |
* a {@link FeatureCollection} |
* a {@link FeatureCollection} |
317 |
*/ |
*/ |
318 |
public static Map<Integer, AttributeMetaData> createDefaultAttributeMetaDataMap( |
public static AttributeMetadataMap createDefaultAttributeMetaDataMap( |
319 |
FeatureCollection fc) { |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc) { |
320 |
HashMap<Integer, AttributeMetaData> metaDataMap = new HashMap<Integer, AttributeMetaData>(); |
AttributeMetadataMap metaDataMap = new AttributeMetadataImplMap(); |
321 |
SimpleFeatureType ftype = fc.getSchema(); |
SimpleFeatureType ftype = fc.getSchema(); |
322 |
for (int i = 0; i < ftype.getAttributeCount(); i++) { |
for (int i = 0; i < ftype.getAttributeCount(); i++) { |
323 |
AttributeType aType = ftype.getAttributeType(i); |
AttributeDescriptor aDesc = ftype.getAttributeDescriptors().get(i); |
324 |
if (aType != ftype.getDefaultGeometry()) |
if (!FeatureUtil.isGeometryAttribute(aDesc)) |
325 |
metaDataMap.put(i, new AttributeMetaData(i, // Column no. |
metaDataMap.put(aDesc.getName(), new AttributeMetadataImpl( |
326 |
true, // visible |
new NameImpl(aDesc.getName().getNamespaceURI(), aDesc |
327 |
new Translation(aType.getName()), // Column name |
.getName().getLocalPart()), true, // visible |
328 |
new Translation(), // description |
new Translation(aDesc.getLocalName()), // Column name |
329 |
|
new Translation(aDesc.getLocalName()), // description |
330 |
"" // Unit |
"" // Unit |
331 |
)); |
)); |
332 |
} |
} |
360 |
/** It will be recreated on the next getFetureSource() **/ |
/** It will be recreated on the next getFetureSource() **/ |
361 |
featureSource = null; |
featureSource = null; |
362 |
|
|
363 |
LOGGER |
LOGGER.warn("Uncache onyl uncached any virtual FeatureSource. Object remains in memory."); |
|
.warn("Uncache onyl uncached any virtual FeatureSource. Object remains in memory."); |
|
364 |
} |
} |
365 |
|
|
366 |
/* |
/* |
373 |
} |
} |
374 |
|
|
375 |
/** |
/** |
376 |
* Same as {@link #getGeoObject()} method, but complies to the {@link StyledFeaturesInterface} |
* Same as {@link #getGeoObject()} method, but complies to the |
377 |
|
* {@link StyledFeaturesInterface}. The associated {@link Filter} is NOT |
378 |
|
* automatically applied. |
379 |
|
* |
380 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
381 |
|
* @see #getFeatureCollectionFiltered() |
382 |
*/ |
*/ |
383 |
@Override |
@Override |
384 |
public FeatureCollection getFeatureCollection() { |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() { |
385 |
return getGeoObject(); |
return getGeoObject(); |
386 |
} |
} |
387 |
|
|
388 |
/** |
/** |
389 |
|
* Same as {@link #getGeoObject()} method, but complies to the |
390 |
|
* {@link StyledFeaturesInterface}. The associated {@link Filter} is |
391 |
|
* automatically applied by creating a {@link SubFeatureCollection}. |
392 |
|
* |
393 |
|
* @see {@link StyledFeaturesInterface} |
394 |
|
* @see #getFeatureCollectionFiltered() |
395 |
|
*/ |
396 |
|
@Override |
397 |
|
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
398 |
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection(); |
399 |
|
if (filter == Filter.EXCLUDE) |
400 |
|
return new EmptyFeatureCollection(fc.getSchema()); |
401 |
|
if (filter == Filter.INCLUDE) |
402 |
|
return fc; |
403 |
|
return fc.subCollection(filter); |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
* Returns a virtual {@link FeatureSource} to access the |
* Returns a virtual {@link FeatureSource} to access the |
408 |
* {@link FeatureCollection}. Once created, it will be reused until |
* {@link FeatureCollection}. Once created, it will be reused until |
409 |
* {@link #uncache()} is called.<br/> |
* {@link #uncache()} is called.<br/> |
410 |
|
* |
411 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
412 |
*/ |
*/ |
413 |
@Override |
@Override |
414 |
public FeatureSource getFeatureSource() { |
public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() { |
415 |
if (featureSource == null) { |
if (featureSource == null) { |
416 |
CollectionDataStore store = new CollectionDataStore(getGeoObject()); |
CollectionDataStore store = new CollectionDataStore(getGeoObject()); |
417 |
try { |
try { |
425 |
return featureSource; |
return featureSource; |
426 |
} |
} |
427 |
|
|
428 |
|
@Override |
429 |
|
public Filter getFilter() { |
430 |
|
return filter; |
431 |
|
} |
432 |
|
|
433 |
|
@Override |
434 |
|
public void setFilter(Filter filter) { |
435 |
|
this.filter = filter; |
436 |
|
} |
437 |
|
|
438 |
|
@Override |
439 |
|
public SimpleFeatureType getSchema() { |
440 |
|
return getGeoObject().getSchema(); |
441 |
|
} |
442 |
|
|
443 |
|
@Override |
444 |
|
public ReferencedEnvelope getReferencedEnvelope() { |
445 |
|
return new ReferencedEnvelope(getEnvelope(), getCrs()); |
446 |
|
} |
447 |
|
|
448 |
|
/** |
449 |
|
* Defaults to the GeometryForm determined with FeatureUtil.getGeometryForm, |
450 |
|
* but can be set to override ANY. |
451 |
|
*/ |
452 |
|
private GeometryForm geometryForm; |
453 |
|
|
454 |
|
/** |
455 |
|
* Defaults to the GeometryForm determined with FeatureUtil.getGeometryForm, |
456 |
|
* but can be set to override ANY. |
457 |
|
*/ |
458 |
|
|
459 |
|
@Override |
460 |
|
public GeometryForm getGeometryForm() { |
461 |
|
if (geometryForm == null) { |
462 |
|
geometryForm = FeatureUtil.getGeometryForm(getSchema()); |
463 |
|
} |
464 |
|
return geometryForm; |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
|
* Defaults to the GeometryForm determined with FeatureUtil.getGeometryForm, |
469 |
|
* but can be set to override ANY. |
470 |
|
*/ |
471 |
|
public void setGeometryForm(GeometryForm geometryForm) { |
472 |
|
this.geometryForm = geometryForm; |
473 |
|
} |
474 |
|
|
475 |
} |
} |