You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将rings数组转换为GeoJSON对象,使用ArcGIS Geometry类实现

解决方案

你之前操作失败的核心原因是Geometry是ArcGIS JS API中的抽象基类,不可直接实例化,你持有的rings属于多边形坐标环,需使用Polygon子类完成实例化后再转换,具体操作如下:


4.x版本API实现步骤

  1. 引入依赖模块
import Polygon from "@arcgis/core/geometry/Polygon";
import * as webMercatorUtils from "@arcgis/core/geometry/support/webMercatorUtils";
import { geojsonUtils } from "@arcgis/core/geometry/support/geojsonUtils.js";
  1. 声明rings数组
const rings = [
    [-13441425.32511117, 4491520.711015136],
    [-13440894.1920939, 4491511.685098792],
    [-13440922.71925878, 4490494.010447212],
    [-13441438.25014971, 4490494.010447212],
    [-13441425.32511117, 4491520.711015136]
];
  1. 创建多边形实例
    注意单个坐标环需要包裹一层外层数组,符合Polygon的rings参数格式要求
const polygon = new Polygon({
    rings: [rings],
    spatialReference: { wkid: 3857 }
});
  1. 转换为GeoJSON对象
    如果需要输出符合标准规范的WGS84坐标系GeoJSON,可以先做坐标转换:
// Web墨卡托转WGS84
const wgs84Polygon = webMercatorUtils.webMercatorToGeographic(polygon);
// 生成GeoJSON对象
const geoJson = geojsonUtils.fromGeometry(wgs84Polygon);

如果不需要转换坐标系,直接传入原多边形实例即可。


3.x版本API实现步骤

require([
    "esri/geometry/Polygon",
    "esri/SpatialReference",
    "esri/geometry/webMercatorUtils",
    "esri/geometry/geojsonUtils"
], function(Polygon, SpatialReference, webMercatorUtils, geojsonUtils) {
    const rings = [
        [-13441425.32511117, 4491520.711015136],
        [-13440894.1920939, 4491511.685098792],
        [-13440922.71925878, 4490494.010447212],
        [-13441438.25014971, 4490494.010447212],
        [-13441425.32511117, 4491520.711015136]
    ];
    const polygon = new Polygon(new SpatialReference(3857));
    polygon.addRing(rings);
    // 坐标转WGS84(可选)
    const wgs84Polygon = webMercatorUtils.webMercatorToGeographic(polygon);
    // 转GeoJSON
    const geoJson = geojsonUtils.toGeoJson(wgs84Polygon);
});

内容的提问来源于stack exchange,提问作者sushifl0p

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 04:06:02