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