Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@asinghvi17
Created August 3, 2019 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asinghvi17/42ed4b7c9b8d2f02502a61b18ec251cb to your computer and use it in GitHub Desktop.
Save asinghvi17/42ed4b7c9b8d2f02502a61b18ec251cb to your computer and use it in GitHub Desktop.
# setup
# import Pkg;
# Pkg.pkg"add Makie#master PlotUtils GeoInterface GeoJSON"
using Makie
using GeoInterface, GeoJSON
using PlotUtils
states = download("https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json")
states_geo = GeoJSON.parse(read(states, String))
features = states_geo.features
props = getproperty.(features, :properties)
densities = getindex.(props, "density")
cr = let ds = sort(densities); (ds[1], ds[end-1]); end
Base.convert(poly::GeoInterface.Polygon, ::Type{Point2f0}) = Point2f0.(poly.coordinates[1])
viridis = cgrad(:viridis; scale = :log10)
function plo(sc::Scene, poly::Polygon; kwargs...)
poly!(sc, convert(poly, Point2f0); kwargs...)
end
function plo(sc, poly::MultiPolygon; kwargs...)
# map(x -> poly!(sc, Point2f0.(x[1]); kwargs...), poly.coordinates)
data = map(x -> Point2f0.(x[1]), poly.coordinates)
poly!(sc, data; kwargs...)
sc
end
sc = Scene()
normalize(val::Real; min = cr[1], max = cr[2]) = (val - min) / (max - min)
for (i, feature) in enumerate(features)
println(i)
try
plo(sc, feature.geometry, color = viridis[normalize(feature.properties["density"])])
catch err
@warn("Feature had dimension zero!")
print(err)
pop!(sc.plots)
end
# display(sc)
end
save("usa-pop-density-log.png", sc)
@asinghvi17
Copy link
Author

Plots generated with this script:
usa-pop-density-log
US-pop-linscale

@asinghvi17
Copy link
Author

asinghvi17 commented Aug 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment