Node JS version
This commit is contained in:
133
node_modules/cassandra-driver/lib/geometry/geometry.js
generated
vendored
Normal file
133
node_modules/cassandra-driver/lib/geometry/geometry.js
generated
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright DataStax, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const endianness = {
|
||||
'0': 'BE',
|
||||
'1': 'LE'
|
||||
};
|
||||
|
||||
function Geometry() {
|
||||
|
||||
}
|
||||
|
||||
Geometry.types = {
|
||||
Point2D: 1,
|
||||
LineString: 2,
|
||||
Polygon: 3
|
||||
};
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @param {Number} code
|
||||
* @returns {String}
|
||||
* @ignore
|
||||
*/
|
||||
Geometry.getEndianness = function (code) {
|
||||
const value = endianness[code.toString()];
|
||||
if (typeof value === 'undefined') {
|
||||
throw new TypeError('Invalid endianness with code ' + code);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads an int32 from binary representation based on endianness.
|
||||
* @protected
|
||||
* @param {Buffer} buffer
|
||||
* @param {String} endianness
|
||||
* @param {Number} offset
|
||||
* @returns Number
|
||||
* @ignore
|
||||
*/
|
||||
Geometry.readInt32 = function (buffer, endianness, offset) {
|
||||
if (endianness === 'BE') {
|
||||
return buffer.readInt32BE(offset, true);
|
||||
}
|
||||
return buffer.readInt32LE(offset, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads an 64-bit double from binary representation based on endianness.
|
||||
* @protected
|
||||
* @param {Buffer} buffer
|
||||
* @param {String} endianness
|
||||
* @param {Number} offset
|
||||
* @returns Number
|
||||
* @ignore
|
||||
*/
|
||||
Geometry.readDouble = function (buffer, endianness, offset) {
|
||||
if (endianness === 'BE') {
|
||||
return buffer.readDoubleBE(offset, true);
|
||||
}
|
||||
return buffer.readDoubleLE(offset, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes an 32-bit integer to binary representation based on OS endianness.
|
||||
* @protected
|
||||
* @param {Number} val
|
||||
* @param {Buffer} buffer
|
||||
* @param {Number} offset
|
||||
* @ignore
|
||||
*/
|
||||
Geometry.prototype.writeInt32 = function (val, buffer, offset) {
|
||||
if (this.useBESerialization()) {
|
||||
return buffer.writeInt32BE(val, offset, true);
|
||||
}
|
||||
return buffer.writeInt32LE(val, offset, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes an 64-bit double to binary representation based on OS endianness.
|
||||
* @protected
|
||||
* @param {Number} val
|
||||
* @param {Buffer} buffer
|
||||
* @param {Number} offset
|
||||
* @ignore
|
||||
*/
|
||||
Geometry.prototype.writeDouble = function (val, buffer, offset) {
|
||||
if (this.useBESerialization()) {
|
||||
return buffer.writeDoubleBE(val, offset, true);
|
||||
}
|
||||
return buffer.writeDoubleLE(val, offset, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes an 8-bit int that represents the OS endianness.
|
||||
* @protected
|
||||
* @param {Buffer} buffer
|
||||
* @param {Number} offset
|
||||
* @ignore
|
||||
*/
|
||||
Geometry.prototype.writeEndianness = function (buffer, offset) {
|
||||
if (this.useBESerialization()) {
|
||||
return buffer.writeInt8(0, offset, true);
|
||||
}
|
||||
return buffer.writeInt8(1, offset, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns true if the serialization must be done in big-endian format.
|
||||
* Designed to allow injection of OS endianness.
|
||||
* @abstract
|
||||
* @ignore
|
||||
*/
|
||||
Geometry.prototype.useBESerialization = function () {
|
||||
throw new Error('Not Implemented');
|
||||
};
|
||||
|
||||
module.exports = Geometry;
|
67
node_modules/cassandra-driver/lib/geometry/index.d.ts
generated
vendored
Normal file
67
node_modules/cassandra-driver/lib/geometry/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright DataStax, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export namespace geometry {
|
||||
class LineString {
|
||||
constructor(...args: Point[]);
|
||||
|
||||
static fromBuffer(buffer: Buffer): LineString;
|
||||
|
||||
static fromString(textValue: string): LineString;
|
||||
|
||||
equals(other: LineString): boolean;
|
||||
|
||||
toBuffer(): Buffer;
|
||||
|
||||
toJSON(): string;
|
||||
|
||||
toString(): string;
|
||||
|
||||
}
|
||||
|
||||
class Point {
|
||||
constructor(x: number, y: number);
|
||||
|
||||
static fromBuffer(buffer: Buffer): Point;
|
||||
|
||||
static fromString(textValue: string): Point;
|
||||
|
||||
equals(other: Point): boolean;
|
||||
|
||||
toBuffer(): Buffer;
|
||||
|
||||
toJSON(): string;
|
||||
|
||||
toString(): string;
|
||||
|
||||
}
|
||||
|
||||
class Polygon {
|
||||
constructor(...args: Point[]);
|
||||
|
||||
static fromBuffer(buffer: Buffer): Polygon;
|
||||
|
||||
static fromString(textValue: string): Polygon;
|
||||
|
||||
equals(other: Polygon): boolean;
|
||||
|
||||
toBuffer(): Buffer;
|
||||
|
||||
toJSON(): string;
|
||||
|
||||
toString(): string;
|
||||
}
|
||||
}
|
30
node_modules/cassandra-driver/lib/geometry/index.js
generated
vendored
Normal file
30
node_modules/cassandra-driver/lib/geometry/index.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright DataStax, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Geometry module.
|
||||
* <p>
|
||||
* Contains the classes to represent the set of additional CQL types for geospatial data that come with
|
||||
* DSE 5.0.
|
||||
* </p>
|
||||
* @module geometry
|
||||
*/
|
||||
|
||||
exports.Geometry = require('./geometry');
|
||||
exports.LineString = require('./line-string');
|
||||
exports.Point = require('./point');
|
||||
exports.Polygon = require('./polygon');
|
197
node_modules/cassandra-driver/lib/geometry/line-string.js
generated
vendored
Normal file
197
node_modules/cassandra-driver/lib/geometry/line-string.js
generated
vendored
Normal file
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright DataStax, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
'use strict';
|
||||
const util = require('util');
|
||||
const utils = require('../utils');
|
||||
const Geometry = require('./geometry');
|
||||
const Point = require('./point');
|
||||
|
||||
/**
|
||||
* Creates a new {@link LineString} instance.
|
||||
* @classdesc
|
||||
* A LineString is a one-dimensional object representing a sequence of points and the line segments connecting them.
|
||||
* @param {...Point}[point] A sequence of [Point]{@link module:geometry~Point} items as arguments.
|
||||
* @example
|
||||
* new LineString(new Point(10.99, 20.02), new Point(14, 26), new Point(34, 1.2));
|
||||
* @constructor
|
||||
* @alias module:geometry~LineString
|
||||
* @extends {Geometry}
|
||||
*/
|
||||
function LineString(point) {
|
||||
let points = Array.prototype.slice.call(arguments);
|
||||
if (points.length === 1 && Array.isArray(points) && Array.isArray(points[0])) {
|
||||
//The first argument is an array of the points
|
||||
points = points[0];
|
||||
}
|
||||
if (points.length === 1) {
|
||||
throw new TypeError('LineString can be either empty or contain 2 or more points');
|
||||
}
|
||||
/**
|
||||
* Returns a frozen Array of points that represent the line.
|
||||
* @type {Array.<Point>}
|
||||
*/
|
||||
this.points = Object.freeze(points);
|
||||
}
|
||||
|
||||
//noinspection JSCheckFunctionSignatures
|
||||
util.inherits(LineString, Geometry);
|
||||
|
||||
/**
|
||||
* Creates a {@link LineString} instance from
|
||||
* a <a href="https://en.wikipedia.org/wiki/Well-known_text">Well-known Text (WKT)</a>
|
||||
* representation of a line.
|
||||
* @param {Buffer} buffer
|
||||
* @returns {LineString}
|
||||
*/
|
||||
LineString.fromBuffer = function (buffer) {
|
||||
if (!buffer || buffer.length < 9) {
|
||||
throw new TypeError('A linestring buffer should contain at least 9 bytes');
|
||||
}
|
||||
const endianness = Geometry.getEndianness(buffer.readInt8(0, true));
|
||||
let offset = 1;
|
||||
if (Geometry.readInt32(buffer, endianness, offset) !== Geometry.types.LineString) {
|
||||
throw new TypeError('Binary representation was not a LineString');
|
||||
}
|
||||
offset += 4;
|
||||
const length = Geometry.readInt32(buffer, endianness, offset);
|
||||
offset += 4;
|
||||
if (buffer.length !== offset + length * 16) {
|
||||
throw new TypeError(util.format('Length of the buffer does not match %d !== %d', buffer.length, offset + length * 8));
|
||||
}
|
||||
const points = new Array(length);
|
||||
for (let i = 0; i < length; i++) {
|
||||
points[i] = new Point(
|
||||
Geometry.readDouble(buffer, endianness, offset),
|
||||
Geometry.readDouble(buffer, endianness, offset + 8));
|
||||
offset += 16;
|
||||
}
|
||||
//noinspection JSCheckFunctionSignatures
|
||||
return new LineString(points);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a {@link LineString} instance from
|
||||
* a <a href="https://en.wikipedia.org/wiki/Well-known_text">Well-known Text (WKT)</a>
|
||||
* representation of a line.
|
||||
* @param {String} textValue
|
||||
* @returns {LineString}
|
||||
*/
|
||||
LineString.fromString = function (textValue) {
|
||||
const wktRegex = /^LINESTRING ?\(([-0-9. ,]+)\)+$/g;
|
||||
const matches = wktRegex.exec(textValue);
|
||||
if (!matches || matches.length !== 2) {
|
||||
throw new TypeError('Invalid WKT: ' + textValue);
|
||||
}
|
||||
const points = LineString.parseSegments(matches[1]);
|
||||
return new LineString(points);
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal method that parses a series of WKT points.
|
||||
* @param {String} textValue
|
||||
* @returns {Array<Point>}
|
||||
* @internal
|
||||
* @ignore
|
||||
*/
|
||||
LineString.parseSegments = function (textValue) {
|
||||
const points = [];
|
||||
const pointParts = textValue.split(',');
|
||||
for (let i = 0; i < pointParts.length; i++) {
|
||||
const p = pointParts[i].trim();
|
||||
if (p.length === 0) {
|
||||
throw new TypeError('Invalid WKT segment: ' + textValue);
|
||||
}
|
||||
const xyText = p.split(' ').filter(function (element) {
|
||||
return (element.trim().length > 0);
|
||||
});
|
||||
if (xyText.length !== 2) {
|
||||
throw new TypeError('Invalid WKT segment: ' + textValue);
|
||||
}
|
||||
points.push(new Point(parseFloat(xyText[0]), parseFloat(xyText[1])));
|
||||
}
|
||||
return points;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a <a href="https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary">Well-known Binary</a> (WKB)
|
||||
* representation of this instance.
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
LineString.prototype.toBuffer = function () {
|
||||
const buffer = utils.allocBufferUnsafe(9 + this.points.length * 16);
|
||||
this.writeEndianness(buffer, 0);
|
||||
let offset = 1;
|
||||
this.writeInt32(Geometry.types.LineString, buffer, offset);
|
||||
offset += 4;
|
||||
this.writeInt32(this.points.length, buffer, offset);
|
||||
offset += 4;
|
||||
this.points.forEach(function (p) {
|
||||
this.writeDouble(p.x, buffer, offset);
|
||||
this.writeDouble(p.y, buffer, offset + 8);
|
||||
offset += 16;
|
||||
}, this);
|
||||
return buffer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns true if the values of the linestrings are the same, otherwise it returns false.
|
||||
* @param {LineString} other
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
LineString.prototype.equals = function (other) {
|
||||
if (!(other instanceof LineString)) {
|
||||
return false;
|
||||
}
|
||||
if (this.points.length !== other.points.length) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < this.points.length; i++) {
|
||||
if (!this.points[i].equals(other.points[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns Well-known text (WKT) representation of the geometry object.
|
||||
* @returns {String}
|
||||
*/
|
||||
LineString.prototype.toString = function () {
|
||||
if (this.points.length === 0) {
|
||||
return 'LINESTRING EMPTY';
|
||||
}
|
||||
return 'LINESTRING ('
|
||||
+ this.points.map(function (p) {
|
||||
return p.x + ' ' + p.y;
|
||||
}).join(', ')
|
||||
+ ')';
|
||||
};
|
||||
|
||||
LineString.prototype.useBESerialization = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a JSON representation of this geo-spatial type.
|
||||
*/
|
||||
LineString.prototype.toJSON = function () {
|
||||
return { type: 'LineString', coordinates: this.points.map(function (p) {
|
||||
return [p.x, p.y];
|
||||
})};
|
||||
};
|
||||
|
||||
module.exports = LineString;
|
134
node_modules/cassandra-driver/lib/geometry/point.js
generated
vendored
Normal file
134
node_modules/cassandra-driver/lib/geometry/point.js
generated
vendored
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright DataStax, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
'use strict';
|
||||
const util = require('util');
|
||||
const utils = require('../utils');
|
||||
const Geometry = require('./geometry');
|
||||
|
||||
/**
|
||||
* Creates a new {@link Point} instance.
|
||||
* @classdesc
|
||||
* A Point is a zero-dimensional object that represents a specific (X,Y)
|
||||
* location in a two-dimensional XY-Plane. In case of Geographic Coordinate
|
||||
* Systems, the X coordinate is the longitude and the Y is the latitude.
|
||||
* @param {Number} x The X coordinate.
|
||||
* @param {Number} y The Y coordinate.
|
||||
* @extends {Geometry}
|
||||
* @alias module:geometry~Point
|
||||
* @constructor
|
||||
*/
|
||||
function Point(x, y) {
|
||||
if (typeof x !== 'number' || typeof y !== 'number') {
|
||||
throw new TypeError('X and Y must be numbers');
|
||||
}
|
||||
if (isNaN(x) || isNaN(y)) {
|
||||
throw new TypeError('X and Y must be numbers');
|
||||
}
|
||||
/**
|
||||
* Returns the X coordinate of this 2D point.
|
||||
* @type {Number}
|
||||
*/
|
||||
this.x = x;
|
||||
/**
|
||||
* Returns the Y coordinate of this 2D point.
|
||||
* @type {Number}
|
||||
*/
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
//noinspection JSCheckFunctionSignatures
|
||||
util.inherits(Point, Geometry);
|
||||
|
||||
/**
|
||||
* Creates a {@link Point} instance from
|
||||
* a <a href="https://en.wikipedia.org/wiki/Well-known_text">Well-known Text (WKT)</a>
|
||||
* representation of a 2D point.
|
||||
* @param {Buffer} buffer
|
||||
* @returns {Point}
|
||||
*/
|
||||
Point.fromBuffer = function (buffer) {
|
||||
if (!buffer || buffer.length !== 21) {
|
||||
throw new TypeError('2D Point buffer should contain 21 bytes');
|
||||
}
|
||||
const endianness = Geometry.getEndianness(buffer.readInt8(0, true));
|
||||
if (Geometry.readInt32(buffer, endianness, 1) !== Geometry.types.Point2D) {
|
||||
throw new TypeError('Binary representation was not a point');
|
||||
}
|
||||
return new Point(Geometry.readDouble(buffer, endianness, 5), Geometry.readDouble(buffer, endianness, 13));
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a {@link Point} instance from
|
||||
* a <a href="https://en.wikipedia.org/wiki/Well-known_text">Well-known Text (WKT)</a>
|
||||
* representation of a 2D point.
|
||||
* @param {String} textValue
|
||||
* @returns {Point}
|
||||
*/
|
||||
Point.fromString = function (textValue) {
|
||||
const wktRegex = /^POINT\s?\(([-0-9.]+) ([-0-9.]+)\)$/g;
|
||||
const matches = wktRegex.exec(textValue);
|
||||
if (!matches || matches.length !== 3) {
|
||||
throw new TypeError('2D Point WTK should contain 2 coordinates');
|
||||
}
|
||||
return new Point(parseFloat(matches[1]), parseFloat(matches[2]));
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a <a href="https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary">Well-known Binary</a> (WKB)
|
||||
* representation of this instance.
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
Point.prototype.toBuffer = function () {
|
||||
const buffer = utils.allocBufferUnsafe(21);
|
||||
this.writeEndianness(buffer, 0);
|
||||
this.writeInt32(Geometry.types.Point2D, buffer, 1);
|
||||
this.writeDouble(this.x, buffer, 5);
|
||||
this.writeDouble(this.y, buffer, 13);
|
||||
return buffer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns true if the values of the point are the same, otherwise it returns false.
|
||||
* @param {Point} other
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
Point.prototype.equals = function (other) {
|
||||
if (!(other instanceof Point)) {
|
||||
return false;
|
||||
}
|
||||
return (this.x === other.x && this.y === other.y);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns Well-known text (WKT) representation of the geometry object.
|
||||
* @returns {String}
|
||||
*/
|
||||
Point.prototype.toString = function () {
|
||||
return util.format('POINT (%d %d)', this.x, this.y);
|
||||
};
|
||||
|
||||
Point.prototype.useBESerialization = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a JSON representation of this geo-spatial type.
|
||||
*/
|
||||
Point.prototype.toJSON = function () {
|
||||
return { type: 'Point', coordinates: [ this.x, this.y ]};
|
||||
};
|
||||
|
||||
module.exports = Point;
|
239
node_modules/cassandra-driver/lib/geometry/polygon.js
generated
vendored
Normal file
239
node_modules/cassandra-driver/lib/geometry/polygon.js
generated
vendored
Normal file
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* Copyright DataStax, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
'use strict';
|
||||
const util = require('util');
|
||||
const utils = require('../utils');
|
||||
const Geometry = require('./geometry');
|
||||
const Point = require('./point');
|
||||
const LineString = require('./line-string');
|
||||
|
||||
/**
|
||||
* Creates a new {@link Polygon} instance.
|
||||
* @classdesc
|
||||
* Represents is a plane geometry figure that is bounded by a finite chain of straight line segments closing in a loop
|
||||
* to form a closed chain or circuit.
|
||||
* @param {...Array.<Point>}[ringPoints] A sequence of Array of [Point]{@link module:geometry~Point} items as arguments
|
||||
* representing the rings of the polygon.
|
||||
* @example
|
||||
* new Polygon([ new Point(30, 10), new Point(40, 40), new Point(10, 20), new Point(30, 10) ]);
|
||||
* @example
|
||||
* //polygon with a hole
|
||||
* new Polygon(
|
||||
* [ new Point(30, 10), new Point(40, 40), new Point(10, 20), new Point(30, 10) ],
|
||||
* [ new Point(25, 20), new Point(30, 30), new Point(20, 20), new Point(25, 20) ]
|
||||
* );
|
||||
* @alias module:geometry~Polygon
|
||||
* @constructor
|
||||
*/
|
||||
function Polygon(ringPoints) {
|
||||
const rings = Array.prototype.slice.call(arguments);
|
||||
/**
|
||||
* Returns a frozen Array of array of points that represent the different rings in the polygon.
|
||||
* @type {Array}
|
||||
*/
|
||||
this.rings = Object.freeze(rings);
|
||||
}
|
||||
|
||||
//noinspection JSCheckFunctionSignatures
|
||||
util.inherits(Polygon, Geometry);
|
||||
|
||||
/**
|
||||
* Creates a {@link Polygon} instance from
|
||||
* a <a href="https://en.wikipedia.org/wiki/Well-known_text">Well-known Text (WKT)</a>
|
||||
* representation of a polygon.
|
||||
* @param {Buffer} buffer
|
||||
* @returns {Polygon}
|
||||
*/
|
||||
Polygon.fromBuffer = function (buffer) {
|
||||
if (!buffer || buffer.length < 9) {
|
||||
throw new TypeError('A Polygon buffer should contain at least 9 bytes');
|
||||
}
|
||||
const endianness = Geometry.getEndianness(buffer.readInt8(0, true));
|
||||
let offset = 1;
|
||||
if (Geometry.readInt32(buffer, endianness, offset) !== Geometry.types.Polygon) {
|
||||
throw new TypeError('Binary representation was not a Polygon');
|
||||
}
|
||||
offset += 4;
|
||||
const ringsLength = Geometry.readInt32(buffer, endianness, offset);
|
||||
offset += 4;
|
||||
const ringsArray = new Array(ringsLength);
|
||||
for (let ringIndex = 0; ringIndex < ringsLength; ringIndex++) {
|
||||
const pointsLength = Geometry.readInt32(buffer, endianness, offset);
|
||||
offset += 4;
|
||||
if (buffer.length < offset + pointsLength * 16) {
|
||||
throw new TypeError(util.format('Length of the buffer does not match'));
|
||||
}
|
||||
const ring = new Array(pointsLength);
|
||||
for (let i = 0; i < pointsLength; i++) {
|
||||
ring[i] = new Point(
|
||||
Geometry.readDouble(buffer, endianness, offset),
|
||||
Geometry.readDouble(buffer, endianness, offset + 8));
|
||||
offset += 16;
|
||||
}
|
||||
ringsArray[ringIndex] = ring;
|
||||
}
|
||||
//Invoke the constructor with each ring as a parameter
|
||||
//ringsArray.unshift(null);
|
||||
//return new (Function.prototype.bind.apply(Polygon, ringsArray));
|
||||
return construct(ringsArray);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a {@link Polygon} instance from
|
||||
* a <a href="https://en.wikipedia.org/wiki/Well-known_text">Well-known Text (WKT)</a>
|
||||
* representation of a shape.
|
||||
* @param {String} textValue
|
||||
* @returns {Polygon}
|
||||
*/
|
||||
Polygon.fromString = function (textValue) {
|
||||
const wktRegex = /^POLYGON ?\((\(.*\))\)$/g;
|
||||
const matches = wktRegex.exec(textValue);
|
||||
function validateWkt(condition) {
|
||||
if (condition) {
|
||||
throw new TypeError('Invalid WKT: ' + textValue);
|
||||
}
|
||||
}
|
||||
validateWkt(!matches || matches.length !== 2);
|
||||
|
||||
const ringsText = matches[1];
|
||||
const ringsArray = [];
|
||||
let ringStart = null;
|
||||
for (let i = 0; i < ringsText.length; i++) {
|
||||
const c = ringsText[i];
|
||||
if (c === '(') {
|
||||
validateWkt(ringStart !== null);
|
||||
ringStart = i+1;
|
||||
continue;
|
||||
}
|
||||
if (c === ')') {
|
||||
validateWkt(ringStart === null);
|
||||
ringsArray.push(ringsText.substring(ringStart, i));
|
||||
ringStart = null;
|
||||
continue;
|
||||
}
|
||||
validateWkt(ringStart === null && c !== ' ' && c !== ',');
|
||||
}
|
||||
return construct(ringsArray.map(LineString.parseSegments));
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new instance of Polygon with each array item as a parameter
|
||||
* @private
|
||||
* @param {Array<Array<Point>>} argsArray
|
||||
* @returns {Polygon}
|
||||
*/
|
||||
function construct(argsArray) {
|
||||
function F() {
|
||||
return Polygon.apply(this, argsArray);
|
||||
}
|
||||
F.prototype = Polygon.prototype;
|
||||
return new F();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a <a href="https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary">Well-known Binary</a> (WKB)
|
||||
* representation of this instance.
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
Polygon.prototype.toBuffer = function () {
|
||||
let totalRingsLength = 0;
|
||||
this.rings.forEach(function (ring) {
|
||||
totalRingsLength += 4 + ring.length * 16;
|
||||
}, this);
|
||||
const buffer = utils.allocBufferUnsafe(9 + totalRingsLength);
|
||||
this.writeEndianness(buffer, 0);
|
||||
let offset = 1;
|
||||
this.writeInt32(Geometry.types.Polygon, buffer, offset);
|
||||
offset += 4;
|
||||
this.writeInt32(this.rings.length, buffer, offset);
|
||||
offset += 4;
|
||||
this.rings.forEach(function (ring) {
|
||||
this.writeInt32(ring.length, buffer, offset);
|
||||
offset += 4;
|
||||
ring.forEach(function (p) {
|
||||
this.writeDouble(p.x, buffer, offset);
|
||||
this.writeDouble(p.y, buffer, offset + 8);
|
||||
offset += 16;
|
||||
}, this);
|
||||
}, this);
|
||||
return buffer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns true if the values of the polygons are the same, otherwise it returns false.
|
||||
* @param {Polygon} other
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
Polygon.prototype.equals = function (other) {
|
||||
if (!(other instanceof Polygon)) {
|
||||
return false;
|
||||
}
|
||||
if (this.rings.length !== other.rings.length) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < this.rings.length; i++) {
|
||||
const r1 = this.rings[i];
|
||||
const r2 = other.rings[i];
|
||||
if (r1.length !== r2.length) {
|
||||
return false;
|
||||
}
|
||||
for (let j = 0; j < r1.length; j++) {
|
||||
if (!r1[i].equals(r2[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
Polygon.prototype.useBESerialization = function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns Well-known text (WKT) representation of the geometry object.
|
||||
* @returns {String}
|
||||
*/
|
||||
Polygon.prototype.toString = function () {
|
||||
if (this.rings.length === 0) {
|
||||
return 'POLYGON EMPTY';
|
||||
}
|
||||
let ringStrings = '';
|
||||
this.rings.forEach(function (r, i) {
|
||||
if (i > 0) {
|
||||
ringStrings += ', ';
|
||||
}
|
||||
ringStrings += '(' +
|
||||
r.map(function (p) {
|
||||
return p.x + ' ' + p.y;
|
||||
}).join(', ')
|
||||
+ ')';
|
||||
});
|
||||
return 'POLYGON (' + ringStrings + ')';
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a JSON representation of this geo-spatial type.
|
||||
*/
|
||||
Polygon.prototype.toJSON = function () {
|
||||
return { type: 'Polygon', coordinates: this.rings.map(function (r) {
|
||||
return r.map(function (p) {
|
||||
return [ p.x, p.y ];
|
||||
});
|
||||
})};
|
||||
};
|
||||
|
||||
module.exports = Polygon;
|
Reference in New Issue
Block a user