使用传单获取 geojson 大陆/国家/州的中心 [英] Get center of geojson Continent/Country/State with leaflet

查看:36
本文介绍了使用传单获取 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 大陆/国家/州的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆