几何操作#

GeoPandas 提供了在 Shapely library 中进行几何操作的所有工具。

注意,所有用于通过两个不同空间数据集之间的关系创建新形状的集合理论工具的文档 - 比如创建交集或差异 - 可以在 与叠加的集合操作 中找到。

构造方法#

GeoSeries.buffer(distance, resolution=16)#

返回一个 GeoSeries,表示在每个几何对象给定的 距离 内的所有点的几何体。

GeoSeries.boundary#

返回一个 GeoSeries,表示每个几何形状的集合论 边界 的低维对象。

GeoSeries.centroid#

返回每个几何重心的GeoSeries的点。

GeoSeries.concave_hull#

返回一个GeoSeries,包含表示包含每个对象中所有点的最小凹形多边形的几何图形,前提是对象中的点数不少于三个。对于两个点,凹形外壳会崩溃为线串;对于一个点,则为

GeoSeries.convex_hull#

返回一个 GeoSeries,其几何图形表示包含每个对象中所有点的最小凸 多边形,除非对象中的点数少于三个。对于两个点,凸包简化为 线串;对于一个点,则为

GeoSeries.delaunay_triangles(tolerance, preserve_topology=True)#

返回一个 GeoSeries,由多边形(默认)或线段(only_edges=True)组成,表示围绕输入几何体的顶点计算得到的Delaunay三角剖分。

GeoSeries.envelope#

返回一个 GeoSeries,表示包含每个对象的点或最小矩形多边形(边与坐标轴平行)的几何形状。

GeoSeries.extract_unique_points()#

返回一个 GeoSeries 的几何体,包含每个输入几何体的所有不同顶点作为一个多点。

GeoSeries.offset_curve(distance, quad_segs=8, join_style='round', mitre_limit=5.0)#

返回一个GeoSeries,其中包含距离右侧或左侧对象的LinestringMultiLineString几何体。

GeoSeries.remove_repeated_points()#

返回一个 GeoSeries,其中包含去除重复点的输入几何体的副本。

GeoSeries.simplify(tolerance, preserve_topology=True)#

返回一个 GeoSeries,包含每个对象的简化表示。

GeoSeries.segmentize(max_segment_length)#

返回一个 GeoSeries,该系列在每个线段上根据 max_segment_length 添加了额外的顶点。

GeoSeries.union_all()#

返回一个包含所有GeoSeries中几何图形的并集的几何体。

仿射变换#

GeoSeries.affine_transform(self, matrix)#

使用仿射变换矩阵转换GeoSeries的几何形状

GeoSeries.rotate(self, angle, origin='center', use_radians=False)#

旋转GeoSeries的坐标。

GeoSeries.scale(self, xfact=1.0, yfact=1.0, zfact=1.0, origin='center')#

按每个 (x, y, z) 维度缩放GeoSeries的几何形状。

GeoSeries.skew(self, angle, origin='center', use_radians=False)#

沿着 x 和 y 维度以角度剪切/倾斜 GeoSeries 的几何形状。

GeoSeries.translate(self, xoff=0.0, yoff=0.0, zoff=0.0)#

移动GeoSeries的坐标。

几何操作示例#

>>> import geopandas
>>> from geopandas import GeoSeries
>>> from shapely.geometry import Polygon
>>> p1 = Polygon([(0, 0), (1, 0), (1, 1)])
>>> p2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
>>> p3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
>>> g = GeoSeries([p1, p2, p3])
>>> g
0         POLYGON ((0 0, 1 0, 1 1, 0 0))
1    POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
2    POLYGON ((2 0, 3 0, 3 1, 2 1, 2 0))
dtype: geometry
../../_images/test.png

一些地理操作返回常规的 pandas 对象。一个 GeoSeriesarea 属性将返回一个 pandas.Series,其中包含 GeoSeries 中每个项目的面积:

>>> print(g.area)
0    0.5
1    1.0
2    1.0
dtype: float64

其他操作返回GeoPandas对象:

>>> g.buffer(0.5)
0    POLYGON ((-0.3535533905932737 0.35355339059327...
1    POLYGON ((-0.5 0, -0.5 1, -0.4975923633360985 ...
2    POLYGON ((1.5 0, 1.5 1, 1.502407636663901 1.04...
dtype: geometry
../../_images/test_buffer.png

GeoPandas对象也知道如何自我绘图。GeoPandas使用 matplotlib 进行绘图。要生成一个 GeoSeries 的图,使用:

>>> g.plot()

GeoPandas 还实现了可以读取Pyogrio识别的任何数据格式的替代构造函数。要读取一个包含纽约市的ESRI shapefile的zip文件(由geodatasets包提供):

>>> import geodatasets
>>> nybb_path = geodatasets.get_path('nybb')
>>> boros = geopandas.read_file(nybb_path)
>>> boros.set_index('BoroCode', inplace=True)
>>> boros.sort_index(inplace=True)
>>> boros
               BoroName     Shape_Leng    Shape_Area  \
BoroCode
1             Manhattan  359299.096471  6.364715e+08
2                 Bronx  464392.991824  1.186925e+09
3              Brooklyn  741080.523166  1.937479e+09
4                Queens  896344.047763  3.045213e+09
5         Staten Island  330470.010332  1.623820e+09

                                                   geometry
BoroCode
1         MULTIPOLYGON (((981219.0557861328 188655.31579...
2         MULTIPOLYGON (((1012821.805786133 229228.26458...
3         MULTIPOLYGON (((1021176.479003906 151374.79699...
4         MULTIPOLYGON (((1029606.076599121 156073.81420...
5         MULTIPOLYGON (((970217.0223999023 145643.33221...
../../_images/nyc.png
>>> boros['geometry'].convex_hull
BoroCode
1    POLYGON ((977855.4451904297 188082.3223876953,...
2    POLYGON ((1017949.977600098 225426.8845825195,...
3    POLYGON ((988872.8212280273 146772.0317993164,...
4    POLYGON ((1000721.531799316 136681.776184082, ...
5    POLYGON ((915517.6877458114 120121.8812543372,...
dtype: geometry
../../_images/nyc_hull.png

为了演示一个更复杂的操作,生成一个 GeoSeries,其中包含2000个随机点:

>>> import numpy as np
>>> from shapely.geometry import Point
>>> xmin, xmax, ymin, ymax = 900000, 1080000, 120000, 280000
>>> xc = (xmax - xmin) * np.random.random(2000) + xmin
>>> yc = (ymax - ymin) * np.random.random(2000) + ymin
>>> pts = GeoSeries([Point(x, y) for x, y in zip(xc, yc)])

现在在每个点周围绘制一个固定半径的圆:

>>> circles = pts.buffer(2000)

您可以将这些圆形合并为一个单一的 MultiPolygon 几何图形,方法是

>>> mp = circles.union_all()

要提取每个区包含的此几何体部分,你可以只使用:

>>> holes = boros['geometry'].intersection(mp)
../../_images/holes.png

以及获取孔外的区域:

>>> boros_with_holes = boros['geometry'].difference(mp)
../../_images/boros_with_holes.png

请注意,这可以稍微简化一下,因为 geometry 作为一个属性可以在 GeoDataFrame 上使用,并且 intersection()difference() 方法分别使用 “&” 和 “-” 运算符实现。例如,后者可以简单地表示为 boros.geometry - mp

在每个区计算位于孔中的分数面积是很简单的:

>>> holes.area / boros.geometry.area
BoroCode
1    0.579939
2    0.586833
3    0.608174
4    0.582172
5    0.558075
dtype: float64