Skip to content

Instantly share code, notes, and snippets.

@AtmaMani
Last active September 18, 2020 20:39
Show Gist options
  • Save AtmaMani/99d01aa39b1422fb6ef86e3989ad3a12 to your computer and use it in GitHub Desktop.
Save AtmaMani/99d01aa39b1422fb6ef86e3989ad3a12 to your computer and use it in GitHub Desktop.
DSX snippets

Jupyter tricks

Creating note cells

<div class="alert alert-info">
    <b>Note:</b> If you are running this guide in an environment <b>without <code>arcpy</code></b>, you can comment out the line that imports it and proceed with the guide. The Python API will look for <code>shapely</code> library and use it for geometry operations.
</div>

Embed Python API map widgets within HBox and VBox

from ipywidgets import HBox, VBox, Label, Layout

# create 3 map widgets
map1 = gis.map("China", 4)
map2 = gis.map("China", 4)
map3 = gis.map("China", 4)

# set hbox layout preferences
hbox_layout = Layout()
hbox_layout.justify_content = 'space-around'

# create 2 hbox - one for title, another for maps
hb1 = HBox([Label('31-Dec-19'),Label('29-Jan-20'), Label('20-Mar-20')])
hb2 = HBox([map1, map2, map3])
hb1.layout,hb2.layout = hbox_layout, hbox_layout

# sync all maps
map1.sync_navigation(map2)
map2.sync_navigation(map3)

# each hbox is a row in the vbox
VBox([hb1,hb2])

ArcPy snippets I always search for

copy fgdb from samplesdata to home folder

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

with zipfile.ZipFile('fgdb.zip', 'r') as zip_handle:
  zip_handle.extractall(home_dir)

List content of a directory / fgdb / server connection

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

# 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']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment