I've got some GIS data with many data fields for each feature. I'm using Mapshaper to simplify it into a light, lean topojson file.

I want the ID fields of these features in the output to be a modified composite of different fields, for example: fieldA_fieldB.

I've tried putting an expression in the export option:

"drop table" "id-field=fieldA+'_'+fieldB"

...but it didn't work. I can't immediately see anything in the command reference to do what I want to do.

For bonus points I'd also like to run an expression over the field to modify it in other ways, for example, one of my fields is a 3-digit number with preceding zeros (e.g. 092) and if possible I'd like to remove these while creating the topojson, e.g. taking 092 and someName and outputting the ID field as 92_someName. If this isn't possible in Mapshaper I can work around it.


You can use the -each command to create a new field for each feature in the currently selected layer based on a javascript expression running on that feature's data which has access to all its field data.

Here's a simple example that creates a field like new_field 'field-data-A_field-data-B'. Get the (case sensitive) names of the fields you want by using -info:

-each "new_field = fieldA + '_' + fieldB"

Then, once you've got that field, you can export it as an ID field with something like this in the export feature:

"id-field=new_field" "drop-table"

Since it's a javascript expression, you can do almost any transformation on the field data, such as regular expression, mathematical operations, etc. So for example, for the example of stripping preceding zeros from a fieldA with values like 004, I could simply convert it from a string to a number then back again:

-each "new_field = parseFloat( fieldA ) + '_' + fieldB"
  • There's no other way to add custom attributes to the export? Currently I'm doing this then using find & replace to split them apart into separate attributes data-name class and id
    – Leeroy
    Apr 28 '20 at 16:53

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.