Rahul
Rahul

Reputation: 1

Openlayers simplify linestring features on the fly

I have an issue on my OpenLayers openstreetmaps where I am loading LineString features from a kml file. Everything works fine except performance due to the sheer complexity of the LineString and the number of linestrings. I wanted to use the simplyfy() function to simplify the linestring geometry/feature. Here is some code I wrote to simplify on the fly. The problem is in the line of code below:

            feature.geometry.components[i].simplify(0.1);

This does not seem to modify the original geometry feature components at all. What am I doing wrong? I think we might need to use removeComponents and then add the simplified geometries using addComponents() but how to do this?

preFeatureInsert: function(feature)

{
    if (feature != "undefined" && feature.geometry != "undefined" && feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Collection");
    {
        if (feature.geometry.components != "undefined" && typeof(feature.geometry.components) != "undefined")
        {
            for (var i = 0; i < feature.geometry.components.length; i++)
            {
                if (feature.geometry.components[i].CLASS_NAME ==  "OpenLayers.Geometry.LineString")
                {
                    feature.geometry.components[i].simplify(0.1);
                }
            }
        }                           
    }
} 

Upvotes: 0

Views: 722

Answers (1)

Alex
Alex

Reputation: 483

The simplify function returns a simplified version of the component and does not modify the component itself. Within your loop you can set the component as needed:

feature.geometry.components[i] = feature.geometry.components[i].simplify(0.1);

Upvotes: 1

Related Questions