slippy-tile
Advanced tools
Comparing version 3.0.2 to 4.0.0
# Changelog | ||
## 4.0.0 - 2018-02-21 | ||
- Convert entire repo to Typescript | ||
- Enforce strict Typescript configs & linting | ||
## 3.0.1 - 2017-10-23 | ||
@@ -5,0 +10,0 @@ |
161
index.js
@@ -1,3 +0,4 @@ | ||
import { googleToBBox, googleToTile, googleToQuadkey, bboxToMeters } from 'global-mercator' | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var global_mercator_1 = require("global-mercator"); | ||
/** | ||
@@ -12,35 +13,39 @@ * Substitutes the given tile information [x, y, z] to the URL tile scheme. | ||
* @param {string} [options.version] Version | ||
* @param {string} [format='image/png'] Image Format | ||
* @param {string} [format="image/png"] Image Format | ||
* @returns {string} parsed URL | ||
* @example | ||
* slippyTile([10, 15, 8], 'https://{s}.tile.openstreetmap.org/{zoom}/{x}/{y}.png') | ||
* //='https://c.tile.openstreetmap.org/8/10/15.png' | ||
* slippyTile([10, 15, 8], "https://{s}.tile.openstreetmap.org/{zoom}/{x}/{y}.png") | ||
* //="https://c.tile.openstreetmap.org/8/10/15.png" | ||
*/ | ||
function slippyTile (tile, url, options) { | ||
options = options || {} | ||
const format = options.format || 'image/png' | ||
const x = tile[0] | ||
const y = tile[1] | ||
const zoom = tile[2] | ||
url = wms(tile, url) | ||
url = wmts(url) | ||
url = parseSwitch(url) | ||
url = url.replace(/{(zoom|z|level)}/gi, String(zoom)) | ||
url = url.replace(/{(x|col)}/gi, String(x)) | ||
url = url.replace(/{(y|row)}/gi, String(y)) | ||
url = url.replace(/{format}/gi, format) | ||
// Replace all additional options | ||
Object.keys(options).forEach(key => { | ||
const pattern = RegExp('{' + key + '}', 'ig') | ||
if (url.match(pattern)) { | ||
if (options[key] === undefined) throw new Error('options.' + key + ' is required') | ||
url = url.replace(pattern, options[key]) | ||
function slippyTile(tile, url, options) { | ||
if (options === void 0) { options = {}; } | ||
var format = options.format || "image/png"; | ||
var x = tile[0]; | ||
var y = tile[1]; | ||
var zoom = tile[2]; | ||
url = wms(tile, url); | ||
url = wmts(url); | ||
url = parseSwitch(url); | ||
url = url.replace(/{(zoom|z|level)}/gi, String(zoom)); | ||
url = url.replace(/{(x|col)}/gi, String(x)); | ||
url = url.replace(/{(y|row)}/gi, String(y)); | ||
url = url.replace(/{format}/gi, format); | ||
// Replace all additional options | ||
Object.keys(options).forEach(function (key) { | ||
var pattern = RegExp("{" + key + "}", "ig"); | ||
if (url.match(pattern)) { | ||
if (options[key] === undefined) { | ||
throw new Error("options." + key + " is required"); | ||
} | ||
url = url.replace(pattern, options[key]); | ||
} | ||
}); | ||
if (url.match(/{-y}/)) { | ||
url = url.replace(/{-y}/gi, String(global_mercator_1.googleToTile(tile)[1])); | ||
} | ||
}) | ||
if (url.match(/{-y}/)) url = url.replace(/{-y}/gi, String(googleToTile(tile)[1])) | ||
if (url.match(/{(quadkey|q)}/)) url = url.replace(/{(quadkey|q)}/gi, googleToQuadkey(tile)) | ||
return url | ||
if (url.match(/{(quadkey|q)}/)) { | ||
url = url.replace(/{(quadkey|q)}/gi, global_mercator_1.googleToQuadkey(tile)); | ||
} | ||
return url; | ||
} | ||
/** | ||
@@ -54,20 +59,20 @@ * Parse WMS URL to friendly SlippyTile format | ||
* @example | ||
* wms([10, 15, 8], 'https://<Tile Server>/?layers=imagery&SRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}') | ||
* //='https://<Tile Server>/?layers=imagery&SRS=EPSG:3857&WIDTH=256&HEIGHT=256&BBOX=-165.9375,82.676285,-164.53125,82.853382' | ||
* wms([10, 15, 8], "/?layers=imagery&SRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}") | ||
* // => "/?layers=imagery&SRS=EPSG:3857&WIDTH=256&HEIGHT=256&BBOX=-165.9375,82.676285,-164.53125,82.853382" | ||
*/ | ||
function wms (tile, url) { | ||
url = url.replace(/{height}/gi, '256') | ||
url = url.replace(/{width}/gi, '256') | ||
url = url.replace(/{size}/gi, '256,256') | ||
url = url.replace(/{(proj|srs|crs)}/gi, 'EPSG:3857') | ||
const bbox = googleToBBox(tile) | ||
const bboxMeters = bboxToMeters(bbox) | ||
if (url.match(/EPSG:(3857|900913)/i) && url.match(/{bbox}/i)) url = url.replace(/{bbox}/gi, bboxMeters.join(',')) | ||
url = url.replace(/{bbox4326}/gi, bbox.join(',')) | ||
url = url.replace(/{bbox3857}/gi, bboxMeters.join(',')) | ||
url = url.replace(/{bbox}/gi, bbox.join(',')) | ||
return url | ||
function wms(tile, url) { | ||
url = url.replace(/{height}/gi, "256"); | ||
url = url.replace(/{width}/gi, "256"); | ||
url = url.replace(/{size}/gi, "256,256"); | ||
url = url.replace(/{(proj|srs|crs)}/gi, "EPSG:3857"); | ||
var bbox = global_mercator_1.googleToBBox(tile); | ||
var bboxMeters = global_mercator_1.bboxToMeters(bbox); | ||
if (url.match(/EPSG:(3857|900913)/i) && url.match(/{bbox}/i)) { | ||
url = url.replace(/{bbox}/gi, bboxMeters.join(",")); | ||
} | ||
url = url.replace(/{bbox4326}/gi, bbox.join(",")); | ||
url = url.replace(/{bbox3857}/gi, bboxMeters.join(",")); | ||
url = url.replace(/{bbox}/gi, bbox.join(",")); | ||
return url; | ||
} | ||
/** | ||
@@ -80,16 +85,15 @@ * Parse WMTS URL to friendly SlippyTile URL format | ||
* @example | ||
* wmts('https://<Tile Server>/WMTS/tile/1.0.0/Imagery/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.jpg') | ||
* //='https://<Tile Server>/WMTS/tile/1.0.0/Imagery/default/GoogleMapsCompatible/{z}/{y}/{x}.jpg' | ||
* wmts("https://<Tile Server>/WMTS/tile/1.0.0/Imagery/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.jpg") | ||
* //="https://<Tile Server>/WMTS/tile/1.0.0/Imagery/default/GoogleMapsCompatible/{z}/{y}/{x}.jpg" | ||
*/ | ||
function wmts (url) { | ||
if (url.match(/{TileCol|TileRow|TileMatrix|TileMatrixSet|Style}/gi)) { | ||
url = url.replace(/{TileCol}/gi, '{x}') | ||
url = url.replace(/{TileRow}/gi, '{y}') | ||
url = url.replace(/{TileMatrix}/gi, '{z}') | ||
url = url.replace(/{TileMatrixSet}/gi, 'GoogleMapsCompatible') | ||
url = url.replace(/{Style}/gi, 'default') | ||
} | ||
return url | ||
function wmts(url) { | ||
if (url.match(/{TileCol|TileRow|TileMatrix|TileMatrixSet|Style}/gi)) { | ||
url = url.replace(/{TileCol}/gi, "{x}"); | ||
url = url.replace(/{TileRow}/gi, "{y}"); | ||
url = url.replace(/{TileMatrix}/gi, "{z}"); | ||
url = url.replace(/{TileMatrixSet}/gi, "GoogleMapsCompatible"); | ||
url = url.replace(/{Style}/gi, "default"); | ||
} | ||
return url; | ||
} | ||
/** | ||
@@ -102,16 +106,18 @@ * Replaces {switch:a,b,c} with a random sample. | ||
* @example | ||
* parseSwitch('http://tile-{switch:a,b,c}.openstreetmap.fr/hot/{zoom}/{x}/{y}.png') | ||
* //='http://tile-b.openstreetmap.fr/hot/{zoom}/{x}/{y}.png' | ||
* parseSwitch("http://tile-{switch:a,b,c}.openstreetmap.fr/hot/{zoom}/{x}/{y}.png") | ||
* //="http://tile-b.openstreetmap.fr/hot/{zoom}/{x}/{y}.png" | ||
*/ | ||
function parseSwitch (url) { | ||
// Default simple switch | ||
if (url.match(/{s}/gi)) return url.replace(/{s}/gi, String(sample(['a', 'b', 'c']))) | ||
// Custom switch | ||
var pattern = /{switch:([a-z,\d]*)}/i | ||
var found = url.match(pattern) | ||
if (found) return url.replace(pattern, String(sample(found[1].split(',')))) | ||
return url | ||
function parseSwitch(url) { | ||
// Default simple switch | ||
if (url.match(/{s}/gi)) { | ||
return url.replace(/{s}/gi, String(sample(["a", "b", "c"]))); | ||
} | ||
// Custom switch | ||
var pattern = /{switch:([a-z,\d]*)}/i; | ||
var found = url.match(pattern); | ||
if (found) { | ||
return url.replace(pattern, String(sample(found[1].split(",")))); | ||
} | ||
return url; | ||
} | ||
/** | ||
@@ -125,10 +131,11 @@ * Sample an item from a given list | ||
* @example | ||
* sample(['a', 'b', 'c']) | ||
* //='b' | ||
* sample(["a", "b", "c"]) | ||
* //="b" | ||
*/ | ||
function sample (array) { | ||
if (array === null || array === undefined || array.length === 0) { return undefined } | ||
return array[Math.floor(Math.random() * array.length)] | ||
function sample(array) { | ||
if (array === null || array === undefined || array.length === 0) { | ||
return undefined; | ||
} | ||
return array[Math.floor(Math.random() * array.length)]; | ||
} | ||
export default slippyTile | ||
exports.default = slippyTile; |
{ | ||
"name": "slippy-tile", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"description": "Helps convert Slippy Map url tile schemas", | ||
"main": "main", | ||
"module": "index", | ||
"jsnext:main": "index", | ||
"types": "index.d.ts", | ||
"main": "index", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js", | ||
"main.js" | ||
"index.ts" | ||
], | ||
"scripts": { | ||
"prepublish": "rollup -c rollup.config.js && echo 'module.exports.default = slippyTile;' >> main.js", | ||
"pretest": "standard index.js && npm run prepublish", | ||
"test": "node -r @std/esm test.js", | ||
"posttest": "tsc types.ts && node types.js" | ||
"pretest": "tsc", | ||
"test": "node test.js", | ||
"posttest": "tslint index.ts --project tsconfig.json" | ||
}, | ||
@@ -33,14 +28,10 @@ "author": "Denis Carriere <@DenisCarriere>", | ||
"devDependencies": { | ||
"@std/esm": "*", | ||
"rollup": "*", | ||
"standard": "*", | ||
"tape": "*" | ||
"@types/tape": "*", | ||
"tape": "*", | ||
"tslint": "*", | ||
"typescript": "*" | ||
}, | ||
"dependencies": { | ||
"global-mercator": "*" | ||
}, | ||
"@std/esm": { | ||
"esm": "js", | ||
"cjs": true | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
261
16476
6
1