C#使用GeoJSON点集合创建类型为ArcGIS Geometry的外接正方形

直接上代码:

        /// <summary>传入一系列的GeoJSON格式的点集,转换得到外包正方形
        /// 
        /// </summary>
        /// <param name="listPoint">GeoJSON格式的点集</param>
        /// <param name="b">可选参数,缓冲距离</param>
        /// <returns></returns>
        public static ESRI.ArcGIS.Geometry.IPolygon CreateEsriPolygon(List<GeoJSON.Net.Geometry.Point> listPoint, double? b = 0.0)
        {
            if (0 >= listPoint.Count)
                return null;

            var firstPoint = listPoint[0].Coordinates;
            double dMinX = firstPoint.X; double dMinY = firstPoint.Y; double dMaxX = firstPoint.X; double dMaxY = firstPoint.Y; double dSumX = 0.00; double dSumY = 0.00;
            foreach (var aPt in listPoint)
            {
                dMinX = aPt.Coordinates.X <= dMinX ? aPt.Coordinates.X : dMinX;
                dMinY = aPt.Coordinates.Y <= dMinY ? aPt.Coordinates.Y : dMinY;
                dMaxX = aPt.Coordinates.X >= dMaxX ? aPt.Coordinates.X : dMaxX;
                dMaxY = aPt.Coordinates.Y >= dMaxY ? aPt.Coordinates.Y : dMaxY;
                dSumX += aPt.Coordinates.X;
                dSumY += aPt.Coordinates.Y;
            }
            double dCenX = dSumX * 100 / listPoint.Count * 0.01;  //正方形中心点X坐标
            double dCenY = dSumY * 100 / listPoint.Count * 0.01;  //正方形中心点Y坐标
            double dHalfLength = (((dMaxX - dMinX) > (dMaxY - dMinY)) ? (dMaxX - dMinX) : (dMaxY - dMinY)) * 0.5;
            dHalfLength += (b.HasValue && (double)b > 0) ? (double)b : 0;  //正方形边长的一半

            ESRI.ArcGIS.Geometry.IPointCollection pPointCol = new ESRI.ArcGIS.Geometry.Polygon();
            pPointCol.AddPoint(CreatePoint(dCenX - dHalfLength, dCenY + dHalfLength));  //左上角点
            pPointCol.AddPoint(CreatePoint(dCenX + dHalfLength, dCenY + dHalfLength));  //右上角点
            pPointCol.AddPoint(CreatePoint(dCenX + dHalfLength, dCenY - dHalfLength));  //右下角点
            pPointCol.AddPoint(CreatePoint(dCenX - dHalfLength, dCenY - dHalfLength));   //左下角点
            pPointCol.AddPoint(CreatePoint(dCenX - dHalfLength, dCenY + dHalfLength));  //左上角点(用于闭合线段)

            return pPointCol as ESRI.ArcGIS.Geometry.IPolygon;
        }


<think> 我们使用ArcGIS API for JavaScript来实现。主要步骤包括: 1. 加载GeoJSON数据 2. 将GeoJSON转换为ArcGIS可用的图形(Graphic)或图层(Layer) 3. 将图形添加到地图上并定位到该区域 具体步骤: a. 引入ArcGIS API for JavaScript b. 创建地图和视图 c. 加载GeoJSON数据(可能通过文件或URL) d. 使用GeoJSON转换工具将GeoJSON转换为ArcGIS的图形 e. 将图形添加到地图的图形图层中 f. 调整视图以定位到该图形 注意:从4.x版本开始,ArcGIS API提供了`esri/Graphic`和`esri/layers/GeoJSONLayer`等类来处理GeoJSON。 有两种主要方式: 方式1:使用GeoJSONLayer(适用于大量数据,作为图层添加) 方式2:使用Graphic(适用于单个或少量几何图形) 如果GeoJSON表示的是单个点、线、面,我们可以使用`geometryUtils`来转换几何,然后创建Graphic并添加到视图的graphics中。 如果GeoJSON是FeatureCollection,则更适合用GeoJSONLayer。 这里我们以FeatureCollection为例,使用GeoJSONLayer。 示例代码: ```javascript require([ "esri/Map", "esri/views/MapView", "esri/layers/GeoJSONLayer" ], function(Map, MapView, GeoJSONLayer) { // 创建地图 const map = new Map({ basemap: "streets-vector" }); // 创建视图 const view = new MapView({ container: "viewDiv", // 容器id map: map, center: [0, 0], // 初始中心 zoom: 2 }); // 创建GeoJSONLayer const geojsonLayer = new GeoJSONLayer({ url: "path/to/your/data.geojson" // 替换为你的GeoJSON文件路径或URL // 或者使用source属性直接传入GeoJSON对象 // source: geojsonObject }); // 将图层添加到地图 map.add(geojsonLayer); // 当图层加载后,可以定位到该图层 geojsonLayer.when(function() { view.goTo(geojsonLayer.fullExtent); // 定位到图层范围 }).otherwise(function(error) { console.error("图层加载失败: ", error); }); }); ``` 如果是单个几何图形,例如一个点,我们可以这样: ```javascript require([ "esri/Map", "esri/views/MapView", "esri/Graphic", "esri/geometry/geometryEngine", "esri/geometry/support/jsonUtils" ], function(Map, MapView, Graphic, geometryEngine, geometryJsonUtils) { // 创建地图和视图(同上) // 假设我们有一个GeoJSON点 const geojsonPoint = { "type": "Point", "coordinates": [120, 30] // [经度, 纬度] }; // 将GeoJSON几何转换为ArcGIS的几何 const pointGeometry = geometryJsonUtils.fromJSON(geojsonPoint); // 创建图形 const pointGraphic = new Graphic({ geometry: pointGeometry, symbol: { ... } // 定义符号 }); // 将图形添加到视图的图形集合中 view.graphics.add(pointGraphic); // 定位到该点 view.goTo(pointGraphic); }); ``` 注意:对于复杂的GeoJSON,如FeatureCollection,建议使用GeoJSONLayer,因为它支持更多特性(如样式、弹出窗口等)。 另外,如果GeoJSON数据是动态获取的(例如通过API),则可以使用`source`属性传入一个Promise。 例如: ```javascript const geojsonLayer = new GeoJSONLayer({ source: fetch("url").then(response => response.json()) }); ``` 这样,当数据获取完成后,图层会自动加载。 总结: - 使用GeoJSONLayer加载FeatureCollection类型GeoJSON,并利用其内置的解析和渲染能力。 - 对于单个几何图形,可以手动转换为Graphic并添加。 请根据实际需求选择合适的方式。 §§相关问题§§ 1. 如何为GeoJSON图层设置样式? 2. 如何在点击GeoJSON要素时显示弹出窗口? 3. 如何处理大型GeoJSON文件的性能问题? 4. 如何将其他格式的地理数据(如Shapefile)转换为GeoJSON供前端使用? </think> ### 使用 ArcGIS API for JavaScript 实现 GeoJSON 定位功能 #### 核心步骤: 1. **初始化地图** ```javascript require(["esri/Map", "esri/views/MapView"], (Map, MapView) => { const map = new Map({ basemap: "streets-vector" }); const view = new MapView({ container: "viewDiv", // HTML容器ID map: map, center: [116.4, 39.9], // 初始中心点 zoom: 10 }); }); ``` 2. **加载 GeoJSON 数据** ```javascript const geojson = { "type": "FeatureCollection", "features": [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [116.4, 39.9] // 北京坐标 } }] }; ``` 3. **转换 GeoJSON 为图形** ```javascript require(["esri/Graphic"], (Graphic) => { const graphic = new Graphic({ geometry: { type: "point", longitude: geojson.features[0].geometry.coordinates[0], latitude: geojson.features[0].geometry.coordinates[1] }, symbol: { type: "simple-marker", color: "red", size: "12px" } }); view.graphics.add(graphic); }); ``` 4. **定位到目标位置** ```javascript view.goTo({ target: graphic.geometry, zoom: 15 // 缩放级别 }); ``` #### 完整代码示例 ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>GeoJSON定位演示</title> <link rel="stylesheet" href="https://js.arcgis.com/4.25/esri/themes/light/main.css"> <script src="https://js.arcgis.com/4.25/"></script> <style> #viewDiv { height: 100vh; width: 100%; } </style> </head> <body> <div id="viewDiv"></div> <script> require(["esri/Map", "esri/views/MapView", "esri/Graphic"], (Map, MapView, Graphic) => { // 1. 初始化地图 const map = new Map({ basemap: "streets-vector" }); const view = new MapView({ container: "viewDiv", map: map, center: [116.4, 39.9], zoom: 5 }); // 2. GeoJSON数据(以点数据为例) const geojson = { type: "FeatureCollection", features: [{ type: "Feature", geometry: { type: "Point", coordinates: [120.15, 30.28] // 杭州坐标 } }] }; // 3. 渲染GeoJSON view.when(() => { const feature = geojson.features[0]; const graphic = new Graphic({ geometry: { type: "point", longitude: feature.geometry.coordinates[0], latitude: feature.geometry.coordinates[1] }, symbol: { type: "simple-marker", color: "blue", size: "16px" }, popupTemplate: { title: "定位点", content: "经度: {longitude}, 纬度: {latitude}" } }); view.graphics.add(graphic); // 4. 定位到目标 view.goTo({ center: [feature.geometry.coordinates[0], feature.geometry.coordinates[1]], zoom: 12 }); }); }); </script> </body> </html> ``` #### 关键处理技巧: 1. **复杂几何类型处理** 对于多边形/线数据,使用 `esri/geometry/geometryEngine` 转换GeoJSON坐标: ```javascript const polygon = { type: "polygon", rings: geojson.features[0].geometry.coordinates }; ``` 2. **批量要素加载** 使用 `GeoJSONLayer` 高效加载大量数据: ```javascript const layer = new GeoJSONLayer({ url: "data.geojson", // GeoJSON文件URL popupTemplate: { /* 弹窗配置 */ } }); map.add(layer); ``` 3. **动态定位** 结合地图事件实现交互式定位: ```javascript document.getElementById("locate-btn").addEventListener("click", () => { view.goTo(graphic.geometry); }); ``` #### 注意事项: - 坐标系需统一(建议使用WGS84,EPSG:4326) - 大型GeoJSON文件需分块加载或使用服务端渲染 - 移动端需添加定位精度提示[^1] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值