tile-cover
Advanced tools
Comparing version 0.3.0 to 0.3.1
130
index.js
var tilebelt = require('tilebelt'), | ||
extent = require('geojson-extent'), | ||
bboxIntersects = require('bbox-intersect'), | ||
intersect = require('turf-intersect'); | ||
extent = require('geojson-extent'), | ||
bboxIntersects = require('bbox-intersect'), | ||
intersect = require('turf-intersect'); | ||
module.exports.geojson = function (geom, limits) { | ||
var seed = [0,0,0]; | ||
module.exports.geojson = function(geom, limits) { | ||
var locked = []; | ||
if(geom.type != 'Point') { | ||
if (geom.type != 'Point') { | ||
var seed = tilebelt.bboxToTile(extent(geom)); | ||
if (!seed[3]) seed = [0, 0, 0]; | ||
splitSeek(seed, geom, locked, limits); | ||
locked = mergeTiles(locked, limits); | ||
} else { | ||
locked.push(tilebelt.pointToTile(geom, limits.max_zoom)); | ||
} | ||
else { | ||
locked.push(tilebelt.pointToTile(geom.coordinates[0], geom.coordinates[1], limits.max_zoom)); | ||
} | ||
@@ -27,29 +27,29 @@ var tileFeatures = locked.map(function(t) { | ||
module.exports.tiles = function (geom, limits) { | ||
var seed = [0,0,0]; | ||
var locked = []; | ||
module.exports.tiles = function(geom, limits) { | ||
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') { | ||
var seed = tilebelt.bboxToTile(extent(geom)); | ||
if (!seed[3]) seed = [0, 0, 0]; | ||
splitSeek(seed, geom, locked, limits); | ||
locked = mergeTiles(locked, limits); | ||
} else { | ||
locked.push(tilebelt.pointToTile(geom, limits.max_zoom)); | ||
} | ||
return locked; | ||
return locked; | ||
}; | ||
module.exports.indexes = function (geom, limits) { | ||
var seed = [0,0,0]; | ||
module.exports.indexes = function(geom, limits) { | ||
var locked = []; | ||
if(geom.type != 'Point') { | ||
if (geom.type != 'Point') { | ||
var seed = tilebelt.bboxToTile(extent(geom)); | ||
if (!seed[3]) seed = [0, 0, 0]; | ||
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.map(function(tile){ | ||
return locked.map(function(tile) { | ||
return tilebelt.tileToQuadkey(tile); | ||
@@ -59,13 +59,12 @@ }); | ||
function mergeTiles (tiles, limits){ | ||
function mergeTiles(tiles, limits) { | ||
var merged = []; | ||
var changed = false; | ||
tiles.forEach(function(t){ | ||
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){ | ||
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{ | ||
} else { | ||
merged = merged.concat(tilebelt.getSiblings(t)); | ||
@@ -75,3 +74,3 @@ } | ||
// does not have all siblings -- add | ||
else if(!tilebelt.hasSiblings(t, tiles)){ | ||
else if (!tilebelt.hasSiblings(t, tiles)) { | ||
merged.push(t); | ||
@@ -81,6 +80,5 @@ } | ||
// stop if the last round had no merges | ||
if(!changed) { | ||
if (!changed) { | ||
return merged; | ||
} | ||
else{ | ||
} else { | ||
return mergeTiles(merged, limits); | ||
@@ -90,24 +88,48 @@ } | ||
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))); | ||
function splitSeek(tile, geom, locked, limits) { | ||
if (!tileGeomEquals(tile, geom)) { | ||
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; | ||
} | ||
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); | ||
} | ||
} else { | ||
locked.push(tile); | ||
} | ||
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); | ||
function tileGeomEquals(tile, geom) { | ||
tile = tilebelt.getParent(tile); | ||
var tileGeojson = tilebelt.tileToGeoJSON(tile).geometry; | ||
if (tileGeojson.coordinates[0].length === 5 && geom.coordinates[0].length === 5) { | ||
var numShared = 0; | ||
geom.coordinates[0].forEach(function(coord1) { | ||
tileGeojson.coordinates[0].forEach(function(coord2) { | ||
if (coord1[0] === coord2[0] && coord1[1] === coord2[1]) { | ||
numShared++; | ||
} | ||
}); | ||
}); | ||
} else if(tileCovers){ | ||
locked.push(tile); | ||
if (numShared === 7) { | ||
return true; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
function feature (geom) { | ||
function feature(geom) { | ||
return { | ||
@@ -120,3 +142,3 @@ type: 'Feature', | ||
function featureCollection (feat) { | ||
function fc(feat) { | ||
return { | ||
@@ -128,3 +150,3 @@ type: 'FeatureCollection', | ||
function needsIntersect (tile, geom) { | ||
function needsIntersect(tile, geom) { | ||
var bboxGeom = extent(geom); | ||
@@ -131,0 +153,0 @@ var bboxTile = extent(tile); |
{ | ||
"name": "tile-cover", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "generate the minimum number of tiles to cover a geojson geometry", | ||
@@ -34,3 +34,3 @@ "main": "index.js", | ||
"geojson-extent": "^0.3.0", | ||
"tilebelt": "^0.1.1", | ||
"tilebelt": "^0.5.1", | ||
"turf-inside": "^0.1.4", | ||
@@ -37,0 +37,0 @@ "turf-intersect": "0.0.0" |
Sorry, the diff of this file is not supported yet
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
748704
182
+ Addedtilebelt@0.5.3(transitive)
- Removedtilebelt@0.1.1(transitive)
Updatedtilebelt@^0.5.1