??本文介紹基于Python中ArcPy
模塊,讀取Excel表格數(shù)據(jù)并生成帶有屬性表的矢量要素圖層,同時(shí)配置該圖層的坐標(biāo)系的方法。
1 任務(wù)需求
??首先,我們來明確一下本文所需實(shí)現(xiàn)的需求。
??現(xiàn)有一個(gè)記錄北京市部分PM2.5濃度監(jiān)測站點(diǎn)信息的Excel表格數(shù)據(jù),格式為.xls
;文件內(nèi)包含站點(diǎn)編號(hào)、X
與Y
坐標(biāo)、站點(diǎn)名稱等四列數(shù)據(jù),部分?jǐn)?shù)據(jù)如下所示。
??我們需要將該表格文件中所記錄的全部站點(diǎn)信息導(dǎo)入到Python中,并將全部站點(diǎn)創(chuàng)建為一個(gè)點(diǎn)要素的矢量圖層;此外,需要同時(shí)可以指定該矢量圖層的投影坐標(biāo)系,并將表格文件中的四列信息作為矢量圖層屬性表的字段與內(nèi)容。
2 代碼實(shí)現(xiàn)
??接下來,我們就基于Python中ArcPy
模塊,進(jìn)行詳細(xì)代碼的撰寫與介紹。
??首先,需要說明的是:當(dāng)初在編寫代碼的時(shí)候,為了方便執(zhí)行,所以希望代碼后期可以在ArcMap中直接通過工具箱運(yùn)行,即用到Python程序腳本新建工具箱與自定義工具的方法;因此,代碼中對(duì)于一些需要初始定義的變量,都用到了arcpy.GetParameterAsText()
函數(shù)。大家如果只是希望在IDLE中運(yùn)行代碼,那么直接對(duì)這些變量進(jìn)行具體賦值即可。關(guān)于Python程序腳本新建工具箱與自定義工具,大家可以查看ArcMap將Python寫的代碼轉(zhuǎn)為工具箱與自定義工具詳細(xì)了解。
??上面提到需要初始定義的變量一共有四個(gè),其中arcpy.env.workspace
參數(shù)表示當(dāng)前工作空間,excel_path
參數(shù)表示存儲(chǔ)有北京市PM2.5濃度監(jiān)測站點(diǎn)信息的Excel數(shù)據(jù)文件,spatial_reference_txt
參數(shù)表示需要對(duì)站點(diǎn)矢量數(shù)據(jù)進(jìn)行投影的坐標(biāo)系類型(在本文中我們以“WGS 1984 UTM Zone 50N”投影為例),shapefile_name
參數(shù)表示投影后站點(diǎn)矢量數(shù)據(jù)的具體文件。
# -*- coding: cp936 -*-
# @author: ChuTianjia
import xlrd
import arcpy
arcpy.env.workspace=arcpy.GetParameterAsText(0)
excel_path=arcpy.GetParameterAsText(1) # 站點(diǎn)信息表格文件
shapefile_name=arcpy.GetParameterAsText(3) # 需要生成的矢量要素的路徑與名稱
file_data=xlrd.open_workbook(excel_path)
sheet_data=file_data.sheets()[0]
sheet_row_num=sheet_data.nrows
point_geometry_list=[]
point_object=arcpy.Point()
# Read Spatial Coordinate Information
spatial_reference_txt=arcpy.GetParameterAsText(2) # 指定投影坐標(biāo)系
spatial_reference=arcpy.SpatialReference()
spatial_reference.loadFromString(spatial_reference_txt)
# Import the Coordinates of Each Point
for i in range(1,sheet_row_num):
x=sheet_data.row(i)[1].value
y=sheet_data.row(i)[2].value
point_object.X=float(x)
point_object.Y=float(y)
point_geometry=arcpy.PointGeometry(point_object,spatial_reference)
point_geometry_list.append(point_geometry)
arcpy.CopyFeatures_management(point_geometry_list,shapefile_name)
# Import the Filed Information
field_list=["X","Y","ID_Own","Name"]
arcpy.AddField_management(shapefile_name,field_list[0],"FLOAT")
arcpy.AddField_management(shapefile_name,field_list[1],"FLOAT")
arcpy.AddField_management(shapefile_name,field_list[2],"SHORT")
arcpy.AddField_management(shapefile_name,field_list[3],"TEXT")
with arcpy.da.UpdateCursor(shapefile_name,field_list) as cursor:
n=1
for row in cursor:
row[0]=sheet_data.row(n)[1].value
row[1]=sheet_data.row(n)[2].value
row[2]=sheet_data.row(n)[0].value
row[3]=sheet_data.row(n)[3].value
cursor.updateRow(row)
n+=1
3 運(yùn)行結(jié)果
??執(zhí)行上述代碼,即可得到包含有表格文件中所列全部站點(diǎn)的點(diǎn)要素矢量圖層文件,且其屬性表中包含了原有表格文件中全部列所對(duì)應(yīng)的字段與內(nèi)容。
??查看該圖層屬性,可以看到其已經(jīng)具有了我們?cè)诖a中所指定的投影坐標(biāo)系。
文章來源:http://www.zghlxwxcb.cn/news/detail-841726.html
??至此,大功告成。文章來源地址http://www.zghlxwxcb.cn/news/detail-841726.html
到了這里,關(guān)于Python基于Excel生成矢量圖層及屬性表信息:ArcPy的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!