Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Latest commit

 

History

History
42 lines (28 loc) · 1.36 KB

README.md

File metadata and controls

42 lines (28 loc) · 1.36 KB

Introduction

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

Build Status

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);