arthur-e / Wicket

A modest library for moving between Well-Known Text (WKT) and various framework geometries
https://arthur-e.github.io/Wicket/
Other
586 stars 226 forks source link

Reading Polygon from flat array returns Point without throwing error #145

Open Portur opened 4 years ago

Portur commented 4 years ago

A polygon is provided as an array of points, which is in geojson format. wicket.wrtie() incorrectly returns a POINT( ... ) rather than a POLYGON ( ... )

let coords = [
[ 28.324979200606098,-25.74442504898913 ]
[ 28.327136778694015,-25.73978746165716 ]
[ 28.323643915439302,-25.7358147570423 ]
[ 28.317993541281737,-25.736479619044854 ]
[ 28.315835810504456,-25.74111718371399 ]
[ 28.3193286065439,-25.745089909049103 ]
[ 28.324979200606098,-25.74442504898913 ]
]
let wicket = new Wkt.Wkt(`{"coordinates": [${coords}], "type": "Polygon"}`)

console.log(wicket.write())
// POLYGON((28.324979200606098 -25.74442504898913))

This is because the above statement turns coords into a string. So from "coordinates" : [ [ [x,y], [x,y], ... ] ] into "coordinates" : "x,y,x,y,x,y,...". This is an invalid format and should rather be "coordinates": "[[[x,y],[x, ],... ]]", however a POINT ( ... ) is still returned as if it was correct notation

Thus the stringified version of coords is invalid, but unfortunately is the result of the syntax shown above and is expected. Ideally the invalid syntax should be handled and simply thrown as is done with other invalid cases.

Using JSON.stringify is the solution and correct way of stringifying arrays.