QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsterraindownloader.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsterraindownloader.cpp
3 --------------------------------------
4 Date : March 2019
5 Copyright : (C) 2019 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <memory>
19
20#include "qgs3dutils.h"
22#include "qgsgdalutils.h"
23#include "qgslogger.h"
24#include "qgsrasterlayer.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31{
33
34 // the whole world is projected to a square:
35 // X going from 180 W to 180 E
36 // Y going from ~85 N to ~85 S (=atan(sinh(pi)) ... to get a square)
37 QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( u"EPSG:4326"_s ), QgsCoordinateReferenceSystem( u"EPSG:3857"_s ), transformContext );
39 const QgsPointXY topLeftLonLat( -180, 180.0 / M_PI * std::atan( std::sinh( M_PI ) ) );
40 const QgsPointXY bottomRightLonLat( 180, 180.0 / M_PI * std::atan( std::sinh( -M_PI ) ) );
41 const QgsPointXY topLeft = ct.transform( topLeftLonLat );
42 const QgsPointXY bottomRight = ct.transform( bottomRightLonLat );
43 mXSpan = ( bottomRight.x() - topLeft.x() );
44}
45
47
49{
50 // using terrain tiles stored on AWS and listed within Registry of Open Data on AWS
51 // see https://registry.opendata.aws/terrain-tiles/
52 //
53 // tiles are generated using a variety of sources (SRTM, ETOPO1 and more detailed data for some countries)
54 // for more details and attribution see https://github.com/tilezen/joerd/blob/master/docs/data-sources.md
55
56 DataSource ds;
57 ds.uri = "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png";
58 ds.zMin = 0;
59 ds.zMax = 15;
60 return ds;
61}
62
64{
65 mDataSource = ds;
66 const QString uri = QString( "type=xyz&url=%1&zmin=%2&zmax=%3" ).arg( mDataSource.uri ).arg( mDataSource.zMin ).arg( mDataSource.zMax );
67 mOnlineDtm = std::make_unique<QgsRasterLayer>( uri, "terrarium", "wms" );
68}
69
70
71void QgsTerrainDownloader::adjustExtentAndResolution( double mupp, const QgsRectangle &extentOrig, QgsRectangle &extent, int &res )
72{
73 const double xMin = floor( extentOrig.xMinimum() / mupp ) * mupp;
74 const double xMax = ceil( extentOrig.xMaximum() / mupp ) * mupp;
75
76 const double yMin = floor( extentOrig.yMinimum() / mupp ) * mupp;
77 const double yMax = ceil( extentOrig.yMaximum() / mupp ) * mupp;
78
79 extent = QgsRectangle( xMin, yMin, xMax, yMax );
80 res = round( ( xMax - xMin ) / mupp );
81}
82
83
84double QgsTerrainDownloader::findBestTileResolution( double requestedMupp ) const
85{
86 int zoom = 0;
87 for ( ; zoom <= 15; ++zoom )
88 {
89 const double tileMupp = mXSpan / ( 256 * ( 1 << zoom ) );
90 if ( tileMupp <= requestedMupp )
91 break;
92 }
93
94 if ( zoom > 15 )
95 zoom = 15;
96 const double finalMupp = mXSpan / ( 256 * ( 1 << zoom ) );
97 return finalMupp;
98}
99
100
101void QgsTerrainDownloader::tileImageToHeightMap( const QImage &img, QByteArray &heightMap )
102{
103 // for description of the "terrarium" format:
104 // https://github.com/tilezen/joerd/blob/master/docs/formats.md
105
106 // assuming ARGB premultiplied but with alpha 255
107 const QRgb *rgb = reinterpret_cast<const QRgb *>( img.constBits() );
108 const int count = img.width() * img.height();
109 heightMap.resize( sizeof( float ) * count );
110 float *hData = reinterpret_cast<float *>( heightMap.data() );
111 for ( int i = 0; i < count; ++i )
112 {
113 const QRgb c = rgb[i];
114 if ( qAlpha( c ) == 255 )
115 {
116 const float h = qRed( c ) * 256 + qGreen( c ) + qBlue( c ) / 256.f - 32768;
117 *hData++ = h;
118 }
119 else
120 {
121 *hData++ = std::numeric_limits<float>::quiet_NaN();
122 }
123 }
124}
125
126
127QByteArray QgsTerrainDownloader::getHeightMap( const QgsRectangle &extentOrig, int res, const QgsCoordinateReferenceSystem &destCrs, const QgsCoordinateTransformContext &context, QString tmpFilenameImg, QString tmpFilenameTif )
128{
129 if ( !mOnlineDtm || !mOnlineDtm->isValid() )
130 {
131 QgsDebugError( "missing a valid data source" );
132 return QByteArray();
133 }
134
135 QgsRectangle extentTr = Qgs3DUtils::tryReprojectExtent2D( extentOrig, destCrs, mOnlineDtm->crs(), context );
136 const double requestedMupp = extentTr.width() / res;
137 const double finalMupp = findBestTileResolution( requestedMupp );
138
139 // adjust extent to match native resolution of terrain tiles
140
141 QgsRectangle extent;
142 const int resOrig = res;
143 adjustExtentAndResolution( finalMupp, extentTr, extent, res );
144
145 // request tile
146
147 QgsRasterBlock *b = mOnlineDtm->dataProvider()->block( 1, extent, res, res );
148 const QImage img = b->image();
149 delete b;
150 if ( !tmpFilenameImg.isEmpty() )
151 img.save( tmpFilenameImg );
152
153 // convert to height data
154
155 QByteArray heightMap;
156 tileImageToHeightMap( img, heightMap );
157
158 // prepare source/destination datasets for resampling
159
160 const gdal::dataset_unique_ptr hSrcDS( QgsGdalUtils::createSingleBandMemoryDataset( GDT_Float32, extent, res, res, mOnlineDtm->crs() ) );
162 if ( !tmpFilenameTif.isEmpty() )
163 hDstDS = QgsGdalUtils::createSingleBandTiffDataset( tmpFilenameTif, GDT_Float32, extentOrig, resOrig, resOrig, destCrs );
164 else
165 hDstDS = QgsGdalUtils::createSingleBandMemoryDataset( GDT_Float32, extentOrig, resOrig, resOrig, destCrs );
166
167 if ( !hSrcDS || !hDstDS )
168 {
169 QgsDebugError( "failed to create GDAL dataset for heightmap" );
170 return QByteArray();
171 }
172
173 const CPLErr err = GDALRasterIO( GDALGetRasterBand( hSrcDS.get(), 1 ), GF_Write, 0, 0, res, res, heightMap.data(), res, res, GDT_Float32, 0, 0 );
174 if ( err != CE_None )
175 {
176 QgsDebugError( "failed to write heightmap data to GDAL dataset" );
177 return QByteArray();
178 }
179
180 // resample to the desired extent + resolution
181 QgsGdalUtils::resampleSingleBandRaster( hSrcDS.get(), hDstDS.get(), GRA_Bilinear, context.calculateCoordinateOperation( mOnlineDtm->crs(), destCrs ).toUtf8().constData() );
182
183 QByteArray heightMapOut;
184 heightMapOut.resize( resOrig * resOrig * sizeof( float ) );
185 char *data = heightMapOut.data();
186
187 // read the data back
188
189 const CPLErr err2 = GDALRasterIO( GDALGetRasterBand( hDstDS.get(), 1 ), GF_Read, 0, 0, resOrig, resOrig, data, resOrig, resOrig, GDT_Float32, 0, 0 );
190 if ( err2 != CE_None )
191 {
192 QgsDebugError( "failed to read heightmap data from GDAL dataset" );
193 return QByteArray();
194 }
195
196 return heightMapOut;
197}
static QgsRectangle tryReprojectExtent2D(const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs1, const QgsCoordinateReferenceSystem &crs2, const QgsCoordinateTransformContext &context)
Reprojects extent from crs1 to crs2 coordinate reference system with context context.
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
QString calculateCoordinateOperation(const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination) const
Returns the Proj coordinate operation string to use when transforming from the specified source CRS t...
Handles coordinate transforms between two coordinate systems.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
static bool resampleSingleBandRaster(GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg, const char *pszCoordinateOperation)
Resamples a single band raster to the destination dataset with different resolution (and possibly wit...
static gdal::dataset_unique_ptr createSingleBandTiffDataset(const QString &filename, GDALDataType dataType, const QgsRectangle &extent, int width, int height, const QgsCoordinateReferenceSystem &crs)
Creates a new single band TIFF dataset with given parameters.
static gdal::dataset_unique_ptr createSingleBandMemoryDataset(GDALDataType dataType, const QgsRectangle &extent, int width, int height, const QgsCoordinateReferenceSystem &crs)
Creates a new single band memory dataset with given parameters.
Represents a 2D point.
Definition qgspointxy.h:62
double x
Definition qgspointxy.h:65
Raster data container.
QImage image() const
Returns an image containing the block data, if the block's data type is color.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
QgsTerrainDownloader(const QgsCoordinateTransformContext &transformContext)
Constructs a QgsTerrainDownloader object.
static DataSource defaultDataSource()
Returns the data source used by default.
QByteArray getHeightMap(const QgsRectangle &extentOrig, int res, const QgsCoordinateReferenceSystem &destCrs, const QgsCoordinateTransformContext &context=QgsCoordinateTransformContext(), QString tmpFilenameImg=QString(), QString tmpFilenameTif=QString())
For given extent and resolution (number of pixels for width/height) in specified CRS,...
void setDataSource(const DataSource &ds)
Configures data source to be used for download of terrain tiles.
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define QgsDebugError(str)
Definition qgslogger.h:59
Definition of data source for terrain tiles (assuming "terrarium" data encoding with usual XYZ tiling...
QString uri
HTTP(S) template for XYZ tiles requests (e.g. http://example.com/{z}/{x}/{y}.png).
int zMin
Minimum zoom level (Z) with valid data.
int zMax
Maximum zoom level (Z) with valid data.