Skip to content

Commit 3a59fd4

Browse files
committedDec 15, 2012
More GRASS direct functions
1 parent 9069f3b commit 3a59fd4

File tree

3 files changed

+144
-6
lines changed

3 files changed

+144
-6
lines changed
 

‎src/providers/grass/CMakeLists.txt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ SET ( FUNCTIONS
6161
"G_add_c_raster_color_rule"
6262
"G_add_d_raster_color_rule"
6363
"G_add_f_raster_color_rule"
64+
"G_add_modular_color_rule"
6465
"G_add_raster_color_rule"
6566
"G_adjust_Cell_head"
6667
"G_adjust_easting"
@@ -116,6 +117,8 @@ SET ( FUNCTIONS
116117
"G_gettext"
117118
"G_get_window"
118119
"G_incr_void_ptr"
120+
"G_index"
121+
"G_init_cell_stats"
119122
"G_init_colors"
120123
"G_init_fp_range"
121124
"G__init_null_patterns"
@@ -124,10 +127,14 @@ SET ( FUNCTIONS
124127
"G_is_c_null_value"
125128
"G_is_d_null_value"
126129
"G_is_f_null_value"
130+
"G_lat_scan"
131+
"G_llres_scan"
132+
"G_lon_scan"
127133
"G__ls"
128134
"G_ls"
129135
"G_ls_format"
130136
"G__malloc"
137+
"G_make_random_colors"
131138
"G_northing_to_row"
132139
"G__projection_name"
133140
"G_percent"
@@ -137,13 +144,18 @@ SET ( FUNCTIONS
137144
"G_quant_free"
138145
"G_quant_get_limits"
139146
"G_quant_init"
147+
"G_putenv"
140148
"G_raster_size"
141149
"G_read_key_value_file"
142150
"G__realloc"
151+
"G_rindex"
143152
"G_row_to_northing"
153+
"G_scan_easting"
154+
"G_scan_northing"
144155
"G_set_c_null_value"
145156
"G_set_d_null_value"
146157
"G_set_d_raster_cat"
158+
"G_set_color_range"
147159
"G_setenv"
148160
"G_set_f_null_value"
149161
"G_set_gisrc_mode"
@@ -187,6 +199,9 @@ SET ( FUNCTIONS
187199
# module of the same name, i.e. in our fake library, not in the original
188200
# true GRASS gis lib, so we have to add all functions called in true lib
189201
# also to mapped functions in fake lib
202+
# Maybe that above is nonsese, but on Linux symbols are resolved when called, but on
203+
# Windows, all symbols are resolved at beginning and thus not all missing symbols are
204+
# always catched on Linux if testing with different data
190205

191206
IF(MSVC)
192207
SET ( FUNCTIONS
@@ -200,6 +215,8 @@ ENDIF(MSVC)
200215
# thus we only need prototype
201216
SET ( FUNCTION_PROTOTYPES
202217
"G_area_of_cell_at_row"
218+
"G_ask_cell_new"
219+
"G_ask_cell_old"
203220
"G_asprintf"
204221
"G_begin_cell_area_calculations"
205222
"G_begin_distance_calculations"
@@ -222,6 +239,7 @@ SET ( FUNCTION_PROTOTYPES
222239
"G_find_file_misc"
223240
"G_find_file2_misc"
224241
"G_fully_qualified_name"
242+
"G_geodesic_distance_lon_to_lon"
225243
"G_get_cellhd"
226244
"G_get_c_raster_row"
227245
"G_get_c_raster_row_nomask"
@@ -244,18 +262,23 @@ SET ( FUNCTION_PROTOTYPES
244262
"G_message"
245263
"G_open_cell_new"
246264
"G_open_cell_old"
265+
"G_open_fp_cell_new"
247266
"G_open_raster_new"
248267
"G_parser"
249268
"G_put_raster_row"
250269
"G_quantize_fp_map_range"
251270
"G_raster_map_is_fp"
271+
"G_read_cats"
252272
"G_read_colors"
253273
"G_read_fp_range"
254274
"G_read_history"
255275
"G_read_range"
256276
"G_read_raster_cats"
277+
"G_remove"
257278
"G_set_cats_title"
258279
"G_set_error_routine"
280+
"G_set_geodesic_distance_lat1"
281+
"G_set_geodesic_distance_lat2"
259282
"G_set_quant_rules"
260283
"G_short_history"
261284
"G__temp_element"
@@ -364,9 +387,12 @@ ELSE(MSVC)
364387
ENDIF(MSVC)
365388
ADD_LIBRARY( ${FAKE_LIB_GRASS_GIS} MODULE qgsgrassgislib.cpp qgsgrassgislibfunctions.cpp )
366389

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

371397
IF (NOT APPLE)
372398
SET_TARGET_PROPERTIES(${FAKE_LIB_GRASS_GIS} PROPERTIES

‎src/providers/grass/qgsgrassgislib.cpp

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,34 @@ QgsGrassGisLib GRASS_LIB_EXPORT *QgsGrassGisLib::instance( )
8585
QgsGrassGisLib::QgsGrassGisLib()
8686
{
8787
// Load original GRASS library
88+
89+
// GRASS_LIBRARY_GIS (cmake GRASS_LIBRARY_gis) is a path to the GRASS library
90+
// in the time of compilation, it may be used on runtime (it is the same)
91+
// on Linux and Mac but on Windows with OSGEO4W the GRASS may be installed
92+
// in a different directory. qgis.env however calls GRASS etc/env.bat
93+
// which sets GISBASE and GRASS_LIBRARY_GIS on Windows is path to .lib, e.g
94+
// grass_gis.lib. Name of the DLL on Windows is e.g.: libgrass_gis6.4.3RC1.dll
95+
96+
QString gisBase = getenv( "GISBASE" );
97+
#ifdef Q_OS_WIN
98+
if ( gisBase.isEmpty() )
99+
{
100+
fatal( "GISBASE environment variable not set" );
101+
}
102+
QString libPath = gisBase + "/lib/libgrass_gis" + QString( GRASS_VERSION ) + ".dll";
103+
#else
88104
QString libPath = QString( GRASS_LIBRARY_GIS );
105+
// Prefere GISBASE if set
106+
if ( !gisBase.isEmpty() )
107+
{
108+
libPath = gisBase + "/lib/" + QFileInfo( libPath ).fileName();
109+
}
110+
#endif
89111
QgsDebugMsg( "libPath = " + libPath );
90112
mLibrary.setFileName( libPath );
91113
if ( !mLibrary.load() )
92114
{
93-
QgsDebugMsg( "Cannot load original GRASS library" );
115+
fatal( "Cannot load true GRASS library, path: " + libPath );
94116
return;
95117
}
96118
}
@@ -116,11 +138,11 @@ void QgsGrassGisLib::warning( QString msg )
116138

117139
void * QgsGrassGisLib::resolve( const char * symbol )
118140
{
119-
//QgsDebugMsg( QString("symbol = %1").arg(symbol));
141+
QgsDebugMsg( QString( "symbol = %1" ).arg( symbol ) );
120142
void * fn = mLibrary.resolve( symbol );
121143
if ( !fn )
122144
{
123-
QgsDebugMsg( "Cannot resolve symbol" );
145+
fatal( "Cannot resolve symbol " + QString( symbol ) );
124146
}
125147
return fn;
126148
}
@@ -178,6 +200,9 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
178200
// Read projection if set
179201
//mCrs.createFromOgcWmsCrs( "EPSG:900913" );
180202
QString crsStr = getenv( "QGIS_GRASS_CRS" );
203+
204+
QgsDebugMsg( "Setting CRS to " + crsStr );
205+
181206
if ( !crsStr.isEmpty() )
182207
{
183208
if ( !mCrs.createFromProj4( crsStr ) )
@@ -228,6 +253,13 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
228253
G_set_window( &window );
229254
#endif
230255

256+
QString regionStr = getenv( "GRASS_REGION" );
257+
if ( regionStr.isEmpty() )
258+
{
259+
fatal( "GRASS_REGION environment variable not set" );
260+
}
261+
262+
QgsDebugMsg( "Getting region via true lib from GRASS_REGION: " + regionStr );
231263
// GRASS true lib reads GRASS_REGION environment variable
232264
G_get_window( &mWindow );
233265

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

272+
QgsDebugMsg( "End" );
240273
return 0;
241274
}
242275

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

605+
int GRASS_LIB_EXPORT G_open_fp_cell_new( const char *name )
606+
{
607+
return QgsGrassGisLib::instance()->G_open_raster_new( name, FCELL_TYPE );
608+
}
609+
572610
RASTER_MAP_TYPE QgsGrassGisLib::G_raster_map_type( const char *name, const char *mapset )
573611
{
574612
Q_UNUSED( mapset );
@@ -1043,6 +1081,44 @@ int GRASS_LIB_EXPORT G_legal_filename( const char *s )
10431081
return 1;
10441082
}
10451083

1084+
int QgsGrassGisLib::G_set_geodesic_distance_lat1( double lat1 )
1085+
{
1086+
mLat1 = lat1;
1087+
return 0;
1088+
}
1089+
1090+
int QgsGrassGisLib::G_set_geodesic_distance_lat2( double lat2 )
1091+
{
1092+
mLat2 = lat2;
1093+
return 0;
1094+
}
1095+
1096+
int GRASS_LIB_EXPORT G_set_geodesic_distance_lat1( double lat1 )
1097+
{
1098+
return QgsGrassGisLib::instance()->G_set_geodesic_distance_lat1( lat1 );
1099+
}
1100+
1101+
int GRASS_LIB_EXPORT G_set_geodesic_distance_lat2( double lat2 )
1102+
{
1103+
return QgsGrassGisLib::instance()->G_set_geodesic_distance_lat2( lat2 );
1104+
}
1105+
1106+
double QgsGrassGisLib::G_geodesic_distance_lon_to_lon( double lon1, double lon2 )
1107+
{
1108+
double dist = mDistanceArea.measureLine( QgsPoint( lon1, mLat1 ), QgsPoint( lon2, mLat2 ) );
1109+
// TODO: not sure about this
1110+
if ( !mCrs.geographicFlag() )
1111+
{
1112+
dist *= G_database_units_to_meters_factor();
1113+
}
1114+
return dist;
1115+
}
1116+
1117+
double GRASS_LIB_EXPORT G_geodesic_distance_lon_to_lon( double lon1, double lon2 )
1118+
{
1119+
return QgsGrassGisLib::instance()->G_geodesic_distance_lon_to_lon( lon1, lon2 );
1120+
}
1121+
10461122
QgsRasterBlock::DataType QgsGrassGisLib::qgisRasterType( RASTER_MAP_TYPE grassType )
10471123
{
10481124
switch ( grassType )
@@ -1149,6 +1225,14 @@ int GRASS_LIB_EXPORT G_quantize_fp_map_range( const char *name, const char *maps
11491225
return 1;
11501226
}
11511227

1228+
int GRASS_LIB_EXPORT G_read_cats( const char *name, const char *mapset, struct Categories *pcats )
1229+
{
1230+
Q_UNUSED( name );
1231+
Q_UNUSED( mapset );
1232+
G_init_raster_cats( "Cats", pcats );
1233+
return 0;
1234+
}
1235+
11521236
int GRASS_LIB_EXPORT G_read_raster_cats( const char *name, const char *mapset, struct Categories *pcats )
11531237
{
11541238
Q_UNUSED( name );
@@ -1221,15 +1305,36 @@ int GRASS_LIB_EXPORT G_make_aspect_fp_colors( struct Colors *colors, DCELL min,
12211305
return 1; // OK
12221306
}
12231307

1224-
int G_check_overwrite( int argc, char **argv )
1308+
int GRASS_LIB_EXPORT G_check_overwrite( int argc, char **argv )
12251309
{
12261310
Q_UNUSED( argc );
12271311
Q_UNUSED( argv );
12281312
return 1; // overwrite
12291313
}
12301314

1231-
char *G_fully_qualified_name( const char *name, const char *mapset )
1315+
char GRASS_LIB_EXPORT *G_fully_qualified_name( const char *name, const char *mapset )
12321316
{
12331317
Q_UNUSED( mapset );
12341318
return G_store( name );
12351319
}
1320+
1321+
char GRASS_LIB_EXPORT *G_ask_cell_new( const char *prompt, char *name )
1322+
{
1323+
Q_UNUSED( prompt );
1324+
Q_UNUSED( name );
1325+
return NULL;
1326+
}
1327+
1328+
char GRASS_LIB_EXPORT *G_ask_cell_old( const char *prompt, char *name )
1329+
{
1330+
Q_UNUSED( prompt );
1331+
Q_UNUSED( name );
1332+
return NULL;
1333+
}
1334+
1335+
int GRASS_LIB_EXPORT G_remove( const char *element, const char *name )
1336+
{
1337+
Q_UNUSED( element );
1338+
Q_UNUSED( name );
1339+
return 1;
1340+
}

‎src/providers/grass/qgsgrassgislib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
7777
char *G_find_cell2( const char * name, const char * mapset );
7878
int G_open_cell_old( const char *name, const char *mapset );
7979
int G_open_raster_new( const char *name, RASTER_MAP_TYPE wr_type );
80+
int G_open_fp_cell_new( const char *name );
8081
int G_close_cell( int fd );
8182
RASTER_MAP_TYPE G_raster_map_type( const char *name, const char *mapset );
8283
RASTER_MAP_TYPE G_get_raster_map_type( int fd );
@@ -93,6 +94,10 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
9394
int G_begin_cell_area_calculations( void );
9495
double G_distance( double e1, double n1, double e2, double n2 );
9596

97+
int G_set_geodesic_distance_lat1( double lat1 );
98+
int G_set_geodesic_distance_lat2( double lat2 );
99+
double G_geodesic_distance_lon_to_lon( double lon1, double lon2 );
100+
96101
/** Get QGIS raster type for GRASS raster type */
97102
QgsRasterBlock::DataType qgisRasterType( RASTER_MAP_TYPE grassType );
98103

@@ -141,6 +146,8 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
141146
/** Current coordinate reference system */
142147
QgsCoordinateReferenceSystem mCrs;
143148
QgsDistanceArea mDistanceArea;
149+
/** lat1, lat2 used for geodesic distance calculation */
150+
double mLat1, mLat2;
144151
};
145152

146153
#endif // QGSGRASSGISLIB_H

0 commit comments

Comments
 (0)
Please sign in to comment.