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

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

问题描述

是否可以找到由多个多边形对象组成的国家/地区(例如美国,加拿大,除主要陆地部分外还有其他岛屿)的中心?我试图使用传单的Layer.getCenter()甚至Layer.getBounds().getCenter(),但是这些方法对于这种类型的国家肯定不起作用,并返回不合格"结果.

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.

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

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,输出

推荐答案

传单只能计算要素边界框的中心.在其他库(例如 质心 的多边形或(multipolygon).

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):


(来源: postgis.net )


(source: postgis.net)

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

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

所以我想您真正想问的是:

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

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

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?

为此,答案是 polylabel :距多边形轮廓线最远的内部点".

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天全站免登陆