mapshaper 0.6.84 → 0.6.85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/mapshaper.js CHANGED
@@ -25429,8 +25429,11 @@ ${svg}
25429
25429
  .option('no-replace', noReplaceOpt);
25430
25430
 
25431
25431
  parser.command('rectangles')
25432
- .describe('create a rectangle around each feature in a layer')
25432
+ .describe('create a rectangle for each feature in a layer')
25433
25433
  .option('offset', offsetOpt)
25434
+ .option('bbox', {
25435
+ describe: 'Use an expression to generate a rectangle for each feature'
25436
+ })
25434
25437
  .option('aspect-ratio', aspectRatioOpt)
25435
25438
  .option('name', nameOpt)
25436
25439
  .option('target', targetOpt)
@@ -38806,18 +38809,20 @@ ${svg}
38806
38809
 
38807
38810
  // Create rectangles around each feature in a layer
38808
38811
  cmd.rectangles = function(targetLyr, targetDataset, opts) {
38809
- if (!layerHasGeometry(targetLyr)) {
38810
- stop("Layer is missing geometric shapes");
38811
- }
38812
38812
  var crsInfo = getDatasetCrsInfo(targetDataset);
38813
38813
  var records = targetLyr.data ? targetLyr.data.getRecords() : null;
38814
- var geometries = targetLyr.shapes.map(function(shp) {
38815
- var bounds = targetLyr.geometry_type == 'point' ?
38816
- getPointFeatureBounds(shp) : targetDataset.arcs.getMultiShapeBounds(shp);
38817
- bounds = applyRectangleOptions(bounds, crsInfo.crs, opts);
38818
- if (!bounds) return null;
38819
- return bboxToPolygon(bounds.toArray(), opts);
38820
- });
38814
+ var geometries;
38815
+
38816
+ if (opts.bbox) {
38817
+ geometries = bboxExpressionToGeometries(opts.bbox, targetLyr, targetDataset);
38818
+
38819
+ } else {
38820
+ if (!layerHasGeometry(targetLyr)) {
38821
+ stop("Layer is missing geometric shapes");
38822
+ }
38823
+ geometries = shapesToBoxGeometries(targetLyr, targetDataset, opts);
38824
+ }
38825
+
38821
38826
  var geojson = {
38822
38827
  type: 'FeatureCollection',
38823
38828
  features: geometries.map(function(geom, i) {
@@ -38839,6 +38844,39 @@ ${svg}
38839
38844
  return outputLayers;
38840
38845
  };
38841
38846
 
38847
+ function shapesToBoxGeometries(lyr, dataset, opts) {
38848
+ var crsInfo = getDatasetCrsInfo(dataset);
38849
+ return lyr.shapes.map(function(shp) {
38850
+ var bounds = lyr.geometry_type == 'point' ?
38851
+ getPointFeatureBounds(shp) : dataset.arcs.getMultiShapeBounds(shp);
38852
+ bounds = applyRectangleOptions(bounds, crsInfo.crs, opts);
38853
+ if (!bounds) return null;
38854
+ return bboxToPolygon(bounds.toArray(), opts);
38855
+ });
38856
+ }
38857
+
38858
+ function bboxExpressionToGeometries(exp, lyr, dataset, opts) {
38859
+ var compiled = compileFeatureExpression(exp, lyr, dataset.arcs, {});
38860
+ var n = getFeatureCount(lyr);
38861
+ var result;
38862
+ var geometries = [];
38863
+ for (var i=0; i<n; i++) {
38864
+ result = compiled(i);
38865
+ if (!looksLikeBbox(result)) {
38866
+ stop('Invalid bbox value (expected a GeoJSON-type bbox):', result);
38867
+ }
38868
+ geometries.push(bboxToPolygon(result));
38869
+ }
38870
+ return geometries;
38871
+ }
38872
+
38873
+ function looksLikeBbox(o) {
38874
+ if (!o || o.length != 4) return false;
38875
+ if (o.some(isNaN)) return false;
38876
+ if (o[0] <= o[2] == false || o[1] <= o[3] == false) return false;
38877
+ return true;
38878
+ }
38879
+
38842
38880
  // Create rectangles around one or more target layers
38843
38881
  //
38844
38882
  cmd.rectangle2 = function(target, opts) {
@@ -45536,7 +45574,7 @@ ${svg}
45536
45574
  });
45537
45575
  }
45538
45576
 
45539
- var version = "0.6.84";
45577
+ var version = "0.6.85";
45540
45578
 
45541
45579
  // Parse command line args into commands and run them
45542
45580
  // Function takes an optional Node-style callback. A Promise is returned if no callback is given.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mapshaper",
3
- "version": "0.6.84",
3
+ "version": "0.6.85",
4
4
  "description": "A tool for editing vector datasets for mapping and GIS.",
5
5
  "keywords": [
6
6
  "shapefile",
package/www/mapshaper.js CHANGED
@@ -25429,8 +25429,11 @@ ${svg}
25429
25429
  .option('no-replace', noReplaceOpt);
25430
25430
 
25431
25431
  parser.command('rectangles')
25432
- .describe('create a rectangle around each feature in a layer')
25432
+ .describe('create a rectangle for each feature in a layer')
25433
25433
  .option('offset', offsetOpt)
25434
+ .option('bbox', {
25435
+ describe: 'Use an expression to generate a rectangle for each feature'
25436
+ })
25434
25437
  .option('aspect-ratio', aspectRatioOpt)
25435
25438
  .option('name', nameOpt)
25436
25439
  .option('target', targetOpt)
@@ -38806,18 +38809,20 @@ ${svg}
38806
38809
 
38807
38810
  // Create rectangles around each feature in a layer
38808
38811
  cmd.rectangles = function(targetLyr, targetDataset, opts) {
38809
- if (!layerHasGeometry(targetLyr)) {
38810
- stop("Layer is missing geometric shapes");
38811
- }
38812
38812
  var crsInfo = getDatasetCrsInfo(targetDataset);
38813
38813
  var records = targetLyr.data ? targetLyr.data.getRecords() : null;
38814
- var geometries = targetLyr.shapes.map(function(shp) {
38815
- var bounds = targetLyr.geometry_type == 'point' ?
38816
- getPointFeatureBounds(shp) : targetDataset.arcs.getMultiShapeBounds(shp);
38817
- bounds = applyRectangleOptions(bounds, crsInfo.crs, opts);
38818
- if (!bounds) return null;
38819
- return bboxToPolygon(bounds.toArray(), opts);
38820
- });
38814
+ var geometries;
38815
+
38816
+ if (opts.bbox) {
38817
+ geometries = bboxExpressionToGeometries(opts.bbox, targetLyr, targetDataset);
38818
+
38819
+ } else {
38820
+ if (!layerHasGeometry(targetLyr)) {
38821
+ stop("Layer is missing geometric shapes");
38822
+ }
38823
+ geometries = shapesToBoxGeometries(targetLyr, targetDataset, opts);
38824
+ }
38825
+
38821
38826
  var geojson = {
38822
38827
  type: 'FeatureCollection',
38823
38828
  features: geometries.map(function(geom, i) {
@@ -38839,6 +38844,39 @@ ${svg}
38839
38844
  return outputLayers;
38840
38845
  };
38841
38846
 
38847
+ function shapesToBoxGeometries(lyr, dataset, opts) {
38848
+ var crsInfo = getDatasetCrsInfo(dataset);
38849
+ return lyr.shapes.map(function(shp) {
38850
+ var bounds = lyr.geometry_type == 'point' ?
38851
+ getPointFeatureBounds(shp) : dataset.arcs.getMultiShapeBounds(shp);
38852
+ bounds = applyRectangleOptions(bounds, crsInfo.crs, opts);
38853
+ if (!bounds) return null;
38854
+ return bboxToPolygon(bounds.toArray(), opts);
38855
+ });
38856
+ }
38857
+
38858
+ function bboxExpressionToGeometries(exp, lyr, dataset, opts) {
38859
+ var compiled = compileFeatureExpression(exp, lyr, dataset.arcs, {});
38860
+ var n = getFeatureCount(lyr);
38861
+ var result;
38862
+ var geometries = [];
38863
+ for (var i=0; i<n; i++) {
38864
+ result = compiled(i);
38865
+ if (!looksLikeBbox(result)) {
38866
+ stop('Invalid bbox value (expected a GeoJSON-type bbox):', result);
38867
+ }
38868
+ geometries.push(bboxToPolygon(result));
38869
+ }
38870
+ return geometries;
38871
+ }
38872
+
38873
+ function looksLikeBbox(o) {
38874
+ if (!o || o.length != 4) return false;
38875
+ if (o.some(isNaN)) return false;
38876
+ if (o[0] <= o[2] == false || o[1] <= o[3] == false) return false;
38877
+ return true;
38878
+ }
38879
+
38842
38880
  // Create rectangles around one or more target layers
38843
38881
  //
38844
38882
  cmd.rectangle2 = function(target, opts) {
@@ -45536,7 +45574,7 @@ ${svg}
45536
45574
  });
45537
45575
  }
45538
45576
 
45539
- var version = "0.6.84";
45577
+ var version = "0.6.85";
45540
45578
 
45541
45579
  // Parse command line args into commands and run them
45542
45580
  // Function takes an optional Node-style callback. A Promise is returned if no callback is given.