Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created February 20, 2015 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/64a6c2af5deac5f54a48 to your computer and use it in GitHub Desktop.
Save tmcw/64a6c2af5deac5f54a48 to your computer and use it in GitHub Desktop.
requirebin sketch
var viewport = require('geo-viewport');
var output = document.body.appendChild(document.createElement('pre'));
output.innerHTML = JSON.stringify(viewport.bounds([0,0], 2, [200,200]), null, 2);
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var SphericalMercator=function(){var cache={},EPSLN=1e-10,D2R=Math.PI/180,R2D=180/Math.PI,A=6378137,MAXEXTENT=20037508.342789244;function SphericalMercator(options){options=options||{};this.size=options.size||256;if(!cache[this.size]){var size=this.size;var c=cache[this.size]={};c.Bc=[];c.Cc=[];c.zc=[];c.Ac=[];for(var d=0;d<30;d++){c.Bc.push(size/360);c.Cc.push(size/(2*Math.PI));c.zc.push(size/2);c.Ac.push(size);size*=2}}this.Bc=cache[this.size].Bc;this.Cc=cache[this.size].Cc;this.zc=cache[this.size].zc;this.Ac=cache[this.size].Ac}SphericalMercator.prototype.px=function(ll,zoom){var d=this.zc[zoom];var f=Math.min(Math.max(Math.sin(D2R*ll[1]),-.9999),.9999);var x=Math.round(d+ll[0]*this.Bc[zoom]);var y=Math.round(d+.5*Math.log((1+f)/(1-f))*-this.Cc[zoom]);x>this.Ac[zoom]&&(x=this.Ac[zoom]);y>this.Ac[zoom]&&(y=this.Ac[zoom]);return[x,y]};SphericalMercator.prototype.ll=function(px,zoom){var g=(px[1]-this.zc[zoom])/-this.Cc[zoom];var lon=(px[0]-this.zc[zoom])/this.Bc[zoom];var lat=R2D*(2*Math.atan(Math.exp(g))-.5*Math.PI);return[lon,lat]};SphericalMercator.prototype.bbox=function(x,y,zoom,tms_style,srs){if(tms_style){y=Math.pow(2,zoom)-1-y}var ll=[x*this.size,(+y+1)*this.size];var ur=[(+x+1)*this.size,y*this.size];var bbox=this.ll(ll,zoom).concat(this.ll(ur,zoom));if(srs==="900913"){return this.convert(bbox,"900913")}else{return bbox}};SphericalMercator.prototype.xyz=function(bbox,zoom,tms_style,srs){if(srs==="900913"){bbox=this.convert(bbox,"WGS84")}var ll=[bbox[0],bbox[1]];var ur=[bbox[2],bbox[3]];var px_ll=this.px(ll,zoom);var px_ur=this.px(ur,zoom);var bounds={minX:Math.floor(px_ll[0]/this.size),minY:Math.floor(px_ur[1]/this.size),maxX:Math.floor((px_ur[0]-1)/this.size),maxY:Math.floor((px_ll[1]-1)/this.size)};if(tms_style){var tms={minY:Math.pow(2,zoom)-1-bounds.maxY,maxY:Math.pow(2,zoom)-1-bounds.minY};bounds.minY=tms.minY;bounds.maxY=tms.maxY}return bounds};SphericalMercator.prototype.convert=function(bbox,to){if(to==="900913"){return this.forward(bbox.slice(0,2)).concat(this.forward(bbox.slice(2,4)))}else{return this.inverse(bbox.slice(0,2)).concat(this.inverse(bbox.slice(2,4)))}};SphericalMercator.prototype.forward=function(ll){var xy=[A*ll[0]*D2R,A*Math.log(Math.tan(Math.PI*.25+.5*ll[1]*D2R))];xy[0]>MAXEXTENT&&(xy[0]=MAXEXTENT);xy[0]<-MAXEXTENT&&(xy[0]=-MAXEXTENT);xy[1]>MAXEXTENT&&(xy[1]=MAXEXTENT);xy[1]<-MAXEXTENT&&(xy[1]=-MAXEXTENT);return xy};SphericalMercator.prototype.inverse=function(xy){return[xy[0]*R2D/A,(Math.PI*.5-2*Math.atan(Math.exp(-xy[1]/A)))*R2D]};return SphericalMercator}();if(typeof module!=="undefined"&&typeof exports!=="undefined"){module.exports=exports=SphericalMercator}},{}],"geo-viewport":[function(require,module,exports){var merc=new(require("sphericalmercator"));module.exports.viewport=viewport;module.exports.bounds=bounds;function viewport(bounds,dimensions,minzoom,maxzoom){minzoom=minzoom===undefined?0:minzoom;maxzoom=maxzoom===undefined?20:maxzoom;var base=maxzoom,bl=merc.px([bounds[0],bounds[1]],base),tr=merc.px([bounds[2],bounds[3]],base),width=tr[0]-bl[0],height=bl[1]-tr[1],ratios=[width/dimensions[0],height/dimensions[1]],center=[(bounds[0]+bounds[2])/2,(bounds[1]+bounds[3])/2],adjusted=Math.floor(Math.min(base-Math.log(ratios[0])/Math.log(2),base-Math.log(ratios[1])/Math.log(2))),zoom=Math.max(minzoom,Math.min(maxzoom,adjusted));return{center:center,zoom:zoom}}function bounds(viewport,zoom,dimensions){if(viewport.lon!==undefined){viewport=[viewport.lon,viewport.lat]}var px=merc.px(viewport,zoom);var tl=merc.ll([px[0]-dimensions[0]/2,px[1]-dimensions[1]/2],zoom);var br=merc.ll([px[0]+dimensions[0]/2,px[1]+dimensions[1]/2],zoom);return[tl[0],br[1],br[0],tl[1]]}function baseLog(x,y){return Math.log(y)/Math.log(x)}},{sphericalmercator:1}]},{},[]);var viewport=require("geo-viewport");var output=document.body.appendChild(document.createElement("pre"));output.innerHTML=JSON.stringify(viewport.bounds([0,0],2,[200,200]),null,2);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"geo-viewport": "0.1.1"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment