/[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 336 by alfonx, Thu Aug 27 09:10:10 2009 UTC trunk/src/skrueger/geotools/StyledFS.java revision 863 by alfonx, Sun May 23 13:42:13 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    
# Line 34  import java.io.FileNotFoundException; Line 34  import java.io.FileNotFoundException;
34  import java.io.IOException;  import java.io.IOException;
35  import java.net.URL;  import java.net.URL;
36  import java.util.Date;  import java.util.Date;
 import java.util.HashMap;  
 import java.util.Map;  
37  import java.util.Random;  import java.util.Random;
38    
39  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
# Line 44  import javax.swing.JPanel; Line 42  import javax.swing.JPanel;
42  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
43  import org.geotools.data.FeatureSource;  import org.geotools.data.FeatureSource;
44  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
45    import org.geotools.feature.NameImpl;
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;
49  import org.opengis.feature.type.AttributeDescriptor;  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 61  import com.vividsolutions.jts.geom.Envel Line 63  import com.vividsolutions.jts.geom.Envel
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 Kr&uuml;ger</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 ;-)
# Line 81  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> map;
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 94  public class StyledFS implements StyledF Line 104  public class StyledFS implements StyledF
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          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs, File sldFile) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
108                            File sldFile) {
109    
110                  this.fs = fs;                  this.fs = fs;
111    
112                  id = StyledFS.class.getSimpleName()                  id = StyledFS.class.getSimpleName()
113                                  + new Random(new Date().getTime()).nextInt(10000000);                                  + new Random(new Date().getTime()).nextInt(10000000);
114    
115                  this.sldFile = sldFile;                  this.sldFile = sldFile;
116    
117                    // datei existiert, dann lesen
118                  if (sldFile != null && sldFile.exists()) {                  if (sldFile != null && sldFile.exists()) {
119                          try {                          try {
120                                  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");  
121                          } catch (Exception e) {                          } catch (Exception e) {
122                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);
123                                    style = null;
124                          }                          }
125                  }                  }
                 title = new Translation();  
                 title.fromOneLine(sldFile.getName());  
126    
127                    title = new Translation();
128                  desc = new Translation();                  desc = new Translation();
129                  desc.fromOneLine(sldFile.getAbsolutePath());  
130                    if (sldFile != null) {
131                            title.fromOneLine(sldFile.getName());
132                            desc.fromOneLine(sldFile.getAbsolutePath());
133                    }
134    
135            }
136    
137            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
138                    this(fs, null);
139          }          }
140    
141          public void dispose() {          public void dispose() {
# Line 137  public class StyledFS implements StyledF Line 156  public class StyledFS implements StyledF
156          }          }
157    
158          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
159                  return fs.getSchema().getCoordinateReferenceSystem();                  if (crs == null) {
160                            crs = fs.getSchema().getCoordinateReferenceSystem();
161                            if (fs.getSchema().getCoordinateReferenceSystem() == null) {
162                                    LOGGER.warn("Could not determine the CRS of " + getTitle()
163                                                    + ". Using default " + GeoImportUtil.getDefaultCRS());
164                                    crs = GeoImportUtil.getDefaultCRS();
165                            }
166                    }
167                    return crs;
168          }          }
169    
170          public Translation getDesc() {          public Translation getDesc() {
# Line 226  public class StyledFS implements StyledF Line 253  public class StyledFS implements StyledF
253          /**          /**
254           *           *
255           */           */
256          public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {          public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
257                  if (map == null) {                  if (map == null) {
258    
259                          map = new HashMap<Integer, AttributeMetaData>();                          map = new AttributeMetadataImplMap();
260    
261                            // // Leaving out the first one, it will be the_geom
262                            // for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {
263                            // AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i);
264                            //
265                            // AttributeMetadataImpl attMetaData = new AttributeMetadataImpl(
266                            // new NameImpl(attDesc
267                            // .getName().getNamespaceURI(), attDesc
268                            // .getName().getLocalPart()), map.getLanguages());
269                            // map.put(attDesc.getName(), attMetaData);
270                            // }
271    
272                          // Leaving out the first one, it will be the_geom                          // Leaving out the first one, it will be the_geom
273                          for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {                          for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {
274                                  AttributeDescriptor att = fs.getSchema().getDescriptor(i);                                  AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i);
275    
276                                  AttributeMetaData attMetaData = new AttributeMetaData(i, att                                  // TODO AttributeMetadataAS would be nicer, which would not work
277                                                  .getLocalName());                                  // with Translations ;-)
278                                  map.put(i, attMetaData);                                  AttributeMetadataImpl attMetaData = new AttributeMetadataImpl(
279                                                    new NameImpl(attDesc.getName().getNamespaceURI(),
280                                                                    attDesc.getName().getLocalPart()), map
281                                                                    .getLanguages());
282                                    if (String.class.isAssignableFrom(attDesc.getType()
283                                                    .getBinding())) {
284                                            // For Strings we add the "" as NODATA values
285                                            attMetaData.addNodataValue("");
286                                    }
287                                    map.put(attDesc.getName(), attMetaData);
288                          }                          }
289                  }                  }
290                  return map;                  return map;
# Line 245  public class StyledFS implements StyledF Line 292  public class StyledFS implements StyledF
292    
293          /**          /**
294           * @return The {@link File} where the SLD was loaded from or           * @return The {@link File} where the SLD was loaded from or
295           *         <code>null</code> if there didn't exist a {@link File}.           *         <code>null</code> if there didn't exist a {@link File}.
296           *           *
297           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
298           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
# Line 264  public class StyledFS implements StyledF Line 311  public class StyledFS implements StyledF
311           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
312           */           */
313          @Override          @Override
314          public FeatureCollection getFeatureCollection() {          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
315                  FeatureCollection features;                  FeatureCollection<SimpleFeatureType, SimpleFeature> features;
316                  try {                  try {
317                          features = getGeoObject().getFeatures();                          features = getGeoObject().getFeatures();
318                  } catch (IOException e) {                  } catch (IOException e) {
# Line 282  public class StyledFS implements StyledF Line 329  public class StyledFS implements StyledF
329           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
330           */           */
331          @Override          @Override
332          public FeatureSource getFeatureSource() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
333                  return getGeoObject();                  return getGeoObject();
334          }          }
335    
336            @Override
337            public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {
338                    // final FeatureCollection<SimpleFeatureType, SimpleFeature> fc =
339                    // getFeatureCollection();
340                    // if (filter == Filter.EXCLUDE)
341                    // return new EmptyFeatureCollection(fc.getSchema());
342                    // if (filter == Filter.INCLUDE)
343                    // return fc;
344                    // return fc.subCollection(filter);
345    
346                    try {
347                            return getFeatureSource().getFeatures(filter);
348                    } catch (IOException e) {
349                            throw new RuntimeException(e);
350                    }
351            }
352    
353            @Override
354            public Filter getFilter() {
355                    return filter;
356            }
357    
358            @Override
359            public void setFilter(Filter filter) {
360                    this.filter = filter;
361            }
362    
363            @Override
364            public SimpleFeatureType getSchema() {
365                    return getGeoObject().getSchema();
366            }
367    
368            /**
369             * Tries to load a style from the file denoted in {@link #getSldFile()}. If
370             * the file doesn't exits, return <code>null</code>;
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  }  }

Legend:
Removed from v.336  
changed lines
  Added in v.863

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26