diff --git a/Project.toml b/Project.toml index 1a0aa62..c6d4c35 100644 --- a/Project.toml +++ b/Project.toml @@ -12,12 +12,21 @@ GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" +[weakdeps] +GeoMakie = "db073c08-6b98-4ee5-b6a4-5efafb3259c6" +Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" + +[extensions] +MakieExt = ["GeoMakie", "Makie"] + [compat] ArchGDAL = "0.10" DataAPI = "1.13" DataFrames = "1.4" GeoFormatTypes = "0.3, 0.4" GeoInterface = "1.0.1" +GeoMakie = "0.6, 0.7" +Makie = "0.20.8, 0.21" Tables = "1" julia = "1.6" diff --git a/ext/MakieExt.jl b/ext/MakieExt.jl new file mode 100644 index 0000000..53a47b3 --- /dev/null +++ b/ext/MakieExt.jl @@ -0,0 +1,80 @@ +module MakieExt + +using GeoDataFrames +using Makie, GeoMakie + +import GeoDataFrames.DataFrame +import GeoMakie.GeoInterface as GI +using GeoMakie.GeometryBasics + + +""" + plot(gdf::DataFrame) + +Plot geometries from first column found to hold geometry data. + +# Arguments +- `gdf` : GeoDataFrame +""" +function GeoDataFrames.plot(gdf::DataFrame) + col = try + first(GI.geometrycolumns(gdf)) + catch err + rethrow(err) + end + + return GeoDataFrames.plot(gdf, col) +end + +""" + plot(gdf::DataFrame, geom_col::Symbol) + +# Arguments +- `gdf` : GeoDataFrame +- `geom_col` : Column holding geometries to display +""" +function GeoDataFrames.plot(gdf::DataFrame, geom_col::Symbol) + f = Figure(; size=(600, 900)) + ga = GeoAxis( + f[1,1]; + dest="+proj=latlong +datum=WGS84", + xlabel="Longitude", + ylabel="Latitude", + xticklabelpad=15, + yticklabelpad=40, + xticklabelsize=10, + yticklabelsize=10, + aspect=AxisAspect(0.75) + ) + + trait = GI.trait(gdf[1, geom_col]) + if trait == GI.PointTrait() + _plot!(ga, GeoMakie.geo2basic(gdf[!, geom_col])) + else + # Try plotting as multipolygon + _plot!(ga, GeoMakie.to_multipoly(gdf[!, geom_col])) + end + + xlims!(ga) + ylims!(ga) + + return f +end + +function _plot!(ga::GeoAxis, data::Vector{<:GeometryBasics.Point})::Nothing + scatter!(ga, data) + + return nothing +end +function _plot!(ga::GeoAxis, data::Vector{<:Vector{<:T}})::Nothing where {T<:GeometryBasics.MultiPolygon} + poly!(ga, data) + + return nothing +end +function _plot!(ga::GeoAxis, data::Vector{<:T})::Nothing where {T<:GeometryBasics.MultiPolygon} + poly!(ga, data) + + return nothing +end + +end diff --git a/src/GeoDataFrames.jl b/src/GeoDataFrames.jl index 4da914b..3c0107d 100644 --- a/src/GeoDataFrames.jl +++ b/src/GeoDataFrames.jl @@ -10,5 +10,6 @@ using DataAPI include("exports.jl") include("io.jl") include("utils.jl") +include("viz.jl") end # module diff --git a/src/viz.jl b/src/viz.jl new file mode 100644 index 0000000..75dbb36 --- /dev/null +++ b/src/viz.jl @@ -0,0 +1,4 @@ +"""Dummy interface functions for extensions to hook into""" + +function plot() +end \ No newline at end of file