/[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 516 by alfonx, Wed Nov 11 21:17:33 2009 UTC trunk/src/skrueger/geotools/StyledLayerUtil.java revision 1203 by alfonx, Tue Nov 2 22:53:55 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 43  import java.io.FileNotFoundException; Line 43  import java.io.FileNotFoundException;
43  import java.io.FileWriter;  import java.io.FileWriter;
44  import java.net.URL;  import java.net.URL;
45  import java.text.DecimalFormat;  import java.text.DecimalFormat;
46    import java.util.ArrayList;
47  import java.util.List;  import java.util.List;
48  import java.util.Map;  import java.util.Map;
49    import java.util.Set;
50    
51  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
52  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
# Line 78  import org.jdom.input.SAXBuilder; Line 80  import org.jdom.input.SAXBuilder;
80  import org.jdom.output.XMLOutputter;  import org.jdom.output.XMLOutputter;
81  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
82  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
83    import org.opengis.feature.type.AttributeDescriptor;
84    import org.opengis.feature.type.GeometryDescriptor;
85  import org.opengis.feature.type.Name;  import org.opengis.feature.type.Name;
86  import org.opengis.parameter.GeneralParameterValue;  import org.opengis.parameter.GeneralParameterValue;
87    
# Line 88  import schmitzm.io.IOUtil; Line 92  import schmitzm.io.IOUtil;
92  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
93  import schmitzm.swing.JPanel;  import schmitzm.swing.JPanel;
94  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
95  import skrueger.AttributeMetadata;  import skrueger.AttributeMetadataImpl;
96    import skrueger.AttributeMetadataInterface;
97  import skrueger.RasterLegendData;  import skrueger.RasterLegendData;
98  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
99    
# Line 245  public class StyledLayerUtil { Line 250  public class StyledLayerUtil {
250           * @param visible           * @param visible
251           *            indicated whether the visible or invisible entries are           *            indicated whether the visible or invisible entries are
252           *            returned           *            returned
253             *
254             *            TODO replace with
255             *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}
256           */           */
257          public static AttributeMetadataMap getVisibleAttributeMetaData(          public static AttributeMetadataMap<? extends AttributeMetadataInterface > getVisibleAttributeMetaData(
258                          final AttributeMetadataMap amdMap, final boolean visible) {                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,
259                            final boolean visible) {
260                  final AttributeMetadataMap filteredMap = new AttributeMetadataMap();  
261                  for (final AttributeMetadata amd : amdMap.values())                  final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap.clone();
262                    if (filteredMap.size() > 0 ) {
263                            filteredMap.clear(); // Just in case the close copies the contents
264                    }
265                    
266                    for (final AttributeMetadataInterface amd : amdMap.values())
267                          if (amd.isVisible() == visible)                          if (amd.isVisible() == visible)
268                                  filteredMap.put(amd.getName(), amd);                                  filteredMap.put(amd.getName(), amd);
269    
# Line 258  public class StyledLayerUtil { Line 271  public class StyledLayerUtil {
271          }          }
272    
273          /**          /**
274           * Parses a {@link AttributeMetadata} object from an JDOM-{@link Element}.           * Parses a {@link AttributeMetadataImpl} object from an JDOM-
275           * This method works like {@link           * {@link Element}. This method works like {@link
276           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.
277           *           *
278             * TODO 20.11.2009, SK: There are some new attribute weight, functiona,
279             * functionX and nodata in AttributeMetaData that should be parsed/exported
280             * too. but this method is only used by ISDSS, which is not supporting that
281             * stuff anyways.
282             *
283           * @param element           * @param element
284           *            {@link Element} to parse           *            {@link Element} to parse
285           */           */
286          public static AttributeMetadata parseAttributeMetaData(final Element element) {          public static AttributeMetadataImpl parseAttributeMetaData(
287                  final String namespace = String.valueOf(element                          final Element element) {
288                                  .getAttributeValue("namespace"));                  final String namespace = element.getAttributeValue("namespace");
289                  final String localname = String.valueOf(element                  final String localname = element.getAttributeValue("localname");
290                                  .getAttributeValue("localname"));                  final NameImpl aName = new NameImpl(namespace, localname);
                 final Name aName = new NameImpl(namespace, localname);  
291                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
292                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
293                  final String unit = element.getAttributeValue("unit");                  final String unit = element.getAttributeValue("unit");
# Line 286  public class StyledLayerUtil { Line 303  public class StyledLayerUtil {
303                          else if (childElement.getName().equals("desc"))                          else if (childElement.getName().equals("desc"))
304                                  desc = parseTranslation(childElement);                                  desc = parseTranslation(childElement);
305                  }                  }
306                  return new AttributeMetadata(aName, visible, name, desc, unit);                  return new AttributeMetadataImpl(aName, visible, name, desc, unit);
307          }          }
308    
309          /**          /**
310           * Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with           * Parses a {@link AttributeMetadataImpl} map from an JDOM-{@link Element}
311           * {@code <attribute>}-childs.           * with {@code <attribute>}-childs.
312           *           *
313           * @param element           * @param element
314           *            {@link Element} to parse           *            {@link Element} to parse
315             *
316             *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
317             *            more attributes which are not used by Xulu/ISDSS. GP
318             *            exports/imports the AMD via AMLExporter and AMLImporter
319             *            classes. (SK, 3.2.2010) *
320           */           */
321          public static AttributeMetadataMap parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
322                          final Element element) {                          final Element element) {
323                  final AttributeMetadataMap metaData = new AttributeMetadataMap();                  final AttributeMetadataMap metaData = new AttributeMetadataImplMap();
324                  final List<Element> attributesElements = element                  final List<Element> attributesElements = element
325                                  .getChildren(ELEM_NAME_ATTRIBUTE);                                  .getChildren(ELEM_NAME_ATTRIBUTE);
326                  for (final Element attibuteElement : attributesElements) {                  for (final Element attibuteElement : attributesElements) {
327                          final AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);                          final AttributeMetadataImpl attrMetaData = parseAttributeMetaData(attibuteElement);
328                          metaData.put(attrMetaData.getName(), attrMetaData);                          metaData.put(attrMetaData.getName(), attrMetaData);
329                  }                  }
330                  return metaData;                  return metaData;
331          }          }
332    
333          /**          /**
334           * Loads a {@link AttributeMetadata} object from an URL.           * Loads a {@link AttributeMetadataImpl} object from an URL.
335           *           *
336           * @param documentUrl           * @param documentUrl
337           *            {@link URL} to parse           *            {@link URL} to parse
# Line 322  public class StyledLayerUtil { Line 344  public class StyledLayerUtil {
344          }          }
345    
346          /**          /**
347           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
348           * object.           * {@link AttributeMetadataImpl} object.
349           *           *
350           * @param amd           * @param amd
351           *            meta data for one attribute           *            meta data for one attribute
352             *
353             *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
354             *            more attributes which are not used by Xulu/ISDSS. GP
355             *            exports/imports the AMD via AMLExporter and AMLImporter
356             *            classes. (SK, 3.2.2010)
357           */           */
358          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
359                          final AttributeMetadata amd) {                          final AttributeMetadataInterface amd) {
360                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);
361                  element.setAttribute("namespace", String.valueOf(amd.getName()                  element.setAttribute("namespace", String.valueOf(amd.getName()
362                                  .getNamespaceURI()));                                  .getNamespaceURI()));
# Line 344  public class StyledLayerUtil { Line 371  public class StyledLayerUtil {
371          }          }
372    
373          /**          /**
374           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
375           * map.           * {@link AttributeMetadataImpl} map.
376           *           *
377           * @param amdMap           * @param amdMap
378           *            map of attribute meta data           *            map of attribute meta data
379           */           */
380          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
381                          final AttributeMetadataMap amdMap) {                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap) {
382                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
383                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadataInterface amd : amdMap.values())
384                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
385                  return element;                  return element;
386          }          }
387    
388          /**          /**
389           * Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL.           * Saves a {@link AttributeMetadataImpl AttributeMetaData-Map} to an URL.
390           *           *
391           * @param amdMap           * @param amdMap
392           *            map of {@link AttributeMetadata}           *            map of {@link AttributeMetadataImpl}
393           * @param documentUrl           * @param documentUrl
394           *            {@link URL} to store the XML           *            {@link URL} to store the XML
395           */           */
# Line 769  public class StyledLayerUtil { Line 796  public class StyledLayerUtil {
796          }          }
797    
798          /**          /**
799           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata           * Loads a {@linkplain Style SLD-Style} and a
800           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} for a given
801           * file must be present. A missing attribute meta-data file is tolerated.           * geo-object (feature) source. The SLD file must be present. A missing
802             * attribute meta-data file is tolerated.
803           *           *
804           * @param geoObjectURL           * @param geoObjectURL
805           *            URL of the (already read) feature object           *            URL of the (already read) feature object
# Line 814  public class StyledLayerUtil { Line 842  public class StyledLayerUtil {
842    
843          /**          /**
844           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
845           * {@linkplain AttributeMetadata AttributeMetaData-Map} from a {@code .amd}           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a {@code
846           * file for a given geo-object (feature) source. The SLD file must be           * .amd} file for a given geo-object (feature) source. The SLD file must be
847           * present. A missing attribute meta-data file is tolerated.           * present. A missing attribute meta-data file is tolerated.
848           *           *
849           * @param geoObjectURL           * @param geoObjectURL
# Line 875  public class StyledLayerUtil { Line 903  public class StyledLayerUtil {
903    
904          /**          /**
905           * 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
906           * meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a           * meta data ({@link RasterLegendData} or {@link AttributeMetadataImpl}) to
907           * {@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.
908           *           *
909           * @param style           * @param style
910           *            style to save           *            style to save
# Line 892  public class StyledLayerUtil { Line 920  public class StyledLayerUtil {
920          }          }
921    
922          /**          /**
923             * *If appended to the name of a rule, this rule shall not be shown in the
924             * legend
925             */
926            public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND";
927    
928            /**
929           * Creates a {@link JPanel} that shows a legend for a list of           * Creates a {@link JPanel} that shows a legend for a list of
930           * {@link FeatureTypeStyle}s and a targeted featureType           * {@link FeatureTypeStyle}s and a targeted featureType
931           *           *
932             * @param style
933             *            The Style to presented in this legend
934           * @param featureType           * @param featureType
935           *            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  
936           *           *
937           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
938           *         Kr&uuml;ger</a>           *         Tzeggai</a>
939           */           */
940          public static JPanel createLegendPanel(Style style,          public static JPanel createLegendSwingPanel(Style style,
941                          final SimpleFeatureType featureType, final int iconWidth,                          final SimpleFeatureType featureType, final int iconWidth,
942                          final int iconHeight) {                          final int iconHeight) {
943    
944                  final List<FeatureTypeStyle> list = style.featureTypeStyles();                  final List<FeatureTypeStyle> list = style.featureTypeStyles();
945    
946                  final JPanel panel = new JPanel(new MigLayout("wrap 2","[]:3:[]"));                  final JPanel panel = new JPanel(new MigLayout("wrap 2", "[]:3:[]"));
947    
948                  if (style == null) {                  if (style == null) {
949                          // No Style => no legend                          // No Style => no legend
# Line 922  public class StyledLayerUtil { Line 956  public class StyledLayerUtil {
956                          final List<Rule> rules = ftStyle.rules();                          final List<Rule> rules = ftStyle.rules();
957                          for (final Rule rule : rules) {                          for (final Rule rule : rules) {
958    
959                                    // Check if this RULE shall actually appear in the legend
960                                    if (rule.getName() != null
961                                                    && rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT))
962                                            continue;
963    
964                                  /**                                  /**
965                                   * Let's not create a hbox for Rules that only contain                                   * Let's not create a hbox for Rules that only contain
966                                   * TextSymbolizers                                   * TextSymbolizers
# Line 937  public class StyledLayerUtil { Line 976  public class StyledLayerUtil {
976                                  final ImageIcon legendIcon = new ImageIcon(imageForRule);                                  final ImageIcon legendIcon = new ImageIcon(imageForRule);
977    
978                                  final JLabel iconLabel = new JLabel(legendIcon);                                  final JLabel iconLabel = new JLabel(legendIcon);
979                                  panel.add(iconLabel,"sgx1");                                  panel.add(iconLabel, "sgx1");
980  //                              hbox.setAlignmentX(0f);                                  // hbox.setAlignmentX(0f);
981  //                              hbox.add(iconLabel);                                  // hbox.add(iconLabel);
982  //                              hbox.add(Box.createHorizontalStrut(3));                                  // hbox.add(Box.createHorizontalStrut(3));
983    
984                                  final Translation labelT = new Translation();                                  final Translation labelT = new Translation();
985                                  labelT.fromOneLine(rule.getDescription().getTitle());                                  labelT.fromOneLine(rule.getDescription().getTitle());
986                                  final JLabel classTitleLabel = new JLabel(labelT.toString());                                  final JLabel classTitleLabel = new JLabel(labelT.toString());
987                                    
988                                  panel.add(classTitleLabel,"sgx2");                                  panel.add(classTitleLabel, "sgx2");
989                                  classTitleLabel.setLabelFor(iconLabel);                                  classTitleLabel.setLabelFor(iconLabel);
990                          }                          }
991                  }                  }
# Line 954  public class StyledLayerUtil { Line 993  public class StyledLayerUtil {
993                  return panel;                  return panel;
994          }          }
995    
996            
997    
998            
999          /**          /**
1000           * Creates a {@link JComponent} that contains a legend for a given           * Creates a {@link JComponent} that contains a legend for a given
1001           * rasterLayer and a given {@link Style}.           * {@link StyledRasterInterface} and a given {@link Style}.
1002           *           *
1003           * @param style           * @param style
1004           *            if <code>null</code>, the default {@link Style} is extracetd           *            if <code>null</code>, the default {@link Style} is extracetd
1005           *            from the {@link StyledRasterInterface}           *            from the {@link StyledRasterInterface}
1006           */           */
1007          public static JPanel createLegendPanel(          public static JPanel createLegendSwingPanel(
1008                          final StyledRasterInterface<?> styledRaster, Style style,                          final StyledRasterInterface<?> styledRaster, Style style,
1009                          final int iconWidth, final int iconHeight) {                          final int iconWidth, final int iconHeight) {
1010    
# Line 986  public class StyledLayerUtil { Line 1028  public class StyledLayerUtil {
1028                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
1029                                  .createSampleRasters();                                  .createSampleRasters();
1030    
1031                  final JPanel panel = new JPanel(new MigLayout("wrap 2"));                  final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0"));
1032    
1033                  for (final Double rValue : legendRasterValues) {                  for (final Double rValue : legendRasterValues) {
1034    
# Line 1000  public class StyledLayerUtil { Line 1042  public class StyledLayerUtil {
1042                          // ****************************************************************************                          // ****************************************************************************
1043                          final BufferedImage buffImage = new BufferedImage(iconWidth,                          final BufferedImage buffImage = new BufferedImage(iconWidth,
1044                                          iconHeight, BufferedImage.TYPE_INT_ARGB);                                          iconHeight, BufferedImage.TYPE_INT_ARGB);
1045                            
1046                          final Graphics2D graphics = buffImage.createGraphics();                          final Graphics2D graphics = buffImage.createGraphics();
1047    
1048                          if (colorModel != null) {                          if (colorModel != null) {
1049                                  // The colors come from the ColorModel!                                  // The colors come from the ColorModel!
1050                                    
1051                                  try {                                  try {
1052                                          Object inData = null;                                          Object inData = null;
1053                                          switch (colorModel.getTransferType()) {                                          switch (colorModel.getTransferType()) {
# Line 1075  public class StyledLayerUtil { Line 1117  public class StyledLayerUtil {
1117                          }                          }
1118    
1119                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));
1120  //                      hbox.setAlignmentX(0f);                          panel.add(iconLabel, "sgx1");
                         panel.add(iconLabel,"sgx1");  
 //                      hbox.add(Box.createHorizontalStrut(3));  
1121    
1122                          final Translation labelT = rasterLegendData.get(rValue);                          final Translation labelT = rasterLegendData.get(rValue);
1123                          final JLabel classTitleLabel = new JLabel(labelT.toString());                          final JLabel classTitleLabel = new JLabel(labelT.toString());
1124                          panel.add(classTitleLabel,"sgx2" + (rasterLegendData.getPaintGaps()? ", gapy 0 3":""));                          panel.add(classTitleLabel, "sgx2"
1125                                            + (rasterLegendData.isPaintGaps() ? ", gapy 0:0:0 5:5:5"
1126                                                            : ""));
1127                          classTitleLabel.setLabelFor(iconLabel);                          classTitleLabel.setLabelFor(iconLabel);
1128    
1129  //                      box.add(hbox);                          if (rasterLegendData.isPaintGaps()) {
   
                         if (rasterLegendData.getPaintGaps()) {  
1130                                  iconLabel                                  iconLabel
1131                                                  .setBorder(BorderFactory.createLineBorder(Color.black));                                                  .setBorder(BorderFactory.createLineBorder(Color.black));
1132                          }                          }
# Line 1095  public class StyledLayerUtil { Line 1135  public class StyledLayerUtil {
1135    
1136                  return panel;                  return panel;
1137          }          }
1138            
1139                    
1140    
1141          /**          /**
1142           * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May           * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May
# Line 1109  public class StyledLayerUtil { Line 1151  public class StyledLayerUtil {
1151                          if (geoObject instanceof GridCoverage2D) {                          if (geoObject instanceof GridCoverage2D) {
1152                                  final GridCoverage2D cov = (GridCoverage2D) geoObject;                                  final GridCoverage2D cov = (GridCoverage2D) geoObject;
1153                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1154                          } else if (styledGrid instanceof StyledRasterPyramidInterface) {                          } else if (styledGrid instanceof StyledGridCoverageReaderInterface) {
1155    
1156                                  final Parameter readGG = new Parameter(                                  final Parameter readGG = new Parameter(
1157                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);
# Line 1145  public class StyledLayerUtil { Line 1187  public class StyledLayerUtil {
1187                          final StyledRasterInterface<?> styledRaster) {                          final StyledRasterInterface<?> styledRaster) {
1188                  final ColorModel colorModel = getColorModel(styledRaster);                  final ColorModel colorModel = getColorModel(styledRaster);
1189    
1190                  LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "                  // LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
1191                                  + colorModel.getClass().getSimpleName());                  // + colorModel != null ? colorModel.getClass().getSimpleName() :
1192                    // "NULL");
1193    
1194                  if (colorModel == null)                  if (colorModel == null)
1195                          return true;                          return true;
# Line 1154  public class StyledLayerUtil { Line 1197  public class StyledLayerUtil {
1197                          return true;                          return true;
1198                  return false;                  return false;
1199          }          }
1200    
1201            /**
1202             * Set the given Style as the Style of the {@link MapLayer}, unless the
1203             * styles are the same (not comparing selection stuff). If the
1204             * {@link MapLayer}s {@link Style} is changed, the selection FTS is kept.<br/>
1205             * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a
1206             * repaint, so only use it when needed.
1207             *
1208             * @return <code>true</code> if the {@link MapLayer}'s {@link Style} has
1209             *         been changed.
1210             */
1211            public static boolean updateMapLayerStyleIfChangedAndKeepSelection(
1212                            MapLayer mapLayer, Style style2) {
1213    
1214                    Style mapLayerStyleCleaned = StylingUtil
1215                                    .removeSelectionFeatureTypeStyle(mapLayer.getStyle());
1216    
1217                    Style newStyleCleaned = StylingUtil
1218                                    .removeSelectionFeatureTypeStyle(style2);
1219    
1220                    if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned, newStyleCleaned)) {
1221    
1222                            // They are different when compared without SELECTION FTS!
1223    
1224                            // Now let's copy any SELECTION FTS to the now style
1225                            FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil
1226                                            .getSelectionFeatureTypeStyle(mapLayer.getStyle());
1227                            if (selectionFeatureTypeStyle != null) {
1228                                    newStyleCleaned.featureTypeStyles().add(
1229                                                    selectionFeatureTypeStyle);
1230                                    // newStyleCleaned is not so clean anymore... We just alled a
1231                                    // selcetion FTS
1232                            }
1233    
1234                            mapLayer.setStyle(newStyleCleaned);
1235    
1236                            return true;
1237    
1238                    } else {
1239                            return false;
1240                    }
1241            }
1242    
1243            /**
1244             * After loading an atlas, the AttribteMetaData contains whatever is written
1245             * in the XML. But the DBF may have changed! This method checks an
1246             * {@link AttributeMetadataMap} against a schema and also corrects
1247             * upperCase/lowerCase problems. It will also remove any geometry column
1248             * attribute metadata.
1249             */
1250            /**
1251             * After loading an atlas, the AttribteMetaData contains whatever is written
1252             * in the XML. But the DBF may have changed!
1253             */
1254            public static void checkAttribMetaData(
1255                            AttributeMetadataMap<AttributeMetadataImpl> attributeMetaDataMap,
1256                            SimpleFeatureType schema) {
1257    
1258                    if (schema == null)
1259                            throw new IllegalArgumentException("schmema may not be null!");
1260    
1261                    ArrayList<Name> willRemove = new ArrayList<Name>();
1262    
1263                    // 1. Check.. all attributes in the atm should be in the schema as well.
1264                    // maybe correct some upperCase/loweCase stuff
1265    
1266                    for (AttributeMetadataInterface atm : attributeMetaDataMap.values()) {
1267    
1268                            AttributeDescriptor foundDescr = schema
1269                                            .getDescriptor(atm.getName());
1270                            if (foundDescr == null) {
1271                                    NameImpl bestMatch = FeatureUtil.findBestMatchingAttribute(schema,
1272                                                    atm.getLocalName());
1273                                    if (bestMatch == null)
1274                                            willRemove.add(atm.getName());
1275                                    else
1276                                            atm.setName(bestMatch);
1277                            } else if (foundDescr instanceof GeometryDescriptor) {
1278                                    // We don't want GeometryColumns in here
1279                                    willRemove.add(atm.getName());
1280                            }
1281                    }
1282    
1283                    // Remove the ones that were not findable in the schema
1284                    for (Name removeName : willRemove) {
1285                            if (attributeMetaDataMap.remove(removeName) == null) {
1286                                    LOGGER.warn("removing the AMData didn't work");
1287                            }
1288                    }
1289    
1290                    // 2. check... all attributes from the schema must have an ATM
1291                    for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
1292                            if (ad instanceof GeometryDescriptor)
1293                                    continue;
1294                            if (!attributeMetaDataMap.containsKey(ad.getName())) {
1295                                    attributeMetaDataMap.put( new NameImpl(ad.getName().getNamespaceURI(), ad.getName().getLocalPart()),
1296                                                    new AttributeMetadataImpl(ad, schema
1297                                                                    .getAttributeDescriptors().indexOf(ad),
1298                                                                    attributeMetaDataMap.getLanguages()));
1299                            }
1300                    }
1301            }
1302    
1303            /**
1304             * Checks every attribute name in the {@link AttributeMetadataMap} for its
1305             * binding type. It the type is textual, add the mrpty string as a NODATA
1306             * value.
1307             *
1308             * @param attributeMetaDataMap
1309             * @param schema
1310             */
1311            public static void addEmptyStringToAllTextualAttributes(
1312                            AttributeMetadataMap<? extends AttributeMetadataInterface> attributeMetaDataMap,
1313                            SimpleFeatureType schema) {
1314    
1315                    for (Name name : attributeMetaDataMap.keySet()) {
1316                            if (String.class.isAssignableFrom(schema.getDescriptor(name)
1317                                            .getType().getBinding())) {
1318                                    attributeMetaDataMap.get(name).getNodataValues().add("");
1319                            }
1320                    }
1321            }
1322    
1323            /**
1324             * @return a nicely formatted String containing all NODATA values of any
1325             *         {@link AttributeMetadataInterface} object. Strings are quoted so that any
1326             *         empty {@link String} can be seen.
1327             */
1328            public static String formatNoDataValues(Set<Object> nodataValuesList) {
1329                    String nicelyFormatted = "";
1330                    if (nodataValuesList != null) {
1331                            if (nodataValuesList.size() == 0)
1332                                    nicelyFormatted = "";
1333                            else {
1334                                    for (Object ndo : nodataValuesList) {
1335                                            if (ndo instanceof String)
1336                                                    nicelyFormatted += "\"" + ndo + "\"";
1337                                            else
1338                                                    nicelyFormatted += ndo.toString();
1339    
1340                                            nicelyFormatted += ",";
1341                                    }
1342                                    // Remove the extra comma
1343                                    nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted
1344                                                    .length() - 1);
1345                            }
1346                    }
1347                    return nicelyFormatted;
1348            }
1349  }  }

Legend:
Removed from v.516  
changed lines
  Added in v.1203

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26