簡體   English   中英

openlayers多邊形顯示差異而不是並集

[英]openlayers polygon displays difference not union

在OpenLayers中,我使用以下形式的代碼創建了一個由兩個LinearRing對象組成的多邊形(這在Java + GWT中,但是對JS同樣適用):

List<LinearRing> linearRingList = new ArrayList<LinearRing>();

List<Point> points1 = new ArrayList<Point>();
... populate points1 ...;
linearRingList.add(new LinearRing(points1.toArray(new Point[points.size()])));

List<Point> points2 = new ArrayList<Point>();
... populate points2 ...;
linearRingList.add(new LinearRing(points2.toArray(new Point[points.size()])));

Polygon poly = new Polygon(linearRingList.toArray(new LinearRing[linearRingList.size()]));
VectorFeature feature = new VectorFeature(poly);
myLayer.addFeature(feature);

當我在地圖上查看該圖層時,如果由points1和points2定義的多邊形重疊,則會看到一個孔:

多邊形孔http://www.jackhollow.co.uk/misc/polygon.png

我正在看到兩個多邊形的“異或”,而不是它們的“異或”,即中間的那個孔用綠色填充。

我該怎么做呢? 我確定這只是Openlayers中的顯示問題,但是我找不到在任何地方對其進行控制的標志。

原始的兩個多邊形為:

多邊形孔http://www.jackhollow.co.uk/misc/polygon2.png

如果一起顯示在單個圖層上,則它們會重疊,但是Openlayers不會填充相交的部分。

問題是您要將兩個LinearRings都添加到同一Polygon中。 在第一個之后,所有環都定義為多邊形的內環(即內部孔),這解釋了您所看到的奇怪的相交行為-試圖繪制實際上不在內部的內環。 請參閱文檔中的組件。 要解決您的問題,您需要創建兩個單獨的Geometry.Polygons和兩個單獨的Feature.Vectors並將它們都添加到Layer.Vector中。

 var linearRing1 = new OpenLayers.Geometry.LinearRing(pointList1);
 var feature1 = new OpenLayers.Feature.Vector(
          new OpenLayers.Geometry.Polygon([linearRing1]));

 var linearRing2 = new OpenLayers.Geometry.LinearRing(pointList2);
 var feature2 = new OpenLayers.Feature.Vector(
          new OpenLayers.Geometry.Polygon([linearRing2]));


var layer = new OpenLayers.Layer.Vector("layername");
layer.addFeatures([feature1, feature2]);

與以前一樣在此處創建點列表。 您還可以將屬性和樣式添加到每個要素矢量,或將樣式添加到圖層矢量,但是為了清楚起見,我將其省略。

OpenLayers中沒有顯式檢查您的幾何形狀是否正確,即內環實際上在外環內部,因此,如果將線串數組交給多邊形構造函數,則會嘗試將其轉換為多邊形適當的SVG或VML對象,結果無法預測。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM