/[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 788 by alfonx, Tue Apr 6 12:18:52 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 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 AttributeMetadataInterface > getVisibleAttributeMetaData(          public static AttributeMetadataMap<? extends AttributeMetadataInterface> getVisibleAttributeMetaData(
259                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,
260                          final boolean visible) {                          final boolean visible) {
261    
262                  final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) 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 AttributeMetadataInterface 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);
# 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");
                 final String localname = String.valueOf(element  
                                 .getAttributeValue("localname"));  
292                  final NameImpl aName = new NameImpl(namespace, localname);                  final NameImpl aName = new NameImpl(namespace, localname);
293                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
294                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
# Line 360  public class StyledLayerUtil { Line 360  public class StyledLayerUtil {
360          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
361                          final AttributeMetadataInterface 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 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 1265  public class StyledLayerUtil { Line 1277  public class StyledLayerUtil {
1277                          AttributeDescriptor foundDescr = schema                          AttributeDescriptor foundDescr = schema
1278                                          .getDescriptor(atm.getName());                                          .getDescriptor(atm.getName());
1279                          if (foundDescr == null) {                          if (foundDescr == null) {
1280                                  NameImpl 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( new NameImpl(ad.getName().getNamespaceURI(), ad.getName().getLocalPart()),                                  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 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 AttributeMetadataInterface} 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.788  
changed lines
  Added in v.1215

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26