Skip to content
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

allow tables and iterators as input #9

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -17,6 +18,7 @@ Colors = "0.12"
GeoInterface = "1"
GeoJSON = "0.6"
JSON3 = "1"
Tables = "1"
WebIO = "0.8"
julia = "1"

Expand Down
36 changes: 35 additions & 1 deletion src/layer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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"))
5 changes: 1 addition & 4 deletions src/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down