Code Monkey home page Code Monkey logo

geogson's Introduction

GeoJSON support for Gson

CircleCI Codacy Badge Download

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.

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.2.21</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.2.21</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!

geogson's People

Contributors

filosganga avatar simon04 avatar qt-jvmakine avatar csabapalfi avatar themowski avatar sethchampagnenrl avatar smirnovegorv avatar rockaport avatar willtemperley avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.