Can't get geometry.overlaps etc to work with Polylines

584
1
01-21-2011 01:43 AM
RobinWilson
New Contributor
Hi,

I am trying to calculate which polylines in my dataset overlap with each other, and can't seem to get it to work. The basic procedure that I'm using is to loop through my features, and then have a second inner loop that loops through the features again and finds out which ones the first feature overlaps with. However, I end up with nothing showing as overlapping (even though some of my features are definitely overlapping). I tried this with some of the other geometry methods such as touches, within, crosses etc, and they also seem to give strange results. For example, I get nothing showing as touching, overlapping, crossing etc, but then also nothing showing as disjoint - which doesn't seem to make sense.

My python code is below, it's quite possible that I've made an elementary error:


import arcpy
input_lines = "D:\\Data\\DunesGIS\\TestOverlaps.shp"

rows = arcpy.SearchCursor(input_lines)

shape_name = arcpy.Describe(input_lines).shapeFieldName

for row in rows:
    shape = row.getValue(shape_name)
       
    comparison_rows = arcpy.SearchCursor(input_lines)
    for comparison in comparison_rows:
        comparison_shape = row.getValue(shape_name)
        #print comparison_shape
        if shape.overlaps(comparison_shape):
            print "FOUND IT!!"

    del comparison_rows
   
print "Done"

del rows
Tags (2)
0 Kudos
1 Reply
KimOllivier
Occasional Contributor III
It seems that geometry objects are not being properly returned from the cursor. If you create a geometry object and populate a geometry list then the comparison can work. In my test of 4 lines, 2 and 3 cross and 1 and 4 have a shared end.
Only crosses() produced a True. A geometry list is not ideal because there are no attributes, but it should be a clue for your problem.

# different method

import arcpy
g = arcpy.Geometry()
g1 = arcpy.Geometry()
g2 = arcpy.Geometry()
gList = arcpy.CopyFeatures_management("c:/data/sample.gdb/exlin", g)

g1 = gList
g2 = gList
n=0
for f1 in g1:
    n+=1
    m=0
    print
    for f2 in g2:
        m+=1
        print n,m,f1.overlaps(f2),f2.crosses(f1)

1 1 False False
1 2 False False
1 3 False False
1 4 False False

2 1 False False
2 2 False False
2 3 False True
2 4 False False

3 1 False False
3 2 False True
3 3 False False
3 4 False False

4 1 False False
4 2 False False
4 3 False False
4 4 False False


I don't particularly like this idea, it seems to be reinventing GIS in Python scripts. Would it not be better to use topology if you have a large dataset?
0 Kudos