Getting started using the mapshaper command line

Author : admin   2023-03-24 09:45:38 Browse: 1014

Abstract: This article will try to use examples to introduce some useful mapshaper commands, so that everyone can have a brief understandin...

This article will try to use examples to introduce some useful mapshaper commands, so that everyone can have a brief understanding of mapshaper.

Get help using mapshaper

# View all commands supported by mapshaper
mapshaper -h

# View help for using specific commands
mapshaper -h simplify

As shown in the figure:

Get help using mapshaper

Get Dataset Information

# Obtain the information and field list of the dataset
mapshaper mystery_ data.json -info

# Calculate certain statistical fields
mapshaper provinces.shp -calc 'min(INCOME)' -calc 'median(INCOME)' -calc 'max(INCOME)'

As shown in the figure:

Get Dataset Information

Convert between file formats

# Convert all shp data in the directory into Geojson
mapshaper *.shp -o format=geojson

#Convert a CSV file with latitude and longitude into a GeoJSON point type
mapshaper locations.csv -points x=lng y=lat -o format=geojson

As shown in the figure:

Convert between file formats

Simplify polygons and polylines

# Simplify by 10%
mapshaper counties.shp -simplify 10% -o out.shp

### Cut polygon, line, and point layers using polygon layers
mapshaper states.shp -clip land_area.shp -o clipped.shp

Make polygon layers erase polygon, line, and point layers

mapshaper land_areas.shp -erase water_bodies.shp -o erased.shp

Blend Polygons

# Merge multiple polygons in a feature layer into a single polygon
mapshaper states.shp -dissolve -o country.shp

Connect an external data table to a feature layer

# Connect CSV to SHP file
# Tip: FIPS: str is used to prevent FIPS fields from being converted to numeric type data. Here, it can be forcibly specified as a string
mapshaper states.shp -join demographics.csv keys=STATE_FIPS,FIPS field-types=FIPS:str -o joined.shp

# Connect Dbf to shp file
mapshaper states.shp -join states2.dbf keys=STATE,STATE -o joined.shp

Edit Property Sheet

# Add fields to shp data (JS syntax)
mapshaper counties.shp -each "STATE_FIPS=CNTY_FIPS.substr(0, 2), AREA=$.area"

Chain Command Call

Example:

# Generate a tract-level Shapefile of populated areas by dissolving census blocks with non-zero population.
mapshaper tabblock2010_36_pophu.shp \
  -each 'TRACT=BLOCKID10.substr(0,11)' \
  -filter 'POP10 > 0' \
  -dissolve TRACT sum-fields=POP10 \
  -o out.shp
# From a county-level Shapefile, generate files for state and national boundaries.
mapshaper counties.shp \
  -dissolve STATE_FIPS name=states \
  -dissolve + name=usa \
  -o out.shp

Is it particularly like lodash?

Using Layers

Most mapshaper commands apply to data features. A layer is a collection of features with the same geometric type and a consistent set of data attributes (or without data attributes). Mapshaper supports polygon, polyline, and point layers, and for all of these types, a single feature may contain a geometric shape, multiple shapes, or no shapes (i.e., empty/empty geometry).

The easiest way to use mapshaper is to import a single level feature, edit it, and save it to a file:

mapshaper counties.shp -filter '$.isNull === false' -o counties_notnull.shp

The 0.2.0 version introduces support for multiple layers and a new syntax for selecting which layers a command should target -target<layers>. Commands use commas to separate one or more layers. Most commands support the target=<layer(s)> option.

The following examples show how to import a province boundary layer, create a new layer that contains only shared boundaries, simplify geometry, and save both layers as GeoJSON files. In this example, -innerlines invokes the command with two options:+ Create a new layer instead of replacing the target layer, name=lines Rename the new layer. The output is two files, out/provinces.json and out/lines.json.

mapshaper provinces.shp \
  -simplify 20% \
  -innerlines + name=lines \
  -target provinces,lines \
  -o format=geojson out/

When importing TopoJSON files, mapshaper treats each named object as a separate layer. The following example shows how to import the TopoJSON file states, extract Hawaiian features, and save them as GeoJSON files.

Calling -filter target=states applies the command to the layer named states, and uses -o to save the file.

mapshaper usa.topojson \
  -filter 'STATE == "HI"' target=states \
  -o out/hawaii.json format=geojson
    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).