1. 引言

空间运算利用几何函数来接收输入的空间数据,对其进行分析,然后生成输出数据,输出数据为针对输入数据执行分析的派生结果。

可从空间运算中获得的派生数据包括:

  • 作为输入要素周围缓冲区的面
  • 作为对几何集合执行分析的结果的单个要素
  • 作为比较结果以确定不与其他要素位于同一物理空间的要素部分的单个要素
  • 作为比较结果以查找与其他要素的物理空间相交的要素部分的单个要素
  • 由彼此不位于同一物理空间的输入要素部分组成的多部分 (multipart) 要素
  • 作为两个几何的并集的要素

参考文档:空间运算—ArcMap | 文档 (arcgis.com)

Turf.js是MapBox公司研发的基于浏览器端的空间分析库,它使用JavaScript进行编写,通过npm进行包管理。值得一提的是,良好的模块化设计使其不仅能够作用于浏览器端、还可通过Node.js在服务端使用。Turf 原生支持 GeoJSON 矢量数据。GeoJSON 的优点是结构简单,并且得到了所有网页地图API的支持;但 GeoJSON 不支持空间索引,这个缺点可能会限制 Turf 处理大型文件的能力效率。其适用于轻量级(数据轻量而非功能轻量)的WebGIS应用

参考文献:Turf.js—让你在浏览器上实现地理分析 - 掘金 (juejin.cn)

turf.js官网:Turf.js | Advanced Geospatial Analysis (turfjs.org)

中文站点:Turf.js中文网 (fenxianglu.cn)

Turf.js Github地址:Turfjs/turf: A modular geospatial engine written in JavaScript (github.com)

空间运算可谓是空间分析的基础,Turf.js提供了大量的空间分析功能,包含了空间运算功能,本文参考OpenLayers与Turf集成的官方示例,使用示例数据和原生JavaScript,进行求交运算与缓冲区运算,并进行可视化

OpenLayers与Turf集成示例:turf.js (openlayers.org)

2.初始化地图

引入OpenLayers和Turf.js的CDN:

<link rel="stylesheet"  href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css"  type="text/css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>

注意:

  • OpenLayers与Turf集成示例使用的Turf版本是2.0.0,版本偏老,与目前版本(6.5.0)部分API不兼容,不建议使用

构建网页基本内容,添加地图容器:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <link rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<style>
html,
body,
#map {
height: 100%;
}
</style>
</head> <body>
<div id="map"></div> </body> </html>

添加地图并加载示例矢量数据:

<script>
fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
.then(response => response.json())
.then(function (json){
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(json, {
featureProjection: 'EPSG:3857'
})
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([126.980366, 37.52654]),
zoom: 15
})
});
})
</script>

完整代码如下:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <link rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<style>
html,
body,
#map {
height: 100%;
}
</style>
</head> <body>
<div id="map"></div>
<script>
fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
.then(response => response.json())
.then(function (json){
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(json, {
featureProjection: 'EPSG:3857'
})
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([126.980366, 37.52654]),
zoom: 15
})
});
})
</script> </body> </html>

初始化的地图:

3. 缓冲区计算

参考官方文档:Turf.js | Advanced geospatial analysis (turfjs.org)

步骤实质就是:

  • 将Openlayers的Features对象转换为Turf支持的GeoJSON对象
  • 调用Turf.buff()函数得到运算结果
  • OpenLayers读取运算后的GeoJSON数据加载到图层中

缓存区计算的核心代码如下:

const features = vectorSource.getFeatures();
const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
featureProjection: 'EPSG:3857'
});
var buffered = turf.buffer(turfLines, 25, { units: 'meters' });
const bufferedLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(buffered, {
featureProjection: 'EPSG:3857'
})
})
})
map.addLayer(bufferedLayer);

注意:

  • 从OpenLayers的Feature对象转换为Turf支持的GeoJSON对象时务必指定坐标系

这一步的完成代码如下:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <link rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<!-- <script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script> -->
<style>
html,
body,
#map {
height: 100%;
}
</style>
</head> <body>
<div id="map"></div> <script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}) ],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([126.980366, 37.52654]),
zoom: 15
})
});
fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
.then(response => response.json())
.then(function (json) {
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(json, {
featureProjection: 'EPSG:3857'
})
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#0000ff',
width: 2
})
})
});
map.addLayer(vectorLayer); const features = vectorSource.getFeatures();
const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
featureProjection: 'EPSG:3857'
});
var buffered = turf.buffer(turfLines, 25, { units: 'meters' });
const bufferedLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(buffered, {
featureProjection: 'EPSG:3857'
})
})
})
map.addLayer(bufferedLayer);
})
</script>
</body> </html>

结果图如下:

4. 相交计算

参考官方文档:Turf.js | Advanced geospatial analysis (turfjs.org)

步骤与缓冲区计算类似:

  • 将Openlayers的Features对象转换为Turf支持的GeoJSON对象
  • 调用Turf.lineIntersect()函数得到运算结果
  • OpenLayers读取运算后的GeoJSON数据加载到图层中

本文所使用的数据是LineString,所以此处的相交计算的是线段相交

相交运算的核心代码如下:

const features = vectorSource.getFeatures();
const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
featureProjection: 'EPSG:3857'
}); const intersectionSource = new ol.source.Vector()
for (let i = 0; i < turfLines.features.length; i++) {
for (let j = i + 1; j < turfLines.features.length; j++) {
const intersections = turf.lineIntersect(turfLines.features[i], turfLines.features[j]);
if (intersections) {
if (intersections.features.length > 0) {
intersectionSource.addFeatures(new ol.format.GeoJSON().readFeatures(intersections, {
featureProjection: 'EPSG:3857'
}))
}
}
}
} const intersectionLayer = new ol.layer.Vector({
source: intersectionSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 2
}),
fill: new ol.style.Fill({
color: '#ff0000'
}),
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: '#ff0000'
})
})
})
})
map.addLayer(intersectionLayer);

注意:

  • 计算结果为点集合(Point),最好设置一定的显示样式,不然难以看见

这一步的完成代码如下:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <link rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<!-- <script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script> -->
<style>
html,
body,
#map {
height: 100%;
}
</style>
</head> <body>
<div id="map"></div> <script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}) ],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([126.980366, 37.52654]),
zoom: 15
})
});
fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
.then(response => response.json())
.then(function (json) {
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(json, {
featureProjection: 'EPSG:3857'
})
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#0000ff',
width: 2
})
})
});
map.addLayer(vectorLayer); const features = vectorSource.getFeatures();
const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
featureProjection: 'EPSG:3857'
}); const intersectionSource = new ol.source.Vector()
for (let i = 0; i < turfLines.features.length; i++) {
for (let j = i + 1; j < turfLines.features.length; j++) {
const intersections = turf.lineIntersect(turfLines.features[i], turfLines.features[j]);
if (intersections) {
// console.log(intersections)
if (intersections.features.length > 0) {
intersectionSource.addFeatures(new ol.format.GeoJSON().readFeatures(intersections, {
featureProjection: 'EPSG:3857'
}))
// console.log(intersectionSource.getFeatures())
}
}
}
}
// console.log(intersectionSource)
const intersectionLayer = new ol.layer.Vector({
source: intersectionSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 2
}),
fill: new ol.style.Fill({
color: '#ff0000'
}),
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: '#ff0000'
})
})
})
})
map.addLayer(intersectionLayer);
})
</script>
</body> </html>

结果图如下:

5. 整合

将两个运算结果结合在一起,完整代码如下:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <link rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<!-- <script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script> -->
<style>
html,
body,
#map {
height: 100%;
}
</style>
</head> <body>
<div id="map"></div> <script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}) ],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([126.980366, 37.52654]),
zoom: 15
})
});
fetch('https://openlayers.org/en/latest/examples/data/geojson/roads-seoul.geojson')
.then(response => response.json())
.then(function (json) {
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(json, {
featureProjection: 'EPSG:3857'
})
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#0000ff',
width: 2
})
})
});
map.addLayer(vectorLayer); const features = vectorSource.getFeatures();
const turfLines = (new ol.format.GeoJSON()).writeFeaturesObject(features, {
featureProjection: 'EPSG:3857'
});
// console.log(turfLines)
// const buffered = turf.buffer(turfLines, 25);
var buffered = turf.buffer(turfLines, 25, { units: 'meters' });
const bufferedLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(buffered, {
featureProjection: 'EPSG:3857'
})
})
})
map.addLayer(bufferedLayer); const intersectionSource = new ol.source.Vector()
for (let i = 0; i < turfLines.features.length; i++) {
for (let j = i + 1; j < turfLines.features.length; j++) {
const intersections = turf.lineIntersect(turfLines.features[i], turfLines.features[j]);
if (intersections) {
// console.log(intersections)
if (intersections.features.length > 0) {
intersectionSource.addFeatures(new ol.format.GeoJSON().readFeatures(intersections, {
featureProjection: 'EPSG:3857'
}))
// console.log(intersectionSource.getFeatures())
}
}
}
}
// console.log(intersectionSource)
const intersectionLayer = new ol.layer.Vector({
source: intersectionSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 2
}),
fill: new ol.style.Fill({
color: '#ff0000'
}),
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: '#ff0000'
})
})
})
})
map.addLayer(intersectionLayer);
})
</script>
</body> </html>

结果图如下:

6. 参考资料

[1]空间运算—ArcMap | 文档 (arcgis.com)

[2]Turf.js—让你在浏览器上实现地理分析 - 掘金 (juejin.cn)

[3]Turf.js | Advanced Geospatial Analysis (turfjs.org)

[4]Turf.js中文网 (fenxianglu.cn)

[5]Turfjs/turf: A modular geospatial engine written in JavaScript (github.com)

[6]使用 Fetch - Web API 接口参考 | MDN (mozilla.org)

[7]turf.js (openlayers.org)

[8]openlayers+turf.js实现缓冲区的绘制_gis_SSS的博客-CSDN博客_openlayers缓冲区分析

[9]OpenLayers v6.14.1 API - Class: EsriJSON

OpenLayers结合Turf实现空间运算的更多相关文章

  1. (转)R空间数据处理与可视化

    前言 很多朋友说在R里没法使用高德地图,这里给出一个基于leaflet包的解决方法. library(leaflet) # 添加高德地图 m <- leaflet() %>% addTil ...

  2. MySQL中的GIS几何函数和空间分析函数

    MySQL空间扩展不仅提供了空间数据的存储能力,而且还具备一些空间运算能力,这些功能通过MySQL内建的几何函数实现.最简单的几何函数昨天已经有所涉及,也就是转换WTK的GEOMFROMTEXT和AS ...

  3. OpenGIS 介绍

    转自:http://www.blogjava.net/sinoly/archive/2007/09/25/148002.html 值此FOSS4G大会即将召开之日,最近我会在Blog上依次介绍一些Op ...

  4. GIS应用及OpenGIS介绍

    转自:http://blog.csdn.net/cdl2008sky/article/details/7266680 GIS的三大应用第一类是政府应用,“电子政务” 是当今政府加强信息化建设的新方向, ...

  5. OpenGIS 介绍(转)

    值此FOSS4G大会即将召开之日,最近我会在Blog上依次介绍一些OpenGIS标准.架构及用于实现的软件.一方面给初涉此行的朋友一个快速入门的概览,另一方面也是对我接触OpenGIS近一年来的总结. ...

  6. Gamma校正与线性空间

    基础知识部分 为了方便理解,首先会对(Luminance)的相关概念做一个简单介绍.如果已经了解就跳到后面吧. 我们用Radiant energy(辐射能量)来描述光照的能量,单位是焦耳(J),因为光 ...

  7. 转自一个CG大神的文章

    <如何学好游戏3D引擎编程>此篇文章献给那些为了游戏编程不怕困难的热血青年,它的神秘要我永远不间断的去挑战自我,超越自我,这样才能攀登到游戏技术的最高峰           ——阿哲VS自 ...

  8. 转载:[转]如何学好3D游戏引擎编程

      [转]如何学好3D游戏引擎编程 Albert 本帖被 gamengines 从 游戏引擎(Game Engine) 此文为转载,但是值得一看. 此篇文章献给那些为了游戏编程不怕困难的热血青年,它的 ...

  9. PRML读书会第七章 Sparse Kernel Machines(支持向量机, support vector machine ,KKT条件,RVM)

    主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:59:22  大家好,今天一起交流下PRML第7章.第六章核函数里提到,有一类机器学习算法,不是对参数做点估计或求其分 ...

  10. ArcGIS 帮助(10.2、10.2.1 和 10.2.2)收集

    帮助首页 [Oracle基础] 快速浏览:Oracle 地理数据库 什么是 Oracle Spatial? 设置到 Oracle 的连接 存储在 Oracle 地理数据库中的系统表 结合企业级地理数据 ...

随机推荐

  1. 18V降压3.3V,15V降压3.3V的降压IC和LDO芯片方案

    在 18V 和 15V 输入中,我们需要给其他电源电路提高供电,有的电路的供电电压在 5V,或者是 3.3V 时, 我们就需要使用降压芯片来组建一个降压电路来给后面的的电路,提供稳定的,持续的 3.3 ...

  2. python安装包出现的两个问题error: Unable to find vcvarsall.bat、提示No module named Crypto.Cipher

    python安装包出现的两个问题 error: Unable to find vcvarsall.bat No module named Crypto.Cipher error: Unable to ...

  3. .NET 6 中外部引用项目NU1105异常问题解决

    .NET 6 Project中,添加了其他解决方案的工程后,本地能编译通过,代码签入后,其他同事下载代码,编译报错: 错误 NU1105 找不到"E:\Teld\01Code\TTP_CTP ...

  4. MongoDB安全加固,防止数据库攻击删除勒索威胁

    前言: 今天发现前段时间自己搭建的一个系统的MongoDB数据找不到了,觉得很奇妙,然后登上MongoDB数据库发现多了一个名为READ__ME_TO_RECOVER_YOUR_DATA的数据库,里面 ...

  5. MySQL约束条件(主键-自增-默认值)

    目录 一:MySQL约束条件 1.什么是约束条件? 二:unsigned(去除正负号) 三:zerofill(不够位数零填充) 四:not null(非空) 1.使用约束条件(不添加会报错) 五:de ...

  6. JavaScript:变量:如何声明变量?

    声明变量可以用下面几种方式: 但是这几种声明方式肯定是有区别的,主要是上面三种方式的区别,这需要结合window对象和作用域来说明,这里不赘述. 声明变量的时候,推荐使用let,这是ES6新推出的更好 ...

  7. 从一道CTF题学习python字节码到源码逆向

    概述: 该题来源为2022爱春秋冬季赛ezpython,难度不是很大刚好适合我这样的萌新入门 题目: 3 0 LOAD_CONST 1 (204) 3 LOAD_CONST 2 (141) 6 LOA ...

  8. 「Goravel 上新」验证表单的三种新姿势,估计你只用过一种

    验证用户输入的数据是我们开发中最常见的需求,Goravel 提供三种验证姿势,个个简单好用! 第一种:简单直接式 根据表单内容直接校验: func (r *PostController) Store( ...

  9. Hadoop详解(04)-Hdfs

    Hadoop详解(04)-Hdfs HDFS概述 HDFS产出背景及定义 背景:随着数据量越来越大,在一个操作系统存不下所有的数据,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,迫切需 ...

  10. MongoDB分片副本集生产环境部署-Windows版本

    title: MongoDB分片副本集生产环境部署(Windows版本) date: 2022-10-29 17:21:11 tags: - 运维 系统架构 配置环境 系统都是windows 10 专 ...