mapshaper - Working with South Korea's Administrative District (Haengjeong-dong) Map Data
This post was migrated from Tistory. You can find the original here.
Map data and visualization
2024.10.01 - [JS,Node,React] - Map visualization with React and D3.js
I covered map data a bit last time.
Let’s dig a little deeper into the mapshaper commands for getting the map data you want.
https://github.com/vuski/admdongkor
Grab the GeoJSON for South Korea’s administrative districts (haengjeong-dong) here,
then use the mapShaper web console to type in commands and shape the map data however you need.
mapshaper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
$ -help
Usage: mapshaper -<command> [options] ...
I/O commands
-i input one or more files
-o output edited content
Editing commands
-affine transform coordinates by shifting, scaling and rotating
-classify assign colors or values using one of several methods
-clean fixes geometry issues, such as polygon overlaps and gaps
-clip use a polygon layer to clip another layer
-colorizer define a function to convert data values to color classes
-dashlines split lines into sections, with or without a gap
-dissolve merge features within a layer
-dissolve2 merge adjacent polygons (repairs overlaps and gaps)
-divide divide lines by polygons, copy polygon data to lines
-dots fill polygons with dots of one or more colors
-drop delete layer(s) or elements within the target layer(s)
-each create/update/delete data fields using a JS expression
-erase use a polygon layer to erase another layer
-explode divide multi-part features into single-part features
-filter delete features using a JS expression
-filter-fields retain a subset of data fields
-filter-islands remove small detached polygon rings (islands)
-filter-slivers remove small polygon rings
-graticule create a graticule layer
-grid create a grid of square or hexagonal polygons
-include import JS data and functions for use in JS expressions
-inlay inscribe a polygon layer inside another polygon layer
-innerlines convert polygons to polylines along shared edges
-join join data records from a file or layer to a layer
-lines convert a polygon or point layer to a polyline layer
-merge-layers merge multiple layers into as few layers as possible
-mosaic convert a polygon layer with overlaps into a flat mosaic
-point-grid create a rectangular grid of points
-points create a point layer from a different layer type
-polygons convert polylines to polygons
-proj project your data (using Proj.4)
-rectangle create a rectangle from a bbox or target layer
-rectangles create a rectangle for each feature in a layer
-rename-fields rename data fields
-rename-layers assign new names to layers
-simplify simplify the geometry of polygon and polyline features
-snap snap together nearby vertices
-sort sort features using a JS expression
-split split a layer into single-feature or multi-feature layers
-split-on-grid split features into separate layers using a grid
-style set SVG style properties using JS or literal values
-symbols symbolize points as arrows, circles, stars, polygons, etc.
-target set active layer (or layers)
-union create a flat mosaic from two or more polygon layers
-uniq delete features with the same id as a previous feature
Experimental commands (may give unexpected results)
-cluster group polygons into compact clusters
-data-fill fill in missing values in a polygon layer
-frame create a rectangular map frame layer at a given display width
-fuzzy-join join points to polygons, with data fill and fuzzy match
-require require a Node module or ES module to use in JS expressions
-run create commands on-the-fly and run them
-scalebar add a simple scale bar to SVG output
-shape create a polyline or polygon from coordinates
-subdivide recursively split a layer using a JS expression
Control flow commands
-if run the following commands if a condition is met
-elif test an alternate condition; used after -if
-else run commands if all preceding -if/-elif conditions are false
-endif mark the end of an -if sequence
-stop stop processing (skip remaining commands)
Informational commands
-calc calculate statistics about the features in a layer
-colors print list of color scheme names
-comment add a comment to the sequence of commands
-encodings print list of supported text encodings (for .dbf import)
-help, -h print help; takes optional command name
-info print information about data layers
-inspect print information about a feature
-print print a message to stdout
-projections print list of supported projections
-quiet inhibit console messages
-verbose print verbose processing messages
-version, -v print mapshaper version
Enter mapshaper -help <command> to view options for a single command
There are quite a few commands available. Let’s look at a handful of them.
The console is in the upper right corner.
-info shows information about the .geojson file, along with the key-value pairs of one sample data field.
A “field” here refers to a key inside the properties object.
-filter: lets you search using field values as conditions.
Out of 3,554 features, 426 remain. If you export at this point, only the filtered data gets exported.
You can use conditional expressions.
-dissolve: merge features within a layer — lets you merge layers together.
Using -dissolve <property> merges features based on that property, leaving only 17 features.
-clip: keeps only the data within a specified boundary.
-each: create/update/delete data fields using a JS expression
It seems you can use this to add new fields, modify existing field values, and do all sorts of other things.
Looking back at the -help output from earlier, there are many more commands available.
If a single map data file (geojson, shapefile, topojson, etc.) is too limiting on its own, you can join and work with multiple files together.
If you need more complex commands, or the web console feels inconvenient, install mapshaper via Node and use it locally instead.





