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 |
|
|
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; |
91 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
92 |
import schmitzm.io.IOUtil; |
import schmitzm.io.IOUtil; |
93 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
94 |
|
import schmitzm.swing.ExceptionDialog; |
95 |
import schmitzm.swing.JPanel; |
import schmitzm.swing.JPanel; |
96 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
|
import skrueger.AttributeMetadataInterface; |
|
97 |
import skrueger.AttributeMetadataImpl; |
import skrueger.AttributeMetadataImpl; |
98 |
|
import skrueger.AttributeMetadataInterface; |
99 |
import skrueger.RasterLegendData; |
import skrueger.RasterLegendData; |
100 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
101 |
|
|
102 |
|
import com.vividsolutions.jts.geom.Geometry; |
103 |
|
|
104 |
/** |
/** |
105 |
* This class provides static helper methods for dealing with |
* This class provides static helper methods for dealing with |
106 |
* {@link StyledLayerInterface} stuff. |
* {@link StyledLayerInterface} stuff. |
258 |
* TODO replace with |
* TODO replace with |
259 |
* {@link AttributeMetadataMap#sortedValuesVisibleOnly()} |
* {@link AttributeMetadataMap#sortedValuesVisibleOnly()} |
260 |
*/ |
*/ |
261 |
public static AttributeMetadataMap<? extends AttributeMetadataInterface > getVisibleAttributeMetaData( |
public static AttributeMetadataMap<? extends AttributeMetadataInterface> getVisibleAttributeMetaData( |
262 |
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap, |
final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap, |
263 |
final boolean visible) { |
final boolean visible) { |
264 |
|
|
265 |
final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap.clone(); |
final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap |
266 |
if (filteredMap.size() > 0 ) { |
.clone(); |
267 |
|
if (filteredMap.size() > 0) { |
268 |
filteredMap.clear(); // Just in case the close copies the contents |
filteredMap.clear(); // Just in case the close copies the contents |
269 |
} |
} |
270 |
|
|
271 |
for (final AttributeMetadataInterface amd : amdMap.values()) |
for (final AttributeMetadataInterface amd : amdMap.values()) |
272 |
if (amd.isVisible() == visible) |
if (amd.isVisible() == visible) |
273 |
filteredMap.put(amd.getName(), amd); |
filteredMap.put(amd.getName(), amd); |
290 |
*/ |
*/ |
291 |
public static AttributeMetadataImpl parseAttributeMetaData( |
public static AttributeMetadataImpl parseAttributeMetaData( |
292 |
final Element element) { |
final Element element) { |
293 |
final String namespace = String.valueOf(element |
final String namespace = element.getAttributeValue("namespace"); |
294 |
.getAttributeValue("namespace")); |
final String localname = element.getAttributeValue("localname"); |
|
final String localname = String.valueOf(element |
|
|
.getAttributeValue("localname")); |
|
295 |
final NameImpl aName = new NameImpl(namespace, localname); |
final NameImpl aName = new NameImpl(namespace, localname); |
296 |
final Boolean visible = Boolean.valueOf(element |
final Boolean visible = Boolean.valueOf(element |
297 |
.getAttributeValue("visible")); |
.getAttributeValue("visible")); |
363 |
public static Element createAttributeMetaDataElement( |
public static Element createAttributeMetaDataElement( |
364 |
final AttributeMetadataInterface amd) { |
final AttributeMetadataInterface amd) { |
365 |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI); |
366 |
element.setAttribute("namespace", String.valueOf(amd.getName() |
element.setAttribute("namespace", |
367 |
.getNamespaceURI())); |
String.valueOf(amd.getName().getNamespaceURI())); |
368 |
element.setAttribute("localname", String.valueOf(amd.getLocalName())); |
element.setAttribute("localname", String.valueOf(amd.getLocalName())); |
369 |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
element.setAttribute("visible", String.valueOf(amd.isVisible())); |
370 |
element.setAttribute("unit", amd.getUnit()); |
element.setAttribute("unit", amd.getUnit()); |
696 |
* @param styledObject |
* @param styledObject |
697 |
* a styled object |
* a styled object |
698 |
* @return {@code StyledLayerStyle<RasterLegendData>} for |
* @return {@code StyledLayerStyle<RasterLegendData>} for |
699 |
* {@link StyledGridCoverageInterface} or {@code |
* {@link StyledGridCoverageInterface} or |
700 |
* StyledLayerStyle<Map<Integer,AttributeMetaData>>} for |
* {@code StyledLayerStyle<Map<Integer,AttributeMetaData>>} for |
701 |
* {@link StyledFeatureCollectionInterface} |
* {@link StyledFeatureCollectionInterface} |
702 |
*/ |
*/ |
703 |
public static StyledLayerStyle<?> getStyledLayerStyle( |
public static StyledLayerStyle<?> getStyledLayerStyle( |
847 |
|
|
848 |
/** |
/** |
849 |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
* Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and |
850 |
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a {@code |
* {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a |
851 |
* .amd} 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 |
852 |
* present. A missing attribute meta-data file is tolerated. |
* must be present. A missing attribute meta-data file is tolerated. |
853 |
* |
* |
854 |
* @param geoObjectURL |
* @param geoObjectURL |
855 |
* URL of the (already read) feature object |
* URL of the (already read) feature object |
883 |
// Store the SLD |
// Store the SLD |
884 |
final Style sldStyle = style.getGeoObjectStyle(); |
final Style sldStyle = style.getGeoObjectStyle(); |
885 |
if (sldStyle != null) { |
if (sldStyle != null) { |
886 |
StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File( |
StylingUtil.saveStyleToSld(sldStyle, IOUtil.changeFileExt(new File( |
887 |
geoObjectURL.toURI()), sldExt)); |
geoObjectURL.toURI()), sldExt)); |
888 |
} |
} |
889 |
|
|
891 |
final T metaData = style.getMetaData(); |
final T metaData = style.getMetaData(); |
892 |
if (metaData != null) { |
if (metaData != null) { |
893 |
if (metaData instanceof RasterLegendData) { |
if (metaData instanceof RasterLegendData) { |
894 |
saveRasterLegendData((RasterLegendData) metaData, IOUtil |
saveRasterLegendData((RasterLegendData) metaData, |
895 |
.changeUrlExt(geoObjectURL, mdExt)); |
IOUtil.changeUrlExt(geoObjectURL, mdExt)); |
896 |
// } else if ( metaData instanceof |
// } else if ( metaData instanceof |
897 |
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
// Map<Integer,AttributeMetaData> ) { // LEIDER NICHT |
898 |
// KOMPILIERBAR!! |
// KOMPILIERBAR!! |
934 |
* Creates a {@link JPanel} that shows a legend for a list of |
* Creates a {@link JPanel} that shows a legend for a list of |
935 |
* {@link FeatureTypeStyle}s and a targeted featureType |
* {@link FeatureTypeStyle}s and a targeted featureType |
936 |
* |
* |
937 |
|
* @param style |
938 |
|
* The Style to presented in this legend |
939 |
* @param featureType |
* @param featureType |
940 |
* 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 |
|
941 |
* |
* |
942 |
* @author <a href="mailto:[email protected]">Stefan Alfons |
* @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a> |
|
* Krüger</a> |
|
943 |
*/ |
*/ |
944 |
public static JPanel createLegendPanel(Style style, |
public static JPanel createLegendSwingPanel(Style style, |
945 |
final SimpleFeatureType featureType, final int iconWidth, |
final SimpleFeatureType featureType, final int iconWidth, |
946 |
final int iconHeight) { |
final int iconHeight) { |
947 |
|
|
948 |
|
if (featureType == null) { |
949 |
|
ExceptionDialog.show(new IllegalStateException( |
950 |
|
"featureType is null!")); |
951 |
|
return new JPanel(); |
952 |
|
} |
953 |
|
|
954 |
final List<FeatureTypeStyle> list = style.featureTypeStyles(); |
final List<FeatureTypeStyle> list = style.featureTypeStyles(); |
955 |
|
|
956 |
final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]")); |
final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]")); |
1005 |
|
|
1006 |
/** |
/** |
1007 |
* Creates a {@link JComponent} that contains a legend for a given |
* Creates a {@link JComponent} that contains a legend for a given |
1008 |
* rasterLayer and a given {@link Style}. |
* {@link StyledRasterInterface} and a given {@link Style}. |
1009 |
* |
* |
1010 |
* @param style |
* @param style |
1011 |
* if <code>null</code>, the default {@link Style} is extracetd |
* if <code>null</code>, the default {@link Style} is extracetd |
1012 |
* from the {@link StyledRasterInterface} |
* from the {@link StyledRasterInterface} |
1013 |
*/ |
*/ |
1014 |
public static JPanel createLegendPanel( |
public static JPanel createLegendSwingPanel( |
1015 |
final StyledRasterInterface<?> styledRaster, Style style, |
final StyledRasterInterface<?> styledRaster, Style style, |
1016 |
final int iconWidth, final int iconHeight) { |
final int iconWidth, final int iconHeight) { |
1017 |
|
|
1097 |
final GridCoverage2D sampleCov = sampleRasters.get(rValue); |
final GridCoverage2D sampleCov = sampleRasters.get(rValue); |
1098 |
GridCoverageRenderer renderer; |
GridCoverageRenderer renderer; |
1099 |
try { |
try { |
1100 |
renderer = new GridCoverageRenderer(sampleCov |
renderer = new GridCoverageRenderer( |
1101 |
.getCoordinateReferenceSystem(), JTSUtil |
sampleCov.getCoordinateReferenceSystem(), |
1102 |
.createEnvelope(sampleCov.getEnvelope()), |
JTSUtil.createEnvelope(sampleCov.getEnvelope()), |
1103 |
new Rectangle(iconWidth, iconHeight), |
new Rectangle(iconWidth, iconHeight), |
1104 |
(AffineTransform) null); |
(AffineTransform) null); |
1105 |
} catch (final Exception e1) { |
} catch (final Exception e1) { |
1156 |
if (geoObject instanceof GridCoverage2D) { |
if (geoObject instanceof GridCoverage2D) { |
1157 |
final GridCoverage2D cov = (GridCoverage2D) geoObject; |
final GridCoverage2D cov = (GridCoverage2D) geoObject; |
1158 |
colorModel = cov.getRenderedImage().getColorModel(); |
colorModel = cov.getRenderedImage().getColorModel(); |
1159 |
} else if (styledGrid instanceof StyledRasterPyramidInterface) { |
} else if (styledGrid instanceof StyledGridCoverageReaderInterface) { |
1160 |
|
|
1161 |
final Parameter readGG = new Parameter( |
final Parameter readGG = new Parameter( |
1162 |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
AbstractGridFormat.READ_GRIDGEOMETRY2D); |
1163 |
|
|
1164 |
final ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope( |
final ReferencedEnvelope mapExtend = new ReferencedEnvelope( |
1165 |
styledGrid.getEnvelope(), styledGrid.getCrs()); |
styledGrid.getEnvelope(), styledGrid.getCrs()); |
1166 |
|
|
1167 |
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope( |
1168 |
new Rectangle(0, 0, 1, 1)), mapExtend)); |
new Rectangle(0, 0, 1, 1)), mapExtend)); |
1169 |
|
|
1170 |
final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject; |
AbstractGridCoverage2DReader aReader; |
1171 |
|
if (geoObject instanceof FeatureCollection) { |
1172 |
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject; |
1173 |
|
|
1174 |
|
aReader = (AbstractGridCoverage2DReader) FeatureUtil |
1175 |
|
.getWrappedGeoObject(rFc); |
1176 |
|
|
1177 |
|
} else if (geoObject instanceof AbstractGridCoverage2DReader) { |
1178 |
|
aReader = (AbstractGridCoverage2DReader) geoObject; |
1179 |
|
|
1180 |
|
} else |
1181 |
|
throw new RuntimeException("need a reader..."); |
1182 |
|
// |
1183 |
|
|
|
final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil |
|
|
.getWrappedGeoObject(rFc); |
|
1184 |
final GridCoverage2D cov = (GridCoverage2D) aReader |
final GridCoverage2D cov = (GridCoverage2D) aReader |
1185 |
.read(new GeneralParameterValue[] { readGG }); |
.read(new GeneralParameterValue[] { readGG }); |
1186 |
colorModel = cov.getRenderedImage().getColorModel(); |
colorModel = cov.getRenderedImage().getColorModel(); |
1210 |
return true; |
return true; |
1211 |
if (colorModel instanceof ComponentColorModel) |
if (colorModel instanceof ComponentColorModel) |
1212 |
return true; |
return true; |
1213 |
|
if (colorModel instanceof IndexColorModel) |
1214 |
|
return true; |
1215 |
|
|
1216 |
return false; |
return false; |
1217 |
} |
} |
1218 |
|
|
1286 |
AttributeDescriptor foundDescr = schema |
AttributeDescriptor foundDescr = schema |
1287 |
.getDescriptor(atm.getName()); |
.getDescriptor(atm.getName()); |
1288 |
if (foundDescr == null) { |
if (foundDescr == null) { |
1289 |
NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute(schema, |
NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute( |
1290 |
atm.getLocalName()); |
schema, atm.getLocalName()); |
1291 |
if (bestMatch == null) |
if (bestMatch == null) |
1292 |
willRemove.add(atm.getName()); |
willRemove.add(atm.getName()); |
1293 |
else |
else |
1310 |
if (ad instanceof GeometryDescriptor) |
if (ad instanceof GeometryDescriptor) |
1311 |
continue; |
continue; |
1312 |
if (!attributeMetaDataMap.containsKey(ad.getName())) { |
if (!attributeMetaDataMap.containsKey(ad.getName())) { |
1313 |
attributeMetaDataMap.put( new NameImpl(ad.getName().getNamespaceURI(), ad.getName().getLocalPart()), |
attributeMetaDataMap.put(new NameImpl(ad.getName() |
1314 |
|
.getNamespaceURI(), ad.getName().getLocalPart()), |
1315 |
new AttributeMetadataImpl(ad, schema |
new AttributeMetadataImpl(ad, schema |
1316 |
.getAttributeDescriptors().indexOf(ad), |
.getAttributeDescriptors().indexOf(ad), |
1317 |
attributeMetaDataMap.getLanguages())); |
attributeMetaDataMap.getLanguages())); |
1341 |
|
|
1342 |
/** |
/** |
1343 |
* @return a nicely formatted String containing all NODATA values of any |
* @return a nicely formatted String containing all NODATA values of any |
1344 |
* {@link AttributeMetadataInterface} object. Strings are quoted so that any |
* {@link AttributeMetadataInterface} object. Strings are quoted so |
1345 |
* empty {@link String} can be seen. |
* that any empty {@link String} can be seen. |
1346 |
*/ |
*/ |
1347 |
public static String formatNoDataValues(Set<Object> nodataValuesList) { |
public static String formatNoDataValues(Set<Object> nodataValuesList) { |
1348 |
String nicelyFormatted = ""; |
String nicelyFormatted = ""; |
1359 |
nicelyFormatted += ","; |
nicelyFormatted += ","; |
1360 |
} |
} |
1361 |
// Remove the extra comma |
// Remove the extra comma |
1362 |
nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted |
nicelyFormatted = nicelyFormatted.substring(0, |
1363 |
.length() - 1); |
nicelyFormatted.length() - 1); |
1364 |
} |
} |
1365 |
} |
} |
1366 |
return nicelyFormatted; |
return nicelyFormatted; |
1367 |
} |
} |
1368 |
|
|
1369 |
|
/** |
1370 |
|
* Creates a new {@link AttributeMetadataMap} with instances of |
1371 |
|
* {@link AttributeMetadataInterface} for every non-geometry attribute. |
1372 |
|
* Default NODATA values (like "" for String) are set. |
1373 |
|
*/ |
1374 |
|
public static AttributeMetadataMap<AttributeMetadataImpl> createDefaultAttributeMetadataMap( |
1375 |
|
SimpleFeatureType schema) { |
1376 |
|
AttributeMetadataImplMap attMap = new AttributeMetadataImplMap(); |
1377 |
|
|
1378 |
|
for (int i = 0; i < schema.getAttributeCount(); i++) { |
1379 |
|
AttributeDescriptor attDesc = schema.getDescriptor(i); |
1380 |
|
|
1381 |
|
if (Geometry.class.isAssignableFrom(attDesc.getType().getBinding())) { |
1382 |
|
// Ignore the Geometry column |
1383 |
|
continue; |
1384 |
|
} |
1385 |
|
|
1386 |
|
// TODO AttributeMetadataAS would be nicer, which would not work |
1387 |
|
// with Translations ;-) |
1388 |
|
AttributeMetadataImpl attMetaData = new AttributeMetadataImpl( |
1389 |
|
new NameImpl(attDesc.getName().getNamespaceURI(), attDesc |
1390 |
|
.getName().getLocalPart()), attMap.getLanguages()); |
1391 |
|
|
1392 |
|
if (String.class.isAssignableFrom(attDesc.getType().getBinding())) { |
1393 |
|
// For Strings we add the "" as NODATA values |
1394 |
|
attMetaData.addNodataValue(""); |
1395 |
|
} |
1396 |
|
|
1397 |
|
attMap.put(attDesc.getName(), attMetaData); |
1398 |
|
} |
1399 |
|
return attMap; |
1400 |
|
} |
1401 |
} |
} |