/jts2geojson-vivid

JTS from/to GeoJSON converter for Java com.vividsolutions:jts entities

Primary LanguageScalaMIT LicenseMIT

Introduction

This is a fork from bjornharrtell/jts2geojson with the only difference that the Objects instantiated by GeoJSONReader are based on the (older) com.vividsolutions:jts library that comes as a transitive dependency of hibernate-spatial and doesn't ship with its own GeoJSON Converter classes. Maybe newer versions will make this project obsolete.

This Java library can convert JTS geometries to GeoJSON and back. Its API is similar to other io.* classes in JTS.

Build Status

Maven

<dependency>
    <groupId>org.wololo</groupId>
    <artifactId>jts2geojson</artifactId>
    <version>0.14.1</version>
</dependency>

Usage

  GeoJSONWriter writer = new GeoJSONWriter();
  GeoJSON json = writer.write(geometry);
  String jsonstring = json.toString();

  GeoJSONReader reader = new GeoJSONReader();
  Geometry geometry = reader.read(json);
  

Features and FeatureCollections

JTS does not have anything like GeoJSON Feature or FeatureCollection but they can be parsed by this library. Example:

  // parse Feature or FeatureCollection
  Feature feature = (Feature) GeoJSONFactory.create(json);
  FeatureCollection featureCollection = (FeatureCollection) GeoJSONFactory.create(json);
  
  // parse Geometry from Feature
  GeoJSONReader reader = new GeoJSONReader();
  Geometry geometry = reader.read(feature.getGeometry());
  geometry = reader.read(featureCollection.getFeatures()[0].getGeometry());
  
  // create and serialize a FeatureCollection
  List<Features> features = new ArrayList<Features>();
  Map<String, Object> properties = new HashMap<String, Object>();
  features.add(new Feature(geometry, properties);
  GeoJSONWriter writer = new GeoJSONWriter();
  GeoJSON json = writer.write(features);