29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.geotools; |
package skrueger.geotools; |
31 |
|
|
32 |
|
import java.awt.Color; |
33 |
|
import java.awt.Dimension; |
34 |
|
import java.awt.Graphics; |
35 |
|
import java.awt.Graphics2D; |
36 |
|
import java.awt.Rectangle; |
37 |
|
import java.awt.geom.AffineTransform; |
38 |
|
import java.awt.image.BufferedImage; |
39 |
|
import java.awt.image.ColorModel; |
40 |
|
import java.awt.image.ComponentColorModel; |
41 |
|
import java.awt.image.DataBuffer; |
42 |
import java.io.File; |
import java.io.File; |
43 |
import java.io.FileNotFoundException; |
import java.io.FileNotFoundException; |
44 |
import java.io.FileWriter; |
import java.io.FileWriter; |
50 |
import java.util.SortedMap; |
import java.util.SortedMap; |
51 |
import java.util.TreeMap; |
import java.util.TreeMap; |
52 |
|
|
53 |
|
import javax.swing.BorderFactory; |
54 |
|
import javax.swing.Box; |
55 |
|
import javax.swing.BoxLayout; |
56 |
|
import javax.swing.ImageIcon; |
57 |
|
import javax.swing.JLabel; |
58 |
|
|
59 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
60 |
|
import org.geotools.coverage.grid.GeneralGridEnvelope; |
61 |
import org.geotools.coverage.grid.GridCoverage2D; |
import org.geotools.coverage.grid.GridCoverage2D; |
62 |
|
import org.geotools.coverage.grid.GridGeometry2D; |
63 |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
64 |
|
import org.geotools.coverage.grid.io.AbstractGridFormat; |
65 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
66 |
|
import org.geotools.gce.imagepyramid.ImagePyramidReader; |
67 |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
68 |
import org.geotools.map.DefaultMapLayer; |
import org.geotools.map.DefaultMapLayer; |
69 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
70 |
|
import org.geotools.parameter.Parameter; |
71 |
|
import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer; |
72 |
import org.geotools.styling.ColorMap; |
import org.geotools.styling.ColorMap; |
73 |
import org.geotools.styling.ColorMapEntry; |
import org.geotools.styling.ColorMapEntry; |
74 |
|
import org.geotools.styling.FeatureTypeStyle; |
75 |
import org.geotools.styling.RasterSymbolizer; |
import org.geotools.styling.RasterSymbolizer; |
76 |
|
import org.geotools.styling.Rule; |
77 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
78 |
import org.jdom.Document; |
import org.jdom.Document; |
79 |
import org.jdom.Element; |
import org.jdom.Element; |
80 |
import org.jdom.input.SAXBuilder; |
import org.jdom.input.SAXBuilder; |
81 |
import org.jdom.output.XMLOutputter; |
import org.jdom.output.XMLOutputter; |
82 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
83 |
|
import org.opengis.parameter.GeneralParameterValue; |
84 |
|
|
85 |
|
import schmitzm.geotools.JTSUtil; |
86 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
87 |
import schmitzm.io.IOUtil; |
import schmitzm.io.IOUtil; |
88 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
94 |
/** |
/** |
95 |
* This class provides static helper methods for dealing with |
* This class provides static helper methods for dealing with |
96 |
* {@link StyledLayerInterface} stuff. |
* {@link StyledLayerInterface} stuff. |
97 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
* |
98 |
|
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
99 |
|
* (University of Bonn/Germany) |
100 |
* @version 1.0 |
* @version 1.0 |
101 |
*/ |
*/ |
102 |
public class StyledLayerUtil { |
public class StyledLayerUtil { |
103 |
private static final Logger LOGGER = Logger.getLogger(StyledLayerUtil.class.getName()); |
private static final Logger LOGGER = Logger.getLogger(StyledLayerUtil.class |
104 |
private static final SAXBuilder SAX_BUILDER = new SAXBuilder(); |
.getName()); |
105 |
private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter(); |
private static final SAXBuilder SAX_BUILDER = new SAXBuilder(); |
106 |
|
private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter(); |
107 |
/** URL for Atlas XML schema */ |
|
108 |
public static final String AMLURI = "http://www.wikisquare.de/AtlasML"; |
/** URL for Atlas XML schema */ |
109 |
/** Name of the XML Element for the attribute meta data map */ |
public static final String AMLURI = "http://www.wikisquare.de/AtlasML"; |
110 |
public static final String ELEM_NAME_AMD = "attributeMetaData"; |
/** Name of the XML Element for the attribute meta data map */ |
111 |
/** Name of the XML Element for the raster legend data */ |
public static final String ELEM_NAME_AMD = "attributeMetaData"; |
112 |
public static final String ELEM_NAME_RLD = "rasterLegendData"; |
/** Name of the XML Element for the raster legend data */ |
113 |
/** Name of the XML Element for an attribute meta data map entry */ |
public static final String ELEM_NAME_RLD = "rasterLegendData"; |
114 |
public static final String ELEM_NAME_ATTRIBUTE = "dataAttribute"; |
/** Name of the XML Element for an attribute meta data map entry */ |
115 |
/** Name of the XML Element for an raster legend data entry */ |
public static final String ELEM_NAME_ATTRIBUTE = "dataAttribute"; |
116 |
public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem"; |
/** Name of the XML Element for an raster legend data entry */ |
117 |
/** Name of the XML Element for a translation */ |
public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem"; |
118 |
public static final String ELEM_NAME_TRANSLATION = "translation"; |
/** Name of the XML Element for a translation */ |
119 |
|
public static final String ELEM_NAME_TRANSLATION = "translation"; |
120 |
/** |
|
121 |
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
/** |
122 |
* {@link StyledLayerInterface} then its sytle is used. In case of direct |
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
123 |
* Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader}, |
* {@link StyledLayerInterface} then its sytle is used. In case of direct |
124 |
* {@link FeatureCollection}) a default style is generated. |
* Geotools objects ({@link GridCoverage2D}, |
125 |
* @param object an Object |
* {@link AbstractGridCoverage2DReader}, {@link FeatureCollection}) a |
126 |
* @exception Exception if {@code null} is given as object or an error occurs during layer creation |
* default style is generated. |
127 |
*/ |
* |
128 |
public static MapLayer createMapLayer(Object object) throws Exception { |
* @param object |
129 |
return createMapLayer(object,null); |
* an Object |
130 |
} |
* @exception Exception |
131 |
|
* if {@code null} is given as object or an error occurs |
132 |
/** |
* during layer creation |
133 |
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
*/ |
134 |
* {@link StyledLayerInterface} then its sytle is used. In case of direct |
public static MapLayer createMapLayer(Object object) throws Exception { |
135 |
* Geotools objects ({@link GridCoverage2D}, {@link AbstractGridCoverage2DReader}, |
return createMapLayer(object, null); |
136 |
* {@link FeatureCollection}) a default style is generated. |
} |
137 |
* @param object an Object |
|
138 |
* @param forcedStyle (SLD-)Style to force for the object |
/** |
139 |
* @exception Exception if {@code null} is given as object or an error occurs during layer creation |
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
140 |
*/ |
* {@link StyledLayerInterface} then its sytle is used. In case of direct |
141 |
public static MapLayer createMapLayer(Object object, Style forcedStyle) throws Exception { |
* Geotools objects ({@link GridCoverage2D}, |
142 |
MapLayer layer = null; |
* {@link AbstractGridCoverage2DReader}, {@link FeatureCollection}) a |
143 |
Style style = null; |
* default style is generated. |
144 |
if ( object instanceof StyledLayerInterface ) { |
* |
145 |
style = ((StyledLayerInterface<?>)object).getStyle(); |
* @param object |
146 |
object = ((StyledLayerInterface<?>)object).getGeoObject(); |
* an Object |
147 |
} |
* @param forcedStyle |
148 |
if ( forcedStyle != null ) |
* (SLD-)Style to force for the object |
149 |
style = forcedStyle; |
* @exception Exception |
150 |
if ( style == null ) |
* if {@code null} is given as object or an error occurs |
151 |
style = StylingUtil.createDefaultStyle(object); |
* during layer creation |
152 |
|
*/ |
153 |
if (object instanceof GridCoverage2D) |
public static MapLayer createMapLayer(Object object, Style forcedStyle) |
154 |
layer = new DefaultMapLayer( (GridCoverage2D) object, style); |
throws Exception { |
155 |
if (object instanceof AbstractGridCoverage2DReader) |
MapLayer layer = null; |
156 |
layer = new DefaultMapLayer( (AbstractGridCoverage2DReader) object, style); |
Style style = null; |
157 |
if (object instanceof FeatureCollection) |
if (object instanceof StyledLayerInterface) { |
158 |
layer = new DefaultMapLayer( (FeatureCollection) object, style); |
style = ((StyledLayerInterface<?>) object).getStyle(); |
159 |
|
object = ((StyledLayerInterface<?>) object).getGeoObject(); |
160 |
if ( layer == null ) |
} |
161 |
throw new Exception("Can not create MapLayer from "+(object == null ? "null" : object.getClass())); |
if (forcedStyle != null) |
162 |
|
style = forcedStyle; |
163 |
return layer; |
if (style == null) |
164 |
} |
style = StylingUtil.createDefaultStyle(object); |
165 |
|
|
166 |
/** |
if (object instanceof GridCoverage2D) |
167 |
* Creates an default instance of {@link StyledLayerInterface} for a Geotools |
layer = new DefaultMapLayer((GridCoverage2D) object, style); |
168 |
* object ({@link GridCoverage2D}, {@link FeatureCollection}) with a default |
if (object instanceof AbstractGridCoverage2DReader) |
169 |
* style. |
layer = new DefaultMapLayer((AbstractGridCoverage2DReader) object, |
170 |
* @param object an Object |
style); |
171 |
* @param title title for the object |
if (object instanceof FeatureCollection) |
172 |
* @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation |
layer = new DefaultMapLayer((FeatureCollection) object, style); |
173 |
*/ |
|
174 |
public static StyledLayerInterface<?> createStyledLayer(Object object, String title) { |
if (layer == null) |
175 |
return createStyledLayer(object, title, null); |
throw new Exception("Can not create MapLayer from " |
176 |
} |
+ (object == null ? "null" : object.getClass())); |
177 |
|
|
178 |
/** |
return layer; |
179 |
* Creates an default instance of {@link StyledLayerInterface} for a Geotools |
} |
180 |
* object ({@link GridCoverage2D}, {@link FeatureCollection}) with a given |
|
181 |
* style. |
/** |
182 |
* @param object an Object |
* Creates an default instance of {@link StyledLayerInterface} for a |
183 |
* @param title title for the object |
* Geotools object ({@link GridCoverage2D}, {@link FeatureCollection}) with |
184 |
* @param style style and meta data for the object |
* a default style. |
185 |
* @exception UnsupportedOperationException if {@code null} is given as object or an error occurs during creation |
* |
186 |
*/ |
* @param object |
187 |
public static StyledLayerInterface<?> createStyledLayer(Object object, String title, StyledLayerStyle style) { |
* an Object |
188 |
StyledLayerInterface<?> styledLayer = null; |
* @param title |
189 |
|
* title for the object |
190 |
String id = (title != null) ? title : "defaultID"; |
* @exception UnsupportedOperationException |
191 |
|
* if {@code null} is given as object or an error occurs |
192 |
if ( object instanceof GridCoverage2D ) |
* during creation |
193 |
styledLayer = new StyledGridCoverage( |
*/ |
194 |
(GridCoverage2D)object, |
public static StyledLayerInterface<?> createStyledLayer(Object object, |
195 |
id, |
String title) { |
196 |
title, |
return createStyledLayer(object, title, null); |
197 |
style |
} |
198 |
); |
|
199 |
else if ( object instanceof AbstractGridCoverage2DReader ) |
/** |
200 |
styledLayer = new StyledGridCoverageReader( |
* Creates an default instance of {@link StyledLayerInterface} for a |
201 |
(AbstractGridCoverage2DReader)object, |
* Geotools object ({@link GridCoverage2D}, {@link FeatureCollection}) with |
202 |
id, |
* a given style. |
203 |
title, |
* |
204 |
style |
* @param object |
205 |
); |
* an Object |
206 |
else if ( object instanceof FeatureCollection ) |
* @param title |
207 |
styledLayer = new StyledFeatureCollection( |
* title for the object |
208 |
(FeatureCollection)object, |
* @param style |
209 |
id, |
* style and meta data for the object |
210 |
title, |
* @exception UnsupportedOperationException |
211 |
style |
* if {@code null} is given as object or an error occurs |
212 |
); |
* during creation |
213 |
|
*/ |
214 |
if ( styledLayer == null ) |
public static StyledLayerInterface<?> createStyledLayer(Object object, |
215 |
throw new UnsupportedOperationException("Can not create StyledLayerInterface object from "+(object == null ? "null" : object.getClass())); |
String title, StyledLayerStyle style) { |
216 |
|
StyledLayerInterface<?> styledLayer = null; |
217 |
return styledLayer; |
|
218 |
} |
String id = (title != null) ? title : "defaultID"; |
219 |
|
|
220 |
/** |
if (object instanceof GridCoverage2D) |
221 |
* Return only the visible or invisible entries of an AttributeMetaData-Map. |
styledLayer = new StyledGridCoverage((GridCoverage2D) object, id, |
222 |
* @param amdMap AttributeMetaData-Map |
title, style); |
223 |
* @param visible indicated whether the visible or invisible entries are |
else if (object instanceof AbstractGridCoverage2DReader) |
224 |
* returned |
styledLayer = new StyledGridCoverageReader( |
225 |
*/ |
(AbstractGridCoverage2DReader) object, id, title, style); |
226 |
public static SortedMap<Integer,AttributeMetaData> getVisibleAttributeMetaData(Map<Integer,AttributeMetaData> amdMap, boolean visible) { |
else if (object instanceof FeatureCollection) |
227 |
SortedMap<Integer,AttributeMetaData> filteredMap = new TreeMap<Integer,AttributeMetaData>(); |
styledLayer = new StyledFeatureCollection( |
228 |
for (AttributeMetaData amd : amdMap.values()) |
(FeatureCollection) object, id, title, style); |
229 |
if ( amd.isVisible() ) |
|
230 |
filteredMap.put(amd.getColIdx(), amd); |
if (styledLayer == null) |
231 |
|
throw new UnsupportedOperationException( |
232 |
return filteredMap; |
"Can not create StyledLayerInterface object from " |
233 |
} |
+ (object == null ? "null" : object.getClass())); |
234 |
|
|
235 |
|
return styledLayer; |
236 |
/** |
} |
237 |
* Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}. |
|
238 |
* This method works like {@link AMLImport#parseDataAttribute(org.w3c.dom.Node}, |
/** |
239 |
* but for JDOM. |
* Return only the visible or invisible entries of an AttributeMetaData-Map. |
240 |
* @param element {@link Element} to parse |
* |
241 |
*/ |
* @param amdMap |
242 |
public static AttributeMetaData parseAttributeMetaData(final Element element) { |
* AttributeMetaData-Map |
243 |
final Integer col = Integer.valueOf(element.getAttributeValue("col")); |
* @param visible |
244 |
final Boolean visible = Boolean.valueOf(element.getAttributeValue("visible")); |
* indicated whether the visible or invisible entries are |
245 |
final String unit = element.getAttributeValue("unit"); |
* returned |
246 |
|
*/ |
247 |
Translation name = new Translation(); |
public static SortedMap<Integer, AttributeMetaData> getVisibleAttributeMetaData( |
248 |
Translation desc = new Translation(); |
Map<Integer, AttributeMetaData> amdMap, boolean visible) { |
249 |
for (final Element childElement : (List<Element>)element.getChildren()) { |
SortedMap<Integer, AttributeMetaData> filteredMap = new TreeMap<Integer, AttributeMetaData>(); |
250 |
if (childElement.getName() == null) |
for (AttributeMetaData amd : amdMap.values()) |
251 |
continue; |
if (amd.isVisible()) |
252 |
|
filteredMap.put(amd.getColIdx(), amd); |
253 |
if (childElement.getName().equals("name")) |
|
254 |
name = parseTranslation(childElement); |
return filteredMap; |
255 |
else if (childElement.getName().equals("desc")) |
} |
256 |
desc = parseTranslation(childElement); |
|
257 |
} |
/** |
258 |
return new AttributeMetaData(col, visible, name, desc, unit); |
* Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}. |
259 |
} |
* This method works like {@link |
260 |
|
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
261 |
/** |
* |
262 |
* Parses a {@link AttributeMetaData} map from an JDOM-{@link Element} |
* @param element |
263 |
* with {@code <attribute>}-childs. |
* {@link Element} to parse |
264 |
* @param element {@link Element} to parse |
*/ |
265 |
*/ |
public static AttributeMetaData parseAttributeMetaData(final Element element) { |
266 |
public static Map<Integer,AttributeMetaData> parseAttributeMetaDataMap(final Element element) { |
final Integer col = Integer.valueOf(element.getAttributeValue("col")); |
267 |
HashMap<Integer,AttributeMetaData> metaData = new HashMap<Integer,AttributeMetaData>(); |
final Boolean visible = Boolean.valueOf(element |
268 |
List<Element> attributesElements = element.getChildren( ELEM_NAME_ATTRIBUTE ); |
.getAttributeValue("visible")); |
269 |
for (Element attibuteElement : attributesElements) |
final String unit = element.getAttributeValue("unit"); |
270 |
{ |
|
271 |
AttributeMetaData attrMetaData = parseAttributeMetaData( attibuteElement ); |
Translation name = new Translation(); |
272 |
metaData.put( attrMetaData.getColIdx(), attrMetaData ); |
Translation desc = new Translation(); |
273 |
} |
for (final Element childElement : (List<Element>) element.getChildren()) { |
274 |
return metaData; |
if (childElement.getName() == null) |
275 |
} |
continue; |
276 |
|
|
277 |
/** |
if (childElement.getName().equals("name")) |
278 |
* Loads a {@link AttributeMetaData} object from an URL. |
name = parseTranslation(childElement); |
279 |
* @param documentUrl {@link URL} to parse |
else if (childElement.getName().equals("desc")) |
280 |
* @see #parseAttributeMetaData(Element) |
desc = parseTranslation(childElement); |
281 |
*/ |
} |
282 |
public static Map<Integer,AttributeMetaData> loadAttributeMetaDataMap(final URL documentUrl) throws Exception { |
return new AttributeMetaData(col, visible, name, desc, unit); |
283 |
Document document = SAX_BUILDER.build(documentUrl); |
} |
284 |
return parseAttributeMetaDataMap( document.getRootElement() ); |
|
285 |
} |
/** |
286 |
|
* Parses a {@link AttributeMetaData} map from an JDOM-{@link Element} with |
287 |
/** |
* {@code <attribute>}-childs. |
288 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
* |
289 |
* object. |
* @param element |
290 |
* @param amd meta data for one attribute |
* {@link Element} to parse |
291 |
*/ |
*/ |
292 |
public static Element createAttributeMetaDataElement(final AttributeMetaData amd) { |
public static Map<Integer, AttributeMetaData> parseAttributeMetaDataMap( |
293 |
final Element element = new Element( ELEM_NAME_ATTRIBUTE , AMLURI); |
final Element element) { |
294 |
element.setAttribute("col", String.valueOf( amd.getColIdx() ) ); |
HashMap<Integer, AttributeMetaData> metaData = new HashMap<Integer, AttributeMetaData>(); |
295 |
element.setAttribute("visible", String.valueOf( amd.isVisible() ) ); |
List<Element> attributesElements = element |
296 |
element.setAttribute("unit", amd.getUnit() ); |
.getChildren(ELEM_NAME_ATTRIBUTE); |
297 |
// Creating a aml:name tag... |
for (Element attibuteElement : attributesElements) { |
298 |
element.addContent( createTranslationElement("name", amd.getTitle()) ); |
AttributeMetaData attrMetaData = parseAttributeMetaData(attibuteElement); |
299 |
// Creating a aml:desc tag... |
metaData.put(attrMetaData.getColIdx(), attrMetaData); |
300 |
element.addContent( createTranslationElement("desc", amd.getDesc()) ); |
} |
301 |
return element; |
return metaData; |
302 |
} |
} |
303 |
|
|
304 |
/** |
/** |
305 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
* Loads a {@link AttributeMetaData} object from an URL. |
306 |
* map. |
* |
307 |
* @param amdMap map of attribute meta data |
* @param documentUrl |
308 |
*/ |
* {@link URL} to parse |
309 |
public static Element createAttributeMetaDataMapElement(final Map<Integer,AttributeMetaData> amdMap) { |
* @see #parseAttributeMetaData(Element) |
310 |
final Element element = new Element( ELEM_NAME_AMD , AMLURI); |
*/ |
311 |
for (AttributeMetaData amd : amdMap.values()) |
public static Map<Integer, AttributeMetaData> loadAttributeMetaDataMap( |
312 |
element.addContent( createAttributeMetaDataElement( amd ) ); |
final URL documentUrl) throws Exception { |
313 |
return element; |
Document document = SAX_BUILDER.build(documentUrl); |
314 |
} |
return parseAttributeMetaDataMap(document.getRootElement()); |
315 |
|
} |
316 |
/** |
|
317 |
* Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL. |
/** |
318 |
* @param amdMap map of {@link AttributeMetaData} |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
319 |
* @param documentUrl {@link URL} to store the XML |
* object. |
320 |
*/ |
* |
321 |
public static void saveAttributeMetaDataMap(final Map<Integer,AttributeMetaData> amdMap, final URL documentUrl) throws Exception { |
* @param amd |
322 |
// Create XML-Document |
* meta data for one attribute |
323 |
final FileWriter out = new FileWriter( new File(documentUrl.toURI()) ); |
*/ |
324 |
XML_OUTPUTTER.output( |
public static Element createAttributeMetaDataElement( |
325 |
createAttributeMetaDataMapElement(amdMap), |
final AttributeMetaData amd) { |
326 |
out |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
327 |
); |
element.setAttribute("col", String.valueOf(amd.getColIdx())); |
328 |
out.flush(); |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
329 |
out.close(); |
element.setAttribute("unit", amd.getUnit()); |
330 |
} |
// Creating a aml:name tag... |
331 |
|
element.addContent(createTranslationElement("name", amd.getTitle())); |
332 |
|
// Creating a aml:desc tag... |
333 |
|
element.addContent(createTranslationElement("desc", amd.getDesc())); |
334 |
/** |
return element; |
335 |
* Parses a {@link RasterLegendData} object from an JDOM-{@link Element}. |
} |
336 |
* This method works like {@link AMLImport#parseRasterLegendData(org.w3c.dom.Node}, |
|
337 |
* but for JDOM. |
/** |
338 |
* @param element {@link Element} to parse |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
339 |
*/ |
* map. |
340 |
public static RasterLegendData parseRasterLegendData(Element element) { |
* |
341 |
|
* @param amdMap |
342 |
final boolean paintGaps = Boolean.valueOf( element.getAttributeValue("paintGaps") ); |
* map of attribute meta data |
343 |
|
*/ |
344 |
RasterLegendData rld = new RasterLegendData(paintGaps); |
public static Element createAttributeMetaDataMapElement( |
345 |
|
final Map<Integer, AttributeMetaData> amdMap) { |
346 |
for ( Element childElement : (List<Element>)element.getChildren() ) { |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
347 |
final String name = childElement.getName(); |
for (AttributeMetaData amd : amdMap.values()) |
348 |
// Cancel if it's an attribute |
element.addContent(createAttributeMetaDataElement(amd)); |
349 |
if ( childElement.getChildren().size() == 0 ) |
return element; |
350 |
continue; |
} |
351 |
|
|
352 |
if (name.equals( ELEM_NAME_RASTERLEGEND )) { |
/** |
353 |
final String valueAttr = childElement.getAttributeValue("value"); |
* Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL. |
354 |
if ( valueAttr == null ) |
* |
355 |
throw new UnsupportedOperationException("Attribute 'value' missing for definition of <"+ELEM_NAME_RASTERLEGEND+">"); |
* @param amdMap |
356 |
final double value = Double.valueOf(valueAttr); |
* map of {@link AttributeMetaData} |
357 |
|
* @param documentUrl |
358 |
// first and only item should be the label |
* {@link URL} to store the XML |
359 |
final Element labelElement = childElement.getChild("label"); |
*/ |
360 |
// id label element is missing, the translation is searched directly |
public static void saveAttributeMetaDataMap( |
361 |
// as childs of the rasterLegendItem element |
final Map<Integer, AttributeMetaData> amdMap, final URL documentUrl) |
362 |
Translation label = parseTranslation( labelElement != null ? labelElement : childElement ); |
throws Exception { |
363 |
rld.put(value, label); |
// Create XML-Document |
364 |
} |
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
365 |
} |
XML_OUTPUTTER.output(createAttributeMetaDataMapElement(amdMap), out); |
366 |
|
out.flush(); |
367 |
return rld; |
out.close(); |
368 |
} |
} |
369 |
|
|
370 |
/** |
/** |
371 |
* Loads a {@link RasterLegendData} object from an URL. |
* Parses a {@link RasterLegendData} object from an JDOM-{@link Element}. |
372 |
* @param documentUrl {@link URL} to parse |
* This method works like {@link |
373 |
* @see #parseAttributeMetaData(Element) |
* AMLImport#parseRasterLegendData(org.w3c.dom.Node}, but for JDOM. |
374 |
*/ |
* |
375 |
public static RasterLegendData loadRasterLegendData(final URL documentUrl) throws Exception { |
* @param element |
376 |
Document document = SAX_BUILDER.build(documentUrl); |
* {@link Element} to parse |
377 |
return parseRasterLegendData( document.getRootElement() ); |
*/ |
378 |
} |
public static RasterLegendData parseRasterLegendData(Element element) { |
379 |
|
|
380 |
/** |
final boolean paintGaps = Boolean.valueOf(element |
381 |
* Creates an JDOM {@link Element} for the given {@link RasterLegendData} |
.getAttributeValue("paintGaps")); |
382 |
* map. |
|
383 |
* @param rld raster legend data |
RasterLegendData rld = new RasterLegendData(paintGaps); |
384 |
*/ |
|
385 |
public static Element createRasterLegendDataElement(final RasterLegendData rld) { |
for (Element childElement : (List<Element>) element.getChildren()) { |
386 |
final Element element = new Element( ELEM_NAME_RLD , AMLURI); |
final String name = childElement.getName(); |
387 |
element.setAttribute("paintGaps", rld.isPaintGaps().toString()); |
// Cancel if it's an attribute |
388 |
for (Double key : rld.getSortedKeys()) { |
if (childElement.getChildren().size() == 0) |
389 |
Element item = new Element( ELEM_NAME_RASTERLEGEND, AMLURI); |
continue; |
390 |
item.setAttribute("value", key.toString()); |
|
391 |
item.addContent( createTranslationElement("label", rld.get(key)) ); |
if (name.equals(ELEM_NAME_RASTERLEGEND)) { |
392 |
element.addContent(item); |
final String valueAttr = childElement |
393 |
} |
.getAttributeValue("value"); |
394 |
return element; |
if (valueAttr == null) |
395 |
} |
throw new UnsupportedOperationException( |
396 |
|
"Attribute 'value' missing for definition of <" |
397 |
/** |
+ ELEM_NAME_RASTERLEGEND + ">"); |
398 |
* Creates {@link RasterLegendData} from a {@link ColorMap}. |
final double value = Double.valueOf(valueAttr); |
399 |
* @param colorMap a color map |
|
400 |
* @param paintGaps indicated whether gaps are painted between the legend items |
// first and only item should be the label |
401 |
* @param digits number of digits the grid value classes (and legend) are |
final Element labelElement = childElement.getChild("label"); |
402 |
* rounded to (null means no round; >= 0 means digits after comma; |
// id label element is missing, the translation is searched |
403 |
* < 0 means digits before comma) */ |
// directly |
404 |
public static RasterLegendData generateRasterLegendData(ColorMap colorMap, boolean paintGaps, Integer digits) { |
// as childs of the rasterLegendItem element |
405 |
DecimalFormat decFormat = digits != null ? new DecimalFormat( SwingUtil.getNumberFormatPattern(digits) ) : null; |
Translation label = parseTranslation(labelElement != null ? labelElement |
406 |
RasterLegendData rld = new RasterLegendData(paintGaps); |
: childElement); |
407 |
for (ColorMapEntry cme : colorMap.getColorMapEntries()) |
rld.put(value, label); |
408 |
{ |
} |
409 |
double value = StylingUtil.getQuantityFromColorMapEntry(cme); |
} |
410 |
String label = cme.getLabel(); |
|
411 |
// if no label is set (e.g. quantitative style), |
return rld; |
412 |
// use the value as label |
} |
413 |
if ( label == null || label.equals("") ) |
|
414 |
if ( digits == null ) |
/** |
415 |
label = String.valueOf(value); |
* Loads a {@link RasterLegendData} object from an URL. |
416 |
else |
* |
417 |
label = decFormat.format( LangUtil.round(value, digits) ); |
* @param documentUrl |
418 |
rld.put( value, new Translation(" "+label) ); |
* {@link URL} to parse |
419 |
} |
* @see #parseAttributeMetaData(Element) |
420 |
return rld; |
*/ |
421 |
} |
public static RasterLegendData loadRasterLegendData(final URL documentUrl) |
422 |
|
throws Exception { |
423 |
/** |
Document document = SAX_BUILDER.build(documentUrl); |
424 |
* Creates {@link RasterLegendData} from the {@link ColorMap} of a style. |
return parseRasterLegendData(document.getRootElement()); |
425 |
* @param style a raster style (must contain a {@link RasterSymbolizer}) |
} |
426 |
* @param paintGaps indicated whether gaps are painted between the legend items |
|
427 |
* @param digits number of digits the grid value classes (and legend) are |
/** |
428 |
* rounded to (null means no round; >= 0 means digits after comma; |
* Creates an JDOM {@link Element} for the given {@link RasterLegendData} |
429 |
* < 0 means digits before comma) */ |
* map. |
430 |
public static RasterLegendData generateRasterLegendData(Style style, boolean paintGaps, Integer digits) { |
* |
431 |
ColorMap colorMap = StylingUtil.getColorMapFromStyle(style); |
* @param rld |
432 |
if ( colorMap == null) |
* raster legend data |
433 |
throw new IllegalArgumentException("Color map can not be determined from style!"); |
*/ |
434 |
return generateRasterLegendData(colorMap, paintGaps, digits); |
public static Element createRasterLegendDataElement( |
435 |
} |
final RasterLegendData rld) { |
436 |
|
final Element element = new Element(ELEM_NAME_RLD, AMLURI); |
437 |
/** |
element.setAttribute("paintGaps", rld.isPaintGaps().toString()); |
438 |
* Saves a {@link RasterLegendData} to an URL. |
for (Double key : rld.getSortedKeys()) { |
439 |
* @param rld raster legend data |
Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI); |
440 |
* @param documentUrl {@link URL} to store the XML |
item.setAttribute("value", key.toString()); |
441 |
*/ |
item.addContent(createTranslationElement("label", rld.get(key))); |
442 |
public static void saveRasterLegendData(final RasterLegendData rld, final URL documentUrl) throws Exception { |
element.addContent(item); |
443 |
// Create XML-Document |
} |
444 |
final FileWriter out = new FileWriter( new File(documentUrl.toURI()) ); |
return element; |
445 |
XML_OUTPUTTER.output( |
} |
446 |
createRasterLegendDataElement(rld), |
|
447 |
out |
/** |
448 |
); |
* Creates {@link RasterLegendData} from a {@link ColorMap}. |
449 |
out.flush(); |
* |
450 |
out.close(); |
* @param colorMap |
451 |
} |
* a color map |
452 |
|
* @param paintGaps |
453 |
/** |
* indicated whether gaps are painted between the legend items |
454 |
* Parses a {@link Translation} object from an JDOM-{@link Element}. |
* @param digits |
455 |
* This method works like {@link AMLImport#parseTranslation(org.w3c.dom.Node}, |
* number of digits the grid value classes (and legend) are |
456 |
* but for JDOM. |
* rounded to (null means no round; >= 0 means digits after |
457 |
* @param element {@link Element} to parse |
* comma; < 0 means digits before comma) |
458 |
*/ |
*/ |
459 |
public final static Translation parseTranslation(final Element element) { |
public static RasterLegendData generateRasterLegendData(ColorMap colorMap, |
460 |
Translation trans = new Translation(); |
boolean paintGaps, Integer digits) { |
461 |
|
DecimalFormat decFormat = digits != null ? new DecimalFormat(SwingUtil |
462 |
if (element == null) |
.getNumberFormatPattern(digits)) : null; |
463 |
return trans; |
RasterLegendData rld = new RasterLegendData(paintGaps); |
464 |
|
for (ColorMapEntry cme : colorMap.getColorMapEntries()) { |
465 |
for (final Element translationElement : (List<Element>)element.getChildren()) { |
double value = StylingUtil.getQuantityFromColorMapEntry(cme); |
466 |
final String name = translationElement.getName(); |
String label = cme.getLabel(); |
467 |
if (name == null) |
// if no label is set (e.g. quantitative style), |
468 |
continue; |
// use the value as label |
469 |
|
if (label == null || label.equals("")) |
470 |
// lang attribute |
if (digits == null) |
471 |
String lang = translationElement.getAttributeValue("lang"); |
label = String.valueOf(value); |
472 |
// set the default, if no language code is set |
else |
473 |
if ( lang == null ) |
label = decFormat.format(LangUtil.round(value, digits)); |
474 |
lang = Translation.DEFAULT_KEY; |
rld.put(value, new Translation(" " + label)); |
475 |
|
} |
476 |
final String translationText = translationElement.getValue(); |
return rld; |
477 |
if (translationText == null) |
} |
478 |
trans.put(lang, ""); |
|
479 |
else |
/** |
480 |
trans.put(lang, translationText); |
* Creates {@link RasterLegendData} from the {@link ColorMap} of a style. |
481 |
} |
* |
482 |
|
* @param style |
483 |
// if no <translation> is given, the value of the node should |
* a raster style (must contain a {@link RasterSymbolizer}) |
484 |
// be used as a default translation |
* @param paintGaps |
485 |
if (trans.size() == 0) |
* indicated whether gaps are painted between the legend items |
486 |
trans.put( Translation.DEFAULT_KEY, element.getValue() ); |
* @param digits |
487 |
// trans = new Translation( ((List<Element>)element.getChildren()).get(0).getValue() ); |
* number of digits the grid value classes (and legend) are |
488 |
|
* rounded to (null means no round; >= 0 means digits after |
489 |
return trans; |
* comma; < 0 means digits before comma) |
490 |
} |
*/ |
491 |
|
public static RasterLegendData generateRasterLegendData(Style style, |
492 |
/** |
boolean paintGaps, Integer digits) { |
493 |
* Creates an JDOM {@link Element} for the given {@link Translation}. |
ColorMap colorMap = StylingUtil.getColorMapFromStyle(style); |
494 |
* @param tagname Name of the Element |
if (colorMap == null) |
495 |
* @param translation Translation to store in the Element |
throw new IllegalArgumentException( |
496 |
*/ |
"Color map can not be determined from style!"); |
497 |
public final static Element createTranslationElement(String tagname, Translation translation) { |
return generateRasterLegendData(colorMap, paintGaps, digits); |
498 |
Element element = new Element(tagname, AMLURI); |
} |
499 |
if ( translation == null ) |
|
500 |
throw new UnsupportedOperationException("Translation element can not be created from null!"); |
/** |
501 |
|
* Saves a {@link RasterLegendData} to an URL. |
502 |
// If only a default translation is set, the <translation lang="..">..</tranlation> |
* |
503 |
// part is not used |
* @param rld |
504 |
if (translation.keySet().size() == 1 && translation.get(Translation.DEFAULT_KEY) != null) { |
* raster legend data |
505 |
element.addContent( translation.get(Translation.DEFAULT_KEY) ); |
* @param documentUrl |
506 |
return element; |
* {@link URL} to store the XML |
507 |
} |
*/ |
508 |
|
public static void saveRasterLegendData(final RasterLegendData rld, |
509 |
// add a <translation lang="..">..</tranlation> part to the element for |
final URL documentUrl) throws Exception { |
510 |
// all languages |
// Create XML-Document |
511 |
for (String lang : translation.keySet()) { |
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
512 |
Element translationElement = new Element( ELEM_NAME_TRANSLATION , AMLURI); |
XML_OUTPUTTER.output(createRasterLegendDataElement(rld), out); |
513 |
translationElement.setAttribute("lang", lang); |
out.flush(); |
514 |
String translationString = translation.get(lang); |
out.close(); |
515 |
if (translationString == null) |
} |
516 |
translationString = ""; |
|
517 |
translationElement.addContent( translationString ); |
/** |
518 |
element.addContent(translationElement); |
* Parses a {@link Translation} object from an JDOM-{@link Element}. This |
519 |
} |
* method works like {@link AMLImport#parseTranslation(org.w3c.dom.Node}, |
520 |
|
* but for JDOM. |
521 |
return element; |
* |
522 |
} |
* @param element |
523 |
|
* {@link Element} to parse |
524 |
|
*/ |
525 |
/** |
public final static Translation parseTranslation(final Element element) { |
526 |
* Sets a style to {@link StyledLayerInterface}. |
Translation trans = new Translation(); |
527 |
* @param styledObject a styled object |
|
528 |
* @param style a Style |
if (element == null) |
529 |
*/ |
return trans; |
530 |
public static void setStyledLayerStyle(StyledLayerInterface styledObject, StyledLayerStyle<?> style) { |
|
531 |
// set SLD style |
for (final Element translationElement : (List<Element>) element |
532 |
styledObject.setStyle( style.getGeoObjectStyle() ); |
.getChildren()) { |
533 |
// set meta data |
final String name = translationElement.getName(); |
534 |
if ( styledObject instanceof StyledGridCoverageInterface && |
if (name == null) |
535 |
(style.getMetaData() instanceof RasterLegendData || style.getMetaData() == null) ) { |
continue; |
536 |
RasterLegendData sourceRld = (RasterLegendData)style.getMetaData(); |
|
537 |
RasterLegendData destRld = ((StyledGridCoverageInterface)styledObject).getLegendMetaData(); |
// lang attribute |
538 |
if ( destRld != null && sourceRld != null ) { |
String lang = translationElement.getAttributeValue("lang"); |
539 |
destRld.setPaintGaps(sourceRld.isPaintGaps()); |
// set the default, if no language code is set |
540 |
destRld.clear(); |
if (lang == null) |
541 |
destRld.putAll( sourceRld ); |
lang = Translation.DEFAULT_KEY; |
542 |
} |
|
543 |
return; |
final String translationText = translationElement.getValue(); |
544 |
} |
if (translationText == null) |
545 |
if ( styledObject instanceof StyledFeatureCollectionInterface && |
trans.put(lang, ""); |
546 |
(style.getMetaData() instanceof Map || style.getMetaData() == null) ) { |
else |
547 |
Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>)style.getMetaData(); |
trans.put(lang, translationText); |
548 |
Map<Integer, AttributeMetaData> destAmd = ((StyledFeatureCollectionInterface)styledObject).getAttributeMetaDataMap(); |
} |
549 |
if ( destAmd != null && sourceAmd != null ) { |
|
550 |
destAmd.clear(); |
// if no <translation> is given, the value of the node should |
551 |
destAmd.putAll( sourceAmd ); |
// be used as a default translation |
552 |
} |
if (trans.size() == 0) |
553 |
return; |
trans.put(Translation.DEFAULT_KEY, element.getValue()); |
554 |
} |
// trans = new Translation( |
555 |
|
// ((List<Element>)element.getChildren()).get(0).getValue() ); |
556 |
throw new UnsupportedOperationException("Style is not compatible to object: " + |
|
557 |
(style.getMetaData() == null ? null : style.getMetaData().getClass().getSimpleName()) + |
return trans; |
558 |
" <-> " + |
} |
559 |
(styledObject == null ? null : styledObject.getClass().getSimpleName())); |
|
560 |
} |
/** |
561 |
|
* Creates an JDOM {@link Element} for the given {@link Translation}. |
562 |
/** |
* |
563 |
* Returns the style a {@link StyledLayerInterface} as a {@link StyledLayerStyle}. |
* @param tagname |
564 |
* @param styledObject a styled object |
* Name of the Element |
565 |
* @return {@code StyledLayerStyle<RasterLegendData>} for {@link StyledGridCoverageInterface} |
* @param translation |
566 |
* or {@code StyledLayerStyle<Map<Integer,AttributeMetaData>>} for |
* Translation to store in the Element |
567 |
* {@link StyledFeatureCollectionInterface} |
*/ |
568 |
*/ |
public final static Element createTranslationElement(String tagname, |
569 |
public static StyledLayerStyle<?> getStyledLayerStyle(StyledLayerInterface styledObject) { |
Translation translation) { |
570 |
if ( styledObject instanceof StyledGridCoverageInterface ) |
Element element = new Element(tagname, AMLURI); |
571 |
return getStyledLayerStyle( (StyledGridCoverageInterface)styledObject ); |
if (translation == null) |
572 |
if ( styledObject instanceof StyledFeatureCollectionInterface ) |
throw new UnsupportedOperationException( |
573 |
return getStyledLayerStyle( (StyledFeatureCollectionInterface)styledObject ); |
"Translation element can not be created from null!"); |
574 |
throw new UnsupportedOperationException("Unknown type of StyledLayerInterface: "+(styledObject == null ? null : styledObject.getClass().getSimpleName())); |
|
575 |
} |
// If only a default translation is set, the <translation |
576 |
|
// lang="..">..</tranlation> |
577 |
/** |
// part is not used |
578 |
* Returns the style and raster meta data of a {@link StyledGridCoverageInterface} |
if (translation.keySet().size() == 1 |
579 |
* as a {@link StyledLayerStyle}. |
&& translation.get(Translation.DEFAULT_KEY) != null) { |
580 |
* @param styledGC a styled grid coverage |
element.addContent(translation.get(Translation.DEFAULT_KEY)); |
581 |
*/ |
return element; |
582 |
public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle(StyledGridCoverageInterface styledGC) { |
} |
583 |
return new StyledLayerStyle<RasterLegendData>( |
|
584 |
styledGC.getStyle(), |
// add a <translation lang="..">..</tranlation> part to the element for |
585 |
styledGC.getLegendMetaData() |
// all languages |
586 |
); |
for (String lang : translation.keySet()) { |
587 |
} |
Element translationElement = new Element(ELEM_NAME_TRANSLATION, |
588 |
|
AMLURI); |
589 |
/** |
translationElement.setAttribute("lang", lang); |
590 |
* Returns the style and attribute meta data of a {@link StyledFeatureCollectionInterface} |
String translationString = translation.get(lang); |
591 |
* as a {@link StyledLayerStyle}. |
if (translationString == null) |
592 |
* @param styledFC a styled feature collection |
translationString = ""; |
593 |
*/ |
translationElement.addContent(translationString); |
594 |
public static StyledLayerStyle<Map<Integer,AttributeMetaData>> getStyledLayerStyle(StyledFeatureCollectionInterface styledFC) { |
element.addContent(translationElement); |
595 |
return new StyledLayerStyle<Map<Integer,AttributeMetaData>>( |
} |
596 |
styledFC.getStyle(), |
|
597 |
styledFC.getAttributeMetaDataMap() |
return element; |
598 |
); |
} |
599 |
} |
|
600 |
|
/** |
601 |
/** |
* Sets a style to {@link StyledLayerInterface}. |
602 |
* Loads a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData Raster-LegendData} |
* |
603 |
* for a given geo-object (raster) source. The SLD file must be present. A missing |
* @param styledObject |
604 |
* raster legend-data file is tolerated. |
* a styled object |
605 |
* @param geoObjectURL URL of the (already read) raster object |
* @param style |
606 |
* @param sldExt file extention for the SLD file |
* a Style |
607 |
* @param rldExt file extention for the raster legend-data file |
*/ |
608 |
* @return {@code null} in case of any error |
public static void setStyledLayerStyle(StyledLayerInterface styledObject, |
609 |
*/ |
StyledLayerStyle<?> style) { |
610 |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL, String sldExt, String rldExt) { |
// set SLD style |
611 |
RasterLegendData metaData = null; |
styledObject.setStyle(style.getGeoObjectStyle()); |
612 |
Style sldStyle = null; |
// set meta data |
613 |
try { |
if (styledObject instanceof StyledGridCoverageInterface |
614 |
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt)); |
&& (style.getMetaData() instanceof RasterLegendData || style |
615 |
// SLD must be present |
.getMetaData() == null)) { |
616 |
if ( styles == null || styles.length == 0 ) |
RasterLegendData sourceRld = (RasterLegendData) style.getMetaData(); |
617 |
return null; |
RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject) |
618 |
sldStyle = styles[0]; |
.getLegendMetaData(); |
619 |
} |
if (destRld != null && sourceRld != null) { |
620 |
catch (Exception err) { |
destRld.setPaintGaps(sourceRld.isPaintGaps()); |
621 |
// SLD must be present |
destRld.clear(); |
622 |
LangUtil.logDebugError(LOGGER,err); |
destRld.putAll(sourceRld); |
623 |
return null; |
} |
624 |
} |
return; |
625 |
|
} |
626 |
try { |
if (styledObject instanceof StyledFeatureCollectionInterface |
627 |
metaData = StyledLayerUtil.loadRasterLegendData( IOUtil.changeUrlExt(geoObjectURL,rldExt) ); |
&& (style.getMetaData() instanceof Map || style.getMetaData() == null)) { |
628 |
} catch (FileNotFoundException err) { |
Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>) style |
629 |
// ignore missing raster legend data |
.getMetaData(); |
630 |
} catch (Exception err) { |
Map<Integer, AttributeMetaData> destAmd = ((StyledFeatureCollectionInterface) styledObject) |
631 |
// any other error during legend data creation leads to error |
.getAttributeMetaDataMap(); |
632 |
LangUtil.logDebugError(LOGGER,err); |
if (destAmd != null && sourceAmd != null) { |
633 |
return null; |
destAmd.clear(); |
634 |
} |
destAmd.putAll(sourceAmd); |
635 |
return new StyledLayerStyle<RasterLegendData>(sldStyle, metaData); |
} |
636 |
} |
return; |
637 |
|
} |
638 |
/** |
|
639 |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
throw new UnsupportedOperationException( |
640 |
* {@linkplain RasterLegendData Raster-LegendData} from a {@code .rld} file |
"Style is not compatible to object: " |
641 |
* for a given geo-object (raster) source. The SLD file must be present. A missing |
+ (style.getMetaData() == null ? null : style |
642 |
* raster legend-data file is tolerated. |
.getMetaData().getClass().getSimpleName()) |
643 |
* @param geoObjectURL URL of the (already read) raster object |
+ " <-> " |
644 |
* @param sldExt file extention for the SLD file |
+ (styledObject == null ? null : styledObject |
645 |
* @param rldExt file extention for the raster legend-data file |
.getClass().getSimpleName())); |
646 |
* @return {@code null} in case of any error |
} |
647 |
*/ |
|
648 |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle(URL geoObjectURL) { |
/** |
649 |
return loadStyledRasterStyle(geoObjectURL, "sld", "rld"); |
* Returns the style a {@link StyledLayerInterface} as a |
650 |
} |
* {@link StyledLayerStyle}. |
651 |
|
* |
652 |
/** |
* @param styledObject |
653 |
* Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData AttributeMetaData-Map} |
* a styled object |
654 |
* for a given geo-object (feature) source. The SLD file must be present. A missing |
* @return {@code StyledLayerStyle<RasterLegendData>} for |
655 |
* attribute meta-data file is tolerated. |
* {@link StyledGridCoverageInterface} or {@code |
656 |
* @param geoObjectURL URL of the (already read) feature object |
* StyledLayerStyle<Map<Integer,AttributeMetaData>>} for |
657 |
* @param sldExt file extention for the SLD file |
* {@link StyledFeatureCollectionInterface} |
658 |
* @param rldExt file extention for the raster legend-data file |
*/ |
659 |
* @return {@code null} in case of any error |
public static StyledLayerStyle<?> getStyledLayerStyle( |
660 |
*/ |
StyledLayerInterface styledObject) { |
661 |
public static StyledLayerStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL, String sldExt, String rldExt) { |
if (styledObject instanceof StyledGridCoverageInterface) |
662 |
Map<Integer,AttributeMetaData> metaData = null; |
return getStyledLayerStyle((StyledGridCoverageInterface) styledObject); |
663 |
Style sldStyle = null; |
if (styledObject instanceof StyledFeatureCollectionInterface) |
664 |
try { |
return getStyledLayerStyle((StyledFeatureCollectionInterface) styledObject); |
665 |
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(geoObjectURL, sldExt)); |
throw new UnsupportedOperationException( |
666 |
// SLD must be present |
"Unknown type of StyledLayerInterface: " |
667 |
if ( styles == null || styles.length == 0 ) |
+ (styledObject == null ? null : styledObject |
668 |
return null; |
.getClass().getSimpleName())); |
669 |
sldStyle = styles[0]; |
} |
670 |
} catch (Exception err) { |
|
671 |
// SLD must be present |
/** |
672 |
LangUtil.logDebugError(LOGGER,err); |
* Returns the style and raster meta data of a |
673 |
return null; |
* {@link StyledGridCoverageInterface} as a {@link StyledLayerStyle}. |
674 |
} |
* |
675 |
|
* @param styledGC |
676 |
try { |
* a styled grid coverage |
677 |
metaData = StyledLayerUtil.loadAttributeMetaDataMap( IOUtil.changeUrlExt(geoObjectURL,rldExt) ); |
*/ |
678 |
} catch (FileNotFoundException err) { |
public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle( |
679 |
// ignore missing attribute meta data |
StyledGridCoverageInterface styledGC) { |
680 |
} catch (Exception err) { |
return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(), |
681 |
// any other error during meta data creation leads to error |
styledGC.getLegendMetaData()); |
682 |
LangUtil.logDebugError(LOGGER,err); |
} |
683 |
return null; |
|
684 |
} |
/** |
685 |
|
* Returns the style and attribute meta data of a |
686 |
return new StyledLayerStyle<Map<Integer,AttributeMetaData>>(sldStyle, metaData); |
* {@link StyledFeatureCollectionInterface} as a {@link StyledLayerStyle}. |
687 |
} |
* |
688 |
|
* @param styledFC |
689 |
/** |
* a styled feature collection |
690 |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
*/ |
691 |
* {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd} file |
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> getStyledLayerStyle( |
692 |
* for a given geo-object (feature) source. The SLD file must be present. A missing |
StyledFeatureCollectionInterface styledFC) { |
693 |
* attribute meta-data file is tolerated. |
return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(styledFC |
694 |
* @param geoObjectURL URL of the (already read) feature object |
.getStyle(), styledFC.getAttributeMetaDataMap()); |
695 |
* @param sldExt file extention for the SLD file |
} |
696 |
* @param rldExt file extention for the raster legend-data file |
|
697 |
* @return {@code null} in case of any error |
/** |
698 |
*/ |
* Loads a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData |
699 |
public static StyledLayerStyle<Map<Integer,AttributeMetaData>> loadStyledFeatureStyle(URL geoObjectURL) { |
* Raster-LegendData} for a given geo-object (raster) source. The SLD file |
700 |
return loadStyledFeatureStyle(geoObjectURL, "sld", "amd"); |
* must be present. A missing raster legend-data file is tolerated. |
701 |
} |
* |
702 |
|
* @param geoObjectURL |
703 |
/** |
* URL of the (already read) raster object |
704 |
* Stores a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData Raster-LegendData} |
* @param sldExt |
705 |
* for a given geo-object (raster) source. |
* file extention for the SLD file |
706 |
* @param style style to save |
* @param rldExt |
707 |
* @param geoObjectURL URL of the raster object |
* file extention for the raster legend-data file |
708 |
* @param sldExt file extention for the SLD file |
* @return {@code null} in case of any error |
709 |
* @param mdExt file extention for the meta-data file |
*/ |
710 |
*/ |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
711 |
public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style, URL geoObjectURL, String sldExt, String mdExt) throws Exception { |
URL geoObjectURL, String sldExt, String rldExt) { |
712 |
// Store the SLD |
RasterLegendData metaData = null; |
713 |
Style sldStyle = style.getGeoObjectStyle(); |
Style sldStyle = null; |
714 |
if ( sldStyle != null ) { |
try { |
715 |
StylingUtil.saveStyleToSLD( |
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
716 |
sldStyle, |
geoObjectURL, sldExt)); |
717 |
IOUtil.changeFileExt( |
// SLD must be present |
718 |
new File(geoObjectURL.toURI()), |
if (styles == null || styles.length == 0) |
719 |
sldExt |
return null; |
720 |
) |
sldStyle = styles[0]; |
721 |
); |
} catch (Exception err) { |
722 |
} |
// SLD must be present |
723 |
|
LangUtil.logDebugError(LOGGER, err); |
724 |
// Store the meta data |
return null; |
725 |
T metaData = style.getMetaData(); |
} |
726 |
if ( metaData != null ) { |
|
727 |
if ( metaData instanceof RasterLegendData ) { |
try { |
728 |
saveRasterLegendData( |
metaData = StyledLayerUtil.loadRasterLegendData(IOUtil |
729 |
(RasterLegendData)metaData, |
.changeUrlExt(geoObjectURL, rldExt)); |
730 |
IOUtil.changeUrlExt(geoObjectURL,mdExt) |
} catch (FileNotFoundException err) { |
731 |
); |
// ignore missing raster legend data |
732 |
// } else if ( metaData instanceof Map<Integer,AttributeMetaData> ) { // LEIDER NICHT KOMPILIERBAR!! |
} catch (Exception err) { |
733 |
} else if ( metaData instanceof Map ) { |
// any other error during legend data creation leads to error |
734 |
saveAttributeMetaDataMap( |
LangUtil.logDebugError(LOGGER, err); |
735 |
(Map<Integer,AttributeMetaData>)metaData, |
return null; |
736 |
IOUtil.changeUrlExt(geoObjectURL,mdExt) |
} |
737 |
); |
return new StyledLayerStyle<RasterLegendData>(sldStyle, metaData); |
738 |
} else |
} |
739 |
throw new UnsupportedOperationException("Export for meta data not yet supported: "+metaData.getClass().getSimpleName()); |
|
740 |
} |
/** |
741 |
} |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
742 |
|
* {@linkplain RasterLegendData Raster-LegendData} from a {@code .rld} file |
743 |
/** |
* for a given geo-object (raster) source. The SLD file must be present. A |
744 |
* Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and |
* missing raster legend-data file is tolerated. |
745 |
* the meta data ({@link RasterLegendData} or {@link AttributeMetaData}) |
* |
746 |
* to a {@code .rld} or {@code .amd} file. |
* @param geoObjectURL |
747 |
* for a given geo-object source. |
* URL of the (already read) raster object |
748 |
* @param style style to save |
* @param sldExt |
749 |
* @param geoObjectURL URL of the (already read) raster object |
* file extention for the SLD file |
750 |
*/ |
* @param rldExt |
751 |
public static void saveStyledLayerStyle(StyledLayerStyle<?> style, URL geoObjectURL) throws Exception { |
* file extention for the raster legend-data file |
752 |
if ( style.getMetaData() instanceof RasterLegendData ) |
* @return {@code null} in case of any error |
753 |
saveStyledLayerStyle(style,geoObjectURL, "sld", "rld"); |
*/ |
754 |
else |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
755 |
saveStyledLayerStyle(style,geoObjectURL, "sld", "amd"); |
URL geoObjectURL) { |
756 |
} |
return loadStyledRasterStyle(geoObjectURL, "sld", "rld"); |
757 |
|
} |
758 |
|
|
759 |
|
/** |
760 |
|
* Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData |
761 |
|
* AttributeMetaData-Map} for a given geo-object (feature) source. The SLD |
762 |
|
* file must be present. A missing attribute meta-data file is tolerated. |
763 |
|
* |
764 |
|
* @param geoObjectURL |
765 |
|
* URL of the (already read) feature object |
766 |
|
* @param sldExt |
767 |
|
* file extention for the SLD file |
768 |
|
* @param rldExt |
769 |
|
* file extention for the raster legend-data file |
770 |
|
* @return {@code null} in case of any error |
771 |
|
*/ |
772 |
|
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle( |
773 |
|
URL geoObjectURL, String sldExt, String rldExt) { |
774 |
|
Map<Integer, AttributeMetaData> metaData = null; |
775 |
|
Style sldStyle = null; |
776 |
|
try { |
777 |
|
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
778 |
|
geoObjectURL, sldExt)); |
779 |
|
// SLD must be present |
780 |
|
if (styles == null || styles.length == 0) |
781 |
|
return null; |
782 |
|
sldStyle = styles[0]; |
783 |
|
} catch (Exception err) { |
784 |
|
// SLD must be present |
785 |
|
LangUtil.logDebugError(LOGGER, err); |
786 |
|
return null; |
787 |
|
} |
788 |
|
|
789 |
|
try { |
790 |
|
metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil |
791 |
|
.changeUrlExt(geoObjectURL, rldExt)); |
792 |
|
} catch (FileNotFoundException err) { |
793 |
|
// ignore missing attribute meta data |
794 |
|
} catch (Exception err) { |
795 |
|
// any other error during meta data creation leads to error |
796 |
|
LangUtil.logDebugError(LOGGER, err); |
797 |
|
return null; |
798 |
|
} |
799 |
|
|
800 |
|
return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(sldStyle, |
801 |
|
metaData); |
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
|
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
806 |
|
* {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd} |
807 |
|
* file for a given geo-object (feature) source. The SLD file must be |
808 |
|
* present. A missing attribute meta-data file is tolerated. |
809 |
|
* |
810 |
|
* @param geoObjectURL |
811 |
|
* URL of the (already read) feature object |
812 |
|
* @param sldExt |
813 |
|
* file extention for the SLD file |
814 |
|
* @param rldExt |
815 |
|
* file extention for the raster legend-data file |
816 |
|
* @return {@code null} in case of any error |
817 |
|
*/ |
818 |
|
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle( |
819 |
|
URL geoObjectURL) { |
820 |
|
return loadStyledFeatureStyle(geoObjectURL, "sld", "amd"); |
821 |
|
} |
822 |
|
|
823 |
|
/** |
824 |
|
* Stores a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData |
825 |
|
* Raster-LegendData} for a given geo-object (raster) source. |
826 |
|
* |
827 |
|
* @param style |
828 |
|
* style to save |
829 |
|
* @param geoObjectURL |
830 |
|
* URL of the raster object |
831 |
|
* @param sldExt |
832 |
|
* file extention for the SLD file |
833 |
|
* @param mdExt |
834 |
|
* file extention for the meta-data file |
835 |
|
*/ |
836 |
|
public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style, |
837 |
|
URL geoObjectURL, String sldExt, String mdExt) throws Exception { |
838 |
|
// Store the SLD |
839 |
|
Style sldStyle = style.getGeoObjectStyle(); |
840 |
|
if (sldStyle != null) { |
841 |
|
StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File( |
842 |
|
geoObjectURL.toURI()), sldExt)); |
843 |
|
} |
844 |
|
|
845 |
|
// Store the meta data |
846 |
|
T metaData = style.getMetaData(); |
847 |
|
if (metaData != null) { |
848 |
|
if (metaData instanceof RasterLegendData) { |
849 |
|
saveRasterLegendData((RasterLegendData) metaData, IOUtil |
850 |
|
.changeUrlExt(geoObjectURL, mdExt)); |
851 |
|
// } else if ( metaData instanceof |
852 |
|
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
853 |
|
// KOMPILIERBAR!! |
854 |
|
} else if (metaData instanceof Map) { |
855 |
|
saveAttributeMetaDataMap( |
856 |
|
(Map<Integer, AttributeMetaData>) metaData, IOUtil |
857 |
|
.changeUrlExt(geoObjectURL, mdExt)); |
858 |
|
} else |
859 |
|
throw new UnsupportedOperationException( |
860 |
|
"Export for meta data not yet supported: " |
861 |
|
+ metaData.getClass().getSimpleName()); |
862 |
|
} |
863 |
|
} |
864 |
|
|
865 |
|
/** |
866 |
|
* Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and the |
867 |
|
* meta data ({@link RasterLegendData} or {@link AttributeMetaData}) to a |
868 |
|
* {@code .rld} or {@code .amd} file. for a given geo-object source. |
869 |
|
* |
870 |
|
* @param style |
871 |
|
* style to save |
872 |
|
* @param geoObjectURL |
873 |
|
* URL of the (already read) raster object |
874 |
|
*/ |
875 |
|
public static void saveStyledLayerStyle(StyledLayerStyle<?> style, |
876 |
|
URL geoObjectURL) throws Exception { |
877 |
|
if (style.getMetaData() instanceof RasterLegendData) |
878 |
|
saveStyledLayerStyle(style, geoObjectURL, "sld", "rld"); |
879 |
|
else |
880 |
|
saveStyledLayerStyle(style, geoObjectURL, "sld", "amd"); |
881 |
|
} |
882 |
|
|
883 |
|
/** |
884 |
|
* Creates a {@link Box} that shows a legend for a list of |
885 |
|
* {@link FeatureTypeStyle}s and a targeted featureType |
886 |
|
* |
887 |
|
* @param featureType |
888 |
|
* If this a legend for Point, Polygon or Line? |
889 |
|
* @param list |
890 |
|
* The Styles to presented in this legend |
891 |
|
* |
892 |
|
* @author <a href="mailto:[email protected]">Stefan Alfons |
893 |
|
* Krüger</a> |
894 |
|
*/ |
895 |
|
public static Box createLegendPanel(List<FeatureTypeStyle> list, |
896 |
|
SimpleFeatureType featureType, int iconWidth, int iconHeight) { |
897 |
|
|
898 |
|
Box box = new Box(BoxLayout.Y_AXIS) { |
899 |
|
|
900 |
|
/** |
901 |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot |
902 |
|
* gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS |
903 |
|
* gesetzt. |
904 |
|
* |
905 |
|
* @author <a href="mailto:[email protected]">Stefan Alfons |
906 |
|
* Krüger</a> |
907 |
|
*/ |
908 |
|
@Override |
909 |
|
public void print(Graphics g) { |
910 |
|
final Color orig = getBackground(); |
911 |
|
setBackground(Color.WHITE); |
912 |
|
// wrap in try/finally so that we always restore the state |
913 |
|
try { |
914 |
|
super.print(g); |
915 |
|
} finally { |
916 |
|
setBackground(orig); |
917 |
|
} |
918 |
|
} |
919 |
|
}; |
920 |
|
|
921 |
|
for (FeatureTypeStyle ftStyle : list) { |
922 |
|
|
923 |
|
// One child-node for every rule |
924 |
|
List<Rule> rules = ftStyle.rules(); |
925 |
|
for (Rule rule : rules) { |
926 |
|
|
927 |
|
/** |
928 |
|
* Let's not create a hbox for Rules that only contain |
929 |
|
* TextSymbolizers |
930 |
|
*/ |
931 |
|
if (StylingUtil.getTextSymbolizers(rule.getSymbolizers()) |
932 |
|
.size() == rule.getSymbolizers().length) |
933 |
|
continue; |
934 |
|
|
935 |
|
Box hbox = new Box(BoxLayout.X_AXIS) { |
936 |
|
|
937 |
|
/** |
938 |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein |
939 |
|
* Screenshot gemacht wird) wird. Dann werden wird der |
940 |
|
* Hintergrund auf WEISS gesetzt. |
941 |
|
*/ |
942 |
|
@Override |
943 |
|
public void print(Graphics g) { |
944 |
|
final Color orig = getBackground(); |
945 |
|
setBackground(Color.WHITE); |
946 |
|
// wrap in try/finally so that we always restore the |
947 |
|
// state |
948 |
|
try { |
949 |
|
super.print(g); |
950 |
|
} finally { |
951 |
|
setBackground(orig); |
952 |
|
} |
953 |
|
} |
954 |
|
}; |
955 |
|
|
956 |
|
/** |
957 |
|
* The size of the legend Symbol is dependent on the size of the |
958 |
|
* font. |
959 |
|
*/ |
960 |
|
final int fontHeight = new JLabel().getFontMetrics( |
961 |
|
new JLabel().getFont()).getHeight(); |
962 |
|
|
963 |
|
final Dimension ICON_SIZE = new Dimension(iconWidth, |
964 |
|
fontHeight > 5 ? fontHeight : iconHeight); |
965 |
|
|
966 |
|
// **************************************************************************** |
967 |
|
// Create the actual icon |
968 |
|
// **************************************************************************** |
969 |
|
final BufferedImage imageForRule = LegendIconFeatureRenderer |
970 |
|
.getInstance().createImageForRule(rule, featureType, |
971 |
|
ICON_SIZE); |
972 |
|
|
973 |
|
// LOGGER.debug("Creating a new Legend Image for RUle name = |
974 |
|
// "+rule.getName()); |
975 |
|
|
976 |
|
ImageIcon legendIcon = new ImageIcon(imageForRule); |
977 |
|
|
978 |
|
final JLabel iconLabel = new JLabel(legendIcon); |
979 |
|
hbox.setAlignmentX(0f); |
980 |
|
hbox.add(iconLabel); |
981 |
|
hbox.add(Box.createHorizontalStrut(3)); |
982 |
|
|
983 |
|
// **************************************************************************** |
984 |
|
// The labeltext is read from the SLD. Its either the title |
985 |
|
// directly, or interpreted as OneLine Code |
986 |
|
// **************************************************************************** |
987 |
|
// final String rawText = |
988 |
|
// rule.getDescription().getTitle().toString(); |
989 |
|
final String rawText = rule.getDescription().getTitle() |
990 |
|
.toString(); |
991 |
|
|
992 |
|
Translation labelT = new Translation(); |
993 |
|
labelT.fromOneLine(rawText); |
994 |
|
|
995 |
|
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
996 |
|
hbox.add(classTitleLabel); |
997 |
|
classTitleLabel.setLabelFor(iconLabel); |
998 |
|
|
999 |
|
box.add(hbox); |
1000 |
|
|
1001 |
|
} |
1002 |
|
} |
1003 |
|
|
1004 |
|
return box; |
1005 |
|
} |
1006 |
|
|
1007 |
|
/** |
1008 |
|
* Creates a |
1009 |
|
* |
1010 |
|
* @param styledRaster |
1011 |
|
* @param iconHeight |
1012 |
|
* @param iconWidth |
1013 |
|
* @return |
1014 |
|
*/ |
1015 |
|
public static Box createLegendPanel(StyledRasterInterface<?> styledRaster, |
1016 |
|
int iconWidth, int iconHeight) { |
1017 |
|
|
1018 |
|
/** |
1019 |
|
* Determine whether a Style is responsible for the coloring |
1020 |
|
*/ |
1021 |
|
ColorModel colorModel = null; |
1022 |
|
if (!isStyleable(styledRaster) |
1023 |
|
|| (isStyleable(styledRaster) && styledRaster.getStyle() == null)) { |
1024 |
|
colorModel = getColorModel(styledRaster); |
1025 |
|
} |
1026 |
|
|
1027 |
|
RasterLegendData rasterLegendData = styledRaster.getLegendMetaData(); |
1028 |
|
List<Double> legendRasterValues = rasterLegendData.getSortedKeys(); |
1029 |
|
Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
1030 |
|
.createSampleRasters(); |
1031 |
|
|
1032 |
|
Box box = new Box(BoxLayout.Y_AXIS) { |
1033 |
|
|
1034 |
|
/** |
1035 |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot |
1036 |
|
* gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS |
1037 |
|
* gesetzt. |
1038 |
|
*/ |
1039 |
|
@Override |
1040 |
|
public void print(Graphics g) { |
1041 |
|
final Color orig = getBackground(); |
1042 |
|
setBackground(Color.WHITE); |
1043 |
|
// wrap in try/finally so that we always restore the state |
1044 |
|
try { |
1045 |
|
super.print(g); |
1046 |
|
} finally { |
1047 |
|
setBackground(orig); |
1048 |
|
} |
1049 |
|
} |
1050 |
|
}; |
1051 |
|
|
1052 |
|
for (Double rValue : legendRasterValues) { |
1053 |
|
|
1054 |
|
/** |
1055 |
|
* |
1056 |
|
*/ |
1057 |
|
Box hbox = new Box(BoxLayout.X_AXIS) { |
1058 |
|
|
1059 |
|
/** |
1060 |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein |
1061 |
|
* Screenshot gemacht wird) wird. Dann werden wird der |
1062 |
|
* Hintergrund auf WEISS gesetzt. |
1063 |
|
*/ |
1064 |
|
@Override |
1065 |
|
public void print(Graphics g) { |
1066 |
|
final Color orig = getBackground(); |
1067 |
|
setBackground(Color.WHITE); |
1068 |
|
// wrap in try/finally so that we always restore the |
1069 |
|
// state |
1070 |
|
try { |
1071 |
|
super.print(g); |
1072 |
|
} finally { |
1073 |
|
setBackground(orig); |
1074 |
|
} |
1075 |
|
} |
1076 |
|
}; |
1077 |
|
|
1078 |
|
final Dimension ICON_SIZE = new Dimension(iconWidth, |
1079 |
|
new JLabel().getFontMetrics(new JLabel().getFont()) |
1080 |
|
.getHeight() > 5 ? new JLabel().getFontMetrics( |
1081 |
|
new JLabel().getFont()).getHeight() : iconHeight); |
1082 |
|
|
1083 |
|
// **************************************************************************** |
1084 |
|
// Create the actual icon |
1085 |
|
// **************************************************************************** |
1086 |
|
BufferedImage buffImage = new BufferedImage(ICON_SIZE.width, |
1087 |
|
ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB); |
1088 |
|
Graphics2D graphics = buffImage.createGraphics(); |
1089 |
|
|
1090 |
|
if (colorModel != null) { |
1091 |
|
|
1092 |
|
try { |
1093 |
|
Object inData = null; |
1094 |
|
switch (colorModel.getTransferType()) { |
1095 |
|
case DataBuffer.TYPE_BYTE: |
1096 |
|
inData = new byte[] { rValue.byteValue() }; |
1097 |
|
break; |
1098 |
|
case DataBuffer.TYPE_USHORT: |
1099 |
|
inData = new short[] { rValue.shortValue() }; |
1100 |
|
break; |
1101 |
|
case DataBuffer.TYPE_INT: |
1102 |
|
inData = new int[] { rValue.intValue() }; |
1103 |
|
break; |
1104 |
|
case DataBuffer.TYPE_SHORT: |
1105 |
|
inData = new short[] { rValue.shortValue() }; |
1106 |
|
break; |
1107 |
|
case DataBuffer.TYPE_FLOAT: |
1108 |
|
inData = new float[] { rValue.floatValue() }; |
1109 |
|
break; |
1110 |
|
case DataBuffer.TYPE_DOUBLE: |
1111 |
|
inData = new double[] { rValue.doubleValue() }; |
1112 |
|
break; |
1113 |
|
default: |
1114 |
|
inData = rValue.intValue(); |
1115 |
|
} |
1116 |
|
final Color color = new Color(colorModel.getRGB(inData)); |
1117 |
|
graphics.setBackground(color); |
1118 |
|
graphics.setColor(color); |
1119 |
|
graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height); |
1120 |
|
} catch (Exception e) { |
1121 |
|
LOGGER.debug("Dann nehmen wir halt den GridCoverageRenderer", e); |
1122 |
|
colorModel = null; |
1123 |
|
} |
1124 |
|
} |
1125 |
|
|
1126 |
|
if (colorModel == null) { |
1127 |
|
|
1128 |
|
/** |
1129 |
|
* The coverage contains only one value of value rValue |
1130 |
|
*/ |
1131 |
|
GridCoverage2D sampleCov = sampleRasters.get(rValue); |
1132 |
|
GridCoverageRenderer renderer; |
1133 |
|
try { |
1134 |
|
renderer = new GridCoverageRenderer(sampleCov |
1135 |
|
.getCoordinateReferenceSystem(), JTSUtil |
1136 |
|
.createEnvelope(sampleCov.getEnvelope()), |
1137 |
|
new Rectangle(iconWidth, iconHeight), |
1138 |
|
(AffineTransform) null); |
1139 |
|
} catch (Exception e1) { |
1140 |
|
throw new RuntimeException( |
1141 |
|
"Creating the GridCoverageRenderer:", e1); |
1142 |
|
} |
1143 |
|
|
1144 |
|
/** |
1145 |
|
* Iterate over all FeatureTypeStyles. |
1146 |
|
*/ |
1147 |
|
// for (FeatureTypeStyle ftStyle : styledGrid.getStyle() |
1148 |
|
// .featureTypeStyles()) { |
1149 |
|
// One child-node for every rule |
1150 |
|
// List<Rule> rules = ftStyle.rules(); |
1151 |
|
// for (Rule rule : rules) { |
1152 |
|
final Style style = styledRaster.getStyle(); |
1153 |
|
List<RasterSymbolizer> rSymbols = StylingUtil |
1154 |
|
.getRasterSymbolizers(style); |
1155 |
|
|
1156 |
|
for (RasterSymbolizer symbolizer : rSymbols) { |
1157 |
|
// LOGGER.debug("Creating a new Legend Image for RUle |
1158 |
|
// name = |
1159 |
|
// "+rule.getName()); |
1160 |
|
try { |
1161 |
|
renderer.paint(graphics, sampleCov, symbolizer); |
1162 |
|
} catch (Exception ee) { |
1163 |
|
LOGGER.error("Unable to paint " + symbolizer |
1164 |
|
+ " into the legend image", ee); |
1165 |
|
} |
1166 |
|
// } |
1167 |
|
// } |
1168 |
|
} |
1169 |
|
} |
1170 |
|
|
1171 |
|
ImageIcon legendIcon = new ImageIcon(buffImage); |
1172 |
|
|
1173 |
|
final JLabel iconLabel = new JLabel(legendIcon); |
1174 |
|
hbox.setAlignmentX(0f); |
1175 |
|
hbox.add(iconLabel); |
1176 |
|
hbox.add(Box.createHorizontalStrut(3)); |
1177 |
|
|
1178 |
|
Translation labelT = rasterLegendData.get(rValue); |
1179 |
|
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
1180 |
|
hbox.add(classTitleLabel); |
1181 |
|
classTitleLabel.setLabelFor(iconLabel); |
1182 |
|
|
1183 |
|
box.add(hbox); |
1184 |
|
|
1185 |
|
if (rasterLegendData.getPaintGaps()) { |
1186 |
|
iconLabel |
1187 |
|
.setBorder(BorderFactory.createLineBorder(Color.black)); |
1188 |
|
box.add(Box.createVerticalStrut(3)); |
1189 |
|
} |
1190 |
|
|
1191 |
|
} |
1192 |
|
|
1193 |
|
return box; |
1194 |
|
} |
1195 |
|
|
1196 |
|
/** |
1197 |
|
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
1198 |
|
* return <code>null</code> if the geoobject can not be accessed. |
1199 |
|
*/ |
1200 |
|
public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) { |
1201 |
|
ColorModel colorModel = null; |
1202 |
|
try { |
1203 |
|
Object geoObject = styledGrid.getGeoObject(); |
1204 |
|
if (geoObject instanceof GridCoverage2D) { |
1205 |
|
GridCoverage2D cov = (GridCoverage2D) geoObject; |
1206 |
|
colorModel = cov.getRenderedImage().getColorModel(); |
1207 |
|
} else if (geoObject instanceof ImagePyramidReader) { |
1208 |
|
|
1209 |
|
Parameter readGG = new Parameter( |
1210 |
|
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
1211 |
|
|
1212 |
|
ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope( |
1213 |
|
styledGrid.getEnvelope(), styledGrid.getCrs()); |
1214 |
|
|
1215 |
|
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
1216 |
|
new Rectangle(0, 0, 10, 10)), mapExtend)); |
1217 |
|
|
1218 |
|
final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) geoObject; |
1219 |
|
GridCoverage2D cov = (GridCoverage2D) aReader |
1220 |
|
.read(new GeneralParameterValue[] { readGG }); |
1221 |
|
colorModel = cov.getRenderedImage().getColorModel(); |
1222 |
|
} |
1223 |
|
} catch (Exception e) { |
1224 |
|
LOGGER.error("Error reading the colormodel from " + styledGrid); |
1225 |
|
return null; |
1226 |
|
} |
1227 |
|
return colorModel; |
1228 |
|
} |
1229 |
|
|
1230 |
|
/** |
1231 |
|
* @return <code>true</code> if a {@link RasterSymbolizer} can be applied |
1232 |
|
* and will have an effect. Some rasters (e.g. GeoTIFF) can come |
1233 |
|
* with their own {@link ColorModel} and will ignore any |
1234 |
|
* {@link RasterSymbolizer} = SLD. |
1235 |
|
*/ |
1236 |
|
public static boolean isStyleable(StyledRasterInterface<?> styledRaster) { |
1237 |
|
ColorModel colorModel = getColorModel(styledRaster); |
1238 |
|
|
1239 |
|
LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is " |
1240 |
|
+ colorModel.getClass().getSimpleName()); |
1241 |
|
|
1242 |
|
if (colorModel == null) |
1243 |
|
return true; |
1244 |
|
if (colorModel instanceof ComponentColorModel) |
1245 |
|
return true; |
1246 |
|
return false; |
1247 |
|
} |
1248 |
} |
} |