How to add a curly shapefile to a map using cartographic

I have two neck files. One is a point shapefile called "point.shp" and the other is a polygon shape file called "polygon.shp". And I want to add a map using cartography. I was able to add "polygon.shp" but not with "point.shp".

Here's my code:

import matplotlib.pyplot as plt
from cartopy import crs
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature

ax = plt.axes(projection=crs.PlateCarree())

# add the polygon file, worked
ax.add_geometries(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='w')

# or(also worked):
ax.add_feature(ShapelyFeature(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='r'))

# but these two ways both failed with the "point.shp"
ax.add_geometries(Reader("point.shp").geometries(), crs.PlateCarree())

# or, this doesn't work neither:
ax.add_feature(ShapelyFeature(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='r'))

      

Does anyone know how to do this, or why not getting all the x, y coordinates and then plotting them out?

And with coordinates (x, y values), ax.plot()

works, but ax.scatter()

fails, why?

thank

+3


source to share


1 answer


add_geometries currently turns the geometry into a polygon and then paints it appropriately, which of course means that when you pass points to add_geometries the polygons are not visible. Potentially cartopy could do it better in the future, but at the same time it seems like you just want to use something like scatter to render your data.

You can achieve this by fetching x and y coordinate values ​​from the geometry and passing them directly to the scatter with the appropriate transformation:

import cartopy.crs as ccrs
import cartopy.io
import matplotlib.pyplot as plt


fname = cartopy.io.shapereader.natural_earth(resolution='10m',
                                               category='cultural',
                                               name='populated_places_simple')

plt.figure(figsize=(12, 6))
ax = plt.axes(projection=ccrs.Robinson())

ax.set_title('Populated places of the world.')
ax.coastlines()

points = list(cartopy.io.shapereader.Reader(fname).geometries())

ax.scatter([point.x for point in points],
           [point.y for point in points],
           transform=ccrs.Geodetic())

plt.show()

      



output

NTN

+3


source







All Articles