I have two tables:
1) feature class (i.e. table in code) = sites under "SiteID" and mean july data under "JulyMean". The sites have duplicates, therefore there are several groups of same sites. There are other fields in this table. There are 280 sites(rows) in total.
2) summary statistics table (i.e. stat_table in code) = summary statistics table containing the sites under "SiteID" with max mean July data under "MAX_JulyMean". There are 23 sites(rows) in total.
I'd like to create a new feature class (i.e. newtable_path) which contains all the sites identified in stat_table with the maximum mean data, however i'd like to include the other fields from the feature class table. I used search cursors to match the site id from the stat table with the feature class table. Then i insert the feature class table's data into the new feature class table. The insert cursor only copies 19/23 sites into the new table (all 19 rows are in order meaning the insertcursor is writing as should). I've read that the problem is due to locks etc... but I don't know how to release the lock.
How do I have all 23 rows copied instead of just 19?
#import packages
import arcpy from arcpy
import * from arcpy
import env from arcpy.sa
import * import os
arcpy.CheckOutExtension("Spatial")
#Input
workspace = r"C:\Research\Water_Temperature_Analysis\Water_Temperature_80PercentOfJuly.gdb"
table =r"C:\Research\Water_Temperature_Analysis\Water_Temperature_80PercentOfJuly.gdb\DFOSLC_80andabove"
field = "SiteID" # field containing groups
field1 = "JulyMean" #field with max and min
stat_table = r"C:\Research\Water_Temperature_Analysis\Water_Temperature_80PercentOfJuly.gdb\DFOSLC_80andabove_extremeMaxMin"
newtable = "DFOSLC_80andabove_ExtremeMeans"
#enter workspace folder
env.workspace = workspace
arcpy.env.overwriteOutput = True
#Find max and min
#arcpy.Statistics_analysis(table, stat_table, [[field1, "MAX"],[field1,"MIN"]], field)
# Create table with max and min
arcpy.CreateFeatureclass_management(workspace, newtable, "POINT", table)
newtable_path= workspace + "\\" + newtable
# Copy Max Values to new table
cursor = arcpy.SearchCursor(table)
maxfield = "MAX_"+ str(field1)
cursor1 = arcpy.SearchCursor(stat_table,[field,maxfield])
cursor2 = arcpy.InsertCursor(newtable_path)
for row1 in cursor1:
for row in cursor:
if row1.getValue(field) == row.getValue(field):
if row1.getValue(maxfield) == row.getValue(field1):
cursor2.insertRow(row)
row1 = cursor1.next()
del cursor2
del row, cursor
del row1, cursor1
Answer
Thank you everyone who helped me. I modified Emil Brundage's code and took advantage of the different ways of writing the same cursor to circumvent... The working code is:
.
.
.
# create list of site id and max mean
stat_data = set()
cursor = arcpy.da.SearchCursor (stat_table, [field, maxfield])
for row in cursor:
siteid= row[0]
maxmean = row[1]
valset = (siteid,maxmean)
stat_data.add(valset)
del row, cursor
print stat_data
# find rows that match criteria and copy
cursor = arcpy.SearchCursor(table)
cursor1 = arcpy.InsertCursor(newtable_path)
for row in cursor:
siteid_o = row.getValue(field)
maxmean_o = row.getValue(field1)
valset_o = (siteid_o, maxmean_o)
#check if vals are in set
if valset_o in stat_data:
cursor1.insertRow(row)
del row, cursorenter code here
No comments:
Post a Comment