Skip to content

Commit

Permalink
GRASS region transformation resent on mapset change
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15758 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Apr 18, 2011
1 parent 61a96e8 commit 7164676
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 46 deletions.
27 changes: 18 additions & 9 deletions src/plugins/grass/qgsgrassplugin.cpp
Expand Up @@ -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() ) );
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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() );

Expand Down Expand Up @@ -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() );
}
Expand Down
42 changes: 42 additions & 0 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -38,6 +38,7 @@ extern "C"
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <grass/gprojects.h>
#include <grass/Vect.h>
#include <grass/version.h>
}
Expand Down Expand Up @@ -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 ) );
Expand Down
3 changes: 3 additions & 0 deletions src/providers/grass/qgsgrass.h
Expand Up @@ -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 );
Expand Down
38 changes: 1 addition & 37 deletions src/providers/grass/qgsgrassprovider.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 7164676

Please sign in to comment.