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

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.kler.cn/a/568163.html

相关文章:

  • 详细对比所有开源许可及其不同版本
  • c++ std::array使用笔记
  • 「Selenium+Python自动化从0到1①|2025最新环境搭建+浏览器驱动避坑指南(附验证代码)」
  • JSON(JavaScript Object Notation)
  • 【Java 基础(人话版)】Java SE vs Java EE
  • 第十五站:循环神经网络(RNN)与长短期记忆网络(LSTM)
  • redis的启动方式
  • Linux——计算机网络
  • 【SDR课堂第12讲】AD9361毛刺问题总结
  • 手写RPC框架-V1版本
  • 一周学会Flask3 Python Web开发-Jinja2模版中加载静态文件
  • 2.9作业
  • 大模型最新面试题系列:训练篇之数据处理与增强
  • Python可视化大框架的研究与应用
  • 聊聊大数据测试开展方向有哪些?
  • Protobuf原理与序列化
  • Android中的四大组件及其生命周期
  • 学习笔记-单片机蓝桥杯大模板更新-米醋
  • uniapp h5页面获取跳转传参的简单方法
  • 设置电脑一接通电源就主动开机