From abc32797525e995de21de76173b84f1070c0d03a Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 11 Sep 2018 08:10:18 +0200 Subject: [PATCH 1/2] Use TopoJSON types from @types/topojson --- package.json | 1 + src/ol/format/TopoJSON.js | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index bfb0df8f3c..05c93f50ff 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "devDependencies": { "@openlayers/eslint-plugin": "^4.0.0-beta.1", "@types/geojson": "^7946.0.4", + "@types/topojson-specification": "^1.0.0", "buble": "^0.19.3", "buble-loader": "^0.5.1", "chaikin-smooth": "^1.0.4", diff --git a/src/ol/format/TopoJSON.js b/src/ol/format/TopoJSON.js index 5466a99592..cc5639844e 100644 --- a/src/ol/format/TopoJSON.js +++ b/src/ol/format/TopoJSON.js @@ -12,6 +12,17 @@ import Point from '../geom/Point.js'; import Polygon from '../geom/Polygon.js'; import {get as getProjection} from '../proj.js'; +/** + * @typedef {import("topojson-specification").Topology} TopoJSONTopology + * @typedef {import("topojson-specification").GeometryCollection} TopoJSONGeometryCollection + * @typedef {import("topojson-specification").GeometryObject} TopoJSONGeometry + * @typedef {import("topojson-specification").Point} TopoJSONPoint + * @typedef {import("topojson-specification").MultiPoint} TopoJSONMultiPoint + * @typedef {import("topojson-specification").LineString} TopoJSONLineString + * @typedef {import("topojson-specification").MultiLineString} TopoJSONMultiLineString + * @typedef {import("topojson-specification").Polygon} TopoJSONPolygon + * @typedef {import("topojson-specification").MultiPolygon} TopoJSONMultiPolygon + */ /** * @typedef {Object} Options @@ -178,7 +189,7 @@ function concatenateArcs(indices, arcs) { /** * Create a point from a TopoJSON geometry object. * - * @param {TopoJSONGeometry} object TopoJSON object. + * @param {TopoJSONPoint} object TopoJSON object. * @param {Array} scale Scale for each dimension. * @param {Array} translate Translation for each dimension. * @return {Point} Geometry. @@ -195,7 +206,7 @@ function readPointGeometry(object, scale, translate) { /** * Create a multi-point from a TopoJSON geometry object. * - * @param {TopoJSONGeometry} object TopoJSON object. + * @param {TopoJSONMultiPoint} object TopoJSON object. * @param {Array} scale Scale for each dimension. * @param {Array} translate Translation for each dimension. * @return {MultiPoint} Geometry. @@ -214,7 +225,7 @@ function readMultiPointGeometry(object, scale, translate) { /** * Create a linestring from a TopoJSON geometry object. * - * @param {TopoJSONGeometry} object TopoJSON object. + * @param {TopoJSONLineString} object TopoJSON object. * @param {Array>} arcs Array of arcs. * @return {LineString} Geometry. */ @@ -227,7 +238,7 @@ function readLineStringGeometry(object, arcs) { /** * Create a multi-linestring from a TopoJSON geometry object. * - * @param {TopoJSONGeometry} object TopoJSON object. + * @param {TopoJSONMultiLineString} object TopoJSON object. * @param {Array>} arcs Array of arcs. * @return {MultiLineString} Geometry. */ @@ -243,7 +254,7 @@ function readMultiLineStringGeometry(object, arcs) { /** * Create a polygon from a TopoJSON geometry object. * - * @param {TopoJSONGeometry} object TopoJSON object. + * @param {TopoJSONPolygon} object TopoJSON object. * @param {Array>} arcs Array of arcs. * @return {Polygon} Geometry. */ @@ -259,7 +270,7 @@ function readPolygonGeometry(object, arcs) { /** * Create a multi-polygon from a TopoJSON geometry object. * - * @param {TopoJSONGeometry} object TopoJSON object. + * @param {TopoJSONMultiPolygon} object TopoJSON object. * @param {Array>} arcs Array of arcs. * @return {MultiPolygon} Geometry. */ From ba53b5e8aa734d6a657837d68fce1e1a9a9cc851 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Fri, 14 Sep 2018 12:45:41 +0200 Subject: [PATCH 2/2] Quotes topojson properties --- src/ol/format/TopoJSON.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ol/format/TopoJSON.js b/src/ol/format/TopoJSON.js index cc5639844e..7875d74ea9 100644 --- a/src/ol/format/TopoJSON.js +++ b/src/ol/format/TopoJSON.js @@ -93,18 +93,18 @@ class TopoJSON extends JSONFeature { if (object.type == 'Topology') { const topoJSONTopology = /** @type {TopoJSONTopology} */ (object); let transform, scale = null, translate = null; - if (topoJSONTopology.transform) { - transform = topoJSONTopology.transform; - scale = transform.scale; - translate = transform.translate; + if (topoJSONTopology['transform']) { + transform = topoJSONTopology['transform']; + scale = transform['scale']; + translate = transform['translate']; } - const arcs = topoJSONTopology.arcs; + const arcs = topoJSONTopology['arcs']; if (transform) { transformArcs(arcs, scale, translate); } /** @type {Array} */ const features = []; - const topoJSONFeatures = topoJSONTopology.objects; + const topoJSONFeatures = topoJSONTopology['objects']; const property = this.layerName_; let feature; for (const objectName in topoJSONFeatures) { @@ -195,7 +195,7 @@ function concatenateArcs(indices, arcs) { * @return {Point} Geometry. */ function readPointGeometry(object, scale, translate) { - const coordinates = object.coordinates; + const coordinates = object['coordinates']; if (scale && translate) { transformVertex(coordinates, scale, translate); } @@ -212,7 +212,7 @@ function readPointGeometry(object, scale, translate) { * @return {MultiPoint} Geometry. */ function readMultiPointGeometry(object, scale, translate) { - const coordinates = object.coordinates; + const coordinates = object['coordinates']; if (scale && translate) { for (let i = 0, ii = coordinates.length; i < ii; ++i) { transformVertex(coordinates[i], scale, translate); @@ -230,7 +230,7 @@ function readMultiPointGeometry(object, scale, translate) { * @return {LineString} Geometry. */ function readLineStringGeometry(object, arcs) { - const coordinates = concatenateArcs(object.arcs, arcs); + const coordinates = concatenateArcs(object['arcs'], arcs); return new LineString(coordinates); } @@ -244,8 +244,8 @@ function readLineStringGeometry(object, arcs) { */ function readMultiLineStringGeometry(object, arcs) { const coordinates = []; - for (let i = 0, ii = object.arcs.length; i < ii; ++i) { - coordinates[i] = concatenateArcs(object.arcs[i], arcs); + for (let i = 0, ii = object['arcs'].length; i < ii; ++i) { + coordinates[i] = concatenateArcs(object['arcs'][i], arcs); } return new MultiLineString(coordinates); } @@ -260,8 +260,8 @@ function readMultiLineStringGeometry(object, arcs) { */ function readPolygonGeometry(object, arcs) { const coordinates = []; - for (let i = 0, ii = object.arcs.length; i < ii; ++i) { - coordinates[i] = concatenateArcs(object.arcs[i], arcs); + for (let i = 0, ii = object['arcs'].length; i < ii; ++i) { + coordinates[i] = concatenateArcs(object['arcs'][i], arcs); } return new Polygon(coordinates); } @@ -276,9 +276,9 @@ function readPolygonGeometry(object, arcs) { */ function readMultiPolygonGeometry(object, arcs) { const coordinates = []; - for (let i = 0, ii = object.arcs.length; i < ii; ++i) { + for (let i = 0, ii = object['arcs'].length; i < ii; ++i) { // for each polygon - const polyArray = object.arcs[i]; + const polyArray = object['arcs'][i]; const ringCoords = []; for (let j = 0, jj = polyArray.length; j < jj; ++j) { // for each ring @@ -305,7 +305,7 @@ function readMultiPolygonGeometry(object, arcs) { * @return {Array} Array of features. */ function readFeaturesFromGeometryCollection(collection, arcs, scale, translate, property, name, opt_options) { - const geometries = collection.geometries; + const geometries = collection['geometries']; const features = []; for (let i = 0, ii = geometries.length; i < ii; ++i) { features[i] = readFeatureFromGeometry(