# set the workspace to whatever location this code is in. So as long as the code and
# the database are in the same place, this code can be run.
import os
#arcpy.env.workspace = os.getcwd()
# Allows Arc to overwrite files if a file with the same name already exists.
arcpy.env.overwriteOutput = True
# Since the initial data processing may have a few steps that can't be done in Python,
# this is what to run once you have the points layer with the necessary fields.
# Set the layers we're working with.
gaugePoints = "Testgroup1" #This is the river gauge points feature class.
rivers = "courses" #This is the rivers feature class
elevationLayer = "SRTM90_V4.elevation.tif" #This is the elevation raster.
def calculateMaxHead(RFID, GFID):
riverIDCDA = str(RFID) #Converts parameters to a string.
pointName = str(GFID)
# These expressions are a bit tricky. Check the tables and adjust accordingly.
# Preferably, use fields that are numeric types.
riverClause = ''' "IDCDA" = '''+riverIDCDA #For picking out a single river
pointClause = ''' "FID" = '''+pointName #For picking out a single point
#Sets objects and file names.
river = "River1"
gauge = "Gauge1"
bufferFileName = "Rings"
headFileName = "HeadPoints"
headFileName2 = "HeadPointsSingle"
elevationFileName = "ElevationPoints"
statsFileName = "MaximumHead"
#Selects the river and the point.
arcpy.Select_analysis(rivers, river, riverClause)
arcpy.Select_analysis("Testgroup1", gauge, pointClause)
#Generates the multiple ring buffers for that point.
#500 m intervals, the 1 m buffer is to include the point itself.
arcpy.MultipleRingBuffer_analysis(gauge, bufferFileName,
[1,500,1000,1500,2000], "Meters", "distance_m")
#Generates the points of intersection between the buffers and the river.
arcpy.Intersect_analysis([bufferFileName, river], headFileName,
"ALL", "", "POINT")
#Converts the multipoint output of intersect into single points.
arcpy.MultipartToSinglepart_management(headFileName, headFileName2)
#Extracts elevation values from the raster to the points.
arcpy.sa.ExtractValuesToPoints(headFileName2, elevationLayer, elevationFileName)
#Runs summary statistics to get the range of elevation.
#Picks out that statistic from the table using a cursor.
arcpy.Statistics_analysis(elevationFileName, statsFileName, [["RASTERVALU", "RANGE"]])
cursor = arcpy.SearchCursor(statsFileName)
headMax = 0
for x in cursor:
headMax = x.getValue("RANGE_RASTERVALU")
return headMax
#Iterates over the entire table and calculates head for each point.
fields = ["IDCDA", "FID", "Head"]
with arcpy.da.UpdateCursor(gaugePoints, fields) as cursor:
for row in cursor:
r = row[0]
g = row[1]
row[2] = calculateMaxHead(r,g)
cursor.updateRow(row)
Here is the error reading I get when I run the code:
The script basically sets an initial starting point, generates heads (difference in elevation between two flow points in a river) based on end points set at regular (Euclidian) length intervals from the start point (out to 2 km), then moves the starting point and repeats the process.
I'm not sure what "cannot acquire a lock" means or hwhy "FID"=0 is an invalid expression. I'm realtively new to using ARcGIs and especially Python in ArcGIS so any help/explanations are appreciated.