arcpy读取excel文件中点数据生成shp文件的代码
时间: 2023-12-05 15:04:15 浏览: 35
可以使用arcpy中的ExcelToTable函数将Excel文件转换为ArcGIS中的表格数据,然后使用MakeXYEventLayer函数将表格数据转换为点图层,最后使用CopyFeatures函数将点图层保存为shp文件。
下面是一段示例代码,其中将Excel文件中的经度和纬度字段转换为点图层,并将点图层保存为shp文件:
```
import arcpy
# 设置工作空间
arcpy.env.workspace = r"C:\data"
# Excel文件路径
excel_path = r"C:\data\points.xlsx"
# Excel文件中经度和纬度字段名
lon_field = "Longitude"
lat_field = "Latitude"
# 表格数据名称
table_name = "points_table"
# Excel文件转换为表格数据
arcpy.ExcelToTable_conversion(excel_path, table_name)
# 创建点图层
point_layer = arcpy.MakeXYEventLayer_management(table_name, lon_field, lat_field)
# 保存点图层为shp文件
arcpy.CopyFeatures_management(point_layer, r"C:\data\points.shp")
```
请根据实际情况修改代码中的参数和路径。
相关问题
arcpy读取.xls文件中点数据生成shp文件的代码
以下是一个简单的示例代码,使用arcpy库来读取Excel文件中的点数据,并将其转换为Shapefile文件:
```python
import arcpy
import xlrd
# 设置工作空间
arcpy.env.workspace = r"C:\data"
# 打开Excel文件并获取第一个工作表
book = xlrd.open_workbook("points.xls")
sheet = book.sheet_by_index(0)
# 创建一个新的Shapefile
out_path = r"C:\data"
out_name = "points.shp"
geometry_type = "POINT"
template = ""
has_m = "DISABLED"
has_z = "DISABLED"
sr = arcpy.SpatialReference(4326) # WGS84坐标系
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template, has_m, has_z, sr)
# 添加字段
arcpy.AddField_management(out_name, "Name", "TEXT")
# 插入点数据
cursor = arcpy.InsertCursor(out_name)
for row_index in range(1, sheet.nrows):
x = sheet.cell(row_index, 0).value
y = sheet.cell(row_index, 1).value
name = sheet.cell(row_index, 2).value
point = arcpy.Point(x, y)
feature = cursor.newRow()
feature.shape = point
feature.setValue("Name", name)
cursor.insertRow(feature)
del cursor
```
这段代码假定Excel文件中的点数据存储在第一张工作表中,并且包含三列数据:X坐标,Y坐标和点名称。它还使用WGS84坐标系(EPSG代码为4326)来定义Shapefile文件的空间参考系。你可以根据自己的需要进行修改。
arcpy读取shp文件并打印
以下是使用arcpy读取shp文件并打印其属性表的示例代码:
```python
import arcpy
# 设置工作空间
arcpy.env.workspace = r"path/to/folder/containing/shp/file"
# 读取shp文件
shp_file = "filename.shp"
shp_path = arcpy.env.workspace + "\\" + shp_file
# 打印shp文件的属性表
with arcpy.da.SearchCursor(shp_path, "*") as cursor:
for row in cursor:
print(row)
```
其中,`arcpy.env.workspace`用于设置工作空间,`shp_file`是要读取的shp文件名,`shp_path`是完整的shp文件路径。使用`arcpy.da.SearchCursor`读取shp文件的属性表,可以通过`*`来读取所有字段。最后,使用一个循环逐行打印属性表中的每一行数据。