Skip to content

Commit

Permalink
Run commands can support multiple targets
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Feb 12, 2024
1 parent 6c74331 commit 40f00e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
13 changes: 6 additions & 7 deletions src/expressions/mapshaper-target-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import { addGetters } from '../expressions/mapshaper-expression-utils';
// import { importGeoJSON } from '../geojson/geojson-import';

export function getTargetProxy(target) {
var lyr = target.layers[0];
var data = getLayerInfo(lyr, target.dataset); // layer_name, feature_count etc
data.layer = lyr;
data.dataset = target.dataset;
addGetters(data, {
var proxy = getLayerInfo(target.layer, target.dataset); // layer_name, feature_count etc
proxy.layer = target.layer;
proxy.dataset = target.dataset;
addGetters(proxy, {
// export as an object, not a string or buffer
geojson: getGeoJSON
});

function getGeoJSON() {
var features = exportLayerAsGeoJSON(lyr, target.dataset, {rfc7946: true}, true);
var features = exportLayerAsGeoJSON(target.layer, target.dataset, {rfc7946: true}, true);
return {
type: 'FeatureCollection',
features: features
};
}

return data;
return proxy;
}
17 changes: 15 additions & 2 deletions src/expressions/mapshaper-template-expressions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ import { getStashedVar } from '../mapshaper-stash';
import { getTargetProxy } from '../expressions/mapshaper-target-proxy';
import { stop, error } from '../utils/mapshaper-logging';
import utils from '../utils/mapshaper-utils';
import { expandCommandTargets } from '../dataset/mapshaper-target-utils';

// Support for evaluating expressions embedded in curly-brace templates

// Returns: a string (e.g. a command string used by the -run command)
export async function evalTemplateExpression(expression, targets, ctx) {
ctx = ctx || getBaseContext();
// TODO: throw an error if target is used when there are multiple targets
if (targets && targets.length == 1) {
Object.defineProperty(ctx, 'target', {value: getTargetProxy(targets[0])});
if (targets) {
var proxies = expandCommandTargets(targets).reduce(function(memo, target) {
var proxy = getTargetProxy(target);
memo.push(proxy);
// index targets by layer name too
if (target.layer.name) {
memo[target.layer.name] = proxy;
}
return memo;
}, []);
Object.defineProperty(ctx, 'targets', {value: proxies});
if (proxies.length == 1) {
Object.defineProperty(ctx, 'target', {value: proxies[0]});
}
}
// Add global functions and data to the expression context
// (e.g. functions imported via the -require command)
Expand Down
17 changes: 17 additions & 0 deletions test/run-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ describe('mapshaper-run.js', function () {
assert.deepEqual(JSON.parse(out['selection.json']), [{foo: 'bam'}])
})

it('supports targets getter for multiple targets', async function() {
var a = {
type: 'LineString',
coordinates: [[0, 0], [1, 1]]
};
var b = {
type: 'LineString',
coordinates: [[0, 0], [2, 2]]
};
// test that targets can be referenced by name in a run expression
var cmd = `-i a.json b.json combine-files
-run '-merge-layers name={targets.a.layer_name + targets.b.layer_name}'
-o`;
var out = await api.applyCommands(cmd, {'a.json': a, 'b.json': b});
assert(!!out['ab.json']);
});

it('supports io.ifile() alias', async function() {
var data = [{foo: 'bar'}, {foo: 'baz'}, {foo: 'bam'}];
var include = '{ \
Expand Down

0 comments on commit 40f00e0

Please sign in to comment.