/*****
*
* WKTParser library
*
* $Id: WKTParserLib.js 94 2010-11-18 18:23:07Z klaus $
*
* Parsing algorithm in WKTParser adapted from WKT.js within openlayers.org project
* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the full text of the license.
*
*****/

var WKTParser = function() {
    this.regExes = {
        'typeStr': /^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/,
        'spaces': /\s+/,
        'parenComma': /\)\s*,\s*\(/,
        'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/,  // can't use {2} here
        'trimParens': /^\s*\(+(.*?)\)+\s*$/ /* original does not work for WebKit /^\s*\(?(.*?)\)?\s*$/ */
    }
    this.wkt_type = null

    // flip y-coordinate
    this.yDown = function(coords) {
        if (coords.length > 0) {
            for (var i=1; i<coords.length; i+=2) {
                coords[i] *= -1
            }
        }
        return coords
    }
}
WKTParser.prototype.parse = function(wkt) {
    var ftype, str, markup
    var matches = this.regExes.typeStr.exec(wkt)
    if(matches) {
        ftype = matches[1].toLowerCase()
        str = matches[2]
        if(this[ftype]) {
            if (ftype == 'point' || ftype == 'multipoint') {
                asPoint = true
                markup = this[ftype](str,asPoint)
            }
            else {
                markup = this[ftype](str)
            }
            this.wkt_type = ftype
            return markup
        }
    }
}
WKTParser.prototype.getType = function() {
    // returns type of last parsed geometry
    return this.wkt_type
}
WKTParser.prototype.point = function(str,asPoint) {
    var coords = this.yDown(str.split(this.regExes.spaces))
    if (asPoint) {
        return { x : coords[0], y : coords[1] }
    }
    else {
        return coords
    }
}
WKTParser.prototype.multipoint = function(str,asPoint) {
    var points = str.split(',')
    var components = []
    for(var i=0; i<points.length; ++i) {
        components[components.length] = this.point(points[i],asPoint)
    }
    return components
}
WKTParser.prototype.linestring = function(str,asPoly) {
    var points = str.split(',')
    var components = []
    for(var i=0; i<points.length; ++i) {
        if (asPoly && i==points.length-1) {
            components[components.length] = "z"
            break
        }
        else {
            components[components.length] = this.point(points[i]).join(" ")
        }
    }
    return "M"+components.join(" ")
}
WKTParser.prototype.multilinestring = function(str) {
    var line
    var lines = str.split(this.regExes.parenComma)
    var components = []
    for(var i=0; i<lines.length; ++i) {
        line = lines[i].replace(this.regExes.trimParens, '$1')
        components[components.length] = this.linestring(line)
    }
    return components.join(" ")
}
WKTParser.prototype.polygon = function(str) {
    var asPoly = true
    var ring, linestring, linearring
    var rings = str.split(this.regExes.parenComma)
    var components = []
    for(var i=0; i<rings.length; ++i) {
        ring = rings[i].replace(this.regExes.trimParens, '$1')
        linestring = this.linestring(ring,asPoly)
        components[components.length] = linestring
    }
    return components.join(" ")
}
WKTParser.prototype.multipolygon = function(str) {
    var polygon
    var polygons = str.split(this.regExes.doubleParenComma)
    var components = []
    for(var i=0; i<polygons.length; ++i) {
        polygon = polygons[i].replace(this.regExes.trimParens, '$1')
        components[components.length] = this.polygon(polygon)
    }
    return components
}

