/[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

branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFS.java revision 464 by alfonx, Tue Oct 13 13:22:31 2009 UTC trunk/src/skrueger/geotools/StyledFS.java revision 1228 by alfonx, Wed Nov 3 20:44:16 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;
# Line 41  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.data.store.EmptyFeatureCollection;  
43  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
44    import org.geotools.feature.NameImpl;
45    import org.geotools.geometry.jts.ReferencedEnvelope;
46  import org.geotools.styling.Style;  import org.geotools.styling.Style;
47  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
48  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
# Line 52  import org.opengis.referencing.crs.Coord Line 52  import org.opengis.referencing.crs.Coord
52    
53  import schmitzm.geotools.io.GeoImportUtil;  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.AttributeMetadataInterface;
57  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
58    
59  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
# Line 61  import com.vividsolutions.jts.geom.Envel Line 62  import com.vividsolutions.jts.geom.Envel
62   * This class enables a non Atlas context to use the Atlas LayerPanel   * This class enables a non Atlas context to use the Atlas LayerPanel
63   * {@link JPanel} as a {@link MapContextManagerInterface}   * {@link JPanel} as a {@link MapContextManagerInterface}
64   *   *
65   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
66     *
67     *         TODO Rename to StyledShapefile
68   */   */
69  public class StyledFS implements StyledFeatureSourceInterface {  public class StyledFS implements StyledFeatureSourceInterface {
70          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);
71    
72          private final FeatureSource<SimpleFeatureType, SimpleFeature> fs;          private final FeatureSource<SimpleFeatureType, SimpleFeature> fs;
73    
74            /** Caching the CRS of the layer **/
75            CoordinateReferenceSystem crs = null;
76    
77          /**          /**
78           * 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
79           * than it should be ;-)           * than it should be ;-)
80           */           */
81          private String id;          final private String id;
82    
83          private Style style;          private Style style;
84    
# Line 82  public class StyledFS implements StyledF Line 88  public class StyledFS implements StyledF
88    
89          private File sldFile;          private File sldFile;
90    
91          private AttributeMetadataMap map;          /** A map of simple attribute names to their meta-data **/
92            private AttributeMetadataMap<AttributeMetadataImpl> attMap;
93    
94          private Filter filter = Filter.INCLUDE;          private Filter filter = Filter.INCLUDE;
95    
# Line 96  public class StyledFS implements StyledF Line 103  public class StyledFS implements StyledF
103           * @param sldFile           * @param sldFile
104           *            may be <code>null</code>. Otherwise the SLD {@link File} to           *            may be <code>null</code>. Otherwise the SLD {@link File} to
105           *            import and associate with this {@link StyledFS}           *            import and associate with this {@link StyledFS}
106             *
107             * @param id
108             *            <code>null</code> is allowed and will autogenerate an id
109           */           */
110          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
111                          File sldFile) {                          File sldFile, String id) {
112    
113                  this.fs = fs;                  this.fs = fs;
114                  id = StyledFS.class.getSimpleName()  
115                                  + new Random(new Date().getTime()).nextInt(10000000);                  if (id == null) {
116                            this.id = StyledFS.class.getSimpleName()
117                                            + new Random(new Date().getTime()).nextInt(10000000);
118                    } else {
119                            this.id = id;
120                    }
121    
122                  this.sldFile = sldFile;                  this.sldFile = sldFile;
123    
124                    // datei existiert, dann lesen
125                  if (sldFile != null && sldFile.exists()) {                  if (sldFile != null && sldFile.exists()) {
126                          try {                          try {
127                                  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");  
128                          } catch (Exception e) {                          } catch (Exception e) {
129                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);
130                                    style = null;
131                          }                          }
132                  }                  }
133    
134                  title = new Translation();                  title = new Translation();
135                  desc = new Translation();                  desc = new Translation();
136    
# Line 127  public class StyledFS implements StyledF Line 142  public class StyledFS implements StyledF
142          }          }
143    
144          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
145                  this(fs, null);                  this(fs, (File) null, null);
146            }
147    
148            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
149                            String id) {
150                    this(fs, null, id);
151          }          }
152    
153          public void dispose() {          public void dispose() {
# Line 148  public class StyledFS implements StyledF Line 168  public class StyledFS implements StyledF
168          }          }
169    
170          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
171                  CoordinateReferenceSystem crs = fs.getSchema().getCoordinateReferenceSystem();                  if (crs == null) {
172                  if (fs.getSchema().getCoordinateReferenceSystem() == null) {                          crs = fs.getSchema().getCoordinateReferenceSystem();
173                          LOGGER.warn("Could not determine the CRS of "+getTitle()+". Using default "+GeoImportUtil.getDefaultCRS());                          if (crs == null) {
174                          crs = GeoImportUtil.getDefaultCRS();  
175                                    crs = fs.getSchema().getGeometryDescriptor()
176                                                    .getCoordinateReferenceSystem();
177    
178                                    if (crs == null) {
179                                            LOGGER.warn("Could not determine the CRS of " + getTitle()
180                                                            + ". Using default "
181                                                            + GeoImportUtil.getDefaultCRS());
182                                            crs = GeoImportUtil.getDefaultCRS();
183                                    }
184                            }
185                  }                  }
186                  return crs;                  return crs;
187          }          }
# Line 242  public class StyledFS implements StyledF Line 272  public class StyledFS implements StyledF
272          /**          /**
273           *           *
274           */           */
275          public AttributeMetadataMap getAttributeMetaDataMap() {          @Override
276                  if (map == null) {          public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
277                    if (attMap == null) {
278                          map = new AttributeMetadataMap();                          attMap = StyledLayerUtil
279                                            .createDefaultAttributeMetadataMap(getSchema());
                         // Leaving out the first one, it will be the_geom  
                         for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {  
                                 AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i);  
   
                                 AttributeMetadata attMetaData = new AttributeMetadata(attDesc.getName());  
                                 map.put(attDesc.getName(), attMetaData);  
                         }  
280                  }                  }
281                  return map;                  return attMap;
282          }          }
283    
284          /**          /**
285           * @return The {@link File} where the SLD was loaded from or           * @return The {@link File} where the SLD was loaded from or
286           *         <code>null</code> if there didn't exist a {@link File}.           *         <code>null</code> if there didn't exist a {@link File}.
287           *           *
288           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
289           */           */
290          public File getSldFile() {          public File getSldFile() {
291                  return sldFile;                  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) {          public void setSldFile(File sldFile) {
301                  this.sldFile = sldFile;                  this.sldFile = sldFile;
302          }          }
# Line 303  public class StyledFS implements StyledF Line 331  public class StyledFS implements StyledF
331    
332          @Override          @Override
333          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {
334                  final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection();                  // final FeatureCollection<SimpleFeatureType, SimpleFeature> fc =
335                  if (filter == Filter.EXCLUDE)                  // getFeatureCollection();
336                          return new EmptyFeatureCollection(fc.getSchema());                  // if (filter == Filter.EXCLUDE)
337                  if (filter == Filter.INCLUDE)                  // return new EmptyFeatureCollection(fc.getSchema());
338                          return fc;                  // if (filter == Filter.INCLUDE)
339                  return fc.subCollection(filter);                  // 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          @Override
# Line 326  public class StyledFS implements StyledF Line 364  public class StyledFS implements StyledF
364                  return getGeoObject().getSchema();                  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  }  }

Legend:
Removed from v.464  
changed lines
  Added in v.1228

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26