Skip to content

Commit

Permalink
Add longitude tests #758 @turf/hex-grid (#763)
Browse files Browse the repository at this point in the history
* Add longitude tests #758 @turf/hex-grid

* Drop meta from tests

* Center x & y Fraction using mid point

* Add tests for distance
  • Loading branch information
DenisCarriere authored May 28, 2017
1 parent 2439c1a commit 4ed6afc
Show file tree
Hide file tree
Showing 20 changed files with 147,704 additions and 206,748 deletions.
51 changes: 25 additions & 26 deletions packages/turf-distance/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const write = require('write-json-file');
const load = require('load-json-file');
const fs = require('fs');
const path = require('path');
const test = require('tape');
const load = require('load-json-file');
const write = require('write-json-file');
const {point} = require('@turf/helpers');
const distance = require('./');

const directories = {
Expand All @@ -19,30 +20,28 @@ const fixtures = fs.readdirSync(directories.in).map(filename => {
});

test('distance', t => {
for (const {name, geojson} of fixtures) {
const pt1 = geojson.features[0];
const pt2 = geojson.features[1];
const miles = distance(pt1, pt2, 'miles');
const nauticalmiles = distance(pt1, pt2, 'nauticalmiles');
const kilometers = distance(pt1, pt2, 'kilometers');
const radians = distance(pt1, pt2, 'radians');
const degrees = distance(pt1, pt2, 'degrees');

if (process.env.REGEN) {
write.sync(directories.out + name + '-miles.json', miles);
write.sync(directories.out + name + '-nauticalmiles.json', nauticalmiles);
write.sync(directories.out + name + '-kilometers.json', kilometers);
write.sync(directories.out + name + '-radians.json', radians);
write.sync(directories.out + name + '-degrees.json', degrees);
}
t.deepEqual(miles, load.sync(directories.out + name + '-miles.json'), name + '-miles');
t.deepEqual(nauticalmiles, load.sync(directories.out + name + '-nauticalmiles.json'), name + '-nauticalmiles');
t.deepEqual(kilometers, load.sync(directories.out + name + '-kilometers.json'), name + '-kilometers');
t.deepEqual(radians, load.sync(directories.out + name + '-radians.json'), name + '-radians');
t.deepEqual(degrees, load.sync(directories.out + name + '-degrees.json'), name + '-degrees');
t.throws(() => {
distance(pt1, pt2, 'blah');
}, 'unknown option given to units');
for (const {name, geojson} of fixtures) {
const [pt1, pt2] = geojson.features;
const distances = {
miles: distance(pt1, pt2, 'miles'),
nauticalmiles: distance(pt1, pt2, 'nauticalmiles'),
kilometers: distance(pt1, pt2, 'kilometers'),
radians: distance(pt1, pt2, 'radians'),
degrees: distance(pt1, pt2, 'degrees')
};
if (process.env.REGEN) write.sync(directories.out + name + '.json', distances);
t.deepEqual(distances, load.sync(directories.out + name + '.json'), name);
}
t.end();
});

// https://github.com/Turfjs/turf/issues/758
test('distance -- Issue #758', t => {
t.equal(Math.round(distance(point([-180, -90]), point([180, -90]))), 0, 'should be 0');
t.end();
});

test('distance -- throws', t => {
t.throws(() => distance(point([0, 0]), point([10, 10]), 'blah'), /units is invalid/);
t.end();
});
1 change: 0 additions & 1 deletion packages/turf-distance/test/out/points-degrees.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/turf-distance/test/out/points-kilometers.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/turf-distance/test/out/points-miles.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/turf-distance/test/out/points-nauticalmiles.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/turf-distance/test/out/points-radians.json

This file was deleted.

7 changes: 7 additions & 0 deletions packages/turf-distance/test/out/points.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"miles": 60.37218405837491,
"nauticalmiles": 52.461979624130436,
"kilometers": 97.15957803131901,
"radians": 0.015245501024842149,
"degrees": 0.8735028650863799
}
24 changes: 16 additions & 8 deletions packages/turf-hex-grid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,25 @@ for (var i = 0; i < 6; i++) {
* var addToMap = [hexgrid]
*/
module.exports = function hexGrid(bbox, cellSize, units, triangles) {
var xFraction = cellSize / (distance(point([bbox[0], bbox[1]]), point([bbox[2], bbox[1]]), units));
var cellWidth = xFraction * (bbox[2] - bbox[0]);
var yFraction = cellSize / (distance(point([bbox[0], bbox[1]]), point([bbox[0], bbox[3]]), units));
var cellHeight = yFraction * (bbox[3] - bbox[1]);
var west = bbox[0];
var south = bbox[1];
var east = bbox[2];
var north = bbox[3];
var centerY = (south + north) / 2;
var centerX = (west + east) / 2;

// https://github.com/Turfjs/turf/issues/758
var xFraction = cellSize / (distance(point([west, centerY]), point([east, centerY]), units));
var cellWidth = xFraction * (east - west);
var yFraction = cellSize / (distance(point([centerX, south]), point([centerX, north]), units));
var cellHeight = yFraction * (north - south);
var radius = cellWidth / 2;

var hex_width = radius * 2;
var hex_height = Math.sqrt(3) / 2 * cellHeight;

var box_width = bbox[2] - bbox[0];
var box_height = bbox[3] - bbox[1];
var box_width = east - west;
var box_height = north - south;

var x_interval = 3 / 4 * hex_width;
var y_interval = hex_height;
Expand Down Expand Up @@ -81,8 +89,8 @@ module.exports = function hexGrid(bbox, cellSize, units, triangles) {
continue;
}

var center_x = x * x_interval + bbox[0] - x_adjust;
var center_y = y * y_interval + bbox[1] + y_adjust;
var center_x = x * x_interval + west - x_adjust;
var center_y = y * y_interval + south + y_adjust;

if (isOdd) {
center_y -= hex_height / 2;
Expand Down
25 changes: 25 additions & 0 deletions packages/turf-hex-grid/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ const bbox1 = require(directories.in + 'bbox1.json');
const bbox2 = require(directories.in + 'bbox2.json');
const bbox3 = require(directories.in + 'bbox3.json');
const bbox4 = require(directories.in + 'bbox4.json');
const bbox5 = require(directories.in + 'bbox5.json');

test('hex-grid', t => {
const grid1 = truncate(grid(bbox1, 50, 'miles'));
const grid2 = truncate(grid(bbox2, 5, 'miles'));
const grid3 = truncate(grid(bbox3, 2, 'miles'));
const grid4 = truncate(grid(bbox4, 50, 'kilometers'));
const grid5 = truncate(grid(bbox5, 500, 'kilometers'));

t.ok(grid1.features.length, '50mi grid');
t.ok(grid2.features.length, '5mi grid');
t.ok(grid3.features.length, '2mi grid');
t.ok(grid4.features.length, '50km grid');
t.ok(grid5.features.length, '500km grid');

t.equal(grid(bbox1, 100, 'miles').features.length, 85);

Expand All @@ -33,11 +36,13 @@ test('hex-grid', t => {
write.sync(directories.out + 'grid2.geojson', grid2);
write.sync(directories.out + 'grid3.geojson', grid3);
write.sync(directories.out + 'grid4.geojson', grid4);
write.sync(directories.out + 'grid5.geojson', grid5);
}
t.deepEqual(load.sync(directories.out + 'grid1.geojson'), grid1, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'grid2.geojson'), grid2, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'grid3.geojson'), grid3, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'grid4.geojson'), grid4, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'grid5.geojson'), grid5, 'grid is correct');

t.end();
});
Expand All @@ -47,11 +52,13 @@ test('hex-tri-grid', t => {
const grid2 = truncate(grid(bbox2, 5, 'miles', true));
const grid3 = truncate(grid(bbox3, 2, 'miles', true));
const grid4 = truncate(grid(bbox4, 50, 'kilometers', true));
const grid5 = truncate(grid(bbox5, 500, 'kilometers', true));

t.ok(grid1.features.length, '50mi grid');
t.ok(grid2.features.length, '5mi grid');
t.ok(grid3.features.length, '2mi grid');
t.ok(grid4.features.length, '50km grid');
t.ok(grid5.features.length, '500km grid');

t.equal(grid(bbox1, 100, 'miles').features.length, 85);

Expand All @@ -60,11 +67,29 @@ test('hex-tri-grid', t => {
write.sync(directories.out + 'trigrid2.geojson', grid2);
write.sync(directories.out + 'trigrid3.geojson', grid3);
write.sync(directories.out + 'trigrid4.geojson', grid4);
write.sync(directories.out + 'trigrid5.geojson', grid5);
}
t.deepEqual(load.sync(directories.out + 'trigrid1.geojson'), grid1, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'trigrid2.geojson'), grid2, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'trigrid3.geojson'), grid3, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'trigrid4.geojson'), grid4, 'grid is correct');
t.deepEqual(load.sync(directories.out + 'trigrid5.geojson'), grid5, 'grid is correct');

t.end();
});

test('longitude (13141439571036224) issue #758', t => {
const bbox = [-179, -90, 179, 90];
const hexgrid = grid(bbox, 500, 'kilometers');

const coords = [];
hexgrid.features.forEach(feature => feature.geometry.coordinates[0].forEach(coord => coords.push(coord)));

for (const [lng, lat] of coords) {
if (lng > 1000 || lng < -1000) {
t.fail(`longitude is +- 1000 [${lng},${lat}]`);
break;
}
}
t.end();
});
6 changes: 6 additions & 0 deletions packages/turf-hex-grid/test/in/bbox5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
-185,
-90,
180,
90
]
Loading

0 comments on commit 4ed6afc

Please sign in to comment.