1 |
mojays |
2 |
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 |
alfonx |
221 |
import org.geotools.feature.FeatureCollection; |
19 |
mojays |
2 |
import org.geotools.styling.Style; |
20 |
|
|
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
21 |
|
|
|
22 |
alfonx |
221 |
import schmitzm.geotools.feature.FeatureOperationTreeFilter; |
23 |
mojays |
2 |
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ü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 |
alfonx |
169 |
} catch (Exception e) { |
82 |
|
|
LOGGER.warn("Reading SLD failed: " + sldFile, e); |
83 |
mojays |
2 |
} |
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 |
alfonx |
208 |
public FeatureSource getGeoObject() { |
127 |
mojays |
2 |
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 |
alfonx |
40 |
/** |
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 |
alfonx |
169 |
* // public boolean isHideInLegend() { // return false; // } |
171 |
alfonx |
40 |
*/ |
172 |
mojays |
2 |
|
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 |
alfonx |
93 |
/** |
197 |
|
|
* |
198 |
|
|
*/ |
199 |
mojays |
2 |
public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() { |
200 |
|
|
if (map == null) { |
201 |
alfonx |
169 |
|
202 |
mojays |
2 |
map = new HashMap<Integer, AttributeMetaData>(); |
203 |
alfonx |
169 |
|
204 |
mojays |
2 |
// 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 |
alfonx |
169 |
|
208 |
|
|
AttributeMetaData attMetaData = new AttributeMetaData(i, att |
209 |
|
|
.getLocalName()); |
210 |
mojays |
2 |
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 |
alfonx |
169 |
* <code>null</code> if there didn't exist a {@link File}. (It could |
219 |
|
|
* be a WFS or a PostGIS |
220 |
mojays |
2 |
* |
221 |
alfonx |
169 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
222 |
|
|
* Krüger</a> |
223 |
mojays |
2 |
*/ |
224 |
|
|
public File getSldFile() { |
225 |
|
|
return sldFile; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
public void setSldFile(File sldFile) { |
229 |
|
|
this.sldFile = sldFile; |
230 |
|
|
} |
231 |
|
|
|
232 |
alfonx |
221 |
/** |
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 |
mojays |
2 |
} |