Skip to content

Commit

Permalink
Better -each expression geojson support
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Aug 16, 2024
1 parent 9be6b9a commit f94afd1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/expressions/mapshaper-each-geojson.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import utils from '../utils/mapshaper-utils';
import GeoJSON from '../geojson/geojson-common';

export function expressionUsesGeoJSON(exp) {
return exp.includes('this.geojson');
return exp.includes('this.geojson') || exp.includes('this.geometry') || exp.includes('this.feature');
}

export function getFeatureEditor(lyr, dataset) {
Expand All @@ -22,6 +22,24 @@ export function getFeatureEditor(lyr, dataset) {
return features[i];
};

api.getGeometry = function(i) {
if (i > 0) features[i-1] = null; // garbage-collect old features
return features[i] ? features[i].geometry : null;
};

api.setGeometry = function(geom, i) {
if (utils.isString(geom)) {
geom = JSON.parse(geom);
}
// TODO: validate? validate geometry in feature setter?
var feat = {
type: 'Feature',
properties: features[i] ? features[i].properties : null,
geometry: geom || null
};
api.set(feat, i);
};

api.set = function(feat, i) {
var arr;

Expand Down
18 changes: 18 additions & 0 deletions src/expressions/mapshaper-feature-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ export function initFeatureProxy(lyr, arcs, optsArg) {
return opts.geojson_editor.get(_id);
}
});

Object.defineProperty(ctx, 'feature', {
set: function(o) {
opts.geojson_editor.set(o, _id);
},
get: function() {
return opts.geojson_editor.get(_id);
}
});

Object.defineProperty(ctx, 'geometry', {
set: function(o) {
opts.geojson_editor.setGeometry(o, _id);
},
get: function() {
return opts.geojson_editor.getGeometry(_id);
}
});
}

if (_records) {
Expand Down

0 comments on commit f94afd1

Please sign in to comment.