Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. # ArcPy snippets I always search for
  2. ## copy fgdb from samplesdata to home folder
  3. ```python
  4. import os
  5. home_dir = os.path.join(os.path.sep, 'arcgis','home')
  6. samples_dir = os.path.join(arcgis_dir, 'samplesdata')
  7.  
  8. print(f"home dir: {home_dir}")
  9. print(f"samples dir: {samples_dir}")
  10.  
  11. def copy_sample_gdb_to_home(gdb_zip_name):
  12. """Given the full filename (with extensions) of a gdb zip file in
  13. /arcgis/samplesdata/, will copy and unzip that gdb to /arcgis/home/
  14. Will return the full path to the unzipped gdb in home"""
  15.  
  16. # Get the full paths of all the source and destination files to copy
  17. gdb_dir_name = gdb_zip_name.split(".zip")[0]
  18. gdb_zip_path_src = os.path.join(samples_dir, gdb_zip_name)
  19. gdb_dir_path_src = os.path.join(samples_dir, gdb_dir_name)
  20.  
  21. gdb_zip_path_dst = os.path.join(home_dir, gdb_zip_name)
  22. gdb_dir_path_dst = os.path.join(home_dir, gdb_dir_name)
  23.  
  24. # If the gdb has been copied/unzipped to home dir before, delete it
  25. if os.path.exists(gdb_zip_path_dst):
  26. os.remove(gdb_zip_path_dst)
  27. if os.path.exists(gdb_dir_path_dst):
  28. shutil.rmtree(gdb_dir_path_dst)
  29.  
  30. # Copy the zip file to home, unzip it
  31. shutil.copy(gdb_zip_path_src, gdb_zip_path_dst)
  32. zip_ref = zipfile.ZipFile(gdb_zip_path_dst, 'r')
  33. zip_ref.extractall(home_dir)
  34. zip_ref.close()
  35.  
  36. # Return the output full path to /arcgis/home/unzipped_gdb
  37. return gdb_dir_path_dst
  38.  
  39. # call the function to copy data needed for this analysis
  40. gdb_path = copy_sample_gdb_to_home('Analyze_Urban_Heat_Using_Kriging.gdb.zip')
  41.  
  42. print(f"GDB succesfully copied to {gdb_path}")
  43. ```
  44. ## Extract a fgdb.zip or shp.zip
  45. ```python
  46. with zipfile.ZipFile('fgdb.zip', 'r') as zip_handle:
  47. zip_handle.extractall(home_dir)
  48. ```
  49. ## List content of a directory / fgdb / server connection
  50.  
  51. ```python
  52. walk_result = arcpy.da.Walk('/arcgis/home/boston')
  53.  
  54. for dirpath, dirnames, filenames in walk_result:
  55. for filename in filenames:
  56. print(f"{dirpath}/{filename}")
  57. ```
  58.  
  59. ## Read a Python API SeDF into in-memory ArcPy Featureclass
  60. ```python
  61. # key here is to specify location as 'memory/dataset-name'
  62. fc = boston_sdf.spatial.to_featureclass('memory/boston_fc')
  63. fc
  64. >>> 'memory/boston_fc'
  65.  
  66. # you can use this in downstream processing and keep chaining results in memory
  67. arcpy.analysis.Buffer(in_features = 'memory/boston_fc',
  68. out_feature_class='memory/boston_buff_fc',
  69. buffer_distance_or_field=10)
  70. >>> <Result 'memory/boston_buff_fc'>
  71.  
  72. # to see the list of datasets you have in-memory, do this
  73. arcpy.env.workspace = 'memory'
  74. arcpy.ListFeatureClasses()
  75. >>> ['boston_buff_fc', 'boston_fc']
  76. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement