geopandas库的基础学习

geopandas库的基础学习

DataWhale组队学习打卡*阶段内容
本学习笔记为Datawhale开源学习训练营21年4月数据挖掘学习的学习内容,学习链接为:团队学习数据挖掘/智慧海洋
所在学习小组:梅利号

geopandas库的学习记录:
pandas是个用于数据整理和分析的常用第三方库。而geopandas是在pandas的基础上建立的,用法与之类似,主要用于处理地理空间中的数据。
geopandas的数据类型:
GeoSeries 、 GeoDataFrame;用于地理空间数据存储器和shapefile文件

准备工作

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline

初体验
通过matplotlib画出geopandas包里数据集的世界地图文件

world = geopandas.read_file(geopandas.datasets.get_path(‘naturalearth_lowres’))
world.plot()
plt.show()

%title插图%num

数据读取
读取shapefile文件
直接读取shapefile文件
data=gpd.read_file(‘xxxxxxx.shp’)

直接读取文件夹
当文件夹下面只有一个shapefile文件时,可以直接读取这个文件夹(效果同读取该文件夹下shapefile文件一样)
读取zip压缩包中的文件
支持读取zip格式压缩包中的shapefile文件
shp文件在压缩包根目录时
#读取数据规则 zip://路径/xxxxxx.zip
data=gpd.read_file(‘zip://路径/xxxxxxxx.zip’)

原理同直接读取文件夹类似,可参考理解

shp文件在压缩包内部文件夹里时
#读取数据规则 zip://路径/xxx.zip!压缩包内指定文件路径
data=gpd.read_file(‘zip://路径/xxxxxxxx.zip!压缩包内文件路径/xxxxxxxxx.shp’)

读取gdb和gpkg
gdb
data=gpd.read_file(‘xxxxx.gdb’,layer=’xxxxx’) #layer参数为对应图层名称

gpkg
data=gpd.read_file(‘xxxxx.gpkg’,layer=’xxxxx’,encoding=’utf-8′) #layer参数为对应图层名称

读取GeoJSON
读取在线地图框架常用的数据源格式GeoJSON

data=gpd.read_file(‘xxxxxxx.json’)

常用CRS:
WGS84纬度-经度投影:
“+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs” ;也可以用proj4字符串调用”+init=epsg:4326″
UTM区域(北):
“+ proj = utm + zone = 33 + ellps = WGS84 + datum = WGS84 + units = m + no_defs”
UTM Zones(南):
“+ proj = utm + zone = 33 + ellps = WGS84 + datum = WGS84 + units = m + no_defs + south”
合并图层时,要确保地图s的共享公用CRS(方便对齐)