I'm trying to create a tool that will calculate the min depth and the max depth of a contour polygon, based on the value of the depth attributes of the surrounding contour lines. This is what I have so far.

def main():

if __name__ == '__main__':
# Import system modules
import arcpy
import arcpy.da
import arcgisscripting
#Set environment settings
arcpy.env.workspace = r"C:\Working"
arcpy.env.overwriteOutput = True
gp = arcgisscripting.create()

area = "conta"
line = "contl"

cursor = arcpy.UpdateCursor(area)
for row in cursor:
    arcpy.Select_analysis(row, "C:\Working\Minnesota\Shape\i_area.shp")
    i_area = r"C:\Working\Minnesota\Shape\i_area.shp"
    arcpy.SelectLayerByLocation_management(line, i_area)

    pcursor = arcpy.da.SearchCursor(line, "DEPTH")
    minDepth =10000
    for row in pcursor:
        if int(row[0]) < int(minDepth):
            minDepth = int(row[0])

    print minDepth

    row.setValue('DEPTHMIN', minDepth)

I keep getting this error:

  Runtime error 
Traceback (most recent call last):
  File "<string>", line 34, in <module>
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\analysis.py", line 84, in Select
    raise e
RuntimeError: Object: Error in executing tool

I think my problem has to do with trying to treat the row instance like its own feature class in order to perform the line selection. I have tried several different ways of isolating the instance into a feature class or just selecting by it. I think the arcpy. functions wont work with just a instance in a for loop.

Does anyone have other ideas?

I thought about isolating the instance outside of a for loop or cursor, but then how do I work through the shapefile?

Maybe I should try a different approach?

The path for contl is pretty much c:\working\contl. I'm not familiar with how to release cursors.

There are polygons between polylines. We use them to for a layer to add shading to for deeper water (conta is really bathymetry).

I looked up slice, and I dont see how I can use it in this case. We usually get our contour data from pubic sources, already rendered. I think I could use that method when we use a raster to get our lines.

I thought the problem was that I was trying to treat an instance in a for loop like a feature class.

Is there something else I'm missing?

  • You're mixing gp and arcpy, not a good idea. I'd stick with arcpy (being the newer) geoprocessor. I suspect it's got something to do with your inputs.. line = "contl", arcpy.env.workspace = r"C:\Working" so is the full path to your contour lines c:\working\contl? Also you're not releasing your cursors which can lead to locking situations. – Michael Stimson Mar 16 '15 at 21:50
  • your contours are polygons? that would be pretty unusual data. Are you sure your data is not a polyline. – Brad Nesom Mar 16 '15 at 21:50
  • @BradNesom, they're called hypsometric areas. Not used much any more, possibly because they're a pain to create and attribute with the loose Esri topology model, possibly also because their funtionallity is easy to emulate with tools like 'slice'. I used to build them in ArcInfo workstation in AML. – Michael Stimson Mar 16 '15 at 21:51
  • I am familiar with them. Is that actually what Zach is working with. – Brad Nesom Mar 16 '15 at 21:54
  • As a new user be sure to take the Tour because SE sites operate quite differently to, and I think much better than, discussion forums and other Q&A sites. – PolyGeo Mar 17 '15 at 2:17

Selection and an updatecursor will do the trick here. It looks like you're working with feature layers (aka layers in the table of contents in arcpy), so I'll skip the usual MakeFeatureLayer_management which would be needed if this code was to be applied outside of an ArcGIS environment. That is, within an IDE.

See comments:

area = "conta"
line = "contl"

#Get OID field name for sql where clause
OIDfld = arcpy.Describe(area).OIDFieldName

#iterate rows
cursor = arcpy.da.UpdateCursor (area, ["OID@", "DEPTHMIN"])
for row in cursor:

    #sql to select feature
    sql = '"{0}" = {1}'.format(OIDfld, row[0])
    #select by attribute single feature
    arcpy.SelectLayerByAttribute_management (area, "", sql)

    #select intersecing contour lines
    arcpy.SelectLayerByLocation_management (line, "", area)

    #list of contour values with use of list comprehension
    depths = [r[0] for r in arcpy.da.SearchCursor (line, "DEPTH")]

    #minimum value
    minimum = min(depths)
    if minimum < 10000:
        minimum = 10000

    print minimum

    #update row with minimum depth
    row[1] = minimum

#clean up
del row
del cursor

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.