JuliaGeo / GeoJSON.jl

Utilities for working with GeoJSON data in Julia
https://juliageo.org/GeoJSON.jl/stable/
MIT License
68 stars 10 forks source link

Abstract types for compatibility with LibGEOS #15

Closed maximerischard closed 7 years ago

maximerischard commented 7 years ago

If I have, say, a GeoJSON.LineString

ls_json = GeoJSON.LineString([[1.0,2.0],[3.0,4.0]])

and would like to convert it to a LibGEOS.LineString

LibGEOS.LineString(ls_json)

I get a MethodError

MethodError: Cannot `convert` an object of type GeoJSON.LineString to an object of type LibGEOS.LineString
This may have arisen from a call to the constructor LibGEOS.LineString(...),
since type constructors fall back to convert methods.

 in LibGEOS.LineString(::GeoJSON.LineString) at ./sysimg.jl:53

LibGEOS does have a method for converting any GeoInterface.AbstractLineString to a LibGEOS.LineString:

methods(LibGEOS.LineString)
# 4 methods for generic function "(::Type)":
…
LibGEOS.LineString{T<:GeoInterface.AbstractLineString}(obj::T) at ~/.julia/v0.5/LibGEOS/src/geos_types.jl:36
(::Type{T}){T}(arg) at sysimg.jl:53

but GeoJSON.LineString is not a GeoInterface.AbstractLineString

LibGEOS.LineString <: GeoInterface.AbstractLineString
true
GeoJSON.LineString <: GeoInterface.AbstractLineString
false

and so I end up having to do the conversion myself

LibGEOS.LineString(ls_json.coordinates)

which admittedly isn't the end of the world, but it would be convenient and elegant if GeoJSON.LineString were a subtype of GeoInterface.AbstractLineString.

yeesian commented 7 years ago

Yeah, you're right (:

I have a local branch that introduces GeoInterface as a dependency for this package, but had been holding off on it because I was worried users might prefer this package to be "standalone".

Having an issue like this is a good signal (to me) that it might not be a bad way to proceed, so thanks!

maximerischard commented 7 years ago

Thanks for the quick response, and for taking the suggestion on board.

yeesian commented 7 years ago

See #16.