Skip to content

Instantly share code, notes, and snippets.

@afontenot
Created March 31, 2020 01:30
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 afontenot/5ce16d39e4cbc0b3f61c7077f3cbe6c5 to your computer and use it in GitHub Desktop.
Save afontenot/5ce16d39e4cbc0b3f61c7077f3cbe6c5 to your computer and use it in GitHub Desktop.
snippet reproducing a bug in shapely's polylabel function
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