twpayne / go-geom

Package geom implements efficient geometry types for geospatial applications.
BSD 2-Clause "Simplified" License
839 stars 104 forks source link

Unmarshal geoJSON []byte to MultiPoint #212

Closed mbn18 closed 2 years ago

mbn18 commented 2 years ago

This fail at compile

    data := geom.NewMultiPoint(geom.XYZM)
    err := geojson.Unmarshal(readFile(), data)

With error:

cannot use data (variable of type geom.MultiPoint) as type geom.T in argument to geojson.Unmarshal: geom.MultiPoint does not implement geom.T (type *geom.T is pointer to interface, not interface)

The only way I could do it was using type hint:

func readMultiPoint() *geom.MultiPoint {
    var g geom.T
    err := geojson.Unmarshal(readFile(), &g)
    if err != nil {
        panic(err)
    }
    return g.(*geom.MultiPoint)
}

Is there a way to make the geojson.Unmarshal to work with MultiPoint? (or other geom).

twpayne commented 2 years ago

No. When you're unmarshaling GeoJSON you don't know what type of geometry you will get, so you have to check afterwards.

mbn18 commented 2 years ago

@twpayne , how should I check the result?

twpayne commented 2 years ago

You can use a type assertion in its two-valued form.