如何在 JSON 文件中插入空间参考以在服务器上添加要素

How to insert Spatial Reference in a JSON file to addFeatures on server

我正在尝试在地图服务器上的 Arcgis Enterprise 上使用 REST API 添加点,但我不知道在哪里插入空间参考属性线段。

我在 Python 上创建了一个脚本,使用 JSON 文件作为输入,其中包含要素的属性和几何形状。

我通过ArcMap上的"Features to Json"工具生成的JSON文件,但是我需要取消初始部分(见下面的代码)否则它不会添加功能,因为它给我一个错误。 该初始部分包含空间参考信息。 问题是一旦我删除了这部分,我需要在其他地方插入空间参考。我尝试在代码末尾插入,但功能定位不正确。 有谁知道 how/where 我应该在 Json 文件中添加包含空间参考信息的部分,以便他可以正确定位要素? 脚本运行正常,问题是 JSON 文件。

此致,

初始部分已删除:

{
  "displayFieldName" : "",
  "fieldAliases" : {
    "OBJECTID" : "OBJECTID",
    "Layer" : "Layer",
    "RefName" : "RefName",
    "SETE" : "SEDE TECNICA",
    "DEF_SETE" : "DEFINIZIONE",
    "LIVELLO" : "LIVELLO",
    "SFID" : "ID"
  },
  "geometryType" : "esriGeometryPoint",
  "spatialReference" : {
    "wkid" : 32633,
    "latestWkid" : 32633
  },
  "fields" : [
    {
PART USED ON THE JSON FILE: 

[
    {
          "attributes" : {
            "OBJECTID" : 251,
            "Layer" : "RM$TXT",
            "RefName" : "ATRIO",
            "SETE" : "LO0445",
            "DEF_SETE" : "APRILIA",
            "LIVELLO" : 0,
            "SFID" : "LO0445_POI_000001"
          },
          "geometry" : {
            "x" : 303244.31379999965,
            "y" : 4607198.9022000004
          },
          "spatialReference" : {
        "wkid" : 32633,
        "latestWkid" : 32633}
        }
      ]

如果我用空间参考代码线取消线,他会加点,但显然没有地理信息。

addFeatures REST API doesn't let you specify the incoming features spatial reference system. You can only specify the features attributes and geometry as indicated here。在将要素的几何图形发送到 API 之前,您需要将它们转换为所需的空间参考。如果您使用的是 python,则可以在发送功能之前使用 arcpy 模块轻松完成投影:

使用 arcpy 游标

如果您通过光标获取几何图形,SHAPE@ 字段将 return 投影一个 projectAs 函数的几何对象,该函数允许我们将点投影到空间参考你需要。

#A cursor to get your data. Use SHAPE@ field to get the geometry
with arcpy.da.searchCursor(table, ["SHAPE@", ...]) as cursor:

    for item in cursor:

        # Get the PointGeometry:
        geometry = item[0]

        #Create the spatial reference you want to convert to
        spatial_reference = arcpy.SpatialReference(102100)

        # Project the point to the spatial reference
        projected = geometry.projectAs(spatial_reference)

        # Get the point's projected location
        projected_x = projected.trueCentroid.X
        projected_y = projected.trueCentroid.Y

没有 arcpy 游标

如果您没有通过搜索光标获得您的功能,您需要创建一个 PointGeometry 功能,然后按照相同的步骤操作:

initialSpatialReference = arcpy.SpatialReference(102100)
point = arcpy.Point(x, y)
pointGeometry = arcpy.PointGeometry(point, initialSpatialReference)

# Now that your (x, y) is a point geometry you can project it:

#Create the spatial reference you want to convert to
spatial_reference = arcpy.SpatialReference(3857)

# Project the point to the spatial reference
projected = pointGeometry .projectAs(spatial_reference)

# Get the point's projected location
projected_x = projected.trueCentroid.X
projected_y = projected.trueCentroid.Y

然后在将几何体的 JSON 发送到 API 时使用投影的 x 和 y 值:

request = {
    features: [
        {
        attributes: {...},
        geometry: {
           x: projected_x,
           y: projected_y,
        },
    ]
}