Advertisement
Guest User

SCripto

a guest
May 27th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.79 KB | None | 0 0
  1. urrent working folder is: C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp
  2. BEGIN EDITING PYTHON SCRIPT
  3. Joining PIBO sites to watershed polygons
  4. Traceback (most recent call last):
  5. File "C:\Users\karenkburke\Documents\ArcGIS\Climate\Python_2015\Climate30_loop1_mcg.py", line 94, in <module>
  6. arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp", "Layer1")
  7. File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 6043, in MakeFeatureLayer
  8. raise e
  9. arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
  10. ERROR 000732: Input Features: Dataset C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp does not exist or is not supported
  11. Failed to execute (MakeFeatureLayer).
  12.  
  13.  
  14. > Process Exit Code: 1
  15. > Time Taken: 01:23
  16.  
  17.  
  18. -----------------------------------------------------------------------------------------------------------
  19. # Climate30_loop1 script, version 1.1
  20.  
  21. # --------------------------------------------------------------------------
  22. # USDA Forest Service - Rocky Mountain Research Station -
  23. # Boise Aquatic Sciences Lab - Boise, ID
  24. # --------------------------------------------------------------------------
  25. # Program Name: Climate30_loop1.py
  26. # Author: Matt Groce - matthewcgroce@fs.fed.us
  27. #
  28. # Date:3-29-15
  29. # Version 1.0: Original PIBO script
  30. # Version 1.1: Adding script to fix issue with small watersheds in Version 1.0.
  31. # --------------------------------------------------------------------------
  32. # 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.
  33. # 1) Workspace name
  34. # 2) points = PIBO point shapefile ("Test_Points.shp")
  35. # 3) sheds = PIBO watershed shapefile ("Test_Sheds.shp")
  36. # 4) Rasters (ppt_71_00, ppt_81_10, tavg71_00, tavg81_10) with values for 30 year climate averages
  37. # 5) specify watershed area threshold (try Area_km < 2.56; this is the value of four DEM pixels) ##
  38. # --------------------------------------------------------------------------
  39. # Outputs:
  40. # 1) point shapefile ("Climate30.shp")
  41. # --------------------------------------------------------------------------
  42. # Notes:
  43. #
  44. #
  45. # --------------------------------------------------------------------------
  46. # History:
  47. #
  48. #
  49. # ---------------------------------------------------------------------------
  50.  
  51. ### The Setup Section where the user specifies local variables and conditions...
  52.  
  53. # Import system modules
  54. import arcpy
  55. import arcpy, sys, os, fileinput, glob, string
  56. from arcpy import env
  57. from arcpy.sa import *
  58.  
  59. arcpy.CheckOutExtension('Spatial') # get liscense for spatial analyst
  60. arcpy.env.overwriteOutput = True # Set environments and overwrite outputs
  61.  
  62. # User input1: make sure the workspace is set
  63. 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.
  64. print "Current working folder is: " + arcpy.env.workspace
  65.  
  66. ### These inputs are NOT REQUIRED by this script ###
  67. #coord = "C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\North America\NAD 1983.prj" # Our coordinate system
  68. #subbasin = r'C:/FS/Subbasins_all_9_24_09.shp' # the input large scale subbasins
  69. #buffs = r'C:/FS/Climate/Input/Bufffile.shp' # the input PIBO buffer shapefile
  70. #segs = r'C:/FS/Climate/Input/Segfile.shp' # the input PIBO segment shapefile
  71. #reaches = r'C:/FS/Climate/Input/Rchfile.shp' # the input PIBO reach shapefile
  72.  
  73. ### These are the REQUIRED VECTOR inputs designated by the user from the 'Input' folder ###
  74. # User input2: (FID, Shape, RchID, SiteName, Stream, UTMZone, UTMN, UTME, MapLat, MapLong, SiteID)
  75. points = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp" # the input PIBO pt shapefile
  76. # User input3: (FID, Shape, ID, GRIDCODE, SiteID, ShedAreakm)
  77. 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)
  78.  
  79. ### These are the REQUIRED RASTER inputs from the 'Data' folder...they should not change unless they are newly updated ###
  80. # User input4:
  81. P71_00 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_71_00"
  82. # User input5:
  83. P81_10 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_81_10"
  84. # User input6:
  85. T71_00 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg71_00"
  86. # User input7:
  87. T81_10 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg81_10"
  88.  
  89. ### Set local variables
  90. # User input8:
  91. areaThreshold = 2.56 # User input: for area threshold
  92. # User input9:
  93. NoShedThreshold = 0 # User input: leave as 0- this is for sites that don't fall on a PIBO sheds, yet are included in analysis.
  94.  
  95. # ---------------------------------------------------------------------------
  96. # Start coding here
  97. # ---------------------------------------------------------------------------
  98.  
  99. ###############################################################
  100.  
  101.  
  102. print "BEGIN EDITING PYTHON SCRIPT"
  103.  
  104.  
  105. ###############################################################
  106.  
  107. #### Step 1: Add "Area_km" to Test_Points.shp by joining points .shp to watershed .shp, then rename fields
  108.  
  109. print "Joining PIBO sites to watershed polygons"
  110. # Join PIBO sites to watershed polygons
  111. arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Sites_no3414.shp", "Layer1")
  112.  
  113. # Do join
  114. arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Input/Test_Sheds.dbf", "SiteID")
  115.  
  116. # Output to shapefiles
  117. arcpy.CopyFeatures_management("Layer1", "Test_Points_join.shp")
  118. arcpy.Delete_management("Layer1")
  119.  
  120. ### Fix all of the field names that were changed
  121.  
  122. # Add field "ReachID" to PIBO sites and copy values from old field
  123. arcpy.AddField_management("Test_Points_join.shp", "ReachID", "DOUBLE", "10", "6")
  124. arcpy.CalculateField_management("Test_Points_join.shp", "ReachID", "[Test_Point]", "VB", "")
  125. dropFields = ["Test_Point"]
  126. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  127.  
  128. # Add field "SiteID" to PIBO sites and copy values from old field
  129. arcpy.AddField_management("Test_Points_join.shp", "SiteID", "DOUBLE", "10", "6")
  130. arcpy.CalculateField_management("Test_Points_join.shp", "SiteID", "[Test_Poi_6]", "VB", "")
  131. dropFields = ["Test_Poi_6"]
  132. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  133.  
  134. # Add field "SiteName" to PIBO sites and copy values from old field
  135. arcpy.AddField_management("Test_Points_join.shp", "SiteName", "TEXT")
  136. arcpy.CalculateField_management("Test_Points_join.shp", "SiteName", "[Test_Poi_1]", "VB", "")
  137. dropFields = ["Test_Poi_1"]
  138. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  139.  
  140. # Add field "Stream" to PIBO sites and copy values from old field
  141. arcpy.AddField_management("Test_Points_join.shp", "Stream", "TEXT")
  142. arcpy.CalculateField_management("Test_Points_join.shp", "Stream", "[Test_Poi_2]", "VB", "")
  143. dropFields = ["Test_Poi_2"]
  144. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  145.  
  146. # Add field "Lat_Map" to PIBO sites and copy values from old field
  147. arcpy.AddField_management("Test_Points_join.shp", "Lat_Map", "DOUBLE", "10", "6")
  148. arcpy.CalculateField_management("Test_Points_join.shp", "Lat_Map", "[Test_Poi_4]", "VB", "")
  149. dropFields = ["Test_Poi_4"]
  150. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  151.  
  152. # Add field "Long_Map" to PIBO sites and copy values from old field
  153. arcpy.AddField_management("Test_Points_join.shp", "Long_Map", "DOUBLE", "10", "6")
  154. arcpy.CalculateField_management("Test_Points_join.shp", "Long_Map", "[Test_Poi_5]", "VB", "")
  155. dropFields = ["Test_Poi_5"]
  156. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  157.  
  158. # Add field "Northing" to PIBO sites and copy values from old field - FOR SOME REASON THE FULL VALUES ARE LOST IN THE JOIN
  159. arcpy.AddField_management("Test_Points_join.shp", "Northing", "DOUBLE", "10", "6")
  160. arcpy.CalculateField_management("Test_Points_join.shp", "Northing", "[Test_Poi_7]", "VB", "")
  161. dropFields = ["Test_Poi_7"]
  162. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  163.  
  164. # Add field "Easting" to PIBO sites and copy values from old field- FOR SOME REASON THE FULL VALUES ARE LOST IN THE JOIN
  165. arcpy.AddField_management("Test_Points_join.shp", "Easting", "DOUBLE", "10", "6")
  166. arcpy.CalculateField_management("Test_Points_join.shp", "Easting", "[Test_Poi_8]", "VB", "")
  167. dropFields = ["Test_Poi_8"]
  168. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  169.  
  170. # Add field "Area_km" to PIBO sites and copy values from old field
  171. arcpy.AddField_management("Test_Points_join.shp", "Area_km", "DOUBLE", "10", "6")
  172. arcpy.CalculateField_management("Test_Points_join.shp", "Area_km", "[Test_She_4]", "VB", "")
  173. dropFields = ["Test_She_4"]
  174. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  175.  
  176. # Add field SMALL_BIG to PIBO sites
  177. arcpy.AddField_management("Test_Points_join.shp", "SMALL_BIG", "SHORT", "8")
  178. arcpy.MakeFeatureLayer_management("Test_Points_join.shp", "Layer6")
  179.  
  180. # Delete unnecessary fields
  181. dropFields = ["Test_Sheds", "Test_Poi_3", "Test_She_1", "Test_She_2", "Test_She_3"]
  182. arcpy.DeleteField_management("Test_Points_join.shp", dropFields)
  183.  
  184. print "Area Threshold"
  185.  
  186. # Set up query to select big sites
  187. areaQuery = "Area_km" # User input: for area threshold field
  188. query1 = "\"" + areaQuery + "\" > "
  189. query2 = str(areaThreshold)
  190. query3 = query1 + query2
  191. print query3
  192.  
  193. # Calculate big to 2
  194. arcpy.SelectLayerByAttribute_management("Layer6", "NEW_SELECTION", query3)
  195. arcpy.CalculateField_management("Layer6", "SMALL_BIG", "2")
  196.  
  197. # Write the big points to a shapefile
  198. arcpy.CopyFeatures_management("Layer6", "t_FinalBigPoints.shp")
  199.  
  200. print "SUCCESSFULLY JOINED PIBO SITES TO WATERSHED POLYGONS USING 'SITE ID' FIELD"
  201.  
  202. ##### Step 2: Determine area threshold to find small watersheds #####
  203.  
  204. print "AREA THRESHOLD"
  205.  
  206. # select watersheds below a specified threshold (Area_km < 2.56) and write them to a new shapefile (t_smallSheds.shp)
  207. areaField = "ShedAreakm" # User input: for area threshold field
  208. arcpy.MakeFeatureLayer_management(sheds, "Layer1")
  209. query4 = "\"" + areaField + "\" < "
  210. query5 = str(areaThreshold)
  211. query6 = query4 + query5
  212. arcpy.SelectLayerByAttribute_management("Layer1", "NEW_SELECTION", query6)
  213. arcpy.CopyFeatures_management("Layer1", "t_smallSheds.shp")
  214. arcpy.Delete_management("Layer1")
  215.  
  216. # select watersheds above a specified threshold (Area_km >= 2.56) and write them to a new shapefile (t_bigSheds.shp)
  217. arcpy.MakeFeatureLayer_management(sheds, "Layer2")
  218. query7 = "\"" + areaField + "\" >= "
  219. query8 = str(areaThreshold)
  220. query9 = query7 + query8
  221. arcpy.SelectLayerByAttribute_management("Layer2", "NEW_SELECTION", query9)
  222. arcpy.CopyFeatures_management("Layer2", "t_bigSheds.shp")
  223. bigSheds = "t_bigSheds.shp"
  224. arcpy.Delete_management("Layer2")
  225.  
  226. print "SUCCESSFULLY COMPLETED AREA THRESHOLD"
  227.  
  228. ##### End of Area Threshold #####
  229.  
  230. ##### Step 3: Convert small watershed polygons to points that are centered inside each small watershed #####
  231. print "TESTING FEATURE TO POINT"
  232.  
  233. # Set local variables
  234. SmallSheds = "t_smallSheds.shp"
  235. SmallSheds_pt = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t_SmallPoints"
  236.  
  237. # Execute FeatureToPoint to turn small watersheds to points
  238. arcpy.FeatureToPoint_management(SmallSheds, SmallSheds_pt, "CENTROID") # Use FeatureToPoint function to find a point inside each small watershed
  239.  
  240. # Delete unnecessary fields
  241. dropFields = ["ORIG_FID", "GRIDCODE", "ID"]
  242. arcpy.DeleteField_management("t_SmallPoints.shp", dropFields)
  243.  
  244. # Add field SMALL_BIG to small sites and calcualte with 1 (small)
  245. arcpy.AddField_management("t_SmallPoints.shp", "SMALL_BIG", "SHORT", "8")
  246. arcpy.MakeFeatureLayer_management("t_SmallPoints.shp", "Layer7")
  247.  
  248. # Set up query to select small sites
  249. areaQuery = "ShedAreakm" # User input: for area threshold field
  250. query10 = "\"" + areaQuery + "\" < "
  251. query11 = str(areaThreshold)
  252. query12 = query10 + query11
  253. print query12
  254.  
  255. # Calculate small to 1
  256. arcpy.SelectLayerByAttribute_management("Layer7", "NEW_SELECTION", query12)
  257. arcpy.CalculateField_management("Layer7", "SMALL_BIG", "1")
  258.  
  259. # Write the small points to a shapefile
  260. arcpy.CopyFeatures_management("Layer7", "t_SmallPoints1.shp")
  261.  
  262. dropFields = ["SMALL_BIG"]
  263. arcpy.DeleteField_management("t_SmallPoints1.shp", dropFields)
  264.  
  265. # Add field "Area_km"
  266. arcpy.AddField_management("t_SmallPoints1.shp", "Area_km", "DOUBLE", "10", "6")
  267. arcpy.CalculateField_management("t_SmallPoints1.shp", "Area_km", "[ShedAreakm]", "VB", "")
  268. dropFields = ["ShedAreakm"]
  269. arcpy.DeleteField_management("t_SmallPoints1.shp", dropFields)
  270.  
  271. ### some sites were excluded...this puts them back in as small sites, whose values will be extracted from the raster in Step 4
  272. arcpy.MakeFeatureLayer_management("Test_Points_join.shp", "NewLayer")
  273.  
  274. # Set up query to select points without associated watersheds (i.e. Area_km = 0)
  275. areaQuery = "Area_km" # User input: for area threshold field
  276. query13 = "\"" + areaQuery + "\" = "
  277. query14 = str(NoShedThreshold)
  278. query15 = query13 + query14
  279. print(query15)
  280.  
  281. # merge these points with "SmallPoints.shp" and calculate SMALL_BIG to 0
  282. arcpy.SelectLayerByAttribute_management("NewLayer", "NEW_SELECTION", query15)
  283. arcpy.CopyFeatures_management("NewLayer", "t_NoSheds.shp")
  284. arcpy.CalculateField_management("NewLayer", "SMALL_BIG", "0")
  285. arcpy.Delete_management("NewLayer")
  286. SmallPoints = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t_FinalSmallPoints.shp"
  287. arcpy.Merge_management(["t_SmallPoints1.shp", "t_NoSheds.shp"], SmallPoints)
  288.  
  289. print "SUCCESSFULLY COMPLETED FEATURE TO POINT"
  290.  
  291. ##### Step 4: Testing EXTRACT VALUES TO POINT #####
  292. print "TESTING EXTRACT VALUES TO POINT"
  293.  
  294. # Set local variables
  295. inPointFeatures = "t_FinalSmallPoints.shp"
  296. inRaster1 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_71_00'
  297. inRaster2 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_81_10'
  298. inRaster3 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg71_00'
  299. inRaster4 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg81_10'
  300. outPointFeature1 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints1"
  301. outPointFeature2 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints2"
  302. outPointFeature3 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints3"
  303. outPointFeature4 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints4"
  304.  
  305. # Execute ExtractValuesToInputPoints to extract the cell values of the raster and record the value in the attribute table of a new output feature class.
  306. ExtractValuesToPoints(inPointFeatures, inRaster1, outPointFeature1, "INTERPOLATE", "VALUE_ONLY")
  307. ExtractValuesToPoints(inPointFeatures, inRaster2, outPointFeature2, "INTERPOLATE", "VALUE_ONLY")
  308. ExtractValuesToPoints(inPointFeatures, inRaster3, outPointFeature3, "INTERPOLATE", "VALUE_ONLY")
  309. ExtractValuesToPoints(inPointFeatures, inRaster4, outPointFeature4, "INTERPOLATE", "VALUE_ONLY")
  310.  
  311. # Add fields for new raster values
  312. arcpy.AddField_management("FinalSmallPoints1.shp", "ppt_71_00", "FLOAT")
  313. arcpy.CalculateField_management("FinalSmallPoints1.shp", "ppt_71_00", "[RASTERVALU]", "VB", "")
  314. dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
  315. arcpy.DeleteField_management("FinalSmallPoints1.shp", dropFields)
  316.  
  317. arcpy.AddField_management("FinalSmallPoints2.shp", "ppt_81_10", "FLOAT")
  318. arcpy.CalculateField_management("FinalSmallPoints2.shp", "ppt_81_10", "[RASTERVALU]", "VB", "")
  319. dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
  320. arcpy.DeleteField_management("FinalSmallPoints2.shp", dropFields)
  321.  
  322. arcpy.AddField_management("FinalSmallPoints3.shp", "tavg71_00", "FLOAT")
  323. arcpy.CalculateField_management("FinalSmallPoints3.shp", "tavg71_00", "[RASTERVALU]", "VB", "")
  324. dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
  325. arcpy.DeleteField_management("FinalSmallPoints3.shp", dropFields)
  326.  
  327. arcpy.AddField_management("FinalSmallPoints4.shp", "tavg81_10", "FLOAT")
  328. arcpy.CalculateField_management("FinalSmallPoints4.shp", "tavg81_10", "[RASTERVALU]", "VB", "")
  329. dropFields = ["RASTERVALU", "SMALL_BIG", "ReachID", "SiteName", "Stream", "Lat_Map", "Long_Map", "Northing", "Easting"]
  330. arcpy.DeleteField_management("FinalSmallPoints4.shp", dropFields)
  331.  
  332. print "Joining point values1"
  333. arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints1.shp", "Layer1")
  334.  
  335. # Do joins
  336. arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints2.dbf", "SiteID")
  337.  
  338. # Output to shapefiles
  339. arcpy.CopyFeatures_management("Layer1", "t1_FinalSmallPoints.shp")
  340. arcpy.Delete_management("Layer1")
  341.  
  342. # Add field "SiteID" and "Area_km", then copy values from old field
  343. arcpy.AddField_management("t1_FinalSmallPoints.shp", "SiteID", "DOUBLE", "10", "6")
  344. arcpy.AddField_management("t1_FinalSmallPoints.shp", "Area_km", "DOUBLE", "10", "6")
  345. arcpy.CalculateField_management("t1_FinalSmallPoints.shp", "SiteID", "[FinalSmall]", "VB", "")
  346. arcpy.CalculateField_management("t1_FinalSmallPoints.shp", "Area_km", "[FinalSma_1]", "VB", "")
  347. dropFields = ["FinalSmall", "FinalSma_1", "FinalSma_3", "FinalSma_4", "FinalSma_5"]
  348. arcpy.DeleteField_management("t1_FinalSmallPoints.shp", dropFields)
  349.  
  350. print "Joining point values2"
  351. arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t1_FinalSmallPoints.shp", "Layer1")
  352.  
  353. # Do joins
  354. arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints3.dbf", "SiteID")
  355.  
  356. # Output to shapefiles
  357. arcpy.CopyFeatures_management("Layer1", "t2_FinalSmallPoints.shp")
  358. arcpy.Delete_management("Layer1")
  359.  
  360. # Add field "SiteID" and "Area_km", then copy values from old field
  361. arcpy.AddField_management("t2_FinalSmallPoints.shp", "SiteID", "DOUBLE", "10", "6")
  362. arcpy.AddField_management("t2_FinalSmallPoints.shp", "Area_km", "DOUBLE", "10", "6")
  363. arcpy.CalculateField_management("t2_FinalSmallPoints.shp", "SiteID", "[FinalSma_1]", "VB", "")
  364. arcpy.CalculateField_management("t2_FinalSmallPoints.shp", "Area_km", "[FinalSma_2]", "VB", "")
  365. dropFields = ["FinalSmall", "FinalSma_1", "FinalSma_2", "t1_Final_2", "t1_Final_3"]
  366. arcpy.DeleteField_management("t2_FinalSmallPoints.shp", dropFields)
  367.  
  368. print "Joining point values3"
  369. arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t2_FinalSmallPoints.shp", "Layer1")
  370.  
  371. # Do joins
  372. arcpy.AddJoin_management("Layer1", "SiteID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalSmallPoints4.dbf", "SiteID")
  373.  
  374. # Output to shapefiles
  375. arcpy.CopyFeatures_management("Layer1", "t3_FinalSmallPoints.shp")
  376. arcpy.Delete_management("Layer1")
  377.  
  378. # Add field "Site_ID" and "Areakm", then copy values from old field
  379. arcpy.AddField_management("t3_FinalSmallPoints.shp", "Site_ID", "DOUBLE", "10", "6")
  380. arcpy.AddField_management("t3_FinalSmallPoints.shp", "Areakm", "DOUBLE", "10", "6")
  381. arcpy.CalculateField_management("t3_FinalSmallPoints.shp", "Site_ID", "[FinalSma_1]", "VB", "")
  382. arcpy.CalculateField_management("t3_FinalSmallPoints.shp", "Areakm", "[FinalSma_2]", "VB", "")
  383. dropFields = ["FinalSmall", "FinalSma_1", "FinalSma_2", "t2_Final_3", "t2_Final_4"]
  384. arcpy.DeleteField_management("t3_FinalSmallPoints.shp", dropFields)
  385.  
  386. # join Test_Points_join.shp to t3_FinalSmallPoints.shp
  387. print "Joining"
  388. arcpy.MakeFeatureLayer_management("C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/t3_FinalSmallPoints.shp", "Layer1")
  389. arcpy.AddJoin_management("Layer1", "Site_ID", "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/Test_Points_join.dbf", "SiteID")
  390. arcpy.CopyFeatures_management("Layer1", "t4_FinalSmallPoints.shp")
  391.  
  392. ### Clean up final layer
  393. print "Cleaning up final layer"
  394. arcpy.CopyFeatures_management("t4_FinalSmallPoints.shp", "FinalSmallPoints.shp")
  395. dropFields = ["t3_Final_4", "t3_Final_5", "Test_Point", "Test_Po_10"]
  396. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  397.  
  398. # Add missing fields
  399. arcpy.AddField_management("FinalSmallPoints.shp", "ReachID", "DOUBLE", "10", "6")
  400. arcpy.CalculateField_management("FinalSmallPoints.shp", "ReachID", "[Test_Poi_1]", "VB", "")
  401. dropFields = ["Test_Poi_1"]
  402. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  403.  
  404. arcpy.AddField_management("FinalSmallPoints.shp", "SiteID", "DOUBLE", "10", "6")
  405. arcpy.CalculateField_management("FinalSmallPoints.shp", "SiteID", "[Test_Poi_2]", "VB", "")
  406. dropFields = ["Test_Poi_2"]
  407. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  408.  
  409. arcpy.AddField_management("FinalSmallPoints.shp", "SiteName", "TEXT")
  410. arcpy.CalculateField_management("FinalSmallPoints.shp", "SiteName", "[Test_Poi_3]", "VB", "")
  411. dropFields = ["Test_Poi_3"]
  412. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  413.  
  414. arcpy.AddField_management("FinalSmallPoints.shp", "Stream", "TEXT")
  415. arcpy.CalculateField_management("FinalSmallPoints.shp", "Stream", "[Test_Poi_4]", "VB", "")
  416. dropFields = ["Test_Poi_4"]
  417. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  418.  
  419. arcpy.AddField_management("FinalSmallPoints.shp", "Lat_Map", "DOUBLE", "10", "6")
  420. arcpy.CalculateField_management("FinalSmallPoints.shp", "Lat_Map", "[Test_Poi_5]", "VB", "")
  421. dropFields = ["Test_Poi_5"]
  422. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  423.  
  424. arcpy.AddField_management("FinalSmallPoints.shp", "Long_Map", "DOUBLE", "10", "6")
  425. arcpy.CalculateField_management("FinalSmallPoints.shp", "Long_Map", "[Test_Poi_6]", "VB", "")
  426. dropFields = ["Test_Poi_6"]
  427. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  428.  
  429. arcpy.AddField_management("FinalSmallPoints.shp", "Northing", "DOUBLE", "10", "6")
  430. arcpy.CalculateField_management("FinalSmallPoints.shp", "Northing", "[Test_Poi_7]", "VB", "")
  431. dropFields = ["Test_Poi_7"]
  432. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  433.  
  434. arcpy.AddField_management("FinalSmallPoints.shp", "Easting", "DOUBLE", "10", "6")
  435. arcpy.CalculateField_management("FinalSmallPoints.shp", "Easting", "[Test_Poi_8]", "VB", "")
  436. dropFields = ["Test_Poi_8"]
  437. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  438.  
  439. arcpy.AddField_management("FinalSmallPoints.shp", "Area_km", "DOUBLE", "10", "6")
  440. arcpy.CalculateField_management("FinalSmallPoints.shp", "Area_km", "[Test_Poi_9]", "VB", "")
  441. dropFields = ["Test_Poi_9"]
  442. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  443.  
  444. arcpy.AddField_management("FinalSmallPoints.shp", "SMALL_BIG", "SHORT")
  445. arcpy.CalculateField_management("FinalSmallPoints.shp", "SMALL_BIG", "1")
  446.  
  447. # Add field "Prec_71_00" and copy values from old field
  448. arcpy.AddField_management("FinalSmallPoints.shp", "Prec_71_00", "FLOAT")
  449. arcpy.CalculateField_management("FinalSmallPoints.shp", "Prec_71_00", "[t3_FinalSm]/100000", "VB", "")
  450. dropFields = ["t3_FinalSm"]
  451. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  452.  
  453. # Add field "Prec_81_10" and copy values from old field
  454. arcpy.AddField_management("FinalSmallPoints.shp", "Prec_81_10", "FLOAT")
  455. arcpy.CalculateField_management("FinalSmallPoints.shp", "Prec_81_10", "[t3_Final_1]/100000", "VB", "")
  456. dropFields = ["t3_Final_1"]
  457. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  458.  
  459. # Add field "Temp_71_00" and copy values from old field
  460. arcpy.AddField_management("FinalSmallPoints.shp", "Temp_71_00", "FLOAT")
  461. arcpy.CalculateField_management("FinalSmallPoints.shp", "Temp_71_00", "[t3_Final_2]/100", "VB", "")
  462. dropFields = ["t3_Final_2"]
  463. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  464.  
  465. # Add field "Temp_81_10" and copy values from old field
  466. arcpy.AddField_management("FinalSmallPoints.shp", "Temp_81_10", "FLOAT")
  467. arcpy.CalculateField_management("FinalSmallPoints.shp", "Temp_81_10", "[t3_Final_3]/100", "VB", "")
  468. dropFields = ["t3_Final_3"]
  469. arcpy.DeleteField_management("FinalSmallPoints.shp", dropFields)
  470.  
  471. print "SUCCESSFULLY COMPLETED EXTRACT VALUES TO POINT"
  472.  
  473. ##### Step 5 - Feed newly created .shp for big sites into existing loop.
  474. print "USE NEW .SHP IN 'FOR' LOOP"
  475.  
  476. # Set local variables
  477. bigSites = "t_FinalBigPoints.shp"
  478. bigSheds = "t_bigSheds.shp"
  479.  
  480. P71_00 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_71_00'
  481. P81_10 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/ppt_81_10'
  482. T71_00 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg71_00'
  483. T81_10 = r'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Data/Temperature/tavg81_10'
  484.  
  485. ###############################################################
  486.  
  487.  
  488. print "END EDITING SCRIPT --> RETURN TO ORIGINAL PYTHON SCRIPT"
  489.  
  490.  
  491. ###############################################################
  492.  
  493. # A loop to select out one feature from the spatial scale shapefiles
  494.  
  495. arcpy.MakeFeatureLayer_management(bigSites, 'inputPointselect') # create a feature layer from the Test_Points
  496. 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
  497.  
  498. #This section adds the fields the will be calculated to the final shapefiles
  499. arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Prec_71_00', 'FLOAT') # Add the Average Precip from 1971-2000 field
  500. arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Prec_81_10', 'FLOAT') # Add the Average Precip from 1981-2010 field
  501. arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Temp_71_00', 'FLOAT') # Add the Average Temp from 1971-2000 field
  502. arcpy.AddField_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Temp_81_10', 'FLOAT') # Add the Average Temp from 1981-2010 field
  503.  
  504. Sheds1 = arcpy.SearchCursor(bigSheds) # The Main Loop - it uses the watershed shapefile to identify sites the will have attributes calculated
  505. for row in Sheds1:
  506. ID1 = row.SiteID # Store the SiteID as ID1
  507. ID1str = str(ID1) # Convert the siteID to a string
  508. print 'Beginning Site %s' % (ID1str)
  509.  
  510. arcpy.MakeFeatureLayer_management(bigSites, 'pointselect') # create a feature layer from the points
  511. arcpy.SelectLayerByAttribute_management ('pointselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single point
  512. arcpy.CopyFeatures_management ('pointselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testpoint.shp') # save the selected point
  513. temppoint = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testpoint.shp' # store the temporary point shapefile as the temppoint variable
  514.  
  515. arcpy.MakeFeatureLayer_management(bigSheds, 'shedselect') # create a feature layer from the watersheds
  516. arcpy.SelectLayerByAttribute_management ('shedselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
  517. arcpy.CopyFeatures_management ('shedselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testshed.shp') # save the selected watershed
  518. tempshed = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testshed.shp' # store the temporary shed shapefile as the tempshed variable
  519.  
  520. #arcpy.MakeFeatureLayer_management(buffs, 'buffselect') # create a feature layer from the watersheds
  521. #arcpy.SelectLayerByAttribute_management ('buffselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
  522. #arcpy.CopyFeatures_management ('buffselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testbuff.shp') # save the selected watershed
  523. #tempbuff = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testbuff.shp' # store the temporary shed shapefile as the tempshed variable
  524.  
  525. #arcpy.MakeFeatureLayer_management(segs, 'segselect') # create a feature layer from the watersheds
  526. #arcpy.SelectLayerByAttribute_management ('segselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
  527. #arcpy.CopyFeatures_management ('segselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testseg.shp') # save the selected watershed
  528. #tempseg = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testseg.shp' # store the temporary shed shapefile as the tempshed variable
  529.  
  530. #arcpy.MakeFeatureLayer_management(reaches, 'rchselect') # create a feature layer from the watersheds
  531. #arcpy.SelectLayerByAttribute_management ('rchselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the single watershed
  532. #arcpy.CopyFeatures_management ('rchselect', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testrch.shp') # save the selected watershed
  533. #temprch = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/testrch.shp' # store the temporary shed shapefile as the tempshed variable
  534.  
  535. #arcpy.MakeFeatureLayer_management(subbasin, 'sb2') # create a feature layer from the subbasins
  536. #arcpy.SelectLayerByLocation_management ('sb2', 'INTERSECT', temppoint, 0.1) # select the HUC that the single point falls in
  537. #arcpy.CopyFeatures_management ('sb2', 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/tempHUC.shp') # save the selected HUC
  538. #tempHUC = 'C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Test/tempHUC.shp' # store the temporary huc shapefile as the temphuc variable
  539.  
  540.  
  541. ### Climate 30 year averages ###
  542.  
  543.  
  544. # This Section calculates the actual precip and temp values
  545.  
  546. ########################################################################
  547.  
  548. ########################################################################
  549.  
  550. 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
  551. 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
  552. 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
  553. 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
  554.  
  555.  
  556. P71_00Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Prec71_00, 'MEAN', 'DATA') #Get the MeAN value from the raster
  557. P71_00Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P71_00Mean.tif') # Save the output from the MEAN
  558. P71_00MeanB = arcpy.sa.Int(P71_00Mean*100) # Turn it into an integer raster
  559. P71_00MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P71_00MeanB.tif') # Save the output raster
  560.  
  561. rowP71Mean = arcpy.SearchCursor(P71_00MeanB) # Run a search cursor to get the MEAN value
  562. for Mean in rowP71Mean:
  563. P71_00Mean2 = Mean.VALUE # Store the MEAN value as a variable
  564. P71_00Mean3 = (float(P71_00Mean2)/10000000)
  565.  
  566. P81_10Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Prec81_10, 'MEAN', 'DATA') #Get the MeAN value from the raster
  567. P81_10Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P81_10Mean.tif') # Save the output from the MEAN
  568. P81_10MeanB = arcpy.sa.Int(P81_10Mean*100) # Turn it into an integer raster
  569. P81_10MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/P81_10MeanB.tif') # Save the output raster
  570.  
  571. rowP71Mean = arcpy.SearchCursor(P81_10MeanB) # Run a search cursor to get the MEAN value
  572. for Mean in rowP71Mean:
  573. P81_10Mean2 = Mean.VALUE # Store the MEAN value as a variable
  574. P81_10Mean3 = (float(P81_10Mean2)/10000000)
  575.  
  576. T71_00Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Temp71_00, 'MEAN', 'DATA') #Get the MeAN value from the raster
  577. T71_00Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T71_00Mean.tif') # Save the output from the MEAN
  578. T71_00MeanB = arcpy.sa.Int(T71_00Mean*100) # Turn it into an integer raster
  579. T71_00MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T71_00MeanB.tif') # Save the output raster
  580.  
  581. rowT71Mean = arcpy.SearchCursor(T71_00MeanB) # Run a search cursor to get the MEAN value
  582. for Mean in rowT71Mean:
  583. T71_00Mean2 = Mean.VALUE # Store the MEAN value as a variable
  584. T71_00Mean3 = (float(T71_00Mean2)/10000)
  585.  
  586. T81_10Mean = arcpy.sa.ZonalStatistics (tempshed, 'SiteID', Temp81_10, 'MEAN', 'DATA') #Get the MeAN value from the raster
  587. T81_10Mean.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T81_10Mean.tif') # Save the output from the MEAN
  588. T81_10MeanB = arcpy.sa.Int(T81_10Mean*100) # Turn it into an integer raster
  589. T81_10MeanB.save('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/T81_10MeanB.tif') # Save the output raster
  590.  
  591. rowT81Mean = arcpy.SearchCursor(T81_10MeanB) # Run a search cursor to get the MEAN value
  592. for Mean in rowT81Mean:
  593. T81_10Mean2 = Mean.VALUE # Store the MEAN value as a variable
  594. T81_10Mean3 = (float(T81_10Mean2)/10000)
  595.  
  596.  
  597.  
  598.  
  599.  
  600. #This section sends the stored values to the data table of a Point Shapefile (No More cryptic CSV's!)
  601. arcpy.MakeFeatureLayer_management('C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Temp/FinalBigPoints.shp', 'Precipselect') # create a feature layer from the final point shapefile
  602. arcpy.SelectLayerByAttribute_management ('Precipselect', 'NEW_SELECTION', 'SiteID = %s' % (ID1str)) # select the point that matches the siteID the loop is on
  603. arcpy.CalculateField_management('Precipselect', 'Prec_71_00', P71_00Mean3) # Put the 30 year avereage precip for 1971-2000 in the Precip_71_00 field
  604. arcpy.CalculateField_management('Precipselect', 'Prec_81_10', P81_10Mean3) # Put the 30 year avereage precip for 1981-2010 in the Precip_81_10 field
  605. arcpy.CalculateField_management('Precipselect', 'Temp_71_00', T71_00Mean3) # Put the 30 year avereage temp for 1971-2000 in the Temp_71_00 field
  606. arcpy.CalculateField_management('Precipselect', 'Temp_81_10', T81_10Mean3) # Put the 30 year avereage temp for 1971-2000 in the Temp_81_10 field
  607.  
  608. ###############################################################
  609.  
  610.  
  611. print "RESUME EDITING PYTHON SCRIPT"
  612.  
  613.  
  614. ###############################################################
  615.  
  616. ##### Last step: Merge shapefiles #####
  617.  
  618. print "Merging and writing output"
  619.  
  620. # Merge the point files
  621. Climate30 = "C:/Users/karenkburke/MyDocuments/ArcGIS/Climate/Output/Climate30.shp"
  622. arcpy.Merge_management(["FinalBigPoints.shp", "FinalSmallPoints.shp"], Climate30)
  623.  
  624. print "SUCCESSFULLY MERGED OUTPUT FILE"
  625.  
  626. ###############################################################
  627.  
  628.  
  629. print "END OF EDITING PYTHON SCRIPT"
  630.  
  631.  
  632. ###############################################################
  633.  
  634. print "END PYTHON SCRIPT!!!"
  635. print "-----------------------------------------------------------------------------------------"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement