UNPKG

reproject-spherical-mercator

Version:

Reproject geojson geometry from WGS84 to Spherical Mercator

1 lines 4.59 kB
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;"undefined"!=typeof window?e=window:"undefined"!=typeof global?e=global:"undefined"!=typeof self&&(e=self),e.reproject=t()}}(function(){return function t(e,r,n){function i(s,c){if(!r[s]){if(!e[s]){var a="function"==typeof require&&require;if(!c&&a)return a(s,!0);if(o)return o(s,!0);var u=new Error("Cannot find module '"+s+"'");throw u.code="MODULE_NOT_FOUND",u}var f=r[s]={exports:{}};e[s][0].call(f.exports,function(t){var r=e[s][1][t];return i(r?r:t)},f,f.exports,t,e,r,n)}return r[s].exports}for(var o="function"==typeof require&&require,s=0;s<n.length;s++)i(n[s]);return i}({1:[function(t,e){function r(t){return t.map(function(t){return o.forward(t)})}var n=t("sphericalmercator"),i=t("geojson-bbox"),o=new n({size:256}),s=function(t){var e={type:t.type};if("Point"==t.type)e.coordinates=o.forward(t.coordinates);else if("LineString"==t.type||"MultiPoint"==t.type)e.coordinates=r(t.coordinates);else if("Polygon"==t.type||"MultiLineString"==t.type)e.coordinates=t.coordinates.map(function(t){return r(t)});else if("MultiPolygon"==t.type)e.coordinates=t.coordinates.map(function(t){return t.map(function(t){return r(t)})});else if("Feature"==t.type)e.geometry=s(t.geometry),t.id&&(e.id=t.id),t.properties&&(e.properties=t.properties),t.bbox&&(e.bbox=i(e));else if("FeatureCollection"==t.type)e.features=t.features.map(function(t){return s(t)}),t.bbox&&(e.bbox=i(e));else{if("GeometryCollection"!=t.type)throw new Error("Not a valid geojson");e.geometries=t.geometries.map(function(t){return s(t)})}return e};e.exports=s},{"geojson-bbox":2,sphericalmercator:3}],2:[function(t,e){function r(t){var e;return"Point"==t.type?e=[t.coordinates]:"LineString"==t.type||"MultiPoint"==t.type?e=t.coordinates:"Polygon"==t.type||"MultiLineString"==t.type?e=t.coordinates.reduce(function(t,e){return t.concat(e)},[]):"MultiPolygon"==t.type?e=t.coordinates.reduce(function(t,e){return t.concat(e.reduce(function(t,e){return t.concat(e)},[]))},[]):"Feature"==t.type?e=r(t.geometry):"GeometryCollection"==t.type?e=t.geometries.reduce(function(t,e){return t.concat(r(e))},[]):"FeatureCollection"==t.type&&(e=t.features.reduce(function(t,e){return t.concat(r(e))},[])),e}e.exports=function(t){var e,n;if(t.hasOwnProperty("type"))return e=r(t),n=[Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY],e.reduce(function(t,e){return[Math.min(e[0],t[0]),Math.min(e[1],t[1]),Math.max(e[0],t[2]),Math.max(e[1],t[3])]},n)}},{}],3:[function(t,e,r){var n=function(){function t(t){if(t=t||{},this.size=t.size||256,!e[this.size]){var r=this.size,n=e[this.size]={};n.Bc=[],n.Cc=[],n.zc=[],n.Ac=[];for(var i=0;30>i;i++)n.Bc.push(r/360),n.Cc.push(r/(2*Math.PI)),n.zc.push(r/2),n.Ac.push(r),r*=2}this.Bc=e[this.size].Bc,this.Cc=e[this.size].Cc,this.zc=e[this.size].zc,this.Ac=e[this.size].Ac}var e={},r=Math.PI/180,n=180/Math.PI,i=6378137,o=20037508.342789244;return t.prototype.px=function(t,e){var n=this.zc[e],i=Math.min(Math.max(Math.sin(r*t[1]),-.9999),.9999),o=Math.round(n+t[0]*this.Bc[e]),s=Math.round(n+.5*Math.log((1+i)/(1-i))*-this.Cc[e]);return o>this.Ac[e]&&(o=this.Ac[e]),s>this.Ac[e]&&(s=this.Ac[e]),[o,s]},t.prototype.ll=function(t,e){var r=(t[1]-this.zc[e])/-this.Cc[e],i=(t[0]-this.zc[e])/this.Bc[e],o=n*(2*Math.atan(Math.exp(r))-.5*Math.PI);return[i,o]},t.prototype.bbox=function(t,e,r,n,i){n&&(e=Math.pow(2,r)-1-e);var o=[t*this.size,(+e+1)*this.size],s=[(+t+1)*this.size,e*this.size],c=this.ll(o,r).concat(this.ll(s,r));return"900913"===i?this.convert(c,"900913"):c},t.prototype.xyz=function(t,e,r,n){"900913"===n&&(t=this.convert(t,"WGS84"));var i=[t[0],t[1]],o=[t[2],t[3]],s=this.px(i,e),c=this.px(o,e),a={minX:Math.floor(s[0]/this.size),minY:Math.floor(c[1]/this.size),maxX:Math.floor((c[0]-1)/this.size),maxY:Math.floor((s[1]-1)/this.size)};if(r){var u={minY:Math.pow(2,e)-1-a.maxY,maxY:Math.pow(2,e)-1-a.minY};a.minY=u.minY,a.maxY=u.maxY}return a},t.prototype.convert=function(t,e){return"900913"===e?this.forward(t.slice(0,2)).concat(this.forward(t.slice(2,4))):this.inverse(t.slice(0,2)).concat(this.inverse(t.slice(2,4)))},t.prototype.forward=function(t){var e=[i*t[0]*r,i*Math.log(Math.tan(.25*Math.PI+.5*t[1]*r))];return e[0]>o&&(e[0]=o),e[0]<-o&&(e[0]=-o),e[1]>o&&(e[1]=o),e[1]<-o&&(e[1]=-o),e},t.prototype.inverse=function(t){return[t[0]*n/i,(.5*Math.PI-2*Math.atan(Math.exp(-t[1]/i)))*n]},t}();"undefined"!=typeof e&&"undefined"!=typeof r&&(e.exports=r=n)},{}]},{},[1])(1)});