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

Get center of geojson Continent/Country/State with leaflet(使用传单获取 geojson 大陆/国家/州的中心)
本文介绍了使用传单获取 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 大陆/国家/州的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)