/[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 862 by alfonx, Sat May 22 01:24:46 2010 UTC revision 1228 by alfonx, Wed Nov 3 20:44:16 2010 UTC
# Line 42  import org.apache.log4j.Logger; Line 42  import org.apache.log4j.Logger;
42  import org.geotools.data.FeatureSource;  import org.geotools.data.FeatureSource;
43  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
44  import org.geotools.feature.NameImpl;  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 53  import org.opengis.referencing.crs.Coord
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.AttributeMetadataImpl;  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 60  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   *         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 **/          /** Caching the CRS of the layer **/
75          CoordinateReferenceSystem crs = null;          CoordinateReferenceSystem crs = null;
76    
# Line 76  public class StyledFS implements StyledF Line 78  public class StyledFS implements StyledF
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 86  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 100  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    
115                  id = StyledFS.class.getSimpleName()                  if (id == null) {
116                                  + new Random(new Date().getTime()).nextInt(10000000);                          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    
# Line 132  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 154  public class StyledFS implements StyledF Line 169  public class StyledFS implements StyledF
169    
170          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
171                  if (crs == null) {                  if (crs == null) {
172                          crs = fs.getSchema()                          crs = fs.getSchema().getCoordinateReferenceSystem();
173                                          .getCoordinateReferenceSystem();                          if (crs == null) {
174                          if (fs.getSchema().getCoordinateReferenceSystem() == null) {  
175                                  LOGGER.warn("Could not determine the CRS of " + getTitle()                                  crs = fs.getSchema().getGeometryDescriptor()
176                                                  + ". Using default " + GeoImportUtil.getDefaultCRS());                                                  .getCoordinateReferenceSystem();
177                                  crs = GeoImportUtil.getDefaultCRS();  
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;
# Line 251  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 AttributeMetadataImplMap();                          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);  
 //  
 //                              AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( new NameImpl(attDesc  
 //                                              .getName().getNamespaceURI(), attDesc  
 //                                              .getName().getLocalPart()), map.getLanguages());  
 //                              map.put(attDesc.getName(), attMetaData);  
 //                      }  
                           
                         // 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);  
   
                                 // TODO AttributeMetadataAS would be nicer, which would not work with Translations ;-)  
                                 AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( new NameImpl(attDesc  
                                                 .getName().getNamespaceURI(), attDesc  
                                                 .getName().getLocalPart()), map.getLanguages());  
                                 if (String.class.isAssignableFrom( attDesc.getType().getBinding() )){  
                                         // For Strings we add the "" as NODATA values  
                                         attMetaData.addNodataValue("");  
                                 }  
                                 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 338  public class StyledFS implements StyledF Line 340  public class StyledFS implements StyledF
340                  // return fc.subCollection(filter);                  // return fc.subCollection(filter);
341    
342                  try {                  try {
343                          return getFeatureSource().getFeatures(filter);                          if (filter != Filter.INCLUDE)
344                                    return getFeatureSource().getFeatures(filter);
345                            else
346                                    return getFeatureSource().getFeatures();
347                  } catch (IOException e) {                  } catch (IOException e) {
348                          throw new RuntimeException(e);                          throw new RuntimeException(e);
349                  }                  }
# Line 359  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.862  
changed lines
  Added in v.1228

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26