如何通过命令行为mapshaper提供可变百分比?

huangapple go评论57阅读模式
英文:

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

如何通过命令行为mapshaper提供可变百分比?

for bonaire. The original data looks like:

如何通过命令行为mapshaper提供可变百分比?

If I increase the percentage, to 12, I get:

如何通过命令行为mapshaper提供可变百分比?

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.

huangapple
  • 本文由 发表于 2023年4月20日 00:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定