How to automate converting feature dataset polygons to lines using python

3384
13
Jump to solution
12-20-2015 11:42 AM
CarolLiu
New Contributor II

I have a feature dataset, polygons,containing about 50 feature class. I need to convert them to lines. I was able to do it using PolygonToLine_management to perform the task for individual feature class in ArcMap 10.0 python window. Does "PolygonToLine_management" only work for individual feature class, or can work for the dataset at once? I tried to use for-in loop, it did not work.

Any advice appreciated!

0 Kudos
1 Solution

Accepted Solutions
NeilAyres
MVP Alum

In your code you are not naming the output. So I am not sure what it is calling the output but it is probably doing all 50 fc's but just overwriting them each time.

Try something like this :

import arcpy
from arcpy import env
env.workspace = "C:/Data/City of Oleander.gdb/FireBoxMaps"
fcFireboxmaps = arcpy.ListFeatureClasses()
for fc in fcFireboxmaps:
    lineout = str(fc) + "_line"
    arcpy.PolygonToLine_management(fc,"C:/Documents/ArcGIS/Default.gdb/" + lineout ,"IDENTIFY_NEIGHBORS")

View solution in original post

13 Replies
DanPatterson_Retired
MVP Emeritus

What was your code?  You have to cycle through the featuredataset to get the associated featureclasses.  Some tips on setting up either a model or a script are here... Using geoprocessing options to control tool execution—Help | ArcGIS for Desktop  and the arcpy structure and info is here... What is geoprocessing?—Help | ArcGIS for Desktop

CarolLiu
New Contributor II

import arcpy

from arcpy import env

env.workspace = "C:/Data/City of Oleander.gdb/FireBoxMaps"

fcFireboxmaps = arcpy.ListFeatureClasses()

for fc in fcFireboxmaps:

    arcpy.PolygonToLine_management(fc,"C:/Documents/ArcGIS/Default.gdb","IDENTIFY_NEIGHBORS")

The above was code. FireBoxMaps is feature dataset, containing about 50 feature classes. I wasn't able to figure out the output feature for PolygonToLine_Management. The above code only output one feature class, not all feature classes.

Thanks, Dan

0 Kudos
NeilAyres
MVP Alum

In your code you are not naming the output. So I am not sure what it is calling the output but it is probably doing all 50 fc's but just overwriting them each time.

Try something like this :

import arcpy
from arcpy import env
env.workspace = "C:/Data/City of Oleander.gdb/FireBoxMaps"
fcFireboxmaps = arcpy.ListFeatureClasses()
for fc in fcFireboxmaps:
    lineout = str(fc) + "_line"
    arcpy.PolygonToLine_management(fc,"C:/Documents/ArcGIS/Default.gdb/" + lineout ,"IDENTIFY_NEIGHBORS")
CarolLiu
New Contributor II

Many thanks, Neil! The code worked fine after a minor change. I am trying to make it clear for anybody who might have the same question in the future.

For the output, I had to remove .gdb from "Default.gdb" in order to make it work. So, the output would be "C:/Documents/ArcGIS/Default". I also noticed one interesting thing. All output files were shapefiles (ended with .shp), instead of feature class. Meanwhile, All output files fell outside .gdb. I would definitely like to find out while I get to know more about Python for GIS.

Again, thank Neil, thank Dan!

0 Kudos
NeilAyres
MVP Alum

The .gdb part indicates a file geodatabase.

As you removed that part it must have put the outputs into a file system folder and made them shapefiles.

Rather than relying on the default, it is better to set up an output specifically for the task.

Make a fgdb using ArcCatalog in a project folder, so you know where everything is going.

Glad to have been of help.

Enjoy.

RebeccaStrauch__GISP
MVP Emeritus

If you only have the 50, and it's a one time thing, you could do it old school and grab the python snippet in the "result" tab....and then do the for...loop for a list of file names.  ArcGIS Desktop  Is the 10.0 help. I'm not sure if the batch option was available back then or not.

edit.  Might also want to look thru this PDF  http://users.clas.ufl.edu/forrest/gis/lectures/2013-10-22%20-%20Data%20and%20Batch%20Geoprocessing%2...

CarolLiu
New Contributor II

Thanks, Rebecca.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Carol, if your question was answered, remember to mark any answers that were helpful, and mark the one that best resolved your question as answered. That will close this thread.  of course, if you still have questions, check out Posting Code blocks in the new GeoNet  (for future questions too).

0 Kudos
CarolLiu
New Contributor II

Thanks for telling me, Rebecca. I already marked "best answer".  I am new to this forum. Are there any restrictions or policies?

0 Kudos