/[schmitzm]/branches/2.2.x/src/skrueger/geotools/StyledFeatureCollection.java
ViewVC logotype

Contents of /branches/2.2.x/src/skrueger/geotools/StyledFeatureCollection.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1046 - (show annotations)
Wed Sep 22 12:10:52 2010 UTC (14 years, 5 months ago) by mojays
File size: 15082 byte(s)
BugFix: deal with "auto-put" behavior of AttributeMetadataMapImpl.get(.) in AttributeMetaDataAttributeTypeFilter, so that "the_geom" is never (automatically) inserted in the AMD
StyledFeatureCollection: check for geometry attribute by FeatureUtil.isGeometryAttribute(.)
1 /*******************************************************************************
2 * Copyright (c) 2009 Martin O. J. Schmitz.
3 *
4 * This file is part of the SCHMITZM library - a collection of utility
5 * classes based on Java 1.6, focusing (not only) on Java Swing
6 * and the Geotools library.
7 *
8 * The SCHMITZM project is hosted at:
9 * http://wald.intevation.org/projects/schmitzm/
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License (license.txt)
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * or try this link: http://www.gnu.org/licenses/lgpl.html
25 *
26 * Contributors:
27 * Martin O. J. Schmitz - initial API and implementation
28 * Stefan A. Tzeggai - additional utility classes
29 ******************************************************************************/
30 package skrueger.geotools;
31
32 import java.io.IOException;
33 import java.net.URL;
34
35 import javax.swing.ImageIcon;
36
37 import org.geotools.data.FeatureSource;
38 import org.geotools.data.collection.CollectionDataStore;
39 import org.geotools.data.store.EmptyFeatureCollection;
40 import org.geotools.feature.FeatureCollection;
41 import org.geotools.feature.NameImpl;
42 import org.geotools.feature.collection.SubFeatureCollection;
43 import org.geotools.styling.Style;
44 import org.opengis.feature.simple.SimpleFeature;
45 import org.opengis.feature.simple.SimpleFeatureType;
46 import org.opengis.feature.type.AttributeDescriptor;
47 import org.opengis.filter.Filter;
48
49 import schmitzm.geotools.feature.FeatureUtil;
50 import skrueger.AttributeMetadataImpl;
51 import skrueger.AttributeMetadataInterface;
52 import skrueger.i8n.Translation;
53
54 /**
55 * This class provides a simple implementation of {@link StyledLayerInterface}
56 * for {@link FeatureCollection}. The uncache functionality is not supported,
57 * because this class bases on an existing {@link FeatureCollection} object in
58 * memory.
59 *
60 * @author <a href="mailto:[email protected]">Martin Schmitz</a>
61 * (University of Bonn/Germany)
62 * @version 1.0
63 */
64 public class StyledFeatureCollection
65 extends
66 AbstractStyledLayer<FeatureCollection<SimpleFeatureType, SimpleFeature>>
67 implements StyledFeatureCollectionInterface {
68
69 /** Holds the meta data for displaying a legend. */
70 protected AttributeMetadataMap<AttributeMetadataImpl> attrMetaData = null;
71
72 /**
73 * We be filled with a "virtual" {@link FeatureSource} on demand.
74 */
75 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
85 * informations.
86 *
87 * @param fc
88 * the {@link FeatureCollection}
89 * @param id
90 * a unique ID for the object
91 * @param title
92 * a (language-specific) short description
93 * @param desc
94 * a (language-specific) long description
95 * @param keywords
96 * (language-specific) keywords for the geo objects
97 * @param style
98 * a display style (if {@code null}, a default style is created)
99 * @param attrMetaData
100 * meta data for displaying a legend
101 * @param icon
102 * an icon for the object (can be {@code null})
103 * @exception IllegalArgumentException
104 * if {@code null} is given as ID or geo object
105 */
106 public StyledFeatureCollection(
107 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
108 Translation title, Translation desc, Translation keywords,
109 Style style, AttributeMetadataMap<? extends AttributeMetadataInterface> attrMetaData, ImageIcon icon) {
110 super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor()
111 .getCoordinateReferenceSystem(), id, title, desc, keywords,
112 style, icon);
113 setAttributeMetaData(attrMetaData);
114 }
115
116 /**
117 * Creates a styled {@link FeatureCollection} with language-specific
118 * informations.
119 *
120 * @param fc
121 * the {@link FeatureCollection}
122 * @param id
123 * a unique ID for the object
124 * @param title
125 * a (language-specific) short description
126 * @param desc
127 * a (language-specific) long description
128 * @param keywords
129 * (language-specific) keywords for the geo objects
130 * @param style
131 * a display style with attribute meta data information
132 * @param icon
133 * an icon for the object (can be {@code null})
134 * @exception IllegalArgumentException
135 * if {@code null} is given as ID or geo object
136 */
137 public StyledFeatureCollection(
138 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
139 Translation title, Translation desc, Translation keywords,
140 StyledLayerStyle<AttributeMetadataMap<AttributeMetadataInterface>> style, ImageIcon icon) {
141 super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor()
142 .getCoordinateReferenceSystem(), id, title, desc, keywords,
143 style != null ? style.getGeoObjectStyle() : null, icon);
144 setAttributeMetaData(style != null ? style.getMetaData() : null);
145 }
146
147 /**
148 * Creates a styled {@link FeatureCollection} with a language-specific
149 * title, no long description, no keywords, default attribute meta data and
150 * no icon.
151 *
152 * @param fc
153 * the {@link FeatureCollection}
154 * @param id
155 * a unique ID for the object
156 * @param title
157 * a short description
158 * @param style
159 * a display style (if {@code null}, a default style is created)
160 * @exception IllegalArgumentException
161 * if {@code null} is given as ID or geo object
162 * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
163 */
164 public StyledFeatureCollection(
165 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
166 Translation title, Style style) {
167 this(fc, id, title, null, null, style, null, null);
168 }
169
170 /**
171 * Creates a styled {@link FeatureCollection} with non-translated
172 * informations.
173 *
174 * @param fc
175 * the {@link FeatureCollection}
176 * @param id
177 * a unique ID for the object
178 * @param title
179 * a short description
180 * @param desc
181 * a long description
182 * @param keywords
183 * keywords for the geo objects
184 * @param style
185 * a display style (if {@code null}, a default style is created)
186 * @param attrMetaData
187 * meta data for displaying a legend
188 * @param icon
189 * an icon for the object (can be {@code null})
190 * @exception IllegalArgumentException
191 * if {@code null} is given as ID or geo object
192 */
193 public StyledFeatureCollection(
194 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
195 String title, String desc, String keywords, Style style,
196 AttributeMetadataMap<AttributeMetadataInterface> attrMetaData, ImageIcon icon) {
197 this(fc, id, (Translation) null, null, null, style, attrMetaData, icon);
198 setTitle(title);
199 setDesc(desc);
200 setKeywords(keywords);
201 }
202
203 /**
204 * Creates a styled {@link FeatureCollection} with non-translated
205 * informations.
206 *
207 * @param fc
208 * the {@link FeatureCollection}
209 * @param id
210 * a unique ID for the object
211 * @param title
212 * a short description
213 * @param desc
214 * a long description
215 * @param keywords
216 * keywords for the geo objects
217 * @param style
218 * a display style with attribute meta data information
219 * @param icon
220 * an icon for the object (can be {@code null})
221 * @exception IllegalArgumentException
222 * if {@code null} is given as ID or geo object
223 */
224 public StyledFeatureCollection(
225 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
226 String title, String desc, String keywords,
227 StyledLayerStyle<AttributeMetadataMap<AttributeMetadataInterface>> style, ImageIcon icon) {
228 this(fc, id, title, desc, keywords, style != null ? style
229 .getGeoObjectStyle() : null, style != null ? style
230 .getMetaData() : null, icon);
231 }
232
233 /**
234 * Creates a styled {@link FeatureCollection} with a non-translated title,
235 * no long description, no keywords, default attribute meta data and no
236 * icon.
237 *
238 * @param fc
239 * the {@link FeatureCollection}
240 * @param id
241 * a unique ID for the object
242 * @param title
243 * a short description
244 * @param style
245 * a display style (if {@code null}, a default style is created)
246 * @exception IllegalArgumentException
247 * if {@code null} is given as ID or geo object
248 * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
249 */
250 public StyledFeatureCollection(
251 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
252 String title, Style style) {
253 this(fc, id, title, null, null, style, null, null);
254 }
255
256 /**
257 * Creates a styled {@link FeatureCollection} with a non-translated title,
258 * no long description, no keywords, default attribute meta data and no
259 * icon.
260 *
261 * @param fc
262 * the {@link FeatureCollection}
263 * @param id
264 * a unique ID for the object
265 * @param title
266 * a short description
267 * @param style
268 * a display style (if {@code null}, a default style is created)
269 * @exception IllegalArgumentException
270 * if {@code null} is given as ID or geo object
271 * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
272 */
273 public StyledFeatureCollection(
274 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
275 String title, StyledLayerStyle<AttributeMetadataMap<AttributeMetadataInterface>> style) {
276 this(fc, id, title, null, null, style != null ? style
277 .getGeoObjectStyle() : null, style != null ? style
278 .getMetaData() : null, null);
279 }
280
281 /**
282 * Creates a default style for the {@link FeatureCollection}.
283 *
284 * @see FeatureUtil#createDefaultStyle(FeatureCollection)
285 */
286 protected Style createDefaultStyle() {
287 return FeatureUtil.createDefaultStyle(geoObject);
288 }
289
290 /**
291 * Returns the meta data needed for displaying a legend.
292 */
293 public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
294 return attrMetaData;
295 }
296
297 /**
298 * Sets the meta data needed for displaying a legend. If {@code legendData}
299 * is {@code null} an empty map is set, so
300 * {@link #getAttributeMetaDataMap()} never returns {@code null}.
301 *
302 * @param attrMetaData
303 * map of attribute meta data
304 */
305 public void setAttributeMetaData(AttributeMetadataMap<? extends AttributeMetadataInterface> attrMetaData) {
306 this.attrMetaData = (attrMetaData != null) ? attrMetaData
307 : createDefaultAttributeMetaDataMap(geoObject);
308 }
309
310 /**
311 * Creates non-translated default meta data for a {@link FeatureCollection}
312 * with all attributes visible and no unit set.
313 *
314 * @param fc
315 * a {@link FeatureCollection}
316 */
317 public static AttributeMetadataMap createDefaultAttributeMetaDataMap(
318 FeatureCollection<SimpleFeatureType, SimpleFeature> fc) {
319 AttributeMetadataMap metaDataMap = new AttributeMetadataImplMap();
320 SimpleFeatureType ftype = fc.getSchema();
321 for (int i = 0; i < ftype.getAttributeCount(); i++) {
322 AttributeDescriptor aDesc = ftype.getAttributeDescriptors().get(i);
323 if ( !FeatureUtil.isGeometryAttribute(aDesc) )
324 metaDataMap.put(aDesc.getName(), new AttributeMetadataImpl( new NameImpl( aDesc.getName().getNamespaceURI(), aDesc.getName().getLocalPart()),
325 true, // visible
326 new Translation(aDesc.getLocalName()), // Column name
327 new Translation(aDesc.getLocalName()), // description
328 "" // Unit
329 ));
330 }
331 return metaDataMap;
332 }
333
334 /**
335 * Simply sets the {@link #geoObject}, {@link #crs}, {@link #envelope} and
336 * {@link #attrMetaData} to {@code null}.
337 */
338 public void dispose() {
339 this.geoObject = null;
340 this.envelope = null;
341 this.crs = null;
342 this.attrMetaData = null;
343 }
344
345 /**
346 * Tests whether the geo object is disposed.
347 */
348 public boolean isDisposed() {
349 return geoObject == null;
350 }
351
352 /**
353 * Does nothing, because the {@link AbstractStyledLayer} bases on existing
354 * objects (in memory) which can not be uncached and reloaded.
355 */
356 public void uncache() {
357
358 /** It will be recreated on the next getFetureSource() **/
359 featureSource = null;
360
361 LOGGER
362 .warn("Uncache onyl uncached any virtual FeatureSource. Object remains in memory.");
363 }
364
365 /*
366 * (non-Javadoc)
367 *
368 * @see skrueger.geotools.StyledLayerInterface#getInfoURL()
369 */
370 public URL getInfoURL() {
371 return null;
372 }
373
374 /**
375 * 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}
380 * @see #getFeatureCollectionFiltered()
381 */
382 @Override
383 public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
384 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
407 * {@link FeatureCollection}. Once created, it will be reused until
408 * {@link #uncache()} is called.<br/>
409 *
410 * @see {@link StyledFeaturesInterface}
411 */
412 @Override
413 public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
414 if (featureSource == null) {
415 CollectionDataStore store = new CollectionDataStore(getGeoObject());
416 try {
417 featureSource = store.getFeatureSource(store.getTypeNames()[0]);
418 } catch (IOException e) {
419 throw new RuntimeException(
420 "Could not create a FeatureSource from the CollectionDataStore:",
421 e);
422 }
423 }
424 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 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26