Python将Excel生成SHP

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gdal
import xlrd
import shapefile


# open the excel file
excel_file = xlrd.open_workbook("../geodata/highest-mountains-europe.xlsx")

# get the first sheet
sh = excel_file.sheet_by_index(0)
w = shapefile.Writer(shapefile.POINT)


# fields available GeoNameId    Name    Country    Latitude    Longitude    Altitude (m)
w.field('GeoNameId','F')
w.field('Name', 'C')
w.field('Country', 'C')
w.field('Latitude', 'F')
w.field('Longitude', 'F')
w.field('Altitude', 'F')

# loop over each row in the excel sheet
for rownum in range(sh.nrows):
    # skips over the first row since it is the header row
    if rownum == 0:
        continue
    else:
        x_coord = sh.cell_value(rowx=rownum, colx=4)
        y_coord = sh.cell_value(rowx=rownum, colx=3)
        w.point(x_coord, y_coord)

        w.record(GeoNameId=sh.cell_value(rowx=rownum, colx=0), Name=sh.cell_value(rowx=rownum, colx=1),
                 Country=sh.cell_value(rowx=rownum, colx=2), Latitude=sh.cell_value(rowx=rownum, colx=3),
                 Longitude=sh.cell_value(rowx=rownum, colx=4),Altitude=sh.cell_value(rowx=rownum, colx=5))
        print( "Adding row: " + str(rownum) + " creating mount: " + sh.cell_value(rowx=rownum, colx=1) )

w.save('../geodata/highest-mountains')

转载于:https://www.cnblogs.com/gispathfinder/p/5789767.html

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果你想在 Python生成 SHP 文件,可以使用以下库之一: 1. GDAL/OGR:这是一个开源的地理空间数据处理库,支持多种地理空间数据格式,包括 SHP 文件。你可以使用 `ogr` 模块来创建 SHP 文件。 2. pyshp:这是一个轻量级的 Python 库,可以轻松读写 SHP 文件。 3. Fiona:这是一个 Python 库,用于读写地理空间数据文件。它使用 GDAL 库来处理地理空间数据,因此也可以用来创建 SHP 文件。 你可以根据自己的需要来选择使用哪个库。 ### 回答2: 要使用Python生成shp文件,可以使用`Geopandas`库来处理空间数据。为了生成shp文件,首先需要创建一个空的geopandas数据框,并定义其列。 ```python import geopandas as gpd from shapely.geometry import Point # 创建一个空的geopandas数据框 gdf = gpd.GeoDataFrame(columns=['X坐标', 'Y坐标', '几何对象']) # 假设你已经有一组坐标数据,存储在x_coords和y_coords列表中 x_coords = [30, 40, 50] y_coords = [20, 25, 35] # 使用这些坐标数据创建Point对象,并将它们添加到geopandas数据框中 for x, y in zip(x_coords, y_coords): point = Point(x, y) gdf = gdf.append({'X坐标': x, 'Y坐标': y, '几何对象': point}, ignore_index=True) # 将数据框保存为shp文件 gdf.to_file('output.shp') ``` 上述代码中,首先导入必要的库,包括`geopandas`和`shapely`。然后,创建一个空的geopandas数据框`gdf`,定义了`X坐标`、`Y坐标`和`几何对象`三列。 接下来,假设你已经有一组x坐标和y坐标数据,存储在`x_coords`和`y_coords`列表中。使用这些坐标数据,在循环中创建Point对象,并将其添加到geopandas数据框中。 最后,使用`to_file`函数将数据框保存为shp文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值