Generating map tiles without a map server. GeoTools the GIS swissknife.

Description

Recently I was playing with latest version of GeoServer. It includes the GeoWebCache, something which can improve your server performance greatly. GeoServer solves and helps lots of problems to work and visualize geospatial data but as you know map servers lakes from scalability.

Because this GeoWebCache is a great tool. Basically, each map request is processed and the result image is stored to be directly returned on subsequent requests. The processed images are stored as a pyramid of tiles depending on the bounding box or zoom level of the request.

Another way to solve scalability problems is directly pre-generate the pyramid of tiles, something that makes Google, Bing, Yahoo, OpenStreetMaps, etc.

What I need?

I have a lightnings database with thousands of lightnings for a period of some months. I need to show lightnings in my maps but only those corresponding to a period or interval of time. For example, render the lightnings from 00:00h to 00:30h and allow the user to go forward or backward in time.

One important thing is I only need an “image” with the information in that period. I don’t need to render each lightning as a feature in the map -this will degrade the performance rendering in a storm with thousands of lightnings per period.

The problem

So why don’t use GeoServer+GeoWebCache for this? I can configure a layer pointing to my lightning database, make requests and rest assured subsequent call will get the previously created map.

The problem is at this moment -while I write this post- GeoServer lakes from TIME support in requests. That means if I define a layer from my lightnings table on DB, every GeoServer request will work against all data while I only need a subset of my data -determined by an interval- to be rendered in the requested maps.

Adopted solution

Ok, be quiet. GeoServer is build on top of GeoTools, an open source Java library which provides standards compliant methods for the manipulation of geospatial data and, more important, GeoTools library implements Open Geospatial Consortium (OGC) specifications as they are developed.

With all this the solution seems easy: code a program to query the desired period of lightning data and generate a pyramid of tiles (for the desired levels).

 A brief description of the implementation

Next is a brief summary of things to do, or take into account, to generate your own pyramid of tiles programmatically with Geotools.

All the lightnings information is stored on a PostgreSQL/PostGIS table called ‘lightnings’. Data related with a lightning are: date (the UTC instant in which the lightning occurs, represented as a long number in Unix time), position (latitude/longitude/altitude), value and sign (the electric charge).

Set the DataSource connection

GeoTools tries to simplify thing and because this it tries to abstracts as much as possible. Features can be provided from many source: files (shapefiles, GML, …) or a database (PostgreSQL/PostGIS, Oracle, …).

The first step then is to set a DataSource instance pointing to our database:

Note: ‘dbconn’ is an object which stores my DB connection parameters.

Filtering data

We don’t want to get all the lightnings in the database but only those withing a period of tim, so what we need is to filter the data using Filter classes. Given a period of time represented by values ‘long start_date’ and ‘long end_date’ we can define the desired filter as:

Create styles before rendering for features

There are some ways to create styles for our features. One is to use a SLD document and the other is doing programmatically.

In my case, I chose to use the second form so here is a bit of cumbersome code (I ommited the try/catch section) which create the desired style to identify positive and negative lightnings.

Create the map and render to a file

The map creating is straightforward:

and then render it to a file. I will paste here the code on the GTRenderer tutorial. You can play a bit with the code and change some values: area of interest, size of the output image, etc.

Conclusions

Every tool is designed and built with a goal in mind, because this normally the use of a map server is always the proper selection. But sometimes you have specific needs that general tools doesn’t solve and here is when open source project like GeoTools can help you.

I would to note that programing this way the image generation is much more faster than use of GeoServer because we are avoiding lots of intermediate steps a map server does: get request, parse, check and validate parameters and once query is executed, image composed it must be returned to the client via HTTP protocol.

References

http://docs.geotools.org

http://docs.geotools.org/latest/userguide/library/render/gtrenderer.html

http://docs.geotools.org/stable/tutorials/filter/query.html

 

2 Responses

  1. asantiago July 25, 2011 / 21:10

    Thanks for your comment Andrea,
    yes it can help a lot in many situations. I need to play with it a bit to compare against the code I created to generate a TMS pyramid.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">