tile-cover
Advanced tools
Comparing version 0.2.2 to 0.3.0
190
index.js
@@ -6,122 +6,122 @@ var tilebelt = require('tilebelt'), | ||
module.exports.geojson = function(geom, limits) { | ||
var seed = [0,0,0]; | ||
var locked = []; | ||
module.exports.geojson = function (geom, limits) { | ||
var seed = [0,0,0]; | ||
var locked = []; | ||
if(geom.type != 'Point') { | ||
splitSeek(seed, geom, locked, limits); | ||
locked = mergeTiles(locked, limits); | ||
} | ||
else { | ||
locked.push(tilebelt.pointToTile(geom.coordinates[0], geom.coordinates[1], limits.max_zoom)); | ||
} | ||
if(geom.type != 'Point') { | ||
splitSeek(seed, geom, locked, limits); | ||
locked = mergeTiles(locked, limits); | ||
} | ||
else { | ||
locked.push(tilebelt.pointToTile(geom.coordinates[0], geom.coordinates[1], limits.max_zoom)); | ||
} | ||
var tileFeatures = locked.map(function(t) { | ||
return tilebelt.tileToGeoJSON(t); | ||
}); | ||
return { | ||
type: 'FeatureCollection', | ||
features: tileFeatures | ||
}; | ||
var tileFeatures = locked.map(function(t) { | ||
return tilebelt.tileToGeoJSON(t); | ||
}); | ||
return { | ||
type: 'FeatureCollection', | ||
features: tileFeatures | ||
}; | ||
}; | ||
module.exports.tiles = function(geom, limits) { | ||
var seed = [0,0,0]; | ||
var locked = []; | ||
module.exports.tiles = function (geom, limits) { | ||
var seed = [0,0,0]; | ||
var locked = []; | ||
if(geom.type != 'Point') { | ||
if(geom.type != 'Point') { | ||
splitSeek(seed, geom, locked, limits); | ||
locked = mergeTiles(locked, limits); | ||
} | ||
else { | ||
} | ||
else { | ||
locked.push(tilebelt.pointToTile(geom.coordinates[0], geom.coordinates[1], limits.max_zoom)); | ||
} | ||
} | ||
return locked; | ||
return locked; | ||
}; | ||
module.exports.indexes = function(geom, limits) { | ||
var seed = [0,0,0]; | ||
var locked = []; | ||
module.exports.indexes = function (geom, limits) { | ||
var seed = [0,0,0]; | ||
var locked = []; | ||
if(geom.type != 'Point') { | ||
splitSeek(seed, geom, locked, limits); | ||
locked = mergeTiles(locked, limits); | ||
} | ||
else { | ||
locked.push(tilebelt.pointToTile(geom.coordinates[0], geom.coordinates[1], limits.max_zoom)); | ||
} | ||
return locked.map(function(tile){ | ||
return tilebelt.tileToQuadkey(tile); | ||
}); | ||
if(geom.type != 'Point') { | ||
splitSeek(seed, geom, locked, limits); | ||
locked = mergeTiles(locked, limits); | ||
} | ||
else { | ||
locked.push(tilebelt.pointToTile(geom.coordinates[0], geom.coordinates[1], limits.max_zoom)); | ||
} | ||
return locked.map(function(tile){ | ||
return tilebelt.tileToQuadkey(tile); | ||
}); | ||
}; | ||
function mergeTiles(tiles, limits){ | ||
var merged = []; | ||
var changed = false; | ||
tiles.forEach(function(t){ | ||
// top left and has all siblings -- merge | ||
if((t[0]%2===0 && t[1]%2===0) && tilebelt.hasSiblings(t, tiles)) { | ||
if(t[2] > limits.min_zoom){ | ||
merged.push(tilebelt.getParent(t)); | ||
changed = true; | ||
} | ||
else{ | ||
merged = merged.concat(tilebelt.getSiblings(t)); | ||
} | ||
function mergeTiles (tiles, limits){ | ||
var merged = []; | ||
var changed = false; | ||
tiles.forEach(function(t){ | ||
// top left and has all siblings -- merge | ||
if((t[0]%2===0 && t[1]%2===0) && tilebelt.hasSiblings(t, tiles)) { | ||
if(t[2] > limits.min_zoom){ | ||
merged.push(tilebelt.getParent(t)); | ||
changed = true; | ||
} | ||
else{ | ||
merged = merged.concat(tilebelt.getSiblings(t)); | ||
} | ||
} | ||
// does not have all siblings -- add | ||
else if(!tilebelt.hasSiblings(t, tiles)){ | ||
merged.push(t); | ||
} | ||
}); | ||
// stop if the last round had no merges | ||
if(!changed) { | ||
return merged; | ||
} | ||
// does not have all siblings -- add | ||
else if(!tilebelt.hasSiblings(t, tiles)){ | ||
merged.push(t); | ||
else{ | ||
return mergeTiles(merged, limits); | ||
} | ||
}); | ||
// stop if the last round had no merges | ||
if(!changed) { | ||
return merged; | ||
} | ||
else{ | ||
return mergeTiles(merged, limits); | ||
} | ||
} | ||
function splitSeek(tile, geom, locked, limits){ | ||
var tileCovers = true; | ||
var doIntersect = needsIntersect(tilebelt.tileToGeoJSON(tile), geom); | ||
var intersects; | ||
if(doIntersect) { | ||
intersects = intersect(fc(tilebelt.tileToGeoJSON(tile)), fc(feature(geom))); | ||
} | ||
if(!intersects || intersects.features[0].type === 'GeometryCollection'){ | ||
tileCovers = false; | ||
} | ||
function splitSeek (tile, geom, locked, limits){ | ||
var tileCovers = true; | ||
var doIntersect = needsIntersect(tilebelt.tileToGeoJSON(tile), geom); | ||
var intersects; | ||
if(doIntersect) { | ||
intersects = intersect(featureCollection(tilebelt.tileToGeoJSON(tile)), featureCollection(feature(geom))); | ||
} | ||
if(!intersects || intersects.features[0].type === 'GeometryCollection'){ | ||
tileCovers = false; | ||
} | ||
if(tile[2] === 0 || (tileCovers && tile[2] < limits.max_zoom)){ | ||
var children = tilebelt.getChildren(tile); | ||
children.forEach(function(t){ | ||
splitSeek(t, intersects.features[0], locked, limits); | ||
}); | ||
} else if(tileCovers){ | ||
locked.push(tile); | ||
} | ||
if(tile[2] === 0 || (tileCovers && tile[2] < limits.max_zoom)){ | ||
var children = tilebelt.getChildren(tile); | ||
children.forEach(function(t){ | ||
splitSeek(t, intersects.features[0], locked, limits); | ||
}); | ||
} else if(tileCovers){ | ||
locked.push(tile); | ||
} | ||
} | ||
function feature(geom){ | ||
return { | ||
type: 'Feature', | ||
geometry: geom, | ||
properties: {} | ||
}; | ||
function feature (geom) { | ||
return { | ||
type: 'Feature', | ||
geometry: geom, | ||
properties: {} | ||
}; | ||
} | ||
function fc(feat){ | ||
return { | ||
type: 'FeatureCollection', | ||
features: [feat] | ||
}; | ||
function featureCollection (feat) { | ||
return { | ||
type: 'FeatureCollection', | ||
features: [feat] | ||
}; | ||
} | ||
function needsIntersect(tile, geom){ | ||
var bboxGeom = extent(geom); | ||
var bboxTile = extent(tile); | ||
return bboxIntersects(bboxGeom, bboxTile); | ||
function needsIntersect (tile, geom) { | ||
var bboxGeom = extent(geom); | ||
var bboxTile = extent(tile); | ||
return bboxIntersects(bboxGeom, bboxTile); | ||
} |
{ | ||
"name": "tile-cover", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "generate the minimum number of tiles to cover a geojson geometry", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,52 +6,52 @@ var cover = require('../'), | ||
test('uk', function(t){ | ||
var uk = JSON.parse(fs.readFileSync(__dirname+'/fixtures/uk.geojson')); | ||
var limits = { | ||
min_zoom: 7, | ||
max_zoom: 9 | ||
}; | ||
var uk = JSON.parse(fs.readFileSync(__dirname+'/fixtures/uk.geojson')); | ||
var limits = { | ||
min_zoom: 7, | ||
max_zoom: 9 | ||
}; | ||
t.ok(cover.geojson(uk.geometry, limits), 'uk geojson'); | ||
t.ok(cover.tiles(uk.geometry, limits).length, 'uk tiles'); | ||
t.ok(cover.indexes(uk.geometry, limits).length, 'uk indexes'); | ||
fs.writeFileSync(__dirname+'/fixtures/uk_out.geojson', JSON.stringify(cover.geojson(uk.geometry, limits))); | ||
t.end(); | ||
t.ok(cover.geojson(uk.geometry, limits), 'uk geojson'); | ||
t.ok(cover.tiles(uk.geometry, limits).length, 'uk tiles'); | ||
t.ok(cover.indexes(uk.geometry, limits).length, 'uk indexes'); | ||
fs.writeFileSync(__dirname+'/fixtures/uk_out.geojson', JSON.stringify(cover.geojson(uk.geometry, limits))); | ||
t.end(); | ||
}); | ||
test('line', function(t){ | ||
var line = JSON.parse(fs.readFileSync(__dirname+'/fixtures/line.geojson')); | ||
var limits = { | ||
min_zoom: 1, | ||
max_zoom: 12 | ||
}; | ||
var line = JSON.parse(fs.readFileSync(__dirname+'/fixtures/line.geojson')); | ||
var limits = { | ||
min_zoom: 1, | ||
max_zoom: 12 | ||
}; | ||
t.ok(cover.geojson(line.geometry, limits), 'line geojson'); | ||
t.ok(cover.tiles(line.geometry, limits).length, 'line tiles'); | ||
t.ok(cover.indexes(line.geometry, limits).length, 'line indexes'); | ||
fs.writeFileSync(__dirname+'/fixtures/line_out.geojson', JSON.stringify(cover.geojson(line.geometry, limits))); | ||
t.end(); | ||
t.ok(cover.geojson(line.geometry, limits), 'line geojson'); | ||
t.ok(cover.tiles(line.geometry, limits).length, 'line tiles'); | ||
t.ok(cover.indexes(line.geometry, limits).length, 'line indexes'); | ||
fs.writeFileSync(__dirname+'/fixtures/line_out.geojson', JSON.stringify(cover.geojson(line.geometry, limits))); | ||
t.end(); | ||
}); | ||
test('point', function(t){ | ||
var point = { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
79.08096313476562, | ||
21.135184856708992 | ||
] | ||
} | ||
}; | ||
var limits = { | ||
min_zoom: 1, | ||
max_zoom: 15 | ||
}; | ||
var point = { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
79.08096313476562, | ||
21.135184856708992 | ||
] | ||
} | ||
}; | ||
var limits = { | ||
min_zoom: 1, | ||
max_zoom: 15 | ||
}; | ||
t.ok(cover.geojson(point.geometry, limits), 'point geojson'); | ||
t.ok(cover.tiles(point.geometry, limits).length, 'point tiles'); | ||
t.ok(cover.indexes(point.geometry, limits).length, 'point indexes'); | ||
t.notEqual(cover.indexes(point.geometry, limits)[0], ''); | ||
fs.writeFileSync(__dirname+'/fixtures/point_out.geojson', JSON.stringify(cover.geojson(point.geometry, limits))); | ||
t.end(); | ||
t.ok(cover.geojson(point.geometry, limits), 'point geojson'); | ||
t.ok(cover.tiles(point.geometry, limits).length, 'point tiles'); | ||
t.ok(cover.indexes(point.geometry, limits).length, 'point indexes'); | ||
t.notEqual(cover.indexes(point.geometry, limits)[0], ''); | ||
fs.writeFileSync(__dirname+'/fixtures/point_out.geojson', JSON.stringify(cover.geojson(point.geometry, limits))); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
747907