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 filesand 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 easyin 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.