import arcpy
import os
# ==============================================
# 加油路线规划:XY10区域内最近加油路线
# 数据路径及参数设置
# ==============================================
workspace = r"F:\Project\试题基础数据"
arcpy.env.workspace = workspace
arcpy.env.overwriteOutput = True
# 用户信息
name_initials = "xy"
student_id_tail = "10"
output_prefix = f"{name_initials}{student_id_tail}"
toll_rate_per_km = 0.5 # 高速公路单价(元/公里),可根据实际调整
# 创建文件地理数据库
gdb_name = f"{output_prefix}_RoutePlanning.gdb"
gdb_path = os.path.join(workspace, gdb_name)
if not arcpy.Exists(gdb_path):
arcpy.CreateFileGDB_management(workspace, gdb_name)
# 创建要素数据集,并设置投影与道路一致
dataset_name = "NetworkDataset"
roads_shp = os.path.join(workspace, "道路.shp")
spatial_ref = arcpy.Describe(roads_shp).spatialReference
fds_path = os.path.join(gdb_path, dataset_name)
if not arcpy.Exists(fds_path):
arcpy.CreateFeatureDataset_management(gdb_path, dataset_name, spatial_ref)
# 导入要素
inputs = {
"Roads": "道路.shp",
"Stations": "加油站.shp",
"Communities": "小区.shp",
"Districts": "区划范围.shp"
}
for fc_name, shp in inputs.items():
in_fc = os.path.join(workspace, shp)
out_fc = os.path.join(fds_path, fc_name)
if not arcpy.Exists(out_fc):
arcpy.CopyFeatures_management(in_fc, out_fc)
# 修正道路几何,并添加高速公路收费字段
roads_fc = os.path.join(fds_path, "Roads")
arcpy.RepairGeometry_management(roads_fc)
# 添加字段 Toll 元素
toll_field = "TollCost"
fields = [f.name for f in arcpy.ListFields(roads_fc)]
if toll_field not in fields:
arcpy.AddField_management(roads_fc, toll_field, "DOUBLE")
# 计算收费:高速公路收费 = Shape_Leng(km)*rate,其它类型收费=0
# 假设 Shape_Leng 单位为米
arcpy.CalculateField_management(
in_table=roads_fc,
field=toll_field,
expression=f"(!Shape_Leng!/1000) * {toll_rate_per_km} if !类型! == '高速公路' else 0",
expression_type="PYTHON3"
)
# 创建网络数据集
# 创建网络数据集
network_name = "RoadNet"
network_path = os.path.join(fds_path, network_name)
if not arcpy.Exists(network_path):
arcpy.na.CreateNetworkDataset(
in_dataset=fds_path,
out_name=network_name,
source_feature_classes=[roads_fc],
source_network_attributes=[
["Length", "COST", "DOUBLE", "", "", "Shape_Leng", "METERS", "METERS"],
["Toll", "COST", "DOUBLE", "", "", "TollCost", "", ""]
]
)
arcpy.na.BuildNetwork(network_path)
# 创建路线图层(Route)
route_layer = f"{output_prefix}_区域内最近加油路线"
arcpy.na.MakeRouteAnalysisLayer(
network_dataset=network_path,
layer_name=route_layer,
impedance_attribute="Length",
accumulate_attributes=["Toll"]
)
# 加载设施(加油站)及顺序点(小区)
facilities = os.path.join(fds_path, "Stations")
comm_fc = os.path.join(fds_path, "Communities")
arcpy.na.AddLocations(route_layer, "Stops", comm_fc, search_tolerance="500 Meters")
arcpy.na.AddLocations(route_layer, "Facilities", facilities, search_tolerance="500 Meters")
# 求解路线
arcpy.na.Solve(route_layer)
# 导出结果路线
routes_out = os.path.join(gdb_path, f"{output_prefix}_区域内最近加油路线")
arcpy.CopyFeatures_management(route_layer + "/Routes", routes_out)
# 添加字段存储总收费及总里程
for fld, ftype in [("TotalLength", "DOUBLE"), ("TotalToll", "DOUBLE")]:
if fld not in [f.name for f in arcpy.ListFields(routes_out)]:
arcpy.AddField_management(routes_out, fld, ftype)
# 提取路线属性并写入字段
with arcpy.da.UpdateCursor(routes_out, ["Name", "Total_Length", "Toll", "TotalLength", "TotalToll"]) as ucur:
for row in ucur:
# NA Results 字段:Total_Length 单位米,Toll 为累计高速收费
row[3] = row[1]
row[4] = row[2]
ucur.updateRow(row)
print("路线规划完成,结果存储于:", routes_out)
这段代码中,网络数据集创建老不成功,道路拓扑没有修正好,请重新修订代码,完成代码的任务。
最新发布