使用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ù)需要錯開,并且網(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í)出錯
arcpy.Delete_management("station2station0")
print str(e)
print str(i)
continue
# 刪除臨時(shí)圖層,避免下次創(chuàng)建圖層時(shí)出錯
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 中eval()函數(shù)的正確使用及其風(fēng)險(xiǎn)分析(使用示例)
eval()是一個(gè)功能強(qiáng)大的工具,但使用時(shí)必須非常小心,了解其工作原理和潛在的風(fēng)險(xiǎn)是確保安全使用的關(guān)鍵,通過遵循上述建議,可以在享受eval()帶來的便利的同時(shí),最大限度地減少安全風(fēng)險(xiǎn),本文介紹Python 中`eval()`函數(shù)的正確使用及其風(fēng)險(xiǎn)分析,感興趣的朋友一起看看吧2024-07-07
python元組和字典的內(nèi)建函數(shù)實(shí)例詳解
這篇文章主要介紹了python元組和字典的內(nèi)建函數(shù),結(jié)合實(shí)例形式詳細(xì)分析了Python元組和字典的各種常見內(nèi)建函數(shù)功能與相關(guān)使用技巧,需要的朋友可以參考下2019-10-10
一鍵搞定python連接mysql驅(qū)動有關(guān)問題(windows版本)
這篇文章主要介紹了對于mysql驅(qū)動問題折騰了一下午,現(xiàn)共享出解決方案,需要的朋友可以參考下2016-04-04
使用Python pyWinAuto庫自動化Windows任務(wù)的示例代碼
pywinauto是Python的一個(gè)強(qiáng)大的自動化庫,它可以用于控制Windows應(yīng)用程序的用戶界面,本文將詳細(xì)介紹pywinauto庫的安裝、基本用法和高級應(yīng)用,以便你能夠更好地了解如何使用它來自動化Windows應(yīng)用程序,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-11-11
python數(shù)據(jù)庫操作--數(shù)據(jù)庫使用概述
這篇文章主要介紹了python中使用mysql數(shù)據(jù)庫詳細(xì)介紹,本文起講解了安裝mysql、安裝MySQL-python、mysql 的基本操作、python 操作mysql數(shù)據(jù)庫基礎(chǔ)等內(nèi)容,需要的朋友可以參考下2021-08-08
Python?matplotlib繪制散點(diǎn)圖配置(萬能模板案例)
這篇文章主要介紹了Python?matplotlib繪制散點(diǎn)圖配置(萬能模板案例),散點(diǎn)圖是指在??回歸分析???中,數(shù)據(jù)點(diǎn)在直角坐標(biāo)系平面上的?分布圖???,散點(diǎn)圖表示因變量隨??自變量???而?變化???的大致趨勢,據(jù)此可以選擇合適的函數(shù)??對數(shù)???據(jù)點(diǎn)進(jìn)行?擬合2022-07-07
pytest實(shí)現(xiàn)多進(jìn)程與多線程運(yùn)行超好用的插件
本文主要介紹了pytest實(shí)現(xiàn)多進(jìn)程與多線程運(yùn)行超好用的插件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Python編程實(shí)現(xiàn)刪除VC臨時(shí)文件及Debug目錄的方法
這篇文章主要介紹了Python編程實(shí)現(xiàn)刪除VC臨時(shí)文件及Debug目錄的方法,涉及Python針對文件與目錄的遍歷、刪除等相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Pyecharts 動態(tài)地圖 geo()和map()的安裝與用法詳解
這篇文章主要介紹了Pyecharts 動態(tài)地圖 geo()和map()的安裝與用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

