1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
alfonx |
862 |
* Stefan A. Tzeggai - additional utility classes |
29 |
alfonx |
244 |
******************************************************************************/ |
30 |
|
|
package skrueger.geotools; |
31 |
|
|
|
32 |
alfonx |
397 |
import java.awt.Color; |
33 |
|
|
import java.awt.Dimension; |
34 |
alfonx |
400 |
import java.awt.Graphics2D; |
35 |
|
|
import java.awt.Rectangle; |
36 |
|
|
import java.awt.geom.AffineTransform; |
37 |
alfonx |
397 |
import java.awt.image.BufferedImage; |
38 |
alfonx |
403 |
import java.awt.image.ColorModel; |
39 |
alfonx |
405 |
import java.awt.image.ComponentColorModel; |
40 |
|
|
import java.awt.image.DataBuffer; |
41 |
alfonx |
244 |
import java.io.File; |
42 |
|
|
import java.io.FileNotFoundException; |
43 |
|
|
import java.io.FileWriter; |
44 |
|
|
import java.net.URL; |
45 |
|
|
import java.text.DecimalFormat; |
46 |
alfonx |
607 |
import java.util.ArrayList; |
47 |
alfonx |
244 |
import java.util.List; |
48 |
|
|
import java.util.Map; |
49 |
alfonx |
769 |
import java.util.Set; |
50 |
alfonx |
244 |
|
51 |
alfonx |
403 |
import javax.swing.BorderFactory; |
52 |
alfonx |
397 |
import javax.swing.ImageIcon; |
53 |
alfonx |
516 |
import javax.swing.JComponent; |
54 |
alfonx |
397 |
import javax.swing.JLabel; |
55 |
|
|
|
56 |
alfonx |
516 |
import net.miginfocom.swing.MigLayout; |
57 |
|
|
|
58 |
alfonx |
244 |
import org.apache.log4j.Logger; |
59 |
alfonx |
403 |
import org.geotools.coverage.grid.GeneralGridEnvelope; |
60 |
alfonx |
244 |
import org.geotools.coverage.grid.GridCoverage2D; |
61 |
alfonx |
403 |
import org.geotools.coverage.grid.GridGeometry2D; |
62 |
alfonx |
244 |
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader; |
63 |
alfonx |
405 |
import org.geotools.coverage.grid.io.AbstractGridFormat; |
64 |
alfonx |
244 |
import org.geotools.feature.FeatureCollection; |
65 |
alfonx |
464 |
import org.geotools.feature.NameImpl; |
66 |
alfonx |
403 |
import org.geotools.geometry.jts.ReferencedEnvelope; |
67 |
alfonx |
244 |
import org.geotools.map.DefaultMapLayer; |
68 |
|
|
import org.geotools.map.MapLayer; |
69 |
alfonx |
405 |
import org.geotools.parameter.Parameter; |
70 |
alfonx |
400 |
import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer; |
71 |
alfonx |
244 |
import org.geotools.styling.ColorMap; |
72 |
|
|
import org.geotools.styling.ColorMapEntry; |
73 |
alfonx |
397 |
import org.geotools.styling.FeatureTypeStyle; |
74 |
alfonx |
244 |
import org.geotools.styling.RasterSymbolizer; |
75 |
alfonx |
397 |
import org.geotools.styling.Rule; |
76 |
alfonx |
244 |
import org.geotools.styling.Style; |
77 |
|
|
import org.jdom.Document; |
78 |
|
|
import org.jdom.Element; |
79 |
|
|
import org.jdom.input.SAXBuilder; |
80 |
|
|
import org.jdom.output.XMLOutputter; |
81 |
alfonx |
420 |
import org.opengis.feature.simple.SimpleFeature; |
82 |
alfonx |
397 |
import org.opengis.feature.simple.SimpleFeatureType; |
83 |
alfonx |
607 |
import org.opengis.feature.type.AttributeDescriptor; |
84 |
|
|
import org.opengis.feature.type.GeometryDescriptor; |
85 |
alfonx |
464 |
import org.opengis.feature.type.Name; |
86 |
alfonx |
403 |
import org.opengis.parameter.GeneralParameterValue; |
87 |
alfonx |
244 |
|
88 |
alfonx |
400 |
import schmitzm.geotools.JTSUtil; |
89 |
alfonx |
420 |
import schmitzm.geotools.feature.FeatureUtil; |
90 |
alfonx |
244 |
import schmitzm.geotools.styling.StylingUtil; |
91 |
|
|
import schmitzm.io.IOUtil; |
92 |
|
|
import schmitzm.lang.LangUtil; |
93 |
alfonx |
516 |
import schmitzm.swing.JPanel; |
94 |
alfonx |
244 |
import schmitzm.swing.SwingUtil; |
95 |
alfonx |
788 |
import skrueger.AttributeMetadataImpl; |
96 |
alfonx |
772 |
import skrueger.AttributeMetadataInterface; |
97 |
alfonx |
244 |
import skrueger.RasterLegendData; |
98 |
|
|
import skrueger.i8n.Translation; |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* This class provides static helper methods for dealing with |
102 |
|
|
* {@link StyledLayerInterface} stuff. |
103 |
alfonx |
400 |
* |
104 |
|
|
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
105 |
|
|
* (University of Bonn/Germany) |
106 |
alfonx |
244 |
* @version 1.0 |
107 |
|
|
*/ |
108 |
|
|
public class StyledLayerUtil { |
109 |
alfonx |
400 |
private static final Logger LOGGER = Logger.getLogger(StyledLayerUtil.class |
110 |
|
|
.getName()); |
111 |
|
|
private static final SAXBuilder SAX_BUILDER = new SAXBuilder(); |
112 |
|
|
private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter(); |
113 |
alfonx |
244 |
|
114 |
alfonx |
400 |
/** URL for Atlas XML schema */ |
115 |
|
|
public static final String AMLURI = "http://www.wikisquare.de/AtlasML"; |
116 |
|
|
/** Name of the XML Element for the attribute meta data map */ |
117 |
|
|
public static final String ELEM_NAME_AMD = "attributeMetaData"; |
118 |
|
|
/** Name of the XML Element for the raster legend data */ |
119 |
|
|
public static final String ELEM_NAME_RLD = "rasterLegendData"; |
120 |
|
|
/** Name of the XML Element for an attribute meta data map entry */ |
121 |
|
|
public static final String ELEM_NAME_ATTRIBUTE = "dataAttribute"; |
122 |
|
|
/** Name of the XML Element for an raster legend data entry */ |
123 |
|
|
public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem"; |
124 |
|
|
/** Name of the XML Element for a translation */ |
125 |
|
|
public static final String ELEM_NAME_TRANSLATION = "translation"; |
126 |
alfonx |
244 |
|
127 |
alfonx |
400 |
/** |
128 |
|
|
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
129 |
|
|
* {@link StyledLayerInterface} then its sytle is used. In case of direct |
130 |
|
|
* Geotools objects ({@link GridCoverage2D}, |
131 |
|
|
* {@link AbstractGridCoverage2DReader}, {@link FeatureCollection}) a |
132 |
|
|
* default style is generated. |
133 |
|
|
* |
134 |
|
|
* @param object |
135 |
|
|
* an Object |
136 |
|
|
* @exception Exception |
137 |
|
|
* if {@code null} is given as object or an error occurs |
138 |
|
|
* during layer creation |
139 |
|
|
*/ |
140 |
alfonx |
516 |
public static MapLayer createMapLayer(final Object object) throws Exception { |
141 |
alfonx |
400 |
return createMapLayer(object, null); |
142 |
|
|
} |
143 |
alfonx |
244 |
|
144 |
alfonx |
400 |
/** |
145 |
|
|
* Creates a Geotools {@link MapLayer} from an object. If the object is a |
146 |
|
|
* {@link StyledLayerInterface} then its sytle is used. In case of direct |
147 |
|
|
* Geotools objects ({@link GridCoverage2D}, |
148 |
|
|
* {@link AbstractGridCoverage2DReader}, {@link FeatureCollection}) a |
149 |
|
|
* default style is generated. |
150 |
|
|
* |
151 |
|
|
* @param object |
152 |
|
|
* an Object |
153 |
|
|
* @param forcedStyle |
154 |
|
|
* (SLD-)Style to force for the object |
155 |
|
|
* @exception Exception |
156 |
|
|
* if {@code null} is given as object or an error occurs |
157 |
|
|
* during layer creation |
158 |
|
|
*/ |
159 |
alfonx |
516 |
public static MapLayer createMapLayer(Object object, final Style forcedStyle) |
160 |
alfonx |
400 |
throws Exception { |
161 |
|
|
MapLayer layer = null; |
162 |
|
|
Style style = null; |
163 |
|
|
if (object instanceof StyledLayerInterface) { |
164 |
|
|
style = ((StyledLayerInterface<?>) object).getStyle(); |
165 |
|
|
object = ((StyledLayerInterface<?>) object).getGeoObject(); |
166 |
|
|
} |
167 |
|
|
if (forcedStyle != null) |
168 |
|
|
style = forcedStyle; |
169 |
|
|
if (style == null) |
170 |
|
|
style = StylingUtil.createDefaultStyle(object); |
171 |
alfonx |
244 |
|
172 |
alfonx |
400 |
if (object instanceof GridCoverage2D) |
173 |
|
|
layer = new DefaultMapLayer((GridCoverage2D) object, style); |
174 |
|
|
if (object instanceof AbstractGridCoverage2DReader) |
175 |
|
|
layer = new DefaultMapLayer((AbstractGridCoverage2DReader) object, |
176 |
|
|
style); |
177 |
|
|
if (object instanceof FeatureCollection) |
178 |
|
|
layer = new DefaultMapLayer((FeatureCollection) object, style); |
179 |
alfonx |
244 |
|
180 |
alfonx |
400 |
if (layer == null) |
181 |
|
|
throw new Exception("Can not create MapLayer from " |
182 |
|
|
+ (object == null ? "null" : object.getClass())); |
183 |
alfonx |
244 |
|
184 |
alfonx |
400 |
return layer; |
185 |
|
|
} |
186 |
alfonx |
244 |
|
187 |
alfonx |
400 |
/** |
188 |
|
|
* Creates an default instance of {@link StyledLayerInterface} for a |
189 |
|
|
* Geotools object ({@link GridCoverage2D}, {@link FeatureCollection}) with |
190 |
|
|
* a default style. |
191 |
|
|
* |
192 |
|
|
* @param object |
193 |
|
|
* an Object |
194 |
|
|
* @param title |
195 |
|
|
* title for the object |
196 |
|
|
* @exception UnsupportedOperationException |
197 |
|
|
* if {@code null} is given as object or an error occurs |
198 |
|
|
* during creation |
199 |
|
|
*/ |
200 |
alfonx |
516 |
public static StyledLayerInterface<?> createStyledLayer( |
201 |
|
|
final Object object, final String title) { |
202 |
alfonx |
400 |
return createStyledLayer(object, title, null); |
203 |
|
|
} |
204 |
alfonx |
244 |
|
205 |
alfonx |
400 |
/** |
206 |
|
|
* Creates an default instance of {@link StyledLayerInterface} for a |
207 |
|
|
* Geotools object ({@link GridCoverage2D}, {@link FeatureCollection}) with |
208 |
|
|
* a given style. |
209 |
|
|
* |
210 |
|
|
* @param object |
211 |
|
|
* an Object |
212 |
|
|
* @param title |
213 |
|
|
* title for the object |
214 |
|
|
* @param style |
215 |
|
|
* style and meta data for the object |
216 |
|
|
* @exception UnsupportedOperationException |
217 |
|
|
* if {@code null} is given as object or an error occurs |
218 |
|
|
* during creation |
219 |
|
|
*/ |
220 |
alfonx |
516 |
public static StyledLayerInterface<?> createStyledLayer( |
221 |
|
|
final Object object, final String title, |
222 |
|
|
final StyledLayerStyle style) { |
223 |
alfonx |
400 |
StyledLayerInterface<?> styledLayer = null; |
224 |
alfonx |
244 |
|
225 |
alfonx |
516 |
final String id = (title != null) ? title : "defaultID"; |
226 |
alfonx |
244 |
|
227 |
alfonx |
400 |
if (object instanceof GridCoverage2D) |
228 |
|
|
styledLayer = new StyledGridCoverage((GridCoverage2D) object, id, |
229 |
|
|
title, style); |
230 |
|
|
else if (object instanceof AbstractGridCoverage2DReader) |
231 |
|
|
styledLayer = new StyledGridCoverageReader( |
232 |
|
|
(AbstractGridCoverage2DReader) object, id, title, style); |
233 |
|
|
else if (object instanceof FeatureCollection) |
234 |
|
|
styledLayer = new StyledFeatureCollection( |
235 |
|
|
(FeatureCollection) object, id, title, style); |
236 |
alfonx |
244 |
|
237 |
alfonx |
400 |
if (styledLayer == null) |
238 |
|
|
throw new UnsupportedOperationException( |
239 |
|
|
"Can not create StyledLayerInterface object from " |
240 |
|
|
+ (object == null ? "null" : object.getClass())); |
241 |
alfonx |
244 |
|
242 |
alfonx |
400 |
return styledLayer; |
243 |
|
|
} |
244 |
alfonx |
244 |
|
245 |
alfonx |
400 |
/** |
246 |
|
|
* Return only the visible or invisible entries of an AttributeMetaData-Map. |
247 |
|
|
* |
248 |
|
|
* @param amdMap |
249 |
|
|
* AttributeMetaData-Map |
250 |
|
|
* @param visible |
251 |
|
|
* indicated whether the visible or invisible entries are |
252 |
|
|
* returned |
253 |
alfonx |
607 |
* |
254 |
|
|
* TODO replace with |
255 |
|
|
* {@link AttributeMetadataMap#sortedValuesVisibleOnly()} |
256 |
alfonx |
400 |
*/ |
257 |
alfonx |
772 |
public static AttributeMetadataMap<? extends AttributeMetadataInterface > getVisibleAttributeMetaData( |
258 |
|
|
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap, |
259 |
alfonx |
769 |
final boolean visible) { |
260 |
alfonx |
516 |
|
261 |
alfonx |
772 |
final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap.clone(); |
262 |
alfonx |
769 |
if (filteredMap.size() > 0 ) { |
263 |
|
|
filteredMap.clear(); // Just in case the close copies the contents |
264 |
|
|
} |
265 |
|
|
|
266 |
alfonx |
772 |
for (final AttributeMetadataInterface amd : amdMap.values()) |
267 |
alfonx |
464 |
if (amd.isVisible() == visible) |
268 |
|
|
filteredMap.put(amd.getName(), amd); |
269 |
alfonx |
244 |
|
270 |
alfonx |
400 |
return filteredMap; |
271 |
|
|
} |
272 |
alfonx |
244 |
|
273 |
alfonx |
400 |
/** |
274 |
alfonx |
769 |
* Parses a {@link AttributeMetadataImpl} object from an JDOM- |
275 |
|
|
* {@link Element}. This method works like {@link |
276 |
alfonx |
400 |
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
277 |
|
|
* |
278 |
alfonx |
534 |
* TODO 20.11.2009, SK: There are some new attribute weight, functiona, |
279 |
|
|
* functionX and nodata in AttributeMetaData that should be parsed/exported |
280 |
|
|
* too. but this method is only used by ISDSS, which is not supporting that |
281 |
|
|
* stuff anyways. |
282 |
|
|
* |
283 |
alfonx |
400 |
* @param element |
284 |
|
|
* {@link Element} to parse |
285 |
|
|
*/ |
286 |
alfonx |
769 |
public static AttributeMetadataImpl parseAttributeMetaData( |
287 |
|
|
final Element element) { |
288 |
alfonx |
516 |
final String namespace = String.valueOf(element |
289 |
|
|
.getAttributeValue("namespace")); |
290 |
|
|
final String localname = String.valueOf(element |
291 |
|
|
.getAttributeValue("localname")); |
292 |
alfonx |
770 |
final NameImpl aName = new NameImpl(namespace, localname); |
293 |
alfonx |
400 |
final Boolean visible = Boolean.valueOf(element |
294 |
|
|
.getAttributeValue("visible")); |
295 |
|
|
final String unit = element.getAttributeValue("unit"); |
296 |
alfonx |
244 |
|
297 |
alfonx |
400 |
Translation name = new Translation(); |
298 |
|
|
Translation desc = new Translation(); |
299 |
|
|
for (final Element childElement : (List<Element>) element.getChildren()) { |
300 |
|
|
if (childElement.getName() == null) |
301 |
|
|
continue; |
302 |
alfonx |
244 |
|
303 |
alfonx |
400 |
if (childElement.getName().equals("name")) |
304 |
|
|
name = parseTranslation(childElement); |
305 |
|
|
else if (childElement.getName().equals("desc")) |
306 |
|
|
desc = parseTranslation(childElement); |
307 |
|
|
} |
308 |
alfonx |
769 |
return new AttributeMetadataImpl(aName, visible, name, desc, unit); |
309 |
alfonx |
400 |
} |
310 |
alfonx |
244 |
|
311 |
alfonx |
400 |
/** |
312 |
alfonx |
769 |
* Parses a {@link AttributeMetadataImpl} map from an JDOM-{@link Element} |
313 |
|
|
* with {@code <attribute>}-childs. |
314 |
alfonx |
400 |
* |
315 |
|
|
* @param element |
316 |
|
|
* {@link Element} to parse |
317 |
alfonx |
677 |
* |
318 |
alfonx |
769 |
* TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has |
319 |
|
|
* more attributes which are not used by Xulu/ISDSS. GP |
320 |
alfonx |
658 |
* exports/imports the AMD via AMLExporter and AMLImporter |
321 |
alfonx |
677 |
* classes. (SK, 3.2.2010) * |
322 |
alfonx |
400 |
*/ |
323 |
alfonx |
420 |
public static AttributeMetadataMap parseAttributeMetaDataMap( |
324 |
alfonx |
400 |
final Element element) { |
325 |
alfonx |
769 |
final AttributeMetadataMap metaData = new AttributeMetadataImplMap(); |
326 |
alfonx |
516 |
final List<Element> attributesElements = element |
327 |
alfonx |
400 |
.getChildren(ELEM_NAME_ATTRIBUTE); |
328 |
alfonx |
516 |
for (final Element attibuteElement : attributesElements) { |
329 |
alfonx |
769 |
final AttributeMetadataImpl attrMetaData = parseAttributeMetaData(attibuteElement); |
330 |
alfonx |
464 |
metaData.put(attrMetaData.getName(), attrMetaData); |
331 |
alfonx |
400 |
} |
332 |
|
|
return metaData; |
333 |
|
|
} |
334 |
alfonx |
244 |
|
335 |
alfonx |
400 |
/** |
336 |
alfonx |
769 |
* Loads a {@link AttributeMetadataImpl} object from an URL. |
337 |
alfonx |
400 |
* |
338 |
|
|
* @param documentUrl |
339 |
|
|
* {@link URL} to parse |
340 |
|
|
* @see #parseAttributeMetaData(Element) |
341 |
|
|
*/ |
342 |
alfonx |
420 |
public static AttributeMetadataMap loadAttributeMetaDataMap( |
343 |
alfonx |
400 |
final URL documentUrl) throws Exception { |
344 |
alfonx |
516 |
final Document document = SAX_BUILDER.build(documentUrl); |
345 |
alfonx |
400 |
return parseAttributeMetaDataMap(document.getRootElement()); |
346 |
|
|
} |
347 |
alfonx |
244 |
|
348 |
alfonx |
400 |
/** |
349 |
alfonx |
769 |
* Creates an JDOM {@link Element} for the given |
350 |
|
|
* {@link AttributeMetadataImpl} object. |
351 |
alfonx |
400 |
* |
352 |
|
|
* @param amd |
353 |
|
|
* meta data for one attribute |
354 |
alfonx |
658 |
* |
355 |
alfonx |
769 |
* TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has |
356 |
|
|
* more attributes which are not used by Xulu/ISDSS. GP |
357 |
alfonx |
658 |
* exports/imports the AMD via AMLExporter and AMLImporter |
358 |
|
|
* classes. (SK, 3.2.2010) |
359 |
alfonx |
400 |
*/ |
360 |
|
|
public static Element createAttributeMetaDataElement( |
361 |
alfonx |
772 |
final AttributeMetadataInterface amd) { |
362 |
alfonx |
400 |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
363 |
alfonx |
516 |
element.setAttribute("namespace", String.valueOf(amd.getName() |
364 |
|
|
.getNamespaceURI())); |
365 |
alfonx |
464 |
element.setAttribute("localname", String.valueOf(amd.getLocalName())); |
366 |
alfonx |
400 |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
367 |
|
|
element.setAttribute("unit", amd.getUnit()); |
368 |
|
|
// Creating a aml:name tag... |
369 |
|
|
element.addContent(createTranslationElement("name", amd.getTitle())); |
370 |
|
|
// Creating a aml:desc tag... |
371 |
|
|
element.addContent(createTranslationElement("desc", amd.getDesc())); |
372 |
|
|
return element; |
373 |
|
|
} |
374 |
alfonx |
244 |
|
375 |
alfonx |
400 |
/** |
376 |
alfonx |
769 |
* Creates an JDOM {@link Element} for the given |
377 |
|
|
* {@link AttributeMetadataImpl} map. |
378 |
alfonx |
400 |
* |
379 |
|
|
* @param amdMap |
380 |
|
|
* map of attribute meta data |
381 |
|
|
*/ |
382 |
|
|
public static Element createAttributeMetaDataMapElement( |
383 |
alfonx |
772 |
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap) { |
384 |
alfonx |
400 |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
385 |
alfonx |
772 |
for (final AttributeMetadataInterface amd : amdMap.values()) |
386 |
alfonx |
400 |
element.addContent(createAttributeMetaDataElement(amd)); |
387 |
|
|
return element; |
388 |
|
|
} |
389 |
alfonx |
244 |
|
390 |
alfonx |
400 |
/** |
391 |
alfonx |
769 |
* Saves a {@link AttributeMetadataImpl AttributeMetaData-Map} to an URL. |
392 |
alfonx |
400 |
* |
393 |
|
|
* @param amdMap |
394 |
alfonx |
769 |
* map of {@link AttributeMetadataImpl} |
395 |
alfonx |
400 |
* @param documentUrl |
396 |
|
|
* {@link URL} to store the XML |
397 |
|
|
*/ |
398 |
|
|
public static void saveAttributeMetaDataMap( |
399 |
alfonx |
420 |
final AttributeMetadataMap amdMap, final URL documentUrl) |
400 |
alfonx |
400 |
throws Exception { |
401 |
|
|
// Create XML-Document |
402 |
|
|
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
403 |
|
|
XML_OUTPUTTER.output(createAttributeMetaDataMapElement(amdMap), out); |
404 |
|
|
out.flush(); |
405 |
|
|
out.close(); |
406 |
|
|
} |
407 |
alfonx |
244 |
|
408 |
alfonx |
400 |
/** |
409 |
|
|
* Parses a {@link RasterLegendData} object from an JDOM-{@link Element}. |
410 |
|
|
* This method works like {@link |
411 |
|
|
* AMLImport#parseRasterLegendData(org.w3c.dom.Node}, but for JDOM. |
412 |
|
|
* |
413 |
|
|
* @param element |
414 |
|
|
* {@link Element} to parse |
415 |
|
|
*/ |
416 |
alfonx |
516 |
public static RasterLegendData parseRasterLegendData(final Element element) { |
417 |
alfonx |
244 |
|
418 |
alfonx |
400 |
final boolean paintGaps = Boolean.valueOf(element |
419 |
|
|
.getAttributeValue("paintGaps")); |
420 |
alfonx |
244 |
|
421 |
alfonx |
516 |
final RasterLegendData rld = new RasterLegendData(paintGaps); |
422 |
alfonx |
244 |
|
423 |
alfonx |
516 |
for (final Element childElement : (List<Element>) element.getChildren()) { |
424 |
alfonx |
400 |
final String name = childElement.getName(); |
425 |
|
|
// Cancel if it's an attribute |
426 |
|
|
if (childElement.getChildren().size() == 0) |
427 |
|
|
continue; |
428 |
alfonx |
244 |
|
429 |
alfonx |
400 |
if (name.equals(ELEM_NAME_RASTERLEGEND)) { |
430 |
|
|
final String valueAttr = childElement |
431 |
|
|
.getAttributeValue("value"); |
432 |
|
|
if (valueAttr == null) |
433 |
|
|
throw new UnsupportedOperationException( |
434 |
|
|
"Attribute 'value' missing for definition of <" |
435 |
|
|
+ ELEM_NAME_RASTERLEGEND + ">"); |
436 |
|
|
final double value = Double.valueOf(valueAttr); |
437 |
alfonx |
244 |
|
438 |
alfonx |
400 |
// first and only item should be the label |
439 |
|
|
final Element labelElement = childElement.getChild("label"); |
440 |
|
|
// id label element is missing, the translation is searched |
441 |
|
|
// directly |
442 |
|
|
// as childs of the rasterLegendItem element |
443 |
alfonx |
516 |
final Translation label = parseTranslation(labelElement != null ? labelElement |
444 |
alfonx |
400 |
: childElement); |
445 |
|
|
rld.put(value, label); |
446 |
|
|
} |
447 |
|
|
} |
448 |
alfonx |
244 |
|
449 |
alfonx |
400 |
return rld; |
450 |
|
|
} |
451 |
alfonx |
244 |
|
452 |
alfonx |
400 |
/** |
453 |
|
|
* Loads a {@link RasterLegendData} object from an URL. |
454 |
|
|
* |
455 |
|
|
* @param documentUrl |
456 |
|
|
* {@link URL} to parse |
457 |
|
|
* @see #parseAttributeMetaData(Element) |
458 |
|
|
*/ |
459 |
|
|
public static RasterLegendData loadRasterLegendData(final URL documentUrl) |
460 |
|
|
throws Exception { |
461 |
alfonx |
516 |
final Document document = SAX_BUILDER.build(documentUrl); |
462 |
alfonx |
400 |
return parseRasterLegendData(document.getRootElement()); |
463 |
|
|
} |
464 |
alfonx |
244 |
|
465 |
alfonx |
400 |
/** |
466 |
|
|
* Creates an JDOM {@link Element} for the given {@link RasterLegendData} |
467 |
|
|
* map. |
468 |
|
|
* |
469 |
|
|
* @param rld |
470 |
|
|
* raster legend data |
471 |
|
|
*/ |
472 |
|
|
public static Element createRasterLegendDataElement( |
473 |
|
|
final RasterLegendData rld) { |
474 |
|
|
final Element element = new Element(ELEM_NAME_RLD, AMLURI); |
475 |
|
|
element.setAttribute("paintGaps", rld.isPaintGaps().toString()); |
476 |
alfonx |
516 |
for (final Double key : rld.getSortedKeys()) { |
477 |
|
|
final Element item = new Element(ELEM_NAME_RASTERLEGEND, AMLURI); |
478 |
alfonx |
400 |
item.setAttribute("value", key.toString()); |
479 |
|
|
item.addContent(createTranslationElement("label", rld.get(key))); |
480 |
|
|
element.addContent(item); |
481 |
|
|
} |
482 |
|
|
return element; |
483 |
|
|
} |
484 |
alfonx |
244 |
|
485 |
alfonx |
400 |
/** |
486 |
|
|
* Creates {@link RasterLegendData} from a {@link ColorMap}. |
487 |
|
|
* |
488 |
|
|
* @param colorMap |
489 |
|
|
* a color map |
490 |
|
|
* @param paintGaps |
491 |
|
|
* indicated whether gaps are painted between the legend items |
492 |
|
|
* @param digits |
493 |
|
|
* number of digits the grid value classes (and legend) are |
494 |
|
|
* rounded to (null means no round; >= 0 means digits after |
495 |
|
|
* comma; < 0 means digits before comma) |
496 |
|
|
*/ |
497 |
alfonx |
516 |
public static RasterLegendData generateRasterLegendData( |
498 |
|
|
final ColorMap colorMap, final boolean paintGaps, |
499 |
|
|
final Integer digits) { |
500 |
|
|
final DecimalFormat decFormat = digits != null ? new DecimalFormat( |
501 |
|
|
SwingUtil.getNumberFormatPattern(digits)) : null; |
502 |
|
|
final RasterLegendData rld = new RasterLegendData(paintGaps); |
503 |
|
|
for (final ColorMapEntry cme : colorMap.getColorMapEntries()) { |
504 |
|
|
final double value = StylingUtil.getQuantityFromColorMapEntry(cme); |
505 |
alfonx |
400 |
String label = cme.getLabel(); |
506 |
|
|
// if no label is set (e.g. quantitative style), |
507 |
|
|
// use the value as label |
508 |
|
|
if (label == null || label.equals("")) |
509 |
|
|
if (digits == null) |
510 |
|
|
label = String.valueOf(value); |
511 |
|
|
else |
512 |
|
|
label = decFormat.format(LangUtil.round(value, digits)); |
513 |
|
|
rld.put(value, new Translation(" " + label)); |
514 |
|
|
} |
515 |
|
|
return rld; |
516 |
|
|
} |
517 |
alfonx |
244 |
|
518 |
alfonx |
400 |
/** |
519 |
|
|
* Creates {@link RasterLegendData} from the {@link ColorMap} of a style. |
520 |
|
|
* |
521 |
|
|
* @param style |
522 |
|
|
* a raster style (must contain a {@link RasterSymbolizer}) |
523 |
|
|
* @param paintGaps |
524 |
|
|
* indicated whether gaps are painted between the legend items |
525 |
|
|
* @param digits |
526 |
|
|
* number of digits the grid value classes (and legend) are |
527 |
|
|
* rounded to (null means no round; >= 0 means digits after |
528 |
|
|
* comma; < 0 means digits before comma) |
529 |
|
|
*/ |
530 |
alfonx |
516 |
public static RasterLegendData generateRasterLegendData(final Style style, |
531 |
|
|
final boolean paintGaps, final Integer digits) { |
532 |
|
|
final ColorMap colorMap = StylingUtil.getColorMapFromStyle(style); |
533 |
alfonx |
400 |
if (colorMap == null) |
534 |
|
|
throw new IllegalArgumentException( |
535 |
|
|
"Color map can not be determined from style!"); |
536 |
|
|
return generateRasterLegendData(colorMap, paintGaps, digits); |
537 |
|
|
} |
538 |
alfonx |
244 |
|
539 |
alfonx |
400 |
/** |
540 |
|
|
* Saves a {@link RasterLegendData} to an URL. |
541 |
|
|
* |
542 |
|
|
* @param rld |
543 |
|
|
* raster legend data |
544 |
|
|
* @param documentUrl |
545 |
|
|
* {@link URL} to store the XML |
546 |
|
|
*/ |
547 |
|
|
public static void saveRasterLegendData(final RasterLegendData rld, |
548 |
|
|
final URL documentUrl) throws Exception { |
549 |
|
|
// Create XML-Document |
550 |
|
|
final FileWriter out = new FileWriter(new File(documentUrl.toURI())); |
551 |
|
|
XML_OUTPUTTER.output(createRasterLegendDataElement(rld), out); |
552 |
|
|
out.flush(); |
553 |
|
|
out.close(); |
554 |
|
|
} |
555 |
alfonx |
244 |
|
556 |
alfonx |
400 |
/** |
557 |
|
|
* Parses a {@link Translation} object from an JDOM-{@link Element}. This |
558 |
|
|
* method works like {@link AMLImport#parseTranslation(org.w3c.dom.Node}, |
559 |
|
|
* but for JDOM. |
560 |
|
|
* |
561 |
|
|
* @param element |
562 |
|
|
* {@link Element} to parse |
563 |
|
|
*/ |
564 |
|
|
public final static Translation parseTranslation(final Element element) { |
565 |
alfonx |
516 |
final Translation trans = new Translation(); |
566 |
alfonx |
244 |
|
567 |
alfonx |
400 |
if (element == null) |
568 |
|
|
return trans; |
569 |
alfonx |
244 |
|
570 |
alfonx |
400 |
for (final Element translationElement : (List<Element>) element |
571 |
|
|
.getChildren()) { |
572 |
|
|
final String name = translationElement.getName(); |
573 |
|
|
if (name == null) |
574 |
|
|
continue; |
575 |
alfonx |
244 |
|
576 |
alfonx |
400 |
// lang attribute |
577 |
|
|
String lang = translationElement.getAttributeValue("lang"); |
578 |
|
|
// set the default, if no language code is set |
579 |
|
|
if (lang == null) |
580 |
|
|
lang = Translation.DEFAULT_KEY; |
581 |
alfonx |
244 |
|
582 |
alfonx |
400 |
final String translationText = translationElement.getValue(); |
583 |
|
|
if (translationText == null) |
584 |
|
|
trans.put(lang, ""); |
585 |
|
|
else |
586 |
|
|
trans.put(lang, translationText); |
587 |
|
|
} |
588 |
alfonx |
244 |
|
589 |
alfonx |
400 |
// if no <translation> is given, the value of the node should |
590 |
|
|
// be used as a default translation |
591 |
|
|
if (trans.size() == 0) |
592 |
|
|
trans.put(Translation.DEFAULT_KEY, element.getValue()); |
593 |
|
|
// trans = new Translation( |
594 |
|
|
// ((List<Element>)element.getChildren()).get(0).getValue() ); |
595 |
alfonx |
244 |
|
596 |
alfonx |
400 |
return trans; |
597 |
|
|
} |
598 |
alfonx |
244 |
|
599 |
alfonx |
400 |
/** |
600 |
|
|
* Creates an JDOM {@link Element} for the given {@link Translation}. |
601 |
|
|
* |
602 |
|
|
* @param tagname |
603 |
|
|
* Name of the Element |
604 |
|
|
* @param translation |
605 |
|
|
* Translation to store in the Element |
606 |
|
|
*/ |
607 |
alfonx |
516 |
public final static Element createTranslationElement(final String tagname, |
608 |
|
|
final Translation translation) { |
609 |
|
|
final Element element = new Element(tagname, AMLURI); |
610 |
alfonx |
400 |
if (translation == null) |
611 |
|
|
throw new UnsupportedOperationException( |
612 |
|
|
"Translation element can not be created from null!"); |
613 |
alfonx |
244 |
|
614 |
alfonx |
400 |
// If only a default translation is set, the <translation |
615 |
|
|
// lang="..">..</tranlation> |
616 |
|
|
// part is not used |
617 |
|
|
if (translation.keySet().size() == 1 |
618 |
|
|
&& translation.get(Translation.DEFAULT_KEY) != null) { |
619 |
|
|
element.addContent(translation.get(Translation.DEFAULT_KEY)); |
620 |
|
|
return element; |
621 |
|
|
} |
622 |
alfonx |
244 |
|
623 |
alfonx |
400 |
// add a <translation lang="..">..</tranlation> part to the element for |
624 |
|
|
// all languages |
625 |
alfonx |
516 |
for (final String lang : translation.keySet()) { |
626 |
|
|
final Element translationElement = new Element( |
627 |
|
|
ELEM_NAME_TRANSLATION, AMLURI); |
628 |
alfonx |
400 |
translationElement.setAttribute("lang", lang); |
629 |
|
|
String translationString = translation.get(lang); |
630 |
|
|
if (translationString == null) |
631 |
|
|
translationString = ""; |
632 |
|
|
translationElement.addContent(translationString); |
633 |
|
|
element.addContent(translationElement); |
634 |
|
|
} |
635 |
alfonx |
244 |
|
636 |
alfonx |
400 |
return element; |
637 |
|
|
} |
638 |
alfonx |
244 |
|
639 |
alfonx |
400 |
/** |
640 |
|
|
* Sets a style to {@link StyledLayerInterface}. |
641 |
|
|
* |
642 |
|
|
* @param styledObject |
643 |
|
|
* a styled object |
644 |
|
|
* @param style |
645 |
|
|
* a Style |
646 |
|
|
*/ |
647 |
alfonx |
516 |
public static void setStyledLayerStyle( |
648 |
|
|
final StyledLayerInterface styledObject, |
649 |
|
|
final StyledLayerStyle<?> style) { |
650 |
alfonx |
400 |
// set SLD style |
651 |
|
|
styledObject.setStyle(style.getGeoObjectStyle()); |
652 |
|
|
// set meta data |
653 |
|
|
if (styledObject instanceof StyledGridCoverageInterface |
654 |
|
|
&& (style.getMetaData() instanceof RasterLegendData || style |
655 |
|
|
.getMetaData() == null)) { |
656 |
alfonx |
516 |
final RasterLegendData sourceRld = (RasterLegendData) style |
657 |
|
|
.getMetaData(); |
658 |
|
|
final RasterLegendData destRld = ((StyledGridCoverageInterface) styledObject) |
659 |
alfonx |
400 |
.getLegendMetaData(); |
660 |
|
|
if (destRld != null && sourceRld != null) { |
661 |
|
|
destRld.setPaintGaps(sourceRld.isPaintGaps()); |
662 |
|
|
destRld.clear(); |
663 |
|
|
destRld.putAll(sourceRld); |
664 |
|
|
} |
665 |
|
|
return; |
666 |
|
|
} |
667 |
|
|
if (styledObject instanceof StyledFeatureCollectionInterface |
668 |
|
|
&& (style.getMetaData() instanceof Map || style.getMetaData() == null)) { |
669 |
alfonx |
516 |
final AttributeMetadataMap sourceAmd = (AttributeMetadataMap) style |
670 |
alfonx |
400 |
.getMetaData(); |
671 |
alfonx |
516 |
final AttributeMetadataMap destAmd = ((StyledFeatureCollectionInterface) styledObject) |
672 |
alfonx |
400 |
.getAttributeMetaDataMap(); |
673 |
|
|
if (destAmd != null && sourceAmd != null) { |
674 |
|
|
destAmd.clear(); |
675 |
|
|
destAmd.putAll(sourceAmd); |
676 |
|
|
} |
677 |
|
|
return; |
678 |
|
|
} |
679 |
alfonx |
244 |
|
680 |
alfonx |
400 |
throw new UnsupportedOperationException( |
681 |
|
|
"Style is not compatible to object: " |
682 |
|
|
+ (style.getMetaData() == null ? null : style |
683 |
|
|
.getMetaData().getClass().getSimpleName()) |
684 |
|
|
+ " <-> " |
685 |
|
|
+ (styledObject == null ? null : styledObject |
686 |
|
|
.getClass().getSimpleName())); |
687 |
|
|
} |
688 |
alfonx |
244 |
|
689 |
alfonx |
400 |
/** |
690 |
|
|
* Returns the style a {@link StyledLayerInterface} as a |
691 |
|
|
* {@link StyledLayerStyle}. |
692 |
|
|
* |
693 |
|
|
* @param styledObject |
694 |
|
|
* a styled object |
695 |
|
|
* @return {@code StyledLayerStyle<RasterLegendData>} for |
696 |
|
|
* {@link StyledGridCoverageInterface} or {@code |
697 |
|
|
* StyledLayerStyle<Map<Integer,AttributeMetaData>>} for |
698 |
|
|
* {@link StyledFeatureCollectionInterface} |
699 |
|
|
*/ |
700 |
|
|
public static StyledLayerStyle<?> getStyledLayerStyle( |
701 |
alfonx |
516 |
final StyledLayerInterface styledObject) { |
702 |
alfonx |
400 |
if (styledObject instanceof StyledGridCoverageInterface) |
703 |
|
|
return getStyledLayerStyle((StyledGridCoverageInterface) styledObject); |
704 |
|
|
if (styledObject instanceof StyledFeatureCollectionInterface) |
705 |
|
|
return getStyledLayerStyle((StyledFeatureCollectionInterface) styledObject); |
706 |
|
|
throw new UnsupportedOperationException( |
707 |
|
|
"Unknown type of StyledLayerInterface: " |
708 |
|
|
+ (styledObject == null ? null : styledObject |
709 |
|
|
.getClass().getSimpleName())); |
710 |
|
|
} |
711 |
alfonx |
244 |
|
712 |
alfonx |
400 |
/** |
713 |
|
|
* Returns the style and raster meta data of a |
714 |
|
|
* {@link StyledGridCoverageInterface} as a {@link StyledLayerStyle}. |
715 |
|
|
* |
716 |
|
|
* @param styledGC |
717 |
|
|
* a styled grid coverage |
718 |
|
|
*/ |
719 |
|
|
public static StyledLayerStyle<RasterLegendData> getStyledLayerStyle( |
720 |
alfonx |
516 |
final StyledGridCoverageInterface styledGC) { |
721 |
alfonx |
400 |
return new StyledLayerStyle<RasterLegendData>(styledGC.getStyle(), |
722 |
|
|
styledGC.getLegendMetaData()); |
723 |
|
|
} |
724 |
alfonx |
244 |
|
725 |
alfonx |
400 |
/** |
726 |
|
|
* Returns the style and attribute meta data of a |
727 |
|
|
* {@link StyledFeatureCollectionInterface} as a {@link StyledLayerStyle}. |
728 |
|
|
* |
729 |
|
|
* @param styledFC |
730 |
|
|
* a styled feature collection |
731 |
|
|
*/ |
732 |
alfonx |
464 |
public static StyledLayerStyle<AttributeMetadataMap> getStyledLayerStyle( |
733 |
alfonx |
516 |
final StyledFeatureCollectionInterface styledFC) { |
734 |
|
|
return new StyledLayerStyle<AttributeMetadataMap>(styledFC.getStyle(), |
735 |
|
|
styledFC.getAttributeMetaDataMap()); |
736 |
alfonx |
400 |
} |
737 |
alfonx |
244 |
|
738 |
alfonx |
400 |
/** |
739 |
|
|
* Loads a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData |
740 |
|
|
* Raster-LegendData} for a given geo-object (raster) source. The SLD file |
741 |
|
|
* must be present. A missing raster legend-data file is tolerated. |
742 |
|
|
* |
743 |
|
|
* @param geoObjectURL |
744 |
|
|
* URL of the (already read) raster object |
745 |
|
|
* @param sldExt |
746 |
|
|
* file extention for the SLD file |
747 |
|
|
* @param rldExt |
748 |
|
|
* file extention for the raster legend-data file |
749 |
|
|
* @return {@code null} in case of any error |
750 |
|
|
*/ |
751 |
|
|
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
752 |
alfonx |
516 |
final URL geoObjectURL, final String sldExt, final String rldExt) { |
753 |
alfonx |
400 |
RasterLegendData metaData = null; |
754 |
|
|
Style sldStyle = null; |
755 |
|
|
try { |
756 |
alfonx |
516 |
final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
757 |
alfonx |
400 |
geoObjectURL, sldExt)); |
758 |
|
|
// SLD must be present |
759 |
|
|
if (styles == null || styles.length == 0) |
760 |
|
|
return null; |
761 |
|
|
sldStyle = styles[0]; |
762 |
alfonx |
516 |
} catch (final Exception err) { |
763 |
alfonx |
400 |
// SLD must be present |
764 |
|
|
LangUtil.logDebugError(LOGGER, err); |
765 |
|
|
return null; |
766 |
|
|
} |
767 |
alfonx |
244 |
|
768 |
alfonx |
400 |
try { |
769 |
|
|
metaData = StyledLayerUtil.loadRasterLegendData(IOUtil |
770 |
|
|
.changeUrlExt(geoObjectURL, rldExt)); |
771 |
alfonx |
516 |
} catch (final FileNotFoundException err) { |
772 |
alfonx |
400 |
// ignore missing raster legend data |
773 |
alfonx |
516 |
} catch (final Exception err) { |
774 |
alfonx |
400 |
// any other error during legend data creation leads to error |
775 |
|
|
LangUtil.logDebugError(LOGGER, err); |
776 |
|
|
return null; |
777 |
|
|
} |
778 |
|
|
return new StyledLayerStyle<RasterLegendData>(sldStyle, metaData); |
779 |
|
|
} |
780 |
alfonx |
244 |
|
781 |
alfonx |
400 |
/** |
782 |
|
|
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
783 |
|
|
* {@linkplain RasterLegendData Raster-LegendData} from a {@code .rld} file |
784 |
|
|
* for a given geo-object (raster) source. The SLD file must be present. A |
785 |
|
|
* missing raster legend-data file is tolerated. |
786 |
|
|
* |
787 |
|
|
* @param geoObjectURL |
788 |
|
|
* URL of the (already read) raster object |
789 |
|
|
* @param sldExt |
790 |
|
|
* file extention for the SLD file |
791 |
|
|
* @param rldExt |
792 |
|
|
* file extention for the raster legend-data file |
793 |
|
|
* @return {@code null} in case of any error |
794 |
|
|
*/ |
795 |
|
|
public static StyledLayerStyle<RasterLegendData> loadStyledRasterStyle( |
796 |
alfonx |
516 |
final URL geoObjectURL) { |
797 |
alfonx |
400 |
return loadStyledRasterStyle(geoObjectURL, "sld", "rld"); |
798 |
|
|
} |
799 |
alfonx |
244 |
|
800 |
alfonx |
400 |
/** |
801 |
alfonx |
769 |
* Loads a {@linkplain Style SLD-Style} and a |
802 |
|
|
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} for a given |
803 |
|
|
* geo-object (feature) source. The SLD file must be present. A missing |
804 |
|
|
* attribute meta-data file is tolerated. |
805 |
alfonx |
400 |
* |
806 |
|
|
* @param geoObjectURL |
807 |
|
|
* URL of the (already read) feature object |
808 |
|
|
* @param sldExt |
809 |
|
|
* file extention for the SLD file |
810 |
|
|
* @param rldExt |
811 |
|
|
* file extention for the raster legend-data file |
812 |
|
|
* @return {@code null} in case of any error |
813 |
|
|
*/ |
814 |
alfonx |
464 |
public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle( |
815 |
alfonx |
516 |
final URL geoObjectURL, final String sldExt, final String rldExt) { |
816 |
alfonx |
464 |
AttributeMetadataMap metaData = null; |
817 |
alfonx |
400 |
Style sldStyle = null; |
818 |
|
|
try { |
819 |
alfonx |
516 |
final Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt( |
820 |
alfonx |
400 |
geoObjectURL, sldExt)); |
821 |
|
|
// SLD must be present |
822 |
|
|
if (styles == null || styles.length == 0) |
823 |
|
|
return null; |
824 |
|
|
sldStyle = styles[0]; |
825 |
alfonx |
516 |
} catch (final Exception err) { |
826 |
alfonx |
400 |
// SLD must be present |
827 |
|
|
LangUtil.logDebugError(LOGGER, err); |
828 |
|
|
return null; |
829 |
|
|
} |
830 |
alfonx |
244 |
|
831 |
alfonx |
400 |
try { |
832 |
|
|
metaData = StyledLayerUtil.loadAttributeMetaDataMap(IOUtil |
833 |
|
|
.changeUrlExt(geoObjectURL, rldExt)); |
834 |
alfonx |
516 |
} catch (final FileNotFoundException err) { |
835 |
alfonx |
400 |
// ignore missing attribute meta data |
836 |
alfonx |
516 |
} catch (final Exception err) { |
837 |
alfonx |
400 |
// any other error during meta data creation leads to error |
838 |
|
|
LangUtil.logDebugError(LOGGER, err); |
839 |
|
|
return null; |
840 |
|
|
} |
841 |
alfonx |
244 |
|
842 |
alfonx |
516 |
return new StyledLayerStyle<AttributeMetadataMap>(sldStyle, metaData); |
843 |
alfonx |
400 |
} |
844 |
alfonx |
244 |
|
845 |
alfonx |
400 |
/** |
846 |
|
|
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
847 |
alfonx |
769 |
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a {@code |
848 |
|
|
* .amd} file for a given geo-object (feature) source. The SLD file must be |
849 |
alfonx |
400 |
* present. A missing attribute meta-data file is tolerated. |
850 |
|
|
* |
851 |
|
|
* @param geoObjectURL |
852 |
|
|
* URL of the (already read) feature object |
853 |
|
|
* @param sldExt |
854 |
|
|
* file extention for the SLD file |
855 |
|
|
* @param rldExt |
856 |
|
|
* file extention for the raster legend-data file |
857 |
|
|
* @return {@code null} in case of any error |
858 |
|
|
*/ |
859 |
alfonx |
464 |
public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle( |
860 |
alfonx |
516 |
final URL geoObjectURL) { |
861 |
alfonx |
400 |
return loadStyledFeatureStyle(geoObjectURL, "sld", "amd"); |
862 |
|
|
} |
863 |
alfonx |
244 |
|
864 |
alfonx |
400 |
/** |
865 |
|
|
* Stores a {@linkplain Style SLD-Style} and {@linkplain RasterLegendData |
866 |
|
|
* Raster-LegendData} for a given geo-object (raster) source. |
867 |
|
|
* |
868 |
|
|
* @param style |
869 |
|
|
* style to save |
870 |
|
|
* @param geoObjectURL |
871 |
|
|
* URL of the raster object |
872 |
|
|
* @param sldExt |
873 |
|
|
* file extention for the SLD file |
874 |
|
|
* @param mdExt |
875 |
|
|
* file extention for the meta-data file |
876 |
|
|
*/ |
877 |
alfonx |
516 |
public static <T> void saveStyledLayerStyle( |
878 |
|
|
final StyledLayerStyle<T> style, final URL geoObjectURL, |
879 |
|
|
final String sldExt, final String mdExt) throws Exception { |
880 |
alfonx |
400 |
// Store the SLD |
881 |
alfonx |
516 |
final Style sldStyle = style.getGeoObjectStyle(); |
882 |
alfonx |
400 |
if (sldStyle != null) { |
883 |
|
|
StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File( |
884 |
|
|
geoObjectURL.toURI()), sldExt)); |
885 |
|
|
} |
886 |
alfonx |
244 |
|
887 |
alfonx |
400 |
// Store the meta data |
888 |
alfonx |
516 |
final T metaData = style.getMetaData(); |
889 |
alfonx |
400 |
if (metaData != null) { |
890 |
|
|
if (metaData instanceof RasterLegendData) { |
891 |
|
|
saveRasterLegendData((RasterLegendData) metaData, IOUtil |
892 |
|
|
.changeUrlExt(geoObjectURL, mdExt)); |
893 |
|
|
// } else if ( metaData instanceof |
894 |
|
|
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
895 |
|
|
// KOMPILIERBAR!! |
896 |
|
|
} else if (metaData instanceof Map) { |
897 |
alfonx |
516 |
saveAttributeMetaDataMap((AttributeMetadataMap) metaData, |
898 |
|
|
IOUtil.changeUrlExt(geoObjectURL, mdExt)); |
899 |
alfonx |
400 |
} else |
900 |
|
|
throw new UnsupportedOperationException( |
901 |
|
|
"Export for meta data not yet supported: " |
902 |
|
|
+ metaData.getClass().getSimpleName()); |
903 |
|
|
} |
904 |
|
|
} |
905 |
alfonx |
244 |
|
906 |
alfonx |
400 |
/** |
907 |
|
|
* Stores the {@linkplain Style SLD-Style} to a {@code .sld} file and the |
908 |
alfonx |
769 |
* meta data ({@link RasterLegendData} or {@link AttributeMetadataImpl}) to |
909 |
|
|
* a {@code .rld} or {@code .amd} file. for a given geo-object source. |
910 |
alfonx |
400 |
* |
911 |
|
|
* @param style |
912 |
|
|
* style to save |
913 |
|
|
* @param geoObjectURL |
914 |
|
|
* URL of the (already read) raster object |
915 |
|
|
*/ |
916 |
alfonx |
516 |
public static void saveStyledLayerStyle(final StyledLayerStyle<?> style, |
917 |
|
|
final URL geoObjectURL) throws Exception { |
918 |
alfonx |
400 |
if (style.getMetaData() instanceof RasterLegendData) |
919 |
|
|
saveStyledLayerStyle(style, geoObjectURL, "sld", "rld"); |
920 |
|
|
else |
921 |
|
|
saveStyledLayerStyle(style, geoObjectURL, "sld", "amd"); |
922 |
|
|
} |
923 |
alfonx |
244 |
|
924 |
alfonx |
397 |
/** |
925 |
alfonx |
694 |
* *If appended to the name of a rule, this rule shall not be shown in the |
926 |
|
|
* legend |
927 |
|
|
*/ |
928 |
|
|
public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND"; |
929 |
|
|
|
930 |
|
|
/** |
931 |
alfonx |
516 |
* Creates a {@link JPanel} that shows a legend for a list of |
932 |
alfonx |
397 |
* {@link FeatureTypeStyle}s and a targeted featureType |
933 |
|
|
* |
934 |
alfonx |
824 |
* @param style |
935 |
|
|
* The Style to presented in this legend |
936 |
alfonx |
397 |
* @param featureType |
937 |
|
|
* If this a legend for Point, Polygon or Line? |
938 |
|
|
* |
939 |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons |
940 |
|
|
* Krüger</a> |
941 |
|
|
*/ |
942 |
alfonx |
824 |
public static JPanel createLegendSwingPanel(Style style, |
943 |
alfonx |
516 |
final SimpleFeatureType featureType, final int iconWidth, |
944 |
|
|
final int iconHeight) { |
945 |
alfonx |
397 |
|
946 |
alfonx |
516 |
final List<FeatureTypeStyle> list = style.featureTypeStyles(); |
947 |
alfonx |
397 |
|
948 |
alfonx |
517 |
final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]")); |
949 |
alfonx |
397 |
|
950 |
alfonx |
516 |
if (style == null) { |
951 |
|
|
// No Style => no legend |
952 |
|
|
return panel; |
953 |
|
|
} |
954 |
alfonx |
397 |
|
955 |
alfonx |
516 |
for (final FeatureTypeStyle ftStyle : list) { |
956 |
|
|
|
957 |
alfonx |
397 |
// One child-node for every rule |
958 |
alfonx |
516 |
final List<Rule> rules = ftStyle.rules(); |
959 |
|
|
for (final Rule rule : rules) { |
960 |
alfonx |
397 |
|
961 |
alfonx |
694 |
// Check if this RULE shall actually appear in the legend |
962 |
alfonx |
769 |
if (rule.getName() != null |
963 |
|
|
&& rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT)) |
964 |
alfonx |
694 |
continue; |
965 |
|
|
|
966 |
alfonx |
397 |
/** |
967 |
|
|
* Let's not create a hbox for Rules that only contain |
968 |
|
|
* TextSymbolizers |
969 |
|
|
*/ |
970 |
|
|
if (StylingUtil.getTextSymbolizers(rule.getSymbolizers()) |
971 |
|
|
.size() == rule.getSymbolizers().length) |
972 |
|
|
continue; |
973 |
|
|
|
974 |
|
|
final BufferedImage imageForRule = LegendIconFeatureRenderer |
975 |
|
|
.getInstance().createImageForRule(rule, featureType, |
976 |
alfonx |
516 |
new Dimension(iconWidth, iconHeight)); |
977 |
alfonx |
397 |
|
978 |
alfonx |
516 |
final ImageIcon legendIcon = new ImageIcon(imageForRule); |
979 |
alfonx |
397 |
|
980 |
|
|
final JLabel iconLabel = new JLabel(legendIcon); |
981 |
alfonx |
517 |
panel.add(iconLabel, "sgx1"); |
982 |
|
|
// hbox.setAlignmentX(0f); |
983 |
|
|
// hbox.add(iconLabel); |
984 |
|
|
// hbox.add(Box.createHorizontalStrut(3)); |
985 |
alfonx |
397 |
|
986 |
alfonx |
516 |
final Translation labelT = new Translation(); |
987 |
alfonx |
409 |
labelT.fromOneLine(rule.getDescription().getTitle()); |
988 |
alfonx |
397 |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
989 |
alfonx |
517 |
|
990 |
|
|
panel.add(classTitleLabel, "sgx2"); |
991 |
alfonx |
400 |
classTitleLabel.setLabelFor(iconLabel); |
992 |
alfonx |
397 |
} |
993 |
|
|
} |
994 |
|
|
|
995 |
alfonx |
516 |
return panel; |
996 |
alfonx |
397 |
} |
997 |
|
|
|
998 |
alfonx |
824 |
|
999 |
|
|
|
1000 |
|
|
|
1001 |
alfonx |
397 |
/** |
1002 |
alfonx |
516 |
* Creates a {@link JComponent} that contains a legend for a given |
1003 |
alfonx |
824 |
* {@link StyledRasterInterface} and a given {@link Style}. |
1004 |
alfonx |
400 |
* |
1005 |
alfonx |
516 |
* @param style |
1006 |
|
|
* if <code>null</code>, the default {@link Style} is extracetd |
1007 |
|
|
* from the {@link StyledRasterInterface} |
1008 |
alfonx |
397 |
*/ |
1009 |
alfonx |
824 |
public static JPanel createLegendSwingPanel( |
1010 |
alfonx |
516 |
final StyledRasterInterface<?> styledRaster, Style style, |
1011 |
|
|
final int iconWidth, final int iconHeight) { |
1012 |
alfonx |
403 |
|
1013 |
alfonx |
516 |
// If no style is given, we use the default style for this layer |
1014 |
|
|
if (style == null) |
1015 |
|
|
style = styledRaster.getStyle(); |
1016 |
|
|
|
1017 |
alfonx |
403 |
/** |
1018 |
|
|
* Determine whether a Style is responsible for the coloring |
1019 |
|
|
*/ |
1020 |
|
|
ColorModel colorModel = null; |
1021 |
alfonx |
405 |
if (!isStyleable(styledRaster) |
1022 |
alfonx |
516 |
|| (isStyleable(styledRaster) && style == null)) { |
1023 |
alfonx |
405 |
colorModel = getColorModel(styledRaster); |
1024 |
alfonx |
403 |
} |
1025 |
|
|
|
1026 |
alfonx |
516 |
final RasterLegendData rasterLegendData = styledRaster |
1027 |
|
|
.getLegendMetaData(); |
1028 |
|
|
final List<Double> legendRasterValues = rasterLegendData |
1029 |
|
|
.getSortedKeys(); |
1030 |
|
|
final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData |
1031 |
alfonx |
400 |
.createSampleRasters(); |
1032 |
|
|
|
1033 |
alfonx |
607 |
final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0")); |
1034 |
alfonx |
400 |
|
1035 |
alfonx |
516 |
for (final Double rValue : legendRasterValues) { |
1036 |
alfonx |
400 |
|
1037 |
alfonx |
516 |
// final Dimension ICON_SIZE = new Dimension(iconWidth, |
1038 |
|
|
// new JLabel().getFontMetrics(new JLabel().getFont()) |
1039 |
|
|
// .getHeight() > 5 ? new JLabel().getFontMetrics( |
1040 |
|
|
// new JLabel().getFont()).getHeight() : iconHeight); |
1041 |
alfonx |
400 |
|
1042 |
|
|
// **************************************************************************** |
1043 |
|
|
// Create the actual icon |
1044 |
|
|
// **************************************************************************** |
1045 |
alfonx |
516 |
final BufferedImage buffImage = new BufferedImage(iconWidth, |
1046 |
|
|
iconHeight, BufferedImage.TYPE_INT_ARGB); |
1047 |
alfonx |
517 |
|
1048 |
alfonx |
516 |
final Graphics2D graphics = buffImage.createGraphics(); |
1049 |
alfonx |
400 |
|
1050 |
alfonx |
405 |
if (colorModel != null) { |
1051 |
alfonx |
516 |
// The colors come from the ColorModel! |
1052 |
alfonx |
517 |
|
1053 |
alfonx |
403 |
try { |
1054 |
alfonx |
405 |
Object inData = null; |
1055 |
|
|
switch (colorModel.getTransferType()) { |
1056 |
|
|
case DataBuffer.TYPE_BYTE: |
1057 |
|
|
inData = new byte[] { rValue.byteValue() }; |
1058 |
|
|
break; |
1059 |
|
|
case DataBuffer.TYPE_USHORT: |
1060 |
|
|
inData = new short[] { rValue.shortValue() }; |
1061 |
|
|
break; |
1062 |
|
|
case DataBuffer.TYPE_INT: |
1063 |
|
|
inData = new int[] { rValue.intValue() }; |
1064 |
|
|
break; |
1065 |
|
|
case DataBuffer.TYPE_SHORT: |
1066 |
|
|
inData = new short[] { rValue.shortValue() }; |
1067 |
|
|
break; |
1068 |
|
|
case DataBuffer.TYPE_FLOAT: |
1069 |
|
|
inData = new float[] { rValue.floatValue() }; |
1070 |
|
|
break; |
1071 |
|
|
case DataBuffer.TYPE_DOUBLE: |
1072 |
|
|
inData = new double[] { rValue.doubleValue() }; |
1073 |
|
|
break; |
1074 |
|
|
default: |
1075 |
|
|
inData = rValue.intValue(); |
1076 |
|
|
} |
1077 |
|
|
final Color color = new Color(colorModel.getRGB(inData)); |
1078 |
alfonx |
403 |
graphics.setBackground(color); |
1079 |
|
|
graphics.setColor(color); |
1080 |
alfonx |
516 |
graphics.fillRect(0, 0, iconWidth, iconHeight); |
1081 |
|
|
} catch (final Exception e) { |
1082 |
|
|
LOGGER.info( |
1083 |
alfonx |
420 |
"Dann nehmen wir halt den GridCoverageRenderer", e); |
1084 |
alfonx |
405 |
colorModel = null; |
1085 |
|
|
} |
1086 |
alfonx |
516 |
} else { |
1087 |
|
|
// The colors come from the Style |
1088 |
alfonx |
400 |
|
1089 |
alfonx |
405 |
/** |
1090 |
|
|
* The coverage contains only one value of value rValue |
1091 |
|
|
*/ |
1092 |
alfonx |
516 |
final GridCoverage2D sampleCov = sampleRasters.get(rValue); |
1093 |
alfonx |
405 |
GridCoverageRenderer renderer; |
1094 |
|
|
try { |
1095 |
|
|
renderer = new GridCoverageRenderer(sampleCov |
1096 |
|
|
.getCoordinateReferenceSystem(), JTSUtil |
1097 |
|
|
.createEnvelope(sampleCov.getEnvelope()), |
1098 |
|
|
new Rectangle(iconWidth, iconHeight), |
1099 |
|
|
(AffineTransform) null); |
1100 |
alfonx |
516 |
} catch (final Exception e1) { |
1101 |
alfonx |
405 |
throw new RuntimeException( |
1102 |
alfonx |
516 |
"Creating a GridCoverageRenderer failed:", e1); |
1103 |
alfonx |
405 |
} |
1104 |
alfonx |
403 |
|
1105 |
alfonx |
405 |
/** |
1106 |
|
|
* Iterate over all FeatureTypeStyles. |
1107 |
|
|
*/ |
1108 |
alfonx |
516 |
final List<RasterSymbolizer> rSymbols = StylingUtil |
1109 |
alfonx |
405 |
.getRasterSymbolizers(style); |
1110 |
|
|
|
1111 |
alfonx |
516 |
for (final RasterSymbolizer symbolizer : rSymbols) { |
1112 |
alfonx |
405 |
try { |
1113 |
|
|
renderer.paint(graphics, sampleCov, symbolizer); |
1114 |
alfonx |
516 |
} catch (final Exception ee) { |
1115 |
alfonx |
405 |
LOGGER.error("Unable to paint " + symbolizer |
1116 |
|
|
+ " into the legend image", ee); |
1117 |
alfonx |
403 |
} |
1118 |
alfonx |
400 |
} |
1119 |
alfonx |
405 |
} |
1120 |
alfonx |
400 |
|
1121 |
alfonx |
516 |
final JLabel iconLabel = new JLabel(new ImageIcon(buffImage)); |
1122 |
alfonx |
517 |
panel.add(iconLabel, "sgx1"); |
1123 |
alfonx |
400 |
|
1124 |
alfonx |
516 |
final Translation labelT = rasterLegendData.get(rValue); |
1125 |
alfonx |
400 |
final JLabel classTitleLabel = new JLabel(labelT.toString()); |
1126 |
alfonx |
517 |
panel.add(classTitleLabel, "sgx2" |
1127 |
alfonx |
607 |
+ (rasterLegendData.isPaintGaps() ? ", gapy 0:0:0 5:5:5" |
1128 |
|
|
: "")); |
1129 |
alfonx |
400 |
classTitleLabel.setLabelFor(iconLabel); |
1130 |
|
|
|
1131 |
alfonx |
607 |
if (rasterLegendData.isPaintGaps()) { |
1132 |
alfonx |
403 |
iconLabel |
1133 |
|
|
.setBorder(BorderFactory.createLineBorder(Color.black)); |
1134 |
|
|
} |
1135 |
|
|
|
1136 |
alfonx |
400 |
} |
1137 |
|
|
|
1138 |
alfonx |
516 |
return panel; |
1139 |
alfonx |
397 |
} |
1140 |
alfonx |
824 |
|
1141 |
|
|
|
1142 |
alfonx |
397 |
|
1143 |
alfonx |
405 |
/** |
1144 |
|
|
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
1145 |
|
|
* return <code>null</code> if the geoobject can not be accessed. |
1146 |
|
|
*/ |
1147 |
alfonx |
420 |
@SuppressWarnings("unchecked") |
1148 |
alfonx |
516 |
public static ColorModel getColorModel( |
1149 |
|
|
final StyledRasterInterface<?> styledGrid) { |
1150 |
alfonx |
405 |
ColorModel colorModel = null; |
1151 |
|
|
try { |
1152 |
alfonx |
516 |
final Object geoObject = styledGrid.getGeoObject(); |
1153 |
alfonx |
405 |
if (geoObject instanceof GridCoverage2D) { |
1154 |
alfonx |
516 |
final GridCoverage2D cov = (GridCoverage2D) geoObject; |
1155 |
alfonx |
405 |
colorModel = cov.getRenderedImage().getColorModel(); |
1156 |
alfonx |
419 |
} else if (styledGrid instanceof StyledRasterPyramidInterface) { |
1157 |
alfonx |
405 |
|
1158 |
alfonx |
516 |
final Parameter readGG = new Parameter( |
1159 |
alfonx |
405 |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
1160 |
|
|
|
1161 |
alfonx |
516 |
final ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope( |
1162 |
alfonx |
405 |
styledGrid.getEnvelope(), styledGrid.getCrs()); |
1163 |
|
|
|
1164 |
|
|
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
1165 |
alfonx |
420 |
new Rectangle(0, 0, 1, 1)), mapExtend)); |
1166 |
alfonx |
405 |
|
1167 |
alfonx |
516 |
final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject; |
1168 |
alfonx |
420 |
|
1169 |
|
|
final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil |
1170 |
|
|
.getWrappedGeoObject(rFc); |
1171 |
alfonx |
516 |
final GridCoverage2D cov = (GridCoverage2D) aReader |
1172 |
alfonx |
405 |
.read(new GeneralParameterValue[] { readGG }); |
1173 |
|
|
colorModel = cov.getRenderedImage().getColorModel(); |
1174 |
|
|
} |
1175 |
alfonx |
516 |
} catch (final Exception e) { |
1176 |
alfonx |
420 |
LOGGER.error("Error reading the colormodel from " + styledGrid, e); |
1177 |
alfonx |
405 |
return null; |
1178 |
|
|
} |
1179 |
|
|
return colorModel; |
1180 |
|
|
} |
1181 |
|
|
|
1182 |
|
|
/** |
1183 |
|
|
* @return <code>true</code> if a {@link RasterSymbolizer} can be applied |
1184 |
|
|
* and will have an effect. Some rasters (e.g. GeoTIFF) can come |
1185 |
|
|
* with their own {@link ColorModel} and will ignore any |
1186 |
|
|
* {@link RasterSymbolizer} = SLD. |
1187 |
|
|
*/ |
1188 |
alfonx |
516 |
public static boolean isStyleable( |
1189 |
|
|
final StyledRasterInterface<?> styledRaster) { |
1190 |
|
|
final ColorModel colorModel = getColorModel(styledRaster); |
1191 |
alfonx |
405 |
|
1192 |
alfonx |
607 |
// LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is " |
1193 |
|
|
// + colorModel != null ? colorModel.getClass().getSimpleName() : |
1194 |
|
|
// "NULL"); |
1195 |
|
|
|
1196 |
alfonx |
405 |
if (colorModel == null) |
1197 |
|
|
return true; |
1198 |
|
|
if (colorModel instanceof ComponentColorModel) |
1199 |
|
|
return true; |
1200 |
|
|
return false; |
1201 |
|
|
} |
1202 |
alfonx |
517 |
|
1203 |
|
|
/** |
1204 |
|
|
* Set the given Style as the Style of the {@link MapLayer}, unless the |
1205 |
|
|
* styles are the same (not comparing selection stuff). If the |
1206 |
|
|
* {@link MapLayer}s {@link Style} is changed, the selection FTS is kept.<br/> |
1207 |
|
|
* Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a |
1208 |
|
|
* repaint, so only use it when needed. |
1209 |
|
|
* |
1210 |
alfonx |
607 |
* @return <code>true</code> if the {@link MapLayer}'s {@link Style} has |
1211 |
|
|
* been changed. |
1212 |
alfonx |
517 |
*/ |
1213 |
alfonx |
607 |
public static boolean updateMapLayerStyleIfChangedAndKeepSelection( |
1214 |
|
|
MapLayer mapLayer, Style style2) { |
1215 |
alfonx |
517 |
|
1216 |
|
|
Style mapLayerStyleCleaned = StylingUtil |
1217 |
alfonx |
607 |
.removeSelectionFeatureTypeStyle(mapLayer.getStyle()); |
1218 |
|
|
|
1219 |
|
|
Style newStyleCleaned = StylingUtil |
1220 |
|
|
.removeSelectionFeatureTypeStyle(style2); |
1221 |
|
|
|
1222 |
|
|
if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned, newStyleCleaned)) { |
1223 |
|
|
|
1224 |
alfonx |
517 |
// They are different when compared without SELECTION FTS! |
1225 |
alfonx |
607 |
|
1226 |
alfonx |
517 |
// Now let's copy any SELECTION FTS to the now style |
1227 |
alfonx |
607 |
FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil |
1228 |
|
|
.getSelectionFeatureTypeStyle(mapLayer.getStyle()); |
1229 |
alfonx |
517 |
if (selectionFeatureTypeStyle != null) { |
1230 |
alfonx |
607 |
newStyleCleaned.featureTypeStyles().add( |
1231 |
|
|
selectionFeatureTypeStyle); |
1232 |
|
|
// newStyleCleaned is not so clean anymore... We just alled a |
1233 |
|
|
// selcetion FTS |
1234 |
|
|
} |
1235 |
|
|
|
1236 |
alfonx |
517 |
mapLayer.setStyle(newStyleCleaned); |
1237 |
alfonx |
607 |
|
1238 |
alfonx |
517 |
return true; |
1239 |
alfonx |
607 |
|
1240 |
alfonx |
526 |
} else { |
1241 |
|
|
return false; |
1242 |
|
|
} |
1243 |
alfonx |
517 |
} |
1244 |
|
|
|
1245 |
alfonx |
607 |
/** |
1246 |
|
|
* After loading an atlas, the AttribteMetaData contains whatever is written |
1247 |
|
|
* in the XML. But the DBF may have changed! This method checks an |
1248 |
|
|
* {@link AttributeMetadataMap} against a schema and also corrects |
1249 |
|
|
* upperCase/lowerCase problems. It will also remove any geometry column |
1250 |
|
|
* attribute metadata. |
1251 |
|
|
*/ |
1252 |
|
|
/** |
1253 |
|
|
* After loading an atlas, the AttribteMetaData contains whatever is written |
1254 |
|
|
* in the XML. But the DBF may have changed! |
1255 |
|
|
*/ |
1256 |
alfonx |
677 |
public static void checkAttribMetaData( |
1257 |
alfonx |
769 |
AttributeMetadataMap<AttributeMetadataImpl> attributeMetaDataMap, |
1258 |
|
|
SimpleFeatureType schema) { |
1259 |
alfonx |
607 |
|
1260 |
alfonx |
769 |
if (schema == null) |
1261 |
|
|
throw new IllegalArgumentException("schmema may not be null!"); |
1262 |
|
|
|
1263 |
alfonx |
607 |
ArrayList<Name> willRemove = new ArrayList<Name>(); |
1264 |
|
|
|
1265 |
|
|
// 1. Check.. all attributes in the atm should be in the schema as well. |
1266 |
|
|
// maybe correct some upperCase/loweCase stuff |
1267 |
|
|
|
1268 |
alfonx |
772 |
for (AttributeMetadataInterface atm : attributeMetaDataMap.values()) { |
1269 |
alfonx |
607 |
|
1270 |
|
|
AttributeDescriptor foundDescr = schema |
1271 |
|
|
.getDescriptor(atm.getName()); |
1272 |
|
|
if (foundDescr == null) { |
1273 |
alfonx |
770 |
NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute(schema, |
1274 |
alfonx |
607 |
atm.getLocalName()); |
1275 |
|
|
if (bestMatch == null) |
1276 |
|
|
willRemove.add(atm.getName()); |
1277 |
|
|
else |
1278 |
|
|
atm.setName(bestMatch); |
1279 |
|
|
} else if (foundDescr instanceof GeometryDescriptor) { |
1280 |
|
|
// We don't want GeometryColumns in here |
1281 |
|
|
willRemove.add(atm.getName()); |
1282 |
|
|
} |
1283 |
|
|
} |
1284 |
|
|
|
1285 |
|
|
// Remove the ones that were not findable in the schema |
1286 |
|
|
for (Name removeName : willRemove) { |
1287 |
alfonx |
677 |
if (attributeMetaDataMap.remove(removeName) == null) { |
1288 |
alfonx |
607 |
LOGGER.warn("removing the AMData didn't work"); |
1289 |
|
|
} |
1290 |
|
|
} |
1291 |
|
|
|
1292 |
|
|
// 2. check... all attributes from the schema must have an ATM |
1293 |
|
|
for (AttributeDescriptor ad : schema.getAttributeDescriptors()) { |
1294 |
|
|
if (ad instanceof GeometryDescriptor) |
1295 |
|
|
continue; |
1296 |
|
|
if (!attributeMetaDataMap.containsKey(ad.getName())) { |
1297 |
alfonx |
770 |
attributeMetaDataMap.put( new NameImpl(ad.getName().getNamespaceURI(), ad.getName().getLocalPart()), |
1298 |
alfonx |
769 |
new AttributeMetadataImpl(ad, schema |
1299 |
|
|
.getAttributeDescriptors().indexOf(ad), |
1300 |
|
|
attributeMetaDataMap.getLanguages())); |
1301 |
alfonx |
607 |
} |
1302 |
|
|
} |
1303 |
|
|
} |
1304 |
|
|
|
1305 |
alfonx |
677 |
/** |
1306 |
|
|
* Checks every attribute name in the {@link AttributeMetadataMap} for its |
1307 |
|
|
* binding type. It the type is textual, add the mrpty string as a NODATA |
1308 |
|
|
* value. |
1309 |
|
|
* |
1310 |
|
|
* @param attributeMetaDataMap |
1311 |
|
|
* @param schema |
1312 |
|
|
*/ |
1313 |
|
|
public static void addEmptyStringToAllTextualAttributes( |
1314 |
alfonx |
772 |
AttributeMetadataMap<? extends AttributeMetadataInterface> attributeMetaDataMap, |
1315 |
alfonx |
769 |
SimpleFeatureType schema) { |
1316 |
alfonx |
677 |
|
1317 |
|
|
for (Name name : attributeMetaDataMap.keySet()) { |
1318 |
|
|
if (String.class.isAssignableFrom(schema.getDescriptor(name) |
1319 |
|
|
.getType().getBinding())) { |
1320 |
|
|
attributeMetaDataMap.get(name).getNodataValues().add(""); |
1321 |
|
|
} |
1322 |
|
|
} |
1323 |
|
|
} |
1324 |
alfonx |
769 |
|
1325 |
|
|
/** |
1326 |
|
|
* @return a nicely formatted String containing all NODATA values of any |
1327 |
alfonx |
772 |
* {@link AttributeMetadataInterface} object. Strings are quoted so that any |
1328 |
alfonx |
769 |
* empty {@link String} can be seen. |
1329 |
|
|
*/ |
1330 |
|
|
public static String formatNoDataValues(Set<Object> nodataValuesList) { |
1331 |
|
|
String nicelyFormatted = ""; |
1332 |
|
|
if (nodataValuesList != null) { |
1333 |
|
|
if (nodataValuesList.size() == 0) |
1334 |
|
|
nicelyFormatted = ""; |
1335 |
|
|
else { |
1336 |
|
|
for (Object ndo : nodataValuesList) { |
1337 |
|
|
if (ndo instanceof String) |
1338 |
|
|
nicelyFormatted += "\"" + ndo + "\""; |
1339 |
|
|
else |
1340 |
|
|
nicelyFormatted += ndo.toString(); |
1341 |
|
|
|
1342 |
|
|
nicelyFormatted += ","; |
1343 |
|
|
} |
1344 |
|
|
// Remove the extra comma |
1345 |
|
|
nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted |
1346 |
|
|
.length() - 1); |
1347 |
|
|
} |
1348 |
|
|
} |
1349 |
|
|
return nicelyFormatted; |
1350 |
|
|
} |
1351 |
alfonx |
244 |
} |