mapshaper 0.6.44 → 0.6.46

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mapshaper",
3
- "version": "0.6.44",
3
+ "version": "0.6.46",
4
4
  "description": "A tool for editing vector datasets for mapping and GIS.",
5
5
  "keywords": [
6
6
  "shapefile",
@@ -68,7 +68,7 @@
68
68
  "browserify": "^17.0.0",
69
69
  "csv-spectrum": "^1.0.0",
70
70
  "eslint": "^8.16.0",
71
- "mocha": "^10.0.0",
71
+ "mocha": "^10.2.0",
72
72
  "rollup": "^2.73.0",
73
73
  "shell-quote": "^1.7.4",
74
74
  "underscore": "^1.13.1"
package/www/index.html CHANGED
@@ -156,7 +156,7 @@
156
156
  <div class="export-formats option-menu">
157
157
  </div>
158
158
 
159
- <div style="height:10px"></div>
159
+ <div style="height:11px"></div>
160
160
 
161
161
  <div class="option-menu"><input type="text" class="text-input advanced-options" placeholder="command line options" />
162
162
  <a href="https://github.com/mbloch/mapshaper/wiki/Command-Reference#-o-output" target="_mapshaper_output_docs">
@@ -164,15 +164,15 @@
164
164
  <div class="tip">Enter options from the command line interface for
165
165
  the -o command. Examples: bbox no-quantization
166
166
  precision=0.001. Click to see all options.</div></div></div></a>
167
- </div>
167
+
168
168
 
169
169
  <!-- <div class="option-menu">
170
170
  <input id="ofile-name" class="text-input" type="text" placeholder="output file name" />
171
171
  </div> -->
172
-
173
- <!-- <div class="cancel-btn btn dialog-btn">Cancel</div> -->
174
- <div class="save-btn btn dialog-btn">Export</div>
175
- <span id="save-preference" class="inline-checkbox" style="display: none;"><input type="checkbox"/>choose directory</span>
172
+ <div><span id="save-preference" style="display: none;"><input type="checkbox"/>choose output directory</span></div>
173
+ <div><span id="save-to-clipboard" style="display: inline-block;"><input type="checkbox"/>save to clipboard</span></div>
174
+ </div>
175
+ <div id="export-btn" class="btn dialog-btn" style="margin-top: 4px;">Export</div>
176
176
 
177
177
  </div>
178
178
  </div>
@@ -1442,7 +1442,7 @@
1442
1442
  }).text('restore');
1443
1443
  El('span').addClass('save-menu-btn').appendTo(line).on('click', async function(e) {
1444
1444
  var obj = await idb.get(item.id);
1445
- await internal.applyCompression(obj, {consume: true});
1445
+ await internal.compressSnapshotForExport(obj);
1446
1446
  var buf = internal.pack(obj);
1447
1447
  // choose output filename and directory every time
1448
1448
  // saveBlobToLocalFile('mapshaper_snapshot.msx', new Blob([buf]));
@@ -3504,6 +3504,7 @@
3504
3504
  // keeping a reference to current arcs and intersections, so intersections
3505
3505
  // don't need to be recalculated when 'repair' button is pressed.
3506
3506
  _currArcs,
3507
+ _currLayer,
3507
3508
  _currXX;
3508
3509
 
3509
3510
  gui.on('simplify_drag_start', hide);
@@ -3524,8 +3525,8 @@
3524
3525
  });
3525
3526
 
3526
3527
  repairBtn.on('click', function() {
3527
- var fixed = internal.repairIntersections(_currArcs, _currXX);
3528
- showIntersections(fixed, _currArcs);
3528
+ _currXX = internal.repairIntersections(_currArcs, _currXX);
3529
+ showIntersections();
3529
3530
  repairBtn.addClass('disabled');
3530
3531
  model.updated({repair: true});
3531
3532
  gui.session.simplificationRepair();
@@ -3573,13 +3574,17 @@
3573
3574
  showBtn = false;
3574
3575
  }
3575
3576
  el.show();
3576
- showIntersections(XX, arcs);
3577
+ _currLayer = e.layer;
3578
+ _currArcs = arcs;
3579
+ _currXX = XX;
3580
+ showIntersections();
3577
3581
  repairBtn.classed('disabled', !showBtn);
3578
3582
  }
3579
3583
 
3580
3584
  function reset() {
3581
3585
  _currArcs = null;
3582
3586
  _currXX = null;
3587
+ _currLayer = null;
3583
3588
  hide();
3584
3589
  }
3585
3590
 
@@ -3590,13 +3595,11 @@
3590
3595
  reset();
3591
3596
  }
3592
3597
 
3593
- function showIntersections(XX, arcs) {
3594
- var n = XX.length, pointLyr;
3595
- _currXX = XX;
3596
- _currArcs = arcs;
3598
+ function showIntersections() {
3599
+ var n = _currXX.length, pointLyr;
3597
3600
  if (n > 0) {
3598
3601
  // console.log("first intersection:", internal.getIntersectionDebugData(XX[0], arcs));
3599
- pointLyr = {geometry_type: 'point', shapes: [internal.getIntersectionPoints(XX)]};
3602
+ pointLyr = internal.getIntersectionLayer(_currXX, _currLayer, _currArcs);
3600
3603
  map.setIntersectionLayer(pointLyr, {layers:[pointLyr]});
3601
3604
  readout.html(utils$1.format('<span class="icon"></span>%s line intersection%s <img class="close-btn" src="images/close.png">', n, utils$1.pluralSuffix(n)));
3602
3605
  readout.findChild('.close-btn').on('click', dismiss);
@@ -3672,11 +3675,12 @@
3672
3675
  return;
3673
3676
  }
3674
3677
 
3675
- new SimpleButton(menu.findChild('.save-btn').addClass('default-btn')).on('click', onExportClick);
3678
+ new SimpleButton(menu.findChild('#export-btn').addClass('default-btn')).on('click', onExportClick);
3676
3679
  gui.addMode('export', turnOn, turnOff, exportBtn);
3677
3680
  gui.keyboard.onMenuSubmit(menu, onExportClick);
3681
+ var savePreferenceCheckbox;
3678
3682
  if (window.showSaveFilePicker) {
3679
- menu.findChild('#save-preference')
3683
+ savePreferenceCheckbox = menu.findChild('#save-preference')
3680
3684
  .css('display', 'inline-block')
3681
3685
  .findChild('input')
3682
3686
  .on('change', function() {
@@ -3684,11 +3688,44 @@
3684
3688
  })
3685
3689
  .attr('checked', GUI.getSavedValue('choose-save-dir') || null);
3686
3690
  }
3691
+ var clipboardCheckbox = menu.findChild('#save-to-clipboard')
3692
+ .findChild('input')
3693
+ .on('change', function() {
3694
+ updateExportCheckboxes();
3695
+ });
3696
+
3697
+ function setDisabled(inputEl, flag) {
3698
+ if (!inputEl) return;
3699
+ inputEl.node().disabled = !!flag;
3700
+ inputEl.parent().css({color: flag ? '#bbb' : 'black'});
3701
+ }
3702
+
3703
+ function checkboxOn(inputEl) {
3704
+ if (!inputEl) return false;
3705
+ return inputEl.node().checked && !inputEl.node().disabled;
3706
+ }
3707
+
3708
+ function updateExportCheckboxes() {
3709
+ // disable cliboard if not usable
3710
+ var canUseClipboard = clipboardIsAvailable();
3711
+ setDisabled(clipboardCheckbox, !canUseClipboard);
3712
+
3713
+ // disable save to directory checkbox if clipboard is selected
3714
+ setDisabled(savePreferenceCheckbox, checkboxOn(clipboardCheckbox));
3715
+ }
3716
+
3717
+ function clipboardIsAvailable() {
3718
+ var layers = getSelectedLayerEntries();
3719
+ var fmt = getSelectedFormat();
3720
+ return layers.length == 1 && ['json', 'geojson', 'dsv', 'topojson'].includes(fmt);
3721
+ }
3722
+
3687
3723
 
3688
3724
  function turnOn() {
3689
3725
  layersArr = initLayerMenu();
3690
3726
  // initZipOption();
3691
3727
  initFormatMenu();
3728
+ updateExportCheckboxes();
3692
3729
  menu.show();
3693
3730
  }
3694
3731
 
@@ -3697,22 +3734,25 @@
3697
3734
  menu.hide();
3698
3735
  }
3699
3736
 
3700
- function getSelectedLayers() {
3701
- var targets = layersArr.reduce(function(memo, o) {
3737
+ function getSelectedLayerEntries() {
3738
+ return layersArr.reduce(function(memo, o) {
3702
3739
  return o.checkbox.checked ? memo.concat(o.target) : memo;
3703
3740
  }, []);
3704
- return internal.groupLayersByDataset(targets);
3741
+ }
3742
+
3743
+ function getExportTargets() {
3744
+ return internal.groupLayersByDataset(getSelectedLayerEntries());
3705
3745
  }
3706
3746
 
3707
3747
  function onExportClick() {
3708
- var layers = getSelectedLayers();
3709
- if (layers.length === 0) {
3748
+ var targets = getExportTargets();
3749
+ if (targets.length === 0) {
3710
3750
  return gui.alert('No layers were selected');
3711
3751
  }
3712
3752
  gui.clearMode();
3713
3753
  gui.showProgressMessage('Exporting');
3714
3754
  setTimeout(function() {
3715
- exportMenuSelection(layers).catch(function(err) {
3755
+ exportMenuSelection(targets).catch(function(err) {
3716
3756
  if (utils$1.isString(err)) {
3717
3757
  gui.alert(err);
3718
3758
  } else {
@@ -3742,12 +3782,22 @@
3742
3782
  }
3743
3783
 
3744
3784
  // done: function(string|Error|null)
3745
- async function exportMenuSelection(layers) {
3785
+ async function exportMenuSelection(targets) {
3746
3786
  var opts = getExportOpts();
3747
3787
  // note: command line "target" option gets ignored
3748
- var files = await internal.exportTargetLayers(layers, opts);
3788
+ var files = await internal.exportTargetLayers(targets, opts);
3749
3789
  gui.session.layersExported(getTargetLayerIds(), getExportOptsAsString());
3750
- await utils$1.promisify(internal.writeFiles)(files, opts);
3790
+ if (files.length == 1 && checkboxOn(clipboardCheckbox)) {
3791
+ await saveFileContentToClipboard(files[0].content);
3792
+ } else {
3793
+ await utils$1.promisify(internal.writeFiles)(files, opts);
3794
+ }
3795
+
3796
+ }
3797
+
3798
+ async function saveFileContentToClipboard(content) {
3799
+ var str = utils$1.isString(content) ? content : content.toString();
3800
+ await navigator.clipboard.writeText(str);
3751
3801
  }
3752
3802
 
3753
3803
  function initLayerItem(o, i) {
@@ -3833,6 +3883,7 @@
3833
3883
  }
3834
3884
 
3835
3885
  function updateToggleBtn() {
3886
+ updateExportCheckboxes(); // checkbox visibility is affected by number of export layers
3836
3887
  if (!toggleBtn) return;
3837
3888
  var state = getSelectionState();
3838
3889
  // style of intermediate checkbox state doesn't look right in Chrome --
@@ -3853,30 +3904,49 @@
3853
3904
  return 'some';
3854
3905
  }
3855
3906
 
3856
- function getInputFormats() {
3857
- return model.getDatasets().reduce(function(memo, d) {
3858
- var fmts = d.info && d.info.input_formats || [];
3859
- return memo.concat(fmts);
3860
- }, []);
3861
- }
3862
-
3863
3907
  function getDefaultExportFormat() {
3864
3908
  var dataset = model.getActiveLayer().dataset;
3865
- return dataset.info && dataset.info.input_formats &&
3866
- dataset.info.input_formats[0] || 'geojson';
3909
+ var inputFmt = dataset.info && dataset.info.input_formats &&
3910
+ dataset.info.input_formats[0];
3911
+ return getExportFormats().includes(inputFmt) ? inputFmt : 'geojson';
3912
+ }
3913
+
3914
+ function getExportFormats() {
3915
+ // return ['shapefile', 'geojson', 'topojson', 'json', 'dsv', 'kml', 'svg', internal.PACKAGE_EXT];
3916
+ return ['shapefile', 'json', 'geojson', 'dsv', 'topojson', 'kml', internal.PACKAGE_EXT, 'svg'];
3867
3917
  }
3868
3918
 
3869
3919
  function initFormatMenu() {
3870
- var defaults = ['shapefile', 'geojson', 'topojson', 'json', 'dsv', 'kml', 'svg', internal.PACKAGE_EXT];
3871
- var formats = utils$1.uniq(defaults.concat(getInputFormats()));
3920
+ var formats = getExportFormats();
3921
+ // var formats = utils.uniq(getExportFormats().concat(getInputFormats()));
3872
3922
  var items = formats.map(function(fmt) {
3873
- return utils$1.format('<div><label><input type="radio" name="format" value="%s"' +
3874
- ' class="radio">%s</label></div>', fmt, internal.getFormatName(fmt));
3923
+ return utils$1.format('<td><label><input type="radio" name="format" value="%s"' +
3924
+ ' class="radio">%s</label></td>', fmt, internal.getFormatName(fmt));
3875
3925
  });
3876
- menu.findChild('.export-formats').html(items.join('\n'));
3926
+ var table = '<table>';
3927
+ for (var i=0; i<items.length; i+=2) {
3928
+ table += '<tr>' + items[i] + items[i+1] + '<tr>';
3929
+ }
3930
+ table += '</table>';
3931
+
3932
+ // menu.findChild('.export-formats').html(items.join('\n'));
3933
+ menu.findChild('.export-formats').html(table);
3877
3934
  menu.findChild('.export-formats input[value="' + getDefaultExportFormat() + '"]').node().checked = true;
3935
+ // update save-as settings when value changes
3936
+ menu.findChildren('input[type="radio"]').forEach(el => {
3937
+ el.on('change', updateExportCheckboxes);
3938
+ });
3878
3939
  }
3879
3940
 
3941
+
3942
+ // function getInputFormats() {
3943
+ // return model.getDatasets().reduce(function(memo, d) {
3944
+ // var fmts = d.info && d.info.input_formats || [];
3945
+ // return memo.concat(fmts);
3946
+ // }, []);
3947
+ // }
3948
+
3949
+
3880
3950
  function initZipOption() {
3881
3951
  var html = `<label><input type="checkbox">Save to .zip file</label>`;
3882
3952
  menu.findChild('.export-zip-option').html(html);
@@ -6303,6 +6373,23 @@
6303
6373
  return [xmin, ymin, xmax, ymax];
6304
6374
  }
6305
6375
 
6376
+
6377
+ function findArcIdFromVertexId(i, ii) {
6378
+ // binary search
6379
+ // possible optimization: use interpolation to find a better partition value.
6380
+ var lower = 0, upper = ii.length - 1;
6381
+ var middle;
6382
+ while (lower < upper) {
6383
+ middle = Math.ceil((lower + upper) / 2);
6384
+ if (i < ii[middle]) {
6385
+ upper = middle - 1;
6386
+ } else {
6387
+ lower = middle;
6388
+ }
6389
+ }
6390
+ return lower; // assumes dataset is not empty
6391
+ }
6392
+
6306
6393
  function deleteVertex(arcs, i) {
6307
6394
  var data = arcs.getVertexData();
6308
6395
  var nn = data.nn;
@@ -6372,6 +6459,61 @@
6372
6459
  arcs.updateVertexData(nn, xx2, yy2, zz2);
6373
6460
  }
6374
6461
 
6462
+ function countFilteredVertices(zz, zlimit) {
6463
+ var count = 0;
6464
+ for (var i=0, n = zz.length; i<n; i++) {
6465
+ if (zz[i] >= zlimit) count++;
6466
+ }
6467
+ return count;
6468
+ }
6469
+
6470
+ function filterVertexData(o, zlimit) {
6471
+ if (!o.zz) error('Expected simplification data');
6472
+ var xx = o.xx,
6473
+ yy = o.yy,
6474
+ zz = o.zz,
6475
+ len2 = countFilteredVertices(zz, zlimit),
6476
+ arcCount = o.nn.length,
6477
+ xx2 = new Float64Array(len2),
6478
+ yy2 = new Float64Array(len2),
6479
+ zz2 = new Float64Array(len2),
6480
+ nn2 = new Int32Array(arcCount),
6481
+ i = 0, i2 = 0,
6482
+ n, n2;
6483
+
6484
+ for (var arcId=0; arcId < arcCount; arcId++) {
6485
+ n2 = 0;
6486
+ n = o.nn[arcId];
6487
+ for (var end = i+n; i < end; i++) {
6488
+ if (zz[i] >= zlimit) {
6489
+ xx2[i2] = xx[i];
6490
+ yy2[i2] = yy[i];
6491
+ zz2[i2] = zz[i];
6492
+ i2++;
6493
+ n2++;
6494
+ }
6495
+ }
6496
+ if (n2 == 1) {
6497
+ error("Collapsed arc");
6498
+ // This should not happen (endpoints should be z == Infinity)
6499
+ // Could handle like this, instead of throwing an error:
6500
+ // n2 = 0;
6501
+ // xx2.pop();
6502
+ // yy2.pop();
6503
+ // zz2.pop();
6504
+ } else if (n2 === 0) {
6505
+ // collapsed arc... ignoring
6506
+ }
6507
+ nn2[arcId] = n2;
6508
+ }
6509
+ return {
6510
+ xx: xx2,
6511
+ yy: yy2,
6512
+ zz: zz2,
6513
+ nn: nn2
6514
+ };
6515
+ }
6516
+
6375
6517
  function getShapeHitTest(displayLayer, ext, interactionMode) {
6376
6518
  var geoType = displayLayer.layer.geometry_type;
6377
6519
  var test;
@@ -8023,14 +8165,13 @@
8023
8165
  });
8024
8166
 
8025
8167
  self.show = function(id, ids, lyr, pinned) {
8026
- var table = lyr.data; // table can be null (e.g. if layer has no attribute data)
8027
8168
  var editable = pinned && gui.interaction.getMode() == 'data';
8028
8169
  var maxHeight = parent.node().clientHeight - 36;
8029
8170
  currId = id;
8030
8171
  // stash a function for refreshing the current popup when data changes
8031
8172
  // while the popup is being displayed (e.g. while dragging a label)
8032
8173
  refresh = function() {
8033
- render(content, id, table, editable);
8174
+ render(content, id, lyr, editable);
8034
8175
  };
8035
8176
  refresh();
8036
8177
  if (ids && ids.length > 1) {
@@ -8069,7 +8210,8 @@
8069
8210
  tab.show();
8070
8211
  }
8071
8212
 
8072
- function render(el, recId, table, editable) {
8213
+ function render(el, recId, lyr, editable) {
8214
+ var table = lyr.data; // table can be null (e.g. if layer has no attribute data)
8073
8215
  var rec = table && (editable ? table.getRecordAt(recId) : table.getReadOnlyRecordAt(recId)) || {};
8074
8216
  var tableEl = El('table').addClass('selectable'),
8075
8217
  rows = 0;
@@ -8111,19 +8253,19 @@
8111
8253
  if (editable) {
8112
8254
  // render "add field" button
8113
8255
  var line = El('div').appendTo(el);
8114
- El('span').addClass('save-menu-btn').appendTo(line).on('click', async function(e) {
8256
+ El('span').addClass('add-field-btn').appendTo(line).on('click', async function(e) {
8115
8257
  // show "add field" dialog
8116
- renderAddFieldPopup(recId, table);
8258
+ renderAddFieldPopup(recId, lyr);
8117
8259
  }).text('+ add field');
8118
8260
  }
8119
8261
  }
8120
8262
 
8121
- function renderAddFieldPopup(recId, table) {
8263
+ function renderAddFieldPopup(recId, lyr) {
8122
8264
  var popup = showPopupAlert('', 'Add field');
8123
8265
  var el = popup.container();
8124
8266
  el.addClass('option-menu');
8125
- var html = `<input type="text" class="field-name text-input" placeholder="field name"><br>
8126
- <input type="text" class="field-value text-input" placeholder="value"><br>
8267
+ var html = `<div><input type="text" class="field-name text-input" placeholder="field name"></div>
8268
+ <div><input type="text" class="field-value text-input" placeholder="value"><div>
8127
8269
  <div tabindex="0" class="btn dialog-btn">Apply</div> <span class="inline-checkbox"><input type="checkbox" class="all" />assign value to all records</span>`;
8128
8270
  el.html(html);
8129
8271
 
@@ -8132,6 +8274,7 @@
8132
8274
  var val = el.findChild('.field-value');
8133
8275
  var box = el.findChild('.all');
8134
8276
  var btn = el.findChild('.btn').on('click', function() {
8277
+ var table = internal.getLayerDataTable(lyr); // creates new table if missing
8135
8278
  var all = box.node().checked;
8136
8279
  var nameStr = name.node().value.trim();
8137
8280
  if (!nameStr) return;
@@ -11439,6 +11582,9 @@
11439
11582
  new SessionSnapshots(gui);
11440
11583
  }
11441
11584
 
11585
+ var msgCount = 0;
11586
+ var clearMsg;
11587
+
11442
11588
  initModeRules(gui);
11443
11589
 
11444
11590
  gui.showProgressMessage = function(msg) {
@@ -11447,10 +11593,25 @@
11447
11593
  .appendTo('body');
11448
11594
  }
11449
11595
  El('<div>').text(msg).appendTo(gui.progressMessage.empty().show());
11596
+ clearMsg = getClearFunction(msgCount);
11450
11597
  };
11451
11598
 
11599
+ function getClearFunction(count) {
11600
+ var time = Date.now();
11601
+ // wait at least [min] milliseconds before closing
11602
+ var min = 400;
11603
+ msgCount = ++count;
11604
+ return function() {
11605
+ setTimeout(function() {
11606
+ if (count != msgCount) return;
11607
+ if (gui.progressMessage) gui.progressMessage.hide();
11608
+ }, Math.max(min - (Date.now() - time), 0));
11609
+ };
11610
+ }
11611
+
11452
11612
  gui.clearProgressMessage = function() {
11453
- if (gui.progressMessage) gui.progressMessage.hide();
11613
+ clearMsg();
11614
+ // if (gui.progressMessage) gui.progressMessage.hide();
11454
11615
  };
11455
11616
 
11456
11617
  gui.consoleIsOpen = function() {