Skip to content

Commit

Permalink
add support for AMD (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolovsky committed Mar 12, 2018
1 parent 5245344 commit 9a1d08e
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 81 deletions.
6 changes: 2 additions & 4 deletions .gitignore
@@ -1,8 +1,6 @@
/node_modules
/doc

.idea/workspace.xml
.idea/
/examples/birmingham/data/identify/
/examples/birmingham/data/split_data.py
/build/main_es5.js
/build/main_es5.js.map

31 changes: 21 additions & 10 deletions Gruntfile.js
Expand Up @@ -5,19 +5,28 @@ module.exports = function(grunt) {
pkg: grunt.file.readJSON('package.json'),

concat : {
options : {
},
dist : {
src : ['src/WGL.js','src/**/*.js'],
dist1 : {
src : [
'src/WGL.js',
'src/**/*.js'
],
dest : 'build/main.js'
}
},
dist2 : {
src : [
'examples/libs/jquery.csv.min.js',
'build/uglf.js'
],
dest : 'build/<%= pkg.name %>.min.js'
}
},

babel: {
options: {
sourceMap: true,
presets: ['env']
presets: [
['env', {modules: false}],
]
},
dist: {
files: {
Expand All @@ -32,8 +41,10 @@ module.exports = function(grunt) {
mangle:true
},
build: {
src: 'build/main_es5.js',
dest: 'build/<%= pkg.name %>.min.js'
src: [
'build/main_es5.js'
],
dest: 'build/uglf.js'
}
},

Expand All @@ -52,6 +63,6 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-jsdoc');

grunt.registerTask('default', ['concat','babel', 'uglify']);
grunt.registerTask('default', ['concat:dist1','babel','uglify', 'concat:dist2']);

};
5 changes: 3 additions & 2 deletions build/.gitignore
@@ -1,4 +1,5 @@
/main.js
/main.js.map
/WebGLayer.min.js

/uglf.js
/main_es5.js
/main_es5.js.map
2 changes: 1 addition & 1 deletion examples/birmingham/js/init.js
Expand Up @@ -20,7 +20,7 @@ function init() {
function visualize(data){

//wgl = new WGL(data.num,'http://localhost:9999/js/webglayer/','map');
WGL.init(data.num,'../../','map', 'OpenLayers_Map_2_OpenLayers_Container');
WGL.init(data.num,'../../','map',false, 'OpenLayers_Map_2_OpenLayers_Container');
window.onresize = function(){
WGL.resize();
}
Expand Down
2 changes: 2 additions & 0 deletions examples/libs/jquery-3.3.1.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/libs/jquery.csv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 62 additions & 59 deletions examples/libs/sphericalmercator.js
@@ -1,7 +1,14 @@
var SphericalMercator = (function(){

// Closures including constants and other precalculated values.
var cache = {},
(function (root, factory) {
if(typeof define === "function" && define.amd) {
define([],factory());
} else if(typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root.SphericalMercator = factory();
}
}(this, function() {
// Closures including constants and other precalculated values.
var cache = {},
EPSLN = 1.0e-10,
D2R = Math.PI / 180,
R2D = 180 / Math.PI,
Expand All @@ -12,35 +19,35 @@ var cache = {},

// SphericalMercator constructor: precaches calculations
// for fast tile lookups.
function SphericalMercator(options) {
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;
}
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;
};
};

// Convert lon lat to screen pixel value
//
// - `ll` {Array} `[lon, lat]` array of geographic coordinates.
// - `zoom` {Number} zoom level.
SphericalMercator.prototype.px = function(ll, zoom) {
SphericalMercator.prototype.px = function(ll, zoom) {
var d = this.zc[zoom];
var f = Math.min(Math.max(Math.sin(D2R * ll[1]), -0.9999), 0.9999);
var x = Math.round(d + ll[0] * this.Bc[zoom]);
Expand All @@ -50,18 +57,18 @@ SphericalMercator.prototype.px = function(ll, zoom) {
//(x < 0) && (x = 0);
//(y < 0) && (y = 0);
return [x, y];
};
};

// Convert screen pixel value to lon lat
//
// - `px` {Array} `[x, y]` array of geographic coordinates.
// - `zoom` {Number} zoom level.
SphericalMercator.prototype.ll = function(px, zoom) {
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)) - 0.5 * Math.PI);
return [lon, lat];
};
};

// Convert tile xyz value to bbox of the form `[w, s, e, n]`
//
Expand All @@ -71,10 +78,10 @@ SphericalMercator.prototype.ll = function(px, zoom) {
// - `tms_style` {Boolean} whether to compute using tms-style.
// - `srs` {String} projection for resulting bbox (WGS84|900913).
// - `return` {Array} bbox array of values in form `[w, s, e, n]`.
SphericalMercator.prototype.bbox = function(x, y, zoom, tms_style, srs) {
SphericalMercator.prototype.bbox = function(x, y, zoom, tms_style, srs) {
// Convert xyz into bbox with srs WGS84
if (tms_style) {
y = (Math.pow(2, zoom) - 1) - y;
y = (Math.pow(2, zoom) - 1) - y;
}
// Use +y to make sure it's a number to avoid inadvertent concatenation.
var ll = [x * this.size, (+y + 1) * this.size]; // lower left
Expand All @@ -84,11 +91,11 @@ SphericalMercator.prototype.bbox = function(x, y, zoom, tms_style, srs) {

// If web mercator requested reproject to 900913.
if (srs === '900913') {
return this.convert(bbox, '900913');
return this.convert(bbox, '900913');
} else {
return bbox;
return bbox;
}
};
};

// Convert bbox to xyx bounds
//
Expand All @@ -97,10 +104,10 @@ SphericalMercator.prototype.bbox = function(x, y, zoom, tms_style, srs) {
// - `tms_style` {Boolean} whether to compute using tms-style.
// - `srs` {String} projection of input bbox (WGS84|900913).
// - `@return` {Object} XYZ bounds containing minX, maxX, minY, maxY properties.
SphericalMercator.prototype.xyz = function(bbox, zoom, tms_style, srs) {
SphericalMercator.prototype.xyz = function(bbox, zoom, tms_style, srs) {
// If web mercator provided reproject to WGS84.
if (srs === '900913') {
bbox = this.convert(bbox, 'WGS84');
bbox = this.convert(bbox, 'WGS84');
}

var ll = [bbox[0], bbox[1]]; // lower left
Expand All @@ -111,62 +118,58 @@ SphericalMercator.prototype.xyz = function(bbox, zoom, tms_style, srs) {
var x = [ Math.floor(px_ll[0] / this.size), Math.floor((px_ur[0] - 1) / this.size) ];
var y = [ Math.floor(px_ur[1] / this.size), Math.floor((px_ll[1] - 1) / this.size) ];
var bounds = {
minX: Math.min.apply(Math, x) < 0 ? 0 : Math.min.apply(Math, x),
minY: Math.min.apply(Math, y) < 0 ? 0 : Math.min.apply(Math, y),
maxX: Math.max.apply(Math, x),
maxY: Math.max.apply(Math, y)
minX: Math.min.apply(Math, x) < 0 ? 0 : Math.min.apply(Math, x),
minY: Math.min.apply(Math, y) < 0 ? 0 : Math.min.apply(Math, y),
maxX: Math.max.apply(Math, x),
maxY: Math.max.apply(Math, y)
};
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;
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;
};
};

// Convert projection of given bbox.
//
// - `bbox` {Number} bbox in the form `[w, s, e, n]`.
// - `to` {String} projection of output bbox (WGS84|900913). Input bbox
// assumed to be the "other" projection.
// - `@return` {Object} bbox with reprojected coordinates.
SphericalMercator.prototype.convert = function(bbox, to) {
SphericalMercator.prototype.convert = function(bbox, to) {
if (to === '900913') {
return this.forward(bbox.slice(0, 2)).concat(this.forward(bbox.slice(2,4)));
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)));
return this.inverse(bbox.slice(0, 2)).concat(this.inverse(bbox.slice(2,4)));
}
};
};

// Convert lon/lat values to 900913 x/y.
SphericalMercator.prototype.forward = function(ll) {
SphericalMercator.prototype.forward = function(ll) {
var xy = [
A * ll[0] * D2R,
A * Math.log(Math.tan((Math.PI*0.25) + (0.5 * ll[1] * D2R)))
A * ll[0] * D2R,
A * Math.log(Math.tan((Math.PI*0.25) + (0.5 * ll[1] * D2R)))
];
// if xy value is beyond maxextent (e.g. poles), return maxextent.
(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;
};
};

// Convert 900913 x/y values to lon/lat.
SphericalMercator.prototype.inverse = function(xy) {
SphericalMercator.prototype.inverse = function(xy) {
return [
(xy[0] * R2D / A),
((Math.PI*0.5) - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D
(xy[0] * R2D / A),
((Math.PI*0.5) - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D
];
};

return SphericalMercator;
};

})();
return {SphericalMercator: SphericalMercator};
}));

if (typeof module !== 'undefined' && typeof exports !== 'undefined') {
module.exports = exports = SphericalMercator;
}
9 changes: 5 additions & 4 deletions examples/mapbox/index.html
Expand Up @@ -10,8 +10,11 @@
<link href="../../src/styles/wgl-selection.css" rel="stylesheet">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"/>

<script src="../libs/jquery-1.9.1.min.js"></script>
<script src="../libs/jquery-3.3.1.min.js"></script>
<script src="../libs/d3.v3.min.js"></script>
<script src="../libs/sphericalmercator.js"></script>
<script src="../libs/jquery.csv.js"></script>
<!--<script src="../../build/WebGLayer.min.js"></script>-->

<script src="../../src/WGL.js"></script>
<script src="../../src/internal/utils.js"></script>
Expand Down Expand Up @@ -49,9 +52,7 @@
<script src="init.js"></script>
<script src="DataLoader.js"></script>

<!--<script src="../libs/OpenLayers.js"></script>-->
<script src="../libs/sphericalmercator.js"></script>
<script src="../libs/jquery.csv.js"></script>

</head>
<body>

Expand Down
2 changes: 1 addition & 1 deletion examples/mapbox/init.js
Expand Up @@ -105,7 +105,7 @@ function getTopLeftTC() {
const TL3857_ZERO = {x: -20037508.34, y: 20037508.34};
const c = map.getCenter();

const proj = new SphericalMercator();
const proj = new SphericalMercator.SphericalMercator();
const center_3857 = proj.forward([c.lng, c.lat]);

return {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -4,6 +4,7 @@
"devDependencies": {
"babel-core": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015-without-strict": "0.0.4",
"grunt": "~0.4.5",
"grunt-babel": "^7.0.0",
"grunt-contrib-concat": "~1.0.0",
Expand Down
12 changes: 12 additions & 0 deletions src/WGL.js
Expand Up @@ -477,3 +477,15 @@ var WGL = (function() {
};

}());

(function (root, factory) {
if(typeof define === "function" && define.amd) {
define([],factory());
} else if(typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root.WGL = factory();
}
}(this, function() {
return WGL;
}));

0 comments on commit 9a1d08e

Please sign in to comment.