Created
March 31, 2020 01:30
-
-
Save afontenot/5ce16d39e4cbc0b3f61c7077f3cbe6c5 to your computer and use it in GitHub Desktop.
snippet reproducing a bug in shapely's polylabel function
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
import cartopy.crs as ccrs | |
import cartopy.io.shapereader as shpreader | |
import matplotlib.pyplot as plt | |
from shapely.ops import polylabel | |
shapename="admin_1_states_provinces_lakes_shp" | |
states_shp = shpreader.natural_earth( | |
resolution='50m', | |
category='cultural', | |
name=shapename | |
) | |
shape_reader = shpreader.FionaReader(states_shp) | |
sd = next(filter( | |
lambda x: x.attributes["name"] == "South Dakota", | |
shape_reader.records() | |
)) | |
print(type(sd), type(sd.geometry)) | |
proj = ccrs.LambertAzimuthalEqualArea( | |
central_longitude=-96, | |
central_latitude=37.5 | |
) | |
fig = plt.figure() | |
ax = fig.add_axes([0, 0, 1, 1], projection=proj) | |
ax.set_extent([-105, -95, 42, 46], ccrs.Geodetic()) | |
ax.outline_patch.set_visible(False) | |
ax.add_geometries( | |
[sd.geometry], | |
ccrs.PlateCarree(), | |
facecolor="white", | |
edgecolor="black", | |
linewidth=0.5 | |
) | |
pos = polylabel(sd.geometry, tolerance=0.000005) | |
ax.text( | |
pos.x, | |
pos.y, | |
s = '·', | |
fontsize=36, | |
transform=ccrs.Geodetic(), | |
horizontalalignment="center", | |
verticalalignment="center" | |
) | |
plt.savefig("sd.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment