• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • tools
  • tilecreator-srtm2
srtm2/tccore.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2011 Niko Sams <niko.sams@gmail.com>
9 //
10 
11 #include "tccore.h"
12 #include <TileLoaderHelper.h>
13 #include <global.h>
14 #include <QFile>
15 #include <cmath>
16 #include <QPainter>
17 #include <QProcess>
18 #include <QFileInfo>
19 #include <QDir>
20 #include <QCache>
21 
22 using namespace Marble;
23 
24 class TileCreatorSourceSrtm : public TileCreatorSource
25 {
26 public:
27  TileCreatorSourceSrtm( const QString &sourceDir )
28  : m_sourceDir( sourceDir )
29  {
30  }
31 
32  virtual QSize fullImageSize() const
33  {
34  return QSize( 512*c_defaultTileSize*2, 512*c_defaultTileSize ); //512: 2**9 (9 zoom levels)
35  }
36 
37  virtual QImage tile( int n, int m, int maxTileLevel )
38  {
39  Q_ASSERT( maxTileLevel == 9 );
40 
41  int nmax = TileLoaderHelper::levelToRow( defaultLevelZeroRows, maxTileLevel );
42  Q_ASSERT( nmax == 512 );
43  int mmax = TileLoaderHelper::levelToColumn( defaultLevelZeroColumns, maxTileLevel );
44 
45  qreal startLat = ( ( ( (qreal)n * 180 ) / nmax ) - 90 ) * -1;
46  qreal startLng = ( ( (qreal)m * 360 ) / mmax ) - 180;
47 
48  int startLngPxResized = c_defaultTileSize * m;
49  int startLatPxResized = c_defaultTileSize * n;
50 
51 
52  if (hgtFileName(std::floor(startLng), std::floor(startLat)).isNull()
53  && hgtFileName(std::floor(startLng)+1, std::floor(startLat)).isNull()
54  && hgtFileName(std::floor(startLng), std::floor(startLat)-1).isNull()
55  && hgtFileName(std::floor(startLng)+1, std::floor(startLat)-1).isNull()
56  ) {
57  QImage ret( c_defaultTileSize, c_defaultTileSize, QImage::Format_ARGB32 );
58  QPainter painter( &ret );
59  painter.fillRect( 0, 0, c_defaultTileSize, c_defaultTileSize, QColor( Qt::black ) );
60  return ret;
61  }
62 
63  QImage image( 2400, 2400, QImage::Format_ARGB32 );
64  {
65  QPainter painter( &image );
66  painter.fillRect( 0, 0, 2400, 2400, QColor( Qt::black ) );
67  QImage i = readHgt(std::floor(startLng), std::floor(startLat));
68  painter.drawImage( 0, 0, i );
69  painter.drawImage( 1200, 0, readHgt( std::floor(startLng)+1, std::floor(startLat) ) );
70  painter.drawImage( 0, 1200, readHgt( std::floor(startLng), std::floor(startLat)-1 ) );
71  painter.drawImage( 1200, 1200, readHgt( std::floor(startLng)+1, std::floor(startLat)-1 ) );
72  }
73 
74  int imageSizeResized = 2400 * (512 * c_defaultTileSize) / (1200 * 180);
75 
76  // Pick the current row and smooth scale it
77  // to make it match the expected size
78  image = image.scaled( QSize(imageSizeResized, imageSizeResized),
79  Qt::IgnoreAspectRatio );
80 
81 
82  //startL??Px: position in px of what we are looking for
83  int startLngPx = startLng * 1200;
84  startLngPx = ( 180 * 1200 ) + startLngPx;
85  //qDebug() << "startLngPx(/1200)" << (qreal)startLngPx / 1200;
86 
87  int startLatPx = startLat * 1200;
88  startLatPx = ( 90 * 1200 ) - startLatPx;
89  //qDebug() << "startLatPx(/1200)" << (qreal)startLatPx / 1200;
90 
91 
92  //imageL??Px: position in px of image
93  int imageLngPx = std::floor(startLng);
94  imageLngPx = 180 + imageLngPx;
95  //qDebug() << "imageLngPx(/1200)" << imageLngPx << "*1200" << imageLngPx*1200;
96  imageLngPx *= 1200;
97 
98  int imageLatPx = std::floor(90 - startLat);
99  //qDebug() << "imageLatPx(/1200)" << imageLatPx;
100  imageLatPx *= 1200;
101 
102  //qDebug() << "lng" << imageLngPx << startLngPx << "offset" << startLngPx - imageLngPx;
103  //qDebug() << "lat" << imageLatPx << startLatPx << "offset" << startLatPx - imageLatPx;
104  Q_ASSERT(1200*2 - (startLngPx - imageLngPx) >= 675);
105  Q_ASSERT(1200*2 - (startLatPx - imageLatPx) >= 675);
106  Q_ASSERT(startLngPx - imageLngPx >= 0);
107  Q_ASSERT(startLatPx - imageLatPx >= 0);
108 
109  int imageLngPxResized = imageLngPx * 1.6; //(512 * c_defaultTileSize) / (1200 * 180);
110  int imageLatPxResized = imageLatPx * 1.6; //(512 * c_defaultTileSize) / (1200 * 180);
111  //qDebug() << "lng(m)" << startLngPxResized << imageLngPxResized << "diff" << startLngPxResized - imageLngPxResized;
112  //qDebug() << "lat(n)" << startLatPxResized << imageLngPxResized << "diff" << startLatPxResized - imageLatPxResized;
113  Q_ASSERT(startLngPxResized - imageLngPxResized < imageSizeResized);
114  Q_ASSERT(startLatPxResized - imageLatPxResized < imageSizeResized);
115  Q_ASSERT(startLngPxResized - imageLngPxResized >= 0);
116  Q_ASSERT(startLatPxResized - imageLatPxResized >= 0);
117 
118  QImage croppedImage = image.copy(startLngPx - imageLngPx, startLatPx - imageLatPx, 675, 675);
119  QImage ret = image.copy(startLngPxResized - imageLngPxResized, startLatPxResized - imageLatPxResized, c_defaultTileSize, c_defaultTileSize);
120  //qDebug() << image.size() << ret.size();
121  return ret;
122  }
123 
124 private:
125  QString hgtFileName( int lng, int lat )
126  {
127  QChar EW( lng >= 0 ? 'E' : 'W' );
128  QChar NS( lat >= 0 ? 'N' : 'S' );
129 
130  QStringList dirs;
131  dirs << "Africa" << "Australia" << "Eurasia" << "Silands" << "North_America" << "South_America";
132  foreach( const QString &dir, dirs) {
133  QString fileName = m_sourceDir + '/' + dir + '/';
134  if ( lat < 0 ) lat *= -1;
135  fileName += QString( "%1%2%3%4.hgt" ).arg( NS ).arg( lat<0 ? lat*-1 : lat, 2, 10, QLatin1Char('0') )
136  .arg( EW ).arg( lng<0 ? lng*-1 : lng, 3, 10, QLatin1Char('0' ) );
137  //qDebug() << fileName;
138 
139  if ( !QFile::exists( fileName ) && QFile::exists( fileName + ".zip" ) ) {
140  qDebug() << "zip found, unzipping";
141  QProcess p;
142  p.execute("unzip", QStringList() << fileName + ".zip" );
143  p.waitForFinished();
144  QFile( QDir::currentPath() + '/' + QFileInfo( fileName ).fileName()).rename(fileName);
145  }
146  if ( QFile::exists( fileName ) ) {
147  return fileName;
148  }
149  }
150 
151  return QString();
152  }
153 
154  QImage readHgt( int lng, int lat )
155  {
156  static QCache<QPair<int, int>, QImage > cache( 10 );
157  if ( cache.contains( qMakePair( lng, lat ) ) ) {
158  return *cache[ qMakePair( lng, lat ) ];
159  }
160  QString fileName = hgtFileName( lng, lat );
161  if ( fileName.isNull() ) {
162  //qDebug() << lng << lat << "hgt file does not exist, returing null image";
163  return QImage();
164  } else {
165  //qDebug() << lng << lat << "reading hgt file" << fileName;
166  }
167 
168  QFile file( fileName );
169 
170  file.open( QIODevice::ReadOnly );
171  int iLat = 0;
172  int iLng = 0;
173 
174  //hgt file is 1201px large, but the last px is overlapping
175  QImage image( 1200, 1200, QImage::Format_ARGB32 );
176  while(true) {
177  QByteArray data = file.read( 2 );
178 
179  if ( iLng < 1200 ) {
180  unsigned short height = *(unsigned short*)data.data();
181  height = ( height << 8 | height >> 8 );
182  unsigned int pixel;
183  pixel = height;
184  pixel += 0xFF000000; //fully opaque
185  image.setPixel( iLng, iLat, pixel );
186  }
187 
188  if ( iLat >= 1199 && iLng >= 1199 ) break;
189 
190  iLng++;
191  if ( iLng > 1200 ) { //here not 1199 but one more, because of overlapping px at the end of the line
192  iLng = 0;
193  iLat++;
194  }
195  }
196  file.close();
197 
198  cache.insert( qMakePair( lng, lat ), new QImage( image ) );
199 
200  return image;
201  }
202 
203  QString m_sourceDir;
204 };
205 
206 TCCoreApplication::TCCoreApplication( int argc, char ** argv ) : QCoreApplication( argc, argv )
207 {
208 
209  if( !(argc < 2) )
210  {
211  TileCreatorSource *source = new TileCreatorSourceSrtm( argv[1] );
212  m_tilecreator = new TileCreator( source, "false", argv[2] );
213  m_tilecreator->setTileFormat( "png" );
214  m_tilecreator->setTileQuality( 25 );
215  m_tilecreator->setResume( true );
216  m_tilecreator->setVerifyExactResult( true );
217  connect( m_tilecreator, SIGNAL(finished()), this, SLOT(quit()) );
218  m_tilecreator->start();
219  }
220 }
QPainter
global.h
Marble::defaultLevelZeroColumns
const int defaultLevelZeroColumns
Definition: MarbleGlobal.h:184
Marble::TileCreatorSource
Base Class for custom tile source.
Definition: TileCreator.h:35
Marble::TileCreator::setVerifyExactResult
void setVerifyExactResult(bool verify)
Definition: TileCreator.cpp:611
Marble::c_defaultTileSize
const unsigned int c_defaultTileSize
Definition: MarbleGlobal.h:244
Marble::TileCreator
Definition: TileCreator.h:53
Marble::TileCreator::setTileFormat
void setTileFormat(const QString &format)
Definition: TileCreator.cpp:581
QCoreApplication
Marble::TCCoreApplication::TCCoreApplication
TCCoreApplication(int &argc, char **argv)
Definition: tccore.cpp:15
Marble::TileLoaderHelper::levelToRow
MARBLE_EXPORT int levelToRow(int levelZeroRows, int level)
Get the maximum number of tile rows for a given tile level.
Definition: TileLoaderHelper.cpp:36
tccore.h
Marble::TileLoaderHelper::levelToColumn
MARBLE_EXPORT int levelToColumn(int levelZeroColumns, int level)
Get the maximum number of tile columns for a given tile level.
Definition: TileLoaderHelper.cpp:46
Marble::TileCreator::setTileQuality
void setTileQuality(int quality)
Definition: TileCreator.cpp:591
TileLoaderHelper.h
Marble::TileCreator::setResume
void setResume(bool resume)
Definition: TileCreator.cpp:601
Marble::defaultLevelZeroRows
const int defaultLevelZeroRows
Definition: MarbleGlobal.h:185
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal