Home | History | Annotate | Download | only in raster-gdal
      1 Reading Geospatial Raster files with GDAL {#tutorial_raster_io_gdal}
      2 =========================================
      3 
      4 Geospatial raster data is a heavily used product in Geographic Information Systems and
      5 Photogrammetry. Raster data typically can represent imagery and Digital Elevation Models (DEM). The
      6 standard library for loading GIS imagery is the Geographic Data Abstraction Library [(GDAL)](http://www.gdal.org). In this
      7 example, we will show techniques for loading GIS raster formats using native OpenCV functions. In
      8 addition, we will show some an example of how OpenCV can use this data for novel and interesting
      9 purposes.
     10 
     11 Goals
     12 -----
     13 
     14 The primary objectives for this tutorial:
     15 
     16 -   How to use OpenCV [imread](@ref imread) to load satellite imagery.
     17 -   How to use OpenCV [imread](@ref imread) to load SRTM Digital Elevation Models
     18 -   Given the corner coordinates of both the image and DEM, correllate the elevation data to the
     19     image to find elevations for each pixel.
     20 -   Show a basic, easy-to-implement example of a terrain heat map.
     21 -   Show a basic use of DEM data coupled with ortho-rectified imagery.
     22 
     23 To implement these goals, the following code takes a Digital Elevation Model as well as a GeoTiff
     24 image of San Francisco as input. The image and DEM data is processed and generates a terrain heat
     25 map of the image as well as labels areas of the city which would be affected should the water level
     26 of the bay rise 10, 50, and 100 meters.
     27 
     28 Code
     29 ----
     30 
     31 @include cpp/tutorial_code/HighGUI/GDAL_IO/gdal-image.cpp
     32 
     33 How to Read Raster Data using GDAL
     34 ----------------------------------
     35 
     36 This demonstration uses the default OpenCV imread function. The primary difference is that in order
     37 to force GDAL to load the image, you must use the appropriate flag.
     38 @code{.cpp}
     39 cv::Mat image = cv::imread( argv[1], cv::IMREAD_LOAD_GDAL );
     40 @endcode
     41 When loading digital elevation models, the actual numeric value of each pixel is essential and
     42 cannot be scaled or truncated. For example, with image data a pixel represented as a double with a
     43 value of 1 has an equal appearance to a pixel which is represented as an unsigned character with a
     44 value of 255. With terrain data, the pixel value represents the elevation in meters. In order to
     45 ensure that OpenCV preserves the native value, use the GDAL flag in imread with the ANYDEPTH flag.
     46 @code{.cpp}
     47 cv::Mat dem = cv::imread( argv[2], cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH );
     48 @endcode
     49 If you know beforehand the type of DEM model you are loading, then it may be a safe bet to test the
     50 Mat::type() or Mat::depth() using an assert or other mechanism. NASA or DOD specification documents
     51 can provide the input types for various elevation models. The major types, SRTM and DTED, are both
     52 signed shorts.
     53 
     54 Notes
     55 -----
     56 
     57 ### Lat/Lon (Geographic) Coordinates should normally be avoided
     58 
     59 The Geographic Coordinate System is a spherical coordinate system, meaning that using them with
     60 Cartesian mathematics is technically incorrect. This demo uses them to increase the readability and
     61 is accurate enough to make the point. A better coordinate system would be Universal Transverse
     62 Mercator.
     63 
     64 ### Finding the corner coordinates
     65 
     66 One easy method to find the corner coordinates of an image is to use the command-line tool gdalinfo.
     67 For imagery which is ortho-rectified and contains the projection information, you can use the [USGS
     68 EarthExplorer](http://http://earthexplorer.usgs.gov).
     69 @code{.bash}
     70 \f$> gdalinfo N37W123.hgt
     71 
     72    Driver: SRTMHGT/SRTMHGT File Format
     73    Files: N37W123.hgt
     74    Size is 3601, 3601
     75    Coordinate System is:
     76    GEOGCS["WGS 84",
     77    DATUM["WGS_1984",
     78 
     79    ... more output ...
     80 
     81    Corner Coordinates:
     82    Upper Left  (-123.0001389,  38.0001389) (123d 0' 0.50"W, 38d 0' 0.50"N)
     83    Lower Left  (-123.0001389,  36.9998611) (123d 0' 0.50"W, 36d59'59.50"N)
     84    Upper Right (-121.9998611,  38.0001389) (121d59'59.50"W, 38d 0' 0.50"N)
     85    Lower Right (-121.9998611,  36.9998611) (121d59'59.50"W, 36d59'59.50"N)
     86    Center      (-122.5000000,  37.5000000) (122d30' 0.00"W, 37d30' 0.00"N)
     87 
     88     ... more output ...
     89 @endcode
     90 Results
     91 -------
     92 
     93 Below is the output of the program. Use the first image as the input. For the DEM model, download
     94 the SRTM file located at the USGS here.
     95 [<http://dds.cr.usgs.gov/srtm/version2_1/SRTM1/Region_04/N37W123.hgt.zip>](http://dds.cr.usgs.gov/srtm/version2_1/SRTM1/Region_04/N37W123.hgt.zip)
     96 
     97 ![Input Image](images/gdal_output.jpg)
     98 
     99 ![Heat Map](images/gdal_heat-map.jpg)
    100 
    101 ![Heat Map Overlay](images/gdal_flood-zone.jpg)
    102