Skip to content

Converting POLYGON to wkt generates NaN Value #160

@donCarlosOne

Description

@donCarlosOne

I've encountered this issue with a valid polygon:

let P='POLYGON (((36.293255632406975 32.40606101801413, 36.33009707717658 33.394854860641324, 35.15060719001822 33.42092621647627, 35.12678389048804 32.43116921770877, 36.293255632406975 32.40606101801413)))';

wkt.read(P).toJson().coordinates
(1) [Array(5)]
0:
(5) [Array(2), Array(2), Array(2), Array(2), Array(2)]
0:
(2) [NaN, 32.40606101801413]
1:
(2) [36.33009707717658, 33.39485486064132]
2:
(2) [35.15060719001822, 33.42092621647627]
3:
(2) [35.12678389048804, 32.43116921770877]
4:
(2) [36.293255632406975, 32.40606101801413]

The first coordinate of the first component, Array element [0][0], is being evaluated to NaN

Tracing through the code, I see that the method this._stripWhitespaceAndParens only removes the first set of parenthesis, whereas the fullStr provided to it contains double parenthesis at the beginning and the end, itself having had the first of the triple parenthesis passed in removed before it is called by the polygon method. As a result, the first coordinate still contains a parenthesis at the beginning, and when the polygon method then converts these values with parseFloat(x_coord) it returns NaN. This does not occur with a trailing parenthesis, which explains why the 5th component, Array element [0][4], is parsed correctly (even though it contains a trailing parenthesis):

parseFloat('36.5')
36.5
parseFloat('(36.5')
NaN
parseFloat('36.5)')
36.5

In my local copy I have added an additional replace to remove the second parenthesis:

var noParens = trimmed.replace(/^(?(.*?))?$/, '$1').replace(/[)(]/g,'');

and this allows it to yield a correct value

Can you please review your implementation of the regex function in this._stripWhitespaceAndParens

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions