As part of trying to find an answer for How to -explode only one subfeature from only one feature? I created some fields in one layer only, then, after doing some operations, tried to merge that layer back with one other specific layer.

The docs for merge-layers say you can use target= and name= but I've not managed to get either to work:

  • Apparently target= can accept a comma-separated list of layers, but it seems not to work:

    • If I wrap each layer name in quotes:

      -merge-layers target="'layer A', 'layer B'"

      ...I get an error suggesting it's looking for one layer called 'layer A', 'layer B':

      Missing target: 'layer A', 'layer B'

      Available layers: [list of layers here]

    • If I don't wrap each layer name in quotes:

      -merge-layers target="layer A, layer B"

      ... then I get no errors, but nothing happens. It doesn't merge the layers.

    • If I add a name then it renames only the first layer in the comma separated list (regardless of what layer is selected). For example, this:

      -merge-layers target="layer A, layer B" name="new layer"

      ...renames layer A to "new layer" but does nothing at all to layer B. So for whatever reason it seems to ignore the layers after the first in the comma seperated list.

  • I also tried using name like this:

    -merge-layers name="'layer A', 'layer B'"

    ...which ran into this error, despite the fact I had used -filter-fields to remove these fields from the layers I was targeting:

    Field missing from one or more layers: someField

    ...but I later discovered that this was because I'd misunderstood the name parameter: this was simply the name the new merged field would take, so since I had no target defined, it was trying to merge all layers and was picking up on errant fields in layers other than the ones I was trying to merge.

  • I also tried using one layer name as a regular parameter and one as a target= but it didn't like that either:

    -merge-layers 'layer A' target="layer B"

    ...gives this error:

    Unexpected option: layer A

  • I also tried two target options, but this doesn't work:

    -merge-layers target="layer A" target="layer B"

All the usage examples I can find for -merge-layers use it to merge every layer.

What's the correct way to target and merge specific layers?


After a lot of trial and error I've found a crude workaround. I'm guessing it's some limitation of the browser-based GUI interface version of MapShaper that target's comma seperated list doesn't work here for some reason?

The only method I could find that worked was:

  • Rename the layers to be merged so they contain something no other layer name has. For example, adding merge to the start:

    -rename-layers 'merge layer A' target="layer A" 
    -rename-layers 'merge layer B' target="layer B"
  • Use -merge-layers with a * wildcard in the target to target them all. Assuming we were trying to merge layer B into layer A:

    -merge-layers target="merge*" name="layer A"

This does work and actually merges the layers into a new layer named "layer A".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.