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
which will generate GeoJSON like:
As you can see id is a special case and you can end up with two if you name a property id.