/[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 332 by alfonx, Wed Aug 26 17:15:49 2009 UTC trunk/src/skrueger/geotools/StyledFS.java revision 685 by alfonx, Wed Feb 10 15:04:02 2010 UTC
# 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;
40  import javax.swing.JPanel;  import javax.swing.JPanel;
41    import javax.xml.transform.TransformerException;
42    
43  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
44  import org.geotools.data.FeatureSource;  import org.geotools.data.FeatureSource;
45  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
46  import org.geotools.styling.Style;  import org.geotools.styling.Style;
47    import org.jfree.util.AttributedStringUtilities;
48    import org.opengis.feature.simple.SimpleFeature;
49    import org.opengis.feature.simple.SimpleFeatureType;
50  import org.opengis.feature.type.AttributeDescriptor;  import org.opengis.feature.type.AttributeDescriptor;
51    import org.opengis.filter.Filter;
52  import org.opengis.referencing.crs.CoordinateReferenceSystem;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
53    
54    import schmitzm.geotools.io.GeoImportUtil;
55  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
56  import skrueger.AttributeMetaData;  import skrueger.AttributeMetadata;
57  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
58    
59  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
# Line 59  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 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
# 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;          private AttributeMetadataMap map;
92    
93            private Filter filter = Filter.INCLUDE;
94    
95          /**          /**
96           * 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 92  public class StyledFS implements StyledF Line 103  public class StyledFS implements StyledF
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          public StyledFS(FeatureSource fs, File sldFile) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
107                            File sldFile) {
108    
109                  this.fs = fs;                  this.fs = fs;
110    
111                  id = StyledFS.class.getSimpleName()                  id = StyledFS.class.getSimpleName()
112                                  + new Random(new Date().getTime()).nextInt(10000000);                                  + new Random(new Date().getTime()).nextInt(10000000);
113    
114                  this.sldFile = sldFile;                  this.sldFile = sldFile;
115    
116                  if ((sldFile != null) && (sldFile.exists())) {                  // datei existiert, dann lesen
117                    if (sldFile != null && sldFile.exists()) {
118                          try {                          try {
119                                  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");  
120                          } catch (Exception e) {                          } catch (Exception e) {
121                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);                                  LOGGER.warn("Reading SLD failed: " + sldFile, e);
122                                    style = null;
123                          }                          }
124                  }                  }
                 title = new Translation();  
                 title.fromOneLine(sldFile.getName());  
125    
126                    title = new Translation();
127                  desc = new Translation();                  desc = new Translation();
128                  desc.fromOneLine(sldFile.getAbsolutePath());  
129                    if (sldFile != null) {
130                            title.fromOneLine(sldFile.getName());
131                            desc.fromOneLine(sldFile.getAbsolutePath());
132                    }
133    
134            }
135    
136            public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
137                    this(fs, null);
138          }          }
139    
140          public void dispose() {          public void dispose() {
141          }          }
142    
143          /**          /**
144           * Returnes human readable {@link String} of the CRS natively used by this           * Returns human readable {@link String} of the CRS natively used by this
145           * {@link DpLayer}           * {@link DpLayer}
146           *           *
147           * If crs == null, it will call {@link #getGeoObject()}           * If CRS == null, it will call {@link #getGeoObject()}
148           *           *
149           */           */
150          public String getCRSString() {          public String getCRSString() {
# Line 135  public class StyledFS implements StyledF Line 155  public class StyledFS implements StyledF
155          }          }
156    
157          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
158                  return fs.getSchema().getDefaultGeometry().getCoordinateSystem();                  if (crs == null) {
159                            crs = fs.getSchema()
160                                            .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 151  public class StyledFS implements StyledF Line 180  public class StyledFS implements StyledF
180                  }                  }
181          }          }
182    
183          public FeatureSource getGeoObject() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getGeoObject() {
184                  return fs;                  return fs;
185          }          }
186    
# Line 224  public class StyledFS implements StyledF Line 253  public class StyledFS implements StyledF
253          /**          /**
254           *           *
255           */           */
256          public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {          public AttributeMetadataMap getAttributeMetaDataMap() {
257                  if (map == null) {                  if (map == null) {
258    
259                          map = new HashMap<Integer, AttributeMetaData>();                          map = new AttributeMetadataMap(new String[] { Translation
260                                            .getActiveLang() });
261    
262                          // Leaving out the first one, it will be the_geom                          // Leaving out the first one, it will be the_geom
263                          for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {                          for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {
264                                  AttributeDescriptor att = fs.getSchema().getAttributeType(i);                                  AttributeDescriptor attDesc = fs.getSchema().getDescriptor(i);
265    
266                                  AttributeMetaData attMetaData = new AttributeMetaData(i, att                                  AttributeMetadata attMetaData = new AttributeMetadata(attDesc
267                                                  .getLocalName());                                                  .getName(), map.getLanguages());
268                                  map.put(i, attMetaData);                                  map.put(attDesc.getName(), attMetaData);
269                          }                          }
270                  }                  }
271                  return map;                  return map;
# Line 243  public class StyledFS implements StyledF Line 273  public class StyledFS implements StyledF
273    
274          /**          /**
275           * @return The {@link File} where the SLD was loaded from or           * @return The {@link File} where the SLD was loaded from or
276           *         <code>null</code> if there didn't exist a {@link File}.           *         <code>null</code> if there didn't exist a {@link File}.
277           *           *
278           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
279           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
# Line 251  public class StyledFS implements StyledF Line 281  public class StyledFS implements StyledF
281          public File getSldFile() {          public File getSldFile() {
282                  return sldFile;                  return sldFile;
283          }          }
284            
285          public void setSldFile(File sldFile) {          public void setSldFile(File sldFile) {
286                  this.sldFile = sldFile;                  this.sldFile = sldFile;
287          }          }
# Line 262  public class StyledFS implements StyledF Line 292  public class StyledFS implements StyledF
292           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
293           */           */
294          @Override          @Override
295          public FeatureCollection getFeatureCollection() {          public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollection() {
296                  FeatureCollection features;                  FeatureCollection<SimpleFeatureType, SimpleFeature> features;
297                  try {                  try {
298                          features = getGeoObject().getFeatures();                          features = getGeoObject().getFeatures();
299                  } catch (IOException e) {                  } catch (IOException e) {
# Line 280  public class StyledFS implements StyledF Line 310  public class StyledFS implements StyledF
310           * @see {@link StyledFeaturesInterface}           * @see {@link StyledFeaturesInterface}
311           */           */
312          @Override          @Override
313          public FeatureSource getFeatureSource() {          public FeatureSource<SimpleFeatureType, SimpleFeature> getFeatureSource() {
314                  return getGeoObject();                  return getGeoObject();
315          }          }
316    
317            @Override
318            public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatureCollectionFiltered() {
319                    // final FeatureCollection<SimpleFeatureType, SimpleFeature> fc =
320                    // getFeatureCollection();
321                    // if (filter == Filter.EXCLUDE)
322                    // return new EmptyFeatureCollection(fc.getSchema());
323                    // if (filter == Filter.INCLUDE)
324                    // return fc;
325                    // return fc.subCollection(filter);
326    
327                    try {
328                            return getFeatureSource().getFeatures(filter);
329                    } catch (IOException e) {
330                            throw new RuntimeException(e);
331                    }
332            }
333    
334            @Override
335            public Filter getFilter() {
336                    return filter;
337            }
338    
339            @Override
340            public void setFilter(Filter filter) {
341                    this.filter = filter;
342            }
343    
344            @Override
345            public SimpleFeatureType getSchema() {
346                    return getGeoObject().getSchema();
347            }
348    
349  }  }

Legend:
Removed from v.332  
changed lines
  Added in v.685

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26