Sometimes, when we want to peek a shapefile, see what’s the shape type it is, how many records it has, or find out what properties it contains. There are some free tools to visualize it. But most of the tools are too heavy or maybe not possible to support multiple platforms such as macOS or Linux. Here is a cross platform command line tool to inspect a specified shapefile on our local machine with Ginkgoch map library. It provides functions to inspect a specified shapefile’s header, fields, records etc. You could see, it is totally free and open source. Enjoy it!

View project in Github

shapefile cli

This command line tool is created by Ginkgoch Map Library which is powerful map library written by Node.js for building cross platform map software for desktop, web service and mobile applications with your own GIS data.

Install

1
2
3
4
5
6
7
8
9
# global install
npm i -g ginkgoch-shapefile-cli

# local install
npm i --save ginkgoch-shapefile-cli

## Help
```terminal
shapefile-cli --help

Usage: index command [options] <file>

Options:

1
2
-V, --version                  output the version number
-h, --help output usage information

Commands:

1
2
3
4
show-header [options] <file>   output a specified shapefile header information
show-fields [options] <file> output a specified shapefile fields information
show-records [options] <file> output a specified shapefile records information
convert-geojson [options] <file> Convert shapefile to GeoJson

Show Header

Show header help

1
node index.js show-header --help

Usage: show-header [options] <file>

Options:

1
2
-p, --pretty  output header with pretty table format
-h, --help output usage information

Example - print a shapefile’s header with pretty table format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
shapefile-cli show-header ~/Downloads/data/shp/USStates.shp -p
╔════════════╤═════════════════════╗
║ fileCode │ 9994 ║
╟────────────┼─────────────────────╢
║ fileLength │ 222344 ║
╟────────────┼─────────────────────╢
║ version │ 1000 ║
╟────────────┼─────────────────────╢
║ fileType │ Polygon ║
╟────────────┼─────────────────────╢
║ minx │ -178.21502685546875 ║
╟────────────┼─────────────────────╢
║ miny │ 18.924781799316406 ║
╟────────────┼─────────────────────╢
║ maxx │ -66.9698486328125 ║
╟────────────┼─────────────────────╢
║ maxy │ 71.40664672851562 ║
╚════════════╧═════════════════════╝

Show Fields

Show fields help

1
shapefile-cli show-fields --help

Usage: show-fields [options] <file>

Options:

1
2
-p, --pretty  output fields with a pretty table format
-h, --help output usage information

Example - print a shapefile’s fields info in pretty table format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
shapefile-cli show-fields ~/Downloads/data/shp/USStates.shp -p
╔════════════╤══════╤════════╤═════════╗
║ name │ type │ length │ decimal ║
╟────────────┼──────┼────────┼─────────╢
║ AREA │ N │ 12 │ 3 ║
╟────────────┼──────┼────────┼─────────╢
║ PERIMETER │ N │ 12 │ 3 ║
╟────────────┼──────┼────────┼─────────╢
║ STATE_ │ N │ 11 │ 0 ║
╟────────────┼──────┼────────┼─────────╢
║ STATE_ID │ N │ 11 │ 0 ║
╟────────────┼──────┼────────┼─────────╢
║ STATE_NAME │ C │ 25 │ NaN ║
╟────────────┼──────┼────────┼─────────╢
║ STATE_FIPS │ C │ 2 │ NaN ║
╟────────────┼──────┼────────┼─────────╢
║ SUB_REGION │ C │ 7 │ NaN ║
╟────────────┼──────┼────────┼─────────╢
║ STATE_ABBR │ C │ 2 │ NaN ║
╟────────────┼──────┼────────┼─────────╢
║ POP1990 │ N │ 9 │ 0 ║
╟────────────┼──────┼────────┼─────────╢
║ RECID │ N │ 9 │ 0 ║
╚════════════╧══════╧════════╧═════════╝

Show Records

Show records help

1
shapefile-cli show-records --help

Usage: show-records [options] <file>

Options:

1
2
3
4
5
-l, --limit <n>        returning records limit. accept any number. 0 means all records. default to 10
-c, --columns <items> returning columns include in the results. Multiple columns are supported by separater ",". Default to all columns
-g, --geom includes geometry in the returned content
-p, --pretty output records with a pretty table format
-h, --help output usage information

Example - print a shpefiles’s top 10 records info with field name STATE_NAME, RECID and geometry in pretty table format.

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
shapefile-cli show-records ~/Downloads/data/shp/USStates.shp -g -l 10 -c STATE_NAME,RECID

╔══════════════╤═══════╤════════════════════════════════╗
║ STATE_NAME │ RECID │ geom ║
╟──────────────┼───────┼────────────────────────────────╢
║ Washington │ 1 │ [[{"x":-122.40074920654297,... ║
╟──────────────┼───────┼────────────────────────────────╢
║ Montana │ 2 │ [[{"x":-111.4746322631836,"... ║
╟──────────────┼───────┼────────────────────────────────╢
║ Maine │ 3 │ [[{"x":-69.77778625488281,"... ║
╟──────────────┼───────┼────────────────────────────────╢
║ North Dakota │ 4 │ [[{"x":-98.73005676269531,"... ║
╟──────────────┼───────┼────────────────────────────────╢
║ South Dakota │ 5 │ [[{"x":-102.78793334960938,... ║
╟──────────────┼───────┼────────────────────────────────╢
║ Wyoming │ 6 │ [[{"x":-104.05310821533203,... ║
╟──────────────┼───────┼────────────────────────────────╢
║ Wisconsin │ 7 │ [[{"x":-87.74845123291016,"... ║
╟──────────────┼───────┼────────────────────────────────╢
║ Idaho │ 8 │ [[{"x":-117.02530670166016,... ║
╟──────────────┼───────┼────────────────────────────────╢
║ Vermont │ 9 │ [[{"x":-73.25850677490234,"... ║
╟──────────────┼───────┼────────────────────────────────╢
║ Minnesota │ 10 │ [[{"x":-91.73021697998047,"... ║
╚══════════════╧═══════╧════════════════════════════════╝

Reading complete. 10/51 record(s)

Tips:
- set option -l, --limit to 0 to read all records
- use option -g, --geom to 0 to read all records with geometry
- use option -p, --pretty to return data with pretty table format
- use option -c, --column to return necessary fields. e.g. -c field1,field2 returns two fields

Convert to GeoJson

Show records help

1
shapefile-cli convert-geojson --help

Usage: convert-geojson [options] <file>

Options:

1
2
3
-c, --columns <items>  returning columns include in the results. Multiple columns are supported by separater ",". Default to all columns
-o, --output <value> output file path. If only directory is specified, the same file name will be used. Default to the same folder of the sourceShapefile
-h, --help output usage information

Example - converts a shapefile to GeoJson format.

1
2
3
shapefile-cli convert-geojson ./tests/data/USStates.shp

Conversion complete. New file is saved at ./tests/data/USStates.json.

Reference