Geo Formats: Working with Data and APIs

Data is presented in different formats. This guide contains information, tips and suggestions on how to use some of the formats presented in the Open Data Portal as well as how to work with some of the application programming interfaces (APIs).

On this page

GeoTIFF

Browse GeoTIFF Datasets

GeoTIFF files are similar to standard graphical TIFF files; they are viewable using a regular image viewer that supports the TIFF format. However, GeoTIFF files contain extra metadata related to the geographical nature of the image. Refer to the official sites on GeoTIFF for additional information on the format:

An open source library exists to enable your program the ability to extract the metadata information from a GeoTIFF. Below is an example on how to install and run a simple C program to extract metadata information.

Here we are using the Homebrew package manager on OSX to install the GeoTIFF library and use a utility to read a GeoTIFF:

$ brew install libgeotiff 
$ listgeo <filename>.tif 

Once the GeoTIFF library is installed, you should be able to compile a simple C program to read the metadata:

/**
 * Simple example of a C program to read metadata from a GeoTIFF.
 *
 * NOTE: This is a simple example with very little error handling.
 *
 * @license http://data.gc.ca/eng/open-government-licence-canada
 */
#include <stdio.h>
#include <stdlib.h>
#include <xtiffio.h>
#include <geotiffio.h>
#include <geokeys.h>

int main(void) {

    TIFF *tif  = (TIFF*)0; // TIFF-level descriptor
    GTIF *gtif = (GTIF*)0; // GeoKey-level descriptor
    double major_axis_key = 0;

    // Open TIFF file
    tif = XTIFFOpen("geo.tif", "r");
    if (!tif) {
        printf("Unable to open GeoTIFF file.\n");
        exit(-1);
    }

    // Open GTIF Key parser; keys will be read at this time.
    gtif = GTIFNew(tif);
    if (!gtif) {
        printf("Unable to access GTIF metadata.\n");
        exit(-1);
     }
    
    // Read a GTIF Key pair
    if (GTIFKeyGet(gtif, GeogSemiMajorAxisGeoKey, &major_axis_key, 0, 1)) {
        printf("GeogSemiMajorAxisGeoKey: %f\n", major_axis_key);
    } else {
        printf("Unable to locate GeogSemiMajorAxisGeoKey\n");
    }

    // Clean up and exit.
    GTIFFree(gtif);
    XTIFFClose(tif);
    return 1;
}

Output:

$ ./geotiffmeta 
GeogSemiMajorAxisGeoKey: 6378137.000000 

Shapefiles

Browse Shapefile Datasets

ESRI Shapefiles (SHP) are popular geospatial vector data files which are accompanied by a series of files necessary to use the SHP file. As listed on Wikipedia, the following is usually packaged into a compressed ZIP file:

Mandatory files:

  • .shp — shape format; the feature geometry itself
  • .shx — shape index format; a positional index of the feature geometry to allow seeking forwards and backwards quickly
  • .dbf — attribute format; columnar attributes for each shape, in dBase IV format

Optional files:

  • .prj — projection format; the coordinate system and projection information, a plain text file describing the projection using well-known text format
  • .sbn and .sbx — a spatial index of the features
  • .fbn and .fbx — a spatial index of the features for shapefiles that are read-only
  • .ain and .aih — an attribute index of the active fields in a table
  • .ixs — a geocoding index for read-write shapefiles
  • .mxs — a geocoding index for read-write shapefiles (ODB format)
  • .atx — an attribute index for the .dbf file in the form of shapefile.columnname.atx (ArcGIS 8 and later)
  • .shp.xml — geospatial metadata in XML format, such as ISO 19115 or other XML schema
  • .cpg — used to specify the code page (only for .dbf) for identifying the character encoding to be used

Source: Shapefile - Wikipedia.org

Links to useful tools:

Converting Shapefiles to GeoJSON

There is a great write-up by Ben Balter on converting Shapefiles to GeoJSON using gdal’s ogr2ogr utility as follows:

ogr2ogr -f GeoJSON -t_srs crs:84 [name].geojson [name].shp 
Date modified: