svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / using-sld-model / org.gvsig.sldsupport / org.gvsig.sldsupport.basic / src / main / java / org / gvsig / sldsupport / basic / sld1_0_0 / BasicSLDReaderWriter.java @ 40758
History | View | Annotate | Download (9.85 KB)
1 |
package org.gvsig.sldsupport.basic.sld1_0_0; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.OutputStream; |
5 |
import java.util.ArrayList; |
6 |
import java.util.List; |
7 |
|
8 |
import org.gvsig.fmap.dal.exception.DataException; |
9 |
import org.gvsig.fmap.dal.exception.ReadException; |
10 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
11 |
import org.gvsig.fmap.dal.feature.FeatureType; |
12 |
import org.gvsig.fmap.geom.Geometry; |
13 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
14 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
15 |
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer; |
16 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
17 |
import org.gvsig.fmap.mapcontext.rendering.legend.ISingleSymbolLegend; |
18 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend; |
19 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend; |
20 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol; |
21 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
22 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
23 |
import org.gvsig.sldsupport.SLDSupportLocator; |
24 |
import org.gvsig.sldsupport.SLDSupportManager; |
25 |
import org.gvsig.sldsupport.exception.SLDReadException; |
26 |
import org.gvsig.sldsupport.exception.SLDWriteException; |
27 |
import org.gvsig.sldsupport.exception.UnsupportedSLDException; |
28 |
import org.gvsig.sldsupport.exception.UnsupportedSLDVersionException; |
29 |
import org.gvsig.sldsupport.filterencoding.Filter; |
30 |
import org.gvsig.sldsupport.io.SLDLegendReader; |
31 |
import org.gvsig.sldsupport.io.SLDLegendWriter; |
32 |
import org.gvsig.sldsupport.layer.ISLDLayer; |
33 |
import org.gvsig.sldsupport.rule.SLDRule; |
34 |
import org.gvsig.sldsupport.style.SLDFeatureTypeStyle; |
35 |
import org.gvsig.sldsupport.style.SLDUserStyle; |
36 |
import org.gvsig.sldsupport.symbolizer.ISLDSymbolizer; |
37 |
import org.gvsig.sldsupport.symbolizer.SLDLineSymbolizer; |
38 |
import org.gvsig.sldsupport.symbolizer.SLDPointSymbolizer; |
39 |
import org.gvsig.sldsupport.symbolizer.SLDPolygonSymbolizer; |
40 |
import org.gvsig.symbology.SymbologyLocator; |
41 |
import org.gvsig.symbology.SymbologyManager; |
42 |
import org.xmlpull.v1.XmlPullParser; |
43 |
|
44 |
public class BasicSLDReaderWriter implements |
45 |
SLDLegendReader, SLDLegendWriter { |
46 |
|
47 |
private static MapContextManager mcoMan = null; |
48 |
private static SymbologyManager symMan = null; |
49 |
private static SymbolManager syMan = null; |
50 |
|
51 |
private static SLDSupportManager sldMan = |
52 |
SLDSupportLocator.getInstance().getSLDSupportManager(); |
53 |
|
54 |
public BasicSLDReaderWriter() {
|
55 |
} |
56 |
|
57 |
public List<String> getSupportedVersions() { |
58 |
return sldMan.getSupportedVersions();
|
59 |
} |
60 |
|
61 |
public List<Class<? extends ILegend>> getSupportedLegends() { |
62 |
|
63 |
List<Class<? extends ILegend>> resp = new ArrayList<Class<? extends ILegend>>(); |
64 |
|
65 |
/*
|
66 |
* Supported by this reader/writer
|
67 |
*/
|
68 |
resp.add(ISingleSymbolLegend.class); |
69 |
resp.add(IVectorialIntervalLegend.class); |
70 |
resp.add(IVectorialUniqueValueLegend.class); |
71 |
|
72 |
return resp;
|
73 |
} |
74 |
|
75 |
public ISLDLayer getSLDLayer(ILegend leg, String layer_name, String version) |
76 |
throws SLDWriteException {
|
77 |
|
78 |
List<SLDRule> rules = null; |
79 |
|
80 |
if (leg instanceof ISingleSymbolLegend) { |
81 |
|
82 |
ISingleSymbolLegend ssl = (ISingleSymbolLegend) leg; |
83 |
try {
|
84 |
rules = getRules(ssl, version); |
85 |
} catch (UnsupportedSLDVersionException e) {
|
86 |
throw new SLDWriteException(e); |
87 |
} |
88 |
|
89 |
} else {
|
90 |
if (leg instanceof IVectorialUniqueValueLegend) { |
91 |
|
92 |
IVectorialUniqueValueLegend uvl = (IVectorialUniqueValueLegend) leg; |
93 |
try {
|
94 |
rules = getRules(uvl, version); |
95 |
} catch (UnsupportedSLDVersionException e) {
|
96 |
throw new SLDWriteException(e); |
97 |
} |
98 |
|
99 |
} else {
|
100 |
if (leg instanceof IVectorialIntervalLegend) { |
101 |
|
102 |
IVectorialIntervalLegend vil = (IVectorialIntervalLegend) leg; |
103 |
rules = getRules(vil, version); |
104 |
} else {
|
105 |
throw new SLDWriteException(new UnsupportedSLDException( |
106 |
leg == null ? "Null" : leg.getClass().getName())); |
107 |
} |
108 |
} |
109 |
} |
110 |
|
111 |
ISLDLayer resp = null;
|
112 |
try {
|
113 |
resp = sldMan.createSLDNamedLayer(version); |
114 |
SLDUserStyle sus = sldMan.createSLDUserStyle(version); |
115 |
SLDFeatureTypeStyle fts = sldMan.createSLDFeatureTypeStyle(version); |
116 |
for (int i=0; i<rules.size(); i++) { |
117 |
fts.addRule(rules.get(i)); |
118 |
} |
119 |
sus.addFeatureTypeStyle(fts); |
120 |
resp.addLayerStyle(sus); |
121 |
resp.setName(layer_name); |
122 |
|
123 |
} catch (UnsupportedSLDVersionException e) {
|
124 |
throw new SLDWriteException(e); |
125 |
} |
126 |
return resp;
|
127 |
} |
128 |
|
129 |
private List<SLDRule> getRules( |
130 |
IVectorialIntervalLegend vil, String v) {
|
131 |
// TODO Auto-generated method stub
|
132 |
return null; |
133 |
} |
134 |
|
135 |
private List<SLDRule> getRules( |
136 |
IVectorialUniqueValueLegend uvl, String v) throws UnsupportedSLDVersionException { |
137 |
|
138 |
List<SLDRule> resp = new ArrayList<SLDRule>(); |
139 |
|
140 |
ISymbol[] syms = uvl.getSymbols();
|
141 |
Object[] vals = uvl.getValues(); |
142 |
String fname = uvl.getClassifyingFieldNames()[0]; |
143 |
SLDRule rule = null;
|
144 |
|
145 |
for (int i=0; i<syms.length; i++) { |
146 |
rule = createEqualsRule(fname, vals[i], syms[i], v); |
147 |
|
148 |
if (rule != null) { |
149 |
resp.add(rule); |
150 |
} |
151 |
} |
152 |
return resp;
|
153 |
} |
154 |
|
155 |
private SLDRule createEqualsRule(
|
156 |
String fname,
|
157 |
Object val,
|
158 |
ISymbol sym, |
159 |
String v) throws UnsupportedSLDVersionException { |
160 |
|
161 |
|
162 |
SLDRule resp = null;
|
163 |
resp = sldMan.createSLDRule(v); |
164 |
resp.setName(val == null ? "[Null]" : val.toString()); |
165 |
|
166 |
Filter fil = new Filter(); |
167 |
// fil.addSymbolizer2Filter(symbol)
|
168 |
|
169 |
// TODO Auto-generated method stub
|
170 |
return null; |
171 |
} |
172 |
|
173 |
private List<SLDRule> getRules(ISingleSymbolLegend ssl, String v) |
174 |
throws UnsupportedSLDVersionException, SLDWriteException {
|
175 |
|
176 |
List<SLDRule> resp = new ArrayList<SLDRule>(); |
177 |
|
178 |
ISymbol sym = ssl.getDefaultSymbol(); |
179 |
ISLDSymbolizer symb = BasicSLDUtils.ISymbol2SLDSymbolizer(sym, v); |
180 |
|
181 |
SLDRule rule = sldMan.createSLDRule(v); |
182 |
if (symb instanceof SLDPointSymbolizer) { |
183 |
rule.addPointSymbolizer((SLDPointSymbolizer) symb); |
184 |
} else {
|
185 |
if (symb instanceof SLDLineSymbolizer) { |
186 |
rule.addLineSymbolizer((SLDLineSymbolizer) symb); |
187 |
} else {
|
188 |
if (symb instanceof SLDPolygonSymbolizer) { |
189 |
rule.addPolygonSymbolizer((SLDPolygonSymbolizer) symb); |
190 |
} else {
|
191 |
|
192 |
} |
193 |
} |
194 |
} |
195 |
resp.add(rule); |
196 |
return resp;
|
197 |
} |
198 |
|
199 |
// ====================================================
|
200 |
// ====================================================
|
201 |
|
202 |
public ILegend getLegend(ISLDLayer sld_layer, VectorLayer vlayer, String v) |
203 |
throws SLDReadException {
|
204 |
|
205 |
ILegend resp = null;
|
206 |
String[] fNames = sld_layer.getFieldNames(); |
207 |
|
208 |
boolean chk = false; |
209 |
|
210 |
try {
|
211 |
chk = checkFieldNames(fNames, vlayer); |
212 |
} catch (DataException e) {
|
213 |
throw new SLDReadException(e); |
214 |
} |
215 |
|
216 |
if (!chk) {
|
217 |
throw new SLDReadException(new Exception( |
218 |
"SLD layer uses fields which are not available in layer: "
|
219 |
+ vlayer.getName())); |
220 |
} |
221 |
|
222 |
int vlayer_gtype = 0; |
223 |
try {
|
224 |
vlayer_gtype = vlayer.getGeometryType().getType(); |
225 |
} catch (ReadException e) {
|
226 |
throw new SLDReadException(e); |
227 |
} |
228 |
//Multishape not yet supported
|
229 |
if (vlayer_gtype == Geometry.TYPES.GEOMETRY ||
|
230 |
vlayer_gtype == Geometry.TYPES.AGGREGATE) { |
231 |
throw new SLDReadException(new Exception( |
232 |
"Multi/aggregate geometry type layers not supported."));
|
233 |
} |
234 |
|
235 |
if (!sld_layer.layerHasFilterForSymbolizers(vlayer_gtype)) {
|
236 |
/*
|
237 |
* Single symbol legend
|
238 |
*/
|
239 |
try {
|
240 |
resp = getSingleSymbolLegend(sld_layer, vlayer_gtype, v); |
241 |
} catch (SLDReadException e) {
|
242 |
throw e;
|
243 |
} catch (Exception e) { |
244 |
throw new SLDReadException(e); |
245 |
} |
246 |
|
247 |
} else {
|
248 |
|
249 |
if (allFiltersAreEqualsTo(sld_layer)) {
|
250 |
|
251 |
} else {
|
252 |
if (allFiltersAreIntervals(sld_layer)) {
|
253 |
|
254 |
|
255 |
} else {
|
256 |
throw new SLDReadException(new UnsupportedSLDException( |
257 |
"Unsupported SLD layer: " + sld_layer.getName()));
|
258 |
} |
259 |
} |
260 |
} |
261 |
return resp;
|
262 |
} |
263 |
|
264 |
|
265 |
private ISingleSymbolLegend getSingleSymbolLegend(
|
266 |
ISLDLayer sld_layer, int geo_type, String v) |
267 |
throws Exception { |
268 |
|
269 |
ISingleSymbolLegend leg = (ISingleSymbolLegend) |
270 |
mcoMan().createLegend(ISingleSymbolLegend.LEGEND_NAME); |
271 |
|
272 |
ArrayList<ISLDSymbolizer> symbolizers;
|
273 |
symbolizers = sld_layer.getSymbolizersByShapeType(geo_type); |
274 |
|
275 |
IMultiLayerSymbol msym = null;
|
276 |
msym = syMan().createMultiLayerSymbol(geo_type); |
277 |
|
278 |
if (msym != null) { |
279 |
for (int i = 0; i < symbolizers.size(); i++) { |
280 |
ISymbol sym = BasicSLDUtils.SLDSymbolizer2ISymbol(symbolizers.get(i), v); |
281 |
msym.addLayer(sym); |
282 |
} |
283 |
ISymbol defaultSym = (msym.getLayerCount() > 1) ?
|
284 |
msym : msym.getLayer(0);
|
285 |
|
286 |
defaultSym.setDescription("Default");
|
287 |
leg.setDefaultSymbol(defaultSym); |
288 |
return leg;
|
289 |
} |
290 |
throw new SLDReadException(new Exception( |
291 |
"Unable to parse symbol in SingleSymbolLegend."));
|
292 |
} |
293 |
// ==========================================
|
294 |
// ==========================================
|
295 |
|
296 |
private boolean allFiltersAreIntervals(ISLDLayer sld_layer) { |
297 |
// TODO Auto-generated method stub
|
298 |
return false; |
299 |
} |
300 |
|
301 |
private boolean allFiltersAreEqualsTo(ISLDLayer sld_layer) { |
302 |
// TODO Auto-generated method stub
|
303 |
return false; |
304 |
} |
305 |
|
306 |
private boolean checkFieldNames(String[] fieldNames, VectorLayer lyr) |
307 |
throws DataException {
|
308 |
|
309 |
FeatureStore sto = lyr.getFeatureStore(); |
310 |
FeatureType fty = sto.getDefaultFeatureType(); |
311 |
|
312 |
for (int i = 0; i < fieldNames.length; i++) { |
313 |
if (fty.getAttributeDescriptor(fieldNames[i]) == null) { |
314 |
return false; |
315 |
} |
316 |
} |
317 |
return true; |
318 |
} |
319 |
|
320 |
|
321 |
// ====================================
|
322 |
private static SymbolManager syMan() { |
323 |
if (syMan == null) { |
324 |
syMan = MapContextLocator.getSymbolManager(); |
325 |
} |
326 |
return syMan;
|
327 |
} |
328 |
|
329 |
private static MapContextManager mcoMan() { |
330 |
if (mcoMan == null) { |
331 |
mcoMan = MapContextLocator.getMapContextManager(); |
332 |
} |
333 |
return mcoMan;
|
334 |
} |
335 |
|
336 |
private static SymbologyManager symMan() { |
337 |
if (symMan == null) { |
338 |
symMan = SymbologyLocator.getSymbologyManager(); |
339 |
} |
340 |
return symMan;
|
341 |
} |
342 |
} |