commit | c0b008629f2661d187ec6a78c6b7e9e34aad47f1 | [log] [tgz] |
---|---|---|
author | Andy Flatt <Andy.Flatt@baesystems.com> | Wed Apr 29 16:20:09 2015 +0100 |
committer | Andy Flatt <Andy.Flatt@baesystems.com> | Wed Apr 29 16:20:09 2015 +0100 |
tree | 8fb5e1ad0809c37952fe5244243662118d946a5d | |
parent | 58af8b28f0c4ec7c2b77c2c7f549ad125c7ca52c [diff] |
Make GEOJSON objects serialisable so that they can be tranferred between JVMS. In my example I am using JMS
A small package of all GeoJson POJOs (Plain Old Java Objects) for serializing and deserializing of objects via JSON Jackson Parser.
If you know what kind of object you expect from a GeoJson file you can directly read it like this:
FeatureCollection featureCollection = new ObjectMapper().readValue(inputStream, FeatureCollection.class);
If you what to read any GeoJson file read the value as GeoJsonObject and then test for the contents via instanceOf:
GeoJsonObject object = new ObjectMapper().readValue(inputStream, GeoJsonObject.class); if (object instanceOf Polygon) { ... } else if (object instanceOf Feature) { ... }
and so on.
Or you can use the GeoJsonObjectVisitor to visit the right method:
GeoJsonObject object = new ObjectMapper().readValue(inputStream, GeoJsonObject.class); object.accept(visitor);
Writing Json is even easier. You just have to create the GeoJson objects and pass them to the Jackson ObjectMapper.
FeatureCollection featureCollection = new FeatureCollection(); featureCollection.add(new Feature()); String json= new ObjectMapper().writeValueAsString(featureCollection);
You can find the library in the Maven Central Repository.
<dependency> <groupId>de.grundid.opendatalab</groupId> <artifactId>geojson-jackson</artifactId> <version>1.3</version> </dependency>