@gotamedia/shapeshifter
Advanced tools
| /* | ||
| * Copyright 2020 Gota Media AB | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
| const AbstractGeometry = require("./AbstractGeometry.js"); | ||
| /** | ||
| * Represents a multi point geometry object. | ||
| * | ||
| * @memberof shapeProvider.openContent | ||
| * @extends shapeProvider.openContent.AbstractGeometry | ||
| */ | ||
| class MultiPoint extends AbstractGeometry { | ||
| /** | ||
| * Create a new multi point instance. | ||
| * | ||
| * @public | ||
| * @param {...Array.<Point>} points The points making up the multi point. | ||
| */ | ||
| constructor(...points) { | ||
| super(); | ||
| this.points = points; | ||
| } | ||
| /** | ||
| * @public | ||
| * @return {Array.<Array.<number>>} | ||
| */ | ||
| get coordinates() { | ||
| return this.points.map(point => point.coordinates); | ||
| } | ||
| } | ||
| module.exports = MultiPoint; |
@@ -8,2 +8,3 @@ /* | ||
| const MultiPoint = require("./MultiPoint.js"); | ||
| const MultiPolygon = require("./MultiPolygon.js"); | ||
@@ -42,2 +43,6 @@ const Point = require("./Point.js"); | ||
| if (geometryString.match(/^MULTIPOINT/)) { | ||
| return this.createMultiPoint(geometryString); | ||
| } | ||
| throw new Error("Could not find a suitable geometry implementation to use"); | ||
@@ -55,3 +60,3 @@ } | ||
| return new Point( | ||
| ...geometryString.match(/POINT ?\(([0-9]+\.[0-9]+) ([0-9]+\.[0-9]+)\)/) | ||
| ...geometryString.match(/POINT ?\(([-]?[0-9]+\.[0-9]+) ([-]?[0-9]+\.[0-9]+)\)/) | ||
| .splice(1) | ||
@@ -122,4 +127,33 @@ .map(parseFloat) | ||
| } | ||
| /** | ||
| * Create a {@link shapeProvider.openContent.MultiPoint} geometry instance. | ||
| * | ||
| * @private | ||
| * @param {string} geometryString A WKT representation of the geometry. | ||
| * @return {shapeProvider.openContent.MultiPoint} A multi point implementation of {@link shapeProvider.openContent.AbstractGeometry}. | ||
| */ | ||
| createMultiPoint(geometryString) { | ||
| return new MultiPoint( | ||
| ...geometryString | ||
| // Selects everything within the first set of parentheses. | ||
| .replace(/.*?\((.+)\).*?/, (_, coordinates) => coordinates) | ||
| .trim() | ||
| // Removes the surrounding parentheses. | ||
| .replace(/^\(|\)$/g, "") | ||
| // Trims any white space between the coordinate groups. | ||
| .replace(/\) ?, ?\(/g, "),(") | ||
| .split("),(") | ||
| .map(coordinates => new Point( | ||
| ...coordinates.split(" ") | ||
| .map(parseFloat) | ||
| )) | ||
| ); | ||
| } | ||
| } | ||
| module.exports = GeometryFactory; |
@@ -250,13 +250,7 @@ /* | ||
| */ | ||
| get section() { | ||
| const link = this.selectSingle("//newsml:link[@rel='subject' and @type='x-im/section']"); | ||
| if (!link) { | ||
| return; | ||
| } | ||
| return { | ||
| id: link.getAttribute("uuid"), | ||
| name: link.getAttribute("title") | ||
| }; | ||
| get sections() { | ||
| return this.select("//newsml:link[@rel='subject' and @type='x-im/section']").map(element => ({ | ||
| id: element.getAttribute("uuid"), | ||
| name: element.getAttribute("title") | ||
| })); | ||
| } | ||
@@ -269,11 +263,15 @@ | ||
| get places() { | ||
| return this.select("//newsml:link[@rel='subject' and @type='x-im/place'][*]").map(element => ({ | ||
| id: element.getAttribute("uuid"), | ||
| name: element.getAttribute("title"), | ||
| geometry: this.createGeometry(this.selectValue(".//newsml:geometry/text()", undefined, element)), | ||
| links: this.select(".//newsml:link[@rel='subject' and @type='x-im/place']", element).map(element => ({ | ||
| return this.select("//newsml:link[@rel='subject' and @type='x-im/place']").map(element => { | ||
| const geometry = this.selectValue(".//newsml:geometry/text()", undefined, element); | ||
| return { | ||
| id: element.getAttribute("uuid"), | ||
| name: element.getAttribute("title") | ||
| })) | ||
| })); | ||
| name: element.getAttribute("title"), | ||
| geometry: geometry ? this.createGeometry(geometry) : null, | ||
| links: this.select(".//newsml:link[@rel='subject' and @type='x-im/place']", element).map(element => ({ | ||
| id: element.getAttribute("uuid"), | ||
| name: element.getAttribute("title") | ||
| })) | ||
| }; | ||
| }); | ||
| } | ||
@@ -280,0 +278,0 @@ |
+1
-1
| { | ||
| "name": "@gotamedia/shapeshifter", | ||
| "version": "0.4.3", | ||
| "version": "0.5.0", | ||
| "description": "Shapeshifter is a general format converter. It converts strings of one format (shape) into a string of another.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
46204
4.82%27
3.85%1395
4.49%