mapshaper 0.7.2 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/mapshaper.js +1 -1
- package/package.json +4 -2
- package/www/mapshaper.js +1 -1
- package/www/docs/_assets/cmd-search.js +0 -213
- package/www/docs/_assets/docs.css +0 -722
- package/www/docs/_assets/docs.js +0 -75
- package/www/docs/_assets/highlight.css +0 -10
- package/www/docs/essentials/command-line.html +0 -127
- package/www/docs/essentials/command-line.html.md +0 -112
- package/www/docs/essentials/web-app.html +0 -138
- package/www/docs/essentials/web-app.html.md +0 -106
- package/www/docs/examples/basics.html +0 -275
- package/www/docs/examples/basics.html.md +0 -370
- package/www/docs/examples/data/Makefile +0 -31
- package/www/docs/examples/data/globe.msx +0 -0
- package/www/docs/examples/data/globe.svg +0 -616
- package/www/docs/examples/data/globe.txt +0 -21
- package/www/docs/examples/data/globe.zip +0 -0
- package/www/docs/examples/data/ne_50m_admin_0_countries.geojson +0 -1
- package/www/docs/examples/data/ne_50m_admin_1_states_provinces_lakes.geojson +0 -1
- package/www/docs/examples/data/us-states.msx +0 -0
- package/www/docs/examples/data/us-states.svg +0 -56
- package/www/docs/examples/data/us-states.txt +0 -6
- package/www/docs/examples/data/us-states.zip +0 -0
- package/www/docs/examples/globe.html +0 -108
- package/www/docs/examples/globe.html.md +0 -64
- package/www/docs/examples/us-states.html +0 -88
- package/www/docs/examples/us-states.html.md +0 -44
- package/www/docs/formats/csv.html +0 -127
- package/www/docs/formats/csv.html.md +0 -97
- package/www/docs/formats/dbf.html +0 -87
- package/www/docs/formats/dbf.html.md +0 -39
- package/www/docs/formats/flatgeobuf.html +0 -85
- package/www/docs/formats/flatgeobuf.html.md +0 -41
- package/www/docs/formats/geojson.html +0 -107
- package/www/docs/formats/geojson.html.md +0 -65
- package/www/docs/formats/geopackage.html +0 -87
- package/www/docs/formats/geopackage.html.md +0 -42
- package/www/docs/formats/json.html +0 -83
- package/www/docs/formats/json.html.md +0 -35
- package/www/docs/formats/kml.html +0 -82
- package/www/docs/formats/kml.html.md +0 -39
- package/www/docs/formats/overview.html +0 -191
- package/www/docs/formats/overview.html.md +0 -34
- package/www/docs/formats/shapefile.html +0 -123
- package/www/docs/formats/shapefile.html.md +0 -84
- package/www/docs/formats/snapshot.html +0 -87
- package/www/docs/formats/snapshot.html.md +0 -39
- package/www/docs/formats/svg.html +0 -99
- package/www/docs/formats/svg.html.md +0 -51
- package/www/docs/formats/topojson.html +0 -102
- package/www/docs/formats/topojson.html.md +0 -54
- package/www/docs/gallery/index.html +0 -80
- package/www/docs/gallery/index.html.md +0 -29
- package/www/docs/guides/combining-layers.html +0 -109
- package/www/docs/guides/combining-layers.html.md +0 -89
- package/www/docs/guides/expressions.html +0 -600
- package/www/docs/guides/expressions.html.md +0 -376
- package/www/docs/guides/programmatic.html +0 -117
- package/www/docs/guides/programmatic.html.md +0 -91
- package/www/docs/guides/projections.html +0 -158
- package/www/docs/guides/projections.html.md +0 -118
- package/www/docs/guides/simplification.html +0 -110
- package/www/docs/guides/simplification.html.md +0 -96
- package/www/docs/guides/topology.html +0 -90
- package/www/docs/guides/topology.html.md +0 -63
- package/www/docs/images/cal-counties.png +0 -0
- package/www/docs/images/simplification-detail.png +0 -0
- package/www/docs/images/simplification-dp.png +0 -0
- package/www/docs/images/simplification-mod2.png +0 -0
- package/www/docs/images/tiger-counties.png +0 -0
- package/www/docs/index.html +0 -101
- package/www/docs/index.html.md +0 -59
- package/www/docs/reference.html +0 -1302
- package/www/docs/reference.html.md +0 -1817
- package/www/docs/whats-new.html +0 -77
- package/www/docs/whats-new.html.md +0 -59
- package/www/llms-full.txt +0 -4053
- package/www/llms.txt +0 -55
package/www/llms-full.txt
DELETED
|
@@ -1,4053 +0,0 @@
|
|
|
1
|
-
# Mapshaper
|
|
2
|
-
|
|
3
|
-
> Mapshaper is a command-line tool and in-browser editor for geographic vector data. It reads and writes Shapefile, GeoJSON, TopoJSON, GeoPackage, FlatGeobuf, KML/KMZ, CSV/TSV, DBF, JSON records and SVG, and can simplify, clip, dissolve, join, project and otherwise transform layers. The CLI and web app share the same command language, so commands written interactively in the browser console run unchanged in scripts.
|
|
4
|
-
|
|
5
|
-
These docs cover the same material in two forms:
|
|
6
|
-
|
|
7
|
-
- **Markdown mirrors** of every page (this file links to them) for use by language models and other tools.
|
|
8
|
-
- **HTML pages** at the same paths without the trailing `.md`, with navigation, search and syntax highlighting, intended for humans.
|
|
9
|
-
|
|
10
|
-
When in doubt about a command's behavior, the [command reference](https://mapshaper.org/docs/reference.html.md) is the source of truth. The Node.js source code lives at <https://github.com/mbloch/mapshaper>.
|
|
11
|
-
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
<!-- https://mapshaper.org/docs/ -->
|
|
15
|
-
|
|
16
|
-
# Mapshaper documentation
|
|
17
|
-
|
|
18
|
-
<p class="lead">Mapshaper is a tool for editing Shapefile, GeoJSON, TopoJSON, GeoPackage, FlatGeobuf, KML and CSV data — available as a command-line program and as a web app at <a href="/">mapshaper.org</a>.</p>
|
|
19
|
-
|
|
20
|
-
<div class="section-cards">
|
|
21
|
-
<a class="section-card" href="/docs/essentials/command-line.html">
|
|
22
|
-
<p class="card-title">Getting started</p>
|
|
23
|
-
<p class="card-desc">Introductions to the CLI and the web app.</p>
|
|
24
|
-
</a>
|
|
25
|
-
<a class="section-card" href="/docs/guides/simplification.html">
|
|
26
|
-
<p class="card-title">Guides</p>
|
|
27
|
-
<p class="card-desc">Task-oriented walk-throughs: simplification, topology, joining layers, scripting from Node.</p>
|
|
28
|
-
</a>
|
|
29
|
-
<a class="section-card" href="/docs/formats/overview.html">
|
|
30
|
-
<p class="card-title">File formats</p>
|
|
31
|
-
<p class="card-desc">Read/write details for Shapefile, GeoJSON, TopoJSON, GeoPackage, FlatGeobuf, KML, CSV and more.</p>
|
|
32
|
-
</a>
|
|
33
|
-
<a class="section-card" href="/docs/examples/basics.html">
|
|
34
|
-
<p class="card-title">Examples</p>
|
|
35
|
-
<p class="card-desc">Short recipes for common GIS tasks, plus a Gallery of full map examples with source data.</p>
|
|
36
|
-
</a>
|
|
37
|
-
</div>
|
|
38
|
-
|
|
39
|
-
## Featured examples
|
|
40
|
-
|
|
41
|
-
<!-- mapshaper:featured count=2 -->
|
|
42
|
-
|
|
43
|
-
<p class="featured-maps-link"><a href="/docs/gallery/">See the full gallery →</a></p>
|
|
44
|
-
|
|
45
|
-
## What's new
|
|
46
|
-
|
|
47
|
-
A curated log of recently added features is at [What's new](/docs/whats-new.html.md). The full [changelog](https://github.com/mbloch/mapshaper/blob/master/CHANGELOG.md) is on GitHub.
|
|
48
|
-
|
|
49
|
-
## Help and feedback
|
|
50
|
-
|
|
51
|
-
- Found a bug or want to request a feature? [Share feedback](https://tally.so/r/44Njok) or [open an issue on GitHub](https://github.com/mbloch/mapshaper/issues).
|
|
52
|
-
- Mapshaper is free software. If it's useful to you, please consider [supporting its development](/sponsor.html).
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
<!-- https://mapshaper.org/docs/whats-new.html -->
|
|
57
|
-
|
|
58
|
-
# What's new
|
|
59
|
-
|
|
60
|
-
This is a curated list of recently added features. For the full list of changes, including bug fixes and internal work, see the [changelog](https://github.com/mbloch/mapshaper/blob/master/CHANGELOG.md) on GitHub.
|
|
61
|
-
|
|
62
|
-
## April 2026
|
|
63
|
-
|
|
64
|
-
<div class="whats-new-entry">
|
|
65
|
-
|
|
66
|
-
**Undo/redo buttons.** The web UI now has an undo/redo toolbar. Undo and redo were already supported in the geometry and attribute editing modes; the toolbar makes them more discoverable for users who didn't know the keyboard shortcuts (**⌘Z** / **⌘⇧Z**) existed.
|
|
67
|
-
|
|
68
|
-
</div>
|
|
69
|
-
|
|
70
|
-
<div class="whats-new-entry">
|
|
71
|
-
|
|
72
|
-
**Command files.** A sequence of Mapshaper commands can be written to a `.txt` file with `#` comments and no shell quoting, and run with `-run <file>` (or just `mapshaper commands.txt`). Command files can also be written in a shell-compatible way, if you want to be able to paste the commands into the terminal or add them to a shell script. In a future release, these files will also be runnable in the browser.
|
|
73
|
-
|
|
74
|
-
```bash
|
|
75
|
-
mapshaper build.txt
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
→ See [Command files](/docs/reference.html.md#command-files) in the reference.
|
|
79
|
-
</div>
|
|
80
|
-
|
|
81
|
-
<div class="whats-new-entry">
|
|
82
|
-
|
|
83
|
-
**Variable interpolation in commands.** Command files and command lines support `{{VAR}}` placeholders, resolved at run time against environment variables (`{{env.HOME}}`), values set with the new `-vars` and `-defaults` commands, and variables defined dynamically by `-calc`, `-define` and `-each` expressions.
|
|
84
|
-
|
|
85
|
-
```bash
|
|
86
|
-
mapshaper -vars YEAR=2030 PCT=5 -run build.txt
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
→ See [Variable interpolation](/docs/reference.html.md#variable-interpolation), [`-vars`](/docs/reference.html.md#-vars) and [`-defaults`](/docs/reference.html.md#-defaults).
|
|
90
|
-
</div>
|
|
91
|
-
|
|
92
|
-
<div class="whats-new-entry">
|
|
93
|
-
|
|
94
|
-
**Farewell to dissolve2.** The `-dissolve` command now uses Mapshaper's most robust dissolve function, which can handle overlaps, gaps and other topology errors. The legacy faster algorithm is still available as `-dissolve no-repair`. (The old `-dissolve2` is just an alias for `-dissolve`.)
|
|
95
|
-
|
|
96
|
-
→ See [`-dissolve`](/docs/reference.html.md#-dissolve).
|
|
97
|
-
</div>
|
|
98
|
-
|
|
99
|
-
<div class="whats-new-entry">
|
|
100
|
-
|
|
101
|
-
**FlatGeobuf and GeoPackage support.** Mapshaper reads and writes FlatGeobuf (`.fgb`) and GeoPackage (`.gpkg`) files.
|
|
102
|
-
|
|
103
|
-
→ See [FlatGeobuf](/docs/formats/flatgeobuf.html.md), [GeoPackage](/docs/formats/geopackage.html.md) and [`-i layers=`](/docs/reference.html.md#-i-input).
|
|
104
|
-
</div>
|
|
105
|
-
|
|
106
|
-
<div class="whats-new-entry">
|
|
107
|
-
|
|
108
|
-
**SVG import.** SVG files exported by Mapshaper can be re-imported — useful for making stylistic edits.
|
|
109
|
-
|
|
110
|
-
→ See [SVG](/docs/formats/svg.html.md).
|
|
111
|
-
</div>
|
|
112
|
-
|
|
113
|
-
---
|
|
114
|
-
|
|
115
|
-
<!-- https://mapshaper.org/docs/reference.html -->
|
|
116
|
-
|
|
117
|
-
# Command reference
|
|
118
|
-
|
|
119
|
-
## Command line syntax
|
|
120
|
-
|
|
121
|
-
Mapshaper takes a list of commands and runs them in sequence, from left to right. A command consists of the name of a command prefixed by a hyphen, followed by options for the command. The initial import command `-i` can be omitted.
|
|
122
|
-
|
|
123
|
-
#### Example
|
|
124
|
-
|
|
125
|
-
```bash
|
|
126
|
-
# Read a Shapefile, simplify using Douglas-Peucker, output as GeoJSON.
|
|
127
|
-
mapshaper provinces.shp -simplify dp 20% -o precision=0.00001 output.geojson
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### Command options can take three forms:
|
|
131
|
-
|
|
132
|
-
- Values, like `provinces.shp` and `output.geojson` in the above example
|
|
133
|
-
|
|
134
|
-
- Flags, like `dp`
|
|
135
|
-
|
|
136
|
-
- Name/value pairs, like `precision=0.00001`
|
|
137
|
-
|
|
138
|
-
### Common options
|
|
139
|
-
|
|
140
|
-
The following options are documented here, because they are used by many commands.
|
|
141
|
-
|
|
142
|
-
`name=` Rename the layer (or layers) modified by a command.
|
|
143
|
-
|
|
144
|
-
`target=` Specify the layer or layers targeted by a command. Takes the name of a layer, the number of a layer (first layer is 1), or a comma-separated list of layer names or numbers. Names may contain the `*` wildcard.
|
|
145
|
-
|
|
146
|
-
`+` Use the output of a command to create a new layer or layers instead of replacing the target layer(s). Use together with the `name=` option to assign a name to the new layer(s).
|
|
147
|
-
|
|
148
|
-
#### Example
|
|
149
|
-
```bash
|
|
150
|
-
# Make a derived layer containing a subset of features
|
|
151
|
-
# while retaining the original layer
|
|
152
|
-
mapshaper states.geojson -filter 'ST == "AK"' + name=alaska -o output/ target=*
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
## Command files
|
|
156
|
-
|
|
157
|
-
As an alternative to typing commands on the command line, you can put them in a plain-text command file and run them with the mapshaper CLI.
|
|
158
|
-
|
|
159
|
-
Command files offer a few conveniences over a shell script or Makefile:
|
|
160
|
-
|
|
161
|
-
- Hash-delimited (`#`) comments, both on their own line and at the end of a line.
|
|
162
|
-
- No need to escape `*` or other shell metacharacters; commands aren't passed through the shell.
|
|
163
|
-
- Trailing-backslash line continuations are accepted but not required — lines that don't begin with `-` are joined onto the previous command.
|
|
164
|
-
- Variable interpolation using `{{VAR}}` placeholders. See [variables](#variables) below.
|
|
165
|
-
|
|
166
|
-
(Support for running command files in the mapshaper web UI is planned for a future release.)
|
|
167
|
-
|
|
168
|
-
### File format
|
|
169
|
-
|
|
170
|
-
A mapshaper command file is a `.txt` file whose first non-blank, non-comment line starts with `mapshaper`.
|
|
171
|
-
|
|
172
|
-
```
|
|
173
|
-
mapshaper
|
|
174
|
-
-i provinces.shp
|
|
175
|
-
# Use Douglas Peucker simplification
|
|
176
|
-
-simplify dp 20%
|
|
177
|
-
-o precision=0.00001 output.geojson
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
If you write the command file using shell-compatible syntax — trailing `\` for line continuations and no `#` comments — it can also be pasted directly onto a bash command line, where the leading `mapshaper` word invokes the CLI. To make the above example shell compatible, you could write:
|
|
181
|
-
|
|
182
|
-
```
|
|
183
|
-
mapshaper \
|
|
184
|
-
-i provinces.shp \
|
|
185
|
-
-simplify dp 20% \
|
|
186
|
-
-o precision=0.00001 output.geojson
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
### Running a command file
|
|
190
|
-
|
|
191
|
-
The command for running a command file is [`-run`](#-run):
|
|
192
|
-
|
|
193
|
-
```bash
|
|
194
|
-
mapshaper -run build.txt
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
`mapshaper commands.txt` is a shortcut for `mapshaper -run commands.txt`.
|
|
198
|
-
|
|
199
|
-
## Variable interpolation
|
|
200
|
-
|
|
201
|
-
Command files and command lines may contain `{{VAR}}` placeholders, which are substituted just before each command runs. Two forms are recognized:
|
|
202
|
-
|
|
203
|
-
- `{{VAR}}` — substituted with the value of `VAR`.
|
|
204
|
-
- `{{env.NAME}}` — substituted with the value of the `NAME` environment variable.
|
|
205
|
-
|
|
206
|
-
This syntax allows you to interpolate all or part of a command option. For example, `-simplify {{SIMPLIFY_METHOD}} resolution={{SIMPLIFY_RESOLUTION}}`.
|
|
207
|
-
|
|
208
|
-
Variables can be set in several ways:
|
|
209
|
-
- The [`-vars`](#-vars) command sets one or more variables, always overwriting any previous value.
|
|
210
|
-
- The [`-defaults`](#-defaults) command set only those values that do not already exist.
|
|
211
|
-
- Assignments in `-calc` and `-define` expressions create new variables.
|
|
212
|
-
- Assigning a property to the `global` object in an `-each` expression creates a new variable.
|
|
213
|
-
|
|
214
|
-
`-vars` and `-defaults` write to a templating-scope store; the other commands write to an expression-scope store (`global`). `{{X}}` substitution checks the templating scope first and falls back to the expression scope, so values from any of the four mechanisms above are reachable. Bare names in JS expressions only see the expression scope — a name set by `-vars` is *not* readable by bare name from inside `-each`, `-filter`, etc. See [JavaScript expressions](/docs/guides/expressions.html.md#sharing-state-across-commands) for the full story.
|
|
215
|
-
|
|
216
|
-
#### Example
|
|
217
|
-
|
|
218
|
-
`build.txt`:
|
|
219
|
-
```
|
|
220
|
-
mapshaper
|
|
221
|
-
-defaults YEAR=2024 PCT=10 # overridable defaults
|
|
222
|
-
-i sources/counties_{{YEAR}}.shp
|
|
223
|
-
-simplify {{PCT}}%
|
|
224
|
-
-o out/counties_{{YEAR}}_simplified.shp
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
Run with the command file's defaults:
|
|
228
|
-
```bash
|
|
229
|
-
mapshaper build.txt
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
Or override default values from the command line:
|
|
233
|
-
```bash
|
|
234
|
-
mapshaper -vars YEAR=2030 PCT=5 -run build.txt
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
## Index of commands
|
|
238
|
-
|
|
239
|
-
**File I/O**
|
|
240
|
-
|
|
241
|
-
[-i (input)](#-i-input)
|
|
242
|
-
[-o (output)](#-o-output)
|
|
243
|
-
|
|
244
|
-
**Editing**
|
|
245
|
-
|
|
246
|
-
[-affine](#-affine)
|
|
247
|
-
[-classify](#-classify)
|
|
248
|
-
[-clean](#-clean)
|
|
249
|
-
[-clip](#-clip)
|
|
250
|
-
[-colorizer](#-colorizer)
|
|
251
|
-
[-dashlines](#-dashlines)
|
|
252
|
-
[-dissolve](#-dissolve)
|
|
253
|
-
[-dissolve2](#-dissolve2)
|
|
254
|
-
[-divide](#-divide)
|
|
255
|
-
[-dots](#-dots)
|
|
256
|
-
[-drop](#-drop)
|
|
257
|
-
[-each](#-each)
|
|
258
|
-
[-erase](#-erase)
|
|
259
|
-
[-explode](#-explode)
|
|
260
|
-
[-filter](#-filter)
|
|
261
|
-
[-filter-fields](#-filter-fields)
|
|
262
|
-
[-filter-islands](#-filter-islands)
|
|
263
|
-
[-filter-slivers](#-filter-slivers)
|
|
264
|
-
[-frame](#-frame)
|
|
265
|
-
[-graticule](#-graticule)
|
|
266
|
-
[-grid](#-grid)
|
|
267
|
-
[-include](#-include)
|
|
268
|
-
[-inlay](#-inlay)
|
|
269
|
-
[-innerlines](#-innerlines)
|
|
270
|
-
[-join](#-join)
|
|
271
|
-
[-lines](#-lines)
|
|
272
|
-
[-merge-layers](#-merge-layers)
|
|
273
|
-
[-mosaic](#-mosaic)
|
|
274
|
-
[-point-grid](#-point-grid)
|
|
275
|
-
[-points](#-points)
|
|
276
|
-
[-polygons](#-polygons)
|
|
277
|
-
[-proj](#-proj)
|
|
278
|
-
[-rectangle](#-rectangle)
|
|
279
|
-
[-rectangles](#-rectangles)
|
|
280
|
-
[-rename-fields](#-rename-fields)
|
|
281
|
-
[-rename-layers](#-rename-layers)
|
|
282
|
-
[-require](#-require)
|
|
283
|
-
[-run](#-run)
|
|
284
|
-
[-scalebar](#-scalebar)
|
|
285
|
-
[-shape](#-shape)
|
|
286
|
-
[-simplify](#-simplify)
|
|
287
|
-
[-snap](#-snap)
|
|
288
|
-
[-sort](#-sort)
|
|
289
|
-
[-split](#-split)
|
|
290
|
-
[-split-on-grid](#-split-on-grid)
|
|
291
|
-
[-subdivide](#-subdivide)
|
|
292
|
-
[-style](#-style)
|
|
293
|
-
[-symbols](#-symbols)
|
|
294
|
-
[-union](#-union)
|
|
295
|
-
[-uniq](#-uniq)
|
|
296
|
-
|
|
297
|
-
**Control Flow**
|
|
298
|
-
|
|
299
|
-
[-if](#-if)
|
|
300
|
-
[-elif](#-elif)
|
|
301
|
-
[-else](#-else)
|
|
302
|
-
[-endif](#-endif)
|
|
303
|
-
[-stop](#-stop)
|
|
304
|
-
[-target](#-target)
|
|
305
|
-
|
|
306
|
-
**Information**
|
|
307
|
-
|
|
308
|
-
[-calc](#-calc)
|
|
309
|
-
[-colors](#-colors)
|
|
310
|
-
[-comment](#-comment)
|
|
311
|
-
[-defaults](#-defaults)
|
|
312
|
-
[-encodings](#-encodings)
|
|
313
|
-
[-help](#-help)
|
|
314
|
-
[-info](#-info)
|
|
315
|
-
[-inspect](#-inspect)
|
|
316
|
-
[-print](#-print)
|
|
317
|
-
[-projections](#-projections)
|
|
318
|
-
[-quiet](#-quiet)
|
|
319
|
-
[-vars](#-vars)
|
|
320
|
-
[-verbose](#-verbose)
|
|
321
|
-
[-version](#-version)
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
## I/O Commands
|
|
325
|
-
|
|
326
|
-
### -i (input)
|
|
327
|
-
|
|
328
|
-
Input one or more files in a supported vector data format. Supported file types include: Shapefile, GeoJSON, TopoJSON, GeoPackage, FlatGeobuf, KML, JSON data records, DBF, CSV/TSV.
|
|
329
|
-
|
|
330
|
-
The `-i` command is assumed if `mapshaper` is followed by the path of an input data file.
|
|
331
|
-
|
|
332
|
-
Mapshaper does not fully support M and Z type Shapefiles. The M and Z data is lost when these files are imported.
|
|
333
|
-
|
|
334
|
-
When multiple input files are given, they can either be processed together (as a group of layers with shared topology) or separately (as a sequence of independent runs). Use `combine-files` to process them together, or `batch-mode` to process them separately.
|
|
335
|
-
|
|
336
|
-
For backward compatibility, multiple input files are currently processed in batch mode by default; this default will change in a future release. Mapshaper prints a deprecation notice when batch mode is triggered implicitly. Existing scripts that rely on batch processing should add the `batch-mode` flag.
|
|
337
|
-
|
|
338
|
-
**Options**
|
|
339
|
-
|
|
340
|
-
`<files>` or `files=` File(s) to input (space-separated list). Use `-` to import from `/dev/stdin`.
|
|
341
|
-
|
|
342
|
-
In place of a file name, you can also pass data inline:
|
|
343
|
-
|
|
344
|
-
- **JSON.** A string starting with `{` or `[` is treated as a literal JSON object or array.
|
|
345
|
-
- **CSV.** A comma-delimited string is treated as inline CSV when it contains a newline (a real one or the literal escape sequence `\n`) and both the first and second lines contain at least one comma.
|
|
346
|
-
|
|
347
|
-
```bash
|
|
348
|
-
mapshaper -i 'lat,lon,label\n48.86,2.35,Paris\n51.51,-0.13,London' -o cities.json
|
|
349
|
-
```
|
|
350
|
-
|
|
351
|
-
`combine-files` Import multiple files to separate layers with shared topology. Useful for generating a single TopoJSON file containing multiple geometry objects.
|
|
352
|
-
|
|
353
|
-
`batch-mode` Apply subsequent commands separately to each input file, as if running mapshaper multiple times with the same set of commands. Used together with `-o` to transform a directory of files. Required (in a future release) to use this batch-processing behavior.
|
|
354
|
-
|
|
355
|
-
`merge-files` (Deprecated) Merge features from multiple input files into as few layers as possible. Preferred method: import files to separate layers using `-i combine-files`, then use the `-merge-layers` command to merge layers.
|
|
356
|
-
|
|
357
|
-
`snap` Snap together vertices within a small distance threshold. This option is intended to fix minor coordinate misalignments in adjacent polygons. The snapping distance is 0.0025 of the average segment length.
|
|
358
|
-
|
|
359
|
-
`snap-interval=` Specify snapping distance in source units.
|
|
360
|
-
|
|
361
|
-
`precision=` (Deprecated) Round all coordinates to a specified precision, e.g. `0.001`. It is recommended to set coordinate precision on export, using `-o precision=`.
|
|
362
|
-
|
|
363
|
-
`no-topology` Skip topology identification to speed up processing of large files. For use with commands like `-filter` that don't require topology.
|
|
364
|
-
|
|
365
|
-
`encoding=` Specify encoding used for reading .dbf files and delimited text files. If the `encoding` option is missing, mapshaper will try to detect the encoding of .dbf files. Dbf encoding can also be set using a .cpg file.
|
|
366
|
-
|
|
367
|
-
`id-field=` (Topo/GeoJSON) Import values of "id" property to this data field.
|
|
368
|
-
|
|
369
|
-
`string-fields=` (CSV) List of fields to import as strings (e.g. FIPS,ZIPCODE). Using `string-fields=*` imports all fields as strings.
|
|
370
|
-
|
|
371
|
-
`field-types=` Type hints for importing delimited text. Takes a comma-separated list of field names with type hints appended; e.g. `FIPS:str,zipcode:str`. Recognized type hints include `:str` or `:string`, `:num` or `:number`. Without a type hint, fields containing text data that looks like numeric data, like ZIP Codes, will be converted to numbers.
|
|
372
|
-
|
|
373
|
-
`csv-skip-lines=` Number of lines to skip at the beginning of a CSV file. Useful when a CSV has been exported from a spreadsheet and there are rows of notes above the data section of the worksheet.
|
|
374
|
-
|
|
375
|
-
`csv-lines=` Number of data records to import from a CSV file (default is all).
|
|
376
|
-
|
|
377
|
-
`csv-field-names=` Comma-sep. list of names to assign each field. Can be used in conjunction with `csv-skip-lines=1` to replace names from an existing set of field headers.
|
|
378
|
-
|
|
379
|
-
`csv-fields=` Comma-sep. list of fields to import from a CSV-formatted input file. Fields are filtered as the file is read, which reduces the memory needed to import very large CSV files.
|
|
380
|
-
|
|
381
|
-
`decimal-comma` Import numbers formatted with decimal commas, not decimal points. Accepted formats: `1.000,01` `1 000,01` (both imported as as 1000.01).
|
|
382
|
-
|
|
383
|
-
`csv-dedup-fields` Assign unique names to CSV fields with duplicate names.
|
|
384
|
-
|
|
385
|
-
`csv-filter=` A JavaScript expression for importing a subset of the records in a CSV file. Records are filtered as the file is read, which reduces the memory needed to import very large CSV files.
|
|
386
|
-
|
|
387
|
-
`json-path=` [JSON] Path to an array of data records or a GeoJSON object. For example, `json-path=data/counties` expects a JSON object with the following structure `{"data": {"counties": []}}`.
|
|
388
|
-
|
|
389
|
-
`layers=` [GeoPackage] Comma-separated list of layer names to import from a GeoPackage file. If omitted, all layers are imported.
|
|
390
|
-
|
|
391
|
-
`name=` Rename the imported layer (or layers).
|
|
392
|
-
|
|
393
|
-
**Example**
|
|
394
|
-
|
|
395
|
-
```bash
|
|
396
|
-
# Input a Shapefile with text data in the latin1 encoding
|
|
397
|
-
# and see what kind of data it contains
|
|
398
|
-
mapshaper countries_wgs84.shp encoding=latin1 -info
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
### -o (output)
|
|
402
|
-
|
|
403
|
-
Save content of the target layer(s) to a file or files.
|
|
404
|
-
|
|
405
|
-
**Options**
|
|
406
|
-
|
|
407
|
-
`<file>|<directory>|-` Name of output file or directory. Use `-` to export text-based formats to `/dev/stdout`.
|
|
408
|
-
|
|
409
|
-
`format=shapefile|geojson|topojson|flatgeobuf|geopackage|json|dbf|csv|tsv|svg` Specify output format. If the `format=` option is missing, Mapshaper tries to infer the format from the output filename. If no filename is given, Mapshaper exports to the same format as the input format. The `json` format is an array of objects containing data properties for each feature.
|
|
410
|
-
|
|
411
|
-
`target=` Specify layer(s) to export (comma-separated list). The default target is the output layer(s) of the previous command. Use `target=*` to select all layers.
|
|
412
|
-
|
|
413
|
-
`force` Allow output files to overwrite input files (without this option, overwriting input files is not allowed).
|
|
414
|
-
|
|
415
|
-
`gzip` Apply gzip compression to output files.
|
|
416
|
-
|
|
417
|
-
`zip` Save output files in a single .zip archive.
|
|
418
|
-
|
|
419
|
-
`cut-table` Detach attribute data from shapes and save as a JSON file.
|
|
420
|
-
|
|
421
|
-
`drop-table` Remove attribute data from output.
|
|
422
|
-
|
|
423
|
-
`precision=` Round all coordinates to a specified precision, e.g. `precision=0.001`. Useful for reducing the size of GeoJSON files.
|
|
424
|
-
|
|
425
|
-
`fix-geometry` Remove segment intersections caused by rounding (via the `precision=` option) or TopoJSON quantization, by reverting intersecting areas to the original coordinates. In the case of quantized TopoJSON output, this option produces delta-encoded arcs that contain some decimal numbers. Be sure to test your software for compatibility. Note that this option is only applied if the original paths are free of intersections. Also, some kinds of invalid geometry, like spikes, do not get fixed.
|
|
426
|
-
|
|
427
|
-
`bbox-index` Export a JSON file containing bounding boxes of each output layer.
|
|
428
|
-
|
|
429
|
-
`encoding=` (Shapefile/CSV) Encoding of input text (by default, Shapefile encoding is auto-detected and CSV files are assumed to be UTF-8).
|
|
430
|
-
|
|
431
|
-
`field-order=` (Shapefile/CSV) `field-order=ascending` sorts data fields in alphabetical order of field names (A-Z, case-insensitive).
|
|
432
|
-
|
|
433
|
-
`id-field=` (Topo/GeoJSON/SVG) Specify one or more data fields to use as the "id" property of GeoJSON, TopoJSON or SVG features (comma-separated list). When exporting multiple layers, you can pass a list of field names. The first listed name that is present in a layer's attribute table will be used as the id field for that layer.
|
|
434
|
-
|
|
435
|
-
`bbox` (Topo/GeoJSON) Add bbox property to the top-level object.
|
|
436
|
-
|
|
437
|
-
`extension=` (Topo/GeoJSON) set file extension (default is ".json").
|
|
438
|
-
|
|
439
|
-
`prettify` (Topo/GeoJSON) Format output for readability.
|
|
440
|
-
|
|
441
|
-
`singles` (TopoJSON) Save each output layer as a separate file. Each output file and the TopoJSON object that it contains are named after the corresponding data layer.
|
|
442
|
-
|
|
443
|
-
`quantization=` (TopoJSON) Specify quantization as the maximum number of differentiable points along either dimension. Equivalent to the quantization parameter used by the [topoquantize](https://github.com/topojson/topojson-client#topoquantize) command line program. By default, mapshaper applies quantization equivalent to 0.02 of the average segment length.
|
|
444
|
-
|
|
445
|
-
`no-quantization` (TopoJSON) Arc coordinates are encoded at full precision and without delta-encoding.
|
|
446
|
-
|
|
447
|
-
`presimplify` (TopoJSON) Add a threshold value to each arc vertex in the z position (i.e. [x, y, z]). Useful for dynamically simplifying paths using vertex filtering. Given W as the width of the map viewport in pixels, S as the ratio of content width to viewport width, and pz as the presimplify value of a point, the following expression tests if the point should be excluded from the output path: `pz > 0 && pz < 10000 / (W * S)`.
|
|
448
|
-
|
|
449
|
-
`topojson-precision=` (TopoJSON) Set quantization as a fraction of the average segment length.
|
|
450
|
-
|
|
451
|
-
`ndjson` (GeoJSON/JSON) Output newline-delimited records.
|
|
452
|
-
|
|
453
|
-
`gj2008` (GeoJSON) Generate output that is consistent with the pre-RFC 7946 GeoJSON spec (dating to 2008). Polygon rings are CW and holes are CCW, which is the opposite of the default RFC 7946-compatible output. Mapshaper's default GeoJSON output is now compatible with the current specification (RFC 7946).
|
|
454
|
-
|
|
455
|
-
`combine-layers` (GeoJSON) Combine multiple output layers into a single GeoJSON file.
|
|
456
|
-
|
|
457
|
-
`geojson-type=` (GeoJSON) Overrides the default output type. Possible values: "FeatureCollection", "GeometryCollection", "Feature" (for a single feature).
|
|
458
|
-
|
|
459
|
-
`no-null-props` (GeoJSON) use `"properties": {}` instead of `"properties": null` when outputting a Feature with no attribute data.
|
|
460
|
-
|
|
461
|
-
`hoist=` (GeoJSON) Move one or more properties to the root level of each Feature. Hoisting a field named "id" creates an id for each Feature. This option can also be used to create non-standard Feature attributes (as used by the tippecanoe program).
|
|
462
|
-
|
|
463
|
-
`width=` (SVG/TopoJSON) Set the width of the output dataset in pixels. When used with TopoJSON output, this option switches the output coordinates from geographic units to pixels and flips the Y axis. SVG output is always in pixels (default SVG width is 800).
|
|
464
|
-
|
|
465
|
-
`height=` (SVG/TopoJSON) Similar to the `width` option. If both `height` and `width` are set, content is centered inside the `[0, 0, width, height]` bounding box.
|
|
466
|
-
|
|
467
|
-
`max-height=` (SVG/TopoJSON) Limit output height (units: pixels).
|
|
468
|
-
|
|
469
|
-
`margin=` (SVG/TopoJSON) Set the margin between coordinate data and the edge of the viewport (default is 1). To assign different margins to each side, pass a list of values in the order `<left,bottom,right,top>` (similar to the `bbox=` option found in other commands).
|
|
470
|
-
|
|
471
|
-
`pixels=` (SVG/TopoJSON) Output area in pixels (alternative to width=).
|
|
472
|
-
|
|
473
|
-
`id-prefix=` Prefix for namespacing layer and feature ids.
|
|
474
|
-
|
|
475
|
-
`svg-data=` (SVG) Export a comma-seperated list of data fields as SVG data-* attributes. Attribute names should match the following regex pattern: `/^[a-z_][a-z0-9_-]*$/`. Non-conforming fields are skipped.
|
|
476
|
-
|
|
477
|
-
`svg-scale=` (SVG) Scale SVG output using geographical units per pixel (an alternative to the `width=` option).
|
|
478
|
-
|
|
479
|
-
`svg-bbox=<xmin,ymin,xmax,ymax>` (SVG) Bounding box of SVG map in projected map units. By default, the extent of SVG output fits the content; this option lets you provide a custom extent. This could be useful when aligning the SVG output with other content layers, such as images or videos.
|
|
480
|
-
|
|
481
|
-
`fit-extent=<layer id>` (SVG) Use a layer (typically a layer containing a single rectangle) to set the extent of the map. Paths that overflow this extent are retained in the SVG output.
|
|
482
|
-
|
|
483
|
-
`point-symbol=square` (SVG) Use squares instead of circles to symbolize point data.
|
|
484
|
-
|
|
485
|
-
`delimiter=` (CSV) Set the field delimiter for CSV/delimited text output; e.g. `delimiter=|`.
|
|
486
|
-
|
|
487
|
-
`decimal-comma` (CSV) Export numbers with decimal commas instead of decimal points (common in Europe and elsewhere).
|
|
488
|
-
|
|
489
|
-
`show-all` [Snapshot] All layers of the exported snapshot will be displayed when opened in the web UI.
|
|
490
|
-
|
|
491
|
-
**Example**
|
|
492
|
-
```bash
|
|
493
|
-
# Convert all the Shapefiles in one directory into GeoJSON
|
|
494
|
-
# files in a different directory.
|
|
495
|
-
mapshaper -i shapefiles/*.shp batch-mode -o geojson/ format=geojson
|
|
496
|
-
```
|
|
497
|
-
|
|
498
|
-
## Editing Commands
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
### -affine
|
|
502
|
-
|
|
503
|
-
Transform coordinates by shifting, scaling and rotating. Not recommended for unprojected datasets.
|
|
504
|
-
|
|
505
|
-
`shift=` X,Y shift in source units (e.g. 5000,-5000)
|
|
506
|
-
|
|
507
|
-
`scale=` Scale (default is 1)
|
|
508
|
-
|
|
509
|
-
`rotate=` Angle of rotation in degrees (default is 0)
|
|
510
|
-
|
|
511
|
-
`anchor=` Center of rotation/scaling (default is center of the bounding box of the selected content)
|
|
512
|
-
|
|
513
|
-
`where=` Use a JS expression to select a subset of features.
|
|
514
|
-
|
|
515
|
-
Common options: `target=`
|
|
516
|
-
|
|
517
|
-
### -classify
|
|
518
|
-
|
|
519
|
-
Assign colors or data values to each feature using one of several classification methods. Methods for sequential data include `quantile`, `equal-interval`, `hybrid` and `nice` or categorical classification to a data field.
|
|
520
|
-
|
|
521
|
-
`<field>` or `field=` Name of the data field to classify.
|
|
522
|
-
|
|
523
|
-
`save-as=` Name of a (new or existing) field to receive the output of classification. The default output field for colors is `fill` or `stroke` (depending on geometry type) and `class` for non-color output.
|
|
524
|
-
|
|
525
|
-
`values=` List of values to assign to data classes. If the number of values differs from the number of classes given by the (optional) `classes` or `breaks` option, then interpolated values will be calculated. Mapshaper uses d3 for interpolation.
|
|
526
|
-
|
|
527
|
-
`colors=` Takes a list of CSS colors, the name of a predefined color scheme, or `random`. Run the [-colors](#-colors) command to list all of the built-in color schemes. Similar to the `values=` option, if the number of listed colors is different from the number of requested classes, interpolated colors are calculated.
|
|
528
|
-
|
|
529
|
-
`non-adjacent` Assign colors to a polygon layer in a randomish pattern, trying not to assign the same color to adjacent polygons. Mapshaper's algorithm balances performance and quality. Usually it can find a solution with four or five colors. If mapshaper is unable to avoid giving the same color to neighboring polygons, it will print a warning. You can resolve the problem by increasing the number of colors.
|
|
530
|
-
|
|
531
|
-
`stops=` A pair of comma-separated numbers (0-100) for limiting the output range of a color ramp.
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
`null-value=` Value (or color) to use for invalid or missing data.
|
|
535
|
-
|
|
536
|
-
`classes=` Number of data classes. This number can also be inferred from the `breaks=` or `values=` options.
|
|
537
|
-
|
|
538
|
-
`breaks=` Specify user-defined sequential class breaks (an alternative to automatic classification using `quantile`, `equal-interval`, etc.).
|
|
539
|
-
|
|
540
|
-
`outer-breaks=` A pair of comma-separated numbers setting min and max breakpoints to use when computing class breaks. This setting overrides the default behavior, which is to use the min and max values of the data field being classified. This setting can be used to prevent extreme data values (outliers) from affecting equal-interval classification. Also useful for setting outside breakpoints for continuous color ramps (when using the `continuous` option).
|
|
541
|
-
|
|
542
|
-
`method=` Classification method. One of: `quantile`, `equal-interval`, `nice`, `hybrid` (sequential data), `categorical`, `non-adjacent` and `indexed`. This parameter is not required if the classification method can be inferred from other options. For example, the `index-field=` parameter implies indexed classification, the `categories=` parameter implies categorical classification.
|
|
543
|
-
|
|
544
|
-
`quantile` Use quantile classification. Shortcut for `method=quantile`.
|
|
545
|
-
|
|
546
|
-
`equal-interval` Use equal interval classification. Shortcut for `method=equal-interval`.
|
|
547
|
-
|
|
548
|
-
`nice` Same as `method=nice`. This scheme finds equally spaced, round breakpoints that roughly divide the dataset into equal parts (similar to quantile classification).
|
|
549
|
-
|
|
550
|
-
`invert` Reverse the order of colors/values.
|
|
551
|
-
|
|
552
|
-
`continuous` Output continuously interpolated values (experimental). Uses linear interpolation between class breaks, which may give poor results with some distributions of data. This option is for creating unclassed/continuous-color maps.
|
|
553
|
-
|
|
554
|
-
`index-field=` Use class ids that have been precalculated and assigned to this field. Values should be integers from `0 ... n-1` (where n is the number of classes). `-1` is the null value.
|
|
555
|
-
|
|
556
|
-
`precision=` Round data values before classification (e.g. `precision=0.1`).
|
|
557
|
-
|
|
558
|
-
`categories=` List of values in the source data field. Using this option triggers categorical classification.
|
|
559
|
-
|
|
560
|
-
`other=` Default value for categorical classification. This value is used when the value of the source data field is not present in the list of values given by `categories=`. Defaults to `null-value=` or null.
|
|
561
|
-
|
|
562
|
-
**Options for generating SVG keys**
|
|
563
|
-
|
|
564
|
-
`key-style=` One of: simple, gradient, dataviz
|
|
565
|
-
|
|
566
|
-
`key-name= ` Name of output SVG file
|
|
567
|
-
|
|
568
|
-
`key-width=` Width of key in pixels
|
|
569
|
-
|
|
570
|
-
`key-font-size=` Font size of tic labels in pixels
|
|
571
|
-
|
|
572
|
-
`key-tile-height=` Height of color tiles in pixels
|
|
573
|
-
|
|
574
|
-
`key-tic-length=` Length of tic mark in pixels
|
|
575
|
-
|
|
576
|
-
`key-label-suffix=` String to append to each label
|
|
577
|
-
|
|
578
|
-
`key-last-suffix=` String to append to the last label
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
**Examples**
|
|
582
|
-
|
|
583
|
-
```bash
|
|
584
|
-
# Apply a sequential color ramp to a polygon dataset using quantiles.
|
|
585
|
-
mapshaper covid_cases.geojson \
|
|
586
|
-
-classify save-as=fill quantile color-scheme=Oranges classes=6 \
|
|
587
|
-
-o out.geojson
|
|
588
|
-
```
|
|
589
|
-
|
|
590
|
-
### -clean
|
|
591
|
-
|
|
592
|
-
This command attempts to repair various kinds of abnormal geometry that might cause problems when running other mapshaper commands or when using other software.
|
|
593
|
-
|
|
594
|
-
Features with null geometries are deleted, unless the `allow-empty` flag is used.
|
|
595
|
-
|
|
596
|
-
Polygon features are cleaned by removing overlaps and filling small gaps between adjacent polygons. Only gaps that are completely enclosed can be filled. Areas that are contained by more than one polygon (overlaps) are assigned to the polygon with the largest area. Similarly, gaps are assigned to the largest-area polygon. This rule may give undesired results and will likely change in the future.
|
|
597
|
-
|
|
598
|
-
Line features are cleaned by removing self-intersections within the same path. Self-intersecting paths are split at the point of intersection and converted into multiple paths within the same feature. When two separate paths intersect in-between segment endpoints, new vertices are inserted at the point of intersection.
|
|
599
|
-
|
|
600
|
-
Point features are cleaned by removing duplicate coordinates within the same feature.
|
|
601
|
-
|
|
602
|
-
`gap-fill-area=` (polygons) Gaps smaller than this area will be filled; larger gaps will be retained as holes in the polygon mosaic. Example values: 2km2 500m2 0. Defaults to a dynamic value calculated from the geometry of the dataset.
|
|
603
|
-
|
|
604
|
-
`sliver-control=` (polygons) Preferentially remove slivers (polygons with a high perimeter-area ratio). Accepts values from 0-1, default is 1. Implementation: multiplies the area of gap areas by the "Polsby Popper" compactness metric before applying area threshold.
|
|
605
|
-
|
|
606
|
-
`overlap-rule=` (polygons) Assign overlapping polygon areas to one of the overlapping features based on this rule. Possible options are: min-id, max-id, min-area, max-area (default is max-area).
|
|
607
|
-
|
|
608
|
-
`allow-overlaps` Allow features to overlap each other. The default behavior is to remove overlaps.
|
|
609
|
-
|
|
610
|
-
`snap-interval=` Snap vertices within a given threshold before performing other kinds of geometry repair. Defaults to a very small threshold. Uses source units.
|
|
611
|
-
|
|
612
|
-
`rewind` Fix errors in the winding order of polygon rings.
|
|
613
|
-
|
|
614
|
-
`allow-empty` Allow null geometries, which are removed by default.
|
|
615
|
-
|
|
616
|
-
Common options: `target=`
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
### -clip
|
|
620
|
-
|
|
621
|
-
Remove features or portions of features that fall outside a clipping area.
|
|
622
|
-
|
|
623
|
-
`<source>` or `source=` Clip to a set of polygon features. Takes the filename or layer id of the clip polygons.
|
|
624
|
-
|
|
625
|
-
`bbox=<xmin,ymin,xmax,ymax>` Delete features or portions of features that fall outside a bounding box.
|
|
626
|
-
|
|
627
|
-
`bbox2=` Faster bounding box clipping than `bbox=` (experimental).
|
|
628
|
-
|
|
629
|
-
`remove-slivers` Remove tiny sliver polygons created by clipping.
|
|
630
|
-
|
|
631
|
-
Common options: [`name=` `+` `target=`](#common-options)
|
|
632
|
-
|
|
633
|
-
```bash
|
|
634
|
-
# Example: Clip a polygon layer using another polygon layer.
|
|
635
|
-
mapshaper usa_counties.shp -clip land-area.shp -o clipped.shp
|
|
636
|
-
```
|
|
637
|
-
|
|
638
|
-
### -colorizer
|
|
639
|
-
|
|
640
|
-
Define a function for converting data values to colors that can be used in subsequent calls to the `-style` command.
|
|
641
|
-
|
|
642
|
-
`name=` Name of the colorizer function.
|
|
643
|
-
|
|
644
|
-
`colors=` List of CSS colors.
|
|
645
|
-
|
|
646
|
-
`random` Randomly assign colors. Uses `colors=` list if given.
|
|
647
|
-
|
|
648
|
-
`breaks=` Ascending-order list of breaks (thresholds) for creating a sequential color scheme.
|
|
649
|
-
|
|
650
|
-
`categories=` List of data values (keys) for creating a categorical color scheme.
|
|
651
|
-
|
|
652
|
-
`other=` Default color for categorical scheme (defaults to `nodata` color).
|
|
653
|
-
|
|
654
|
-
`nodata=` Color to use for invalid or missing data (default is white).
|
|
655
|
-
|
|
656
|
-
`precision=` Rounding precision to apply to numerical data before converting to a color (e.g. 0.1).
|
|
657
|
-
|
|
658
|
-
```bash
|
|
659
|
-
# Example: define a function for a sequential color scheme
|
|
660
|
-
# and assign colors based on data values
|
|
661
|
-
mapshaper data.json \
|
|
662
|
-
-colorizer name=getColor \
|
|
663
|
-
colors='#f0f9e8,#bae4bc,#7bccc4,#2b8cbe' breaks=25,50,75 \
|
|
664
|
-
-each 'color = getColor(PCT)' \
|
|
665
|
-
-o output.json
|
|
666
|
-
|
|
667
|
-
# Example: define a function for a categorical color scheme
|
|
668
|
-
# and use it to assign fill colors
|
|
669
|
-
mapshaper data.json \
|
|
670
|
-
-colorizer name=calcFill colors='red,blue,green' \
|
|
671
|
-
categories='Republican,Democrat,Other' \
|
|
672
|
-
-style fill='calcFill(PARTY)' \
|
|
673
|
-
-o output.svg
|
|
674
|
-
```
|
|
675
|
-
|
|
676
|
-
### -dashlines
|
|
677
|
-
|
|
678
|
-
Split lines into sections, with or without a gap.
|
|
679
|
-
|
|
680
|
-
`dash-length=` Length of split-apart lines (e.g. 200km)
|
|
681
|
-
`gap-length=` Length of gaps between dashes (default is 0)
|
|
682
|
-
`scaled` Scale dashes and gaps to prevent partial dashes
|
|
683
|
-
`planar` Use planar geometry
|
|
684
|
-
`where=` Use a JS expression to select a subset of features.
|
|
685
|
-
|
|
686
|
-
### -dissolve
|
|
687
|
-
|
|
688
|
-
Aggregate groups of features using a data field, or aggregate all features if no field is given. For polygon layers, `-dissolve` merges adjacent polygons by erasing shared boundaries. For point layers, `-dissolve` replaces a group of points with their centroid. For polyline layers, `-dissolve` tries to merge contiguous polylines into as few polylines as possible.
|
|
689
|
-
|
|
690
|
-
For polygon layers, `-dissolve` repairs topology before dissolving, so it produces correct results on inputs that contain overlaps, gaps or other topology errors. The `no-repair` option skips this step for a faster (but less robust) dissolve.
|
|
691
|
-
|
|
692
|
-
`<fields>` or `fields=` (optional) Name of a data field or fields to dissolve on. Accepts a comma-separated list of field names.
|
|
693
|
-
|
|
694
|
-
`group-points` [points] Group the points from each dissolved group of features into a multi-point feature instead of converting multiple points into a single-point centroid feature.
|
|
695
|
-
|
|
696
|
-
`weight=` [points] Name of a field or a JS expression for generating weighted centroids. For example, the following command estimates the "center of mass" of the U.S. population: ` mapshaper census_tracts.shp -points -dissolve weight=POPULATION -o out.shp`
|
|
697
|
-
|
|
698
|
-
`planar` [points] Treat decimal degree coordinates as planar cartesian coordinates when calculating dissolve centroids. (By default, mapshaper calculates the centroids of lat-long point data in 3D space.)
|
|
699
|
-
|
|
700
|
-
`gap-fill-area=` [polygons] Gaps smaller than this area will be filled; larger gaps will be retained as holes in the polygon mosaic. Example values: 2km2 500m2 0. Defaults to a dynamic value calculated from the geometry of the dataset.
|
|
701
|
-
|
|
702
|
-
`sliver-control=` [polygons] Preferentially remove slivers (polygons with a high perimeter-area ratio). Accepts values from 0-1, default is 1. Implementation: multiplies the area of gap areas by the "Polsby Popper" compactness metric before applying area threshold.
|
|
703
|
-
|
|
704
|
-
`allow-overlaps` [polygons] Allow dissolved groups of features to overlap each other. The default behavior is to remove overlaps.
|
|
705
|
-
|
|
706
|
-
`no-repair` [polygons] Skip topology repair before dissolving. Use when the input is known to be clean and you want a faster dissolve. Mapshaper checks for segment intersections and prints a warning if the assumption appears to be wrong, but it still performs the dissolve. Incompatible with `gap-fill-area=`, `sliver-control=` and `allow-overlaps`.
|
|
707
|
-
|
|
708
|
-
`calc=` Use built-in JavaScript functions to create data fields in the dissolved layer. See example below; see [-calc](#-calc) for a list of supported functions.
|
|
709
|
-
|
|
710
|
-
`sum-fields=` Fields to sum when dissolving (comma-sep. list).
|
|
711
|
-
|
|
712
|
-
`copy-fields=` Fields to copy when dissolving (comma-sep. list). Copies values from the first feature in each group of dissolved features.
|
|
713
|
-
|
|
714
|
-
`multipart` Group features from the target layer into multipart features, without otherwise modifying geometry.
|
|
715
|
-
|
|
716
|
-
`where=` Use a JS expression to select a subset of features to dissolve.
|
|
717
|
-
|
|
718
|
-
Common options: `name=` `+` `target=`
|
|
719
|
-
|
|
720
|
-
```bash
|
|
721
|
-
# Example: Aggregate county polygons to states
|
|
722
|
-
mapshaper counties.shp -dissolve STATE -o states.shp
|
|
723
|
-
|
|
724
|
-
# Example: Use the calc= option to count the number of dissolved features
|
|
725
|
-
# and perform other calculations
|
|
726
|
-
mapshaper counties.shp \
|
|
727
|
-
-dissolve STATE calc='n = count(),
|
|
728
|
-
total_pop = sum(POP),
|
|
729
|
-
max_pop = max(POP),
|
|
730
|
-
min_pop = min(POP)'
|
|
731
|
-
```
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
### -dissolve2
|
|
735
|
-
|
|
736
|
-
Deprecated alias for [`-dissolve`](#-dissolve). The topology-repairing behavior of `-dissolve2` has been promoted to be the default behavior of `-dissolve`. Existing scripts that use `-dissolve2` will continue to work but print a deprecation notice.
|
|
737
|
-
|
|
738
|
-
### -divide
|
|
739
|
-
|
|
740
|
-
Divide a polyline layer by a polygon layer. Line features that cross polygon boundaries are divided into separate features. Data fields from the polygon layer are copied to the line layer, as in the `-join` command.
|
|
741
|
-
|
|
742
|
-
`<file|layer>` or `source=` File or layer containing polygon features.
|
|
743
|
-
|
|
744
|
-
`fields=` A comma-separated list of fields to copy from the polygon layer (see `-join` command).
|
|
745
|
-
|
|
746
|
-
`calc=` Use JS assignments and built-in functions to convert values from the polygon layer to (new) fields the target table (see `-join` command).
|
|
747
|
-
|
|
748
|
-
Other options: `target=`
|
|
749
|
-
|
|
750
|
-
### -dots
|
|
751
|
-
|
|
752
|
-
Fill polygons with random points, for making dot density maps. This command should be applied to projected layers.
|
|
753
|
-
|
|
754
|
-
`<fields>` or `fields=` List of one or more data fields containing data for the number of dots to place in each polygon.
|
|
755
|
-
|
|
756
|
-
`colors=` List of dot colors (one color for each field in the `fields=` parameter). Dots of different colors are placed in random sequence, so dots of one color do not consistently cover up dots of other colors in the densest areas.
|
|
757
|
-
|
|
758
|
-
`values=` List of values to assign to dots (alternative to `colors=`).
|
|
759
|
-
|
|
760
|
-
`save-as=` Name of a (new or existing) field to receive the assigned colors or values. (By default, colors are assigned to the `fill` field.)
|
|
761
|
-
|
|
762
|
-
`r=` Dot radius in pixels.
|
|
763
|
-
|
|
764
|
-
`evenness=` A value from 0-1. 0 corresponds to purely random placement, 1 maintains (fairly) even spacing between the dots within each polygon. The default is 1.
|
|
765
|
-
|
|
766
|
-
`per-dot=` A number for scaling data values. For example, use `per-dot=100` to make a map that displays one dot per 100 people (or whatever entity is being visualized).
|
|
767
|
-
|
|
768
|
-
`copy-fields=` List of fields to copy from the original polygon layer to each dot feature.
|
|
769
|
-
|
|
770
|
-
`multipart` Combine groups of same-color dots into multi-part features.
|
|
771
|
-
|
|
772
|
-
Other options: `name=` `+` `target=`
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
### -drop
|
|
776
|
-
|
|
777
|
-
Delete the target layer(s) or elements within the target layer(s).
|
|
778
|
-
|
|
779
|
-
`fields=` Delete a (comma-separated) list of attribute data fields. To delete all fields, use `fields=*`.
|
|
780
|
-
|
|
781
|
-
`geometry` Delete all geometry.
|
|
782
|
-
|
|
783
|
-
`holes` Delete any holes from a polygon layer.
|
|
784
|
-
|
|
785
|
-
`target=` Layer(s) to target.
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
### -each
|
|
789
|
-
|
|
790
|
-
Apply a JavaScript expression to each feature in a layer. Data properties are available as local variables; the feature's geometry-derived properties are available on the `this` object (e.g. `this.area`, `this.centroidX`, `this.bbox`).
|
|
791
|
-
|
|
792
|
-
**Tip:** Enclose JS expressions in single quotes when using the bash shell (Mac and Linux) to avoid shell expansion of `!` and other special characters. Using the Windows command interpreter, enclose JS expressions in double quotes.
|
|
793
|
-
|
|
794
|
-
`<expression>` or `expression=` JavaScript expression to apply to each feature.
|
|
795
|
-
|
|
796
|
-
`where=` Secondary boolean JS expression for targetting a subset of features.
|
|
797
|
-
|
|
798
|
-
`target=` Layer to target.
|
|
799
|
-
|
|
800
|
-
The same expression syntax and execution context are used by all commands that support expressions. See [JavaScript expressions](/docs/guides/expressions.html.md) for the full reference. The [Basics](/docs/examples/basics.html.md) page has practical recipes that put expressions to work.
|
|
801
|
-
|
|
802
|
-
**Examples**
|
|
803
|
-
|
|
804
|
-
```bash
|
|
805
|
-
# Create two fields
|
|
806
|
-
mapshaper counties.shp \
|
|
807
|
-
-each 'STATE_FIPS=COUNTY_FIPS.substr(0, 2), AREA=this.area' \
|
|
808
|
-
-o out.shp
|
|
809
|
-
|
|
810
|
-
# Delete two fields
|
|
811
|
-
mapshaper states.shp -each 'delete STATE_NAME, delete GEOID' -o out.shp
|
|
812
|
-
|
|
813
|
-
# Rename a field
|
|
814
|
-
mapshaper states.shp -each 'STATE_NAME=NAME, delete NAME' -o out.shp
|
|
815
|
-
|
|
816
|
-
# Print the value of a field to the console
|
|
817
|
-
mapshaper states.shp -each 'console.log(NAME)'
|
|
818
|
-
|
|
819
|
-
# Assign a new data record to each feature
|
|
820
|
-
mapshaper states.shp -each 'this.properties = {FID: this.id}' -o out.shp
|
|
821
|
-
```
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
### -erase
|
|
825
|
-
|
|
826
|
-
Remove features or portions of features that fall inside an area.
|
|
827
|
-
|
|
828
|
-
`<source>` or `source=` File or layer containing erase polygons. Takes the filename or layer id of the erase polygons.
|
|
829
|
-
|
|
830
|
-
`bbox=<xmin,ymin,xmax,ymax>` Delete features or portions of features that fall inside a bounding box. Similar to `-clip bbox=`.
|
|
831
|
-
|
|
832
|
-
`bbox2=` Faster bounding box erasing than `bbox=` (experimental).
|
|
833
|
-
|
|
834
|
-
`remove-slivers` Remove tiny sliver polygons created by erasing.
|
|
835
|
-
|
|
836
|
-
Common options: [`name=` `+` `target=`](#common-options)
|
|
837
|
-
|
|
838
|
-
```bash
|
|
839
|
-
# Example: Erase a polygon layer using another polygon layer.
|
|
840
|
-
mapshaper usa_counties.shp -erase lakes.shp -o out.shp
|
|
841
|
-
```
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
### -explode
|
|
845
|
-
|
|
846
|
-
Divide each multi-part feature into several single-part features.
|
|
847
|
-
|
|
848
|
-
Common options: `target=`
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
### -filter
|
|
852
|
-
|
|
853
|
-
Apply a boolean JavaScript expression to each feature, removing features that evaluate to false.
|
|
854
|
-
|
|
855
|
-
`<expression>` or `expression=` JS expression evaluating to `true` or `false`. Uses the same execution context as [`-each`](#-each).
|
|
856
|
-
|
|
857
|
-
`bbox=` Retains features that intersect the given bounding box (xmin,ymin,xmax,ymax).
|
|
858
|
-
|
|
859
|
-
`invert` Invert the filter -- retain only those features that would have been deleted.
|
|
860
|
-
|
|
861
|
-
`remove-empty` Delete features with null geometry. May be used by itself or in combination with an `<expression>`.
|
|
862
|
-
|
|
863
|
-
Common options: [`name=` `+` `target=` ](#common-options)
|
|
864
|
-
|
|
865
|
-
```bash
|
|
866
|
-
# Example: Select counties from New England states
|
|
867
|
-
mapshaper usa_counties.shp \
|
|
868
|
-
-filter '"ME,VT,NH,MA,CT,RI".indexOf(STATE) > -1' \
|
|
869
|
-
-o ne_counties.shp
|
|
870
|
-
```
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
### -filter-fields
|
|
874
|
-
|
|
875
|
-
Delete fields in an attribute table, by listing the fields to retain. If no files are given, then all attributes are removed.
|
|
876
|
-
|
|
877
|
-
`<fields>` or `fields=` Comma-separated list of data fields to retain.
|
|
878
|
-
|
|
879
|
-
`invert` Invert the filter -- delete the listed fields instead of retaining them.
|
|
880
|
-
|
|
881
|
-
Common options: `target=`
|
|
882
|
-
|
|
883
|
-
```bash
|
|
884
|
-
# Example: Retain two fields
|
|
885
|
-
mapshaper states.shp -filter-fields FID,NAME -o out.shp
|
|
886
|
-
```
|
|
887
|
-
|
|
888
|
-
### -filter-islands
|
|
889
|
-
|
|
890
|
-
Remove small detached polygon rings (islands).
|
|
891
|
-
|
|
892
|
-
`min-area=` Remove small-area islands using an area threshold (e.g. 10km2).
|
|
893
|
-
|
|
894
|
-
`min-vertices=` Remove low-vertex-count islands.
|
|
895
|
-
|
|
896
|
-
`remove-empty` Delete features with null geometry.
|
|
897
|
-
|
|
898
|
-
[`target=`](#common-options)
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
### -filter-slivers
|
|
902
|
-
|
|
903
|
-
Remove small polygon rings.
|
|
904
|
-
|
|
905
|
-
`min-area=` Area threshold for removal (e.g. 10km2).
|
|
906
|
-
|
|
907
|
-
`sliver-control=` (polygons) Preferentially remove slivers (polygons with a high perimeter-area ratio). Accepts values from 0-1, default is 1. Implementation: multiplies the area of polygon rings by the "Polsby Popper" compactness metric before applying area threshold.
|
|
908
|
-
|
|
909
|
-
`remove-empty` Delete features with null geometry.
|
|
910
|
-
|
|
911
|
-
[`target=`](#common-options)
|
|
912
|
-
|
|
913
|
-
### -frame
|
|
914
|
-
|
|
915
|
-
Create a rectangular frame layer at a given display width. Frame size is used for scaling symbols and for setting the display size of SVG output. The geographical extent of the frame is based on the `bbox=` option or the bounding box of the target layer or layers, if `bbox=` is omitted.
|
|
916
|
-
|
|
917
|
-
`width=` Width of frame (e.g. 5in, 10cm, 600px; default is 800px)
|
|
918
|
-
|
|
919
|
-
`height=` Height of frame (in addition to or instead of width= option)
|
|
920
|
-
|
|
921
|
-
`aspect-ratio=` Aspect ratio of frame (optional)
|
|
922
|
-
|
|
923
|
-
`bbox=` Bounding coordinates of frame contents in projected map coordinates (xmin,ymin,xmax,ymax). If omitted, the bounding box of the target layer(s) is used.
|
|
924
|
-
|
|
925
|
-
`offset=` Padding around the frame's `bbox` in display units or pct of width/height, e.g. 5cm 20px 5%
|
|
926
|
-
|
|
927
|
-
`offsets=` Comma-sep. list of offsets for each side of the map frame, in l,b,r,t order
|
|
928
|
-
|
|
929
|
-
Other options: `name=` `target=`
|
|
930
|
-
|
|
931
|
-
### -graticule
|
|
932
|
-
|
|
933
|
-
Create a graticule layer appropriate for a world map centered on longitude 0.
|
|
934
|
-
|
|
935
|
-
`polygon` Create an polygon enclosing the entire area of the graticule. Useful for creating background or outline shapes for clipped projections, like Robinson or Stereographic.
|
|
936
|
-
|
|
937
|
-
`interval=` Specify the spacing of graticule lines (in degrees). Common options are: 5, 10, 15, 30, 45. Default is 10.
|
|
938
|
-
|
|
939
|
-
### -grid
|
|
940
|
-
|
|
941
|
-
Create a continuous grid of square or hexagonal polygons.
|
|
942
|
-
|
|
943
|
-
The `-grid` command should have a projected layer as its target. The cells of the grid will completely enclose the bounding box of the target layer.
|
|
944
|
-
|
|
945
|
-
This command is intended for visualizing data in a grid. Typically, you would use the `-join` command to join data from a polygon or point layer to a grid layer. Use `-join interpolate=<fields>` to interpolate data values (typically count data) from the polygon layer to the grid layer based on area. Use `-join calc='<field> = sum(<field>)'` or `-join calc='<field> = count()'` to aggregate point data values.
|
|
946
|
-
|
|
947
|
-
`type=` Supported values: `square` `hex` `hex2`. The `hex` and `hex2` types have different rotations.
|
|
948
|
-
|
|
949
|
-
`interval=` The length of one side of a grid cell. Example values: `500m` `2km`.
|
|
950
|
-
|
|
951
|
-
Other options: `name=` `+` `target=`
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
### -include
|
|
955
|
-
|
|
956
|
-
`<file>` or `file=` Path to the external .js file to load. The file should contain a single JS object. The properties of this object are converted to variables in the JS expression used by the `-each` command.
|
|
957
|
-
|
|
958
|
-
### -inlay
|
|
959
|
-
|
|
960
|
-
Inscribe a polygon layer within another polygon layer.
|
|
961
|
-
|
|
962
|
-
`<source>` or `source=` File or layer containing polygons to inlay
|
|
963
|
-
|
|
964
|
-
Other options: `target=`
|
|
965
|
-
|
|
966
|
-
### -innerlines
|
|
967
|
-
|
|
968
|
-
Create a polyline layer consisting of shared boundaries with no attribute data.
|
|
969
|
-
|
|
970
|
-
`where=` Filter lines using a JS expression (see the `-lines where=` option).
|
|
971
|
-
|
|
972
|
-
Other options: `name=` `+` `target=`
|
|
973
|
-
|
|
974
|
-
```bash
|
|
975
|
-
# Example: Extract the boundary between two states.
|
|
976
|
-
mapshaper states.shp -filter 'STATE=="OR" || STATE=="WA"' -innerlines -o out.shp
|
|
977
|
-
```
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
### -join
|
|
981
|
-
|
|
982
|
-
Join attribute data from a source layer or file to a target layer. If the `keys=` option is used, Mapshaper will join records by matching the values of key fields. If the `keys=` option is missing, Mapshaper will perform a polygon-to-polygon, point-to-polygon, polygon-to-point or point-to-point spatial join.
|
|
983
|
-
|
|
984
|
-
`<file|layer>` or `source=` File or layer containing data records to join.
|
|
985
|
-
|
|
986
|
-
`keys=` Names of two fields to use as join keys, separated by a comma. The key field from the destination table is followed by the key field from the source table. If the `keys=` option is missing, mapshaper performs a spatial join.
|
|
987
|
-
|
|
988
|
-
`calc=` Use JS assignments and built-in functions to convert values from the source table to (new) fields the target table. See the [`-calc` command reference](#-calc) for a list of supported functions. Useful for handling many-to-one joins. See example below.
|
|
989
|
-
|
|
990
|
-
`where=` Use a boolean JS expression to filter records from the source table. The expression has the same syntax as the expression used by the `-filter` command. The functions `isMax(<field>)` `isMin(<field>)` and `isMode(<field>)` can be used in many-to-one joins to select among source records.
|
|
991
|
-
|
|
992
|
-
`fields=` A comma-separated list of fields to copy from the external table. If the `fields` option and `calc` options are both absent, all fields are copied except the key field (if joining on keys) unless the. Use `fields=*` to copy all fields, including any key field. Use `fields=` (empty list) to copy no fields.
|
|
993
|
-
|
|
994
|
-
`prefix=` Add a prefix to the names of fields joined from the external attribute table.
|
|
995
|
-
|
|
996
|
-
`interpolate=` (polygon-to-polygon joins only) A list of fields to interpolate/reaggregate based on area of overlap. Interpolates fields containing count data, such as population counts or vote counts. Treats data as being uniformly distributed within polygon areas. Also interpolates string fields containing categorical data. The value associated with the largest area of overlap between source and target polygons gets copied to the target feature.
|
|
997
|
-
|
|
998
|
-
`point-method` (polygon-to-polygon joins only) Use an alternate method for joining two polygon layers. The default polygon-polygon join method detects areas of overlap between two polygon layers by compositing the two layers internally. This method is simpler -- it generates a temporary point layer from the source layer with the greater number of features (using the same inner-point method as the `-points inner` command), and then performs a point-to-polygon or polygon-to-point join. This method does not support the `interpolate=` option.
|
|
999
|
-
|
|
1000
|
-
`largest-overlap` (polygon-to-polygon joins only) selects a single polygon to join when multiple source polygons overlap a target polygon, based on largest area of overlap.
|
|
1001
|
-
|
|
1002
|
-
`min-overlap-pct=` (polygon-to-polygon joins only) Only source features with at least this percentage overlap of the target feature (by area) get joined.
|
|
1003
|
-
|
|
1004
|
-
`min-overlap-area=` (polygon-to-polygon joins only) Only source features with at least this much areal overlap of the target feature get joined.
|
|
1005
|
-
|
|
1006
|
-
`max-distance=` (point-to-point joins only) Join source layer points within this distance of a target layer point.
|
|
1007
|
-
|
|
1008
|
-
`duplication` Create duplicate features in the target layer on many-to-one joins.
|
|
1009
|
-
|
|
1010
|
-
`sum-fields=` (deprecated) A comma-separated list of fields to sum when several source records match the same target record. This option is equivalent to using the `sum()` function inside a `calc=` expression like this: `calc='FIELD = sum(FIELD)'`.
|
|
1011
|
-
|
|
1012
|
-
`string-fields=` A comma-separated list of fields in source CSV file to import as strings (e.g. FIPS,ZIPCODE).
|
|
1013
|
-
|
|
1014
|
-
`field-types=` A comma-separated list of type hints (when joining a CSV file or other delimited text file). See `-i field-types=` above.
|
|
1015
|
-
|
|
1016
|
-
`force` Allow values in the target data table to be overwritten by values in the source table when both tables contain identically named fields.
|
|
1017
|
-
|
|
1018
|
-
`unjoined` Copy unjoined records from the source table to a layer named "unjoined".
|
|
1019
|
-
|
|
1020
|
-
`unmatched` Copy unmatched records from the destination table to a layer named "unmatched".
|
|
1021
|
-
|
|
1022
|
-
Other options: `encoding=` `target=`
|
|
1023
|
-
|
|
1024
|
-
**Examples**
|
|
1025
|
-
|
|
1026
|
-
Join a point layer to a polygon layer (spatial join), using the `calc=` option to handle many-to-one matches.
|
|
1027
|
-
|
|
1028
|
-
```bash
|
|
1029
|
-
mapshaper states.shp \
|
|
1030
|
-
-join points.shp calc='median_score = median(SCORE),
|
|
1031
|
-
mean_score = average(SCORE),
|
|
1032
|
-
join_count = count()' \
|
|
1033
|
-
-o out.shp
|
|
1034
|
-
```
|
|
1035
|
-
|
|
1036
|
-
Copy data from a csv file to the attribute table of a Shapefile by matching values from the *STATE_FIPS* field of the Shapefile and the *FIPS* field of the csv file. (The string-fields=FIPS argument prevents FIPS codes in the CSV file from being converted to numbers.)
|
|
1037
|
-
|
|
1038
|
-
```bash
|
|
1039
|
-
mapshaper states.shp \
|
|
1040
|
-
-join demographics.txt keys=STATE_FIPS,FIPS string-fields=FIPS \
|
|
1041
|
-
-o out.shp
|
|
1042
|
-
```
|
|
1043
|
-
|
|
1044
|
-
### -lines
|
|
1045
|
-
|
|
1046
|
-
Converts points and polygons to lines. Polygons are converted to topological boundaries. Without the `<fields>` argument, external (unshared) polygon boundaries are attributed as `TYPE: "outer", RANK: 0` and internal (shared) boundaries are `TYPE: "inner", RANK: 1`.
|
|
1047
|
-
|
|
1048
|
-
`<fields>` or `fields=` (Optional) comma-separated list of attribute fields for creating a hierarchy of polygon boundaries. A single field name adds an intermediate level of hierarchy with attributes: `TYPE: <field name>, RANK: 1`, and the lowest-level internal boundaries are given attributes `TYPE: "outer", RANK: 2`. A comma-separated list of fields adds additional levels of hierarchy.
|
|
1049
|
-
|
|
1050
|
-
`where=` Use a JS expression for filtering polygon boundaries using properties of adjacent polygons. The expression context has objects named A and B, which represent features on eather side of a path. B is null if a path only belongs to a single feature.
|
|
1051
|
-
|
|
1052
|
-
`each=` Apply a JS expression to each line (using A and B, like the `where=` option).
|
|
1053
|
-
|
|
1054
|
-
`groupby=` Convert a point layer into multiple lines, using a field value for grouping.
|
|
1055
|
-
|
|
1056
|
-
Common options: `name=` `+` `target=`
|
|
1057
|
-
|
|
1058
|
-
```bash
|
|
1059
|
-
# Example: Classify national, state and county boundaries.
|
|
1060
|
-
mapshaper counties.shp -lines STATE_FIPS -o boundaries.shp
|
|
1061
|
-
```
|
|
1062
|
-
|
|
1063
|
-
```bash
|
|
1064
|
-
# Example: add the names of neighboring countries to each section of border
|
|
1065
|
-
mapshaper countries.geojson \
|
|
1066
|
-
-lines each='COUNTRIES = A.NAME + (B ? "," + B.NAME : "")' \
|
|
1067
|
-
-o borders.geojson
|
|
1068
|
-
```
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
### -merge-layers
|
|
1072
|
-
|
|
1073
|
-
Merge features from several layers into a single layer. Layers can only be merged if they have compatible geometry types. Target layers should also have compatible data fields, unless the `force` option is used.
|
|
1074
|
-
|
|
1075
|
-
`force` Allow merging layers with inconsistent fields. When a layer is missing a particular field, the field will be added, with the values set to `undefined`. Using this option, you are still prevented from merging fields with different data types (e.g. a field containing numbers in one layer and strings in another). You are also still prevented from merging layers containing different geometry types.
|
|
1076
|
-
|
|
1077
|
-
`flatten` (polygon layers) Remove polygon overlaps by assigning overlapping areas to the last overlapping polygon (the topmost feature if features are rendered in sequence).
|
|
1078
|
-
|
|
1079
|
-
Common options: `name=` `target=`
|
|
1080
|
-
|
|
1081
|
-
```bash
|
|
1082
|
-
# Example: Combine features from several Shapefiles into a single Shapefile.
|
|
1083
|
-
# -i combine-files is used because files are processed separately by default.
|
|
1084
|
-
mapshaper -i OR.shp WA.shp CA.shp AK.shp combine-files \
|
|
1085
|
-
-merge-layers \
|
|
1086
|
-
-o pacific_states.shp
|
|
1087
|
-
```
|
|
1088
|
-
|
|
1089
|
-
### -mosaic
|
|
1090
|
-
|
|
1091
|
-
Flatten a polygon layer by converting overlapping areas to separate polygons.
|
|
1092
|
-
|
|
1093
|
-
`calc=` Use a JavaScript expression to handle many-to-one aggregation (similar to the `calc=` option of the`-join` and `-dissolve` functions). See [-calc](#-calc) for a list of supported functions.
|
|
1094
|
-
|
|
1095
|
-
Common options: `name=` `+` `target=`
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
### -point-grid
|
|
1099
|
-
|
|
1100
|
-
Create a rectangular grid of points.
|
|
1101
|
-
|
|
1102
|
-
`<cols,rows>` Size of the grid, e.g. `-point-grid 100,100`.
|
|
1103
|
-
|
|
1104
|
-
`interval=` Distance between adjacent points, in source units (alternative to setting the number of cols and rows).
|
|
1105
|
-
|
|
1106
|
-
`bbox=` Fit the grid to a bounding box (xmin,ymin,xmax,ymax). Defaults to the bounding box of the other data layers, or of the world if no other layers are present.
|
|
1107
|
-
|
|
1108
|
-
`name=` Set the name of the point grid layer
|
|
1109
|
-
|
|
1110
|
-
### -points
|
|
1111
|
-
|
|
1112
|
-
Create a point layer, either from polygon or polyline geometry or from values in the attribute table. By default, polygon features are replaced by a single point located at the centroid of the polygon ring, or the largest ring of a multipart polygon. By default, polyline features are replaced by a single point located at the polyline vertex that is closest to the center of the feature's bounding box (this can be used to join polylines to polygons using a point-to-polygon spatial join).
|
|
1113
|
-
|
|
1114
|
-
`x=` Name of field containing x coordinate values. Common X-coordinate names are auto-detected (e.g. longitude, LON).
|
|
1115
|
-
|
|
1116
|
-
`y=` Name of field containing y coordinate values. Common Y-coordinate names are auto-detected (e.g. latitude, LAT).
|
|
1117
|
-
|
|
1118
|
-
`centroid` Create points at the centroid of the largest ring of each polygon feature. Point placement is currrently not affected by holes.
|
|
1119
|
-
|
|
1120
|
-
`inner` Create points in the interior of the largest ring of each polygon feature. Inner points are located away from polygon boundaries.
|
|
1121
|
-
|
|
1122
|
-
`vertices` Convert polygon and polyline features into point features containing the unique vertices in each shape.
|
|
1123
|
-
|
|
1124
|
-
`vertices2` Convert all the vertices in polygon and polyline features into points, including duplicate coordinates (e.g. the duplicate endpoint coordinates of polygon rings).
|
|
1125
|
-
|
|
1126
|
-
`endpoints` Capture the unique endpoints of polygon and polyline arcs.
|
|
1127
|
-
|
|
1128
|
-
`midpoints` Find the midpoint of each path in a polyline layer.
|
|
1129
|
-
|
|
1130
|
-
`interpolated` Interpolate points along polylines. Requires the `interval=` option to be set. Original vertices are replaced by interpolated vertices.
|
|
1131
|
-
|
|
1132
|
-
`interval=` Distance between interpolated points (in meters if coordinates are unprojected, or projected units).
|
|
1133
|
-
|
|
1134
|
-
Common options: `name=` `+` `target=`
|
|
1135
|
-
|
|
1136
|
-
```bash
|
|
1137
|
-
# Example: Create points in the interior of each polygon
|
|
1138
|
-
mapshaper counties.shp -points inner -o points.shp
|
|
1139
|
-
|
|
1140
|
-
# Example: Create points in the interior of each polygon (alternate method)
|
|
1141
|
-
mapshaper counties.shp \
|
|
1142
|
-
-each 'cx=this.innerX, cy=this.innerY' \
|
|
1143
|
-
-points x=cx y=cy \
|
|
1144
|
-
-o points.shp
|
|
1145
|
-
```
|
|
1146
|
-
|
|
1147
|
-
### -polygons
|
|
1148
|
-
|
|
1149
|
-
Convert a polyline layer to a polygon layer by linking together intersecting polylines to form rings.
|
|
1150
|
-
|
|
1151
|
-
`gap-tolerance=` Close gaps ("undershoots") between polylines up to the distance specified by this option.
|
|
1152
|
-
|
|
1153
|
-
`from-rings` Convert a layer of closed polyline rings into polygons. Nested rings in multipart features are converted into holes.
|
|
1154
|
-
|
|
1155
|
-
Common options: `target=`
|
|
1156
|
-
|
|
1157
|
-
### -proj
|
|
1158
|
-
|
|
1159
|
-
Project a dataset using a PROJ string, EPSG code or alias. This command affects all layers in the dataset(s) containing the targeted layer or layers. Information on PROJ string syntax can be found on the [PROJ website](https://proj.org/usage/index.html).
|
|
1160
|
-
|
|
1161
|
-
`<crs>` or `crs=` Target CRS, given as a Proj.4 definition or an alias. Use the [`-projections`](#-projections) command to list available projections and aliases. In projections which require additional parameters, such as a zone in UTM, you can pass a Proj4 string enclosed in quotes. For example, `crs='+proj=utm +zone=27'`.
|
|
1162
|
-
|
|
1163
|
-
`densify` Interpolate vertices along long line segments as needed to approximate curved lines.
|
|
1164
|
-
|
|
1165
|
-
`match=` Match the projection of the given layer or .prj file.
|
|
1166
|
-
|
|
1167
|
-
`init=` Define the pre-projected coordinate system, if unknown. This option is not needed if the source coordinate system is defined by a .prj file, or if the source CRS is WGS84. As with `crs`, you can pass a Proj4 string enclosed in quotes if the selected projection requires extra parameters, for example `init='+proj=utm +zone=33'`.
|
|
1168
|
-
|
|
1169
|
-
`target=` Layer(s) to target. All layers belonging to the same dataset as a targeted layer will be reprojected. To reproject all datasets, use `target=*`.
|
|
1170
|
-
|
|
1171
|
-
**Examples**
|
|
1172
|
-
```bash
|
|
1173
|
-
# Convert a GeoJSON file to New York Long Island state plane CRS,
|
|
1174
|
-
# using a Proj.4 string
|
|
1175
|
-
mapshaper nyc.json \
|
|
1176
|
-
-proj +proj=lcc \
|
|
1177
|
-
+lat_1=41.03333333333333 +lat_2=40.66666666666666 \
|
|
1178
|
-
+lat_0=40.16666666666666 +lon_0=-74 \
|
|
1179
|
-
+x_0=300000 +y_0=0 \
|
|
1180
|
-
+ellps=GRS80 +datum=NAD83 +units=m \
|
|
1181
|
-
-o out.json
|
|
1182
|
-
|
|
1183
|
-
# Apply the same projection using an EPSG code
|
|
1184
|
-
mapshaper nyc.json -proj EPSG:2831 -o out.json
|
|
1185
|
-
|
|
1186
|
-
# Convert a projected Shapefile to WGS84 coordinates
|
|
1187
|
-
mapshaper area.shp -proj wgs84 -o out.shp
|
|
1188
|
-
|
|
1189
|
-
# Use the Winkel Tripel projection with a custom central meridian
|
|
1190
|
-
mapshaper countries.shp -proj +proj=wintri +lon_0=10 -o out.shp
|
|
1191
|
-
|
|
1192
|
-
# Shortcut notation for the above projection
|
|
1193
|
-
mapshaper countries.shp -proj wintri +lon_0=10 -o out.shp
|
|
1194
|
-
|
|
1195
|
-
# Convert an unprojected U.S. Shapefile into a composite projection with Alaska
|
|
1196
|
-
# and Hawaii repositioned and rescaled to fit in the lower left corner.
|
|
1197
|
-
# Show Puerto Rico and the U.S. Virgin Islands
|
|
1198
|
-
# Override the default central meridian and scale of the Alaska inset
|
|
1199
|
-
mapshaper us_states.shp \
|
|
1200
|
-
-proj albersusa +PR +VI +AK.lon_0=-141 +AK.scale=0.4 \
|
|
1201
|
-
-o out.shp
|
|
1202
|
-
```
|
|
1203
|
-
|
|
1204
|
-
### -rectangle
|
|
1205
|
-
|
|
1206
|
-
Create a new layer containing a rectangular polygon.
|
|
1207
|
-
|
|
1208
|
-
`bbox=<xmin,ymin,xmax,ymax>` Give the coordinates of the rectangle.
|
|
1209
|
-
|
|
1210
|
-
`source=` Create a bounding box around a given layer.
|
|
1211
|
-
|
|
1212
|
-
`aspect-ratio=` Aspect ratio as a number or range (e.g. 2 0.8,1.6 ,2).
|
|
1213
|
-
|
|
1214
|
-
`offset=` Padding as a distance or percentage of width/height (single value or list).
|
|
1215
|
-
|
|
1216
|
-
`name=` Assign a name to the newly created layer.
|
|
1217
|
-
|
|
1218
|
-
### -rectangles
|
|
1219
|
-
|
|
1220
|
-
Create a new layer containing a rectangular polygon for each feature in the layer.
|
|
1221
|
-
|
|
1222
|
-
`aspect-ratio=` Aspect ratio as a number or range (e.g. 2 0.8,1.6 ,2).
|
|
1223
|
-
|
|
1224
|
-
`bbox=` Use an expression to generate rectangle bounds for each feature. The expression should evaluate to a GeoJSON-style bbox array.
|
|
1225
|
-
|
|
1226
|
-
`offset=` Padding as a distance or percentage of width/height (single value or list).
|
|
1227
|
-
|
|
1228
|
-
`name=` Assign a name to the newly created layer.
|
|
1229
|
-
|
|
1230
|
-
### -rename-fields
|
|
1231
|
-
|
|
1232
|
-
Rename data fields. To rename a field from A to B, use the assignment operator: B=A.
|
|
1233
|
-
|
|
1234
|
-
`<fields>` or `fields=` List of fields to rename as a comma-separated list.
|
|
1235
|
-
|
|
1236
|
-
Common options: `target=`
|
|
1237
|
-
|
|
1238
|
-
```bash
|
|
1239
|
-
# Example: rename STATE_FIPS to FIPS and STATE_NAME to NAME
|
|
1240
|
-
mapshaper states.shp -rename-fields FIPS=STATE_FIPS,NAME=STATE_NAME -o out.shp
|
|
1241
|
-
```
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
### -rename-layers
|
|
1245
|
-
|
|
1246
|
-
Assign new names to layers. If fewer names are given than there are layers, the last name in the list is repeated with numbers appended (e.g. layer1, layer2).
|
|
1247
|
-
|
|
1248
|
-
`<names>` or `names=` One or more layer names (comma-separated).
|
|
1249
|
-
|
|
1250
|
-
`target=` Rename a subset of all layers.
|
|
1251
|
-
|
|
1252
|
-
```bash
|
|
1253
|
-
# Example: Create a TopoJSON file with sensible object names.
|
|
1254
|
-
mapshaper ne_50m_rivers_lake_centerlines.shp ne_50m_land.shp combine-files \
|
|
1255
|
-
-rename-layers water,land -o target=* layers.topojson
|
|
1256
|
-
```
|
|
1257
|
-
|
|
1258
|
-
### -require
|
|
1259
|
-
|
|
1260
|
-
Require a Node module or ES module for use in commands like `-each` and `-run`. Modules are added to the expression context. When the `alias=` option is given, modules are accessed via their aliases. Modules that are imported by name (e.g. `-require d3`) are accessed via their name, or by their alias if the `alias=` option is used. Module files without an alias name have their exported functions and data added directly to the expression context.
|
|
1261
|
-
|
|
1262
|
-
`<module>` or `module=` Name of an installed module or path to a module file.
|
|
1263
|
-
|
|
1264
|
-
`alias=` Import the module as a custom-named variable.
|
|
1265
|
-
|
|
1266
|
-
```bash
|
|
1267
|
-
# Example: use the underscore module (which has been installed locally)
|
|
1268
|
-
$ mapshaper data.json \
|
|
1269
|
-
-require underscore alias=_ \
|
|
1270
|
-
-each 'id = _.uniqueId()' \
|
|
1271
|
-
-o data2.json
|
|
1272
|
-
```
|
|
1273
|
-
|
|
1274
|
-
```bash
|
|
1275
|
-
# Example: import a module file containing a user-defined function
|
|
1276
|
-
$ mapshaper data.json \
|
|
1277
|
-
-require scripts/includes.mjs \
|
|
1278
|
-
-each 'displayname = getDisplayName(d)' \
|
|
1279
|
-
-o data2.json
|
|
1280
|
-
```
|
|
1281
|
-
|
|
1282
|
-
### -run
|
|
1283
|
-
|
|
1284
|
-
Run mapshaper commands from a [command file](#command-files) or generated on-the-fly from a JS expression.
|
|
1285
|
-
|
|
1286
|
-
`<file|expression>` Either:
|
|
1287
|
-
- A path to a mapshaper [command file](#command-files).
|
|
1288
|
-
- A JS expression or template containing embedded expressions, for generating one or more mapshaper commands.
|
|
1289
|
-
|
|
1290
|
-
* Embedded expressions are enclosed in curly braces (see below).
|
|
1291
|
-
* Expressions can access `target` and `io` objects.
|
|
1292
|
-
* Expressions can also access functions and data loaded with the `-require` command.
|
|
1293
|
-
* Functions can be async.
|
|
1294
|
-
|
|
1295
|
-
Expression context:
|
|
1296
|
-
|
|
1297
|
-
If command has a single target layer:
|
|
1298
|
-
`target` object provides data and information about the command's target layer
|
|
1299
|
-
- `target.layer_name` Name of layer
|
|
1300
|
-
- `target.geojson` (getter/setter) Returns a GeoJSON FeatureCollection for the layer (getter) or replaces the layer with the contents of a GeoJSON object (setter).
|
|
1301
|
-
- `target.geometry_type` One of: polygon, polyline, point, `undefined`
|
|
1302
|
-
- `target.feature_count` Number of features in the layer
|
|
1303
|
-
- `target.null_shape_count` Number of features with null geometry
|
|
1304
|
-
- `target.null_data_count` Number of features with no attribute data
|
|
1305
|
-
- `target.bbox` GeoJSON-style bounding box
|
|
1306
|
-
- `target.proj4` PROJ-formatted string giving the CRS (coordinate reference system) of the layer
|
|
1307
|
-
|
|
1308
|
-
`targets` object gives access to all layers targetted by the run command.
|
|
1309
|
-
- by numerical index, like an array (`targets[0]` refers to the first target layer)
|
|
1310
|
-
- by layer name (`targets.states` refers to a layer named "states")
|
|
1311
|
-
|
|
1312
|
-
`io` object has a method for passing data to the `-i` command.
|
|
1313
|
-
- `io.ifile(<filename>, <data>)` Create a temp file to use as input in a `-run` command (see example 2 below)
|
|
1314
|
-
|
|
1315
|
-
**Example 1:** Apply a custom projection based on the layer extent.
|
|
1316
|
-
|
|
1317
|
-
```bash
|
|
1318
|
-
$ mapshaper -i country.shp \
|
|
1319
|
-
-require projection.js \
|
|
1320
|
-
-run '-proj {tmerc(target.bbox)}' \
|
|
1321
|
-
-o
|
|
1322
|
-
```
|
|
1323
|
-
|
|
1324
|
-
```javascript
|
|
1325
|
-
// contents of projection.js file
|
|
1326
|
-
module.exports.tmerc = function(bbox) {
|
|
1327
|
-
var lon0 = (bbox[0] + bbox[2]) / 2,
|
|
1328
|
-
lat0 = (bbox[1] + bbox[3]) / 2;
|
|
1329
|
-
return `+proj=tmerc lat_0=${lat0} lon_0=${lon0}`;
|
|
1330
|
-
};
|
|
1331
|
-
```
|
|
1332
|
-
|
|
1333
|
-
**Example 2:** Convert points to a Voronoi diagram using a template expression
|
|
1334
|
-
together with an external script.
|
|
1335
|
-
|
|
1336
|
-
```bash
|
|
1337
|
-
$ mapshaper points.geojson \
|
|
1338
|
-
-require script.js \
|
|
1339
|
-
-run '-i {io.ifile("voronoi.json", voronoi(target.geojson, target.bbox))}' \
|
|
1340
|
-
-o
|
|
1341
|
-
```
|
|
1342
|
-
|
|
1343
|
-
```javascript
|
|
1344
|
-
// contents of script.js file
|
|
1345
|
-
module.exports.voronoi = async function(points, bbox) {
|
|
1346
|
-
const d3 = await import('d3-delaunay'); // installed locally
|
|
1347
|
-
const coords = points.features.map(feat => feat.geometry.coordinates);
|
|
1348
|
-
const voronoi = d3.Delaunay.from(coords).voronoi(bbox);
|
|
1349
|
-
const features = Array.from(voronoi.cellPolygons()).map(function(ring, i) {
|
|
1350
|
-
return {
|
|
1351
|
-
type: 'Feature',
|
|
1352
|
-
properties: points.features[i].properties,
|
|
1353
|
-
geometry: {
|
|
1354
|
-
type: 'Polygon',
|
|
1355
|
-
coordinates: [ring]
|
|
1356
|
-
}
|
|
1357
|
-
};
|
|
1358
|
-
});
|
|
1359
|
-
return {type: 'FeatureCollection', features: features};
|
|
1360
|
-
};
|
|
1361
|
-
```
|
|
1362
|
-
|
|
1363
|
-
### -scalebar
|
|
1364
|
-
|
|
1365
|
-
Add a scale bar to an SVG map. The command creates a data-only layer containing the scale bar's data properties. A scale bar is included in the SVG output file if the scale bar layer is included as an output layer.
|
|
1366
|
-
|
|
1367
|
-
The length of the scale bar reflects the scale in the center of the map's rectangular frame.
|
|
1368
|
-
|
|
1369
|
-
`<label>` or `label=` Optional label giving the size and units of the scalebar, e.g. `"25 k.m."`. If only units are given, a length will be assigned. If the `label` property is missing, scale bar properties will be auto-generated. If two lengths are given (e.g. `1000 km,500 miles`), a dual-unit bar will be generated, if the scale bar style supports it.
|
|
1370
|
-
|
|
1371
|
-
`style=` Scalebar style, `a` or `b`. Style `b` has tic marks.
|
|
1372
|
-
|
|
1373
|
-
`bar-width=` Line width of bar.
|
|
1374
|
-
|
|
1375
|
-
`tic-length=` (style b) length of tic marks.
|
|
1376
|
-
|
|
1377
|
-
`label-position=` Position of labels relative to the bar (`top` or `bottom`).
|
|
1378
|
-
|
|
1379
|
-
`position=` Position of the scalebar on the map (default is `top-left`).
|
|
1380
|
-
|
|
1381
|
-
`margin=` Offset in pixels from edge of map.
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
### -shape
|
|
1385
|
-
|
|
1386
|
-
Create a new layer containing a single polyline or polygon shape.
|
|
1387
|
-
|
|
1388
|
-
`coordinates=<x,y,...>` Specify vertex coordinates as a comma-separated list.
|
|
1389
|
-
|
|
1390
|
-
`offsets=<dx,dy,...>` Specify vertex coordinates as a list of offsets from the previous vertex. The first vertex in the list is offset from the last coordinate in the `coordinates=` list.
|
|
1391
|
-
|
|
1392
|
-
`closed` Close an open path to form a polygon shape.
|
|
1393
|
-
|
|
1394
|
-
`name=` Assign a name to the newly created layer.
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
### -simplify
|
|
1398
|
-
|
|
1399
|
-
Mapshaper supports Douglas-Peucker simplification and two kinds of Visvalingam simplification.
|
|
1400
|
-
|
|
1401
|
-
Douglas-Peucker (a.k.a. Ramer-Douglas-Peucker) produces simplified lines that remain within a specified distance of the original line. It is effective for thinning dense vertices but tends to form spikes at high simplification.
|
|
1402
|
-
|
|
1403
|
-
Visvalingam simplification iteratively removes the least important point from a polyline. The importance of points is measured using a metric based on the geometry of the triangle formed by each non-endpoint vertex and the two neighboring vertices. The `visvalingam` option uses the "effective area" metric — points forming smaller-area triangles are removed first.
|
|
1404
|
-
|
|
1405
|
-
Mapshaper's default simplification method uses Visvalingam simplification but weights the effective area of each point so that smaller-angle vertices are preferentially removed, resulting in a smoother appearance.
|
|
1406
|
-
|
|
1407
|
-
When working with multiple polygon and polyline layers, the `-simplify` command is applied to all of the layers.
|
|
1408
|
-
|
|
1409
|
-
**Options**
|
|
1410
|
-
|
|
1411
|
-
`<percentage>` or `percentage=` Percentage of removable points to retain. Accepts values in the range `0%-100%` or `0-1`.
|
|
1412
|
-
|
|
1413
|
-
`dp` `rdp` Use Douglas-Peucker simplification.
|
|
1414
|
-
|
|
1415
|
-
`visvalingam` Use Visvalingam simplification with the "effective area" metric.
|
|
1416
|
-
|
|
1417
|
-
`weighted` Use weighted Visvalingam simplification (this is the default). Points located at the vertex of more acute angles are preferentially removed, for a smoother appearance.
|
|
1418
|
-
|
|
1419
|
-
`weighting=` Coefficient for weighting Visvalingam simplification (default is 0.7). Higher values produce smoother output. `weighting=0` is equivalent to unweighted Visvalingam simplification.
|
|
1420
|
-
|
|
1421
|
-
`resolution=` Use an output resolution (e.g. `5000` or `1000x800`) to control the amount of simplification.
|
|
1422
|
-
|
|
1423
|
-
`interval=` Specify simplification amount in units of distance (e.g. `100m`). If the distance unit is omitted (e.g. `interval=100`), uses meters when simplifying unprojected datasets in 3D space (see `planar` option below), otherwise uses the same units as the source data.
|
|
1424
|
-
|
|
1425
|
-
`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 [JavaScript expressions](/docs/guides/expressions.html.md) for the available context.
|
|
1426
|
-
|
|
1427
|
-
`planar` By default, mapshaper simplifies decimal degree coordinates in 3D space (using geocentric x,y,z coordinates). The `planar` option treats lng,lat coordinates as x,y coordinates on a Cartesian plane.
|
|
1428
|
-
|
|
1429
|
-
`keep-shapes` Prevent polygon features from disappearing at high simplification. For multipart features, mapshaper preserves the part with the largest original bounding box.
|
|
1430
|
-
|
|
1431
|
-
`no-repair` By default, mapshaper rolls back simplification along pairs of intersecting line segments by re-introducing removed points until either the intersection disappears or there are no more points to add. This option disables intersection repair.
|
|
1432
|
-
|
|
1433
|
-
`stats` Display summary statistics relating to the geometry of simplified paths.
|
|
1434
|
-
|
|
1435
|
-
**Examples**
|
|
1436
|
-
```bash
|
|
1437
|
-
# Simplify counties.shp using the default algorithm,
|
|
1438
|
-
# retaining 10% of removable vertices.
|
|
1439
|
-
mapshaper counties.shp -simplify 10% -o simplified.shp
|
|
1440
|
-
|
|
1441
|
-
# Use Douglas-Peucker simplification with a 100 meter threshold.
|
|
1442
|
-
mapshaper states.shp -simplify dp interval=100 -o simplified/
|
|
1443
|
-
```
|
|
1444
|
-
|
|
1445
|
-
### -snap
|
|
1446
|
-
|
|
1447
|
-
Snap together nearby vertices
|
|
1448
|
-
|
|
1449
|
-
`interval` Snap tolerance (default is small).
|
|
1450
|
-
|
|
1451
|
-
`endpoints` Only snap endpoints of polyline features.
|
|
1452
|
-
|
|
1453
|
-
`precision=` Round all coordinates to a given decimal precision (e.g. 0.000001).
|
|
1454
|
-
|
|
1455
|
-
`fix-geometry` Remove segment intersections caused by rounding or snapping by reverting intersecting areas to original coordinates. This option is only applied if the original paths are free of intersections.
|
|
1456
|
-
|
|
1457
|
-
[`target=`](#common-options)
|
|
1458
|
-
|
|
1459
|
-
### -sort
|
|
1460
|
-
|
|
1461
|
-
Sort features in a data layer using a JavaScript expression.
|
|
1462
|
-
|
|
1463
|
-
`<expression>` or `expression=` Apply a JavaScript expression to each feature, using the resulting values for sorting the features. Uses the same execution environment as [`-each`](#-each).
|
|
1464
|
-
|
|
1465
|
-
`ascending` Sort in ascending order (this is the default).
|
|
1466
|
-
|
|
1467
|
-
`descending` Sort in descending order.
|
|
1468
|
-
|
|
1469
|
-
[`target=`](#common-options)
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
### -split
|
|
1473
|
-
|
|
1474
|
-
Distributes features in the target layer to multiple output layers. If the `expression=` option is present, features with the same value are grouped together. The value of the expression is used to name the split-apart fields. If no argument is supplied, split-apart layers are numbered.
|
|
1475
|
-
|
|
1476
|
-
`<expression>` or `expression=` JS expression or name of the attribute field to split on.
|
|
1477
|
-
|
|
1478
|
-
Common options: `+` `target=`
|
|
1479
|
-
|
|
1480
|
-
**Examples**
|
|
1481
|
-
```bash
|
|
1482
|
-
# Split features from a named layer into new GeoJSON files using a data field.
|
|
1483
|
-
# Output names use the original layer name + data values,
|
|
1484
|
-
# e.g. states-AK.json, states-AL.json, etc.
|
|
1485
|
-
mapshaper states.shp -split STATE -o format=geojson
|
|
1486
|
-
|
|
1487
|
-
# Split features from an unnamed layer into new GeoJSON files
|
|
1488
|
-
# using a data field.
|
|
1489
|
-
# Output names contain data values,
|
|
1490
|
-
# e.g. AK.json, AL.json, etc.
|
|
1491
|
-
mapshaper states.shp name='' -split STATE -o format=geojson
|
|
1492
|
-
|
|
1493
|
-
# Split source features into individual GeoJSON files (no data field supplied).
|
|
1494
|
-
# Output names use source layer name + ascending number,
|
|
1495
|
-
# e.g. states-1, states-2, etc.
|
|
1496
|
-
mapshaper states.shp -split -o format=geojson
|
|
1497
|
-
```
|
|
1498
|
-
|
|
1499
|
-
### -split-on-grid
|
|
1500
|
-
|
|
1501
|
-
Split features into separate layers using a grid of cols,rows cells. Useful for dividing a large dataset into smaller files that can be loaded dynamically into an interactive map. Use `-o bbox-index` to export a file containing the name and bounding box of the shapes in each file. Empty cells are removed from the output.
|
|
1502
|
-
|
|
1503
|
-
`<cols,rows>` Size of the grid, e.g. `-split-on-grid 12,10`
|
|
1504
|
-
|
|
1505
|
-
Common options: `target=`
|
|
1506
|
-
|
|
1507
|
-
### -subdivide
|
|
1508
|
-
|
|
1509
|
-
Recursively divide a layer using a boolean JS expression. The expression is first evaluated against all features in the layer. If true, the features are spatially partitioned either vertically or horizontally, according to whether the aggregate bounding box is relatively tall or wide. See example below.
|
|
1510
|
-
|
|
1511
|
-
Subdivide expressions can call several functions that operate on a group of features. The `sum()` function takes a feature-level expression as an argument and returns the summed result after applying the expression to each feature in the group. Similar functions include `min()` `max()` `average()` and `median()`.
|
|
1512
|
-
|
|
1513
|
-
`<expression>` or `expression=` Boolean JavaScript expression
|
|
1514
|
-
|
|
1515
|
-
Common options: `target=`
|
|
1516
|
-
|
|
1517
|
-
**Example**
|
|
1518
|
-
```bash
|
|
1519
|
-
# Aggregate census tracts into groups of less than 1,000,000 population
|
|
1520
|
-
# and less than 100 sq km in area.
|
|
1521
|
-
mapshaper tracts.shp
|
|
1522
|
-
-subdivide "sum('POPULATION') >= 1000000 && sum('this.area') > 1e8" \
|
|
1523
|
-
-dissolve sum-fields=POPULATION \
|
|
1524
|
-
-merge-layers \
|
|
1525
|
-
-o tract_groups.shp
|
|
1526
|
-
```
|
|
1527
|
-
|
|
1528
|
-
### -style
|
|
1529
|
-
|
|
1530
|
-
Add common SVG attributes for SVG export and display in the web UI. Attribute values take either a literal value or a JS expression. See [JavaScript expressions](/docs/guides/expressions.html.md) for the available context. This command was named `-svg-style` in earlier versions of mapshaper.
|
|
1531
|
-
|
|
1532
|
-
`where=` Boolean JS expression for targetting a subset of features.
|
|
1533
|
-
|
|
1534
|
-
`clear` Remove all style properties from a layer.
|
|
1535
|
-
|
|
1536
|
-
`class=` One or more CSS classes, separated by spaces (e.g. `class="light semi-transparent"`)
|
|
1537
|
-
|
|
1538
|
-
`css=` Inline CSS to use as the `style=` attribute of each SVG symbol.
|
|
1539
|
-
|
|
1540
|
-
`fill=` Fill color (e.g. `#eee` `pink` `rgba(0, 0, 0, 0.2)`)
|
|
1541
|
-
|
|
1542
|
-
`fill-pattern=` Definition string for a pattern. There are four pattern types: hatches, dots, squares and dashes. The syntax for each pattern is:
|
|
1543
|
-
|
|
1544
|
-
- hatches [rotation] width1 color1 [width2 color2 ...]
|
|
1545
|
-
- dots [rotation] size color1 [color2 ...] spacing background-color
|
|
1546
|
-
- squares [rotation] size color1 [color2 ...] spacing background-color
|
|
1547
|
-
- dashes [rotation] dash-length space-length line-width color line-spacing background-color
|
|
1548
|
-
|
|
1549
|
-
Example: `hatches 45deg 2px red 2px grey`
|
|
1550
|
-
|
|
1551
|
-
`fill-effect=sphere` Add a gradient effect to the bounding circle of a globe projection (e.g. `ortho` `npers`) to create a 3d effect.
|
|
1552
|
-
|
|
1553
|
-
`stroke=` Stroke color
|
|
1554
|
-
|
|
1555
|
-
`stroke-width=` Stroke width
|
|
1556
|
-
|
|
1557
|
-
`stroke-dasharray=` Dashes
|
|
1558
|
-
|
|
1559
|
-
`opacity=` Symbol opacity (e.g. `opacity=0.5`)
|
|
1560
|
-
|
|
1561
|
-
`r=` Circle radius. Setting this exports points as SVG `<circle>` symbols, unless the `-o point-symbol=square` option is used.
|
|
1562
|
-
|
|
1563
|
-
`label-text=` Label text (set this to export points as labels). To create multiline labels, insert line delimiters into the label text. There are three possible line delimiters: the newline character, `\n` (backslash + "n"), and `<br>`. (When importing JSON data, `\n` in a JSON string is parsed as a newline and `\\n` is parsed as backslash + "n"). Note that Mapshaper doesn't accept multiline strings as input on the command line.
|
|
1564
|
-
|
|
1565
|
-
`text-anchor=` Horizontal justification of label text. Possible values are: start, end or middle (the default).
|
|
1566
|
-
|
|
1567
|
-
`dx=` X offset of labels (default is 0)
|
|
1568
|
-
|
|
1569
|
-
`dy=` Y offset of labels (default is baseline-aligned)
|
|
1570
|
-
|
|
1571
|
-
`font-size=` Size of label text (default is 12)
|
|
1572
|
-
|
|
1573
|
-
`font-family=` CSS font-family of labels (default is sans-serif)
|
|
1574
|
-
|
|
1575
|
-
`font-weight=` CSS font-weight property of labels (e.g. bold, 700)
|
|
1576
|
-
|
|
1577
|
-
`font-style=` CSS font-style property of labels (e.g. italic)
|
|
1578
|
-
|
|
1579
|
-
`font-stretch=` CSS font-stretch property of labels (e.g. condensed)
|
|
1580
|
-
|
|
1581
|
-
`letter-spacing=` CSS letter-spacing property of labels
|
|
1582
|
-
|
|
1583
|
-
`line-height=` Line spacing of multi-line labels (default is 1.1em). Lines are separated by newline characters or `<br>` tags in the label text.
|
|
1584
|
-
|
|
1585
|
-
Common options: `target=`
|
|
1586
|
-
|
|
1587
|
-
**Example**
|
|
1588
|
-
|
|
1589
|
-
```bash
|
|
1590
|
-
# Apply a 2px grey stroke and no fill to a polygon layer
|
|
1591
|
-
mapshaper polygons.geojson \
|
|
1592
|
-
-style fill=none stroke='#aaa' stroke-width=2 \
|
|
1593
|
-
-o out.svg
|
|
1594
|
-
```
|
|
1595
|
-
|
|
1596
|
-
### -symbols
|
|
1597
|
-
|
|
1598
|
-
Symbolize points as regular polygons, circles, stars, arrows and other shapes.
|
|
1599
|
-
|
|
1600
|
-
`type=` Basic types: star, polygon, circle, arrow, ring. Aliases: triangle, square, pentagon, etc.
|
|
1601
|
-
|
|
1602
|
-
`fill=` Symbol fill color
|
|
1603
|
-
|
|
1604
|
-
`stroke=` Symbol line color (linear symbols only)
|
|
1605
|
-
|
|
1606
|
-
`stroke-width=` Symbol line width (linear symbols only)
|
|
1607
|
-
|
|
1608
|
-
`opacity=` Symbol opacity
|
|
1609
|
-
|
|
1610
|
-
`geographic` Make geographic shapes (polygons or polylines) instead of SVG objects
|
|
1611
|
-
|
|
1612
|
-
`pixel-scale=` Set symbol scale in meters-per-pixel (for polygons option)
|
|
1613
|
-
|
|
1614
|
-
`rotated` Symbol is rotated to an alternate orientation
|
|
1615
|
-
|
|
1616
|
-
`rotation=` Rotation of symbol in degrees
|
|
1617
|
-
|
|
1618
|
-
`scale=` Scale symbols by a multiplier
|
|
1619
|
-
|
|
1620
|
-
`radius=` Distance from center to farthest point on the symbol
|
|
1621
|
-
|
|
1622
|
-
`sides=` (polygon) number of sides of a polygon symbol
|
|
1623
|
-
|
|
1624
|
-
`points=` (star) number of points
|
|
1625
|
-
|
|
1626
|
-
`point-ratio=` (star) ratio of minor to major radius of star
|
|
1627
|
-
|
|
1628
|
-
`radii=` (ring) comma-sep. list of concentric radii, ascending order
|
|
1629
|
-
|
|
1630
|
-
`length=` (arrow) length of arrow in pixels
|
|
1631
|
-
|
|
1632
|
-
`direction=` (arrow) angle off of vertical (-90 = left-pointing)
|
|
1633
|
-
|
|
1634
|
-
`head-angle=` (arrow) angle of tip of arrow (default is 40 degrees)
|
|
1635
|
-
|
|
1636
|
-
`head-width=` (arrow) width of arrow head from side to side
|
|
1637
|
-
|
|
1638
|
-
`head-length=` (arrow) length of head (alternative to head-angle). Use `head-length=0` to make headless arrows (i.e. simple lines)
|
|
1639
|
-
|
|
1640
|
-
`stem-width=` (arrow) width of stem at its widest point
|
|
1641
|
-
|
|
1642
|
-
`stem-length=` (arrow) alternative to length
|
|
1643
|
-
|
|
1644
|
-
`stem-taper=` (arrow) factor for tapering the width of the stem (0-1)
|
|
1645
|
-
|
|
1646
|
-
`stem-curve=` (arrow) curvature in degrees (default is 0)
|
|
1647
|
-
|
|
1648
|
-
`min-stem-ratio=` (arrow) minimum ratio of stem to total length. This option scales down the entire symbol instead of making the stem shorter than the given ratio.
|
|
1649
|
-
|
|
1650
|
-
`anchor=` (arrow) takes one of: start, middle, end (default is start)
|
|
1651
|
-
|
|
1652
|
-
Common options: `name=` `+` `target=`
|
|
1653
|
-
|
|
1654
|
-
### -union
|
|
1655
|
-
|
|
1656
|
-
Create a composite layer (a polygon mosaic without overlaps) from two or more target polygon layers.
|
|
1657
|
-
|
|
1658
|
-
Data values are copied from source features to output features. (Areal interpolation may
|
|
1659
|
-
be added in the future. The `-join` command currently supports areal interpolation between polygon layers using the `-join interpolate=<fields>` option.) Same-named fields in source layers are renamed in the output layer. For example, two source-layer fields named "id" will be renamed to "id_1" and "id_2".
|
|
1660
|
-
|
|
1661
|
-
`fields=` Fields to retain (default is all fields).
|
|
1662
|
-
|
|
1663
|
-
Common options: `name=` `+` `target=`
|
|
1664
|
-
|
|
1665
|
-
### -uniq
|
|
1666
|
-
|
|
1667
|
-
Delete features with the same id as a previous feature
|
|
1668
|
-
|
|
1669
|
-
`<expression>` or `expression=` JS expression to obtain the id of a feature. Uses the same expression syntax as [`-each`](#-each).
|
|
1670
|
-
|
|
1671
|
-
`max-count=` Allow multiple features with the same id (default is 1).
|
|
1672
|
-
|
|
1673
|
-
`invert` Retain only features that would ordinarily be deleted by `-uniq`.
|
|
1674
|
-
|
|
1675
|
-
`verbose` Print information about each removed feature.
|
|
1676
|
-
|
|
1677
|
-
`target=`
|
|
1678
|
-
|
|
1679
|
-
```bash
|
|
1680
|
-
# Example: Retain only the largest parts of each multipart polygon
|
|
1681
|
-
mapshaper polygons.shp \
|
|
1682
|
-
-each 'fid = this.id' \
|
|
1683
|
-
-explode \
|
|
1684
|
-
-sort 'this.area' descending \
|
|
1685
|
-
-uniq 'fid' \
|
|
1686
|
-
-o out.shp
|
|
1687
|
-
```
|
|
1688
|
-
|
|
1689
|
-
## Control Flow Commands
|
|
1690
|
-
|
|
1691
|
-
### -if
|
|
1692
|
-
|
|
1693
|
-
The `if` command runs the following commands if a condition is met.
|
|
1694
|
-
|
|
1695
|
-
`<expression>` or `expression=` Use a JavaScript expression to test a condition.
|
|
1696
|
-
|
|
1697
|
-
`empty` Test if layer is empty.
|
|
1698
|
-
|
|
1699
|
-
`not-empty` Test if layer contains data.
|
|
1700
|
-
|
|
1701
|
-
`layer=` Name or id of layer to test (default is current target layer).
|
|
1702
|
-
|
|
1703
|
-
**Properties of `this`**
|
|
1704
|
-
|
|
1705
|
-
- `this.name` Layer name, or <undefined> if layer is unnamed.
|
|
1706
|
-
- `this.size` Number of features in the layer.
|
|
1707
|
-
- `this.empty` True if layer contains 0 features.
|
|
1708
|
-
- `this.data` Array of attribute data records, one object per feature.
|
|
1709
|
-
- `this.type` Geometry type, one of: polygon, polyline, point, <undefined>.
|
|
1710
|
-
- `this.bbox` An array [xmin, ymin, xmax, ymax] with additional properties: cx, cy, height, width, left, bottom, top, right.
|
|
1711
|
-
|
|
1712
|
-
**Functions of `this`**
|
|
1713
|
-
|
|
1714
|
-
- `this.field_exists(<name>)` Tests if a data field exists in the target layer.
|
|
1715
|
-
- `this.field_type(<name>)` Returns the data type of a field, or `null` if a field is empty or missing. Types include: `"string" "number" "boolean" "date" "object"`. If a field includes multiple data types (which may occur in GeoJSON), the type of the first non-empty data value is returned.
|
|
1716
|
-
- `this.field_includes(<value>)` Tests if a given value occurs at least once in a data field.
|
|
1717
|
-
- `this.file_exists(<filename>)` Tests if a file exists.
|
|
1718
|
-
|
|
1719
|
-
**Example**
|
|
1720
|
-
|
|
1721
|
-
```bash
|
|
1722
|
-
mapshaper -i shapes.json -if '!this.empty' -dissolve -o out/dissolved.json
|
|
1723
|
-
```
|
|
1724
|
-
|
|
1725
|
-
### -elif
|
|
1726
|
-
|
|
1727
|
-
One or more `-elif` (short for "else if") commands may be added to test for alternate conditions, following an `-if` statement. The `-elif` command accepts the same options as the `-if` command.
|
|
1728
|
-
|
|
1729
|
-
### -else
|
|
1730
|
-
|
|
1731
|
-
Run the following commands if all preceding -if/-elif conditions are false.
|
|
1732
|
-
|
|
1733
|
-
### -endif
|
|
1734
|
-
|
|
1735
|
-
Mark the end of an -if/-elif/-else sequence.
|
|
1736
|
-
|
|
1737
|
-
### -stop
|
|
1738
|
-
|
|
1739
|
-
Stop processing (skip remaining commands). Useful when writing scripts, in combination with -if/-elif/-else.
|
|
1740
|
-
|
|
1741
|
-
**Example**
|
|
1742
|
-
|
|
1743
|
-
```bash
|
|
1744
|
-
# Don't try to process a missing file
|
|
1745
|
-
mapshaper -if '!file_exists("boundaries.geojson")' -stop -endif \
|
|
1746
|
-
-i boundaries.geojson -proj robin -o output/boundaries.shp
|
|
1747
|
-
```
|
|
1748
|
-
|
|
1749
|
-
### -target
|
|
1750
|
-
|
|
1751
|
-
Set the target layer or layers for the following command.
|
|
1752
|
-
|
|
1753
|
-
`<target>` or `target=` Name or id of a layer (first layer is 1).
|
|
1754
|
-
|
|
1755
|
-
`type=` Type of layer(s) to match (polygon, polyline or point). This is useful when importing GeoJSON files containing several types of geometry.
|
|
1756
|
-
|
|
1757
|
-
`name=` Rename the target layer.
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
## Informational Commands
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
### -calc
|
|
1764
|
-
|
|
1765
|
-
Perform calculations on a collection of records and display the results, using a JavaScript expression. The expression can use one or more of the following built-in functions. Most functions take the name of a data field or a JS expression as the first argument.
|
|
1766
|
-
|
|
1767
|
-
The `calc` functions are also available in the context of `calc=` expressions, which can be used as options to `-join`, `-dissolve` and several other commands. See example below. For the full expression context (per-feature properties available inside `<expr>`, the `where=` filter syntax, etc.), see [JavaScript expressions](/docs/guides/expressions.html.md).
|
|
1768
|
-
|
|
1769
|
-
| function | description |
|
|
1770
|
-
| --- | --- |
|
|
1771
|
-
| `count ()` | returns number of records in the collection |
|
|
1772
|
-
| `sum (<expr>)` | |
|
|
1773
|
-
| `mean (<expr>)` | |
|
|
1774
|
-
| `average (<expr>)` | same as mean() |
|
|
1775
|
-
| `median (<expr>)` | |
|
|
1776
|
-
| `mode (<expr>)` | returns most frequently occuring value in the collection (or the first such value in case of a tie). |
|
|
1777
|
-
| `min (<expr>)` | |
|
|
1778
|
-
| `max (<expr>)` | |
|
|
1779
|
-
| `quartile1 (<expr>)` | first quartile |
|
|
1780
|
-
| `quartile2 (<expr>)` | same as median() |
|
|
1781
|
-
| `quartile3 (<expr>)` | third quartile |
|
|
1782
|
-
| `iqr (<expr>)` | interquartile range |
|
|
1783
|
-
| `quantile (<expr>, <pct>)` | arbitrary percentile (`<pct>` is 0-1) |
|
|
1784
|
-
| `collect (<expr>)` | returns array containing all values |
|
|
1785
|
-
| `collectIds ()` | returns array of feature indexes (features are indexed 0 to n-1)
|
|
1786
|
-
| `first (<expr>)` | returns first value in the collection |
|
|
1787
|
-
| `last (<expr>)` | returns last value in the collection |
|
|
1788
|
-
| `every (<expr>)` | returns true if expression is true for all elements in the collection |
|
|
1789
|
-
| `some (<expr>)` | returns true if expression is true for one or more elements in the collection |
|
|
1790
|
-
|
|
1791
|
-
Argument expressions take the same form as `-each` expressions. If no records are processed, `count()` and `sum()` return `0`, and the other functions return `null`.
|
|
1792
|
-
|
|
1793
|
-
**Assignments**
|
|
1794
|
-
|
|
1795
|
-
The `-calc` expression can take the form of one or more assignments. This creates global variables that can be accessed by subsequent expressions using the `global` namespace. For example:
|
|
1796
|
-
|
|
1797
|
-
```
|
|
1798
|
-
mapshaper data.csv \
|
|
1799
|
-
-calc 'N = count()' \
|
|
1800
|
-
-if 'global.N < 5' \
|
|
1801
|
-
-print 'LOW SAMPLE SIZE, STOPPING' \
|
|
1802
|
-
-stop \
|
|
1803
|
-
-endif
|
|
1804
|
-
```
|
|
1805
|
-
|
|
1806
|
-
**Options**
|
|
1807
|
-
|
|
1808
|
-
`<expression>` or `expression=` JS expression containing calls to one or more `-calc` functions.
|
|
1809
|
-
|
|
1810
|
-
`where=` Perform calculations on a subset of records, using a boolean JS expression as a filter (similar to [`-filter`](#-filter) command).
|
|
1811
|
-
`+` Save output to a layer.
|
|
1812
|
-
`name=` Name the output layer (default name is "calc").
|
|
1813
|
-
`target=`
|
|
1814
|
-
|
|
1815
|
-
**Examples**
|
|
1816
|
-
|
|
1817
|
-
```bash
|
|
1818
|
-
# Calculate the sum of a data field
|
|
1819
|
-
mapshaper ny-census-blocks.shp -calc 'sum(POPULATION)'
|
|
1820
|
-
|
|
1821
|
-
# Count census blocks in NY with zero population
|
|
1822
|
-
mapshaper ny-census-blocks.shp -calc 'count()' where='POPULATION == 0'
|
|
1823
|
-
|
|
1824
|
-
# Using calc functions in conjunction with the `-dissolve` command
|
|
1825
|
-
mapshaper counties.csv \
|
|
1826
|
-
-dissolve STATE_NAME calc='NUM_COUNTIES = count()' \
|
|
1827
|
-
-o states.csv
|
|
1828
|
-
```
|
|
1829
|
-
|
|
1830
|
-
### -colors
|
|
1831
|
-
Print list of built-in color schemes. Color schemes can be used with the `color-scheme=` option of the [-classify](#-classify) command. (These color schemes come from the [d3-scale-chromatic](https://github.com/d3/d3-scale-chromatic) library.)
|
|
1832
|
-
|
|
1833
|
-
### -comment
|
|
1834
|
-
The following text up to the next command is treated as a comment. Useful for adding explanatory comments to a long sequence of commands.
|
|
1835
|
-
|
|
1836
|
-
### -encodings
|
|
1837
|
-
Print list of supported text encodings (for .dbf import).
|
|
1838
|
-
|
|
1839
|
-
### -help
|
|
1840
|
-
|
|
1841
|
-
Print usage tips and a list of commands.
|
|
1842
|
-
|
|
1843
|
-
`<command>` Show options for a single command, e.g. `mapshaper -h join`.
|
|
1844
|
-
|
|
1845
|
-
### -info
|
|
1846
|
-
|
|
1847
|
-
Print information about a dataset. Useful for seeing the fields in a layer's attribute data table. Also useful for summarizing the result of a series of commands, or for debugging unexpected output.
|
|
1848
|
-
|
|
1849
|
-
```bash
|
|
1850
|
-
# Example: Get information about an unknown GeoJSON or TopoJSON dataset
|
|
1851
|
-
mapshaper mystery_file.json -info
|
|
1852
|
-
```
|
|
1853
|
-
|
|
1854
|
-
`save-to=` Save information to a .json file.
|
|
1855
|
-
`+` Save output to a layer.
|
|
1856
|
-
`name=` Name the output layer (default name is "info").
|
|
1857
|
-
|
|
1858
|
-
### -inspect
|
|
1859
|
-
|
|
1860
|
-
Print information about the data attributes of a feature.
|
|
1861
|
-
|
|
1862
|
-
`<expression>` or `expression=` JS expression for selecting a feature (see [JavaScript expressions](/docs/guides/expressions.html.md) for the syntax and context).
|
|
1863
|
-
|
|
1864
|
-
Common options: `target=`
|
|
1865
|
-
|
|
1866
|
-
```bash
|
|
1867
|
-
# Example: View attribute data for a state
|
|
1868
|
-
mapshaper states.geojson -inspect 'NAME == "Delaware"'
|
|
1869
|
-
```
|
|
1870
|
-
|
|
1871
|
-
### -print
|
|
1872
|
-
|
|
1873
|
-
Prints a message to the console or terminal (using stdout). This command is useful in combination with the `-if/-elif/-else` commands.
|
|
1874
|
-
|
|
1875
|
-
```bash
|
|
1876
|
-
# Example
|
|
1877
|
-
mapshaper cities.json \
|
|
1878
|
-
-if 'this.empty' \
|
|
1879
|
-
-print FILE IS EMPTY
|
|
1880
|
-
```
|
|
1881
|
-
|
|
1882
|
-
### -projections
|
|
1883
|
-
|
|
1884
|
-
Print list of supported proj4 projection ids and projection aliases.
|
|
1885
|
-
|
|
1886
|
-
### -quiet
|
|
1887
|
-
|
|
1888
|
-
Inhibit console messages.
|
|
1889
|
-
|
|
1890
|
-
### -vars
|
|
1891
|
-
|
|
1892
|
-
Define variables for [`{{VAR}}` interpolation](#variables). Each argument is either an inline assignment (`KEY=value`) or a path to a JSON file containing a flat object whose keys are variable names and whose values are strings, numbers or booleans.
|
|
1893
|
-
|
|
1894
|
-
`-vars` can be used on the command line, inside a command file, or anywhere else a command is accepted. Each invocation writes to the templating-scope variable store, overwriting any previously defined value. Use [`-defaults`](#-defaults) instead if you want set-if-unset behavior.
|
|
1895
|
-
|
|
1896
|
-
Values set by `-vars` are visible to `{{X}}` substitution but **not** by bare name in JS expressions (`-each`, `-filter`, `-define`, etc.). If you want a value usable from both, set it once with [`-define`](#-define) — `{{X}}` substitution falls back to the expression scope when a name isn't in the templating scope.
|
|
1897
|
-
|
|
1898
|
-
**Options**
|
|
1899
|
-
|
|
1900
|
-
`<values>` Space-separated list of `KEY=value` assignments and/or paths to JSON files containing variable definitions.
|
|
1901
|
-
|
|
1902
|
-
**Example**
|
|
1903
|
-
|
|
1904
|
-
```bash
|
|
1905
|
-
# Set YEAR=2024 before running the command file
|
|
1906
|
-
mapshaper -vars YEAR=2024 -run build.txt
|
|
1907
|
-
|
|
1908
|
-
# Load values from a JSON file, then override one of them
|
|
1909
|
-
mapshaper -vars values.json YEAR=2030 -run build.txt
|
|
1910
|
-
```
|
|
1911
|
-
|
|
1912
|
-
### -defaults
|
|
1913
|
-
|
|
1914
|
-
Like [`-vars`](#-vars), but writes only those keys that are not already defined. Used inside a command file to declare overridable defaults: a CLI `-vars` (or any earlier write) preempts the command file's `-defaults` for the same key.
|
|
1915
|
-
|
|
1916
|
-
**Options**
|
|
1917
|
-
|
|
1918
|
-
`<values>` Space-separated list of `KEY=value` assignments and/or paths to JSON files containing variable definitions.
|
|
1919
|
-
|
|
1920
|
-
**Example**
|
|
1921
|
-
|
|
1922
|
-
```bash
|
|
1923
|
-
# build.txt declares YEAR=2024 as a default; this CLI invocation overrides it
|
|
1924
|
-
mapshaper -vars YEAR=2030 -run build.txt
|
|
1925
|
-
```
|
|
1926
|
-
|
|
1927
|
-
### -verbose
|
|
1928
|
-
|
|
1929
|
-
Print verbose messages, including the time taken by each processing step.
|
|
1930
|
-
|
|
1931
|
-
### -version
|
|
1932
|
-
|
|
1933
|
-
Print mapshaper version.
|
|
1934
|
-
|
|
1935
|
-
---
|
|
1936
|
-
|
|
1937
|
-
<!-- https://mapshaper.org/docs/gallery/ -->
|
|
1938
|
-
|
|
1939
|
-
# Gallery
|
|
1940
|
-
|
|
1941
|
-
A collection of example maps made with Mapshaper. Each tile links to a full write-up with the recipe, the source data, and a one-click link to open the finished snapshot in the [web app](/).
|
|
1942
|
-
|
|
1943
|
-
<!-- mapshaper:gallery -->
|
|
1944
|
-
|
|
1945
|
-
---
|
|
1946
|
-
|
|
1947
|
-
<!-- https://mapshaper.org/docs/essentials/command-line.html -->
|
|
1948
|
-
|
|
1949
|
-
# The command-line tool
|
|
1950
|
-
|
|
1951
|
-
Mapshaper ships as a command-line tool and a [web app](/docs/essentials/web-app.html.md). This page is a tour of the CLI and the most common things you'll use it for.
|
|
1952
|
-
|
|
1953
|
-
## Install
|
|
1954
|
-
|
|
1955
|
-
Mapshaper requires [Node.js](https://nodejs.org). With Node installed:
|
|
1956
|
-
|
|
1957
|
-
```bash
|
|
1958
|
-
npm install -g mapshaper
|
|
1959
|
-
```
|
|
1960
|
-
|
|
1961
|
-
That gives you three executables:
|
|
1962
|
-
|
|
1963
|
-
- `mapshaper` — the main CLI.
|
|
1964
|
-
- `mapshaper-xl` — same as `mapshaper`, but launched with a larger Node heap (8 GB by default) for processing very large files. Override the limit with `mapshaper-xl 16gb [commands]`.
|
|
1965
|
-
- `mapshaper-gui` — runs the [web UI](/docs/essentials/web-app.html.md) locally on `http://localhost:5555`.
|
|
1966
|
-
|
|
1967
|
-
You can also run mapshaper without installing it via [Bun](https://bun.sh/) (`bunx mapshaper [commands]`) or with `npx mapshaper [commands]`.
|
|
1968
|
-
|
|
1969
|
-
Check the install with:
|
|
1970
|
-
|
|
1971
|
-
```bash
|
|
1972
|
-
mapshaper -v
|
|
1973
|
-
```
|
|
1974
|
-
|
|
1975
|
-
## Anatomy of a Mapshaper command
|
|
1976
|
-
|
|
1977
|
-
A Mapshaper invocation is a sequence of commands run left-to-right. Each command starts with a hyphen-prefixed name and is followed by zero or more options. The initial `-i` (input) command is implied if the first argument is a file path.
|
|
1978
|
-
|
|
1979
|
-
```bash
|
|
1980
|
-
mapshaper provinces.shp -simplify dp 20% -o precision=0.00001 output.geojson
|
|
1981
|
-
```
|
|
1982
|
-
|
|
1983
|
-
This reads a Shapefile, simplifies it using the Douglas-Peucker algorithm to 20% of its vertices, and writes a GeoJSON file with rounded coordinates.
|
|
1984
|
-
|
|
1985
|
-
Options come in three forms:
|
|
1986
|
-
|
|
1987
|
-
- **Values** like `provinces.shp` and `output.geojson`.
|
|
1988
|
-
- **Flags** like `dp`.
|
|
1989
|
-
- **Name/value pairs** like `precision=0.00001`.
|
|
1990
|
-
|
|
1991
|
-
For the full reference, see the [command reference](/docs/reference.html.md).
|
|
1992
|
-
|
|
1993
|
-
## Some examples
|
|
1994
|
-
|
|
1995
|
-
### Get help
|
|
1996
|
-
|
|
1997
|
-
```bash
|
|
1998
|
-
mapshaper -h # list all commands
|
|
1999
|
-
mapshaper -h simplify # detailed options for one command
|
|
2000
|
-
```
|
|
2001
|
-
|
|
2002
|
-
### Chain commands
|
|
2003
|
-
|
|
2004
|
-
```bash
|
|
2005
|
-
# From census blocks: dissolve populated Census blocks to tract level.
|
|
2006
|
-
mapshaper tabblock2010_36_pophu.shp \
|
|
2007
|
-
-filter 'POP10 > 0' \
|
|
2008
|
-
-each 'TRACT=BLOCKID10.substr(0,11)' \
|
|
2009
|
-
-dissolve TRACT sum-fields=POP10 \
|
|
2010
|
-
-o tracts.shp
|
|
2011
|
-
```
|
|
2012
|
-
|
|
2013
|
-
```bash
|
|
2014
|
-
# Generate state and national boundaries from one county-level Shapefile.
|
|
2015
|
-
# Output: states.shp, usa.shp and other Shapefile component files
|
|
2016
|
-
mapshaper counties.shp \
|
|
2017
|
-
-dissolve STATE_FIPS name=states \
|
|
2018
|
-
-dissolve + name=usa \
|
|
2019
|
-
-o target='*'
|
|
2020
|
-
```
|
|
2021
|
-
|
|
2022
|
-
For a broader set of recipes — filtering, joining, dissolving, reprojection, styling and more — see [Basics](/docs/examples/basics.html.md).
|
|
2023
|
-
|
|
2024
|
-
## Working with layers
|
|
2025
|
-
|
|
2026
|
-
Most commands operate on **layers** of data features. A layer is a collection of features with the same geometry type and a consistent attribute schema. Mapshaper supports polygon, polyline and point layers; a single feature may contain one shape, multiple shapes, or no shapes at all.
|
|
2027
|
-
|
|
2028
|
-
The simplest case is a single layer in, a single layer out:
|
|
2029
|
-
|
|
2030
|
-
```bash
|
|
2031
|
-
mapshaper counties.shp -filter 'this.isNull === false' -o counties_notnull.shp
|
|
2032
|
-
```
|
|
2033
|
-
|
|
2034
|
-
When a command runs on a multi-layer dataset, it acts on the **target** layer(s). Most commands accept a `target=` option, and the [`-target`](/docs/reference.html.md#-target) command sets the target for everything that follows.
|
|
2035
|
-
|
|
2036
|
-
The `+` option keeps the original layer and creates a new one from the command's output. Combined with `name=`, it's the idiomatic way to derive a new layer:
|
|
2037
|
-
|
|
2038
|
-
```bash
|
|
2039
|
-
# Output: out/provinces.json and out/lines.json
|
|
2040
|
-
mapshaper provinces.shp \
|
|
2041
|
-
-simplify 20% \
|
|
2042
|
-
-innerlines + name=lines \
|
|
2043
|
-
-target provinces,lines \
|
|
2044
|
-
-o format=geojson out/
|
|
2045
|
-
```
|
|
2046
|
-
|
|
2047
|
-
This produces `out/provinces.json` and `out/lines.json`.
|
|
2048
|
-
|
|
2049
|
-
When importing a TopoJSON file, each named object becomes a layer:
|
|
2050
|
-
|
|
2051
|
-
```bash
|
|
2052
|
-
mapshaper usa.topojson \
|
|
2053
|
-
-filter 'STATE == "HI"' target=states \
|
|
2054
|
-
-o out/hawaii.geojson
|
|
2055
|
-
```
|
|
2056
|
-
|
|
2057
|
-
---
|
|
2058
|
-
|
|
2059
|
-
<!-- https://mapshaper.org/docs/essentials/web-app.html -->
|
|
2060
|
-
|
|
2061
|
-
# The web app
|
|
2062
|
-
|
|
2063
|
-
The Mapshaper web app at [mapshaper.org](/) is designed for interactive editing and visual exploration. Its built-in console exposes the complete command set, so almost anything the CLI can do is available directly in the browser.
|
|
2064
|
-
|
|
2065
|
-
All processing happens in your browser. Your data stays on your machine, even when you use the public website.
|
|
2066
|
-
|
|
2067
|
-
## Loading data
|
|
2068
|
-
|
|
2069
|
-
Drag-drop, paste, or use the **Add files** button to import data.
|
|
2070
|
-
|
|
2071
|
-
A few less-obvious behaviors:
|
|
2072
|
-
|
|
2073
|
-
- **Drop a `.zip` containing a Shapefile bundle** — Mapshaper unzips it on the fly and pulls out the sidecars. (Same goes for `.gz` for single-file formats and `.kmz` for KML.)
|
|
2074
|
-
- **Paste a URL** anywhere on the page to import the file at that address.
|
|
2075
|
-
- **Query-string preload** — `https://mapshaper.org/?files=URL1,URL2` imports a comma-separated list of URLs. All files need to be served from a host that allows cross-origin requests. Append `&q` to skip the import dialog and open the files immediately.
|
|
2076
|
-
- **With advanced options** is a freeform text field. Anything you'd pass after `-i` on the CLI works here, including `encoding=`, `string-fields=`, `csv-fields=`, `csv-filter=`, `combine-files`, `name=` and so on.
|
|
2077
|
-
- **Multiple files do not auto-combine.** Selecting several files at once imports them as independent layers. To get a shared topology (so common boundaries simplify identically), tick **with advanced options** and add `combine-files`.
|
|
2078
|
-
|
|
2079
|
-
### Tips for importing Shapefiles
|
|
2080
|
-
|
|
2081
|
-
- Drag-drop or select the `.shp`, `.dbf` and `.prj` files together. Without `.dbf` you'll have geometry but no attributes; without `.prj`, projection-dependent commands won't know what to do.
|
|
2082
|
-
- If you see a warning about an unknown text encoding, re-import using the **with advanced options** checkbox and set `encoding=` (for example, `encoding=big5` for Big-5).
|
|
2083
|
-
|
|
2084
|
-
## The console
|
|
2085
|
-
|
|
2086
|
-
The Console (top-right of the header, or **space bar** to toggle) is the most powerful part of the UI. Anything the [CLI](/docs/reference.html.md) can do, you can do here.
|
|
2087
|
-
|
|
2088
|
-
### Keyboard
|
|
2089
|
-
|
|
2090
|
-
- **Space** — open or close the console (only when you're not typing in another text field).
|
|
2091
|
-
- **Esc** — close the console, or close any open panel.
|
|
2092
|
-
- **Up / Down** — cycle through previous commands. The history persists across page reloads.
|
|
2093
|
-
- **Backslash `\` at end of line** — continue a long command on the next line. The Enter key adds the wrap; another **Enter** runs the full command.
|
|
2094
|
-
|
|
2095
|
-
### Syntax
|
|
2096
|
-
|
|
2097
|
-
- The leading `-` is optional in the console: `clip places` works the same as `-clip places`.
|
|
2098
|
-
- Commands run on the **currently-selected layer** by default. Switch layers in the layer panel before issuing a command, or pass `target=` to be explicit (`target=*` runs against every layer).
|
|
2099
|
-
|
|
2100
|
-
### Magic words at the prompt
|
|
2101
|
-
|
|
2102
|
-
These are recognized directly by the console, not by Mapshaper:
|
|
2103
|
-
|
|
2104
|
-
- `history` — print the current session as a single command-line string. Handy for reproducing an interactive workflow as a script.
|
|
2105
|
-
- `layers` — print the list of loaded layers.
|
|
2106
|
-
- `clear` — clear the console buffer.
|
|
2107
|
-
- `close` / `exit` / `quit` — close the console.
|
|
2108
|
-
|
|
2109
|
-
### Discovering commands
|
|
2110
|
-
|
|
2111
|
-
- `help` lists every available command.
|
|
2112
|
-
- `help <command>` shows the full options for one command, e.g. `help dissolve`.
|
|
2113
|
-
- The [command reference](/docs/reference.html.md) is the same content with a search box.
|
|
2114
|
-
|
|
2115
|
-
## The map
|
|
2116
|
-
|
|
2117
|
-
### Right-click menu
|
|
2118
|
-
|
|
2119
|
-
The right-click menu adapts to what's under the cursor:
|
|
2120
|
-
|
|
2121
|
-
- **Copy lon, lat** — copy the WGS84 coordinates of the click point to the clipboard.
|
|
2122
|
-
- **Copy x, y** — copy the projected coordinates of the click point.
|
|
2123
|
-
- **Copy as GeoJSON** — copy the selected feature(s) to the clipboard as GeoJSON. Useful for snipping out one polygon for use elsewhere.
|
|
2124
|
-
- **Delete vertex** / **delete point** / **delete feature** — available in the corresponding edit modes.
|
|
2125
|
-
|
|
2126
|
-
### Layer navigation
|
|
2127
|
-
|
|
2128
|
-
- **Left / Right arrows** (when not typing) — cycle through the loaded layers.
|
|
2129
|
-
|
|
2130
|
-
## Display options
|
|
2131
|
-
|
|
2132
|
-
The **Display** button at the top right opens the display options panel.
|
|
2133
|
-
|
|
2134
|
-
- **Detect line intersections** — highlights self-intersections in red as you simplify or edit. The quickest way to spot simplification damage. The setting is remembered between sessions.
|
|
2135
|
-
|
|
2136
|
-
## Snapshots and session history
|
|
2137
|
-
|
|
2138
|
-
The ribbon icon in the layer panel opens the snapshot menu. Snapshots save the current state of a session so you can return to it. They also record the **session history** that produced the snapshot, so when you re-open one the full history is available too.
|
|
2139
|
-
|
|
2140
|
-
- **Create a snapshot** — saves to in-browser storage. These are session-scoped and **deleted when the tab closes or the page is reloaded.** For anything you want to keep, **Save snapshot to file** writes a `.msx` file you can re-open later.
|
|
2141
|
-
- **View session history** — a shortcut for typing `history` in the console: prints the full sequence of commands that produced the current state.
|
|
2142
|
-
|
|
2143
|
-
See the [Mapshaper snapshot format page](/docs/formats/snapshot.html.md) for more on what a `.msx` file contains and how to use it from the CLI.
|
|
2144
|
-
|
|
2145
|
-
## Running the web UI locally
|
|
2146
|
-
|
|
2147
|
-
`mapshaper-gui` (installed alongside `mapshaper` when you `npm install -g mapshaper`) starts a local Node web server and opens the web UI at `http://localhost:5555`. Use `--port` to pick a different port.
|
|
2148
|
-
|
|
2149
|
-
You can pre-load files by listing them on the command line, which skips the import dialog:
|
|
2150
|
-
|
|
2151
|
-
```bash
|
|
2152
|
-
mapshaper-gui states.shp rivers.shp
|
|
2153
|
-
```
|
|
2154
|
-
|
|
2155
|
-
## Browser support
|
|
2156
|
-
|
|
2157
|
-
When importing very large files (hundreds of megabytes), the web app may run out of memory and crash. Firefox used to be better than Chrome at handling large files, but Chrome seems to have improved recently. If the web app crashes, try the [`mapshaper-xl` command-line tool](/docs/essentials/command-line.html.md), which can allocate a large amount of memory.
|
|
2158
|
-
|
|
2159
|
-
## Privacy
|
|
2160
|
-
|
|
2161
|
-
The Mapshaper web app runs entirely in your browser. No file content is uploaded to any server. The only network traffic is for static assets (the app itself), basemap tiles when you've enabled a basemap, and analytics for `mapshaper.org` page loads. See the [privacy policy](/privacy.html) for details.
|
|
2162
|
-
|
|
2163
|
-
---
|
|
2164
|
-
|
|
2165
|
-
<!-- https://mapshaper.org/docs/guides/simplification.html -->
|
|
2166
|
-
|
|
2167
|
-
# Simplification
|
|
2168
|
-
|
|
2169
|
-
Simplification reduces the number of vertices in polylines and polygon boundaries while preserving as much of their shape as possible. It is Mapshaper's original feature, and the workhorse for reducing large, detailed datasets to a size practical for web maps.
|
|
2170
|
-
|
|
2171
|
-
## Choosing a method
|
|
2172
|
-
|
|
2173
|
-
Mapshaper offers three simplification methods, selectable as flags to `-simplify`:
|
|
2174
|
-
|
|
2175
|
-
- **`dp`** — Douglas-Peucker (also known as Ramer–Douglas–Peucker). Guarantees that simplified lines stay within a fixed distance of the original. Good for stripping excess vertices to reduce file size, but tends to grow visible spikes at high simplification.
|
|
2176
|
-
- **`visvalingam`** — The Visvalingam algorithm. Iteratively removes the point that forms the smallest triangle with its two neighbors.
|
|
2177
|
-
- **`weighted_visvalingam`** (Mapshaper's default) — Visvalingam's effective-area algorithm with a custom weighting that underweights points at sharp angles, so they are removed earlier than in standard Visvalingam. The result is visibly smoother lines and fewer jagged spikes at high simplification.
|
|
2178
|
-
|
|
2179
|
-
Weighted Visvalingam is the default because it has proven to be versatile and effective at reducing detail in highly detailed source data. This method can be effective at generalizing very detailed source files, but be careful that it doesn't remove long, thin geographic features that you want to keep.You can control the amount of weighting used by Weighted Visvalingam with the `weighting=` option (default is 0.7).
|
|
2180
|
-
|
|
2181
|
-
If you are only interested in minimizing file size, Douglas-Peucker is generally the better choice.
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
**Figures**
|
|
2186
|
-
|
|
2187
|
-
Natural Earth 10m coastlines, simplified with modified Visvalingam at 5% point retention.
|
|
2188
|
-

|
|
2189
|
-
|
|
2190
|
-
Same file using Douglas-Peucker, also 5% simplification.
|
|
2191
|
-

|
|
2192
|
-
|
|
2193
|
-
Zoomed-in view of Norwegian coastline at 5% simplification; left: weighted Visvalingam, right: Douglas-Peucker.
|
|
2194
|
-

|
|
2195
|
-
|
|
2196
|
-
## Simplification amount
|
|
2197
|
-
|
|
2198
|
-
On the command line, there are three ways to specify the amount of simplification to apply: `percentage`, `interval`, and `resolution`.
|
|
2199
|
-
|
|
2200
|
-
Percentage is the default (you don't need to type `percentage=`). It gives the percentage of removable vertices to retain, so lower numbers = more simplification.
|
|
2201
|
-
|
|
2202
|
-
```bash
|
|
2203
|
-
mapshaper provinces.geojson -simplify 20% \
|
|
2204
|
-
-o provinces_simplified.geojson
|
|
2205
|
-
```
|
|
2206
|
-
|
|
2207
|
-
The `interval` option takes a distance threshold. With Douglas-Peucker simplification (see below), this is the maximum deviation of the simplified line from the original. With Visvalingam-based methods, `interval=` describes the approximate size of the smallest details in the simplified output.
|
|
2208
|
-
|
|
2209
|
-
```bash
|
|
2210
|
-
mapshaper provinces.geojson -simplify interval=500m \
|
|
2211
|
-
-o provinces_simplified.geojson
|
|
2212
|
-
```
|
|
2213
|
-
|
|
2214
|
-
The `resolution=` option lets you specify the intended display size of your map in SVG units (equivalent to CSS pixels). A larger value retains more detail, since Mapshaper estimates the display size using the full extent of your data. Be careful with this option if your final map will show a smaller geographic area, as the paths may be over-simplified.
|
|
2215
|
-
|
|
2216
|
-
```bash
|
|
2217
|
-
mapshaper provinces.geojson -simplify resolution=800 \
|
|
2218
|
-
-o provinces_simplified.geojson
|
|
2219
|
-
```
|
|
2220
|
-
|
|
2221
|
-
See the [`-simplify` reference](/docs/reference.html.md#-simplify) for the full set of options.
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
## Avoiding shape removal
|
|
2225
|
-
|
|
2226
|
-
At high simplification, small polygons can disappear entirely. Pass `keep-shapes` to `-simplify` (or tick **prevent shape removal** in the web UI's Simplify panel) to retain at least one ring per multipart feature, regardless of how aggressive the simplification is.
|
|
2227
|
-
|
|
2228
|
-
```bash
|
|
2229
|
-
mapshaper provinces.shp -simplify 5% keep-shapes -o provinces.geojson
|
|
2230
|
-
```
|
|
2231
|
-
|
|
2232
|
-
## Spherical vs planar geometry
|
|
2233
|
-
|
|
2234
|
-
By default, Mapshaper simplifies lat/long coordinates on the surface of a sphere, using 3D geometry. This applies a consistent amount of simplification across the whole globe, including near the poles. If your data is in a projected coordinate system, simplification uses 2D planar geometry.
|
|
2235
|
-
|
|
2236
|
-
## Avoiding self-intersections
|
|
2237
|
-
|
|
2238
|
-
Heavy simplification can pull adjacent polygon edges across each other, producing self-intersections. The `-simplify` command detects and tries to remove intersections automatically by rolling back simplification where the intersections occur. In the web UI, you can enable "detect line intersections" on the Display panel to show intersections as red dots. In this mode, you will see a button for repairing intersections caused by simplification.
|
|
2239
|
-
|
|
2240
|
-
The [`-clean`](/docs/reference.html.md#-clean) command will also remove intersections:
|
|
2241
|
-
|
|
2242
|
-
```bash
|
|
2243
|
-
mapshaper provinces.shp -simplify 5% -clean -o provinces.geojson
|
|
2244
|
-
```
|
|
2245
|
-
|
|
2246
|
-
## Simplifying multiple layers consistently
|
|
2247
|
-
|
|
2248
|
-
When you import multiple layers using `-i combine-files`, Mapshaper builds a shared topology. This means that boundaries shared between layers — for example, aligned state and county polygon borders — are simplified identically across both.
|
|
2249
|
-
Without this, the layers would diverge during simplification, creating visible gaps and overlaps where they should align.
|
|
2250
|
-
|
|
2251
|
-
```bash
|
|
2252
|
-
mapshaper -i states.shp counties.shp combine-files \
|
|
2253
|
-
-simplify 10% \
|
|
2254
|
-
-o out/
|
|
2255
|
-
```
|
|
2256
|
-
|
|
2257
|
-
The web app does **not** combine files automatically when you import multiple layers. To get the shared-topology behavior in the web app, tick **with advanced options** in the import dialog and add `combine-files` to the options field.
|
|
2258
|
-
|
|
2259
|
-
---
|
|
2260
|
-
|
|
2261
|
-
<!-- https://mapshaper.org/docs/guides/expressions.html -->
|
|
2262
|
-
|
|
2263
|
-
# JavaScript expressions
|
|
2264
|
-
|
|
2265
|
-
Many Mapshaper commands take a **JS expression** as an argument or option. Expressions let you read and write per-feature attributes, derive new fields, filter records, sort, generate templated commands, and inspect layer-level metadata. The same expression syntax and execution context are reused across commands, so once you've learned the shape of a `-each` expression you can use it almost everywhere.
|
|
2266
|
-
|
|
2267
|
-
```bash
|
|
2268
|
-
mapshaper counties.shp \
|
|
2269
|
-
-each 'STATE_FIPS = COUNTY_FIPS.substr(0, 2),
|
|
2270
|
-
AREA_KM2 = this.area / 1e6' \
|
|
2271
|
-
-o out.shp
|
|
2272
|
-
```
|
|
2273
|
-
|
|
2274
|
-
Expressions are plain JavaScript. They can use any built-in language feature (arithmetic, string methods, conditionals, regex, etc.). Some commands also expect the expression to return a particular kind of value — `-filter` and `-inspect` expect `true` or `false`, `-sort` expects a sort key, `-split` expects a group identifier, and so on.
|
|
2275
|
-
|
|
2276
|
-
## Where expressions appear
|
|
2277
|
-
|
|
2278
|
-
| Command | Expression role | Type |
|
|
2279
|
-
| --- | --- | --- |
|
|
2280
|
-
| [`-each`](/docs/reference.html.md#-each) | Run side-effects per feature, including assignments to data fields | feature |
|
|
2281
|
-
| [`-filter`](/docs/reference.html.md#-filter) | Boolean test, kept if `true` | feature, returns boolean |
|
|
2282
|
-
| [`-sort`](/docs/reference.html.md#-sort) | Returns the sort key for each feature | feature |
|
|
2283
|
-
| [`-inspect`](/docs/reference.html.md#-inspect) | Boolean test, prints matching feature(s) | feature, returns boolean |
|
|
2284
|
-
| [`-split`](/docs/reference.html.md#-split) | Returns the value used to group features into output layers | feature |
|
|
2285
|
-
| [`-subdivide`](/docs/reference.html.md#-subdivide) | Boolean test driving recursive partitioning, can call group functions like `sum()` | feature, returns boolean |
|
|
2286
|
-
| [`-calc` and `calc=` options](/docs/reference.html.md#-calc) | Aggregations across a group of features (`sum`, `count`, `median`, etc.) | calc |
|
|
2287
|
-
| `where=` (on `-filter`, `-each`, `-affine`, `-dashlines`, `-dissolve`, `-innerlines`, `-join`, `-style`, `-symbols`, `-calc`) | Sub-filter applied before the main operation | feature, returns boolean |
|
|
2288
|
-
| `weight=` on `-dissolve`/`-points` | Weighting expression for centroid calculation | feature |
|
|
2289
|
-
| Attribute options on [`-style`](/docs/reference.html.md#-style) and [`-symbols`](/docs/reference.html.md#-symbols) | Most values (`fill=`, `stroke=`, `stroke-width=`, `opacity=`, `r=`, `label-text=`, `dx=`, `dy=`, `font-size=`, etc.) accept either a literal or a JS expression evaluated per feature | feature |
|
|
2290
|
-
| [`-lines where=` and `each=`](/docs/reference.html.md#-lines) | Operates on **pairs** of features either side of a path, exposed as `A` and `B` | pair |
|
|
2291
|
-
| [`-if` / `-elif`](/docs/reference.html.md#-if) | Boolean test on layer-level metadata | layer |
|
|
2292
|
-
| [`-define`](/docs/reference.html.md#-define) | Stores variables and helper functions in a global namespace shared by later expressions | layer |
|
|
2293
|
-
| [`-run`](/docs/reference.html.md#-run) | Generates command strings, with embedded `{...}` template substitutions | template |
|
|
2294
|
-
|
|
2295
|
-
<!-- | `bbox=` on `-rectangles` | Returns a `[xmin, ymin, xmax, ymax]` per feature | feature | -->
|
|
2296
|
-
These five flavors — **feature**, **calc**, **pair**, **layer** and **template** — share most of their context but differ in which variables are available and which functions are in scope.
|
|
2297
|
-
|
|
2298
|
-
## The execution context
|
|
2299
|
-
|
|
2300
|
-
Inside any feature-level expression you have access to:
|
|
2301
|
-
|
|
2302
|
-
- **Field names as bare variables.** Reading a field name returns its value. Assigning to a field name updates the current feature's record (and creates the field on first use). If a field name is not a valid JavaScript identifier (e.g. it contains spaces or starts with a digit), use `d["field name"]` to reference it.
|
|
2303
|
-
- **`this`**, the feature proxy. Provides geometry-derived properties (`this.area`, `this.bbox`, etc.) and read/write access to the feature's `properties`, `geojson` and `coordinates`.
|
|
2304
|
-
- **`d`**, a reference to the data record (the same object as `this.properties`).
|
|
2305
|
-
- **`global`**, an object that persists across commands. Variables created by `-define`, by assignment in a `-calc` expression, or by writing to `global.foo = ...` inside `-each` end up here. Values set by [`-vars`](/docs/reference.html.md#-vars) and [`-defaults`](/docs/reference.html.md#-defaults) live in a separate *templating* scope (read by `{{X}}` substitution) and are **not** visible by bare name in expressions; use `-define` if you want a value reachable from both `{{X}}` and JS expressions.
|
|
2306
|
-
- **`console.log()`** for printing values to stderr while debugging.
|
|
2307
|
-
- **Built-in helpers** (see [Helper functions](#helper-functions) below).
|
|
2308
|
-
- **User helpers** loaded by [`-define`](/docs/reference.html.md#-define), [`-include`](/docs/reference.html.md#-include) or [`-require`](/docs/reference.html.md#-require).
|
|
2309
|
-
|
|
2310
|
-
If a name is referenced but not present in any of the above, JavaScript treats it as `undefined`, *not* an error. This is convenient when chaining expressions across heterogeneous datasets but can mask typos — double-check field names with `mapshaper -info` if a `-filter` returns a suspiciously empty result.
|
|
2311
|
-
|
|
2312
|
-
### Field assignment
|
|
2313
|
-
|
|
2314
|
-
Assigning to a bare name creates or updates a data field on the current feature:
|
|
2315
|
-
|
|
2316
|
-
```bash
|
|
2317
|
-
mapshaper counties.shp -each 'POP_DENSITY = POPULATION / (this.area / 1e6)' -o
|
|
2318
|
-
```
|
|
2319
|
-
|
|
2320
|
-
Bare assignments like `POP_DENSITY = ...` will create the data table if the layer doesn't already have one. Assignments routed through `this.properties.X = ...` or `d.X = ...` only update an existing data table — prefer a bare assignment if you're not sure the layer has one yet.
|
|
2321
|
-
|
|
2322
|
-
To delete a field, use the JS `delete` operator:
|
|
2323
|
-
|
|
2324
|
-
```bash
|
|
2325
|
-
mapshaper states.shp -each 'delete STATE_NAME, delete GEOID' -o
|
|
2326
|
-
```
|
|
2327
|
-
|
|
2328
|
-
To replace the entire record, assign to `this.properties`:
|
|
2329
|
-
|
|
2330
|
-
```bash
|
|
2331
|
-
mapshaper states.shp -each 'this.properties = {FID: this.id, NAME: NAME}' -o
|
|
2332
|
-
```
|
|
2333
|
-
|
|
2334
|
-
### Multiple statements
|
|
2335
|
-
|
|
2336
|
-
Use commas to evaluate multiple sub-expressions. The value of the whole expression is the value of the last sub-expression (relevant for `-filter`, `-sort`, `-split`):
|
|
2337
|
-
|
|
2338
|
-
```bash
|
|
2339
|
-
mapshaper data.csv -each 'A = parseInt(A), B = A * 2, C = A + B'
|
|
2340
|
-
```
|
|
2341
|
-
|
|
2342
|
-
Inside command files, you can also break a long expression across lines with `\`:
|
|
2343
|
-
|
|
2344
|
-
```
|
|
2345
|
-
-each '
|
|
2346
|
-
STATE_FIPS = COUNTY_FIPS.substr(0, 2), \
|
|
2347
|
-
AREA_KM2 = this.area / 1e6, \
|
|
2348
|
-
CENTROID_X = this.centroidX, \
|
|
2349
|
-
CENTROID_Y = this.centroidY
|
|
2350
|
-
'
|
|
2351
|
-
```
|
|
2352
|
-
|
|
2353
|
-
## Feature properties (`this`)
|
|
2354
|
-
|
|
2355
|
-
`this` is a proxy for the current feature. It gives you geometry-derived properties and a few editing affordances. The properties below are read-only unless the description says otherwise.
|
|
2356
|
-
|
|
2357
|
-
### All layer types
|
|
2358
|
-
|
|
2359
|
-
| Name | Description |
|
|
2360
|
-
| --- | --- |
|
|
2361
|
-
| `this.id` | 0-based numerical id of the feature |
|
|
2362
|
-
| `this.layer_name` | Name of the layer (or empty string) |
|
|
2363
|
-
| `this.properties` | Data record. Read/write — assign a new object to replace all attributes. |
|
|
2364
|
-
| `this.layer` | Layer proxy — see [Layer-level properties](#layer-level-properties-thislayer) |
|
|
2365
|
-
| `this.geojson` | GeoJSON Feature (geometry + properties). Read/write — assign a new Feature to replace this one. |
|
|
2366
|
-
| `this.geometry` | Just the GeoJSON geometry. Read/write. |
|
|
2367
|
-
|
|
2368
|
-
### Polygon, polyline and point layers (with geometry)
|
|
2369
|
-
|
|
2370
|
-
| Name | Description |
|
|
2371
|
-
| --- | --- |
|
|
2372
|
-
| `this.partCount` | 1 for single-part features, >1 for multi-part, 0 for null |
|
|
2373
|
-
| `this.isNull` | `true` if `partCount === 0` |
|
|
2374
|
-
| `this.bbox` | `[xmin, ymin, xmax, ymax]` |
|
|
2375
|
-
| `this.width`, `this.height` | Bounding-box width and height |
|
|
2376
|
-
| `this.bboxContainsPoint(x, y)` | `true` if the bbox covers the point |
|
|
2377
|
-
| `this.bboxIntersectsRectangle(a, b, c, d)` | `true` if the bbox overlaps the rectangle |
|
|
2378
|
-
| `this.bboxContainsRectangle(a, b, c, d)` | `true` if the bbox fully contains the rectangle |
|
|
2379
|
-
| `this.bboxContainedByRectangle(a, b, c, d)` | `true` if the bbox is fully inside the rectangle |
|
|
2380
|
-
|
|
2381
|
-
### Polygon-only
|
|
2382
|
-
|
|
2383
|
-
| Name | Description |
|
|
2384
|
-
| --- | --- |
|
|
2385
|
-
| `this.area` | Area in source units (square meters for unprojected lat/long, computed on a sphere) |
|
|
2386
|
-
| `this.planarArea` | Treats lat/long as planar — useful inside expressions that already account for projection |
|
|
2387
|
-
| `this.originalArea` | Area before any `-simplify` was applied |
|
|
2388
|
-
| `this.perimeter` | Perimeter length (meters for unprojected lat/long) |
|
|
2389
|
-
| `this.compactness` | Polsby-Popper compactness ratio (0–1) |
|
|
2390
|
-
| `this.innerPct` | Fraction of the perimeter that is shared with neighboring polygons |
|
|
2391
|
-
| `this.centroidX`, `this.centroidY` | Centroid coordinates (computed from the largest ring; ignores holes) |
|
|
2392
|
-
| `this.innerX`, `this.innerY` | An interior point useful for placing a label or symbol |
|
|
2393
|
-
|
|
2394
|
-
### Polyline-only
|
|
2395
|
-
|
|
2396
|
-
| Name | Description |
|
|
2397
|
-
| --- | --- |
|
|
2398
|
-
| `this.length` | Total length (meters for unprojected lat/long) |
|
|
2399
|
-
|
|
2400
|
-
### Point-only
|
|
2401
|
-
|
|
2402
|
-
| Name | Description |
|
|
2403
|
-
| --- | --- |
|
|
2404
|
-
| `this.coordinates` | The full nested coordinate array, or `null`. Read/write — assign `null` to drop the geometry. |
|
|
2405
|
-
| `this.x`, `this.y` | Coordinates of the first point of the (possibly multi-) feature. Read/write. |
|
|
2406
|
-
|
|
2407
|
-
> **Why it matters for unprojected data:** `this.area` and `this.length` use *spherical* (not planar or ellipsoidal) geometry on lat/long datasets. Results are in square meters / meters and accurate to within ~0.5% for most use cases. If you need ellipsoidal accuracy, project first with `-proj`.
|
|
2408
|
-
|
|
2409
|
-
## Layer-level properties (`this.layer`)
|
|
2410
|
-
|
|
2411
|
-
`this.layer` exposes information about the layer the feature belongs to. Useful in expressions that need to know about other features:
|
|
2412
|
-
|
|
2413
|
-
| Name | Description |
|
|
2414
|
-
| --- | --- |
|
|
2415
|
-
| `this.layer.name` | Layer name |
|
|
2416
|
-
| `this.layer.type` | `'polygon'`, `'polyline'`, `'point'` or `null` |
|
|
2417
|
-
| `this.layer.size` | Feature count |
|
|
2418
|
-
| `this.layer.empty` | `true` if `size === 0` |
|
|
2419
|
-
| `this.layer.bbox` | `[xmin, ymin, xmax, ymax]`, with extra `cx`, `cy`, `width`, `height`, `left`, `right`, `top`, `bottom` properties |
|
|
2420
|
-
| `this.layer.data` | The full array of data records (use sparingly inside per-feature loops) |
|
|
2421
|
-
| `this.layer.field_exists(name)` | Returns `true` if a field exists |
|
|
2422
|
-
| `this.layer.field_type(name)` | Returns `'string'`, `'number'`, `'object'` etc., or `null` |
|
|
2423
|
-
| `this.layer.field_includes(name, value)` | Returns `true` if any record's `name` field equals `value` |
|
|
2424
|
-
|
|
2425
|
-
## Helper functions
|
|
2426
|
-
|
|
2427
|
-
These are always in scope inside feature expressions:
|
|
2428
|
-
|
|
2429
|
-
- `round(num [, decimals])` — Round to N decimal places (default 0). Faster and easier than `Math.round`.
|
|
2430
|
-
- `sprintf(fmt, ...)` — printf-style formatter (uses [printj](https://github.com/SheetJS/printj) syntax).
|
|
2431
|
-
- `format_dms(coord [, fmt])` — Format a number as a degrees/minutes/seconds string. Common formats: `'DD° MM′ SS.SSSSS″ [NS]'`, `'DdMmSs [EW]'`, `'[+-]DDDMM.MMMMM'`, `'[-]DD.DDDDD°'`.
|
|
2432
|
-
- `parse_dms(string [, fmt])` — Parse a DMS string back to a number.
|
|
2433
|
-
- `blend(c1, c2, ...)` — Mix CSS color strings together (returns a hex string).
|
|
2434
|
-
- `console.log(...)` — Write to stderr.
|
|
2435
|
-
|
|
2436
|
-
JavaScript's built-in `Math`, `JSON`, `Number`, `String`, `Array`, `Date`, `Object` etc. are all available. Node-specific globals like `process`, `require` and `setTimeout` are not.
|
|
2437
|
-
|
|
2438
|
-
## Calc expressions
|
|
2439
|
-
|
|
2440
|
-
`-calc` and any command's `calc=` option use the same context as `-each` plus a set of *aggregate* functions that operate over the entire group of features (or the entire layer for `-calc`). Each aggregate function takes a per-feature expression and reduces it to a single value across the group.
|
|
2441
|
-
|
|
2442
|
-
| Function | Description |
|
|
2443
|
-
| --- | --- |
|
|
2444
|
-
| `count()` | Number of records in the collection |
|
|
2445
|
-
| `sum(<expr>)` | Sum of the per-feature expression |
|
|
2446
|
-
| `mean(<expr>)`, `average(<expr>)` | Arithmetic mean |
|
|
2447
|
-
| `median(<expr>)` | Median value |
|
|
2448
|
-
| `mode(<expr>)` | Most common value (first one wins ties) |
|
|
2449
|
-
| `min(<expr>)`, `max(<expr>)` | Extremes |
|
|
2450
|
-
| `quartile1(<expr>)`, `quartile2(<expr>)`, `quartile3(<expr>)` | Quartiles |
|
|
2451
|
-
| `iqr(<expr>)` | Interquartile range |
|
|
2452
|
-
| `quantile(<expr>, <pct>)` | Arbitrary percentile (0–1) |
|
|
2453
|
-
| `collect(<expr>)` | Array of all values (preserves order) |
|
|
2454
|
-
| `collectIds()` | Array of feature ids |
|
|
2455
|
-
| `first(<expr>)`, `last(<expr>)` | First / last value seen |
|
|
2456
|
-
| `every(<expr>)`, `some(<expr>)` | Boolean reductions |
|
|
2457
|
-
|
|
2458
|
-
Argument expressions use the same syntax as `-each`, so per-feature properties and helpers are available:
|
|
2459
|
-
|
|
2460
|
-
```bash
|
|
2461
|
-
mapshaper counties.shp \
|
|
2462
|
-
-calc 'TOTAL_POP = sum(POP),
|
|
2463
|
-
MEAN_AREA_KM2 = sum(this.area / 1e6) / count(),
|
|
2464
|
-
TOP_DENSITY = max(POP / this.area)'
|
|
2465
|
-
```
|
|
2466
|
-
|
|
2467
|
-
Calc expressions can also use assignments to expose values to subsequent commands via the `global` namespace (see [Sharing state across commands](#sharing-state-across-commands) below).
|
|
2468
|
-
|
|
2469
|
-
## Pair expressions (`A` and `B`)
|
|
2470
|
-
|
|
2471
|
-
The `-lines where=` and `each=` options operate on path segments shared between two adjacent features. Inside these expressions:
|
|
2472
|
-
|
|
2473
|
-
- `A` is the feature on one side of the path
|
|
2474
|
-
- `B` is the feature on the other side, or `null` for outer boundaries
|
|
2475
|
-
|
|
2476
|
-
Both `A` and `B` give you the full set of feature properties (`A.properties`, `A.area`, `A.id`, etc.).
|
|
2477
|
-
|
|
2478
|
-
```bash
|
|
2479
|
-
# Keep only inner boundaries between two different states
|
|
2480
|
-
mapshaper counties.shp \
|
|
2481
|
-
-lines where='B && A.STATE != B.STATE' \
|
|
2482
|
-
-o state-borders.shp
|
|
2483
|
-
```
|
|
2484
|
-
|
|
2485
|
-
## Layer-level expressions (`-if`, `-define`)
|
|
2486
|
-
|
|
2487
|
-
The `-if` family and `-define` evaluate against the *current command's target layer(s)*, not per feature. The context exposes:
|
|
2488
|
-
|
|
2489
|
-
- `target` — the proxy for the single target layer (only set when there's exactly one target)
|
|
2490
|
-
- `targets` — an array-like of layer proxies, also indexable by name (`targets.states`)
|
|
2491
|
-
- `layer_name`, `data`, `type`, `size`, `empty`, `bbox`
|
|
2492
|
-
- `field_exists(name)`, `field_type(name)`, `field_includes(name, value)`
|
|
2493
|
-
- `layer_exists(name [, geometry_type])`
|
|
2494
|
-
- `file_exists(path)`
|
|
2495
|
-
- `global` — the shared variable namespace
|
|
2496
|
-
|
|
2497
|
-
```bash
|
|
2498
|
-
mapshaper data.csv \
|
|
2499
|
-
-calc 'N = count()' \
|
|
2500
|
-
-if 'global.N < 5' -print 'LOW SAMPLE SIZE, STOPPING' -stop -endif
|
|
2501
|
-
```
|
|
2502
|
-
|
|
2503
|
-
Each entry in `targets` exposes useful summary stats from `-info`: `layer_name`, `feature_count`, `null_shape_count`, `null_data_count`, `bbox`, `proj4`. Reading `targets[0].geojson` returns the layer as a GeoJSON FeatureCollection; assigning to it replaces the layer with the FeatureCollection you provide.
|
|
2504
|
-
|
|
2505
|
-
## Template expressions (`-run`)
|
|
2506
|
-
|
|
2507
|
-
`-run` accepts either a path to a [command file](/docs/reference.html.md#command-files) or a string containing one or more curly-brace template expressions. Each `{...}` is evaluated as a JS expression and substituted into the resulting command string before Mapshaper parses it.
|
|
2508
|
-
|
|
2509
|
-
```bash
|
|
2510
|
-
# Project to a transverse Mercator centred on the layer
|
|
2511
|
-
mapshaper -i country.shp -require projection.js \
|
|
2512
|
-
-run '-proj {tmerc(target.bbox)}' -o
|
|
2513
|
-
```
|
|
2514
|
-
|
|
2515
|
-
Inside the curly braces you have:
|
|
2516
|
-
|
|
2517
|
-
- `target` and `targets` (same as `-if`)
|
|
2518
|
-
- `io.ifile(filename, data)` — spill data to a temp file and yield its path, useful for piping computed JSON back into `-i`
|
|
2519
|
-
- Anything loaded by `-require` or `-define`
|
|
2520
|
-
|
|
2521
|
-
Bare function calls outside curly braces are also evaluated directly, so `-run 'tmerc(target.bbox)'` works the same as `-run '{tmerc(target.bbox)}'` when the function name was loaded via `-require`.
|
|
2522
|
-
|
|
2523
|
-
## Loading helpers
|
|
2524
|
-
|
|
2525
|
-
Three commands extend the expression context with your own variables and helpers:
|
|
2526
|
-
|
|
2527
|
-
- [`-define`](/docs/reference.html.md#-define) takes an inline JS expression and stores any assignments on the global namespace. Good for one-liners.
|
|
2528
|
-
- [`-include`](/docs/reference.html.md#-include) loads a `.js` file containing a single object literal; each property of that object becomes a variable in subsequent expressions.
|
|
2529
|
-
- [`-require`](/docs/reference.html.md#-require) loads an installed npm module or a local module file. With `alias=foo` the module is bound to that name; without an alias, the module's exported names are added directly to the context.
|
|
2530
|
-
|
|
2531
|
-
```bash
|
|
2532
|
-
mapshaper data.json \
|
|
2533
|
-
-require ./helpers.mjs \
|
|
2534
|
-
-each 'displayname = formatName(d)' \
|
|
2535
|
-
-o data.json
|
|
2536
|
-
```
|
|
2537
|
-
|
|
2538
|
-
```bash
|
|
2539
|
-
mapshaper -define 'KM_PER_MILE = 1.609344' \
|
|
2540
|
-
routes.geojson \
|
|
2541
|
-
-each 'KM = MILES * global.KM_PER_MILE' \
|
|
2542
|
-
-o
|
|
2543
|
-
```
|
|
2544
|
-
|
|
2545
|
-
## Sharing state across commands
|
|
2546
|
-
|
|
2547
|
-
Mapshaper has two scopes for values that persist between commands. They share a name lookup for `{{X}}` substitution but are otherwise independent.
|
|
2548
|
-
|
|
2549
|
-
- **Expression scope (`global`)** — written by `-define`, `-include`, `-require`, `-colorizer`, and `-calc` assignments (e.g. `N = count()`) or any `global.foo = ...` inside `-each`. Values can be any JavaScript value (numbers, strings, functions, objects). Read by JS expressions as bare names, and as `global.X` everywhere. `{{X}}` substitution falls back to this scope, so `-define base = "out"` → `-o {{base}}.geojson` and `-calc 'N = count()'` → `-if '{{N}} > 100'` work as you'd expect.
|
|
2550
|
-
- **Templating scope** — written by [`-vars`](/docs/reference.html.md#-vars) and [`-defaults`](/docs/reference.html.md#-defaults). Values must be primitives (string / number / boolean / null) and are validated at write time. Read by `{{X}}` substitution; `{{X}}` checks the templating scope first, then falls back to the expression scope. **Not** visible by bare name in JS expressions — that's deliberate, so a string set by `-vars N=5` can't silently coerce into arithmetic.
|
|
2551
|
-
|
|
2552
|
-
If you want one value usable in both contexts, set it once with `-define`. If you only need it in command strings, use `-vars` (or `-defaults` for command-file overridable defaults).
|
|
2553
|
-
|
|
2554
|
-
```bash
|
|
2555
|
-
mapshaper counties.shp \
|
|
2556
|
-
-calc 'BIG = count("POP > 1000000")' \
|
|
2557
|
-
-if 'global.BIG > 0' \
|
|
2558
|
-
-filter 'POP > 1000000' \
|
|
2559
|
-
-o big-counties.shp \
|
|
2560
|
-
-endif
|
|
2561
|
-
```
|
|
2562
|
-
|
|
2563
|
-
## Common pitfalls
|
|
2564
|
-
|
|
2565
|
-
- **Quoting.** In bash/zsh, wrap expressions in single quotes so the shell doesn't expand `!`, `$` or backticks. In Windows `cmd.exe`, use double quotes and escape inner quotes with backslashes. In PowerShell, prefer single quotes, or escape `$` with a backtick.
|
|
2566
|
-
- **Type coercion from CSVs.** Numeric-looking strings in CSVs are parsed as numbers by default; identifier-like strings (FIPS, ZIP) need `string-fields=` on `-i` to preserve leading zeros. See [CSV practical notes](/docs/formats/csv.html.md#practical-notes).
|
|
2567
|
-
- **Field name collisions.** A field called `area`, `length`, `id` etc. shadows the built-in property of the same name. Mapshaper prints a warning. Either rename the field with `-rename-fields`, or read the property via `this.area` rather than the bare name.
|
|
2568
|
-
- **Lat/long area surprises.** `this.area` on an unprojected polygon returns *square meters on a sphere*, not square degrees. To get square kilometres, divide by `1e6`. To get planar square degrees (e.g. for sanity checks), use `this.planarArea`.
|
|
2569
|
-
- **Centroids ignore holes.** `this.centroidX/Y` is the centroid of the largest ring. For a labelling point that's guaranteed inside the polygon, use `innerX`/`innerY`.
|
|
2570
|
-
- **`-each` doesn't return values.** Its expression is evaluated for side-effects only. Use `-filter`, `-sort` or `-calc` if you want the return value to drive behavior.
|
|
2571
|
-
- **Reserved names.** `this`, `d`, `_`, `global`, `console`, `target`, `targets` and the helper function names listed above are not safe to use as field names.
|
|
2572
|
-
- **Auto-vivification of fields.** Assigning to a name that isn't a known field creates a new field on every record. If you only want to set a field on *some* records, wrap it in a conditional and assign explicit `null` for the others, otherwise downstream readers may see `undefined` instead of a real null.
|
|
2573
|
-
|
|
2574
|
-
## Examples
|
|
2575
|
-
|
|
2576
|
-
```bash
|
|
2577
|
-
# Add two derived fields
|
|
2578
|
-
mapshaper counties.shp \
|
|
2579
|
-
-each 'STATE_FIPS = COUNTY_FIPS.substr(0, 2),
|
|
2580
|
-
AREA_KM2 = round(this.area / 1e6, 2)' \
|
|
2581
|
-
-o out.shp
|
|
2582
|
-
|
|
2583
|
-
# Drop features outside a date window
|
|
2584
|
-
mapshaper events.csv \
|
|
2585
|
-
-filter 'new Date(DATE) >= new Date("2020-01-01")' \
|
|
2586
|
-
-o recent.csv
|
|
2587
|
-
|
|
2588
|
-
# Sort polygons largest-first
|
|
2589
|
-
mapshaper countries.geojson \
|
|
2590
|
-
-sort '-this.area' \
|
|
2591
|
-
-o sorted.geojson
|
|
2592
|
-
|
|
2593
|
-
# Look up one feature
|
|
2594
|
-
mapshaper states.geojson -inspect 'NAME == "Delaware"'
|
|
2595
|
-
|
|
2596
|
-
# Aggregate stats during a dissolve
|
|
2597
|
-
mapshaper counties.shp \
|
|
2598
|
-
-dissolve STATE calc='N = count(),
|
|
2599
|
-
POP = sum(POP),
|
|
2600
|
-
MEDIAN_INC = median(MEDIAN_INC)' \
|
|
2601
|
-
-o states.shp
|
|
2602
|
-
|
|
2603
|
-
# Conditional pipeline based on a calc result
|
|
2604
|
-
mapshaper data.csv \
|
|
2605
|
-
-calc 'N = count()' \
|
|
2606
|
-
-if 'global.N == 0' -stop -endif \
|
|
2607
|
-
-o data.csv
|
|
2608
|
-
|
|
2609
|
-
# Filter shared boundaries
|
|
2610
|
-
mapshaper counties.shp \
|
|
2611
|
-
-lines where='B && A.STATE != B.STATE' \
|
|
2612
|
-
-o state-borders.shp
|
|
2613
|
-
|
|
2614
|
-
# Per-feature styling: circle radius from POP, fill from an expression
|
|
2615
|
-
mapshaper cities.geojson \
|
|
2616
|
-
-style r='Math.sqrt(POP) / 40' \
|
|
2617
|
-
fill='POP > 1e6 ? "#c33" : "#39c"' \
|
|
2618
|
-
opacity=0.7 \
|
|
2619
|
-
-o cities.svg
|
|
2620
|
-
|
|
2621
|
-
# Project to a layer-specific CRS
|
|
2622
|
-
mapshaper -i country.shp -require ./projection.js \
|
|
2623
|
-
-run '-proj {tmerc(target.bbox)}' -o
|
|
2624
|
-
```
|
|
2625
|
-
|
|
2626
|
-
## See also
|
|
2627
|
-
|
|
2628
|
-
- [`-each`](/docs/reference.html.md#-each) — the canonical feature-expression command
|
|
2629
|
-
- [`-filter`](/docs/reference.html.md#-filter)
|
|
2630
|
-
- [`-calc`](/docs/reference.html.md#-calc)
|
|
2631
|
-
- [`-define`](/docs/reference.html.md#-define), [`-include`](/docs/reference.html.md#-include), [`-require`](/docs/reference.html.md#-require)
|
|
2632
|
-
- [`-run`](/docs/reference.html.md#-run)
|
|
2633
|
-
- [Basics](/docs/examples/basics.html.md) — recipes that put expressions to work
|
|
2634
|
-
|
|
2635
|
-
---
|
|
2636
|
-
|
|
2637
|
-
<!-- https://mapshaper.org/docs/guides/projections.html -->
|
|
2638
|
-
|
|
2639
|
-
# Projections
|
|
2640
|
-
|
|
2641
|
-
Mapshaper's [`-proj`](/docs/reference.html.md#-proj) command reprojects a dataset from one coordinate reference system (CRS) to another, using a JavaScript port of the [PROJ](https://proj.org/) coordinate transformation library.
|
|
2642
|
-
|
|
2643
|
-
**Examples**
|
|
2644
|
-
|
|
2645
|
-
```bash
|
|
2646
|
-
# Project a Shapefile to UTM zone 11N using a PROJ string
|
|
2647
|
-
mapshaper nevada.shp -proj +proj=utm +zone=11 -o
|
|
2648
|
-
|
|
2649
|
-
# Convert a projected Shapefile to WGS84 — the following are equivalent:
|
|
2650
|
-
mapshaper nyc.shp -proj EPSG:4326 -o
|
|
2651
|
-
mapshaper nyc.shp -proj wgs84 -o
|
|
2652
|
-
mapshaper nyc.shp -proj +proj=longlat +datum=WGS84 -o
|
|
2653
|
-
|
|
2654
|
-
# Composite projection for U.S. maps with Alaska, Hawaii, and Puerto Rico insets
|
|
2655
|
-
mapshaper us-states.shp -proj albersusa +PR -o
|
|
2656
|
-
```
|
|
2657
|
-
|
|
2658
|
-
## Forms of CRS notation
|
|
2659
|
-
|
|
2660
|
-
`-proj` accepts a CRS in any of three forms.
|
|
2661
|
-
|
|
2662
|
-
**PROJ strings** are sequences of `+key=value` parameters. They are the lowest-level form and expose the full set of options for each projection. Parameters that have sensible defaults (datum, units, false easting/northing) can usually be omitted.
|
|
2663
|
-
|
|
2664
|
-
```bash
|
|
2665
|
-
mapshaper data.shp -proj +proj=lcc +lat_1=33 +lat_2=45 +lat_0=39 +lon_0=-96 -o
|
|
2666
|
-
```
|
|
2667
|
-
|
|
2668
|
-
**EPSG codes** are numeric identifiers from the [EPSG registry](https://epsg.io/). Thousands of national and regional coordinate systems have EPSG codes, making them a compact and unambiguous way to specify a CRS.
|
|
2669
|
-
|
|
2670
|
-
```bash
|
|
2671
|
-
mapshaper data.shp -proj EPSG:3857 -o # Web Mercator
|
|
2672
|
-
mapshaper data.shp -proj EPSG:32611 -o # UTM zone 11N (WGS84)
|
|
2673
|
-
```
|
|
2674
|
-
|
|
2675
|
-
**Aliases** are short names for common projections. Run `mapshaper -projections` to print the full list. The built-in aliases are:
|
|
2676
|
-
|
|
2677
|
-
| Alias | Equivalent PROJ string |
|
|
2678
|
-
|---|---|
|
|
2679
|
-
| `wgs84` | `+proj=longlat +datum=WGS84` |
|
|
2680
|
-
| `webmercator` | `+proj=merc +a=6378137 +b=6378137` |
|
|
2681
|
-
| `robinson` | `+proj=robin +datum=WGS84` |
|
|
2682
|
-
| `albersusa` | [Composite U.S. projection](#albersusa) (see below) |
|
|
2683
|
-
|
|
2684
|
-
You can also use a bare PROJ projection name (without `+proj=`) as shorthand when no extra parameters are required:
|
|
2685
|
-
|
|
2686
|
-
```bash
|
|
2687
|
-
mapshaper world.shp -proj robin -o
|
|
2688
|
-
```
|
|
2689
|
-
|
|
2690
|
-
## Auto-fitted parameters
|
|
2691
|
-
|
|
2692
|
-
For some conic and cylindrical projections, you can supply just the projection name and Mapshaper will calculate suitable parameters from the extent of the data. This is useful when you want a locally appropriate projection without looking up specific values.
|
|
2693
|
-
|
|
2694
|
-
For **Lambert Conformal Conic** (`lcc`) and **Albers Equal Area Conic** (`aea`), Mapshaper calculates the central meridian (`lon_0`) and two standard parallels (`lat_1`, `lat_2`) using the one-sixth rule applied to the data's bounding box.
|
|
2695
|
-
|
|
2696
|
-
For **Transverse Mercator** (`tmerc`, `etmerc`), it calculates the central meridian and latitude of origin (`lon_0`, `lat_0`) from the center of the bounding box.
|
|
2697
|
-
|
|
2698
|
-
```bash
|
|
2699
|
-
# Mapshaper fills in lon_0, lat_1, lat_2 based on the data extent
|
|
2700
|
-
mapshaper region.geojson -proj lcc -o region_lcc.geojson
|
|
2701
|
-
|
|
2702
|
-
# Equivalent — Mapshaper fills in lon_0 and lat_0
|
|
2703
|
-
mapshaper region.geojson -proj tmerc -o region_tmerc.geojson
|
|
2704
|
-
```
|
|
2705
|
-
|
|
2706
|
-
When Mapshaper auto-fits parameters, it prints the expanded PROJ string so you can see exactly what was applied — for example: `Converted "lcc" to "+proj=lcc +lon_0=-95.5 +lat_1=30.17 +lat_2=44.83"`. You can copy that string and use it explicitly if you need reproducible output.
|
|
2707
|
-
|
|
2708
|
-
## albersusa
|
|
2709
|
-
|
|
2710
|
-
`albersusa` is a Mapshaper-specific composite projection for maps of the United States. It is not part of the PROJ library. It applies Albers Equal Area Conic to the contiguous 48 states, then tiles Alaska (scaled down) and Hawaii as insets in the lower-left corner of the map.
|
|
2711
|
-
|
|
2712
|
-
```bash
|
|
2713
|
-
mapshaper us-states.shp -proj albersusa -o
|
|
2714
|
-
```
|
|
2715
|
-
|
|
2716
|
-
Two optional flags add insets for outlying territories:
|
|
2717
|
-
|
|
2718
|
-
- `+PR` — Puerto Rico
|
|
2719
|
-
- `+VI` — U.S. Virgin Islands (placed alongside Puerto Rico)
|
|
2720
|
-
|
|
2721
|
-
```bash
|
|
2722
|
-
mapshaper us-states.shp -proj albersusa +PR +VI -o
|
|
2723
|
-
```
|
|
2724
|
-
|
|
2725
|
-
The position, scale, rotation, and other properties of each inset can be overridden with named parameters if the defaults do not suit your map. See the [`-proj` reference](/docs/reference.html.md#-proj) for the full option syntax.
|
|
2726
|
-
|
|
2727
|
-
## Finding CRS definitions
|
|
2728
|
-
|
|
2729
|
-
Several websites provide PROJ strings and EPSG codes for coordinate systems worldwide:
|
|
2730
|
-
|
|
2731
|
-
- **[EPSG.io](https://epsg.io/)** — search by place name, CRS name, or EPSG code. Each entry shows the PROJ string and WKT definition and lets you preview the projection on a map.
|
|
2732
|
-
- **[SpatialReference.org](https://spatialreference.org/)** — similar database built directly from the PROJ library. Good for browsing the full set of supported systems.
|
|
2733
|
-
- **[PROJ documentation](https://proj.org/operations/projections/)** — reference for every projection in PROJ, including all supported parameters. Mapshaper's JavaScript port supports most but not all of them; run `mapshaper -projections` to see the exact list.
|
|
2734
|
-
|
|
2735
|
-
## Coordinate system quirks and limitations
|
|
2736
|
-
|
|
2737
|
-
- GeoJSON and TopoJSON files are assumed to use WGS84 when their bounding boxes fall within the normal range for decimal degree coordinates.
|
|
2738
|
-
- Mapshaper does not support coordinate transformations that require grid-shift files (for example, NAD27 → WGS84). If a transformation silently fails, this is the likely cause.
|
|
2739
|
-
- Projections that can only represent part of the globe — including orthographic (`ortho`), near-side perspective (`nsper`, `geos`), gnomonic (`gnom`), stereographic (`stere`), and Lambert Azimuthal Equal-Area (`laea`) — automatically clip input data to the projection's valid extent before projecting. This prevents distorted or invalid geometry from coordinates outside the visible area.
|
|
2740
|
-
- For projections that introduce significant curvature along straight lines, add the `densify` option to interpolate extra vertices along long segments:
|
|
2741
|
-
|
|
2742
|
-
```bash
|
|
2743
|
-
mapshaper data.shp -proj +proj=ortho +lat_0=45 +lon_0=-100 densify -o
|
|
2744
|
-
```
|
|
2745
|
-
|
|
2746
|
-
- When `-proj` targets a layer, all topologically related layers (those sharing the same geometry) are also reprojected. To reproject all layers, use `target=*`.
|
|
2747
|
-
- The `init=` option is available for files whose source CRS is unknown and cannot be inferred from a `.prj` file. Shapefiles normally carry a `.prj` sidecar; GeoJSON and TopoJSON are assumed to be WGS84 when their coordinates fall within the standard lat/long range.
|
|
2748
|
-
|
|
2749
|
-
## The -proj command
|
|
2750
|
-
|
|
2751
|
-
See the [`-proj` reference](/docs/reference.html.md#-proj) for the full list of options.
|
|
2752
|
-
|
|
2753
|
-
---
|
|
2754
|
-
|
|
2755
|
-
<!-- https://mapshaper.org/docs/guides/topology.html -->
|
|
2756
|
-
|
|
2757
|
-
# Topology and cleaning
|
|
2758
|
-
|
|
2759
|
-
Shapefile and GeoJSON are non-topological formats — they don't record the spatial relationships between adjacent polygons or intersecting polylines. Each polygon is just a list of coordinates; whether two polygons share an edge has to be detected.
|
|
2760
|
-
|
|
2761
|
-
Mapshaper detects topology on import by identifying coordinates that are exactly shared between features. This is what makes operations like simplification, dissolving and clipping work correctly: when two polygons share an edge, the shared path (or "arc") is stored once and edited once.
|
|
2762
|
-
|
|
2763
|
-
But coordinates that "should be" identical often aren't. Source datasets routinely contain misalignments (tiny gaps or overlaps between adjacent polygons) that defeat exact-match topology detection. The result is that what looks like a clean boundary turns into duplicated, slightly-offset arcs — and simplification, dissolving and clipping all start to misbehave.
|
|
2764
|
-
|
|
2765
|
-
## Snapping
|
|
2766
|
-
|
|
2767
|
-
The simplest fix is to ask Mapshaper to snap nearby vertices together at import time.
|
|
2768
|
-
|
|
2769
|
-
In the **command line**, pass the `snap` flag to `-i`:
|
|
2770
|
-
|
|
2771
|
-
```bash
|
|
2772
|
-
mapshaper countries.shp snap -dissolve CONTINENT -o continents.shp
|
|
2773
|
-
```
|
|
2774
|
-
|
|
2775
|
-
In the **web app**, tick "snap vertices" in the import dialog (open the import options with the **with advanced options** checkbox).
|
|
2776
|
-
|
|
2777
|
-
By default, snapping uses an automatic threshold of about 0.0025× the average segment length, which is designed to catch misalignments caused by floating-point rounding. To set an explicit snapping distance, use `snap-interval=`:
|
|
2778
|
-
|
|
2779
|
-
```bash
|
|
2780
|
-
mapshaper countries.shp snap-interval=0.0001 -o cleaned.shp
|
|
2781
|
-
```
|
|
2782
|
-
|
|
2783
|
-
## Cleaning
|
|
2784
|
-
|
|
2785
|
-
Snapping handles slightly offset pairs of vertices, but doesn't help much when adjacent polygons have small overlapping regions or gaps along their shared boundaries. The [`-clean`](/docs/reference.html.md#-clean) command repairs these by recomputing the polygon mosaic and snapping geometry that's nearly identical:
|
|
2786
|
-
|
|
2787
|
-
```bash
|
|
2788
|
-
mapshaper countries.shp -clean -o cleaned.shp
|
|
2789
|
-
```
|
|
2790
|
-
|
|
2791
|
-
`-clean` accepts a `gap-fill-area=` option to control how aggressively gaps are filled, and a `sliver-control=` setting for handling sliver polygons. [`-dissolve`](/docs/reference.html.md#-dissolve) runs an equivalent repair by default, so explicitly running `-clean` is mainly useful when you want clean output without dissolving anything.
|
|
2792
|
-
|
|
2793
|
-
In the web app, `-clean` runs from the **Console** the same way as on the CLI — the leading `-` is optional, e.g. just `clean gap-fill-area=100`.
|
|
2794
|
-
|
|
2795
|
-
## Dissolving with topology repair
|
|
2796
|
-
|
|
2797
|
-
[`-dissolve`](/docs/reference.html.md#-dissolve) repairs topology automatically. To skip the repair pass (faster, but only safe when you trust the input topology), pass `no-repair` — Mapshaper will then warn if it detects segment intersections in the input.
|
|
2798
|
-
|
|
2799
|
-
```bash
|
|
2800
|
-
mapshaper counties.shp -dissolve STATE_FIPS -o states.shp
|
|
2801
|
-
```
|
|
2802
|
-
|
|
2803
|
-
## Detecting line intersections
|
|
2804
|
-
|
|
2805
|
-
The web app can highlight self-intersections in your data: open the **Display** panel and tick "detect line intersections". Intersections often indicate either a topology error in the source data or self-intersections introduced by simplification — the **Repair** button at the top-left of the map attempts to fix the latter.
|
|
2806
|
-
|
|
2807
|
-
On the command line, [`-clean`](/docs/reference.html.md#-clean) and [`-dissolve`](/docs/reference.html.md#-dissolve) both detect and fix intersections.
|
|
2808
|
-
|
|
2809
|
-
## Notes on common sources of topology errors
|
|
2810
|
-
|
|
2811
|
-
A few patterns to watch out for:
|
|
2812
|
-
|
|
2813
|
-
- **`.shp` files exported from older GIS tools.** Some pipelines round coordinates inconsistently between adjacent features, producing systematic misalignments. Importing with `snap` is usually enough.
|
|
2814
|
-
- **Older versions of ArcGIS's dissolve tool** have been observed to produce topology errors when dissolving a Shapefile that hasn't first been added to a Geodatabase. If you're starting from such output, run it through `mapshaper input.shp -clean -o cleaned.shp` to repair before further processing.
|
|
2815
|
-
|
|
2816
|
-
---
|
|
2817
|
-
|
|
2818
|
-
<!-- https://mapshaper.org/docs/guides/combining-layers.html -->
|
|
2819
|
-
|
|
2820
|
-
# Tutorial: combining two layers in the web UI
|
|
2821
|
-
|
|
2822
|
-
> Originally contributed by Amanda Hickman to the project wiki.
|
|
2823
|
-
|
|
2824
|
-
This walkthrough shows how to combine and prune two boundary layers to produce a single GeoJSON file you can use as a custom basemap (for example, in [Datawrapper](https://www.datawrapper.de/)).
|
|
2825
|
-
|
|
2826
|
-
The example builds a basemap of the San Francisco Bay Area, with both county boundaries and the cities ("places") inside them.
|
|
2827
|
-
|
|
2828
|
-
The walkthrough uses the web app at [mapshaper.org](/), driving the workflow from the **Console**. The same commands work on the CLI — chain them together with leading `-` prefixes (so `clip bayarea_county` becomes `-clip bayarea_county`) and connect them with backslash line continuations.
|
|
2829
|
-
|
|
2830
|
-
## The starting data
|
|
2831
|
-
|
|
2832
|
-
We'll use two source files:
|
|
2833
|
-
|
|
2834
|
-
- A county boundary file from the California Open Data Portal — or, in this case, a [version clipped to the shoreline](https://geodata.lib.berkeley.edu/catalog/ark28722-s7hs4j) from UC Berkeley's Geo Data Commons. The shoreline-clipped version reads more naturally on a map than the legal-boundary version, which extends into the bay.
|
|
2835
|
-
- A statewide [places boundary file](https://geodata.lib.berkeley.edu/catalog/ark28722-s7bp4z) for city boundaries.
|
|
2836
|
-
|
|
2837
|
-
The legal county boundaries from the Census TIGER files extend straight across the water — San Francisco even reaches out to include the Farallon Islands:
|
|
2838
|
-
|
|
2839
|
-

|
|
2840
|
-
|
|
2841
|
-
The Berkeley library's version is clipped to the shoreline, so the bay shows as empty space between the counties:
|
|
2842
|
-
|
|
2843
|
-

|
|
2844
|
-
|
|
2845
|
-
Download both as `.zip` files.
|
|
2846
|
-
|
|
2847
|
-
## Open them in Mapshaper
|
|
2848
|
-
|
|
2849
|
-
Drag the two `.zip` files onto [mapshaper.org](/), or run the web app locally with `mapshaper-gui`. You'll end up with two layers loaded into the session.
|
|
2850
|
-
|
|
2851
|
-
## Set the projection
|
|
2852
|
-
|
|
2853
|
-
Datawrapper expects WGS84 coordinates. Open the **Console** (top-right of the header) and run, with each layer selected:
|
|
2854
|
-
|
|
2855
|
-
```
|
|
2856
|
-
proj wgs84
|
|
2857
|
-
```
|
|
2858
|
-
|
|
2859
|
-
You'll need to run this once per layer — switch which layer is selected in the layer panel between runs.
|
|
2860
|
-
|
|
2861
|
-
→ See the [`-proj` reference](/docs/reference.html.md#-proj).
|
|
2862
|
-
|
|
2863
|
-
## Clip the places layer to the Bay Area
|
|
2864
|
-
|
|
2865
|
-
The places layer covers the whole state. We only want places inside the Bay Area counties. With the places layer selected:
|
|
2866
|
-
|
|
2867
|
-
```
|
|
2868
|
-
clip bayarea_county
|
|
2869
|
-
```
|
|
2870
|
-
|
|
2871
|
-
→ See the [`-clip` reference](/docs/reference.html.md#-clip).
|
|
2872
|
-
|
|
2873
|
-
Alternatively, if the places layer has a `COUNTY` attribute, a filter expression works too:
|
|
2874
|
-
|
|
2875
|
-
```
|
|
2876
|
-
filter '["Marin", "Contra Costa", "Alameda",
|
|
2877
|
-
"San Francisco", "Santa Clara", "San Mateo"].includes(COUNTY)'
|
|
2878
|
-
```
|
|
2879
|
-
|
|
2880
|
-
→ See the [`-filter` reference](/docs/reference.html.md#-filter).
|
|
2881
|
-
|
|
2882
|
-
## Merge the layers
|
|
2883
|
-
|
|
2884
|
-
The two layers can now be combined into a single layer with [`-merge-layers`](/docs/reference.html.md#-merge-layers):
|
|
2885
|
-
|
|
2886
|
-
```
|
|
2887
|
-
merge-layers target=bayarea_county,california_place_clipped force
|
|
2888
|
-
```
|
|
2889
|
-
|
|
2890
|
-
The `force` flag is needed because the two layers have different attribute schemas; without it, Mapshaper refuses to merge layers whose fields don't match.
|
|
2891
|
-
|
|
2892
|
-
## Export
|
|
2893
|
-
|
|
2894
|
-
Open the **Export** panel (top-right of the header), pick **GeoJSON** as the format, and save the merged layer. You can now upload that file to Datawrapper (or any other tool that accepts GeoJSON) as a custom basemap.
|
|
2895
|
-
|
|
2896
|
-
## What you've learned
|
|
2897
|
-
|
|
2898
|
-
- How to load multiple data files into one Mapshaper session.
|
|
2899
|
-
- How to apply a projection inside the web UI's console.
|
|
2900
|
-
- How to clip one layer by another.
|
|
2901
|
-
- How to merge two layers into a single layer for export.
|
|
2902
|
-
|
|
2903
|
-
For more on layers and how Mapshaper organizes multi-layer datasets, see [The command-line tool](/docs/essentials/command-line.html.md#working-with-layers).
|
|
2904
|
-
|
|
2905
|
-
---
|
|
2906
|
-
|
|
2907
|
-
<!-- https://mapshaper.org/docs/guides/programmatic.html -->
|
|
2908
|
-
|
|
2909
|
-
# Using Mapshaper from Node.js
|
|
2910
|
-
|
|
2911
|
-
This page is for developers who want to use Mapshaper's geoprocessing functions inside their own programs — either by shelling out to the CLI from a build tool, or by calling Mapshaper's API from Node.js code directly.
|
|
2912
|
-
|
|
2913
|
-
## Calling the CLI from a build tool
|
|
2914
|
-
|
|
2915
|
-
The simplest way to script Mapshaper is to invoke the `mapshaper` (or `mapshaper-xl`) command from `make` or a shell script.
|
|
2916
|
-
|
|
2917
|
-
Example `Makefile` target:
|
|
2918
|
-
|
|
2919
|
-
```make
|
|
2920
|
-
europe.topojson: shp/world_countries.shp
|
|
2921
|
-
mapshaper $< \
|
|
2922
|
-
-filter "CONTINENT == 'Europe'" \
|
|
2923
|
-
-simplify interval=100m keep-shapes \
|
|
2924
|
-
-o $@
|
|
2925
|
-
```
|
|
2926
|
-
|
|
2927
|
-
An alternative to embedding a long series of Mapshaper commands on the command line is to put them in a `.txt` file and pass the file to `mapshaper`. See [command files](/docs/reference.html.md#command-files) and [variable interpolation](/docs/reference.html.md#variable-interpolation) in the reference.
|
|
2928
|
-
|
|
2929
|
-
## The Node.js API
|
|
2930
|
-
|
|
2931
|
-
Mapshaper exposes three top-level functions for running editing commands programmatically. All three accept the same command-line string format as the `mapshaper` CLI.
|
|
2932
|
-
|
|
2933
|
-
### `runCommands(commands[, input][, callback])`
|
|
2934
|
-
|
|
2935
|
-
Runs a command line against files on disk (or in-memory). The `-o` command(s) write their output to disk.
|
|
2936
|
-
|
|
2937
|
-
- `commands` — a command-line string.
|
|
2938
|
-
- `input` (optional) — an object whose keys are filenames and whose values are file contents. Files referenced by `-i` are looked up here first, then on the filesystem.
|
|
2939
|
-
- `callback` (optional) — a Node-style `function(err)`. Without a callback, `runCommands` returns a Promise.
|
|
2940
|
-
|
|
2941
|
-
```javascript
|
|
2942
|
-
import mapshaper from 'mapshaper';
|
|
2943
|
-
|
|
2944
|
-
await mapshaper.runCommands('-i shapefiles/*.shp -o geojson/ format=geojson');
|
|
2945
|
-
```
|
|
2946
|
-
|
|
2947
|
-
### `applyCommands(commands[, input][, callback])`
|
|
2948
|
-
|
|
2949
|
-
Same signature as `runCommands`, but instead of writing files to disk, the contents produced by `-o` are returned to the caller as a `{ filename: Buffer }` object. Useful for processing data without touching the filesystem.
|
|
2950
|
-
|
|
2951
|
-
```javascript
|
|
2952
|
-
import mapshaper from 'mapshaper';
|
|
2953
|
-
|
|
2954
|
-
const input = { 'input.csv': 'lat,lng,value\n40.3,-72.3,1000' };
|
|
2955
|
-
const cmd = '-i input.csv -points x=lng y=lat -o output.geojson';
|
|
2956
|
-
|
|
2957
|
-
const output = await mapshaper.applyCommands(cmd, input);
|
|
2958
|
-
// output['output.geojson'] is a Buffer containing GeoJSON
|
|
2959
|
-
```
|
|
2960
|
-
|
|
2961
|
-
### `runCommandsXL(commands[, options][, callback])`
|
|
2962
|
-
|
|
2963
|
-
Like `runCommands`, but the work runs in a child Node process configured with a larger maximum heap (8 GB by default). Equivalent to running `mapshaper-xl` from the command line. Override the heap size with the `xl` option:
|
|
2964
|
-
|
|
2965
|
-
```javascript
|
|
2966
|
-
await mapshaper.runCommandsXL(commands, { xl: '16gb' });
|
|
2967
|
-
```
|
|
2968
|
-
|
|
2969
|
-
This function reads input only from the filesystem — there's no `input` argument as on `runCommands`/`applyCommands`.
|
|
2970
|
-
|
|
2971
|
-
## Working with Shapefiles in `applyCommands`
|
|
2972
|
-
|
|
2973
|
-
Shapefiles are really a set of component files. To import one through `applyCommands`, pass the contents of all the parts you care about:
|
|
2974
|
-
|
|
2975
|
-
- `.shp` — geometry (Buffer or ArrayBuffer)
|
|
2976
|
-
- `.dbf` — attribute table (Buffer or ArrayBuffer)
|
|
2977
|
-
- `.prj` — coordinate system (string)
|
|
2978
|
-
|
|
2979
|
-
Without `.dbf` you'll get geometry but no attributes; without `.prj`, projection-dependent commands won't have a coordinate system to work from.
|
|
2980
|
-
|
|
2981
|
-
```javascript
|
|
2982
|
-
const input = {
|
|
2983
|
-
'world.shp': shpBuffer,
|
|
2984
|
-
'world.dbf': dbfBuffer,
|
|
2985
|
-
'world.prj': prjString
|
|
2986
|
-
};
|
|
2987
|
-
const output = await mapshaper.applyCommands(
|
|
2988
|
-
'-i world.shp -simplify 10% -o world.geojson', input
|
|
2989
|
-
);
|
|
2990
|
-
```
|
|
2991
|
-
|
|
2992
|
-
## Versioning
|
|
2993
|
-
|
|
2994
|
-
The Node API is stable across minor Mapshaper releases, but new options and commands appear regularly. The full set of accepted command-line options is the same as the CLI's, so the [command reference](/docs/reference.html.md) is the authoritative list of what you can put into the `commands` string.
|
|
2995
|
-
|
|
2996
|
-
---
|
|
2997
|
-
|
|
2998
|
-
<!-- https://mapshaper.org/docs/formats/overview.html -->
|
|
2999
|
-
|
|
3000
|
-
# File formats
|
|
3001
|
-
|
|
3002
|
-
This section explains how each supported file format is handled, what format-specific options are available, and what to watch out for.
|
|
3003
|
-
|
|
3004
|
-
## Comparison
|
|
3005
|
-
|
|
3006
|
-
<div class="formats-table-wrap">
|
|
3007
|
-
|
|
3008
|
-
| Format | Extension | Read | Write | Geometry | Attributes | Topology | Multi-layer |
|
|
3009
|
-
|---|---|:---:|:---:|---|---|:---:|:---:|
|
|
3010
|
-
| [Shapefile](/docs/formats/shapefile.html.md) | `.shp` | ✓ | ✓ | vector | DBF (10-char names) | — | — |
|
|
3011
|
-
| [GeoJSON](/docs/formats/geojson.html.md) | `.json` `.geojson` | ✓ | ✓ | vector | yes | — | — |
|
|
3012
|
-
| [TopoJSON](/docs/formats/topojson.html.md) | `.json` `.topojson` | ✓ | ✓ | vector | yes | **✓** | ✓ |
|
|
3013
|
-
| [GeoPackage](/docs/formats/geopackage.html.md) | `.gpkg` | ✓ | ✓ | vector | yes | — | ✓ |
|
|
3014
|
-
| [FlatGeobuf](/docs/formats/flatgeobuf.html.md) | `.fgb` | ✓ | ✓ | vector | yes | — | — |
|
|
3015
|
-
| [KML / KMZ](/docs/formats/kml.html.md) | `.kml` `.kmz` | ✓ | ✓ | vector | limited | — | ✓ |
|
|
3016
|
-
| [CSV / TSV](/docs/formats/csv.html.md) | `.csv` `.tsv` | ✓ | ✓ | points (X/Y) | yes | — | — |
|
|
3017
|
-
| [DBF](/docs/formats/dbf.html.md) | `.dbf` | ✓ | ✓ | none | yes | — | — |
|
|
3018
|
-
| [JSON records](/docs/formats/json.html.md) | `.json` | ✓ | ✓ | none | yes | — | — |
|
|
3019
|
-
| [SVG](/docs/formats/svg.html.md) | `.svg` | ✓ | ✓ | vector | as `data-*` | — | ✓ |
|
|
3020
|
-
| [Mapshaper snapshot](/docs/formats/snapshot.html.md) | `.msx` | ✓ | ✓ | vector | yes | **✓** | ✓ |
|
|
3021
|
-
|
|
3022
|
-
</div>
|
|
3023
|
-
|
|
3024
|
-
A few things worth knowing across all formats:
|
|
3025
|
-
|
|
3026
|
-
- **Auto-detection by extension.** You usually don't need to tell Mapshaper what format a file is — it picks the right reader from the file extension. Use `format=` on `-i` or `-o` to override.
|
|
3027
|
-
- **TopoJSON is the only interchange format that preserves topology** in the file itself. Topology-aware operations like [`-dissolve`](/docs/reference.html.md#-dissolve), [`-clean`](/docs/reference.html.md#-clean) and [`-simplify`](/docs/reference.html.md#-simplify) work correctly regardless of the input format, but only TopoJSON keeps shared boundaries between adjacent polygons from being duplicated on disk. (Mapshaper's own [`.msx`](/docs/formats/snapshot.html.md) snapshots also preserve topology, but they're not readable by other tools.)
|
|
3028
|
-
- **Encoding.** The `encoding=` option on `-i` and `-o` applies to Shapefile, DBF and CSV/TSV i/o (UTF-8 is the default) — the other formats are UTF-8-only.
|
|
3029
|
-
|
|
3030
|
-
---
|
|
3031
|
-
|
|
3032
|
-
<!-- https://mapshaper.org/docs/formats/shapefile.html -->
|
|
3033
|
-
|
|
3034
|
-
# Shapefile
|
|
3035
|
-
|
|
3036
|
-
Shapefile is ESRI's long-standing vector format and remains widely used as an exchange format in desktop GIS workflows. Rather than a single file, a Shapefile is a collection of files — at minimum .shp, .shx, and .dbf, plus commonly .prj (coordinate reference system) and .cpg (character encoding). It has well-known limitations, including a 2 GB size cap per component file, attribute field names limited to 10 characters, attribute values limited to 254 characters, no support for mixed geometry types, and unreliable character encoding declaration. Newer formats like [GeoPackage](/docs/formats/geopackage.html.md) and [FlatGeobuf](/docs/formats/flatgeobuf.html.md) solve most of the limitations.
|
|
3037
|
-
|
|
3038
|
-
**File extensions:** `.shp` (geometry), `.dbf` (attributes), `.shx` (index), `.prj` (projection), `.cpg` (encoding hint)
|
|
3039
|
-
· **Read:** ✓ · **Write:** ✓ · **Multi-layer:** no
|
|
3040
|
-
|
|
3041
|
-
### CLI examples
|
|
3042
|
-
|
|
3043
|
-
```bash
|
|
3044
|
-
mapshaper provinces.shp -info
|
|
3045
|
-
mapshaper provinces.shp -simplify 20% -o provinces.geojson
|
|
3046
|
-
mapshaper input.geojson -o provinces.shp
|
|
3047
|
-
```
|
|
3048
|
-
|
|
3049
|
-
### Format-specific input options
|
|
3050
|
-
|
|
3051
|
-
- `encoding=` — encoding used by the `.dbf` text fields. If omitted, Mapshaper auto-detects, falling back to the value declared in a `.cpg` file when present. Common values: `latin1`, `utf8`, `gb18030`. Run `mapshaper -encodings` for the full list.
|
|
3052
|
-
|
|
3053
|
-
### Format-specific output options
|
|
3054
|
-
|
|
3055
|
-
- `encoding=` — text encoding for the `.dbf`. Defaults to UTF-8 (with a matching `.cpg` sidecar).
|
|
3056
|
-
- `field-order=ascending` — sort attribute fields alphabetically (case-insensitive).
|
|
3057
|
-
|
|
3058
|
-
### Practical notes
|
|
3059
|
-
|
|
3060
|
-
- A Shapefile dataset is a bundle of files sharing a base name. The Mapshaper CLI inputs the `.shp` and picks up any sibling `.dbf`, `.shx`, `.prj` and `.cpg` files automatically. In the [web app](/docs/essentials/web-app.html.md) you select or drag-drop them together — see [In the web app](#in-the-web-app) below.
|
|
3061
|
-
- Field names longer than 10 characters are silently truncated on write, which can produce duplicates. Mapshaper disambiguates by appending digits, or you can rename fields beforehand with [`-rename-fields`](/docs/reference.html.md#-rename-fields).
|
|
3062
|
-
- Field values longer than 254 characters are truncated on write.
|
|
3063
|
-
- Mapshaper does not fully support M (measured) and Z (3D) Shapefiles — M and Z values are dropped on import.
|
|
3064
|
-
- When exporting, the Mapshaper CLI produces separate companion files (`.shp`, `.shx`, `.dbf`, `.prj`). In the web app, the component files get bundled in a `.zip` file.
|
|
3065
|
-
- If the `.prj` file is missing, Mapshaper reads the geometry without coordinate-reference information. Coordinates in the lat-long range are assumed to be WGS-84. You can use the `-proj` command to assign a CRS (e.g. `-proj init=...`).
|
|
3066
|
-
- A standalone `.dbf` can be imported on its own as a tabular layer — see the [DBF page](/docs/formats/dbf.html.md).
|
|
3067
|
-
|
|
3068
|
-
### In the web app
|
|
3069
|
-
|
|
3070
|
-
Browsers can't read files from the filesystem the CLI can, so you have to supply all the parts of a Shapefile together. Two options are:
|
|
3071
|
-
|
|
3072
|
-
1. **Select all the components together.** Click **Add files** and shift- or cmd-click the `.shp`, `.dbf`, `.prj` (and `.shx`/`.cpg` if present) in one go, or drag the whole selection onto the import area.
|
|
3073
|
-
2. **Drop a `.zip` containing the bundle.** Shapefiles are very commonly distributed this way.
|
|
3074
|
-
|
|
3075
|
-
If the import warns about an unknown text encoding, re-import with the **with advanced options** checkbox ticked and pass `encoding=` (e.g. `encoding=win1252`, `encoding=gb18030`). The same `encoding=` values that work on the CLI work here.
|
|
3076
|
-
|
|
3077
|
-
### Reading a Shapefile with missing sidecars
|
|
3078
|
-
|
|
3079
|
-
Mapshaper will read a `.shp` whose `.dbf` and/or `.shx` companion files are missing — useful when you're handed an incomplete bundle, or when you only care about geometry:
|
|
3080
|
-
|
|
3081
|
-
- **Missing `.dbf`**: the geometry is imported with no attribute table.
|
|
3082
|
-
- **Missing `.shx`**: Mapshaper recovers feature offsets by reading the `.shp` directly. This works for normal Shapefiles with densely-packed records; it only fails on the rare Shapefiles that contain out-of-order records, where the `.shx` is needed to locate each feature.
|
|
3083
|
-
|
|
3084
|
-
### `.dbf` text encoding
|
|
3085
|
-
|
|
3086
|
-
The .dbf format is a legacy binary format dating to dBASE III in the early 1980s, predating Unicode. Character encoding is not declared within the file itself, which can cause encoding errors when working with Shapefiles containing non-ASCII characters. In practice, most Shapefiles now come with a `.cpg` file or use UTF-8, which is almost always auto-detected correctly.
|
|
3087
|
-
|
|
3088
|
-
Mapshaper handles encoding in the following order:
|
|
3089
|
-
|
|
3090
|
-
1. If `encoding=` is set on `-i`, that wins.
|
|
3091
|
-
2. If a `.cpg` sidecar file is present, Mapshaper uses the encoding it names.
|
|
3092
|
-
3. Otherwise Mapshaper tries to auto-detect the encoding from the `.dbf` contents.
|
|
3093
|
-
|
|
3094
|
-
Auto-detection covers most public datasets, but it can guess wrong on sparsely-populated columns or unusual codepages. If you see mojibake (`é` instead of `é`, `?` characters where accented letters should be), set `encoding=` explicitly:
|
|
3095
|
-
|
|
3096
|
-
```bash
|
|
3097
|
-
mapshaper -i provinces.shp encoding=utf8 -info
|
|
3098
|
-
mapshaper -i historical.shp encoding=win1252 -o cleaned.geojson
|
|
3099
|
-
mapshaper -i china.shp encoding=gb18030 -o cleaned.geojson
|
|
3100
|
-
```
|
|
3101
|
-
|
|
3102
|
-
When writing, Mapshaper emits UTF-8 plus a `.cpg` sidecar by default so other tools can decode correctly. Override with `encoding=` on `-o` if a downstream consumer requires a specific codepage.
|
|
3103
|
-
|
|
3104
|
-
## External resources
|
|
3105
|
-
|
|
3106
|
-
- [Shapefile file extensions (ArcMap docs)](https://desktop.arcgis.com/en/arcmap/latest/manage-data/shapefiles/shapefile-file-extensions.htm) — ESRI's own practical reference describing what each `.shp`/`.shx`/`.dbf`/`.prj`/`.cpg`/`.sbn` etc. file actually contains. Useful when you encounter unfamiliar sidecar files.
|
|
3107
|
-
- [Switch from Shapefile](https://switchfromshapefile.org/) — a long-running campaign cataloguing the format's well-known limitations and pointing to alternatives.
|
|
3108
|
-
- [ESRI Shapefile Technical Description (PDF)](https://www.esri.com/content/dam/esrisites/sitecore-archive/Files/Pdfs/library/whitepapers/pdfs/shapefile.pdf) — the original 1998 white paper that defines the format. Dry but authoritative.
|
|
3109
|
-
|
|
3110
|
-
## See also
|
|
3111
|
-
|
|
3112
|
-
- [DBF format](/docs/formats/dbf.html.md)
|
|
3113
|
-
|
|
3114
|
-
---
|
|
3115
|
-
|
|
3116
|
-
<!-- https://mapshaper.org/docs/formats/geojson.html -->
|
|
3117
|
-
|
|
3118
|
-
# GeoJSON
|
|
3119
|
-
|
|
3120
|
-
GeoJSON is a simple, human-readable format for geospatial vector data. It is used by many web mapping APIs, although formats like TopoJSON, FlatGeobuf, GeoParquet, and vector tiles have replaced it for specific use cases such as large-file efficiency, cloud-optimized access, and tiled rendering.
|
|
3121
|
-
|
|
3122
|
-
**File extensions:** `.json`, `.geojson` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** one layer per file (combinable)
|
|
3123
|
-
|
|
3124
|
-
### CLI examples
|
|
3125
|
-
|
|
3126
|
-
```bash
|
|
3127
|
-
mapshaper input.shp -o provinces.geojson
|
|
3128
|
-
mapshaper input.shp -o precision=0.001 prettify provinces.geojson
|
|
3129
|
-
```
|
|
3130
|
-
|
|
3131
|
-
### Format-specific input options
|
|
3132
|
-
|
|
3133
|
-
- `id-field=` — import the value of each Feature's top-level `id` property into a data field of the given name.
|
|
3134
|
-
- `json-path=` — for files where the GeoJSON object is nested inside a larger JSON document, e.g. `json-path=data/regions`.
|
|
3135
|
-
|
|
3136
|
-
### Format-specific output options
|
|
3137
|
-
|
|
3138
|
-
- `precision=` — round coordinates to a fixed precision. This is a simple way to reduce file size.
|
|
3139
|
-
- `prettify` — pretty-print the JSON with line breaks and indentation.
|
|
3140
|
-
- `id-field=` — promote one or more attribute fields to the GeoJSON `id` property (comma-separated; first matching field per layer is used).
|
|
3141
|
-
- `bbox` — add a `bbox` array to the top-level FeatureCollection.
|
|
3142
|
-
- `extension=` — override the default `.json` extension (e.g. `extension=geojson`).
|
|
3143
|
-
- `combine-layers` — merge multiple layers into a single GeoJSON output file (geometries are kept separate as Features, attribute schemas are unioned).
|
|
3144
|
-
- `geojson-type=` — output a `Feature`, `FeatureCollection` or bare `GeometryCollection` instead of the default FeatureCollection.
|
|
3145
|
-
- `no-null-props` — emit `"properties": {}` instead of `"properties": null` for Features without attributes.
|
|
3146
|
-
- `hoist=` — promote one or more properties out of the `properties` object onto the Feature itself. Useful for non-standard consumers like [tippecanoe](https://github.com/felt/tippecanoe).
|
|
3147
|
-
- `gj2008` — emit pre-RFC-7946 GeoJSON (clockwise outer rings). This option produces files that can be rendered with `d3`.
|
|
3148
|
-
- `ndjson` — write one Feature per line as newline-delimited JSON (works with the [`json` records](/docs/formats/json.html.md) family of options as well).
|
|
3149
|
-
- `id-prefix=` — prefix layer/feature ids when exporting multiple layers.
|
|
3150
|
-
|
|
3151
|
-
### Practical notes
|
|
3152
|
-
|
|
3153
|
-
- The GeoJSON spec states that GeoJSON uses WGS-84 coordinates (the lat-long coordinate system used by GPS), but Mapshaper will also export GeoJSON files with projected coordinates.
|
|
3154
|
-
- Coordinates are emitted at full precision — consider `precision=` to reduce file size. `precision=0.0001` equates to ~11 m at the equator, ~8 m in New York City, and ~6 m in Reykjavík, Iceland.
|
|
3155
|
-
- Polygon ring winding follows RFC 7946 (CCW outer, CW holes); the `gj2008` option outputs the CW outer rings expected by `d3`.
|
|
3156
|
-
- Output is minified by default; pass `prettify` for human-readable JSON.
|
|
3157
|
-
- If you are loading the data into a web map and you want the smallest possible file size, consider [TopoJSON](/docs/formats/topojson.html.md) as an alternative to GeoJSON. For datasets with shared boundaries, file sizes are often a fraction of the equivalent GeoJSON size.
|
|
3158
|
-
- `precision=` rounding can introduce sliver overlaps at boundaries. Pair it with `fix-geometry` if downstream tools are strict.
|
|
3159
|
-
|
|
3160
|
-
### Reading very large GeoJSON files
|
|
3161
|
-
|
|
3162
|
-
Mapshaper's custom GeoJSON parser is not limited by the ~500 MB ceiling affecting tools that use `JSON.parse()`.
|
|
3163
|
-
|
|
3164
|
-
In the [web app](/docs/essentials/web-app.html.md), the theoretical upper bound is around 2 GB per file in most browsers, though in practice the browser may run out of memory and crash well before that. If a GeoJSON is too big to open in the browser, use the CLI instead.
|
|
3165
|
-
|
|
3166
|
-
`mapshaper-xl` can handle multi-gigabyte files. It allocates 8 GB of memory by default, but you can assign more.
|
|
3167
|
-
|
|
3168
|
-
```bash
|
|
3169
|
-
mapshaper-xl huge.geojson -info
|
|
3170
|
-
mapshaper-xl 32gb huge.geojson -simplify 5% -o huge.topojson
|
|
3171
|
-
```
|
|
3172
|
-
|
|
3173
|
-
## External resources
|
|
3174
|
-
|
|
3175
|
-
- [RFC 7946: The GeoJSON Format](https://datatracker.ietf.org/doc/html/rfc7946) — the IETF specification Mapshaper writes by default.
|
|
3176
|
-
- [More than you ever wanted to know about GeoJSON](https://macwright.com/2015/03/23/geojson-second-bite.html) — Tom MacWright's detailed practical introduction.
|
|
3177
|
-
- [geojson.org](https://geojson.org/) — spec home with links to tooling and validators.
|
|
3178
|
-
|
|
3179
|
-
---
|
|
3180
|
-
|
|
3181
|
-
<!-- https://mapshaper.org/docs/formats/topojson.html -->
|
|
3182
|
-
|
|
3183
|
-
# TopoJSON
|
|
3184
|
-
|
|
3185
|
-
TopoJSON is a JSON-based format that encodes geographic topology: shared boundaries between adjacent features are stored once instead of duplicated. For datasets with shared boundaries (administrative divisions, watersheds, anything with adjacency) the resulting file is often 2–5× smaller than the equivalent GeoJSON. A single TopoJSON file can hold multiple layers, making it a natural choice for shipping a complete map (e.g. countries + states + cities) in one HTTP request.
|
|
3186
|
-
|
|
3187
|
-
**File extensions:** `.json`, `.topojson` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** ✓
|
|
3188
|
-
|
|
3189
|
-
### CLI examples
|
|
3190
|
-
|
|
3191
|
-
```bash
|
|
3192
|
-
mapshaper world.topojson -info
|
|
3193
|
-
mapshaper world.topojson -o format=geojson world.geojson
|
|
3194
|
-
mapshaper provinces.shp -o provinces.topojson
|
|
3195
|
-
mapshaper provinces.shp -o quantization=10000 provinces.topojson
|
|
3196
|
-
mapshaper countries.shp states.shp -o singles output/
|
|
3197
|
-
```
|
|
3198
|
-
|
|
3199
|
-
### Format-specific input options
|
|
3200
|
-
|
|
3201
|
-
- `id-field=` — import the `id` property of each Feature into a data field of the given name.
|
|
3202
|
-
- `json-path=` — for files where the TopoJSON object is nested inside a larger document.
|
|
3203
|
-
|
|
3204
|
-
### Format-specific output options
|
|
3205
|
-
|
|
3206
|
-
- `quantization=` — number of distinct integer values that x and y coordinates are quantized to, *per axis*. For example, `quantization=10000` produces a 10000×10000 integer grid regardless of the bounding box's aspect ratio (x and y use independent scales). Lower values produce smaller files at the cost of precision. Equivalent to the [`topoquantize`](https://github.com/topojson/topojson-server/blob/master/README.md#topoquantize) CLI's parameter.
|
|
3207
|
-
- `topojson-precision=` — alternative way to set quantization, expressed as a fraction of the average segment length.
|
|
3208
|
-
- `no-quantization` — emit full-precision arc coordinates.
|
|
3209
|
-
- `singles` — write each layer as a separate file, named after the layer.
|
|
3210
|
-
- `prettify` — pretty-print the JSON.
|
|
3211
|
-
- `id-field=` — promote an attribute field to the `id` property of each output object.
|
|
3212
|
-
- `bbox` — add a top-level `bbox` array.
|
|
3213
|
-
- `extension=` — override the default `.json` extension (e.g. `extension=topojson`).
|
|
3214
|
-
- `width=` / `height=` / `pixels=` / `margin=` — switch the output coordinate system from geographic units to pixels, flipping the Y axis. Useful when generating a TopoJSON intended for direct use as SVG path data.
|
|
3215
|
-
|
|
3216
|
-
### Practical notes
|
|
3217
|
-
|
|
3218
|
-
- Quantization is on by default with a value calibrated to the geometry (about 0.02 of the average segment length), which keeps files compact while staying visually lossless. Use the `quantization=` option to override the default.
|
|
3219
|
-
- Use `no-quantization` to save coordinates losslessly.
|
|
3220
|
-
- TopoJSON does not store coordinate system metadata. If your data is in a projected coordinate system, you'll need to manage the projection separately.
|
|
3221
|
-
- Output is minified by default; pass `prettify` for human-readable JSON.
|
|
3222
|
-
- Aggressive quantization can introduce visible misalignments and sliver overlaps. If this happens, raise the `quantization=` value.
|
|
3223
|
-
|
|
3224
|
-
## External resources
|
|
3225
|
-
|
|
3226
|
-
- [TopoJSON specification](https://github.com/topojson/topojson-specification) — the format spec on GitHub.
|
|
3227
|
-
- [How To Infer Topology](https://bost.ocks.org/mike/topology/) — Mike Bostock's original explainer of the algorithm and data model behind TopoJSON. Required reading if you want to understand what makes the format compact.
|
|
3228
|
-
|
|
3229
|
-
## See also
|
|
3230
|
-
|
|
3231
|
-
- [Quantized TopoJSON](/docs/examples/basics.html.md#quantized-topojson)
|
|
3232
|
-
|
|
3233
|
-
---
|
|
3234
|
-
|
|
3235
|
-
<!-- https://mapshaper.org/docs/formats/geopackage.html -->
|
|
3236
|
-
|
|
3237
|
-
# GeoPackage
|
|
3238
|
-
|
|
3239
|
-
GeoPackage is the OGC's modern, open replacement for Shapefile — a single SQLite database file that holds one or many vector layers along with their CRS metadata. It solves most of Shapefile's problems (long field names, UTF-8 encoding, no companion files, multiple layers per file, no 2 GB cap) and is well-supported across QGIS, ArcGIS and `ogr2ogr`.
|
|
3240
|
-
|
|
3241
|
-
**File extension:** `.gpkg` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** ✓
|
|
3242
|
-
|
|
3243
|
-
### CLI examples
|
|
3244
|
-
|
|
3245
|
-
```bash
|
|
3246
|
-
mapshaper basemap.gpkg -info
|
|
3247
|
-
mapshaper basemap.gpkg -o format=geojson regions.geojson
|
|
3248
|
-
mapshaper -i basemap.gpkg layers=provinces,cities -info
|
|
3249
|
-
mapshaper provinces.shp -o provinces.gpkg
|
|
3250
|
-
mapshaper a.shp b.shp -o combined.gpkg
|
|
3251
|
-
```
|
|
3252
|
-
|
|
3253
|
-
### Format-specific input options
|
|
3254
|
-
|
|
3255
|
-
- `layers=` — comma-separated list of layer names to import. Useful for picking a subset out of a large multi-layer GeoPackage. Omit to import everything.
|
|
3256
|
-
|
|
3257
|
-
### Format-specific output options
|
|
3258
|
-
|
|
3259
|
-
There are no GeoPackage-specific `-o` options. The format honors the general flags (`precision=`, `gzip`, `zip`, etc.) where they apply.
|
|
3260
|
-
|
|
3261
|
-
### Practical notes
|
|
3262
|
-
|
|
3263
|
-
- By default, every vector layer in the file is imported as a separate Mapshaper layer. To pick a subset, use the `layers=` option on the CLI; in the web app, tick the **with advanced options** checkbox in the import dialog to bring up a per-layer selection list.
|
|
3264
|
-
- When multiple layers are exported to a single `.gpkg`, each becomes a separate layer table inside the database, named after the source layer.
|
|
3265
|
-
- Raster tile layers (the OGC GeoPackage spec also covers tiles) are ignored — Mapshaper is vector-only.
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
## External resources
|
|
3269
|
-
|
|
3270
|
-
- [geopackage.org](https://www.geopackage.org/) — project home with spec, FAQs and tool support.
|
|
3271
|
-
- [QGIS user manual: supported data formats](https://docs.qgis.org/latest/en/docs/user_manual/managing_data_source/supported_data.html) — the practical reference on how QGIS treats GeoPackage, including multi-layer use and project storage.
|
|
3272
|
-
- [Learn spatial SQL and master GeoPackage with QGIS](https://www.gispo.fi/en/blog/learn-spatial-sql-and-master-geopackage-with-qgis-3/) — tutorial from Gispo showing how to query GeoPackage layers with SQL directly from QGIS.
|
|
3273
|
-
- GeoPackage reading and writing in Mapshaper is delegated to NGA's [`@ngageoint/geopackage`](https://github.com/ngageoint/geopackage-js) library, which handles the underlying SQLite database, the OGC table schemas and the WKB-to-GeoJSON geometry conversions.
|
|
3274
|
-
|
|
3275
|
-
---
|
|
3276
|
-
|
|
3277
|
-
<!-- https://mapshaper.org/docs/formats/flatgeobuf.html -->
|
|
3278
|
-
|
|
3279
|
-
# FlatGeobuf
|
|
3280
|
-
|
|
3281
|
-
FlatGeobuf is a modern binary vector format designed for fast streaming reads. A single self-contained file with UTF-8 text encoding, it avoids the companion-file clutter of Shapefile. Its optional embedded spatial index enables efficient bounding-box queries without reading the whole file, making it well-suited to large datasets served over HTTP. It also works well as a general-purpose GIS exchange format.
|
|
3282
|
-
|
|
3283
|
-
**File extension:** `.fgb` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** no (one layer per file)
|
|
3284
|
-
|
|
3285
|
-
### CLI examples
|
|
3286
|
-
|
|
3287
|
-
```bash
|
|
3288
|
-
mapshaper buildings.fgb -info
|
|
3289
|
-
mapshaper buildings.fgb -simplify 5% -o buildings.geojson
|
|
3290
|
-
mapshaper provinces.shp -o provinces.fgb
|
|
3291
|
-
```
|
|
3292
|
-
|
|
3293
|
-
### Format-specific input options
|
|
3294
|
-
|
|
3295
|
-
There are no FlatGeobuf-specific `-i` options.
|
|
3296
|
-
|
|
3297
|
-
### Format-specific output options
|
|
3298
|
-
|
|
3299
|
-
There are no FlatGeobuf-specific `-o` options. The format honors the general flags (`precision=`, `gzip`, `zip`, etc.) where they apply.
|
|
3300
|
-
|
|
3301
|
-
### Practical notes
|
|
3302
|
-
|
|
3303
|
-
- Mapshaper reads CRS metadata from the file header when it's encoded as an EPSG code. WKT2-only definitions can't be parsed and produce an "Unable to import WKT2 CRS from FlatGeobuf" warning — the layer comes in without a CRS.
|
|
3304
|
-
- On output, Mapshaper embeds an EPSG code in the FlatGeobuf header whenever it can derive one from the source: a round-tripped FlatGeobuf or GeoPackage CRS, an `epsg:NNNN` string passed to `-proj`, an `AUTHORITY["EPSG", N]` clause in a Shapefile `.prj`, or any encoding of WGS-84 or Web Mercator (which covers most GeoJSON, CSV-with-lat/lon and `-proj wgs84`/`-proj webmercator` outputs).
|
|
3305
|
-
- Mapshaper cannot yet convert an arbitrary proj4 definition (such as a custom Albers projection set with `-proj +proj=aea ...`) to an EPSG code. In that case the file is written with no CRS in the header and a warning is printed: *"Wrote `foo.fgb` without a CRS in the FlatGeobuf header..."*. Re-run the file through `ogr2ogr` if you need the CRS embedded for downstream tools.
|
|
3306
|
-
- Mapshaper does not write the optional packed R-tree spatial index, and it doesn't use the index for selective reads of indexed input either — the whole file is read into memory. If you need an indexed `.fgb` for HTTP range-request reads, build it with `ogr2ogr` or the [`flatgeobuf` CLI](https://github.com/flatgeobuf/flatgeobuf).
|
|
3307
|
-
|
|
3308
|
-
## External resources
|
|
3309
|
-
|
|
3310
|
-
- [flatgeobuf.org](https://flatgeobuf.org/) — project home with spec links and language bindings.
|
|
3311
|
-
- [Kicking the Tires: FlatGeobuf](https://worace.works/2022/02/23/kicking-the-tires-flatgeobuf/) — an independent practical writeup with benchmarks against Shapefile, GeoJSON and GeoPackage.
|
|
3312
|
-
- [Bryce Mecum: Flatgeobuf](https://brycemecum.com/2022/04/04/flatgeobuf/) — a hands-on exploration of streaming reads in the browser, including a worth-knowing gotcha about the spatial index sitting at the front of the file.
|
|
3313
|
-
- [Cloud-Native Geospatial Formats Guide: FlatGeobuf](https://guide.cloudnativegeo.org/flatgeobuf/) — how the format fits into modern cloud-storage workflows.
|
|
3314
|
-
- FlatGeobuf reading and writing in Mapshaper is built on the official [`flatgeobuf`](https://github.com/flatgeobuf/flatgeobuf) JavaScript library, which provides the FlatBuffers schema, header parsing and feature serialisation primitives.
|
|
3315
|
-
|
|
3316
|
-
---
|
|
3317
|
-
|
|
3318
|
-
<!-- https://mapshaper.org/docs/formats/kml.html -->
|
|
3319
|
-
|
|
3320
|
-
# KML and KMZ
|
|
3321
|
-
|
|
3322
|
-
KML is Google's XML-based format for geographic data, originally created for Google Earth and now an OGC standard. KMZ is a zipped KML. KML emphasises display (icons, styles, balloons) over attributes, so it's most useful for handing files to viewers like Google Earth, mobile mapping apps and Google My Maps — as a data interchange format it has weaker attribute typing and schema support than [GeoJSON](/docs/formats/geojson.html.md) or [Shapefile](/docs/formats/shapefile.html.md).
|
|
3323
|
-
|
|
3324
|
-
**File extensions:** `.kml`, `.kmz` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** ✓
|
|
3325
|
-
|
|
3326
|
-
### CLI examples
|
|
3327
|
-
|
|
3328
|
-
```bash
|
|
3329
|
-
mapshaper places.kml -info
|
|
3330
|
-
mapshaper places.kmz -o format=geojson places.geojson
|
|
3331
|
-
mapshaper provinces.shp -proj wgs84 -o provinces.kml
|
|
3332
|
-
```
|
|
3333
|
-
|
|
3334
|
-
### Format-specific input options
|
|
3335
|
-
|
|
3336
|
-
There are no KML-specific `-i` options. Encoding is always UTF-8 per the KML spec.
|
|
3337
|
-
|
|
3338
|
-
### Format-specific output options
|
|
3339
|
-
|
|
3340
|
-
There are no KML-specific `-o` options.
|
|
3341
|
-
|
|
3342
|
-
### Practical notes
|
|
3343
|
-
|
|
3344
|
-
- KML stores all attribute values as strings, so numeric attributes are imported as strings.
|
|
3345
|
-
- KML requires WGS84 coordinates, but Mapshaper does **not** reproject on export — coordinates are written through as-is. If your dataset is in any other CRS, run `-proj wgs84` first, otherwise the output will not be conformant KML and viewers will misplace the geometry.
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
## External resources
|
|
3349
|
-
|
|
3350
|
-
- [OGC KML standard](https://www.ogc.org/standards/kml/) — the formal OGC specification.
|
|
3351
|
-
- [Google KML Reference](https://developers.google.com/kml/documentation/kmlreference) — the practical element-by-element reference; more readable than the OGC spec.
|
|
3352
|
-
- [Google KML Tutorial](https://developers.google.com/kml/documentation/kml_tut) — covers how Google Earth interprets the format.
|
|
3353
|
-
- KML reading and writing in Mapshaper is delegated to two third-party libraries: [`@tmcw/togeojson`](https://github.com/placemark/togeojson) for parsing KML into GeoJSON on import, and [`@placemarkio/tokml`](https://github.com/placemark/tokml) for serialising GeoJSON back to KML on export.
|
|
3354
|
-
|
|
3355
|
-
---
|
|
3356
|
-
|
|
3357
|
-
<!-- https://mapshaper.org/docs/formats/csv.html -->
|
|
3358
|
-
|
|
3359
|
-
# CSV and TSV
|
|
3360
|
-
|
|
3361
|
-
Plain-text tabular data. Mapshaper treats CSV/TSV as pure attribute data by default, or as a point layer when longitude and latitude columns are present. Combined with [`-join`](/docs/reference.html.md#-join), CSV is the lowest-friction way to attach external attributes (population, election results, anything keyed on a stable id) to a geometry layer.
|
|
3362
|
-
|
|
3363
|
-
**File extensions:** `.csv`, `.tsv` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** no
|
|
3364
|
-
|
|
3365
|
-
### CLI examples
|
|
3366
|
-
|
|
3367
|
-
```bash
|
|
3368
|
-
mapshaper data.csv -info
|
|
3369
|
-
mapshaper data.csv -points x=lon y=lat -o points.geojson
|
|
3370
|
-
mapshaper big.csv csv-fields=id,name,pop -info
|
|
3371
|
-
mapshaper provinces.shp -drop-table -o stats.csv
|
|
3372
|
-
mapshaper data.csv -o delimiter=";" out.csv
|
|
3373
|
-
```
|
|
3374
|
-
|
|
3375
|
-
### Format-specific input options
|
|
3376
|
-
|
|
3377
|
-
- `encoding=` — text encoding. Default is UTF-8.
|
|
3378
|
-
- `string-fields=` — comma-separated list of fields to import as strings even if their values look numeric. Use `string-fields=*` to import every field as a string. Essential for ZIP codes, FIPS codes and anything else where leading zeros matter.
|
|
3379
|
-
- `field-types=` — per-field type hints, e.g. `FIPS:str,population:num`. More flexible alternative to `string-fields=`.
|
|
3380
|
-
- `csv-skip-lines=` — skip N lines at the top of the file. Useful for spreadsheet exports with notes above the data.
|
|
3381
|
-
- `csv-lines=` — import only the first N data rows.
|
|
3382
|
-
- `csv-field-names=` — assign explicit field names. Combine with `csv-skip-lines=1` to override existing headers.
|
|
3383
|
-
- `csv-fields=` — import only the named columns. Filtering happens during the read, so this option dramatically reduces peak memory for wide CSVs.
|
|
3384
|
-
- `csv-filter=` — a JavaScript expression evaluated per row. Rows that return false are dropped before they ever reach the layer.
|
|
3385
|
-
- `csv-dedup-fields` — rename duplicate column headers (otherwise Mapshaper errors out).
|
|
3386
|
-
- `decimal-comma` — parse numbers using `1.000,01` European convention instead of `1,000.01`.
|
|
3387
|
-
|
|
3388
|
-
### Format-specific output options
|
|
3389
|
-
|
|
3390
|
-
- `encoding=` — text encoding for the output. Default is UTF-8.
|
|
3391
|
-
- `delimiter=` — override the field delimiter, e.g. `delimiter="|"`.
|
|
3392
|
-
- `decimal-comma` — emit numbers using the European decimal-comma convention.
|
|
3393
|
-
- `field-order=ascending` — sort columns alphabetically.
|
|
3394
|
-
|
|
3395
|
-
### Practical notes
|
|
3396
|
-
|
|
3397
|
-
- The delimiter is auto-detected from the extension (`.csv` → comma, `.tsv` → tab). Use `-i format=csv` to force CSV parsing for a differently-named file (e.g. `.txt`).
|
|
3398
|
-
- When exporting non-point geometry to CSV, Mapshaper writes only the attribute table. Use [`-points`](/docs/reference.html.md#-points) first if you want to export point coordinates as `lon`/`lat` columns.
|
|
3399
|
-
- BOM-prefixed files (typically from Excel) are handled transparently on read.
|
|
3400
|
-
- Output has no quoting unless a value contains the delimiter, a quote or a newline.
|
|
3401
|
-
|
|
3402
|
-
### Importing identifier-like fields (ZIP, FIPS, phone numbers…)
|
|
3403
|
-
|
|
3404
|
-
This is the single biggest CSV footgun. Mapshaper guesses each column's type from its values, so a column of US ZIP codes like `02134`, `90210`, `10001` looks numeric and gets imported as numbers — silently stripping leading zeros and breaking any subsequent join. The same applies to FIPS codes, phone numbers, account numbers and any other identifier that happens to contain only digits.
|
|
3405
|
-
|
|
3406
|
-
Always declare these columns as strings on import:
|
|
3407
|
-
|
|
3408
|
-
```bash
|
|
3409
|
-
mapshaper -i counties.csv string-fields=GEOID,STATEFP,COUNTYFP -info
|
|
3410
|
-
mapshaper -i zips.csv string-fields=zipcode -join points key=zipcode,zipcode
|
|
3411
|
-
```
|
|
3412
|
-
|
|
3413
|
-
If you don't trust the schema at all, `string-fields=*` imports every column as a string. You can also be precise with `field-types=`:
|
|
3414
|
-
|
|
3415
|
-
```bash
|
|
3416
|
-
mapshaper -i data.csv field-types=GEOID:str,population:num,year:str -info
|
|
3417
|
-
```
|
|
3418
|
-
|
|
3419
|
-
A symptom of getting this wrong is a join silently dropping all rows because `02134` (string) doesn't match `2134` (number).
|
|
3420
|
-
|
|
3421
|
-
### Prefiltering large CSVs
|
|
3422
|
-
|
|
3423
|
-
For multi-gigabyte CSVs — election precinct records, OSM extract attribute tables, parcel data — it usually isn't viable to load the whole file into memory and then filter. Mapshaper has two options that filter **during** the read, before the data lands in a layer:
|
|
3424
|
-
|
|
3425
|
-
- `csv-fields=` — keep only the named columns. Wide CSVs (hundreds of columns, only a few of interest) shrink dramatically.
|
|
3426
|
-
- `csv-filter=` — a JavaScript expression evaluated per row. Rows that return false are skipped.
|
|
3427
|
-
|
|
3428
|
-
Example (note that numerical fields have not been converted from strings at this point):
|
|
3429
|
-
|
|
3430
|
-
```bash
|
|
3431
|
-
mapshaper -i big.csv \
|
|
3432
|
-
csv-fields=GEOID,name,population,state \
|
|
3433
|
-
csv-filter='state == "TX" && population > "10000"' \
|
|
3434
|
-
-o cities-tx.csv
|
|
3435
|
-
```
|
|
3436
|
-
|
|
3437
|
-
### In the web app
|
|
3438
|
-
|
|
3439
|
-
The same import options work in the [web app](/docs/essentials/web-app.html.md). Tick **with advanced options** in the import dialog and pass any of `string-fields=`, `field-types=`, `encoding=`, `csv-fields=`, `csv-filter=` etc. as you would on the CLI:
|
|
3440
|
-
|
|
3441
|
-
```
|
|
3442
|
-
string-fields=GEOID,STATEFP encoding=utf8
|
|
3443
|
-
```
|
|
3444
|
-
|
|
3445
|
-
For very large CSVs, `csv-fields=` and `csv-filter=` are the most useful options for keeping memory under control. If a file is too big for the browser to load, the [`mapshaper-xl` CLI](/docs/essentials/command-line.html.md) is the fallback.
|
|
3446
|
-
|
|
3447
|
-
## External resources
|
|
3448
|
-
|
|
3449
|
-
- [RFC 4180: Common Format and MIME Type for CSV](https://datatracker.ietf.org/doc/html/rfc4180) — the closest thing to a CSV specification, though many real-world files diverge from it.
|
|
3450
|
-
- [Frictionless CSV Dialect spec](https://specs.frictionlessdata.io/csv-dialect/) — a practical schema for declaring how a particular CSV file is formatted (delimiter, quoting, line terminator).
|
|
3451
|
-
|
|
3452
|
-
---
|
|
3453
|
-
|
|
3454
|
-
<!-- https://mapshaper.org/docs/formats/dbf.html -->
|
|
3455
|
-
|
|
3456
|
-
# DBF
|
|
3457
|
-
|
|
3458
|
-
DBF is the dBase database format. It's best known as the attribute-table half of a Shapefile, but Mapshaper can also import a `.dbf` on its own as a tabular layer with no geometry. [CSV](/docs/formats/csv.html.md) is generally preferred as an exchange format for tabular data.
|
|
3459
|
-
|
|
3460
|
-
**File extension:** `.dbf` · **Read:** ✓ · **Write:** ✓ · **Geometry:** none
|
|
3461
|
-
|
|
3462
|
-
### CLI examples
|
|
3463
|
-
|
|
3464
|
-
```bash
|
|
3465
|
-
mapshaper provinces.dbf -info
|
|
3466
|
-
mapshaper provinces.dbf -filter '"BC,AB,SK".indexOf(prov) > -1' -o subset.csv
|
|
3467
|
-
mapshaper data.csv -o data.dbf
|
|
3468
|
-
```
|
|
3469
|
-
|
|
3470
|
-
### Format-specific input options
|
|
3471
|
-
|
|
3472
|
-
- `encoding=` — text encoding. If omitted, Mapshaper auto-detects, falling back to a `.cpg` sidecar file if present. Run `mapshaper -encodings` for the list of supported encodings.
|
|
3473
|
-
|
|
3474
|
-
### Format-specific output options
|
|
3475
|
-
|
|
3476
|
-
- `encoding=` — output text encoding. Default UTF-8 (with a matching `.cpg` sidecar).
|
|
3477
|
-
- `field-order=ascending` — sort columns alphabetically.
|
|
3478
|
-
|
|
3479
|
-
### Practical notes
|
|
3480
|
-
|
|
3481
|
-
- DBF holds tabular data only — no geometry.
|
|
3482
|
-
- DBF files do not declare their text encoding internally. Mapshaper auto-detects against UTF-8, Windows-1252 and a few other common encodings. See the [Shapefile encoding notes](/docs/formats/shapefile.html.md#dbf-text-encoding) for the full picture.
|
|
3483
|
-
- Field names are limited to 10 ASCII characters. Longer names are truncated on write; duplicate truncated names are disambiguated with numeric suffixes.
|
|
3484
|
-
- Field values are limited to 254 characters; longer strings will have been truncated when the file was written.
|
|
3485
|
-
|
|
3486
|
-
## External resources
|
|
3487
|
-
|
|
3488
|
-
- [Wikipedia: .dbf](https://en.wikipedia.org/wiki/.dbf) — useful overview of the format's history and dialects.
|
|
3489
|
-
- [Xbase File Format Description](https://www.clicketyclick.dk/databases/xbase/format/) — Erik Bachmann's reference for the dBase / xBase family. The standard external citation for byte-level DBF details.
|
|
3490
|
-
|
|
3491
|
-
---
|
|
3492
|
-
|
|
3493
|
-
<!-- https://mapshaper.org/docs/formats/json.html -->
|
|
3494
|
-
|
|
3495
|
-
# JSON records
|
|
3496
|
-
|
|
3497
|
-
A JSON-records file is a plain JSON array of objects, one per record, with no geometry.
|
|
3498
|
-
|
|
3499
|
-
**File extension:** `.json` · **Read:** ✓ · **Write:** ✓ · **Geometry:** none
|
|
3500
|
-
|
|
3501
|
-
A JSON-records file looks like this:
|
|
3502
|
-
|
|
3503
|
-
```json
|
|
3504
|
-
[
|
|
3505
|
-
{ "id": 1, "name": "Alice", "city": "Toronto" },
|
|
3506
|
-
{ "id": 2, "name": "Bob", "city": "Vancouver" }
|
|
3507
|
-
]
|
|
3508
|
-
```
|
|
3509
|
-
|
|
3510
|
-
### Format-specific input options
|
|
3511
|
-
|
|
3512
|
-
- `json-path=` — for files where the array is nested inside a larger object, e.g. `json-path=data/records` for `{"data": {"records": [...]}}`.
|
|
3513
|
-
|
|
3514
|
-
### Format-specific output options
|
|
3515
|
-
|
|
3516
|
-
- `ndjson` — emit newline-delimited JSON instead of an array. One record per line. Often easier to process with line-oriented tools.
|
|
3517
|
-
|
|
3518
|
-
### Practical notes
|
|
3519
|
-
|
|
3520
|
-
- When exporting non-tabular layers as JSON records, the geometry is dropped — you get just the attribute table as JSON.
|
|
3521
|
-
|
|
3522
|
-
## External resources
|
|
3523
|
-
|
|
3524
|
-
- [JSON Lines (jsonlines.org)](https://jsonlines.org/) — the spec for the newline-delimited JSON variant emitted by Mapshaper's `ndjson` option.
|
|
3525
|
-
|
|
3526
|
-
---
|
|
3527
|
-
|
|
3528
|
-
<!-- https://mapshaper.org/docs/formats/svg.html -->
|
|
3529
|
-
|
|
3530
|
-
# SVG
|
|
3531
|
-
|
|
3532
|
-
SVG is the W3C standard for vector graphics on the web. Mapshaper writes SVG and can also import its own SVG output files. Use SVG when you want to drop a non-interactive map straight into a web page or edit your map in Illustrator — it's a display format, not a data interchange format.
|
|
3533
|
-
|
|
3534
|
-
**File extension:** `.svg` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** ✓
|
|
3535
|
-
|
|
3536
|
-
### CLI examples
|
|
3537
|
-
|
|
3538
|
-
```bash
|
|
3539
|
-
mapshaper provinces.shp -o provinces.svg
|
|
3540
|
-
mapshaper provinces.shp -o width=1200 svg-data=name,pop provinces.svg
|
|
3541
|
-
mapshaper a.shp b.shp -o combined.svg
|
|
3542
|
-
```
|
|
3543
|
-
|
|
3544
|
-
### Format-specific output options
|
|
3545
|
-
|
|
3546
|
-
- `width=` — output width in pixels (default 800). Geometry is fitted to this width.
|
|
3547
|
-
- `height=` — output height in pixels. If both `width` and `height` are set, content is centred inside a `[0, 0, width, height]` viewport.
|
|
3548
|
-
- `max-height=` — cap the output height in pixels.
|
|
3549
|
-
- `pixels=` — total output area in pixels (alternative to `width=`).
|
|
3550
|
-
- `margin=` — padding between content and viewport edge (default 1 px). Pass `<left,bottom,right,top>` for asymmetric margins.
|
|
3551
|
-
- `svg-scale=` — scale in source units per pixel. Alternative to `width=` when you want a fixed scale rather than a fixed canvas size.
|
|
3552
|
-
- `svg-bbox=` — explicit `xmin,ymin,xmax,ymax` for the SVG viewport. Useful for aligning multiple SVG layers exported separately.
|
|
3553
|
-
- `fit-extent=` — use a layer (typically a single rectangle) to define the viewport.
|
|
3554
|
-
- `svg-data=` — comma-separated list of attribute fields to emit as `data-*` attributes on each `<path>`. Field names must match `[a-z_][a-z0-9_-]*`.
|
|
3555
|
-
- `id-field=` — promote one or more attribute fields to the SVG `id` attribute.
|
|
3556
|
-
- `id-prefix=` — prefix all generated layer/feature ids.
|
|
3557
|
-
- `point-symbol=square` — render points as squares instead of circles.
|
|
3558
|
-
|
|
3559
|
-
### Practical notes
|
|
3560
|
-
|
|
3561
|
-
- Each layer becomes a `<g>` group, with the layer name as the group id. Features become `<path>` (polygons/lines) or `<circle>` (points).
|
|
3562
|
-
- No data attributes are emitted unless you pass `svg-data=`.
|
|
3563
|
-
- The output is unstyled by default. Use [`-style`](/docs/reference.html.md#-style) to assign inline style attributes.
|
|
3564
|
-
- Very large or detailed layers can produce SVGs that are slow to render in browsers. Consider using [`-simplify`](/docs/reference.html.md#-simplify) before exporting.
|
|
3565
|
-
|
|
3566
|
-
## External resources
|
|
3567
|
-
|
|
3568
|
-
- [W3C SVG 2 specification](https://www.w3.org/TR/SVG2/) — the formal spec.
|
|
3569
|
-
- [MDN SVG tutorial](https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial) — the friendliest practical introduction, with browser support notes.
|
|
3570
|
-
|
|
3571
|
-
## See also
|
|
3572
|
-
|
|
3573
|
-
- [Add SVG styling for export](/docs/examples/basics.html.md#add-svg-styling-for-export)
|
|
3574
|
-
- [Quantile-classify into a color ramp](/docs/examples/basics.html.md#quantile-classify-into-a-color-ramp)
|
|
3575
|
-
- [Simplify a polygon layer for the web](/docs/examples/basics.html.md#simplify-a-polygon-layer-for-the-web)
|
|
3576
|
-
|
|
3577
|
-
---
|
|
3578
|
-
|
|
3579
|
-
<!-- https://mapshaper.org/docs/formats/snapshot.html -->
|
|
3580
|
-
|
|
3581
|
-
# Mapshaper snapshot (.msx)
|
|
3582
|
-
|
|
3583
|
-
A Mapshaper snapshot captures the current state of a session — arcs, layers, attributes, CRS metadata, topology and (when written from the web app) the command history that produced it — as a single binary file. Snapshots are Mapshaper-specific and not intended for interchange with other GIS tools; for that, use [Shapefile](/docs/formats/shapefile.html.md), [GeoPackage](/docs/formats/geopackage.html.md), [FlatGeobuf](/docs/formats/flatgeobuf.html.md) or [GeoJSON](/docs/formats/geojson.html.md).
|
|
3584
|
-
|
|
3585
|
-
**File extension:** `.msx` · **Read:** ✓ · **Write:** ✓ · **Multi-layer:** ✓
|
|
3586
|
-
|
|
3587
|
-
### When to use a snapshot
|
|
3588
|
-
|
|
3589
|
-
- **Saving work-in-progress** for later editing, with topology, layer order and (in the web app) command history intact.
|
|
3590
|
-
- **Bundling a collection of datasets** into a single compact file that is quicker to re-open than re-running an import pipeline.
|
|
3591
|
-
- **In the browser, as a quick "save point"** before doing something experimental that might fail or give the wrong result — restore the snapshot to roll back.
|
|
3592
|
-
|
|
3593
|
-
### From the CLI
|
|
3594
|
-
|
|
3595
|
-
`-o foo.msx` captures the **entire session** — every dataset and every layer Mapshaper has loaded, not just the ones currently selected by `-target`. This makes a `.msx` file a faithful "save point" of the working state, regardless of which layers happen to be active.
|
|
3596
|
-
|
|
3597
|
-
`-target` controls *visibility and stacking order*, not which layers are written:
|
|
3598
|
-
|
|
3599
|
-
- Layers matched by the active `-target` (or the `target=` option on `-o` itself) come back **visible** in the web app, with no further setup.
|
|
3600
|
-
- They are stacked in the order matched by `-target` — first targeted on the bottom, last on top — so the GUI's layer stack matches the draw order you'd get from an SVG export of the same target list.
|
|
3601
|
-
- Layers that weren't targeted come along **hidden**, parked at the bottom of the layer panel. They're still in the file (so you can pin them visible later), but they don't get in the way of the intended view.
|
|
3602
|
-
- If you want to drop layers from the snapshot rather than just hide them, run an explicit step like `-filter-layers` or `-target b -drop` before `-o foo.msx`.
|
|
3603
|
-
|
|
3604
|
-
### In the web app
|
|
3605
|
-
|
|
3606
|
-
The ribbon icon in the layer panel opens the **snapshot menu**. From there you can:
|
|
3607
|
-
|
|
3608
|
-
- **Create a snapshot** — saves to in-browser storage. These are session-scoped and **deleted when the tab closes or the page is reloaded.** For anything you want to keep, **Save snapshot to file** writes a `.msx` file you can re-open later.
|
|
3609
|
-
- **Export** a stored snapshot to a `.msx` file on disk. Persisted `.msx` files survive browser restarts and can be re-imported by drag-drop, the **Add files** button, or the `?files=` URL parameter.
|
|
3610
|
-
- **Restore** a stored snapshot into the current session.
|
|
3611
|
-
|
|
3612
|
-
## External resources
|
|
3613
|
-
|
|
3614
|
-
- [msgpack.org](https://msgpack.org/) — the binary serialization format used inside `.msx` (via the [msgpackr](https://github.com/kriszyp/msgpackr) library).
|
|
3615
|
-
|
|
3616
|
-
---
|
|
3617
|
-
|
|
3618
|
-
<!-- https://mapshaper.org/docs/examples/basics.html -->
|
|
3619
|
-
|
|
3620
|
-
# Basics
|
|
3621
|
-
|
|
3622
|
-
Short Mapshaper examples for common tasks. The filenames and field names are illustrative — substitute your own. Most recipes also work in the [web app's](/docs/essentials/web-app.html.md) console: drop the leading `mapshaper` and the input filename; the GUI should already your data loaded.
|
|
3623
|
-
|
|
3624
|
-
> Looking for the syntax of a particular command or option? See the [command reference](/docs/reference.html.md). For the JS expression context used by `-each`, `-filter`, `calc=` and other commands, see [JavaScript expressions](/docs/guides/expressions.html.md).
|
|
3625
|
-
|
|
3626
|
-
## Inspecting and exploring
|
|
3627
|
-
|
|
3628
|
-
### Print a summary of a dataset
|
|
3629
|
-
|
|
3630
|
-
```bash
|
|
3631
|
-
mapshaper input.shp -info
|
|
3632
|
-
```
|
|
3633
|
-
|
|
3634
|
-
`-info` prints the geometry type, feature count, CRS (if known), bounding box, and the name and type of every attribute field. It's the fastest way to remind yourself what a file actually contains. Add `save-to=info.json` to capture it as JSON.
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
### Count features grouped by a field
|
|
3638
|
-
|
|
3639
|
-
```bash
|
|
3640
|
-
mapshaper counties.shp \
|
|
3641
|
-
-drop geometry \
|
|
3642
|
-
-dissolve STATE calc='N = count(), POP = sum(POP)' \
|
|
3643
|
-
-o state_data.csv
|
|
3644
|
-
```
|
|
3645
|
-
|
|
3646
|
-
A dissolve with a `calc=` clause is an idiomatic way to aggregate data values.
|
|
3647
|
-
|
|
3648
|
-
## Filtering and selecting
|
|
3649
|
-
|
|
3650
|
-
### Keep features matching a condition
|
|
3651
|
-
|
|
3652
|
-
```bash
|
|
3653
|
-
mapshaper countries.geojson -filter 'CONTINENT == "Asia" && POP > 1e7' \
|
|
3654
|
-
-o output.geojson
|
|
3655
|
-
```
|
|
3656
|
-
|
|
3657
|
-
`-filter` keeps features for which the expression returns `true`.
|
|
3658
|
-
|
|
3659
|
-
### Keep features inside a bounding box
|
|
3660
|
-
|
|
3661
|
-
```bash
|
|
3662
|
-
mapshaper world.shp -clip bbox=-10,35,30,60 -o europe.shp
|
|
3663
|
-
```
|
|
3664
|
-
|
|
3665
|
-
The four bbox numbers are `xmin,ymin,xmax,ymax` in the layer's own coordinates.
|
|
3666
|
-
|
|
3667
|
-
### Keep the top N features by some attribute
|
|
3668
|
-
|
|
3669
|
-
```bash
|
|
3670
|
-
mapshaper cities.geojson \
|
|
3671
|
-
-sort POP descending \
|
|
3672
|
-
-filter 'this.id < 50' \
|
|
3673
|
-
-o top50.geojson
|
|
3674
|
-
```
|
|
3675
|
-
|
|
3676
|
-
`-sort POP descending` orders by population from largest to smallest. Features have numerical ids starting with `0`, so `this.id < 50` keeps the first 50.
|
|
3677
|
-
|
|
3678
|
-
### Drop features with empty geometry
|
|
3679
|
-
|
|
3680
|
-
```bash
|
|
3681
|
-
mapshaper messy.geojson -filter remove-empty -o clean.geojson
|
|
3682
|
-
```
|
|
3683
|
-
|
|
3684
|
-
The `remove-empty` flag drops features with missing or empty geometry.
|
|
3685
|
-
|
|
3686
|
-
## Editing attributes
|
|
3687
|
-
|
|
3688
|
-
### Add derived fields
|
|
3689
|
-
|
|
3690
|
-
```bash
|
|
3691
|
-
mapshaper counties.shp \
|
|
3692
|
-
-each 'STATE_FIPS = COUNTY_FIPS.substr(0, 2),
|
|
3693
|
-
AREA_KM2 = round(this.area / 1e6, 1)' \
|
|
3694
|
-
-o out.shp
|
|
3695
|
-
```
|
|
3696
|
-
|
|
3697
|
-
`-each` runs a JS expression on each feature; assigning to a bare name creates or updates a field. `round(x, 1)` is Mapshaper's rounding helper (one decimal here). On unprojected lat/long data, `this.area` returns square meters, dividing by 1e6 converts to square kilometers.
|
|
3698
|
-
|
|
3699
|
-
### Rename and filter fields
|
|
3700
|
-
|
|
3701
|
-
```bash
|
|
3702
|
-
mapshaper data.csv \
|
|
3703
|
-
-rename-fields POPULATION=POP,MEDIAN_INCOME=MEDIAN_INC \
|
|
3704
|
-
-filter-fields STATE,COUNTY,POPULATION,MEDIAN_INCOME \
|
|
3705
|
-
-o cleaned.csv
|
|
3706
|
-
```
|
|
3707
|
-
|
|
3708
|
-
`-rename-fields` takes `NEW=OLD` pairs. `-filter-fields` keeps only the listed fields, in the listed order — convenient for shaping CSV output.
|
|
3709
|
-
|
|
3710
|
-
### Preserve leading zeros from a CSV
|
|
3711
|
-
|
|
3712
|
-
```bash
|
|
3713
|
-
mapshaper -i counties.csv string-fields=FIPS,STATEFIPS \
|
|
3714
|
-
-o counties.shp
|
|
3715
|
-
```
|
|
3716
|
-
|
|
3717
|
-
By default Mapshaper parses any numeric-looking CSV column as a number, which silently drops leading zeros from FIPS, ZIP and similar identifier columns. `string-fields=` forces the named columns to stay as strings.
|
|
3718
|
-
|
|
3719
|
-
## Joining data
|
|
3720
|
-
|
|
3721
|
-
### Join a CSV to a Shapefile by key
|
|
3722
|
-
|
|
3723
|
-
```bash
|
|
3724
|
-
mapshaper states.shp \
|
|
3725
|
-
-join demographics.csv keys=STATE_FIPS,FIPS string-fields=FIPS \
|
|
3726
|
-
-o states_with_data.shp
|
|
3727
|
-
```
|
|
3728
|
-
|
|
3729
|
-
`keys=A,B` means "match the target's `A` field to the source's `B` field". `string-fields=FIPS` (passed through to the CSV import) keeps the FIPS code as a string so `"06"` matches `"06"` rather than being parsed to `6`. To pull just a few columns from the source, add `fields=FIELD_A,FIELD_B`.
|
|
3730
|
-
|
|
3731
|
-
### Spatial join: tag points with the polygon they fall in
|
|
3732
|
-
|
|
3733
|
-
```bash
|
|
3734
|
-
mapshaper crimes.geojson \
|
|
3735
|
-
-join precinct_polygons.shp \
|
|
3736
|
-
-o crimes_with_precinct.geojson
|
|
3737
|
-
```
|
|
3738
|
-
|
|
3739
|
-
When `keys=` is omitted, `-join` falls back to a spatial join based on the geometry types involved. Here, every point inherits the attributes of the polygon containing it. Use `fields=PRECINCT_ID,DIVISION` to keep just specific columns.
|
|
3740
|
-
|
|
3741
|
-
### Many-to-one join with an aggregation
|
|
3742
|
-
|
|
3743
|
-
```bash
|
|
3744
|
-
mapshaper precincts.shp \
|
|
3745
|
-
-join crimes.geojson calc='N = count(), AVG_SEVERITY = mean(SEVERITY)' \
|
|
3746
|
-
-o precincts_with_stats.shp
|
|
3747
|
-
```
|
|
3748
|
-
|
|
3749
|
-
A polygon-to-many-points join can use `calc=` to summarize the matched source records into one or more new fields per target feature. The functions in `calc=` are the same set documented under [`-calc`](/docs/reference.html.md#-calc).
|
|
3750
|
-
|
|
3751
|
-
## Simplifying
|
|
3752
|
-
|
|
3753
|
-
### Simplify a polygon layer for the web
|
|
3754
|
-
|
|
3755
|
-
```bash
|
|
3756
|
-
mapshaper provinces.shp \
|
|
3757
|
-
-simplify 5% keep-shapes \
|
|
3758
|
-
-clean \
|
|
3759
|
-
-o format=topojson provinces.topojson
|
|
3760
|
-
```
|
|
3761
|
-
|
|
3762
|
-
`-simplify 5%` keeps 5% of the original vertices using the default weighted Visvalingam algorithm. `keep-shapes` prevents simplification from wiping out very small polygons. `-clean` mops up any topology errors introduced by aggressive simplification. TopoJSON output is usually 2–5x smaller than the equivalent GeoJSON.
|
|
3763
|
-
|
|
3764
|
-
### Simplify multiple files with shared topology
|
|
3765
|
-
|
|
3766
|
-
```bash
|
|
3767
|
-
mapshaper -i states.shp counties.shp combine-files \
|
|
3768
|
-
-simplify 10% \
|
|
3769
|
-
-o out/
|
|
3770
|
-
```
|
|
3771
|
-
|
|
3772
|
-
`combine-files` merges the inputs into one dataset before Mapshaper builds its arc topology, so vertices shared between layers stay shared after simplification — no gaps or overlaps along state/county boundaries. Each input still gets written back out as a separate file in `out/`. In the web app, add `combine-files` in the **Advanced options** field at import time to get the same behavior.
|
|
3773
|
-
|
|
3774
|
-
### Repair topology errors
|
|
3775
|
-
|
|
3776
|
-
```bash
|
|
3777
|
-
mapshaper messy.shp -clean -o cleaned.shp
|
|
3778
|
-
```
|
|
3779
|
-
|
|
3780
|
-
`-clean` snaps near-duplicate vertices, removes tiny gaps and overlaps between polygons, and fixes self-intersecting lines. It's a safe first step before any spatial operation. You can tune `gap-fill-area=`, `sliver-control=` or `snap-interval=` if necessary.
|
|
3781
|
-
|
|
3782
|
-
## Aggregating
|
|
3783
|
-
|
|
3784
|
-
### Dissolve to a parent geography
|
|
3785
|
-
|
|
3786
|
-
```bash
|
|
3787
|
-
mapshaper counties.shp -dissolve STATE -o states.shp
|
|
3788
|
-
```
|
|
3789
|
-
|
|
3790
|
-
`-dissolve` merges adjacent polygons that share a value in the named field.
|
|
3791
|
-
|
|
3792
|
-
### Dissolve while computing per-group stats
|
|
3793
|
-
|
|
3794
|
-
```bash
|
|
3795
|
-
mapshaper counties.shp \
|
|
3796
|
-
-dissolve STATE calc='N = count(),
|
|
3797
|
-
POP = sum(POP),
|
|
3798
|
-
MEDIAN_INC = median(MEDIAN_INC)' \
|
|
3799
|
-
-o states_with_stats.shp
|
|
3800
|
-
```
|
|
3801
|
-
|
|
3802
|
-
`calc=` works inside `-dissolve` as well as `-join`. The named functions (`count`, `sum`, `mean`, `median`, `mode`, `min`, `max`, `quartile1/2/3`, `iqr`, `quantile`, `collect`, `every`, `some`, `first`, `last`) all see the same per-feature context as `-each`.
|
|
3803
|
-
|
|
3804
|
-
### Convert points to population-weighted centroids
|
|
3805
|
-
|
|
3806
|
-
```bash
|
|
3807
|
-
mapshaper cities.shp \
|
|
3808
|
-
-dissolve STATE_FIPS weight=POPULATION \
|
|
3809
|
-
-o state_centers.shp
|
|
3810
|
-
```
|
|
3811
|
-
|
|
3812
|
-
Dissolve groups of points that share a `STATE_FIPS` value into a single weighted centroid per group. The `weight=` option accepts a field name or a JS expression. Omitting a grouping field dissolves all points into one centroid.
|
|
3813
|
-
|
|
3814
|
-
## Spatial operations
|
|
3815
|
-
|
|
3816
|
-
### Erase one layer from another
|
|
3817
|
-
|
|
3818
|
-
```bash
|
|
3819
|
-
mapshaper land.shp -erase lakes.shp -o land_no_lakes.shp
|
|
3820
|
-
```
|
|
3821
|
-
|
|
3822
|
-
`-erase` removes the parts of the target layer that fall inside the source layer's polygons. Useful for masking out water, parks, or any "do-not-count" region. The inverse is `-clip`, which keeps the inside instead.
|
|
3823
|
-
|
|
3824
|
-
### Compute interior boundaries
|
|
3825
|
-
|
|
3826
|
-
```bash
|
|
3827
|
-
mapshaper counties.shp -innerlines -o county_borders.shp
|
|
3828
|
-
```
|
|
3829
|
-
|
|
3830
|
-
`-innerlines` returns the shared boundaries between polygons as a polyline layer, which is usually used for adding a stroke to interior boundaries on a styled map. Use [`-lines`](/docs/reference.html.md#-lines) instead if you want to retain both outer and inner boundaries.
|
|
3831
|
-
|
|
3832
|
-
### Generate a hex grid covering a layer
|
|
3833
|
-
|
|
3834
|
-
```bash
|
|
3835
|
-
mapshaper region.shp \
|
|
3836
|
-
-grid type=hex interval=10km name=hex \
|
|
3837
|
-
-o hex.shp
|
|
3838
|
-
```
|
|
3839
|
-
|
|
3840
|
-
`-grid` builds a regular grid (`square`, `hex`, `hex2`) covering the target's bounding box. Pair it with a spatial join to style the grid cells using interpolated count data (`-join interpolate=POPULATION`).
|
|
3841
|
-
|
|
3842
|
-
## Reprojection
|
|
3843
|
-
|
|
3844
|
-
### Project to a named CRS
|
|
3845
|
-
|
|
3846
|
-
```bash
|
|
3847
|
-
mapshaper world.geojson -proj robinson -o world_robinson.geojson
|
|
3848
|
-
```
|
|
3849
|
-
|
|
3850
|
-
`-proj` accepts EPSG codes (`EPSG:3857`), PROJ strings (`+proj=tmerc +lon_0=...`), or short aliases (`wgs84`, `webmercator`, `robinson`, `albersusa`). See the [Projections guide](/docs/guides/projections.html.md) for details.
|
|
3851
|
-
|
|
3852
|
-
### Match the projection of another file
|
|
3853
|
-
|
|
3854
|
-
```bash
|
|
3855
|
-
mapshaper points.shp -proj match=basemap.shp -o points_aligned.shp
|
|
3856
|
-
```
|
|
3857
|
-
|
|
3858
|
-
`match=` reads the CRS of another file and projects the target to match it — handy when assembling multiple datasets that need to share a coordinate system.
|
|
3859
|
-
|
|
3860
|
-
## Classification and styling
|
|
3861
|
-
|
|
3862
|
-
### Quantile-classify into a color ramp
|
|
3863
|
-
|
|
3864
|
-
```bash
|
|
3865
|
-
mapshaper covid_cases.geojson \
|
|
3866
|
-
-classify save-as=fill quantile classes=6 color-scheme=Oranges \
|
|
3867
|
-
-o themed.geojson
|
|
3868
|
-
```
|
|
3869
|
-
|
|
3870
|
-
`-classify` writes a class index or a derived value (here, a fill color) into the named field. The default is sequential quantile classification, but you can switch to `equal-interval`, `nice`, `categorical` or `non-adjacent`. Run `mapshaper -colors` to list the built-in color schemes.
|
|
3871
|
-
|
|
3872
|
-
### Add SVG styling for export
|
|
3873
|
-
|
|
3874
|
-
```bash
|
|
3875
|
-
mapshaper boundaries.shp \
|
|
3876
|
-
-style stroke="#444" stroke-width=0.5 fill=none \
|
|
3877
|
-
-style where='RANK == 0' stroke="#000" stroke-width=1.5 \
|
|
3878
|
-
-o map.svg
|
|
3879
|
-
```
|
|
3880
|
-
|
|
3881
|
-
`-style` writes SVG presentation attributes onto each feature; the `where=` form lets you set them conditionally. The result is a valid SVG you can drop into a print or web layout.
|
|
3882
|
-
|
|
3883
|
-
## Output and conversion
|
|
3884
|
-
|
|
3885
|
-
### Convert Shapefile ↔ GeoJSON
|
|
3886
|
-
|
|
3887
|
-
```bash
|
|
3888
|
-
mapshaper input.shp -o input.geojson
|
|
3889
|
-
mapshaper input.geojson -o input.shp
|
|
3890
|
-
```
|
|
3891
|
-
|
|
3892
|
-
The output format is inferred from the file extension. Use `format=` to force it (e.g. `-o format=topojson out.json`). When writing a Shapefile, Mapshaper produces `.shp`, `.shx`, `.dbf`, and `.prj`. If no output filename is given, the output file takes the name of the targeted layer.
|
|
3893
|
-
|
|
3894
|
-
### Quantized TopoJSON
|
|
3895
|
-
|
|
3896
|
-
```bash
|
|
3897
|
-
mapshaper boundaries.shp -o boundaries.topojson
|
|
3898
|
-
```
|
|
3899
|
-
|
|
3900
|
-
TopoJSON output is quantized by default — Mapshaper picks a quantization level based on the data's coordinate range. Quantization rounds coordinates to a grid, which dramatically shrinks file size. The default can be overridden with `quantization=` (e.g. `quantization=1e5`). Combined with `-simplify` and TopoJSON's shared-arc encoding, quantized output is routinely 2–5× smaller than equivalent GeoJSON.
|
|
3901
|
-
|
|
3902
|
-
### Output as ndjson
|
|
3903
|
-
|
|
3904
|
-
```bash
|
|
3905
|
-
mapshaper big.geojson -o ndjson big.ndjson
|
|
3906
|
-
```
|
|
3907
|
-
|
|
3908
|
-
Newline-delimited JSON is friendlier to line-oriented tools like jq, BigQuery, and DuckDB. One Feature per line, no enclosing FeatureCollection.
|
|
3909
|
-
|
|
3910
|
-
### Split a layer into multiple files
|
|
3911
|
-
|
|
3912
|
-
```bash
|
|
3913
|
-
mapshaper counties.shp \
|
|
3914
|
-
-split STATE \
|
|
3915
|
-
-o out/ extension=geojson
|
|
3916
|
-
```
|
|
3917
|
-
|
|
3918
|
-
`-split FIELD` partitions features into separate layers by the value of `FIELD`; `-o` to a directory then writes one file per layer, named after the field value.
|
|
3919
|
-
|
|
3920
|
-
## Workflow patterns
|
|
3921
|
-
|
|
3922
|
-
### Run a chain of commands from a file
|
|
3923
|
-
|
|
3924
|
-
```
|
|
3925
|
-
# build.txt
|
|
3926
|
-
mapshaper
|
|
3927
|
-
-i counties.shp
|
|
3928
|
-
-rename-fields POP=POPULATION
|
|
3929
|
-
-dissolve STATE calc='POP = sum(POP)'
|
|
3930
|
-
-simplify 5%
|
|
3931
|
-
-o out/states.topojson
|
|
3932
|
-
```
|
|
3933
|
-
|
|
3934
|
-
```bash
|
|
3935
|
-
mapshaper -run build.txt
|
|
3936
|
-
# or simply:
|
|
3937
|
-
mapshaper build.txt
|
|
3938
|
-
```
|
|
3939
|
-
|
|
3940
|
-
Long pipelines can be kept in a [command file](/docs/reference.html.md#command-files).
|
|
3941
|
-
|
|
3942
|
-
### Parameterize a command file
|
|
3943
|
-
|
|
3944
|
-
```bash
|
|
3945
|
-
mapshaper -vars YEAR=2024 PCT=10 -run build.txt
|
|
3946
|
-
```
|
|
3947
|
-
|
|
3948
|
-
```
|
|
3949
|
-
# build.txt
|
|
3950
|
-
mapshaper
|
|
3951
|
-
-defaults YEAR=2020 PCT=5
|
|
3952
|
-
-i sources/counties_{{YEAR}}.shp
|
|
3953
|
-
-simplify {{PCT}}%
|
|
3954
|
-
-o out/counties_{{YEAR}}.shp
|
|
3955
|
-
```
|
|
3956
|
-
|
|
3957
|
-
`{{VAR}}` placeholders are substituted at parse time. `-defaults` sets values that the caller can override with `-vars` (or with `{{env.NAME}}` for environment variables).
|
|
3958
|
-
|
|
3959
|
-
### Stop a pipeline early on bad input
|
|
3960
|
-
|
|
3961
|
-
```bash
|
|
3962
|
-
mapshaper input.csv \
|
|
3963
|
-
-calc 'N = count()' \
|
|
3964
|
-
-if 'global.N == 0' \
|
|
3965
|
-
-print 'No records, exiting' \
|
|
3966
|
-
-stop \
|
|
3967
|
-
-endif \
|
|
3968
|
-
-o out.csv
|
|
3969
|
-
```
|
|
3970
|
-
|
|
3971
|
-
`-calc` expressions can publish values to the `global` object via simple assignment. `-if`/`-stop` then guard the rest of the pipeline. Useful in scripts where bad upstream data should fail loudly rather than silently produce empty output.
|
|
3972
|
-
|
|
3973
|
-
### Increase the heap for very large files
|
|
3974
|
-
|
|
3975
|
-
```bash
|
|
3976
|
-
mapshaper-xl 16gb counties_5m.shp -simplify 10% -o counties_5m.topojson
|
|
3977
|
-
```
|
|
3978
|
-
|
|
3979
|
-
`mapshaper-xl` is a wrapper that launches Node with extra heap space (default 8 GB; pass a size to override). Use it whenever you see "JavaScript heap out of memory" errors.
|
|
3980
|
-
|
|
3981
|
-
## See also
|
|
3982
|
-
|
|
3983
|
-
- [Command reference](/docs/reference.html.md) — every command and option
|
|
3984
|
-
- [JavaScript expressions](/docs/guides/expressions.html.md) — the syntax and context used by `-each`, `-filter`, `calc=`, etc.
|
|
3985
|
-
|
|
3986
|
-
---
|
|
3987
|
-
|
|
3988
|
-
<!-- https://mapshaper.org/docs/examples/globe.html -->
|
|
3989
|
-
|
|
3990
|
-
# Globe locator map
|
|
3991
|
-
|
|
3992
|
-
Difficulty: moderate
|
|
3993
|
-
|
|
3994
|
-
<!-- mapshaper:image -->
|
|
3995
|
-
|
|
3996
|
-
### Steps
|
|
3997
|
-
|
|
3998
|
-
1. Load country polygons (source: Natural Earth)
|
|
3999
|
-
2. Project countries using the Earth from space projection
|
|
4000
|
-
3. Simplify country borders
|
|
4001
|
-
4. Derive a line layer from the country polygons
|
|
4002
|
-
5. Add a circle so we can show a background color
|
|
4003
|
-
6. Add graticule lines
|
|
4004
|
-
7. Add a dot and a label
|
|
4005
|
-
8. Style all the layers
|
|
4006
|
-
9. Export as SVG
|
|
4007
|
-
|
|
4008
|
-
### Code
|
|
4009
|
-
|
|
4010
|
-
<!-- mapshaper:code -->
|
|
4011
|
-
|
|
4012
|
-
### Notes
|
|
4013
|
-
|
|
4014
|
-
* The PROJ string `+proj=nsper +h=1e7 +lat_0=35 +lon_0=2.35` uses the near-side perspective projection (sometimes called "Earth from space") from a height of 10,000 km above the Earth. This gives a more zoomed-in appearance than the orthographic projection (`+proj=ortho`), which is also commonly used for globe maps.
|
|
4015
|
-
* `-graticule polygon` creates a polygon that matches the boundary of the graticule, to give the map a background shape.
|
|
4016
|
-
* `-filter true +` is the Mapshaper idiom for copying a layer (the filter expression is `true`, which means every feature is retained).
|
|
4017
|
-
|
|
4018
|
-
### Assets
|
|
4019
|
-
|
|
4020
|
-
<!-- mapshaper:assets -->
|
|
4021
|
-
|
|
4022
|
-
---
|
|
4023
|
-
|
|
4024
|
-
<!-- https://mapshaper.org/docs/examples/us-states.html -->
|
|
4025
|
-
|
|
4026
|
-
# U.S. state map
|
|
4027
|
-
|
|
4028
|
-
Difficulty: easy
|
|
4029
|
-
|
|
4030
|
-
<!-- mapshaper:image -->
|
|
4031
|
-
|
|
4032
|
-
### Steps
|
|
4033
|
-
|
|
4034
|
-
1. Load state/province polygons (source: Natural Earth)
|
|
4035
|
-
2. Keep only U.S. states
|
|
4036
|
-
3. Project to the "Albers USA" projection
|
|
4037
|
-
4. Assign random, non-adjacent colors
|
|
4038
|
-
5. Export as SVG
|
|
4039
|
-
|
|
4040
|
-
### Code
|
|
4041
|
-
|
|
4042
|
-
<!-- mapshaper:code -->
|
|
4043
|
-
|
|
4044
|
-
### Notes
|
|
4045
|
-
|
|
4046
|
-
* To see the list of built-in color schemes to use with `-classify`, run `mapshaper -colors`. You can also use `colors=random`.
|
|
4047
|
-
* The Albers USA projection (`-proj albersusa`) is a custom projection used by The New York Times for U.S. maps.
|
|
4048
|
-
|
|
4049
|
-
### Assets
|
|
4050
|
-
|
|
4051
|
-
<!-- mapshaper:assets -->
|
|
4052
|
-
|
|
4053
|
-
---
|