global-mercator
Advanced tools
Comparing version 2.8.2 to 2.8.3
@@ -6,2 +6,3 @@ | ||
- Add `wrapTile` method | ||
- Include tests side by side with `tilebelt` | ||
@@ -8,0 +9,0 @@ - Fixed issue with `tileToQuadkey` |
@@ -45,3 +45,4 @@ /** | ||
export declare function latitude(lat: number): number; | ||
export declare function pointToTileFraction(lnglat: LngLat, zoom: number): Tile; | ||
export declare function pointToTile(lnglat: LngLat, zoom: number): Tile; | ||
export declare function pointToTileFraction(lnglat: LngLat, zoom: number, validate?: boolean): Tile; | ||
export declare function pointToTile(lnglat: LngLat, zoom: number, validate?: boolean): Tile; | ||
export declare function wrapTile(tile: Tile): Tile; |
71
index.js
@@ -31,2 +31,4 @@ var originShift = 2 * Math.PI * 6378137 / 2.0 | ||
* @param {number} zoom Zoom level | ||
* @param {boolean} [validate=true] validates LatLng coordinates | ||
* @returns {Google} Google (XYZ) Tile | ||
* @example | ||
@@ -36,4 +38,4 @@ * var tile = mercator.pointToTile([1, 1], 12) | ||
*/ | ||
function pointToTile (lnglat, zoom) { | ||
var tile = pointToTileFraction(lnglat, zoom) | ||
function pointToTile (lnglat, zoom, validate) { | ||
var tile = pointToTileFraction(lnglat, zoom, validate) | ||
tile[0] = Math.floor(tile[0]) | ||
@@ -51,10 +53,12 @@ tile[1] = Math.floor(tile[1]) | ||
* @param {number} zoom Zoom level | ||
* @returns {[number, number, number]} tile fraction | ||
* @param {boolean} [validate=true] validates LatLng coordinates | ||
* @returns {Google} Google (XYZ) Tile | ||
* var tile = mercator.pointToTileFraction([1, 1], 12) | ||
* //= [ 2059.3777777777777, 2036.6216445333432, 12 ] | ||
*/ | ||
function pointToTileFraction (lnglat, zoom) { | ||
function pointToTileFraction (lnglat, zoom, validate) { | ||
// lnglat = validateLngLat(lnglat, validate) | ||
var z = zoom | ||
var lon = lnglat[0] | ||
var lat = lnglat[1] | ||
var lon = longitude(lnglat[0]) | ||
var lat = latitude(lnglat[1]) | ||
var sin = Math.sin(lat * d2r) | ||
@@ -64,3 +68,3 @@ var z2 = Math.pow(2, z) | ||
var y = z2 * (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI) | ||
return [x, y, z] | ||
return validateTile([x, y, z], validate) | ||
} | ||
@@ -518,9 +522,15 @@ | ||
if (validate === false) return tile | ||
validateZoom(zoom) | ||
if (zoom === undefined || zoom === null) throw new Error('<zoom> is required') | ||
if (tx === undefined || tx === null) throw new Error('<x> is required') | ||
if (ty === undefined || ty === null) throw new Error('<y> is required') | ||
if (tx < 0) throw new Error('<x> must not be less than 0') | ||
if (ty < 0) throw new Error('<y> must not be less than 0') | ||
var maxCount = Math.pow(2, zoom) | ||
if (tx >= maxCount || ty >= maxCount) throw new Error('Illegal parameters for tile') | ||
// Adjust values of tiles to fit within tile scheme | ||
zoom = validateZoom(zoom) | ||
tile = wrapTile(tile) | ||
// // Check to see if tile is valid based on the zoom level | ||
// // Currently impossible to hit since WrapTile handles this error | ||
// // will keep this test commented out in case it doesnt handle it | ||
// var maxCount = Math.pow(2, zoom) | ||
// if (tile[0] >= maxCount || tile[1] >= maxCount) throw new Error('Illegal parameters for tile') | ||
return tile | ||
@@ -530,2 +540,36 @@ } | ||
/** | ||
* Wrap Tile -- Handles tiles which crosses the 180th meridian or 90th parallel | ||
* | ||
* @param {[number, number, number]} tile Tile | ||
* @param {number} zoom Zoom Level | ||
* @returns {[number, number, number]} Wrapped Tile | ||
* @example | ||
* mercator.wrapTile([0, 3, 2]) | ||
* //= [0, 3, 2] -- Valid Tile X | ||
* mercator.wrapTile([4, 2, 2]) | ||
* //= [0, 2, 2] -- Tile 4 does not exist, wrap around to TileX=0 | ||
*/ | ||
function wrapTile (tile) { | ||
var tx = tile[0] | ||
var ty = tile[1] | ||
var zoom = tile[2] | ||
// Maximum tile allowed | ||
// zoom 0 => 1 | ||
// zoom 1 => 2 | ||
// zoom 2 => 4 | ||
// zoom 3 => 8 | ||
var maxTile = 1 << zoom | ||
// Handle Tile X | ||
tx = tx % maxTile | ||
if (tx < 0) tx = tx + maxTile | ||
// Handle Tile Y | ||
ty = ty % maxTile | ||
if (ty < 0) ty = ty + maxTile | ||
return [tx, ty, zoom] | ||
} | ||
/** | ||
* Validates Zoom level | ||
@@ -757,3 +801,4 @@ * | ||
pointToTileFraction: pointToTileFraction, | ||
pointToTile: pointToTile | ||
pointToTile: pointToTile, | ||
wrapTile: wrapTile | ||
} |
{ | ||
"name": "global-mercator", | ||
"version": "2.8.2", | ||
"version": "2.8.3", | ||
"description": "Tools to help with TMS, Quadkey & Google (XYZ) Tiles", | ||
@@ -52,5 +52,6 @@ "repository": { | ||
"standard": "^10.0.2", | ||
"tap": "^10.7.0" | ||
"tap": "^10.7.0", | ||
"tape": "^4.7.0" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -35,33 +35,34 @@ # [Global Mercator](https://www.npmjs.com/package/global-mercator) | ||
| Function | Description | | ||
| ------------------------------------------------- | :-------------------------------------------------------- | | ||
| [lngLatToMeters(LngLat)](#lnglattometers) | Converts LngLat coordinates to Meters coordinates. | | ||
| [metersToLngLat(Meters)](#meterstolnglat) | Converts Meters coordinates to LngLat coordinates. | | ||
| [metersToPixels(Meters, zoom)](#meterstopixels) | Converts Meters coordinates to Pixels coordinates. | | ||
| [lngLatToTile(LngLat, zoom)](#lnglattotile) | Converts LngLat coordinates to TMS Tile. | | ||
| [lngLatToGoogle(LngLat, zoom)](#lnglattogoogle) | Converts LngLat coordinates to Google (XYZ) Tile. | | ||
| [metersToTile(Meters, zoom)](#meterstotile) | Converts Meters coordinates to TMS Tile. | | ||
| [pixelsToMeters(Pixels)](#pixelstometers) | Converts Pixels coordinates to Meters coordinates. | | ||
| [pixelsToTile(Pixels)](#pixelstotile) | Converts Pixels coordinates to TMS Tile. | | ||
| [tileToBBoxMeters(tile)](#tiletobboxmeters) | Converts TMS Tile to bbox in Meters coordinates. | | ||
| [tileToBBox(tile)](#tiletobbox) | Converts TMS Tile to bbox in LngLat coordinates. | | ||
| [googleToBBoxMeters(google)](#googletobboxmeters) | Converts Google (XYZ) Tile to bbox in Meters coordinates. | | ||
| [googleToBBox(google)](#googletobbox) | Converts Google (XYZ) Tile to bbox in LngLat coordinates. | | ||
| [tileToGoogle(tile)](#tiletogoogle) | Converts TMS Tile to Google (XYZ) Tile. | | ||
| [googleToTile(google)](#googletotile) | Converts Google (XYZ) Tile to TMS Tile. | | ||
| [googleToQuadkey(google)](#googletoquadkey) | Converts Google (XYZ) Tile to Quadkey. | | ||
| [tileToQuadkey(tile)](#tiletoquadkey) | Converts TMS Tile to QuadKey. | | ||
| [quadkeyToTile(quadkey)](#quadkeytotile) | Converts Quadkey to TMS Tile. | | ||
| [quadkeyToGoogle(quadkey)](#quadkeytogoogle) | Converts Quadkey to Google (XYZ) Tile. | | ||
| [hash(tile)](#hash) | Hash tile for unique id key | | ||
| [validateTile(tile)](#validatetile) | Validates TMS Tile | | ||
| [validateZoom(zoom)](#validatezoom) | Validates Zoom level | | ||
| [validateLngLat(LngLat)](#validatelnglat) | Validates LngLat coordinates | | ||
| [validatePixels(Pixels)](#validatepixels) | Validates Pixels coordinates | | ||
| [maxBBox(BBox\[\])](#maxbbox) | Maximum extent of BBox | | ||
| [validTile(tile)](#validtile) | Valid Tile | | ||
| [longitude(degree)](#longitude) | Modifies a Longitude to fit within +/-180 degrees. | | ||
| [latitude(degree)](#latitude) | Modifies a Latitude to fit within +/-90 degrees. | | ||
| [pointToTile(lnglat, zoom)](#pointtotile) | Get the tile for a point at a specified zoom level | | ||
| [pointToTileFraction(lnglat, zoom)](#pointtotilefraction) | Get the precise fractional tile location for a point at a zoom level | | ||
| Function | Description | | ||
| --------------------------------------------------------- | :------------------------------------------------------------------- | | ||
| [lngLatToMeters(LngLat)](#lnglattometers) | Converts LngLat coordinates to Meters coordinates. | | ||
| [metersToLngLat(Meters)](#meterstolnglat) | Converts Meters coordinates to LngLat coordinates. | | ||
| [metersToPixels(Meters, zoom)](#meterstopixels) | Converts Meters coordinates to Pixels coordinates. | | ||
| [lngLatToTile(LngLat, zoom)](#lnglattotile) | Converts LngLat coordinates to TMS Tile. | | ||
| [lngLatToGoogle(LngLat, zoom)](#lnglattogoogle) | Converts LngLat coordinates to Google (XYZ) Tile. | | ||
| [metersToTile(Meters, zoom)](#meterstotile) | Converts Meters coordinates to TMS Tile. | | ||
| [pixelsToMeters(Pixels)](#pixelstometers) | Converts Pixels coordinates to Meters coordinates. | | ||
| [pixelsToTile(Pixels)](#pixelstotile) | Converts Pixels coordinates to TMS Tile. | | ||
| [tileToBBoxMeters(tile)](#tiletobboxmeters) | Converts TMS Tile to bbox in Meters coordinates. | | ||
| [tileToBBox(tile)](#tiletobbox) | Converts TMS Tile to bbox in LngLat coordinates. | | ||
| [googleToBBoxMeters(google)](#googletobboxmeters) | Converts Google (XYZ) Tile to bbox in Meters coordinates. | | ||
| [googleToBBox(google)](#googletobbox) | Converts Google (XYZ) Tile to bbox in LngLat coordinates. | | ||
| [tileToGoogle(tile)](#tiletogoogle) | Converts TMS Tile to Google (XYZ) Tile. | | ||
| [googleToTile(google)](#googletotile) | Converts Google (XYZ) Tile to TMS Tile. | | ||
| [googleToQuadkey(google)](#googletoquadkey) | Converts Google (XYZ) Tile to Quadkey. | | ||
| [tileToQuadkey(tile)](#tiletoquadkey) | Converts TMS Tile to QuadKey. | | ||
| [quadkeyToTile(quadkey)](#quadkeytotile) | Converts Quadkey to TMS Tile. | | ||
| [quadkeyToGoogle(quadkey)](#quadkeytogoogle) | Converts Quadkey to Google (XYZ) Tile. | | ||
| [hash(tile)](#hash) | Hash tile for unique id key | | ||
| [validateTile(tile)](#validatetile) | Validates TMS Tile | | ||
| [validateZoom(zoom)](#validatezoom) | Validates Zoom level | | ||
| [validateLngLat(LngLat)](#validatelnglat) | Validates LngLat coordinates | | ||
| [validatePixels(Pixels)](#validatepixels) | Validates Pixels coordinates | | ||
| [maxBBox(BBox\[\])](#maxbbox) | Maximum extent of BBox | | ||
| [validTile(tile)](#validtile) | Valid Tile | | ||
| [longitude(degree)](#longitude) | Modifies a Longitude to fit within +/-180 degrees. | | ||
| [latitude(degree)](#latitude) | Modifies a Latitude to fit within +/-90 degrees. | | ||
| [pointToTile(lnglat, zoom)](#pointtotile) | Get the tile for a point at a specified zoom level | | ||
| [pointToTileFraction(lnglat, zoom)](#pointtotilefraction) | Get the precise fractional tile location for a point at a zoom level | | ||
| [wrapTile(tile)](#wraptile) | Handles tiles which crosses the 180th meridian or 90th parallel | | ||
@@ -98,2 +99,3 @@ ## API | ||
- `zoom` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** Zoom level | ||
- `validate` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** validates LatLng coordinates (optional, default `true`) | ||
@@ -107,2 +109,4 @@ **Examples** | ||
Returns **Google** Google (XYZ) Tile | ||
### pointToTileFraction | ||
@@ -117,4 +121,5 @@ | ||
- `zoom` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** Zoom level | ||
- `validate` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** validates LatLng coordinates (optional, default `true`) | ||
Returns **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** tile fraction | ||
Returns **Google** Google (XYZ) Tile | ||
var tile = mercator.pointToTileFraction([1, 1], 12) | ||
@@ -509,2 +514,22 @@ //= [ 2059.3777777777777, 2036.6216445333432, 12 ] | ||
### wrapTile | ||
Wrap Tile -- Handles tiles which crosses the 180th meridian or 90th parallel | ||
**Parameters** | ||
- `tile` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** Tile | ||
- `zoom` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** Zoom Level | ||
**Examples** | ||
```javascript | ||
mercator.wrapTile([0, 3, 2]) | ||
//= [0, 3, 2] -- Valid Tile X | ||
mercator.wrapTile([4, 2, 2]) | ||
//= [0, 2, 2] -- Tile 4 does not exist, wrap around to TileX=0 | ||
``` | ||
Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** Tile X | ||
### validateZoom | ||
@@ -511,0 +536,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50958
790
647
6