2

I'm trying to collect the 2D area, 3D area and volume of a given basin at 0.5 meter increment in plane height, but since I want to graph the relationship between plane height and the areas and volume, i want to collect many many points.

Doing this one by one is tedious. I'm wondering whether there is a python code that allows me to process a whole bunch of plane heights at once and congregate results in one .txt file or excel file.

currently, the code I'm using is

import arcpy
text="c:\\document\riverside\text.txt"
arcpy.SurfaceVolume_3d("watersheddem",text,"BELOW",250,1,1)

but this code only allows me to read result on the side bar, when i actually check the text.txt file it is empty even when my arcpy.surfacevolume_3d was ran successfully.

  • 1
    Welcome to GIS SE! As a new user be sure to take the Tour. It may just be the way you have formed your pathname - try text=r"c:\document\riverside\text.txt". – PolyGeo Sep 4 '15 at 4:00
  • see your GeoNet post for further comments – user681 Sep 4 '15 at 4:08
2

This tool will loop through a range of user-defined values and write the outputs to a text file. You can get the Arctoolbox tool version here https://github.com/gerry1138/ArcGIS-Loop-Surface-Volumes

try:
    import sys, arcpy, traceback
    arcpy.CheckOutExtension("3D")

    arcpy.AddMessage( "Multi-Volumes for ArcGIS 10")
    arcpy.AddMessage("Loop the 3D Analyist Surface Volume tools for a range of values...")


    def GetValues(themessage, x):
        thevaluesatx = str(x)
        themessage = themessage.split("\n")
        thevalues = themessage[2].split("  ")
        for item in thevalues:
            thevalue = item.split("=")
            thevalue = thevalue[-1]
            thevaluesatx = thevaluesatx + "," + str(thevalue)
        thevaluesatx = thevaluesatx +"\n"
        arcpy.AddMessage(thevaluesatx)
        return thevaluesatx


    #My_txt = r"Z:\GISpublic\GerryG\aaaa.txt"
    #My_surface= r"I:\SurfaceModels\USGS_DEM\ldeltagrid5ft"
    #direction = "above"
    #startingplane = 0
    #z = 1
    #y = .5
    #q = 2

    My_surface= arcpy.GetParameterAsText(0)
    My_txt = arcpy.GetParameterAsText(1)
    direction = arcpy.GetParameterAsText(2)

    startingplane = float(arcpy.GetParameterAsText(3))
    z = float(arcpy.GetParameterAsText(4))
    graduations = float(arcpy.GetParameterAsText(5))
    endingplane = float(arcpy.GetParameterAsText(6))

    direction  = direction.upper()
    f = open(My_txt,'a')
    f.writelines(r"elevation, 2d_area, 3d_area, volume" + "\n")

    arcpy.AddMessage("Please wait, this script is processing...")

    if direction == "BELOW":
        arcpy.AddMessage("startingplane = " +str(startingplane))
        arcpy.AddMessage("endingplane = " +str(endingplane))

        while startingplane > endingplane:
            arcpy.AddMessage("Getting Results...")
            result = arcpy.SurfaceVolume_3d(My_surface, "", direction, startingplane, z)
            thevaluesatx = GetValues(result.getMessages(0), startingplane)
            f.writelines(thevaluesatx)

            startingplane = startingplane - graduations

    if direction == "ABOVE":
        arcpy.AddMessage("Executing above...")
        x = startingplane
        while startingplane < endingplane:
            result = arcpy.SurfaceVolume_3d(My_surface, "", direction, startingplane, z)
            thevaluesatx = GetValues(result.getMessages(0), startingplane)
            print thevaluesatx
            f.writelines(thevaluesatx)

            startingplane = startingplane + graduations


    f.close()

    arcpy.AddMessage("Finished without errors!")

except arcpy.ExecuteError: 
    msgs = arcpy.GetMessages(2) 
    arcpy.AddError(msgs)  

except:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)

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.