/[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 1231 by alfonx, Thu Nov 4 00:53:20 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.io.GeoImportUtil;
52  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
53  import skrueger.AttributeMetaData;  import skrueger.AttributeMetadataImpl;
54  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
55    
56  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
# Line 58  import com.vividsolutions.jts.geom.Envel Line 59  import com.vividsolutions.jts.geom.Envel
59   * This class enables a non Atlas context to use the Atlas LayerPanel   * This class enables a non Atlas context to use the Atlas LayerPanel
60   * {@link JPanel} as a {@link MapContextManagerInterface}   * {@link JPanel} as a {@link MapContextManagerInterface}
61   *   *
62   * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>   * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
63     *
64     *         TODO Rename to StyledShapefile
65   */   */
66  public class StyledFS implements StyledFeatureSourceInterface {  public class StyledFS implements StyledFeatureSourceInterface {
67          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);          private static final Logger LOGGER = Logger.getLogger(StyledFS.class);
68    
69          private final FeatureSource fs;          private final FeatureSource<SimpleFeatureType, SimpleFeature> fs;
70    
71            /** Caching the CRS of the layer **/
72            CoordinateReferenceSystem crs = null;
73    
74          /**          /**
75           * 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
76           * than it should be ;-)           * than it should be ;-)
77           */           */
78          private String id;          final private String id;
79    
80          private Style style;          private Style style;
81    
# Line 79  public class StyledFS implements StyledF Line 85  public class StyledFS implements StyledF
85    
86          private File sldFile;          private File sldFile;
87    
88          private HashMap<Integer, AttributeMetaData> map;          /** A map of simple attribute names to their meta-data **/
89            private AttributeMetadataMap<AttributeMetadataImpl> attMap;
90    
91            private Filter filter = Filter.INCLUDE;
92    
93          /**          /**
94           * 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 100  public class StyledFS implements StyledF
100           * @param sldFile           * @param sldFile
101           *            may be <code>null</code>. Otherwise the SLD {@link File} to           *            may be <code>null</code>. Otherwise the SLD {@link File} to
102           *            import and associate with this {@link StyledFS}           *            import and associate with this {@link StyledFS}
103             *
104             * @param id
105             *            <code>null</code> is allowed and will autogenerate an id
106           */           */
107          public StyledFS(FeatureSource fs, File sldFile) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
108                            File sldFile, String id) {
109    
110                  this.fs = fs;                  this.fs = fs;
111                  id = StyledFS.class.getSimpleName()  
112                                  + new Random(new Date().getTime()).nextInt(10000000);                  if (id == null) {
113                            this.id = StyledFS.class.getSimpleName()
114                                            + new Random(new Date().getTime()).nextInt(10000000);
115                    } else {
116                            this.id = id;
117                    }
118    
119                  this.sldFile = sldFile;                  this.sldFile = sldFile;
120    
121                  if ((sldFile != null) && (sldFile.exists())) {                  // datei existiert, dann lesen
122                    if (sldFile != null && sldFile.exists()) {
123                          try {                          try {
124                                  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");  
125                          } catch (Exception e) {                          } catch (Exception e) {
126                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);
127                                    style = null;
128                          }                          }
129                  }                  }
                 title = new Translation();  
                 title.fromOneLine(sldFile.getName());  
130    
131                    title = new Translation();
132                  desc = new Translation();                  desc = new Translation();
133                  desc.fromOneLine(sldFile.getAbsolutePath());  
134                    if (sldFile != null) {
135                            title.fromOneLine(sldFile.getName());
136                            desc.fromOneLine(sldFile.getAbsolutePath());
137                    }
138    
139            }
140    
141            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
142                    this(fs, (File) null, null);
143            }
144    
145            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
146                            String id) {
147                    this(fs, null, id);
148          }          }
149    
150          public void dispose() {          public void dispose() {
151          }          }
152    
153          /**          /**
154           * Returnes human readable {@link String} of the CRS natively used by this           * Returns human readable {@link String} of the CRS natively used by this
155           * {@link DpLayer}           * {@link DpLayer}
156           *           *
157           * If crs == null, it will call {@link #getGeoObject()}           * If CRS == null, it will call {@link #getGeoObject()}
158           *           *
159           */           */
160          public String getCRSString() {          public String getCRSString() {
# Line 135  public class StyledFS implements StyledF Line 165  public class StyledFS implements StyledF
165          }          }
166    
167          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
168                  return fs.getSchema().getDefaultGeometry().getCoordinateSystem();                  if (crs == null) {
169                            crs = fs.getSchema().getCoordinateReferenceSystem();
170                            if (crs == null) {
171    
172                                    crs = fs.getSchema().getGeometryDescriptor()
173                                                    .getCoordinateReferenceSystem();
174    
175                                    if (crs == null) {
176                                            LOGGER.warn("Could not determine the CRS of " + getTitle()
177                                                            + ". Using default "
178                                                            + GeoImportUtil.getDefaultCRS());
179                                            crs = GeoImportUtil.getDefaultCRS();
180                                    }
181                            }
182                    }
183                    return crs;
184          }          }
185    
186          public Translation getDesc() {          public Translation getDesc() {
# Line 151  public class StyledFS implements StyledF Line 196  public class StyledFS implements StyledF
196                  }                  }
197          }          }
198    
199          public FeatureSource getGeoObject() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getGeoObject() {
200                  return fs;                  return fs;
201          }          }
202    
# Line 224  public class StyledFS implements StyledF Line 269  public class StyledFS implements StyledF
269          /**          /**
270           *           *
271           */           */
272          public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {          @Override
273                  if (map == null) {          public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
274                    if (attMap == null) {
275                          map = new HashMap<Integer, AttributeMetaData>();                          attMap = StyledLayerUtil
276                                            .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);  
                         }  
277                  }                  }
278                  return map;                  return attMap;
279          }          }
280    
281          /**          /**
282           * @return The {@link File} where the SLD was loaded from or           * @return The {@link File} where the SLD was loaded from or
283           *         <code>null</code> if there didn't exist a {@link File}.           *         <code>null</code> if there didn't exist a {@link File}.
284           *           *
285           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
286           */           */
287          public File getSldFile() {          public File getSldFile() {
288                  return sldFile;                  return sldFile;
289          }          }
290    
291            /**
292             * Associates this .sld with the {@link FeatureSource}, but does not
293             * automatically load the file. It must not even exist.
294             *
295             * @param sldFile
296             */
297          public void setSldFile(File sldFile) {          public void setSldFile(File sldFile) {
298                  this.sldFile = sldFile;                  this.sldFile = sldFile;
299          }          }
# Line 262  public class StyledFS implements StyledF Line 304  public class StyledFS implements StyledF
304           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
305           */           */
306          @Override          @Override
307          public FeatureCollection getFeatureCollection() {          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
308                  FeatureCollection features;                  FeatureCollection<SimpleFeatureType, SimpleFeature> features;
309                  try {                  try {
310                          features = getGeoObject().getFeatures();                          features = getGeoObject().getFeatures();
311                  } catch (IOException e) {                  } catch (IOException e) {
# Line 280  public class StyledFS implements StyledF Line 322  public class StyledFS implements StyledF
322           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
323           */           */
324          @Override          @Override
325          public FeatureSource getFeatureSource() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
326                  return getGeoObject();                  return getGeoObject();
327          }          }
328    
329            @Override
330            public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {
331                    // final FeatureCollection<SimpleFeatureType, SimpleFeature> fc =
332                    // getFeatureCollection();
333                    // if (filter == Filter.EXCLUDE)
334                    // return new EmptyFeatureCollection(fc.getSchema());
335                    // if (filter == Filter.INCLUDE)
336                    // return fc;
337                    // return fc.subCollection(filter);
338    
339                    try {
340                            if (filter != Filter.INCLUDE)
341                                    return getFeatureSource().getFeatures(filter);
342                            else
343                                    return getFeatureSource().getFeatures();
344                    } catch (IOException e) {
345                            throw new RuntimeException(e);
346                    }
347            }
348    
349            @Override
350            public Filter getFilter() {
351                    return filter;
352            }
353    
354            @Override
355            public void setFilter(Filter filter) {
356                    this.filter = filter;
357            }
358    
359            @Override
360            public SimpleFeatureType getSchema() {
361                    return getGeoObject().getSchema();
362            }
363    
364            /**
365             * Tries to load a style from the file denoted in {@link #getSldFile()}. If
366             * the file doesn't exits, return <code>null</code>;
367             *
368             * @return <code>true</code> is style was loaded
369             */
370            public boolean loadStyle() {
371                    if (getSldFile() == null)
372                            return false;
373    
374                    try {
375                            Style[] loadSLD = StylingUtil.loadSLD(getSldFile());
376                            setStyle(loadSLD[0]);
377                            return true;
378                    } catch (Exception e) {
379                            return false;
380                    }
381    
382            }
383    
384            public void setTitle(String title) {
385                    setTitle(new Translation(title));
386            }
387    
388            public void setDesc(String desc) {
389                    setDesc(new Translation(desc));
390            }
391    
392            public void setCRS(CoordinateReferenceSystem crs2) {
393                    crs = crs2;
394            }
395    
396            @Override
397            public ReferencedEnvelope getReferencedEnvelope() {
398                    return new ReferencedEnvelope(getEnvelope(), getCrs());
399            }
400    
401  }  }

Legend:
Removed from v.256  
changed lines
  Added in v.1231

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26