Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ArcPy snippets I always search for
- ## copy fgdb from samplesdata to home folder
- ```python
- import os
- home_dir = os.path.join(os.path.sep, 'arcgis','home')
- samples_dir = os.path.join(arcgis_dir, 'samplesdata')
- print(f"home dir: {home_dir}")
- print(f"samples dir: {samples_dir}")
- def copy_sample_gdb_to_home(gdb_zip_name):
- """Given the full filename (with extensions) of a gdb zip file in
- /arcgis/samplesdata/, will copy and unzip that gdb to /arcgis/home/
- Will return the full path to the unzipped gdb in home"""
- # Get the full paths of all the source and destination files to copy
- gdb_dir_name = gdb_zip_name.split(".zip")[0]
- gdb_zip_path_src = os.path.join(samples_dir, gdb_zip_name)
- gdb_dir_path_src = os.path.join(samples_dir, gdb_dir_name)
- gdb_zip_path_dst = os.path.join(home_dir, gdb_zip_name)
- gdb_dir_path_dst = os.path.join(home_dir, gdb_dir_name)
- # If the gdb has been copied/unzipped to home dir before, delete it
- if os.path.exists(gdb_zip_path_dst):
- os.remove(gdb_zip_path_dst)
- if os.path.exists(gdb_dir_path_dst):
- shutil.rmtree(gdb_dir_path_dst)
- # Copy the zip file to home, unzip it
- shutil.copy(gdb_zip_path_src, gdb_zip_path_dst)
- zip_ref = zipfile.ZipFile(gdb_zip_path_dst, 'r')
- zip_ref.extractall(home_dir)
- zip_ref.close()
- # Return the output full path to /arcgis/home/unzipped_gdb
- return gdb_dir_path_dst
- # call the function to copy data needed for this analysis
- gdb_path = copy_sample_gdb_to_home('Analyze_Urban_Heat_Using_Kriging.gdb.zip')
- print(f"GDB succesfully copied to {gdb_path}")
- ```
- ## Extract a fgdb.zip or shp.zip
- ```python
- with zipfile.ZipFile('fgdb.zip', 'r') as zip_handle:
- zip_handle.extractall(home_dir)
- ```
- ## List content of a directory / fgdb / server connection
- ```python
- walk_result = arcpy.da.Walk('/arcgis/home/boston')
- for dirpath, dirnames, filenames in walk_result:
- for filename in filenames:
- print(f"{dirpath}/{filename}")
- ```
- ## Read a Python API SeDF into in-memory ArcPy Featureclass
- ```python
- # key here is to specify location as 'memory/dataset-name'
- fc = boston_sdf.spatial.to_featureclass('memory/boston_fc')
- fc
- >>> 'memory/boston_fc'
- # you can use this in downstream processing and keep chaining results in memory
- arcpy.analysis.Buffer(in_features = 'memory/boston_fc',
- out_feature_class='memory/boston_buff_fc',
- buffer_distance_or_field=10)
- >>> <Result 'memory/boston_buff_fc'>
- # to see the list of datasets you have in-memory, do this
- arcpy.env.workspace = 'memory'
- arcpy.ListFeatureClasses()
- >>> ['boston_buff_fc', 'boston_fc']
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement