jts2geojson

repository·master·Indexed 40 days ago

https://github.com/bjornharrtell/jts2geojson

A Java library for bidirectional conversion between JTS (Java Topology Suite) geometries and GeoJSON format. It provides GeoJSONWriter and GeoJSONReader for serialization and deserialization, as well as GeoJSONFactory for parsing GeoJSON Feature and FeatureCollection types.

Tokens
749
Snippets
4
Records
4
Agent score
0%

What's inside jts2geojson

  1. Convert JTS Geometries to GeoJSON and back

    master

    Use GeoJSONWriter to serialize JTS Geometry objects into GeoJSON objects, and GeoJSONReader to deserialize GeoJSON back into JTS Geometry objects. The API follows the standard JTS io.* pattern.

      GeoJSONWriter writer = new GeoJSONWriter();
      GeoJSON json = writer.write(geometry);
      String jsonstring = json.toString();
    
      GeoJSONReader reader = new GeoJSONReader();
      Geometry geometry = reader.read(json);
  2. Create and serialize a FeatureCollection

    master

    You can create a FeatureCollection by building a list of Feature objects (each containing a Geometry and a Map<String, Object> of properties) and passing that list to GeoJSONWriter.write().

      // 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);
  3. Parse GeoJSON Features and FeatureCollections

    master

    While JTS does not natively support GeoJSON Feature or FeatureCollection types, this library provides GeoJSONFactory to parse them. You can cast the result of GeoJSONFactory.create(json) to Feature or FeatureCollection to access their properties and geometries.

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