/[schmitzm]/branches/2.4.x/src/skrueger/geotools/StyledFS.java
ViewVC logotype

Annotation of /branches/2.4.x/src/skrueger/geotools/StyledFS.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26