From caf52d9dc3ef3ad55c818c48d89c1b3fa6cb1c19 Mon Sep 17 00:00:00 2001 From: rblazek Date: Mon, 18 Apr 2011 14:48:33 +0000 Subject: [PATCH] GRASS region transformation resent on mapset change git-svn-id: http://svn.osgeo.org/qgis/trunk@15758 c8812cc2-4d05-0410-92ff-de0c093fc19c --- src/plugins/grass/qgsgrassplugin.cpp | 27 ++++++++++----- src/providers/grass/qgsgrass.cpp | 42 ++++++++++++++++++++++++ src/providers/grass/qgsgrass.h | 3 ++ src/providers/grass/qgsgrassprovider.cpp | 38 +-------------------- 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/plugins/grass/qgsgrassplugin.cpp b/src/plugins/grass/qgsgrassplugin.cpp index 6e15ac84451..277ea50fdce 100644 --- a/src/plugins/grass/qgsgrassplugin.cpp +++ b/src/plugins/grass/qgsgrassplugin.cpp @@ -115,6 +115,8 @@ void QgsGrassPlugin::initGui() mCanvas = qGisInterface->mapCanvas(); QWidget* qgis = qGisInterface->mainWindow(); + connect( mCanvas->mapRenderer(), SIGNAL( destinationSrsChanged() ), this, SLOT( setTransform() ) ); + // Connect project connect( qgis, SIGNAL( projectRead() ), this, SLOT( projectRead() ) ); connect( qgis, SIGNAL( newProject() ), this, SLOT( newProject() ) ); @@ -244,6 +246,20 @@ void QgsGrassPlugin::mapsetChanged() { mTools->mapsetChanged(); } + QString gisdbase = QgsGrass::getDefaultGisdbase(); + QString location = QgsGrass::getDefaultLocation(); + try + { + mCrs = QgsGrass::crsDirect( gisdbase, location ); + } + catch ( QgsGrass::Exception &e ) + { + QgsDebugMsg( "Cannot read GRASS CRS : " + QString( e.what() ) ); + mCrs = QgsCoordinateReferenceSystem(); + } + QgsDebugMsg( "mCrs: " + mCrs.toWkt() ); + setTransform(); + redrawRegion(); } } @@ -590,15 +606,6 @@ void QgsGrassPlugin::displayRegion() QgsGrass::setLocation( gisdbase, location ); - // TODO: check better if we have to init + maybe the location can change -> mCrs must be reloaded - if ( !mCrs.isValid() ) - { - mCrs = QgsGrass::crs( gisdbase, location ); - QgsDebugMsg( "mCrs: " + mCrs.toWkt() ); - setTransform(); - connect( mCanvas->mapRenderer(), SIGNAL( destinationSrsChanged() ), this, SLOT( setTransform() ) ); - } - struct Cell_head window; char *err = G__get_window( &window, ( char * ) "", ( char * ) "WIND", mapset.toLatin1().data() ); @@ -877,6 +884,8 @@ void QgsGrassPlugin::setTransform() { if ( mCrs.isValid() && mCanvas->mapRenderer()->destinationCrs().isValid() ) { + QgsDebugMsg( "srcCrs: " + mCrs.toWkt() ); + QgsDebugMsg( "destCrs " + mCanvas->mapRenderer()->destinationCrs().toWkt() ); mCoordinateTransform.setSourceCrs( mCrs ); mCoordinateTransform.setDestCRS( mCanvas->mapRenderer()->destinationCrs() ); } diff --git a/src/providers/grass/qgsgrass.cpp b/src/providers/grass/qgsgrass.cpp index 94ada574cc7..36d98e04fba 100644 --- a/src/providers/grass/qgsgrass.cpp +++ b/src/providers/grass/qgsgrass.cpp @@ -38,6 +38,7 @@ extern "C" #ifndef _MSC_VER #include #endif +#include #include #include } @@ -1160,6 +1161,47 @@ QgsCoordinateReferenceSystem GRASS_EXPORT QgsGrass::crs( QString gisdbase, QStri return crs; } +QgsCoordinateReferenceSystem GRASS_EXPORT QgsGrass::crsDirect( QString gisdbase, QString location ) +{ + QString Wkt; + + struct Cell_head cellhd; + + QgsGrass::resetError(); + QgsGrass::setLocation( gisdbase, location ); + + const char *oldlocale = setlocale( LC_NUMERIC, NULL ); + setlocale( LC_NUMERIC, "C" ); + + try + { + G_get_default_window( &cellhd ); + } + catch ( QgsGrass::Exception &e ) + { + Q_UNUSED( e ); + setlocale( LC_NUMERIC, oldlocale ); + QgsDebugMsg( QString( "Cannot get default window: %1" ).arg( e.what() ) ); + return QgsCoordinateReferenceSystem(); + } + + if ( cellhd.proj != PROJECTION_XY ) + { + struct Key_Value *projinfo = G_get_projinfo(); + struct Key_Value *projunits = G_get_projunits(); + char *wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 ); + Wkt = QString( wkt ); + G_free( wkt ); + } + + setlocale( LC_NUMERIC, oldlocale ); + + QgsCoordinateReferenceSystem srs; + srs.createFromWkt( Wkt ); + + return srs; +} + QgsRectangle GRASS_EXPORT QgsGrass::extent( QString gisdbase, QString location, QString mapset, QString map, MapType type ) { QgsDebugMsg( QString( "gisdbase = %1 location = %2" ).arg( gisdbase ).arg( location ) ); diff --git a/src/providers/grass/qgsgrass.h b/src/providers/grass/qgsgrass.h index 807d3553d04..e7fd21d7837 100644 --- a/src/providers/grass/qgsgrass.h +++ b/src/providers/grass/qgsgrass.h @@ -196,6 +196,9 @@ class QgsGrass // ! Get location projection static GRASS_EXPORT QgsCoordinateReferenceSystem crs( QString gisdbase, QString location ); + // ! Get location projection calling directly GRASS library + static GRASS_EXPORT QgsCoordinateReferenceSystem crsDirect( QString gisdbase, QString location ); + // ! Get map extent static GRASS_EXPORT QgsRectangle extent( QString gisdbase, QString location, QString mapset, QString map, MapType type = None ); diff --git a/src/providers/grass/qgsgrassprovider.cpp b/src/providers/grass/qgsgrassprovider.cpp index 53abd2f3081..037d8ae0428 100644 --- a/src/providers/grass/qgsgrassprovider.cpp +++ b/src/providers/grass/qgsgrassprovider.cpp @@ -1316,43 +1316,7 @@ struct Map_info *QgsGrassProvider::layerMap( int layerId ) QgsCoordinateReferenceSystem QgsGrassProvider::crs() { - QString Wkt; - - struct Cell_head cellhd; - - QgsGrass::resetError(); - QgsGrass::setLocation( mGisdbase, mLocation ); - - const char *oldlocale = setlocale( LC_NUMERIC, NULL ); - setlocale( LC_NUMERIC, "C" ); - - try - { - G_get_default_window( &cellhd ); - } - catch ( QgsGrass::Exception &e ) - { - Q_UNUSED( e ); - setlocale( LC_NUMERIC, oldlocale ); - QgsDebugMsg( QString( "Cannot get default window: %1" ).arg( e.what() ) ); - return QgsCoordinateReferenceSystem(); - } - - if ( cellhd.proj != PROJECTION_XY ) - { - struct Key_Value *projinfo = G_get_projinfo(); - struct Key_Value *projunits = G_get_projunits(); - char *wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 ); - Wkt = QString( wkt ); - G_free( wkt ); - } - - setlocale( LC_NUMERIC, oldlocale ); - - QgsCoordinateReferenceSystem srs; - srs.createFromWkt( Wkt ); - - return srs; + return QgsGrass::crs( mGisdbase, mLocation ); } int QgsGrassProvider::grassLayer()