/[schmitzm]/trunk/src/skrueger/geotools/StyledFeatureCollection.java
ViewVC logotype

Contents of /trunk/src/skrueger/geotools/StyledFeatureCollection.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1047 - (show annotations)
Wed Sep 22 12:11:15 2010 UTC (14 years, 5 months ago) by mojays
File size: 14827 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.i8n.Translation;
52
53 /**
54 * This class provides a simple implementation of {@link StyledLayerInterface}
55 * for {@link FeatureCollection}. The uncache functionality is not supported,
56 * because this class bases on an existing {@link FeatureCollection} object in
57 * memory.
58 *
59 * @author <a href="mailto:[email protected]">Martin Schmitz</a>
60 * (University of Bonn/Germany)
61 * @version 1.0
62 */
63 public class StyledFeatureCollection
64 extends
65 AbstractStyledLayer<FeatureCollection<SimpleFeatureType, SimpleFeature>>
66 implements StyledFeatureCollectionInterface {
67
68 /** Holds the meta data for displaying a legend. */
69 protected AttributeMetadataMap<AttributeMetadataImpl> attrMetaData = null;
70
71 /**
72 * We be filled with a "virtual" {@link FeatureSource} on demand.
73 */
74 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
84 * informations.
85 *
86 * @param fc
87 * the {@link FeatureCollection}
88 * @param id
89 * a unique ID for the object
90 * @param title
91 * a (language-specific) short description
92 * @param desc
93 * a (language-specific) long description
94 * @param keywords
95 * (language-specific) keywords for the geo objects
96 * @param style
97 * a display style (if {@code null}, a default style is created)
98 * @param attrMetaData
99 * meta data for displaying a legend
100 * @param icon
101 * an icon for the object (can be {@code null})
102 * @exception IllegalArgumentException
103 * if {@code null} is given as ID or geo object
104 */
105 public StyledFeatureCollection(
106 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
107 Translation title, Translation desc, Translation keywords,
108 Style style, AttributeMetadataMap attrMetaData, ImageIcon icon) {
109 super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor()
110 .getCoordinateReferenceSystem(), id, title, desc, keywords,
111 style, icon);
112 setAttributeMetaData(attrMetaData);
113 }
114
115 /**
116 * Creates a styled {@link FeatureCollection} with language-specific
117 * informations.
118 *
119 * @param fc
120 * the {@link FeatureCollection}
121 * @param id
122 * a unique ID for the object
123 * @param title
124 * a (language-specific) short description
125 * @param desc
126 * a (language-specific) long description
127 * @param keywords
128 * (language-specific) keywords for the geo objects
129 * @param style
130 * a display style with attribute meta data information
131 * @param icon
132 * an icon for the object (can be {@code null})
133 * @exception IllegalArgumentException
134 * if {@code null} is given as ID or geo object
135 */
136 public StyledFeatureCollection(
137 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
138 Translation title, Translation desc, Translation keywords,
139 StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) {
140 super(fc, fc.getBounds(), fc.getSchema().getGeometryDescriptor()
141 .getCoordinateReferenceSystem(), id, title, desc, keywords,
142 style != null ? style.getGeoObjectStyle() : null, icon);
143 setAttributeMetaData(style != null ? style.getMetaData() : null);
144 }
145
146 /**
147 * Creates a styled {@link FeatureCollection} with a language-specific
148 * title, no long description, no keywords, default attribute meta data and
149 * no icon.
150 *
151 * @param fc
152 * the {@link FeatureCollection}
153 * @param id
154 * a unique ID for the object
155 * @param title
156 * a short description
157 * @param style
158 * a display style (if {@code null}, a default style is created)
159 * @exception IllegalArgumentException
160 * if {@code null} is given as ID or geo object
161 * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
162 */
163 public StyledFeatureCollection(
164 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
165 Translation title, Style style) {
166 this(fc, id, title, null, null, style, null, null);
167 }
168
169 /**
170 * Creates a styled {@link FeatureCollection} with non-translated
171 * informations.
172 *
173 * @param fc
174 * the {@link FeatureCollection}
175 * @param id
176 * a unique ID for the object
177 * @param title
178 * a short description
179 * @param desc
180 * a long description
181 * @param keywords
182 * keywords for the geo objects
183 * @param style
184 * a display style (if {@code null}, a default style is created)
185 * @param attrMetaData
186 * meta data for displaying a legend
187 * @param icon
188 * an icon for the object (can be {@code null})
189 * @exception IllegalArgumentException
190 * if {@code null} is given as ID or geo object
191 */
192 public StyledFeatureCollection(
193 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
194 String title, String desc, String keywords, Style style,
195 AttributeMetadataMap attrMetaData, ImageIcon icon) {
196 this(fc, id, (Translation) null, null, null, style, attrMetaData, icon);
197 setTitle(title);
198 setDesc(desc);
199 setKeywords(keywords);
200 }
201
202 /**
203 * Creates a styled {@link FeatureCollection} with non-translated
204 * informations.
205 *
206 * @param fc
207 * the {@link FeatureCollection}
208 * @param id
209 * a unique ID for the object
210 * @param title
211 * a short description
212 * @param desc
213 * a long description
214 * @param keywords
215 * keywords for the geo objects
216 * @param style
217 * a display style with attribute meta data information
218 * @param icon
219 * an icon for the object (can be {@code null})
220 * @exception IllegalArgumentException
221 * if {@code null} is given as ID or geo object
222 */
223 public StyledFeatureCollection(
224 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
225 String title, String desc, String keywords,
226 StyledLayerStyle<AttributeMetadataMap> style, ImageIcon icon) {
227 this(fc, id, title, desc, keywords, style != null ? style
228 .getGeoObjectStyle() : null, style != null ? style
229 .getMetaData() : null, icon);
230 }
231
232 /**
233 * Creates a styled {@link FeatureCollection} with a non-translated title,
234 * no long description, no keywords, default attribute meta data and no
235 * icon.
236 *
237 * @param fc
238 * the {@link FeatureCollection}
239 * @param id
240 * a unique ID for the object
241 * @param title
242 * a short description
243 * @param style
244 * a display style (if {@code null}, a default style is created)
245 * @exception IllegalArgumentException
246 * if {@code null} is given as ID or geo object
247 * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
248 */
249 public StyledFeatureCollection(
250 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
251 String title, Style style) {
252 this(fc, id, title, null, null, style, null, null);
253 }
254
255 /**
256 * Creates a styled {@link FeatureCollection} with a non-translated title,
257 * no long description, no keywords, default attribute meta data and no
258 * icon.
259 *
260 * @param fc
261 * the {@link FeatureCollection}
262 * @param id
263 * a unique ID for the object
264 * @param title
265 * a short description
266 * @param style
267 * a display style (if {@code null}, a default style is created)
268 * @exception IllegalArgumentException
269 * if {@code null} is given as ID or geo object
270 * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
271 */
272 public StyledFeatureCollection(
273 FeatureCollection<SimpleFeatureType, SimpleFeature> fc, String id,
274 String title, StyledLayerStyle<AttributeMetadataMap> style) {
275 this(fc, id, title, null, null, style != null ? style
276 .getGeoObjectStyle() : null, style != null ? style
277 .getMetaData() : null, null);
278 }
279
280 /**
281 * Creates a default style for the {@link FeatureCollection}.
282 *
283 * @see FeatureUtil#createDefaultStyle(FeatureCollection)
284 */
285 protected Style createDefaultStyle() {
286 return FeatureUtil.createDefaultStyle(geoObject);
287 }
288
289 /**
290 * Returns the meta data needed for displaying a legend.
291 */
292 public AttributeMetadataMap getAttributeMetaDataMap() {
293 return attrMetaData;
294 }
295
296 /**
297 * Sets the meta data needed for displaying a legend. If {@code legendData}
298 * is {@code null} an empty map is set, so
299 * {@link #getAttributeMetaDataMap()} never returns {@code null}.
300 *
301 * @param attrMetaData
302 * map of attribute meta data
303 */
304 public void setAttributeMetaData(AttributeMetadataMap attrMetaData) {
305 this.attrMetaData = (attrMetaData != null) ? attrMetaData
306 : createDefaultAttributeMetaDataMap(geoObject);
307 }
308
309 /**
310 * Creates non-translated default meta data for a {@link FeatureCollection}
311 * with all attributes visible and no unit set.
312 *
313 * @param fc
314 * a {@link FeatureCollection}
315 */
316 public static AttributeMetadataMap createDefaultAttributeMetaDataMap(
317 FeatureCollection<SimpleFeatureType, SimpleFeature> fc) {
318 AttributeMetadataMap metaDataMap = new AttributeMetadataImplMap();
319 SimpleFeatureType ftype = fc.getSchema();
320 for (int i = 0; i < ftype.getAttributeCount(); i++) {
321 AttributeDescriptor aDesc = ftype.getAttributeDescriptors().get(i);
322 if ( !FeatureUtil.isGeometryAttribute(aDesc) )
323 metaDataMap.put(aDesc.getName(), new AttributeMetadataImpl( new NameImpl( aDesc.getName().getNamespaceURI(), aDesc.getName().getLocalPart()),
324 true, // visible
325 new Translation(aDesc.getLocalName()), // Column name
326 new Translation(aDesc.getLocalName()), // description
327 "" // Unit
328 ));
329 }
330 return metaDataMap;
331 }
332
333 /**
334 * Simply sets the {@link #geoObject}, {@link #crs}, {@link #envelope} and
335 * {@link #attrMetaData} to {@code null}.
336 */
337 public void dispose() {
338 this.geoObject = null;
339 this.envelope = null;
340 this.crs = null;
341 this.attrMetaData = null;
342 }
343
344 /**
345 * Tests whether the geo object is disposed.
346 */
347 public boolean isDisposed() {
348 return geoObject == null;
349 }
350
351 /**
352 * Does nothing, because the {@link AbstractStyledLayer} bases on existing
353 * objects (in memory) which can not be uncached and reloaded.
354 */
355 public void uncache() {
356
357 /** It will be recreated on the next getFetureSource() **/
358 featureSource = null;
359
360 LOGGER
361 .warn("Uncache onyl uncached any virtual FeatureSource. Object remains in memory.");
362 }
363
364 /*
365 * (non-Javadoc)
366 *
367 * @see skrueger.geotools.StyledLayerInterface#getInfoURL()
368 */
369 public URL getInfoURL() {
370 return null;
371 }
372
373 /**
374 * 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}
379 * @see #getFeatureCollectionFiltered()
380 */
381 @Override
382 public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
383 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
406 * {@link FeatureCollection}. Once created, it will be reused until
407 * {@link #uncache()} is called.<br/>
408 *
409 * @see {@link StyledFeaturesInterface}
410 */
411 @Override
412 public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
413 if (featureSource == null) {
414 CollectionDataStore store = new CollectionDataStore(getGeoObject());
415 try {
416 featureSource = store.getFeatureSource(store.getTypeNames()[0]);
417 } catch (IOException e) {
418 throw new RuntimeException(
419 "Could not create a FeatureSource from the CollectionDataStore:",
420 e);
421 }
422 }
423 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 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26