/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollection.java
ViewVC logotype

Diff of /branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollection.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.243  
changed lines
  Added in v.244

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26