/[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 315 by mojays, Wed Aug 26 11:03:27 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;
 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.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;
48    import org.opengis.feature.simple.SimpleFeatureType;
49    import org.opengis.feature.type.AttributeDescriptor;
50    import org.opengis.filter.Filter;
51  import org.opengis.referencing.crs.CoordinateReferenceSystem;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
52    
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.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 58  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 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 79  public class StyledFS implements StyledF Line 88  public class StyledFS implements StyledF
88    
89          private File sldFile;          private File sldFile;
90    
91          private HashMap<Integer, AttributeMetaData> map;          /** A map of simple attribute names to their meta-data **/
92            private AttributeMetadataMap<AttributeMetadataImpl> attMap;
93    
94            private Filter filter = Filter.INCLUDE;
95    
96          /**          /**
97           * 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 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 fs, File sldFile) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
111                            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                  if ((sldFile != null) && (sldFile.exists())) {                  // datei existiert, dann lesen
125                    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                  }                  }
                 title = new Translation();  
                 title.fromOneLine(sldFile.getName());  
133    
134                    title = new Translation();
135                  desc = new Translation();                  desc = new Translation();
136                  desc.fromOneLine(sldFile.getAbsolutePath());  
137                    if (sldFile != null) {
138                            title.fromOneLine(sldFile.getName());
139                            desc.fromOneLine(sldFile.getAbsolutePath());
140                    }
141    
142            }
143    
144            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
145                    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() {
154          }          }
155    
156          /**          /**
157           * Returnes human readable {@link String} of the CRS natively used by this           * Returns human readable {@link String} of the CRS natively used by this
158           * {@link DpLayer}           * {@link DpLayer}
159           *           *
160           * If crs == null, it will call {@link #getGeoObject()}           * If CRS == null, it will call {@link #getGeoObject()}
161           *           *
162           */           */
163          public String getCRSString() {          public String getCRSString() {
# Line 135  public class StyledFS implements StyledF Line 168  public class StyledFS implements StyledF
168          }          }
169    
170          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
171                  return fs.getSchema().getDefaultGeometry().getCoordinateSystem();                  if (crs == null) {
172                            crs = fs.getSchema().getCoordinateReferenceSystem();
173                            if (crs == null) {
174    
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;
187          }          }
188    
189          public Translation getDesc() {          public Translation getDesc() {
# Line 151  public class StyledFS implements StyledF Line 199  public class StyledFS implements StyledF
199                  }                  }
200          }          }
201    
202          public FeatureSource getGeoObject() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getGeoObject() {
203                  return fs;                  return fs;
204          }          }
205    
# Line 224  public class StyledFS implements StyledF Line 272  public class StyledFS implements StyledF
272          /**          /**
273           *           *
274           */           */
275          public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {          @Override
276                  if (map == null) {          public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
277                    if (attMap == null) {
278                          map = new HashMap<Integer, AttributeMetaData>();                          attMap = StyledLayerUtil
279                                            .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);  
                         }  
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 262  public class StyledFS implements StyledF Line 307  public class StyledFS implements StyledF
307           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
308           */           */
309          @Override          @Override
310          public FeatureCollection getFeatureCollection() {          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
311                  FeatureCollection features;                  FeatureCollection<SimpleFeatureType, SimpleFeature> features;
312                  try {                  try {
313                          features = getGeoObject().getFeatures();                          features = getGeoObject().getFeatures();
314                  } catch (IOException e) {                  } catch (IOException e) {
# Line 280  public class StyledFS implements StyledF Line 325  public class StyledFS implements StyledF
325           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
326           */           */
327          @Override          @Override
328          public FeatureSource getFeatureSource() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
329                  return getGeoObject();                  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  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26