简体   繁体   中英

Plotting shapely polygon on cartopy

I'm looking to plot a shapely polygon on a Cartopy map. I've looked across both Cartopy and shapely documentations, as well as looked over various solutions across StackOverflow, and nothing seems to work, and I'm not sure as to why.

Here is what I have:

from shapely.geometry.polygon import Polygon
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

# make the map
bounds = [(-122., -72., 25., 50.)]
plt.figure(figsize=(5, 3))
ax = plt.axes(projection=ccrs.LambertConformal())
ax.set_extent(*bounds, crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE.with_scale('50m'), linewidth=0.75)
ax.add_feature(cfeature.STATES, linewidth=0.5)

# add in the polygon
ax.add_geometries([polygon], crs=ccrs.PlateCarree(), facecolor = 'b', edgecolor='black', alpha=0.5)

With the current code, I get a blank image of just the USA. I'm thinking that either: 1) the projections are wrong and is instead placing the polygon elsewhere, or 2) the polygon is being placed onto the map correctly and is just not showing because of the ax.add_geometry properties not being set properly.

For above, polygon is the following:

polygon = Polygon[(-6719446.159777582, 8747162.244957967),
 (-6714522.73594268, 8747162.244957967),
 (-6714522.73594268, 8742238.821123065),
 (-6704675.888272878, 8742238.821123065),
 (-6704675.888272878, 8737315.397288164),
 (-6689905.616768174, 8737315.397288164),
 (-6689905.616768174, 8732391.973453263),
 (-6684982.1929332735, 8732391.973453263),
 (-6684982.1929332735, 8727468.549618362),
 (-6675135.345263471, 8727468.549618362),
 (-6675135.345263471, 8722545.125783462),
 (-6665288.497593668, 8722545.125783462),
 (-6665288.497593668, 8717621.70194856),
 (-6660365.073758768, 8717621.70194856),
 (-6660365.073758768, 8712698.27811366),
 (-6655441.649923867, 8712698.27811366),
 (-6655441.649923867, 8717621.70194856),
 (-6650518.226088965, 8717621.70194856),
 (-6650518.226088965, 8707774.854278758),
 (-6640671.378419163, 8707774.854278758),
 (-6640671.378419163, 8702851.430443857),
 (-6625901.106914459, 8702851.430443857),
 (-6625901.106914459, 8693004.582774054),
 (-6611130.835409755, 8693004.582774054),
 (-6611130.835409755, 8688081.158939153),
 (-6606207.411574854, 8688081.158939153),
 (-6606207.411574854, 8678234.31126935),
 (-6601283.987739952, 8678234.31126935),
 (-6601283.987739952, 8673310.887434449),
 (-6591437.140070151, 8673310.887434449),
 (-6591437.140070151, 8663464.039764646),
 (-6586513.716235249, 8663464.039764646),
 (-6586513.716235249, 8653617.192094844),
 (-6576666.868565448, 8653617.192094844),
 (-6576666.868565448, 8648693.768259943),
 (-6571743.444730546, 8648693.768259943),
 (-6571743.444730546, 8638846.92059014),
 (-6561896.597060744, 8638846.92059014),
 (-6561896.597060744, 8633923.496755239),
 (-6556973.173225842, 8633923.496755239),
 (-6556973.173225842, 8624076.649085436),
 (-6552049.749390941, 8624076.649085436),
 (-6552049.749390941, 8619153.225250535),
 (-6547126.32555604, 8619153.225250535),
 (-6547126.32555604, 8614229.801415635),
 (-6542202.9017211385, 8614229.801415635),
 (-6542202.9017211385, 8609306.377580734),
 (-6537279.477886237, 8609306.377580734),
 (-6537279.477886237, 8599459.529910931),
 (-6527432.630216435, 8599459.529910931),
 (-6527432.630216435, 8589612.682241129),
 (-6522509.206381533, 8589612.682241129),
 (-6522509.206381533, 8584689.258406227),
 (-6512662.358711731, 8584689.258406227),
 (-6512662.358711731, 8574842.410736425),
 (-6507738.93487683, 8574842.410736425),
 (-6507738.93487683, 8569918.986901524),
 (-6502815.511041929, 8569918.986901524),
 (-6502815.511041929, 8564995.563066622),
 (-6497892.087207028, 8564995.563066622),
 (-6497892.087207028, 8560072.139231721),
 (-6492968.663372126, 8560072.139231721),
 (-6492968.663372126, 8550225.29156192),
 (-6488045.239537226, 8550225.29156192),
 (-6488045.239537226, 8545301.867727019),
 (-6483121.815702325, 8545301.867727019),
 (-6483121.815702325, 8540378.443892118),
 (-6478198.391867423, 8540378.443892118),
 (-6478198.391867423, 8535455.020057216),
 (-6473274.968032522, 8535455.020057216),
 (-6473274.968032522, 8525608.172387414),
 (-6468351.544197621, 8525608.172387414),
 (-6468351.544197621, 8520684.748552512),
 (-6463428.1203627195, 8520684.748552512),
 (-6463428.1203627195, 8510837.90088271),
 (-6458504.696527818, 8510837.90088271),
 (-6458504.696527818, 8500991.053212907),
 (-6453581.272692917, 8500991.053212907),
 (-6453581.272692917, 8496067.629378006),
 (-6448657.848858016, 8496067.629378006),
 (-6448657.848858016, 8486220.781708203),
 (-6443734.425023114, 8486220.781708203),
 (-6443734.425023114, 8476373.9340384),
 (-6438811.001188213, 8476373.9340384),
 (-6438811.001188213, 8471450.5102035),
 (-6433887.577353312, 8471450.5102035),
 (-6433887.577353312, 8461603.662533697),
 (-6428964.15351841, 8461603.662533697),
 (-6428964.15351841, 8451756.814863894),
 (-6424040.72968351, 8451756.814863894),
 (-6424040.72968351, 8446833.391028993),
 (-6419117.305848609, 8446833.391028993),
 (-6419117.305848609, 8436986.543359192),
 (-6414193.882013707, 8436986.543359192),
 (-6414193.882013707, 8427139.69568939),
 (-6409270.458178806, 8427139.69568939),
 (-6409270.458178806, 8422216.271854488),
 (-6404347.034343905, 8422216.271854488),
 (-6404347.034343905, 8412369.424184686),
 (-6399423.610509004, 8412369.424184686),
 (-6399423.610509004, 8402522.576514883),
 (-6394500.186674103, 8402522.576514883),
 (-6394500.186674103, 8392675.72884508),
 (-6389576.762839202, 8392675.72884508),
 (-6389576.762839202, 8387752.305010179),
 (-6379729.915169399, 8387752.305010179),
 (-6379729.915169399, 8377905.457340376),
 (-6374806.491334498, 8377905.457340376),
 (-6374806.491334498, 8372982.033505476),
 (-6369883.067499597, 8372982.033505476),
 (-6369883.067499597, 8363135.1858356735),
 (-6364959.643664695, 8363135.1858356735),
 (-6364959.643664695, 8343441.490496069),
 (-6369883.067499597, 8343441.490496069),
 (-6369883.067499597, 8333594.642826267),
 (-6374806.491334498, 8333594.642826267),
 (-6374806.491334498, 8323747.795156464),
 (-6379729.915169399, 8323747.795156464),
 (-6379729.915169399, 8318824.371321563),
 (-6384653.339004301, 8318824.371321563),
 (-6384653.339004301, 8308977.52365176),
 (-6389576.762839202, 8308977.52365176),
 (-6389576.762839202, 8304054.099816859),
 (-6394500.186674103, 8304054.099816859),
 (-6399423.610509004, 8304054.099816859),
 (-6399423.610509004, 8299130.675981957),
 (-6404347.034343905, 8299130.675981957),
 (-6409270.458178806, 8299130.675981957),
 (-6409270.458178806, 8294207.252147056),
 (-6414193.882013707, 8294207.252147056),
 (-6414193.882013707, 8289283.828312156),
 (-6419117.305848609, 8289283.828312156),
 (-6424040.72968351, 8289283.828312156),
 (-6424040.72968351, 8284360.4044772545),
 (-6428964.15351841, 8284360.4044772545),
 (-6433887.577353312, 8284360.4044772545),
 (-6433887.577353312, 8279436.980642353),
 (-6438811.001188213, 8279436.980642353),
 (-6438811.001188213, 8274513.556807452),
 (-6443734.425023114, 8274513.556807452),
 (-6448657.848858016, 8274513.556807452),
 (-6448657.848858016, 8269590.132972551),
 (-6453581.272692917, 8269590.132972551),
 (-6453581.272692917, 8274513.556807452),
 (-6458504.696527818, 8274513.556807452),
 (-6458504.696527818, 8269590.132972551),
 (-6463428.1203627195, 8269590.132972551),
 (-6463428.1203627195, 8274513.556807452),
 (-6468351.544197621, 8274513.556807452),
 (-6468351.544197621, 8269590.132972551),
 (-6473274.968032522, 8269590.132972551),
 (-6492968.663372126, 8269590.132972551),
 (-6492968.663372126, 8264666.70913765),
 (-6497892.087207028, 8264666.70913765),
 (-6497892.087207028, 8269590.132972551),
 (-6502815.511041929, 8269590.132972551),
 (-6502815.511041929, 8264666.70913765),
 (-6507738.93487683, 8264666.70913765),
 (-6507738.93487683, 8269590.132972551),
 (-6512662.358711731, 8269590.132972551),
 (-6512662.358711731, 8264666.70913765),
 (-6517585.782546632, 8264666.70913765),
 (-6517585.782546632, 8269590.132972551),
 (-6522509.206381533, 8269590.132972551),
 (-6522509.206381533, 8264666.70913765),
 (-6532356.054051336, 8264666.70913765),
 (-6532356.054051336, 8274513.556807452),
 (-6542202.9017211385, 8274513.556807452),
 (-6542202.9017211385, 8279436.980642353),
 (-6547126.32555604, 8279436.980642353),
 (-6547126.32555604, 8289283.828312156),
 (-6556973.173225842, 8289283.828312156),
 (-6556973.173225842, 8294207.252147056),
 (-6561896.597060744, 8294207.252147056),
 (-6561896.597060744, 8304054.099816859),
 (-6571743.444730546, 8304054.099816859),
 (-6571743.444730546, 8313900.947486661),
 (-6576666.868565448, 8313900.947486661),
 (-6576666.868565448, 8318824.371321563),
 (-6581590.292400348, 8318824.371321563),
 (-6581590.292400348, 8323747.795156464),
 (-6586513.716235249, 8323747.795156464),
 (-6586513.716235249, 8328671.218991365),
 (-6591437.140070151, 8328671.218991365),
 (-6591437.140070151, 8333594.642826267),
 (-6601283.987739952, 8333594.642826267),
 (-6601283.987739952, 8343441.490496069),
 (-6606207.411574854, 8343441.490496069),
 (-6606207.411574854, 8348364.9143309705),
 (-6616054.259244656, 8348364.9143309705),
 (-6616054.259244656, 8358211.762000772),
 (-6620977.6830795575, 8358211.762000772),
 (-6620977.6830795575, 8368058.609670575),
 (-6625901.106914459, 8368058.609670575),
 (-6625901.106914459, 8363135.1858356735),
 (-6630824.53074936, 8363135.1858356735),
 (-6630824.53074936, 8372982.033505476),
 (-6635747.954584261, 8372982.033505476),
 (-6635747.954584261, 8382828.881175278),
 (-6640671.378419163, 8382828.881175278),
 (-6640671.378419163, 8377905.457340376),
 (-6645594.802254064, 8377905.457340376),
 (-6645594.802254064, 8387752.305010179),
 (-6650518.226088965, 8387752.305010179),
 (-6650518.226088965, 8397599.152679982),
 (-6660365.073758768, 8397599.152679982),
 (-6660365.073758768, 8402522.576514883),
 (-6665288.497593668, 8402522.576514883),
 (-6665288.497593668, 8412369.424184686),
 (-6675135.345263471, 8412369.424184686),
 (-6675135.345263471, 8417292.848019587),
 (-6680058.769098372, 8417292.848019587),
 (-6680058.769098372, 8427139.69568939),
 (-6689905.616768174, 8427139.69568939),
 (-6689905.616768174, 8436986.543359192),
 (-6699752.4644379765, 8436986.543359192),
 (-6699752.4644379765, 8441909.967194093),
 (-6709599.312107779, 8441909.967194093),
 (-6709599.312107779, 8446833.391028993),
 (-6719446.159777582, 8446833.391028993),
 (-6719446.159777582, 8456680.238698795),
 (-6729293.007447384, 8456680.238698795),
 (-6729293.007447384, 8461603.662533697),
 (-6739139.855117187, 8461603.662533697),
 (-6739139.855117187, 8466527.086368598),
 (-6744063.278952088, 8466527.086368598),
 (-6744063.278952088, 8471450.5102035),
 (-6753910.12662189, 8471450.5102035),
 (-6753910.12662189, 8476373.9340384),
 (-6758833.550456791, 8476373.9340384),
 (-6758833.550456791, 8481297.357873302),
 (-6763756.9742916925, 8481297.357873302),
 (-6763756.9742916925, 8486220.781708203),
 (-6768680.398126594, 8486220.781708203),
 (-6768680.398126594, 8491144.205543105),
 (-6773603.821961494, 8491144.205543105),
 (-6773603.821961494, 8500991.053212907),
 (-6778527.2457963955, 8500991.053212907),
 (-6778527.2457963955, 8520684.748552512),
 (-6783450.669631297, 8520684.748552512),
 (-6783450.669631297, 8525608.172387414),
 (-6788374.093466198, 8525608.172387414),
 (-6788374.093466198, 8535455.020057216),
 (-6793297.517301099, 8535455.020057216),
 (-6793297.517301099, 8545301.867727019),
 (-6798220.941136001, 8545301.867727019),
 (-6798220.941136001, 8555148.71539682),
 (-6803144.364970902, 8555148.71539682),
 (-6803144.364970902, 8560072.139231721),
 (-6808067.788805803, 8560072.139231721),
 (-6808067.788805803, 8579765.834571326),
 (-6812991.212640705, 8579765.834571326),
 (-6812991.212640705, 8589612.682241129),
 (-6817914.636475606, 8589612.682241129),
 (-6817914.636475606, 8594536.10607603),
 (-6822838.060310507, 8594536.10607603),
 (-6822838.060310507, 8619153.225250535),
 (-6817914.636475606, 8619153.225250535),
 (-6817914.636475606, 8633923.496755239),
 (-6822838.060310507, 8633923.496755239),
 (-6822838.060310507, 8638846.92059014),
 (-6817914.636475606, 8638846.92059014),
 (-6817914.636475606, 8643770.344425041),
 (-6812991.212640705, 8643770.344425041),
 (-6812991.212640705, 8648693.768259943),
 (-6817914.636475606, 8648693.768259943),
 (-6817914.636475606, 8663464.039764646),
 (-6812991.212640705, 8663464.039764646),
 (-6812991.212640705, 8668387.463599548),
 (-6817914.636475606, 8668387.463599548),
 (-6817914.636475606, 8673310.887434449),
 (-6812991.212640705, 8673310.887434449),
 (-6812991.212640705, 8693004.582774054),
 (-6808067.788805803, 8693004.582774054),
 (-6808067.788805803, 8697928.006608956),
 (-6803144.364970902, 8697928.006608956),
 (-6798220.941136001, 8697928.006608956),
 (-6798220.941136001, 8702851.430443857),
 (-6793297.517301099, 8702851.430443857),
 (-6793297.517301099, 8707774.854278758),
 (-6788374.093466198, 8707774.854278758),
 (-6783450.669631297, 8707774.854278758),
 (-6783450.669631297, 8712698.27811366),
 (-6778527.2457963955, 8712698.27811366),
 (-6773603.821961494, 8712698.27811366),
 (-6773603.821961494, 8717621.70194856),
 (-6768680.398126594, 8717621.70194856),
 (-6763756.9742916925, 8717621.70194856),
 (-6763756.9742916925, 8722545.125783462),
 (-6758833.550456791, 8722545.125783462),
 (-6758833.550456791, 8727468.549618362),
 (-6753910.12662189, 8727468.549618362),
 (-6748986.7027869895, 8727468.549618362),
 (-6748986.7027869895, 8732391.973453263),
 (-6744063.278952088, 8732391.973453263),
 (-6739139.855117187, 8732391.973453263),
 (-6739139.855117187, 8737315.397288164),
 (-6734216.431282286, 8737315.397288164),
 (-6729293.007447384, 8737315.397288164),
 (-6729293.007447384, 8742238.821123065),
 (-6724369.583612483, 8742238.821123065),
 (-6719446.159777582, 8742238.821123065),
 (-6719446.159777582, 8747162.244957967)]```

The main reason that your polygon is not plotted properly because its coordinates are

  1. not PlateCarree(), because x's are not in the range (-180, 180), or y's are not in the range (-90, 90). So, the use of ax.add_geometries([polygon], crs=ccrs.PlateCarree(), ...) is wrong.

  2. not falling within the extents of the plot (see comment within the code)

Here is the code that attempts to create a sample polygon and plot it on the map.

from shapely.geometry.polygon import Polygon
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

# make the map
bounds = [-122., -72., 25., 50.]
plt.figure(figsize=(5, 3))
ax = plt.axes(projection=ccrs.LambertConformal())
ax.set_extent(bounds, crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE.with_scale('110m'), linewidth=0.75)
ax.add_feature(cfeature.STATES, linewidth=0.5)

# this makes use of 4 points within
# .. the range of the plotting areas
# x-range: ax.get_xlim()-->(-2649555.771560139, 2450710.00017399)
# y-range: ax.get_ylim()-->(-1558882.932595755, 1491858.0195908)
# to create a sample polygon, `pgon` for demonstration
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
xs, ys = xmax-xmin, ymax-ymin
padx, pady = xs/6.0, ys/6.0
xll, yll, xur, yur = xmin+padx, ymin+pady, xmax-padx, ymax-pady
# create a sample polygon, `pgon`
pgon = Polygon(((xll, yll),
        (xll, yur),
        (xur, yur),
        (xur, yll),
        (xll, yll)))

# this plots the polygon
# must declare correct coordinate system of the data
# here, coordinates in `pgon` are LambertConformal, 
# it must be specified here as `crs=ccrs.LambertConformal()`
ax.add_geometries([pgon], crs=ccrs.LambertConformal(), facecolor='b', edgecolor='red', alpha=0.8)

plt.show()

The output will be:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM