commit | e3974e3873c0aa63338487b1ec90ff96ba789dc7 | [log] [tgz] |
---|---|---|
author | Eric Arseneau <earseneau@google.com> | Mon Dec 06 14:36:23 2021 -0800 |
committer | Eric Arseneau <earseneau@google.com> | Mon Dec 06 14:36:23 2021 -0800 |
tree | 5520f8b348fa75934708b0b968252192d513274b | |
parent | e2fd265ebd009f4a658d64bd56c83419b0b48a0b [diff] | |
parent | d499e0d79582bc26b8a56f2e34c36219055d1d22 [diff] |
Merge mpr-2021-11-05 Change-Id: I8c60eb0c991831b1252fc9ac118f19484fe4b366
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 want 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.8.1</version> </dependency>