void QgsRasterTerrainAnalysisPlugin::hillshade() { QgsRasterTerrainAnalysisDialog d( QgsRasterTerrainAnalysisDialog::HillshadeInput, mIface->mainWindow() ); d.setWindowTitle( tr( "Hillshade" ) ); if ( d.exec() == QDialog::Accepted ) { QString outputFile = d.outputFile(); QgsHillshadeFilter hillshade( d.inputFile(), outputFile, d.outputFormat(), d.lightAzimuth(), d.lightAngle() ); hillshade.setZFactor( d.zFactor() ); QProgressDialog p( tr( "Calculating hillshade..." ), tr( "Abort" ), 0, 0 ); p.setWindowModality( Qt::WindowModal ); hillshade.processRaster( &p ); if ( d.addResultToProject() ) { mIface->addRasterLayer( outputFile, QFileInfo( outputFile ).baseName() ); } } }
void Hillshade(const Eigen::MatrixBase<Derived> &surface, const double &altitude, const double &azimuth, const Eigen::MatrixBase<Derived> &hillshade_) { double zenith_deg = 90.0 - altitude; double zenith_rad = zenith_deg * kPI / 180.0; double azimuth_math = 360.0 - azimuth + 90.0; if (azimuth_math > 360.0) azimuth_math -= 360.0; double azimuth_rad = azimuth_math * kPI / 180.0; double z_factor = 1.0; double cellsize = 5.0; Eigen::MatrixBase<Derived> &hillshade = const_cast<Eigen::MatrixBase<Derived>& >(hillshade_); hillshade.derived().resize(surface.rows(), surface.cols()); hillshade.setConstant(0); for (boost::int32_t r = 1; r < surface.rows() - 1; ++r) { for (boost::int32_t c = 1; c < surface.cols() - 1; ++c) { double dzdx = ((surface(r - 1, c + 1) + 2 * surface(r, c + 1) + surface(r + 1, c + 1)) - (surface(r - 1, c - 1) + 2 * surface(r, c - 1) + surface(r + 1, c - 1))) / (8 * cellsize); double dzdy = ((surface(r + 1, c - 1) + 2 * surface(r + 1, c) + surface(r + 1, c + 1)) - (surface(r - 1, c - 1) + 2 * surface(r - 1, c) + surface(r - 1, c + 1))) / (8 * cellsize); double slope_rad = std::atan(z_factor * std::sqrt(dzdx * dzdx + dzdy * dzdy)); double aspect_rad = 0.0; if (std::abs(dzdx) > 0.0) { aspect_rad = std::atan2(dzdy, -dzdx); if (aspect_rad < 0.0) { aspect_rad = 2 * kPI + aspect_rad; } } else { if (dzdy > 0.0) { aspect_rad = kPI / 2.0; } else if (dzdy < 0.0) { aspect_rad = 2.0 * kPI - kPI / 2.0; } // else aspect_rad = aspect_rad; ?? } hillshade(r, c) = 255.0 * ((std::cos(zenith_rad) * std::cos(slope_rad)) + (std::sin(zenith_rad) * std::sin(slope_rad) * std::cos(azimuth_rad - aspect_rad))); } } }
void QgsRasterTerrainAnalysisPlugin::initGui() { //create Action if ( mIface ) { //find raster menu QString rasterText = QCoreApplication::translate( "QgisApp", "&Raster" ); QMainWindow* mainWindow = qobject_cast<QMainWindow*>( mIface->mainWindow() ); if ( !mainWindow ) { return; } QMenuBar* menuBar = mainWindow->menuBar(); if ( !menuBar ) { return; } QMenu* rasterMenu = 0; QList<QAction *> menuBarActions = menuBar->actions(); QList<QAction *>::iterator menuActionIt = menuBarActions.begin(); for ( ; menuActionIt != menuBarActions.end(); ++menuActionIt ) { if (( *menuActionIt )->menu() && ( *menuActionIt )->menu()->title() == rasterText ) { rasterMenu = ( *menuActionIt )->menu(); rasterMenu->addSeparator(); break; } } if ( !rasterMenu ) { return; } mTerrainAnalysisMenu = new QMenu( tr( "Terrain analysis" ), rasterMenu ); mTerrainAnalysisMenu->setObjectName( "mTerrainAnalysisMenu" ); mTerrainAnalysisMenu->setIcon( QIcon( ":/raster/dem.png" ) ); QAction *slopeAction = mTerrainAnalysisMenu->addAction( tr( "Slope" ), this, SLOT( slope() ) ); slopeAction->setObjectName( "slopeAction" ); QAction *aspectAction = mTerrainAnalysisMenu->addAction( tr( "Aspect" ), this, SLOT( aspect() ) ); aspectAction->setObjectName( "aspectAction" ); QAction *hilshadeAction = mTerrainAnalysisMenu->addAction( tr( "Hillshade" ), this, SLOT( hillshade() ) ); hilshadeAction->setObjectName( "hilshadeAction" ); QAction *reliefAction = mTerrainAnalysisMenu->addAction( tr( "Relief" ), this, SLOT( relief() ) ); reliefAction->setObjectName( "reliefAction" ); QAction *ruggednesIndex = mTerrainAnalysisMenu->addAction( tr( "Ruggedness index" ), this, SLOT( ruggedness() ) ); ruggednesIndex->setObjectName( "ruggednesIndex" ); rasterMenu->addMenu( mTerrainAnalysisMenu ); } }