A vegetation map in OpenLayers

For a project a few years ago we created a vegetation map for central and southwest Kenya (see http://ecodiv.org/trapnell for more information). The map together with documentation was made available on CD-ROM. However, we wanted to publish the map online too. The original maps were in svg format, and turned out to be too slow for online publishing. Moreover, they could not be combined easily with other maps.

A widely used and open source option for online publishing of maps is OpenLayers. This is a JavaScript library for displaying and building rich web-based geographic applications, similar to what is offered by e.g., Google Maps and MSN Virtual Earth. An important difference though is that OpenLayers is open source software. It furthermore is easy to create dynamic maps on your website with your own data and with direct access to many different maps, including Google, Yahoo, Bing, OpenStreetMap and other datalayers.

As a pilot I have created an OpenLayer map with the vegetation map.It gives a simple (semi-transparent) overlay of the vegetation map on an OpenStreetMap data layer. It offers the option to select/deselect the different vegetation types. Clicking on the map will give you a pop-up with the name of the vegetation type.

A vegetation map of central and southwest Kenya in OpenLayers

A vegetation map of central and southwest Kenya in OpenLayers. Click on image for larger version.

The vegetation map in this version is a GML vector layer. Because of the high detail and complexity of the layers (and therefore the size of the files) rendering and displaying the maps is very slow. I therefore simplified the map by reducing all areas smaller than 200 hectares (see below). Displaying the map remains fairly slow and ideally these maps should therefore be made available through a webserver such as Geoserver or Mapserver.

I won’t go in all details of creating the map, but see below for a few notes about steps I had to follow before creating the OpenLayer online map.

Using the right projection

Although OpenLayers can reproject data on the fly, the easiest way to get things running is to reproject your data layers in the same projection used by OpenLayers, viz. the spherical mercator projection (EPGS 900913). To do this in GRASS you need the following:

First I included the line below to the proj4 data definition file (on linux this is normally /usr/share/proj/epsg).

<900913> +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs

Next I created a new location OpenLayer in my GRASS database using the projection above (in GRASS on command line):

> g.proj -c epsg=900913 location=Openlayer

Reprojecting the original vegetation map (which is in the WGS84 projection, EPSG 4326) is as simple as copying the original layer into the newly created mapset using the command given below:

> v.proj input=trapnell location=latlon mapset=vegetation dbase=/home/pbreugel/Data/GRASSdb output=trapnel

Simplifying the map

As mentioned earlier, the Trapnell map is very detailed and very slow to render on the web. I therefore simplified the map by removing areas smaller than 200 hectares and pruning all vertices with a distance of less than 200 meter (just to give you an idea, this reduced the number of vertices with 70%). In GRASS GIS this is done with the following commands:

> v.clean input=trapnel1a@PERMANENT output=trapnel1b tool=prune thresh=200

> v.clean input=trapnel1b@PERMANENT output=trapnel1 tool=rmarea thresh=2000000

Splitting the vegetation map in one map per vegetation type

For the OpenLayer map I wanted to have one layer per vegetation type. I thus needed to split the vector layer creating one vector map per vegetation type. See this post for tips how to do this in GRASS or QGIS.

Next step is to export these layers as GML files. To do this I added to following to the R script given in the above-mentioned post:

# Export the created vector maps as gml files. Only features with category will be exported
for(i in 1:length(namelist1){
param <- list(input=namelist2[i], type="area", dsn=paste(outputdir,namelist2[i], ".gml", sep=""), format="GML")
flags <- c("e","c")
execGRASS("v.out.ogr", parameters=param, flags=flags)
}

The OpenLayer map

OK, now I had the vector layers. But how to combine them in one OpenLayers map? A good starting point is the OpenLayer page with examples. Just check out the page source of the different examples and try to adapt them to your needs. What I did was creating an OpenLayers map using the QGIS OGR2Layers plugin as template, but with a number of adaptations to suit my needs. Main adaptations I made were the inclusion of different vector layers (one vector map per vegetation type) and adaptation of the query option to enable querying multiple vector layers in once. Check out the map and the html source here. Note however that for now this map only works in Firefox and Chrome due to some bug which I haven’t been able to trace yet.

A vegetation map of central and southwest Kenya in OpenLayers.

A vegetation map of central and southwest Kenya in OpenLayers. Click on the image for larger view. Go to http://ecodiv.org/trapnell for the actual map.

About these ads

About pvanb

I am a tropical forest ecologist with a focus on spatial and temporal patterns and processes at population and ecosystem level. I am furthermore very interested in issues related to conservation and sustainable use of biodiversity and natural resources under current and future climates. I have worked in the Middle East (Syria and Lebanon) and South America (Brazil) and in Eastern Africa (Kenya).
This entry was posted in GIS, GRASS GIS, online mapping, vegetation and tagged , , , , . Bookmark the permalink.

2 Responses to A vegetation map in OpenLayers

  1. Pingback: MapTiler to create online maps « Ecostudies

  2. Wilfo says:

    Interesting map…i have problems with my re.projection from qgis to openlayers…I can not see anything on my web only openlayer pan,zoom but no map..I know that my projection could be my error but how do i reproject my shape file?
    Pease i, it’s urgent…thanks guys…
    logica_razon@hotmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s