/[schmitzm]/trunk/src/skrueger/geotools/StyledLayerUtil.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/StyledLayerUtil.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 769 by alfonx, Sun Mar 21 11:02:34 2010 UTC revision 1215 by alfonx, Wed Nov 3 11:06:22 2010 UTC
# Line 25  Line 25 
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    
# Line 38  import java.awt.image.BufferedImage; Line 38  import java.awt.image.BufferedImage;
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;
# Line 92  import schmitzm.io.IOUtil; Line 93  import schmitzm.io.IOUtil;
93  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
94  import schmitzm.swing.JPanel;  import schmitzm.swing.JPanel;
95  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
 import skrueger.AttributeMetadata;  
96  import skrueger.AttributeMetadataImpl;  import skrueger.AttributeMetadataImpl;
97    import skrueger.AttributeMetadataInterface;
98  import skrueger.RasterLegendData;  import skrueger.RasterLegendData;
99  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
100    
# Line 254  public class StyledLayerUtil { Line 255  public class StyledLayerUtil {
255           *            TODO replace with           *            TODO replace with
256           *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}           *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}
257           */           */
258          public static AttributeMetadataMap<? extends AttributeMetadata > getVisibleAttributeMetaData(          public static AttributeMetadataMap<? extends AttributeMetadataInterface> getVisibleAttributeMetaData(
259                          final AttributeMetadataMap<? extends AttributeMetadata> amdMap,                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,
260                          final boolean visible) {                          final boolean visible) {
261    
262                  final AttributeMetadataMap<AttributeMetadata> filteredMap = (AttributeMetadataMap<AttributeMetadata>) amdMap.clone();                  final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap
263                  if (filteredMap.size() > 0 ) {                                  .clone();
264                    if (filteredMap.size() > 0) {
265                          filteredMap.clear(); // Just in case the close copies the contents                          filteredMap.clear(); // Just in case the close copies the contents
266                  }                  }
267                    
268                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadataInterface amd : amdMap.values())
269                          if (amd.isVisible() == visible)                          if (amd.isVisible() == visible)
270                                  filteredMap.put(amd.getName(), amd);                                  filteredMap.put(amd.getName(), amd);
271    
# Line 285  public class StyledLayerUtil { Line 287  public class StyledLayerUtil {
287           */           */
288          public static AttributeMetadataImpl parseAttributeMetaData(          public static AttributeMetadataImpl parseAttributeMetaData(
289                          final Element element) {                          final Element element) {
290                  final String namespace = String.valueOf(element                  final String namespace = element.getAttributeValue("namespace");
291                                  .getAttributeValue("namespace"));                  final String localname = element.getAttributeValue("localname");
292                  final String localname = String.valueOf(element                  final NameImpl aName = new NameImpl(namespace, localname);
                                 .getAttributeValue("localname"));  
                 final Name aName = new NameImpl(namespace, localname);  
293                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
294                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
295                  final String unit = element.getAttributeValue("unit");                  final String unit = element.getAttributeValue("unit");
# Line 358  public class StyledLayerUtil { Line 358  public class StyledLayerUtil {
358           *            classes. (SK, 3.2.2010)           *            classes. (SK, 3.2.2010)
359           */           */
360          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
361                          final AttributeMetadata amd) {                          final AttributeMetadataInterface amd) {
362                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);
363                  element.setAttribute("namespace", String.valueOf(amd.getName()                  element.setAttribute("namespace",
364                                  .getNamespaceURI()));                                  String.valueOf(amd.getName().getNamespaceURI()));
365                  element.setAttribute("localname", String.valueOf(amd.getLocalName()));                  element.setAttribute("localname", String.valueOf(amd.getLocalName()));
366                  element.setAttribute("visible", String.valueOf(amd.isVisible()));                  element.setAttribute("visible", String.valueOf(amd.isVisible()));
367                  element.setAttribute("unit", amd.getUnit());                  element.setAttribute("unit", amd.getUnit());
# Line 380  public class StyledLayerUtil { Line 380  public class StyledLayerUtil {
380           *            map of attribute meta data           *            map of attribute meta data
381           */           */
382          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
383                          final AttributeMetadataMap<? extends AttributeMetadata> amdMap) {                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap) {
384                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
385                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadataInterface amd : amdMap.values())
386                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
387                  return element;                  return element;
388          }          }
# Line 693  public class StyledLayerUtil { Line 693  public class StyledLayerUtil {
693           * @param styledObject           * @param styledObject
694           *            a styled object           *            a styled object
695           * @return {@code StyledLayerStyle<RasterLegendData>} for           * @return {@code StyledLayerStyle<RasterLegendData>} for
696           *         {@link StyledGridCoverageInterface} or {@code           *         {@link StyledGridCoverageInterface} or
697           *         StyledLayerStyle<Map<Integer,AttributeMetaData>>} for           *         {@code StyledLayerStyle<Map<Integer,AttributeMetaData>>} for
698           *         {@link StyledFeatureCollectionInterface}           *         {@link StyledFeatureCollectionInterface}
699           */           */
700          public static StyledLayerStyle<?> getStyledLayerStyle(          public static StyledLayerStyle<?> getStyledLayerStyle(
# Line 844  public class StyledLayerUtil { Line 844  public class StyledLayerUtil {
844    
845          /**          /**
846           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
847           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a {@code           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a
848           * .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
849           * present. A missing attribute meta-data file is tolerated.           * must be present. A missing attribute meta-data file is tolerated.
850           *           *
851           * @param geoObjectURL           * @param geoObjectURL
852           *            URL of the (already read) feature object           *            URL of the (already read) feature object
# Line 880  public class StyledLayerUtil { Line 880  public class StyledLayerUtil {
880                  // Store the SLD                  // Store the SLD
881                  final Style sldStyle = style.getGeoObjectStyle();                  final Style sldStyle = style.getGeoObjectStyle();
882                  if (sldStyle != null) {                  if (sldStyle != null) {
883                          StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File(                          StylingUtil.saveStyleToSld(sldStyle, IOUtil.changeFileExt(new File(
884                                          geoObjectURL.toURI()), sldExt));                                          geoObjectURL.toURI()), sldExt));
885                  }                  }
886    
# Line 888  public class StyledLayerUtil { Line 888  public class StyledLayerUtil {
888                  final T metaData = style.getMetaData();                  final T metaData = style.getMetaData();
889                  if (metaData != null) {                  if (metaData != null) {
890                          if (metaData instanceof RasterLegendData) {                          if (metaData instanceof RasterLegendData) {
891                                  saveRasterLegendData((RasterLegendData) metaData, IOUtil                                  saveRasterLegendData((RasterLegendData) metaData,
892                                                  .changeUrlExt(geoObjectURL, mdExt));                                                  IOUtil.changeUrlExt(geoObjectURL, mdExt));
893                                  // } else if ( metaData instanceof                                  // } else if ( metaData instanceof
894                                  // Map<Integer,AttributeMetaData> ) { // LEIDER NICHT                                  // Map<Integer,AttributeMetaData> ) { // LEIDER NICHT
895                                  // KOMPILIERBAR!!                                  // KOMPILIERBAR!!
# Line 931  public class StyledLayerUtil { Line 931  public class StyledLayerUtil {
931           * Creates a {@link JPanel} that shows a legend for a list of           * Creates a {@link JPanel} that shows a legend for a list of
932           * {@link FeatureTypeStyle}s and a targeted featureType           * {@link FeatureTypeStyle}s and a targeted featureType
933           *           *
934             * @param style
935             *            The Style to presented in this legend
936           * @param featureType           * @param featureType
937           *            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  
938           *           *
939           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
940           */           */
941          public static JPanel createLegendPanel(Style style,          public static JPanel createLegendSwingPanel(Style style,
942                          final SimpleFeatureType featureType, final int iconWidth,                          final SimpleFeatureType featureType, final int iconWidth,
943                          final int iconHeight) {                          final int iconHeight) {
944    
# Line 997  public class StyledLayerUtil { Line 996  public class StyledLayerUtil {
996    
997          /**          /**
998           * Creates a {@link JComponent} that contains a legend for a given           * Creates a {@link JComponent} that contains a legend for a given
999           * rasterLayer and a given {@link Style}.           * {@link StyledRasterInterface} and a given {@link Style}.
1000           *           *
1001           * @param style           * @param style
1002           *            if <code>null</code>, the default {@link Style} is extracetd           *            if <code>null</code>, the default {@link Style} is extracetd
1003           *            from the {@link StyledRasterInterface}           *            from the {@link StyledRasterInterface}
1004           */           */
1005          public static JPanel createLegendPanel(          public static JPanel createLegendSwingPanel(
1006                          final StyledRasterInterface<?> styledRaster, Style style,                          final StyledRasterInterface<?> styledRaster, Style style,
1007                          final int iconWidth, final int iconHeight) {                          final int iconWidth, final int iconHeight) {
1008    
# Line 1089  public class StyledLayerUtil { Line 1088  public class StyledLayerUtil {
1088                                  final GridCoverage2D sampleCov = sampleRasters.get(rValue);                                  final GridCoverage2D sampleCov = sampleRasters.get(rValue);
1089                                  GridCoverageRenderer renderer;                                  GridCoverageRenderer renderer;
1090                                  try {                                  try {
1091                                          renderer = new GridCoverageRenderer(sampleCov                                          renderer = new GridCoverageRenderer(
1092                                                          .getCoordinateReferenceSystem(), JTSUtil                                                          sampleCov.getCoordinateReferenceSystem(),
1093                                                          .createEnvelope(sampleCov.getEnvelope()),                                                          JTSUtil.createEnvelope(sampleCov.getEnvelope()),
1094                                                          new Rectangle(iconWidth, iconHeight),                                                          new Rectangle(iconWidth, iconHeight),
1095                                                          (AffineTransform) null);                                                          (AffineTransform) null);
1096                                  } catch (final Exception e1) {                                  } catch (final Exception e1) {
# Line 1148  public class StyledLayerUtil { Line 1147  public class StyledLayerUtil {
1147                          if (geoObject instanceof GridCoverage2D) {                          if (geoObject instanceof GridCoverage2D) {
1148                                  final GridCoverage2D cov = (GridCoverage2D) geoObject;                                  final GridCoverage2D cov = (GridCoverage2D) geoObject;
1149                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1150                          } else if (styledGrid instanceof StyledRasterPyramidInterface) {                          } else if (styledGrid instanceof StyledGridCoverageReaderInterface) {
1151    
1152                                  final Parameter readGG = new Parameter(                                  final Parameter readGG = new Parameter(
1153                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);
1154    
1155                                  final ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope(                                  final ReferencedEnvelope mapExtend = new ReferencedEnvelope(
1156                                                  styledGrid.getEnvelope(), styledGrid.getCrs());                                                  styledGrid.getEnvelope(), styledGrid.getCrs());
1157    
1158                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(
1159                                                  new Rectangle(0, 0, 1, 1)), mapExtend));                                                  new Rectangle(0, 0, 1, 1)), mapExtend));
1160    
1161                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;                                  AbstractGridCoverage2DReader aReader;
1162                                    if (geoObject instanceof FeatureCollection) {
1163                                            final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;
1164    
1165                                            aReader = (AbstractGridCoverage2DReader) FeatureUtil
1166                                                            .getWrappedGeoObject(rFc);
1167    
1168                                    } else if (geoObject instanceof AbstractGridCoverage2DReader) {
1169                                            aReader = (AbstractGridCoverage2DReader) geoObject;
1170    
1171                                    } else
1172                                            throw new RuntimeException("need a reader...");
1173                                    //
1174    
                                 final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil  
                                                 .getWrappedGeoObject(rFc);  
1175                                  final GridCoverage2D cov = (GridCoverage2D) aReader                                  final GridCoverage2D cov = (GridCoverage2D) aReader
1176                                                  .read(new GeneralParameterValue[] { readGG });                                                  .read(new GeneralParameterValue[] { readGG });
1177                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
# Line 1192  public class StyledLayerUtil { Line 1201  public class StyledLayerUtil {
1201                          return true;                          return true;
1202                  if (colorModel instanceof ComponentColorModel)                  if (colorModel instanceof ComponentColorModel)
1203                          return true;                          return true;
1204                    if (colorModel instanceof IndexColorModel)
1205                            return true;
1206    
1207                  return false;                  return false;
1208          }          }
1209    
# Line 1260  public class StyledLayerUtil { Line 1272  public class StyledLayerUtil {
1272                  // 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.
1273                  // maybe correct some upperCase/loweCase stuff                  // maybe correct some upperCase/loweCase stuff
1274    
1275                  for (AttributeMetadata atm : attributeMetaDataMap.values()) {                  for (AttributeMetadataInterface atm : attributeMetaDataMap.values()) {
1276    
1277                          AttributeDescriptor foundDescr = schema                          AttributeDescriptor foundDescr = schema
1278                                          .getDescriptor(atm.getName());                                          .getDescriptor(atm.getName());
1279                          if (foundDescr == null) {                          if (foundDescr == null) {
1280                                  Name bestMatch = FeatureUtil.findBestMatchingAttribute(schema,                                  NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute(
1281                                                  atm.getLocalName());                                                  schema, atm.getLocalName());
1282                                  if (bestMatch == null)                                  if (bestMatch == null)
1283                                          willRemove.add(atm.getName());                                          willRemove.add(atm.getName());
1284                                  else                                  else
# Line 1289  public class StyledLayerUtil { Line 1301  public class StyledLayerUtil {
1301                          if (ad instanceof GeometryDescriptor)                          if (ad instanceof GeometryDescriptor)
1302                                  continue;                                  continue;
1303                          if (!attributeMetaDataMap.containsKey(ad.getName())) {                          if (!attributeMetaDataMap.containsKey(ad.getName())) {
1304                                  attributeMetaDataMap.put(ad.getName(),                                  attributeMetaDataMap.put(new NameImpl(ad.getName()
1305                                                    .getNamespaceURI(), ad.getName().getLocalPart()),
1306                                                  new AttributeMetadataImpl(ad, schema                                                  new AttributeMetadataImpl(ad, schema
1307                                                                  .getAttributeDescriptors().indexOf(ad),                                                                  .getAttributeDescriptors().indexOf(ad),
1308                                                                  attributeMetaDataMap.getLanguages()));                                                                  attributeMetaDataMap.getLanguages()));
# Line 1306  public class StyledLayerUtil { Line 1319  public class StyledLayerUtil {
1319           * @param schema           * @param schema
1320           */           */
1321          public static void addEmptyStringToAllTextualAttributes(          public static void addEmptyStringToAllTextualAttributes(
1322                          AttributeMetadataMap<? extends AttributeMetadata> attributeMetaDataMap,                          AttributeMetadataMap<? extends AttributeMetadataInterface> attributeMetaDataMap,
1323                          SimpleFeatureType schema) {                          SimpleFeatureType schema) {
1324    
1325                  for (Name name : attributeMetaDataMap.keySet()) {                  for (Name name : attributeMetaDataMap.keySet()) {
# Line 1319  public class StyledLayerUtil { Line 1332  public class StyledLayerUtil {
1332    
1333          /**          /**
1334           * @return a nicely formatted String containing all NODATA values of any           * @return a nicely formatted String containing all NODATA values of any
1335           *         {@link AttributeMetadata} object. Strings are quoted so that any           *         {@link AttributeMetadataInterface} object. Strings are quoted so
1336           *         empty {@link String} can be seen.           *         that any empty {@link String} can be seen.
1337           */           */
1338          public static String formatNoDataValues(Set<Object> nodataValuesList) {          public static String formatNoDataValues(Set<Object> nodataValuesList) {
1339                  String nicelyFormatted = "";                  String nicelyFormatted = "";
# Line 1337  public class StyledLayerUtil { Line 1350  public class StyledLayerUtil {
1350                                          nicelyFormatted += ",";                                          nicelyFormatted += ",";
1351                                  }                                  }
1352                                  // Remove the extra comma                                  // Remove the extra comma
1353                                  nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted                                  nicelyFormatted = nicelyFormatted.substring(0,
1354                                                  .length() - 1);                                                  nicelyFormatted.length() - 1);
1355                          }                          }
1356                  }                  }
1357                  return nicelyFormatted;                  return nicelyFormatted;

Legend:
Removed from v.769  
changed lines
  Added in v.1215

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26