paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
910 stars 103 forks source link

geojson.UnmarshalGeometry() fails to detect an invalid geojson #63

Closed aneeskA closed 3 years ago

aneeskA commented 3 years ago

I have this geojson,

{
    "coordinates": [
        [
            [
                39.793632515714904,
                75.04223433164586
            ],
            null,
            [
                40.36804504271686,
                74.37994240330923
            ],
            [
                38.396554228796475,
                74.38108104787864
            ],
            [
                37.83643684788231,
                75.0431604849482
            ],
            [
                39.793632515714904,
                75.04223433164586
            ]
        ]
    ],
    "type": "Polygon",
    "srid": "epsg:4326"
}

which has a null in it. When I try to use the following code check if the above is good or not, my expectation is that geojson.UnmarshalGeometry will fail. But that is not happening.

        f, err := json.Marshal(_abovejson)
    if err != nil {
        return err
    }

    _footprint, err := geojson.UnmarshalGeometry(f)
    if err != nil {
        return err
    }

What am I missing?

paulmach commented 3 years ago

I haven't looked into the underlying reason but this seems to be how the standard encoding/json works. It just fills that coordinate location with the zero values which would be [0,0]

https://play.golang.org/p/oZdzCxh5V4t

I guess you'd have to manually unmarshal into something like []*orb.Point and see if there are any null values?

aneeskA commented 3 years ago

Yes, I did this experiment independently to arrive at the same conclusion :) Then I thought I was missing something obvious. What I need is something like Postgis' ST_IsValid() function. Do you have any recommendation for me?

paulmach commented 3 years ago

geojson validation is being discussed https://github.com/paulmach/orb/issues/45