/[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 256 by alfonx, Fri Jul 31 14:43:47 2009 UTC revision 1273 by alfonx, Mon Nov 15 16:22:47 2010 UTC
# Line 25  Line 25 
25   *   *
26   * Contributors:   * Contributors:
27   *     Martin O. J. Schmitz - initial API and implementation   *     Martin O. J. Schmitz - initial API and implementation
28   *     Stefan A. Krüger - additional utility classes   *     Stefan A. Tzeggai - additional utility classes
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger.geotools;  package skrueger.geotools;
31    
32  import java.io.File;  import java.io.File;
 import java.io.FileNotFoundException;  
33  import java.io.IOException;  import java.io.IOException;
34  import java.net.URL;  import java.net.URL;
35  import java.util.Date;  import java.util.Date;
 import java.util.HashMap;  
 import java.util.Map;  
36  import java.util.Random;  import java.util.Random;
37    
38  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
# Line 43  import javax.swing.JPanel; Line 40  import javax.swing.JPanel;
40    
41  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
42  import org.geotools.data.FeatureSource;  import org.geotools.data.FeatureSource;
 import org.geotools.feature.AttributeType;  
43  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
44    import org.geotools.geometry.jts.ReferencedEnvelope;
45  import org.geotools.styling.Style;  import org.geotools.styling.Style;
46    import org.opengis.feature.simple.SimpleFeature;
47    import org.opengis.feature.simple.SimpleFeatureType;
48    import org.opengis.filter.Filter;
49  import org.opengis.referencing.crs.CoordinateReferenceSystem;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
50    
51    import schmitzm.geotools.feature.FeatureUtil;
52    import schmitzm.geotools.feature.FeatureUtil.GeometryForm;
53    import schmitzm.geotools.io.GeoImportUtil;
54  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
55  import skrueger.AttributeMetaData;  import skrueger.AttributeMetadataImpl;
56  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
57    
58  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
# Line 58  import com.vividsolutions.jts.geom.Envel Line 61  import com.vividsolutions.jts.geom.Envel
61   * This class enables a non Atlas context to use the Atlas LayerPanel   * This class enables a non Atlas context to use the Atlas LayerPanel
62   * {@link JPanel} as a {@link MapContextManagerInterface}   * {@link JPanel} as a {@link MapContextManagerInterface}
63   *   *
64   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
65     *
66     *         TODO Rename to StyledShapefile
67   */   */
68  public class StyledFS implements StyledFeatureSourceInterface {  public class StyledFS implements StyledFeatureSourceInterface {
69          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);
70    
71          private final FeatureSource fs;          private final FeatureSource<SimpleFeatureType, SimpleFeature> fs;
72    
73            /** Caching the CRS of the layer **/
74            CoordinateReferenceSystem crs = null;
75    
76          /**          /**
77           * A unique ID which identifies the Layer in the Atlas. It's more important           * A unique ID which identifies the Layer in the Atlas. It's more important
78           * than it should be ;-)           * than it should be ;-)
79           */           */
80          private String id;          final private String id;
81    
82          private Style style;          private Style style;
83    
# Line 79  public class StyledFS implements StyledF Line 87  public class StyledFS implements StyledF
87    
88          private File sldFile;          private File sldFile;
89    
90          private HashMap<Integer, AttributeMetaData> map;          /** A map of simple attribute names to their meta-data **/
91            private AttributeMetadataMap<AttributeMetadataImpl> attMap;
92    
93            private Filter filter = Filter.INCLUDE;
94    
95          /**          /**
96           * This class enables a non Atlas context to use the Atlas LayerPanel           * This class enables a non Atlas context to use the Atlas LayerPanel
# Line 91  public class StyledFS implements StyledF Line 102  public class StyledFS implements StyledF
102           * @param sldFile           * @param sldFile
103           *            may be <code>null</code>. Otherwise the SLD {@link File} to           *            may be <code>null</code>. Otherwise the SLD {@link File} to
104           *            import and associate with this {@link StyledFS}           *            import and associate with this {@link StyledFS}
105             *
106             * @param id
107             *            <code>null</code> is allowed and will autogenerate an id
108           */           */
109          public StyledFS(FeatureSource fs, File sldFile) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
110                            File sldFile, String id) {
111    
112                  this.fs = fs;                  this.fs = fs;
113                  id = StyledFS.class.getSimpleName()  
114                                  + new Random(new Date().getTime()).nextInt(10000000);                  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    
121                  this.sldFile = sldFile;                  this.sldFile = sldFile;
122    
123                  if ((sldFile != null) && (sldFile.exists())) {                  // datei existiert, dann lesen
124                    if (sldFile != null && sldFile.exists()) {
125                          try {                          try {
126                                  style = StylingUtil.loadSLD(sldFile)[0];                                  style = StylingUtil.loadSLD(sldFile)[0];
                         } catch (FileNotFoundException e) {  
                                 LOGGER  
                                                 .debug("The SLD file passed was empty. Leaving the Style untouched. (We are in the constructor.. so its null");  
127                          } catch (Exception e) {                          } catch (Exception e) {
128                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);
129                                    style = null;
130                          }                          }
131                  }                  }
                 title = new Translation();  
                 title.fromOneLine(sldFile.getName());  
132    
133                    title = new Translation();
134                  desc = new Translation();                  desc = new Translation();
135                  desc.fromOneLine(sldFile.getAbsolutePath());  
136                    if (sldFile != null) {
137                            title.fromOneLine(sldFile.getName());
138                            desc.fromOneLine(sldFile.getAbsolutePath());
139                    }
140    
141            }
142    
143            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
144                    this(fs, (File) null, null);
145            }
146    
147            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
148                            String id) {
149                    this(fs, null, id);
150          }          }
151    
152          public void dispose() {          public void dispose() {
153          }          }
154    
155          /**          /**
156           * Returnes human readable {@link String} of the CRS natively used by this           * Returns human readable {@link String} of the CRS natively used by this
157           * {@link DpLayer}           * {@link DpLayer}
158           *           *
159           * If crs == null, it will call {@link #getGeoObject()}           * If CRS == null, it will call {@link #getGeoObject()}
160           *           *
161           */           */
162          public String getCRSString() {          public String getCRSString() {
# Line 135  public class StyledFS implements StyledF Line 167  public class StyledFS implements StyledF
167          }          }
168    
169          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
170                  return fs.getSchema().getDefaultGeometry().getCoordinateSystem();                  if (crs == null) {
171                            crs = fs.getSchema().getCoordinateReferenceSystem();
172                            if (crs == null) {
173    
174                                    crs = fs.getSchema().getGeometryDescriptor()
175                                                    .getCoordinateReferenceSystem();
176    
177                                    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                            }
184                    }
185                    return crs;
186          }          }
187    
188          public Translation getDesc() {          public Translation getDesc() {
# Line 151  public class StyledFS implements StyledF Line 198  public class StyledFS implements StyledF
198                  }                  }
199          }          }
200    
201          public FeatureSource getGeoObject() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getGeoObject() {
202                  return fs;                  return fs;
203          }          }
204    
# Line 224  public class StyledFS implements StyledF Line 271  public class StyledFS implements StyledF
271          /**          /**
272           *           *
273           */           */
274          public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {          @Override
275                  if (map == null) {          public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
276                    if (attMap == null) {
277                          map = new HashMap<Integer, AttributeMetaData>();                          attMap = StyledLayerUtil
278                                            .createDefaultAttributeMetadataMap(getSchema());
                         // Leaving out the first one, it will be the_geom  
                         for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {  
                                 AttributeType att = fs.getSchema().getAttributeType(i);  
   
                                 AttributeMetaData attMetaData = new AttributeMetaData(i, att  
                                                 .getLocalName());  
                                 map.put(i, attMetaData);  
                         }  
279                  }                  }
280                  return map;                  return attMap;
281          }          }
282    
283          /**          /**
284           * @return The {@link File} where the SLD was loaded from or           * @return The {@link File} where the SLD was loaded from or
285           *         <code>null</code> if there didn't exist a {@link File}.           *         <code>null</code> if there didn't exist a {@link File}.
286           *           *
287           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
288           */           */
289          public File getSldFile() {          public File getSldFile() {
290                  return sldFile;                  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) {          public void setSldFile(File sldFile) {
300                  this.sldFile = sldFile;                  this.sldFile = sldFile;
301          }          }
# Line 262  public class StyledFS implements StyledF Line 306  public class StyledFS implements StyledF
306           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
307           */           */
308          @Override          @Override
309          public FeatureCollection getFeatureCollection() {          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
310                  FeatureCollection features;                  FeatureCollection<SimpleFeatureType, SimpleFeature> features;
311                  try {                  try {
312                          features = getGeoObject().getFeatures();                          features = getGeoObject().getFeatures();
313                  } catch (IOException e) {                  } catch (IOException e) {
# Line 280  public class StyledFS implements StyledF Line 324  public class StyledFS implements StyledF
324           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
325           */           */
326          @Override          @Override
327          public FeatureSource getFeatureSource() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
328                  return getGeoObject();                  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.256  
changed lines
  Added in v.1273

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26