如何使用cartopy添加点要素shapefile以进行地图绘制

时间:2014-08-16 13:03:02

标签: cartopy

我有两个shapefile。一个是点特征shapefile,名为“point.shp”,另一个是名为“polygon.shp”的多边形shapefile。我想用cartopy添加到地图。 我设法添加了“polygon.shp”,但失败了“point.shp”。

这是我的代码:

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'))

有没有人知道如何做到这一点,或为什么,没有检索所有点'x,y coords然后绘制它们?

使用坐标(x,y值),ax.plot()有效,但ax.scatter()失败,为什么?

由于

1 个答案:

答案 0 :(得分:5)

add_geometries目前将几何体转换为多边形,然后适当地对其进行着色,这当然意味着当您传递add_geometries点时,多边形不可见。有可能在未来可能会有更好的工作,但与此同时,听起来你只是想使用散点图来显示数据。

您可以通过从几何体中获取x和y坐标值并直接传递这些坐标值以使用适当的变换进行分散来实现此目的:

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

HTH