Select to view content in your preferred language

How can I convert an arcpy geometry object to a shapefile?

5725
12
02-26-2011 09:48 AM
SeldaSamiri
Emerging Contributor
Hi there,

I'm quite new to arcpy, so maybe this is stupid question:

I have a script that produces streams as polylines for every branch one line. I use "unsplit line" to merge them to one line with a geometry object as output. This is done several times (several streams) so in the end I have a list with several geometry objects that contain polylines.
Now I want to convert these geometries to a shapefile or geodatabase feature. How can I do this?

CopyFeatures_managements needs a shapefile/Feature as input. Is there a similar tool/opportunity for geometry objects?

Thanks for any help!

Selda
0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus
0 Kudos
SeldaSamiri
Emerging Contributor
I was hoping that there is an easier and faster way...
0 Kudos
ChrisFox3
Frequent Contributor
Hi Selda,

You should be able to pass a list of geometries as an input to the copy features tool. In tools that take a feature class or feature layer you can generally subsititute them with geometry objects.
0 Kudos
SeldaSamiri
Emerging Contributor
Hi Chris,

I thought that, too. But what I get is an error message: "Input Data Element: Dataset in_memory\f1F06F212_6D71_42CA_B754_D6EA38AB0E4B does not exist or is not supported"
But it's there and it was the output of the dissolve tool, so it should be ok?!

Any idea?

Selda
0 Kudos
ChrisFox3
Frequent Contributor
Are you doing something like this in your code?

import arcpy
g = arcpy.Geometry()
geoList = arcpy.Dissolve_management(r'C:\Data\ArcGIS\TemplateData.gdb\USA\states', g)
arcpy.CopyFeatures_management(geoList, r'C:\Data\ArcGIS\TemplateData.gdb\USA\statesDiss")
0 Kudos
SeldaSamiri
Emerging Contributor
Ah, thanks, I did not know, that I have to assign the result to a variable! I tried to use 'g' as input to the arcpy.CopyFeatures_management tool! Now it works.

But now I have another problem:
Inside a loop I append the polylines from the dissolve tool to a list:
 
line_geom = arcpy.Geometry()
lines = arcpy.Dissolve_management(newline, line_geom)
upstream.append(lines[0])


print upstream gives this:
 [<Polyline object at 0x107918d0[0x104daaa0]>,
 <Polyline object at 0xbebd770[0x104ce278]>,
 <Polyline object at 0xbdf6cf0[0xba9c980]>,
 <Polyline object at 0xbe0e750[0x104dad58]>,
 <Polyline object at 0x573d770[0x104ce338]>]


When I now use this list as input to the arcpy.CopyFeatures_management tool I get a shape-file that contains the first polyline in the list 5 times instead of 5 different polylines.

But when I make a shapefile out of every single line from the list like this:
for i in range (0, len(upstream)):
    ln = upstream
    arcpy.CopyFeatures_management(ln, "line" +str(i))


then I get the five different lines correctly. So they are there in the list and they are correct.

How could that happen?


Thank you for your help!
Selda
0 Kudos
ChrisFox3
Frequent Contributor
I am not sure, could you share all the code from the loop used to create the list of geometries to the CopyFeatures function call?
0 Kudos
SeldaSamiri
Emerging Contributor
this is not the original code as there are many steps inbetween, but it gives the general way I'm doing it.

The overall task was to extract the part of a stream upstream of several sampling sites. I'm doing this in several steps with comparing the elevation in a dem at the stream vertices. As the stream contains of several branches, I need every branch of the upstream part.

In the end I have a list of objects that contains the x/y coordinates for the vertices of the new polyline feature I want to make. Each branch has its own pointlist.

That's the way how I make the polylines:

upstream =[] # for each sampling site a polyline shall be added to this list (one stream contains several branches that are dissolved to one polyline with the dissolve tool in the end)


# do for each sampling site:

for streamsegment_list in sampling_list:


    lineList = []  # for each branch a polyline shall be added to this list


    # do for each branch of the actual stream:

    for segmentlist in streamsegment_list:  # streamsegment_list contains the points for all branches of one stream, segmentlist gives one branch


        pointList = arcpy.Array()         # the points for one branch of the stream-polyline shall be added to this list


        # do for each point of the actual branch:

        for segment in segmentlist:  # segmentlist gives one branch of the stream, segment is an object that contains the x/y coordinates

            PS_pt = arcpy.Point(segment.firstx, segment.firsty)   #firstx and firsty contain the coordinates for the points

            pointList.add(PS_pt)  # add the point to the list


        ln = arcpy.Polyline(pointList)  # make a polyline out of the pointlist (one branch)

        lineList.append(ln)  # append this polyline to the linelist

        ln = None


    line_geom = arcpy.Geometry()

    lines = arcpy.Dissolve_management(lineList, line_geom) # dissolve the branches of one stream to one polyline

    upstream.append(lines[0]) # append this polyline to the upstream-list


# make a new geodatabase feature class for the streams:

arcpy.CopyFeatures_management(upstream, "streams")



but then instead of 5 different polylines I get five times the first polyline in the upstream list as I described.
0 Kudos
ChrisFox3
Frequent Contributor
And you are saying that after the code to create the geometry list that when you loop through the geometry list and process each individually that you get 5 feature classes each with one feature with unique geometry and if you process the whole geometry list you can one feature class with 5 features each with the same geometry? If that is the case I don't really have any idea what could be causing it and would recommend you contact Technical Support to troubleshoot it further.

# make a new geodatabase feature class for the streams:

for i in range (0, len(upstream)):
    ln = upstream
    arcpy.CopyFeatures_management(ln, "line" +str(i))

arcpy.CopyFeatures_management(upstream, "streams")


Also just to verify you might want to try running the code below just to get more of an idea of the geometry in the list. If the extents are different then the geometries you are creating are different and I am not really sure why they would not be outputed that way when passed to Copy Features.

for g in upstream:
    print "Min X: {0}, Min Y: {1}, Max X: {2}, Max Y: {3}".format(g.extent.XMIN, g.extent.YMIN, g.extent.XMax, g.extent.YMax)
0 Kudos