function lineBuffer(line, radius, units, resolution) { var lineOffset = []; line.geometry.coordinates = removeDuplicates(line.geometry.coordinates); if (!(equalArrays(line.geometry.coordinates[0],line.geometry.coordinates[line.geometry.coordinates.length-1]))) { // situation at first point var firstLinePoint = helpers.point(line.geometry.coordinates[0]); var secondLinePoint = helpers.point(line.geometry.coordinates[1]); var firstLineBearing = bearing(firstLinePoint, secondLinePoint); var firstBufferPoint = destination(firstLinePoint, radius, firstLineBearing + 90, units); // situation at last point var lastLinePoint = helpers.point(line.geometry.coordinates[line.geometry.coordinates.length-1]); var secondlastLinePoint = helpers.point(line.geometry.coordinates[line.geometry.coordinates.length-2]); var lastLineBearing = bearing(lastLinePoint, secondlastLinePoint); lineOffset.push([]); lineOffset[0].push.apply(lineOffset[0],[firstBufferPoint.geometry.coordinates]); // Add first buffer point in order to close ring lineOffset[0].push.apply(lineOffset[0],lineOffsetOneSide(line, radius, units, resolution, false, true).geometry.coordinates); lineOffset[0].push.apply(lineOffset[0],arc(lastLinePoint, radius, lastLineBearing - 90, lastLineBearing + 90, units, resolution, true).geometry.coordinates); lineOffset[0].push.apply(lineOffset[0],lineOffsetOneSide(line, radius, units, resolution, true, true).geometry.coordinates); lineOffset[0].push.apply(lineOffset[0],arc(firstLinePoint, radius, firstLineBearing - 90, firstLineBearing + 90, units, resolution, true).geometry.coordinates); return offsetToBuffer(helpers.polygon(lineOffset)); } else { lineOffset.push(ringOffsetOneSide(line, radius, units, resolution, false, true).geometry.coordinates); lineOffset.push(ringOffsetOneSide(line, radius, units, resolution, true, true).geometry.coordinates); return offsetToBuffer(helpers.polygon(lineOffset)); } }
function rewind(poly){ // outer ring to winding +1, inner rings to winding -1 if (winding(helpers.polygon([poly.geometry.coordinates[0]])) == -1) poly.geometry.coordinates[0] = poly.geometry.coordinates[0].reverse(); for (var i = 1; i < poly.geometry.coordinates.length; i++) { if (winding(helpers.polygon([poly.geometry.coordinates[i]])) == 1) poly.geometry.coordinates[i] = poly.geometry.coordinates[i].reverse(); } return poly }
module.exports = function squareGrid(bbox, cellSize, units) { var fc = featurecollection([]); 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 currentX = bbox[0]; while (currentX <= bbox[2]) { var currentY = bbox[1]; while (currentY <= bbox[3]) { var cellPoly = polygon([[ [currentX, currentY], [currentX, currentY + cellHeight], [currentX + cellWidth, currentY + cellHeight], [currentX + cellWidth, currentY], [currentX, currentY] ]]); fc.features.push(cellPoly); currentY += cellHeight; } currentX += cellWidth; } return fc; };
test('bad type', function (t) { var poly = polygon([[[0,0], [0,100], [100,100], [100,0], [0,0]]]); t.throws(function() { inside(poly, poly); }, /A coordinate, feature, or point geometry is required/); t.end(); });
test('featureCollection', function (t) { // test for a simple polygon var poly = polygon([[[0,0], [0,100], [100,100], [100,0], [0,0]]]); var ptIn = point([50, 50]); var ptOut = point([140, 150]); t.true(inside(ptIn, poly), 'point inside simple polygon'); t.false(inside(ptOut, poly), 'point outside simple polygon'); // test for a concave polygon var concavePoly = polygon([[[0,0], [50, 50], [0,100], [100,100], [100,0], [0,0]]]); var ptConcaveIn = point([75, 75]); var ptConcaveOut = point([25, 50]); t.true(inside(ptConcaveIn, concavePoly), 'point inside concave polygon'); t.false(inside(ptConcaveOut, concavePoly), 'point outside concave polygon'); t.end(); });
//Center should be [x, y] function hexagon(center, rx, ry) { var vertices = []; for (var i = 0; i < 6; i++) { var x = center[0] + rx * cosines[i]; var y = center[1] + ry * sines[i]; vertices.push([x, y]); } //first and last vertex must be the same vertices.push(vertices[0]); return polygon([vertices]); }
})).map(function (triangle) { return polygon([[ [triangle.a.x, triangle.a.y], [triangle.b.x, triangle.b.y], [triangle.c.x, triangle.c.y], [triangle.a.x, triangle.a.y] ]], { a: triangle.a.z, b: triangle.b.z, c: triangle.c.z }); }));
function pointBuffer(pt, radius, units, resolution) { var pointOffset = [[]]; var resMultiple = 360/resolution; for(var i = 0; i < resolution; i++) { var spoke = destination(pt, radius, i*resMultiple, units); pointOffset[0].push(spoke.geometry.coordinates); } if(!(equalArrays(pointOffset[0][0],pointOffset[0][pointOffset[0].length-1]))) { pointOffset[0].push(pointOffset[0][0]); } return helpers.polygon(pointOffset) }
module.exports = function (bbox) { var lowLeft = [bbox[0], bbox[1]]; var topLeft = [bbox[0], bbox[3]]; var topRight = [bbox[2], bbox[3]]; var lowRight = [bbox[2], bbox[1]]; return polygon([[ lowLeft, lowRight, topRight, topLeft, lowLeft ]]); };
function polygonBuffer(poly, radius, units, resolution) { var polygonOffset = []; poly = rewind(poly); poly.geometry.coordinates[0] = removeDuplicates(poly.geometry.coordinates[0]); for (var i = 1; i < poly.geometry.coordinates.length; i++) { poly.geometry.coordinates[i] = removeDuplicates(poly.geometry.coordinates[i]); } polygonOffset.push(ringOffsetOneSide(helpers.lineString(poly.geometry.coordinates[0]), radius, units, resolution, false, true).geometry.coordinates); for (var i = 1; i < poly.geometry.coordinates.length; i++) { polygonOffset.push(ringOffsetOneSide(helpers.lineString(poly.geometry.coordinates[i]), radius, units, resolution, false, true).geometry.coordinates); } return offsetToBuffer(helpers.polygon(polygonOffset)); }
//Center should be [x, y] function hexTriangles(center, rx, ry) { var triangles = []; for (var i = 0; i < 6; i++) { var vertices = []; vertices.push(center); vertices.push([ center[0] + rx * cosines[i], center[1] + ry * sines[i] ]); vertices.push([ center[0] + rx * cosines[(i + 1) % 6], center[1] + ry * sines[(i + 1) % 6] ]); vertices.push(center); triangles.push(polygon([vertices])); } return triangles; }
function processPolygon(coordinates) { var data = flattenCoords(coordinates); var dim = 2; var result = earcut(data.vertices, data.holes, dim); var features = []; var vertices = []; result.forEach(function (vert, i) { var index = result[i]; vertices.push([data.vertices[index * dim], data.vertices[index * dim + 1]]); }); for (var i = 0; i < vertices.length; i += 3) { var coords = vertices.slice(i, i + 3); coords.push(vertices[i]); features.push(polygon([coords])); } return features; }
module.exports = function (bbox, cellSize, units) { var fc = featurecollection([]); var xFraction = cellSize / (distance([bbox[0], bbox[1]], [bbox[2], bbox[1]], units)); var cellWidth = xFraction * (bbox[2] - bbox[0]); var yFraction = cellSize / (distance([bbox[0], bbox[1]], [bbox[0], bbox[3]], units)); var cellHeight = yFraction * (bbox[3] - bbox[1]); var xi = 0; var currentX = bbox[0]; while (currentX <= bbox[2]) { var yi = 0; var currentY = bbox[1]; while (currentY <= bbox[3]) { if (xi % 2 === 0 && yi % 2 === 0) { fc.features.push(polygon([[ [currentX, currentY], [currentX, currentY + cellHeight], [currentX + cellWidth, currentY], [currentX, currentY] ]]), polygon([[ [currentX, currentY + cellHeight], [currentX + cellWidth, currentY + cellHeight], [currentX + cellWidth, currentY], [currentX, currentY + cellHeight] ]])); } else if (xi % 2 === 0 && yi % 2 === 1) { fc.features.push(polygon([[ [currentX, currentY], [currentX + cellWidth, currentY + cellHeight], [currentX + cellWidth, currentY], [currentX, currentY] ]]), polygon([[ [currentX, currentY], [currentX, currentY + cellHeight], [currentX + cellWidth, currentY + cellHeight], [currentX, currentY] ]])); } else if (yi % 2 === 0 && xi % 2 === 1) { fc.features.push(polygon([[ [currentX, currentY], [currentX, currentY + cellHeight], [currentX + cellWidth, currentY + cellHeight], [currentX, currentY] ]]), polygon([[ [currentX, currentY], [currentX + cellWidth, currentY + cellHeight], [currentX + cellWidth, currentY], [currentX, currentY] ]])); } else if (yi % 2 === 1 && xi % 2 === 1) { fc.features.push(polygon([[ [currentX, currentY], [currentX, currentY + cellHeight], [currentX + cellWidth, currentY], [currentX, currentY] ]]), polygon([[ [currentX, currentY + cellHeight], [currentX + cellWidth, currentY + cellHeight], [currentX + cellWidth, currentY], [currentX, currentY + cellHeight] ]])); } currentY += cellHeight; yi++; } xi++; currentX += cellWidth; } return fc; };
feature.geometry.coordinates.forEach(function(coords) { buffers.push(polygonBuffer(helpers.polygon(coords), radius, units, resolution)); });