# Import Modules
import arcgisscripting
# Create geoprocessor object
gp = arcgisscripting.create (9.3)
# Get parameters
catchments = r"F:\Climate\test\Catchments.gdb\Catch_02AA"
#catchments = gp.GetParameterAsText(0) # Feature Layer
watershed = "02AA"
#watershed = gp.GetParameterAsText(1)
idField = "UCID"
#idField = gp.GetParameterAsText(2) # Field
climateRaster = r"F:\Climate\test\Climate.gdb\Avg_Temp_January"
#climateRaster = gp.GetParameterAsText(3) # Raster Layer
month = "January"
#month = gp.GetParameterAsText (4) # String
workspace = r"F:\Climate\test\Climate.gdb"
#workspace = gp.GetParameterAsText (5) # Workspace
outWorkspace = r"F:\Climate\test"
#outWorkspace = gp.GetParameterAsText (6) # Folder
statTable = "zStat" + month
statTableV = "zStat" + month + "V"
missingStats = "zStat" + month + "Missing"
points = month + "Point"
pointValues = month + "PointValue"
pointValuesV = month + "PointValuesV"
finalTable = month + "Averages" + watershed
gp.Workspace = workspace
gp.overwriteoutput = True
# Check-out the Spatial Analyst extension
gp.CheckOutExtension ("Spatial")
# Run Zonal Statistics As Table
gp.ZonalStatisticsAsTable_sa (catchments, idField, climateRaster, statTable, "DATA")
# Create a feature layer from the catchments
gp.MakeFeatureLayer_management (catchments, "layer", "", workspace, "")
# Join zonal statistics table to catchments layer
gp.AddJoin_management ("layer", idField, statTable, idField, "KEEP_ALL")
# Select and export the catchments that don't have any zonal statistics
gp.Select_analysis ("layer", missingStats, '"' + statTable + "." + idField + '"' + ' is null')
# Un-join zonal statistics table from catchments
gp.RemoveJoin_management ("layer", statTable)
# Convert the exported "missing stats" catchments to point centroids
gp.FeatureToPoint_management (missingStats, points, "INSIDE")
# Add the underlying temperature values to the points
gp.ExtractValuesToPoints_sa (points, climateRaster, pointValues, "INTERPOLATE", "VALUE_ONLY")
# Make table views from the zonal statistics and value to points tables
gp.MakeTableView_management (statTable, statTableV, "", workspace, idField + " " + idField + " VISIBLE, RASTERVALU Temp VISIBLE")
gp.MakeTableView_management (pointValues, pointValuesV, "", workspace, idField + " " + idField + " VISIBLE, MEAN Temp VISIBLE")
# Convert table views to dBASE tables
gp.TableToDBASE_conversion (statTableV, outWorkspace)
gp.TableToDBASE_conversion (pointValuesV, outWorkspace)
# Prepare the field maps for the final table
fieldMappings = gp.createobject ("FieldMappings")
fldmap_UCID = gp.createobject ("FieldMap")
fldmap_Temp = gp.createobject ("FieldMap")
vt = gp.CreateObject("ValueTable")
fieldMappings.AddTable(statTableV)
fieldMappings.AddTable(pointValuesV)
fldmap_UCID.AddInputField(statTableV, "UCID")
fldmap_UCID.AddInputField(pointValuesV, "CATCH_02_3")
fldmap_Temp.AddInputField(statTableV, "MEAN")
fldmap_Temp.AddInputField(pointValuesV, "RASTERVALU")
fld_UCID = fldmap_UCID.OutputField
fld_Temp = fldmap_Temp.OutputField
fld_UCID.Name = "UCID"
fld_Temp.Name = "Temp"
fldmap_UCID.OutputField = fld_UCID
fldmap_Temp.OutputField = fld_Temp
fieldMappings.AddFieldMap(fldmap_UCID)
fieldMappings.AddFieldMap(fldmap_Temp)
vt.AddRow(statTableV)
vt.AddRow(pointValuesV)
# Merge the two tables
gp.Merge_management (vt, finalTable, fieldMappings)
# Delete all the temporary datasets
gp.Delete_management (statTable)
gp.Delete_management (missingStats)
gp.Delete_management (points)
gp.Delete_management (pointValues)
gp.Delete_management (statTableV + ".dbf")
gp.Delete_management (pointValuesV + ".dbf")
count = fieldMappings.FieldCount print fieldMappings.GetFieldMap(16).OutputField.Name <<-- No error here for i in range (0, count-1, 1): print str(i) + "/" + str(count-1) fieldMap = fieldMappings.GetFieldMap(i) <<----RuntimeError: FieldMappings: Error in getting field map from field mapping for GetFieldMap outField = fieldMap.OutputField print outField.Name if fieldMappings.GetFieldMap(i) == fldmap_UCID: print "Found UCID: " + str(i) elif outField.Name == "Temp": print "Found Temp: " + str(i) else: fieldMappings.RemoveFieldMap(i) print 'done' print 'done'
If you are only editing the existing field map and dont delete them from the field map they will be transfered to your new feature class. You can create a new field map and only add the fields you want to that object though. I do not really like the field mapping functions very much.
ouput = #<feature class> dropFields = list() fieldList = gp.ListFields(output) for f in fieldList: if f.name <> "BLOCKSTAGE" and f.type <> "OID" and f.type <> "Geometry": print "Adding field to drop list "+f.name dropFields.append(f.name) gp.DeleteField_management(output, dropFields)
# Convert table views to dBASE tables
gp.TableToDBASE_conversion (statTableV, outWorkspace)
gp.TableToDBASE_conversion (pointValuesV, outWorkspace)
gp.Workspace = outWorkspace
# Convert the table views to geodatabase tables
#gp.TableToGeodatabase_conversion (statTableV, workspace)
#gp.TableToGeodatabase_conversion (pointValuesV, workspace)
# Change the temp fields to be float
gp.AddField_management (pointValuesV + ".dbf", "TempFloat", "FLOAT", 5, 1,"#","", "NULLABLE", "NON_REQUIRED","")
gp.CalculateField_management (pointValuesV + ".dbf", "TempFloat", "!RASTERVALU!", "PYTHON_9.3","")
gp.AddField_management (statTableV + ".dbf", "TempFloat2", "FLOAT", 5, 1, "#","", "NULLABLE", "NON_REQUIRED","")
gp.CalculateField_management (statTableV + ".dbf", "TempFloat2", "!MEAN!", "PYTHON_9.3","")
# Prepare the field maps for the final table
#* Create the field mappings object
fieldMappings = gp.createobject ("FieldMappings")
#* Create a UCID field map object and add the two UCID fields
fldmap_UCID = gp.CreateObject ("FieldMap")
fldmap_UCID.AddInputField(pointValuesV + ".dbf", "CATCH_02_3")
fldmap_UCID.AddInputField(statTableV + ".dbf", "UCID")
print "UCID input fields: " + str(fldmap_UCID.InputFieldCount)
#* Create a field map for temperature and add the two fields
fldmap_Temp = gp.createobject ("FieldMap")
fldmap_Temp.AddInputField(statTableV + ".dbf", "TempFloat2")
fldmap_Temp.AddInputField(pointValuesV + ".dbf", "TempFloat")
print "Temp input fields: " + str(fldmap_Temp.InputFieldCount)
#* Create both of the output fields and add them to the field mappings object
fld_UCID = fldmap_UCID.OutputField
fld_UCID.Name = "UCID"
fld_UCID.Type = "Text"
fld_UCID.Length = 9
fldmap_UCID.OutputField = fld_UCID
fieldMappings.AddFieldMap(fldmap_UCID)
fld_Temp = fldmap_Temp.OutputField
fld_Temp.Name = "Temp"
fld_Temp.Type = "Float"
fld_Temp.Precision = 5
fld_Temp.Scale = 1
fldmap_Temp.OutputField = fld_Temp
fieldMappings.AddFieldMap(fldmap_Temp)
# Merge the two tables
#gp.Merge_management (statTableV + ".dbf;" + pointValuesV + ".dbf", finalTable, fieldMappings)
gp.Merge_management (outWorkspace + "/" + statTableV + ".dbf;" + outWorkspace + "/" + pointValuesV + ".dbf", outWorkspace + "/" + finalTable, fieldMappings)