1. 主页
  2. 文档
  3. GeoPandas 0.12.2中文文档
  4. User Guide
  5. Data Structures
  6. GeoDataFrame

GeoDataFrame

GeoDataFrame is a tabular data structure that contains a GeoSeries.

GeoDataFrame是一个包含GeoSeries的表格式数据结构。

The most important property of a GeoDataFrame is that it always has one GeoSeries column that holds a special status. This GeoSeries is referred to as the GeoDataFrame’s “geometry”. When a spatial method is applied to a GeoDataFrame (or a spatial attribute like area is called), this commands will always act on the “geometry” column.

GeoDataFrame最重要的属性是它总是有一个拥有特殊地位的GeoSeries列。这个GeoSeries被称为GeoDataFrame的 “geometry”。当一个空间方法被应用于GeoDataFrame时(例如像面积这样的空间属性被调用时),该方法将始终作用于 “geometry”列。

The “geometry” column – no matter its name – can be accessed through the geometry attribute (gdf.geometry), and the name of the geometry column can be found by typing gdf.geometry.name.

“geometry”列——无论其名称如何——都可以通过几何属性 (gdf.geometry) 访问,并且可以通过gdf.geometry.name 得到geometry列的名称。

GeoDataFrame may also contain other columns with geometrical (shapely) objects, but only one column can be the active geometry at a time. To change which column is the active geometry column, use the GeoDataFrame.set_geometry() method.

GeoDataFrame 还可以包含其他具有几何(shapely)对象的列,但一次只能有一个列是活动几何。要更改哪一列是活动几何列,请使用 GeoDataFrame.set_geometry() 方法。

An example using the worlds GeoDataFrame:

In [1]: world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

In [2]: world.head()
Out[2]: 
       pop_est  ...                                           geometry
0     889953.0  ...  MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
1   58005463.0  ...  POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2     603253.0  ...  POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
3   37589262.0  ...  MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4  328239523.0  ...  MULTIPOLYGON (((-122.84000 49.00000, -120.0000...

[5 rows x 6 columns]

#Plot countries
In [3]: world.plot();

Currently, the column named “geometry” with country borders is the active geometry column:

目前,带有国家边界的名为“geometry”的列是活动几何列。

In [4]: world.geometry.name
Out[4]: 'geometry'

We can also rename this column to “borders”:

我们也可以将这一列重命名为“borders”。

In [5]: world = world.rename(columns={'geometry': 'borders'}).set_geometry('borders')

In [6]: world.geometry.name
Out[6]: 'borders'

Now, we create centroids and make it the geometry:

现在,我们创建质心并使其成为几何体。

In [7]: world['centroid_column'] = world.centroid

In [8]: world = world.set_geometry('centroid_column')

In [9]: world.plot();

Note: A GeoDataFrame keeps track of the active column by name, so if you rename the active geometry column, you must also reset the geometry:

注意:GeoDataFrame 按名称跟踪活动列,因此如果重命名活动几何列,则还必须重置几何对象。

gdf = gdf.rename(columns={'old_name': 'new_name'}).set_geometry('new_name')

Note 2: Somewhat confusingly, by default when you use the read_file() command, the column containing spatial objects from the file is named “geometry” by default, and will be set as the active geometry column. However, despite using the same term for the name of the column and the name of the special attribute that keeps track of the active column, they are distinct. You can easily shift the active geometry column to a different GeoSeries with the set_geometry() command. Further, gdf.geometry will always return the active geometry column, not the column named geometry. If you wish to call a column named “geometry”, and a different column is the active geometry column, use gdf['geometry'], not gdf.geometry.

注意:有点令人困惑的是,默认情况下,当您使用 read_file() 命令时,文件中的空间对象列会默认命名为“geometry”,并将被设置为活动几何列。然而,尽管对列名和跟踪活动列的特殊属性的名称使用相同的术语,但它们是不同的。您可以使用 set_geometry() 命令轻松地将活动几何列移动到不同的 GeoSeries。此外,gdf.geometry 将始终返回活动几何列,而不是名为几何的列。如果您希望调用名为“geometry”的列,并且另一列是活动几何列,请使用 gdf[‘geometry’],而不是 gdf.geometry。

Attributes and Methods

Any of the attributes calls or methods described for a GeoSeries will work on a GeoDataFrame – effectively, they are just applied to the “geometry” GeoSeries.

GeoSeries 的任何属性调用或方法都可以在 GeoDataFrame 上使用——实际上,它们只是应用于“geometry”列。

However, GeoDataFrames also have a few extra methods for input and output which are described on the Input and Output page and for geocoding with are described in Geocoding.

但是,GeoDataFrames 也有一些额外的输入和输出方法,这些方法在输入和输出页面上进行了描述,而用于地理编码的方法在地理编码中进行了描述。

标签 , ,
这篇文章对您有用吗?

我们要如何帮助您?

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here