/[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-RC1/src/skrueger/geotools/StyledLayerUtil.java revision 604 by alfonx, Wed Dec 9 14:15:53 2009 UTC trunk/src/skrueger/geotools/StyledLayerUtil.java revision 695 by alfonx, Fri Feb 12 20:46:08 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    
# Line 78  import org.jdom.input.SAXBuilder; Line 79  import org.jdom.input.SAXBuilder;
79  import org.jdom.output.XMLOutputter;  import org.jdom.output.XMLOutputter;
80  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
81  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
82    import org.opengis.feature.type.AttributeDescriptor;
83    import org.opengis.feature.type.GeometryDescriptor;
84  import org.opengis.feature.type.Name;  import org.opengis.feature.type.Name;
85  import org.opengis.parameter.GeneralParameterValue;  import org.opengis.parameter.GeneralParameterValue;
86    
# Line 245  public class StyledLayerUtil { Line 248  public class StyledLayerUtil {
248           * @param visible           * @param visible
249           *            indicated whether the visible or invisible entries are           *            indicated whether the visible or invisible entries are
250           *            returned           *            returned
251           *                       *
252           * TODO replace with {@link AttributeMetadataMap#sortedValuesVisibleOnly()}           *            TODO replace with
253             *            {@link AttributeMetadataMap#sortedValuesVisibleOnly()}
254           */           */
255          public static AttributeMetadataMap getVisibleAttributeMetaData(          public static AttributeMetadataMap getVisibleAttributeMetaData(
256                          final AttributeMetadataMap amdMap, final boolean visible) {                          final AttributeMetadataMap amdMap, final boolean visible) {
257    
258                  final AttributeMetadataMap filteredMap = new AttributeMetadataMap(amdMap.getLanguages());                  final AttributeMetadataMap filteredMap = new AttributeMetadataMap(
259                                    amdMap.getLanguages());
260                  for (final AttributeMetadata amd : amdMap.values())                  for (final AttributeMetadata amd : amdMap.values())
261                          if (amd.isVisible() == visible)                          if (amd.isVisible() == visible)
262                                  filteredMap.put(amd.getName(), amd);                                  filteredMap.put(amd.getName(), amd);
# Line 302  public class StyledLayerUtil { Line 307  public class StyledLayerUtil {
307           *           *
308           * @param element           * @param element
309           *            {@link Element} to parse           *            {@link Element} to parse
310             *
311             *            TODO Since GP 1.3 the {@link AttributeMetadata} class has more
312             *            attributes which are not used by Xulu/ISDSS. GP
313             *            exports/imports the AMD via AMLExporter and AMLImporter
314             *            classes. (SK, 3.2.2010) *
315           */           */
316          public static AttributeMetadataMap parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
317                          final Element element) {                          final Element element) {
# Line 334  public class StyledLayerUtil { Line 344  public class StyledLayerUtil {
344           *           *
345           * @param amd           * @param amd
346           *            meta data for one attribute           *            meta data for one attribute
347             *
348             *            TODO Since GP 1.3 the {@link AttributeMetadata} class has more
349             *            attributes which are not used by Xulu/ISDSS. GP
350             *            exports/imports the AMD via AMLExporter and AMLImporter
351             *            classes. (SK, 3.2.2010)
352           */           */
353          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
354                          final AttributeMetadata amd) {                          final AttributeMetadata amd) {
# Line 899  public class StyledLayerUtil { Line 914  public class StyledLayerUtil {
914          }          }
915    
916          /**          /**
917             * *If appended to the name of a rule, this rule shall not be shown in the
918             * legend
919             */
920            public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND";
921    
922            /**
923           * Creates a {@link JPanel} that shows a legend for a list of           * Creates a {@link JPanel} that shows a legend for a list of
924           * {@link FeatureTypeStyle}s and a targeted featureType           * {@link FeatureTypeStyle}s and a targeted featureType
925           *           *
# Line 929  public class StyledLayerUtil { Line 950  public class StyledLayerUtil {
950                          final List<Rule> rules = ftStyle.rules();                          final List<Rule> rules = ftStyle.rules();
951                          for (final Rule rule : rules) {                          for (final Rule rule : rules) {
952    
953                                    // Check if this RULE shall actually appear in the legend
954                                    if (rule.getName() != null && rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT))
955                                            continue;
956    
957                                  /**                                  /**
958                                   * Let's not create a hbox for Rules that only contain                                   * Let's not create a hbox for Rules that only contain
959                                   * TextSymbolizers                                   * TextSymbolizers
# Line 993  public class StyledLayerUtil { Line 1018  public class StyledLayerUtil {
1018                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData                  final Map<Double, GridCoverage2D> sampleRasters = rasterLegendData
1019                                  .createSampleRasters();                                  .createSampleRasters();
1020    
1021                  final JPanel panel = new JPanel(new MigLayout("wrap 2"));                  final JPanel panel = new JPanel(new MigLayout("wrap 2, gapy 0"));
1022    
1023                  for (final Double rValue : legendRasterValues) {                  for (final Double rValue : legendRasterValues) {
1024    
# Line 1082  public class StyledLayerUtil { Line 1107  public class StyledLayerUtil {
1107                          }                          }
1108    
1109                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));                          final JLabel iconLabel = new JLabel(new ImageIcon(buffImage));
                         // hbox.setAlignmentX(0f);  
1110                          panel.add(iconLabel, "sgx1");                          panel.add(iconLabel, "sgx1");
                         // hbox.add(Box.createHorizontalStrut(3));  
1111    
1112                          final Translation labelT = rasterLegendData.get(rValue);                          final Translation labelT = rasterLegendData.get(rValue);
1113                          final JLabel classTitleLabel = new JLabel(labelT.toString());                          final JLabel classTitleLabel = new JLabel(labelT.toString());
1114                          panel.add(classTitleLabel, "sgx2"                          panel.add(classTitleLabel, "sgx2"
1115                                          + (rasterLegendData.getPaintGaps() ? ", gapy 0 3" : ""));                                          + (rasterLegendData.isPaintGaps() ? ", gapy 0:0:0 5:5:5"
1116                                                            : ""));
1117                          classTitleLabel.setLabelFor(iconLabel);                          classTitleLabel.setLabelFor(iconLabel);
1118    
1119                          // box.add(hbox);                          if (rasterLegendData.isPaintGaps()) {
   
                         if (rasterLegendData.getPaintGaps()) {  
1120                                  iconLabel                                  iconLabel
1121                                                  .setBorder(BorderFactory.createLineBorder(Color.black));                                                  .setBorder(BorderFactory.createLineBorder(Color.black));
1122                          }                          }
# Line 1152  public class StyledLayerUtil { Line 1174  public class StyledLayerUtil {
1174          public static boolean isStyleable(          public static boolean isStyleable(
1175                          final StyledRasterInterface<?> styledRaster) {                          final StyledRasterInterface<?> styledRaster) {
1176                  final ColorModel colorModel = getColorModel(styledRaster);                  final ColorModel colorModel = getColorModel(styledRaster);
1177                    
1178  //              LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "                  // LOGGER.info("The colormodel of " + styledRaster.getTitle() + " is "
1179  //                              + colorModel != null ? colorModel.getClass().getSimpleName() : "NULL");                  // + colorModel != null ? colorModel.getClass().getSimpleName() :
1180                    // "NULL");
1181    
1182                  if (colorModel == null)                  if (colorModel == null)
1183                          return true;                          return true;
# Line 1170  public class StyledLayerUtil { Line 1193  public class StyledLayerUtil {
1193           * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a           * Remember {@link MapLayer#setStyle(Style)} triggers an event leading to a
1194           * repaint, so only use it when needed.           * repaint, so only use it when needed.
1195           *           *
1196           * @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
1197             *         been changed.
1198           */           */
1199          public static boolean updateMapLayerStyleIfChangedAndKeepSelection(MapLayer mapLayer,          public static boolean updateMapLayerStyleIfChangedAndKeepSelection(
1200                          Style style2) {                          MapLayer mapLayer, Style style2) {
1201    
1202                  Style mapLayerStyleCleaned = StylingUtil                  Style mapLayerStyleCleaned = StylingUtil
1203                                                  .removeSelectionFeatureTypeStyle(mapLayer.getStyle());                                  .removeSelectionFeatureTypeStyle(mapLayer.getStyle());
1204                    
1205                  Style newStyleCleaned = StylingUtil.removeSelectionFeatureTypeStyle(style2);                  Style newStyleCleaned = StylingUtil
1206                                                    .removeSelectionFeatureTypeStyle(style2);
1207                  if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned,  
1208                                  newStyleCleaned)) {                  if (StylingUtil.isStyleDifferent(mapLayerStyleCleaned, newStyleCleaned)) {
1209                            
1210                          // They are different when compared without SELECTION FTS!                          // They are different when compared without SELECTION FTS!
1211                            
1212                          // Now let's copy any SELECTION FTS to the now style                          // Now let's copy any SELECTION FTS to the now style
1213                          FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil.getSelectionFeatureTypeStyle( mapLayer.getStyle() );                          FeatureTypeStyle selectionFeatureTypeStyle = StylingUtil
1214                                            .getSelectionFeatureTypeStyle(mapLayer.getStyle());
1215                          if (selectionFeatureTypeStyle != null) {                          if (selectionFeatureTypeStyle != null) {
1216                                  newStyleCleaned.featureTypeStyles().add(selectionFeatureTypeStyle);                                  newStyleCleaned.featureTypeStyles().add(
1217                                  // newStyleCleaned is not so clean anymore... We just alled a selcetion FTS                                                  selectionFeatureTypeStyle);
1218                          }                                    // newStyleCleaned is not so clean anymore... We just alled a
1219                                                            // selcetion FTS
1220                            }
1221    
1222                          mapLayer.setStyle(newStyleCleaned);                          mapLayer.setStyle(newStyleCleaned);
1223                            
1224                          return true;                          return true;
1225                            
1226                  } else {                  } else {
1227                          return false;                          return false;
1228                  }                  }
1229          }          }
1230    
1231            /**
1232             * After loading an atlas, the AttribteMetaData contains whatever is written
1233             * in the XML. But the DBF may have changed! This method checks an
1234             * {@link AttributeMetadataMap} against a schema and also corrects
1235             * upperCase/lowerCase problems. It will also remove any geometry column
1236             * attribute metadata.
1237             */
1238            /**
1239             * After loading an atlas, the AttribteMetaData contains whatever is written
1240             * in the XML. But the DBF may have changed!
1241             */
1242            public static void checkAttribMetaData(
1243                            AttributeMetadataMap attributeMetaDataMap, SimpleFeatureType schema) {
1244    
1245                    ArrayList<Name> willRemove = new ArrayList<Name>();
1246    
1247                    // 1. Check.. all attributes in the atm should be in the schema as well.
1248                    // maybe correct some upperCase/loweCase stuff
1249    
1250                    for (AttributeMetadata atm : attributeMetaDataMap.values()) {
1251    
1252                            AttributeDescriptor foundDescr = schema
1253                                            .getDescriptor(atm.getName());
1254                            if (foundDescr == null) {
1255                                    Name bestMatch = FeatureUtil.findBestMatchingAttribute(schema,
1256                                                    atm.getLocalName());
1257                                    if (bestMatch == null)
1258                                            willRemove.add(atm.getName());
1259                                    else
1260                                            atm.setName(bestMatch);
1261                            } else if (foundDescr instanceof GeometryDescriptor) {
1262                                    // We don't want GeometryColumns in here
1263                                    willRemove.add(atm.getName());
1264                            }
1265                    }
1266    
1267                    // Remove the ones that were not findable in the schema
1268                    for (Name removeName : willRemove) {
1269                            if (attributeMetaDataMap.remove(removeName) == null) {
1270                                    LOGGER.warn("removing the AMData didn't work");
1271                            }
1272                    }
1273    
1274                    // 2. check... all attributes from the schema must have an ATM
1275                    for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
1276                            if (ad instanceof GeometryDescriptor)
1277                                    continue;
1278                            if (!attributeMetaDataMap.containsKey(ad.getName())) {
1279                                    attributeMetaDataMap.put(ad.getName(), new AttributeMetadata(
1280                                                    ad, schema.getAttributeDescriptors().indexOf(ad),
1281                                                    attributeMetaDataMap.getLanguages()));
1282                            }
1283                    }
1284            }
1285    
1286            /**
1287             * Checks every attribute name in the {@link AttributeMetadataMap} for its
1288             * binding type. It the type is textual, add the mrpty string as a NODATA
1289             * value.
1290             *
1291             * @param attributeMetaDataMap
1292             * @param schema
1293             */
1294            public static void addEmptyStringToAllTextualAttributes(
1295                            AttributeMetadataMap attributeMetaDataMap, SimpleFeatureType schema) {
1296    
1297                    for (Name name : attributeMetaDataMap.keySet()) {
1298                            if (String.class.isAssignableFrom(schema.getDescriptor(name)
1299                                            .getType().getBinding())) {
1300                                    attributeMetaDataMap.get(name).getNodataValues().add("");
1301                            }
1302                    }
1303            }
1304  }  }

Legend:
Removed from v.604  
changed lines
  Added in v.695

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26