Upload datasets to use with ArcPy

ArcGIS Notebook Server allows you to upload shapefiles and file geodatabases that can be accessed within notebooks to use with ArcPy.

Upload datasets to use in a notebook

To upload shapefiles or file geodatabases to use with ArcPy in a notebook, do the following:

  1. Compress the dataset you want to upload into a .zip file.
  2. In the notebook editor, click the Files tab.
  3. On the Files tab, browse to /arcgis/home.
  4. Click Choose file and select the .zip file of the dataset.
  5. Click Upload.
  6. In the notebook, use one of the following methods to unzip the file:
    1. Use IPython magic statements from within a notebook cell.
      !unzip /arcgis/home/watersheds.zip -d /arcgis/home
      
    2. Use the Python Zip module to unzip the file.
      import zipfile
      with zipfile.ZipFile("/arcgis/home/watersheds.zip", "r") as zip_ref:
          zip_ref.extractall("/arcgis/home")
      

To learn more about using ArcPy in a notebook, see Use ArcPy in a notebook.

Use uploaded datasets with ArcPy in a notebook

Once you have uploaded a shapefile or file geodatabase, you can access it from a notebook.

Use an uploaded shapefile with ArcPy

The following steps outline an example workflow of using the ArcPy Buffer tool with an uploaded shapefile:

  1. Download the sample dataset from the Python start dataset item page.
  2. Upload the .zip file to a notebook workspace using the steps listed in the Upload datasets to use in a notebook section above.
  3. Import ArcGIS API for Python and ArcPy.

    from arcgis.gis import GIS
    gis = GIS("home")
    import arcpy
    

  4. Unzip the dataset that you uploaded to the workspace directory.

    !unzip /arcgis/home/PythonStart.zip -d /arcgis/home
    

  5. Set the ArcPy workspace to the directory path of the extracted file.

    arcpy.env.workspace = "/arcgis/home/PythonStart"
    

  6. Create a buffer of 500 meters around each fire station in the fire_stations.shp file.

    result = arcpy.analysis.Buffer("fire_stations.shp", "fire_stations_500m", "500 METERS")
    

  7. Generate and print a description of the resulting buffer shapefile dataset.

    # Describe the resulting shapefile dataset
    desc = arcpy.Describe("fire_stations_500m.shp")
    
    # Print dataset properties
    print(f"Dataset Type: {desc.datasetType}")
    print(f"Shape Type: {desc.shapeType}")
    print(f"Feature Type: {desc.featureType}")
    print(f"Spatial Index: {desc.hasSpatialIndex}")
    print(f"Spatial reference name: {desc.spatialReference.name}")
    print(f"Extent:\n\tXMin: {desc.extent.XMin}\n\tXMax: {desc.extent.XMax}")
    print(f"\tYMin: {desc.extent.YMin}\n\tYMax: {desc.extent.YMax}")
    

  8. Print the names and types of fields in the buffer shapefile.

    for field in desc.fields:
        print("%-22s %s %s" % (field.name, ":", field.type))
    

  9. Create a .zip file of the buffer shapefile dataset.

    import os
    import fnmatch
    import zipfile
     
    # The path for listing items
    path = '/arcgis/home/PythonStart/'
    os.chdir(path)
     
    # List of files in complete directory
    file_list = []
    
    # Loop to extract files containing word "fire_stations_500m"
    for path, folders, files in os.walk(path):
        for file in files:
            if fnmatch.fnmatch(file, '*fire_stations_500m*'):
                file_list.append(file)
    
    with zipfile.ZipFile('/arcgis/home/fire_stations_500m.zip', 'w') as zipF:
        for file in file_list:
            zipF.write(file, compress_type=zipfile.ZIP_DEFLATED)
    

  10. Publish the buffer shapefile as a hosted feature layer.

    item = gis.content.add({}, '/arcgis/home/fire_stations_500m.zip')
    published_item = item.publish()
    published_item.share(everyone=True)
    display(published_item)
    

  11. Delete the buffer shapefile.

    arcpy.management.Delete("fire_stations_500m.shp")
    

Through this example workflow, you will have created and published a new buffer shapefile by using ArcPy with an uploaded dataset.

Use an uploaded file geodatabase with ArcPy

The following steps outline an example workflow for uploading a file geodatabase to use with ArcPy.

  1. Download the sample dataset from the Singapore data geodatabase item page.
  2. Upload the .zip file containing the file geodatabase to a notebook using the steps listed in the Upload datasets to use in a notebook section above.
  3. Import ArcGIS API for Python and ArcPy.

    from arcgis.gis import GIS
    gis = GIS("home")
    import arcpy
    

  4. Unzip the dataset uploaded to a workspace directory.

    !unzip /arcgis/home/Singapore_Data.gdb.zip -d /arcgis/home
    

  5. Set the ArcPy workspace to the directory path of the extracted file.

    arcpy.env.workspace = "/arcgis/home/Singapore_Data.gdb"
    

  6. List the names of feature classes contained within the file geodatabase.

    singapore_data = arcpy.ListFeatureClasses()
    singapore_data
    

  7. List the fields contained within one of the feature classes.

    singapore_tourist_attractions = singapore_data[2]
    singapore_tourist_attractions_fields = []
    fields = arcpy.ListFields(singapore_tourist_attractions)
    for field in fields:
        if (field.name != 'Shape'):
            singapore_tourist_attractions_fields.append(field.name)
    singapore_tourist_attractions_fields
    

  8. For each row in the dataset, print the object ID, place-name, and address field values.

    with arcpy.da.SearchCursor(singapore_tourist_attractions, singapore_tourist_attractions_fields) as cursor:
        for row in cursor:
            print(f'{row[0]}. {row[1]}, {row[2]}')