We are using NetTopologySuite.IO.GeoJSON4STJ
to convert GeoJSON used in our front-end using Google Javascript API to data stored in a database. A mapper class is used to handle the incoming string
containing GeoJSON and return a FeatureCollection
from it:
GeometryFactory geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory();
JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new GeoJsonConverterFactory(geometryFactory));
FeatureCollection? features = JsonSerializer.Deserialize<FeatureCollection>(geoJsonString, options);
If the incoming GeoJSON contains a feature such as a LineString or Polygon that contains only a single Point, ex:
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-122.43652826547623,47.64769550647011]]},"properties":{}}]}
An ArgumentException
is thrown:
Invalid number of points in LineString (found 1 - must be 0 or >= 2)
I can't really handle this easily on the front-end, as the data can come from multiple external sources that use this converter. I would like to just ignore any features of this type that are invalid. The only way to handle it I have is to catch the ArgumentException
and return an empty feature collection, but I would like to still deserialize other features in the collection that are valid and include them as well. I haven't found anything in the NTS options to handle this. Is there any?