/[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

branches/1.0-gt2-2.6/src/skrueger/geotools/StyledLayerUtil.java revision 517 by alfonx, Thu Nov 12 10:12:39 2009 UTC trunk/src/skrueger/geotools/StyledLayerUtil.java revision 1228 by alfonx, Wed Nov 3 20:44:16 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;
45  import java.net.URL;  import java.net.URL;
46  import java.text.DecimalFormat;  import java.text.DecimalFormat;
47    import java.util.ArrayList;
48  import java.util.List;  import java.util.List;
49  import java.util.Map;  import java.util.Map;
50    import java.util.Set;
51    
52  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
53  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
# Line 78  import org.jdom.input.SAXBuilder; Line 81  import org.jdom.input.SAXBuilder;
81  import org.jdom.output.XMLOutputter;  import org.jdom.output.XMLOutputter;
82  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
83  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
84    import org.opengis.feature.type.AttributeDescriptor;
85    import org.opengis.feature.type.GeometryDescriptor;
86  import org.opengis.feature.type.Name;  import org.opengis.feature.type.Name;
87  import org.opengis.parameter.GeneralParameterValue;  import org.opengis.parameter.GeneralParameterValue;
88    
# Line 86  import schmitzm.geotools.feature.Feature Line 91  import schmitzm.geotools.feature.Feature
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;
97  import skrueger.AttributeMetadata;  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.
# Line 245  public class StyledLayerUtil { Line 254  public class StyledLayerUtil {
254           * @param visible           * @param visible
255           *            indicated whether the visible or invisible entries are           *            indicated whether the visible or invisible entries are
256           *            returned           *            returned
257             *
258             *            TODO replace with
259             *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}
260           */           */
261          public static AttributeMetadataMap getVisibleAttributeMetaData(          public static AttributeMetadataMap<? extends AttributeMetadataInterface> getVisibleAttributeMetaData(
262                          final AttributeMetadataMap amdMap, final boolean visible) {                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,
263                            final boolean visible) {
264    
265                  final AttributeMetadataMap filteredMap = new AttributeMetadataMap();                  final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap
266                  for (final AttributeMetadata amd : amdMap.values())                                  .clone();
267                    if (filteredMap.size() > 0) {
268                            filteredMap.clear(); // Just in case the close copies the contents
269                    }
270    
271                    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);
274    
# Line 258  public class StyledLayerUtil { Line 276  public class StyledLayerUtil {
276          }          }
277    
278          /**          /**
279           * Parses a {@link AttributeMetadata} object from an JDOM-{@link Element}.           * Parses a {@link AttributeMetadataImpl} object from an JDOM-
280           * This method works like {@link           * {@link Element}. This method works like {@link
281           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.
282           *           *
283             * TODO 20.11.2009, SK: There are some new attribute weight, functiona,
284             * functionX and nodata in AttributeMetaData that should be parsed/exported
285             * too. but this method is only used by ISDSS, which is not supporting that
286             * stuff anyways.
287             *
288           * @param element           * @param element
289           *            {@link Element} to parse           *            {@link Element} to parse
290           */           */
291          public static AttributeMetadata parseAttributeMetaData(final Element element) {          public static AttributeMetadataImpl parseAttributeMetaData(
292                  final String namespace = String.valueOf(element                          final Element element) {
293                                  .getAttributeValue("namespace"));                  final String namespace = element.getAttributeValue("namespace");
294                  final String localname = String.valueOf(element                  final String localname = element.getAttributeValue("localname");
295                                  .getAttributeValue("localname"));                  final NameImpl aName = new NameImpl(namespace, localname);
                 final Name aName = new NameImpl(namespace, localname);  
296                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
297                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
298                  final String unit = element.getAttributeValue("unit");                  final String unit = element.getAttributeValue("unit");
# Line 286  public class StyledLayerUtil { Line 308  public class StyledLayerUtil {
308                          else if (childElement.getName().equals("desc"))                          else if (childElement.getName().equals("desc"))
309                                  desc = parseTranslation(childElement);                                  desc = parseTranslation(childElement);
310                  }                  }
311                  return new AttributeMetadata(aName, visible, name, desc, unit);                  return new AttributeMetadataImpl(aName, visible, name, desc, unit);
312          }          }
313    
314          /**          /**
315           * Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with           * Parses a {@link AttributeMetadataImpl} map from an JDOM-{@link Element}
316           * {@code <attribute>}-childs.           * with {@code <attribute>}-childs.
317           *           *
318           * @param element           * @param element
319           *            {@link Element} to parse           *            {@link Element} to parse
320             *
321             *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
322             *            more attributes which are not used by Xulu/ISDSS. GP
323             *            exports/imports the AMD via AMLExporter and AMLImporter
324             *            classes. (SK, 3.2.2010) *
325           */           */
326          public static AttributeMetadataMap parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
327                          final Element element) {                          final Element element) {
328                  final AttributeMetadataMap metaData = new AttributeMetadataMap();                  final AttributeMetadataMap metaData = new AttributeMetadataImplMap();
329                  final List<Element> attributesElements = element                  final List<Element> attributesElements = element
330                                  .getChildren(ELEM_NAME_ATTRIBUTE);                                  .getChildren(ELEM_NAME_ATTRIBUTE);
331                  for (final Element attibuteElement : attributesElements) {                  for (final Element attibuteElement : attributesElements) {
332                          final AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);                          final AttributeMetadataImpl attrMetaData = parseAttributeMetaData(attibuteElement);
333                          metaData.put(attrMetaData.getName(), attrMetaData);                          metaData.put(attrMetaData.getName(), attrMetaData);
334                  }                  }
335                  return metaData;                  return metaData;
336          }          }
337    
338          /**          /**
339           * Loads a {@link AttributeMetadata} object from an URL.           * Loads a {@link AttributeMetadataImpl} object from an URL.
340           *           *
341           * @param documentUrl           * @param documentUrl
342           *            {@link URL} to parse           *            {@link URL} to parse
# Line 322  public class StyledLayerUtil { Line 349  public class StyledLayerUtil {
349          }          }
350    
351          /**          /**
352           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
353           * object.           * {@link AttributeMetadataImpl} object.
354           *           *
355           * @param amd           * @param amd
356           *            meta data for one attribute           *            meta data for one attribute
357             *
358             *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
359             *            more attributes which are not used by Xulu/ISDSS. GP
360             *            exports/imports the AMD via AMLExporter and AMLImporter
361             *            classes. (SK, 3.2.2010)
362           */           */
363          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
364                          final AttributeMetadata 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());
# Line 344  public class StyledLayerUtil { Line 376  public class StyledLayerUtil {
376          }          }
377    
378          /**          /**
379           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
380           * map.           * {@link AttributeMetadataImpl} map.
381           *           *
382           * @param amdMap           * @param amdMap
383           *            map of attribute meta data           *            map of attribute meta data
384           */           */
385          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
386                          final AttributeMetadataMap amdMap) {                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap) {
387                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
388                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadataInterface amd : amdMap.values())
389                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
390                  return element;                  return element;
391          }          }
392    
393          /**          /**
394           * Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL.           * Saves a {@link AttributeMetadataImpl AttributeMetaData-Map} to an URL.
395           *           *
396           * @param amdMap           * @param amdMap
397           *            map of {@link AttributeMetadata}           *            map of {@link AttributeMetadataImpl}
398           * @param documentUrl           * @param documentUrl
399           *            {@link URL} to store the XML           *            {@link URL} to store the XML
400           */           */
# Line 664  public class StyledLayerUtil { Line 696  public class StyledLayerUtil {
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(
# Line 769  public class StyledLayerUtil { Line 801  public class StyledLayerUtil {
801          }          }
802    
803          /**          /**
804           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata           * Loads a {@linkplain Style SLD-Style} and a
805           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} for a given
806           * file must be present. A missing attribute meta-data file is tolerated.           * geo-object (feature) source. The SLD file must be present. A missing
807             * attribute meta-data file is tolerated.
808           *           *
809           * @param geoObjectURL           * @param geoObjectURL
810           *            URL of the (already read) feature object           *            URL of the (already read) feature object
# Line 814  public class StyledLayerUtil { Line 847  public class StyledLayerUtil {
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 AttributeMetadata AttributeMetaData-Map} from a {@code .amd}           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a
851           * 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
# Line 850  public class StyledLayerUtil { Line 883  public class StyledLayerUtil {
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    
# Line 858  public class StyledLayerUtil { Line 891  public class StyledLayerUtil {
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!!
# Line 875  public class StyledLayerUtil { Line 908  public class StyledLayerUtil {
908    
909          /**          /**
910           * 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
911           * meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a           * meta data ({@link RasterLegendData} or {@link AttributeMetadataImpl}) to
912           * {@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.
913           *           *
914           * @param style           * @param style
915           *            style to save           *            style to save
# Line 892  public class StyledLayerUtil { Line 925  public class StyledLayerUtil {
925          }          }
926    
927          /**          /**
928             * *If appended to the name of a rule, this rule shall not be shown in the
929             * legend
930             */
931            public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND";
932    
933            /**
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&uuml;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:[]"));
# Line 922  public class StyledLayerUtil { Line 966  public class StyledLayerUtil {
966                          final List<Rule> rules = ftStyle.rules();                          final List<Rule> rules = ftStyle.rules();
967                          for (final Rule rule : rules) {                          for (final Rule rule : rules) {
968    
969                                    // Check if this RULE shall actually appear in the legend
970                                    if (rule.getName() != null
971                                                    && rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT))
972                                            continue;
973    
974                                  /**                                  /**
975                                   * Let's not create a hbox for Rules that only contain                                   * Let's not create a hbox for Rules that only contain
976                                   * TextSymbolizers                                   * TextSymbolizers
# Line 956  public class StyledLayerUtil { Line 1005  public class StyledLayerUtil {
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    
# Line 986  public class StyledLayerUtil { Line 1035  public class StyledLayerUtil {
1035                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
1036                                  .createSampleRasters();                                  .createSampleRasters();
1037    
1038                  final JPanel panel = new JPanel(new MigLayout("wrap 2"));                  final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0"));
1039    
1040                  for (final Double rValue : legendRasterValues) {                  for (final Double rValue : legendRasterValues) {
1041    
# Line 1048  public class StyledLayerUtil { Line 1097  public class StyledLayerUtil {
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) {
# Line 1075  public class StyledLayerUtil { Line 1124  public class StyledLayerUtil {
1124                          }                          }
1125    
1126                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));
                         // hbox.setAlignmentX(0f);  
1127                          panel.add(iconLabel, "sgx1");                          panel.add(iconLabel, "sgx1");
                         // hbox.add(Box.createHorizontalStrut(3));  
1128    
1129                          final Translation labelT = rasterLegendData.get(rValue);                          final Translation labelT = rasterLegendData.get(rValue);
1130                          final JLabel classTitleLabel = new JLabel(labelT.toString());                          final JLabel classTitleLabel = new JLabel(labelT.toString());
1131                          panel.add(classTitleLabel, "sgx2"                          panel.add(classTitleLabel, "sgx2"
1132                                          + (rasterLegendData.getPaintGaps() ? ", gapy 0 3" : ""));                                          + (rasterLegendData.isPaintGaps() ? ", gapy 0:0:0 5:5:5"
1133                                                            : ""));
1134                          classTitleLabel.setLabelFor(iconLabel);                          classTitleLabel.setLabelFor(iconLabel);
1135    
1136                          // box.add(hbox);                          if (rasterLegendData.isPaintGaps()) {
   
                         if (rasterLegendData.getPaintGaps()) {  
1137                                  iconLabel                                  iconLabel
1138                                                  .setBorder(BorderFactory.createLineBorder(Color.black));                                                  .setBorder(BorderFactory.createLineBorder(Color.black));
1139                          }                          }
# Line 1110  public class StyledLayerUtil { Line 1156  public class StyledLayerUtil {
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();
# Line 1146  public class StyledLayerUtil { Line 1202  public class StyledLayerUtil {
1202                          final StyledRasterInterface<?> styledRaster) {                          final StyledRasterInterface<?> styledRaster) {
1203                  final ColorModel colorModel = getColorModel(styledRaster);                  final ColorModel colorModel = getColorModel(styledRaster);
1204    
1205                  LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "                  // LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
1206                                  + colorModel.getClass().getSimpleName());                  // + colorModel != null ? colorModel.getClass().getSimpleName() :
1207                    // "NULL");
1208    
1209                  if (colorModel == null)                  if (colorModel == null)
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    
# Line 1163  public class StyledLayerUtil { Line 1223  public class StyledLayerUtil {
1223           * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a           * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a
1224           * repaint, so only use it when needed.           * repaint, so only use it when needed.
1225           *           *
1226           * @return <code>true</code> if the {@link MapLayer}'s {@link Style} has been changed.           * @return <code>true</code> if the {@link MapLayer}'s {@link Style} has
1227             *         been changed.
1228           */           */
1229          public static boolean updateMapLayerStyleIfChangedAndKeepSelection(MapLayer mapLayer,          public static boolean updateMapLayerStyleIfChangedAndKeepSelection(
1230                          Style style2) {                          MapLayer mapLayer, Style style2) {
1231    
1232                  Style mapLayerStyleCleaned = StylingUtil                  Style mapLayerStyleCleaned = StylingUtil
1233                                                  .removeSelectionFeatureTypeStyle(mapLayer.getStyle());                                  .removeSelectionFeatureTypeStyle(mapLayer.getStyle());
1234                    
1235                  Style newStyleCleaned = StylingUtil.removeSelectionFeatureTypeStyle(style2);                  Style newStyleCleaned = StylingUtil
1236                                                    .removeSelectionFeatureTypeStyle(style2);
1237                  if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned,  
1238                                  newStyleCleaned)) {                  if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned, newStyleCleaned)) {
1239                            
1240                          // They are different when compared without SELECTION FTS!                          // They are different when compared without SELECTION FTS!
1241                            
1242                          // Now let's copy any SELECTION FTS to the now style                          // Now let's copy any SELECTION FTS to the now style
1243                          FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil.getSelectionFeatureTypeStyle( mapLayer.getStyle() );                          FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil
1244                                            .getSelectionFeatureTypeStyle(mapLayer.getStyle());
1245                          if (selectionFeatureTypeStyle != null) {                          if (selectionFeatureTypeStyle != null) {
1246                                  newStyleCleaned.featureTypeStyles().add(selectionFeatureTypeStyle);                                  newStyleCleaned.featureTypeStyles().add(
1247                                  // newStyleCleaned is not so clean anymore... We just alled a selcetion FTS                                                  selectionFeatureTypeStyle);
1248                          }                                    // newStyleCleaned is not so clean anymore... We just alled a
1249                                                            // selcetion FTS
1250                            }
1251    
1252                          mapLayer.setStyle(newStyleCleaned);                          mapLayer.setStyle(newStyleCleaned);
1253                            
1254                          return true;                          return true;
1255                            
1256                  } else return false;                  } else {
1257                            return false;
1258                    }
1259          }          }
1260    
1261            /**
1262             * After loading an atlas, the AttribteMetaData contains whatever is written
1263             * in the XML. But the DBF may have changed! This method checks an
1264             * {@link AttributeMetadataMap} against a schema and also corrects
1265             * upperCase/lowerCase problems. It will also remove any geometry column
1266             * attribute metadata.
1267             */
1268            /**
1269             * After loading an atlas, the AttribteMetaData contains whatever is written
1270             * in the XML. But the DBF may have changed!
1271             */
1272            public static void checkAttribMetaData(
1273                            AttributeMetadataMap<AttributeMetadataImpl> attributeMetaDataMap,
1274                            SimpleFeatureType schema) {
1275    
1276                    if (schema == null)
1277                            throw new IllegalArgumentException("schmema may not be null!");
1278    
1279                    ArrayList<Name> willRemove = new ArrayList<Name>();
1280    
1281                    // 1. Check.. all attributes in the atm should be in the schema as well.
1282                    // maybe correct some upperCase/loweCase stuff
1283    
1284                    for (AttributeMetadataInterface atm : attributeMetaDataMap.values()) {
1285    
1286                            AttributeDescriptor foundDescr = schema
1287                                            .getDescriptor(atm.getName());
1288                            if (foundDescr == null) {
1289                                    NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute(
1290                                                    schema, atm.getLocalName());
1291                                    if (bestMatch == null)
1292                                            willRemove.add(atm.getName());
1293                                    else
1294                                            atm.setName(bestMatch);
1295                            } else if (foundDescr instanceof GeometryDescriptor) {
1296                                    // We don't want GeometryColumns in here
1297                                    willRemove.add(atm.getName());
1298                            }
1299                    }
1300    
1301                    // Remove the ones that were not findable in the schema
1302                    for (Name removeName : willRemove) {
1303                            if (attributeMetaDataMap.remove(removeName) == null) {
1304                                    LOGGER.warn("removing the AMData didn't work");
1305                            }
1306                    }
1307    
1308                    // 2. check... all attributes from the schema must have an ATM
1309                    for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
1310                            if (ad instanceof GeometryDescriptor)
1311                                    continue;
1312                            if (!attributeMetaDataMap.containsKey(ad.getName())) {
1313                                    attributeMetaDataMap.put(new NameImpl(ad.getName()
1314                                                    .getNamespaceURI(), ad.getName().getLocalPart()),
1315                                                    new AttributeMetadataImpl(ad, schema
1316                                                                    .getAttributeDescriptors().indexOf(ad),
1317                                                                    attributeMetaDataMap.getLanguages()));
1318                            }
1319                    }
1320            }
1321    
1322            /**
1323             * Checks every attribute name in the {@link AttributeMetadataMap} for its
1324             * binding type. It the type is textual, add the mrpty string as a NODATA
1325             * value.
1326             *
1327             * @param attributeMetaDataMap
1328             * @param schema
1329             */
1330            public static void addEmptyStringToAllTextualAttributes(
1331                            AttributeMetadataMap<? extends AttributeMetadataInterface> attributeMetaDataMap,
1332                            SimpleFeatureType schema) {
1333    
1334                    for (Name name : attributeMetaDataMap.keySet()) {
1335                            if (String.class.isAssignableFrom(schema.getDescriptor(name)
1336                                            .getType().getBinding())) {
1337                                    attributeMetaDataMap.get(name).getNodataValues().add("");
1338                            }
1339                    }
1340            }
1341    
1342            /**
1343             * @return a nicely formatted String containing all NODATA values of any
1344             *         {@link AttributeMetadataInterface} object. Strings are quoted so
1345             *         that any empty {@link String} can be seen.
1346             */
1347            public static String formatNoDataValues(Set<Object> nodataValuesList) {
1348                    String nicelyFormatted = "";
1349                    if (nodataValuesList != null) {
1350                            if (nodataValuesList.size() == 0)
1351                                    nicelyFormatted = "";
1352                            else {
1353                                    for (Object ndo : nodataValuesList) {
1354                                            if (ndo instanceof String)
1355                                                    nicelyFormatted += "\"" + ndo + "\"";
1356                                            else
1357                                                    nicelyFormatted += ndo.toString();
1358    
1359                                            nicelyFormatted += ",";
1360                                    }
1361                                    // Remove the extra comma
1362                                    nicelyFormatted = nicelyFormatted.substring(0,
1363                                                    nicelyFormatted.length() - 1);
1364                            }
1365                    }
1366                    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  }  }

Legend:
Removed from v.517  
changed lines
  Added in v.1228

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26