Error when saving Network Analyst service area sub-layer as shapefile using Arcpy

637
2
Jump to solution
09-16-2021 08:54 AM
SG_Johnson
New Contributor

I am using python to save the service area polygons from a Network Analyst layer file as shapefiles but the script fails to execute the copy features function:

ExecuteError: Failed to execute. Parameters are not valid; ERROR 000840: The value is not a Feature Layer; ERROR 000840: The value is not a Raster Catalog Layer; Failed to execute (CopyFeatures)

Can anyone tell me what is wrong with the script? 

# Import module
import arcpy
import os

# Local variables:
layer_name = "FireStationCoverage"
FireStationCoverage_lyr = "C:/Data/FireStationCoverage.lyr"
Polygons = FireStationCoverage_lyr
output_dir = "C:/Data/poly_output"
out_featureclass = os.path.join(output_dir,layer_name)

# Process: Select Data
arcpy.SelectData_management(FireStationCoverage, "Polygons")
print "data selected"

# Process: Feature Class to Feature Class
arcpy.management.CopyFeatures(Polygons, out_featureclass)
print "data saved"

 

0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
Esri Regular Contributor

Retrieving an NA layer's sublayer is a common workflow in standalone python, and it is admittedly not obvious how to do it.

First, I will assume you're using ArcMap or 10.x Server since you're working with .lyr files. So, my response here is appropriate for ArcMap and not Pro.

The first problem is that the SelectData utility is a Model Builder utility only and should not be called from standalone python. It just doesn't work.

I think something like this will work:

layer_object = arcpy.mapping.Layer(FireStationCoverage_lyr)

polygons_sublayer = arcpy.mapping.ListLayers(layer_object, "Polygons")[0]

arcpy.management.CopyFeatures(polygons_sublayer, out_featureclass)

 

Note that if you switch to Pro, you can use the GetNASublayer function for a much less confusing experience. If you are doing a full Network Analyst workflow, there are actually much better and faster ways to do it in Pro using the arcpy.nax module, which eliminates the need for NA layers entirely.

View solution in original post

2 Replies
MelindaMorang
Esri Regular Contributor

Retrieving an NA layer's sublayer is a common workflow in standalone python, and it is admittedly not obvious how to do it.

First, I will assume you're using ArcMap or 10.x Server since you're working with .lyr files. So, my response here is appropriate for ArcMap and not Pro.

The first problem is that the SelectData utility is a Model Builder utility only and should not be called from standalone python. It just doesn't work.

I think something like this will work:

layer_object = arcpy.mapping.Layer(FireStationCoverage_lyr)

polygons_sublayer = arcpy.mapping.ListLayers(layer_object, "Polygons")[0]

arcpy.management.CopyFeatures(polygons_sublayer, out_featureclass)

 

Note that if you switch to Pro, you can use the GetNASublayer function for a much less confusing experience. If you are doing a full Network Analyst workflow, there are actually much better and faster ways to do it in Pro using the arcpy.nax module, which eliminates the need for NA layers entirely.

SG_Johnson
New Contributor

Thank you very much! You have got the script working for me!

0 Kudos