Skip to content

Commit

Permalink
Improve support for empty layers
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Sep 28, 2023
1 parent aad3096 commit bd5fd1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/commands/mapshaper-split.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { compileValueExpression } from '../expressions/mapshaper-expressions';
import { getFeatureCount } from '../dataset/mapshaper-layer-utils';
import { copyLayer } from '../dataset/mapshaper-layer-utils';
import { getFeatureCount, copyLayer } from '../dataset/mapshaper-layer-utils';
import cmd from '../mapshaper-cmd';
import utils from '../utils/mapshaper-utils';
import { DataTable } from '../datatable/mapshaper-data-table';
Expand All @@ -13,6 +12,7 @@ cmd.splitLayer = function(src, expression, optsArg) {
shapes = lyr0.shapes,
index = {},
splitLayers = [],
n = getFeatureCount(lyr0),
namer;

if (opts.ids) {
Expand All @@ -21,11 +21,18 @@ cmd.splitLayer = function(src, expression, optsArg) {
namer = getSplitNameFunction(lyr0, expression);
}

// // halt if split field is missing
// if (splitField) {
// internal.requireDataField(lyr0, splitField);
// }

utils.repeat(getFeatureCount(lyr0), function(i) {
// if input layer is empty, return original layer
// TODO: consider halting
if (n === 0) {
return [lyr0];
}

utils.repeat(n, function(i) {
var name = namer(i),
lyr;

Expand All @@ -48,6 +55,7 @@ cmd.splitLayer = function(src, expression, optsArg) {
lyr.data.getRecords().push(properties[i]);
}
});

return splitLayers;
};

Expand Down
5 changes: 4 additions & 1 deletion src/commands/mapshaper-svg-style.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLayerDataTable } from '../dataset/mapshaper-layer-utils';
import { getLayerDataTable, getFeatureCount } from '../dataset/mapshaper-layer-utils';
import { getSymbolPropertyAccessor } from '../svg/svg-properties';
import { compileValueExpression } from '../expressions/mapshaper-expressions';
import { initDataTable } from '../dataset/mapshaper-layer-utils';
Expand All @@ -7,6 +7,9 @@ import cmd from '../mapshaper-cmd';

cmd.svgStyle = function(lyr, dataset, opts) {
var filter;
if (getFeatureCount(lyr) === 0) {
return;
}
if (!lyr.data) {
initDataTable(lyr);
}
Expand Down

0 comments on commit bd5fd1b

Please sign in to comment.