25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.geotools; |
package skrueger.geotools; |
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; |
38 |
import java.awt.image.ColorModel; |
import java.awt.image.ColorModel; |
39 |
import java.awt.image.ComponentColorModel; |
import java.awt.image.ComponentColorModel; |
40 |
import java.awt.image.DataBuffer; |
import java.awt.image.DataBuffer; |
41 |
|
import java.awt.image.IndexColorModel; |
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; |
45 |
import java.net.URL; |
import java.net.URL; |
46 |
import java.text.DecimalFormat; |
import java.text.DecimalFormat; |
47 |
import java.util.HashMap; |
import java.util.ArrayList; |
48 |
|
import java.util.Collections; |
49 |
import java.util.List; |
import java.util.List; |
50 |
import java.util.Map; |
import java.util.Map; |
51 |
import java.util.SortedMap; |
import java.util.Set; |
|
import java.util.TreeMap; |
|
52 |
|
|
53 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
|
import javax.swing.Box; |
|
|
import javax.swing.BoxLayout; |
|
54 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
55 |
|
import javax.swing.JComponent; |
56 |
import javax.swing.JLabel; |
import javax.swing.JLabel; |
57 |
|
|
58 |
|
import net.miginfocom.swing.MigLayout; |
59 |
|
|
60 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
61 |
import org.geotools.coverage.grid.GeneralGridEnvelope; |
import org.geotools.coverage.grid.GeneralGridEnvelope; |
62 |
import org.geotools.coverage.grid.GridCoverage2D; |
import org.geotools.coverage.grid.GridCoverage2D; |
64 |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
65 |
import org.geotools.coverage.grid.io.AbstractGridFormat; |
import org.geotools.coverage.grid.io.AbstractGridFormat; |
66 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
67 |
import org.geotools.gce.imagepyramid.ImagePyramidReader; |
import org.geotools.feature.NameImpl; |
68 |
import org.geotools.geometry.jts.ReferencedEnvelope; |
import org.geotools.geometry.jts.ReferencedEnvelope; |
69 |
import org.geotools.map.DefaultMapLayer; |
import org.geotools.map.DefaultMapLayer; |
70 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
80 |
import org.jdom.Element; |
import org.jdom.Element; |
81 |
import org.jdom.input.SAXBuilder; |
import org.jdom.input.SAXBuilder; |
82 |
import org.jdom.output.XMLOutputter; |
import org.jdom.output.XMLOutputter; |
83 |
|
import org.opengis.feature.simple.SimpleFeature; |
84 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
85 |
|
import org.opengis.feature.type.AttributeDescriptor; |
86 |
|
import org.opengis.feature.type.GeometryDescriptor; |
87 |
|
import org.opengis.feature.type.Name; |
88 |
|
import org.opengis.filter.FilterFactory2; |
89 |
import org.opengis.parameter.GeneralParameterValue; |
import org.opengis.parameter.GeneralParameterValue; |
90 |
|
|
91 |
|
import schmitzm.geotools.FilterUtil; |
92 |
import schmitzm.geotools.JTSUtil; |
import schmitzm.geotools.JTSUtil; |
93 |
|
import schmitzm.geotools.feature.FeatureUtil; |
94 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
95 |
import schmitzm.io.IOUtil; |
import schmitzm.io.IOUtil; |
96 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
97 |
|
import schmitzm.swing.ExceptionDialog; |
98 |
|
import schmitzm.swing.JPanel; |
99 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
100 |
import skrueger.AttributeMetaData; |
import skrueger.AttributeMetadataImpl; |
101 |
|
import skrueger.AttributeMetadataInterface; |
102 |
import skrueger.RasterLegendData; |
import skrueger.RasterLegendData; |
103 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
104 |
|
|
105 |
|
import com.vividsolutions.jts.geom.Geometry; |
106 |
|
|
107 |
/** |
/** |
108 |
* This class provides static helper methods for dealing with |
* This class provides static helper methods for dealing with |
109 |
* {@link StyledLayerInterface} stuff. |
* {@link StyledLayerInterface} stuff. |
118 |
private static final SAXBuilder SAX_BUILDER = new SAXBuilder(); |
private static final SAXBuilder SAX_BUILDER = new SAXBuilder(); |
119 |
private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter(); |
private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter(); |
120 |
|
|
121 |
|
/** |
122 |
|
* Is appended to the name of a rule, this rule shall not be shown in the |
123 |
|
* legend |
124 |
|
*/ |
125 |
|
public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND"; |
126 |
|
|
127 |
/** URL for Atlas XML schema */ |
/** URL for Atlas XML schema */ |
128 |
public static final String AMLURI = "http://www.wikisquare.de/AtlasML"; |
public static final String AMLURI = "http://www.wikisquare.de/AtlasML"; |
129 |
/** Name of the XML Element for the attribute meta data map */ |
/** Name of the XML Element for the attribute meta data map */ |
136 |
public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem"; |
public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem"; |
137 |
/** Name of the XML Element for a translation */ |
/** Name of the XML Element for a translation */ |
138 |
public static final String ELEM_NAME_TRANSLATION = "translation"; |
public static final String ELEM_NAME_TRANSLATION = "translation"; |
139 |
|
private static FilterFactory2 ff = FilterUtil.FILTER_FAC2; |
140 |
|
|
141 |
/** |
/** |
142 |
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
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) throws Exception { |
public static MapLayer createMapLayer(final Object object) throws Exception { |
155 |
return createMapLayer(object, null); |
return createMapLayer(object, null); |
156 |
} |
} |
157 |
|
|
170 |
* if {@code null} is given as object or an error occurs |
* if {@code null} is given as object or an error occurs |
171 |
* during layer creation |
* during layer creation |
172 |
*/ |
*/ |
173 |
public static MapLayer createMapLayer(Object object, Style forcedStyle) |
public static MapLayer createMapLayer(Object object, final Style forcedStyle) |
174 |
throws Exception { |
throws Exception { |
175 |
MapLayer layer = null; |
MapLayer layer = null; |
176 |
Style style = null; |
Style style = null; |
211 |
* if {@code null} is given as object or an error occurs |
* if {@code null} is given as object or an error occurs |
212 |
* during creation |
* during creation |
213 |
*/ |
*/ |
214 |
public static StyledLayerInterface<?> createStyledLayer(Object object, |
public static StyledLayerInterface<?> createStyledLayer( |
215 |
String title) { |
final Object object, final String title) { |
216 |
return createStyledLayer(object, title, null); |
return createStyledLayer(object, title, null); |
217 |
} |
} |
218 |
|
|
231 |
* if {@code null} is given as object or an error occurs |
* if {@code null} is given as object or an error occurs |
232 |
* during creation |
* during creation |
233 |
*/ |
*/ |
234 |
public static StyledLayerInterface<?> createStyledLayer(Object object, |
public static StyledLayerInterface<?> createStyledLayer( |
235 |
String title, StyledLayerStyle style) { |
final Object object, final String title, |
236 |
|
final StyledLayerStyle style) { |
237 |
StyledLayerInterface<?> styledLayer = null; |
StyledLayerInterface<?> styledLayer = null; |
238 |
|
|
239 |
String id = (title != null) ? title : "defaultID"; |
final String id = (title != null) ? title : "defaultID"; |
240 |
|
|
241 |
if (object instanceof GridCoverage2D) |
if (object instanceof GridCoverage2D) |
242 |
styledLayer = new StyledGridCoverage((GridCoverage2D) object, id, |
styledLayer = new StyledGridCoverage((GridCoverage2D) object, id, |
264 |
* @param visible |
* @param visible |
265 |
* indicated whether the visible or invisible entries are |
* indicated whether the visible or invisible entries are |
266 |
* returned |
* returned |
267 |
|
* |
268 |
|
* TODO replace with |
269 |
|
* {@link AttributeMetadataMap#sortedValuesVisibleOnly()} |
270 |
*/ |
*/ |
271 |
public static SortedMap<Integer, AttributeMetaData> getVisibleAttributeMetaData( |
public static AttributeMetadataMap<? extends AttributeMetadataInterface> getVisibleAttributeMetaData( |
272 |
Map<Integer, AttributeMetaData> amdMap, boolean visible) { |
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap, |
273 |
SortedMap<Integer, AttributeMetaData> filteredMap = new TreeMap<Integer, AttributeMetaData>(); |
final boolean visible) { |
274 |
for (AttributeMetaData amd : amdMap.values()) |
|
275 |
if (amd.isVisible()) |
final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap |
276 |
filteredMap.put(amd.getColIdx(), amd); |
.clone(); |
277 |
|
if (filteredMap.size() > 0) { |
278 |
|
filteredMap.clear(); // Just in case the close copies the contents |
279 |
|
} |
280 |
|
|
281 |
|
for (final AttributeMetadataInterface amd : amdMap.values()) |
282 |
|
if (amd.isVisible() == visible) |
283 |
|
filteredMap.put(amd.getName(), amd); |
284 |
|
|
285 |
return filteredMap; |
return filteredMap; |
286 |
} |
} |
287 |
|
|
288 |
/** |
/** |
289 |
* Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}. |
* Parses a {@link AttributeMetadataImpl} object from an JDOM- |
290 |
* This method works like {@link |
* {@link Element}. This method works like {@link |
291 |
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
292 |
* |
* |
293 |
|
* TODO 20.11.2009, SK: There are some new attribute weight, functiona, |
294 |
|
* functionX and nodata in AttributeMetaData that should be parsed/exported |
295 |
|
* too. but this method is only used by ISDSS, which is not supporting that |
296 |
|
* stuff anyways. |
297 |
|
* |
298 |
* @param element |
* @param element |
299 |
* {@link Element} to parse |
* {@link Element} to parse |
300 |
*/ |
*/ |
301 |
public static AttributeMetaData parseAttributeMetaData(final Element element) { |
public static AttributeMetadataImpl parseAttributeMetaData( |
302 |
final Integer col = Integer.valueOf(element.getAttributeValue("col")); |
final Element element) { |
303 |
|
final String namespace = element.getAttributeValue("namespace"); |
304 |
|
final String localname = element.getAttributeValue("localname"); |
305 |
|
final NameImpl aName = new NameImpl(namespace, localname); |
306 |
final Boolean visible = Boolean.valueOf(element |
final Boolean visible = Boolean.valueOf(element |
307 |
.getAttributeValue("visible")); |
.getAttributeValue("visible")); |
308 |
final String unit = element.getAttributeValue("unit"); |
final String unit = element.getAttributeValue("unit"); |
318 |
else if (childElement.getName().equals("desc")) |
else if (childElement.getName().equals("desc")) |
319 |
desc = parseTranslation(childElement); |
desc = parseTranslation(childElement); |
320 |
} |
} |
321 |
return new AttributeMetaData(col, visible, name, desc, unit); |
return new AttributeMetadataImpl(aName, visible, name, desc, unit); |
322 |
} |
} |
323 |
|
|
324 |
/** |
/** |
325 |
* Parses a {@link AttributeMetaData} map from an JDOM-{@link Element} with |
* Parses a {@link AttributeMetadataImpl} map from an JDOM-{@link Element} |
326 |
* {@code <attribute>}-childs. |
* with {@code <attribute>}-childs. |
327 |
* |
* |
328 |
* @param element |
* @param element |
329 |
* {@link Element} to parse |
* {@link Element} to parse |
330 |
|
* |
331 |
|
* TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has |
332 |
|
* more attributes which are not used by Xulu/ISDSS. GP |
333 |
|
* exports/imports the AMD via AMLExporter and AMLImporter |
334 |
|
* classes. (SK, 3.2.2010) * |
335 |
*/ |
*/ |
336 |
public static Map<Integer, AttributeMetaData> parseAttributeMetaDataMap( |
public static AttributeMetadataMap parseAttributeMetaDataMap( |
337 |
final Element element) { |
final Element element) { |
338 |
HashMap<Integer, AttributeMetaData> metaData = new HashMap<Integer, AttributeMetaData>(); |
final AttributeMetadataMap metaData = new AttributeMetadataImplMap(); |
339 |
List<Element> attributesElements = element |
final List<Element> attributesElements = element |
340 |
.getChildren(ELEM_NAME_ATTRIBUTE); |
.getChildren(ELEM_NAME_ATTRIBUTE); |
341 |
for (Element attibuteElement : attributesElements) { |
for (final Element attibuteElement : attributesElements) { |
342 |
AttributeMetaData attrMetaData = parseAttributeMetaData(attibuteElement); |
final AttributeMetadataImpl attrMetaData = parseAttributeMetaData(attibuteElement); |
343 |
metaData.put(attrMetaData.getColIdx(), attrMetaData); |
metaData.put(attrMetaData.getName(), attrMetaData); |
344 |
} |
} |
345 |
return metaData; |
return metaData; |
346 |
} |
} |
347 |
|
|
348 |
/** |
/** |
349 |
* Loads a {@link AttributeMetaData} object from an URL. |
* Loads a {@link AttributeMetadataImpl} object from an URL. |
350 |
* |
* |
351 |
* @param documentUrl |
* @param documentUrl |
352 |
* {@link URL} to parse |
* {@link URL} to parse |
353 |
* @see #parseAttributeMetaData(Element) |
* @see #parseAttributeMetaData(Element) |
354 |
*/ |
*/ |
355 |
public static Map<Integer, AttributeMetaData> loadAttributeMetaDataMap( |
public static AttributeMetadataMap loadAttributeMetaDataMap( |
356 |
final URL documentUrl) throws Exception { |
final URL documentUrl) throws Exception { |
357 |
Document document = SAX_BUILDER.build(documentUrl); |
final Document document = SAX_BUILDER.build(documentUrl); |
358 |
return parseAttributeMetaDataMap(document.getRootElement()); |
return parseAttributeMetaDataMap(document.getRootElement()); |
359 |
} |
} |
360 |
|
|
361 |
/** |
/** |
362 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
* Creates an JDOM {@link Element} for the given |
363 |
* object. |
* {@link AttributeMetadataImpl} object. |
364 |
* |
* |
365 |
* @param amd |
* @param amd |
366 |
* meta data for one attribute |
* meta data for one attribute |
367 |
|
* |
368 |
|
* TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has |
369 |
|
* more attributes which are not used by Xulu/ISDSS. GP |
370 |
|
* exports/imports the AMD via AMLExporter and AMLImporter |
371 |
|
* classes. (SK, 3.2.2010) |
372 |
*/ |
*/ |
373 |
public static Element createAttributeMetaDataElement( |
public static Element createAttributeMetaDataElement( |
374 |
final AttributeMetaData amd) { |
final AttributeMetadataInterface amd) { |
375 |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
376 |
element.setAttribute("col", String.valueOf(amd.getColIdx())); |
element.setAttribute("namespace", |
377 |
|
String.valueOf(amd.getName().getNamespaceURI())); |
378 |
|
element.setAttribute("localname", String.valueOf(amd.getLocalName())); |
379 |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
380 |
element.setAttribute("unit", amd.getUnit()); |
element.setAttribute("unit", amd.getUnit()); |
381 |
// Creating a aml:name tag... |
// Creating a aml:name tag... |
386 |
} |
} |
387 |
|
|
388 |
/** |
/** |
389 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetaData} |
* Creates an JDOM {@link Element} for the given |
390 |
* map. |
* {@link AttributeMetadataImpl} map. |
391 |
* |
* |
392 |
* @param amdMap |
* @param amdMap |
393 |
* map of attribute meta data |
* map of attribute meta data |
394 |
*/ |
*/ |
395 |
public static Element createAttributeMetaDataMapElement( |
public static Element createAttributeMetaDataMapElement( |
396 |
final Map<Integer, AttributeMetaData> amdMap) { |
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap) { |
397 |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
398 |
for (AttributeMetaData amd : amdMap.values()) |
for (final AttributeMetadataInterface amd : amdMap.values()) |
399 |
element.addContent(createAttributeMetaDataElement(amd)); |
element.addContent(createAttributeMetaDataElement(amd)); |
400 |
return element; |
return element; |
401 |
} |
} |
402 |
|
|
403 |
/** |
/** |
404 |
* Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL. |
* Saves a {@link AttributeMetadataImpl AttributeMetaData-Map} to an URL. |
405 |
* |
* |
406 |
* @param amdMap |
* @param amdMap |
407 |
* map of {@link AttributeMetaData} |
* map of {@link AttributeMetadataImpl} |
408 |
* @param documentUrl |
* @param documentUrl |
409 |
* {@link URL} to store the XML |
* {@link URL} to store the XML |
410 |
*/ |
*/ |
411 |
public static void saveAttributeMetaDataMap( |
public static void saveAttributeMetaDataMap( |
412 |
final Map<Integer, AttributeMetaData> amdMap, final URL documentUrl) |
final AttributeMetadataMap amdMap, final URL documentUrl) |
413 |
throws Exception { |
throws Exception { |
414 |
// Create XML-Document |
// Create XML-Document |
415 |
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
426 |
* @param element |
* @param element |
427 |
* {@link Element} to parse |
* {@link Element} to parse |
428 |
*/ |
*/ |
429 |
public static RasterLegendData parseRasterLegendData(Element element) { |
public static RasterLegendData parseRasterLegendData(final Element element) { |
430 |
|
|
431 |
final boolean paintGaps = Boolean.valueOf(element |
final boolean paintGaps = Boolean.valueOf(element |
432 |
.getAttributeValue("paintGaps")); |
.getAttributeValue("paintGaps")); |
433 |
|
|
434 |
RasterLegendData rld = new RasterLegendData(paintGaps); |
final RasterLegendData rld = new RasterLegendData(paintGaps); |
435 |
|
|
436 |
for (Element childElement : (List<Element>) element.getChildren()) { |
for (final Element childElement : (List<Element>) element.getChildren()) { |
437 |
final String name = childElement.getName(); |
final String name = childElement.getName(); |
438 |
// Cancel if it's an attribute |
// Cancel if it's an attribute |
439 |
if (childElement.getChildren().size() == 0) |
if (childElement.getChildren().size() == 0) |
453 |
// id label element is missing, the translation is searched |
// id label element is missing, the translation is searched |
454 |
// directly |
// directly |
455 |
// as childs of the rasterLegendItem element |
// as childs of the rasterLegendItem element |
456 |
Translation label = parseTranslation(labelElement != null ? labelElement |
final Translation label = parseTranslation(labelElement != null ? labelElement |
457 |
: childElement); |
: childElement); |
458 |
rld.put(value, label); |
rld.put(value, label); |
459 |
} |
} |
471 |
*/ |
*/ |
472 |
public static RasterLegendData loadRasterLegendData(final URL documentUrl) |
public static RasterLegendData loadRasterLegendData(final URL documentUrl) |
473 |
throws Exception { |
throws Exception { |
474 |
Document document = SAX_BUILDER.build(documentUrl); |
final Document document = SAX_BUILDER.build(documentUrl); |
475 |
return parseRasterLegendData(document.getRootElement()); |
return parseRasterLegendData(document.getRootElement()); |
476 |
} |
} |
477 |
|
|
486 |
final RasterLegendData rld) { |
final RasterLegendData rld) { |
487 |
final Element element = new Element(ELEM_NAME_RLD, AMLURI); |
final Element element = new Element(ELEM_NAME_RLD, AMLURI); |
488 |
element.setAttribute("paintGaps", rld.isPaintGaps().toString()); |
element.setAttribute("paintGaps", rld.isPaintGaps().toString()); |
489 |
for (Double key : rld.getSortedKeys()) { |
for (final Double key : rld.getSortedKeys()) { |
490 |
Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI); |
final Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI); |
491 |
item.setAttribute("value", key.toString()); |
item.setAttribute("value", key.toString()); |
492 |
item.addContent(createTranslationElement("label", rld.get(key))); |
item.addContent(createTranslationElement("label", rld.get(key))); |
493 |
element.addContent(item); |
element.addContent(item); |
507 |
* rounded to (null means no round; >= 0 means digits after |
* rounded to (null means no round; >= 0 means digits after |
508 |
* comma; < 0 means digits before comma) |
* comma; < 0 means digits before comma) |
509 |
*/ |
*/ |
510 |
public static RasterLegendData generateRasterLegendData(ColorMap colorMap, |
public static RasterLegendData generateRasterLegendData( |
511 |
boolean paintGaps, Integer digits) { |
final ColorMap colorMap, final boolean paintGaps, |
512 |
DecimalFormat decFormat = digits != null ? new DecimalFormat(SwingUtil |
final Integer digits) { |
513 |
.getNumberFormatPattern(digits)) : null; |
final DecimalFormat decFormat = digits != null ? new DecimalFormat( |
514 |
RasterLegendData rld = new RasterLegendData(paintGaps); |
SwingUtil.getNumberFormatPattern(digits)) : null; |
515 |
for (ColorMapEntry cme : colorMap.getColorMapEntries()) { |
final RasterLegendData rld = new RasterLegendData(paintGaps); |
516 |
double value = StylingUtil.getQuantityFromColorMapEntry(cme); |
for (final ColorMapEntry cme : colorMap.getColorMapEntries()) { |
517 |
|
final double value = StylingUtil.getQuantityFromColorMapEntry(cme); |
518 |
String label = cme.getLabel(); |
String label = cme.getLabel(); |
519 |
// if no label is set (e.g. quantitative style), |
// if no label is set (e.g. quantitative style), |
520 |
// use the value as label |
// use the value as label |
540 |
* rounded to (null means no round; >= 0 means digits after |
* rounded to (null means no round; >= 0 means digits after |
541 |
* comma; < 0 means digits before comma) |
* comma; < 0 means digits before comma) |
542 |
*/ |
*/ |
543 |
public static RasterLegendData generateRasterLegendData(Style style, |
public static RasterLegendData generateRasterLegendData(final Style style, |
544 |
boolean paintGaps, Integer digits) { |
final boolean paintGaps, final Integer digits) { |
545 |
ColorMap colorMap = StylingUtil.getColorMapFromStyle(style); |
final ColorMap colorMap = StylingUtil.getColorMapFromStyle(style); |
546 |
if (colorMap == null) |
if (colorMap == null) |
547 |
throw new IllegalArgumentException( |
throw new IllegalArgumentException( |
548 |
"Color map can not be determined from style!"); |
"Color map can not be determined from style!"); |
575 |
* {@link Element} to parse |
* {@link Element} to parse |
576 |
*/ |
*/ |
577 |
public final static Translation parseTranslation(final Element element) { |
public final static Translation parseTranslation(final Element element) { |
578 |
Translation trans = new Translation(); |
final Translation trans = new Translation(); |
579 |
|
|
580 |
if (element == null) |
if (element == null) |
581 |
return trans; |
return trans; |
617 |
* @param translation |
* @param translation |
618 |
* Translation to store in the Element |
* Translation to store in the Element |
619 |
*/ |
*/ |
620 |
public final static Element createTranslationElement(String tagname, |
public final static Element createTranslationElement(final String tagname, |
621 |
Translation translation) { |
final Translation translation) { |
622 |
Element element = new Element(tagname, AMLURI); |
final Element element = new Element(tagname, AMLURI); |
623 |
if (translation == null) |
if (translation == null) |
624 |
throw new UnsupportedOperationException( |
throw new UnsupportedOperationException( |
625 |
"Translation element can not be created from null!"); |
"Translation element can not be created from null!"); |
635 |
|
|
636 |
// add a <translation lang="..">..</tranlation> part to the element for |
// add a <translation lang="..">..</tranlation> part to the element for |
637 |
// all languages |
// all languages |
638 |
for (String lang : translation.keySet()) { |
for (final String lang : translation.keySet()) { |
639 |
Element translationElement = new Element(ELEM_NAME_TRANSLATION, |
final Element translationElement = new Element( |
640 |
AMLURI); |
ELEM_NAME_TRANSLATION, AMLURI); |
641 |
translationElement.setAttribute("lang", lang); |
translationElement.setAttribute("lang", lang); |
642 |
String translationString = translation.get(lang); |
String translationString = translation.get(lang); |
643 |
if (translationString == null) |
if (translationString == null) |
657 |
* @param style |
* @param style |
658 |
* a Style |
* a Style |
659 |
*/ |
*/ |
660 |
public static void setStyledLayerStyle(StyledLayerInterface styledObject, |
public static void setStyledLayerStyle( |
661 |
StyledLayerStyle<?> style) { |
final StyledLayerInterface styledObject, |
662 |
|
final StyledLayerStyle<?> style) { |
663 |
// set SLD style |
// set SLD style |
664 |
styledObject.setStyle(style.getGeoObjectStyle()); |
styledObject.setStyle(style.getGeoObjectStyle()); |
665 |
// set meta data |
// set meta data |
666 |
if (styledObject instanceof StyledGridCoverageInterface |
if (styledObject instanceof StyledGridCoverageInterface |
667 |
&& (style.getMetaData() instanceof RasterLegendData || style |
&& (style.getMetaData() instanceof RasterLegendData || style |
668 |
.getMetaData() == null)) { |
.getMetaData() == null)) { |
669 |
RasterLegendData sourceRld = (RasterLegendData) style.getMetaData(); |
final RasterLegendData sourceRld = (RasterLegendData) style |
670 |
RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject) |
.getMetaData(); |
671 |
|
final RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject) |
672 |
.getLegendMetaData(); |
.getLegendMetaData(); |
673 |
if (destRld != null && sourceRld != null) { |
if (destRld != null && sourceRld != null) { |
674 |
destRld.setPaintGaps(sourceRld.isPaintGaps()); |
destRld.setPaintGaps(sourceRld.isPaintGaps()); |
679 |
} |
} |
680 |
if (styledObject instanceof StyledFeatureCollectionInterface |
if (styledObject instanceof StyledFeatureCollectionInterface |
681 |
&& (style.getMetaData() instanceof Map || style.getMetaData() == null)) { |
&& (style.getMetaData() instanceof Map || style.getMetaData() == null)) { |
682 |
Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>) style |
final AttributeMetadataMap sourceAmd = (AttributeMetadataMap) style |
683 |
.getMetaData(); |
.getMetaData(); |
684 |
Map<Integer, AttributeMetaData> destAmd = ((StyledFeatureCollectionInterface) styledObject) |
final AttributeMetadataMap destAmd = ((StyledFeatureCollectionInterface) styledObject) |
685 |
.getAttributeMetaDataMap(); |
.getAttributeMetaDataMap(); |
686 |
if (destAmd != null && sourceAmd != null) { |
if (destAmd != null && sourceAmd != null) { |
687 |
destAmd.clear(); |
destAmd.clear(); |
706 |
* @param styledObject |
* @param styledObject |
707 |
* a styled object |
* a styled object |
708 |
* @return {@code StyledLayerStyle<RasterLegendData>} for |
* @return {@code StyledLayerStyle<RasterLegendData>} for |
709 |
* {@link StyledGridCoverageInterface} or {@code |
* {@link StyledGridCoverageInterface} or |
710 |
* StyledLayerStyle<Map<Integer,AttributeMetaData>>} for |
* {@code StyledLayerStyle<Map<Integer,AttributeMetaData>>} for |
711 |
* {@link StyledFeatureCollectionInterface} |
* {@link StyledFeatureCollectionInterface} |
712 |
*/ |
*/ |
713 |
public static StyledLayerStyle<?> getStyledLayerStyle( |
public static StyledLayerStyle<?> getStyledLayerStyle( |
714 |
StyledLayerInterface styledObject) { |
final StyledLayerInterface styledObject) { |
715 |
if (styledObject instanceof StyledGridCoverageInterface) |
if (styledObject instanceof StyledGridCoverageInterface) |
716 |
return getStyledLayerStyle((StyledGridCoverageInterface) styledObject); |
return getStyledLayerStyle((StyledGridCoverageInterface) styledObject); |
717 |
if (styledObject instanceof StyledFeatureCollectionInterface) |
if (styledObject instanceof StyledFeatureCollectionInterface) |
730 |
* a styled grid coverage |
* a styled grid coverage |
731 |
*/ |
*/ |
732 |
public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle( |
public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle( |
733 |
StyledGridCoverageInterface styledGC) { |
final StyledGridCoverageInterface styledGC) { |
734 |
return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(), |
return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(), |
735 |
styledGC.getLegendMetaData()); |
styledGC.getLegendMetaData()); |
736 |
} |
} |
742 |
* @param styledFC |
* @param styledFC |
743 |
* a styled feature collection |
* a styled feature collection |
744 |
*/ |
*/ |
745 |
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> getStyledLayerStyle( |
public static StyledLayerStyle<AttributeMetadataMap> getStyledLayerStyle( |
746 |
StyledFeatureCollectionInterface styledFC) { |
final StyledFeatureCollectionInterface styledFC) { |
747 |
return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(styledFC |
return new StyledLayerStyle<AttributeMetadataMap>(styledFC.getStyle(), |
748 |
.getStyle(), styledFC.getAttributeMetaDataMap()); |
styledFC.getAttributeMetaDataMap()); |
749 |
} |
} |
750 |
|
|
751 |
/** |
/** |
762 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
763 |
*/ |
*/ |
764 |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
765 |
URL geoObjectURL, String sldExt, String rldExt) { |
final URL geoObjectURL, final String sldExt, final String rldExt) { |
766 |
RasterLegendData metaData = null; |
RasterLegendData metaData = null; |
767 |
Style sldStyle = null; |
Style sldStyle = null; |
768 |
try { |
try { |
769 |
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
770 |
geoObjectURL, sldExt)); |
geoObjectURL, sldExt)); |
771 |
// SLD must be present |
// SLD must be present |
772 |
if (styles == null || styles.length == 0) |
if (styles == null || styles.length == 0) |
773 |
return null; |
return null; |
774 |
sldStyle = styles[0]; |
sldStyle = styles[0]; |
775 |
} catch (Exception err) { |
} catch (final Exception err) { |
776 |
// SLD must be present |
// SLD must be present |
777 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
778 |
return null; |
return null; |
781 |
try { |
try { |
782 |
metaData = StyledLayerUtil.loadRasterLegendData(IOUtil |
metaData = StyledLayerUtil.loadRasterLegendData(IOUtil |
783 |
.changeUrlExt(geoObjectURL, rldExt)); |
.changeUrlExt(geoObjectURL, rldExt)); |
784 |
} catch (FileNotFoundException err) { |
} catch (final FileNotFoundException err) { |
785 |
// ignore missing raster legend data |
// ignore missing raster legend data |
786 |
} catch (Exception err) { |
} catch (final Exception err) { |
787 |
// any other error during legend data creation leads to error |
// any other error during legend data creation leads to error |
788 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
789 |
return null; |
return null; |
806 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
807 |
*/ |
*/ |
808 |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
809 |
URL geoObjectURL) { |
final URL geoObjectURL) { |
810 |
return loadStyledRasterStyle(geoObjectURL, "sld", "rld"); |
return loadStyledRasterStyle(geoObjectURL, "sld", "rld"); |
811 |
} |
} |
812 |
|
|
813 |
/** |
/** |
814 |
* Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData |
* Loads a {@linkplain Style SLD-Style} and a |
815 |
* AttributeMetaData-Map} for a given geo-object (feature) source. The SLD |
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} for a given |
816 |
* file must be present. A missing attribute meta-data file is tolerated. |
* geo-object (feature) source. The SLD file must be present. A missing |
817 |
|
* attribute meta-data file is tolerated. |
818 |
* |
* |
819 |
* @param geoObjectURL |
* @param geoObjectURL |
820 |
* URL of the (already read) feature object |
* URL of the (already read) feature object |
824 |
* file extention for the raster legend-data file |
* file extention for the raster legend-data file |
825 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
826 |
*/ |
*/ |
827 |
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle( |
public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle( |
828 |
URL geoObjectURL, String sldExt, String rldExt) { |
final URL geoObjectURL, final String sldExt, final String rldExt) { |
829 |
Map<Integer, AttributeMetaData> metaData = null; |
AttributeMetadataMap metaData = null; |
830 |
Style sldStyle = null; |
Style sldStyle = null; |
831 |
try { |
try { |
832 |
Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
833 |
geoObjectURL, sldExt)); |
geoObjectURL, sldExt)); |
834 |
// SLD must be present |
// SLD must be present |
835 |
if (styles == null || styles.length == 0) |
if (styles == null || styles.length == 0) |
836 |
return null; |
return null; |
837 |
sldStyle = styles[0]; |
sldStyle = styles[0]; |
838 |
} catch (Exception err) { |
} catch (final Exception err) { |
839 |
// SLD must be present |
// SLD must be present |
840 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
841 |
return null; |
return null; |
844 |
try { |
try { |
845 |
metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil |
metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil |
846 |
.changeUrlExt(geoObjectURL, rldExt)); |
.changeUrlExt(geoObjectURL, rldExt)); |
847 |
} catch (FileNotFoundException err) { |
} catch (final FileNotFoundException err) { |
848 |
// ignore missing attribute meta data |
// ignore missing attribute meta data |
849 |
} catch (Exception err) { |
} catch (final Exception err) { |
850 |
// any other error during meta data creation leads to error |
// any other error during meta data creation leads to error |
851 |
LangUtil.logDebugError(LOGGER, err); |
LangUtil.logDebugError(LOGGER, err); |
852 |
return null; |
return null; |
853 |
} |
} |
854 |
|
|
855 |
return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(sldStyle, |
return new StyledLayerStyle<AttributeMetadataMap>(sldStyle, metaData); |
|
metaData); |
|
856 |
} |
} |
857 |
|
|
858 |
/** |
/** |
859 |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
860 |
* {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd} |
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a |
861 |
* file for a given geo-object (feature) source. The SLD file must be |
* {@code .amd} file for a given geo-object (feature) source. The SLD file |
862 |
* present. A missing attribute meta-data file is tolerated. |
* must be present. A missing attribute meta-data file is tolerated. |
863 |
* |
* |
864 |
* @param geoObjectURL |
* @param geoObjectURL |
865 |
* URL of the (already read) feature object |
* URL of the (already read) feature object |
869 |
* file extention for the raster legend-data file |
* file extention for the raster legend-data file |
870 |
* @return {@code null} in case of any error |
* @return {@code null} in case of any error |
871 |
*/ |
*/ |
872 |
public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle( |
public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle( |
873 |
URL geoObjectURL) { |
final URL geoObjectURL) { |
874 |
return loadStyledFeatureStyle(geoObjectURL, "sld", "amd"); |
return loadStyledFeatureStyle(geoObjectURL, "sld", "amd"); |
875 |
} |
} |
876 |
|
|
887 |
* @param mdExt |
* @param mdExt |
888 |
* file extention for the meta-data file |
* file extention for the meta-data file |
889 |
*/ |
*/ |
890 |
public static <T> void saveStyledLayerStyle(StyledLayerStyle<T> style, |
public static <T> void saveStyledLayerStyle( |
891 |
URL geoObjectURL, String sldExt, String mdExt) throws Exception { |
final StyledLayerStyle<T> style, final URL geoObjectURL, |
892 |
|
final String sldExt, final String mdExt) throws Exception { |
893 |
// Store the SLD |
// Store the SLD |
894 |
Style sldStyle = style.getGeoObjectStyle(); |
final Style sldStyle = style.getGeoObjectStyle(); |
895 |
if (sldStyle != null) { |
if (sldStyle != null) { |
896 |
StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File( |
StylingUtil.saveStyleToSld(sldStyle, IOUtil.changeFileExt(new File( |
897 |
geoObjectURL.toURI()), sldExt)); |
geoObjectURL.toURI()), sldExt)); |
898 |
} |
} |
899 |
|
|
900 |
// Store the meta data |
// Store the meta data |
901 |
T metaData = style.getMetaData(); |
final T metaData = style.getMetaData(); |
902 |
if (metaData != null) { |
if (metaData != null) { |
903 |
if (metaData instanceof RasterLegendData) { |
if (metaData instanceof RasterLegendData) { |
904 |
saveRasterLegendData((RasterLegendData) metaData, IOUtil |
saveRasterLegendData((RasterLegendData) metaData, |
905 |
.changeUrlExt(geoObjectURL, mdExt)); |
IOUtil.changeUrlExt(geoObjectURL, mdExt)); |
906 |
// } else if ( metaData instanceof |
// } else if ( metaData instanceof |
907 |
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
908 |
// KOMPILIERBAR!! |
// KOMPILIERBAR!! |
909 |
} else if (metaData instanceof Map) { |
} else if (metaData instanceof Map) { |
910 |
saveAttributeMetaDataMap( |
saveAttributeMetaDataMap((AttributeMetadataMap) metaData, |
911 |
(Map<Integer, AttributeMetaData>) metaData, IOUtil |
IOUtil.changeUrlExt(geoObjectURL, mdExt)); |
|
.changeUrlExt(geoObjectURL, mdExt)); |
|
912 |
} else |
} else |
913 |
throw new UnsupportedOperationException( |
throw new UnsupportedOperationException( |
914 |
"Export for meta data not yet supported: " |
"Export for meta data not yet supported: " |
918 |
|
|
919 |
/** |
/** |
920 |
* 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 |
921 |
* meta data ({@link RasterLegendData} or {@link AttributeMetaData}) to a |
* meta data ({@link RasterLegendData} or {@link AttributeMetadataImpl}) to |
922 |
* {@code .rld} or {@code .amd} file. for a given geo-object source. |
* a {@code .rld} or {@code .amd} file. for a given geo-object source. |
923 |
* |
* |
924 |
* @param style |
* @param style |
925 |
* style to save |
* style to save |
926 |
* @param geoObjectURL |
* @param geoObjectURL |
927 |
* URL of the (already read) raster object |
* URL of the (already read) raster object |
928 |
*/ |
*/ |
929 |
public static void saveStyledLayerStyle(StyledLayerStyle<?> style, |
public static void saveStyledLayerStyle(final StyledLayerStyle<?> style, |
930 |
URL geoObjectURL) throws Exception { |
final URL geoObjectURL) throws Exception { |
931 |
if (style.getMetaData() instanceof RasterLegendData) |
if (style.getMetaData() instanceof RasterLegendData) |
932 |
saveStyledLayerStyle(style, geoObjectURL, "sld", "rld"); |
saveStyledLayerStyle(style, geoObjectURL, "sld", "rld"); |
933 |
else |
else |
935 |
} |
} |
936 |
|
|
937 |
/** |
/** |
938 |
* Creates a {@link Box} that shows a legend for a list of |
* Creates a {@link JPanel} that shows a legend for a list of |
939 |
* {@link FeatureTypeStyle}s and a targeted featureType |
* {@link FeatureTypeStyle}s and a targeted featureType |
940 |
* |
* |
941 |
|
* @param style |
942 |
|
* The Style to presented in this legend |
943 |
* @param featureType |
* @param featureType |
944 |
* If this a legend for Point, Polygon or Line? |
* If this a legend for Point, Polygon or Line? Or ANY or NONE? |
945 |
* @param list |
* |
946 |
* The Styles to presented in this legend |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
947 |
* |
*/ |
948 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
public static JPanel createLegendSwingPanel(Style style, |
949 |
* Krüger</a> |
final SimpleFeatureType featureType, final int iconWidth, |
950 |
*/ |
final int iconHeight) { |
951 |
public static Box createLegendPanel(List<FeatureTypeStyle> list, |
|
952 |
SimpleFeatureType featureType, int iconWidth, int iconHeight) { |
if (featureType == null) { |
953 |
|
ExceptionDialog.show(new IllegalStateException( |
954 |
Box box = new Box(BoxLayout.Y_AXIS) { |
"featureType is null!")); |
955 |
|
return new JPanel(); |
956 |
/** |
} |
|
* Nuetzlich wenn die Componente gedruckt (z.B. wenn ein Screenshot |
|
|
* gemacht wird) wird. Dann werden wird der Hintergrund auf WEISS |
|
|
* gesetzt. |
|
|
* |
|
|
* @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); |
|
|
} |
|
|
} |
|
|
}; |
|
957 |
|
|
958 |
for (FeatureTypeStyle ftStyle : list) { |
final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]")); |
959 |
|
if (style == null) { |
960 |
|
// No Style => no legend |
961 |
|
return panel; |
962 |
|
} |
963 |
|
|
964 |
|
// Reversing the list, because a point that is painted above a polygon |
965 |
|
// should be higher in the list. |
966 |
|
final List<FeatureTypeStyle> ftsList = style.featureTypeStyles(); |
967 |
|
Collections.reverse(ftsList); |
968 |
|
for (final FeatureTypeStyle ftStyle : ftsList) { |
969 |
|
|
970 |
|
// // Try to import the FeatureType into an AtlasStyler RuleList to |
971 |
|
// // determine whether this RL is actually disabled. |
972 |
|
// StylingUtil.sldToString(ftStyle).contains(RL_DISABLED_FILTER.toString()) |
973 |
|
// return; |
974 |
|
|
975 |
// One child-node for every rule |
// One child-node for every rule |
976 |
List<Rule> rules = ftStyle.rules(); |
final List<Rule> rules = ftStyle.rules(); |
977 |
for (Rule rule : rules) { |
for (final Rule rule : rules) { |
978 |
|
|
979 |
|
// Check if this RULE shall actually appear in the legend |
980 |
|
if (rule.getName() != null |
981 |
|
&& rule.getName().contains(HIDE_IN_LAYER_LEGEND_HINT)) |
982 |
|
continue; |
983 |
|
|
984 |
/** |
/** |
985 |
* Let's not create a hbox for Rules that only contain |
* Let's not create a hbox for Rules that only contain |
989 |
.size() == rule.getSymbolizers().length) |
.size() == rule.getSymbolizers().length) |
990 |
continue; |
continue; |
991 |
|
|
|
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 |
|
|
// **************************************************************************** |
|
992 |
final BufferedImage imageForRule = LegendIconFeatureRenderer |
final BufferedImage imageForRule = LegendIconFeatureRenderer |
993 |
.getInstance().createImageForRule(rule, featureType, |
.getInstance().createImageForRule(rule, featureType, |
994 |
ICON_SIZE); |
new Dimension(iconWidth, iconHeight)); |
995 |
|
|
996 |
// LOGGER.debug("Creating a new Legend Image for RUle name = |
final ImageIcon legendIcon = new ImageIcon(imageForRule); |
|
// "+rule.getName()); |
|
|
|
|
|
ImageIcon legendIcon = new ImageIcon(imageForRule); |
|
997 |
|
|
998 |
final JLabel iconLabel = new JLabel(legendIcon); |
final JLabel iconLabel = new JLabel(legendIcon); |
999 |
hbox.setAlignmentX(0f); |
panel.add(iconLabel, "sgx1"); |
1000 |
hbox.add(iconLabel); |
// hbox.setAlignmentX(0f); |
1001 |
hbox.add(Box.createHorizontalStrut(3)); |
// hbox.add(iconLabel); |
1002 |
|
// hbox.add(Box.createHorizontalStrut(3)); |
1003 |
|
|
1004 |
Translation labelT = new Translation(); |
final Translation labelT = new Translation(); |
1005 |
labelT.fromOneLine(rule.getDescription().getTitle()); |
labelT.fromOneLine(rule.getDescription().getTitle()); |
|
|
|
1006 |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
|
hbox.add(classTitleLabel); |
|
|
classTitleLabel.setLabelFor(iconLabel); |
|
|
|
|
|
box.add(hbox); |
|
1007 |
|
|
1008 |
|
panel.add(classTitleLabel, "sgx2"); |
1009 |
|
classTitleLabel.setLabelFor(iconLabel); |
1010 |
} |
} |
1011 |
} |
} |
1012 |
|
|
1013 |
return box; |
return panel; |
1014 |
} |
} |
1015 |
|
|
1016 |
/** |
/** |
1017 |
* Creates a |
* Creates a {@link JComponent} that contains a legend for a given |
1018 |
|
* {@link StyledRasterInterface} and a given {@link Style}. |
1019 |
* |
* |
1020 |
* @param styledRaster |
* @param style |
1021 |
* @param iconHeight |
* if <code>null</code>, the default {@link Style} is extracetd |
1022 |
* @param iconWidth |
* from the {@link StyledRasterInterface} |
|
* @return |
|
1023 |
*/ |
*/ |
1024 |
public static Box createLegendPanel(StyledRasterInterface<?> styledRaster, |
public static JPanel createLegendSwingPanel( |
1025 |
int iconWidth, int iconHeight) { |
final StyledRasterInterface<?> styledRaster, Style style, |
1026 |
|
final int iconWidth, final int iconHeight) { |
1027 |
|
|
1028 |
|
// If no style is given, we use the default style for this layer |
1029 |
|
if (style == null) |
1030 |
|
style = styledRaster.getStyle(); |
1031 |
|
|
1032 |
/** |
/** |
1033 |
* Determine whether a Style is responsible for the coloring |
* Determine whether a Style is responsible for the coloring |
1034 |
*/ |
*/ |
1035 |
ColorModel colorModel = null; |
ColorModel colorModel = null; |
1036 |
if (!isStyleable(styledRaster) |
if (!isStyleable(styledRaster) |
1037 |
|| (isStyleable(styledRaster) && styledRaster.getStyle() == null)) { |
|| (isStyleable(styledRaster) && style == null)) { |
1038 |
colorModel = getColorModel(styledRaster); |
colorModel = getColorModel(styledRaster); |
1039 |
} |
} |
1040 |
|
|
1041 |
RasterLegendData rasterLegendData = styledRaster.getLegendMetaData(); |
final RasterLegendData rasterLegendData = styledRaster |
1042 |
List<Double> legendRasterValues = rasterLegendData.getSortedKeys(); |
.getLegendMetaData(); |
1043 |
Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
final List<Double> legendRasterValues = rasterLegendData |
1044 |
|
.getSortedKeys(); |
1045 |
|
final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
1046 |
.createSampleRasters(); |
.createSampleRasters(); |
1047 |
|
|
1048 |
Box box = new Box(BoxLayout.Y_AXIS) { |
final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0")); |
|
|
|
|
/** |
|
|
* 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) { |
|
1049 |
|
|
1050 |
/** |
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); |
|
|
} |
|
|
} |
|
|
}; |
|
1051 |
|
|
1052 |
final Dimension ICON_SIZE = new Dimension(iconWidth, |
// final Dimension ICON_SIZE = new Dimension(iconWidth, |
1053 |
new JLabel().getFontMetrics(new JLabel().getFont()) |
// new JLabel().getFontMetrics(new JLabel().getFont()) |
1054 |
.getHeight() > 5 ? new JLabel().getFontMetrics( |
// .getHeight() > 5 ? new JLabel().getFontMetrics( |
1055 |
new JLabel().getFont()).getHeight() : iconHeight); |
// new JLabel().getFont()).getHeight() : iconHeight); |
1056 |
|
|
1057 |
// **************************************************************************** |
// **************************************************************************** |
1058 |
// Create the actual icon |
// Create the actual icon |
1059 |
// **************************************************************************** |
// **************************************************************************** |
1060 |
BufferedImage buffImage = new BufferedImage(ICON_SIZE.width, |
final BufferedImage buffImage = new BufferedImage(iconWidth, |
1061 |
ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB); |
iconHeight, BufferedImage.TYPE_INT_ARGB); |
1062 |
Graphics2D graphics = buffImage.createGraphics(); |
|
1063 |
|
final Graphics2D graphics = buffImage.createGraphics(); |
1064 |
|
|
1065 |
if (colorModel != null) { |
if (colorModel != null) { |
1066 |
|
// The colors come from the ColorModel! |
1067 |
|
|
1068 |
try { |
try { |
1069 |
Object inData = null; |
Object inData = null; |
1092 |
final Color color = new Color(colorModel.getRGB(inData)); |
final Color color = new Color(colorModel.getRGB(inData)); |
1093 |
graphics.setBackground(color); |
graphics.setBackground(color); |
1094 |
graphics.setColor(color); |
graphics.setColor(color); |
1095 |
graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height); |
graphics.fillRect(0, 0, iconWidth, iconHeight); |
1096 |
} catch (Exception e) { |
} catch (final Exception e) { |
1097 |
LOGGER.debug("Dann nehmen wir halt den GridCoverageRenderer", e); |
LOGGER.info( |
1098 |
|
"Dann nehmen wir halt den GridCoverageRenderer", e); |
1099 |
colorModel = null; |
colorModel = null; |
1100 |
} |
} |
1101 |
} |
} else { |
1102 |
|
// The colors come from the Style |
|
if (colorModel == null) { |
|
1103 |
|
|
1104 |
/** |
/** |
1105 |
* The coverage contains only one value of value rValue |
* The coverage contains only one value of value rValue |
1106 |
*/ |
*/ |
1107 |
GridCoverage2D sampleCov = sampleRasters.get(rValue); |
final GridCoverage2D sampleCov = sampleRasters.get(rValue); |
1108 |
GridCoverageRenderer renderer; |
GridCoverageRenderer renderer; |
1109 |
try { |
try { |
1110 |
renderer = new GridCoverageRenderer(sampleCov |
renderer = new GridCoverageRenderer( |
1111 |
.getCoordinateReferenceSystem(), JTSUtil |
sampleCov.getCoordinateReferenceSystem(), |
1112 |
.createEnvelope(sampleCov.getEnvelope()), |
JTSUtil.createEnvelope(sampleCov.getEnvelope()), |
1113 |
new Rectangle(iconWidth, iconHeight), |
new Rectangle(iconWidth, iconHeight), |
1114 |
(AffineTransform) null); |
(AffineTransform) null); |
1115 |
} catch (Exception e1) { |
} catch (final Exception e1) { |
1116 |
throw new RuntimeException( |
throw new RuntimeException( |
1117 |
"Creating the GridCoverageRenderer:", e1); |
"Creating a GridCoverageRenderer failed:", e1); |
1118 |
} |
} |
1119 |
|
|
1120 |
/** |
/** |
1121 |
* Iterate over all FeatureTypeStyles. |
* Iterate over all FeatureTypeStyles. |
1122 |
*/ |
*/ |
1123 |
// 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 |
|
1124 |
.getRasterSymbolizers(style); |
.getRasterSymbolizers(style); |
1125 |
|
|
1126 |
for (RasterSymbolizer symbolizer : rSymbols) { |
for (final RasterSymbolizer symbolizer : rSymbols) { |
|
// LOGGER.debug("Creating a new Legend Image for RUle |
|
|
// name = |
|
|
// "+rule.getName()); |
|
1127 |
try { |
try { |
1128 |
renderer.paint(graphics, sampleCov, symbolizer); |
renderer.paint(graphics, sampleCov, symbolizer); |
1129 |
} catch (Exception ee) { |
} catch (final Exception ee) { |
1130 |
LOGGER.error("Unable to paint " + symbolizer |
LOGGER.error("Unable to paint " + symbolizer |
1131 |
+ " into the legend image", ee); |
+ " into the legend image", ee); |
1132 |
} |
} |
|
// } |
|
|
// } |
|
1133 |
} |
} |
1134 |
} |
} |
1135 |
|
|
1136 |
ImageIcon legendIcon = new ImageIcon(buffImage); |
final JLabel iconLabel = new JLabel(new ImageIcon(buffImage)); |
1137 |
|
panel.add(iconLabel, "sgx1"); |
|
final JLabel iconLabel = new JLabel(legendIcon); |
|
|
hbox.setAlignmentX(0f); |
|
|
hbox.add(iconLabel); |
|
|
hbox.add(Box.createHorizontalStrut(3)); |
|
1138 |
|
|
1139 |
Translation labelT = rasterLegendData.get(rValue); |
final Translation labelT = rasterLegendData.get(rValue); |
1140 |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
1141 |
hbox.add(classTitleLabel); |
panel.add(classTitleLabel, "sgx2" |
1142 |
|
+ (rasterLegendData.isPaintGaps() ? ", gapy 0:0:0 5:5:5" |
1143 |
|
: "")); |
1144 |
classTitleLabel.setLabelFor(iconLabel); |
classTitleLabel.setLabelFor(iconLabel); |
1145 |
|
|
1146 |
box.add(hbox); |
if (rasterLegendData.isPaintGaps()) { |
|
|
|
|
if (rasterLegendData.getPaintGaps()) { |
|
1147 |
iconLabel |
iconLabel |
1148 |
.setBorder(BorderFactory.createLineBorder(Color.black)); |
.setBorder(BorderFactory.createLineBorder(Color.black)); |
|
box.add(Box.createVerticalStrut(3)); |
|
1149 |
} |
} |
1150 |
|
|
1151 |
} |
} |
1152 |
|
|
1153 |
return box; |
return panel; |
1154 |
} |
} |
1155 |
|
|
1156 |
/** |
/** |
1157 |
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
1158 |
* return <code>null</code> if the geoobject can not be accessed. |
* return <code>null</code> if the geoobject can not be accessed. |
1159 |
*/ |
*/ |
1160 |
public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) { |
@SuppressWarnings("unchecked") |
1161 |
|
public static ColorModel getColorModel( |
1162 |
|
final StyledRasterInterface<?> styledGrid) { |
1163 |
ColorModel colorModel = null; |
ColorModel colorModel = null; |
1164 |
try { |
try { |
1165 |
Object geoObject = styledGrid.getGeoObject(); |
final Object geoObject = styledGrid.getGeoObject(); |
1166 |
if (geoObject instanceof GridCoverage2D) { |
if (geoObject instanceof GridCoverage2D) { |
1167 |
GridCoverage2D cov = (GridCoverage2D) geoObject; |
final GridCoverage2D cov = (GridCoverage2D) geoObject; |
1168 |
colorModel = cov.getRenderedImage().getColorModel(); |
colorModel = cov.getRenderedImage().getColorModel(); |
1169 |
} else if (styledGrid instanceof StyledRasterPyramidInterface) { |
} else if (styledGrid instanceof StyledGridCoverageReaderInterface) { |
1170 |
|
|
1171 |
Parameter readGG = new Parameter( |
final Parameter readGG = new Parameter( |
1172 |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
1173 |
|
|
1174 |
ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope( |
final ReferencedEnvelope mapExtend = new ReferencedEnvelope( |
1175 |
styledGrid.getEnvelope(), styledGrid.getCrs()); |
styledGrid.getEnvelope(), styledGrid.getCrs()); |
1176 |
|
|
1177 |
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
1178 |
new Rectangle(0, 0, 10, 10)), mapExtend)); |
new Rectangle(0, 0, 1, 1)), mapExtend)); |
1179 |
|
|
1180 |
|
AbstractGridCoverage2DReader aReader; |
1181 |
|
if (geoObject instanceof FeatureCollection) { |
1182 |
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject; |
1183 |
|
|
1184 |
|
aReader = (AbstractGridCoverage2DReader) FeatureUtil |
1185 |
|
.getWrappedGeoObject(rFc); |
1186 |
|
|
1187 |
final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) geoObject; |
} else if (geoObject instanceof AbstractGridCoverage2DReader) { |
1188 |
GridCoverage2D cov = (GridCoverage2D) aReader |
aReader = (AbstractGridCoverage2DReader) geoObject; |
1189 |
|
|
1190 |
|
} else |
1191 |
|
throw new RuntimeException("need a reader..."); |
1192 |
|
// |
1193 |
|
|
1194 |
|
final GridCoverage2D cov = aReader |
1195 |
.read(new GeneralParameterValue[] { readGG }); |
.read(new GeneralParameterValue[] { readGG }); |
1196 |
colorModel = cov.getRenderedImage().getColorModel(); |
colorModel = cov.getRenderedImage().getColorModel(); |
1197 |
} |
} |
1198 |
} catch (Exception e) { |
} catch (final Exception e) { |
1199 |
LOGGER.error("Error reading the colormodel from " + styledGrid); |
LOGGER.error("Error reading the colormodel from " + styledGrid, e); |
1200 |
return null; |
return null; |
1201 |
} |
} |
1202 |
return colorModel; |
return colorModel; |
1208 |
* with their own {@link ColorModel} and will ignore any |
* with their own {@link ColorModel} and will ignore any |
1209 |
* {@link RasterSymbolizer} = SLD. |
* {@link RasterSymbolizer} = SLD. |
1210 |
*/ |
*/ |
1211 |
public static boolean isStyleable(StyledRasterInterface<?> styledRaster) { |
public static boolean isStyleable( |
1212 |
ColorModel colorModel = getColorModel(styledRaster); |
final StyledRasterInterface<?> styledRaster) { |
1213 |
|
final ColorModel colorModel = getColorModel(styledRaster); |
1214 |
LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is " |
|
1215 |
+ colorModel.getClass().getSimpleName()); |
// LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is " |
1216 |
|
// + colorModel != null ? colorModel.getClass().getSimpleName() : |
1217 |
|
// "NULL"); |
1218 |
|
|
1219 |
if (colorModel == null) |
if (colorModel == null) |
1220 |
return true; |
return true; |
1221 |
if (colorModel instanceof ComponentColorModel) |
if (colorModel instanceof ComponentColorModel) |
1222 |
return true; |
return true; |
1223 |
|
if (colorModel instanceof IndexColorModel) |
1224 |
|
return true; |
1225 |
|
|
1226 |
return false; |
return false; |
1227 |
} |
} |
1228 |
|
|
1229 |
|
/** |
1230 |
|
* Set the given Style as the Style of the {@link MapLayer}, unless the |
1231 |
|
* styles are the same (not comparing selection stuff). If the |
1232 |
|
* {@link MapLayer}s {@link Style} is changed, the selection FTS is kept.<br/> |
1233 |
|
* Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a |
1234 |
|
* repaint, so only use it when needed. |
1235 |
|
* |
1236 |
|
* @return <code>true</code> if the {@link MapLayer}'s {@link Style} has |
1237 |
|
* been changed. |
1238 |
|
*/ |
1239 |
|
public static boolean updateMapLayerStyleIfChangedAndKeepSelection( |
1240 |
|
MapLayer mapLayer, Style style2) { |
1241 |
|
|
1242 |
|
Style mapLayerStyleCleaned = StylingUtil |
1243 |
|
.removeSelectionFeatureTypeStyle(mapLayer.getStyle()); |
1244 |
|
|
1245 |
|
Style newStyleCleaned = StylingUtil |
1246 |
|
.removeSelectionFeatureTypeStyle(style2); |
1247 |
|
|
1248 |
|
if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned, newStyleCleaned)) { |
1249 |
|
|
1250 |
|
// They are different when compared without SELECTION FTS! |
1251 |
|
|
1252 |
|
// Now let's copy any SELECTION FTS to the now style |
1253 |
|
FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil |
1254 |
|
.getSelectionFeatureTypeStyle(mapLayer.getStyle()); |
1255 |
|
if (selectionFeatureTypeStyle != null) { |
1256 |
|
newStyleCleaned.featureTypeStyles().add( |
1257 |
|
selectionFeatureTypeStyle); |
1258 |
|
// newStyleCleaned is not so clean anymore... We just alled a |
1259 |
|
// selcetion FTS |
1260 |
|
} |
1261 |
|
|
1262 |
|
mapLayer.setStyle(newStyleCleaned); |
1263 |
|
|
1264 |
|
return true; |
1265 |
|
|
1266 |
|
} else { |
1267 |
|
return false; |
1268 |
|
} |
1269 |
|
} |
1270 |
|
|
1271 |
|
/** |
1272 |
|
* After loading an atlas, the AttribteMetaData contains whatever is written |
1273 |
|
* in the XML. But the DBF may have changed! This method checks an |
1274 |
|
* {@link AttributeMetadataMap} against a schema and also corrects |
1275 |
|
* upperCase/lowerCase problems. It will also remove any geometry column |
1276 |
|
* attribute metadata. |
1277 |
|
*/ |
1278 |
|
/** |
1279 |
|
* After loading an atlas, the AttribteMetaData contains whatever is written |
1280 |
|
* in the XML. But the DBF may have changed! |
1281 |
|
*/ |
1282 |
|
public static void checkAttribMetaData( |
1283 |
|
AttributeMetadataMap<AttributeMetadataImpl> attributeMetaDataMap, |
1284 |
|
SimpleFeatureType schema) { |
1285 |
|
|
1286 |
|
if (schema == null) |
1287 |
|
throw new IllegalArgumentException("schmema may not be null!"); |
1288 |
|
|
1289 |
|
ArrayList<Name> willRemove = new ArrayList<Name>(); |
1290 |
|
|
1291 |
|
// 1. Check.. all attributes in the atm should be in the schema as well. |
1292 |
|
// maybe correct some upperCase/loweCase stuff |
1293 |
|
|
1294 |
|
for (AttributeMetadataInterface atm : attributeMetaDataMap.values()) { |
1295 |
|
|
1296 |
|
AttributeDescriptor foundDescr = schema |
1297 |
|
.getDescriptor(atm.getName()); |
1298 |
|
if (foundDescr == null) { |
1299 |
|
NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute( |
1300 |
|
schema, atm.getLocalName()); |
1301 |
|
if (bestMatch == null) |
1302 |
|
willRemove.add(atm.getName()); |
1303 |
|
else |
1304 |
|
atm.setName(bestMatch); |
1305 |
|
} else if (foundDescr instanceof GeometryDescriptor) { |
1306 |
|
// We don't want GeometryColumns in here |
1307 |
|
willRemove.add(atm.getName()); |
1308 |
|
} |
1309 |
|
} |
1310 |
|
|
1311 |
|
// Remove the ones that were not findable in the schema |
1312 |
|
for (Name removeName : willRemove) { |
1313 |
|
if (attributeMetaDataMap.remove(removeName) == null) { |
1314 |
|
LOGGER.warn("removing the AMData didn't work"); |
1315 |
|
} |
1316 |
|
} |
1317 |
|
|
1318 |
|
// 2. check... all attributes from the schema must have an ATM |
1319 |
|
for (AttributeDescriptor ad : schema.getAttributeDescriptors()) { |
1320 |
|
if (ad instanceof GeometryDescriptor) |
1321 |
|
continue; |
1322 |
|
if (!attributeMetaDataMap.containsKey(ad.getName())) { |
1323 |
|
attributeMetaDataMap.put(new NameImpl(ad.getName() |
1324 |
|
.getNamespaceURI(), ad.getName().getLocalPart()), |
1325 |
|
new AttributeMetadataImpl(ad, schema |
1326 |
|
.getAttributeDescriptors().indexOf(ad), |
1327 |
|
attributeMetaDataMap.getLanguages())); |
1328 |
|
} |
1329 |
|
} |
1330 |
|
} |
1331 |
|
|
1332 |
|
/** |
1333 |
|
* Checks every attribute name in the {@link AttributeMetadataMap} for its |
1334 |
|
* binding type. It the type is textual, add the mrpty string as a NODATA |
1335 |
|
* value. |
1336 |
|
* |
1337 |
|
* @param attributeMetaDataMap |
1338 |
|
* @param schema |
1339 |
|
*/ |
1340 |
|
public static void addEmptyStringToAllTextualAttributes( |
1341 |
|
AttributeMetadataMap<? extends AttributeMetadataInterface> attributeMetaDataMap, |
1342 |
|
SimpleFeatureType schema) { |
1343 |
|
|
1344 |
|
for (Name name : attributeMetaDataMap.keySet()) { |
1345 |
|
if (String.class.isAssignableFrom(schema.getDescriptor(name) |
1346 |
|
.getType().getBinding())) { |
1347 |
|
attributeMetaDataMap.get(name).getNodataValues().add(""); |
1348 |
|
} |
1349 |
|
} |
1350 |
|
} |
1351 |
|
|
1352 |
|
/** |
1353 |
|
* @return a nicely formatted String containing all NODATA values of any |
1354 |
|
* {@link AttributeMetadataInterface} object. Strings are quoted so |
1355 |
|
* that any empty {@link String} can be seen. |
1356 |
|
*/ |
1357 |
|
public static String formatNoDataValues(Set<Object> nodataValuesList) { |
1358 |
|
String nicelyFormatted = ""; |
1359 |
|
if (nodataValuesList != null) { |
1360 |
|
if (nodataValuesList.size() == 0) |
1361 |
|
nicelyFormatted = ""; |
1362 |
|
else { |
1363 |
|
for (Object ndo : nodataValuesList) { |
1364 |
|
if (ndo instanceof String) |
1365 |
|
nicelyFormatted += "\"" + ndo + "\""; |
1366 |
|
else |
1367 |
|
nicelyFormatted += ndo.toString(); |
1368 |
|
|
1369 |
|
nicelyFormatted += ","; |
1370 |
|
} |
1371 |
|
// Remove the extra comma |
1372 |
|
nicelyFormatted = nicelyFormatted.substring(0, |
1373 |
|
nicelyFormatted.length() - 1); |
1374 |
|
} |
1375 |
|
} |
1376 |
|
return nicelyFormatted; |
1377 |
|
} |
1378 |
|
|
1379 |
|
/** |
1380 |
|
* Creates a new {@link AttributeMetadataMap} with instances of |
1381 |
|
* {@link AttributeMetadataInterface} for every non-geometry attribute. |
1382 |
|
* Default NODATA values (like "" for String) are set. |
1383 |
|
*/ |
1384 |
|
public static AttributeMetadataMap<AttributeMetadataImpl> createDefaultAttributeMetadataMap( |
1385 |
|
SimpleFeatureType schema) { |
1386 |
|
AttributeMetadataImplMap attMap = new AttributeMetadataImplMap(); |
1387 |
|
|
1388 |
|
for (int i = 0; i < schema.getAttributeCount(); i++) { |
1389 |
|
AttributeDescriptor attDesc = schema.getDescriptor(i); |
1390 |
|
|
1391 |
|
if (Geometry.class.isAssignableFrom(attDesc.getType().getBinding())) { |
1392 |
|
// Ignore the Geometry column |
1393 |
|
continue; |
1394 |
|
} |
1395 |
|
|
1396 |
|
// TODO AttributeMetadataAS would be nicer, which would not work |
1397 |
|
// with Translations ;-) |
1398 |
|
AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
1399 |
|
new NameImpl(attDesc.getName().getNamespaceURI(), attDesc |
1400 |
|
.getName().getLocalPart()), attMap.getLanguages()); |
1401 |
|
|
1402 |
|
if (String.class.isAssignableFrom(attDesc.getType().getBinding())) { |
1403 |
|
// For Strings we add the "" as NODATA values |
1404 |
|
attMetaData.addNodataValue(""); |
1405 |
|
} |
1406 |
|
|
1407 |
|
attMap.put(attDesc.getName(), attMetaData); |
1408 |
|
} |
1409 |
|
return attMap; |
1410 |
|
} |
1411 |
} |
} |