Skip to content

Commit

Permalink
Update website
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Apr 4, 2024
1 parent d8a13ef commit 314f1e4
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions mapshaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25429,8 +25429,11 @@ ${svg}
.option('no-replace', noReplaceOpt);

parser.command('rectangles')
.describe('create a rectangle around each feature in a layer')
.describe('create a rectangle for each feature in a layer')
.option('offset', offsetOpt)
.option('bbox', {
describe: 'Use an expression to generate a rectangle for each feature'
})
.option('aspect-ratio', aspectRatioOpt)
.option('name', nameOpt)
.option('target', targetOpt)
Expand Down Expand Up @@ -38806,18 +38809,20 @@ ${svg}

// Create rectangles around each feature in a layer
cmd.rectangles = function(targetLyr, targetDataset, opts) {
if (!layerHasGeometry(targetLyr)) {
stop("Layer is missing geometric shapes");
}
var crsInfo = getDatasetCrsInfo(targetDataset);
var records = targetLyr.data ? targetLyr.data.getRecords() : null;
var geometries = targetLyr.shapes.map(function(shp) {
var bounds = targetLyr.geometry_type == 'point' ?
getPointFeatureBounds(shp) : targetDataset.arcs.getMultiShapeBounds(shp);
bounds = applyRectangleOptions(bounds, crsInfo.crs, opts);
if (!bounds) return null;
return bboxToPolygon(bounds.toArray(), opts);
});
var geometries;

if (opts.bbox) {
geometries = bboxExpressionToGeometries(opts.bbox, targetLyr, targetDataset);

} else {
if (!layerHasGeometry(targetLyr)) {
stop("Layer is missing geometric shapes");
}
geometries = shapesToBoxGeometries(targetLyr, targetDataset, opts);
}

var geojson = {
type: 'FeatureCollection',
features: geometries.map(function(geom, i) {
Expand All @@ -38839,6 +38844,39 @@ ${svg}
return outputLayers;
};

function shapesToBoxGeometries(lyr, dataset, opts) {
var crsInfo = getDatasetCrsInfo(dataset);
return lyr.shapes.map(function(shp) {
var bounds = lyr.geometry_type == 'point' ?
getPointFeatureBounds(shp) : dataset.arcs.getMultiShapeBounds(shp);
bounds = applyRectangleOptions(bounds, crsInfo.crs, opts);
if (!bounds) return null;
return bboxToPolygon(bounds.toArray(), opts);
});
}

function bboxExpressionToGeometries(exp, lyr, dataset, opts) {
var compiled = compileFeatureExpression(exp, lyr, dataset.arcs, {});
var n = getFeatureCount(lyr);
var result;
var geometries = [];
for (var i=0; i<n; i++) {
result = compiled(i);
if (!looksLikeBbox(result)) {
stop('Invalid bbox value (expected a GeoJSON-type bbox):', result);
}
geometries.push(bboxToPolygon(result));
}
return geometries;
}

function looksLikeBbox(o) {
if (!o || o.length != 4) return false;
if (o.some(isNaN)) return false;
if (o[0] <= o[2] == false || o[1] <= o[3] == false) return false;
return true;
}

// Create rectangles around one or more target layers
//
cmd.rectangle2 = function(target, opts) {
Expand Down Expand Up @@ -45536,7 +45574,7 @@ ${svg}
});
}

var version = "0.6.84";
var version = "0.6.85";

// Parse command line args into commands and run them
// Function takes an optional Node-style callback. A Promise is returned if no callback is given.
Expand Down

0 comments on commit 314f1e4

Please sign in to comment.