1 |
package skrueger.geotools; |
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
import java.io.File; |
* |
4 |
import java.io.FileNotFoundException; |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
import java.io.IOException; |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
import java.net.URL; |
* and the Geotools library. |
7 |
import java.util.Date; |
* |
8 |
import java.util.HashMap; |
* The SCHMITZM project is hosted at: |
9 |
import java.util.Map; |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
import java.util.Random; |
* |
11 |
|
* This program is free software; you can redistribute it and/or |
12 |
import javax.swing.ImageIcon; |
* modify it under the terms of the GNU Lesser General Public License |
13 |
import javax.swing.JPanel; |
* as published by the Free Software Foundation; either version 3 |
14 |
|
* of the License, or (at your option) any later version. |
15 |
import org.apache.log4j.Logger; |
* |
16 |
import org.geotools.data.FeatureSource; |
* This program is distributed in the hope that it will be useful, |
17 |
import org.geotools.feature.AttributeType; |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
import org.geotools.styling.Style; |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
* GNU General Public License for more details. |
20 |
|
* |
21 |
import schmitzm.geotools.styling.StylingUtil; |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
import skrueger.AttributeMetaData; |
* along with this program; if not, write to the Free Software |
23 |
import skrueger.i8n.Translation; |
* 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 |
import com.vividsolutions.jts.geom.Envelope; |
* |
26 |
|
* Contributors: |
27 |
/** |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* This class enables a non Atlas context to use the Atlas LayerPanel |
* Stefan A. Tzeggai - additional utility classes |
29 |
* {@link JPanel} as a {@link MapContextManagerInterface} |
******************************************************************************/ |
30 |
* |
package skrueger.geotools; |
31 |
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
|
32 |
*/ |
import java.io.File; |
33 |
public class StyledFS implements StyledFeatureSourceInterface { |
import java.io.IOException; |
34 |
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
import java.net.URL; |
35 |
|
import java.util.Date; |
36 |
private final FeatureSource fs; |
import java.util.Random; |
37 |
|
|
38 |
/** |
import javax.swing.ImageIcon; |
39 |
* A unique ID which identifies the Layer in the Atlas. It's more important |
import javax.swing.JPanel; |
40 |
* than it should be ;-) |
|
41 |
*/ |
import org.apache.log4j.Logger; |
42 |
private String id; |
import org.geotools.data.FeatureSource; |
43 |
|
import org.geotools.feature.FeatureCollection; |
44 |
private Style style; |
import org.geotools.feature.NameImpl; |
45 |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
46 |
private Translation title; |
import org.geotools.styling.Style; |
47 |
|
import org.opengis.feature.simple.SimpleFeature; |
48 |
private Translation desc; |
import org.opengis.feature.simple.SimpleFeatureType; |
49 |
|
import org.opengis.feature.type.AttributeDescriptor; |
50 |
private File sldFile; |
import org.opengis.filter.Filter; |
51 |
|
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
52 |
private HashMap<Integer, AttributeMetaData> map; |
|
53 |
|
import schmitzm.geotools.io.GeoImportUtil; |
54 |
/** |
import schmitzm.geotools.styling.StylingUtil; |
55 |
* This class enables a non Atlas context to use the Atlas LayerPanel |
import skrueger.AttributeMetadataImpl; |
56 |
* {@link JPanel} as a {@link MapContextManagerInterface} |
import skrueger.AttributeMetadataInterface; |
57 |
* |
import skrueger.i8n.Translation; |
58 |
* @param fs |
|
59 |
* {@link FeatureSource} that is beeing styled. |
import com.vividsolutions.jts.geom.Envelope; |
60 |
* |
|
61 |
* @param sldFile |
/** |
62 |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
* This class enables a non Atlas context to use the Atlas LayerPanel |
63 |
* import and associate with this {@link StyledFS} |
* {@link JPanel} as a {@link MapContextManagerInterface} |
64 |
*/ |
* |
65 |
public StyledFS(FeatureSource fs, File sldFile) { |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
66 |
|
* |
67 |
this.fs = fs; |
* TODO Rename to StyledShapefile |
68 |
id = StyledFS.class.getSimpleName() |
*/ |
69 |
+ new Random(new Date().getTime()).nextInt(10000000); |
public class StyledFS implements StyledFeatureSourceInterface { |
70 |
|
private static final Logger LOGGER = Logger.getLogger(StyledFS.class); |
71 |
this.sldFile = sldFile; |
|
72 |
|
private final FeatureSource<SimpleFeatureType, SimpleFeature> fs; |
73 |
if ((sldFile != null) && (sldFile.exists())) { |
|
74 |
try { |
/** Caching the CRS of the layer **/ |
75 |
style = StylingUtil.loadSLD(sldFile)[0]; |
CoordinateReferenceSystem crs = null; |
76 |
} catch (FileNotFoundException e) { |
|
77 |
LOGGER |
/** |
78 |
.debug("The SLD file passed was empty. Leaving the Style untouched. (We are in the constructor.. so its null"); |
* A unique ID which identifies the Layer in the Atlas. It's more important |
79 |
} catch (Exception e) { |
* than it should be ;-) |
80 |
LOGGER.warn("Reading SLD failed: " + sldFile, e); |
*/ |
81 |
} |
final private String id; |
82 |
} |
|
83 |
title = new Translation(); |
private Style style; |
84 |
title.fromOneLine(sldFile.getName()); |
|
85 |
|
private Translation title; |
86 |
desc = new Translation(); |
|
87 |
desc.fromOneLine(sldFile.getAbsolutePath()); |
private Translation desc; |
88 |
} |
|
89 |
|
private File sldFile; |
90 |
public void dispose() { |
|
91 |
} |
/** A map of simple attribute names to their meta-data **/ |
92 |
|
private AttributeMetadataMap<AttributeMetadataImpl> attMap; |
93 |
/** |
|
94 |
* Returnes human readable {@link String} of the CRS natively used by this |
private Filter filter = Filter.INCLUDE; |
95 |
* {@link DpLayer} |
|
96 |
* |
/** |
97 |
* If crs == null, it will call {@link #getGeoObject()} |
* This class enables a non Atlas context to use the Atlas LayerPanel |
98 |
* |
* {@link JPanel} as a {@link MapContextManagerInterface} |
99 |
*/ |
* |
100 |
public String getCRSString() { |
* @param fs |
101 |
if (getCrs() == null) |
* {@link FeatureSource} that is beeing styled. |
102 |
return "CRS?"; |
* |
103 |
|
* @param sldFile |
104 |
return getCrs().getName().getCode(); |
* may be <code>null</code>. Otherwise the SLD {@link File} to |
105 |
} |
* import and associate with this {@link StyledFS} |
106 |
|
* |
107 |
public CoordinateReferenceSystem getCrs() { |
* @param id |
108 |
return fs.getSchema().getDefaultGeometry().getCoordinateSystem(); |
* <code>null</code> is allowed and will autogenerate an id |
109 |
} |
*/ |
110 |
|
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
111 |
public Translation getDesc() { |
File sldFile, String id) { |
112 |
return desc; |
|
113 |
} |
this.fs = fs; |
114 |
|
|
115 |
public Envelope getEnvelope() { |
if (id == null) { |
116 |
try { |
this.id = StyledFS.class.getSimpleName() |
117 |
return fs.getBounds(); |
+ new Random(new Date().getTime()).nextInt(10000000); |
118 |
} catch (IOException e) { |
} else { |
119 |
e.printStackTrace(); |
this.id = id; |
120 |
return null; |
} |
121 |
} |
|
122 |
} |
this.sldFile = sldFile; |
123 |
|
|
124 |
public FeatureSource getGeoObject() throws Exception { |
// datei existiert, dann lesen |
125 |
return fs; |
if (sldFile != null && sldFile.exists()) { |
126 |
} |
try { |
127 |
|
style = StylingUtil.loadSLD(sldFile)[0]; |
128 |
public String getId() { |
} catch (Exception e) { |
129 |
return id; |
LOGGER.warn("Reading SLD failed: " + sldFile, e); |
130 |
} |
style = null; |
131 |
|
} |
132 |
public ImageIcon getImageIcon() { |
} |
133 |
return null; |
|
134 |
} |
title = new Translation(); |
135 |
|
desc = new Translation(); |
136 |
public URL getInfoURL() { |
|
137 |
return null; |
if (sldFile != null) { |
138 |
} |
title.fromOneLine(sldFile.getName()); |
139 |
|
desc.fromOneLine(sldFile.getAbsolutePath()); |
140 |
public Translation getKeywords() { |
} |
141 |
return null; |
|
142 |
} |
} |
143 |
|
|
144 |
public Style getStyle() { |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) { |
145 |
return style; |
this(fs, (File) null, null); |
146 |
} |
} |
147 |
|
|
148 |
public Translation getTitle() { |
public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
149 |
return title; |
String id) { |
150 |
} |
this(fs, null, id); |
151 |
|
} |
152 |
public boolean isDisposed() { |
|
153 |
return false; |
public void dispose() { |
154 |
} |
} |
155 |
|
|
156 |
/** |
/** |
157 |
* If true, this layer will not be shown in the legend. Default = false |
* Returns human readable {@link String} of the CRS natively used by this |
158 |
*/ |
* {@link DpLayer} |
159 |
/** |
* |
160 |
* |
* If CRS == null, it will call {@link #getGeoObject()} |
161 |
* Killed by SK: 6. April 09: Ein Layer soll nicht generell auf |
* |
162 |
* verstecken/nicht verstecken gestellt werden können. Das sind |
*/ |
163 |
* Eigenschaften der Karte/MapContext, ebenso wie die Reihenfolge der Layer. |
public String getCRSString() { |
164 |
* Im Atlas verwaltet deshalb nun die Klasse skrueger.atlas.Map welche Layer |
if (getCrs() == null) |
165 |
* nicht in der Legende auftauchen sollen. Meines Wissens hat keiner bisher |
return "CRS?"; |
166 |
* die Funktion genutzt. |
|
167 |
* |
return getCrs().getName().getCode(); |
168 |
* // public boolean isHideInLegend() { // return false; // } |
} |
169 |
*/ |
|
170 |
|
public CoordinateReferenceSystem getCrs() { |
171 |
public void setDesc(Translation dec) { |
if (crs == null) { |
172 |
this.desc = dec; |
crs = fs.getSchema().getCoordinateReferenceSystem(); |
173 |
} |
if (crs == null) { |
174 |
|
|
175 |
public void setImageIcon(ImageIcon icon) { |
crs = fs.getSchema().getGeometryDescriptor() |
176 |
} |
.getCoordinateReferenceSystem(); |
177 |
|
|
178 |
public void setKeywords(Translation keywords) { |
if (crs == null) { |
179 |
} |
LOGGER.warn("Could not determine the CRS of " + getTitle() |
180 |
|
+ ". Using default " |
181 |
public void setStyle(Style style) { |
+ GeoImportUtil.getDefaultCRS()); |
182 |
this.style = style; |
crs = GeoImportUtil.getDefaultCRS(); |
183 |
|
} |
184 |
} |
} |
185 |
|
} |
186 |
public void setTitle(Translation title) { |
return crs; |
187 |
this.title = title; |
} |
188 |
|
|
189 |
} |
public Translation getDesc() { |
190 |
|
return desc; |
191 |
public void uncache() { |
} |
192 |
} |
|
193 |
|
public Envelope getEnvelope() { |
194 |
/** |
try { |
195 |
* |
return fs.getBounds(); |
196 |
*/ |
} catch (IOException e) { |
197 |
public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() { |
e.printStackTrace(); |
198 |
if (map == null) { |
return null; |
199 |
|
} |
200 |
map = new HashMap<Integer, AttributeMetaData>(); |
} |
201 |
|
|
202 |
// Leaving out the first one, it will be the_geom |
public FeatureSource<SimpleFeatureType, SimpleFeature> getGeoObject() { |
203 |
for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) { |
return fs; |
204 |
AttributeType att = fs.getSchema().getAttributeType(i); |
} |
205 |
|
|
206 |
AttributeMetaData attMetaData = new AttributeMetaData(i, att |
public String getId() { |
207 |
.getLocalName()); |
return id; |
208 |
map.put(i, attMetaData); |
} |
209 |
} |
|
210 |
} |
public ImageIcon getImageIcon() { |
211 |
return map; |
return null; |
212 |
} |
} |
213 |
|
|
214 |
/** |
public URL getInfoURL() { |
215 |
* @return The {@link File} where the SLD was loaded from or |
return null; |
216 |
* <code>null</code> if there didn't exist a {@link File}. (It could |
} |
217 |
* be a WFS or a PostGIS |
|
218 |
* |
public Translation getKeywords() { |
219 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
return null; |
220 |
* Krüger</a> |
} |
221 |
*/ |
|
222 |
public File getSldFile() { |
public Style getStyle() { |
223 |
return sldFile; |
return style; |
224 |
} |
} |
225 |
|
|
226 |
public void setSldFile(File sldFile) { |
public Translation getTitle() { |
227 |
this.sldFile = sldFile; |
return title; |
228 |
} |
} |
229 |
|
|
230 |
} |
public boolean isDisposed() { |
231 |
|
return false; |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
|
* If true, this layer will not be shown in the legend. Default = false |
236 |
|
*/ |
237 |
|
/** |
238 |
|
* |
239 |
|
* Killed by SK: 6. April 09: Ein Layer soll nicht generell auf |
240 |
|
* verstecken/nicht verstecken gestellt werden können. Das sind |
241 |
|
* Eigenschaften der Karte/MapContext, ebenso wie die Reihenfolge der Layer. |
242 |
|
* Im Atlas verwaltet deshalb nun die Klasse skrueger.atlas.Map welche Layer |
243 |
|
* nicht in der Legende auftauchen sollen. Meines Wissens hat keiner bisher |
244 |
|
* die Funktion genutzt. |
245 |
|
* |
246 |
|
* // public boolean isHideInLegend() { // return false; // } |
247 |
|
*/ |
248 |
|
|
249 |
|
public void setDesc(Translation dec) { |
250 |
|
this.desc = dec; |
251 |
|
} |
252 |
|
|
253 |
|
public void setImageIcon(ImageIcon icon) { |
254 |
|
} |
255 |
|
|
256 |
|
public void setKeywords(Translation keywords) { |
257 |
|
} |
258 |
|
|
259 |
|
public void setStyle(Style style) { |
260 |
|
this.style = style; |
261 |
|
|
262 |
|
} |
263 |
|
|
264 |
|
public void setTitle(Translation title) { |
265 |
|
this.title = title; |
266 |
|
|
267 |
|
} |
268 |
|
|
269 |
|
public void uncache() { |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
|
* |
274 |
|
*/ |
275 |
|
@Override |
276 |
|
public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() { |
277 |
|
if (attMap == null) { |
278 |
|
attMap = StyledLayerUtil |
279 |
|
.createDefaultAttributeMetadataMap(getSchema()); |
280 |
|
} |
281 |
|
return attMap; |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
|
* @return The {@link File} where the SLD was loaded from or |
286 |
|
* <code>null</code> if there didn't exist a {@link File}. |
287 |
|
* |
288 |
|
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
289 |
|
*/ |
290 |
|
public File getSldFile() { |
291 |
|
return sldFile; |
292 |
|
} |
293 |
|
|
294 |
|
/** |
295 |
|
* Associates this .sld with the {@link FeatureSource}, but does not |
296 |
|
* automatically load the file. It must not even exist. |
297 |
|
* |
298 |
|
* @param sldFile |
299 |
|
*/ |
300 |
|
public void setSldFile(File sldFile) { |
301 |
|
this.sldFile = sldFile; |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
|
* Returns the features of the {@link FeatureSource}. |
306 |
|
* |
307 |
|
* @see {@link StyledFeaturesInterface} |
308 |
|
*/ |
309 |
|
@Override |
310 |
|
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() { |
311 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> features; |
312 |
|
try { |
313 |
|
features = getGeoObject().getFeatures(); |
314 |
|
} catch (IOException e) { |
315 |
|
throw new RuntimeException( |
316 |
|
"Error getting the features of the FeatureSource"); |
317 |
|
} |
318 |
|
return features; |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
|
* Same as {@link #getGeoObject()} method, but complies to the |
323 |
|
* {@link StyledFeaturesInterface} |
324 |
|
* |
325 |
|
* @see {@link StyledFeaturesInterface} |
326 |
|
*/ |
327 |
|
@Override |
328 |
|
public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() { |
329 |
|
return getGeoObject(); |
330 |
|
} |
331 |
|
|
332 |
|
@Override |
333 |
|
public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() { |
334 |
|
// final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = |
335 |
|
// getFeatureCollection(); |
336 |
|
// if (filter == Filter.EXCLUDE) |
337 |
|
// return new EmptyFeatureCollection(fc.getSchema()); |
338 |
|
// if (filter == Filter.INCLUDE) |
339 |
|
// return fc; |
340 |
|
// return fc.subCollection(filter); |
341 |
|
|
342 |
|
try { |
343 |
|
if (filter != Filter.INCLUDE) |
344 |
|
return getFeatureSource().getFeatures(filter); |
345 |
|
else |
346 |
|
return getFeatureSource().getFeatures(); |
347 |
|
} catch (IOException e) { |
348 |
|
throw new RuntimeException(e); |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
|
@Override |
353 |
|
public Filter getFilter() { |
354 |
|
return filter; |
355 |
|
} |
356 |
|
|
357 |
|
@Override |
358 |
|
public void setFilter(Filter filter) { |
359 |
|
this.filter = filter; |
360 |
|
} |
361 |
|
|
362 |
|
@Override |
363 |
|
public SimpleFeatureType getSchema() { |
364 |
|
return getGeoObject().getSchema(); |
365 |
|
} |
366 |
|
|
367 |
|
/** |
368 |
|
* Tries to load a style from the file denoted in {@link #getSldFile()}. If |
369 |
|
* the file doesn't exits, return <code>null</code>; |
370 |
|
* |
371 |
|
* @return <code>true</code> is style was loaded |
372 |
|
*/ |
373 |
|
public boolean loadStyle() { |
374 |
|
if (getSldFile() == null) |
375 |
|
return false; |
376 |
|
|
377 |
|
try { |
378 |
|
Style[] loadSLD = StylingUtil.loadSLD(getSldFile()); |
379 |
|
setStyle(loadSLD[0]); |
380 |
|
return true; |
381 |
|
} catch (Exception e) { |
382 |
|
return false; |
383 |
|
} |
384 |
|
|
385 |
|
} |
386 |
|
|
387 |
|
public void setTitle(String title) { |
388 |
|
setTitle(new Translation(title)); |
389 |
|
} |
390 |
|
|
391 |
|
public void setDesc(String desc) { |
392 |
|
setDesc(new Translation(desc)); |
393 |
|
} |
394 |
|
|
395 |
|
public void setCRS(CoordinateReferenceSystem crs2) { |
396 |
|
crs = crs2; |
397 |
|
} |
398 |
|
|
399 |
|
@Override |
400 |
|
public ReferencedEnvelope getReferencedEnvelope() { |
401 |
|
return new ReferencedEnvelope(getEnvelope(), getCrs()); |
402 |
|
} |
403 |
|
|
404 |
|
} |