geopandas:数据读取、坐标设置与转换、空间查询连接、数据输出

geopandas:数据读取、坐标设置与转换、空间查询连接、数据输出,第1张

导入相关包
import geopandas as gp
import pandas as pd
数据读取
polygonShp=gp.read_file("./shp/globeland30-10.shp")
polygonShp
OBJECTIDIdgridcodeShape_LengShape_Areageometry
011101740.059400.000000POLYGON ((321652.212 3472141.992, 321562.212 3...
122101020.027900.000001POLYGON ((320602.212 3472111.992, 320572.212 3...
233101080.020700.000000POLYGON ((320842.212 3472111.992, 320812.212 3...
34410120.0900.000000POLYGON ((318442.212 3472021.992, 318412.212 3...
45510120.0900.000000POLYGON ((318502.212 3471991.992, 318472.212 3...
.....................
299829992999102040.0107100.000000POLYGON ((341932.212 3376801.992, 341752.212 3...
299930003000105100.0202500.000000POLYGON ((342412.212 3376171.992, 342382.212 3...
300030013001105280.0337500.000001POLYGON ((342052.212 3375511.992, 341932.212 3...
300130023002101380.060300.000001POLYGON ((340822.212 3375061.992, 340552.212 3...
300230033003101620.074700.000000POLYGON ((341812.212 3374521.992, 341692.212 3...

3003 rows × 6 columns

基于带有坐标的Pandas DataFrame创建GeoDataFrame
pointcsv=pd.read_csv("./datadata-Test.csv")
pointcsv
OBJECTIDField1Field2Field3
01109.35308030.492034-9.465
12109.34687030.4915653.000
23109.34646730.4923988.000
34109.34656730.4924134.000
45109.35158930.49435939.000
...............
84296278429628109.04703031.3688690.000
84296288429629109.04719631.3688950.000
84296298429630109.04693131.3689810.000
84296308429631109.04705231.3690000.000
84296318429632109.04682031.3690920.000

8429632 rows × 4 columns

pointShp = gp.GeoDataFrame(pointcsv, geometry=gp.points_from_xy(pointcsv.Field1,pointcsv.Field2))
pointShp
OBJECTIDField1Field2Field3geometry
01109.35308030.492034-9.465POINT (109.35308 30.49203)
12109.34687030.4915653.000POINT (109.34687 30.49157)
23109.34646730.4923988.000POINT (109.34647 30.49240)
34109.34656730.4924134.000POINT (109.34657 30.49241)
45109.35158930.49435939.000POINT (109.35159 30.49436)
..................
84296278429628109.04703031.3688690.000POINT (109.04703 31.36887)
84296288429629109.04719631.3688950.000POINT (109.04720 31.36889)
84296298429630109.04693131.3689810.000POINT (109.04693 31.36898)
84296308429631109.04705231.3690000.000POINT (109.04705 31.36900)
84296318429632109.04682031.3690920.000POINT (109.04682 31.36909)

8429632 rows × 5 columns

polygonShp.plot()

GeoDataFrame设置坐标系与进行坐标转换
# 设置坐标系
pointShp.set_crs(epsg=4326,inplace=True)
pointShp.to_crs(epsg=32649,inplace=True)
两个GeoDataFrame间进行空间连接
join_inner_df = gp.sjoin(pointShp, polygonShp, how='inner', op='intersects')
join_inner_df
OBJECTID_leftField1Field2Field3geometryindex_rightOBJECTID_rightIdgridcodeShape_LengShape_Area
56109.35201630.49442639.0POINT (341838.913 3374728.302)300230033003101620.074700.000000
345346109.34017830.4972931.0POINT (340707.335 3375062.731)300130023002101380.060300.000001
346347109.34028230.4973107.0POINT (340717.363 3375064.410)300130023002101380.060300.000001
347348109.34037030.4973246.0POINT (340725.746 3375065.815)300130023002101380.060300.000001
348349109.34046330.4973385.0POINT (340734.720 3375067.319)300130023002101380.060300.000001
....................................
84284378428438109.08045931.3702700.0POINT (317445.086 3472230.637)677101860.0108900.000000
84285538428554109.07996431.3703210.0POINT (317398.122 3472237.057)677101860.0108900.000000
84285548428555109.08010431.370343-1.0POINT (317411.484 3472239.253)677101860.0108900.000000
84286718428672109.07992131.370442-1.0POINT (317394.240 3472250.568)677101860.0108900.000000
84286728428673109.08006831.370465-1.0POINT (317408.250 3472252.871)677101860.0108900.000000

4393811 rows × 11 columns

结果输出
join_inner_df.to_csv("./result.csv",columns=['OBJECTID_left','Field1','Field2','Field3'],index=False)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/942226.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存