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