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 .zip file.
- In the notebook editor, click the Files tab.
- On the Files tab, browse to /arcgis/home.
- Click Choose file and select the .zip file of your dataset.
- Click Upload.
- In your notebook, use one of the following methods to unzip your file:
- Use IPython magic statements from within a notebook cell.
!unzip /arcgis/home/watersheds.zip -d /arcgis/home
- 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")
- Use IPython magic statements from within a notebook cell.
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 .zip file 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 arcpy
- Unzip the dataset that you uploaded to your workspace directory.
!unzip /arcgis/home/PythonStart.zip -d /arcgis/home
- Set 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.shp file.
result = arcpy.Buffer_analysis("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("Feature Type: " + desc.featureType) print("Shape Type : " + desc.shapeType) print("Spatial Index: " + str(desc.hasSpatialIndex)) print(("Dataset Type: {0}".format(desc.datasetType))) print(("Extent:\n XMin: {0}, XMax: {1}, YMin: {2}, YMax: {3}".format( desc.extent.XMin, desc.extent.XMax, desc.extent.YMin, desc.extent.YMax))) print(("Spatial reference name: {0}:".format(desc.spatialReference.name)))
- Print the names and types of fields in the buffer shapefile.
for field in desc.fields: print("%-22s %s %s" % (field.name, ":", field.type)) #print field.name + " = " + field.type
- Create a .zip file of the buffer shapefile dataset.
import os, 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.Delete_management("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 .zip file 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 arcpy
- Unzip the dataset uploaded to your workspace directory.
!unzip /arcgis/home/Singapore_Data.gdb.zip -d /arcgis/home
- Set 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_data
- 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
- For 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(u'{0}, {1}, {2}'.format(row[0], row[1], row[2]))