/[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 1203 by alfonx, Tue Nov 2 22:53:55 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.i8n.Translation;  import skrueger.i8n.Translation;
57    
58  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
# Line 61  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<SimpleFeatureType, SimpleFeature> 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 82  public class StyledFS implements StyledF Line 87  public class StyledFS implements StyledF
87    
88          private File sldFile;          private File sldFile;
89    
90          private AttributeMetadataMap map;          /** A map of simple attribute names to their meta-data **/
91            private AttributeMetadataMap<AttributeMetadataImpl> map;
92    
93          private Filter filter = Filter.INCLUDE;          private Filter filter = Filter.INCLUDE;
94    
# Line 96  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<SimpleFeatureType, SimpleFeature> fs,          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
110                          File sldFile) {                          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                    // datei existiert, dann lesen
124                  if (sldFile != null && sldFile.exists()) {                  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                  }                  }
132    
133                  title = new Translation();                  title = new Translation();
134                  desc = new Translation();                  desc = new Translation();
135    
# Line 127  public class StyledFS implements StyledF Line 141  public class StyledFS implements StyledF
141          }          }
142    
143          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
144                  this(fs, null);                  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() {
# Line 148  public class StyledFS implements StyledF Line 167  public class StyledFS implements StyledF
167          }          }
168    
169          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
170                  CoordinateReferenceSystem crs = fs.getSchema().getCoordinateReferenceSystem();                  if (crs == null) {
171                  if (fs.getSchema().getCoordinateReferenceSystem() == null) {                          crs = fs.getSchema().getCoordinateReferenceSystem();
172                          LOGGER.warn("Could not determine the CRS of "+getTitle()+". Using default "+GeoImportUtil.getDefaultCRS());                          if (crs == null) {
173                          crs = GeoImportUtil.getDefaultCRS();  
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;                  return crs;
186          }          }
# Line 242  public class StyledFS implements StyledF Line 271  public class StyledFS implements StyledF
271          /**          /**
272           *           *
273           */           */
274          public AttributeMetadataMap getAttributeMetaDataMap() {          public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
275                  if (map == null) {                  if (map == null) {
276    
277                          map = new AttributeMetadataMap();                          map = new AttributeMetadataImplMap();
278    
279                            // // Leaving out the first one, it will be the_geom
280                            // for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {
281                            // AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i);
282                            //
283                            // AttributeMetadataImpl attMetaData = new AttributeMetadataImpl(
284                            // new NameImpl(attDesc
285                            // .getName().getNamespaceURI(), attDesc
286                            // .getName().getLocalPart()), map.getLanguages());
287                            // map.put(attDesc.getName(), attMetaData);
288                            // }
289    
290                          // Leaving out the first one, it will be the_geom                          // Leaving out the first one, it will be the_geom
291                          for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {                          for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {
292                                  AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i);                                  AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i);
293    
294                                  AttributeMetadata attMetaData = new AttributeMetadata(attDesc.getName());                                  // TODO AttributeMetadataAS would be nicer, which would not work
295                                    // with Translations ;-)
296                                    AttributeMetadataImpl attMetaData = new AttributeMetadataImpl(
297                                                    new NameImpl(attDesc.getName().getNamespaceURI(),
298                                                                    attDesc.getName().getLocalPart()),
299                                                    map.getLanguages());
300                                    if (String.class.isAssignableFrom(attDesc.getType()
301                                                    .getBinding())) {
302                                            // For Strings we add the "" as NODATA values
303                                            attMetaData.addNodataValue("");
304                                    }
305                                  map.put(attDesc.getName(), attMetaData);                                  map.put(attDesc.getName(), attMetaData);
306                          }                          }
307                  }                  }
# Line 262  public class StyledFS implements StyledF Line 312  public class StyledFS implements StyledF
312           * @return The {@link File} where the SLD was loaded from or           * @return The {@link File} where the SLD was loaded from or
313           *         <code>null</code> if there didn't exist a {@link File}.           *         <code>null</code> if there didn't exist a {@link File}.
314           *           *
315           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
316           */           */
317          public File getSldFile() {          public File getSldFile() {
318                  return sldFile;                  return sldFile;
319          }          }
320    
321            /**
322             * Associates this .sld with the {@link FeatureSource}, but does not
323             * automatically load the file. It must not even exist.
324             *
325             * @param sldFile
326             */
327          public void setSldFile(File sldFile) {          public void setSldFile(File sldFile) {
328                  this.sldFile = sldFile;                  this.sldFile = sldFile;
329          }          }
# Line 303  public class StyledFS implements StyledF Line 358  public class StyledFS implements StyledF
358    
359          @Override          @Override
360          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {
361                  final FeatureCollection<SimpleFeatureType, SimpleFeature> fc = getFeatureCollection();                  // final FeatureCollection<SimpleFeatureType, SimpleFeature> fc =
362                  if (filter == Filter.EXCLUDE)                  // getFeatureCollection();
363                          return new EmptyFeatureCollection(fc.getSchema());                  // if (filter == Filter.EXCLUDE)
364                  if (filter == Filter.INCLUDE)                  // return new EmptyFeatureCollection(fc.getSchema());
365                          return fc;                  // if (filter == Filter.INCLUDE)
366                  return fc.subCollection(filter);                  // return fc;
367                    // return fc.subCollection(filter);
368    
369                    try {
370                            if (filter != Filter.INCLUDE)
371                                    return getFeatureSource().getFeatures(filter);
372                            else
373                                    return getFeatureSource().getFeatures();
374                    } catch (IOException e) {
375                            throw new RuntimeException(e);
376                    }
377          }          }
378    
379          @Override          @Override
# Line 326  public class StyledFS implements StyledF Line 391  public class StyledFS implements StyledF
391                  return getGeoObject().getSchema();                  return getGeoObject().getSchema();
392          }          }
393    
394            /**
395             * Tries to load a style from the file denoted in {@link #getSldFile()}. If
396             * the file doesn't exits, return <code>null</code>;
397             *
398             * @return <code>true</code> is style was loaded
399             */
400            public boolean loadStyle() {
401                    if (getSldFile() == null)
402                            return false;
403    
404                    try {
405                            Style[] loadSLD = StylingUtil.loadSLD(getSldFile());
406                            setStyle(loadSLD[0]);
407                            return true;
408                    } catch (Exception e) {
409                            return false;
410                    }
411    
412            }
413    
414            public void setTitle(String title) {
415                    setTitle(new Translation(title));
416            }
417    
418            public void setDesc(String desc) {
419                    setDesc(new Translation(desc));
420            }
421    
422            public void setCRS(CoordinateReferenceSystem crs2) {
423                    crs = crs2;
424            }
425    
426            @Override
427            public ReferencedEnvelope getReferencedEnvelope() {
428                    return new ReferencedEnvelope(getEnvelope(), getCrs());
429            }
430    
431  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26