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