GeoJSON.jl icon indicating copy to clipboard operation
GeoJSON.jl copied to clipboard

JSON3 Array doesn't work with GeoInterface

Open evetion opened this issue 3 years ago • 3 comments

In the sense that an GeoInterface.coordinates will yield a JSON3 Array that no other package will parse as the coordinates they are.

julia> GeoInterface.coordinates(poly1)
1-element Vector{JSON3.Array}:
 JSON3.Array[[0.1, 0.0], [1.1, 0.2], [0, 1], [0.3, 0.0]]

evetion avatar Oct 12 '22 19:10 evetion

Why is it that no other package will accept it? The docs say

Return (an iterator of) point coordinates.

For say a LineString we return a JSON3.Array of JSON3.Array, [[-89, 43], [-88, 44], [-88, 45]]. That seems to be ok since it contains coordinates, of which geomtrait returns PointTrait().

For say a MultiPolygon there are more nested arrays, like [[[ [-117.913883,33.96657], [-117.907767,33.967747], [-117.912919,33.96445], [-117.913883,33.96657] ]]]. I thought that GeoInterface.coordinates was supposed to be like GeoJSON coordinates, returning a list of polygon coordinates. But are you saying that it should return a fully flattened vector of points?

visr avatar Oct 13 '22 19:10 visr

I should be more specific, most packages hardcode Vector{Vector{Float64}}.

evetion avatar Oct 13 '22 19:10 evetion

I thought AbstractVector was fine too. It really should be for performance

rafaqz avatar Oct 14 '22 08:10 rafaqz

@evetion do you know witch packages use Vector rather than AbstractVector ? lets just swap that.

rafaqz avatar Dec 13 '22 14:12 rafaqz

I ran into this issue with conversion toLibGEOS.Polygon from GeoJSON.Polygon.

ianfiske avatar Dec 13 '22 21:12 ianfiske

Ok, thinking about this some more, it's two issues:

  • Some packages don't accept AbstractVectors
  • JSON3 doesn't show the nested array, while most packages require the nested type.
julia> v
1-element Vector{JSON3.Array}:
 JSON3.Array[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]

julia> copy.(v)
1-element Vector{Vector{Vector{Float64}}}:
 [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]

evetion avatar Jan 13 '23 12:01 evetion

So we really have to materialize at least the geometries from JSON3.jl to real Arrays. Although keeping the FeautureCollection itself lazy may be best. See https://github.com/quinnj/JSON3.jl/issues/250

That would solve this issue as well, although it would still be good if ArchGDAL accepted AbstractArray, for e.g. StaticArrays points to work.

rafaqz avatar Jan 14 '23 20:01 rafaqz