Skip to content

Commit

Permalink
More GRASS direct functions
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Dec 15, 2012
1 parent 9069f3b commit 3a59fd4
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 6 deletions.
28 changes: 27 additions & 1 deletion src/providers/grass/CMakeLists.txt
Expand Up @@ -61,6 +61,7 @@ SET ( FUNCTIONS
"G_add_c_raster_color_rule"
"G_add_d_raster_color_rule"
"G_add_f_raster_color_rule"
"G_add_modular_color_rule"
"G_add_raster_color_rule"
"G_adjust_Cell_head"
"G_adjust_easting"
Expand Down Expand Up @@ -116,6 +117,8 @@ SET ( FUNCTIONS
"G_gettext"
"G_get_window"
"G_incr_void_ptr"
"G_index"
"G_init_cell_stats"
"G_init_colors"
"G_init_fp_range"
"G__init_null_patterns"
Expand All @@ -124,10 +127,14 @@ SET ( FUNCTIONS
"G_is_c_null_value"
"G_is_d_null_value"
"G_is_f_null_value"
"G_lat_scan"
"G_llres_scan"
"G_lon_scan"
"G__ls"
"G_ls"
"G_ls_format"
"G__malloc"
"G_make_random_colors"
"G_northing_to_row"
"G__projection_name"
"G_percent"
Expand All @@ -137,13 +144,18 @@ SET ( FUNCTIONS
"G_quant_free"
"G_quant_get_limits"
"G_quant_init"
"G_putenv"
"G_raster_size"
"G_read_key_value_file"
"G__realloc"
"G_rindex"
"G_row_to_northing"
"G_scan_easting"
"G_scan_northing"
"G_set_c_null_value"
"G_set_d_null_value"
"G_set_d_raster_cat"
"G_set_color_range"
"G_setenv"
"G_set_f_null_value"
"G_set_gisrc_mode"
Expand Down Expand Up @@ -187,6 +199,9 @@ SET ( FUNCTIONS
# module of the same name, i.e. in our fake library, not in the original
# true GRASS gis lib, so we have to add all functions called in true lib
# also to mapped functions in fake lib
# Maybe that above is nonsese, but on Linux symbols are resolved when called, but on
# Windows, all symbols are resolved at beginning and thus not all missing symbols are
# always catched on Linux if testing with different data

IF(MSVC)
SET ( FUNCTIONS
Expand All @@ -200,6 +215,8 @@ ENDIF(MSVC)
# thus we only need prototype
SET ( FUNCTION_PROTOTYPES
"G_area_of_cell_at_row"
"G_ask_cell_new"
"G_ask_cell_old"
"G_asprintf"
"G_begin_cell_area_calculations"
"G_begin_distance_calculations"
Expand All @@ -222,6 +239,7 @@ SET ( FUNCTION_PROTOTYPES
"G_find_file_misc"
"G_find_file2_misc"
"G_fully_qualified_name"
"G_geodesic_distance_lon_to_lon"
"G_get_cellhd"
"G_get_c_raster_row"
"G_get_c_raster_row_nomask"
Expand All @@ -244,18 +262,23 @@ SET ( FUNCTION_PROTOTYPES
"G_message"
"G_open_cell_new"
"G_open_cell_old"
"G_open_fp_cell_new"
"G_open_raster_new"
"G_parser"
"G_put_raster_row"
"G_quantize_fp_map_range"
"G_raster_map_is_fp"
"G_read_cats"
"G_read_colors"
"G_read_fp_range"
"G_read_history"
"G_read_range"
"G_read_raster_cats"
"G_remove"
"G_set_cats_title"
"G_set_error_routine"
"G_set_geodesic_distance_lat1"
"G_set_geodesic_distance_lat2"
"G_set_quant_rules"
"G_short_history"
"G__temp_element"
Expand Down Expand Up @@ -364,9 +387,12 @@ ELSE(MSVC)
ENDIF(MSVC)
ADD_LIBRARY( ${FAKE_LIB_GRASS_GIS} MODULE qgsgrassgislib.cpp qgsgrassgislibfunctions.cpp )

# GRASS_LIBRARY_gis is path to the GRASS library used for compilation, it is the same
# on runtime on Linux and Mac but on Windows with OSGEO4W the GRASS may be installed
# in different directory
SET_TARGET_PROPERTIES(${FAKE_LIB_GRASS_GIS} PROPERTIES
CLEAN_DIRECT_OUTPUT 1
COMPILE_FLAGS "\"-DGRASS_LIB_EXPORT=${DLLEXPORT}\" \"-DGRASS_EXPORT=${DLLIMPORT}\" -DGRASS_LIBRARY_GIS=\\\"${GRASS_LIBRARY_gis}\\\" \"-I${CMAKE_CURRENT_SOURCE_DIR}\" ")
COMPILE_FLAGS "\"-DGRASS_LIB_EXPORT=${DLLEXPORT}\" \"-DGRASS_EXPORT=${DLLIMPORT}\" -DGRASS_VERSION=\\\"${GRASS_VERSION}\\\" -DGRASS_LIBRARY_GIS=\\\"${GRASS_LIBRARY_gis}\\\" \"-I${CMAKE_CURRENT_SOURCE_DIR}\" ")

IF (NOT APPLE)
SET_TARGET_PROPERTIES(${FAKE_LIB_GRASS_GIS} PROPERTIES
Expand Down
115 changes: 110 additions & 5 deletions src/providers/grass/qgsgrassgislib.cpp
Expand Up @@ -85,12 +85,34 @@ QgsGrassGisLib GRASS_LIB_EXPORT *QgsGrassGisLib::instance( )
QgsGrassGisLib::QgsGrassGisLib()
{
// Load original GRASS library

// GRASS_LIBRARY_GIS (cmake GRASS_LIBRARY_gis) is a path to the GRASS library
// in the time of compilation, it may be used on runtime (it is the same)
// on Linux and Mac but on Windows with OSGEO4W the GRASS may be installed
// in a different directory. qgis.env however calls GRASS etc/env.bat
// which sets GISBASE and GRASS_LIBRARY_GIS on Windows is path to .lib, e.g
// grass_gis.lib. Name of the DLL on Windows is e.g.: libgrass_gis6.4.3RC1.dll

QString gisBase = getenv( "GISBASE" );
#ifdef Q_OS_WIN
if ( gisBase.isEmpty() )
{
fatal( "GISBASE environment variable not set" );
}
QString libPath = gisBase + "/lib/libgrass_gis" + QString( GRASS_VERSION ) + ".dll";
#else
QString libPath = QString( GRASS_LIBRARY_GIS );
// Prefere GISBASE if set
if ( !gisBase.isEmpty() )
{
libPath = gisBase + "/lib/" + QFileInfo( libPath ).fileName();
}
#endif
QgsDebugMsg( "libPath = " + libPath );
mLibrary.setFileName( libPath );
if ( !mLibrary.load() )
{
QgsDebugMsg( "Cannot load original GRASS library" );
fatal( "Cannot load true GRASS library, path: " + libPath );
return;
}
}
Expand All @@ -116,11 +138,11 @@ void QgsGrassGisLib::warning( QString msg )

void * QgsGrassGisLib::resolve( const char * symbol )
{
//QgsDebugMsg( QString("symbol = %1").arg(symbol));
QgsDebugMsg( QString( "symbol = %1" ).arg( symbol ) );
void * fn = mLibrary.resolve( symbol );
if ( !fn )
{
QgsDebugMsg( "Cannot resolve symbol" );
fatal( "Cannot resolve symbol " + QString( symbol ) );
}
return fn;
}
Expand Down Expand Up @@ -178,6 +200,9 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
// Read projection if set
//mCrs.createFromOgcWmsCrs( "EPSG:900913" );
QString crsStr = getenv( "QGIS_GRASS_CRS" );

QgsDebugMsg( "Setting CRS to " + crsStr );

if ( !crsStr.isEmpty() )
{
if ( !mCrs.createFromProj4( crsStr ) )
Expand Down Expand Up @@ -228,6 +253,13 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
G_set_window( &window );
#endif

QString regionStr = getenv( "GRASS_REGION" );
if ( regionStr.isEmpty() )
{
fatal( "GRASS_REGION environment variable not set" );
}

QgsDebugMsg( "Getting region via true lib from GRASS_REGION: " + regionStr );
// GRASS true lib reads GRASS_REGION environment variable
G_get_window( &mWindow );

Expand All @@ -237,6 +269,7 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
mXRes = mExtent.width() / mColumns;
mYRes = mExtent.height() / mColumns;

QgsDebugMsg( "End" );
return 0;
}

Expand Down Expand Up @@ -569,6 +602,11 @@ int GRASS_LIB_EXPORT G_open_cell_new( const char *name )
return QgsGrassGisLib::instance()->G_open_raster_new( name, CELL_TYPE );
}

int GRASS_LIB_EXPORT G_open_fp_cell_new( const char *name )
{
return QgsGrassGisLib::instance()->G_open_raster_new( name, FCELL_TYPE );
}

RASTER_MAP_TYPE QgsGrassGisLib::G_raster_map_type( const char *name, const char *mapset )
{
Q_UNUSED( mapset );
Expand Down Expand Up @@ -1043,6 +1081,44 @@ int GRASS_LIB_EXPORT G_legal_filename( const char *s )
return 1;
}

int QgsGrassGisLib::G_set_geodesic_distance_lat1( double lat1 )
{
mLat1 = lat1;
return 0;
}

int QgsGrassGisLib::G_set_geodesic_distance_lat2( double lat2 )
{
mLat2 = lat2;
return 0;
}

int GRASS_LIB_EXPORT G_set_geodesic_distance_lat1( double lat1 )
{
return QgsGrassGisLib::instance()->G_set_geodesic_distance_lat1( lat1 );
}

int GRASS_LIB_EXPORT G_set_geodesic_distance_lat2( double lat2 )
{
return QgsGrassGisLib::instance()->G_set_geodesic_distance_lat2( lat2 );
}

double QgsGrassGisLib::G_geodesic_distance_lon_to_lon( double lon1, double lon2 )
{
double dist = mDistanceArea.measureLine( QgsPoint( lon1, mLat1 ), QgsPoint( lon2, mLat2 ) );
// TODO: not sure about this
if ( !mCrs.geographicFlag() )
{
dist *= G_database_units_to_meters_factor();
}
return dist;
}

double GRASS_LIB_EXPORT G_geodesic_distance_lon_to_lon( double lon1, double lon2 )
{
return QgsGrassGisLib::instance()->G_geodesic_distance_lon_to_lon( lon1, lon2 );
}

QgsRasterBlock::DataType QgsGrassGisLib::qgisRasterType( RASTER_MAP_TYPE grassType )
{
switch ( grassType )
Expand Down Expand Up @@ -1149,6 +1225,14 @@ int GRASS_LIB_EXPORT G_quantize_fp_map_range( const char *name, const char *maps
return 1;
}

int GRASS_LIB_EXPORT G_read_cats( const char *name, const char *mapset, struct Categories *pcats )
{
Q_UNUSED( name );
Q_UNUSED( mapset );
G_init_raster_cats( "Cats", pcats );
return 0;
}

int GRASS_LIB_EXPORT G_read_raster_cats( const char *name, const char *mapset, struct Categories *pcats )
{
Q_UNUSED( name );
Expand Down Expand Up @@ -1221,15 +1305,36 @@ int GRASS_LIB_EXPORT G_make_aspect_fp_colors( struct Colors *colors, DCELL min,
return 1; // OK
}

int G_check_overwrite( int argc, char **argv )
int GRASS_LIB_EXPORT G_check_overwrite( int argc, char **argv )
{
Q_UNUSED( argc );
Q_UNUSED( argv );
return 1; // overwrite
}

char *G_fully_qualified_name( const char *name, const char *mapset )
char GRASS_LIB_EXPORT *G_fully_qualified_name( const char *name, const char *mapset )
{
Q_UNUSED( mapset );
return G_store( name );
}

char GRASS_LIB_EXPORT *G_ask_cell_new( const char *prompt, char *name )
{
Q_UNUSED( prompt );
Q_UNUSED( name );
return NULL;
}

char GRASS_LIB_EXPORT *G_ask_cell_old( const char *prompt, char *name )
{
Q_UNUSED( prompt );
Q_UNUSED( name );
return NULL;
}

int GRASS_LIB_EXPORT G_remove( const char *element, const char *name )
{
Q_UNUSED( element );
Q_UNUSED( name );
return 1;
}
7 changes: 7 additions & 0 deletions src/providers/grass/qgsgrassgislib.h
Expand Up @@ -77,6 +77,7 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
char *G_find_cell2( const char * name, const char * mapset );
int G_open_cell_old( const char *name, const char *mapset );
int G_open_raster_new( const char *name, RASTER_MAP_TYPE wr_type );
int G_open_fp_cell_new( const char *name );
int G_close_cell( int fd );
RASTER_MAP_TYPE G_raster_map_type( const char *name, const char *mapset );
RASTER_MAP_TYPE G_get_raster_map_type( int fd );
Expand All @@ -93,6 +94,10 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
int G_begin_cell_area_calculations( void );
double G_distance( double e1, double n1, double e2, double n2 );

int G_set_geodesic_distance_lat1( double lat1 );
int G_set_geodesic_distance_lat2( double lat2 );
double G_geodesic_distance_lon_to_lon( double lon1, double lon2 );

/** Get QGIS raster type for GRASS raster type */
QgsRasterBlock::DataType qgisRasterType( RASTER_MAP_TYPE grassType );

Expand Down Expand Up @@ -141,6 +146,8 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
/** Current coordinate reference system */
QgsCoordinateReferenceSystem mCrs;
QgsDistanceArea mDistanceArea;
/** lat1, lat2 used for geodesic distance calculation */
double mLat1, mLat2;
};

#endif // QGSGRASSGISLIB_H

0 comments on commit 3a59fd4

Please sign in to comment.