Skip to content

Instantly share code, notes, and snippets.

@ungoldman
Created February 28, 2014 21:31
Show Gist options
  • Save ungoldman/9280348 to your computer and use it in GitHub Desktop.
Save ungoldman/9280348 to your computer and use it in GitHub Desktop.
requirebin sketch
var Terraformer = require('terraformer')
var point = new Terraformer.Point({
type: 'Point',
coordinates: [-122.677425, 45.521637]
})
var res = point.toMercator()
document.documentElement.innerHTML = res.coordinates
require=function e(t,r,o){function n(s,a){if(!r[s]){if(!t[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(i)return i(s,!0);throw Error("Cannot find module '"+s+"'")}var c=r[s]={exports:{}};t[s][0].call(c.exports,function(e){var r=t[s][1][e];return n(r?r:e)},c,c.exports,e,t,r,o)}return r[s].exports}for(var i="function"==typeof require&&require,s=0;o.length>s;s++)n(o[s]);return n}({"1fMU8r":[function(t,e,r){(function(t,o){"object"==typeof e&&"object"==typeof e.exports&&(r=e.exports=o()),"object"==typeof window&&(t.Terraformer=o())})(this,function(){function t(t){return"[object Array]"===Object.prototype.toString.call(t)}function e(){var t=Array.prototype.slice.apply(arguments);void 0!==typeof console&&console.warn&&console.warn.apply(console,t)}function r(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);return t}function o(t){if(t.type)switch(t.type){case"Point":return[t.coordinates[0],t.coordinates[1],t.coordinates[0],t.coordinates[1]];case"MultiPoint":return s(t.coordinates);case"LineString":return s(t.coordinates);case"MultiLineString":return n(t.coordinates);case"Polygon":return n(t.coordinates);case"MultiPolygon":return i(t.coordinates);case"Feature":return t.geometry?o(t.geometry):null;case"FeatureCollection":return a(t);case"GeometryCollection":return u(t);default:throw Error("Unknown type: "+t.type)}return null}function n(t){for(var e=null,r=null,o=null,n=null,i=0;t.length>i;i++)for(var s=t[i],a=0;s.length>a;a++){var u=s[a],c=u[0],l=u[1];null===e?e=c:e>c&&(e=c),null===r?r=c:c>r&&(r=c),null===o?o=l:o>l&&(o=l),null===n?n=l:l>n&&(n=l)}return[e,o,r,n]}function i(t){for(var e=null,r=null,o=null,n=null,i=0;t.length>i;i++)for(var s=t[i],a=0;s.length>a;a++)for(var u=s[a],c=0;u.length>c;c++){var l=u[c],h=l[0],f=l[1];null===e?e=h:e>h&&(e=h),null===r?r=h:h>r&&(r=h),null===o?o=f:o>f&&(o=f),null===n?n=f:f>n&&(n=f)}return[e,o,r,n]}function s(t){for(var e=null,r=null,o=null,n=null,i=0;t.length>i;i++){var s=t[i],a=s[0],u=s[1];null===e?e=a:e>a&&(e=a),null===r?r=a:a>r&&(r=a),null===o?o=u:o>u&&(o=u),null===n?n=u:u>n&&(n=u)}return[e,o,r,n]}function a(t){for(var e,r=[],n=t.features.length-1;n>=0;n--)e=o(t.features[n].geometry),r.push([e[0],e[1]]),r.push([e[2],e[3]]);return s(r)}function u(t){for(var e,r=[],n=t.geometries.length-1;n>=0;n--)e=o(t.geometries[n]),r.push([e[0],e[1]]),r.push([e[2],e[3]]);return s(r)}function c(t){var e=o(t);return{x:e[0],y:e[1],w:Math.abs(e[0]-e[2]),h:Math.abs(e[1]-e[3])}}function l(t){return t*Z}function h(t){return t*$}function f(t,e){for(var r=0;t.length>r;r++)"number"==typeof t[r][0]&&(t[r]=e(t[r])),"object"==typeof t[r]&&(t[r]=f(t[r],e));return t}function p(t){var e=t[0],r=t[1];return[l(e/Y)-360*Math.floor((l(e/Y)+180)/360),l(Math.PI/2-2*Math.atan(Math.exp(-1*r/Y)))]}function y(t){var e=t[0],r=Math.max(Math.min(t[1],89.99999),-89.99999);return[h(e)*Y,Y/2*Math.log((1+Math.sin(h(r)))/(1-Math.sin(h(r))))]}function d(t,e,r){if("Point"===t.type)t.coordinates=e(t.coordinates);else if("Feature"===t.type)t.geometry=d(t.geometry,e,!0);else if("FeatureCollection"===t.type)for(var o=0;t.features.length>o;o++)t.features[o]=d(t.features[o],e,!0);else if("GeometryCollection"===t.type)for(var n=0;t.geometries.length>n;n++)t.geometries[n]=d(t.geometries[n],e,!0);else t.coordinates=f(t.coordinates,e);return r||e===y&&(t.crs=_),e===p&&delete t.crs,t}function g(t){return d(t,y)}function v(t){return d(t,p)}function m(t,e){return e>t?-1:t>e?1:0}function w(t,e){return t[0]-e[0]>t[1]-e[1]?1:t[0]-e[0]<t[1]-e[1]?-1:0}function P(t,e,r){return m((e[0]-t[0])*(r[1]-t[1])-(r[0]-t[0])*(e[1]-t[1]),0)}function M(t,e){var r=e[0]-t[0],o=e[1]-t[1];return r*r+o*o}function T(t,e){var r=e;for(var o in t){var n=P(e,r,t[o]);(-1===n||0===n&&M(e,t[o])>M(e,r))&&(r=t[o])}return r}function S(t){if(0===t.length)return[];if(1===t.length)return t;for(var e=[t.sort(w)[0]],r=0;e.length>r;r++){var o=T(t,e[r]);o!==e[0]&&e.push(o)}return e}function L(t,e){for(var r=!1,o=-1,n=t.length,i=n-1;n>++o;i=o)(t[o][1]<=e[1]&&e[1]<t[i][1]||t[i][1]<=e[1]&&e[1]<t[o][1])&&e[0]<(t[i][0]-t[o][0])*(e[1]-t[o][1])/(t[i][1]-t[o][1])+t[o][0]&&(r=!r);return r}function C(t,e){if(t&&t.length){if(1===t.length)return L(t[0],e);if(L(t[0],e)){for(var r=1;t.length>r;r++)if(L(t[r],e))return!1;return!0}return!1}return!1}function x(t,e,r,o){var n=(o[0]-r[0])*(t[1]-r[1])-(o[1]-r[1])*(t[0]-r[0]),i=(e[0]-t[0])*(t[1]-r[1])-(e[1]-t[1])*(t[0]-r[0]),s=(o[1]-r[1])*(e[0]-t[0])-(o[0]-r[0])*(e[1]-t[1]);if(0!==s){var a=n/s,u=i/s;if(a>=0&&1>=a&&u>=0&&1>=u)return!0}return!1}function F(t,e){for(var r=0;t.length-1>r;r++)for(var o=0;e.length-1>o;o++)if(x(t[r],t[r+1],e[o],e[o+1]))return!0;return!1}function b(t,e){for(var r=0;e.length>r;r++)for(var o=e[r],n=0;o.length-1>n;n++)for(var i=0;t.length-1>i;i++)if(x(o[n],o[n+1],t[i],t[i+1]))return!0;return!1}function E(t,e){for(var r=0;t.length>r;r++)if(b(t[r],e))return!0;return!1}function G(t,e){for(var r=0;e.length>r;r++)return b(t,e[r])?!0:!1}function k(t,e){for(var r=0;t.length>r;r++)return G(t[r],e)?!0:!1}function O(t,e){for(var r=0;t.length>r;r++)return k(t[r],e)?!0:!1}function q(t){for(var e=[],r=0;t.length>r;r++){var o=t[r].slice();V(o[0],o[o.length-1])===!1&&o.push(o[0]),e.push(o)}return e}function V(t,e){for(var r=0;t.length>r;r++)if(t[r]!==e[r])return!1;return!0}function A(t,e){if(t.length!==e.length)return!1;for(var r=t.slice().sort(w),o=e.slice().sort(w),n=0;r.length>n;n++){if(r[n].length!==o[n].length)return!1;for(var i=0;r.length>i;i++)if(r[n][i]!==o[n][i])return!1}return!0}function H(t){if(t)switch(t.type){case"Point":return new U(t);case"MultiPoint":return new I(t);case"LineString":return new J(t);case"MultiLineString":return new N(t);case"Polygon":return new R(t);case"MultiPolygon":return new B(t);case"Feature":return new z(t);case"FeatureCollection":return new D(t);case"GeometryCollection":return new K(t);default:throw Error("Unknown type: "+t.type)}}function U(e){var o=Array.prototype.slice.call(arguments);if(e&&"Point"===e.type&&e.coordinates)r(this,e);else if(e&&t(e))this.coordinates=e;else{if(!(o.length>=2))throw"Terraformer: invalid input for Terraformer.Point";this.coordinates=o}this.type="Point"}function I(e){if(e&&"MultiPoint"===e.type&&e.coordinates)r(this,e);else{if(!t(e))throw"Terraformer: invalid input for Terraformer.MultiPoint";this.coordinates=e}this.type="MultiPoint"}function J(e){if(e&&"LineString"===e.type&&e.coordinates)r(this,e);else{if(!t(e))throw"Terraformer: invalid input for Terraformer.LineString";this.coordinates=e}this.type="LineString"}function N(e){if(e&&"MultiLineString"===e.type&&e.coordinates)r(this,e);else{if(!t(e))throw"Terraformer: invalid input for Terraformer.MultiLineString";this.coordinates=e}this.type="MultiLineString"}function R(e){if(e&&"Polygon"===e.type&&e.coordinates)r(this,e);else{if(!t(e))throw"Terraformer: invalid input for Terraformer.Polygon";this.coordinates=e}this.type="Polygon"}function B(e){if(e&&"MultiPolygon"===e.type&&e.coordinates)r(this,e);else{if(!t(e))throw"Terraformer: invalid input for Terraformer.MultiPolygon";this.coordinates=e}this.type="MultiPolygon"}function z(t){if(t&&"Feature"===t.type)r(this,t);else{if(!(t&&t.type&&t.coordinates))throw"Terraformer: invalid input for Terraformer.Feature";this.geometry=t}this.type="Feature"}function D(e){if(e&&"FeatureCollection"===e.type&&e.features)r(this,e);else{if(!t(e))throw"Terraformer: invalid input for Terraformer.FeatureCollection";this.features=e}this.type="FeatureCollection"}function K(e){if(e&&"GeometryCollection"===e.type&&e.geometries)r(this,e);else if(t(e))this.geometries=e;else{if(!e.coordinates||!e.type)throw"Terraformer: invalid input for Terraformer.GeometryCollection";this.type="GeometryCollection",this.geometries=[e]}this.type="GeometryCollection"}function Q(t,e,r){for(var o=y(t),n=r||64,i={type:"Polygon",coordinates:[[]]},s=1;n>=s;s++){var a=s*(360/n)*Math.PI/180;i.coordinates[0].push([o[0]+e*Math.cos(a),o[1]+e*Math.sin(a)])}return i.coordinates=q(i.coordinates),v(i)}function W(t,e,o){var n=o||64,i=e||250;if(!t||2>t.length||!i||!n)throw Error("Terraformer: missing parameter for Terraformer.Circle");r(this,new z({type:"Feature",geometry:Q(t,i,n),properties:{radius:i,center:t,steps:n}}))}var X={},Y=6378137,Z=57.29577951308232,$=.017453292519943,_={type:"link",properties:{href:"http://spatialreference.org/ref/sr-org/6928/ogcwkt/",type:"ogcwkt"}},te={type:"link",properties:{href:"http://spatialreference.org/ref/epsg/4326/ogcwkt/",type:"ogcwkt"}},ee=["length"];return H.prototype.toMercator=function(){return g(this)},H.prototype.toGeographic=function(){return v(this)},H.prototype.envelope=function(){return c(this)},H.prototype.bbox=function(){return o(this)},H.prototype.convexHull=function(){var t,e,r=[];if("Point"===this.type)return null;if("LineString"===this.type||"MultiPoint"===this.type){if(!(this.coordinates&&this.coordinates.length>=3))return null;r=this.coordinates}else if("Polygon"===this.type||"MultiLineString"===this.type){if(!(this.coordinates&&this.coordinates.length>0))return null;for(t=0;this.coordinates.length>t;t++)r=r.concat(this.coordinates[t]);if(3>r.length)return null}else if("MultiPolygon"===this.type){if(!(this.coordinates&&this.coordinates.length>0))return null;for(t=0;this.coordinates.length>t;t++)for(e=0;this.coordinates[t].length>e;e++)r=r.concat(this.coordinates[t][e]);if(3>r.length)return null}else if("Feature"===this.type){var o=new H(this.geometry);return o.convexHull()}return new R({type:"Polygon",coordinates:q([S(r)])})},H.prototype.toJSON=function(){var t={};for(var e in this)this.hasOwnProperty(e)&&-1===ee.indexOf(e)&&(t[e]=this[e]);return t.bbox=o(this),t},H.prototype.contains=function(t){return new H(t).within(this)},H.prototype.within=function(t){var e,r,o;if("Point"===t.type&&"Point"===this.type)return V(this.coordinates,t.coordinates);if("MultiLineString"===t.type&&"Point"===this.type)for(r=0;t.coordinates.length>r;r++){var n={type:"LineString",coordinates:t.coordinates[r]};if(this.within(n))return!0}if(("LineString"===t.type||"MultiPoint"===t.type)&&"Point"===this.type)for(r=0;t.coordinates.length>r;r++){if(this.coordinates.length!==t.coordinates[r].length)return!1;if(V(this.coordinates,t.coordinates[r]))return!0}if("Polygon"===t.type){if("Polygon"===this.type){if(t.coordinates.length===this.coordinates.length)for(r=0;this.coordinates.length>r;r++)if(A(this.coordinates[r],t.coordinates[r]))return!0;return this.coordinates.length&&C(t.coordinates,this.coordinates[0][0])?!E(q(this.coordinates),q(t.coordinates)):!1}if("Point"===this.type)return C(t.coordinates,this.coordinates);if("LineString"===this.type||"MultiPoint"===this.type){if(!this.coordinates||0===this.coordinates.length)return!1;for(r=0;this.coordinates.length>r;r++)if(C(t.coordinates,this.coordinates[r])===!1)return!1;return!0}if("MultiLineString"===this.type){for(r=0;this.coordinates.length>r;r++){var i=new J(this.coordinates[r]);if(i.within(t)===!1)return o++,!1}return!0}if("MultiPolygon"===this.type){for(r=0;this.coordinates.length>r;r++){var s=new H({type:"Polygon",coordinates:this.coordinates[r]});if(s.within(t)===!1)return!1}return!0}}if("MultiPolygon"===t.type){if("Point"===this.type){if(t.coordinates.length)for(r=0;t.coordinates.length>r;r++)if(e=t.coordinates[r],C(e,this.coordinates)&&E(this.coordinates,t.coordinates)===!1)return!0;return!1}if("Polygon"===this.type){for(r=0;this.coordinates.length>r;r++)if(t.coordinates[r].length===this.coordinates.length)for(j=0;this.coordinates.length>j;j++)if(A(this.coordinates[j],t.coordinates[r][j]))return!0;if(k(this.coordinates,t.coordinates)===!1&&t.coordinates.length){for(r=0;t.coordinates.length>r;r++)e=t.coordinates[r],o=C(e,this.coordinates[0][0])===!1?!1:!0;return o}}else if("LineString"===this.type||"MultiPoint"===this.type)for(r=0;t.coordinates.length>r;r++){var a={type:"Polygon",coordinates:t.coordinates[r]};return this.within(a)?!0:!1}else{if("MultiLineString"===this.type){for(r=0;this.coordinates.length>r;r++){var u=new J(this.coordinates[r]);if(u.within(t)===!1)return!1}return!0}if("MultiPolygon"===this.type){for(r=0;t.coordinates.length>r;r++){var c={type:"Polygon",coordinates:t.coordinates[r]};if(this.within(c)===!1)return!1}return!0}}}return!1},H.prototype.intersects=function(t){"Feature"===t.type&&(t=t.geometry);var r=new H(t);if(this.within(t)||r.within(this))return!0;if("LineString"===this.type){if("LineString"===t.type)return F(this.coordinates,t.coordinates);if("MultiLineString"===t.type)return b(this.coordinates,t.coordinates);if("Polygon"===t.type)return b(this.coordinates,q(t.coordinates));if("MultiPolygon"===t.type)return G(this.coordinates,t.coordinates)}else if("MultiLineString"===this.type){if("LineString"===t.type)return b(t.coordinates,this.coordinates);if("Polygon"===t.type||"MultiLineString"===t.type)return E(this.coordinates,t.coordinates);if("MultiPolygon"===t.type)return k(this.coordinates,t.coordinates)}else if("Polygon"===this.type){if("LineString"===t.type)return b(t.coordinates,q(this.coordinates));if("MultiLineString"===t.type)return E(q(this.coordinates),t.coordinates);if("Polygon"===t.type)return E(q(this.coordinates),q(t.coordinates));if("MultiPolygon"===t.type)return k(q(this.coordinates),t.coordinates)}else if("MultiPolygon"===this.type){if("LineString"===t.type)return G(t.coordinates,this.coordinates);if("Polygon"===t.type||"MultiLineString"===t.type)return k(q(t.coordinates),this.coordinates);if("MultiPolygon"===t.type)return O(this.coordinates,t.coordinates)}else if("Feature"===this.type){var o=new H(this.geometry);return o.intersects(t)}return e("Type "+this.type+" to "+t.type+" intersection is not supported by intersects"),!1},U.prototype=new H,U.prototype.constructor=U,I.prototype=new H,I.prototype.constructor=I,I.prototype.forEach=function(t){for(var e=0;this.coordinates.length>e;e++)t.apply(this,[this.coordinates[e],e,this.coordinates]);return this},I.prototype.addPoint=function(t){return this.coordinates.push(t),this},I.prototype.insertPoint=function(t,e){return this.coordinates.splice(e,0,t),this},I.prototype.removePoint=function(t){return"number"==typeof t?this.coordinates.splice(t,1):this.coordinates.splice(this.coordinates.indexOf(t),1),this},I.prototype.get=function(t){return new U(this.coordinates[t])},J.prototype=new H,J.prototype.constructor=J,J.prototype.addVertex=function(t){return this.coordinates.push(t),this},J.prototype.insertVertex=function(t,e){return this.coordinates.splice(e,0,t),this},J.prototype.removeVertex=function(t){return this.coordinates.splice(t,1),this},N.prototype=new H,N.prototype.constructor=N,N.prototype.forEach=function(t){for(var e=0;this.coordinates.length>e;e++)t.apply(this,[this.coordinates[e],e,this.coordinates])},N.prototype.get=function(t){return new J(this.coordinates[t])},R.prototype=new H,R.prototype.constructor=R,R.prototype.addVertex=function(t){return this.coordinates[0].push(t),this},R.prototype.insertVertex=function(t,e){return this.coordinates[0].splice(e,0,t),this},R.prototype.removeVertex=function(t){return this.coordinates[0].splice(t,1),this},R.prototype.close=function(){this.coordinates=q(this.coordinates)},B.prototype=new H,B.prototype.constructor=B,B.prototype.forEach=function(t){for(var e=0;this.coordinates.length>e;e++)t.apply(this,[this.coordinates[e],e,this.coordinates])},B.prototype.get=function(t){return new R(this.coordinates[t])},B.prototype.close=function(){var t=[];return this.forEach(function(e){t.push(q(e))}),this.coordinates=t,this},z.prototype=new H,z.prototype.constructor=z,D.prototype=new H,D.prototype.constructor=D,D.prototype.forEach=function(t){for(var e=0;this.features.length>e;e++)t.apply(this,[this.features[e],e,this.features])},D.prototype.get=function(t){var e;return this.forEach(function(r){r.id===t&&(e=r)}),new z(e)},K.prototype=new H,K.prototype.constructor=K,K.prototype.forEach=function(t){for(var e=0;this.geometries.length>e;e++)t.apply(this,[this.geometries[e],e,this.geometries])},K.prototype.get=function(t){return new H(this.geometries[t])},W.prototype=new H,W.prototype.constructor=W,W.prototype.recalculate=function(){return this.geometry=Q(this.properties.center,this.properties.radius,this.properties.steps),this},W.prototype.center=function(t){return t&&(this.properties.center=t,this.recalculate()),this.properties.center},W.prototype.radius=function(t){return t&&(this.properties.radius=t,this.recalculate()),this.properties.radius},W.prototype.steps=function(t){return t&&(this.properties.steps=t,this.recalculate()),this.properties.steps},W.prototype.toJSON=function(){var t=H.prototype.toJSON.call(this);return t},X.Primitive=H,X.Point=U,X.MultiPoint=I,X.LineString=J,X.MultiLineString=N,X.Polygon=R,X.MultiPolygon=B,X.Feature=z,X.FeatureCollection=D,X.GeometryCollection=K,X.Circle=W,X.toMercator=g,X.toGeographic=v,X.Tools={},X.Tools.positionToMercator=y,X.Tools.positionToGeographic=p,X.Tools.applyConverter=d,X.Tools.toMercator=g,X.Tools.toGeographic=v,X.Tools.createCircle=Q,X.Tools.calculateBounds=o,X.Tools.calculateEnvelope=c,X.Tools.coordinatesContainPoint=L,X.Tools.polygonContainsPoint=C,X.Tools.arrayIntersectsArray=F,X.Tools.coordinatesContainPoint=L,X.Tools.coordinatesEqual=A,X.Tools.convexHull=S,X.MercatorCRS=_,X.GeographicCRS=te,X})},{}],terraformer:[function(t,e){e.exports=t("1fMU8r")},{}]},{},[]);var Terraformer=require("terraformer"),point=new Terraformer.Point({type:"Point",coordinates:[-122.677425,45.521637]}),res=point.toMercator();document.documentElement.innerHTML=res.coordinates;
<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