Published
Edited
May 25, 2020
Insert cell
Insert cell
parse('Polygon ((1 2, 3 4), (5 6, 7 8))')
Insert cell
parse('MultiPolygon (((1 2, 3 4), (5 6, 7 8)), ((9 10)))')
Insert cell
function parse(raw) {
// remove "Polygon" or "MultiPolygon"
raw = raw.substr(raw.indexOf('('))
return raw.startsWith('(((')
// "(((..." means it's a multi-polygon
? parseMultiPolygon(raw)
// otherwise, it's a polygon
: parsePolygon(raw)
}
Insert cell
function parseMultiPolygon(raw) {
return raw
// split multipolygon into poloygons
.split(/\)\),\s*\(\(/)
// and parse
.map(parsePolygon)
}
Insert cell
function parsePolygon(raw) {
return raw
// "((1 2, 3 4)), ((5 6, 7 8))" => ["((1 2, 3 4", "5 6, 7 8))"]
.split(/\),\s*\(/)
.map(function(line) {
return line
// remove parentheses
.replace(/[()]/g, '')
// "1 2, 3 4" => ["1 2", " 3 4"]
.split(',')
.map(function(pair) {
return pair
// " 3 4" => "3 4"
.trim()
// "3 4" => ["3", "4"]
.split(' ')
// ["3", "4"] => [3, 4]
.map(parseFloat)
})
})
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more