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