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.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.styling.Style; |
import org.geotools.styling.Style; |
44 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
45 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
46 |
import org.opengis.feature.type.AttributeDescriptor; |
import org.opengis.feature.type.AttributeDescriptor; |
47 |
|
import org.opengis.filter.Filter; |
48 |
|
|
49 |
import schmitzm.geotools.feature.FeatureUtil; |
import schmitzm.geotools.feature.FeatureUtil; |
50 |
import skrueger.AttributeMetaData; |
import skrueger.AttributeMetadataImpl; |
51 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
52 |
|
|
53 |
/** |
/** |
54 |
* This class provides a simple implementation of {@link StyledLayerInterface} for |
* This class provides a simple implementation of {@link StyledLayerInterface} |
55 |
* {@link FeatureCollection}. The uncache functionality is not supported, |
* for {@link FeatureCollection}. The uncache functionality is not supported, |
56 |
* because this class bases on an existing {@link FeatureCollection} object in |
* because this class bases on an existing {@link FeatureCollection} object in |
57 |
* memory. |
* memory. |
58 |
* |
* |
60 |
* (University of Bonn/Germany) |
* (University of Bonn/Germany) |
61 |
* @version 1.0 |
* @version 1.0 |
62 |
*/ |
*/ |
63 |
public class StyledFeatureCollection extends |
public class StyledFeatureCollection |
64 |
AbstractStyledLayer<FeatureCollection<SimpleFeatureType, SimpleFeature> > implements |
extends |
65 |
StyledFeatureCollectionInterface { |
AbstractStyledLayer<FeatureCollection<SimpleFeatureType, SimpleFeature>> |
66 |
|
implements StyledFeatureCollectionInterface { |
67 |
|
|
68 |
/** Holds the meta data for displaying a legend. */ |
/** Holds the meta data for displaying a legend. */ |
69 |
protected AttributeMetadataMap attrMetaData = null; |
protected AttributeMetadataMap<AttributeMetadataImpl> attrMetaData = null; |
70 |
|
|
71 |
/** |
/** |
72 |
* We be filled with a "virtual" {@link FeatureSource} on demand. |
* We be filled with a "virtual" {@link FeatureSource} on demand. |
74 |
private FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null; |
private FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null; |
75 |
|
|
76 |
/** |
/** |
77 |
|
* A Filter that will be applied internally applied to this |
78 |
|
* FeatureCollection |
79 |
|
**/ |
80 |
|
private Filter filter = Filter.INCLUDE; |
81 |
|
|
82 |
|
/** |
83 |
* Creates a styled {@link FeatureCollection} with language-specific |
* Creates a styled {@link FeatureCollection} with language-specific |
84 |
* informations. |
* informations. |
85 |
* |
* |
102 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
103 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
104 |
*/ |
*/ |
105 |
public StyledFeatureCollection(FeatureCollection<SimpleFeatureType,SimpleFeature> fc, String id, |
public StyledFeatureCollection( |
106 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
107 |
Translation title, Translation desc, Translation keywords, |
Translation title, Translation desc, Translation keywords, |
108 |
Style style, AttributeMetadataMap attrMetaData, |
Style style, AttributeMetadataMap attrMetaData, ImageIcon icon) { |
|
ImageIcon icon) { |
|
109 |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
110 |
.getCoordinateReferenceSystem(), id, title, desc, keywords, style, icon); |
.getCoordinateReferenceSystem(), id, title, desc, keywords, |
111 |
|
style, icon); |
112 |
setAttributeMetaData(attrMetaData); |
setAttributeMetaData(attrMetaData); |
113 |
} |
} |
114 |
|
|
133 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
134 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
135 |
*/ |
*/ |
136 |
public StyledFeatureCollection(FeatureCollection<SimpleFeatureType,SimpleFeature> fc, String id, |
public StyledFeatureCollection( |
137 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
138 |
Translation title, Translation desc, Translation keywords, |
Translation title, Translation desc, Translation keywords, |
139 |
StyledLayerStyle<AttributeMetadataMap> style, |
StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) { |
|
ImageIcon icon) { |
|
140 |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
141 |
.getCoordinateReferenceSystem(), id, title, desc, keywords, |
.getCoordinateReferenceSystem(), id, title, desc, keywords, |
142 |
style != null ? style.getGeoObjectStyle() : null, icon); |
style != null ? style.getGeoObjectStyle() : null, icon); |
160 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
161 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
162 |
*/ |
*/ |
163 |
public StyledFeatureCollection(FeatureCollection<SimpleFeatureType,SimpleFeature> fc, String id, |
public StyledFeatureCollection( |
164 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
165 |
Translation title, Style style) { |
Translation title, Style style) { |
166 |
this(fc, id, title, null, null, style, null, null); |
this(fc, id, title, null, null, style, null, null); |
167 |
} |
} |
189 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
190 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
191 |
*/ |
*/ |
192 |
public StyledFeatureCollection(FeatureCollection<SimpleFeatureType,SimpleFeature> fc, String id, |
public StyledFeatureCollection( |
193 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
194 |
String title, String desc, String keywords, Style style, |
String title, String desc, String keywords, Style style, |
195 |
AttributeMetadataMap attrMetaData, ImageIcon icon) { |
AttributeMetadataMap attrMetaData, ImageIcon icon) { |
196 |
this(fc, id, (Translation) null, null, null, style, attrMetaData, icon); |
this(fc, id, (Translation) null, null, null, style, attrMetaData, icon); |
220 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
221 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
222 |
*/ |
*/ |
223 |
public StyledFeatureCollection(FeatureCollection<SimpleFeatureType,SimpleFeature> fc, String id, |
public StyledFeatureCollection( |
224 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
225 |
String title, String desc, String keywords, |
String title, String desc, String keywords, |
226 |
StyledLayerStyle<AttributeMetadataMap> style, |
StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) { |
|
ImageIcon icon) { |
|
227 |
this(fc, id, title, desc, keywords, style != null ? style |
this(fc, id, title, desc, keywords, style != null ? style |
228 |
.getGeoObjectStyle() : null, style != null ? style |
.getGeoObjectStyle() : null, style != null ? style |
229 |
.getMetaData() : null, icon); |
.getMetaData() : null, icon); |
246 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
247 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
248 |
*/ |
*/ |
249 |
public StyledFeatureCollection(FeatureCollection<SimpleFeatureType,SimpleFeature> fc, String id, |
public StyledFeatureCollection( |
250 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
251 |
String title, Style style) { |
String title, Style style) { |
252 |
this(fc, id, title, null, null, style, null, null); |
this(fc, id, title, null, null, style, null, null); |
253 |
} |
} |
269 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
270 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
271 |
*/ |
*/ |
272 |
public StyledFeatureCollection(FeatureCollection<SimpleFeatureType,SimpleFeature> fc, String id, |
public StyledFeatureCollection( |
273 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
274 |
String title, StyledLayerStyle<AttributeMetadataMap> style) { |
String title, StyledLayerStyle<AttributeMetadataMap> style) { |
275 |
this(fc, id, title, null, null, style != null ? style |
this(fc, id, title, null, null, style != null ? style |
276 |
.getGeoObjectStyle() : null, style != null ? style |
.getGeoObjectStyle() : null, style != null ? style |
301 |
* @param attrMetaData |
* @param attrMetaData |
302 |
* map of attribute meta data |
* map of attribute meta data |
303 |
*/ |
*/ |
304 |
public void setAttributeMetaData( |
public void setAttributeMetaData(AttributeMetadataMap attrMetaData) { |
|
AttributeMetadataMap attrMetaData) { |
|
305 |
this.attrMetaData = (attrMetaData != null) ? attrMetaData |
this.attrMetaData = (attrMetaData != null) ? attrMetaData |
306 |
: createDefaultAttributeMetaDataMap(geoObject); |
: createDefaultAttributeMetaDataMap(geoObject); |
307 |
} |
} |
314 |
* a {@link FeatureCollection} |
* a {@link FeatureCollection} |
315 |
*/ |
*/ |
316 |
public static AttributeMetadataMap createDefaultAttributeMetaDataMap( |
public static AttributeMetadataMap createDefaultAttributeMetaDataMap( |
317 |
FeatureCollection<SimpleFeatureType,SimpleFeature> fc) { |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc) { |
318 |
AttributeMetadataMap metaDataMap = new AttributeMetadataMap(); |
AttributeMetadataMap metaDataMap = new AttributeMetadataImplMap(); |
319 |
SimpleFeatureType ftype = fc.getSchema(); |
SimpleFeatureType ftype = fc.getSchema(); |
320 |
for (int i = 0; i < ftype.getAttributeCount(); i++) { |
for (int i = 0; i < ftype.getAttributeCount(); i++) { |
321 |
AttributeDescriptor aType = ftype.getAttributeDescriptors().get(i); |
AttributeDescriptor aDesc = ftype.getAttributeDescriptors().get(i); |
322 |
if (aType != ftype.getGeometryDescriptor()) |
if (aDesc != ftype.getGeometryDescriptor()) |
323 |
metaDataMap.put(i, new AttributeMetaData(i, // Column no. |
metaDataMap.put(aDesc.getName(), new AttributeMetadataImpl( new NameImpl( aDesc.getName().getNamespaceURI(), aDesc.getName().getLocalPart()), |
324 |
true, // visible |
true, // visible |
325 |
new Translation(aType.getLocalName()), // Column name |
new Translation(aDesc.getLocalName()), // Column name |
326 |
new Translation(), // description |
new Translation(aDesc.getLocalName()), // description |
327 |
"" // Unit |
"" // Unit |
328 |
)); |
)); |
329 |
} |
} |
371 |
} |
} |
372 |
|
|
373 |
/** |
/** |
374 |
* Same as {@link #getGeoObject()} method, but complies to the {@link StyledFeaturesInterface} |
* Same as {@link #getGeoObject()} method, but complies to the |
375 |
|
* {@link StyledFeaturesInterface}. The associated {@link Filter} is NOT |
376 |
|
* automatically applied. |
377 |
|
* |
378 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
379 |
|
* @see #getFeatureCollectionFiltered() |
380 |
*/ |
*/ |
381 |
@Override |
@Override |
382 |
public FeatureCollection<SimpleFeatureType,SimpleFeature> getFeatureCollection() { |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() { |
383 |
return getGeoObject(); |
return getGeoObject(); |
384 |
} |
} |
385 |
|
|
386 |
/** |
/** |
387 |
|
* Same as {@link #getGeoObject()} method, but complies to the |
388 |
|
* {@link StyledFeaturesInterface}. The associated {@link Filter} is |
389 |
|
* automatically applied by creating a {@link SubFeatureCollection}. |
390 |
|
* |
391 |
|
* @see {@link StyledFeaturesInterface} |
392 |
|
* @see #getFeatureCollectionFiltered() |
393 |
|
*/ |
394 |
|
@Override |
395 |
|
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
396 |
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection(); |
397 |
|
if (filter == Filter.EXCLUDE) |
398 |
|
return new EmptyFeatureCollection(fc.getSchema()); |
399 |
|
if (filter == Filter.INCLUDE) |
400 |
|
return fc; |
401 |
|
return fc.subCollection(filter); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
* Returns a virtual {@link FeatureSource} to access the |
* Returns a virtual {@link FeatureSource} to access the |
406 |
* {@link FeatureCollection}. Once created, it will be reused until |
* {@link FeatureCollection}. Once created, it will be reused until |
407 |
* {@link #uncache()} is called.<br/> |
* {@link #uncache()} is called.<br/> |
408 |
|
* |
409 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
410 |
*/ |
*/ |
411 |
@Override |
@Override |
412 |
public FeatureSource<SimpleFeatureType,SimpleFeature> getFeatureSource() { |
public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() { |
413 |
if (featureSource == null) { |
if (featureSource == null) { |
414 |
CollectionDataStore store = new CollectionDataStore(getGeoObject()); |
CollectionDataStore store = new CollectionDataStore(getGeoObject()); |
415 |
try { |
try { |
423 |
return featureSource; |
return featureSource; |
424 |
} |
} |
425 |
|
|
426 |
|
@Override |
427 |
|
public Filter getFilter() { |
428 |
|
return filter; |
429 |
|
} |
430 |
|
|
431 |
|
@Override |
432 |
|
public void setFilter(Filter filter) { |
433 |
|
this.filter = filter; |
434 |
|
} |
435 |
|
|
436 |
|
@Override |
437 |
|
public SimpleFeatureType getSchema() { |
438 |
|
return getGeoObject().getSchema(); |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
} |
} |