NetTopologySuite.IO.GeoJSON icon indicating copy to clipboard operation
NetTopologySuite.IO.GeoJSON copied to clipboard

Exception null coordinates inner polygon geometry

Open vgallego opened this issue 3 years ago • 9 comments

I updated to the last version, but I still breaks the reading if I receive null coordinates inside the geometry.

Here I put an example of feature that is not working for me:

{
    "type": "Feature",
    "id": "955r48cb-129f-44ce-a229-cbd065a67bcf",
    "properties": {
        "name": "test",
        "updated": "",
        "altitude": 2657,
        "longitude": -116.425598,
        "latitude": 33.523399
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    null
                ]
            ]
        ]
    }
}

I am using ver. 2.0.4

vgallego avatar Jan 27 '22 14:01 vgallego

this is not a valid GeoJSON file, at least as far as I know. what do you expect as result from this input?

DGuidi avatar Jan 27 '22 14:01 DGuidi

I know that is not a geojson, I paste a part of feature collection, the main problem is that I request a geojson that returns some geometries (polygon type) with null coordinates, and when I try to read it, I can't deserialize it... Can I remove the nodes that contains null values for each empty polygon?

vgallego avatar Jan 27 '22 16:01 vgallego

@DGuidi is right, there was a , (comma) after the latitude property which made the snipplet invalid. I removed it.

FObermaier avatar Jan 27 '22 16:01 FObermaier

@FObermaier I think the comma is accepted by the "legacy" geoJSON parser, but the error is related to the missing coordinate values that are actually not handled. See test case I added. Not sure how input like this should be handled: should an empty geometry returned in any case?

DGuidi avatar Jan 27 '22 16:01 DGuidi

Jsonlint says it not valid.

FObermaier avatar Jan 27 '22 17:01 FObermaier

I got an example of this schema collection, I know as geojson, it is not valid, but some elements of the collection come like this sometimes, the geometry is not null, but its internal coordinates are. I need to know if nts discards these null values:

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "id":"d2673d31-6adc-4ea2-84c0-019202b6c415",
         "properties":{
            "name":"FeatureName2",
            "longitude":-118.2276,
            "latitude":41.980499,
            "state":"ID"
         },
         "geometry":{
            "type":"Polygon",
            "coordinates":[
               [
                  [
                     null
                  ]
               ]
            ]
         }
      },
      {
         "type":"Feature",
         "id":"d2673d31-6adc-4ea2-84c0-019202b6c415",
         "properties":{
            "name":"FeatureName2",
            "longitude":-116.6576,
            "latitude":42.980499,
            "state":"ID"
         },
         "geometry":{
            "type":"Polygon",
            "coordinates":[
               [
                  [
                     null
                  ]
               ]
            ]
         }
      }
   ]
}

vgallego avatar Jan 27 '22 19:01 vgallego

the code you posted is valid json but invalid geojson image

{
	"type": "FeatureCollection",
	"features": [{
		"type": "Feature",
		"id": "d2673d31-6adc-4ea2-84c0-019202b6c415",
		"properties": {
			"name": "FeatureName2",
			"longitude": -118.2276,
			"latitude": 41.980499,
			"state": "ID"
		},
		"geometry": {
			"type": "Polygon",
			"coordinates": [
				[
					[
						null
					]
				]
			]
		}
	}, {
		"type": "Feature",
		"id": "d2673d31-6adc-4ea2-84c0-019202b6c415",
		"properties": {
			"name": "FeatureName2",
			"longitude": -116.6576,
			"latitude": 42.980499,
			"state": "ID"
		},
		"geometry": {
			"type": "Polygon",
			"coordinates": [
				[
					[
						null
					]
				]
			]
		}
	}]
}

It's the same if we remove the "nulls"

{
	"type": "FeatureCollection",
	"features": [{
		"type": "Feature",
		"id": "d2673d31-6adc-4ea2-84c0-019202b6c415",
		"properties": {
			"name": "FeatureName2",
			"longitude": -118.2276,
			"latitude": 41.980499,
			"state": "ID"
		},
		"geometry": {
			"type": "Polygon",
			"coordinates": [
				[
					[
					]
				]
			]
		}
	}, {
		"type": "Feature",
		"id": "d2673d31-6adc-4ea2-84c0-019202b6c415",
		"properties": {
			"name": "FeatureName2",
			"longitude": -116.6576,
			"latitude": 42.980499,
			"state": "ID"
		},
		"geometry": {
			"type": "Polygon",
			"coordinates": [
				[
					[
					]
				]
			]
		}
	}]
}

see also this interesting conversation related to other libraries Does Shapely support serialization of GeoJSON with empty and non-empty coordinate sequences?

DGuidi avatar Jan 28 '22 07:01 DGuidi

This looks a valid geojson of an empty polygon, that is not serialized correctly by GeoJSON library (but serialized correctly by GeoJSON4STJ)

{
	"type": "Feature",
	"id": "955r48cb-129f-44ce-a229-cbd065a67bcf",
	"properties": {
		"name": "test",
		"updated": "",
		"altitude": 2657,
		"longitude": -116.425598,
		"latitude": 33.523399
	},
	"geometry": {
		"type": "Polygon",
		"coordinates": []
	}
}

DGuidi avatar Jan 28 '22 07:01 DGuidi

I need to know if nts discards these null values

@vgallego the simple answer is "no": NTS as a library currenyly handles only valid data, and throws an error in case of invalid input. Not sure worth to introduce an additional flag to avoid throwing exception and simply ignore invalid data. The problem I see is that when we talk about "invalid data" I can think of an infinite number of combinations of "invalid data", from the null coordinate in a coordinate sequence (as in your example) to a "xml formatted data" passed to the serializer. So, talking about a general case and forgetting the specific case of this issue, what are the meaningful "invalidities" that should be handled?

DGuidi avatar Jan 28 '22 08:01 DGuidi