1

The larger goal with this code is to create a temporary polygon based on the calculated coordinates (which it does), then to see if the polygon intersects with the "unobscured_FC". After I run the select by location the polygon will be deleted, and all sorts of other hilarity will ensue. I only need the temporary polygon for the select.

Now, I've read that you can't run arcpy.SelectLayerByLocation_management() on shapefiles or feature classes, which is why I tried to make feature layers from the two. However, despite that, I'm getting this error:

ExecuteError: Failed to execute. Parameters are not valid.
The value cannot be a feature class
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).

I've searched thoroughly for an answer to this problem, and they all appear to be telling me that I need to do exactly what I'm already doing.

How am I failing here to make both of these into feature layers?

Code below...

unobscured_FC = r"C:\example.gdb\example_FC"
arcpy.MakeFeatureLayer_management(unobscured_FC, "unobscured_lyr")

#These coordinates are calculated above in a code block I've excluded...
coordList = [[[Xmax,Ymax], [Xmin,Ymax], [Xmin,Ymin], [Xmax,Ymin]]]
point = arcpy.Point()
array = arcpy.Array()
featureList = []

for feature in coordList:
    for coordPair in feature:
        point.X = coordPair[0]
        point.Y = coordPair[1]
        array.add(point)     
    array.add(array.getObject(0))    
    polygon = arcpy.Polygon(array)    
    array.removeAll()
    featureList.append(polygon)

tempShape = os.path.join(sys.path[0], "temp.shp")
arcpy.CopyFeatures_management(featureList, tempShape)
arcpy.MakeFeatureLayer_management(tempShape, "tempShape_lyr")

arcpy.SelectLayerByLocation_management("unobscured_lyr", "INTERSECT",   "tempShape_lyr", "", "NEW_SELECTION")
  • 1
    The first line starts with " and ends with ', one or the other. Both are valid string delimiters in python but you must match them, if you start with " you need to end with ". – Michael Stimson Sep 16 '15 at 23:08
  • @ Michael Miles-Stimson I fixed it. It's not like that in the actual code. I had to change the file path when posting. – CSB Sep 17 '15 at 13:49
  • I don't know if MakeFeatureLayer_management will work on shapefiles. Can you copy your temp polygon into a temp GDB instead? – Mintx Sep 17 '15 at 21:10
  • @Mintx I will try that and get back to you. Thanks for the suggestion! – CSB Sep 17 '15 at 21:16
  • Make feature layer works with any feature class, shapefiles included. What line gives you the error? I"m thinking it's the Copy Features... you're trying to insert a geometry into the temp shape which isn't quite right. It looks like you're making a polygon (or polygons) from a list of points and then you want to use that to select from unobscured_FC is that right? – Michael Stimson Sep 17 '15 at 21:37
1

The problem is that CopyFeatures doesn't want to take a list of polygons and make it into a feature class, it might work that way but as the spatial reference isn't set the polygon objects are incomplete. The help docs say the input must be either Feature Layer;Raster Catalog Layer.

Reading the writing new Geometries in the Esri help docs will give you a better understanding, but I think you've seen that page before as you're creating the geometries just fine.

This is how I would normally create a geometry from known points:

SR = arcpy.SpatialReference(28356) # this example is MGA94 Zone 56 (Brisbane, Sydney, Newcastle) change to match your data
tempShape = os.path.join(sys.path[0], "temp.shp")

# Create a new empty feature class
arcpy.CreateFeatureclass_management(sys.path[0], "temp.shp","POLYGON",spatial_reference=SR)

coordList = [[[Xmax,Ymax], [Xmin,Ymax], [Xmin,Ymin], [Xmax,Ymin]]]
point = arcpy.Point()
array = arcpy.Array()

with arcpy.da.InsertCursor(tempShape,"SHAPE@") as ICur:
    for feature in coordList:
            for coordPair in feature:
                    point.X = coordPair[0]
                    point.Y = coordPair[1]
                    array.add(point)     
            array.add(array.getObject(0)) # that's right, add the first to the last

            # make a polygon from the array
            polygon = arcpy.Polygon(array)
            polygon.spatial_reference = SR # set spatial reference

            ICur.insertRow(polygon) # insert it into the feature class

            array.removeAll() # get ready for the next one

Then continue your code as normal to select layer by location.

  • Right on. It might take me a day or two to test this out but I'll get back to when I do. And, thanks for the info. I've been through a number of the ESRI help pages but after a while my eyes start to glaze over... – CSB Sep 17 '15 at 21:57
  • Been there, done that, got the bags under my eyes to prove it. Hope it works for you - it's only part code, not a full program, so I can't test it by executing it.. I'll leave that up to you. Let me know how it goes. – Michael Stimson Sep 17 '15 at 22:00
  • I'm working on this now, still haven't given up, and am getting the error "argument must be sequence of values". trying to figure out what that's all about. – CSB Sep 23 '15 at 20:56
  • On what line? Seeing as this is a different error I recommend to post the updated code, the full error message and any new details in a new question and reference this post for history. – Michael Stimson Sep 23 '15 at 21:30
  • Apologies... I've fiddle with it so much now I don't have the exact same code. It was in the ICur.insertRow(polygon) line. – CSB Sep 23 '15 at 21:40
1

This ended up working and returning a count of 1 - which it should have:

unobscured_FC = r"C:\example.gdb\example_FC"
arcpy.MakeFeatureLayer_management(unobscured_FC, "unobscured_lyr")


SR = arcpy.Describe(unobscured_FC).spatialReference
tempShape = os.path.join(sys.path[0], "temp.shp")
arcpy.CreateFeatureclass_management(sys.path[0], "temp.shp","POLYGON",spatial_reference=SR)
array = arcpy.Array()
point = arcpy.Point()
featureList = []

arcpy.env.overwriteOutput = True
for feature in coordList:
    for coordPair in feature:
        point.X = coordPair[0]
        point.Y = coordPair[1]
        array.add(point)     
    array.add(array.getObject(0))    
    polygon = arcpy.Polygon(array)    
    array.removeAll()
    featureList.append(polygon)

arcpy.CopyFeatures_management(featureList, tempShape)
arcpy.MakeFeatureLayer_management(tempShape, "tempShape_lyr")

arcpy.SelectLayerByLocation_management("unobscured_lyr", "INTERSECT",   "tempShape_lyr", "", "NEW_SELECTION")

polyCursor = arcpy.SearchCursor("unobscured_lyr")
polyRow = polyCursor.next()
count = 0


while polyRow:
    count = count + 1
    polyRow = polyCursor.next()

print count

After adding the spatial reference it still wasn't working. But once I added the overwrite feature it did. Not sure why that is. I'd love an explanation!

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.