在openlayers中有一种简单的方法来简化绘制多边形中的顶点吗?

时间:2012-12-14 19:44:01

标签: javascript maps openlayers openstreetmap

我注意到在openlayers中他们有一个内置的功能来简化LineString的顶点,但我没有看到任何多边形。是否有捷径可寻? 这是线串的示例。 http://openlayers.org/dev/examples/simplify-linestring.html

2 个答案:

答案 0 :(得分:3)

多边形由OpenLayers.Geometry.LinearRing组成,它基本上是一个关闭的特殊LineString。这意味着您可以将Polygon转换为LineString。像这样:

var originalAsLinearRing = new OpenLayers.Geometry.LinearRing(originalPolygon.components[0].components);
var originalAsLineString = new OpenLayers.Geometry.LineString(originalAsLinearRing.components); 

然后简化LineString,如示例所示,并将LineString转换回Polygon。我相信有更优雅的方式,但这也有效。

答案 1 :(得分:0)

我根据马丁的回答创造了一个小提琴: http://jsfiddle.net/gdonarum/wxnd5gom/

var linearRing = new OpenLayers.Geometry.LinearRing(original.components[0].components);
var lineString = new OpenLayers.Geometry.LineString(linearRing.components); 
var newLineString = lineString.simplify(tolerance);
var newLinearRing = new OpenLayers.Geometry.LinearRing(newLineString.getVertices());
var newPolygon = new OpenLayers.Geometry.Polygon(newLinearRing);
var reducedFeature = new OpenLayers.Feature.Vector(newPolygon);