Export Route Layer from Closest Facility using Python

5391
5
Jump to solution
04-21-2015 01:10 AM
PriyankaMehta
New Contributor

Hi,

How should I export route layer that is a part of closest facility layer.

It works with the model builder if I use "Select Data" followed by Copy Features but if I even save that model builder as python script , it does not work.

This is the exported python script from model builder
# Local variables:
Closest_NationalParks_Facility = "Closest_NationalParks_Facility"
Routes__2_ = "C:\\Projects\\RAFT\\Closest_NationalParks_Punggol\\Routes"
CFRoutes_CopyFeatures = "C:\\Users\\mehtap\\Documents\\ArcGIS\\Default.gdb\\CFRoutes_CopyFeatures"
# Process: Select Data
arcpy.SelectData_management(Closest_NationalParks_Facility, "Routes")
# Process: Copy Features
arcpy.CopyFeatures_management(Routes__2_, CFRoutes_CopyFeatures, "", "0", "0", "0")

It does not recognize Closest_NationalParks_Facility as a dataset at the first place and gives this error

"ERROR 000732: Input Data Element: Dataset Closest_NationalParks_Facility does not exist or is not supported"

I tried modifying it by adding ".lyr" to the input. It can do select data with that but still not copy features.

# Local variables:
Closest_NationalParks_Facility = "C:\\Projects\\RAFT\\Closest_NationalParks_Facility.lyr"
CFRoutes_CopyFeatures = "C:\\Users\\mehtap\\Documents\\ArcGIS\\Default.gdb\\CFRoutes_CopyFeatures2"
# Process: Select Data
sel = arcpy.SelectData_management(Closest_NationalParks_Facility, "Routes")
# Process: Copy Features
arcpy.CopyFeatures_management(sel, CFRoutes_CopyFeatures, "", "0", "0", "0")

"sel" returns :

>>> sel
<Result 'C:\\Projects\\RAFT\\Closest_NationalParks_Facility.lyr\\Closest_NationalParks_Facility\\Routes'>

I cannot understand how to make "Routes" sub layer be recognized or anyhow export Route layer to geodatabase.

Regards,

Priyanka Mehta

0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
Esri Regular Contributor

Hello, Priyanka.  Accessing the NA sublayers in python can be a little tricky.  The Select Data tool is a Model-Builder-only tool.  You have to play with layer objects when you're using python.

The best thing for you to do is to look at the last code sample on this page: ArcGIS Help (10.2, 10.2.1, and 10.2.2)

It's for the Route solver and not Closest Facility, but it's the same idea.  The OD Cost Matrix and Service Area doc pages also have similar samples that might help.

View solution in original post

0 Kudos
5 Replies
JohannesBierer
Occasional Contributor III

If think you should first tell python where's the location of your Closest_Facility feature class, and then create a lyr out of that. Without python can't find the feature layer.

0 Kudos
PriyankaMehta
New Contributor

Actually I am trying to automate the workflow of finding closest facility in a network dataset, mostly following help file. This is my work flow

1) Make Closest Facility Layer - arcpy.MakeClosestFacilityLayer_na

2) Add Facility Locations - arcpy.AddLocations_na

3) Add Incident Locations - arcpy.AddLocations_na

4) Solve - arcpy.Solve_na

5) Save to Layer File  -  arcpy.management.SaveToLayerFile

This flow creates a layer file as first output itself (the one that I am  trying to export as a feature class - "Closest_NationalParks_Facility"). I do not know a way of avoiding layer creation at the first place for closest facility analysis.

Please let me know if there is another way of doing it.

0 Kudos
JohannesBierer
Occasional Contributor III

As I mentioned you have to define your feature class first, before you can make a layer file.

So simply write:

Closest_NationalParks_Facility = "YourPathtotheFeatureClass"

0 Kudos
MelindaMorang
Esri Regular Contributor

Hello, Priyanka.  Accessing the NA sublayers in python can be a little tricky.  The Select Data tool is a Model-Builder-only tool.  You have to play with layer objects when you're using python.

The best thing for you to do is to look at the last code sample on this page: ArcGIS Help (10.2, 10.2.1, and 10.2.2)

It's for the Route solver and not Closest Facility, but it's the same idea.  The OD Cost Matrix and Service Area doc pages also have similar samples that might help.

0 Kudos
PriyankaMehta
New Contributor

Thanks a ton Melinda ! This is exactly what I needed.

Cheers,

Priyanka

0 Kudos