Sunday, June 12, 2011

Visualizing GIS data in JavaFX 2.0 beta using GeoTools

Geographic data mostly comprises of polygon coordinates sets along with attributes, like country or city name, etc. This is quite easy to visualize in JavaFX, which supports rendering for SVG paths.
In the article, I show how to read such GIS data from ESRI type database files using open source library GeoTools.
The data itself comes for free from www.naturalearthdata.com.
Sample code can be found here: Browse on GitHub.



GIS data usually comes in form of SHP and DBF files. In order to read it, we use GeoTools parser. Following code iterates over so called "features" from within data files and retrieves name attribute and shape geometry.


Next, we need to create JavaFX polygons for each feature from iteration. Small note here. Each feature may comprise of multiple polygons. For example "United States" shape may contain separate polygon for Alaska. So we need additional loop to generate such polygons.
In order to create a polygon in JavaFX, we use Path class along with MoveTo and LineTo path elements. Following snippet does the job.


The remaining part is to implement zoom and panning functionality. This is fairly easy in JavaFX. We can use translate and scale properties from main Group shape. Panning functionality is handled using following snippet:


Zoom is coded this way:


That's it. Now we have basic GIS data viewer in JavaFX 2.

5 comments:

  1. Yes :-), I was surprised myself it's so easy these days.

    ReplyDelete
  2. Hi Rafal,
    I was using canvas instead of scene, as far as i can see it's more convenient to geotools.
    I am trying to parse kml in order to put png to map with no success. Can we talk somewhere private if you are willing to help? I have so much questions and almost noone to ask.
    Thank you

    ReplyDelete
  3. You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things


    online idea lab

    ReplyDelete