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 |
|
|
46 |
import java.util.ArrayList; |
import java.util.ArrayList; |
47 |
import java.util.List; |
import java.util.List; |
48 |
import java.util.Map; |
import java.util.Map; |
49 |
|
import java.util.Set; |
50 |
|
|
51 |
import javax.swing.BorderFactory; |
import javax.swing.BorderFactory; |
52 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
92 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
93 |
import schmitzm.swing.JPanel; |
import schmitzm.swing.JPanel; |
94 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
95 |
import skrueger.AttributeMetadata; |
import skrueger.AttributeMetadataImpl; |
96 |
|
import skrueger.AttributeMetadataInterface; |
97 |
import skrueger.RasterLegendData; |
import skrueger.RasterLegendData; |
98 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
99 |
|
|
254 |
* TODO replace with |
* TODO replace with |
255 |
* {@link AttributeMetadataMap#sortedValuesVisibleOnly()} |
* {@link AttributeMetadataMap#sortedValuesVisibleOnly()} |
256 |
*/ |
*/ |
257 |
public static AttributeMetadataMap getVisibleAttributeMetaData( |
public static AttributeMetadataMap<? extends AttributeMetadataInterface > getVisibleAttributeMetaData( |
258 |
final AttributeMetadataMap amdMap, final boolean visible) { |
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap, |
259 |
|
final boolean visible) { |
260 |
final AttributeMetadataMap filteredMap = new AttributeMetadataMap( |
|
261 |
amdMap.getLanguages()); |
final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap.clone(); |
262 |
for (final AttributeMetadata amd : amdMap.values()) |
if (filteredMap.size() > 0 ) { |
263 |
|
filteredMap.clear(); // Just in case the close copies the contents |
264 |
|
} |
265 |
|
|
266 |
|
for (final AttributeMetadataInterface amd : amdMap.values()) |
267 |
if (amd.isVisible() == visible) |
if (amd.isVisible() == visible) |
268 |
filteredMap.put(amd.getName(), amd); |
filteredMap.put(amd.getName(), amd); |
269 |
|
|
271 |
} |
} |
272 |
|
|
273 |
/** |
/** |
274 |
* Parses a {@link AttributeMetadata} object from an JDOM-{@link Element}. |
* Parses a {@link AttributeMetadataImpl} object from an JDOM- |
275 |
* This method works like {@link |
* {@link Element}. This method works like {@link |
276 |
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
* AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM. |
277 |
* |
* |
278 |
* TODO 20.11.2009, SK: There are some new attribute weight, functiona, |
* TODO 20.11.2009, SK: There are some new attribute weight, functiona, |
283 |
* @param element |
* @param element |
284 |
* {@link Element} to parse |
* {@link Element} to parse |
285 |
*/ |
*/ |
286 |
public static AttributeMetadata parseAttributeMetaData(final Element element) { |
public static AttributeMetadataImpl parseAttributeMetaData( |
287 |
final String namespace = String.valueOf(element |
final Element element) { |
288 |
.getAttributeValue("namespace")); |
final String namespace = element.getAttributeValue("namespace"); |
289 |
final String localname = String.valueOf(element |
final String localname = element.getAttributeValue("localname"); |
290 |
.getAttributeValue("localname")); |
final NameImpl aName = new NameImpl(namespace, localname); |
|
final Name aName = new NameImpl(namespace, localname); |
|
291 |
final Boolean visible = Boolean.valueOf(element |
final Boolean visible = Boolean.valueOf(element |
292 |
.getAttributeValue("visible")); |
.getAttributeValue("visible")); |
293 |
final String unit = element.getAttributeValue("unit"); |
final String unit = element.getAttributeValue("unit"); |
303 |
else if (childElement.getName().equals("desc")) |
else if (childElement.getName().equals("desc")) |
304 |
desc = parseTranslation(childElement); |
desc = parseTranslation(childElement); |
305 |
} |
} |
306 |
return new AttributeMetadata(aName, visible, name, desc, unit); |
return new AttributeMetadataImpl(aName, visible, name, desc, unit); |
307 |
} |
} |
308 |
|
|
309 |
/** |
/** |
310 |
* Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with |
* Parses a {@link AttributeMetadataImpl} map from an JDOM-{@link Element} |
311 |
* {@code <attribute>}-childs. |
* with {@code <attribute>}-childs. |
312 |
* |
* |
313 |
* @param element |
* @param element |
314 |
* {@link Element} to parse |
* {@link Element} to parse |
315 |
* |
* |
316 |
* TODO Since GP 1.3 the {@link AttributeMetadata} class has more |
* TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has |
317 |
* attributes which are not used by Xulu/ISDSS. GP |
* more attributes which are not used by Xulu/ISDSS. GP |
318 |
* exports/imports the AMD via AMLExporter and AMLImporter |
* exports/imports the AMD via AMLExporter and AMLImporter |
319 |
* classes. (SK, 3.2.2010) * |
* classes. (SK, 3.2.2010) * |
320 |
*/ |
*/ |
321 |
public static AttributeMetadataMap parseAttributeMetaDataMap( |
public static AttributeMetadataMap parseAttributeMetaDataMap( |
322 |
final Element element) { |
final Element element) { |
323 |
final AttributeMetadataMap metaData = new AttributeMetadataMap(); |
final AttributeMetadataMap metaData = new AttributeMetadataImplMap(); |
324 |
final List<Element> attributesElements = element |
final List<Element> attributesElements = element |
325 |
.getChildren(ELEM_NAME_ATTRIBUTE); |
.getChildren(ELEM_NAME_ATTRIBUTE); |
326 |
for (final Element attibuteElement : attributesElements) { |
for (final Element attibuteElement : attributesElements) { |
327 |
final AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement); |
final AttributeMetadataImpl attrMetaData = parseAttributeMetaData(attibuteElement); |
328 |
metaData.put(attrMetaData.getName(), attrMetaData); |
metaData.put(attrMetaData.getName(), attrMetaData); |
329 |
} |
} |
330 |
return metaData; |
return metaData; |
331 |
} |
} |
332 |
|
|
333 |
/** |
/** |
334 |
* Loads a {@link AttributeMetadata} object from an URL. |
* Loads a {@link AttributeMetadataImpl} object from an URL. |
335 |
* |
* |
336 |
* @param documentUrl |
* @param documentUrl |
337 |
* {@link URL} to parse |
* {@link URL} to parse |
344 |
} |
} |
345 |
|
|
346 |
/** |
/** |
347 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetadata} |
* Creates an JDOM {@link Element} for the given |
348 |
* object. |
* {@link AttributeMetadataImpl} object. |
349 |
* |
* |
350 |
* @param amd |
* @param amd |
351 |
* meta data for one attribute |
* meta data for one attribute |
352 |
* |
* |
353 |
* TODO Since GP 1.3 the {@link AttributeMetadata} class has more |
* TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has |
354 |
* attributes which are not used by Xulu/ISDSS. GP |
* more attributes which are not used by Xulu/ISDSS. GP |
355 |
* exports/imports the AMD via AMLExporter and AMLImporter |
* exports/imports the AMD via AMLExporter and AMLImporter |
356 |
* classes. (SK, 3.2.2010) |
* classes. (SK, 3.2.2010) |
357 |
*/ |
*/ |
358 |
public static Element createAttributeMetaDataElement( |
public static Element createAttributeMetaDataElement( |
359 |
final AttributeMetadata amd) { |
final AttributeMetadataInterface amd) { |
360 |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
361 |
element.setAttribute("namespace", String.valueOf(amd.getName() |
element.setAttribute("namespace", String.valueOf(amd.getName() |
362 |
.getNamespaceURI())); |
.getNamespaceURI())); |
371 |
} |
} |
372 |
|
|
373 |
/** |
/** |
374 |
* Creates an JDOM {@link Element} for the given {@link AttributeMetadata} |
* Creates an JDOM {@link Element} for the given |
375 |
* map. |
* {@link AttributeMetadataImpl} map. |
376 |
* |
* |
377 |
* @param amdMap |
* @param amdMap |
378 |
* map of attribute meta data |
* map of attribute meta data |
379 |
*/ |
*/ |
380 |
public static Element createAttributeMetaDataMapElement( |
public static Element createAttributeMetaDataMapElement( |
381 |
final AttributeMetadataMap amdMap) { |
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap) { |
382 |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
final Element element = new Element(ELEM_NAME_AMD, AMLURI); |
383 |
for (final AttributeMetadata amd : amdMap.values()) |
for (final AttributeMetadataInterface amd : amdMap.values()) |
384 |
element.addContent(createAttributeMetaDataElement(amd)); |
element.addContent(createAttributeMetaDataElement(amd)); |
385 |
return element; |
return element; |
386 |
} |
} |
387 |
|
|
388 |
/** |
/** |
389 |
* Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL. |
* Saves a {@link AttributeMetadataImpl AttributeMetaData-Map} to an URL. |
390 |
* |
* |
391 |
* @param amdMap |
* @param amdMap |
392 |
* map of {@link AttributeMetadata} |
* map of {@link AttributeMetadataImpl} |
393 |
* @param documentUrl |
* @param documentUrl |
394 |
* {@link URL} to store the XML |
* {@link URL} to store the XML |
395 |
*/ |
*/ |
796 |
} |
} |
797 |
|
|
798 |
/** |
/** |
799 |
* Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata |
* Loads a {@linkplain Style SLD-Style} and a |
800 |
* AttributeMetaData-Map} for a given geo-object (feature) source. The SLD |
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} for a given |
801 |
* file must be present. A missing attribute meta-data file is tolerated. |
* geo-object (feature) source. The SLD file must be present. A missing |
802 |
|
* attribute meta-data file is tolerated. |
803 |
* |
* |
804 |
* @param geoObjectURL |
* @param geoObjectURL |
805 |
* URL of the (already read) feature object |
* URL of the (already read) feature object |
842 |
|
|
843 |
/** |
/** |
844 |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
845 |
* {@linkplain AttributeMetadata AttributeMetaData-Map} from a {@code .amd} |
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a {@code |
846 |
* file for a given geo-object (feature) source. The SLD file must be |
* .amd} file for a given geo-object (feature) source. The SLD file must be |
847 |
* present. A missing attribute meta-data file is tolerated. |
* present. A missing attribute meta-data file is tolerated. |
848 |
* |
* |
849 |
* @param geoObjectURL |
* @param geoObjectURL |
903 |
|
|
904 |
/** |
/** |
905 |
* 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 |
906 |
* meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a |
* meta data ({@link RasterLegendData} or {@link AttributeMetadataImpl}) to |
907 |
* {@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. |
908 |
* |
* |
909 |
* @param style |
* @param style |
910 |
* style to save |
* style to save |
929 |
* Creates a {@link JPanel} that shows a legend for a list of |
* Creates a {@link JPanel} that shows a legend for a list of |
930 |
* {@link FeatureTypeStyle}s and a targeted featureType |
* {@link FeatureTypeStyle}s and a targeted featureType |
931 |
* |
* |
932 |
|
* @param style |
933 |
|
* The Style to presented in this legend |
934 |
* @param featureType |
* @param featureType |
935 |
* If this a legend for Point, Polygon or Line? |
* If this a legend for Point, Polygon or Line? |
|
* @param list |
|
|
* The Styles to presented in this legend |
|
936 |
* |
* |
937 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons |
938 |
* Krüger</a> |
* Tzeggai</a> |
939 |
*/ |
*/ |
940 |
public static JPanel createLegendPanel(Style style, |
public static JPanel createLegendSwingPanel(Style style, |
941 |
final SimpleFeatureType featureType, final int iconWidth, |
final SimpleFeatureType featureType, final int iconWidth, |
942 |
final int iconHeight) { |
final int iconHeight) { |
943 |
|
|
957 |
for (final Rule rule : rules) { |
for (final Rule rule : rules) { |
958 |
|
|
959 |
// Check if this RULE shall actually appear in the legend |
// Check if this RULE shall actually appear in the legend |
960 |
if (rule.getName() != null && rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT)) |
if (rule.getName() != null |
961 |
|
&& rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT)) |
962 |
continue; |
continue; |
963 |
|
|
964 |
/** |
/** |
993 |
return panel; |
return panel; |
994 |
} |
} |
995 |
|
|
996 |
|
|
997 |
|
|
998 |
|
|
999 |
/** |
/** |
1000 |
* Creates a {@link JComponent} that contains a legend for a given |
* Creates a {@link JComponent} that contains a legend for a given |
1001 |
* rasterLayer and a given {@link Style}. |
* {@link StyledRasterInterface} and a given {@link Style}. |
1002 |
* |
* |
1003 |
* @param style |
* @param style |
1004 |
* if <code>null</code>, the default {@link Style} is extracetd |
* if <code>null</code>, the default {@link Style} is extracetd |
1005 |
* from the {@link StyledRasterInterface} |
* from the {@link StyledRasterInterface} |
1006 |
*/ |
*/ |
1007 |
public static JPanel createLegendPanel( |
public static JPanel createLegendSwingPanel( |
1008 |
final StyledRasterInterface<?> styledRaster, Style style, |
final StyledRasterInterface<?> styledRaster, Style style, |
1009 |
final int iconWidth, final int iconHeight) { |
final int iconWidth, final int iconHeight) { |
1010 |
|
|
1135 |
|
|
1136 |
return panel; |
return panel; |
1137 |
} |
} |
1138 |
|
|
1139 |
|
|
1140 |
|
|
1141 |
/** |
/** |
1142 |
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
* Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May |
1151 |
if (geoObject instanceof GridCoverage2D) { |
if (geoObject instanceof GridCoverage2D) { |
1152 |
final GridCoverage2D cov = (GridCoverage2D) geoObject; |
final GridCoverage2D cov = (GridCoverage2D) geoObject; |
1153 |
colorModel = cov.getRenderedImage().getColorModel(); |
colorModel = cov.getRenderedImage().getColorModel(); |
1154 |
} else if (styledGrid instanceof StyledRasterPyramidInterface) { |
} else if (styledGrid instanceof StyledGridCoverageReaderInterface) { |
1155 |
|
|
1156 |
final Parameter readGG = new Parameter( |
final Parameter readGG = new Parameter( |
1157 |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
1252 |
* in the XML. But the DBF may have changed! |
* in the XML. But the DBF may have changed! |
1253 |
*/ |
*/ |
1254 |
public static void checkAttribMetaData( |
public static void checkAttribMetaData( |
1255 |
AttributeMetadataMap attributeMetaDataMap, SimpleFeatureType schema) { |
AttributeMetadataMap<AttributeMetadataImpl> attributeMetaDataMap, |
1256 |
|
SimpleFeatureType schema) { |
1257 |
|
|
1258 |
|
if (schema == null) |
1259 |
|
throw new IllegalArgumentException("schmema may not be null!"); |
1260 |
|
|
1261 |
ArrayList<Name> willRemove = new ArrayList<Name>(); |
ArrayList<Name> willRemove = new ArrayList<Name>(); |
1262 |
|
|
1263 |
// 1. Check.. all attributes in the atm should be in the schema as well. |
// 1. Check.. all attributes in the atm should be in the schema as well. |
1264 |
// maybe correct some upperCase/loweCase stuff |
// maybe correct some upperCase/loweCase stuff |
1265 |
|
|
1266 |
for (AttributeMetadata atm : attributeMetaDataMap.values()) { |
for (AttributeMetadataInterface atm : attributeMetaDataMap.values()) { |
1267 |
|
|
1268 |
AttributeDescriptor foundDescr = schema |
AttributeDescriptor foundDescr = schema |
1269 |
.getDescriptor(atm.getName()); |
.getDescriptor(atm.getName()); |
1270 |
if (foundDescr == null) { |
if (foundDescr == null) { |
1271 |
Name bestMatch = FeatureUtil.findBestMatchingAttribute(schema, |
NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute(schema, |
1272 |
atm.getLocalName()); |
atm.getLocalName()); |
1273 |
if (bestMatch == null) |
if (bestMatch == null) |
1274 |
willRemove.add(atm.getName()); |
willRemove.add(atm.getName()); |
1292 |
if (ad instanceof GeometryDescriptor) |
if (ad instanceof GeometryDescriptor) |
1293 |
continue; |
continue; |
1294 |
if (!attributeMetaDataMap.containsKey(ad.getName())) { |
if (!attributeMetaDataMap.containsKey(ad.getName())) { |
1295 |
attributeMetaDataMap.put(ad.getName(), new AttributeMetadata( |
attributeMetaDataMap.put( new NameImpl(ad.getName().getNamespaceURI(), ad.getName().getLocalPart()), |
1296 |
ad, schema.getAttributeDescriptors().indexOf(ad), |
new AttributeMetadataImpl(ad, schema |
1297 |
attributeMetaDataMap.getLanguages())); |
.getAttributeDescriptors().indexOf(ad), |
1298 |
|
attributeMetaDataMap.getLanguages())); |
1299 |
} |
} |
1300 |
} |
} |
1301 |
} |
} |
1309 |
* @param schema |
* @param schema |
1310 |
*/ |
*/ |
1311 |
public static void addEmptyStringToAllTextualAttributes( |
public static void addEmptyStringToAllTextualAttributes( |
1312 |
AttributeMetadataMap attributeMetaDataMap, SimpleFeatureType schema) { |
AttributeMetadataMap<? extends AttributeMetadataInterface> attributeMetaDataMap, |
1313 |
|
SimpleFeatureType schema) { |
1314 |
|
|
1315 |
for (Name name : attributeMetaDataMap.keySet()) { |
for (Name name : attributeMetaDataMap.keySet()) { |
1316 |
if (String.class.isAssignableFrom(schema.getDescriptor(name) |
if (String.class.isAssignableFrom(schema.getDescriptor(name) |
1319 |
} |
} |
1320 |
} |
} |
1321 |
} |
} |
1322 |
|
|
1323 |
|
/** |
1324 |
|
* @return a nicely formatted String containing all NODATA values of any |
1325 |
|
* {@link AttributeMetadataInterface} object. Strings are quoted so that any |
1326 |
|
* empty {@link String} can be seen. |
1327 |
|
*/ |
1328 |
|
public static String formatNoDataValues(Set<Object> nodataValuesList) { |
1329 |
|
String nicelyFormatted = ""; |
1330 |
|
if (nodataValuesList != null) { |
1331 |
|
if (nodataValuesList.size() == 0) |
1332 |
|
nicelyFormatted = ""; |
1333 |
|
else { |
1334 |
|
for (Object ndo : nodataValuesList) { |
1335 |
|
if (ndo instanceof String) |
1336 |
|
nicelyFormatted += "\"" + ndo + "\""; |
1337 |
|
else |
1338 |
|
nicelyFormatted += ndo.toString(); |
1339 |
|
|
1340 |
|
nicelyFormatted += ","; |
1341 |
|
} |
1342 |
|
// Remove the extra comma |
1343 |
|
nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted |
1344 |
|
.length() - 1); |
1345 |
|
} |
1346 |
|
} |
1347 |
|
return nicelyFormatted; |
1348 |
|
} |
1349 |
} |
} |