[GIS] How to pass custom data in GeoJSON point

geojsongeotoolsjava

I have an application, which creates a GeoJSON stream on the server, which is then rendered on the client (using CesiumJS). GeoJSON is created using GeoTools.

I want to pass some custom data to the client about a particular point. This custom data will determine, how the client renders that point.

I add points using this code:

protected void addPoint(final double lon, final double lat,
    final GeometryBuilder builder, final SimpleFeatureBuilder fbuilder,
    final List<SimpleFeature> features) {
    final Point point = builder.point(lon, lat);
    fbuilder.add(point);
    final SimpleFeature feature = fbuilder.buildFeature(null);
    features.add(feature);
}

If I wanted to add a string property to GeoJSON, with, say, name id and value fooBar123.

How do I do it in Java (how can I modify the code above in order for id:fooBar123 to appear in the results GeoJSON string) ?

Best Answer

For the ID simply pass it in to the featureBuilder instead of null;

For other properties you need to modify the schema you generated the featureBuilder with. The easiest way to do this is to use DataUtilities.createType

SimpleFeatureType TYPE = DataUtilities.createType("Test",
   "ian:String,location:Point");
final Point point = builder.point(132.159633, 43.350116);
SimpleFeatureBuilder fBuild = new SimpleFeatureBuilder(TYPE);
fBuild.add("testing testing");
fBuild.add(point);
SimpleFeature feature = fBuild.buildFeature("foo123");

which will generate GeoJSON like:

{"type":"Feature",
 "geometry":{"type":"Point","coordinates":[132.1596,43.3501]},
 "properties":{"ian":"testing testing"},
 "id":"foo123"}

As you can see id is a special case and you can end up with two if you name a property id.