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

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

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)