Comments (4)
Hi!
I should be able to make some changes to support your use cases. Part of the problem may be that mapshaper is using import()
instead of require()
to load external modules. import()
can load node-style modules, but (as far as I understand) doesn't search the same set of paths as require()
, which explains why modules installed locally with npm
aren't loading. I could try using require()
if import()
fails.
I'll look into the global context issues you reported.
from mapshaper.
I investigated a bit further and i think the last bullet (no access to context) is just caused because the function is enforced to be a nested property of the alias and not on the global level.
with this script...
module.exports = {
fnA:function(){
console.log(Object.getOwnPropertyDescriptors(this))
},
nested:{
fnB:function(){
console.log(Object.getOwnPropertyDescriptors(this))
}
}
}
mapshaper -require "./index.js" -run "{fnA()}" -run "{nested.fnB()}"
... fnA
has context in this
and nested.fnB
does not
At the moment functions in npm modules are always a nested properties. This make the difference?!
from mapshaper.
Just to give you some context. The functions I use most often generate geojson output based on geojson input. And the template I use looks something like this:
const fnName = "myFn"
module.exports = {
[fnName]: function (arg1, arg2, arg3) {
const { $, target } = this;
if ($) {
const geojson = $.geojson;
// generate modified geojson
return geojsonMOD;
} else if (target) {
const fnName_ = `[+ ${fnName}]`;
console.info(`${fnName_} The target gets cloned and renamed - use '-each ${fnName}(...)' to alter it in place`)
let args = [...arguments].map(JSON.stringify)
let name = [target.layer_name, `_${fnName}`, ...args]
return `-filter "true" target=${target.layer_name} + name=${name.join('_')}
-each this.geojson=${fnName}(${args.join(',')})`;
}
}
}
This allows the function to be called with -run myFn(1,2,3)
to create a new layer based on the current target as well as with -each this.geojson = myFn(1,2,3)
to change the current target in place with the same arguments.
(maybe I should use the io api of -run
to generate new layer instead of copying them with -filter "true"
🤔 )
from mapshaper.
I've just tested how to make the context thing work. Turns out that If npm modules would also be asigned to global like so the context would be available with this. This would fix this issue as well.
Maybe the functions in the module should be assigned to global
by default and only should nested if alias
is used
from mapshaper.
Related Issues (20)
- Filter lines still create polygons HOT 5
- When hovering mouse over feature and scrolling, fill color freaks out HOT 1
- Point layers and data layers do not show up HOT 2
- Cannot convert between WGS84 and Webmercator after converting and saving data as EPSG:3857. HOT 3
- Error on simplifying GeoJSON file HOT 11
- Javascript error related to polygon editing causes GUI to crash HOT 1
- There is an issue when you import a shapefile HOT 2
- Clip command removes Features visually but not from exported file. HOT 4
- Sticky hand HOT 1
- snap effects multiples layers HOT 1
- geojson from points HOT 2
- Export failed Export failed for an unknown reason HOT 3
- topojson files in power bi HOT 2
- Problem in drawing polygon and importing snapshot file HOT 4
- Export to Shape File Not working HOT 12
- Bug: can't open snapshot file I created on mapshaper.org HOT 3
- Bug: copy as GeoJSON doesn't copy GeoJSON HOT 1
- filter-slivers remove-empty bug HOT 3
- Template expressions and object literals HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapshaper.