gissimplifymapshaper

How can I provide a variable percentage to mapshaper via the cli?


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

bonaire simplified

for bonaire. The original data looks like:

bonaire original

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

bonaire simplified better

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?


Solution

  • 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.