使用传单获取 geojson 大陆/国家/州的中心

2023-03-18前端开发问题
56

本文介绍了使用传单获取 geojson 大陆/国家/州的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有没有办法为由多个多边形对象组成的国家/大陆(例如美国、加拿大,除了主要陆地部分还有一个岛屿)找到一个中心?我试图使用传单的 Layer.getCenter() 甚至 Layer.getBounds().getCenter(),但这些方法肯定不适用于这类国家并返回no-go"结果.

可以通过以下链接(传单游乐场)查看示例.只需滚动并单击美国或加拿大,然后查看 circleMarker 将出现的位置.然后尝试点击较小的实体,例如美国南部的一些岛屿,他们会将此标记精确地放置在它们的中心:


(来源:postgis.net)

(图 1.根据 PostGIS 的 ST_Centroid 文档的多边形质心)

所以我猜你真正想问的是:

<块引用>

给定一个多边形或多多边形,我如何计算一个中心(或类似中心的点),它保证在多边形内并在视觉上看起来在它的中心,以便使用 Leaflet 将符号器添加到该点?

为此,答案是 极难访问" 由 实现polylabel:多边形轮廓最远的内部点.

Is there a way of finding a center for countries/continents which consist of more than one polygon object(ex. USA, Canada, which have an islands besides main land part)? I was trying to use leaflet's Layer.getCenter() or even Layer.getBounds().getCenter(), but those methods certainly don't work for this type of countries and return a "no-go" results.

Example can be seen by the following link (leaflet playground). Just scroll and click on US or Canada and see where circleMarker will appear. Then try clicking on smaller entities, like some islands on south of US, they shall place this marker precisely on center of'em:

http://playground-leaflet.rhcloud.com/voyi/1/edit?html,output

解决方案

Leaflet can only calculate the center of the bounding box of a feature. With a bit of help from other libraries (such as Turf.js) you can calculate the centroid of the polygon or (multipolygon).

In any case, you are getting confused because sometimes the centroid is not within the polygon (or multipolygon):


(source: postgis.net)

(Figure 1. Centroid of a polygon as per PostGIS's ST_Centroid documentation)

So I guess that what you really wanted to ask is:

Given a polygon or multipolygon, how can I calculate a center (or center-like point) which is guaranteed to be within the polygon and looks visually in the center of it, in order to add symbolizers to that point with Leaflet?

And for that, the answer is "pole of inaccessibility" as implemented by polylabel: the most distant internal point from the polygon outline.

这篇关于使用传单获取 geojson 大陆/国家/州的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5