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