forked from connexta/wellknown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wellknown.js
273 lines (238 loc) · 6.72 KB
/
wellknown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.wellknown = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/*eslint-disable no-cond-assign */
module.exports = parse;
module.exports.parse = parse;
module.exports.stringify = stringify;
var numberRegexp = /[-+]?([0-9]*\.[0-9]+|[0-9]+)([eE][-+]?[0-9]+)?/;
// Matches sequences like '100 100' or '100 100 100'.
var tuples = new RegExp('^' + numberRegexp.source + '\\s' + numberRegexp.source + '(\\s' + numberRegexp.source + ')?');
/*
* Parse WKT and return GeoJSON.
*
* @param {string} _ A WKT geometry
* @return {?Object} A GeoJSON geometry object
*/
function parse (input) {
var parts = input.split(';');
var _ = parts.pop();
var srid = (parts.shift() || '').split('=').pop();
var i = 0;
function $ (re) {
var match = _.substring(i).match(re);
if (!match) return null;
else {
i += match[0].length;
return match[0];
}
}
function crs (obj) {
if (obj && srid.match(/\d+/)) {
obj.crs = {
type: 'name',
properties: {
name: 'urn:ogc:def:crs:EPSG::' + srid
}
};
}
return obj;
}
function white () { $(/^\s*/); }
function multicoords () {
white();
var depth = 0;
var rings = [];
var stack = [rings];
var pointer = rings;
var elem;
while (elem =
$(/^(\()/) ||
$(/^(\))/) ||
$(/^(\,)/) ||
$(tuples)) {
if (elem === '(') {
stack.push(pointer);
pointer = [];
stack[stack.length - 1].push(pointer);
depth++;
} else if (elem === ')') {
// For the case: Polygon(), ...
if (pointer.length === 0) return null;
pointer = stack.pop();
// the stack was empty, input was malformed
if (!pointer) return null;
depth--;
if (depth === 0) break;
} else if (elem === ',') {
pointer = [];
stack[stack.length - 1].push(pointer);
} else if (!elem.split(/\s/g).some(isNaN)) {
Array.prototype.push.apply(pointer, elem.split(/\s/g).map(parseFloat));
} else {
return null;
}
white();
}
if (depth !== 0) return null;
return rings;
}
function coords () {
var list = [];
var item;
var pt;
while (pt =
$(tuples) ||
$(/^(\,)/)) {
if (pt === ',') {
list.push(item);
item = [];
} else if (!pt.split(/\s/g).some(isNaN)) {
if (!item) item = [];
Array.prototype.push.apply(item, pt.split(/\s/g).map(parseFloat));
}
white();
}
if (item) list.push(item);
else return null;
return list.length ? list : null;
}
function point () {
if (!$(/^(point)/i)) return null;
white();
if (!$(/^(\()/)) return null;
var c = coords();
if (!c) return null;
white();
if (!$(/^(\))/)) return null;
return {
type: 'Point',
coordinates: c[0]
};
}
function multipoint () {
if (!$(/^(multipoint)/i)) return null;
white();
var c = multicoords();
if (!c) return null;
white();
return {
type: 'MultiPoint',
coordinates: c
};
}
function multilinestring () {
if (!$(/^(multilinestring)/i)) return null;
white();
var c = multicoords();
if (!c) return null;
white();
return {
type: 'MultiLineString',
coordinates: c
};
}
function linestring () {
if (!$(/^(linestring)/i)) return null;
white();
if (!$(/^(\()/)) return null;
var c = coords();
if (!c) return null;
if (!$(/^(\))/)) return null;
return {
type: 'LineString',
coordinates: c
};
}
function polygon () {
if (!$(/^(polygon)/i)) return null;
white();
var c = multicoords();
if (!c) return null;
return {
type: 'Polygon',
coordinates: c
};
}
function multipolygon () {
if (!$(/^(multipolygon)/i)) return null;
white();
var c = multicoords();
if (!c) return null;
return {
type: 'MultiPolygon',
coordinates: c
};
}
function geometrycollection () {
var geometries = [];
var geometry;
if (!$(/^(geometrycollection)/i)) return null;
white();
if (!$(/^(\()/)) return null;
while (geometry = root()) {
geometries.push(geometry);
white();
$(/^(\,)/);
white();
}
if (!$(/^(\))/)) return null;
return {
type: 'GeometryCollection',
geometries: geometries
};
}
function root () {
return point() ||
linestring() ||
polygon() ||
multipoint() ||
multilinestring() ||
multipolygon() ||
geometrycollection();
}
return crs(root());
}
/**
* Stringifies a GeoJSON object into WKT
*/
function stringify (gj) {
if (gj.type === 'Feature') {
gj = gj.geometry;
}
function pairWKT (c) {
if (c.length === 2) {
return c[0] + ' ' + c[1];
} else if (c.length === 3) {
return c[0] + ' ' + c[1] + ' ' + c[2];
}
}
function ringWKT (r) {
return r.map(pairWKT).join(', ');
}
function ringsWKT (r) {
return r.map(ringWKT).map(wrapParens).join(', ');
}
function multiRingsWKT (r) {
return r.map(ringsWKT).map(wrapParens).join(', ');
}
function wrapParens (s) { return '(' + s + ')'; }
switch (gj.type) {
case 'Point':
return 'POINT (' + pairWKT(gj.coordinates) + ')';
case 'LineString':
return 'LINESTRING (' + ringWKT(gj.coordinates) + ')';
case 'Polygon':
return 'POLYGON (' + ringsWKT(gj.coordinates) + ')';
case 'MultiPoint':
return 'MULTIPOINT (' + ringWKT(gj.coordinates) + ')';
case 'MultiPolygon':
return 'MULTIPOLYGON (' + multiRingsWKT(gj.coordinates) + ')';
case 'MultiLineString':
return 'MULTILINESTRING (' + ringsWKT(gj.coordinates) + ')';
case 'GeometryCollection':
return 'GEOMETRYCOLLECTION (' + gj.geometries.map(stringify).join(', ') + ')';
default:
throw new Error('stringify requires a valid GeoJSON Feature or geometry object as input');
}
}
},{}]},{},[1])(1)
});