Project 1811 gdal_question 02 - geopaparazzi/Spatialite-Tasks-with-Sql-Scripts GitHub Wiki

<-- 'Index Page for RasterLite2'

<-- 'Project 1811'

<-- 'Question of 2015-06-10 from gdal-dev'

--> 'Question 02'

--> 'Answer to question 3: Can I read a part of the GeoTIFF using GDALRasterIO and write as PNG with the same transparency preserved.'

<-- 'Answer to question 1: Can I create a transparent GeoTIFF like PNG with alpha channel ?'


2. Can I used gdal_rasterize to draw shape over the GeoTIFF with transparency


Goal:

  • create an png image
    • which is a combination of the pgn-image created in 'Question 3'

QGIS :

  • a rasterized image of the streets of 1786 (in green - 0,173,83)
  • a rasterized image of the streets of 1844 (in red - 255,0,0)

With the exception of the given colour, the commands are the same:

gdal_rasterize -tr 0.475211759942328 0.475212956422887 -te 24800 20700 25800 21300 -ot Byte -init 192 -a_nodata 192 -burn 0 -burn 173 -burn 83 -sql "SELECT ST_Buffer(soldner_segment,2.5) FROM segments_berlin_1786" ../sources/berlin_segments_qgis.db segments_berlin_1786_green.tif

Parameters:

Use of the following parameters only for new images

  • -a_nodata, -a_srs, -co, -init, -of, -ot, -tap, -te, -tr and -ts

For creation of new image only:

  • -a_nodata

    • 192
      • read value from gdalinfo
  • -te: [ulx (min_x) lry (min_y) lrx (max_x) uly (max_y)]

    • 24800,20700,25800,21300
      • --projwin [ulx (min_x) uly (max_y) lrx (max_x) lry (min_y)]
        • 24800 21300 25800 20700
          • do not get confused at the different order between -te and projwin
  • -tr (xres yres)

    • 0.475211759942328 0.475212956422887
      • use the resolution found in the final png-image with gdalinfo
        • Pixel Size = (0.475211759942328,-0.475212956422887)
          • Note: the yres must be a positive value
  • -init: Pre-initialize the output image bands

    • 192
  • -ot: for an RGB Image

    • Byte

For creation of both new image and existing image:

  • -burn the RGB values desired

    • green (dark) : 0,173,83
      • -burn 0 -burn 173 -burn 83
    • red : 255,0,0
  • -sql

    • this is needed for 2 reasons:
      • the used view has more than one field/column
        • using -l with the view-name returns a blank image
        • thus SELECT soldner_segment FROM segments_berlin_1786
          • soldner_segment will be used
            • Warning: this will cause the created line to be 1 pixel wide
      • ST_Buffer(): this will set the width of the created line
      • the final sql used was
        • SELECT ST_Buffer(soldner_segment,2.5) FROM segments_berlin_1786
  • ogr-source

    • in this case I used a spatialite database
  • output-file

Note:

  • the combination of -init and -ot seem to insure that the created image is an RGB image
    • and thus show up correctly in QGIS (otherwise the lines are black, Grayscale)

Next step: combine the 3 images with:

  • 'gdalbuildvrt'
    • first create a file with a list of the input-images
      • 1811.Neuester_Grundriss_von_Berlin.streets.txt
      • for use with the -input_file_list parameter
1811.Neuester_Grundriss_von_Berlin.5800.3068.area_rote_rathausturm_panorama.png
segments_berlin_1844_red.tif
segments_berlin_1786_green.tif

Note:

  • the order is important
    • that that comes last, wins (i.e. will be shown)
      • in this case I want the 'younger/red/1844' streets
        • to be over-painted by the 'older/green/1786' streets
          • both should over-paint the map
gdalbuildvrt -overwrite -b 1 -b 2 -b 3 -resolution highest -vrtnodata '192' -input_file_list 1811.Neuester_Grundriss_von_Berlin.streets.txt 1811.Neuester_Grundriss_von_Berlin.streets.vrt
# create the final png, after checking the .vrt file in QGIS
gdal_translate -of PNG  1811.Neuester_Grundriss_von_Berlin.streets.vrt 1811.Neuester_Grundriss_von_Berlin.streets.3068.png

The final result of 1811.Neuester_Grundriss_von_Berlin.streets.3068.png:

PNG :

All roads lead to Rome

  • this is one, of possibly, many

--> 'Answer to question 3: Can I read a part of the GeoTIFF using GDALRasterIO and write as PNG with the same transparency preserved.'

<-- 'Answer to question 1: Can I create a transparent GeoTIFF like PNG with alpha channel ?'

<-- 'Question of 2015-06-10 from gdal-dev'


2015-06-11: Mark Johnson, Berlin Germany