From 7852a74ef2c53fe0f52f30327768b83744f31054 Mon Sep 17 00:00:00 2001 From: monty Date: Sun, 15 Dec 2024 05:49:06 -0700 Subject: [PATCH] mapbox --- src/NMFkHelpers.jl | 2 +- src/NMFkPlotMap.jl | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/NMFkHelpers.jl b/src/NMFkHelpers.jl index f9c1603..35c4cc4 100644 --- a/src/NMFkHelpers.jl +++ b/src/NMFkHelpers.jl @@ -374,7 +374,7 @@ function flip!(X) end function flip(X) - -X .+ NMFk.maximumnan(X) .+ NMFk.minimumnan(X) + return -X .+ NMFk.maximumnan(X) .+ NMFk.minimumnan(X) end function estimateflip_permutedims(X::AbstractMatrix{T}, Y::AbstractMatrix{T}, A::AbstractMatrix{T}, B::AbstractMatrix{T}, nNNF=10; save=false, method=:ipopt, regularizationweight=1e-8, kw...) where {T <: Number} diff --git a/src/NMFkPlotMap.jl b/src/NMFkPlotMap.jl index cd2200f..4f7b98b 100644 --- a/src/NMFkPlotMap.jl +++ b/src/NMFkPlotMap.jl @@ -259,7 +259,7 @@ function mapbox(df::DataFrames.DataFrame; column::Union{Symbol,AbstractString}=" end end -function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::AbstractVector{T2}; title::AbstractString="", text::AbstractVector=repeat([""], length(lon)), dot_size::Number=3, lonc::AbstractFloat=minimum(lon)+(maximum(lon)-minimum(lon))/2, latc::AbstractFloat=minimum(lat)+(maximum(lat)-minimum(lat))/2, zoom::Number=4, style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token, filename::AbstractString="", figuredir::AbstractString=".", format::AbstractString=splitext(filename)[end][2:end], width::Union{Nothing,Int}=nothing, height::Union{Nothing,Int}=nothing, scale::Real=1, legend::Bool=true, colorscale::Symbol=:rainbow) where {T1 <: Real, T2 <: Any} +function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::AbstractVector{T2}; title::AbstractString="", text::AbstractVector=repeat([""], length(lon)), dot_size::Number=3, lonc::AbstractFloat=minimum(lon)+(maximum(lon)-minimum(lon))/2, latc::AbstractFloat=minimum(lat)+(maximum(lat)-minimum(lat))/2, zoom::Number=4, style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token, filename::AbstractString="", figuredir::AbstractString=".", format::AbstractString=splitext(filename)[end][2:end], width::Union{Nothing,Int}=nothing, height::Union{Nothing,Int}=nothing, scale::Real=1, legend::Bool=true, colorscale::Symbol=:rainbow) where {T1 <: AbstractFloat, T2 <: AbstractFloat} @assert length(lon) == length(lat) @assert length(lon) == length(color) @assert length(lon) == length(text) @@ -277,7 +277,8 @@ function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::Abstrac marker=marker, attributionControl=false ) - p = PlotlyJS.plot(plot, map_plotly_layout(lonc, latc, zoom; style=style, mapbox_token=mapbox_token), config=PlotlyJS.PlotConfig(; scrollZoom=true, staticPlot=false, displayModeBar=false, responsive=true)) + layout = plotly_layout(lonc, latc, zoom; title=title, style=style, mapbox_token=mapbox_token) + p = PlotlyJS.plot(plot, layout; config=PlotlyJS.PlotConfig(; scrollZoom=true, staticPlot=false, displayModeBar=false, responsive=true)) if filename != "" fn = joinpathcheck(figuredir, filename) PlotlyJS.savefig(p, fn; format=format, width=width, height=height, scale=scale) @@ -285,14 +286,46 @@ function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::Abstrac return p end -function map_dot(lon::AbstractFloat=-105.9378, lat::AbstractFloat=35.6870; color::AbstractString="purple", text::AbstractString="EnviTrace LLC", dot_size::Number=12, kw...) +function mapbox(lon::AbstractVector{T1}, lat::AbstractVector{T1}, color::AbstractVector{T2}; title::AbstractString="", dot_size::Number=3, text::AbstractVector=string.(color), lonc::AbstractFloat=minimum(lon)+(maximum(lon)-minimum(lon))/2, latc::AbstractFloat=minimum(lat)+(maximum(lat)-minimum(lat))/2, zoom::Number=4, style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token, filename::AbstractString="", figuredir::AbstractString=".", format::AbstractString=splitext(filename)[end][2:end], width::Union{Nothing,Int}=nothing, height::Union{Nothing,Int}=nothing, scale::Real=1, legend::Bool=true, colorscale::Symbol=:rainbow) where {T1 <: AbstractFloat, T2 <: Union{Number,Symbol,AbstractString,AbstractChar}} + @assert length(lon) == length(lat) + @assert length(lon) == length(color) + @assert length(lon) == length(text) + traces = [] + for (j, i) in enumerate(unique(sort(color))) + iz = color .== i + jj = j % length(NMFk.colors) + k = jj == 0 ? length(NMFk.colors) : jj + marker = PlotlyJS.attr(; size=dot_size, color=NMFk.colors[k]) + trace = PlotlyJS.scattermapbox(; + lon=lon[iz], + lat=lat[iz], + hoverinfo="text", + text=text[iz], + name="$i [$(sum(iz))]", + marker=marker, + showlegend=legend, + attributionControl=false) + push!(traces, trace) + end + traces = convert(Vector{typeof(traces[1])}, traces) + layout = plotly_layout(lonc, latc, zoom; title=title, style=style, mapbox_token=mapbox_token) + p = PlotlyJS.plot(traces, layout; config=PlotlyJS.PlotConfig(; scrollZoom=true, staticPlot=false, displayModeBar=false, responsive=true)) + if filename != "" + fn = joinpathcheck(figuredir, filename) + PlotlyJS.savefig(p, fn; format=format, width=width, height=height, scale=scale) + end + return p +end + +function mapbox(lon::AbstractFloat=-105.9378, lat::AbstractFloat=35.6870; color::AbstractString="purple", text::AbstractString="EnviTrace LLC", dot_size::Number=12, kw...) mapbox([lon], [lat], [color]; text=[text], dot_size=dot_size, legend=false, kw...) end # Plotly map layout -function map_plotly_layout(lonc::Number=-105.9378, latc::Number=35.6870, zoom::Number=4; style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token) +function plotly_layout(lonc::Number=-105.9378, latc::Number=35.6870, zoom::Number=4; title::AbstractString="", style="mapbox://styles/mapbox/satellite-streets-v12", mapbox_token=NMFk.mapbox_token) layout = PlotlyJS.Layout( margin = PlotlyJS.attr(r=0, t=0, b=0, l=0), + title=title, paper_bgcolor="#FFF", mapbox = PlotlyJS.attr( accesstoken=mapbox_token,