Seth
Seth

Reputation: 363

In openlayers is there an easy way to simplify the vertices in a drawn polygon?

I noticed in openlayers they have a built in feature to simplify the vertices for LineString but I do not see anything for polygons. Is there an easy way to do this? Here is the example for the linestring. http://openlayers.org/dev/examples/simplify-linestring.html

Upvotes: 0

Views: 2283

Answers (2)

gdonarum
gdonarum

Reputation: 11

I created a fiddle based on Martin's answer: 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);

Upvotes: 0

Martin
Martin

Reputation: 896

A polygon consists of an OpenLayers.Geometry.LinearRing which is basically a special LineString that is closed. This means you could just convert your Polygon into a LineString. Like this:

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

Then simplify the LineString as shown in the example and convert the LineString back into a Polygon. I am sure there is a more elegant way, but this works as well.

Upvotes: 3

Related Questions