[GRASS-SVN] r49118 - in grass-addons/raster: . r.colors.out_sld

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Nov 6 00:04:48 EDT 2011


Author: hamish
Date: 2011-11-05 21:04:48 -0700 (Sat, 05 Nov 2011)
New Revision: 49118

Added:
   grass-addons/raster/r.colors.out_sld/
   grass-addons/raster/r.colors.out_sld/Makefile
   grass-addons/raster/r.colors.out_sld/r.colors.out_sld
Log:
prototype script to output grass color rules as an OGC SLD file

Copied: grass-addons/raster/r.colors.out_sld/Makefile (from rev 48206, grass-addons/raster/r.colors.out_vtk/Makefile)
===================================================================
--- grass-addons/raster/r.colors.out_sld/Makefile	                        (rev 0)
+++ grass-addons/raster/r.colors.out_sld/Makefile	2011-11-06 04:04:48 UTC (rev 49118)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.colors.out_sld
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/raster/r.colors.out_sld/r.colors.out_sld
===================================================================
--- grass-addons/raster/r.colors.out_sld/r.colors.out_sld	                        (rev 0)
+++ grass-addons/raster/r.colors.out_sld/r.colors.out_sld	2011-11-06 04:04:48 UTC (rev 49118)
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+############################################################################
+#
+# MODULE:       r.colors.out_sld
+# AUTHOR(S):    Hamish Bowman
+# PURPOSE:      Export GRASS raster color table to OGC SLD template v1.0.0
+# COPYRIGHT:    (C) 2011 by Hamish Bowman, and the GRASS Development Team
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+# Based on hints from
+#  http://docs.geoserver.org/latest/en/user/styling/sld-cookbook/rasters.html
+#
+############################################################################
+#%Module
+#% description: Exports the color table associated with a raster map layer in SLD format.
+#% keywords: raster, export, color table
+#%End
+#%Option
+#% key: map
+#% type: string
+#% required: yes
+#% multiple: no
+#% key_desc: name
+#% description: Name of input raster map
+#% gisprompt: old,cell,raster
+#%End
+#%Option
+#% key: output
+#% type: string
+#% required: no
+#% multiple: no
+#% key_desc: filename
+#% label: Name for output SLD rules file
+#% description: "-" to write to stdout
+#% answer: -
+#% gisprompt: new_file,file,output
+#%End
+
+if [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." 1>&2
+    exit 1
+fi
+
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+
+#### check if we have awk
+if [ ! -x "`which awk`" ] ; then
+    g.message -e "awk required, please install awk or gawk first"
+    exit 1
+fi
+
+# set environment so that awk works properly in all locales
+unset LC_ALL
+LC_NUMERIC=C
+export LC_NUMERIC
+
+if [ "$GIS_OPT_OUTPUT" != "-" ] && [ -e "$GIS_OPT_OUTPUT" ] ; then
+   if [ -z "$GRASS_OVERWRITE" ] || [ "$GRASS_OVERWRITE" -ne 1 ] ; then
+      g.message -e "Output file already exists" 
+      exit 1
+   fi
+fi
+
+
+TEMPFILE="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
+    g.message -e "unable to create temporary files"
+    exit 1
+fi
+
+# map title:
+title=`r.info -m "$GIS_OPT_MAP" | cut -f2- -d=`
+
+#write file header
+cat << EOF > "$TEMPFILE"
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<StyledLayerDescriptor version="1.0.0" 
+    xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" 
+    xmlns="http://www.opengis.net/sld" 
+    xmlns:ogc="http://www.opengis.net/ogc" 
+    xmlns:xlink="http://www.w3.org/1999/xlink" 
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+  <NamedLayer>
+    <Name>$GIS_OPT_MAP: $title</Name>
+    <UserStyle>
+      <Title>GRASS GIS color rules</Title>
+      <FeatureTypeStyle>
+        <Rule>
+          <RasterSymbolizer>
+            <ColorMap>
+EOF
+
+
+r.colors.out "$GIS_OPT_MAP" | sort -n | tr ':' ' ' | awk \
+  '$1!="nv" && $1!="default" {
+     printf("              <ColorMapEntry color=\"#%02X%02X%02X\" quantity=\"%s\" />\n", $2, $3, $4, $1)
+  }' \
+  >> "$TEMPFILE"
+
+#how to handle special rules?
+# nv -> ?? (set opactity="0" ?)
+# default -> fallbackValue="#aabbcc" ?
+
+
+#write file footer
+cat << EOF >> "$TEMPFILE"
+            </ColorMap>
+          </RasterSymbolizer>
+        </Rule>
+      </FeatureTypeStyle>
+    </UserStyle>
+  </NamedLayer>
+</StyledLayerDescriptor>
+EOF
+
+
+
+# labels?
+# CELL, FCELL, or DCELL?
+eval `r.info -t "$GIS_OPT_MAP"`
+#if [ "$datatype" = "CELL" ] ; then
+#  r.category "$GIS_OPT_MAP"
+#  1	interstate
+#  2	primary highway, hard surface
+#  3	secondary highway, hard surface
+#  4	light-duty road, improved surface
+#  5	unimproved road
+
+
+
+if [ "$GIS_OPT_OUTPUT" = "-" ] ; then
+   cat "$TEMPFILE"
+   rm -f "$TEMPFILE"
+else
+   mv "$TEMPFILE" "$GIS_OPT_OUTPUT"
+fi
+


Property changes on: grass-addons/raster/r.colors.out_sld/r.colors.out_sld
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-sh
Added: svn:eol-style
   + native



More information about the grass-commit mailing list