Skip to content

techbrew-mc/geogson

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GeoJSON support for Gson

This library provide a minimal support to parse and write geo spatial entities using the well known GeoJSON standard. I started write it because seems that there are no extension to Gson available to support GeoJSON format, and it is pretty common now across the new social application API.

If you are not familiar with the Gson library take a look at the Gson project page.

If you are not familiar with the GeoJSON format, please take a look at the The GeoJSON specification.

The Travis-CI build status is: The Build Status

Download the last version from bintray: Download

Quick Start

How to use the GeoGson in few easy steps.

Add the Maven dependency to your pom.xml

Add the following statement to your pom.xml.

<dependency>
   <groupId>com.github.filosganga</groupId>
   <artifactId>geogson-core</artifactId>
   <version>1.1.72</version>
</dependency

Register the TypeAdapterFactory with Gson

Use the GsonBuilder to register the GeometryAdapterFactory supplied.

Gson gson = new GsonBuilder()
   .registerTypeAdapterFactory(new GeometryAdapterFactory())
   .create();

Serialize and de-serialize with Gson

Now your Gson instance is able to parse and write any Geometry instance using the GeoJSON format.

String json = "{\"type\":\"Point\",\"coordinates\": [23.5,20.125]}";

Point point = gson.fromJson(json, Point.class);

String json = gson.toJson(point);

Geometry geometry = gson.fromJson(json); // It will be an instance of Point.

Additional modules:

There are currently only one additional module for GeoGSON, the Java Topology Suite (JTS) support.

If you are not familiar with the JTS library take a look at the JTS Home Page

JTS support

To enable the JTS support, you need declare the geogson-jts dependency and register the JtsAdapterFactory as well.

In pom.xml:

<dependency>
   <groupId>com.github.filosganga</groupId>
   <artifactId>geogson-jts</artifactId>
   <version>1.1.72</version>
</dependency

The Gson building code:

Gson gson = new GsonBuilder()
   .registerTypeAdapterFactory(new JtsAdapterFactory())
   .registerTypeAdapterFactory(new GeometryAdapterFactory())
   .create();

Please note that you need the GeometryAdapterFactory anyway. The JTS support is a thin layer on top of the native geometry domain.

You can optionally configure GeometryFactory as well. In this example all GeoJSON geometries are parsed as JTS geometries in WGS84 in centimeter precision:

Gson gson = new GsonBuilder()
   .registerTypeAdapterFactory(new JtsAdapterFactory(
       new GeometryFactory(new PrecisionModel(100), 4326)))
   .registerTypeAdapterFactory(new GeometryAdapterFactory())
   .create();

Enjoy beer with your friends, and buy me one!

About

GeoJSON support for Google gson library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.8%
  • Shell 0.2%