AsShape

摘要

AsShape 函数用于将 Esri JSON 或 GeoJSON 几何转换为 ArcPy 几何,并将 Esri JSON 要素集转换为 ArcPy 要素集。 GeoJSON 是一种地理空间数据的交换格式,可用于对地理数据结构进行编码。

说明

根据 GeoJSON 创建的几何对象的空间参考将为 WGS84。

注:

几何对象具有的 JSON 属性会将几何返回为 Esri JSON。

语法

AsShape (geojson_struct, {esri_json})
参数说明数据类型
geojson_struct

The geojson_struct includes type and coordinates.

The following strings are included for type: Point, LineString, Polygon, MultiPoint, and MultiLineString.

Dictionary
esri_json

Specifies whether the input JSON is evaluated as Esri JSON or GeoJSON. If True, the input is evaluated as Esri JSON.

(默认值为 False)

Boolean
返回值
数据类型说明
Geometry

基于输入 GeoJSON 或 Esri JSON 对象返回几何对象(PointGeometryMultipointPolylinePolygon)。

import arcpy
geojson_point = {"type": "Point", "coordinates": [5.0, 5.0]}
point = arcpy.AsShape(geojson_point)

如果 Esri JSON 是要素集,AsShape 将返回 FeatureSet。 ArcGIS REST API 规范将要素集定义为具有特定几何类型、字段和空间参考的要素集合。 JSON 转要素工具可用于将 Esri JSON 直接转换为要素类。

代码示例

AsShape 示例 1

使用 GeoJSON 对象创建 PointGeometry 对象。

import arcpy

geojson_point = {
    "type": "Point", 
    "coordinates": [5.0, 5.0]}
point = arcpy.AsShape(geojson_point)
AsShape 示例 2

使用 Esri JSON 对象创建 PointGeometry 对象。

import arcpy

esri_json = {
    "x": -122.65,
    "y": 45.53,
    "spatialReference": {
        "wkid": 4326}}
# Set the second parameter to True to evaluate as Esri JSON
point = arcpy.AsShape(esri_json, True)
AsShape 示例 3

使用 GeoJSON 对象创建 Multipoint 对象。

import arcpy

geojson_multipoint = {
    "type": "MultiPoint",
    "coordinates": [[5.0, 4.0], [8.0, 7.0]]}
multipoint = arcpy.AsShape(geojson_multipoint)
AsShape 示例 4

使用 Esri JSON 对象创建 Multipoint 对象。

import arcpy

esri_json = {
    "points" : [
        [-97.06138, 32.837],
        [-97.06133, 32.836],
        [-97.06124, 32.834],
        [-97.06127, 32.832]],
    "spatialReference" : {"wkid" : 4326}}
# Set the second parameter to True to evaluate as Esri JSON
multipoint = arcpy.AsShape(esri_json, True)
AsShape 示例 5

使用 GeoJSON 对象创建 Polyline 对象。

import arcpy

geojson_linestring = {
    "type": "LineString",
    "coordinates": [[5.0, 4.0], [8.0, 7.0]]}
polyline = arcpy.AsShape(geojson_linestring)
AsShape 示例 6

使用 Esri JSON 对象创建 Polyline 对象。

import arcpy

esri_json = {
    "paths" : [
        [[-97.08, 32.8], [-97.05, 32.6], [-97.06, 32.7],
         [-97.07, 32.6]],
        [[-97.4, 32.5], [-97.2, 32.75]]],
    "spatialReference" : {"wkid" : 4326}}
# Set the second parameter to True to evaluate as Esri JSON
polyline = arcpy.AsShape(esri_json, True)
AsShape 示例 7

使用 GeoJSON 对象创建多部件 Polyline 对象。

import arcpy

geojson_multilinestring = {
    "type": "MultiLineString",
    "coordinates": [
        [[5.0, 4.0], [8.0, 7.0]],
        [[4.0, 5.0], [7.0, 8.0]]]}
polyline = arcpy.AsShape(geojson_multilinestring)
AsShape 示例 8

使用 GeoJSON 对象创建 Polygon 对象。

import arcpy

geojson_polygon = {
    "type": "Polygon",
    "coordinates": [
        [[10.0, 0.0], [20.0, 0.0], [20.0, 10.0], [10.0, 10.0],
         [10.0, 0.0]]]}
polygon = arcpy.AsShape(geojson_polygon)
AsShape 示例 9

使用 GeoJSON 对象创建包含孔的 Polygon 对象。

import arcpy

geojson_polygon = {
    "type": "Polygon",
    "coordinates": [
        [[10.0, 0.0], [20.0, 0.0], [20.0, 10.0], [10.0, 10.0],
         [10.0, 0.0]],
        [[12.0, 2.0], [18.0, 2.0], [18.0,  8.0], [12.0,  8.0],
         [12.0, 2.0]]]}
polygon = arcpy.AsShape(geojson_polygon)

相关主题