Created
August 3, 2019 10:04
-
-
Save asinghvi17/42ed4b7c9b8d2f02502a61b18ec251cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Plots generated with this script: