Repository for Ideas & Research
Open Source GIS, Hydrologic Modeling, Optimization

How to know the map type of a GRASS GIS archive file

GRASS GIS provides the r.pack and v.pack modules that we can use to export raster and vector maps, respectively, to archive files in the .pack extension. However, unless we specify the map type explicitly in the output parameter, there is no way to know which type an archive file is for without trying r.pack or v.pack first. These modules will tell us which type the archive file is for. What if we don’t have access to GRASS GIS for whatever reason?

Pack files are actually .tar.gz files, so we can use tar to look into the list of contents in a pack file without uncompressing it.

> tar -tf map.pack # I know it's a map, but is it raster or vector?
map/cidx
map/coor
map/dbln
map/head
map/hist
map/sidx
map/topo
db.sqlite
PROJ_INFO
PROJ_UNITS
PROJ_EPSG

The above pack file is a vector map because there is db.sqlite, but checking for db.sqlite only works when a map is linked to a database. We could use map/dbln because this metadata file always exists, but we have to pass the map name + /dbln to tar like this:

> tar -tf map.pack map/dbln # exit code 0 if vector, exit code 2 if raster

Let’s take a look at a raster pack file.

> tar -tf map.pack
map/PROJ_EPSG
map/PROJ_INFO
map/PROJ_UNITS
map/cats
map/cell
map/cell_misc/
map/cell_misc/nullcmpr
map/cell_misc/range
map/cell_misc/stats
map/cellhd
map/colr
map/hist

Unlike the contents of vector pack files, those of racter pack files are all contained inside a root directory. We can use this fact to simplify our check.

> tar -tf map.pack PROJ_INFO > /dev/null 2>&1 # keep it quiet
> echo $? # exit code 0 if vector, exit code 2 if raster

In a shell script,

if tar -tf map.pack PROJ_INFO > /dev/null 2>&1; then
	echo vector
else
	echo raster
fi

References

Please leave this field blank: