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

Support constructing FeatureCollection from Tables #55

Open visr opened 1 year ago

visr commented 1 year ago

Right now we can convert FeatureCollection to DataFrames, but not back:

julia> using GeoJSON, DataFrames

julia> p = GeoJSON.Point(coordinates = [1.1, 2.2])
Point([1.1, 2.2])

julia> f = GeoJSON.Feature(p; properties = (a = 1, geometry = "g", b = 2))
Feature with a Point and 3 properties: (:geometry, :a, :b)

julia> features = [f]
1-element Vector{GeoJSON.Feature{NamedTuple{(:type, :geometry, :properties), Tuple{String, GeoJSON.Point{NamedTuple{(:type, :coordinates), Tuple{String, Vector{Float64}}}}, NamedTuple{(:a, :geometry, :b), Tuple{Int64, String, Int64}}}}}}:
 Feature with a Point and 3 properties: (:geometry, :a, :b)

julia> fc = GeoJSON.FeatureCollection(features)
FeatureCollection with 1 Features

julia> df = DataFrame(fc)
1×3 DataFrame
 Row │ geometry           a      b
     │ Geometry           Int64  Int64
─────┼─────────────────────────────────
   1 │ Point([1.1, 2.2])      1      2

julia> GeoJSON.FeatureCollection(df)
ERROR: ArgumentError: column name :features not found in the data frame

It would be nice if this worked. We could also test with GeoDataFrames, and also look at that package for how to identify geometry columns (GeoInterface.geometrycolumns).

rafaqz commented 1 year ago

The issue will be that we don't have GeoInterface.geometrycolumns on a DataFrame. So we could just look for :geometry and have a geometrycolumn keyword to specify if it's different.

To reduce the need for the keyword we could also use DataAPI.jl metadata https://github.com/JuliaGeo/GeoInterface.jl/issues/77

tlnagy commented 5 months ago

Just ran into this. I would love to be able to filter a FeatureCollection using all the power of the DataFrames-suite and then plot with GeoMakie. Do you have any suggestions on how to do this?

rafaqz commented 5 months ago

You can already conveet to a dataframe with DataFrame(feature). The problem here is getting it back to a GeoJSON.FeatureCollection... but it doesnt sound like you need that?

And geomakie should accept a vector of geometries?

You can also just plot a vector of GeoJSON geometries (e.g. a DataFrame column) with raw Makie.plot.

rafaqz commented 5 months ago

@visr you can just use GeoInterface.FeatureCollection now. We should add a Tables.jl extension to GeoInterface to let that injest tables.