使用geopandas裁剪行政区域地图
前言:这个小技巧前面白化的推文也使用过,专门拿出来写一篇
版本 python 3.9import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
# 读取全国地图数据
china_map = gpd.read_file('/home/mw/input/china1656/china_map/china_map/China_Province_2022.shp')
china_map
# 获取行政区域数据
tj = china_map == '天津市']
# 保存裁剪后的tjshp文件
tj.to_file('/home/mw/project/tj.shp')
---------------------------------------------------------------------------
CPLE_AppDefinedError Traceback (most recent call last)
fiona/ogrext.pyx in fiona.ogrext.WritingSession.start()
fiona/_err.pyx in fiona._err.exc_wrap_int()
CPLE_AppDefinedError: Failed to create field name '省': cannot convert to ISO-8859-1
During handling of the above exception, another exception occurred:
SchemaError Traceback (most recent call last)
<ipython-input-10-634bcee8b14b> in <module>
2 tj = china_map == '天津市']
3 # 保存裁剪后的tjshp文件
----> 4 tj.to_file('/home/mw/project/tj.shp')
/opt/conda/lib/python3.9/site-packages/geopandas/geodataframe.py in to_file(self, filename, driver, schema, index, **kwargs)
1112 from geopandas.io.file import _to_file
1113
-> 1114 _to_file(self, filename, driver, schema, index, **kwargs)
1115
1116 def set_crs(self, crs=None, epsg=None, inplace=False, allow_override=False):
/opt/conda/lib/python3.9/site-packages/geopandas/io/file.py in _to_file(df, filename, driver, schema, index, mode, crs, **kwargs)
391 elif crs:
392 crs_wkt = crs.to_wkt("WKT1_GDAL")
--> 393 with fiona.open(
394 filename, mode=mode, driver=driver, crs_wkt=crs_wkt, schema=schema, **kwargs
395 ) as colxn:
/opt/conda/lib/python3.9/site-packages/fiona/env.py in wrapper(*args, **kwargs)
406 def wrapper(*args, **kwargs):
407 if local._env:
--> 408 return f(*args, **kwargs)
409 else:
410 if isinstance(args, str):
/opt/conda/lib/python3.9/site-packages/fiona/__init__.py in open(fp, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt, **kwargs)
270 else:
271 this_schema = None
--> 272 c = Collection(path, mode, crs=crs, driver=driver, schema=this_schema,
273 encoding=encoding, layer=layer, enabled_drivers=enabled_drivers, crs_wkt=crs_wkt,
274 **kwargs)
/opt/conda/lib/python3.9/site-packages/fiona/collection.py in __init__(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, ignore_fields, ignore_geometry, **kwargs)
163 elif self.mode in ('a', 'w'):
164 self.session = WritingSession()
--> 165 self.session.start(self, **kwargs)
166 except IOError:
167 self.session = None
fiona/ogrext.pyx in fiona.ogrext.WritingSession.start()
SchemaError: Failed to create field name '省': cannot convert to ISO-8859-1报错原因是由于Fiona库在写入shapefile文件时,遇到了无法转换为ISO-8859-1编码的字符。Shapefile的标准不支持UTF-8编码,因此在处理包含非ISO-8859-1字符(例如中文)的字段时,可能会出现问题。
下面介绍两种方法
方法一:改变列名
tj1 = tj.rename(columns={'省': 'Province','省级码':'code','省类型':'type'})
tj1.to_file('/home/mw/project/tj1.shp')
shp = gpd.read_file('/home/mw/project/tj1.shp',crs=None)
shp.plot()
plt.show()
好像一只烧鸭
方法二:存取为支持UTF-8编码的GeoJSON格式
# 获取行政区域数据
tj = china_map == '天津市']
tj2.to_file('/home/mw/project/tj2.geojson', driver='GeoJSON')
/opt/conda/lib/python3.9/site-packages/geopandas/io/file.py:362: FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead.
pd.Int64Index,
# 读取GeoJSON文件
tj2 = gpd.read_file('/home/mw/project/tj2.geojson')
# 绘制GeoDataFrame
tj2.plot()
plt.show()
烧鸭二号完成。可喜可贺。
文章来源于微信公众号:气ython风雨
页:
[1]