geopandas 与pandas

geopandas是基于pandas的逻辑开发的能够处理矢量数据的python库(是否能够处理栅格不太确定)那他与pandas的关系如何呢 用一个例子测试一下

1 载入测试数据

import geopandas as gpd
import matplotlib.pyplot as plt
icosa =gpd.read_file('./icosa.shp')
icosa1 =gpd.read_file('./icosaPoint.shp')

geopandas 与pandas

2 测试geopandas的merge.与pandas的merge函数相似,但是有一点区别

pdJoinData =icosa.merge(icosa1,on='global_id')
print(type(pdJoinData))

输出:

官方文档:

Attribute joins are accomplished using the merge method. In general, it is recommended to use the merge method called from the spatial dataset. With that said, the stand-alone merge function will work if the GeoDataFrame is in the left argument; if a DataFrame is in the left argument and a GeoDataFrame is in the right position, the result will no longer be a GeoDataFrame.

属性连接是使用该 merge方法完成的。一般情况下,建议使用 merge从空间数据集调用的方法。话虽如此, merge如果 GeoDataFrame 在 left参数中,独立函数将起作用;如果 DataFrame 在 left参数中并且 GeoDataFrame 在 right位置,结果将不再是 GeoDataFrame。

也就是所这个合并方法如果dataframe调用的,那么返回的是dataframe (pandas的数据结构)如果是GeoDataFrame 调用 并且右参数是dataframe类型那么返回的是GeoDataFrame类型

还有一种情况官方没有说 两个GeoDataFrame调用属性合并merge得到什么?如上图得到的是pandas的DataFrame 而且里面存储了两个shp的geometry 也就是存储了两个geoserise

geopandas 与pandas

通过构造函数能把这个DataFrame转化为geoDataFrame 需要指定geometry,现在gdf就是geodataframe了。和普通的相比,它有多列GeoSeries 只有最后一列 geometry代表的空间。而且不能输出为地理格式 因为geometry不能作为属性输出 但是可以to_csv

gdf = geopandas.GeoDataFrame(pdJoinData, geometry=pdJoinData['geometry_y'])

geopandas 与pandas

综上,pandas和geopandas的数据类型是完全一样的 pandas也可以包含GeoSeries 但是geopandas要包含一个默认的geometry 代表地理空间位置

Original: https://blog.csdn.net/qq_17792451/article/details/119457915
Author: GIS–段小楼
Title: geopandas 与pandas

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/742007/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球