2

I have created a script that produces multiple "temporary" tables (using the arcpy.statistics_analysis function). These temporary tables are exported to the %scratchGDB%, I would like to insert python code into my script at the very start of my script that clears the %scratchGDB% before the tool is run. When I try to re-run the tool without deleting out these temporary files / clearing the scratchGDB I get an error that the table already exists and cannot be exported. If there is a way to overwrite the table, that would also be helpful. My script is attached below for reference.

  # Import arcpy module
import arcpy

# Script arguments
BldgSpace_Feature = arcpy.GetParameterAsText(0)
Building_Floor_Area_Feature_Class = arcpy.GetParameterAsText(1)
Output_Table_Name_and_Location = arcpy.GetParameterAsText(2)



# Local variables:
building_space_area = BldgSpace_Feature
BuildingSpace_A2 = r'%scratchGDB%/BuildingSpaceArea2'
BldgSpaceAreaStatistics1 = r'%scratchGDB%/BldgSpaceStatistics1'
BldgSpaceAreaStatistics2 = r'%scratchGDB%/BldgSpaceStatistics2'
BldgSpaceAreaStatistics4 = r'%scratchGDB%/BldgSpaceStatistics4'
BldgSpaceAreaStatistics3 = r'%scratchGDB%/BldgSpaceStatistics3'
BuildingSpaceStatistics = r'%scratchGDB%/BldgSpaceStatisticsRPUIDsum'
BuildingFloor_A_Layer = r'%scratchGDB%/BldgFloorMainStructureLayer'
BldgMainStructureAreaStatistics1 = r'%scratchGDB%/BldgMainStructureStatistics1'
BldgFloorNonMain = r'%scratchGDB%/BldgFloorNonMainLayer'
BldgNonMainAreaStatistics1 = r'%scratchGDB%/BldgNonMainStatistics1'
BldgSpaceArea = r'%scratchGDB%/BuildingSpaceAreaTypes'


# Process: Delete Any Applicable Fields of Input Feature Class
arcpy.DeleteField_management(BldgSpace_Feature, "CalculatedArea;CommonAndSpaceType;CommonIdentifier;UID")

# Process: Add Field - UID
arcpy.AddField_management(BldgSpace_Feature, "UID", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate UID
arcpy.CalculateField_management(BldgSpace_Feature, "UID", "[realPropertyUniqueID] &\"_\"& [unit] &\"_\"& [dodCategoryCode]", "VB", "")

# Process: Summary Statistics 4 
arcpy.Statistics_analysis(BldgSpace_Feature, BldgSpaceAreaStatistics4, "UID COUNT", "UID;unit;realPropertyUniqueID;facilityID;dodCategoryCode")

# Process: SumAreaSize - UID - RPUID - SpaceDiscriminator
arcpy.Statistics_analysis(BldgSpace_Feature, BldgSpaceAreaStatistics1, "areaSize SUM", "UID;spaceDiscriminator;realPropertyUniqueID")

# Process: CreateSpaceTypeRPUID field
arcpy.AddField_management(BldgSpaceAreaStatistics1, "SpaceTypeRPUID", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Combine SpaceDiscriminator with RPUID Statistics1 Table
arcpy.CalculateField_management(BldgSpaceAreaStatistics1, "SpaceTypeRPUID", "[realPropertyUniqueID] & [spaceDiscriminator]", "VB", "")

# Process: Calc Total RPUID Area
arcpy.Statistics_analysis(BldgSpace_Feature, BldgSpaceAreaStatistics2, "areaSize SUM", "realPropertyUniqueID")

# Process: Join Field RPUID Area and UID / RPUID / SpaceDiscriminator Area
arcpy.JoinField_management(BldgSpaceAreaStatistics1, "realPropertyUniqueID", BldgSpaceAreaStatistics2, "realPropertyUniqueID", "SUM_areaSize")

# Process: SumArea - SpaceDiscriminator - RPUID
arcpy.Statistics_analysis(BldgSpace_Feature, BldgSpaceAreaStatistics3, "areaSize SUM", "spaceDiscriminator;realPropertyUniqueID")

# Process: Create SpaceTypeRPUID Field
arcpy.AddField_management(BldgSpaceAreaStatistics3, "SpaceTypeRPUID", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Combine RPUID and SpaceDiscriminator
arcpy.CalculateField_management(BldgSpaceAreaStatistics3, "SpaceTypeRPUID", "[realPropertyUniqueID] & [spaceDiscriminator]", "VB", "")

# Process: Join Field (2)
arcpy.JoinField_management(BldgSpaceAreaStatistics1, "SpaceTypeRPUID", BldgSpaceAreaStatistics3, "SpaceTypeRPUID", "SUM_areaSize")

# Process: Sum of RPUID
arcpy.Statistics_analysis(BldgSpace_Feature, BuildingSpaceStatistics, "areaSize SUM", "realPropertyUniqueID")

# Process: Select all Main Structure 
arcpy.MakeFeatureLayer_management(Building_Floor_Area_Feature_Class, "BuildingFloor_A_Layer", "floorType = 'mainStructure'", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;buildingFloorIDPK buildingFloorIDPK VISIBLE NONE;sdsID sdsID VISIBLE NONE;sdsFeatureName sdsFeatureName VISIBLE NONE;sdsFeatureDescription sdsFeatureDescription VISIBLE NONE;sdsMetadataID sdsMetadataID VISIBLE NONE;areaSize areaSize VISIBLE NONE;areaSizeUOM areaSizeUOM VISIBLE NONE;perimeterSize perimeterSize VISIBLE NONE;perimeterSizeUOM perimeterSizeUOM VISIBLE NONE;latitude latitude VISIBLE NONE;longitude longitude VISIBLE NONE;MGRScentroid MGRScentroid VISIBLE NONE;floorID floorID VISIBLE NONE;buildngID buildngID VISIBLE NONE;floorName floorName VISIBLE NONE;floorCost floorCost VISIBLE NONE;floorUseableArea floorUseableArea VISIBLE NONE;floorUseableAreaUOM floorUseableAreaUOM VISIBLE NONE;floorBusinessArea floorBusinessArea VISIBLE NONE;floorBusinessAreaUOM floorBusinessAreaUOM VISIBLE NONE;floorCommonArea floorCommonArea VISIBLE NONE;floorCommonAreaUOM floorCommonAreaUOM VISIBLE NONE;floorRentableArea floorRentableArea VISIBLE NONE;floorRentableAreaUOM floorRentableAreaUOM VISIBLE NONE;facilityID facilityID VISIBLE NONE;exteriorWallThicknessUOM exteriorWallThicknessUOM VISIBLE NONE;exteriorWallThickness exteriorWallThickness VISIBLE NONE;floorType floorType VISIBLE NONE;realPropertyUniqueID realPropertyUniqueID VISIBLE NONE;installationID installationID VISIBLE NONE;installationName installationName VISIBLE NONE;siteID siteID VISIBLE NONE;majorCommand majorCommand VISIBLE NONE;realPropertySiteUniqueID realPropertySiteUniqueID VISIBLE NONE;wacInnrCode wacInnrCode VISIBLE NONE;dataSteward dataSteward VISIBLE NONE;country country VISIBLE NONE;owner owner VISIBLE NONE;createDate createDate VISIBLE NONE;creator creator VISIBLE NONE;dataCollection dataCollection VISIBLE NONE;dataSource dataSource VISIBLE NONE;editor editor VISIBLE NONE;dateEdited dateEdited VISIBLE NONE;metaNotes metaNotes VISIBLE NONE;mediaLink mediaLink VISIBLE NONE;narrative narrative VISIBLE NONE;COORD_ID COORD_ID VISIBLE NONE;FLOORKEY FLOORKEY VISIBLE NONE;GLOBALID GLOBALID VISIBLE NONE;GRID_VALUE GRID_VALUE VISIBLE NONE;MAP_ID MAP_ID VISIBLE NONE;USER_FLAG USER_FLAG VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")

# Process: Sum of all main structure area
arcpy.Statistics_analysis("BuildingFloor_A_Layer", BldgMainStructureAreaStatistics1, "areaSize SUM", "realPropertyUniqueID")

# Process: Join Field (5)
arcpy.JoinField_management(BuildingSpaceStatistics, "realPropertyUniqueID", BldgMainStructureAreaStatistics1, "realPropertyUniqueID", "SUM_areaSize")

# Process: AddField - Difference Btw Floor Main and RPUID Area space
arcpy.AddField_management(BuildingSpaceStatistics, "DiffFloorAreaSpaceArea", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calcs the diff btw Total floor area space from the bldgSpace RPUID
arcpy.CalculateField_management(BuildingSpaceStatistics, "DiffFloorAreaSpaceArea", "[SUM_areaSize_1]- [SUM_areaSize]", "VB", "")

# Process: Make Feature Layer (3)
arcpy.MakeFeatureLayer_management(Building_Floor_Area_Feature_Class, "BldgFloorNonMain", "floorType <> 'mainStructure'", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;buildingFloorIDPK buildingFloorIDPK VISIBLE NONE;sdsID sdsID VISIBLE NONE;sdsFeatureName sdsFeatureName VISIBLE NONE;sdsFeatureDescription sdsFeatureDescription VISIBLE NONE;sdsMetadataID sdsMetadataID VISIBLE NONE;areaSize areaSize VISIBLE NONE;areaSizeUOM areaSizeUOM VISIBLE NONE;perimeterSize perimeterSize VISIBLE NONE;perimeterSizeUOM perimeterSizeUOM VISIBLE NONE;latitude latitude VISIBLE NONE;longitude longitude VISIBLE NONE;MGRScentroid MGRScentroid VISIBLE NONE;floorID floorID VISIBLE NONE;buildngID buildngID VISIBLE NONE;floorName floorName VISIBLE NONE;floorCost floorCost VISIBLE NONE;floorUseableArea floorUseableArea VISIBLE NONE;floorUseableAreaUOM floorUseableAreaUOM VISIBLE NONE;floorBusinessArea floorBusinessArea VISIBLE NONE;floorBusinessAreaUOM floorBusinessAreaUOM VISIBLE NONE;floorCommonArea floorCommonArea VISIBLE NONE;floorCommonAreaUOM floorCommonAreaUOM VISIBLE NONE;floorRentableArea floorRentableArea VISIBLE NONE;floorRentableAreaUOM floorRentableAreaUOM VISIBLE NONE;facilityID facilityID VISIBLE NONE;exteriorWallThicknessUOM exteriorWallThicknessUOM VISIBLE NONE;exteriorWallThickness exteriorWallThickness VISIBLE NONE;floorType floorType VISIBLE NONE;realPropertyUniqueID realPropertyUniqueID VISIBLE NONE;installationID installationID VISIBLE NONE;installationName installationName VISIBLE NONE;siteID siteID VISIBLE NONE;majorCommand majorCommand VISIBLE NONE;realPropertySiteUniqueID realPropertySiteUniqueID VISIBLE NONE;wacInnrCode wacInnrCode VISIBLE NONE;dataSteward dataSteward VISIBLE NONE;country country VISIBLE NONE;owner owner VISIBLE NONE;createDate createDate VISIBLE NONE;creator creator VISIBLE NONE;dataCollection dataCollection VISIBLE NONE;dataSource dataSource VISIBLE NONE;editor editor VISIBLE NONE;dateEdited dateEdited VISIBLE NONE;metaNotes metaNotes VISIBLE NONE;mediaLink mediaLink VISIBLE NONE;narrative narrative VISIBLE NONE;COORD_ID COORD_ID VISIBLE NONE;FLOORKEY FLOORKEY VISIBLE NONE;GLOBALID GLOBALID VISIBLE NONE;GRID_VALUE GRID_VALUE VISIBLE NONE;MAP_ID MAP_ID VISIBLE NONE;USER_FLAG USER_FLAG VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")

# Process: Summary Statistics (3)
arcpy.Statistics_analysis("BldgFloorNonMain", BldgNonMainAreaStatistics1, "areaSize SUM", "realPropertyUniqueID")

# Process: CalcFloorNonMain Structure area - multiplies by half
arcpy.CalculateField_management(BldgNonMainAreaStatistics1, "SUM_areaSize", "[SUM_areaSize] *0.5", "VB", "")

# Process: Join Field (6)
arcpy.JoinField_management(BuildingSpaceStatistics, "realPropertyUniqueID", BldgNonMainAreaStatistics1, "realPropertyUniqueID", "SUM_areaSize")

# Process: Calculate Field (4)
arcpy.CalculateField_management(BuildingSpaceStatistics, "SUM_areaSize_12", "updateValue( !SUM_areaSize_12!)", "PYTHON_9.3", "def updateValue(value):\\n if value is None:\\n      return '0'\\n else: return value")

# Process: Add Field (3)
arcpy.AddField_management(BuildingSpaceStatistics, "SumTotalCommonSpace", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (5)
arcpy.CalculateField_management(BuildingSpaceStatistics, "SumTotalCommonSpace", "[DiffFloorAreaSpaceArea]+ [SUM_areaSize_12]", "VB", "")

# Process: Join Field (7)
arcpy.JoinField_management(BldgSpace_Feature, "realPropertyUniqueID", BuildingSpaceStatistics, "realPropertyUniqueID", "SumTotalCommonSpace")

# Process: Join Field (8)
arcpy.JoinField_management(BldgSpaceAreaStatistics1, "realPropertyUniqueID", BldgSpace_Feature, "realPropertyUniqueID", "SumTotalCommonSpace")

# Process: Create Total Calculated Area Field
arcpy.AddField_management(BldgSpaceAreaStatistics1, "CalculatedArea", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Area
arcpy.CalculateField_management(BldgSpaceAreaStatistics1, "CalculatedArea", "(( [SUM_areaSize]/ [SUM_areaSize_12])*(([SUM_areaSize_1]- [SUM_areaSize_12])+ [SumTotalCommonSpace])+ [SUM_areaSize])", "VB", "")

# Process: Create Common Identifier Field
arcpy.AddField_management(BldgSpaceAreaStatistics1, "CommonIdentifier", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Common Identifier
arcpy.CalculateField_management(BldgSpaceAreaStatistics1, "CommonIdentifier", "[SUM_areaSize_1]/ [SUM_areaSize_12]", "VB", "")

# Process: Join Field (4)
arcpy.JoinField_management(BldgSpace_Feature, "UID", BldgSpaceAreaStatistics1, "UID", "CalculatedArea;CommonIdentifier")

# Process: Create Common Unique ID Field
arcpy.AddField_management(BldgSpace_Feature, "CommonAndSpaceType", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Common and SpaceType
arcpy.CalculateField_management(BldgSpace_Feature, "CommonAndSpaceType", "[CommonIdentifier]& [spaceDiscriminator]", "VB", "")

# Process: Select
arcpy.Select_analysis(BldgSpace_Feature, BldgSpaceArea, "CommonAndSpaceType LIKE'1c%' OR CommonAndSpaceType LIKE'1b%' OR CommonAndSpaceType LIKE'%business'")

# Process: Join Field (3)
arcpy.JoinField_management(BldgSpaceAreaStatistics4, "UID", BldgSpaceArea, "UID", "CalculatedArea")

# Process: Delete Field
arcpy.DeleteField_management(BldgSpaceAreaStatistics4, "FREQUENCY;COUNT_UID")

# Process: Table To Excel
arcpy.TableToExcel_conversion(BldgSpaceAreaStatistics4, Output_Table_Name_and_Location, "NAME", "CODE")

# Process: Delete Field (2)
arcpy.DeleteField_management(BldgSpace_Feature, "SumTotalCommonSpace;CommonAndSpaceType;CommonIdentifier;CalculatedArea")
6

If you want to simply overwrite existing tables then set overwrite to be true at the beginning of your script as such:

arcpy.env.overwriteOutput = True

Personally I don't use those system variables such as %scratchGDB% as that is the system making a decision for you. Why not create a folder with a file geodatabase called fGDB_Scratch and read\write from that. At least you know where your data is going...

| improve this answer | |
  • 1
    Thanks, seemed to do the trick! Unfortunately this script is going out to a field of users otherwise I would do just that. – Dom B Jun 20 '17 at 16:46
2

Here is another way to clear a geodatabase of tables or feature classes.:

import arcpy

arcpy.env.workspace = "C:\\TargetGeodatabase.gdb"

LocalTables = arcpy.ListTables()

for table in LocalTables:
    arcpy.Delete_management(table)

The example above is for tables, but if you change arcpy.ListTables() to arcpy.ListFeatureClasses() it will delete those instead.

| improve this answer | |

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.