Skip to content

Instantly share code, notes, and snippets.

@briochemc
Created October 30, 2020 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briochemc/06041cb7c552d3af3479990944de07bc to your computer and use it in GitHub Desktop.
Save briochemc/06041cb7c552d3af3479990944de07bc to your computer and use it in GitHub Desktop.
### A Pluto.jl notebook ###
# v0.12.4
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 46c16988-1a54-11eb-2c41-1b8d3c73124b
using Shapefile, Plots, AbstractPlotting, PlutoUI
# ╔═╡ 0cf13ffc-1a4b-11eb-3de2-93e18cef8dfc
begin
natural_earth_dir = joinpath("$(homedir())/Data", "natural_earth")
shp = Shapefile.shapes(Shapefile.Table(joinpath(natural_earth_dir, "ne_110m_land.shp")))
end
# ╔═╡ 103350c2-1a4b-11eb-103d-63c9dac0acca
function polyvectors(shp)
npoints = length(shp.points)
offsets = shp.parts .+ 1 # 1 based
map(zip(offsets, vcat(offsets[2:end], npoints+1))) do (from, to)
[Point(p.x, p.y) for p in view(shp.points, from:to-1)]
end
end
# ╔═╡ 5b16a3bc-1a54-11eb-3da7-c79198117474
md"""
Central longitdue
$(@bind cl PlutoUI.Slider(0:360, show_value=true))
"""
# ╔═╡ d3aa5738-1a56-11eb-3615-35e318bf4c6a
isleft(p, lon) = all(pt[1] ≤ lon for pt in p) # check ≤ or <
# ╔═╡ e34bc008-1a56-11eb-30c8-5980b9155bb3
isright(p, lon) = all(pt[1] ≥ lon for pt in p) # check ≥ or >
# ╔═╡ 1d395a3e-1a55-11eb-19be-1173031965ea
shift(p) = [(pt[1]+360, pt[2]) for pt in p]
# ╔═╡ 29162a60-1a54-11eb-1b91-89a2b6986d64
function split(p, central_longitude)
p2 = shift(p)
if isleft(p, central_longitude - 180)
[p2]
elseif isright(p2, central_longitude + 180)
[p]
else
[p, p2]
end
end
# ╔═╡ a48530f8-1a4b-11eb-27a8-734309b11b38
begin
plt = Plots.plot()
[Plots.plot!(plt, y, lab="", legend=:outerbottom) for s in shp for x in polyvectors(s) for y in split(x, cl)]
Plots.plot!(plt, xlim=(-200, 560))
Plots.vspan!(plt, [cl-180, cl+180], fc=:black, α=0.1, lab="window")
Plots.vline!(plt, [cl], fc=:black, c=:black, lab="central longitude")
end
# ╔═╡ Cell order:
# ╠═46c16988-1a54-11eb-2c41-1b8d3c73124b
# ╠═0cf13ffc-1a4b-11eb-3de2-93e18cef8dfc
# ╠═103350c2-1a4b-11eb-103d-63c9dac0acca
# ╟─5b16a3bc-1a54-11eb-3da7-c79198117474
# ╠═a48530f8-1a4b-11eb-27a8-734309b11b38
# ╠═d3aa5738-1a56-11eb-3615-35e318bf4c6a
# ╠═e34bc008-1a56-11eb-30c8-5980b9155bb3
# ╠═29162a60-1a54-11eb-1b91-89a2b6986d64
# ╠═1d395a3e-1a55-11eb-19be-1173031965ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment