2

I'm fairly new to OpenLayers so please excuse me if this is an ignorant question. I'm curious why the LinearRing getVertices implementation

getVertices: function(nodes) {
    return (nodes === true) ? [] : this.components.slice(0, this.components.length-1);
 },

does not return the last point since I was under the impression a LinearRing was supposed to represent a closed ring. I was using getVertices to serialize to another format which requires closure and found this interesting. Can someone enlighten me?

1
  • Welcome to our site! Yours actually is a great question because it's clearly stated, it goes to material that's fundamental to GIS, and it has elicited a clear, beautifully researched answer.
    – whuber
    Commented Jul 20, 2011 at 22:04

1 Answer 1

3

The Linear Ring is closed, unless you are overriding some of the classes functions with your own code.

A Linear Ring is a special LineString which is closed. It closes itself automatically on every addPoint/removePoint by adding a copy of the first point as the last point.

You're right this looks like a bug in OpenLayers. The latest full code for a LineString is correct:

getVertices: function(nodes) {
    var vertices;
    if(nodes === true) {
        vertices = [
            this.components[0],
            this.components[this.components.length-1]
        ];
    } else if (nodes === false) {
        vertices = this.components.slice(1, this.components.length-1);
    } else {
        vertices = this.components.slice();
    }
    return vertices;
}

So if nodes are requested, only the first and last point are returned.

If nodes are not wanted, only the vertices inbetween these points are returned.

Otherwise all points are returned.

But for Linear ring it's:

getVertices: function(nodes) {
    return (nodes === true) ? [] : this.components.slice(0, this.components.length-1);
},

So if code requests end nodes, then nothing is returned (which is correct, as a circle has no ends), but otherwise it returns all nodes except the last - perhaps intentionally to remove duplicates, but this goes against the definition of a linear ring.

5
  • While Javascript arrays are 0 based and I agree that there is no point at component[components.length], the Javascript array slice() method uses the end parameter to specify 'where to end the selection'. Commented Jul 20, 2011 at 16:42
  • If all the compoents were to be included the end parameter should be omitted from the call to slice. Commented Jul 20, 2011 at 16:51
  • Good point. I looked at the latest source in full - are you using an old version? If no nodes parameter is passed in you should get the full array. Commented Jul 20, 2011 at 16:54
  • Actually that's for LineString. You may want to log this as a bug in OpenLayers Commented Jul 20, 2011 at 16:59
  • Thanks @geographika. I'll go forward and submit a bug. Just wanted to get some verification before doing so. Commented Jul 20, 2011 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.