/[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/2.0-RC2/src/skrueger/geotools/StyledLayerUtil.java revision 658 by alfonx, Wed Feb 3 15:32:21 2010 UTC trunk/src/skrueger/geotools/StyledLayerUtil.java revision 1270 by alfonx, Sun Nov 14 00:25:52 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;  import java.util.ArrayList;
48    import java.util.Collections;
49  import java.util.List;  import java.util.List;
50  import java.util.Map;  import java.util.Map;
51    import java.util.Set;
52    
53  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
54  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
# Line 82  import org.opengis.feature.simple.Simple Line 85  import org.opengis.feature.simple.Simple
85  import org.opengis.feature.type.AttributeDescriptor;  import org.opengis.feature.type.AttributeDescriptor;
86  import org.opengis.feature.type.GeometryDescriptor;  import org.opengis.feature.type.GeometryDescriptor;
87  import org.opengis.feature.type.Name;  import org.opengis.feature.type.Name;
88    import org.opengis.filter.FilterFactory2;
89  import org.opengis.parameter.GeneralParameterValue;  import org.opengis.parameter.GeneralParameterValue;
90    
91    import schmitzm.geotools.FilterUtil;
92  import schmitzm.geotools.JTSUtil;  import schmitzm.geotools.JTSUtil;
93  import schmitzm.geotools.feature.FeatureUtil;  import schmitzm.geotools.feature.FeatureUtil;
94  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
95  import schmitzm.io.IOUtil;  import schmitzm.io.IOUtil;
96  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
97    import schmitzm.swing.ExceptionDialog;
98  import schmitzm.swing.JPanel;  import schmitzm.swing.JPanel;
99  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
100  import skrueger.AttributeMetadata;  import skrueger.AttributeMetadataImpl;
101    import skrueger.AttributeMetadataInterface;
102  import skrueger.RasterLegendData;  import skrueger.RasterLegendData;
103  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
104    
105    import com.vividsolutions.jts.geom.Geometry;
106    
107  /**  /**
108   * This class provides static helper methods for dealing with   * This class provides static helper methods for dealing with
109   * {@link StyledLayerInterface} stuff.   * {@link StyledLayerInterface} stuff.
# Line 109  public class StyledLayerUtil { Line 118  public class StyledLayerUtil {
118          private static final SAXBuilder SAX_BUILDER = new SAXBuilder();          private static final SAXBuilder SAX_BUILDER = new SAXBuilder();
119          private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter();          private static final XMLOutputter XML_OUTPUTTER = new XMLOutputter();
120    
121            /**
122             * Is appended to the name of a rule, this rule shall not be shown in the
123             * legend
124             */
125            public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND";
126    
127          /** URL for Atlas XML schema */          /** URL for Atlas XML schema */
128          public static final String AMLURI = "http://www.wikisquare.de/AtlasML";          public static final String AMLURI = "http://www.wikisquare.de/AtlasML";
129          /** Name of the XML Element for the attribute meta data map */          /** Name of the XML Element for the attribute meta data map */
# Line 121  public class StyledLayerUtil { Line 136  public class StyledLayerUtil {
136          public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem";          public static final String ELEM_NAME_RASTERLEGEND = "rasterLegendItem";
137          /** Name of the XML Element for a translation */          /** Name of the XML Element for a translation */
138          public static final String ELEM_NAME_TRANSLATION = "translation";          public static final String ELEM_NAME_TRANSLATION = "translation";
139            private static FilterFactory2 ff = FilterUtil.FILTER_FAC2;
140    
141          /**          /**
142           * Creates a Geotools {@link MapLayer} from an object. If the object is a           * Creates a Geotools {@link MapLayer} from an object. If the object is a
# Line 252  public class StyledLayerUtil { Line 268  public class StyledLayerUtil {
268           *            TODO replace with           *            TODO replace with
269           *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}           *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}
270           */           */
271          public static AttributeMetadataMap getVisibleAttributeMetaData(          public static AttributeMetadataMap<? extends AttributeMetadataInterface> getVisibleAttributeMetaData(
272                          final AttributeMetadataMap amdMap, final boolean visible) {                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,
273                            final boolean visible) {
274    
275                  final AttributeMetadataMap filteredMap = new AttributeMetadataMap(                  final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap
276                                  amdMap.getLanguages());                                  .clone();
277                  for (final AttributeMetadata amd : amdMap.values())                  if (filteredMap.size() > 0) {
278                            filteredMap.clear(); // Just in case the close copies the contents
279                    }
280    
281                    for (final AttributeMetadataInterface amd : amdMap.values())
282                          if (amd.isVisible() == visible)                          if (amd.isVisible() == visible)
283                                  filteredMap.put(amd.getName(), amd);                                  filteredMap.put(amd.getName(), amd);
284    
# Line 265  public class StyledLayerUtil { Line 286  public class StyledLayerUtil {
286          }          }
287    
288          /**          /**
289           * Parses a {@link AttributeMetadata} object from an JDOM-{@link Element}.           * Parses a {@link AttributeMetadataImpl} object from an JDOM-
290           * This method works like {@link           * {@link Element}. This method works like {@link
291           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.
292           *           *
293           * TODO 20.11.2009, SK: There are some new attribute weight, functiona,           * TODO 20.11.2009, SK: There are some new attribute weight, functiona,
# Line 277  public class StyledLayerUtil { Line 298  public class StyledLayerUtil {
298           * @param element           * @param element
299           *            {@link Element} to parse           *            {@link Element} to parse
300           */           */
301          public static AttributeMetadata parseAttributeMetaData(final Element element) {          public static AttributeMetadataImpl parseAttributeMetaData(
302                  final String namespace = String.valueOf(element                          final Element element) {
303                                  .getAttributeValue("namespace"));                  final String namespace = element.getAttributeValue("namespace");
304                  final String localname = String.valueOf(element                  final String localname = element.getAttributeValue("localname");
305                                  .getAttributeValue("localname"));                  final NameImpl aName = new NameImpl(namespace, localname);
                 final Name aName = new NameImpl(namespace, localname);  
306                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
307                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
308                  final String unit = element.getAttributeValue("unit");                  final String unit = element.getAttributeValue("unit");
# Line 298  public class StyledLayerUtil { Line 318  public class StyledLayerUtil {
318                          else if (childElement.getName().equals("desc"))                          else if (childElement.getName().equals("desc"))
319                                  desc = parseTranslation(childElement);                                  desc = parseTranslation(childElement);
320                  }                  }
321                  return new AttributeMetadata(aName, visible, name, desc, unit);                  return new AttributeMetadataImpl(aName, visible, name, desc, unit);
322          }          }
323    
324          /**          /**
325           * Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with           * Parses a {@link AttributeMetadataImpl} map from an JDOM-{@link Element}
326           * {@code <attribute>}-childs.           * with {@code <attribute>}-childs.
327           *           *
328           * @param element           * @param element
329           *            {@link Element} to parse           *            {@link Element} to parse
330           *                       *
331           *            TODO Since GP 1.3 the {@link AttributeMetadata} class has more           *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
332           *            attributes which are not used by Xulu/ISDSS. GP           *            more attributes which are not used by Xulu/ISDSS. GP
333           *            exports/imports the AMD via AMLExporter and AMLImporter           *            exports/imports the AMD via AMLExporter and AMLImporter
334           *            classes. (SK, 3.2.2010)    *                       *            classes. (SK, 3.2.2010) *
335           */           */
336          public static AttributeMetadataMap parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
337                          final Element element) {                          final Element element) {
338                  final AttributeMetadataMap metaData = new AttributeMetadataMap();                  final AttributeMetadataMap metaData = new AttributeMetadataImplMap();
339                  final List<Element> attributesElements = element                  final List<Element> attributesElements = element
340                                  .getChildren(ELEM_NAME_ATTRIBUTE);                                  .getChildren(ELEM_NAME_ATTRIBUTE);
341                  for (final Element attibuteElement : attributesElements) {                  for (final Element attibuteElement : attributesElements) {
342                          final AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);                          final AttributeMetadataImpl attrMetaData = parseAttributeMetaData(attibuteElement);
343                          metaData.put(attrMetaData.getName(), attrMetaData);                          metaData.put(attrMetaData.getName(), attrMetaData);
344                  }                  }
345                  return metaData;                  return metaData;
346          }          }
347    
348          /**          /**
349           * Loads a {@link AttributeMetadata} object from an URL.           * Loads a {@link AttributeMetadataImpl} object from an URL.
350           *           *
351           * @param documentUrl           * @param documentUrl
352           *            {@link URL} to parse           *            {@link URL} to parse
# Line 339  public class StyledLayerUtil { Line 359  public class StyledLayerUtil {
359          }          }
360    
361          /**          /**
362           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
363           * object.           * {@link AttributeMetadataImpl} object.
364           *           *
365           * @param amd           * @param amd
366           *            meta data for one attribute           *            meta data for one attribute
367           *           *
368           *            TODO Since GP 1.3 the {@link AttributeMetadata} class has more           *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
369           *            attributes which are not used by Xulu/ISDSS. GP           *            more attributes which are not used by Xulu/ISDSS. GP
370           *            exports/imports the AMD via AMLExporter and AMLImporter           *            exports/imports the AMD via AMLExporter and AMLImporter
371           *            classes. (SK, 3.2.2010)           *            classes. (SK, 3.2.2010)
372           */           */
373          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
374                          final AttributeMetadata amd) {                          final AttributeMetadataInterface amd) {
375                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);
376                  element.setAttribute("namespace", String.valueOf(amd.getName()                  element.setAttribute("namespace",
377                                  .getNamespaceURI()));                                  String.valueOf(amd.getName().getNamespaceURI()));
378                  element.setAttribute("localname", String.valueOf(amd.getLocalName()));                  element.setAttribute("localname", String.valueOf(amd.getLocalName()));
379                  element.setAttribute("visible", String.valueOf(amd.isVisible()));                  element.setAttribute("visible", String.valueOf(amd.isVisible()));
380                  element.setAttribute("unit", amd.getUnit());                  element.setAttribute("unit", amd.getUnit());
# Line 366  public class StyledLayerUtil { Line 386  public class StyledLayerUtil {
386          }          }
387    
388          /**          /**
389           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
390           * map.           * {@link AttributeMetadataImpl} map.
391           *           *
392           * @param amdMap           * @param amdMap
393           *            map of attribute meta data           *            map of attribute meta data
394           */           */
395          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
396                          final AttributeMetadataMap amdMap) {                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap) {
397                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
398                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadataInterface amd : amdMap.values())
399                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
400                  return element;                  return element;
401          }          }
402    
403          /**          /**
404           * Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL.           * Saves a {@link AttributeMetadataImpl AttributeMetaData-Map} to an URL.
405           *           *
406           * @param amdMap           * @param amdMap
407           *            map of {@link AttributeMetadata}           *            map of {@link AttributeMetadataImpl}
408           * @param documentUrl           * @param documentUrl
409           *            {@link URL} to store the XML           *            {@link URL} to store the XML
410           */           */
# Line 686  public class StyledLayerUtil { Line 706  public class StyledLayerUtil {
706           * @param styledObject           * @param styledObject
707           *            a styled object           *            a styled object
708           * @return {@code StyledLayerStyle<RasterLegendData>} for           * @return {@code StyledLayerStyle<RasterLegendData>} for
709           *         {@link StyledGridCoverageInterface} or {@code           *         {@link StyledGridCoverageInterface} or
710           *         StyledLayerStyle<Map<Integer,AttributeMetaData>>} for           *         {@code StyledLayerStyle<Map<Integer,AttributeMetaData>>} for
711           *         {@link StyledFeatureCollectionInterface}           *         {@link StyledFeatureCollectionInterface}
712           */           */
713          public static StyledLayerStyle<?> getStyledLayerStyle(          public static StyledLayerStyle<?> getStyledLayerStyle(
# Line 791  public class StyledLayerUtil { Line 811  public class StyledLayerUtil {
811          }          }
812    
813          /**          /**
814           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata           * Loads a {@linkplain Style SLD-Style} and a
815           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} for a given
816           * file must be present. A missing attribute meta-data file is tolerated.           * geo-object (feature) source. The SLD file must be present. A missing
817             * attribute meta-data file is tolerated.
818           *           *
819           * @param geoObjectURL           * @param geoObjectURL
820           *            URL of the (already read) feature object           *            URL of the (already read) feature object
# Line 836  public class StyledLayerUtil { Line 857  public class StyledLayerUtil {
857    
858          /**          /**
859           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
860           * {@linkplain AttributeMetadata AttributeMetaData-Map} from a {@code .amd}           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a
861           * 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
862           * present. A missing attribute meta-data file is tolerated.           * must be present. A missing attribute meta-data file is tolerated.
863           *           *
864           * @param geoObjectURL           * @param geoObjectURL
865           *            URL of the (already read) feature object           *            URL of the (already read) feature object
# Line 872  public class StyledLayerUtil { Line 893  public class StyledLayerUtil {
893                  // Store the SLD                  // Store the SLD
894                  final Style sldStyle = style.getGeoObjectStyle();                  final Style sldStyle = style.getGeoObjectStyle();
895                  if (sldStyle != null) {                  if (sldStyle != null) {
896                          StylingUtil.saveStyleToSLD(sldStyle, IOUtil.changeFileExt(new File(                          StylingUtil.saveStyleToSld(sldStyle, IOUtil.changeFileExt(new File(
897                                          geoObjectURL.toURI()), sldExt));                                          geoObjectURL.toURI()), sldExt));
898                  }                  }
899    
# Line 880  public class StyledLayerUtil { Line 901  public class StyledLayerUtil {
901                  final T metaData = style.getMetaData();                  final T metaData = style.getMetaData();
902                  if (metaData != null) {                  if (metaData != null) {
903                          if (metaData instanceof RasterLegendData) {                          if (metaData instanceof RasterLegendData) {
904                                  saveRasterLegendData((RasterLegendData) metaData, IOUtil                                  saveRasterLegendData((RasterLegendData) metaData,
905                                                  .changeUrlExt(geoObjectURL, mdExt));                                                  IOUtil.changeUrlExt(geoObjectURL, mdExt));
906                                  // } else if ( metaData instanceof                                  // } else if ( metaData instanceof
907                                  // Map<Integer,AttributeMetaData> ) { // LEIDER NICHT                                  // Map<Integer,AttributeMetaData> ) { // LEIDER NICHT
908                                  // KOMPILIERBAR!!                                  // KOMPILIERBAR!!
# Line 897  public class StyledLayerUtil { Line 918  public class StyledLayerUtil {
918    
919          /**          /**
920           * 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
921           * meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a           * meta data ({@link RasterLegendData} or {@link AttributeMetadataImpl}) to
922           * {@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.
923           *           *
924           * @param style           * @param style
925           *            style to save           *            style to save
# Line 917  public class StyledLayerUtil { Line 938  public class StyledLayerUtil {
938           * Creates a {@link JPanel} that shows a legend for a list of           * Creates a {@link JPanel} that shows a legend for a list of
939           * {@link FeatureTypeStyle}s and a targeted featureType           * {@link FeatureTypeStyle}s and a targeted featureType
940           *           *
941             * @param style
942             *            The Style to presented in this legend
943           * @param featureType           * @param featureType
944           *            If this a legend for Point, Polygon or Line?           *            If this a legend for Point, Polygon or Line? Or ANY or NONE?
          * @param list  
          *            The Styles to presented in this legend  
945           *           *
946           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
947           */           */
948          public static JPanel createLegendPanel(Style style,          public static JPanel createLegendSwingPanel(Style style,
949                          final SimpleFeatureType featureType, final int iconWidth,                          final SimpleFeatureType featureType, final int iconWidth,
950                          final int iconHeight) {                          final int iconHeight) {
951    
952                  final List<FeatureTypeStyle> list = style.featureTypeStyles();                  if (featureType == null) {
953                            ExceptionDialog.show(new IllegalStateException(
954                                            "featureType is null!"));
955                            return new JPanel();
956                    }
957    
958                  final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]"));                  final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]"));
   
959                  if (style == null) {                  if (style == null) {
960                          // No Style => no legend                          // No Style => no legend
961                          return panel;                          return panel;
962                  }                  }
963    
964                  for (final FeatureTypeStyle ftStyle : list) {                  // Reversing the list, because a point that is painted above a polygon
965                    // should be higher in the list.
966                    final List<FeatureTypeStyle> ftsList = style.featureTypeStyles();
967                    Collections.reverse(ftsList);
968                    for (final FeatureTypeStyle ftStyle : ftsList) {
969    
970                            // // Try to import the FeatureType into an AtlasStyler RuleList to
971                            // // determine whether this RL is actually disabled.
972                            // StylingUtil.sldToString(ftStyle).contains(RL_DISABLED_FILTER.toString())
973                            // return;
974    
975                          // One child-node for every rule                          // One child-node for every rule
976                          final List<Rule> rules = ftStyle.rules();                          final List<Rule> rules = ftStyle.rules();
977                          for (final Rule rule : rules) {                          for (final Rule rule : rules) {
978    
979                                    // Check if this RULE shall actually appear in the legend
980                                    if (rule.getName() != null
981                                                    && rule.getName().contains(HIDE_IN_LAYER_LEGEND_HINT))
982                                            continue;
983    
984                                  /**                                  /**
985                                   * Let's not create a hbox for Rules that only contain                                   * Let's not create a hbox for Rules that only contain
986                                   * TextSymbolizers                                   * TextSymbolizers
# Line 978  public class StyledLayerUtil { Line 1015  public class StyledLayerUtil {
1015    
1016          /**          /**
1017           * Creates a {@link JComponent} that contains a legend for a given           * Creates a {@link JComponent} that contains a legend for a given
1018           * rasterLayer and a given {@link Style}.           * {@link StyledRasterInterface} and a given {@link Style}.
1019           *           *
1020           * @param style           * @param style
1021           *            if <code>null</code>, the default {@link Style} is extracetd           *            if <code>null</code>, the default {@link Style} is extracetd
1022           *            from the {@link StyledRasterInterface}           *            from the {@link StyledRasterInterface}
1023           */           */
1024          public static JPanel createLegendPanel(          public static JPanel createLegendSwingPanel(
1025                          final StyledRasterInterface<?> styledRaster, Style style,                          final StyledRasterInterface<?> styledRaster, Style style,
1026                          final int iconWidth, final int iconHeight) {                          final int iconWidth, final int iconHeight) {
1027    
# Line 1070  public class StyledLayerUtil { Line 1107  public class StyledLayerUtil {
1107                                  final GridCoverage2D sampleCov = sampleRasters.get(rValue);                                  final GridCoverage2D sampleCov = sampleRasters.get(rValue);
1108                                  GridCoverageRenderer renderer;                                  GridCoverageRenderer renderer;
1109                                  try {                                  try {
1110                                          renderer = new GridCoverageRenderer(sampleCov                                          renderer = new GridCoverageRenderer(
1111                                                          .getCoordinateReferenceSystem(), JTSUtil                                                          sampleCov.getCoordinateReferenceSystem(),
1112                                                          .createEnvelope(sampleCov.getEnvelope()),                                                          JTSUtil.createEnvelope(sampleCov.getEnvelope()),
1113                                                          new Rectangle(iconWidth, iconHeight),                                                          new Rectangle(iconWidth, iconHeight),
1114                                                          (AffineTransform) null);                                                          (AffineTransform) null);
1115                                  } catch (final Exception e1) {                                  } catch (final Exception e1) {
# Line 1129  public class StyledLayerUtil { Line 1166  public class StyledLayerUtil {
1166                          if (geoObject instanceof GridCoverage2D) {                          if (geoObject instanceof GridCoverage2D) {
1167                                  final GridCoverage2D cov = (GridCoverage2D) geoObject;                                  final GridCoverage2D cov = (GridCoverage2D) geoObject;
1168                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1169                          } else if (styledGrid instanceof StyledRasterPyramidInterface) {                          } else if (styledGrid instanceof StyledGridCoverageReaderInterface) {
1170    
1171                                  final Parameter readGG = new Parameter(                                  final Parameter readGG = new Parameter(
1172                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);
1173    
1174                                  final ReferencedEnvelope mapExtend = new org.geotools.geometry.jts.ReferencedEnvelope(                                  final ReferencedEnvelope mapExtend = new ReferencedEnvelope(
1175                                                  styledGrid.getEnvelope(), styledGrid.getCrs());                                                  styledGrid.getEnvelope(), styledGrid.getCrs());
1176    
1177                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(
1178                                                  new Rectangle(0, 0, 1, 1)), mapExtend));                                                  new Rectangle(0, 0, 1, 1)), mapExtend));
1179    
1180                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;                                  AbstractGridCoverage2DReader aReader;
1181                                    if (geoObject instanceof FeatureCollection) {
1182                                            final FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;
1183    
1184                                            aReader = (AbstractGridCoverage2DReader) FeatureUtil
1185                                                            .getWrappedGeoObject(rFc);
1186    
1187                                    } else if (geoObject instanceof AbstractGridCoverage2DReader) {
1188                                            aReader = (AbstractGridCoverage2DReader) geoObject;
1189    
1190                                    } else
1191                                            throw new RuntimeException("need a reader...");
1192                                    //
1193    
1194                                  final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil                                  final GridCoverage2D cov = aReader
                                                 .getWrappedGeoObject(rFc);  
                                 final GridCoverage2D cov = (GridCoverage2D) aReader  
1195                                                  .read(new GeneralParameterValue[] { readGG });                                                  .read(new GeneralParameterValue[] { readGG });
1196                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1197                          }                          }
# Line 1173  public class StyledLayerUtil { Line 1220  public class StyledLayerUtil {
1220                          return true;                          return true;
1221                  if (colorModel instanceof ComponentColorModel)                  if (colorModel instanceof ComponentColorModel)
1222                          return true;                          return true;
1223                    if (colorModel instanceof IndexColorModel)
1224                            return true;
1225    
1226                  return false;                  return false;
1227          }          }
1228    
# Line 1229  public class StyledLayerUtil { Line 1279  public class StyledLayerUtil {
1279           * After loading an atlas, the AttribteMetaData contains whatever is written           * After loading an atlas, the AttribteMetaData contains whatever is written
1280           * in the XML. But the DBF may have changed!           * in the XML. But the DBF may have changed!
1281           */           */
1282          public static void checkAttribMetaData(AttributeMetadataMap attributeMetaDataMap,          public static void checkAttribMetaData(
1283                            AttributeMetadataMap<AttributeMetadataImpl> attributeMetaDataMap,
1284                          SimpleFeatureType schema) {                          SimpleFeatureType schema) {
1285    
1286                    if (schema == null)
1287                            throw new IllegalArgumentException("schmema may not be null!");
1288    
1289                  ArrayList<Name> willRemove = new ArrayList<Name>();                  ArrayList<Name> willRemove = new ArrayList<Name>();
1290    
1291                  // 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.
1292                  // maybe correct some upperCase/loweCase stuff                  // maybe correct some upperCase/loweCase stuff
1293    
1294                  for (AttributeMetadata atm : attributeMetaDataMap.values()) {                  for (AttributeMetadataInterface atm : attributeMetaDataMap.values()) {
1295    
1296                          AttributeDescriptor foundDescr = schema                          AttributeDescriptor foundDescr = schema
1297                                          .getDescriptor(atm.getName());                                          .getDescriptor(atm.getName());
1298                          if (foundDescr == null) {                          if (foundDescr == null) {
1299                                  Name bestMatch = FeatureUtil.findBestMatchingAttribute(schema,                                  NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute(
1300                                                  atm.getLocalName());                                                  schema, atm.getLocalName());
1301                                  if (bestMatch == null)                                  if (bestMatch == null)
1302                                          willRemove.add(atm.getName());                                          willRemove.add(atm.getName());
1303                                  else                                  else
# Line 1256  public class StyledLayerUtil { Line 1310  public class StyledLayerUtil {
1310    
1311                  // Remove the ones that were not findable in the schema                  // Remove the ones that were not findable in the schema
1312                  for (Name removeName : willRemove) {                  for (Name removeName : willRemove) {
1313                          if (attributeMetaDataMap.remove(removeName) == null){                          if (attributeMetaDataMap.remove(removeName) == null) {
1314                                  LOGGER.warn("removing the AMData didn't work");                                  LOGGER.warn("removing the AMData didn't work");
1315                          }                          }
1316                  }                  }
# Line 1266  public class StyledLayerUtil { Line 1320  public class StyledLayerUtil {
1320                          if (ad instanceof GeometryDescriptor)                          if (ad instanceof GeometryDescriptor)
1321                                  continue;                                  continue;
1322                          if (!attributeMetaDataMap.containsKey(ad.getName())) {                          if (!attributeMetaDataMap.containsKey(ad.getName())) {
1323                                  attributeMetaDataMap.put(ad.getName(), new AttributeMetadata(                                  attributeMetaDataMap.put(new NameImpl(ad.getName()
1324                                                  ad, schema.getAttributeDescriptors().indexOf(ad), attributeMetaDataMap                                                  .getNamespaceURI(), ad.getName().getLocalPart()),
1325                                                                  .getLanguages()));                                                  new AttributeMetadataImpl(ad, schema
1326                                                                    .getAttributeDescriptors().indexOf(ad),
1327                                                                    attributeMetaDataMap.getLanguages()));
1328                          }                          }
1329                  }                  }
1330            }
1331    
1332            /**
1333             * Checks every attribute name in the {@link AttributeMetadataMap} for its
1334             * binding type. It the type is textual, add the mrpty string as a NODATA
1335             * value.
1336             *
1337             * @param attributeMetaDataMap
1338             * @param schema
1339             */
1340            public static void addEmptyStringToAllTextualAttributes(
1341                            AttributeMetadataMap<? extends AttributeMetadataInterface> attributeMetaDataMap,
1342                            SimpleFeatureType schema) {
1343    
1344                    for (Name name : attributeMetaDataMap.keySet()) {
1345                            if (String.class.isAssignableFrom(schema.getDescriptor(name)
1346                                            .getType().getBinding())) {
1347                                    attributeMetaDataMap.get(name).getNodataValues().add("");
1348                            }
1349                    }
1350            }
1351    
1352            /**
1353             * @return a nicely formatted String containing all NODATA values of any
1354             *         {@link AttributeMetadataInterface} object. Strings are quoted so
1355             *         that any empty {@link String} can be seen.
1356             */
1357            public static String formatNoDataValues(Set<Object> nodataValuesList) {
1358                    String nicelyFormatted = "";
1359                    if (nodataValuesList != null) {
1360                            if (nodataValuesList.size() == 0)
1361                                    nicelyFormatted = "";
1362                            else {
1363                                    for (Object ndo : nodataValuesList) {
1364                                            if (ndo instanceof String)
1365                                                    nicelyFormatted += "\"" + ndo + "\"";
1366                                            else
1367                                                    nicelyFormatted += ndo.toString();
1368    
1369                                            nicelyFormatted += ",";
1370                                    }
1371                                    // Remove the extra comma
1372                                    nicelyFormatted = nicelyFormatted.substring(0,
1373                                                    nicelyFormatted.length() - 1);
1374                            }
1375                    }
1376                    return nicelyFormatted;
1377          }          }
1378    
1379            /**
1380             * Creates a new {@link AttributeMetadataMap} with instances of
1381             * {@link AttributeMetadataInterface} for every non-geometry attribute.
1382             * Default NODATA values (like "" for String) are set.
1383             */
1384            public static AttributeMetadataMap<AttributeMetadataImpl> createDefaultAttributeMetadataMap(
1385                            SimpleFeatureType schema) {
1386                    AttributeMetadataImplMap attMap = new AttributeMetadataImplMap();
1387    
1388                    for (int i = 0; i < schema.getAttributeCount(); i++) {
1389                            AttributeDescriptor attDesc = schema.getDescriptor(i);
1390    
1391                            if (Geometry.class.isAssignableFrom(attDesc.getType().getBinding())) {
1392                                    // Ignore the Geometry column
1393                                    continue;
1394                            }
1395    
1396                            // TODO AttributeMetadataAS would be nicer, which would not work
1397                            // with Translations ;-)
1398                            AttributeMetadataImpl attMetaData = new AttributeMetadataImpl(
1399                                            new NameImpl(attDesc.getName().getNamespaceURI(), attDesc
1400                                                            .getName().getLocalPart()), attMap.getLanguages());
1401    
1402                            if (String.class.isAssignableFrom(attDesc.getType().getBinding())) {
1403                                    // For Strings we add the "" as NODATA values
1404                                    attMetaData.addNodataValue("");
1405                            }
1406    
1407                            attMap.put(attDesc.getName(), attMetaData);
1408                    }
1409                    return attMap;
1410            }
1411  }  }

Legend:
Removed from v.658  
changed lines
  Added in v.1270

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26