-
Notifications
You must be signed in to change notification settings - Fork 535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
run
command that imports the return value
#608
Comments
You can do something like this already using the
You might want to process the I wonder if there could be a new way to use the
|
I think I used this feature in each before; but didn't thought about it now. Maybe because of its current "on in one out" nature. A modified |
I can confirm that the const h3 = require("h3-js");
let offset;
module.exports = {
h3: function (target, res = 3, keep) {
offset = 0;
if (typeof keep === 'string') keep = `"${keep}"`;
return `
-explode
-each this.geojson=h3_each(this.geojson,${res},${keep})
-explode
-clean
-each h3_postex(this)`;
},
h3_each: function (feat, res, keep) {
const hexagons = h3.polygonToCells(feat.geometry.coordinates, res, true);
const coordinates = hexagons.map(h3Idx => [h3.cellToBoundary(h3Idx, true)]);
const properties = { $offset: offset, _h3: hexagons };
if (keep) {
let props = feat.properties;
if (typeof keep === 'string') {
props = Object.fromEntries(
keep.split(",")
.filter(key => key in props)
.map(key => [key, props[key]])
);
}
Object.assign(properties, props);
}
offset += hexagons.length;
return {
type: "Feature",
geometry: { type: "MultiPolygon", coordinates },
properties
};
},
h3_postex: function (feat) {
Object.keys(feat.properties).forEach((_key) => {
if (_key.startsWith('_')) {
let key = _key.slice(1);
let i = feat.id - (feat.properties.$offset || 0)
feat.properties[key] = feat.properties[_key][i];
delete feat.properties[_key];
}
});
delete feat.properties.$offset;
}
}; ... that can be called like this: mapshaper ne_110m_admin_0_countries.shp `
-filter "['Germany','Italy'].includes(NAME)" `
-require ms_h3.js `
-run "h3(target,5,'NAME,ISO_A3')" `
-o ger_ita_h3_res5.json "format=geojson"
This would make it much easier. |
You found a smart solution... seems like we could make this sort of thing much easier... This morning I published an update (v0.4.66) that improves the I'll keep thinking about extending the |
👏 that is a great improvement. It cuts down the complexity significantly: const h3 = require("h3-js");
module.exports = {
h3: function (feat, res = 3, keep) {
const hexagons = h3.polygonToCells(feat.geometry.coordinates, res, true);
let props;
if (keep) {
props = feat.properties;
if (typeof keep === 'string')
props = Object.fromEntries(
keep.split(",")
.filter(key => key in props)
.map(key => [key, props[key]]));
}
const features = hexagons.map(h3Idx => {
const coordinates = [h3.cellToBoundary(h3Idx, true)];
const properties = Object.assign({ h3: h3Idx }, props);
return {
type: "Feature",
geometry: { type: "Polygon", coordinates },
properties
}
});
return {
type: "FeatureCollection",
features
};
}
}; mapshaper `
"ne_110m_admin_0_countries.shp" `
-filter "['Germany','Italy'].includes(NAME)" `
-explode `
-require ms_h3.js `
-each "this.geojson=h3(this.geojson,5,'NAME,ISO_A3')" `
-clean `
-o ger_ita_h3_res5.json "format=geojson" I think this change is fantastic and opens up many new possibilities. The question of whether this functionality should be a variation of the There might be another place where new geometry could be created - the import command
or
|
I'm not quite ready to support function calls as arguments to the There's a new The Finally, I added the ability to put JSON-formatted data (GeoJSON, TopoJSON or an array of JSON records) directly into the command line, like this: These updates should make the |
This all sounds great. I'm currently working with |
I've tried it the new options of the run command and think it gives all the flexibility one could think of. The 3-4 steps necessary (require module, run script, add to io, import by name) maybe feels a bit clumsy/verbose but is doable. 👍 |
Hi, I've made some updates to the This isn't any less verbose than my earlier attempts... I guess I'm going for versatility more than conciseness here. It would be nice to find a less kludgy syntax. Maybe the |
Thanks for the proactive information.
I'm going to test my assumptions, and see which hold true... |
I fail to test right at the start #613 😢 |
#613 should be resolved now. To your questions...
|
This all sounds very reasonable. I had time to make my first steps with the new syntax and it works quite well for me. 👍 |
I wonder if there is a way to to run a required command that returns e.g. geojson that gets directly imported in the mapshaper context.
I think this would be handy to act like a simple plugin system.
I'm thinking of using 3rd-party geo libraries like h3-js polygonToCells, geojson-buffer or turf...
maybe it could just be an extension of the
run
command and look like this:mapshaper somePoly.shp -require 'myH3wraper.js' -run 'poly2Cells(target)' input -o poly_h3.json
The
input
option after-run
would make mapshaper not run the return value as a command but use the return value (Array) as new inputs. Or it could be a new command named '-add' or '-import'The text was updated successfully, but these errors were encountered: