5

I am trying to use 2 geometry objects to check if a line is within a (rather complex) polygon. The geometry check always fails, even if the line is clearly within the polygon. I use the following code:

#get the polygon
sC = arcpy.SearchCursor(polygon)
for row in sC:
    polygonshape = row.Shape
del sC

#creation of the polyline(s):

feature_info = [[[p1.x, p1.y], [x, y]], [[p2.x, p2.y], [x, y]], [[p3.x, p3.y], [x, y]]]
features = []
for feature in feature_info:
    array = arcpy.Array()
    for point in feature:
        P = arcpy.Point()
        P.X = point[0]
        P.Y = point[1]
        array.add(P)
    features.append(arcpy.Polyline(array))

#the actual check:
for item in features:
    in_poly = polygonshape.contains(item) #this is always false

Any ideas why this fails? I considered, that one of the layers (polygon) has a coordinate system, while the other doesn't but according to another answer here (which I can't find right now) coordinate systems are supposedly ignored for this kind of checks. Could the size/complexity of the Polygon be an issue?

If I create a Feature Class of the line and do a spatial selection (within) afterwards, it works as expected. Also: this behaviour returns unexpected results for contains as well as within.

segment in polygon the whole polygon

  • 4
    "coordinate systems are supposedly ignored for this kind of checks"? I highly doubt if that is correct.. – vinayan Oct 21 '14 at 9:53
  • 1
    just a quick remark, but I think that you should unindent features.append(arcpy.Polyline(array)) – radouxju Oct 21 '14 at 9:55
  • @radouxju thanks, you're right. That was a formatting error when I copied to SE. – Ben the bear Oct 21 '14 at 10:12
  • @vinayan I got this information from this post: gis.stackexchange.com/questions/52936/… After some testing I do however believe this might indeed be the source of my problems. After initializing the polyline with a spatial_reference the problem seems to go away. If you can provide an answer or source, as to why this causes problems (since the coordinates are in the right CS, it's just not defined), I will accept as answer. – Ben the bear Oct 21 '14 at 10:41
  • Far from asserting that coordinate systems don't matter, that question demonstrates that they do. In fact, the entire coordinate reference must be identical. This has been the case since before ArcPy, or even ArcObjects, existed. – Vince Oct 21 '14 at 10:51
2

My methodology to accomplish this task has always been using ArcGIS's selection tools on feature layers.

Something like this:

arcpy.MakeFeatureLayer_management (polygon, "lyr")

#The check
for item in features:
    arcpy.SelectLayerByLocation_management ("lyr", "CONTAINS", item)
    #Check for selection, i.e. item is contained within polygon
    if arcpy.Describe ("lyr").FIDSet:

item might not be a valid input for a SelectLayerByLocation select_feature, but if not conversion to feature class will do the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.