英文:
How can I provide a variable percentage to mapshaper via the cli?
问题
尝试使用 mapshaper 控制百分比的方法似乎没有生效。您是否尝试过直接联系 mapshaper 的开发者或社区获取支持?
英文:
I need to simplify Admin 0 – Details > map units
10m NaturalEarthData ( file ). It is 25mb and I would like to get it down to around 1mb or less. This should be easy because the 110m near-equivalent data is ~1mb (it is missing features I need).
mapshaper looks like it should do the job.
Trying the command:
mapshaper ne_10m_admin_0_map_units.geo.json -simplify 1% keep-shapes -o simplified.geo.json
produces
for bonaire. The original data looks like:
If I increase the percentage, to 12, I get:
which is good enough, but now other polygons contain more detail than what I need, increasing the file size.
What I would like to be able to do is have more control over the percentage or something so that I can keep the file small. Small areas, like Bonaire, would have 12 applied and everyone else would be reduced to 5 or something.
According to the simplify documentation:
> variable
Apply a variable amount of simplification to the paths in a polygon or polygon layer. This flag changes the interval=, percentage= and resolution= options to accept JavaScript expressions instead of literal values. (See the -each command for information on mapshaper JS expressions).
The each command implies that there is be a this
object available with the information I need to control the percentage.
To test, creating the javascript file:
function percentage() {
console.log( this )
return 0.01
}
exports.percentage = percentage
and executed the command:
mapshaper ne_10m_admin_0_map_units.geo.json -require js_expression.js alias=_ -simplify variable percentage='_.percentage()' weighted keep-shapes -o simplified.geo.json
My function is used and the console.logging of this
works, but it outputs:
{ percentage: [Function: percentage] }
and not the this
that the each
documentation discusses.
Is there a way to use mapshaper to control the percentage? If so, what have I missed?
Is there another way to use mapshaper to do what I need it to do?
答案1
得分: 0
以下是已翻译的内容:
错过的关键部分是需要将this
参数传递给命令行中的函数。它不会自动传递。
调用mapshaper
如下:
mapshaper ne_10m_admin_0_map_units.geo.json -require js_expression.js alias=_ -simplify variable percentage='_.percentage(this)' weighted keep-shapes -o simplified.geo.json
然后编写JavaScript代码如下:
function percentage(a) {
console.log( a.area )
return 0.01
}
exports.percentage = percentage
可以工作并输出处理过的要素的面积。返回的值可以是所需的任何值。
英文:
The key part that was missed is that this
needs to be passed to the function on the command line. It is not automatically passed.
Calling mapshaper with:
mapshaper ne_10m_admin_0_map_units.geo.json -require js_expression.js alias=_ -simplify variable percentage='_.percentage(this)' weighted keep-shapes -o simplified.geo.json
and then writing the javascript code as:
function percentage(a) {
console.log( a.area )
return 0.01
}
exports.percentage = percentage
works and will output the area of the features processed. The value returned can be anything required.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论