/[schmitzm]/trunk/src/skrueger/geotools/StyledFS.java
ViewVC logotype

Contents of /trunk/src/skrueger/geotools/StyledFS.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 221 - (show annotations)
Tue Jul 14 14:40:52 2009 UTC (15 years, 7 months ago) by alfonx
File size: 6239 byte(s)
* Neues Interface StyledFeatures<T> eingefüht. Es hat getFeatureCollection und getFeatureSource und überwindet somit den Dualismus zwischen FeatureSource und FeatureCollection. 
Ein paar mehr Infos: http://wald.intevation.org/tracker/index.php?func=detail&aid=1061&group_id=37&atid=295

* Noch ein bisschen Feinschliff in den StyleCharts
1 package skrueger.geotools;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.net.URL;
7 import java.util.Date;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Random;
11
12 import javax.swing.ImageIcon;
13 import javax.swing.JPanel;
14
15 import org.apache.log4j.Logger;
16 import org.geotools.data.FeatureSource;
17 import org.geotools.feature.AttributeType;
18 import org.geotools.feature.FeatureCollection;
19 import org.geotools.styling.Style;
20 import org.opengis.referencing.crs.CoordinateReferenceSystem;
21
22 import schmitzm.geotools.feature.FeatureOperationTreeFilter;
23 import schmitzm.geotools.styling.StylingUtil;
24 import skrueger.AttributeMetaData;
25 import skrueger.i8n.Translation;
26
27 import com.vividsolutions.jts.geom.Envelope;
28
29 /**
30 * This class enables a non Atlas context to use the Atlas LayerPanel
31 * {@link JPanel} as a {@link MapContextManagerInterface}
32 *
33 * @author <a href="mailto:[email protected]">Stefan Alfons Kr&uuml;ger</a>
34 */
35 public class StyledFS implements StyledFeatureSourceInterface {
36 private static final Logger LOGGER = Logger.getLogger(StyledFS.class);
37
38 private final FeatureSource fs;
39
40 /**
41 * A unique ID which identifies the Layer in the Atlas. It's more important
42 * than it should be ;-)
43 */
44 private String id;
45
46 private Style style;
47
48 private Translation title;
49
50 private Translation desc;
51
52 private File sldFile;
53
54 private HashMap<Integer, AttributeMetaData> map;
55
56 /**
57 * This class enables a non Atlas context to use the Atlas LayerPanel
58 * {@link JPanel} as a {@link MapContextManagerInterface}
59 *
60 * @param fs
61 * {@link FeatureSource} that is beeing styled.
62 *
63 * @param sldFile
64 * may be <code>null</code>. Otherwise the SLD {@link File} to
65 * import and associate with this {@link StyledFS}
66 */
67 public StyledFS(FeatureSource fs, File sldFile) {
68
69 this.fs = fs;
70 id = StyledFS.class.getSimpleName()
71 + new Random(new Date().getTime()).nextInt(10000000);
72
73 this.sldFile = sldFile;
74
75 if ((sldFile != null) && (sldFile.exists())) {
76 try {
77 style = StylingUtil.loadSLD(sldFile)[0];
78 } catch (FileNotFoundException e) {
79 LOGGER
80 .debug("The SLD file passed was empty. Leaving the Style untouched. (We are in the constructor.. so its null");
81 } catch (Exception e) {
82 LOGGER.warn("Reading SLD failed: " + sldFile, e);
83 }
84 }
85 title = new Translation();
86 title.fromOneLine(sldFile.getName());
87
88 desc = new Translation();
89 desc.fromOneLine(sldFile.getAbsolutePath());
90 }
91
92 public void dispose() {
93 }
94
95 /**
96 * Returnes human readable {@link String} of the CRS natively used by this
97 * {@link DpLayer}
98 *
99 * If crs == null, it will call {@link #getGeoObject()}
100 *
101 */
102 public String getCRSString() {
103 if (getCrs() == null)
104 return "CRS?";
105
106 return getCrs().getName().getCode();
107 }
108
109 public CoordinateReferenceSystem getCrs() {
110 return fs.getSchema().getDefaultGeometry().getCoordinateSystem();
111 }
112
113 public Translation getDesc() {
114 return desc;
115 }
116
117 public Envelope getEnvelope() {
118 try {
119 return fs.getBounds();
120 } catch (IOException e) {
121 e.printStackTrace();
122 return null;
123 }
124 }
125
126 public FeatureSource getGeoObject() {
127 return fs;
128 }
129
130 public String getId() {
131 return id;
132 }
133
134 public ImageIcon getImageIcon() {
135 return null;
136 }
137
138 public URL getInfoURL() {
139 return null;
140 }
141
142 public Translation getKeywords() {
143 return null;
144 }
145
146 public Style getStyle() {
147 return style;
148 }
149
150 public Translation getTitle() {
151 return title;
152 }
153
154 public boolean isDisposed() {
155 return false;
156 }
157
158 /**
159 * If true, this layer will not be shown in the legend. Default = false
160 */
161 /**
162 *
163 * Killed by SK: 6. April 09: Ein Layer soll nicht generell auf
164 * verstecken/nicht verstecken gestellt werden können. Das sind
165 * Eigenschaften der Karte/MapContext, ebenso wie die Reihenfolge der Layer.
166 * Im Atlas verwaltet deshalb nun die Klasse skrueger.atlas.Map welche Layer
167 * nicht in der Legende auftauchen sollen. Meines Wissens hat keiner bisher
168 * die Funktion genutzt.
169 *
170 * // public boolean isHideInLegend() { // return false; // }
171 */
172
173 public void setDesc(Translation dec) {
174 this.desc = dec;
175 }
176
177 public void setImageIcon(ImageIcon icon) {
178 }
179
180 public void setKeywords(Translation keywords) {
181 }
182
183 public void setStyle(Style style) {
184 this.style = style;
185
186 }
187
188 public void setTitle(Translation title) {
189 this.title = title;
190
191 }
192
193 public void uncache() {
194 }
195
196 /**
197 *
198 */
199 public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {
200 if (map == null) {
201
202 map = new HashMap<Integer, AttributeMetaData>();
203
204 // Leaving out the first one, it will be the_geom
205 for (int i = 1; i < fs.getSchema().getAttributeCount(); i++) {
206 AttributeType att = fs.getSchema().getAttributeType(i);
207
208 AttributeMetaData attMetaData = new AttributeMetaData(i, att
209 .getLocalName());
210 map.put(i, attMetaData);
211 }
212 }
213 return map;
214 }
215
216 /**
217 * @return The {@link File} where the SLD was loaded from or
218 * <code>null</code> if there didn't exist a {@link File}. (It could
219 * be a WFS or a PostGIS
220 *
221 * @author <a href="mailto:[email protected]">Stefan Alfons
222 * Kr&uuml;ger</a>
223 */
224 public File getSldFile() {
225 return sldFile;
226 }
227
228 public void setSldFile(File sldFile) {
229 this.sldFile = sldFile;
230 }
231
232 /**
233 * Returns the features of the {@link FeatureSource}.
234 *
235 * @see {@link StyledFeatureInterface}
236 */
237 @Override
238 public FeatureCollection getFeatureCollection() {
239 FeatureCollection features;
240 try {
241 features = getGeoObject().getFeatures();
242 } catch (IOException e) {
243 throw new RuntimeException(
244 "Error getting the features of the FeatureSource");
245 }
246 return features;
247 }
248
249 /**
250 * Same as {@link #getGeoObject()} method, but complies to the
251 * {@link StyledFeatureInterface}
252 *
253 * @see {@link StyledFeatureInterface}
254 */
255 @Override
256 public FeatureSource getFeatureSource() {
257 return getGeoObject();
258 }
259
260 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26