/[schmitzm]/trunk/src/skrueger/geotools/StyledFS.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/StyledFS.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 223 by alfonx, Tue Jul 14 15:01:57 2009 UTC revision 1273 by alfonx, Mon Nov 15 16:22:47 2010 UTC
# Line 1  Line 1 
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.feature.FeatureCollection;   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  import org.geotools.styling.Style;   * GNU General Public License for more details.
20  import org.opengis.referencing.crs.CoordinateReferenceSystem;   *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22  import schmitzm.geotools.styling.StylingUtil;   * along with this program; if not, write to the Free Software
23  import skrueger.AttributeMetaData;   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  import skrueger.i8n.Translation;   * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26  import com.vividsolutions.jts.geom.Envelope;   * Contributors:
27     *     Martin O. J. Schmitz - initial API and implementation
28  /**   *     Stefan A. Tzeggai - additional utility classes
29   * This class enables a non Atlas context to use the Atlas LayerPanel   ******************************************************************************/
30   * {@link JPanel} as a {@link MapContextManagerInterface}  package skrueger.geotools;
31   *  
32   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>  import java.io.File;
33   */  import java.io.IOException;
34  public class StyledFS implements StyledFeatureSourceInterface {  import java.net.URL;
35          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);  import java.util.Date;
36    import java.util.Random;
37          private final FeatureSource fs;  
38    import javax.swing.ImageIcon;
39          /**  import javax.swing.JPanel;
40           * A unique ID which identifies the Layer in the Atlas. It's more important  
41           * than it should be ;-)  import org.apache.log4j.Logger;
42           */  import org.geotools.data.FeatureSource;
43          private String id;  import org.geotools.feature.FeatureCollection;
44    import org.geotools.geometry.jts.ReferencedEnvelope;
45          private Style style;  import org.geotools.styling.Style;
46    import org.opengis.feature.simple.SimpleFeature;
47          private Translation title;  import org.opengis.feature.simple.SimpleFeatureType;
48    import org.opengis.filter.Filter;
49          private Translation desc;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
50    
51          private File sldFile;  import schmitzm.geotools.feature.FeatureUtil;
52    import schmitzm.geotools.feature.FeatureUtil.GeometryForm;
53          private HashMap<Integer, AttributeMetaData> map;  import schmitzm.geotools.io.GeoImportUtil;
54    import schmitzm.geotools.styling.StylingUtil;
55          /**  import skrueger.AttributeMetadataImpl;
56           * This class enables a non Atlas context to use the Atlas LayerPanel  import skrueger.i8n.Translation;
57           * {@link JPanel} as a {@link MapContextManagerInterface}  
58           *  import com.vividsolutions.jts.geom.Envelope;
59           * @param fs  
60           *            {@link FeatureSource} that is beeing styled.  /**
61           *   * This class enables a non Atlas context to use the Atlas LayerPanel
62           * @param sldFile   * {@link JPanel} as a {@link MapContextManagerInterface}
63           *            may be <code>null</code>. Otherwise the SLD {@link File} to   *
64           *            import and associate with this {@link StyledFS}   * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
65           */   *
66          public StyledFS(FeatureSource fs, File sldFile) {   *         TODO Rename to StyledShapefile
67     */
68                  this.fs = fs;  public class StyledFS implements StyledFeatureSourceInterface {
69                  id = StyledFS.class.getSimpleName()          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);
70                                  + new Random(new Date().getTime()).nextInt(10000000);  
71            private final FeatureSource<SimpleFeatureType, SimpleFeature> fs;
72                  this.sldFile = sldFile;  
73            /** Caching the CRS of the layer **/
74                  if ((sldFile != null) && (sldFile.exists())) {          CoordinateReferenceSystem crs = null;
75                          try {  
76                                  style = StylingUtil.loadSLD(sldFile)[0];          /**
77                          } catch (FileNotFoundException e) {           * A unique ID which identifies the Layer in the Atlas. It's more important
78                                  LOGGER           * than it should be ;-)
79                                                  .debug("The SLD file passed was empty. Leaving the Style untouched. (We are in the constructor.. so its null");           */
80                          } catch (Exception e) {          final private String id;
81                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);  
82                          }          private Style style;
83                  }  
84                  title = new Translation();          private Translation title;
85                  title.fromOneLine(sldFile.getName());  
86            private Translation desc;
87                  desc = new Translation();  
88                  desc.fromOneLine(sldFile.getAbsolutePath());          private File sldFile;
89          }  
90            /** A map of simple attribute names to their meta-data **/
91          public void dispose() {          private AttributeMetadataMap<AttributeMetadataImpl> attMap;
92          }  
93            private Filter filter = Filter.INCLUDE;
94          /**  
95           * Returnes human readable {@link String} of the CRS natively used by this          /**
96           * {@link DpLayer}           * This class enables a non Atlas context to use the Atlas LayerPanel
97           *           * {@link JPanel} as a {@link MapContextManagerInterface}
98           * If crs == null, it will call {@link #getGeoObject()}           *
99           *           * @param fs
100           */           *            {@link FeatureSource} that is beeing styled.
101          public String getCRSString() {           *
102                  if (getCrs() == null)           * @param sldFile
103                          return "CRS?";           *            may be <code>null</code>. Otherwise the SLD {@link File} to
104             *            import and associate with this {@link StyledFS}
105                  return getCrs().getName().getCode();           *
106          }           * @param id
107             *            <code>null</code> is allowed and will autogenerate an id
108          public CoordinateReferenceSystem getCrs() {           */
109                  return fs.getSchema().getDefaultGeometry().getCoordinateSystem();          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
110          }                          File sldFile, String id) {
111    
112          public Translation getDesc() {                  this.fs = fs;
113                  return desc;  
114          }                  if (id == null) {
115                            this.id = StyledFS.class.getSimpleName()
116          public Envelope getEnvelope() {                                          + new Random(new Date().getTime()).nextInt(10000000);
117                  try {                  } else {
118                          return fs.getBounds();                          this.id = id;
119                  } catch (IOException e) {                  }
120                          e.printStackTrace();  
121                          return null;                  this.sldFile = sldFile;
122                  }  
123          }                  // datei existiert, dann lesen
124                    if (sldFile != null && sldFile.exists()) {
125          public FeatureSource getGeoObject() {                          try {
126                  return fs;                                  style = StylingUtil.loadSLD(sldFile)[0];
127          }                          } catch (Exception e) {
128                                    LOGGER.warn("Reading SLD failed: " + sldFile, e);
129          public String getId() {                                  style = null;
130                  return id;                          }
131          }                  }
132    
133          public ImageIcon getImageIcon() {                  title = new Translation();
134                  return null;                  desc = new Translation();
135          }  
136                    if (sldFile != null) {
137          public URL getInfoURL() {                          title.fromOneLine(sldFile.getName());
138                  return null;                          desc.fromOneLine(sldFile.getAbsolutePath());
139          }                  }
140    
141          public Translation getKeywords() {          }
142                  return null;  
143          }          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
144                    this(fs, (File) null, null);
145          public Style getStyle() {          }
146                  return style;  
147          }          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
148                            String id) {
149          public Translation getTitle() {                  this(fs, null, id);
150                  return title;          }
151          }  
152            public void dispose() {
153          public boolean isDisposed() {          }
154                  return false;  
155          }          /**
156             * Returns human readable {@link String} of the CRS natively used by this
157          /**           * {@link DpLayer}
158           * If true, this layer will not be shown in the legend. Default = false           *
159           */           * If CRS == null, it will call {@link #getGeoObject()}
160          /**           *
161           *           */
162           * Killed by SK: 6. April 09: Ein Layer soll nicht generell auf          public String getCRSString() {
163           * verstecken/nicht verstecken gestellt werden können. Das sind                  if (getCrs() == null)
164           * Eigenschaften der Karte/MapContext, ebenso wie die Reihenfolge der Layer.                          return "CRS?";
165           * Im Atlas verwaltet deshalb nun die Klasse skrueger.atlas.Map welche Layer  
166           * nicht in der Legende auftauchen sollen. Meines Wissens hat keiner bisher                  return getCrs().getName().getCode();
167           * die Funktion genutzt.          }
168           *  
169           * // public boolean isHideInLegend() { // return false; // }          public CoordinateReferenceSystem getCrs() {
170           */                  if (crs == null) {
171                            crs = fs.getSchema().getCoordinateReferenceSystem();
172          public void setDesc(Translation dec) {                          if (crs == null) {
173                  this.desc = dec;  
174          }                                  crs = fs.getSchema().getGeometryDescriptor()
175                                                    .getCoordinateReferenceSystem();
176          public void setImageIcon(ImageIcon icon) {  
177          }                                  if (crs == null) {
178                                            LOGGER.warn("Could not determine the CRS of " + getTitle()
179          public void setKeywords(Translation keywords) {                                                          + ". Using default "
180          }                                                          + GeoImportUtil.getDefaultCRS());
181                                            crs = GeoImportUtil.getDefaultCRS();
182          public void setStyle(Style style) {                                  }
183                  this.style = style;                          }
184                    }
185          }                  return crs;
186            }
187          public void setTitle(Translation title) {  
188                  this.title = title;          public Translation getDesc() {
189                    return desc;
190          }          }
191    
192          public void uncache() {          public Envelope getEnvelope() {
193          }                  try {
194                            return fs.getBounds();
195          /**                  } catch (IOException e) {
196           *                          e.printStackTrace();
197           */                          return null;
198          public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {                  }
199                  if (map == null) {          }
200    
201                          map = new HashMap<Integer, AttributeMetaData>();          public FeatureSource<SimpleFeatureType, SimpleFeature> getGeoObject() {
202                    return fs;
203                          // Leaving out the first one, it will be the_geom          }
204                          for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {  
205                                  AttributeType att = fs.getSchema().getAttributeType(i);          public String getId() {
206                    return id;
207                                  AttributeMetaData attMetaData = new AttributeMetaData(i, att          }
208                                                  .getLocalName());  
209                                  map.put(i, attMetaData);          public ImageIcon getImageIcon() {
210                          }                  return null;
211                  }          }
212                  return map;  
213          }          public URL getInfoURL() {
214                    return null;
215          /**          }
216           * @return The {@link File} where the SLD was loaded from or  
217           *         <code>null</code> if there didn't exist a {@link File}. (It could          public Translation getKeywords() {
218           *         be a WFS or a PostGIS                  return null;
219           *          }
220           * @author <a href="mailto:[email protected]">Stefan Alfons  
221           *         Kr&uuml;ger</a>          public Style getStyle() {
222           */                  return style;
223          public File getSldFile() {          }
224                  return sldFile;  
225          }          public Translation getTitle() {
226                    return title;
227          public void setSldFile(File sldFile) {          }
228                  this.sldFile = sldFile;  
229          }          public boolean isDisposed() {
230                    return false;
231          /**          }
232           * Returns the features of the {@link FeatureSource}.  
233           *          /**
234           * @see {@link StyledFeaturesInterface}           * If true, this layer will not be shown in the legend. Default = false
235           */           */
236          @Override          /**
237          public FeatureCollection getFeatureCollection() {           *
238                  FeatureCollection features;           * Killed by SK: 6. April 09: Ein Layer soll nicht generell auf
239                  try {           * verstecken/nicht verstecken gestellt werden können. Das sind
240                          features = getGeoObject().getFeatures();           * Eigenschaften der Karte/MapContext, ebenso wie die Reihenfolge der Layer.
241                  } catch (IOException e) {           * Im Atlas verwaltet deshalb nun die Klasse skrueger.atlas.Map welche Layer
242                          throw new RuntimeException(           * nicht in der Legende auftauchen sollen. Meines Wissens hat keiner bisher
243                                          "Error getting the features of the  FeatureSource");           * die Funktion genutzt.
244                  }           *
245                  return features;           * // public boolean isHideInLegend() { // return false; // }
246          }           */
247    
248          /**          public void setDesc(Translation dec) {
249           * Same as {@link #getGeoObject()} method, but complies to the                  this.desc = dec;
250           * {@link StyledFeaturesInterface}          }
251           *  
252           * @see {@link StyledFeaturesInterface}          public void setImageIcon(ImageIcon icon) {
253           */          }
254          @Override  
255          public FeatureSource getFeatureSource() {          public void setKeywords(Translation keywords) {
256                  return getGeoObject();          }
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            @Override
275            public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
276                    if (attMap == null) {
277                            attMap = StyledLayerUtil
278                                            .createDefaultAttributeMetadataMap(getSchema());
279                    }
280                    return attMap;
281            }
282    
283            /**
284             * @return The {@link File} where the SLD was loaded from or
285             *         <code>null</code> if there didn't exist a {@link File}.
286             *
287             * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
288             */
289            public File getSldFile() {
290                    return sldFile;
291            }
292    
293            /**
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            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            public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
310                    FeatureCollection<SimpleFeatureType, SimpleFeature> features;
311                    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            public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
328                    return getGeoObject();
329            }
330    
331            @Override
332            public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {
333                    // 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                    try {
342                            if (filter != Filter.INCLUDE)
343                                    return getFeatureSource().getFeatures(filter);
344                            else
345                                    return getFeatureSource().getFeatures();
346                    } catch (IOException e) {
347                            throw new RuntimeException(e);
348                    }
349            }
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            /**
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             *
370             * @return <code>true</code> is style was loaded
371             */
372            public boolean loadStyle() {
373                    if (getSldFile() == null)
374                            return false;
375    
376                    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            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            public void setCRS(CoordinateReferenceSystem crs2) {
395                    crs = crs2;
396            }
397    
398            @Override
399            public ReferencedEnvelope getReferencedEnvelope() {
400                    return new ReferencedEnvelope(getEnvelope(), getCrs());
401            }
402    
403            /**
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    }

Legend:
Removed from v.223  
changed lines
  Added in v.1273

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26