geopandas 写shp
时间: 2023-05-29 19:03:59 浏览: 427
要使用geopandas写入shp文件,需要执行以下步骤:
1.导入geopandas库和其他必要的库:
```
import geopandas as gpd
from shapely.geometry import Point
```
2.创建空的GeoDataFrame对象:
```
crs = {'init': 'epsg:4326'}
gdf = gpd.GeoDataFrame(crs=crs)
```
3.添加数据到GeoDataFrame对象:
```
gdf['id'] = [1, 2, 3]
gdf['name'] = ['New York', 'Los Angeles', 'Chicago']
gdf['geometry'] = [Point(-74.0059, 40.7128), Point(-118.2437, 34.0522), Point(-87.6298, 41.8781)]
```
4.将GeoDataFrame对象写入shp文件:
```
gdf.to_file('cities.shp', driver='ESRI Shapefile')
```
完整的代码示例如下:
```
import geopandas as gpd
from shapely.geometry import Point
crs = {'init': 'epsg:4326'}
gdf = gpd.GeoDataFrame(crs=crs)
gdf['id'] = [1, 2, 3]
gdf['name'] = ['New York', 'Los Angeles', 'Chicago']
gdf['geometry'] = [Point(-74.0059, 40.7128), Point(-118.2437, 34.0522), Point(-87.6298, 41.8781)]
gdf.to_file('cities.shp', driver='ESRI Shapefile')
```
阅读全文