shapefile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // https://github.com/mbostock/shapefile Version 0.6.2. Copyright 2017 Mike Bostock.
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (factory((global.shapefile = global.shapefile || {})));
  6. }(this, (function (exports) { 'use strict';
  7. var array_cancel = function() {
  8. this._array = null;
  9. return Promise.resolve();
  10. };
  11. var array_read = function() {
  12. var array = this._array;
  13. this._array = null;
  14. return Promise.resolve(array ? {done: false, value: array} : {done: true, value: undefined});
  15. };
  16. function array(array) {
  17. return new ArraySource(array instanceof Uint8Array ? array : new Uint8Array(array));
  18. }
  19. function ArraySource(array) {
  20. this._array = array;
  21. }
  22. ArraySource.prototype.read = array_read;
  23. ArraySource.prototype.cancel = array_cancel;
  24. var fetchPath = function(url) {
  25. return fetch(url).then(function(response) {
  26. return response.body && response.body.getReader
  27. ? response.body.getReader()
  28. : response.arrayBuffer().then(array);
  29. });
  30. };
  31. var requestPath = function(url) {
  32. return new Promise(function(resolve, reject) {
  33. var request = new XMLHttpRequest;
  34. request.responseType = "arraybuffer";
  35. request.onload = function() { resolve(array(request.response)); };
  36. request.onerror = reject;
  37. request.ontimeout = reject;
  38. request.open("GET", url, true);
  39. request.send();
  40. });
  41. };
  42. function path(path) {
  43. return (typeof fetch === "function" ? fetchPath : requestPath)(path);
  44. }
  45. function stream(source) {
  46. return typeof source.read === "function" ? source : source.getReader();
  47. }
  48. var empty = new Uint8Array(0);
  49. var slice_cancel = function() {
  50. return this._source.cancel();
  51. };
  52. function concat(a, b) {
  53. if (!a.length) return b;
  54. if (!b.length) return a;
  55. var c = new Uint8Array(a.length + b.length);
  56. c.set(a);
  57. c.set(b, a.length);
  58. return c;
  59. }
  60. var slice_read = function() {
  61. var that = this, array = that._array.subarray(that._index);
  62. return that._source.read().then(function(result) {
  63. that._array = empty;
  64. that._index = 0;
  65. return result.done ? (array.length > 0
  66. ? {done: false, value: array}
  67. : {done: true, value: undefined})
  68. : {done: false, value: concat(array, result.value)};
  69. });
  70. };
  71. var slice_slice = function(length) {
  72. if ((length |= 0) < 0) throw new Error("invalid length");
  73. var that = this, index = this._array.length - this._index;
  74. // If the request fits within the remaining buffer, resolve it immediately.
  75. if (this._index + length <= this._array.length) {
  76. return Promise.resolve(this._array.subarray(this._index, this._index += length));
  77. }
  78. // Otherwise, read chunks repeatedly until the request is fulfilled.
  79. var array = new Uint8Array(length);
  80. array.set(this._array.subarray(this._index));
  81. return (function read() {
  82. return that._source.read().then(function(result) {
  83. // When done, it’s possible the request wasn’t fully fullfilled!
  84. // If so, the pre-allocated array is too big and needs slicing.
  85. if (result.done) {
  86. that._array = empty;
  87. that._index = 0;
  88. return index > 0 ? array.subarray(0, index) : null;
  89. }
  90. // If this chunk fulfills the request, return the resulting array.
  91. if (index + result.value.length >= length) {
  92. that._array = result.value;
  93. that._index = length - index;
  94. array.set(result.value.subarray(0, length - index), index);
  95. return array;
  96. }
  97. // Otherwise copy this chunk into the array, then read the next chunk.
  98. array.set(result.value, index);
  99. index += result.value.length;
  100. return read();
  101. });
  102. })();
  103. };
  104. function slice(source) {
  105. return typeof source.slice === "function" ? source :
  106. new SliceSource(typeof source.read === "function" ? source
  107. : source.getReader());
  108. }
  109. function SliceSource(source) {
  110. this._source = source;
  111. this._array = empty;
  112. this._index = 0;
  113. }
  114. SliceSource.prototype.read = slice_read;
  115. SliceSource.prototype.slice = slice_slice;
  116. SliceSource.prototype.cancel = slice_cancel;
  117. var dbf_cancel = function() {
  118. return this._source.cancel();
  119. };
  120. var readBoolean = function(value) {
  121. return /^[nf]$/i.test(value) ? false
  122. : /^[yt]$/i.test(value) ? true
  123. : null;
  124. };
  125. var readDate = function(value) {
  126. return new Date(+value.substring(0, 4), value.substring(4, 6) - 1, +value.substring(6, 8));
  127. };
  128. var readNumber = function(value) {
  129. return !(value = value.trim()) || isNaN(value = +value) ? null : value;
  130. };
  131. var readString = function(value) {
  132. return value.trim() || null;
  133. };
  134. var types = {
  135. B: readNumber,
  136. C: readString,
  137. D: readDate,
  138. F: readNumber,
  139. L: readBoolean,
  140. M: readNumber,
  141. N: readNumber
  142. };
  143. var dbf_read = function() {
  144. var that = this, i = 1;
  145. return that._source.slice(that._recordLength).then(function(value) {
  146. return value && (value[0] !== 0x1a) ? {done: false, value: that._fields.reduce(function(p, f) {
  147. p[f.name] = types[f.type](that._decode(value.subarray(i, i += f.length)));
  148. return p;
  149. }, {})} : {done: true, value: undefined};
  150. });
  151. };
  152. var view = function(array) {
  153. return new DataView(array.buffer, array.byteOffset, array.byteLength);
  154. };
  155. var dbf = function(source, decoder) {
  156. source = slice(source);
  157. return source.slice(32).then(function(array) {
  158. var head = view(array);
  159. return source.slice(head.getUint16(8, true) - 32).then(function(array) {
  160. return new Dbf(source, decoder, head, view(array));
  161. });
  162. });
  163. };
  164. function Dbf(source, decoder, head, body) {
  165. this._source = source;
  166. this._decode = decoder.decode.bind(decoder);
  167. this._recordLength = head.getUint16(10, true);
  168. this._fields = [];
  169. for (var n = 0; body.getUint8(n) !== 0x0d; n += 32) {
  170. for (var j = 0; j < 11; ++j) if (body.getUint8(n + j) === 0) break;
  171. this._fields.push({
  172. name: this._decode(new Uint8Array(body.buffer, body.byteOffset + n, j)),
  173. type: String.fromCharCode(body.getUint8(n + 11)),
  174. length: body.getUint8(n + 16)
  175. });
  176. }
  177. }
  178. var prototype = Dbf.prototype;
  179. prototype.read = dbf_read;
  180. prototype.cancel = dbf_cancel;
  181. function cancel() {
  182. return this._source.cancel();
  183. }
  184. var readMultiPoint = function(record) {
  185. var i = 40, j, n = record.getInt32(36, true), coordinates = new Array(n);
  186. for (j = 0; j < n; ++j, i += 16) coordinates[j] = [record.getFloat64(i, true), record.getFloat64(i + 8, true)];
  187. return {type: "MultiPoint", coordinates: coordinates};
  188. };
  189. var readNull = function() {
  190. return null;
  191. };
  192. var readPoint = function(record) {
  193. return {type: "Point", coordinates: [record.getFloat64(4, true), record.getFloat64(12, true)]};
  194. };
  195. var readPolygon = function(record) {
  196. var i = 44, j, n = record.getInt32(36, true), m = record.getInt32(40, true), parts = new Array(n), points = new Array(m), polygons = [], holes = [];
  197. for (j = 0; j < n; ++j, i += 4) parts[j] = record.getInt32(i, true);
  198. for (j = 0; j < m; ++j, i += 16) points[j] = [record.getFloat64(i, true), record.getFloat64(i + 8, true)];
  199. parts.forEach(function(i, j) {
  200. var ring = points.slice(i, parts[j + 1]);
  201. if (ringClockwise(ring)) polygons.push([ring]);
  202. else holes.push(ring);
  203. });
  204. holes.forEach(function(hole) {
  205. polygons.some(function(polygon) {
  206. if (ringContainsSome(polygon[0], hole)) {
  207. polygon.push(hole);
  208. return true;
  209. }
  210. }) || polygons.push([hole]);
  211. });
  212. return polygons.length === 1
  213. ? {type: "Polygon", coordinates: polygons[0]}
  214. : {type: "MultiPolygon", coordinates: polygons};
  215. };
  216. function ringClockwise(ring) {
  217. if ((n = ring.length) < 4) return false;
  218. var i = 0, n, area = ring[n - 1][1] * ring[0][0] - ring[n - 1][0] * ring[0][1];
  219. while (++i < n) area += ring[i - 1][1] * ring[i][0] - ring[i - 1][0] * ring[i][1];
  220. return area >= 0;
  221. }
  222. function ringContainsSome(ring, hole) {
  223. var i = -1, n = hole.length, c;
  224. while (++i < n) {
  225. if (c = ringContains(ring, hole[i])) {
  226. return c > 0;
  227. }
  228. }
  229. return false;
  230. }
  231. function ringContains(ring, point) {
  232. var x = point[0], y = point[1], contains = -1;
  233. for (var i = 0, n = ring.length, j = n - 1; i < n; j = i++) {
  234. var pi = ring[i], xi = pi[0], yi = pi[1],
  235. pj = ring[j], xj = pj[0], yj = pj[1];
  236. if (segmentContains(pi, pj, point)) {
  237. return 0;
  238. }
  239. if (((yi > y) !== (yj > y)) && ((x < (xj - xi) * (y - yi) / (yj - yi) + xi))) {
  240. contains = -contains;
  241. }
  242. }
  243. return contains;
  244. }
  245. function segmentContains(p0, p1, p2) {
  246. var x20 = p2[0] - p0[0], y20 = p2[1] - p0[1];
  247. if (x20 === 0 && y20 === 0) return true;
  248. var x10 = p1[0] - p0[0], y10 = p1[1] - p0[1];
  249. if (x10 === 0 && y10 === 0) return false;
  250. var t = (x20 * x10 + y20 * y10) / (x10 * x10 + y10 * y10);
  251. return t < 0 || t > 1 ? false : t === 0 || t === 1 ? true : t * x10 === x20 && t * y10 === y20;
  252. }
  253. var readPolyLine = function(record) {
  254. var i = 44, j, n = record.getInt32(36, true), m = record.getInt32(40, true), parts = new Array(n), points = new Array(m);
  255. for (j = 0; j < n; ++j, i += 4) parts[j] = record.getInt32(i, true);
  256. for (j = 0; j < m; ++j, i += 16) points[j] = [record.getFloat64(i, true), record.getFloat64(i + 8, true)];
  257. return n === 1
  258. ? {type: "LineString", coordinates: points}
  259. : {type: "MultiLineString", coordinates: parts.map(function(i, j) { return points.slice(i, parts[j + 1]); })};
  260. };
  261. var readPolyLineZ = function(record) {
  262. var i = 44, j, n = record.getInt32(36, true), m = record.getInt32(40, true), parts = new Array(n), points = new Array(m);
  263. for (j = 0; j < n; ++j, i += 4) parts[j] = record.getInt32(i, true);
  264. for (j = 0; j < m; ++j, i += 16) points[j] = [record.getFloat64(i, true), record.getFloat64(i + 8, true)];
  265. // Advance two doubles (z min, z max)
  266. i += 16;
  267. for (j = 0; j < m; ++j, i += 8) points[j].push(record.getFloat64(i, true));
  268. return n === 1
  269. ? {type: "LineStringZ", coordinates: points}
  270. : {type: "MultiLineStringZ", coordinates: parts.map(function(i, j) { return points.slice(i, parts[j + 1]); })};
  271. };
  272. var shp_read = function() {
  273. var that = this;
  274. return that._source.slice(8).then(function(array) {
  275. if (array == null) return {done: true, value: undefined};
  276. var header = view(array);
  277. return that._source.slice(header.getInt32(4, false) * 2).then(function(array) {
  278. var record = view(array);
  279. return {done: false, value: record.getInt32(0, true) ? that._type(record) : readNull()};
  280. });
  281. });
  282. };
  283. var types$1 = {
  284. 0: readNull,
  285. 1: readPoint,
  286. 3: readPolyLine,
  287. 5: readPolygon,
  288. 8: readMultiPoint,
  289. 11: readPoint,
  290. 13: readPolyLineZ,
  291. 15: readPolygon,
  292. 18: readMultiPoint
  293. };
  294. var shp = function(source) {
  295. source = slice(source);
  296. return source.slice(100).then(function(array) {
  297. return new Shp(source, view(array));
  298. });
  299. };
  300. function Shp(source, header) {
  301. var type = header.getInt32(32, true);
  302. if (!(type in types$1)) throw new Error("unsupported shape type: " + type);
  303. this._source = source;
  304. this._type = types$1[type];
  305. this.bbox = [header.getFloat64(36, true), header.getFloat64(44, true), header.getFloat64(52, true), header.getFloat64(60, true)];
  306. }
  307. var prototype$2 = Shp.prototype;
  308. prototype$2.read = shp_read;
  309. prototype$2.cancel = cancel;
  310. function noop() {}
  311. var shapefile_cancel = function() {
  312. return Promise.all([
  313. this._dbf && this._dbf.cancel(),
  314. this._shp.cancel()
  315. ]).then(noop);
  316. };
  317. var shapefile_read = function() {
  318. var that = this;
  319. return Promise.all([
  320. that._dbf ? that._dbf.read() : {value: {}},
  321. that._shp.read()
  322. ]).then(function(results) {
  323. var dbf = results[0], shp = results[1];
  324. return shp.done ? shp : {
  325. done: false,
  326. value: {
  327. type: "Feature",
  328. properties: dbf.value,
  329. geometry: shp.value
  330. }
  331. };
  332. });
  333. };
  334. var shapefile = function(shpSource, dbfSource, decoder) {
  335. return Promise.all([
  336. shp(shpSource),
  337. dbfSource && dbf(dbfSource, decoder)
  338. ]).then(function(sources) {
  339. return new Shapefile(sources[0], sources[1]);
  340. });
  341. };
  342. function Shapefile(shp$$1, dbf$$1) {
  343. this._shp = shp$$1;
  344. this._dbf = dbf$$1;
  345. this.bbox = shp$$1.bbox;
  346. }
  347. var prototype$1 = Shapefile.prototype;
  348. prototype$1.read = shapefile_read;
  349. prototype$1.cancel = shapefile_cancel;
  350. function open(shp$$1, dbf$$1, options) {
  351. if (typeof dbf$$1 === "string") {
  352. if (!/\.dbf$/.test(dbf$$1)) dbf$$1 += ".dbf";
  353. dbf$$1 = path(dbf$$1, options);
  354. } else if (dbf$$1 instanceof ArrayBuffer || dbf$$1 instanceof Uint8Array) {
  355. dbf$$1 = array(dbf$$1);
  356. } else if (dbf$$1 != null) {
  357. dbf$$1 = stream(dbf$$1);
  358. }
  359. if (typeof shp$$1 === "string") {
  360. if (!/\.shp$/.test(shp$$1)) shp$$1 += ".shp";
  361. if (dbf$$1 === undefined) dbf$$1 = path(shp$$1.substring(0, shp$$1.length - 4) + ".dbf", options).catch(function() {});
  362. shp$$1 = path(shp$$1, options);
  363. } else if (shp$$1 instanceof ArrayBuffer || shp$$1 instanceof Uint8Array) {
  364. shp$$1 = array(shp$$1);
  365. } else {
  366. shp$$1 = stream(shp$$1);
  367. }
  368. return Promise.all([shp$$1, dbf$$1]).then(function(sources) {
  369. var shp$$1 = sources[0], dbf$$1 = sources[1], encoding = "windows-1252";
  370. if (options && options.encoding != null) encoding = options.encoding;
  371. return shapefile(shp$$1, dbf$$1, dbf$$1 && new TextDecoder(encoding));
  372. });
  373. }
  374. function openShp(source, options) {
  375. if (typeof source === "string") {
  376. if (!/\.shp$/.test(source)) source += ".shp";
  377. source = path(source, options);
  378. } else if (source instanceof ArrayBuffer || source instanceof Uint8Array) {
  379. source = array(source);
  380. } else {
  381. source = stream(source);
  382. }
  383. return Promise.resolve(source).then(shp);
  384. }
  385. function openDbf(source, options) {
  386. var encoding = "windows-1252";
  387. if (options && options.encoding != null) encoding = options.encoding;
  388. encoding = new TextDecoder(encoding);
  389. if (typeof source === "string") {
  390. if (!/\.dbf$/.test(source)) source += ".dbf";
  391. source = path(source, options);
  392. } else if (source instanceof ArrayBuffer || source instanceof Uint8Array) {
  393. source = array(source);
  394. } else {
  395. source = stream(source);
  396. }
  397. return Promise.resolve(source).then(function(source) {
  398. return dbf(source, encoding);
  399. });
  400. }
  401. function read(shp$$1, dbf$$1, options) {
  402. return open(shp$$1, dbf$$1, options).then(function(source) {
  403. var features = [], collection = {type: "FeatureCollection", features: features, bbox: source.bbox};
  404. return source.read().then(function read(result) {
  405. if (result.done) return collection;
  406. features.push(result.value);
  407. return source.read().then(read);
  408. });
  409. });
  410. }
  411. exports.open = open;
  412. exports.openShp = openShp;
  413. exports.openDbf = openDbf;
  414. exports.read = read;
  415. Object.defineProperty(exports, '__esModule', { value: true });
  416. })));