2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focussing (not only) on Java Swing |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
* and the Geotools library. |
7 |
* |
* |
8 |
* The SCHMITZM project is hosted at: |
* The SCHMITZM project is hosted at: |
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.FeatureType; |
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; |
import org.geotools.styling.Style; |
45 |
|
import org.opengis.feature.simple.SimpleFeature; |
46 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
47 |
|
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 skrueger.AttributeMetadataImpl; |
52 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
53 |
|
|
54 |
/** |
/** |
55 |
* This class provides a simple implementation of {@link StyledLayerInterface} for |
* This class provides a simple implementation of {@link StyledLayerInterface} |
56 |
* {@link FeatureCollection}. The uncache functionality is not supported, |
* for {@link FeatureCollection}. The uncache functionality is not supported, |
57 |
* because this class bases on an existing {@link FeatureCollection} object in |
* because this class bases on an existing {@link FeatureCollection} object in |
58 |
* memory. |
* memory. |
59 |
* |
* |
61 |
* (University of Bonn/Germany) |
* (University of Bonn/Germany) |
62 |
* @version 1.0 |
* @version 1.0 |
63 |
*/ |
*/ |
64 |
public class StyledFeatureCollection extends |
public class StyledFeatureCollection |
65 |
AbstractStyledLayer<FeatureCollection> implements |
extends |
66 |
StyledFeatureCollectionInterface { |
AbstractStyledLayer<FeatureCollection<SimpleFeatureType, SimpleFeature>> |
67 |
|
implements StyledFeatureCollectionInterface { |
68 |
|
|
69 |
/** Holds the meta data for displaying a legend. */ |
/** Holds the meta data for displaying a legend. */ |
70 |
protected Map<Integer, AttributeMetaData> attrMetaData = null; |
protected AttributeMetadataMap<AttributeMetadataImpl> attrMetaData = null; |
71 |
|
|
72 |
/** |
/** |
73 |
* We be filled with a "virtual" {@link FeatureSource} on demand. |
* We be filled with a "virtual" {@link FeatureSource} on demand. |
74 |
*/ |
*/ |
75 |
private FeatureSource featureSource = null; |
private FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null; |
76 |
|
|
77 |
|
/** |
78 |
|
* A Filter that will be applied internally applied to this |
79 |
|
* FeatureCollection |
80 |
|
**/ |
81 |
|
private Filter filter = Filter.INCLUDE; |
82 |
|
|
83 |
/** |
/** |
84 |
* Creates a styled {@link FeatureCollection} with language-specific |
* Creates a styled {@link FeatureCollection} with language-specific |
103 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
104 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
105 |
*/ |
*/ |
106 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
107 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
108 |
Translation title, Translation desc, Translation keywords, |
Translation title, Translation desc, Translation keywords, |
109 |
Style style, Map<Integer, AttributeMetaData> attrMetaData, |
Style style, AttributeMetadataMap attrMetaData, ImageIcon icon) { |
110 |
ImageIcon icon) { |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
111 |
super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry() |
.getCoordinateReferenceSystem(), id, title, desc, keywords, |
112 |
.getCoordinateSystem(), id, title, desc, keywords, style, icon); |
style, icon); |
113 |
setAttributeMetaData(attrMetaData); |
setAttributeMetaData(attrMetaData); |
114 |
} |
} |
115 |
|
|
134 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
135 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
136 |
*/ |
*/ |
137 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
138 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
139 |
Translation title, Translation desc, Translation keywords, |
Translation title, Translation desc, Translation keywords, |
140 |
StyledLayerStyle<Map<Integer, AttributeMetaData>> style, |
StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) { |
141 |
ImageIcon icon) { |
super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor() |
142 |
super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry() |
.getCoordinateReferenceSystem(), id, title, desc, keywords, |
|
.getCoordinateSystem(), id, title, desc, keywords, |
|
143 |
style != null ? style.getGeoObjectStyle() : null, icon); |
style != null ? style.getGeoObjectStyle() : null, icon); |
144 |
setAttributeMetaData(style != null ? style.getMetaData() : null); |
setAttributeMetaData(style != null ? style.getMetaData() : null); |
145 |
} |
} |
161 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
162 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
163 |
*/ |
*/ |
164 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
165 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
166 |
Translation title, Style style) { |
Translation title, Style style) { |
167 |
this(fc, id, title, null, null, style, null, null); |
this(fc, id, title, null, null, style, null, null); |
168 |
} |
} |
190 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
191 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
192 |
*/ |
*/ |
193 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
194 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
195 |
String title, String desc, String keywords, Style style, |
String title, String desc, String keywords, Style style, |
196 |
Map<Integer, AttributeMetaData> attrMetaData, ImageIcon icon) { |
AttributeMetadataMap attrMetaData, ImageIcon icon) { |
197 |
this(fc, id, (Translation) null, null, null, style, attrMetaData, icon); |
this(fc, id, (Translation) null, null, null, style, attrMetaData, icon); |
198 |
setTitle(title); |
setTitle(title); |
199 |
setDesc(desc); |
setDesc(desc); |
221 |
* @exception IllegalArgumentException |
* @exception IllegalArgumentException |
222 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
223 |
*/ |
*/ |
224 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
225 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
226 |
String title, String desc, String keywords, |
String title, String desc, String keywords, |
227 |
StyledLayerStyle<Map<Integer, AttributeMetaData>> style, |
StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) { |
|
ImageIcon icon) { |
|
228 |
this(fc, id, title, desc, keywords, style != null ? style |
this(fc, id, title, desc, keywords, style != null ? style |
229 |
.getGeoObjectStyle() : null, style != null ? style |
.getGeoObjectStyle() : null, style != null ? style |
230 |
.getMetaData() : null, icon); |
.getMetaData() : null, icon); |
247 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
248 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
249 |
*/ |
*/ |
250 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
251 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
252 |
String title, Style style) { |
String title, Style style) { |
253 |
this(fc, id, title, null, null, style, null, null); |
this(fc, id, title, null, null, style, null, null); |
254 |
} |
} |
270 |
* if {@code null} is given as ID or geo object |
* if {@code null} is given as ID or geo object |
271 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
272 |
*/ |
*/ |
273 |
public StyledFeatureCollection(FeatureCollection fc, String id, |
public StyledFeatureCollection( |
274 |
String title, StyledLayerStyle<Map<Integer, AttributeMetaData>> style) { |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id, |
275 |
|
String title, StyledLayerStyle<AttributeMetadataMap> style) { |
276 |
this(fc, id, title, null, null, style != null ? style |
this(fc, id, title, null, null, style != null ? style |
277 |
.getGeoObjectStyle() : null, style != null ? style |
.getGeoObjectStyle() : null, style != null ? style |
278 |
.getMetaData() : null, null); |
.getMetaData() : null, null); |
290 |
/** |
/** |
291 |
* Returns the meta data needed for displaying a legend. |
* Returns the meta data needed for displaying a legend. |
292 |
*/ |
*/ |
293 |
public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() { |
public AttributeMetadataMap getAttributeMetaDataMap() { |
294 |
return attrMetaData; |
return attrMetaData; |
295 |
} |
} |
296 |
|
|
302 |
* @param attrMetaData |
* @param attrMetaData |
303 |
* map of attribute meta data |
* map of attribute meta data |
304 |
*/ |
*/ |
305 |
public void setAttributeMetaData( |
public void setAttributeMetaData(AttributeMetadataMap attrMetaData) { |
|
Map<Integer, AttributeMetaData> attrMetaData) { |
|
306 |
this.attrMetaData = (attrMetaData != null) ? attrMetaData |
this.attrMetaData = (attrMetaData != null) ? attrMetaData |
307 |
: createDefaultAttributeMetaDataMap(geoObject); |
: createDefaultAttributeMetaDataMap(geoObject); |
308 |
} |
} |
314 |
* @param fc |
* @param fc |
315 |
* a {@link FeatureCollection} |
* a {@link FeatureCollection} |
316 |
*/ |
*/ |
317 |
public static Map<Integer, AttributeMetaData> createDefaultAttributeMetaDataMap( |
public static AttributeMetadataMap createDefaultAttributeMetaDataMap( |
318 |
FeatureCollection fc) { |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc) { |
319 |
HashMap<Integer, AttributeMetaData> metaDataMap = new HashMap<Integer, AttributeMetaData>(); |
AttributeMetadataMap metaDataMap = new AttributeMetadataImplMap(); |
320 |
FeatureType ftype = fc.getSchema(); |
SimpleFeatureType ftype = fc.getSchema(); |
321 |
for (int i = 0; i < ftype.getAttributeCount(); i++) { |
for (int i = 0; i < ftype.getAttributeCount(); i++) { |
322 |
AttributeType aType = ftype.getAttributeType(i); |
AttributeDescriptor aDesc = ftype.getAttributeDescriptors().get(i); |
323 |
if (aType != ftype.getDefaultGeometry()) |
if ( !FeatureUtil.isGeometryAttribute(aDesc) ) |
324 |
metaDataMap.put(i, new AttributeMetaData(i, // Column no. |
metaDataMap.put(aDesc.getName(), new AttributeMetadataImpl( new NameImpl( aDesc.getName().getNamespaceURI(), aDesc.getName().getLocalPart()), |
325 |
true, // visible |
true, // visible |
326 |
new Translation(aType.getName()), // Column name |
new Translation(aDesc.getLocalName()), // Column name |
327 |
new Translation(), // description |
new Translation(aDesc.getLocalName()), // description |
328 |
"" // Unit |
"" // Unit |
329 |
)); |
)); |
330 |
} |
} |
372 |
} |
} |
373 |
|
|
374 |
/** |
/** |
375 |
* Same as {@link #getGeoObject()} method, but complies to the {@link StyledFeaturesInterface} |
* Same as {@link #getGeoObject()} method, but complies to the |
376 |
|
* {@link StyledFeaturesInterface}. The associated {@link Filter} is NOT |
377 |
|
* automatically applied. |
378 |
|
* |
379 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
380 |
|
* @see #getFeatureCollectionFiltered() |
381 |
*/ |
*/ |
382 |
@Override |
@Override |
383 |
public FeatureCollection getFeatureCollection() { |
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() { |
384 |
return getGeoObject(); |
return getGeoObject(); |
385 |
} |
} |
386 |
|
|
387 |
/** |
/** |
388 |
|
* Same as {@link #getGeoObject()} method, but complies to the |
389 |
|
* {@link StyledFeaturesInterface}. The associated {@link Filter} is |
390 |
|
* automatically applied by creating a {@link SubFeatureCollection}. |
391 |
|
* |
392 |
|
* @see {@link StyledFeaturesInterface} |
393 |
|
* @see #getFeatureCollectionFiltered() |
394 |
|
*/ |
395 |
|
@Override |
396 |
|
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
397 |
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection(); |
398 |
|
if (filter == Filter.EXCLUDE) |
399 |
|
return new EmptyFeatureCollection(fc.getSchema()); |
400 |
|
if (filter == Filter.INCLUDE) |
401 |
|
return fc; |
402 |
|
return fc.subCollection(filter); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
* Returns a virtual {@link FeatureSource} to access the |
* Returns a virtual {@link FeatureSource} to access the |
407 |
* {@link FeatureCollection}. Once created, it will be reused until |
* {@link FeatureCollection}. Once created, it will be reused until |
408 |
* {@link #uncache()} is called.<br/> |
* {@link #uncache()} is called.<br/> |
409 |
|
* |
410 |
* @see {@link StyledFeaturesInterface} |
* @see {@link StyledFeaturesInterface} |
411 |
*/ |
*/ |
412 |
@Override |
@Override |
413 |
public FeatureSource getFeatureSource() { |
public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() { |
414 |
if (featureSource == null) { |
if (featureSource == null) { |
415 |
CollectionDataStore store = new CollectionDataStore(getGeoObject()); |
CollectionDataStore store = new CollectionDataStore(getGeoObject()); |
416 |
try { |
try { |
424 |
return featureSource; |
return featureSource; |
425 |
} |
} |
426 |
|
|
427 |
|
@Override |
428 |
|
public Filter getFilter() { |
429 |
|
return filter; |
430 |
|
} |
431 |
|
|
432 |
|
@Override |
433 |
|
public void setFilter(Filter filter) { |
434 |
|
this.filter = filter; |
435 |
|
} |
436 |
|
|
437 |
|
@Override |
438 |
|
public SimpleFeatureType getSchema() { |
439 |
|
return getGeoObject().getSchema(); |
440 |
|
} |
441 |
|
|
442 |
|
|
443 |
|
@Override |
444 |
|
public ReferencedEnvelope getReferencedEnvelope() { |
445 |
|
return new ReferencedEnvelope(getEnvelope(), getCrs()); |
446 |
|
} |
447 |
|
|
448 |
} |
} |