dgeoutils 2.4.4 → 2.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,13 @@
1
1
  import { DLine } from './DLine';
2
2
  import { DPolygon } from './DPolygon';
3
+ import { Point } from 'geojson';
3
4
  export declare const EARTH_RADIUS_IN_METERS = 6371008.8;
4
5
  export declare type DCoord = [number, number] | [number, number, number];
5
6
  export interface LatLng {
6
7
  lat: number;
7
- lng: number;
8
+ lng?: number;
9
+ lon?: number;
10
+ alt?: number;
8
11
  }
9
12
  export declare const HALF_PI_IN_DEGREE = 90;
10
13
  export declare const PI_IN_DEGREE = 180;
@@ -24,12 +27,15 @@ export declare class DPoint {
24
27
  constructor(x: number, y: number);
25
28
  constructor(x: number, y: number, z?: number);
26
29
  static zero(): DPoint;
27
- static parse(c: LatLng | number[] | DCoord): DPoint;
30
+ static parse(c: LatLng | number[] | DCoord | Point, format?: string): DPoint;
28
31
  static parseFromWKT(wkt: string): DPoint;
29
32
  static random(): DPoint;
33
+ static getTileFromQuadKey(quadKey: string): DPoint;
30
34
  getTileFromCoords(zoom?: number): DPoint;
35
+ getQuadKeyFromTile(zoom?: number): string;
31
36
  getCoordsFromTile(zoom?: number): DPoint;
32
- toCoords(): DCoord;
37
+ toCoords(format?: string): DCoord;
38
+ toGeoJSON(format?: string): Point;
33
39
  findLine(p: DPoint): DLine;
34
40
  findInnerAngle(p1: DPoint, p3: DPoint): number;
35
41
  toString(): string;
@@ -57,13 +57,34 @@ var DPoint = (function () {
57
57
  DPoint.zero = function () {
58
58
  return new DPoint();
59
59
  };
60
- DPoint.parse = function (c) {
61
- var _a = c, lat = _a.lat, lng = _a.lng;
60
+ DPoint.parse = function (c, format) {
61
+ if (format === void 0) { format = 'xyz'; }
62
+ var _a = c, lat = _a.lat, lon = _a.lon, _b = _a.lng, lng = _b === void 0 ? lon : _b, alt = _a.alt;
62
63
  if (lat && lng) {
63
- return new DPoint(lat, lng, 0);
64
+ return new DPoint(lat, lng, alt !== null && alt !== void 0 ? alt : 0);
64
65
  }
65
- var _b = __read(c, 3), x = _b[0], y = _b[1], z = _b[2];
66
- return new DPoint(x, y, z);
66
+ var t = c;
67
+ if (c.type === 'Point') {
68
+ t = c.coordinates;
69
+ }
70
+ return format.replace(/[^x-z]/gmiu, '')
71
+ .split('')
72
+ .reduce(function (a, k, index) {
73
+ var _a, _b;
74
+ switch (k) {
75
+ case 'x':
76
+ a.x = (_a = t[index]) !== null && _a !== void 0 ? _a : 0;
77
+ break;
78
+ case 'y':
79
+ a.y = (_b = t[index]) !== null && _b !== void 0 ? _b : 0;
80
+ break;
81
+ case 'z':
82
+ a.z = t[index];
83
+ break;
84
+ default:
85
+ }
86
+ return a;
87
+ }, new DPoint());
67
88
  };
68
89
  DPoint.parseFromWKT = function (wkt) {
69
90
  var regexp = /POINT \((?<data>(?:(?!\)).)*?)\)$/miu;
@@ -75,6 +96,29 @@ var DPoint = (function () {
75
96
  DPoint.random = function () {
76
97
  return new DPoint(Math.random(), Math.random());
77
98
  };
99
+ DPoint.getTileFromQuadKey = function (quadKey) {
100
+ var p = new DPoint(0, 0, quadKey.length);
101
+ for (var i = p.z; i > 0; i--) {
102
+ var mask = 1 << (i - 1);
103
+ switch (quadKey[p.z - i]) {
104
+ case '0':
105
+ break;
106
+ case '1':
107
+ p.x |= mask;
108
+ break;
109
+ case '2':
110
+ p.y |= mask;
111
+ break;
112
+ case '3':
113
+ p.x |= mask;
114
+ p.y |= mask;
115
+ break;
116
+ default:
117
+ throw new Error('Invalid QuadKey digit sequence.');
118
+ }
119
+ }
120
+ return p;
121
+ };
78
122
  DPoint.prototype.getTileFromCoords = function (zoom) {
79
123
  if (zoom === void 0) { zoom = this.z; }
80
124
  (0, utils_1.checkFunction)('getTileFromCoords')
@@ -84,6 +128,23 @@ var DPoint = (function () {
84
128
  var y = Math.floor((1 - Math.log(Math.tan(this.y * exports.PI_TO_DEGREE) + 1 / Math.cos(this.y * exports.PI_TO_DEGREE)) / Math.PI) / 2 * (Math.pow(2, zoom)));
85
129
  return new DPoint(x, y, zoom);
86
130
  };
131
+ DPoint.prototype.getQuadKeyFromTile = function (zoom) {
132
+ if (zoom === void 0) { zoom = this.z; }
133
+ var quadKey = [];
134
+ for (var i = zoom; i > 0; i--) {
135
+ var digit = 0;
136
+ var mask = 1 << (i - 1);
137
+ if ((this.x & mask) !== 0) {
138
+ digit++;
139
+ }
140
+ if ((this.y & mask) !== 0) {
141
+ digit++;
142
+ digit++;
143
+ }
144
+ quadKey.push(digit);
145
+ }
146
+ return quadKey.join('');
147
+ };
87
148
  DPoint.prototype.getCoordsFromTile = function (zoom) {
88
149
  if (zoom === void 0) { zoom = this.z; }
89
150
  (0, utils_1.checkFunction)('getCoordsFromTile')
@@ -94,11 +155,23 @@ var DPoint = (function () {
94
155
  var y = exports.PI_IN_DEGREE / Math.PI * Math.atan((Math.exp(n) - Math.exp(-n)) / 2);
95
156
  return new DPoint(x, y, zoom);
96
157
  };
97
- DPoint.prototype.toCoords = function () {
98
- if (this.z === undefined) {
99
- return [this.x, this.y];
100
- }
101
- return [this.x, this.y, this.z];
158
+ DPoint.prototype.toCoords = function (format) {
159
+ var _this = this;
160
+ if (format === void 0) { format = 'xyz'; }
161
+ return format.replace(/[^x-z]/gmiu, '').split('')
162
+ .map(function (k) { return ({
163
+ x: _this.x,
164
+ y: _this.y,
165
+ z: _this.z
166
+ })[k]; })
167
+ .filter(function (r) { return r !== undefined; });
168
+ };
169
+ DPoint.prototype.toGeoJSON = function (format) {
170
+ if (format === void 0) { format = 'xyz'; }
171
+ return {
172
+ type: 'Point',
173
+ coordinates: this.toCoords(format)
174
+ };
102
175
  };
103
176
  DPoint.prototype.findLine = function (p) {
104
177
  (0, utils_1.checkFunction)('findLine')
@@ -3,12 +3,13 @@ import { DCoord, DPoint, LatLng } from './DPoint';
3
3
  import { DLine } from './DLine';
4
4
  import { DPolygonLoop } from './DPolygonLoop';
5
5
  import { True } from './utils';
6
+ import { LineString, Polygon, Geometry as GeoJsonGeometry, Feature, FeatureCollection } from 'geojson';
7
+ interface DeepArray<T> extends Array<T | DeepArray<T>> {
8
+ }
6
9
  export declare const MIN_POINTS_IN_VALID_POLYGON = 3;
7
10
  export declare class DPolygon {
8
11
  private pPoints;
9
- properties: {
10
- [key: string]: any;
11
- };
12
+ properties: Record<string, any>;
12
13
  holes: DPolygon[];
13
14
  private searchStore;
14
15
  constructor(pPoints?: DPoint[]);
@@ -93,10 +94,14 @@ export declare class DPolygon {
93
94
  onBorder(p: DPoint): boolean;
94
95
  nextStart(): DPolygon;
95
96
  removeDuplicates(): DPolygon;
96
- static parse(a: LatLng[]): DPolygon;
97
- static parse(a: number[][]): DPolygon;
98
- static parse(a: DCoord[]): DPolygon;
99
- toArrayOfCoords(): DCoord[];
97
+ static toGeoJSONFeatureCollection(polygons: DPolygon[], format?: string): FeatureCollection<LineString | Polygon, Record<string, any>>;
98
+ static parse(a: LatLng[], format?: string): DPolygon;
99
+ static parse(a: number[][], format?: string): DPolygon;
100
+ static parse(a: DCoord[], format?: string): DPolygon;
101
+ static parse(a: GeoJsonGeometry | Feature | FeatureCollection<LineString | Polygon>, format?: string): DPolygon | DeepArray<DPolygon>;
102
+ toArrayOfCoords(format?: string): DCoord[];
103
+ toGeoJSONFeature(format?: string): Feature<LineString | Polygon, Record<string, any>>;
104
+ toGeoJSON(format?: string): LineString | Polygon;
100
105
  divideToPieces(piecesCount: number, withAltitude?: boolean): DPolygon;
101
106
  prepareToFastSearch(): void;
102
107
  fastHas({ x, y, z }: DPoint): boolean;
@@ -122,3 +127,4 @@ export declare class DPolygon {
122
127
  private getJSTSGeometry;
123
128
  private simpleLogicFunction;
124
129
  }
130
+ export {};
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __generator = (this && this.__generator) || function (thisArg, body) {
3
14
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4
15
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
@@ -955,11 +966,96 @@ var DPolygon = (function () {
955
966
  }
956
967
  return this;
957
968
  };
958
- DPolygon.parse = function (a) {
959
- return new DPolygon(a.map(function (r) { return DPoint_1.DPoint.parse(r); }));
969
+ DPolygon.toGeoJSONFeatureCollection = function (polygons, format) {
970
+ if (format === void 0) { format = 'xyz'; }
971
+ return {
972
+ type: 'FeatureCollection',
973
+ features: polygons.map(function (polygon) { return polygon.toGeoJSONFeature(format); })
974
+ };
960
975
  };
961
- DPolygon.prototype.toArrayOfCoords = function () {
962
- return this.mapArray(function (r) { return r.toCoords(); });
976
+ DPolygon.parse = function (a, format) {
977
+ if (format === void 0) { format = 'xyz'; }
978
+ if (a.type) {
979
+ switch (a.type) {
980
+ case 'FeatureCollection':
981
+ return a.features.reduce(function (ak, f) {
982
+ var t = DPolygon.parse(f, format);
983
+ if (Array.isArray(t)) {
984
+ ak.push.apply(ak, __spreadArray([], __read(t), false));
985
+ }
986
+ else {
987
+ ak.push(t);
988
+ }
989
+ return ak;
990
+ }, []);
991
+ case 'Feature': {
992
+ var t = DPolygon.parse(a.geometry, format);
993
+ t.properties = __assign(__assign({}, a.properties), { id: a.id });
994
+ return t;
995
+ }
996
+ case 'LineString':
997
+ case 'MultiPoint':
998
+ return new DPolygon(a.coordinates.map(function (c) { return DPoint_1.DPoint.parse(c, format); }));
999
+ case 'Polygon':
1000
+ return a.coordinates.reduce(function (ak, line, index) {
1001
+ if (index === 0) {
1002
+ ak.points = line.map(function (c) { return DPoint_1.DPoint.parse(c, format); });
1003
+ }
1004
+ else {
1005
+ ak.holes.push(new DPolygon(line.map(function (c) { return DPoint_1.DPoint.parse(c, format); })));
1006
+ }
1007
+ return ak;
1008
+ }, new DPolygon());
1009
+ case 'MultiLineString':
1010
+ return a.coordinates.reduce(function (ak, line) {
1011
+ ak.push(new DPolygon(line.map(function (c) { return DPoint_1.DPoint.parse(c, format); })));
1012
+ return ak;
1013
+ }, []);
1014
+ case 'MultiPolygon':
1015
+ return a.coordinates.reduce(function (ak, coordinates) {
1016
+ ak.push(DPolygon.parse({
1017
+ type: 'Polygon',
1018
+ coordinates: coordinates
1019
+ }, format));
1020
+ return ak;
1021
+ }, []);
1022
+ case 'GeometryCollection':
1023
+ return a.geometries.reduce(function (ak, line) {
1024
+ ak.push(DPolygon.parse(line, format));
1025
+ return ak;
1026
+ }, []);
1027
+ default:
1028
+ }
1029
+ }
1030
+ return new DPolygon(a
1031
+ .map(function (r) { return DPoint_1.DPoint.parse(r, format); }));
1032
+ };
1033
+ DPolygon.prototype.toArrayOfCoords = function (format) {
1034
+ if (format === void 0) { format = 'xyz'; }
1035
+ return this.mapArray(function (r) { return r.toCoords(format); });
1036
+ };
1037
+ DPolygon.prototype.toGeoJSONFeature = function (format) {
1038
+ if (format === void 0) { format = 'xyz'; }
1039
+ return {
1040
+ type: 'Feature',
1041
+ properties: __assign({}, this.properties),
1042
+ geometry: this.toGeoJSON(format)
1043
+ };
1044
+ };
1045
+ DPolygon.prototype.toGeoJSON = function (format) {
1046
+ if (format === void 0) { format = 'xyz'; }
1047
+ if (this.closed) {
1048
+ return {
1049
+ type: 'Polygon',
1050
+ coordinates: __spreadArray([
1051
+ this.toArrayOfCoords(format)
1052
+ ], __read(this.holes.map(function (h) { return h.toArrayOfCoords(format); })), false)
1053
+ };
1054
+ }
1055
+ return {
1056
+ type: 'LineString',
1057
+ coordinates: this.toArrayOfCoords(format)
1058
+ };
963
1059
  };
964
1060
  DPolygon.prototype.divideToPieces = function (piecesCount, withAltitude) {
965
1061
  var e_13, _a;
@@ -25,13 +25,33 @@ export class DPoint {
25
25
  static zero() {
26
26
  return new DPoint();
27
27
  }
28
- static parse(c) {
29
- const { lat, lng } = c;
28
+ static parse(c, format = 'xyz') {
29
+ const { lat, lon, lng = lon, alt } = c;
30
30
  if (lat && lng) {
31
- return new DPoint(lat, lng, 0);
31
+ return new DPoint(lat, lng, alt !== null && alt !== void 0 ? alt : 0);
32
32
  }
33
- const [x, y, z] = c;
34
- return new DPoint(x, y, z);
33
+ let t = c;
34
+ if (c.type === 'Point') {
35
+ t = c.coordinates;
36
+ }
37
+ return format.replace(/[^x-z]/gmiu, '')
38
+ .split('')
39
+ .reduce((a, k, index) => {
40
+ var _a, _b;
41
+ switch (k) {
42
+ case 'x':
43
+ a.x = (_a = t[index]) !== null && _a !== void 0 ? _a : 0;
44
+ break;
45
+ case 'y':
46
+ a.y = (_b = t[index]) !== null && _b !== void 0 ? _b : 0;
47
+ break;
48
+ case 'z':
49
+ a.z = t[index];
50
+ break;
51
+ default:
52
+ }
53
+ return a;
54
+ }, new DPoint());
35
55
  }
36
56
  static parseFromWKT(wkt) {
37
57
  const regexp = /POINT \((?<data>(?:(?!\)).)*?)\)$/miu;
@@ -43,6 +63,29 @@ export class DPoint {
43
63
  static random() {
44
64
  return new DPoint(Math.random(), Math.random());
45
65
  }
66
+ static getTileFromQuadKey(quadKey) {
67
+ const p = new DPoint(0, 0, quadKey.length);
68
+ for (let i = p.z; i > 0; i--) {
69
+ const mask = 1 << (i - 1);
70
+ switch (quadKey[p.z - i]) {
71
+ case '0':
72
+ break;
73
+ case '1':
74
+ p.x |= mask;
75
+ break;
76
+ case '2':
77
+ p.y |= mask;
78
+ break;
79
+ case '3':
80
+ p.x |= mask;
81
+ p.y |= mask;
82
+ break;
83
+ default:
84
+ throw new Error('Invalid QuadKey digit sequence.');
85
+ }
86
+ }
87
+ return p;
88
+ }
46
89
  getTileFromCoords(zoom = this.z) {
47
90
  checkFunction('getTileFromCoords')
48
91
  .checkArgument('this')
@@ -51,6 +94,22 @@ export class DPoint {
51
94
  const y = Math.floor((1 - Math.log(Math.tan(this.y * PI_TO_DEGREE) + 1 / Math.cos(this.y * PI_TO_DEGREE)) / Math.PI) / 2 * (Math.pow(2, zoom)));
52
95
  return new DPoint(x, y, zoom);
53
96
  }
97
+ getQuadKeyFromTile(zoom = this.z) {
98
+ const quadKey = [];
99
+ for (let i = zoom; i > 0; i--) {
100
+ let digit = 0;
101
+ const mask = 1 << (i - 1);
102
+ if ((this.x & mask) !== 0) {
103
+ digit++;
104
+ }
105
+ if ((this.y & mask) !== 0) {
106
+ digit++;
107
+ digit++;
108
+ }
109
+ quadKey.push(digit);
110
+ }
111
+ return quadKey.join('');
112
+ }
54
113
  getCoordsFromTile(zoom = this.z) {
55
114
  checkFunction('getCoordsFromTile')
56
115
  .checkArgument('this')
@@ -60,11 +119,20 @@ export class DPoint {
60
119
  const y = PI_IN_DEGREE / Math.PI * Math.atan((Math.exp(n) - Math.exp(-n)) / 2);
61
120
  return new DPoint(x, y, zoom);
62
121
  }
63
- toCoords() {
64
- if (this.z === undefined) {
65
- return [this.x, this.y];
66
- }
67
- return [this.x, this.y, this.z];
122
+ toCoords(format = 'xyz') {
123
+ return format.replace(/[^x-z]/gmiu, '').split('')
124
+ .map((k) => ({
125
+ x: this.x,
126
+ y: this.y,
127
+ z: this.z
128
+ })[k])
129
+ .filter((r) => r !== undefined);
130
+ }
131
+ toGeoJSON(format = 'xyz') {
132
+ return {
133
+ type: 'Point',
134
+ coordinates: this.toCoords(format)
135
+ };
68
136
  }
69
137
  findLine(p) {
70
138
  checkFunction('findLine')
@@ -618,11 +618,92 @@ export class DPolygon {
618
618
  }
619
619
  return this;
620
620
  }
621
- static parse(a) {
622
- return new DPolygon(a.map((r) => DPoint.parse(r)));
621
+ static toGeoJSONFeatureCollection(polygons, format = 'xyz') {
622
+ return {
623
+ type: 'FeatureCollection',
624
+ features: polygons.map((polygon) => polygon.toGeoJSONFeature(format))
625
+ };
626
+ }
627
+ static parse(a, format = 'xyz') {
628
+ if (a.type) {
629
+ switch (a.type) {
630
+ case 'FeatureCollection':
631
+ return a.features.reduce((ak, f) => {
632
+ const t = DPolygon.parse(f, format);
633
+ if (Array.isArray(t)) {
634
+ ak.push(...t);
635
+ }
636
+ else {
637
+ ak.push(t);
638
+ }
639
+ return ak;
640
+ }, []);
641
+ case 'Feature': {
642
+ const t = DPolygon.parse(a.geometry, format);
643
+ t.properties = Object.assign(Object.assign({}, a.properties), { id: a.id });
644
+ return t;
645
+ }
646
+ case 'LineString':
647
+ case 'MultiPoint':
648
+ return new DPolygon(a.coordinates.map((c) => DPoint.parse(c, format)));
649
+ case 'Polygon':
650
+ return a.coordinates.reduce((ak, line, index) => {
651
+ if (index === 0) {
652
+ ak.points = line.map((c) => DPoint.parse(c, format));
653
+ }
654
+ else {
655
+ ak.holes.push(new DPolygon(line.map((c) => DPoint.parse(c, format))));
656
+ }
657
+ return ak;
658
+ }, new DPolygon());
659
+ case 'MultiLineString':
660
+ return a.coordinates.reduce((ak, line) => {
661
+ ak.push(new DPolygon(line.map((c) => DPoint.parse(c, format))));
662
+ return ak;
663
+ }, []);
664
+ case 'MultiPolygon':
665
+ return a.coordinates.reduce((ak, coordinates) => {
666
+ ak.push(DPolygon.parse({
667
+ type: 'Polygon',
668
+ coordinates
669
+ }, format));
670
+ return ak;
671
+ }, []);
672
+ case 'GeometryCollection':
673
+ return a.geometries.reduce((ak, line) => {
674
+ ak.push(DPolygon.parse(line, format));
675
+ return ak;
676
+ }, []);
677
+ default:
678
+ }
679
+ }
680
+ return new DPolygon(a
681
+ .map((r) => DPoint.parse(r, format)));
623
682
  }
624
- toArrayOfCoords() {
625
- return this.mapArray((r) => r.toCoords());
683
+ toArrayOfCoords(format = 'xyz') {
684
+ return this.mapArray((r) => r.toCoords(format));
685
+ }
686
+ toGeoJSONFeature(format = 'xyz') {
687
+ return {
688
+ type: 'Feature',
689
+ properties: Object.assign({}, this.properties),
690
+ geometry: this.toGeoJSON(format)
691
+ };
692
+ }
693
+ toGeoJSON(format = 'xyz') {
694
+ if (this.closed) {
695
+ return {
696
+ type: 'Polygon',
697
+ coordinates: [
698
+ this.toArrayOfCoords(format),
699
+ ...this.holes.map((h) => h.toArrayOfCoords(format))
700
+ ]
701
+ };
702
+ }
703
+ return {
704
+ type: 'LineString',
705
+ coordinates: this.toArrayOfCoords(format)
706
+ };
626
707
  }
627
708
  divideToPieces(piecesCount, withAltitude = false) {
628
709
  const { fullLength } = this;
@@ -54,13 +54,34 @@ var DPoint = (function () {
54
54
  DPoint.zero = function () {
55
55
  return new DPoint();
56
56
  };
57
- DPoint.parse = function (c) {
58
- var _a = c, lat = _a.lat, lng = _a.lng;
57
+ DPoint.parse = function (c, format) {
58
+ if (format === void 0) { format = 'xyz'; }
59
+ var _a = c, lat = _a.lat, lon = _a.lon, _b = _a.lng, lng = _b === void 0 ? lon : _b, alt = _a.alt;
59
60
  if (lat && lng) {
60
- return new DPoint(lat, lng, 0);
61
+ return new DPoint(lat, lng, alt !== null && alt !== void 0 ? alt : 0);
61
62
  }
62
- var _b = __read(c, 3), x = _b[0], y = _b[1], z = _b[2];
63
- return new DPoint(x, y, z);
63
+ var t = c;
64
+ if (c.type === 'Point') {
65
+ t = c.coordinates;
66
+ }
67
+ return format.replace(/[^x-z]/gmiu, '')
68
+ .split('')
69
+ .reduce(function (a, k, index) {
70
+ var _a, _b;
71
+ switch (k) {
72
+ case 'x':
73
+ a.x = (_a = t[index]) !== null && _a !== void 0 ? _a : 0;
74
+ break;
75
+ case 'y':
76
+ a.y = (_b = t[index]) !== null && _b !== void 0 ? _b : 0;
77
+ break;
78
+ case 'z':
79
+ a.z = t[index];
80
+ break;
81
+ default:
82
+ }
83
+ return a;
84
+ }, new DPoint());
64
85
  };
65
86
  DPoint.parseFromWKT = function (wkt) {
66
87
  var regexp = /POINT \((?<data>(?:(?!\)).)*?)\)$/miu;
@@ -72,6 +93,29 @@ var DPoint = (function () {
72
93
  DPoint.random = function () {
73
94
  return new DPoint(Math.random(), Math.random());
74
95
  };
96
+ DPoint.getTileFromQuadKey = function (quadKey) {
97
+ var p = new DPoint(0, 0, quadKey.length);
98
+ for (var i = p.z; i > 0; i--) {
99
+ var mask = 1 << (i - 1);
100
+ switch (quadKey[p.z - i]) {
101
+ case '0':
102
+ break;
103
+ case '1':
104
+ p.x |= mask;
105
+ break;
106
+ case '2':
107
+ p.y |= mask;
108
+ break;
109
+ case '3':
110
+ p.x |= mask;
111
+ p.y |= mask;
112
+ break;
113
+ default:
114
+ throw new Error('Invalid QuadKey digit sequence.');
115
+ }
116
+ }
117
+ return p;
118
+ };
75
119
  DPoint.prototype.getTileFromCoords = function (zoom) {
76
120
  if (zoom === void 0) { zoom = this.z; }
77
121
  checkFunction('getTileFromCoords')
@@ -81,6 +125,23 @@ var DPoint = (function () {
81
125
  var y = Math.floor((1 - Math.log(Math.tan(this.y * PI_TO_DEGREE) + 1 / Math.cos(this.y * PI_TO_DEGREE)) / Math.PI) / 2 * (Math.pow(2, zoom)));
82
126
  return new DPoint(x, y, zoom);
83
127
  };
128
+ DPoint.prototype.getQuadKeyFromTile = function (zoom) {
129
+ if (zoom === void 0) { zoom = this.z; }
130
+ var quadKey = [];
131
+ for (var i = zoom; i > 0; i--) {
132
+ var digit = 0;
133
+ var mask = 1 << (i - 1);
134
+ if ((this.x & mask) !== 0) {
135
+ digit++;
136
+ }
137
+ if ((this.y & mask) !== 0) {
138
+ digit++;
139
+ digit++;
140
+ }
141
+ quadKey.push(digit);
142
+ }
143
+ return quadKey.join('');
144
+ };
84
145
  DPoint.prototype.getCoordsFromTile = function (zoom) {
85
146
  if (zoom === void 0) { zoom = this.z; }
86
147
  checkFunction('getCoordsFromTile')
@@ -91,11 +152,23 @@ var DPoint = (function () {
91
152
  var y = PI_IN_DEGREE / Math.PI * Math.atan((Math.exp(n) - Math.exp(-n)) / 2);
92
153
  return new DPoint(x, y, zoom);
93
154
  };
94
- DPoint.prototype.toCoords = function () {
95
- if (this.z === undefined) {
96
- return [this.x, this.y];
97
- }
98
- return [this.x, this.y, this.z];
155
+ DPoint.prototype.toCoords = function (format) {
156
+ var _this = this;
157
+ if (format === void 0) { format = 'xyz'; }
158
+ return format.replace(/[^x-z]/gmiu, '').split('')
159
+ .map(function (k) { return ({
160
+ x: _this.x,
161
+ y: _this.y,
162
+ z: _this.z
163
+ })[k]; })
164
+ .filter(function (r) { return r !== undefined; });
165
+ };
166
+ DPoint.prototype.toGeoJSON = function (format) {
167
+ if (format === void 0) { format = 'xyz'; }
168
+ return {
169
+ type: 'Point',
170
+ coordinates: this.toCoords(format)
171
+ };
99
172
  };
100
173
  DPoint.prototype.findLine = function (p) {
101
174
  checkFunction('findLine')