当前位置: 首页 > news >正文

openlayers结合turf geojson面获取面积和中心点

在 OpenLayers 中绘制 GeoJSON 面要素并计算面积和中心点,可以结合 OpenLayers 的 ol/format/GeoJSON 模块将 GeoJSON 数据转换为 OpenLayers 的 Feature,然后使用 Turf.js 进行计算。示例代码如下

import Map from 'ol/Map';
import View from 'ol/View';
import { Tile as TileLayer } from 'ol/layer';
import OSM from 'ol/source/OSM';
import { Vector as VectorLayer } from 'ol/layer';
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';
import * as turf from '@turf/turf';// 创建地图
const map = new Map({target: 'map',layers: [new TileLayer({ source: new OSM() })],view: new View({center: [0, 0],zoom: 2})
});// 创建一个 GeoJSON 面要素
const geojson = {type: 'Feature',geometry: {type: 'Polygon',coordinates: [[[116.39, 39.91],[116.41, 39.91],[116.41, 39.93],[116.39, 39.93],[116.39, 39.91]]]}
};// 将 GeoJSON 转换为 OpenLayers Feature
const vectorSource = new VectorSource({features: new GeoJSON().readFeatures(geojson, {featureProjection: 'EPSG:4326'})
});const vectorLayer = new VectorLayer({ source: vectorSource });
map.addLayer(vectorLayer);// 使用 Turf.js 计算面积和中心点
const polygon = turf.polygon(geojson.geometry.coordinates);
const area = turf.area(polygon);
const center = turf.center(polygon);console.log('面积:', area.toFixed(2) + ' 平方米');
console.log('中心点:', center.geometry.coordinates);

http://www.lryc.cn/news/543254.html

相关文章:

  • 【SRC实战】修改金币数量实现财富自由
  • 地理数据可视化:飞线说明(笔记)
  • 2024最新版鸿蒙纯血原生应用开发教程文档丨学习ArkTS语言-基本语法
  • 微信小程序-二维码绘制
  • 轻量化网络设计|ShuffleNet:深度学习中的轻量化革命
  • 一天记20个忘10个之五:land
  • Python 类(创建和使用类)
  • LeetCode 解题思路 3(Hot 100)
  • 算法-二叉树篇11-左叶子之和
  • MaxKB上架至阿里云轻量应用服务器镜像市场
  • 用户态和内核态是什么?
  • 2025年SCI一区智能优化算法:混沌进化优化算法(Chaotic Evolution Optimization, CEO),提供MATLAB代码
  • 普中单片机-51TFT-LCD显示屏(1.8寸 STM32)
  • SGMII(Serial Gigabit Media Independent Interface)详解
  • DeepSeek:我的AI助手之旅
  • 图片批量去重---(均值哈希、插值哈希、感知哈希、三/单通道直方图)
  • Linux:(3)
  • vscode设置自动换行
  • Instagram 隐私设置全面解析:如何保护你的个人数据?
  • Activiti 5 + Spring Boot全流程开发指南
  • spring结合mybatis多租户实现单库分表
  • 面向对象编程(OOP)基础:Java入门指南
  • day7作业
  • 图像处理之图像边缘检测算法
  • 第二十五 :搭建 pinia 环境
  • 学习Java数组操作:从基础到高级技巧详解
  • 算法题(79):两个数组的交集
  • TFChat:腾讯大模型知识引擎+飞书机器人实现AI智能助手
  • Linux红帽:RHCSA认证知识讲解(四)修改远程配置文件,取消root禁用,便于使用root身份远程
  • 验证码介绍及生成与验证(HTML + JavaScript实现)