shapy
is a toy project that showcases the very basic usage of the picocli
framework for creating powerful and elegant Java console applications.
This cli application is capable of reading a shapefile binary file according to the ESRI Shapefile Technical Description and outputting its header information and features coordinates.
Working with shapefiles for years using other people's shapefile reader implementations,
I thought that it would be interesting to write my own one in some other language than Python.
To keep the boilerplate code simple, only point shapefiles are supported.
shapy
doesn't provide functionality to interact with the features attributes
which are stored in dBase format.
Implementing support for other geometries would be trivial and so would be reading bytes out of .dbf
files.
Reading shapefile involves reading bytes sequentially which is quite straightforward to do using DataInputStream. Because the integers and double-precision integers in a shapefile binary file can be stored both in little endian and in big endian byte order, an external utility - EndianUtils - was used to deal with different endian systems.
Unit tests are written using the JUnit4 framework and JaCoCo library is used for generating HTML code coverage reports.
The picocli
documentation provides multiple options on
how to package the cli application for distribution.
To avoid providing class paths to the picocli
, other dependencies, and the shapy
's compiled classes
when calling the cli application, a Maven plugin
Appassembler came in handy.
It can be used to generate a single .jar
artifact which contains the application's code as well as all dependencies
artifacts:
$ mvn clean compile assembly:single
$ java -jar target/shapy-1.0-SNAPSHOT-jar-with-dependencies.jar show-header "C:\GIS\test_data\sites.shp"
The path to artifact can be simplified by using an alias if you have access to Bash:
$ alias shapy="java -jar target/shapy-1.0-SNAPSHOT-jar-with-dependencies.jar"
$ shapy show-header "C:\GIS\test_data\sites.shp"
The full build command (with tests and code coverage reporting):
$ mvn clean compile test assembly:single
$ alias shapy="java -jar target/shapy-1.0-SNAPSHOT-jar-with-dependencies.jar"
$ shapy show-header "C:\GIS\test_data\sites.shp"
Usage: shapy [-hV] [COMMAND]
Command line utility to inspect shapefiles.
-h, --help Show this help message and exit.
-V, --version Print version information and exit.
Commands:
show-header Show shapefile header information.
show-features Show shapefile features information.
Usage: shapy show-header <path>
Show shapefile header information.
<path> The shapefile to inspect.
Usage: shapy show-features [--limit=<limit>] <path>
Show shapefile features information.
<path> The shapefile to inspect.
--limit=<limit> The number of features to show.