31 |
|
|
32 |
import java.awt.Color; |
import java.awt.Color; |
33 |
import java.awt.Dimension; |
import java.awt.Dimension; |
|
import java.awt.Graphics; |
|
34 |
import java.awt.Graphics2D; |
import java.awt.Graphics2D; |
35 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
36 |
import java.awt.geom.AffineTransform; |
import java.awt.geom.AffineTransform; |
43 |
import java.io.FileWriter; |
import java.io.FileWriter; |
44 |
import java.net.URL; |
import java.net.URL; |
45 |
import java.text.DecimalFormat; |
import java.text.DecimalFormat; |
|
import java.util.HashMap; |
|
46 |
import java.util.List; |
import java.util.List; |
47 |
import java.util.Map; |
import java.util.Map; |
|
import java.util.SortedMap; |
|
|
import java.util.TreeMap; |
|
48 |
|
|
49 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
|
import javax.swing.Box; |
|
|
import javax.swing.BoxLayout; |
|
50 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
51 |
|
import javax.swing.JComponent; |
52 |
import javax.swing.JLabel; |
import javax.swing.JLabel; |
53 |
|
|
54 |
|
import net.miginfocom.swing.MigLayout; |
55 |
|
|
56 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
57 |
import org.geotools.coverage.grid.GeneralGridEnvelope; |
import org.geotools.coverage.grid.GeneralGridEnvelope; |
58 |
import org.geotools.coverage.grid.GridCoverage2D; |
import org.geotools.coverage.grid.GridCoverage2D; |
60 |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
61 |
import org.geotools.coverage.grid.io.AbstractGridFormat; |
import org.geotools.coverage.grid.io.AbstractGridFormat; |
62 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
63 |
import org.geotools.gce.imagepyramid.ImagePyramidReader; |
import org.geotools.feature.NameImpl; |
64 |
import org.geotools.geometry.jts.ReferencedEnvelope; |
import org.geotools.geometry.jts.ReferencedEnvelope; |
65 |
import org.geotools.map.DefaultMapLayer; |
import org.geotools.map.DefaultMapLayer; |
66 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
76 |
import org.jdom.Element; |
import org.jdom.Element; |
77 |
import org.jdom.input.SAXBuilder; |
import org.jdom.input.SAXBuilder; |
78 |
import org.jdom.output.XMLOutputter; |
import org.jdom.output.XMLOutputter; |
79 |
|
import org.opengis.feature.simple.SimpleFeature; |
80 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
81 |
|
import org.opengis.feature.type.Name; |
82 |
import org.opengis.parameter.GeneralParameterValue; |
import org.opengis.parameter.GeneralParameterValue; |
83 |
|
|
84 |
import schmitzm.geotools.JTSUtil; |
import schmitzm.geotools.JTSUtil; |
85 |
|
import schmitzm.geotools.feature.FeatureUtil; |
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; |
89 |
|
import schmitzm.swing.JPanel; |
90 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
91 |
import skrueger.AttributeMetaData; |
import skrueger.AttributeMetadata; |
92 |
import skrueger.RasterLegendData; |
import skrueger.RasterLegendData; |
93 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
94 |
|
|
132 |
* if {@code null} is given as object or an error occurs |
* if {@code null} is given as object or an error occurs |
133 |
* during layer creation |
* during layer creation |
134 |
*/ |
*/ |
135 |
public static MapLayer createMapLayer(Object object) throws Exception { |
public static MapLayer createMapLayer(final Object object) throws Exception { |
136 |
return createMapLayer(object, null); |
return createMapLayer(object, null); |
137 |
} |
} |
138 |
|
|
151 |
* if {@code null} is given as object or an error occurs |
* if {@code null} is given as object or an error occurs |
152 |
* during layer creation |
* during layer creation |
153 |
*/ |
*/ |
154 |
public static MapLayer createMapLayer(Object object, Style forcedStyle) |
public static MapLayer createMapLayer(Object object, final Style forcedStyle) |
155 |
throws Exception { |
throws Exception { |
156 |
MapLayer layer = null; |
MapLayer layer = null; |
157 |
Style style = null; |
Style style = null; |
192 |
* if {@code null} is given as object or an error occurs |
* if {@code null} is given as object or an error occurs |
193 |
* during creation |
* during creation |
194 |
*/ |
*/ |
195 |
public static StyledLayerInterface<?> createStyledLayer(Object object, |
public static StyledLayerInterface<?> createStyledLayer( |
196 |
String title) { |
final Object object, final String title) { |
197 |
return createStyledLayer(object, title, null); |
return createStyledLayer(object, title, null); |
198 |
} |
} |
199 |
|
|
212 |
* if {@code null} is given as object or an error occurs |
* if {@code null} is given as object or an error occurs |
213 |
* during creation |
* during creation |
214 |
*/ |
*/ |
215 |
public static StyledLayerInterface<?> createStyledLayer(Object object, |
public static StyledLayerInterface<?> createStyledLayer( |
216 |
String title, StyledLayerStyle style) { |
final Object object, final String title, |
217 |
|
final StyledLayerStyle style) { |
218 |
StyledLayerInterface<?> styledLayer = null; |
StyledLayerInterface<?> styledLayer = null; |
219 |
|
|
220 |
String id = (title != null) ? title : "defaultID"; |
final String id = (title != null) ? title : "defaultID"; |
221 |
|
|
222 |
if (object instanceof GridCoverage2D) |
if (object instanceof GridCoverage2D) |
223 |
styledLayer = new StyledGridCoverage((GridCoverage2D) object, id, |
styledLayer = new StyledGridCoverage((GridCoverage2D) object, id, |
245 |
* @param visible |
* @param visible |
246 |
* indicated whether the visible or invisible entries are |
* indicated whether the visible or invisible entries are |
247 |
* returned |
* returned |
248 |
|
* |
249 |
|
* TODO replace with {@link AttributeMetadataMap#sortedValuesVisibleOnly()} |
250 |
*/ |
*/ |
251 |
public static SortedMap<Integer, AttributeMetaData> getVisibleAttributeMetaData( |
public static AttributeMetadataMap getVisibleAttributeMetaData( |
252 |
Map<Integer, AttributeMetaData> amdMap, boolean visible) { |
final AttributeMetadataMap amdMap, final boolean visible) { |
253 |
SortedMap<Integer, AttributeMetaData> filteredMap = new TreeMap<Integer, AttributeMetaData>(); |
|
254 |
for (AttributeMetaData amd : amdMap.values()) |
final AttributeMetadataMap filteredMap = new AttributeMetadataMap(amdMap.getLanguages()); |
255 |
if (amd.isVisible()) |
for (final AttributeMetadata amd : amdMap.values()) |
256 |
filteredMap.put(amd.getColIdx(), amd); |
if (amd.isVisible() == visible) |
257 |
|
filteredMap.put(amd.getName(), amd); |
258 |
|
|
259 |
return filteredMap; |
return filteredMap; |
260 |
} |
} |
261 |
|
|
262 |
/** |
/** |
263 |
* Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}. |
* Parses a {@link AttributeMetadata} object from an JDOM-{@link Element}. |
264 |
* This method works like {@link |
* This method works like {@link |
265 |
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
266 |
* |
* |
267 |
|
* TODO 20.11.2009, SK: There are some new attribute weight, functiona, |
268 |
|
* functionX and nodata in AttributeMetaData that should be parsed/exported |
269 |
|
* too. but this method is only used by ISDSS, which is not supporting that |
270 |
|
* stuff anyways. |
271 |
|
* |
272 |
* @param element |
* @param element |
273 |
* {@link Element} to parse |
* {@link Element} to parse |
274 |
*/ |
*/ |
275 |
public static AttributeMetaData parseAttributeMetaData(final Element element) { |
public static AttributeMetadata parseAttributeMetaData(final Element element) { |
276 |
final Integer col = Integer.valueOf(element.getAttributeValue("col")); |
final String namespace = String.valueOf(element |
277 |
|
.getAttributeValue("namespace")); |
278 |
|
final String localname = String.valueOf(element |
279 |
|
.getAttributeValue("localname")); |
280 |
|
final Name aName = new NameImpl(namespace, localname); |
281 |
final Boolean visible = Boolean.valueOf(element |
final Boolean visible = Boolean.valueOf(element |
282 |
.getAttributeValue("visible")); |
.getAttributeValue("visible")); |
283 |
final String unit = element.getAttributeValue("unit"); |
final String unit = element.getAttributeValue("unit"); |
293 |
else if (childElement.getName().equals("desc")) |
else if (childElement.getName().equals("desc")) |
294 |
desc = parseTranslation(childElement); |
desc = parseTranslation(childElement); |
295 |
} |
} |
296 |
return new AttributeMetaData(col, visible, name, desc, unit); |
return new AttributeMetadata(aName, visible, name, desc, unit); |
297 |
} |
} |
298 |
|
|
299 |
/** |
/** |
300 |
* Parses a {@link AttributeMetaData} map from an JDOM-{@link Element} with |
* Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with |
301 |
* {@code <attribute>}-childs. |
* {@code <attribute>}-childs. |
302 |
* |
* |
303 |
* @param element |
* @param element |
304 |
* {@link Element} to parse |
* {@link Element} to parse |
305 |
*/ |
*/ |
306 |
public static Map<Integer, AttributeMetaData> parseAttributeMetaDataMap( |
public static AttributeMetadataMap parseAttributeMetaDataMap( |
307 |
final Element element) { |
final Element element) { |
308 |
HashMap<Integer, AttributeMetaData> metaData = new HashMap<Integer, AttributeMetaData>(); |
final AttributeMetadataMap metaData = new AttributeMetadataMap(); |
309 |
List<Element> attributesElements = element |
final List<Element> attributesElements = element |
310 |
.getChildren(ELEM_NAME_ATTRIBUTE); |
.getChildren(ELEM_NAME_ATTRIBUTE); |
311 |
for (Element attibuteElement : attributesElements) { |
for (final Element attibuteElement : attributesElements) { |
312 |
AttributeMetaData attrMetaData = parseAttributeMetaData(attibuteElement); |
final AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement); |
313 |
metaData.put(attrMetaData.getColIdx(), attrMetaData); |
metaData.put(attrMetaData.getName(), attrMetaData); |
314 |
} |
} |
315 |
return metaData; |
return metaData; |
316 |
} |
} |
317 |
|
|
318 |
/** |
/** |
319 |
* Loads a {@link AttributeMetaData} object from an URL. |
* Loads a {@link AttributeMetadata} object from an URL. |
320 |
* |
* |
321 |
* @param documentUrl |
* @param documentUrl |
322 |
* {@link URL} to parse |
* {@link URL} to parse |
323 |
* @see #parseAttributeMetaData(Element) |
* @see #parseAttributeMetaData(Element) |
324 |
*/ |
*/ |
325 |
public static Map<Integer, AttributeMetaData> loadAttributeMetaDataMap( |
public static AttributeMetadataMap loadAttributeMetaDataMap( |
326 |
final URL documentUrl) throws Exception { |
final URL documentUrl) throws Exception { |
327 |
Document document = SAX_BUILDER.build(documentUrl); |
final Document document = SAX_BUILDER.build(documentUrl); |
328 |
return parseAttributeMetaDataMap(document.getRootElement()); |
return parseAttributeMetaDataMap(document.getRootElement()); |
329 |
} |
} |
330 |
|
|
331 |
/** |
/** |
332 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
* Creates an JDOM {@link Element} for the given {@link AttributeMetadata} |
333 |
* object. |
* object. |
334 |
* |
* |
335 |
* @param amd |
* @param amd |
336 |
* meta data for one attribute |
* meta data for one attribute |
337 |
*/ |
*/ |
338 |
public static Element createAttributeMetaDataElement( |
public static Element createAttributeMetaDataElement( |
339 |
final AttributeMetaData amd) { |
final AttributeMetadata amd) { |
340 |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
341 |
element.setAttribute("col", String.valueOf(amd.getColIdx())); |
element.setAttribute("namespace", String.valueOf(amd.getName() |
342 |
|
.getNamespaceURI())); |
343 |
|
element.setAttribute("localname", String.valueOf(amd.getLocalName())); |
344 |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
345 |
element.setAttribute("unit", amd.getUnit()); |
element.setAttribute("unit", amd.getUnit()); |
346 |
// Creating a aml:name tag... |
// Creating a aml:name tag... |
351 |
} |
} |
352 |
|
|
353 |
/** |
/** |
354 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
* Creates an JDOM {@link Element} for the given {@link AttributeMetadata} |
355 |
* map. |
* map. |
356 |
* |
* |
357 |
* @param amdMap |
* @param amdMap |
358 |
* map of attribute meta data |
* map of attribute meta data |
359 |
*/ |
*/ |
360 |
public static Element createAttributeMetaDataMapElement( |
public static Element createAttributeMetaDataMapElement( |
361 |
final Map<Integer, AttributeMetaData> amdMap) { |
final AttributeMetadataMap amdMap) { |
362 |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
363 |
for (AttributeMetaData amd : amdMap.values()) |
for (final AttributeMetadata amd : amdMap.values()) |
364 |
element.addContent(createAttributeMetaDataElement(amd)); |
element.addContent(createAttributeMetaDataElement(amd)); |
365 |
return element; |
return element; |
366 |
} |
} |
367 |
|
|
368 |
/** |
/** |
369 |
* Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL. |
* Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL. |
370 |
* |
* |
371 |
* @param amdMap |
* @param amdMap |
372 |
* map of {@link AttributeMetaData} |
* map of {@link AttributeMetadata} |
373 |
* @param documentUrl |
* @param documentUrl |
374 |
* {@link URL} to store the XML |
* {@link URL} to store the XML |
375 |
*/ |
*/ |
376 |
public static void saveAttributeMetaDataMap( |
public static void saveAttributeMetaDataMap( |
377 |
final Map<Integer, AttributeMetaData> amdMap, final URL documentUrl) |
final AttributeMetadataMap amdMap, final URL documentUrl) |
378 |
throws Exception { |
throws Exception { |
379 |
// Create XML-Document |
// Create XML-Document |
380 |
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
391 |
* @param element |
* @param element |
392 |
* {@link Element} to parse |
* {@link Element} to parse |
393 |
*/ |
*/ |
394 |
public static RasterLegendData parseRasterLegendData(Element element) { |
public static RasterLegendData parseRasterLegendData(final Element element) { |
395 |
|
|
396 |
final boolean paintGaps = Boolean.valueOf(element |
final boolean paintGaps = Boolean.valueOf(element |
397 |
.getAttributeValue("paintGaps")); |
.getAttributeValue("paintGaps")); |
398 |
|
|
399 |
RasterLegendData rld = new RasterLegendData(paintGaps); |
final RasterLegendData rld = new RasterLegendData(paintGaps); |
400 |
|
|
401 |
for (Element childElement : (List<Element>) element.getChildren()) { |
for (final Element childElement : (List<Element>) element.getChildren()) { |
402 |
final String name = childElement.getName(); |
final String name = childElement.getName(); |
403 |
// Cancel if it's an attribute |
// Cancel if it's an attribute |
404 |
if (childElement.getChildren().size() == 0) |
if (childElement.getChildren().size() == 0) |
418 |
// id label element is missing, the translation is searched |
// id label element is missing, the translation is searched |
419 |
// directly |
// directly |
420 |
// as childs of the rasterLegendItem element |
// as childs of the rasterLegendItem element |
421 |
Translation label = parseTranslation(labelElement != null ? labelElement |
final Translation label = parseTranslation(labelElement != null ? labelElement |
422 |
: childElement); |
: childElement); |
423 |
rld.put(value, label); |
rld.put(value, label); |
424 |
} |
} |
436 |
*/ |
*/ |
437 |
public static RasterLegendData loadRasterLegendData(final URL documentUrl) |
public static RasterLegendData loadRasterLegendData(final URL documentUrl) |
438 |
throws Exception { |
throws Exception { |
439 |
Document document = SAX_BUILDER.build(documentUrl); |
final Document document = SAX_BUILDER.build(documentUrl); |
440 |
return parseRasterLegendData(document.getRootElement()); |
return parseRasterLegendData(document.getRootElement()); |
441 |
} |
} |
442 |
|
|
451 |
final RasterLegendData rld) { |
final RasterLegendData rld) { |
452 |
final Element element = new Element(ELEM_NAME_RLD, AMLURI); |
final Element element = new Element(ELEM_NAME_RLD, AMLURI); |
453 |
element.setAttribute("paintGaps", rld.isPaintGaps().toString()); |
element.setAttribute("paintGaps", rld.isPaintGaps().toString()); |
454 |
for (Double key : rld.getSortedKeys()) { |
for (final Double key : rld.getSortedKeys()) { |
455 |
Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI); |
final Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI); |
456 |
item.setAttribute("value", key.toString()); |
item.setAttribute("value", key.toString()); |
457 |
item.addContent(createTranslationElement("label", rld.get(key))); |
item.addContent(createTranslationElement("label", rld.get(key))); |
458 |
element.addContent(item); |
element.addContent(item); |
472 |
* rounded to (null means no round; >= 0 means digits after |
* rounded to (null means no round; >= 0 means digits after |
473 |
* comma; < 0 means digits before comma) |
* comma; < 0 means digits before comma) |
474 |
*/ |
*/ |
475 |
public static RasterLegendData generateRasterLegendData(ColorMap colorMap, |
public static RasterLegendData generateRasterLegendData( |
476 |
boolean paintGaps, Integer digits) { |
final ColorMap colorMap, final boolean paintGaps, |
477 |
DecimalFormat decFormat = digits != null ? new DecimalFormat(SwingUtil |
final Integer digits) { |
478 |
.getNumberFormatPattern(digits)) : null; |
final DecimalFormat decFormat = digits != null ? new DecimalFormat( |
479 |
RasterLegendData rld = new RasterLegendData(paintGaps); |
SwingUtil.getNumberFormatPattern(digits)) : null; |
480 |
for (ColorMapEntry cme : colorMap.getColorMapEntries()) { |
final RasterLegendData rld = new RasterLegendData(paintGaps); |
481 |
double value = StylingUtil.getQuantityFromColorMapEntry(cme); |
for (final ColorMapEntry cme : colorMap.getColorMapEntries()) { |
482 |
|
final double value = StylingUtil.getQuantityFromColorMapEntry(cme); |
483 |
String label = cme.getLabel(); |
String label = cme.getLabel(); |
484 |
// if no label is set (e.g. quantitative style), |
// if no label is set (e.g. quantitative style), |
485 |
// use the value as label |
// use the value as label |
505 |
* rounded to (null means no round; >= 0 means digits after |
* rounded to (null means no round; >= 0 means digits after |
506 |
* comma; < 0 means digits before comma) |
* comma; < 0 means digits before comma) |
507 |
*/ |
*/ |
508 |
public static RasterLegendData generateRasterLegendData(Style style, |
public static RasterLegendData generateRasterLegendData(final Style style, |
509 |
boolean paintGaps, Integer digits) { |
final boolean paintGaps, final Integer digits) { |
510 |
ColorMap colorMap = StylingUtil.getColorMapFromStyle(style); |
final ColorMap colorMap = StylingUtil.getColorMapFromStyle(style); |
511 |
if (colorMap == null) |
if (colorMap == null) |
512 |
throw new IllegalArgumentException( |
throw new IllegalArgumentException( |
513 |
"Color map can not be determined from style!"); |
"Color map can not be determined from style!"); |
540 |
* {@link Element} to parse |
* {@link Element} to parse |
541 |
*/ |
*/ |
542 |
public final static Translation parseTranslation(final Element element) { |
public final static Translation parseTranslation(final Element element) { |
543 |
Translation trans = new Translation(); |
final Translation trans = new Translation(); |
544 |
|
|
545 |
if (element == null) |
if (element == null) |
546 |
return trans; |
return trans; |
582 |
* @param translation |
* @param translation |
583 |
* Translation to store in the Element |
* Translation to store in the Element |
584 |
*/ |
*/ |
585 |
public final static Element createTranslationElement(String tagname, |
public final static Element createTranslationElement(final String tagname, |
586 |
Translation translation) { |
final Translation translation) { |
587 |
Element element = new Element(tagname, AMLURI); |
final Element element = new Element(tagname, AMLURI); |
588 |
if (translation == null) |
if (translation == null) |
589 |
throw new UnsupportedOperationException( |
throw new UnsupportedOperationException( |
590 |
"Translation element can not be created from null!"); |
"Translation element can not be created from null!"); |
600 |
|
|
601 |
// add a <translation lang="..">..</tranlation> part to the element for |
// add a <translation lang="..">..</tranlation> part to the element for |
602 |
// all languages |
// all languages |
603 |
for (String lang : translation.keySet()) { |
for (final String lang : translation.keySet()) { |
604 |
Element translationElement = new Element(ELEM_NAME_TRANSLATION, |
final Element translationElement = new Element( |
605 |
AMLURI); |
ELEM_NAME_TRANSLATION, AMLURI); |
606 |
translationElement.setAttribute("lang", lang); |
translationElement.setAttribute("lang", lang); |
607 |
String translationString = translation.get(lang); |
String translationString = translation.get(lang); |
608 |
if (translationString == null) |
if (translationString == null) |
622 |
* @param style |
* @param style |
623 |
* a Style |
* a Style |
624 |
*/ |
*/ |
625 |
public static void setStyledLayerStyle(StyledLayerInterface styledObject, |
public static void setStyledLayerStyle( |
626 |
StyledLayerStyle<?> style) { |
final StyledLayerInterface styledObject, |
627 |
|
final StyledLayerStyle<?> style) { |
628 |
// set SLD style |
// set SLD style |
629 |
styledObject.setStyle(style.getGeoObjectStyle()); |
styledObject.setStyle(style.getGeoObjectStyle()); |
630 |
// set meta data |
// set meta data |
631 |
if (styledObject instanceof StyledGridCoverageInterface |
if (styledObject instanceof StyledGridCoverageInterface |
632 |
&& (style.getMetaData() instanceof RasterLegendData || style |
&& (style.getMetaData() instanceof RasterLegendData || style |
633 |
.getMetaData() == null)) { |
.getMetaData() == null)) { |
634 |
RasterLegendData sourceRld = (RasterLegendData) style.getMetaData(); |
final RasterLegendData sourceRld = (RasterLegendData) style |
635 |
RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject) |
.getMetaData(); |
636 |
|
final RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject) |
637 |
.getLegendMetaData(); |
.getLegendMetaData(); |
638 |
if (destRld != null && sourceRld != null) { |
if (destRld != null && sourceRld != null) { |
639 |
destRld.setPaintGaps(sourceRld.isPaintGaps()); |
destRld.setPaintGaps(sourceRld.isPaintGaps()); |
644 |
} |
} |
645 |
if (styledObject instanceof StyledFeatureCollectionInterface |
if (styledObject instanceof StyledFeatureCollectionInterface |
646 |
&& (style.getMetaData() instanceof Map || style.getMetaData() == null)) { |
&& (style.getMetaData() instanceof Map || style.getMetaData() == null)) { |
647 |
Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>) style |
final AttributeMetadataMap sourceAmd = (AttributeMetadataMap) style |
648 |
.getMetaData(); |
.getMetaData(); |
649 |
Map<Integer, AttributeMetaData> destAmd = ((StyledFeatureCollectionInterface) styledObject) |
final AttributeMetadataMap destAmd = ((StyledFeatureCollectionInterface) styledObject) |
650 |
.getAttributeMetaDataMap(); |
.getAttributeMetaDataMap(); |
651 |
if (destAmd != null && sourceAmd != null) { |
if (destAmd != null && sourceAmd != null) { |
652 |
destAmd.clear(); |
destAmd.clear(); |
676 |
* {@link StyledFeatureCollectionInterface} |
* {@link StyledFeatureCollectionInterface} |
677 |
*/ |
*/ |
678 |
public static StyledLayerStyle<?> getStyledLayerStyle( |
public static StyledLayerStyle<?> getStyledLayerStyle( |
679 |
StyledLayerInterface styledObject) { |
final StyledLayerInterface styledObject) { |
680 |
if (styledObject instanceof StyledGridCoverageInterface) |
if (styledObject instanceof StyledGridCoverageInterface) |
681 |
return getStyledLayerStyle((StyledGridCoverageInterface) styledObject); |
return getStyledLayerStyle((StyledGridCoverageInterface) styledObject); |
682 |
if (styledObject instanceof StyledFeatureCollectionInterface) |
if (styledObject instanceof StyledFeatureCollectionInterface) |
695 |
* a styled grid coverage |
* a styled grid coverage |
696 |
*/ |
*/ |
697 |
public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle( |
public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle( |
698 |
StyledGridCoverageInterface styledGC) { |
final StyledGridCoverageInterface styledGC) { |
699 |
return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(), |
return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(), |
700 |
styledGC.getLegendMetaData()); |
styledGC.getLegendMetaData()); |
701 |
} |
} |
707 |
* @param styledFC |
* @param styledFC |
708 |
* a styled feature collection |
* a styled feature collection |
709 |
*/ |
*/ |
710 |
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> getStyledLayerStyle( |
public static StyledLayerStyle<AttributeMetadataMap> getStyledLayerStyle( |
711 |
StyledFeatureCollectionInterface styledFC) { |
final StyledFeatureCollectionInterface styledFC) { |
712 |
return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(styledFC |
return new StyledLayerStyle<AttributeMetadataMap>(styledFC.getStyle(), |
713 |
.getStyle(), styledFC.getAttributeMetaDataMap()); |
styledFC.getAttributeMetaDataMap()); |
714 |
} |
} |
715 |
|
|
716 |
/** |
/** |
727 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
728 |
*/ |
*/ |
729 |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
730 |
URL geoObjectURL, String sldExt, String rldExt) { |
final URL geoObjectURL, final String sldExt, final String rldExt) { |
731 |
RasterLegendData metaData = null; |
RasterLegendData metaData = null; |
732 |
Style sldStyle = null; |
Style sldStyle = null; |
733 |
try { |
try { |
734 |
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
735 |
geoObjectURL, sldExt)); |
geoObjectURL, sldExt)); |
736 |
// SLD must be present |
// SLD must be present |
737 |
if (styles == null || styles.length == 0) |
if (styles == null || styles.length == 0) |
738 |
return null; |
return null; |
739 |
sldStyle = styles[0]; |
sldStyle = styles[0]; |
740 |
} catch (Exception err) { |
} catch (final Exception err) { |
741 |
// SLD must be present |
// SLD must be present |
742 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
743 |
return null; |
return null; |
746 |
try { |
try { |
747 |
metaData = StyledLayerUtil.loadRasterLegendData(IOUtil |
metaData = StyledLayerUtil.loadRasterLegendData(IOUtil |
748 |
.changeUrlExt(geoObjectURL, rldExt)); |
.changeUrlExt(geoObjectURL, rldExt)); |
749 |
} catch (FileNotFoundException err) { |
} catch (final FileNotFoundException err) { |
750 |
// ignore missing raster legend data |
// ignore missing raster legend data |
751 |
} catch (Exception err) { |
} catch (final Exception err) { |
752 |
// any other error during legend data creation leads to error |
// any other error during legend data creation leads to error |
753 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
754 |
return null; |
return null; |
771 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
772 |
*/ |
*/ |
773 |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
774 |
URL geoObjectURL) { |
final URL geoObjectURL) { |
775 |
return loadStyledRasterStyle(geoObjectURL, "sld", "rld"); |
return loadStyledRasterStyle(geoObjectURL, "sld", "rld"); |
776 |
} |
} |
777 |
|
|
778 |
/** |
/** |
779 |
* Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData |
* Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata |
780 |
* AttributeMetaData-Map} for a given geo-object (feature) source. The SLD |
* AttributeMetaData-Map} for a given geo-object (feature) source. The SLD |
781 |
* file must be present. A missing attribute meta-data file is tolerated. |
* file must be present. A missing attribute meta-data file is tolerated. |
782 |
* |
* |
788 |
* file extention for the raster legend-data file |
* file extention for the raster legend-data file |
789 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
790 |
*/ |
*/ |
791 |
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle( |
public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle( |
792 |
URL geoObjectURL, String sldExt, String rldExt) { |
final URL geoObjectURL, final String sldExt, final String rldExt) { |
793 |
Map<Integer, AttributeMetaData> metaData = null; |
AttributeMetadataMap metaData = null; |
794 |
Style sldStyle = null; |
Style sldStyle = null; |
795 |
try { |
try { |
796 |
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
797 |
geoObjectURL, sldExt)); |
geoObjectURL, sldExt)); |
798 |
// SLD must be present |
// SLD must be present |
799 |
if (styles == null || styles.length == 0) |
if (styles == null || styles.length == 0) |
800 |
return null; |
return null; |
801 |
sldStyle = styles[0]; |
sldStyle = styles[0]; |
802 |
} catch (Exception err) { |
} catch (final Exception err) { |
803 |
// SLD must be present |
// SLD must be present |
804 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
805 |
return null; |
return null; |
808 |
try { |
try { |
809 |
metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil |
metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil |
810 |
.changeUrlExt(geoObjectURL, rldExt)); |
.changeUrlExt(geoObjectURL, rldExt)); |
811 |
} catch (FileNotFoundException err) { |
} catch (final FileNotFoundException err) { |
812 |
// ignore missing attribute meta data |
// ignore missing attribute meta data |
813 |
} catch (Exception err) { |
} catch (final Exception err) { |
814 |
// any other error during meta data creation leads to error |
// any other error during meta data creation leads to error |
815 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
816 |
return null; |
return null; |
817 |
} |
} |
818 |
|
|
819 |
return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(sldStyle, |
return new StyledLayerStyle<AttributeMetadataMap>(sldStyle, metaData); |
|
metaData); |
|
820 |
} |
} |
821 |
|
|
822 |
/** |
/** |
823 |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
824 |
* {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd} |
* {@linkplain AttributeMetadata AttributeMetaData-Map} from a {@code .amd} |
825 |
* file for a given geo-object (feature) source. The SLD file must be |
* file for a given geo-object (feature) source. The SLD file must be |
826 |
* present. A missing attribute meta-data file is tolerated. |
* present. A missing attribute meta-data file is tolerated. |
827 |
* |
* |
833 |
* file extention for the raster legend-data file |
* file extention for the raster legend-data file |
834 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
835 |
*/ |
*/ |
836 |
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle( |
public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle( |
837 |
URL geoObjectURL) { |
final URL geoObjectURL) { |
838 |
return loadStyledFeatureStyle(geoObjectURL, "sld", "amd"); |
return loadStyledFeatureStyle(geoObjectURL, "sld", "amd"); |
839 |
} |
} |
840 |
|
|
851 |
* @param mdExt |
* @param mdExt |
852 |
* file extention for the meta-data file |
* file extention for the meta-data file |
853 |
*/ |
*/ |
854 |
public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style, |
public static <T> void saveStyledLayerStyle( |
855 |
URL geoObjectURL, String sldExt, String mdExt) throws Exception { |
final StyledLayerStyle<T> style, final URL geoObjectURL, |
856 |
|
final String sldExt, final String mdExt) throws Exception { |
857 |
// Store the SLD |
// Store the SLD |
858 |
Style sldStyle = style.getGeoObjectStyle(); |
final Style sldStyle = style.getGeoObjectStyle(); |
859 |
if (sldStyle != null) { |
if (sldStyle != null) { |
860 |
StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File( |
StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File( |
861 |
geoObjectURL.toURI()), sldExt)); |
geoObjectURL.toURI()), sldExt)); |
862 |
} |
} |
863 |
|
|
864 |
// Store the meta data |
// Store the meta data |
865 |
T metaData = style.getMetaData(); |
final T metaData = style.getMetaData(); |
866 |
if (metaData != null) { |
if (metaData != null) { |
867 |
if (metaData instanceof RasterLegendData) { |
if (metaData instanceof RasterLegendData) { |
868 |
saveRasterLegendData((RasterLegendData) metaData, IOUtil |
saveRasterLegendData((RasterLegendData) metaData, IOUtil |
871 |
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
872 |
// KOMPILIERBAR!! |
// KOMPILIERBAR!! |
873 |
} else if (metaData instanceof Map) { |
} else if (metaData instanceof Map) { |
874 |
saveAttributeMetaDataMap( |
saveAttributeMetaDataMap((AttributeMetadataMap) metaData, |
875 |
(Map<Integer, AttributeMetaData>) metaData, IOUtil |
IOUtil.changeUrlExt(geoObjectURL, mdExt)); |
|
.changeUrlExt(geoObjectURL, mdExt)); |
|
876 |
} else |
} else |
877 |
throw new UnsupportedOperationException( |
throw new UnsupportedOperationException( |
878 |
"Export for meta data not yet supported: " |
"Export for meta data not yet supported: " |
882 |
|
|
883 |
/** |
/** |
884 |
* Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and the |
* Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and the |
885 |
* meta data ({@link RasterLegendData} or {@link AttributeMetaData}) to a |
* meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a |
886 |
* {@code .rld} or {@code .amd} file. for a given geo-object source. |
* {@code .rld} or {@code .amd} file. for a given geo-object source. |
887 |
* |
* |
888 |
* @param style |
* @param style |
890 |
* @param geoObjectURL |
* @param geoObjectURL |
891 |
* URL of the (already read) raster object |
* URL of the (already read) raster object |
892 |
*/ |
*/ |
893 |
public static void saveStyledLayerStyle(StyledLayerStyle<?> style, |
public static void saveStyledLayerStyle(final StyledLayerStyle<?> style, |
894 |
URL geoObjectURL) throws Exception { |
final URL geoObjectURL) throws Exception { |
895 |
if (style.getMetaData() instanceof RasterLegendData) |
if (style.getMetaData() instanceof RasterLegendData) |
896 |
saveStyledLayerStyle(style, geoObjectURL, "sld", "rld"); |
saveStyledLayerStyle(style, geoObjectURL, "sld", "rld"); |
897 |
else |
else |
899 |
} |
} |
900 |
|
|
901 |
/** |
/** |
902 |
* Creates a {@link Box} that shows a legend for a list of |
* Creates a {@link JPanel} that shows a legend for a list of |
903 |
* {@link FeatureTypeStyle}s and a targeted featureType |
* {@link FeatureTypeStyle}s and a targeted featureType |
904 |
* |
* |
905 |
* @param featureType |
* @param featureType |
910 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons |
911 |
* Krüger</a> |
* Krüger</a> |
912 |
*/ |
*/ |
913 |
public static Box createLegendPanel(List<FeatureTypeStyle> list, |
public static JPanel createLegendPanel(Style style, |
914 |
SimpleFeatureType featureType, int iconWidth, int iconHeight) { |
final SimpleFeatureType featureType, final int iconWidth, |
915 |
|
final int iconHeight) { |
916 |
|
|
917 |
Box box = new Box(BoxLayout.Y_AXIS) { |
final List<FeatureTypeStyle> list = style.featureTypeStyles(); |
918 |
|
|
919 |
/** |
final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]")); |
920 |
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot |
|
921 |
* gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS |
if (style == null) { |
922 |
* gesetzt. |
// No Style => no legend |
923 |
* |
return panel; |
924 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
} |
|
* Krüger</a> |
|
|
*/ |
|
|
@Override |
|
|
public void print(Graphics g) { |
|
|
final Color orig = getBackground(); |
|
|
setBackground(Color.WHITE); |
|
|
// wrap in try/finally so that we always restore the state |
|
|
try { |
|
|
super.print(g); |
|
|
} finally { |
|
|
setBackground(orig); |
|
|
} |
|
|
} |
|
|
}; |
|
925 |
|
|
926 |
for (FeatureTypeStyle ftStyle : list) { |
for (final FeatureTypeStyle ftStyle : list) { |
927 |
|
|
928 |
// One child-node for every rule |
// One child-node for every rule |
929 |
List<Rule> rules = ftStyle.rules(); |
final List<Rule> rules = ftStyle.rules(); |
930 |
for (Rule rule : rules) { |
for (final Rule rule : rules) { |
931 |
|
|
932 |
/** |
/** |
933 |
* Let's not create a hbox for Rules that only contain |
* Let's not create a hbox for Rules that only contain |
937 |
.size() == rule.getSymbolizers().length) |
.size() == rule.getSymbolizers().length) |
938 |
continue; |
continue; |
939 |
|
|
|
Box hbox = new Box(BoxLayout.X_AXIS) { |
|
|
|
|
|
/** |
|
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein |
|
|
* Screenshot gemacht wird) wird. Dann werden wird der |
|
|
* Hintergrund auf WEISS gesetzt. |
|
|
*/ |
|
|
@Override |
|
|
public void print(Graphics g) { |
|
|
final Color orig = getBackground(); |
|
|
setBackground(Color.WHITE); |
|
|
// wrap in try/finally so that we always restore the |
|
|
// state |
|
|
try { |
|
|
super.print(g); |
|
|
} finally { |
|
|
setBackground(orig); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
/** |
|
|
* The size of the legend Symbol is dependent on the size of the |
|
|
* font. |
|
|
*/ |
|
|
final int fontHeight = new JLabel().getFontMetrics( |
|
|
new JLabel().getFont()).getHeight(); |
|
|
|
|
|
final Dimension ICON_SIZE = new Dimension(iconWidth, |
|
|
fontHeight > 5 ? fontHeight : iconHeight); |
|
|
|
|
|
// **************************************************************************** |
|
|
// Create the actual icon |
|
|
// **************************************************************************** |
|
940 |
final BufferedImage imageForRule = LegendIconFeatureRenderer |
final BufferedImage imageForRule = LegendIconFeatureRenderer |
941 |
.getInstance().createImageForRule(rule, featureType, |
.getInstance().createImageForRule(rule, featureType, |
942 |
ICON_SIZE); |
new Dimension(iconWidth, iconHeight)); |
|
|
|
|
// LOGGER.debug("Creating a new Legend Image for RUle name = |
|
|
// "+rule.getName()); |
|
943 |
|
|
944 |
ImageIcon legendIcon = new ImageIcon(imageForRule); |
final ImageIcon legendIcon = new ImageIcon(imageForRule); |
945 |
|
|
946 |
final JLabel iconLabel = new JLabel(legendIcon); |
final JLabel iconLabel = new JLabel(legendIcon); |
947 |
hbox.setAlignmentX(0f); |
panel.add(iconLabel, "sgx1"); |
948 |
hbox.add(iconLabel); |
// hbox.setAlignmentX(0f); |
949 |
hbox.add(Box.createHorizontalStrut(3)); |
// hbox.add(iconLabel); |
950 |
|
// hbox.add(Box.createHorizontalStrut(3)); |
|
// **************************************************************************** |
|
|
// The labeltext is read from the SLD. Its either the title |
|
|
// directly, or interpreted as OneLine Code |
|
|
// **************************************************************************** |
|
|
// final String rawText = |
|
|
// rule.getDescription().getTitle().toString(); |
|
|
final String rawText = rule.getDescription().getTitle() |
|
|
.toString(); |
|
|
|
|
|
Translation labelT = new Translation(); |
|
|
labelT.fromOneLine(rawText); |
|
951 |
|
|
952 |
|
final Translation labelT = new Translation(); |
953 |
|
labelT.fromOneLine(rule.getDescription().getTitle()); |
954 |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
|
hbox.add(classTitleLabel); |
|
|
classTitleLabel.setLabelFor(iconLabel); |
|
|
|
|
|
box.add(hbox); |
|
955 |
|
|
956 |
|
panel.add(classTitleLabel, "sgx2"); |
957 |
|
classTitleLabel.setLabelFor(iconLabel); |
958 |
} |
} |
959 |
} |
} |
960 |
|
|
961 |
return box; |
return panel; |
962 |
} |
} |
963 |
|
|
964 |
/** |
/** |
965 |
* Creates a |
* Creates a {@link JComponent} that contains a legend for a given |
966 |
|
* rasterLayer and a given {@link Style}. |
967 |
* |
* |
968 |
* @param styledRaster |
* @param style |
969 |
* @param iconHeight |
* if <code>null</code>, the default {@link Style} is extracetd |
970 |
* @param iconWidth |
* from the {@link StyledRasterInterface} |
|
* @return |
|
971 |
*/ |
*/ |
972 |
public static Box createLegendPanel(StyledRasterInterface<?> styledRaster, |
public static JPanel createLegendPanel( |
973 |
int iconWidth, int iconHeight) { |
final StyledRasterInterface<?> styledRaster, Style style, |
974 |
|
final int iconWidth, final int iconHeight) { |
975 |
|
|
976 |
|
// If no style is given, we use the default style for this layer |
977 |
|
if (style == null) |
978 |
|
style = styledRaster.getStyle(); |
979 |
|
|
980 |
/** |
/** |
981 |
* Determine whether a Style is responsible for the coloring |
* Determine whether a Style is responsible for the coloring |
982 |
*/ |
*/ |
983 |
ColorModel colorModel = null; |
ColorModel colorModel = null; |
984 |
if (!isStyleable(styledRaster) |
if (!isStyleable(styledRaster) |
985 |
|| (isStyleable(styledRaster) && styledRaster.getStyle() != null)) { |
|| (isStyleable(styledRaster) && style == null)) { |
986 |
colorModel = getColorModel(styledRaster); |
colorModel = getColorModel(styledRaster); |
987 |
} |
} |
988 |
|
|
989 |
RasterLegendData rasterLegendData = styledRaster.getLegendMetaData(); |
final RasterLegendData rasterLegendData = styledRaster |
990 |
List<Double> legendRasterValues = rasterLegendData.getSortedKeys(); |
.getLegendMetaData(); |
991 |
Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
final List<Double> legendRasterValues = rasterLegendData |
992 |
|
.getSortedKeys(); |
993 |
|
final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
994 |
.createSampleRasters(); |
.createSampleRasters(); |
995 |
|
|
996 |
Box box = new Box(BoxLayout.Y_AXIS) { |
final JPanel panel = new JPanel(new MigLayout("wrap 2")); |
|
|
|
|
/** |
|
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot |
|
|
* gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS |
|
|
* gesetzt. |
|
|
*/ |
|
|
@Override |
|
|
public void print(Graphics g) { |
|
|
final Color orig = getBackground(); |
|
|
setBackground(Color.WHITE); |
|
|
// wrap in try/finally so that we always restore the state |
|
|
try { |
|
|
super.print(g); |
|
|
} finally { |
|
|
setBackground(orig); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
for (Double rValue : legendRasterValues) { |
|
997 |
|
|
998 |
/** |
for (final Double rValue : legendRasterValues) { |
|
* |
|
|
*/ |
|
|
Box hbox = new Box(BoxLayout.X_AXIS) { |
|
|
|
|
|
/** |
|
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein |
|
|
* Screenshot gemacht wird) wird. Dann werden wird der |
|
|
* Hintergrund auf WEISS gesetzt. |
|
|
*/ |
|
|
@Override |
|
|
public void print(Graphics g) { |
|
|
final Color orig = getBackground(); |
|
|
setBackground(Color.WHITE); |
|
|
// wrap in try/finally so that we always restore the |
|
|
// state |
|
|
try { |
|
|
super.print(g); |
|
|
} finally { |
|
|
setBackground(orig); |
|
|
} |
|
|
} |
|
|
}; |
|
999 |
|
|
1000 |
final Dimension ICON_SIZE = new Dimension(iconWidth, |
// final Dimension ICON_SIZE = new Dimension(iconWidth, |
1001 |
new JLabel().getFontMetrics(new JLabel().getFont()) |
// new JLabel().getFontMetrics(new JLabel().getFont()) |
1002 |
.getHeight() > 5 ? new JLabel().getFontMetrics( |
// .getHeight() > 5 ? new JLabel().getFontMetrics( |
1003 |
new JLabel().getFont()).getHeight() : iconHeight); |
// new JLabel().getFont()).getHeight() : iconHeight); |
1004 |
|
|
1005 |
// **************************************************************************** |
// **************************************************************************** |
1006 |
// Create the actual icon |
// Create the actual icon |
1007 |
// **************************************************************************** |
// **************************************************************************** |
1008 |
BufferedImage buffImage = new BufferedImage(ICON_SIZE.width, |
final BufferedImage buffImage = new BufferedImage(iconWidth, |
1009 |
ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB); |
iconHeight, BufferedImage.TYPE_INT_ARGB); |
1010 |
Graphics2D graphics = buffImage.createGraphics(); |
|
1011 |
|
final Graphics2D graphics = buffImage.createGraphics(); |
1012 |
|
|
1013 |
if (colorModel != null) { |
if (colorModel != null) { |
1014 |
|
// The colors come from the ColorModel! |
1015 |
|
|
1016 |
try { |
try { |
1017 |
Object inData = null; |
Object inData = null; |
1040 |
final Color color = new Color(colorModel.getRGB(inData)); |
final Color color = new Color(colorModel.getRGB(inData)); |
1041 |
graphics.setBackground(color); |
graphics.setBackground(color); |
1042 |
graphics.setColor(color); |
graphics.setColor(color); |
1043 |
graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height); |
graphics.fillRect(0, 0, iconWidth, iconHeight); |
1044 |
} catch (Exception e) { |
} catch (final Exception e) { |
1045 |
LOGGER.debug("Dann nehmen wir halt den GridCoverageRenderer", e); |
LOGGER.info( |
1046 |
|
"Dann nehmen wir halt den GridCoverageRenderer", e); |
1047 |
colorModel = null; |
colorModel = null; |
1048 |
} |
} |
1049 |
} |
} else { |
1050 |
|
// The colors come from the Style |
|
if (colorModel == null) { |
|
1051 |
|
|
1052 |
/** |
/** |
1053 |
* The coverage contains only one value of value rValue |
* The coverage contains only one value of value rValue |
1054 |
*/ |
*/ |
1055 |
GridCoverage2D sampleCov = sampleRasters.get(rValue); |
final GridCoverage2D sampleCov = sampleRasters.get(rValue); |
1056 |
GridCoverageRenderer renderer; |
GridCoverageRenderer renderer; |
1057 |
try { |
try { |
1058 |
renderer = new GridCoverageRenderer(sampleCov |
renderer = new GridCoverageRenderer(sampleCov |
1060 |
.createEnvelope(sampleCov.getEnvelope()), |
.createEnvelope(sampleCov.getEnvelope()), |
1061 |
new Rectangle(iconWidth, iconHeight), |
new Rectangle(iconWidth, iconHeight), |
1062 |
(AffineTransform) null); |
(AffineTransform) null); |
1063 |
} catch (Exception e1) { |
} catch (final Exception e1) { |
1064 |
throw new RuntimeException( |
throw new RuntimeException( |
1065 |
"Creating the GridCoverageRenderer:", e1); |
"Creating a GridCoverageRenderer failed:", e1); |
1066 |
} |
} |
1067 |
|
|
1068 |
/** |
/** |
1069 |
* Iterate over all FeatureTypeStyles. |
* Iterate over all FeatureTypeStyles. |
1070 |
*/ |
*/ |
1071 |
// for (FeatureTypeStyle ftStyle : styledGrid.getStyle() |
final List<RasterSymbolizer> rSymbols = StylingUtil |
|
// .featureTypeStyles()) { |
|
|
// One child-node for every rule |
|
|
// List<Rule> rules = ftStyle.rules(); |
|
|
// for (Rule rule : rules) { |
|
|
final Style style = styledRaster.getStyle(); |
|
|
List<RasterSymbolizer> rSymbols = StylingUtil |
|
1072 |
.getRasterSymbolizers(style); |
.getRasterSymbolizers(style); |
1073 |
|
|
1074 |
for (RasterSymbolizer symbolizer : rSymbols) { |
for (final RasterSymbolizer symbolizer : rSymbols) { |
|
// LOGGER.debug("Creating a new Legend Image for RUle |
|
|
// name = |
|
|
// "+rule.getName()); |
|
1075 |
try { |
try { |
1076 |
renderer.paint(graphics, sampleCov, symbolizer); |
renderer.paint(graphics, sampleCov, symbolizer); |
1077 |
} catch (Exception ee) { |
} catch (final Exception ee) { |
1078 |
LOGGER.error("Unable to paint " + symbolizer |
LOGGER.error("Unable to paint " + symbolizer |
1079 |
+ " into the legend image", ee); |
+ " into the legend image", ee); |
1080 |
} |
} |
|
// } |
|
|
// } |
|
1081 |
} |
} |
1082 |
} |
} |
1083 |
|
|
1084 |
ImageIcon legendIcon = new ImageIcon(buffImage); |
final JLabel iconLabel = new JLabel(new ImageIcon(buffImage)); |
1085 |
|
// hbox.setAlignmentX(0f); |
1086 |
|
panel.add(iconLabel, "sgx1"); |
1087 |
|
// hbox.add(Box.createHorizontalStrut(3)); |
1088 |
|
|
1089 |
final JLabel iconLabel = new JLabel(legendIcon); |
final Translation labelT = rasterLegendData.get(rValue); |
|
hbox.setAlignmentX(0f); |
|
|
hbox.add(iconLabel); |
|
|
hbox.add(Box.createHorizontalStrut(3)); |
|
|
|
|
|
Translation labelT = rasterLegendData.get(rValue); |
|
1090 |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
1091 |
hbox.add(classTitleLabel); |
panel.add(classTitleLabel, "sgx2" |
1092 |
|
+ (rasterLegendData.getPaintGaps() ? ", gapy 0 3" : "")); |
1093 |
classTitleLabel.setLabelFor(iconLabel); |
classTitleLabel.setLabelFor(iconLabel); |
1094 |
|
|
1095 |
box.add(hbox); |
// box.add(hbox); |
1096 |
|
|
1097 |
if (rasterLegendData.getPaintGaps()) { |
if (rasterLegendData.getPaintGaps()) { |
1098 |
iconLabel |
iconLabel |
1099 |
.setBorder(BorderFactory.createLineBorder(Color.black)); |
.setBorder(BorderFactory.createLineBorder(Color.black)); |
|
box.add(Box.createVerticalStrut(3)); |
|
1100 |
} |
} |
1101 |
|
|
1102 |
} |
} |
1103 |
|
|
1104 |
return box; |
return panel; |
1105 |
} |
} |
1106 |
|
|
1107 |
/** |
/** |
1108 |
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
1109 |
* return <code>null</code> if the geoobject can not be accessed. |
* return <code>null</code> if the geoobject can not be accessed. |
1110 |
*/ |
*/ |
1111 |
public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) { |
@SuppressWarnings("unchecked") |
1112 |
|
public static ColorModel getColorModel( |
1113 |
|
final StyledRasterInterface<?> styledGrid) { |
1114 |
ColorModel colorModel = null; |
ColorModel colorModel = null; |
1115 |
try { |
try { |
1116 |
Object geoObject = styledGrid.getGeoObject(); |
final Object geoObject = styledGrid.getGeoObject(); |
1117 |
if (geoObject instanceof GridCoverage2D) { |
if (geoObject instanceof GridCoverage2D) { |
1118 |
GridCoverage2D cov = (GridCoverage2D) geoObject; |
final GridCoverage2D cov = (GridCoverage2D) geoObject; |
1119 |
colorModel = cov.getRenderedImage().getColorModel(); |
colorModel = cov.getRenderedImage().getColorModel(); |
1120 |
} else if (geoObject instanceof ImagePyramidReader) { |
} else if (styledGrid instanceof StyledRasterPyramidInterface) { |
1121 |
|
|
1122 |
Parameter readGG = new Parameter( |
final Parameter readGG = new Parameter( |
1123 |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
1124 |
|
|
1125 |
ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope( |
final ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope( |
1126 |
styledGrid.getEnvelope(), styledGrid.getCrs()); |
styledGrid.getEnvelope(), styledGrid.getCrs()); |
1127 |
|
|
1128 |
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
1129 |
new Rectangle(0, 0, 10, 10)), mapExtend)); |
new Rectangle(0, 0, 1, 1)), mapExtend)); |
1130 |
|
|
1131 |
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject; |
1132 |
|
|
1133 |
final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) geoObject; |
final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil |
1134 |
GridCoverage2D cov = (GridCoverage2D) aReader |
.getWrappedGeoObject(rFc); |
1135 |
|
final GridCoverage2D cov = (GridCoverage2D) aReader |
1136 |
.read(new GeneralParameterValue[] { readGG }); |
.read(new GeneralParameterValue[] { readGG }); |
1137 |
colorModel = cov.getRenderedImage().getColorModel(); |
colorModel = cov.getRenderedImage().getColorModel(); |
1138 |
} |
} |
1139 |
} catch (Exception e) { |
} catch (final Exception e) { |
1140 |
LOGGER.error("Error reading the colormodel from " + styledGrid); |
LOGGER.error("Error reading the colormodel from " + styledGrid, e); |
1141 |
return null; |
return null; |
1142 |
} |
} |
1143 |
return colorModel; |
return colorModel; |
1149 |
* with their own {@link ColorModel} and will ignore any |
* with their own {@link ColorModel} and will ignore any |
1150 |
* {@link RasterSymbolizer} = SLD. |
* {@link RasterSymbolizer} = SLD. |
1151 |
*/ |
*/ |
1152 |
public static boolean isStyleable(StyledRasterInterface<?> styledRaster) { |
public static boolean isStyleable( |
1153 |
ColorModel colorModel = getColorModel(styledRaster); |
final StyledRasterInterface<?> styledRaster) { |
1154 |
|
final ColorModel colorModel = getColorModel(styledRaster); |
1155 |
LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is " |
|
1156 |
+ colorModel.getClass().getSimpleName()); |
// LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is " |
1157 |
|
// + colorModel != null ? colorModel.getClass().getSimpleName() : "NULL"); |
1158 |
|
|
1159 |
if (colorModel == null) |
if (colorModel == null) |
1160 |
return true; |
return true; |
1162 |
return true; |
return true; |
1163 |
return false; |
return false; |
1164 |
} |
} |
1165 |
|
|
1166 |
|
/** |
1167 |
|
* Set the given Style as the Style of the {@link MapLayer}, unless the |
1168 |
|
* styles are the same (not comparing selection stuff). If the |
1169 |
|
* {@link MapLayer}s {@link Style} is changed, the selection FTS is kept.<br/> |
1170 |
|
* Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a |
1171 |
|
* repaint, so only use it when needed. |
1172 |
|
* |
1173 |
|
* @return <code>true</code> if the {@link MapLayer}'s {@link Style} has been changed. |
1174 |
|
*/ |
1175 |
|
public static boolean updateMapLayerStyleIfChangedAndKeepSelection(MapLayer mapLayer, |
1176 |
|
Style style2) { |
1177 |
|
|
1178 |
|
Style mapLayerStyleCleaned = StylingUtil |
1179 |
|
.removeSelectionFeatureTypeStyle(mapLayer.getStyle()); |
1180 |
|
|
1181 |
|
Style newStyleCleaned = StylingUtil.removeSelectionFeatureTypeStyle(style2); |
1182 |
|
|
1183 |
|
if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned, |
1184 |
|
newStyleCleaned)) { |
1185 |
|
|
1186 |
|
// They are different when compared without SELECTION FTS! |
1187 |
|
|
1188 |
|
// Now let's copy any SELECTION FTS to the now style |
1189 |
|
FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil.getSelectionFeatureTypeStyle( mapLayer.getStyle() ); |
1190 |
|
if (selectionFeatureTypeStyle != null) { |
1191 |
|
newStyleCleaned.featureTypeStyles().add(selectionFeatureTypeStyle); |
1192 |
|
// newStyleCleaned is not so clean anymore... We just alled a selcetion FTS |
1193 |
|
} |
1194 |
|
|
1195 |
|
mapLayer.setStyle(newStyleCleaned); |
1196 |
|
|
1197 |
|
return true; |
1198 |
|
|
1199 |
|
} else { |
1200 |
|
return false; |
1201 |
|
} |
1202 |
|
} |
1203 |
|
|
1204 |
} |
} |