/[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/2.0-RC2/src/skrueger/geotools/StyledFS.java revision 621 by alfonx, Thu Jan 28 10:06:05 2010 UTC trunk/src/skrueger/geotools/StyledFS.java 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;
# Line 38  import java.util.Random; Line 37  import java.util.Random;
37    
38  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
39  import javax.swing.JPanel;  import javax.swing.JPanel;
 import javax.xml.transform.TransformerException;  
40    
41  import org.apache.log4j.Logger;  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.geometry.jts.ReferencedEnvelope;
45  import org.geotools.styling.Style;  import org.geotools.styling.Style;
 import org.jfree.util.AttributedStringUtilities;  
46  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
47  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
 import org.opengis.feature.type.AttributeDescriptor;  
48  import org.opengis.filter.Filter;  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;  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 62  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   *         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<SimpleFeatureType, SimpleFeature> fs;          private final FeatureSource<SimpleFeatureType, SimpleFeature> fs;
70            
71          /** Caching the CRS of the layer **/          /** Caching the CRS of the layer **/
72          CoordinateReferenceSystem crs = null;          CoordinateReferenceSystem crs = null;
73    
# Line 78  public class StyledFS implements StyledF Line 75  public class StyledFS implements StyledF
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 88  public class StyledFS implements StyledF Line 85  public class StyledFS implements StyledF
85    
86          private File sldFile;          private File sldFile;
87    
88          private AttributeMetadataMap map;          /** A map of simple attribute names to their meta-data **/
89            private AttributeMetadataMap<AttributeMetadataImpl> attMap;
90    
91          private Filter filter = Filter.INCLUDE;          private Filter filter = Filter.INCLUDE;
92    
# Line 102  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<SimpleFeatureType, SimpleFeature> fs,          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
108                          File sldFile) {                          File sldFile, String id) {
109    
110                  this.fs = fs;                  this.fs = fs;
111    
112                  id = StyledFS.class.getSimpleName()                  if (id == null) {
113                                  + new Random(new Date().getTime()).nextInt(10000000);                          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    
# Line 134  public class StyledFS implements StyledF Line 139  public class StyledFS implements StyledF
139          }          }
140    
141          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {          public StyledFS(FeatureSource<SimpleFeatureType, SimpleFeature> fs) {
142                  this(fs, null);                  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() {
# Line 156  public class StyledFS implements StyledF Line 166  public class StyledFS implements StyledF
166    
167          public CoordinateReferenceSystem getCrs() {          public CoordinateReferenceSystem getCrs() {
168                  if (crs == null) {                  if (crs == null) {
169                          crs = fs.getSchema()                          crs = fs.getSchema().getCoordinateReferenceSystem();
170                                          .getCoordinateReferenceSystem();                          if (crs == null) {
171                          if (fs.getSchema().getCoordinateReferenceSystem() == null) {  
172                                  LOGGER.warn("Could not determine the CRS of " + getTitle()                                  crs = fs.getSchema().getGeometryDescriptor()
173                                                  + ". Using default " + GeoImportUtil.getDefaultCRS());                                                  .getCoordinateReferenceSystem();
174                                  crs = GeoImportUtil.getDefaultCRS();  
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;                  return crs;
# Line 253  public class StyledFS implements StyledF Line 269  public class StyledFS implements StyledF
269          /**          /**
270           *           *
271           */           */
272          public AttributeMetadataMap getAttributeMetaDataMap() {          @Override
273                  if (map == null) {          public AttributeMetadataMap<AttributeMetadataImpl> getAttributeMetaDataMap() {
274                    if (attMap == null) {
275                          map = new AttributeMetadataMap(new String[] { Translation                          attMap = StyledLayerUtil
276                                          .getActiveLang() });                                          .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);  
   
                                 AttributeMetadata attMetaData = new AttributeMetadata(attDesc  
                                                 .getName(), map.getLanguages());  
                                 map.put(attDesc.getName(), 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 325  public class StyledFS implements StyledF Line 337  public class StyledFS implements StyledF
337                  // return fc.subCollection(filter);                  // return fc.subCollection(filter);
338    
339                  try {                  try {
340                          return getFeatureSource().getFeatures(filter);                          if (filter != Filter.INCLUDE)
341                                    return getFeatureSource().getFeatures(filter);
342                            else
343                                    return getFeatureSource().getFeatures();
344                  } catch (IOException e) {                  } catch (IOException e) {
345                          throw new RuntimeException(e);                          throw new RuntimeException(e);
346                  }                  }
# Line 346  public class StyledFS implements StyledF Line 361  public class StyledFS implements StyledF
361                  return getGeoObject().getSchema();                  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.621  
changed lines
  Added in v.1231

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26