-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add a Tables.jl extension for FeatureCollection and Feature #156
Draft
asinghvi17
wants to merge
2
commits into
main
Choose a base branch
from
as/table_for_featurecollection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
module GeoInterfaceTablesExt | ||
|
||
using GeoInterface | ||
using GeoInterface.Wrappers | ||
using Tables | ||
|
||
# This module is meant to extend the Tables.jl interface to features and feature collections, such that they can be used with Tables.jl. | ||
# This enables the use of the Tables.jl ecosystem with GeoInterface wrapper geometries. | ||
|
||
# First, define the Tables interface | ||
|
||
Tables.istable(::Type{<: Wrappers.FeatureCollection}) = true | ||
Tables.isrowtable(::Type{<: Wrappers.FeatureCollection}) = true | ||
Tables.rowaccess(::Type{<: Wrappers.FeatureCollection}) = true | ||
Tables.rows(fc::Wrappers.FeatureCollection{P, C, E}) where {P <: Union{AbstractArray{<: Wrappers.Feature}, Tuple{Vararg{<: Wrappers.Feature}}}, C, E} = GeoInterface.getfeature(fc) | ||
Tables.rows(fc::Wrappers.FeatureCollection) = Iterators.map(Wrappers.Feature, GeoInterface.getfeature(fc)) | ||
Tables.schema(fc::Wrappers.FeatureCollection) = property_schema(GeoInterface.getfeature(fc)) | ||
|
||
# Define the row access interface for feature wrappers | ||
function Tables.getcolumn(row::Wrappers.Feature, i::Int) | ||
if i == 1 | ||
return GeoInterface.geometry(row) | ||
else | ||
return GeoInterface.properties(row)[i-1] | ||
end | ||
end | ||
Tables.getcolumn(row::Wrappers.Feature, nm::Symbol) = nm === :geometry ? GeoInterface.geometry(row) : Tables.getcolumn(GeoInterface.properties(row), nm) | ||
Tables.columnnames(row::Wrappers.Feature) = (:geometry, propertynames(GeoInterface.properties(row))...) | ||
|
||
# Copied from GeoJSON.jl | ||
# Credit to [Rafael Schouten](@rafaqz) | ||
# Adapted from JSONTables.jl jsontable method | ||
# We cannot simply use their method as we have concrete types and need the key/value pairs | ||
# of the properties field, rather than the main object | ||
# TODO: Is `missT` required? | ||
# TODO: The `getfield` is probably required once | ||
missT(::Type{Nothing}) = Missing | ||
missT(::Type{T}) where {T} = T | ||
|
||
function property_schema(features) | ||
# Otherwise find the shared names | ||
names = Set{Symbol}() | ||
types = Dict{Symbol,Type}() | ||
for feature in features | ||
props = GeoInterface.properties(feature) | ||
isnothing(props) && continue | ||
if isempty(names) | ||
for k in keys(props) | ||
k === :geometry && continue | ||
push!(names, k) | ||
types[k] = missT(typeof(props[k])) | ||
end | ||
push!(names, :geometry) | ||
types[:geometry] = missT(typeof(GeoInterface.geometry(feature))) | ||
else | ||
for nm in names | ||
T = types[nm] | ||
if haskey(props, nm) | ||
v = props[nm] | ||
if !(missT(typeof(v)) <: T) | ||
types[nm] = Union{T,missT(typeof(v))} | ||
end | ||
elseif hasfield(typeof(feature), nm) | ||
v = getfield(feature, nm) | ||
if !(missT(typeof(v)) <: T) | ||
types[nm] = Union{T,missT(typeof(v))} | ||
end | ||
elseif !(T isa Union && T.a === Missing) | ||
types[nm] = Union{Missing,types[nm]} | ||
end | ||
end | ||
for (k, v) in pairs(props) | ||
k === :geometry && continue | ||
if !(k in names) | ||
push!(names, k) | ||
types[k] = Union{Missing,missT(typeof(v))} | ||
end | ||
end | ||
end | ||
end | ||
return collect(names), types | ||
end | ||
|
||
|
||
|
||
# Finally, define the metadata interface. FeatureCollection wrappers have no metadata, so we simply specify geometry columns and CRS. | ||
|
||
Tables.DataAPI.metadatasupport(::Type{<: Wrappers.FeatureCollection}) = (; read = true, write = false) | ||
Tables.DataAPI.metadatakeys(::Wrappers.FeatureCollection) = ("GEOINTERFACE:geometrycolumns", "GEOINTERFACE:crs") | ||
function Tables.DataAPI.metadata(fc::Wrappers.FeatureCollection, key::AbstractString; style = false) | ||
result = if key == "GEOINTERFACE:geometrycolumns" | ||
(:geometry,) | ||
elseif key == "GEOINTERFACE:crs" | ||
if isnothing(GeoInterface.crs(fc)) | ||
nothing | ||
# or | ||
#= | ||
GeoFormatTypes.ESRIWellKnownText( | ||
""" | ||
ENGCRS["Undefined Cartesian SRS with unknown unit", | ||
EDATUM["Unknown engineering datum"], | ||
CS[Cartesian,2], | ||
AXIS["X",unspecified, | ||
ORDER[1], | ||
LENGTHUNIT["unknown",0]], | ||
AXIS["Y",unspecified, | ||
ORDER[2], | ||
LENGTHUNIT["unknown",0]]] | ||
""" | ||
) | ||
=# | ||
else | ||
GeoInterface.crs(fc) | ||
end | ||
else | ||
throw(KeyError(key)) | ||
end | ||
|
||
if style | ||
return (result, :note) | ||
else | ||
return result | ||
end | ||
end | ||
|
||
|
||
|
||
|
||
end # module |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work for GeoJSON too? Would be good to delete the code there if it does