Save as Shapefile

1463
6
09-27-2011 02:59 AM
JamesSmith5
New Contributor
Hi,

I'm sure that this is a very simple question, but I can't find the answer unfortunately. I have wrote  a short Python script to do some geoprocessing, Network Analysis to be exact, and I want the routes that have been created from this process to be outputted as a Shapefile and saved to my PC - but using Python rather than right clicking and doing 'Data Export'. How do I go about this please? What is the Syntax?

Thanks

James
Tags (2)
0 Kudos
6 Replies
ClaireParsons
New Contributor
Hi James

Well, I've managed to write the code that will loop through all your layers in the mxd remembering there names 'ListLayers' but I'm flumaxed as to how to export them.  The saveas property of Layer exports everything to a layer file rather than a shapefile.

sorry, I can't be more helpful.

Claire
0 Kudos
JamesSmith5
New Contributor
Hi James

Well, I've managed to write the code that will loop through all your layers in the mxd remembering there names 'ListLayers' but I'm flumaxed as to how to export them.  The saveas property of Layer exports everything to a layer file rather than a shapefile.

sorry, I can't be more helpful.

Claire


Hey Claire,

Thanks loads for trying to help. To be honest, maybe I'm asking the wrong question. At the end of using Network Analyst I'm given a 'Route'. If I right click on this, I can view the attribute table of the Route (there is just one row in my example but there could be more if I was using the Closest Facility tool instead). This table has has attributes such as FirstStopID, LastStopID, RouteName etc. What I want to do is to save this table somewhere, so that the next piece of my Python code can access the fields and do some additional processing.

If anyone can suggest a way forward that would be great please.

Thanks

James
0 Kudos
StacyRendall1
Occasional Contributor III
Claire, this or the stuff below might help you: arcpy.featureClassToShapefile_conversion()

James (just working off the top of my head - I think this is correct - will have to check it out tomorrow and get back to you), you can select the Routes simply by adding "\\Routes" to the end of the layer name, i.e.:
arcpy.Solve_na(outNALayer)
routes = outNALayer + '\\Routes'

then copy the selected routes to a new shapefile with:
arcpy.Copy_management(routes, "C:\\output\\routesOutput.shp")


You may also want to look into arcpy.CopyRows_management as you are just interested in the table...
0 Kudos
JamesSmith5
New Contributor
I'll try this out later today - thank you very much!
0 Kudos
JamesSmith5
New Contributor
Hi guys,

Some feedback...

- arcpy.featureClassToShapefile_conversion("Sample_Routing\Routes", "E:\\")
This appears to work, but then says it had a problem naming the output. Which is odd, as this isn't even a parameter in the code.

- arcpy.CopyFeatures_management("Sample_Routing\Routes", "test")
I didn't think that this would work, but it does. It makes a shapefile of the Routes and saves them to the default location. Note, there's no need for .shp in the code.

-arcpy.FeatureClassToFeatureClass_conversion(in_features, out_path, out_name)
- Although rather strangely named (how is feature to feature a conversion?), this also makes a shapefile as its output.

So in summary, thanks!

James
0 Kudos
StacyRendall1
Occasional Contributor III
Glad to hear it worked, James!

Just one thing, I think you need either two back slashes or one forward slash in your paths, i.e.:
"Sample_Routing\\Routes" or "Sample_Routing/Routes" not "Sample_Routing\Routes"

Cheers
0 Kudos