Skip To Content

上传数据集以用于 ArcPy

ArcGIS Notebook Server 允许您上传 shapefile 和文件地理数据库,然后可以在您的笔记本中访问以用于 ArcPy

上传要在笔记本中使用的数据集

要上传 shapefile 或文件地理数据库以在笔记本中用于 ArcPy,请执行以下操作:

  1. 将您想要上传的数据集压缩到 .zip 文件中。
  2. 在笔记本编辑器中,单击文件选项卡。
  3. 文件选项卡上,浏览到 /arcgis/home
  4. 单击选择文件然后选择数据集的 .zip 文件。
  5. 单击上传
  6. 在您的笔记本中,使用以下任一方法解压文件。
    1. 在笔记本单元格中使用 IPython 魔术语句。
      !unzip /arcgis/home/watersheds.zip -d /arcgis/home
    2. 使用 Python 压缩模块解压文件。
      import zipfile
      with zipfile.ZipFile("/arcgis/home/watersheds.zip", "r") as zip_ref:
          zip_ref.extractall("/arcgis/home")

要了解在笔记本中使用 ArcPy 的详细信息,请参阅在笔记本中使用 ArcPy

在笔记本中将上传的数据集用于 ArcPy

上传 shapefile 或文件地理数据库后,您可以通过笔记本进行访问。

将上传的 shapefile 用于 ArcPy

以下步骤概述了通过上传的 shapefile 使用 ArcPy 缓冲区工具的工作流的示例:

  1. Python 起始数据集项目页面下载示例数据集。
  2. 使用以上上传数据集以在笔记本中使用部分中列出的步骤将 .zip 文件上传到您的笔记本工作空间。
  3. 导入 ArcGIS API for PythonArcPy

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

  4. 将您上传到工作空间目录的数据集解压。

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

  5. ArcPy 工作空间设置为已提取文件的目录路径。

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

  6. fire_stations.shp 文件中的每个消防站周围创建一个 500 米的缓冲区。

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

  7. 生成并打印结果缓冲区 shapefile 数据集的描述。

    # 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. 打印缓冲区 shapefile 中的字段名称和类型。

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

  9. 创建缓冲区 shapefile 数据集的 .zip 文件。

    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. 将缓冲区 shapefile 发布为托管要素图层。

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

  11. 删除缓冲区 shapefile。

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

通过此示例工作流,您已通过将 ArcPy 与上传的数据集配合使用的方法创建并发布了新的缓冲区 shapefile。

将上传的文件地理数据库用于 ArcPy

以下步骤概述了上传文件地理数据库以用于 ArcPy 的示例工作流。

  1. 新加坡数据地理数据库项目页面下载样本数据集。
  2. 使用以上上传数据集以在笔记本中使用部分中列出的步骤将包含文件地理数据库的 .zip 文件上传到您的笔记本。
  3. 导入 ArcGIS API for PythonArcPy

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

  4. 将上传到工作空间目录的数据集解压。

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

  5. ArcPy 工作空间设置为已提取文件的目录路径。

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

  6. 列出文件地理数据库包含的要素类的名称。

    singapore_data = arcpy.ListFeatureClasses()
    singapore_data

  7. 列出以下要素类之一包含的字段。

    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. 对于数据集中的每一行,打印 ObjectID,地名和地址字段值。

    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]}')