Last active
December 13, 2024 04:30
-
-
Save robert-claypool/0da43e4f217e78bce878e82d546b53b6 to your computer and use it in GitHub Desktop.
Setup My Geodatabase Network Dataset (ArcPy)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
import os | |
def create_network_dataset(gdb_path): | |
""" | |
Creates a network dataset in a file geodatabase based on an XML workspace document schema. | |
Args: | |
gdb_path (str): Path to the file geodatabase. | |
""" | |
if not os.path.exists(gdb_path): | |
print(f"Error: Geodatabase not found at {gdb_path}") | |
return | |
arcpy.env.workspace = gdb_path | |
arcpy.env.overwriteOutput = True | |
feature_dataset_name = "transportation" | |
network_dataset_name = "transportation_ND" | |
# Define the path to the feature dataset | |
feature_dataset_path = os.path.join(gdb_path, feature_dataset_name) | |
# Verify feature dataset exists | |
if not arcpy.Exists(feature_dataset_path): | |
print(f"Error: Feature dataset '{feature_dataset_name}' not found.") | |
return | |
# Check if a network dataset with the same name exists; if so, delete it. | |
if arcpy.Exists(os.path.join(feature_dataset_path, network_dataset_name)): | |
print(f"Warning: Network dataset '{network_dataset_name}' already exists. Deleting...") | |
arcpy.Delete_management(os.path.join(feature_dataset_path, network_dataset_name)) | |
# Define edge sources | |
edge_sources = [ | |
["Waterways_ConnectorLinks", "esriNETEdge", 1, 3], | |
["Railways_ConnectorLinks", "esriNETEdge", 1, 2], | |
["Streets_ConnectorLinks", "esriNETEdge", 1, 1], | |
["waterways_2020", "esriNETEdge", 1, 3], | |
["railways_2020", "esriNETEdge", 1, 2], | |
["streets_here2020_last_process", "esriNETEdge", 1, 1], | |
] | |
# Define junction sources | |
junction_sources = [ | |
["DOEOtherFacilities", "esriNETJunction", 0, [1, 2, 3]], | |
["NuclearReactors", "esriNETJunction", 0, [1, 2, 3]], | |
["ShutdownSites", "esriNETJunction", 0, [1, 2, 3]], | |
["Waterways_Override_Junctions", "esriNETJunction", 1, [1, 2, 3]], | |
["New_Transload_Sites", "esriNETJunction", 0, [1, 2, 3]], | |
["transportation_ND_Junctions", "esriNETJunction", 0, []] | |
] | |
# Create the network dataset | |
arcpy.CreateNetworkDataset_na( | |
in_feature_dataset=feature_dataset_path, | |
out_network_dataset=network_dataset_name, | |
in_edge_sources=[source[0] for source in edge_sources], | |
in_junction_sources=[source[0] for source in junction_sources], | |
in_turn_sources=["Streets_HERE_2020_Turns"], | |
connectivity_policy="HONOR" | |
) | |
# Set properties for edge sources | |
for source in edge_sources: | |
if source[1] == "esriNETEdge": | |
# get full path of source | |
full_source_path = os.path.join(feature_dataset_path, source[0]) | |
# set properyies for edge sources | |
arcpy.na.SetNetworkSourceProperties( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_element_type=source[1], | |
connectivity_group=source[3] | |
) | |
# Set properties for junction sources | |
for source in junction_sources: | |
if source[1] == "esriNETJunction": | |
# get full path of source | |
full_source_path = os.path.join(feature_dataset_path, source[0]) | |
# set properyies for junction sources | |
if source[3]: | |
arcpy.na.SetNetworkSourceProperties( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_element_type=source[1], | |
connectivity_policy="ANY_VERTEX", | |
class_groups=source[3], | |
) | |
else: | |
arcpy.na.SetNetworkSourceProperties( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_element_type=source[1], | |
connectivity_policy="END_VERTEX" | |
) | |
# Define network attributes | |
network_attributes = [ | |
{"name": "Miles", "units": "Miles", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "RoadClass", "units": "Unknown", "data_type": "esriNADTInteger", "usage_type": "esriNAUTDescriptor", "use_by_default": False}, | |
{"name": "Avoid Unpaved Roads", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Avoid Private Roads", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Avoid Toll Roads", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "AccidentRate", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Avoid Roads Closed to Radioactive Material", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Avoid X and A Abandoned Rail", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "BargeAndRailOnly", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "BargeAndTruckOnly", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "BargeOnly", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "Length", "units": "Meters", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "TravelTimeHeavyHaul", "units": "Minutes", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "TravelTimeLegalWeight", "units": "Minutes", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "TruckAndRailOnly", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "TruckOnly", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "Weighted_Population_2500m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Weighted_Population_800m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Rail_Crossings", "units": "Unknown", "data_type": "esriNADTInteger", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "RailOnly", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "Mixed_2575_2500m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Mixed_2575_800m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Mixed_5050_2500m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Mixed_5050_800m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Mixed_7525_2500m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Mixed_7525_800m", "units": "Unknown", "data_type": "esriNADTDouble", "usage_type": "esriNAUTCost", "use_by_default": False}, | |
{"name": "Prefer Bypass Roads", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Prefer Radioactive Designated Roads", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Prefer Uninterrupted High Speed Traffic", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Prohibit 4WD Required Roads", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Prohibit Boat and Rail Ferry", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Prohibit R and T Abandoned Rail", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Prohibit Tunnels", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "RAIL Density 4657 is Preferred", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "RAIL Multiple Tracks Preferred", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "RAIL Prefer HIGH Owner to Owner", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "RAIL Prefer LOW Owner to Trackage", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "RAIL Prefer LOW Trackage to Trackage", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "RAIL Prefer MEDIUM Trackage to Owner", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "RAIL Siding 1 Preferred", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "RAIL STRACNET CS Preferred", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "RAIL TrackClass 4 5 Preferred", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "RAIL Prohibited Carriers", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True}, | |
{"name": "Hierarchy", "units": "Unknown", "data_type": "esriNADTInteger", "usage_type": "esriNAUTHierarchy", "use_by_default": True}, | |
{"name": "RAIL Prohibit Sharp Turn", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": False}, | |
{"name": "Oneway", "units": "Unknown", "data_type": "esriNADTBoolean", "usage_type": "esriNAUTRestriction", "use_by_default": True} | |
] | |
for attribute in network_attributes: | |
# if attribute['name'] == 'Rail_Crossings': | |
# print (f"Adding attribute {attribute['name']} of data type {attribute['data_type']}") | |
# arcpy.na.AddNetworkAttribute( | |
# in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
# in_name=attribute['name'], | |
# in_usage_type=attribute['usage_type'], | |
# in_data_type=attribute['data_type'] | |
# ) | |
# elif attribute['name'] == 'Hierarchy': | |
# print (f"Adding attribute {attribute['name']} of data type {attribute['data_type']}") | |
# arcpy.na.AddNetworkAttribute( | |
# in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
# in_name=attribute['name'], | |
# in_usage_type=attribute['usage_type'], | |
# in_data_type=attribute['data_type'], | |
# is_hierarchy = True, | |
# ) | |
# else: | |
print (f"Adding attribute {attribute['name']} of data type {attribute['data_type']}") | |
arcpy.na.AddNetworkAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_name=attribute['name'], | |
in_usage_type=attribute['usage_type'], | |
in_data_type=attribute['data_type'] | |
) | |
# Set restriction usage and parameters | |
for attribute in network_attributes: | |
if attribute['usage_type'] == 'esriNAUTRestriction': | |
if 'AttributeParameters' in attribute: | |
for parameter in attribute['AttributeParameters']: | |
if parameter['ParameterUsageType'] == 'esriNAPUTRestriction': | |
print (f"Setting parameter usage type for {attribute['name']} as {parameter['ParameterUsageType']}") | |
arcpy.na.SetNetworkAttributeParameter( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_attribute_name=attribute['name'], | |
in_parameter_name=parameter['Name'], | |
in_parameter_usage_type=parameter['ParameterUsageType'], | |
in_value=parameter['Value'], | |
) | |
if parameter['ParameterUsageType'] == 'esriNAPUTGeneral': | |
print (f"Setting parameter usage type for {attribute['name']} as {parameter['ParameterUsageType']}") | |
arcpy.na.SetNetworkAttributeParameter( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_attribute_name=attribute['name'], | |
in_parameter_name=parameter['Name'], | |
in_parameter_usage_type=parameter['ParameterUsageType'], | |
in_value=parameter['Value'] | |
) | |
else: | |
print (f"Seting use by default for {attribute['name']} to {attribute['use_by_default']}") | |
arcpy.na.SetNetworkAttributeUsage( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_attribute_name = attribute['name'], | |
is_default = attribute['use_by_default'] | |
) | |
# Assign attribute to data sources | |
for attribute in network_attributes: | |
if attribute["name"] == "Miles": | |
edge_names = [source[0] for source in edge_sources] | |
for source_name in edge_names: | |
print(f"Assigning 'Miles' attribute to {source_name}...") | |
full_source_path = os.path.join(feature_dataset_path, source_name) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name = "Miles", | |
in_edge_direction = "esriNEDAlongDigitized", | |
use_default_value=False | |
) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name = "Miles", | |
in_edge_direction = "esriNEDAgainstDigitized", | |
use_default_value=False | |
) | |
if attribute["name"] == "RoadClass": | |
source_name = "streets_here2020_last_process" | |
print(f"Assigning 'RoadClass' attribute to {source_name}...") | |
full_source_path = os.path.join(feature_dataset_path, source_name) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name="RoadClass", | |
in_edge_direction="esriNEDAlongDigitized", | |
use_default_value=False | |
) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name="RoadClass", | |
in_edge_direction="esriNEDAgainstDigitized", | |
use_default_value=False | |
) | |
if attribute["name"] == "Avoid Unpaved Roads": | |
source_name = "streets_here2020_last_process" | |
print(f"Assigning 'Avoid Unpaved Roads' to {source_name}...") | |
full_source_path = os.path.join(feature_dataset_path, source_name) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name = "Avoid Unpaved Roads", | |
in_edge_direction = "esriNEDAlongDigitized", | |
use_default_value=False | |
) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_attribute_name="Avoid Unpaved Roads", | |
in_edge_direction="esriNEDAgainstDigitized", | |
use_default_value=False | |
) | |
if attribute["name"] == "Avoid Private Roads": | |
source_name = "streets_here2020_last_process" | |
print(f"Assigning 'Avoid Private Roads' to {source_name}...") | |
full_source_path = os.path.join(feature_dataset_path, source_name) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name="Avoid Private Roads", | |
in_edge_direction="esriNEDAlongDigitized", | |
use_default_value=False | |
) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_attribute_name="Avoid Private Roads", | |
in_edge_direction="esriNEDAgainstDigitized", | |
use_default_value=False | |
) | |
if attribute["name"] == "Avoid Toll Roads": | |
source_name = "streets_here2020_last_process" | |
print(f"Assigning 'Avoid Toll Roads' to {source_name}...") | |
full_source_path = os.path.join(feature_dataset_path, source_name) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_attribute_name = "Avoid Toll Roads", | |
in_edge_direction = "esriNEDAlongDigitized", | |
use_default_value=False | |
) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset = os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_attribute_name = "Avoid Toll Roads", | |
in_edge_direction = "esriNEDAgainstDigitized", | |
use_default_value=False | |
) | |
if attribute["name"] == "AccidentRate": | |
edge_names = [source[0] for source in edge_sources] | |
for source_name in edge_names: | |
print(f"Assigning 'AccidentRate' to {source_name}...") | |
full_source_path = os.path.join(feature_dataset_path, source_name) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name = "AccidentRate", | |
in_edge_direction = "esriNEDAlongDigitized", | |
use_default_value=False | |
) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name=full_source_path, | |
in_attribute_name="AccidentRate", | |
in_edge_direction = "esriNEDAgainstDigitized", | |
use_default_value=False | |
) | |
if attribute["name"] == "Avoid Roads Closed to Radioactive Material": | |
source_name = "streets_here2020_last_process" | |
print(f"Assigning 'Avoid Roads Closed to Radioactive Material' to {source_name}...") | |
full_source_path = os.path.join(feature_dataset_path, source_name) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_attribute_name="Avoid Roads Closed to Radioactive Material", | |
in_edge_direction="esriNEDAlongDigitized", | |
use_default_value=False | |
) | |
arcpy.na.AddNetworkSourceAttribute( | |
in_network_dataset=os.path.join(feature_dataset_path, network_dataset_name), | |
in_source_name = full_source_path, | |
in_ | |
content_copy | |
Use code with caution. | |
Python |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment