From d3bdd3f405e650355954a5900a5c6cb0c2fe2ed7 Mon Sep 17 00:00:00 2001 From: rafaqz Date: Fri, 30 Dec 2022 13:02:34 +0100 Subject: [PATCH] allow tables and iterators as input --- Project.toml | 2 ++ src/layer.jl | 36 +++++++++++++++++++++++++++++++++++- src/map.jl | 5 +---- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index d64d458..71ae668 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f" GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9" JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" +Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29" @@ -17,6 +18,7 @@ Colors = "0.12" GeoInterface = "1" GeoJSON = "0.6" JSON3 = "1" +Tables = "1" WebIO = "0.8" julia = "1" diff --git a/src/layer.jl b/src/layer.jl index 9350f5b..5080d7b 100644 --- a/src/layer.jl +++ b/src/layer.jl @@ -34,6 +34,37 @@ function Layer( marker_size = 3.0, border_width = 2.0, ) + + # Handle inputs that are not geometries or features + if isnothing(GeoInterface.trait(data)) + if Tables.istable(data) + # Create a FeatureCollection from a Tables.jl compatible table + geoms = FeatureCollection(data) + else + # Otherwise try to treat `data` as an iterator + try + if !isnothing(iterate(data)) + x = first(data) + if GeoInterface.isgeometry(x) + geoms = FeatureCollection(GeoJSON.Feature.(data)) + elseif GeoInterface.isfeature(x) + features = data isa AbstractArray ? data : collect(data) + geoms = FeatureCollection(features) + else + _not_compatible_error() + end + else + _not_compatible_error() + end + catch + _not_compatible_error() + end + end + else + geoms = data + end + + # Define options options = Dict( :color_map => string(color_map), :color => string(color), @@ -42,5 +73,8 @@ function Layer( :marker_size => marker_size, :border_width => border_width, ) - Layer(data, options) + + return Layer(geoms, options) end + +_not_compatible_error() = throw(ArgumentError("data is not a GeoInterace compatible Feature or Geometry")) diff --git a/src/map.jl b/src/map.jl index b2ba803..9968ff2 100644 --- a/src/map.jl +++ b/src/map.jl @@ -97,10 +97,7 @@ end function leaflet_javascript(layers, cfg::Config) io = IOBuffer() for (i, layer) in enumerate(layers) - data = layer.data - isnothing(GeoInterface.trait(data)) && - throw(ArgumentError("data is not a GeoInterace compatible Feature or Geometry")) - write(io, "var data$i = ", GeoJSON.write(data), ";\n") + write(io, "var data$i = ", GeoJSON.write(layer.data), ";\n") if layer.options[:color] != "nothing" color = layer.options[:color] if isa(color, Symbol)