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

revision 1203 by alfonx, Tue Nov 2 22:53:55 2010 UTC revision 1270 by alfonx, Sun Nov 14 00:25:52 2010 UTC
# 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;  import java.util.Set;
# Line 83  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.AttributeMetadataImpl;  import skrueger.AttributeMetadataImpl;
# Line 97  import skrueger.AttributeMetadataInterfa Line 102  import skrueger.AttributeMetadataInterfa
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 111  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 123  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 254  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<? extends AttributeMetadataInterface > getVisibleAttributeMetaData(          public static AttributeMetadataMap<? extends AttributeMetadataInterface> getVisibleAttributeMetaData(
272                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,                          final AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap,
273                          final boolean visible) {                          final boolean visible) {
274    
275                  final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap.clone();                  final AttributeMetadataMap<AttributeMetadataInterface> filteredMap = (AttributeMetadataMap<AttributeMetadataInterface>) amdMap
276                  if (filteredMap.size() > 0 ) {                                  .clone();
277                    if (filteredMap.size() > 0) {
278                          filteredMap.clear(); // Just in case the close copies the contents                          filteredMap.clear(); // Just in case the close copies the contents
279                  }                  }
280                    
281                  for (final AttributeMetadataInterface amd : amdMap.values())                  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);
# Line 358  public class StyledLayerUtil { Line 373  public class StyledLayerUtil {
373          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
374                          final AttributeMetadataInterface 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 691  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 842  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 AttributeMetadataImpl AttributeMetaData-Map} from a {@code           * {@linkplain AttributeMetadataImpl AttributeMetaData-Map} from a
861           * .amd} 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 878  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 886  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 920  public class StyledLayerUtil { Line 935  public class StyledLayerUtil {
935          }          }
936    
937          /**          /**
          * *If appended to the name of a rule, this rule shall not be shown in the  
          * legend  
          */  
         public final static String HIDE_IN_LAYER_LEGEND_HINT = "HIDE_IN_LEGEND";  
   
         /**  
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           * @param style
942           *            The Style to presented in this legend           *            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?
945           *           *
946           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Tzeggai</a>  
947           */           */
948          public static JPanel createLegendSwingPanel(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();
# Line 958  public class StyledLayerUtil { Line 978  public class StyledLayerUtil {
978    
979                                  // Check if this RULE shall actually appear in the legend                                  // Check if this RULE shall actually appear in the legend
980                                  if (rule.getName() != null                                  if (rule.getName() != null
981                                                  && rule.getName().endsWith(HIDE_IN_LAYER_LEGEND_HINT))                                                  && rule.getName().contains(HIDE_IN_LAYER_LEGEND_HINT))
982                                          continue;                                          continue;
983    
984                                  /**                                  /**
# Line 993  public class StyledLayerUtil { Line 1013  public class StyledLayerUtil {
1013                  return panel;                  return panel;
1014          }          }
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           * {@link StyledRasterInterface} and a given {@link Style}.           * {@link StyledRasterInterface} and a given {@link Style}.
# Line 1090  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 1135  public class StyledLayerUtil { Line 1152  public class StyledLayerUtil {
1152    
1153                  return panel;                  return panel;
1154          }          }
           
                   
1155    
1156          /**          /**
1157           * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May           * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May
# Line 1156  public class StyledLayerUtil { Line 1171  public class StyledLayerUtil {
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                                  final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil                                  } else if (geoObject instanceof AbstractGridCoverage2DReader) {
1188                                                  .getWrappedGeoObject(rFc);                                          aReader = (AbstractGridCoverage2DReader) geoObject;
1189                                  final GridCoverage2D cov = (GridCoverage2D) aReader  
1190                                    } else
1191                                            throw new RuntimeException("need a reader...");
1192                                    //
1193    
1194                                    final GridCoverage2D cov = aReader
1195                                                  .read(new GeneralParameterValue[] { readGG });                                                  .read(new GeneralParameterValue[] { readGG });
1196                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1197                          }                          }
# Line 1195  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 1268  public class StyledLayerUtil { Line 1296  public class StyledLayerUtil {
1296                          AttributeDescriptor foundDescr = schema                          AttributeDescriptor foundDescr = schema
1297                                          .getDescriptor(atm.getName());                                          .getDescriptor(atm.getName());
1298                          if (foundDescr == null) {                          if (foundDescr == null) {
1299                                  NameImpl 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 1292  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( new NameImpl(ad.getName().getNamespaceURI(), ad.getName().getLocalPart()),                                  attributeMetaDataMap.put(new NameImpl(ad.getName()
1324                                                    .getNamespaceURI(), ad.getName().getLocalPart()),
1325                                                  new AttributeMetadataImpl(ad, schema                                                  new AttributeMetadataImpl(ad, schema
1326                                                                  .getAttributeDescriptors().indexOf(ad),                                                                  .getAttributeDescriptors().indexOf(ad),
1327                                                                  attributeMetaDataMap.getLanguages()));                                                                  attributeMetaDataMap.getLanguages()));
# Line 1322  public class StyledLayerUtil { Line 1351  public class StyledLayerUtil {
1351    
1352          /**          /**
1353           * @return a nicely formatted String containing all NODATA values of any           * @return a nicely formatted String containing all NODATA values of any
1354           *         {@link AttributeMetadataInterface} object. Strings are quoted so that any           *         {@link AttributeMetadataInterface} object. Strings are quoted so
1355           *         empty {@link String} can be seen.           *         that any empty {@link String} can be seen.
1356           */           */
1357          public static String formatNoDataValues(Set<Object> nodataValuesList) {          public static String formatNoDataValues(Set<Object> nodataValuesList) {
1358                  String nicelyFormatted = "";                  String nicelyFormatted = "";
# Line 1340  public class StyledLayerUtil { Line 1369  public class StyledLayerUtil {
1369                                          nicelyFormatted += ",";                                          nicelyFormatted += ",";
1370                                  }                                  }
1371                                  // Remove the extra comma                                  // Remove the extra comma
1372                                  nicelyFormatted = nicelyFormatted.substring(0, nicelyFormatted                                  nicelyFormatted = nicelyFormatted.substring(0,
1373                                                  .length() - 1);                                                  nicelyFormatted.length() - 1);
1374                          }                          }
1375                  }                  }
1376                  return nicelyFormatted;                  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.1203  
changed lines
  Added in v.1270

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26