如何将rings数组转换为GeoJSON对象,使用ArcGIS Geometry类实现
解决方案
你之前操作失败的核心原因是Geometry是ArcGIS JS API中的抽象基类,不可直接实例化,你持有的rings属于多边形坐标环,需使用Polygon子类完成实例化后再转换,具体操作如下:
4.x版本API实现步骤
- 引入依赖模块
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";
- 声明rings数组
const rings = [ [-13441425.32511117, 4491520.711015136], [-13440894.1920939, 4491511.685098792], [-13440922.71925878, 4490494.010447212], [-13441438.25014971, 4490494.010447212], [-13441425.32511117, 4491520.711015136] ];
- 创建多边形实例
注意单个坐标环需要包裹一层外层数组,符合Polygon的rings参数格式要求
const polygon = new Polygon({ rings: [rings], spatialReference: { wkid: 3857 } });
- 转换为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
相关产品推荐
相关产品推荐

