[GRASS-user] Raster kml export script?

David Finlayson david.p.finlayson at gmail.com
Thu Jan 3 18:39:59 EST 2008


I should add a little explaination:

The script is designed to export a raster to Google Earth. You must
run the script from a Lat-Long projection with WGS84 datum (the
projection of Google Earth). Eventually, it will include some map
decorations like a legend, but that isn't coded up yet.

David



On Jan 3, 2008 3:36 PM, David Finlayson <david.p.finlayson at gmail.com> wrote:
> I have attached a script that almost works.
>
> The problem I am having is that I can't create a PNG file EXACTLY the
> same size as the region. I know I could export the raster as a GeoTiff
> and get an accurate bounding box, but I would lose transparency.
>
> Can anyone give me a hand in fixing the bounding box of the PNG to be
> the same size as the region?
>
> Here is the script
>
> >>>> script starts <<<<
>
> #!/bin/bash
> ############################################################################
> #
> # MODULE:       r.out.kml
> # AUTHOR(S):    David Finlayson, 2008
> #               david.p.finlayson at gmail.com
> #
> # PURPOSE:          Create a skeleton kml file from a raster map.
> #
> # COPYRIGHT:    (C) 2007 by the GRASS Development Team
> #
> #               This program is free software under the GNU General Public
> #               License (>=v2). Read the file COPYING that comes with GRASS
> #               for details.
> #
> #############################################################################
> #
> # January 2008 - First version of the script posted to GRASS
>
> #%Module
> #%  description: creates a kml file from a GRASS raster
> #%End
> #%flag
> #%  key: l
> #%  description: add legend icon to kml
> #%end
> #%option
> #% key: input
> #% type: string
> #% gisprompt: old,cell,raster
> #% description: Name of input raster
> #% required : yes
> #%end
> #%option
> #% key: output
> #% type: string
> #% description: Name of output kml file
> #% required : yes
> #%end
>
> # Constants (hard to guess what will work best)
> LEGEND_WIDTH=500
> LEGEND_HEIGHT=500
>
> # Check that we are in GRASS
> if [ -z "$GISBASE" ] ; then
>     echo "You must be in GRASS GIS to run this program." 1>&2
>     exit 1
> fi
>
> # Run g.parser
> if [ "$1" != "@ARGS_PARSED@" ] ; then
>   PARSER=`which g.parser`
>   exec $PARSER "$0" "$@"
> fi
>
> # Check that this is a geographic projection (required by Google Earth)
> PROJECTION=`g.proj -p | grep "name" | sed "s/\ //g" | cut -c6-`
> if [ "$PROJECTION" != "Latitude-Longitude" ] ; then
>     echo "r.out.kml: error: export only works from a
> Latitude-Longitude location"
>     exit 1
> fi
>
> # Check that this is a WGS84 datum
> DATUM=`g.proj -d | grep "datum code: wgs84" | cut -d: -f2`
> if [ "$DATUM" != " wgs84" ] ; then
>     echo "r.out.kml: error: export only works from a WGS84 datum location"
>     exit 1
> fi
>
> # Store current environment
> g.region rast=$GIS_OPT_INPUT
> OLD_GRASS_WIDTH=$GRASS_WIDTH
> OLD_GRASS_HEIGHT=$GRASS_HEIGHT
> OLD_GRASS_PNGFILE=$GRASS_PNGFILE
> OLD_GRASS_TRANSPARENT=$GRASS_TRANSPARENT
> OLD_GRASS_TRUECOLOR=$GRASS_TRUECOLOR
>
> # define the driver settings
> export GRASS_WIDTH=`g.region -g | grep "cols" | cut -d= -f2`
> export GRASS_HEIGHT=`g.region -g | grep "rows" | cut -d= -f2`
> export GRASS_PNGFILE=${GIS_OPT_OUTPUT}.png
> export GRASS_TRANSPARENT=TRUE
> export GRASS_TRUECOLOR=TRUE
>
> # draw the raster
> if [ -n "$GIS_OPT_INPUT" ] ; then
>     d.mon start=PNG
>     d.mon select=PNG
>     d.rast -o $GIS_OPT_INPUT
>     d.mon stop=PNG
> fi
>
> # draw the legend (if requested)
> if [ $GIS_FLAG_L -eq 1 ] ; then
>     export GRASS_WIDTH=$LEGEND_WIDTH
>     export GRASS_HEIGHT=$LEGEND_HEIGHT
>     export GRASS_PNGFILE=${GIS_OPT_OUTPUT}_legend.png
>     d.mon start=PNG
>     d.mon select=PNG
>     d.legend -s map=$GIS_OPT_INPUT
>     d.mon stop=PNG
> fi
>
> # Create the kml file
> TITLE=`r.info -m $GIS_OPT_INPUT | cut -d= -f2`
> NORTH=`g.region -g | grep -m1 "n=" | cut -d= -f2`
> SOUTH=`g.region -g | grep -m1 "s=" | cut -d= -f2`
> EAST=`g.region -g | grep -m1 "e=" | cut -d= -f2`
> WEST=`g.region -g | grep -m1 "w=" | cut -d= -f2`
>
> echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > ${GIS_OPT_OUTPUT}.kml
> echo "<kml xmlns=\"http://earth.google.com/kml/2.2\">" >> ${GIS_OPT_OUTPUT}.kml
> echo "<Folder>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<name>$TITLE</name>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<description>$TITLE</description>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<GroundOverlay>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<name>$TITLE</name>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<description>Enter Description Here</description>" >>
> ${GIS_OPT_OUTPUT}.kml
> echo "<Icon>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<href>${GIS_OPT_OUTPUT}.png</href>" >> ${GIS_OPT_OUTPUT}.kml
> echo "</Icon>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<LatLonBox>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<north>$NORTH</north>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<south>$SOUTH</south>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<east>$EAST</east>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<west>$WEST</west>" >> ${GIS_OPT_OUTPUT}.kml
> echo "<rotation>0.0</rotation>" >> ${GIS_OPT_OUTPUT}.kml
> echo "</LatLonBox>"  >> ${GIS_OPT_OUTPUT}.kml
> echo "</GroundOverlay>"  >> ${GIS_OPT_OUTPUT}.kml
> echo "</Folder>"  >> ${GIS_OPT_OUTPUT}.kml
> echo "</kml>"  >> ${GIS_OPT_OUTPUT}.kml
>
> # Restore the environment
> export GRASS_WIDTH=$OLD_GRASS_WIDTH
> export GRASS_HEIGHT=$OLD_GRASS_HEIGHT
> export GRASS_PNGFILE=$OLD_GRASS_PNGFILE
> export GRASS_TRANSPARENT=$OLD_GRASS_TRANSPARENT
> export GRASS_TRUECOLOR=$OLD_GRASS_TRUECOLOR
>
> >>>> script ends <<<<
>
>
> On Dec 30, 2007 7:12 AM, Markus Neteler <neteler at osgeo.org> wrote:
> > On Dec 22, 2007 6:48 AM, David Finlayson <david.p.finlayson at gmail.com> wrote:
> > > Before I write one myself, I was wondering if anyone has a handy
> > > export script to convert a GRASS raster to kml?
> >
> > I would be also interested.
> >
> > > By the way, what happened to all of the scripts on the Wiki? There
> > > used to be a page with several good scripts there.
> >
> > Several URLs fixed:
> > http://grass.gdf-hannover.de/wiki/GRASS_AddOns
> >
> > Markus
> > _______________________________________________
> > grass-user mailing list
> > grass-user at lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/grass-user
> >
>
>
>
> --
> David Finlayson, Ph.D.
> Operational Geologist
>
> U.S. Geological Survey
> Pacific Science Center
> 400 Natural Bridges Drive
> Santa Cruz, CA  95060, USA
>
> Tel: 831-427-4757, Fax: 831-427-4748, E-mail: dfinlayson at usgs.gov
>



-- 
David Finlayson, Ph.D.
Operational Geologist

U.S. Geological Survey
Pacific Science Center
400 Natural Bridges Drive
Santa Cruz, CA  95060, USA

Tel: 831-427-4757, Fax: 831-427-4748, E-mail: dfinlayson at usgs.gov


More information about the grass-user mailing list