Skip to content

Commit

Permalink
Add some tests for linestrings
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Oct 9, 2019
1 parent 81d30c6 commit 4f62eaa
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
8 changes: 8 additions & 0 deletions tests/testdata/boundary.geojson
@@ -0,0 +1,8 @@
{
"type": "FeatureCollection",
"name": "boundary",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "popupContent": "This is the Auraria West Campus", "style": { "weight": 2, "color": "#999", "opacity": 1, "fillColor": "#B0DE5C", "fillOpacity": 0.800000 } }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ -105.00432014465332, 39.747321954898609 ], [ -105.007152557373047, 39.746200068351698 ], [ -105.00921249389647, 39.744682192770377 ], [ -105.010671615600586, 39.743626259601051 ], [ -105.011959075927734, 39.742900296160542 ], [ -105.009899139404311, 39.74078835902781 ], [ -105.00758171081543, 39.740590361603168 ], [ -105.003461837768555, 39.740590361603168 ], [ -105.00097274780272, 39.740590361603168 ], [ -105.000629425048814, 39.740722359949459 ], [ -105.000200271606445, 39.741910333688651 ], [ -105.000715255737305, 39.742768301986011 ], [ -105.00097274780272, 39.743692255898182 ], [ -105.00097274780272, 39.744616197421358 ], [ -105.001230239868164, 39.745342142783947 ], [ -105.001831054687514, 39.746134074456528 ], [ -105.00432014465332, 39.747321954898609 ] ], [ [ -105.003612041473374, 39.743543764140718 ], [ -105.003011226654053, 39.742784801271632 ], [ -105.002217292785645, 39.743164283751078 ], [ -105.002839565277114, 39.743906743427409 ], [ -105.003612041473374, 39.743543764140718 ] ], [ [ -105.009427070617676, 39.739897366137079 ], [ -105.009427070617676, 39.739105362785658 ], [ -105.006852149963379, 39.739237363976308 ], [ -105.003848075866713, 39.739105362785658 ], [ -105.001745223999023, 39.739039362095518 ], [ -105.000414848327637, 39.739105362785658 ], [ -105.000414848327637, 39.739798366215922 ], [ -105.005350112915039, 39.739864366179162 ], [ -105.009427070617676, 39.739897366137079 ] ] ] } }
]
}
Binary file added tests/testdata/boundary/10/213/388.mvt
Binary file not shown.
Binary file added tests/testdata/boundary/12/853/1554.mvt
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/testdata/boundary2mvt.js
@@ -0,0 +1,24 @@
/* Generate tiles from GeoJSON data.
* Sample data from leflet: https://leafletjs.com/examples/geojson/example.html
* npm install geojson2mvt
*/

var fs = require('fs');
var geojson2mvt = require('geojson2mvt');

const filePath = './boundary.geojson';

var options = {
rootDir: 'boundary',
bbox : [39.73,-105.02,39.75,-105.00],
zoom : {
min : 8,
max : 14,
},
layerName: 'boundary',
};

var geoJson = JSON.parse(fs.readFileSync(filePath, "utf8"));

// build the static tile pyramid
geojson2mvt(geoJson, options);
38 changes: 29 additions & 9 deletions tests/testthat/test-mapbox.R
@@ -1,9 +1,9 @@
context('MapBox Vector Tiles')

test_that("Read multipolygon", {
# Original GeoJSON data
campus <- sf::read_sf('../testdata/campus.geojson', quiet = TRUE, as_tibble = FALSE)
# Original GeoJSON data
campus <- sf::read_sf('../testdata/campus.geojson', quiet = TRUE, as_tibble = FALSE)

test_that("Read multipolygon", {
# Converted to MVT
mvt10 = read_mvt_sf('../testdata/campus/10/213/388.mvt')
mvt12 = read_mvt_sf('../testdata/campus/12/853/1554.mvt')
Expand All @@ -17,14 +17,34 @@ test_that("Read multipolygon", {
all.equal(attributes(mvt10$campus$geometry), attributes(campus$geometry), tol = 1e-6)
all.equal(attributes(mvt12$campus$geometry), attributes(campus$geometry), tol = 1e-8)

# Convert to EPSG:3857 to work with st_area() without lwgeom
out1 <- sf::st_transform(campus, 3857)
out2 <- sf::st_transform(mvt10$campus, 3857)
out3 <- sf::st_transform(mvt12$campus, 3857)
# Convert to EPSG:3857 to test st_area() without lwgeom
wgs0 <- sf::st_transform(campus, 3857)
wgs10 <- sf::st_transform(mvt10$campus, 3857)
wgs12 <- sf::st_transform(mvt12$campus, 3857)

# Compare geometry data (zoom reduces accuracy)
expect_equal(sf::st_area(out1), sf::st_area(out2), tol = 1e-2)
expect_equal(sf::st_area(out1), sf::st_area(out3), tol = 1e-2)
expect_equal(sf::st_area(wgs0), sf::st_area(wgs10), tol = 1e-2)
expect_equal(sf::st_area(wgs0), sf::st_area(wgs12), tol = 1e-2)
})

test_that("Read multilinestring", {
# Original GeoJSON data
boundary <- sf::st_boundary(campus)

# Read from MVT
mvt10 <- read_mvt_sf('../testdata/boundary/10/213/388.mvt')
mvt12 <- read_mvt_sf('../testdata/boundary/12/853/1554.mvt')

# Metadata
expect_equal(attr(mvt10$boundary$geometry, 'bbox'), attr(boundary$geometry, 'bbox'), tol = 1e-6)
expect_equal(attr(mvt12$boundary$geometry, 'bbox'), attr(boundary$geometry, 'bbox'), tol = 1e-8)

# Convert to EPSG:3857 to test st_length() without lwgeom
wgs0 <- sf::st_transform(boundary, 3857)
wgs10 <- sf::st_transform(mvt10$boundary, 3857)
wgs12 <- sf::st_transform(mvt12$boundary, 3857)

# Compare geometry data (zoom reduces accuracy)
expect_equal(sf::st_length(wgs0), sf::st_length(wgs10), tol = 1e-4)
expect_equal(sf::st_length(wgs0), sf::st_length(wgs12), tol = 1e-5)
})

0 comments on commit 4f62eaa

Please sign in to comment.