使用Arcgis做路徑規(guī)劃方式(使用python腳本調(diào)用)
場景
如何在全部的鐵路線路數(shù)據(jù)中找到點(diǎn)到點(diǎn)的路徑,并且點(diǎn)與點(diǎn)之前的路線可能跨越其它點(diǎn)-也就是路徑規(guī)劃的問題。
須知
- argis: “計(jì)算機(jī)制圖”應(yīng)用,包含了全球范圍內(nèi)的底圖、地圖數(shù)據(jù)、應(yīng)用程序,以及可配置的應(yīng)用模板和開發(fā)人員使用的 GIS 工具和 API,可用于創(chuàng)建 Web 地圖、發(fā)布GIS服務(wù)、共享地圖、數(shù)據(jù)和應(yīng)用程序
- 路徑規(guī)劃:根據(jù)起始出發(fā)點(diǎn)和到達(dá)點(diǎn)(可設(shè)置途經(jīng)點(diǎn)和障礙點(diǎn)),在已確定的路網(wǎng)數(shù)據(jù)中找到最優(yōu)路線(最短路程,最低時(shí)間)
工具
- argis10.2
- python2(建議使用arcgis自帶python)
數(shù)據(jù)
以鐵路數(shù)據(jù)為例:
- 路網(wǎng)數(shù)據(jù)(全國鐵路線路shp文件)
- 列車行駛中所有的點(diǎn)到點(diǎn)的列表數(shù)據(jù)(shp文件形式,可用cvs轉(zhuǎn)成shp)
步驟
argis配置
配置好argis,在argis客戶端計(jì)算出一次路徑分析,保證argis可執(zhí)行
配置允許進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)集操作
點(diǎn)擊菜單customize->extensions,勾選network analyst

點(diǎn)擊菜單customize->toolbar,勾選network analyst

數(shù)據(jù)導(dǎo)入
1.打開ArcCatalog,找到文件樹后右鍵新建“File Geodatabase”,隨后在新建的gdb上右鍵新建“feature dataset ”,之后在dataset上右鍵,選擇“Import”–>“Feature Class”導(dǎo)入你的路網(wǎng)數(shù)據(jù)

創(chuàng)建dataset時(shí)需要選擇對應(yīng)的投影和地理坐標(biāo)系


結(jié)果如下:

2.線處理:進(jìn)行線要素的處理
- 相交線打斷。在arcToolBox的data management tools —>features—>featuretoline。
- 在點(diǎn)要素處打斷線。在arcToolBox的data management tools —>features—>split line at point。
3.構(gòu)建拓?fù)浣Y(jié)構(gòu):在數(shù)據(jù)集dataset上右鍵,New —> Network Dataset…

結(jié)果如下:

4.新建一次分析:network analyst —> New Route

結(jié)果如下,左側(cè)為本次分析的數(shù)據(jù)集界面:

5.增加途經(jīng)點(diǎn):Stops 右鍵 -> Load Locations…

選擇途經(jīng)點(diǎn)圖層導(dǎo)入途經(jīng)點(diǎn)
結(jié)果如下:

6.分析路徑:
點(diǎn)擊 solve

結(jié)果如下:

左側(cè)下的Routes就是我們需要的數(shù)據(jù)
python執(zhí)行準(zhǔn)備
腳本:以下代碼
前面配置的網(wǎng)絡(luò)數(shù)據(jù)集
火車站數(shù)據(jù)以出發(fā)點(diǎn),結(jié)束點(diǎn)兩個(gè)點(diǎn)為一組,全部路線數(shù)據(jù)(shp),如下圖所示,相同的sort為一組:

執(zhí)行腳本
PS: 數(shù)據(jù)量多時(shí),可同時(shí)執(zhí)行多個(gè)腳本,但是執(zhí)行的參數(shù)需要錯(cuò)開,并且網(wǎng)絡(luò)數(shù)據(jù)集需要每個(gè)腳本一個(gè),避免出現(xiàn)操作文件是出現(xiàn)沖突,導(dǎo)致異常。
#調(diào)用argis接口 根據(jù)點(diǎn)查找路徑,并將路徑保存到shp文件中
import arcpy
from arcpy import env
try:
#Check out the Network Analyst extension license
arcpy.CheckOutExtension("Network")
#Set environment settings 配置argis的當(dāng)前基礎(chǔ)數(shù)據(jù)的數(shù)據(jù)文件地址
env.workspace = "C:/Documents/ArcGIS/routeana0.gdb"
env.overwriteOutput = True
#Set local variables 配置好的網(wǎng)絡(luò)數(shù)據(jù)集(NetworkDataset)位置 即基礎(chǔ)數(shù)據(jù)
inNetworkDataset = "train/train_ND"
#輸出的經(jīng)過點(diǎn)的圖層名稱
outNALayerName = "StationRoute"
impedanceAttribute = "Length"
inAddressLocator = "train_station_copy"
#所有經(jīng)過點(diǎn)的圖層位置
allinFeatures = "s2sdis"
#輸入經(jīng)過點(diǎn)數(shù)據(jù)表位置
inAddressTable = "G:/ArcGIS/python/StopAddresses.csv"
#輸入經(jīng)過點(diǎn)數(shù)據(jù)表 字段
inAddressFields = "Name Name VISIBLE NONE"
#輸出經(jīng)過點(diǎn)圖層名稱
outStops = "GeocodedStops"
#輸出經(jīng)過點(diǎn)圖層保存地址
outLayerFile = "G:/ArcGIS/python" + "/" + outNALayerName + ".lyr"
searchTolerance = "3000 Meters"
#Create a new Route layer. For this scenario, the default value for all the
#remaining parameters statisfies the analysis requirements
outNALayer = arcpy.na.MakeRouteLayer(inNetworkDataset, outNALayerName,
impedanceAttribute)
#Get the layer object from the result object. The route layer can now be
#referenced using the layer object. 得到輸出圖層集
outNALayer = outNALayer.getOutput(0)
#Get the names of all the sublayers within the route layer.
#得到輸出圖層名稱
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Stores the layer names that we will use later 根據(jù)名稱獲取對應(yīng)圖層
stopsLayerName = subLayerNames["Stops"]
routesLayerName = subLayerNames["Routes"]
#需要分析多次 篩選多批次的經(jīng)過點(diǎn) 數(shù)據(jù)量大時(shí)可分批執(zhí)行
for i in range(int(start),int(end)) :
篩選當(dāng)前批次的經(jīng)過點(diǎn)到臨時(shí)圖層station2station0
inFeatures = arcpy.Select_analysis(allinFeatures, "station2station0", '"sort" = '+str(i))
#Load the geocoded address locations as stops mapping the address field from
#geocoded stop features as Name property using field mappings.
#網(wǎng)絡(luò)數(shù)據(jù)集中清除原來的點(diǎn),并加入輸出經(jīng)過點(diǎn)圖層中的點(diǎn)
arcpy.na.AddLocations(outNALayer, stopsLayerName, inFeatures, "Name Name #",
searchTolerance, append = "CLEAR",exclude_restricted_elements = "EXCLUDE")
#Solve the route layer, ignore any invalid locations such as those that
#can not be geocoded
#執(zhí)行查找路徑方法 可能存在找不到路徑的情況 捕捉異常,不打斷循環(huán)
try:
result = arcpy.na.Solve(outNALayer,"SKIP")
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print "An error occured on line %i" % tb.tb_lineno
# 刪除臨時(shí)圖層,避免下次創(chuàng)建圖層時(shí)出錯(cuò)
arcpy.Delete_management("station2station0")
print str(e)
print str(i)
continue
# 刪除臨時(shí)圖層,避免下次創(chuàng)建圖層時(shí)出錯(cuò)
arcpy.Delete_management("station2station0")
#Get the polygons sublayer from the service area layer
#獲取結(jié)果中路徑的圖層
routes_sublayer = arcpy.mapping.ListLayers(outNALayer,
routesLayerName)[0]
#將獲得的路徑匯總到時(shí)s2s的shp中
arcpy.Append_management([routes_sublayer], "s2s", "TEST")
print("Script completed successfully")
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print ("An error occured on line %i" % tb.tb_lineno)
print (str(e))總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)冒泡排序算法的示例解析
冒泡排序(Bubble Sort)是一種簡單的排序算法。本文將詳細(xì)為大家講講Python實(shí)現(xiàn)冒泡排序算法的方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06
用 Python 檢測兩個(gè)文本文件的相似性的幾種實(shí)現(xiàn)方法
Python 提供了多種方法來實(shí)現(xiàn)這一功能,包括基于字符串匹配、詞頻統(tǒng)計(jì)和機(jī)器學(xué)習(xí)的方法,這篇文章主要介紹了用 Python 檢測兩個(gè)文本文件的相似性的幾種方法,需要的朋友可以參考下2025-04-04
Python Selenium 之?dāng)?shù)據(jù)驅(qū)動(dòng)測試的實(shí)現(xiàn)
這篇文章主要介紹了Python Selenium 之?dāng)?shù)據(jù)驅(qū)動(dòng)測試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python根據(jù)Excel表進(jìn)行文件重命名的實(shí)現(xiàn)示例
在日常辦公過程中,批量重命名是經(jīng)常使用的操作,本文主要介紹了Python根據(jù)Excel表進(jìn)行文件重命名,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
編寫Python爬蟲抓取豆瓣電影TOP100及用戶頭像的方法
這篇文章主要介紹了編寫Python爬蟲抓取豆瓣電影TOP100及用戶頭像的方法,用到了Python的urllib和urllib2模塊,需要的朋友可以參考下2016-01-01
4個(gè)必學(xué)的Python自動(dòng)化技巧分享
在當(dāng)今快節(jié)奏的工作環(huán)境中,自動(dòng)化是提升效率的重要手段,本文將介紹4個(gè)必學(xué)的Python自動(dòng)化技巧,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
python ForMaiR實(shí)現(xiàn)自定義規(guī)則的郵件自動(dòng)轉(zhuǎn)發(fā)工具
這篇文章主要為大家介紹了python ForMaiR實(shí)現(xiàn)自定義規(guī)則的郵件自動(dòng)轉(zhuǎn)發(fā)工具示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Python實(shí)現(xiàn)動(dòng)態(tài)二維碼生成的示例代碼
這篇文章主要和大家分享兩個(gè)制作二維碼的Python庫,可以生成普通的二維碼、圖片背景版二維碼、動(dòng)圖GIF版二維。文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-05-05

