問題描述
有沒有辦法為由多個多邊形對象組成的國家/大陸(例如美國、加拿大,除了主要陸地部分還有一個島嶼)找到一個中心?我試圖使用傳單的 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 大陸/國家/州的中心的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!