Merge pull request #8618 from fredj/topojson_types
Use TopoJSON types from @types/topojson
This commit is contained in:
@@ -41,6 +41,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@openlayers/eslint-plugin": "^4.0.0-beta.1",
|
"@openlayers/eslint-plugin": "^4.0.0-beta.1",
|
||||||
"@types/geojson": "^7946.0.4",
|
"@types/geojson": "^7946.0.4",
|
||||||
|
"@types/topojson-specification": "^1.0.0",
|
||||||
"buble": "^0.19.3",
|
"buble": "^0.19.3",
|
||||||
"buble-loader": "^0.5.1",
|
"buble-loader": "^0.5.1",
|
||||||
"chaikin-smooth": "^1.0.4",
|
"chaikin-smooth": "^1.0.4",
|
||||||
|
|||||||
+33
-22
@@ -12,6 +12,17 @@ import Point from '../geom/Point.js';
|
|||||||
import Polygon from '../geom/Polygon.js';
|
import Polygon from '../geom/Polygon.js';
|
||||||
import {get as getProjection} from '../proj.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
|
* @typedef {Object} Options
|
||||||
@@ -82,18 +93,18 @@ class TopoJSON extends JSONFeature {
|
|||||||
if (object.type == 'Topology') {
|
if (object.type == 'Topology') {
|
||||||
const topoJSONTopology = /** @type {TopoJSONTopology} */ (object);
|
const topoJSONTopology = /** @type {TopoJSONTopology} */ (object);
|
||||||
let transform, scale = null, translate = null;
|
let transform, scale = null, translate = null;
|
||||||
if (topoJSONTopology.transform) {
|
if (topoJSONTopology['transform']) {
|
||||||
transform = topoJSONTopology.transform;
|
transform = topoJSONTopology['transform'];
|
||||||
scale = transform.scale;
|
scale = transform['scale'];
|
||||||
translate = transform.translate;
|
translate = transform['translate'];
|
||||||
}
|
}
|
||||||
const arcs = topoJSONTopology.arcs;
|
const arcs = topoJSONTopology['arcs'];
|
||||||
if (transform) {
|
if (transform) {
|
||||||
transformArcs(arcs, scale, translate);
|
transformArcs(arcs, scale, translate);
|
||||||
}
|
}
|
||||||
/** @type {Array<Feature>} */
|
/** @type {Array<Feature>} */
|
||||||
const features = [];
|
const features = [];
|
||||||
const topoJSONFeatures = topoJSONTopology.objects;
|
const topoJSONFeatures = topoJSONTopology['objects'];
|
||||||
const property = this.layerName_;
|
const property = this.layerName_;
|
||||||
let feature;
|
let feature;
|
||||||
for (const objectName in topoJSONFeatures) {
|
for (const objectName in topoJSONFeatures) {
|
||||||
@@ -178,13 +189,13 @@ function concatenateArcs(indices, arcs) {
|
|||||||
/**
|
/**
|
||||||
* Create a point from a TopoJSON geometry object.
|
* Create a point from a TopoJSON geometry object.
|
||||||
*
|
*
|
||||||
* @param {TopoJSONGeometry} object TopoJSON object.
|
* @param {TopoJSONPoint} object TopoJSON object.
|
||||||
* @param {Array<number>} scale Scale for each dimension.
|
* @param {Array<number>} scale Scale for each dimension.
|
||||||
* @param {Array<number>} translate Translation for each dimension.
|
* @param {Array<number>} translate Translation for each dimension.
|
||||||
* @return {Point} Geometry.
|
* @return {Point} Geometry.
|
||||||
*/
|
*/
|
||||||
function readPointGeometry(object, scale, translate) {
|
function readPointGeometry(object, scale, translate) {
|
||||||
const coordinates = object.coordinates;
|
const coordinates = object['coordinates'];
|
||||||
if (scale && translate) {
|
if (scale && translate) {
|
||||||
transformVertex(coordinates, scale, translate);
|
transformVertex(coordinates, scale, translate);
|
||||||
}
|
}
|
||||||
@@ -195,13 +206,13 @@ function readPointGeometry(object, scale, translate) {
|
|||||||
/**
|
/**
|
||||||
* Create a multi-point from a TopoJSON geometry object.
|
* Create a multi-point from a TopoJSON geometry object.
|
||||||
*
|
*
|
||||||
* @param {TopoJSONGeometry} object TopoJSON object.
|
* @param {TopoJSONMultiPoint} object TopoJSON object.
|
||||||
* @param {Array<number>} scale Scale for each dimension.
|
* @param {Array<number>} scale Scale for each dimension.
|
||||||
* @param {Array<number>} translate Translation for each dimension.
|
* @param {Array<number>} translate Translation for each dimension.
|
||||||
* @return {MultiPoint} Geometry.
|
* @return {MultiPoint} Geometry.
|
||||||
*/
|
*/
|
||||||
function readMultiPointGeometry(object, scale, translate) {
|
function readMultiPointGeometry(object, scale, translate) {
|
||||||
const coordinates = object.coordinates;
|
const coordinates = object['coordinates'];
|
||||||
if (scale && translate) {
|
if (scale && translate) {
|
||||||
for (let i = 0, ii = coordinates.length; i < ii; ++i) {
|
for (let i = 0, ii = coordinates.length; i < ii; ++i) {
|
||||||
transformVertex(coordinates[i], scale, translate);
|
transformVertex(coordinates[i], scale, translate);
|
||||||
@@ -214,12 +225,12 @@ function readMultiPointGeometry(object, scale, translate) {
|
|||||||
/**
|
/**
|
||||||
* Create a linestring from a TopoJSON geometry object.
|
* Create a linestring from a TopoJSON geometry object.
|
||||||
*
|
*
|
||||||
* @param {TopoJSONGeometry} object TopoJSON object.
|
* @param {TopoJSONLineString} object TopoJSON object.
|
||||||
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
||||||
* @return {LineString} Geometry.
|
* @return {LineString} Geometry.
|
||||||
*/
|
*/
|
||||||
function readLineStringGeometry(object, arcs) {
|
function readLineStringGeometry(object, arcs) {
|
||||||
const coordinates = concatenateArcs(object.arcs, arcs);
|
const coordinates = concatenateArcs(object['arcs'], arcs);
|
||||||
return new LineString(coordinates);
|
return new LineString(coordinates);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,14 +238,14 @@ function readLineStringGeometry(object, arcs) {
|
|||||||
/**
|
/**
|
||||||
* Create a multi-linestring from a TopoJSON geometry object.
|
* Create a multi-linestring from a TopoJSON geometry object.
|
||||||
*
|
*
|
||||||
* @param {TopoJSONGeometry} object TopoJSON object.
|
* @param {TopoJSONMultiLineString} object TopoJSON object.
|
||||||
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
||||||
* @return {MultiLineString} Geometry.
|
* @return {MultiLineString} Geometry.
|
||||||
*/
|
*/
|
||||||
function readMultiLineStringGeometry(object, arcs) {
|
function readMultiLineStringGeometry(object, arcs) {
|
||||||
const coordinates = [];
|
const coordinates = [];
|
||||||
for (let i = 0, ii = object.arcs.length; i < ii; ++i) {
|
for (let i = 0, ii = object['arcs'].length; i < ii; ++i) {
|
||||||
coordinates[i] = concatenateArcs(object.arcs[i], arcs);
|
coordinates[i] = concatenateArcs(object['arcs'][i], arcs);
|
||||||
}
|
}
|
||||||
return new MultiLineString(coordinates);
|
return new MultiLineString(coordinates);
|
||||||
}
|
}
|
||||||
@@ -243,14 +254,14 @@ function readMultiLineStringGeometry(object, arcs) {
|
|||||||
/**
|
/**
|
||||||
* Create a polygon from a TopoJSON geometry object.
|
* Create a polygon from a TopoJSON geometry object.
|
||||||
*
|
*
|
||||||
* @param {TopoJSONGeometry} object TopoJSON object.
|
* @param {TopoJSONPolygon} object TopoJSON object.
|
||||||
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
||||||
* @return {Polygon} Geometry.
|
* @return {Polygon} Geometry.
|
||||||
*/
|
*/
|
||||||
function readPolygonGeometry(object, arcs) {
|
function readPolygonGeometry(object, arcs) {
|
||||||
const coordinates = [];
|
const coordinates = [];
|
||||||
for (let i = 0, ii = object.arcs.length; i < ii; ++i) {
|
for (let i = 0, ii = object['arcs'].length; i < ii; ++i) {
|
||||||
coordinates[i] = concatenateArcs(object.arcs[i], arcs);
|
coordinates[i] = concatenateArcs(object['arcs'][i], arcs);
|
||||||
}
|
}
|
||||||
return new Polygon(coordinates);
|
return new Polygon(coordinates);
|
||||||
}
|
}
|
||||||
@@ -259,15 +270,15 @@ function readPolygonGeometry(object, arcs) {
|
|||||||
/**
|
/**
|
||||||
* Create a multi-polygon from a TopoJSON geometry object.
|
* Create a multi-polygon from a TopoJSON geometry object.
|
||||||
*
|
*
|
||||||
* @param {TopoJSONGeometry} object TopoJSON object.
|
* @param {TopoJSONMultiPolygon} object TopoJSON object.
|
||||||
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
* @param {Array<Array<import("../coordinate.js").Coordinate>>} arcs Array of arcs.
|
||||||
* @return {MultiPolygon} Geometry.
|
* @return {MultiPolygon} Geometry.
|
||||||
*/
|
*/
|
||||||
function readMultiPolygonGeometry(object, arcs) {
|
function readMultiPolygonGeometry(object, arcs) {
|
||||||
const coordinates = [];
|
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
|
// for each polygon
|
||||||
const polyArray = object.arcs[i];
|
const polyArray = object['arcs'][i];
|
||||||
const ringCoords = [];
|
const ringCoords = [];
|
||||||
for (let j = 0, jj = polyArray.length; j < jj; ++j) {
|
for (let j = 0, jj = polyArray.length; j < jj; ++j) {
|
||||||
// for each ring
|
// for each ring
|
||||||
@@ -294,7 +305,7 @@ function readMultiPolygonGeometry(object, arcs) {
|
|||||||
* @return {Array<Feature>} Array of features.
|
* @return {Array<Feature>} Array of features.
|
||||||
*/
|
*/
|
||||||
function readFeaturesFromGeometryCollection(collection, arcs, scale, translate, property, name, opt_options) {
|
function readFeaturesFromGeometryCollection(collection, arcs, scale, translate, property, name, opt_options) {
|
||||||
const geometries = collection.geometries;
|
const geometries = collection['geometries'];
|
||||||
const features = [];
|
const features = [];
|
||||||
for (let i = 0, ii = geometries.length; i < ii; ++i) {
|
for (let i = 0, ii = geometries.length; i < ii; ++i) {
|
||||||
features[i] = readFeatureFromGeometry(
|
features[i] = readFeatureFromGeometry(
|
||||||
|
|||||||
Reference in New Issue
Block a user