Select to view content in your preferred language

Examples of Geometry object - polygon and intersect

4332
6
Jump to solution
06-10-2015 01:07 PM
LauraBlackburn
Deactivated User

I have many polygon layers and one roads layer.  I am trying to essentially clip the roads layer by each polygon in my polygon layers (one polygon at a time) in order to retrieve the total length of roads within each polygon in my list of polygon layers.  When I use arcpy.Clip_analysis this takes a very long time.  So, I have been pointed to the geometry objects on the polygon class.  I typically use the code snippets in the help to aid in my code writing.  However, with this example the code provided seems useless to me.  Does anyone have a good example of how to use the geometry object to intersect another layer and return the length of the lines that fell inside the polygon?

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Intersect will provide you nearly what you need. You will get a copy of each section of road within each polygon. If the polygons overlap over a section of road, you will get overlapping sections of road. Each road section will have the attributes of the polygon it intersected with, so you can run Summary Statistics or Dissolve on the polygon FID to get the total length inside each polygon.

View solution in original post

6 Replies
DarrenWiens2
MVP Honored Contributor

Before getting in polygon objects, have you tried the Intersect tool? This seems to be what you're after.

0 Kudos
DanPatterson_Retired
MVP Emeritus

what did you try so far?  Did you try the intersect to get the polylines within the cutting polygon first? You should be able to follow your regular procedure.  If you have an Advanced license level you could look at Tabulate Intersection

LauraBlackburn
Deactivated User

Here's what I tried before...

seqField = "Sequence"

where_clause = arcpy.AddFieldDelimiters("clipLayer", seqField)+ sN

arcpy.SelectLayerByAttribute_management("clipLayer", "NEW_SELECTION", where_clause)

              

# Clip roads w/ selected polygon - for this I will need a search cursor

clipRoadsShp = arcpy.Clip_analysis(Road, "clipLayer", "in_memory")

              

# Use geometry/length to get the total length of clipped roads(in meters)

print "calculating road lengths...."

print 'Clip complete '  + strftime("%Y-%m-%d %H:%M:%S")

g = arcpy.Geometry()

geometryList = arcpy.CopyFeatures_management(clipRoadsShp, g)

length = 0

     for geometry in geometryList:

             length +=geometry.length

I think that the where-clause and the clipping really slowed the process down and my total processing time was going to be weeks!  Also, my polygons overlap and while I do have the advanced license, I am not sure the tabulate intersect tool would work because of the overlapping.  I will try the intersect tool and see if that helps.  I was thinking that creating a bunch of clip or intersect layers was a waste of space & time because I really only need them to compute the length of roads inside the polygon and that's it.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Intersect will provide you nearly what you need. You will get a copy of each section of road within each polygon. If the polygons overlap over a section of road, you will get overlapping sections of road. Each road section will have the attributes of the polygon it intersected with, so you can run Summary Statistics or Dissolve on the polygon FID to get the total length inside each polygon.

LauraBlackburn
Deactivated User

Thanks Darren and Dan!  It's awesome to get responses so quickly.  Darren you were right on.  This is exactly what I was looking for, I am currently running the code to see how much time I'll save vs. clipping and I think it will be hours compared to weeks.  So, my code is definitely speeding up. Dan thanks for pointing out a new tool that I never knew existed!

0 Kudos
RichardFairhurst
MVP Honored Contributor

Laura:

The Intersect tool Darren encouraged you to try is the same tool I wanted you to use in the original thread you had created for this code.  I agree that this approach is likely to save a huge amount of time over processing intersect on each geometry separately.  My experience shows that the Esri programmers have optimized their own geoprocessing tools that have a specific function much more than they have the python geometry methods which have to be adaptable enough to be applied to all sorts of application situations the programmers have no control over.