Upload datasets to use with ArcPy
ArcGIS Notebook Server allows you to upload shapefiles and file geodatabases that can be accessed within your notebooks to use with ArcPy.
Upload datasets to use in a notebook
To upload shapefiles or file geodatabases to use with ArcPy in your notebook, do the following:
Compress the dataset you want to upload into a
.zipfile.In the notebook editor, click the Files tab.
On the Files tab, browse to
/arcgis/home.Click Choose file and select the
.zipfile of your dataset.Click Upload.
In your notebook, use one of the following methods to unzip your file:
Unzip the dataset that you uploaded to your workspace directory.
!unzip /arcgis/home/PythonStart.zip -d /arcgis/homeUnzip the dataset that you uploaded to your workspace directory.
!tar -xf /arcgis/home/PythonStart.zip -C /arcgis/homeUse 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 your notebook, see Use ArcPy in a notebook.
Use uploaded datasets with ArcPy in a notebook
Once you have uploaded your shapefile or file geodatabase, you can access it from your notebook.
Use an uploaded shapefile with ArcPy
The following steps outline an example workflow of using the ArcPy Buffer tool with an uploaded shapefile:
Download the sample dataset from the Python start dataset item page.
Upload the
.zipfile to your notebook workspace using the steps listed in the Upload datasets to use in a notebook section above.Import ArcGIS API for Python and ArcPy.
from arcgis.gis import GIS gis = GIS("home") import arcpyUnzip the dataset that you uploaded to your workspace directory.
!unzip /arcgis/home/PythonStart.zip -d /arcgis/homeUnzip the dataset that you uploaded to your workspace directory.
!tar -xf /arcgis/home/PythonStart.zip -C /arcgis/homeSet the ArcPy workspace to the directory path of the extracted file.
arcpy.env.workspace = "/arcgis/home/PythonStart"Create a buffer of 500 meters around each fire station in the
fire_stations.shpfile.result = arcpy.analysis.Buffer("fire_stations.shp", "fire_stations_500m", "500 METERS")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}")Print the names and types of fields in the buffer shapefile.
for field in desc.fields: print("%-22s %s %s" % (field.name, ":", field.type))Create a
.zipfile 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)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)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.
Download the sample dataset from the Singapore data geodatabase item page.
Upload the
.zipfile containing the file geodatabase to your notebook using the steps listed in the Upload datasets to use in a notebook section above.Import ArcGIS API for Python and ArcPy.
from arcgis.gis import GIS gis = GIS("home") import arcpyUnzip the dataset that you uploaded to your workspace directory.
!unzip /arcgis/home/PythonStart.zip -d /arcgis/homeUnzip the dataset that you uploaded to your workspace directory.
!tar -xf /arcgis/home/PythonStart.zip -C /arcgis/homeSet the ArcPy workspace to the directory path of the extracted file.
arcpy.env.workspace = "/arcgis/home/Singapore_Data.gdb"List the names of feature classes contained within the file geodatabase.
singapore_data = arcpy.ListFeatureClasses() singapore_dataList 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_fieldsFor each row in the dataset, print the objectid, 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]}')