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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26