[GRASS-user] Raster kml export script?

Before I write one myself, I was wondering if anyone has a handy
export script to convert a GRASS raster to kml?

By the way, what happened to all of the scripts on the Wiki? There
used to be a page with several good scripts there.

--
David

On Dec 22, 2007 6:48 AM, David Finlayson <david.p.finlayson@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

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@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\\&quot;&gt;&quot; >> ${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@osgeo.org> wrote:

On Dec 22, 2007 6:48 AM, David Finlayson <david.p.finlayson@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@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@usgs.gov

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@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@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\\&quot;&gt;&quot; >> ${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@osgeo.org> wrote:
> On Dec 22, 2007 6:48 AM, David Finlayson <david.p.finlayson@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@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@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@usgs.gov

Here is a link to my web site with the latest version:

http://david.p.finlayson.googlepages.com/grasstogoogleearth

I have something that works for me, but I was forced to use image
magick to clip the nodata collar off of the exported png file. If I
can figure out how to get a one-to-one correspondence between raster
cells and image pixels, I fix it again.

David

That's a script with great utility although I have not tried it yet. Thanks for making this as I'm sure it will be popular. (At least it should be!)

John

On Jan 4, 2008, at 5:23 PM, David Finlayson wrote:

Here is a link to my web site with the latest version:

http://david.p.finlayson.googlepages.com/grasstogoogleearth

I have something that works for me, but I was forced to use image
magick to clip the nodata collar off of the exported png file. If I
can figure out how to get a one-to-one correspondence between raster
cells and image pixels, I fix it again.

David
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

On 05/01/08 02:23, David Finlayson wrote:

Here is a link to my web site with the latest version:

http://david.p.finlayson.googlepages.com/grasstogoogleearth

I have something that works for me, but I was forced to use image
magick to clip the nodata collar off of the exported png file. If I
can figure out how to get a one-to-one correspondence between raster
cells and image pixels, I fix it again.

Have you checked out gdal2tiles [1]? I have no idea if this is in anyway close to what you are trying to do, but maybe there are some ideas in there.

Also CC'ing Rainer as this might also be something useful for him.

Moritz

[1] http://www.gdal.org/gdal2tiles.html

I don't know how to avoid the use of convert... I've found my way to
produce georeferenced transparent images:

export the raster with r.out.png, create the pgw world file
(extracting the values from r.info), and convert the outpu.png with
"convert -transparent white output.png output_transp.png"

I works fine for me.

2008/1/5, Moritz Lennert <mlennert@club.worldonline.be>:

On 05/01/08 02:23, David Finlayson wrote:
> Here is a link to my web site with the latest version:
>
> http://david.p.finlayson.googlepages.com/grasstogoogleearth
>
> I have something that works for me, but I was forced to use image
> magick to clip the nodata collar off of the exported png file. If I
> can figure out how to get a one-to-one correspondence between raster
> cells and image pixels, I fix it again.

Have you checked out gdal2tiles [1]? I have no idea if this is in anyway
close to what you are trying to do, but maybe there are some ideas in there.

Also CC'ing Rainer as this might also be something useful for him.

Moritz

[1] http://www.gdal.org/gdal2tiles.html
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Obviously, before exporting with r.out.png you have to set ther region
with the input raster.

2008/1/6, G. Allegri <giohappy@gmail.com>:

I don't know how to avoid the use of convert... I've found my way to
produce georeferenced transparent images:

export the raster with r.out.png, create the pgw world file
(extracting the values from r.info), and convert the outpu.png with
"convert -transparent white output.png output_transp.png"

I works fine for me.

2008/1/5, Moritz Lennert <mlennert@club.worldonline.be>:
> On 05/01/08 02:23, David Finlayson wrote:
> > Here is a link to my web site with the latest version:
> >
> > http://david.p.finlayson.googlepages.com/grasstogoogleearth
> >
> > I have something that works for me, but I was forced to use image
> > magick to clip the nodata collar off of the exported png file. If I
> > can figure out how to get a one-to-one correspondence between raster
> > cells and image pixels, I fix it again.
>
> Have you checked out gdal2tiles [1]? I have no idea if this is in anyway
> close to what you are trying to do, but maybe there are some ideas in there.
>
> Also CC'ing Rainer as this might also be something useful for him.
>
> Moritz
>
> [1] http://www.gdal.org/gdal2tiles.html
> _______________________________________________
> grass-user mailing list
> grass-user@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
>