Skip to content

Commit

Permalink
Added an example that shows how to display a GeoJSON map inside a bro…
Browse files Browse the repository at this point in the history
…wser (test/index.html).
  • Loading branch information
jclo committed Aug 25, 2020
1 parent 896cd7f commit eeb1567
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
### HEAD

### 0.0.8 (August 25, 2020)

* Added an example that shows how to display a GeoJSON map inside a browser (test/index.html),
* ...,


### 0.0.7 (August 25, 2020)

* Added the method _load,
Expand Down
6 changes: 3 additions & 3 deletions _dist/lib/shp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*! ****************************************************************************
* SHP v0.0.7
* SHP v0.0.8
*
* A library for reading Natural Earth's SHP files.
* (you can download it from npm or github repositories)
Expand Down Expand Up @@ -137,7 +137,7 @@
const obj = Object.create(methods);
obj._library = {
name: 'SHP',
version: '0.0.7',
version: '0.0.8',
};
obj._dbf = {
buf: null,
Expand All @@ -154,7 +154,7 @@

// Attaches constants to SHP that provide name and version of the lib.
SHP.NAME = 'SHP';
SHP.VERSION = '0.0.7';
SHP.VERSION = '0.0.8';


// -- Private Static Methods -----------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions _dist/lib/shp.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions _dist/lib/shp.min.mjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions _dist/lib/shp.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*! ****************************************************************************
* SHP v0.0.7
* SHP v0.0.8
*
* A library for reading Natural Earth's SHP files.
* (you can download it from npm or github repositories)
Expand Down Expand Up @@ -138,7 +138,7 @@ const $__ES6GLOB = {};
const obj = Object.create(methods);
obj._library = {
name: 'SHP',
version: '0.0.7',
version: '0.0.8',
};
obj._dbf = {
buf: null,
Expand All @@ -155,7 +155,7 @@ const $__ES6GLOB = {};

// Attaches constants to SHP that provide name and version of the lib.
SHP.NAME = 'SHP';
SHP.VERSION = '0.0.7';
SHP.VERSION = '0.0.8';


// -- Private Static Methods -----------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions lib/shp.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
const obj = Object.create(methods);
obj._library = {
name: 'SHP',
version: '0.0.7',
version: '0.0.8',
};
obj._dbf = {
buf: null,
Expand All @@ -144,7 +144,7 @@

// Attaches constants to SHP that provide name and version of the lib.
SHP.NAME = 'SHP';
SHP.VERSION = '0.0.7';
SHP.VERSION = '0.0.8';


// -- Private Static Methods -----------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions lib/shp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const $__ES6GLOB = {};
const obj = Object.create(methods);
obj._library = {
name: 'SHP',
version: '0.0.7',
version: '0.0.8',
};
obj._dbf = {
buf: null,
Expand All @@ -145,7 +145,7 @@ const $__ES6GLOB = {};

// Attaches constants to SHP that provide name and version of the lib.
SHP.NAME = 'SHP';
SHP.VERSION = '0.0.7';
SHP.VERSION = '0.0.8';


// -- Private Static Methods -----------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mobilabs/shp",
"version": "0.0.7",
"version": "0.0.8",
"description": "A library for reading Natural Earth's SHP files",
"main": "_dist/lib/shp.js",
"minified": "_dist/lib/shp.min.js",
Expand Down
24 changes: 22 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,40 @@
<![endif]-->

<!-- Add your site or application content here -->
<div id="app"></div>

<!-- Add your scripts here -->
<!-- <script src="..."></script> -->
<script src="../lib/shp.js"></script>
<script src="./lib/svg.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
async function run() {

// Read the Natural Earth's db and extract a GeoJSON map:
const shp = SHP();
await shp.load('./db/ne_50m_admin_0_countries');
const collection = shp.getCollection();
const feature = shp.getFeature(1);
console.log(collection);
// const feature = shp.getFeature(1);
// console.log(collection);

// Compute the SVG Map:
const options = { scale: 2, projection: 'mercator', mirror: 'x' };
const xmlstring = SVG.to(collection, options);

// Attach it to the DOM:
const el = document.getElementById('app');
el.insertAdjacentHTML('beforeend', xmlstring);

// Set attributes:
const svg = el.firstElementChild;
svg.setAttribute('width', '1000px');
svg.setAttribute('height', '1000px');
svg.setAttribute('fill', 'gray');
svg.firstElementChild.setAttribute('transform', 'translate(500, 500) scale(1, 1)');
}

// Main
run();
});
</script>
Expand Down
166 changes: 166 additions & 0 deletions test/lib/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* eslint-disable one-var, semi-style, no-underscore-dangle, prefer-destructuring */

const SVG = (function() {
//
// -- Private Functions ------------------------------------------------------

/**
* Applies a mirror projection.
*/
function _mirror(coord, mirror) {
let c = [];

switch (mirror) {
case 'x':
c[0] = coord[0];
c[1] = -coord[1];
break;

case 'y':
c[0] = -coord[0];
c[1] = coord[1];
break;

case 'xy':
c[0] = -coord[0];
c[1] = -coord[1];
break;

default:
c = [...coord];
}

return c;
}

/**
* Applies a mercator projection.
*/
function _mercator(φdegree, λdegree, zoom, λodegree) {
const z = zoom || 1
, r = Math.PI / 180
, φ = φdegree * r
// , λ = λdegree * r
, λo = λodegree || 0
;

// let x = λ - λo;
// x /= r;
const x = λdegree - λo;
// A mercator projection for a latitude greater than 85.05° or lower
// than -85.05° tends to infinity. Thus, we limit the latitude
// to |85.05°| i.e., a projection to |180|.
let y;
if (φdegree > 85.05) {
y = 180;
} else if (φdegree < -85.05) {
y = -180;
} else {
y = Math.log(Math.tan((1 / 4) * Math.PI + (1 / 2) * φ)) / r;
}
return [x * z, y * z];
}

/**
* Applies a linear projection.
*/
function _linear(φ, λ) {
return [λ, φ];
}

/**
* Applies the requested transformations.
*/
function _transform(coord, option) {
let c;

// Projection
if (option && option.projection && option.projection === 'mercator') {
c = _mercator(coord[0], coord[1], 1, 0);
} else {
c = _linear(coord[0], coord[1]);
}

// Mirroring
if (option && option.mirror) {
c = _mirror(c, option.mirror);
}

// Scaling
if (option && option.scale && typeof option.scale === 'number' && option.scale > 1) {
c = [c[0] * option.scale, c[1] * option.scale];
}
return c;
}

/**
* Converts the coordinates to an SVG path.
*/
function _getSVGPath(coord, options) {
let path
, subpath
, x
, y
, i
, j
;

path = '';
for (i = 0; i < coord.length; i++) {
subpath = '';
for (j = 0; j < coord[i].length; j++) {
[x, y] = _transform(coord[i][j], options);
if (subpath === '') {
subpath = `M${x},${y}`;
} else {
subpath += `L${x},${y}`;
}
}
subpath += 'z';
path += subpath;
}
return path;
}

/**
* Converts a GEOJSON to an XML SVG string.
*/
function _process(json, options) {
// Add the header:
let s = [
'<!-- Made with Natural Earth. Free vector and raster map data @ naturalearthdata.com. -->\n',
'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\n',
' <g transform="translate(0, 0) scale(1, 1)">\n',
].join('');

// Add the SVG elements:
let d;
if (json.type === 'FeatureCollection') {
for (let i = 0; i < json.features.length; i++) {
d = _getSVGPath(json.features[i].geometry.coordinates, options);
s += ` <path id="" class="land" d="${d}"></path>\n`;
}
} else {
d = _getSVGPath(json.geometry.coordinates, options);
s += ` <path id="" class="land" d="${d}"></path>\n`;
}

// Append the XML Footer:
s += ' </g>\n';
s += '</svg>\n';

return s;
}


// -- Public -----------------------------------------------------------------

return {
/**
* Converts a GeoJSON to an XML SVG string.
*/
to(json, options) {
return _process(json, options);
},
};
}());

0 comments on commit eeb1567

Please sign in to comment.