Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- urrent working folder is: C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp
- BEGIN EDITING PYTHON SCRIPT
- Joining PIBO sites to watershed polygons
- Traceback (most recent call last):
- File "C:\Users\karenkburke\Documents\ArcGIS\Climate\Python_2015\Climate30_loop1_mcg.py", line 94, in <module>
- arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp", "Layer1")
- File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 6043, in MakeFeatureLayer
- raise e
- arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
- ERROR 000732: Input Features: Dataset C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp does not exist or is not supported
- Failed to execute (MakeFeatureLayer).
- > Process Exit Code: 1
- > Time Taken: 01:23
- -----------------------------------------------------------------------------------------------------------
- # Climate30_loop1 script, version 1.1
- # --------------------------------------------------------------------------
- # USDA Forest Service - Rocky Mountain Research Station -
- # Boise Aquatic Sciences Lab - Boise, ID
- # --------------------------------------------------------------------------
- # Program Name: Climate30_loop1.py
- # Author: Matt Groce - matthewcgroce@fs.fed.us
- #
- # Date:3-29-15
- # Version 1.0: Original PIBO script
- # Version 1.1: Adding script to fix issue with small watersheds in Version 1.0.
- # --------------------------------------------------------------------------
- # Note on required inputs: This python script was written specifically for the inputs listed below. Each of these inputs should have EXACTLY the same fields and attributes listed, otherwise the script is likely to crash.
- # 1) Workspace name
- # 2) points = PIBO point shapefile ("Test_Points.shp")
- # 3) sheds = PIBO watershed shapefile ("Test_Sheds.shp")
- # 4) Rasters (ppt_71_00, ppt_81_10, tavg71_00, tavg81_10) with values for 30 year climate averages
- # 5) specify watershed area threshold (try Area_km < 2.56; this is the value of four DEM pixels) ##
- # --------------------------------------------------------------------------
- # Outputs:
- # 1) point shapefile ("Climate30.shp")
- # --------------------------------------------------------------------------
- # Notes:
- #
- #
- # --------------------------------------------------------------------------
- # History:
- #
- #
- # ---------------------------------------------------------------------------
- ### The Setup Section where the user specifies local variables and conditions...
- # Import system modules
- import arcpy
- import arcpy, sys, os, fileinput, glob, string
- from arcpy import env
- from arcpy.sa import *
- arcpy.CheckOutExtension('Spatial') # get liscense for spatial analyst
- arcpy.env.overwriteOutput = True # Set environments and overwrite outputs
- # User input1: make sure the workspace is set
- arcpy.env.workspace = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp' # This line allows the user to set the working directory and name the working folder.
- print "Current working folder is: " + arcpy.env.workspace
- ### These inputs are NOT REQUIRED by this script ###
- #coord = "C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\North America\NAD 1983.prj" # Our coordinate system
- #subbasin = r'C:/FS/Subbasins_all_9_24_09.shp' # the input large scale subbasins
- #buffs = r'C:/FS/Climate/Input/Bufffile.shp' # the input PIBO buffer shapefile
- #segs = r'C:/FS/Climate/Input/Segfile.shp' # the input PIBO segment shapefile
- #reaches = r'C:/FS/Climate/Input/Rchfile.shp' # the input PIBO reach shapefile
- ### These are the REQUIRED VECTOR inputs designated by the user from the 'Input' folder ###
- # User input2: (FID, Shape, RchID, SiteName, Stream, UTMZone, UTMN, UTME, MapLat, MapLong, SiteID)
- points = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp" # the input PIBO pt shapefile
- # User input3: (FID, Shape, ID, GRIDCODE, SiteID, ShedAreakm)
- sheds = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Test_Sheds.shp" # the input PIBO watershed shapefile (Make sure the sites you are running are the same in both points and sheds)
- ### These are the REQUIRED RASTER inputs from the 'Data' folder...they should not change unless they are newly updated ###
- # User input4:
- P71_00 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_71_00"
- # User input5:
- P81_10 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_81_10"
- # User input6:
- T71_00 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg71_00"
- # User input7:
- T81_10 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg81_10"
- ### Set local variables
- # User input8:
- areaThreshold = 2.56 # User input: for area threshold
- # User input9:
- NoShedThreshold = 0 # User input: leave as 0- this is for sites that don't fall on a PIBO sheds, yet are included in analysis.
- # ---------------------------------------------------------------------------
- # Start coding here
- # ---------------------------------------------------------------------------
- ###############################################################
- print "BEGIN EDITING PYTHON SCRIPT"
- ###############################################################
- #### Step 1: Add "Area_km" to Test_Points.shp by joining points .shp to watershed .shp, then rename fields
- print "Joining PIBO sites to watershed polygons"
- # Join PIBO sites to watershed polygons
- arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp", "Layer1")
- # Do join
- arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Test_Sheds.dbf", "SiteID")
- # Output to shapefiles
- arcpy.CopyFeatures_management("Layer1", "Test_Points_join.shp")
- arcpy.Delete_management("Layer1")
- ### Fix all of the field names that were changed
- # Add field "ReachID" to PIBO sites and copy values from old field
- arcpy.AddField_management("Test_Points_join.shp", "ReachID", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("Test_Points_join.shp", "ReachID", "[Test_Point]", "VB", "")
- dropFields = ["Test_Point"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "SiteID" to PIBO sites and copy values from old field
- arcpy.AddField_management("Test_Points_join.shp", "SiteID", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("Test_Points_join.shp", "SiteID", "[Test_Poi_6]", "VB", "")
- dropFields = ["Test_Poi_6"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "SiteName" to PIBO sites and copy values from old field
- arcpy.AddField_management("Test_Points_join.shp", "SiteName", "TEXT")
- arcpy.CalculateField_management("Test_Points_join.shp", "SiteName", "[Test_Poi_1]", "VB", "")
- dropFields = ["Test_Poi_1"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "Stream" to PIBO sites and copy values from old field
- arcpy.AddField_management("Test_Points_join.shp", "Stream", "TEXT")
- arcpy.CalculateField_management("Test_Points_join.shp", "Stream", "[Test_Poi_2]", "VB", "")
- dropFields = ["Test_Poi_2"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "Lat_Map" to PIBO sites and copy values from old field
- arcpy.AddField_management("Test_Points_join.shp", "Lat_Map", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("Test_Points_join.shp", "Lat_Map", "[Test_Poi_4]", "VB", "")
- dropFields = ["Test_Poi_4"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "Long_Map" to PIBO sites and copy values from old field
- arcpy.AddField_management("Test_Points_join.shp", "Long_Map", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("Test_Points_join.shp", "Long_Map", "[Test_Poi_5]", "VB", "")
- dropFields = ["Test_Poi_5"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "Northing" to PIBO sites and copy values from old field - FOR SOME REASON THE FULL VALUES ARE LOST IN THE JOIN
- arcpy.AddField_management("Test_Points_join.shp", "Northing", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("Test_Points_join.shp", "Northing", "[Test_Poi_7]", "VB", "")
- dropFields = ["Test_Poi_7"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "Easting" to PIBO sites and copy values from old field- FOR SOME REASON THE FULL VALUES ARE LOST IN THE JOIN
- arcpy.AddField_management("Test_Points_join.shp", "Easting", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("Test_Points_join.shp", "Easting", "[Test_Poi_8]", "VB", "")
- dropFields = ["Test_Poi_8"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field "Area_km" to PIBO sites and copy values from old field
- arcpy.AddField_management("Test_Points_join.shp", "Area_km", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("Test_Points_join.shp", "Area_km", "[Test_She_4]", "VB", "")
- dropFields = ["Test_She_4"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- # Add field SMALL_BIG to PIBO sites
- arcpy.AddField_management("Test_Points_join.shp", "SMALL_BIG", "SHORT", "8")
- arcpy.MakeFeatureLayer_management("Test_Points_join.shp", "Layer6")
- # Delete unnecessary fields
- dropFields = ["Test_Sheds", "Test_Poi_3", "Test_She_1", "Test_She_2", "Test_She_3"]
- arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
- print "Area Threshold"
- # Set up query to select big sites
- areaQuery = "Area_km" # User input: for area threshold field
- query1 = "\"" + areaQuery + "\" > "
- query2 = str(areaThreshold)
- query3 = query1 + query2
- print query3
- # Calculate big to 2
- arcpy.SelectLayerByAttribute_management("Layer6", "NEW_SELECTION", query3)
- arcpy.CalculateField_management("Layer6", "SMALL_BIG", "2")
- # Write the big points to a shapefile
- arcpy.CopyFeatures_management("Layer6", "t_FinalBigPoints.shp")
- print "SUCCESSFULLY JOINED PIBO SITES TO WATERSHED POLYGONS USING 'SITE ID' FIELD"
- ##### Step 2: Determine area threshold to find small watersheds #####
- print "AREA THRESHOLD"
- # select watersheds below a specified threshold (Area_km < 2.56) and write them to a new shapefile (t_smallSheds.shp)
- areaField = "ShedAreakm" # User input: for area threshold field
- arcpy.MakeFeatureLayer_management(sheds, "Layer1")
- query4 = "\"" + areaField + "\" < "
- query5 = str(areaThreshold)
- query6 = query4 + query5
- arcpy.SelectLayerByAttribute_management("Layer1", "NEW_SELECTION", query6)
- arcpy.CopyFeatures_management("Layer1", "t_smallSheds.shp")
- arcpy.Delete_management("Layer1")
- # select watersheds above a specified threshold (Area_km >= 2.56) and write them to a new shapefile (t_bigSheds.shp)
- arcpy.MakeFeatureLayer_management(sheds, "Layer2")
- query7 = "\"" + areaField + "\" >= "
- query8 = str(areaThreshold)
- query9 = query7 + query8
- arcpy.SelectLayerByAttribute_management("Layer2", "NEW_SELECTION", query9)
- arcpy.CopyFeatures_management("Layer2", "t_bigSheds.shp")
- bigSheds = "t_bigSheds.shp"
- arcpy.Delete_management("Layer2")
- print "SUCCESSFULLY COMPLETED AREA THRESHOLD"
- ##### End of Area Threshold #####
- ##### Step 3: Convert small watershed polygons to points that are centered inside each small watershed #####
- print "TESTING FEATURE TO POINT"
- # Set local variables
- SmallSheds = "t_smallSheds.shp"
- SmallSheds_pt = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t_SmallPoints"
- # Execute FeatureToPoint to turn small watersheds to points
- arcpy.FeatureToPoint_management(SmallSheds, SmallSheds_pt, "CENTROID") # Use FeatureToPoint function to find a point inside each small watershed
- # Delete unnecessary fields
- dropFields = ["ORIG_FID", "GRIDCODE", "ID"]
- arcpy.DeleteField_management("t_SmallPoints.shp", dropFields)
- # Add field SMALL_BIG to small sites and calcualte with 1 (small)
- arcpy.AddField_management("t_SmallPoints.shp", "SMALL_BIG", "SHORT", "8")
- arcpy.MakeFeatureLayer_management("t_SmallPoints.shp", "Layer7")
- # Set up query to select small sites
- areaQuery = "ShedAreakm" # User input: for area threshold field
- query10 = "\"" + areaQuery + "\" < "
- query11 = str(areaThreshold)
- query12 = query10 + query11
- print query12
- # Calculate small to 1
- arcpy.SelectLayerByAttribute_management("Layer7", "NEW_SELECTION", query12)
- arcpy.CalculateField_management("Layer7", "SMALL_BIG", "1")
- # Write the small points to a shapefile
- arcpy.CopyFeatures_management("Layer7", "t_SmallPoints1.shp")
- dropFields = ["SMALL_BIG"]
- arcpy.DeleteField_management("t_SmallPoints1.shp", dropFields)
- # Add field "Area_km"
- arcpy.AddField_management("t_SmallPoints1.shp", "Area_km", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("t_SmallPoints1.shp", "Area_km", "[ShedAreakm]", "VB", "")
- dropFields = ["ShedAreakm"]
- arcpy.DeleteField_management("t_SmallPoints1.shp", dropFields)
- ### some sites were excluded...this puts them back in as small sites, whose values will be extracted from the raster in Step 4
- arcpy.MakeFeatureLayer_management("Test_Points_join.shp", "NewLayer")
- # Set up query to select points without associated watersheds (i.e. Area_km = 0)
- areaQuery = "Area_km" # User input: for area threshold field
- query13 = "\"" + areaQuery + "\" = "
- query14 = str(NoShedThreshold)
- query15 = query13 + query14
- print(query15)
- # merge these points with "SmallPoints.shp" and calculate SMALL_BIG to 0
- arcpy.SelectLayerByAttribute_management("NewLayer", "NEW_SELECTION", query15)
- arcpy.CopyFeatures_management("NewLayer", "t_NoSheds.shp")
- arcpy.CalculateField_management("NewLayer", "SMALL_BIG", "0")
- arcpy.Delete_management("NewLayer")
- SmallPoints = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t_FinalSmallPoints.shp"
- arcpy.Merge_management(["t_SmallPoints1.shp", "t_NoSheds.shp"], SmallPoints)
- print "SUCCESSFULLY COMPLETED FEATURE TO POINT"
- ##### Step 4: Testing EXTRACT VALUES TO POINT #####
- print "TESTING EXTRACT VALUES TO POINT"
- # Set local variables
- inPointFeatures = "t_FinalSmallPoints.shp"
- inRaster1 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_71_00'
- inRaster2 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_81_10'
- inRaster3 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg71_00'
- inRaster4 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg81_10'
- outPointFeature1 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints1"
- outPointFeature2 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints2"
- outPointFeature3 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints3"
- outPointFeature4 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints4"
- # Execute ExtractValuesToInputPoints to extract the cell values of the raster and record the value in the attribute table of a new output feature class.
- ExtractValuesToPoints(inPointFeatures, inRaster1, outPointFeature1, "INTERPOLATE", "VALUE_ONLY")
- ExtractValuesToPoints(inPointFeatures, inRaster2, outPointFeature2, "INTERPOLATE", "VALUE_ONLY")
- ExtractValuesToPoints(inPointFeatures, inRaster3, outPointFeature3, "INTERPOLATE", "VALUE_ONLY")
- ExtractValuesToPoints(inPointFeatures, inRaster4, outPointFeature4, "INTERPOLATE", "VALUE_ONLY")
- # Add fields for new raster values
- arcpy.AddField_management("FinalSmallPoints1.shp", "ppt_71_00", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints1.shp", "ppt_71_00", "[RASTERVALU]", "VB", "")
- dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
- arcpy.DeleteField_management("FinalSmallPoints1.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints2.shp", "ppt_81_10", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints2.shp", "ppt_81_10", "[RASTERVALU]", "VB", "")
- dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
- arcpy.DeleteField_management("FinalSmallPoints2.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints3.shp", "tavg71_00", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints3.shp", "tavg71_00", "[RASTERVALU]", "VB", "")
- dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
- arcpy.DeleteField_management("FinalSmallPoints3.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints4.shp", "tavg81_10", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints4.shp", "tavg81_10", "[RASTERVALU]", "VB", "")
- dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
- arcpy.DeleteField_management("FinalSmallPoints4.shp", dropFields)
- print "Joining point values1"
- arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints1.shp", "Layer1")
- # Do joins
- arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints2.dbf", "SiteID")
- # Output to shapefiles
- arcpy.CopyFeatures_management("Layer1", "t1_FinalSmallPoints.shp")
- arcpy.Delete_management("Layer1")
- # Add field "SiteID" and "Area_km", then copy values from old field
- arcpy.AddField_management("t1_FinalSmallPoints.shp", "SiteID", "DOUBLE", "10", "6")
- arcpy.AddField_management("t1_FinalSmallPoints.shp", "Area_km", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("t1_FinalSmallPoints.shp", "SiteID", "[FinalSmall]", "VB", "")
- arcpy.CalculateField_management("t1_FinalSmallPoints.shp", "Area_km", "[FinalSma_1]", "VB", "")
- dropFields = ["FinalSmall", "FinalSma_1", "FinalSma_3", "FinalSma_4", "FinalSma_5"]
- arcpy.DeleteField_management("t1_FinalSmallPoints.shp", dropFields)
- print "Joining point values2"
- arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t1_FinalSmallPoints.shp", "Layer1")
- # Do joins
- arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints3.dbf", "SiteID")
- # Output to shapefiles
- arcpy.CopyFeatures_management("Layer1", "t2_FinalSmallPoints.shp")
- arcpy.Delete_management("Layer1")
- # Add field "SiteID" and "Area_km", then copy values from old field
- arcpy.AddField_management("t2_FinalSmallPoints.shp", "SiteID", "DOUBLE", "10", "6")
- arcpy.AddField_management("t2_FinalSmallPoints.shp", "Area_km", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("t2_FinalSmallPoints.shp", "SiteID", "[FinalSma_1]", "VB", "")
- arcpy.CalculateField_management("t2_FinalSmallPoints.shp", "Area_km", "[FinalSma_2]", "VB", "")
- dropFields = ["FinalSmall", "FinalSma_1", "FinalSma_2", "t1_Final_2", "t1_Final_3"]
- arcpy.DeleteField_management("t2_FinalSmallPoints.shp", dropFields)
- print "Joining point values3"
- arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t2_FinalSmallPoints.shp", "Layer1")
- # Do joins
- arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints4.dbf", "SiteID")
- # Output to shapefiles
- arcpy.CopyFeatures_management("Layer1", "t3_FinalSmallPoints.shp")
- arcpy.Delete_management("Layer1")
- # Add field "Site_ID" and "Areakm", then copy values from old field
- arcpy.AddField_management("t3_FinalSmallPoints.shp", "Site_ID", "DOUBLE", "10", "6")
- arcpy.AddField_management("t3_FinalSmallPoints.shp", "Areakm", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("t3_FinalSmallPoints.shp", "Site_ID", "[FinalSma_1]", "VB", "")
- arcpy.CalculateField_management("t3_FinalSmallPoints.shp", "Areakm", "[FinalSma_2]", "VB", "")
- dropFields = ["FinalSmall", "FinalSma_1", "FinalSma_2", "t2_Final_3", "t2_Final_4"]
- arcpy.DeleteField_management("t3_FinalSmallPoints.shp", dropFields)
- # join Test_Points_join.shp to t3_FinalSmallPoints.shp
- print "Joining"
- arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t3_FinalSmallPoints.shp", "Layer1")
- arcpy.AddJoin_management("Layer1", "Site_ID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/Test_Points_join.dbf", "SiteID")
- arcpy.CopyFeatures_management("Layer1", "t4_FinalSmallPoints.shp")
- ### Clean up final layer
- print "Cleaning up final layer"
- arcpy.CopyFeatures_management("t4_FinalSmallPoints.shp", "FinalSmallPoints.shp")
- dropFields = ["t3_Final_4", "t3_Final_5", "Test_Point", "Test_Po_10"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- # Add missing fields
- arcpy.AddField_management("FinalSmallPoints.shp", "ReachID", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "ReachID", "[Test_Poi_1]", "VB", "")
- dropFields = ["Test_Poi_1"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "SiteID", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "SiteID", "[Test_Poi_2]", "VB", "")
- dropFields = ["Test_Poi_2"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "SiteName", "TEXT")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "SiteName", "[Test_Poi_3]", "VB", "")
- dropFields = ["Test_Poi_3"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "Stream", "TEXT")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Stream", "[Test_Poi_4]", "VB", "")
- dropFields = ["Test_Poi_4"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "Lat_Map", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Lat_Map", "[Test_Poi_5]", "VB", "")
- dropFields = ["Test_Poi_5"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "Long_Map", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Long_Map", "[Test_Poi_6]", "VB", "")
- dropFields = ["Test_Poi_6"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "Northing", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Northing", "[Test_Poi_7]", "VB", "")
- dropFields = ["Test_Poi_7"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "Easting", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Easting", "[Test_Poi_8]", "VB", "")
- dropFields = ["Test_Poi_8"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "Area_km", "DOUBLE", "10", "6")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Area_km", "[Test_Poi_9]", "VB", "")
- dropFields = ["Test_Poi_9"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- arcpy.AddField_management("FinalSmallPoints.shp", "SMALL_BIG", "SHORT")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "SMALL_BIG", "1")
- # Add field "Prec_71_00" and copy values from old field
- arcpy.AddField_management("FinalSmallPoints.shp", "Prec_71_00", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Prec_71_00", "[t3_FinalSm]/100000", "VB", "")
- dropFields = ["t3_FinalSm"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- # Add field "Prec_81_10" and copy values from old field
- arcpy.AddField_management("FinalSmallPoints.shp", "Prec_81_10", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Prec_81_10", "[t3_Final_1]/100000", "VB", "")
- dropFields = ["t3_Final_1"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- # Add field "Temp_71_00" and copy values from old field
- arcpy.AddField_management("FinalSmallPoints.shp", "Temp_71_00", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Temp_71_00", "[t3_Final_2]/100", "VB", "")
- dropFields = ["t3_Final_2"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- # Add field "Temp_81_10" and copy values from old field
- arcpy.AddField_management("FinalSmallPoints.shp", "Temp_81_10", "FLOAT")
- arcpy.CalculateField_management("FinalSmallPoints.shp", "Temp_81_10", "[t3_Final_3]/100", "VB", "")
- dropFields = ["t3_Final_3"]
- arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
- print "SUCCESSFULLY COMPLETED EXTRACT VALUES TO POINT"
- ##### Step 5 - Feed newly created .shp for big sites into existing loop.
- print "USE NEW .SHP IN 'FOR' LOOP"
- # Set local variables
- bigSites = "t_FinalBigPoints.shp"
- bigSheds = "t_bigSheds.shp"
- P71_00 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_71_00'
- P81_10 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_81_10'
- T71_00 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg71_00'
- T81_10 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg81_10'
- ###############################################################
- print "END EDITING SCRIPT --> RETURN TO ORIGINAL PYTHON SCRIPT"
- ###############################################################
- # A loop to select out one feature from the spatial scale shapefiles
- arcpy.MakeFeatureLayer_management(bigSites, 'inputPointselect') # create a feature layer from the Test_Points
- arcpy.CopyFeatures_management ('inputPointselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp') # save the Test_Points as a new shapefile for the final data
- #This section adds the fields the will be calculated to the final shapefiles
- arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Prec_71_00', 'FLOAT') # Add the Average Precip from 1971-2000 field
- arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Prec_81_10', 'FLOAT') # Add the Average Precip from 1981-2010 field
- arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Temp_71_00', 'FLOAT') # Add the Average Temp from 1971-2000 field
- arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Temp_81_10', 'FLOAT') # Add the Average Temp from 1981-2010 field
- Sheds1 = arcpy.SearchCursor(bigSheds) # The Main Loop - it uses the watershed shapefile to identify sites the will have attributes calculated
- for row in Sheds1:
- ID1 = row.SiteID # Store the SiteID as ID1
- ID1str = str(ID1) # Convert the siteID to a string
- print 'Beginning Site %s' % (ID1str)
- arcpy.MakeFeatureLayer_management(bigSites, 'pointselect') # create a feature layer from the points
- arcpy.SelectLayerByAttribute_management ('pointselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single point
- arcpy.CopyFeatures_management ('pointselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testpoint.shp') # save the selected point
- temppoint = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testpoint.shp' # store the temporary point shapefile as the temppoint variable
- arcpy.MakeFeatureLayer_management(bigSheds, 'shedselect') # create a feature layer from the watersheds
- arcpy.SelectLayerByAttribute_management ('shedselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
- arcpy.CopyFeatures_management ('shedselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testshed.shp') # save the selected watershed
- tempshed = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testshed.shp' # store the temporary shed shapefile as the tempshed variable
- #arcpy.MakeFeatureLayer_management(buffs, 'buffselect') # create a feature layer from the watersheds
- #arcpy.SelectLayerByAttribute_management ('buffselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
- #arcpy.CopyFeatures_management ('buffselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testbuff.shp') # save the selected watershed
- #tempbuff = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testbuff.shp' # store the temporary shed shapefile as the tempshed variable
- #arcpy.MakeFeatureLayer_management(segs, 'segselect') # create a feature layer from the watersheds
- #arcpy.SelectLayerByAttribute_management ('segselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
- #arcpy.CopyFeatures_management ('segselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testseg.shp') # save the selected watershed
- #tempseg = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testseg.shp' # store the temporary shed shapefile as the tempshed variable
- #arcpy.MakeFeatureLayer_management(reaches, 'rchselect') # create a feature layer from the watersheds
- #arcpy.SelectLayerByAttribute_management ('rchselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
- #arcpy.CopyFeatures_management ('rchselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testrch.shp') # save the selected watershed
- #temprch = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testrch.shp' # store the temporary shed shapefile as the tempshed variable
- #arcpy.MakeFeatureLayer_management(subbasin, 'sb2') # create a feature layer from the subbasins
- #arcpy.SelectLayerByLocation_management ('sb2', 'INTERSECT', temppoint, 0.1) # select the HUC that the single point falls in
- #arcpy.CopyFeatures_management ('sb2', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/tempHUC.shp') # save the selected HUC
- #tempHUC = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Test/tempHUC.shp' # store the temporary huc shapefile as the temphuc variable
- ### Climate 30 year averages ###
- # This Section calculates the actual precip and temp values
- ########################################################################
- ########################################################################
- Prec71_00 = arcpy.Clip_management(P71_00, '#', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P71_00clip.tif', tempshed, '', 'ClippingGeometry') # Clip the Raster by the watershed
- Prec81_10 = arcpy.Clip_management(P81_10, '#', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P81_10clip.tif', tempshed, '', 'ClippingGeometry') # Clip the Raster by the watershed
- Temp71_00 = arcpy.Clip_management(T71_00, '#', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T71_00clip.tif', tempshed, '', 'ClippingGeometry') # Clip the Raster by the watershed
- Temp81_10 = arcpy.Clip_management(T81_10, '#', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T81_10clip.tif', tempshed, '', 'ClippingGeometry') # Clip the Raster by the watershed
- P71_00Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Prec71_00, 'MEAN', 'DATA') #Get the MeAN value from the raster
- P71_00Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P71_00Mean.tif') # Save the output from the MEAN
- P71_00MeanB = arcpy.sa.Int(P71_00Mean*100) # Turn it into an integer raster
- P71_00MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P71_00MeanB.tif') # Save the output raster
- rowP71Mean = arcpy.SearchCursor(P71_00MeanB) # Run a search cursor to get the MEAN value
- for Mean in rowP71Mean:
- P71_00Mean2 = Mean.VALUE # Store the MEAN value as a variable
- P71_00Mean3 = (float(P71_00Mean2)/10000000)
- P81_10Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Prec81_10, 'MEAN', 'DATA') #Get the MeAN value from the raster
- P81_10Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P81_10Mean.tif') # Save the output from the MEAN
- P81_10MeanB = arcpy.sa.Int(P81_10Mean*100) # Turn it into an integer raster
- P81_10MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P81_10MeanB.tif') # Save the output raster
- rowP71Mean = arcpy.SearchCursor(P81_10MeanB) # Run a search cursor to get the MEAN value
- for Mean in rowP71Mean:
- P81_10Mean2 = Mean.VALUE # Store the MEAN value as a variable
- P81_10Mean3 = (float(P81_10Mean2)/10000000)
- T71_00Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Temp71_00, 'MEAN', 'DATA') #Get the MeAN value from the raster
- T71_00Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T71_00Mean.tif') # Save the output from the MEAN
- T71_00MeanB = arcpy.sa.Int(T71_00Mean*100) # Turn it into an integer raster
- T71_00MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T71_00MeanB.tif') # Save the output raster
- rowT71Mean = arcpy.SearchCursor(T71_00MeanB) # Run a search cursor to get the MEAN value
- for Mean in rowT71Mean:
- T71_00Mean2 = Mean.VALUE # Store the MEAN value as a variable
- T71_00Mean3 = (float(T71_00Mean2)/10000)
- T81_10Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Temp81_10, 'MEAN', 'DATA') #Get the MeAN value from the raster
- T81_10Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T81_10Mean.tif') # Save the output from the MEAN
- T81_10MeanB = arcpy.sa.Int(T81_10Mean*100) # Turn it into an integer raster
- T81_10MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T81_10MeanB.tif') # Save the output raster
- rowT81Mean = arcpy.SearchCursor(T81_10MeanB) # Run a search cursor to get the MEAN value
- for Mean in rowT81Mean:
- T81_10Mean2 = Mean.VALUE # Store the MEAN value as a variable
- T81_10Mean3 = (float(T81_10Mean2)/10000)
- #This section sends the stored values to the data table of a Point Shapefile (No More cryptic CSV's!)
- arcpy.MakeFeatureLayer_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Precipselect') # create a feature layer from the final point shapefile
- arcpy.SelectLayerByAttribute_management ('Precipselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the point that matches the siteID the loop is on
- arcpy.CalculateField_management('Precipselect', 'Prec_71_00', P71_00Mean3) # Put the 30 year avereage precip for 1971-2000 in the Precip_71_00 field
- arcpy.CalculateField_management('Precipselect', 'Prec_81_10', P81_10Mean3) # Put the 30 year avereage precip for 1981-2010 in the Precip_81_10 field
- arcpy.CalculateField_management('Precipselect', 'Temp_71_00', T71_00Mean3) # Put the 30 year avereage temp for 1971-2000 in the Temp_71_00 field
- arcpy.CalculateField_management('Precipselect', 'Temp_81_10', T81_10Mean3) # Put the 30 year avereage temp for 1971-2000 in the Temp_81_10 field
- ###############################################################
- print "RESUME EDITING PYTHON SCRIPT"
- ###############################################################
- ##### Last step: Merge shapefiles #####
- print "Merging and writing output"
- # Merge the point files
- Climate30 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Output/Climate30.shp"
- arcpy.Merge_management(["FinalBigPoints.shp", "FinalSmallPoints.shp"], Climate30)
- print "SUCCESSFULLY MERGED OUTPUT FILE"
- ###############################################################
- print "END OF EDITING PYTHON SCRIPT"
- ###############################################################
- print "END PYTHON SCRIPT!!!"
- print "-----------------------------------------------------------------------------------------"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement