3

I need to create a GeoJSON string, which contains exactly one point at lon/lat 132.159633, 43.350116.

I'm trying to use GeoTools 12-RC1 for that.

Let's say I have following method, which is supposed to generate GeoJSON with that point:

public String geoData() {
    final GeometryBuilder builder = new GeometryBuilder();
    final Point point = builder.point(132.159633, 43.350116);
    return null; // Now I need to convert point to a GeoJSON string
}

How do I generate a GeoJSON string with point?

6

You need to import the GeoJSON plugn

<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-geojson</artifactId>
  <version>${geotools.version}</version>
</dependency>

and then call it like this (a full example is here):

public String geoData() {
    final GeometryBuilder builder = new GeometryBuilder();
    final Point point = builder.point(132.159633, 43.350116);
    SimpleFeatureBuilder fBuild = new SimpleFeatureBuilder(TYPE);
    fBuild.add(point);
    SimpleFeature feature = fBuild.buildFeature(null);
    FeatureJSON fjson = new FeatureJSON();
    StringWriter writer = new StringWriter();
    try {
        fjson.writeFeature(feature, writer);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String json = writer.toString();
    return json;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.