1 |
package skrueger.geotools; |
2 |
|
3 |
import java.net.URL; |
4 |
import java.util.Map; |
5 |
import java.util.HashMap; |
6 |
import javax.swing.ImageIcon; |
7 |
|
8 |
import org.geotools.styling.Style; |
9 |
import org.geotools.feature.FeatureCollection; |
10 |
import org.geotools.feature.FeatureType; |
11 |
import org.geotools.feature.AttributeType; |
12 |
|
13 |
import schmitzm.geotools.feature.FeatureUtil; |
14 |
|
15 |
import skrueger.i8n.Translation; |
16 |
import skrueger.AttributeMetaData; |
17 |
|
18 |
|
19 |
/** |
20 |
* This class provides a simple implementation of {@link StyledMapInterface} |
21 |
* for {@link FeatureCollection}. The uncache functionality is not supported, |
22 |
* because this class bases on an existing {@link FeatureCollection} object in |
23 |
* memory. |
24 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
25 |
* @version 1.0 |
26 |
*/ |
27 |
public class StyledFeatureCollection extends AbstractStyledMap<FeatureCollection> implements StyledFeatureCollectionInterface { |
28 |
|
29 |
/** Holds the meta data for displaying a legend. */ |
30 |
protected Map<Integer,AttributeMetaData> attrMetaData = null; |
31 |
|
32 |
/** |
33 |
* Creates a styled {@link FeatureCollection} with language-specific informations. |
34 |
* @param fc the {@link FeatureCollection} |
35 |
* @param id a unique ID for the object |
36 |
* @param title a (language-specific) short description |
37 |
* @param desc a (language-specific) long description |
38 |
* @param keywords (language-specific) keywords for the geo objects |
39 |
* @param style a display style (if {@code null}, a default style is created) |
40 |
* @param attrMetaData meta data for displaying a legend |
41 |
* @param icon an icon for the object (can be {@code null}) |
42 |
* @exception IllegalArgumentException if {@code null} is given as ID or geo object |
43 |
*/ |
44 |
public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Translation desc, Translation keywords, Style style, Map<Integer,AttributeMetaData> attrMetaData, ImageIcon icon) { |
45 |
super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry().getCoordinateSystem(), id, title, desc, keywords, style, icon); |
46 |
setAttributeMetaData( attrMetaData ); |
47 |
} |
48 |
|
49 |
/** |
50 |
* Creates a styled {@link FeatureCollection} with language-specific informations. |
51 |
* @param fc the {@link FeatureCollection} |
52 |
* @param id a unique ID for the object |
53 |
* @param title a (language-specific) short description |
54 |
* @param desc a (language-specific) long description |
55 |
* @param keywords (language-specific) keywords for the geo objects |
56 |
* @param style a display style with attribute meta data information |
57 |
* @param icon an icon for the object (can be {@code null}) |
58 |
* @exception IllegalArgumentException if {@code null} is given as ID or geo object |
59 |
*/ |
60 |
public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Translation desc, Translation keywords, StyledMapStyle<Map<Integer,AttributeMetaData>> style, ImageIcon icon) { |
61 |
super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry().getCoordinateSystem(), id, title, desc, keywords, style != null ? style.getGeoObjectStyle() : null, icon); |
62 |
setAttributeMetaData( style != null ? style.getMetaData() : null ); |
63 |
} |
64 |
|
65 |
/** |
66 |
* Creates a styled {@link FeatureCollection} with a language-specific title, |
67 |
* no long description, no keywords, default attribute meta data and no icon. |
68 |
* @param fc the {@link FeatureCollection} |
69 |
* @param id a unique ID for the object |
70 |
* @param title a short description |
71 |
* @param style a display style (if {@code null}, a default style is created) |
72 |
* @exception IllegalArgumentException if {@code null} is given as ID or geo object |
73 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
74 |
*/ |
75 |
public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Style style) { |
76 |
this(fc, id, title, null, null, style, null, null); |
77 |
} |
78 |
|
79 |
/** |
80 |
* Creates a styled {@link FeatureCollection} with non-translated informations. |
81 |
* @param fc the {@link FeatureCollection} |
82 |
* @param id a unique ID for the object |
83 |
* @param title a short description |
84 |
* @param desc a long description |
85 |
* @param keywords keywords for the geo objects |
86 |
* @param style a display style (if {@code null}, a default style is created) |
87 |
* @param attrMetaData meta data for displaying a legend |
88 |
* @param icon an icon for the object (can be {@code null}) |
89 |
* @exception IllegalArgumentException if {@code null} is given as ID or geo object |
90 |
*/ |
91 |
public StyledFeatureCollection(FeatureCollection fc, String id, String title, String desc, String keywords, Style style, Map<Integer,AttributeMetaData> attrMetaData, ImageIcon icon) { |
92 |
this(fc, id, (Translation)null, null, null, style, attrMetaData, icon); |
93 |
setTitle(title); |
94 |
setDesc(desc); |
95 |
setKeywords(keywords); |
96 |
} |
97 |
|
98 |
/** |
99 |
* Creates a styled {@link FeatureCollection} with non-translated informations. |
100 |
* @param fc the {@link FeatureCollection} |
101 |
* @param id a unique ID for the object |
102 |
* @param title a short description |
103 |
* @param desc a long description |
104 |
* @param keywords keywords for the geo objects |
105 |
* @param style a display style with attribute meta data information |
106 |
* @param icon an icon for the object (can be {@code null}) |
107 |
* @exception IllegalArgumentException if {@code null} is given as ID or geo object |
108 |
*/ |
109 |
public StyledFeatureCollection(FeatureCollection fc, String id, String title, String desc, String keywords, StyledMapStyle<Map<Integer,AttributeMetaData>> style, ImageIcon icon) { |
110 |
this(fc, |
111 |
id, |
112 |
title, |
113 |
desc, |
114 |
keywords, |
115 |
style != null ? style.getGeoObjectStyle() : null, |
116 |
style != null ? style.getMetaData() : null, |
117 |
icon |
118 |
); |
119 |
} |
120 |
|
121 |
/** |
122 |
* Creates a styled {@link FeatureCollection} with a non-translated title, |
123 |
* no long description, no keywords, default attribute meta data and no icon. |
124 |
* @param fc the {@link FeatureCollection} |
125 |
* @param id a unique ID for the object |
126 |
* @param title a short description |
127 |
* @param style a display style (if {@code null}, a default style is created) |
128 |
* @exception IllegalArgumentException if {@code null} is given as ID or geo object |
129 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
130 |
*/ |
131 |
public StyledFeatureCollection(FeatureCollection fc, String id, String title, Style style) { |
132 |
this(fc, id, title, null, null, style, null, null); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Creates a styled {@link FeatureCollection} with a non-translated title, |
137 |
* no long description, no keywords, default attribute meta data and no icon. |
138 |
* @param fc the {@link FeatureCollection} |
139 |
* @param id a unique ID for the object |
140 |
* @param title a short description |
141 |
* @param style a display style (if {@code null}, a default style is created) |
142 |
* @exception IllegalArgumentException if {@code null} is given as ID or geo object |
143 |
* @see #createDefaultAttributeMetaDataMap(FeatureCollection) |
144 |
*/ |
145 |
public StyledFeatureCollection(FeatureCollection fc, String id, String title, StyledMapStyle<Map<Integer,AttributeMetaData>> style) { |
146 |
this( |
147 |
fc, |
148 |
id, |
149 |
title, |
150 |
null, |
151 |
null, |
152 |
style != null ? style.getGeoObjectStyle() : null, |
153 |
style != null ? style.getMetaData() : null, |
154 |
null |
155 |
); |
156 |
} |
157 |
|
158 |
/** |
159 |
* Creates a default style for the {@link FeatureCollection}. |
160 |
* @see FeatureUtil#createDefaultStyle(FeatureCollection) |
161 |
*/ |
162 |
protected Style createDefaultStyle() { |
163 |
return FeatureUtil.createDefaultStyle( geoObject ); |
164 |
} |
165 |
|
166 |
/** |
167 |
* Returns the meta data needed for displaying a legend. |
168 |
*/ |
169 |
public Map<Integer,AttributeMetaData> getAttributeMetaDataMap() { |
170 |
return attrMetaData; |
171 |
} |
172 |
|
173 |
/** |
174 |
* Sets the meta data needed for displaying a legend. |
175 |
* If {@code legendData} is {@code null} an empty map is set, so |
176 |
* {@link #getAttributeMetaDataMap()} never returns {@code null}. |
177 |
* @param attrMetaData map of attribute meta data |
178 |
*/ |
179 |
public void setAttributeMetaData(Map<Integer,AttributeMetaData> attrMetaData) { |
180 |
this.attrMetaData = (attrMetaData != null) ? attrMetaData : createDefaultAttributeMetaDataMap(geoObject); |
181 |
} |
182 |
|
183 |
/** |
184 |
* Creates non-translated default meta data for a {@link FeatureCollection} |
185 |
* with all attributes visible and no unit set. |
186 |
* @param fc a {@link FeatureCollection} |
187 |
*/ |
188 |
public static Map<Integer,AttributeMetaData> createDefaultAttributeMetaDataMap(FeatureCollection fc) { |
189 |
HashMap<Integer,AttributeMetaData> metaDataMap = new HashMap<Integer,AttributeMetaData>(); |
190 |
FeatureType ftype = fc.getSchema(); |
191 |
for (int i=0; i<ftype.getAttributeCount(); i++) { |
192 |
AttributeType aType = ftype.getAttributeType(i); |
193 |
if ( aType != ftype.getDefaultGeometry() ) |
194 |
metaDataMap.put( |
195 |
i, |
196 |
new AttributeMetaData( |
197 |
i, // Column no. |
198 |
true, // visible |
199 |
new Translation( aType.getName() ), // Column name |
200 |
new Translation(), // description |
201 |
"" // Unit |
202 |
) |
203 |
); |
204 |
} |
205 |
return metaDataMap; |
206 |
} |
207 |
|
208 |
/** |
209 |
* Simply sets the {@link #geoObject}, {@link #crs}, {@link #envelope} and |
210 |
* {@link #attrMetaData} to {@code null}. |
211 |
*/ |
212 |
public void dispose() { |
213 |
this.geoObject = null; |
214 |
this.envelope = null; |
215 |
this.crs = null; |
216 |
this.attrMetaData = null; |
217 |
} |
218 |
|
219 |
/** |
220 |
* Tests whether the geo object is disposed. |
221 |
*/ |
222 |
public boolean isDisposed() { |
223 |
return geoObject == null; |
224 |
} |
225 |
|
226 |
/** |
227 |
* Does nothing, because the {@link AbstractStyledMap} bases on existing |
228 |
* objects (in memory) which can not be uncached and reloaded. |
229 |
*/ |
230 |
public void uncache() { |
231 |
LOGGER.warn("Uncache functionality is not supported. Object remains in memory."); |
232 |
} |
233 |
|
234 |
|
235 |
/* |
236 |
* (non-Javadoc) |
237 |
* @see skrueger.geotools.StyledMapInterface#getInfoURL() |
238 |
*/ |
239 |
public URL getInfoURL() { |
240 |
return null; |
241 |
} |
242 |
|
243 |
public boolean isHideInLegend() { |
244 |
return false; |
245 |
} |
246 |
} |