Hello, I am trying to write a code that will generate 4 Network Service Analyst Layers for 1.5, 2.5, 5, and 7 miles.
Then be able to extract the lines and polygons from each of those service layers, and save each of them as their own individual feature classes.
Right now I have the code written to generate each service analysis layer successfully, but have yet to figure out how to extract and create the individual features.
Here is the code so far:
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\PPCAnalysis\PPCAnalyis\PPCAnalyis.gdb"
stationSelect = arcpy.GetParameterAsText(0).split(";")
createServiceArea = arcpy.GetParameterAsText(1).split(";")
stationFeatures = "FireStations"
Streetmap = r"C:\GIS\Streetmap Premium\FGDB\StreetMap_Data\NorthAmerica.gdb\Routing\Routing_ND"
qry = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(datasource= stationFeatures, field = 'StationID'), ', '.join(stationSelect))
#Select the hydrants by WaterSysID
arcpy.SelectLayerByAttribute_management(in_layer_or_view=stationFeatures, where_clause=qry)
if '1.5' in createServiceArea:
mile_Half_Service = arcpy.na.MakeServiceAreaAnalysisLayer(
network_data_source=Streetmap,
layer_name="Service Area",
travel_mode="Driving Distance",
travel_direction="FROM_FACILITIES",
cutoffs=[2.41402],
time_of_day=None,
time_zone="LOCAL_TIME_AT_LOCATIONS",
output_type="POLYGONS_AND_LINES",
polygon_detail="STANDARD",
geometry_at_overlaps="OVERLAP",
geometry_at_cutoffs="RINGS",
polygon_trim_distance="100 Meters",
exclude_sources_from_polygon_generation=None,
accumulate_attributes=None,
ignore_invalid_locations="SKIP")
updated_Mile_Half = arcpy.na.AddLocations(
in_network_analysis_layer= mile_Half_Service,
sub_layer="Facilities",
in_table= stationFeatures,
field_mappings="Name Address #;CurbApproach # 0;Attr_Minutes # 0;Attr_TravelTime # 0;Attr_Miles # 0;Attr_Kilometers # 0;Attr_TimeAt1KPH # 0;Attr_WalkTime # 0;Attr_TruckMinutes # 0;Attr_TruckTravelTime # 0;Breaks_Minutes # #;Breaks_TravelTime # #;Breaks_Miles # #;Breaks_Kilometers # #;Breaks_TimeAt1KPH # #;Breaks_WalkTime # #;Breaks_TruckMinutes # #;Breaks_TruckTravelTime # #",
search_tolerance="5000 Meters",
sort_field=None,
search_criteria="Routing_Streets SHAPE;Routing_Streets_Override NONE;Routing_ND_Junctions NONE",
match_type="MATCH_TO_CLOSEST",
append="APPEND",
snap_to_position_along_network="NO_SNAP",
snap_offset="5 Meters",
exclude_restricted_elements="EXCLUDE",
search_query=None,
allow_auto_relocate="ALLOW")
solve_Mile_Half = arcpy.na.Solve(
in_network_analysis_layer= updated_Mile_Half,
ignore_invalids="SKIP",
terminate_on_solve_error="TERMINATE",
simplification_tolerance=None,
overrides="")
arcpy.AddMessage("1.5 mile service area was successfully created")
else:
arcpy.AddMessage('Did not select 1.5')
if '2.5' in createServiceArea:
two_Half_Service = arcpy.na.MakeServiceAreaAnalysisLayer(
network_data_source=Streetmap,
layer_name="Service Area",
travel_mode="Driving Distance",
travel_direction="FROM_FACILITIES",
cutoffs=[4.02336],
time_of_day=None,
time_zone="LOCAL_TIME_AT_LOCATIONS",
output_type="POLYGONS_AND_LINES",
polygon_detail="STANDARD",
geometry_at_overlaps="OVERLAP",
geometry_at_cutoffs="RINGS",
polygon_trim_distance="100 Meters",
exclude_sources_from_polygon_generation=None,
accumulate_attributes=None,
ignore_invalid_locations="SKIP")
updated_Two_Half = arcpy.na.AddLocations(
in_network_analysis_layer= two_Half_Service,
sub_layer="Facilities",
in_table= stationFeatures,
field_mappings="Name Address #;CurbApproach # 0;Attr_Minutes # 0;Attr_TravelTime # 0;Attr_Miles # 0;Attr_Kilometers # 0;Attr_TimeAt1KPH # 0;Attr_WalkTime # 0;Attr_TruckMinutes # 0;Attr_TruckTravelTime # 0;Breaks_Minutes # #;Breaks_TravelTime # #;Breaks_Miles # #;Breaks_Kilometers # #;Breaks_TimeAt1KPH # #;Breaks_WalkTime # #;Breaks_TruckMinutes # #;Breaks_TruckTravelTime # #",
search_tolerance="5000 Meters",
sort_field=None,
search_criteria="Routing_Streets SHAPE;Routing_Streets_Override NONE;Routing_ND_Junctions NONE",
match_type="MATCH_TO_CLOSEST",
append="APPEND",
snap_to_position_along_network="NO_SNAP",
snap_offset="5 Meters",
exclude_restricted_elements="EXCLUDE",
search_query=None,
allow_auto_relocate="ALLOW")
solve_Two_Half = arcpy.na.Solve(
in_network_analysis_layer= updated_Two_Half,
ignore_invalids="SKIP",
terminate_on_solve_error="TERMINATE",
simplification_tolerance=None,
overrides="")
arcpy.AddMessage("2.5 mile service area was successfully created")
else:
arcpy.AddMessage('Did not select 2.5')
if '5' in createServiceArea:
five_Service = arcpy.na.MakeServiceAreaAnalysisLayer(
network_data_source=Streetmap,
layer_name="Service Area",
travel_mode="Driving Distance",
travel_direction="FROM_FACILITIES",
cutoffs=[8.04672],
time_of_day=None,
time_zone="LOCAL_TIME_AT_LOCATIONS",
output_type="POLYGONS_AND_LINES",
polygon_detail="STANDARD",
geometry_at_overlaps="OVERLAP",
geometry_at_cutoffs="RINGS",
polygon_trim_distance="100 Meters",
exclude_sources_from_polygon_generation=None,
accumulate_attributes=None,
ignore_invalid_locations="SKIP")
updated_Five = arcpy.na.AddLocations(
in_network_analysis_layer= five_Service,
sub_layer="Facilities",
in_table= stationFeatures,
field_mappings="Name Address #;CurbApproach # 0;Attr_Minutes # 0;Attr_TravelTime # 0;Attr_Miles # 0;Attr_Kilometers # 0;Attr_TimeAt1KPH # 0;Attr_WalkTime # 0;Attr_TruckMinutes # 0;Attr_TruckTravelTime # 0;Breaks_Minutes # #;Breaks_TravelTime # #;Breaks_Miles # #;Breaks_Kilometers # #;Breaks_TimeAt1KPH # #;Breaks_WalkTime # #;Breaks_TruckMinutes # #;Breaks_TruckTravelTime # #",
search_tolerance="5000 Meters",
sort_field=None,
search_criteria="Routing_Streets SHAPE;Routing_Streets_Override NONE;Routing_ND_Junctions NONE",
match_type="MATCH_TO_CLOSEST",
append="APPEND",
snap_to_position_along_network="NO_SNAP",
snap_offset="5 Meters",
exclude_restricted_elements="EXCLUDE",
search_query=None,
allow_auto_relocate="ALLOW")
solve_Five = arcpy.na.Solve(
in_network_analysis_layer= updated_Five,
ignore_invalids="SKIP",
terminate_on_solve_error="TERMINATE",
simplification_tolerance=None,
overrides="")
arcpy.AddMessage("5 mile service area was successfully created")
else:
arcpy.AddMessage('Did not select 5')
if '7' in createServiceArea:
seven_Service = arcpy.na.MakeServiceAreaAnalysisLayer(
network_data_source=Streetmap,
layer_name="Service Area",
travel_mode="Driving Distance",
travel_direction="FROM_FACILITIES",
cutoffs=[11.2654],
time_of_day=None,
time_zone="LOCAL_TIME_AT_LOCATIONS",
output_type="POLYGONS_AND_LINES",
polygon_detail="STANDARD",
geometry_at_overlaps="OVERLAP",
geometry_at_cutoffs="RINGS",
polygon_trim_distance="100 Meters",
exclude_sources_from_polygon_generation=None,
accumulate_attributes=None,
ignore_invalid_locations="SKIP")
updated_Seven = arcpy.na.AddLocations(
in_network_analysis_layer= seven_Service,
sub_layer="Facilities",
in_table= stationFeatures,
field_mappings="Name Address #;CurbApproach # 0;Attr_Minutes # 0;Attr_TravelTime # 0;Attr_Miles # 0;Attr_Kilometers # 0;Attr_TimeAt1KPH # 0;Attr_WalkTime # 0;Attr_TruckMinutes # 0;Attr_TruckTravelTime # 0;Breaks_Minutes # #;Breaks_TravelTime # #;Breaks_Miles # #;Breaks_Kilometers # #;Breaks_TimeAt1KPH # #;Breaks_WalkTime # #;Breaks_TruckMinutes # #;Breaks_TruckTravelTime # #",
search_tolerance="5000 Meters",
sort_field=None,
search_criteria="Routing_Streets SHAPE;Routing_Streets_Override NONE;Routing_ND_Junctions NONE",
match_type="MATCH_TO_CLOSEST",
append="APPEND",
snap_to_position_along_network="NO_SNAP",
snap_offset="5 Meters",
exclude_restricted_elements="EXCLUDE",
search_query=None,
allow_auto_relocate="ALLOW")
solve_Seven = arcpy.na.Solve(
in_network_analysis_layer= updated_Seven,
ignore_invalids="SKIP",
terminate_on_solve_error="TERMINATE",
simplification_tolerance=None,
overrides="")
arcpy.AddMessage("7W mile service area was successfully created")
else:
arcpy.AddMessage('Did not select 7W')
Thanks in advance
Hi @ColeNelson,
I recently having the same issue using arcpy.na, however, you can do this using arcpy.nax. Here is a great sample that shows how to export polygons from a service area:
# Export the results to a feature class
if result.solveSucceeded:
result.export(arcpy.nax.ServiceAreaOutputDataType.Polygons, output_polygons)