/[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 534 by alfonx, Fri Nov 20 10:28:01 2009 UTC trunk/src/skrueger/geotools/StyledLayerUtil.java revision 769 by alfonx, Sun Mar 21 11:02:34 2010 UTC
# 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 89  import schmitzm.lang.LangUtil; Line 93  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.AttributeMetadata;
96    import skrueger.AttributeMetadataImpl;
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 {@link AttributeMetadataMap#sortedValuesVisibleOnly()}           *            TODO replace with
255             *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}
256           */           */
257          public static AttributeMetadataMap getVisibleAttributeMetaData(          public static AttributeMetadataMap<? extends AttributeMetadata > getVisibleAttributeMetaData(
258                          final AttributeMetadataMap amdMap, final boolean visible) {                          final AttributeMetadataMap<? extends AttributeMetadata> amdMap,
259                            final boolean visible) {
260                  final AttributeMetadataMap filteredMap = new AttributeMetadataMap(amdMap.getLanguages());  
261                    final AttributeMetadataMap<AttributeMetadata> filteredMap = (AttributeMetadataMap<AttributeMetadata>) amdMap.clone();
262                    if (filteredMap.size() > 0 ) {
263                            filteredMap.clear(); // Just in case the close copies the contents
264                    }
265                    
266                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadata amd : amdMap.values())
267                          if (amd.isVisible() == visible)                          if (amd.isVisible() == visible)
268                                  filteredMap.put(amd.getName(), amd);                                  filteredMap.put(amd.getName(), amd);
# Line 260  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,           * TODO 20.11.2009, SK: There are some new attribute weight, functiona,
# Line 272  public class StyledLayerUtil { Line 283  public class StyledLayerUtil {
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 Element element) {
288                  final String namespace = String.valueOf(element                  final String namespace = String.valueOf(element
289                                  .getAttributeValue("namespace"));                                  .getAttributeValue("namespace"));
290                  final String localname = String.valueOf(element                  final String localname = String.valueOf(element
# Line 293  public class StyledLayerUtil { Line 305  public class StyledLayerUtil {
305                          else if (childElement.getName().equals("desc"))                          else if (childElement.getName().equals("desc"))
306                                  desc = parseTranslation(childElement);                                  desc = parseTranslation(childElement);
307                  }                  }
308                  return new AttributeMetadata(aName, visible, name, desc, unit);                  return new AttributeMetadataImpl(aName, visible, name, desc, unit);
309          }          }
310    
311          /**          /**
312           * Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with           * Parses a {@link AttributeMetadataImpl} map from an JDOM-{@link Element}
313           * {@code <attribute>}-childs.           * with {@code <attribute>}-childs.
314           *           *
315           * @param element           * @param element
316           *            {@link Element} to parse           *            {@link Element} to parse
317             *
318             *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
319             *            more attributes which are not used by Xulu/ISDSS. GP
320             *            exports/imports the AMD via AMLExporter and AMLImporter
321             *            classes. (SK, 3.2.2010) *
322           */           */
323          public static AttributeMetadataMap parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
324                          final Element element) {                          final Element element) {
325                  final AttributeMetadataMap metaData = new AttributeMetadataMap();                  final AttributeMetadataMap metaData = new AttributeMetadataImplMap();
326                  final List<Element> attributesElements = element                  final List<Element> attributesElements = element
327                                  .getChildren(ELEM_NAME_ATTRIBUTE);                                  .getChildren(ELEM_NAME_ATTRIBUTE);
328                  for (final Element attibuteElement : attributesElements) {                  for (final Element attibuteElement : attributesElements) {
329                          final AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);                          final AttributeMetadataImpl attrMetaData = parseAttributeMetaData(attibuteElement);
330                          metaData.put(attrMetaData.getName(), attrMetaData);                          metaData.put(attrMetaData.getName(), attrMetaData);
331                  }                  }
332                  return metaData;                  return metaData;
333          }          }
334    
335          /**          /**
336           * Loads a {@link AttributeMetadata} object from an URL.           * Loads a {@link AttributeMetadataImpl} object from an URL.
337           *           *
338           * @param documentUrl           * @param documentUrl
339           *            {@link URL} to parse           *            {@link URL} to parse
# Line 329  public class StyledLayerUtil { Line 346  public class StyledLayerUtil {
346          }          }
347    
348          /**          /**
349           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
350           * object.           * {@link AttributeMetadataImpl} object.
351           *           *
352           * @param amd           * @param amd
353           *            meta data for one attribute           *            meta data for one attribute
354             *
355             *            TODO Since GP 1.3 the {@link AttributeMetadataImpl} class has
356             *            more attributes which are not used by Xulu/ISDSS. GP
357             *            exports/imports the AMD via AMLExporter and AMLImporter
358             *            classes. (SK, 3.2.2010)
359           */           */
360          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
361                          final AttributeMetadata amd) {                          final AttributeMetadata amd) {
# Line 351  public class StyledLayerUtil { Line 373  public class StyledLayerUtil {
373          }          }
374    
375          /**          /**
376           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}           * Creates an JDOM {@link Element} for the given
377           * map.           * {@link AttributeMetadataImpl} map.
378           *           *
379           * @param amdMap           * @param amdMap
380           *            map of attribute meta data           *            map of attribute meta data
381           */           */
382          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
383                          final AttributeMetadataMap amdMap) {                          final AttributeMetadataMap<? extends AttributeMetadata> amdMap) {
384                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
385                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadata amd : amdMap.values())
386                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
# Line 366  public class StyledLayerUtil { Line 388  public class StyledLayerUtil {
388          }          }
389    
390          /**          /**
391           * Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL.           * Saves a {@link AttributeMetadataImpl AttributeMetaData-Map} to an URL.
392           *           *
393           * @param amdMap           * @param amdMap
394           *            map of {@link AttributeMetadata}           *            map of {@link AttributeMetadataImpl}
395           * @param documentUrl           * @param documentUrl
396           *            {@link URL} to store the XML           *            {@link URL} to store the XML
397           */           */
# Line 776  public class StyledLayerUtil { Line 798  public class StyledLayerUtil {
798          }          }
799    
800          /**          /**
801           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata           * Loads a {@linkplain Style SLD-Style} and a
802           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} for a given
803           * file must be present. A missing attribute meta-data file is tolerated.           * geo-object (feature) source. The SLD file must be present. A missing
804             * attribute meta-data file is tolerated.
805           *           *
806           * @param geoObjectURL           * @param geoObjectURL
807           *            URL of the (already read) feature object           *            URL of the (already read) feature object
# Line 821  public class StyledLayerUtil { Line 844  public class StyledLayerUtil {
844    
845          /**          /**
846           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
847           * {@linkplain AttributeMetadata AttributeMetaData-Map} from a {@code .amd}           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a {@code
848           * 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
849           * present. A missing attribute meta-data file is tolerated.           * present. A missing attribute meta-data file is tolerated.
850           *           *
851           * @param geoObjectURL           * @param geoObjectURL
# Line 882  public class StyledLayerUtil { Line 905  public class StyledLayerUtil {
905    
906          /**          /**
907           * 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
908           * meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a           * meta data ({@link RasterLegendData} or {@link AttributeMetadataImpl}) to
909           * {@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.
910           *           *
911           * @param style           * @param style
912           *            style to save           *            style to save
# Line 899  public class StyledLayerUtil { Line 922  public class StyledLayerUtil {
922          }          }
923    
924          /**          /**
925             * *If appended to the name of a rule, this rule shall not be shown in the
926             * legend
927             */
928            public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND";
929    
930            /**
931           * Creates a {@link JPanel} that shows a legend for a list of           * Creates a {@link JPanel} that shows a legend for a list of
932           * {@link FeatureTypeStyle}s and a targeted featureType           * {@link FeatureTypeStyle}s and a targeted featureType
933           *           *
# Line 929  public class StyledLayerUtil { Line 958  public class StyledLayerUtil {
958                          final List<Rule> rules = ftStyle.rules();                          final List<Rule> rules = ftStyle.rules();
959                          for (final Rule rule : rules) {                          for (final Rule rule : rules) {
960    
961                                    // Check if this RULE shall actually appear in the legend
962                                    if (rule.getName() != null
963                                                    && rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT))
964                                            continue;
965    
966                                  /**                                  /**
967                                   * Let's not create a hbox for Rules that only contain                                   * Let's not create a hbox for Rules that only contain
968                                   * TextSymbolizers                                   * TextSymbolizers
# Line 993  public class StyledLayerUtil { Line 1027  public class StyledLayerUtil {
1027                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
1028                                  .createSampleRasters();                                  .createSampleRasters();
1029    
1030                  final JPanel panel = new JPanel(new MigLayout("wrap 2"));                  final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0"));
1031    
1032                  for (final Double rValue : legendRasterValues) {                  for (final Double rValue : legendRasterValues) {
1033    
# Line 1082  public class StyledLayerUtil { Line 1116  public class StyledLayerUtil {
1116                          }                          }
1117    
1118                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));
                         // hbox.setAlignmentX(0f);  
1119                          panel.add(iconLabel, "sgx1");                          panel.add(iconLabel, "sgx1");
                         // hbox.add(Box.createHorizontalStrut(3));  
1120    
1121                          final Translation labelT = rasterLegendData.get(rValue);                          final Translation labelT = rasterLegendData.get(rValue);
1122                          final JLabel classTitleLabel = new JLabel(labelT.toString());                          final JLabel classTitleLabel = new JLabel(labelT.toString());
1123                          panel.add(classTitleLabel, "sgx2"                          panel.add(classTitleLabel, "sgx2"
1124                                          + (rasterLegendData.getPaintGaps() ? ", gapy 0 3" : ""));                                          + (rasterLegendData.isPaintGaps() ? ", gapy 0:0:0 5:5:5"
1125                                                            : ""));
1126                          classTitleLabel.setLabelFor(iconLabel);                          classTitleLabel.setLabelFor(iconLabel);
1127    
1128                          // box.add(hbox);                          if (rasterLegendData.isPaintGaps()) {
   
                         if (rasterLegendData.getPaintGaps()) {  
1129                                  iconLabel                                  iconLabel
1130                                                  .setBorder(BorderFactory.createLineBorder(Color.black));                                                  .setBorder(BorderFactory.createLineBorder(Color.black));
1131                          }                          }
# Line 1152  public class StyledLayerUtil { Line 1183  public class StyledLayerUtil {
1183          public static boolean isStyleable(          public static boolean isStyleable(
1184                          final StyledRasterInterface<?> styledRaster) {                          final StyledRasterInterface<?> styledRaster) {
1185                  final ColorModel colorModel = getColorModel(styledRaster);                  final ColorModel colorModel = getColorModel(styledRaster);
1186                    
1187  //              LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "                  // LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
1188  //                              + colorModel != null ? colorModel.getClass().getSimpleName() : "NULL");                  // + colorModel != null ? colorModel.getClass().getSimpleName() :
1189                    // "NULL");
1190    
1191                  if (colorModel == null)                  if (colorModel == null)
1192                          return true;                          return true;
# Line 1170  public class StyledLayerUtil { Line 1202  public class StyledLayerUtil {
1202           * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a           * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a
1203           * repaint, so only use it when needed.           * repaint, so only use it when needed.
1204           *           *
1205           * @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
1206             *         been changed.
1207           */           */
1208          public static boolean updateMapLayerStyleIfChangedAndKeepSelection(MapLayer mapLayer,          public static boolean updateMapLayerStyleIfChangedAndKeepSelection(
1209                          Style style2) {                          MapLayer mapLayer, Style style2) {
1210    
1211                  Style mapLayerStyleCleaned = StylingUtil                  Style mapLayerStyleCleaned = StylingUtil
1212                                                  .removeSelectionFeatureTypeStyle(mapLayer.getStyle());                                  .removeSelectionFeatureTypeStyle(mapLayer.getStyle());
1213                    
1214                  Style newStyleCleaned = StylingUtil.removeSelectionFeatureTypeStyle(style2);                  Style newStyleCleaned = StylingUtil
1215                                                    .removeSelectionFeatureTypeStyle(style2);
1216                  if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned,  
1217                                  newStyleCleaned)) {                  if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned, newStyleCleaned)) {
1218                            
1219                          // They are different when compared without SELECTION FTS!                          // They are different when compared without SELECTION FTS!
1220                            
1221                          // Now let's copy any SELECTION FTS to the now style                          // Now let's copy any SELECTION FTS to the now style
1222                          FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil.getSelectionFeatureTypeStyle( mapLayer.getStyle() );                          FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil
1223                                            .getSelectionFeatureTypeStyle(mapLayer.getStyle());
1224                          if (selectionFeatureTypeStyle != null) {                          if (selectionFeatureTypeStyle != null) {
1225                                  newStyleCleaned.featureTypeStyles().add(selectionFeatureTypeStyle);                                  newStyleCleaned.featureTypeStyles().add(
1226                                  // newStyleCleaned is not so clean anymore... We just alled a selcetion FTS                                                  selectionFeatureTypeStyle);
1227                          }                                    // newStyleCleaned is not so clean anymore... We just alled a
1228                                                            // selcetion FTS
1229                            }
1230    
1231                          mapLayer.setStyle(newStyleCleaned);                          mapLayer.setStyle(newStyleCleaned);
1232                            
1233                          return true;                          return true;
1234                            
1235                  } else {                  } else {
1236                          return false;                          return false;
1237                  }                  }
1238          }          }
1239    
1240            /**
1241             * After loading an atlas, the AttribteMetaData contains whatever is written
1242             * in the XML. But the DBF may have changed! This method checks an
1243             * {@link AttributeMetadataMap} against a schema and also corrects
1244             * upperCase/lowerCase problems. It will also remove any geometry column
1245             * attribute metadata.
1246             */
1247            /**
1248             * After loading an atlas, the AttribteMetaData contains whatever is written
1249             * in the XML. But the DBF may have changed!
1250             */
1251            public static void checkAttribMetaData(
1252                            AttributeMetadataMap<AttributeMetadataImpl> attributeMetaDataMap,
1253                            SimpleFeatureType schema) {
1254    
1255                    if (schema == null)
1256                            throw new IllegalArgumentException("schmema may not be null!");
1257    
1258                    ArrayList<Name> willRemove = new ArrayList<Name>();
1259    
1260                    // 1. Check.. all attributes in the atm should be in the schema as well.
1261                    // maybe correct some upperCase/loweCase stuff
1262    
1263                    for (AttributeMetadata atm : attributeMetaDataMap.values()) {
1264    
1265                            AttributeDescriptor foundDescr = schema
1266                                            .getDescriptor(atm.getName());
1267                            if (foundDescr == null) {
1268                                    Name bestMatch = FeatureUtil.findBestMatchingAttribute(schema,
1269                                                    atm.getLocalName());
1270                                    if (bestMatch == null)
1271                                            willRemove.add(atm.getName());
1272                                    else
1273                                            atm.setName(bestMatch);
1274                            } else if (foundDescr instanceof GeometryDescriptor) {
1275                                    // We don't want GeometryColumns in here
1276                                    willRemove.add(atm.getName());
1277                            }
1278                    }
1279    
1280                    // Remove the ones that were not findable in the schema
1281                    for (Name removeName : willRemove) {
1282                            if (attributeMetaDataMap.remove(removeName) == null) {
1283                                    LOGGER.warn("removing the AMData didn't work");
1284                            }
1285                    }
1286    
1287                    // 2. check... all attributes from the schema must have an ATM
1288                    for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
1289                            if (ad instanceof GeometryDescriptor)
1290                                    continue;
1291                            if (!attributeMetaDataMap.containsKey(ad.getName())) {
1292                                    attributeMetaDataMap.put(ad.getName(),
1293                                                    new AttributeMetadataImpl(ad, schema
1294                                                                    .getAttributeDescriptors().indexOf(ad),
1295                                                                    attributeMetaDataMap.getLanguages()));
1296                            }
1297                    }
1298            }
1299    
1300            /**
1301             * Checks every attribute name in the {@link AttributeMetadataMap} for its
1302             * binding type. It the type is textual, add the mrpty string as a NODATA
1303             * value.
1304             *
1305             * @param attributeMetaDataMap
1306             * @param schema
1307             */
1308            public static void addEmptyStringToAllTextualAttributes(
1309                            AttributeMetadataMap<? extends AttributeMetadata> attributeMetaDataMap,
1310                            SimpleFeatureType schema) {
1311    
1312                    for (Name name : attributeMetaDataMap.keySet()) {
1313                            if (String.class.isAssignableFrom(schema.getDescriptor(name)
1314                                            .getType().getBinding())) {
1315                                    attributeMetaDataMap.get(name).getNodataValues().add("");
1316                            }
1317                    }
1318            }
1319    
1320            /**
1321             * @return a nicely formatted String containing all NODATA values of any
1322             *         {@link AttributeMetadata} object. Strings are quoted so that any
1323             *         empty {@link String} can be seen.
1324             */
1325            public static String formatNoDataValues(Set<Object> nodataValuesList) {
1326                    String nicelyFormatted = "";
1327                    if (nodataValuesList != null) {
1328                            if (nodataValuesList.size() == 0)
1329                                    nicelyFormatted = "";
1330                            else {
1331                                    for (Object ndo : nodataValuesList) {
1332                                            if (ndo instanceof String)
1333                                                    nicelyFormatted += "\"" + ndo + "\"";
1334                                            else
1335                                                    nicelyFormatted += ndo.toString();
1336    
1337                                            nicelyFormatted += ",";
1338                                    }
1339                                    // Remove the extra comma
1340                                    nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted
1341                                                    .length() - 1);
1342                            }
1343                    }
1344                    return nicelyFormatted;
1345            }
1346  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26