/[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 406 by alfonx, Mon Sep 14 18:06:27 2009 UTC revision 464 by alfonx, Tue Oct 13 13:22:31 2009 UTC
# Line 44  import java.io.FileNotFoundException; Line 44  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;
 import java.util.HashMap;  
47  import java.util.List;  import java.util.List;
48  import java.util.Map;  import java.util.Map;
 import java.util.SortedMap;  
 import java.util.TreeMap;  
49    
50  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
51  import javax.swing.Box;  import javax.swing.Box;
# Line 63  import org.geotools.coverage.grid.GridGe Line 60  import org.geotools.coverage.grid.GridGe
60  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;  import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
61  import org.geotools.coverage.grid.io.AbstractGridFormat;  import org.geotools.coverage.grid.io.AbstractGridFormat;
62  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
63  import org.geotools.gce.imagepyramid.ImagePyramidReader;  import org.geotools.feature.NameImpl;
64  import org.geotools.geometry.jts.ReferencedEnvelope;  import org.geotools.geometry.jts.ReferencedEnvelope;
65  import org.geotools.map.DefaultMapLayer;  import org.geotools.map.DefaultMapLayer;
66  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
# Line 79  import org.jdom.Document; Line 76  import org.jdom.Document;
76  import org.jdom.Element;  import org.jdom.Element;
77  import org.jdom.input.SAXBuilder;  import org.jdom.input.SAXBuilder;
78  import org.jdom.output.XMLOutputter;  import org.jdom.output.XMLOutputter;
79    import org.opengis.feature.simple.SimpleFeature;
80  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
81    import org.opengis.feature.type.Name;
82  import org.opengis.parameter.GeneralParameterValue;  import org.opengis.parameter.GeneralParameterValue;
83    
84  import schmitzm.geotools.JTSUtil;  import schmitzm.geotools.JTSUtil;
85    import schmitzm.geotools.feature.FeatureUtil;
86  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
87  import schmitzm.io.IOUtil;  import schmitzm.io.IOUtil;
88  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
89  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
90  import skrueger.AttributeMetaData;  import skrueger.AttributeMetadata;
91  import skrueger.RasterLegendData;  import skrueger.RasterLegendData;
92  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
93    
# Line 244  public class StyledLayerUtil { Line 244  public class StyledLayerUtil {
244           *            indicated whether the visible or invisible entries are           *            indicated whether the visible or invisible entries are
245           *            returned           *            returned
246           */           */
247          public static SortedMap<Integer, AttributeMetaData> getVisibleAttributeMetaData(          public static AttributeMetadataMap getVisibleAttributeMetaData(
248                          Map<Integer, AttributeMetaData> amdMap, boolean visible) {                          AttributeMetadataMap amdMap, boolean visible) {
249                  SortedMap<Integer, AttributeMetaData> filteredMap = new TreeMap<Integer, AttributeMetaData>();                  
250                  for (AttributeMetaData amd : amdMap.values())                  AttributeMetadataMap filteredMap = new AttributeMetadataMap();
251                          if (amd.isVisible())                  for (AttributeMetadata amd : amdMap.values())
252                                  filteredMap.put(amd.getColIdx(), amd);                          if (amd.isVisible() == visible)
253                                    filteredMap.put(amd.getName(), amd);
254    
255                  return filteredMap;                  return filteredMap;
256          }          }
257    
258          /**          /**
259           * Parses a {@link AttributeMetaData} object from an JDOM-{@link Element}.           * Parses a {@link AttributeMetadata} object from an JDOM-{@link Element}.
260           * This method works like {@link           * This method works like {@link
261           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.           * AMLImport#parseDataAttribute(org.w3c.dom.Node}, but for JDOM.
262           *           *
263           * @param element           * @param element
264           *            {@link Element} to parse           *            {@link Element} to parse
265           */           */
266          public static AttributeMetaData parseAttributeMetaData(final Element element) {          public static AttributeMetadata parseAttributeMetaData(final Element element) {
267                  final Integer col = Integer.valueOf(element.getAttributeValue("col"));                  final String namespace = String.valueOf(element.getAttributeValue("namespace"));
268                    final String localname = String.valueOf(element.getAttributeValue("localname"));
269                    final Name aName= new NameImpl(namespace,localname);
270                  final Boolean visible = Boolean.valueOf(element                  final Boolean visible = Boolean.valueOf(element
271                                  .getAttributeValue("visible"));                                  .getAttributeValue("visible"));
272                  final String unit = element.getAttributeValue("unit");                  final String unit = element.getAttributeValue("unit");
# Line 279  public class StyledLayerUtil { Line 282  public class StyledLayerUtil {
282                          else if (childElement.getName().equals("desc"))                          else if (childElement.getName().equals("desc"))
283                                  desc = parseTranslation(childElement);                                  desc = parseTranslation(childElement);
284                  }                  }
285                  return new AttributeMetaData(col, visible, name, desc, unit);                  return new AttributeMetadata(aName, visible, name, desc, unit);
286          }          }
287    
288          /**          /**
289           * Parses a {@link AttributeMetaData} map from an JDOM-{@link Element} with           * Parses a {@link AttributeMetadata} map from an JDOM-{@link Element} with
290           * {@code <attribute>}-childs.           * {@code <attribute>}-childs.
291           *           *
292           * @param element           * @param element
293           *            {@link Element} to parse           *            {@link Element} to parse
294           */           */
295          public static Map<Integer, AttributeMetaData> parseAttributeMetaDataMap(          public static AttributeMetadataMap parseAttributeMetaDataMap(
296                          final Element element) {                          final Element element) {
297                  HashMap<Integer, AttributeMetaData> metaData = new HashMap<Integer, AttributeMetaData>();                  AttributeMetadataMap metaData = new AttributeMetadataMap();
298                  List<Element> attributesElements = element                  List<Element> attributesElements = element
299                                  .getChildren(ELEM_NAME_ATTRIBUTE);                                  .getChildren(ELEM_NAME_ATTRIBUTE);
300                  for (Element attibuteElement : attributesElements) {                  for (Element attibuteElement : attributesElements) {
301                          AttributeMetaData attrMetaData = parseAttributeMetaData(attibuteElement);                          AttributeMetadata attrMetaData = parseAttributeMetaData(attibuteElement);
302                          metaData.put(attrMetaData.getColIdx(), attrMetaData);                          metaData.put(attrMetaData.getName(), attrMetaData);
303                  }                  }
304                  return metaData;                  return metaData;
305          }          }
306    
307          /**          /**
308           * Loads a {@link AttributeMetaData} object from an URL.           * Loads a {@link AttributeMetadata} object from an URL.
309           *           *
310           * @param documentUrl           * @param documentUrl
311           *            {@link URL} to parse           *            {@link URL} to parse
312           * @see #parseAttributeMetaData(Element)           * @see #parseAttributeMetaData(Element)
313           */           */
314          public static Map<Integer, AttributeMetaData> loadAttributeMetaDataMap(          public static AttributeMetadataMap loadAttributeMetaDataMap(
315                          final URL documentUrl) throws Exception {                          final URL documentUrl) throws Exception {
316                  Document document = SAX_BUILDER.build(documentUrl);                  Document document = SAX_BUILDER.build(documentUrl);
317                  return parseAttributeMetaDataMap(document.getRootElement());                  return parseAttributeMetaDataMap(document.getRootElement());
318          }          }
319    
320          /**          /**
321           * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}
322           * object.           * object.
323           *           *
324           * @param amd           * @param amd
325           *            meta data for one attribute           *            meta data for one attribute
326           */           */
327          public static Element createAttributeMetaDataElement(          public static Element createAttributeMetaDataElement(
328                          final AttributeMetaData amd) {                          final AttributeMetadata amd) {
329                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);                  final Element element = new Element(ELEM_NAME_ATTRIBUTE, AMLURI);
330                  element.setAttribute("col", String.valueOf(amd.getColIdx()));                  element.setAttribute("namespace", String.valueOf(amd.getName().getNamespaceURI()));
331                    element.setAttribute("localname", String.valueOf(amd.getLocalName()));
332                  element.setAttribute("visible", String.valueOf(amd.isVisible()));                  element.setAttribute("visible", String.valueOf(amd.isVisible()));
333                  element.setAttribute("unit", amd.getUnit());                  element.setAttribute("unit", amd.getUnit());
334                  // Creating a aml:name tag...                  // Creating a aml:name tag...
# Line 335  public class StyledLayerUtil { Line 339  public class StyledLayerUtil {
339          }          }
340    
341          /**          /**
342           * Creates an JDOM {@link Element} for the given {@link AttributeMetaData}           * Creates an JDOM {@link Element} for the given {@link AttributeMetadata}
343           * map.           * map.
344           *           *
345           * @param amdMap           * @param amdMap
346           *            map of attribute meta data           *            map of attribute meta data
347           */           */
348          public static Element createAttributeMetaDataMapElement(          public static Element createAttributeMetaDataMapElement(
349                          final Map<Integer, AttributeMetaData> amdMap) {                          final AttributeMetadataMap amdMap) {
350                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);                  final Element element = new Element(ELEM_NAME_AMD, AMLURI);
351                  for (AttributeMetaData amd : amdMap.values())                  for (AttributeMetadata amd : amdMap.values())
352                          element.addContent(createAttributeMetaDataElement(amd));                          element.addContent(createAttributeMetaDataElement(amd));
353                  return element;                  return element;
354          }          }
355    
356          /**          /**
357           * Saves a {@link AttributeMetaData AttributeMetaData-Map} to an URL.           * Saves a {@link AttributeMetadata AttributeMetaData-Map} to an URL.
358           *           *
359           * @param amdMap           * @param amdMap
360           *            map of {@link AttributeMetaData}           *            map of {@link AttributeMetadata}
361           * @param documentUrl           * @param documentUrl
362           *            {@link URL} to store the XML           *            {@link URL} to store the XML
363           */           */
364          public static void saveAttributeMetaDataMap(          public static void saveAttributeMetaDataMap(
365                          final Map<Integer, AttributeMetaData> amdMap, final URL documentUrl)                          final AttributeMetadataMap amdMap, final URL documentUrl)
366                          throws Exception {                          throws Exception {
367                  // Create XML-Document                  // Create XML-Document
368                  final FileWriter out = new FileWriter(new File(documentUrl.toURI()));                  final FileWriter out = new FileWriter(new File(documentUrl.toURI()));
# Line 625  public class StyledLayerUtil { Line 629  public class StyledLayerUtil {
629                  }                  }
630                  if (styledObject instanceof StyledFeatureCollectionInterface                  if (styledObject instanceof StyledFeatureCollectionInterface
631                                  && (style.getMetaData() instanceof Map || style.getMetaData() == null)) {                                  && (style.getMetaData() instanceof Map || style.getMetaData() == null)) {
632                          Map<Integer, AttributeMetaData> sourceAmd = (Map<Integer, AttributeMetaData>) style                          AttributeMetadataMap sourceAmd = (AttributeMetadataMap) style
633                                          .getMetaData();                                          .getMetaData();
634                          Map<Integer, AttributeMetaData> destAmd = ((StyledFeatureCollectionInterface) styledObject)                          AttributeMetadataMap destAmd = ((StyledFeatureCollectionInterface) styledObject)
635                                          .getAttributeMetaDataMap();                                          .getAttributeMetaDataMap();
636                          if (destAmd != null && sourceAmd != null) {                          if (destAmd != null && sourceAmd != null) {
637                                  destAmd.clear();                                  destAmd.clear();
# Line 688  public class StyledLayerUtil { Line 692  public class StyledLayerUtil {
692           * @param styledFC           * @param styledFC
693           *            a styled feature collection           *            a styled feature collection
694           */           */
695          public static StyledLayerStyle<Map<Integer, AttributeMetaData>> getStyledLayerStyle(          public static StyledLayerStyle<AttributeMetadataMap> getStyledLayerStyle(
696                          StyledFeatureCollectionInterface styledFC) {                          StyledFeatureCollectionInterface styledFC) {
697                  return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(styledFC                  return new StyledLayerStyle<AttributeMetadataMap>(styledFC
698                                  .getStyle(), styledFC.getAttributeMetaDataMap());                                  .getStyle(), styledFC.getAttributeMetaDataMap());
699          }          }
700    
# Line 757  public class StyledLayerUtil { Line 761  public class StyledLayerUtil {
761          }          }
762    
763          /**          /**
764           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetaData           * Loads a {@linkplain Style SLD-Style} and a {@linkplain AttributeMetadata
765           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD           * AttributeMetaData-Map} for a given geo-object (feature) source. The SLD
766           * file must be present. A missing attribute meta-data file is tolerated.           * file must be present. A missing attribute meta-data file is tolerated.
767           *           *
# Line 769  public class StyledLayerUtil { Line 773  public class StyledLayerUtil {
773           *            file extention for the raster legend-data file           *            file extention for the raster legend-data file
774           * @return {@code null} in case of any error           * @return {@code null} in case of any error
775           */           */
776          public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle(          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(
777                          URL geoObjectURL, String sldExt, String rldExt) {                          URL geoObjectURL, String sldExt, String rldExt) {
778                  Map<Integer, AttributeMetaData> metaData = null;                  AttributeMetadataMap metaData = null;
779                  Style sldStyle = null;                  Style sldStyle = null;
780                  try {                  try {
781                          Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(                          Style[] styles = StylingUtil.loadSLD(IOUtil.changeUrlExt(
# Line 797  public class StyledLayerUtil { Line 801  public class StyledLayerUtil {
801                          return null;                          return null;
802                  }                  }
803    
804                  return new StyledLayerStyle<Map<Integer, AttributeMetaData>>(sldStyle,                  return new StyledLayerStyle<AttributeMetadataMap>(sldStyle,
805                                  metaData);                                  metaData);
806          }          }
807    
808          /**          /**
809           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and           * Loads a {@linkplain Style SLD-Style} from a {@code .sld} file and
810           * {@linkplain AttributeMetaData AttributeMetaData-Map} from a {@code .amd}           * {@linkplain AttributeMetadata AttributeMetaData-Map} from a {@code .amd}
811           * file for a given geo-object (feature) source. The SLD file must be           * file for a given geo-object (feature) source. The SLD file must be
812           * present. A missing attribute meta-data file is tolerated.           * present. A missing attribute meta-data file is tolerated.
813           *           *
# Line 815  public class StyledLayerUtil { Line 819  public class StyledLayerUtil {
819           *            file extention for the raster legend-data file           *            file extention for the raster legend-data file
820           * @return {@code null} in case of any error           * @return {@code null} in case of any error
821           */           */
822          public static StyledLayerStyle<Map<Integer, AttributeMetaData>> loadStyledFeatureStyle(          public static StyledLayerStyle<AttributeMetadataMap> loadStyledFeatureStyle(
823                          URL geoObjectURL) {                          URL geoObjectURL) {
824                  return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");                  return loadStyledFeatureStyle(geoObjectURL, "sld", "amd");
825          }          }
# Line 853  public class StyledLayerUtil { Line 857  public class StyledLayerUtil {
857                                  // KOMPILIERBAR!!                                  // KOMPILIERBAR!!
858                          } else if (metaData instanceof Map) {                          } else if (metaData instanceof Map) {
859                                  saveAttributeMetaDataMap(                                  saveAttributeMetaDataMap(
860                                                  (Map<Integer, AttributeMetaData>) metaData, IOUtil                                                  (AttributeMetadataMap) metaData, IOUtil
861                                                                  .changeUrlExt(geoObjectURL, mdExt));                                                                  .changeUrlExt(geoObjectURL, mdExt));
862                          } else                          } else
863                                  throw new UnsupportedOperationException(                                  throw new UnsupportedOperationException(
# Line 864  public class StyledLayerUtil { Line 868  public class StyledLayerUtil {
868    
869          /**          /**
870           * 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
871           * meta data ({@link RasterLegendData} or {@link AttributeMetaData}) to a           * meta data ({@link RasterLegendData} or {@link AttributeMetadata}) to a
872           * {@code .rld} or {@code .amd} file. for a given geo-object source.           * {@code .rld} or {@code .amd} file. for a given geo-object source.
873           *           *
874           * @param style           * @param style
# Line 980  public class StyledLayerUtil { Line 984  public class StyledLayerUtil {
984                                  hbox.add(iconLabel);                                  hbox.add(iconLabel);
985                                  hbox.add(Box.createHorizontalStrut(3));                                  hbox.add(Box.createHorizontalStrut(3));
986    
                                 // ****************************************************************************  
                                 // The labeltext is read from the SLD. Its either the title  
                                 // directly, or interpreted as OneLine Code  
                                 // ****************************************************************************  
                                 // final String rawText =  
                                 // rule.getDescription().getTitle().toString();  
                                 final String rawText = rule.getDescription().getTitle()  
                                                 .toString();  
   
987                                  Translation labelT = new Translation();                                  Translation labelT = new Translation();
988                                  labelT.fromOneLine(rawText);                                  labelT.fromOneLine(rule.getDescription().getTitle());
989    
990                                  final JLabel classTitleLabel = new JLabel(labelT.toString());                                  final JLabel classTitleLabel = new JLabel(labelT.toString());
991                                  hbox.add(classTitleLabel);                                  hbox.add(classTitleLabel);
# Line 1118  public class StyledLayerUtil { Line 1113  public class StyledLayerUtil {
1113                                          graphics.setColor(color);                                          graphics.setColor(color);
1114                                          graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height);                                          graphics.fillRect(0, 0, ICON_SIZE.width, ICON_SIZE.height);
1115                                  } catch (Exception e) {                                  } catch (Exception e) {
1116                                          LOGGER.debug("Dann nehmen wir halt den GridCoverageRenderer", e);                                          LOGGER.debug(
1117                                                            "Dann nehmen wir halt den GridCoverageRenderer", e);
1118                                          colorModel = null;                                          colorModel = null;
1119                                  }                                  }
1120                          }                          }
# Line 1197  public class StyledLayerUtil { Line 1193  public class StyledLayerUtil {
1193           * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May           * Extracts the {@link ColorModel} of any {@link StyledRasterInterface}. May
1194           * return <code>null</code> if the geoobject can not be accessed.           * return <code>null</code> if the geoobject can not be accessed.
1195           */           */
1196            @SuppressWarnings("unchecked")
1197          public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) {          public static ColorModel getColorModel(StyledRasterInterface<?> styledGrid) {
1198                  ColorModel colorModel = null;                  ColorModel colorModel = null;
1199                  try {                  try {
# Line 1204  public class StyledLayerUtil { Line 1201  public class StyledLayerUtil {
1201                          if (geoObject instanceof GridCoverage2D) {                          if (geoObject instanceof GridCoverage2D) {
1202                                  GridCoverage2D cov = (GridCoverage2D) geoObject;                                  GridCoverage2D cov = (GridCoverage2D) geoObject;
1203                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1204                          } else if (geoObject instanceof ImagePyramidReader) {                          } else if (styledGrid instanceof StyledRasterPyramidInterface) {
1205    
1206                                  Parameter readGG = new Parameter(                                  Parameter readGG = new Parameter(
1207                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);                                                  AbstractGridFormat.READ_GRIDGEOMETRY2D);
# Line 1213  public class StyledLayerUtil { Line 1210  public class StyledLayerUtil {
1210                                                  styledGrid.getEnvelope(), styledGrid.getCrs());                                                  styledGrid.getEnvelope(), styledGrid.getCrs());
1211    
1212                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(                                  readGG.setValue(new GridGeometry2D(new GeneralGridEnvelope(
1213                                                  new Rectangle(0, 0, 10, 10)), mapExtend));                                                  new Rectangle(0, 0, 1, 1)), mapExtend));
1214    
1215                                    FeatureCollection<SimpleFeatureType, SimpleFeature> rFc = (FeatureCollection<SimpleFeatureType, SimpleFeature>) geoObject;
1216    
1217                                  final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) geoObject;                                  final AbstractGridCoverage2DReader aReader = (AbstractGridCoverage2DReader) FeatureUtil
1218                                                    .getWrappedGeoObject(rFc);
1219                                  GridCoverage2D cov = (GridCoverage2D) aReader                                  GridCoverage2D cov = (GridCoverage2D) aReader
1220                                                  .read(new GeneralParameterValue[] { readGG });                                                  .read(new GeneralParameterValue[] { readGG });
1221                                  colorModel = cov.getRenderedImage().getColorModel();                                  colorModel = cov.getRenderedImage().getColorModel();
1222                          }                          }
1223                  } catch (Exception e) {                  } catch (Exception e) {
1224                          LOGGER.error("Error reading the colormodel from " + styledGrid);                          LOGGER.error("Error reading the colormodel from " + styledGrid, e);
1225                          return null;                          return null;
1226                  }                  }
1227                  return colorModel;                  return colorModel;

Legend:
Removed from v.406  
changed lines
  Added in v.464

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26