Skip to content

Commit 49102e2

Browse files
committedDec 14, 2012
More GRASS direct functions
1 parent 46016e2 commit 49102e2

File tree

3 files changed

+213
-1
lines changed

3 files changed

+213
-1
lines changed
 

‎src/providers/grass/CMakeLists.txt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,55 @@ SET ( FUNCTIONS
6363
"G_add_f_raster_color_rule"
6464
"G_add_raster_color_rule"
6565
"G_adjust_Cell_head"
66+
"G_adjust_easting"
67+
"G_adjust_east_longitude"
6668
"G_align_window"
69+
"G__allocate_null_bits"
6770
"G_allocate_cell_buf"
6871
"G_allocate_c_raster_buf"
6972
"G_allocate_d_raster_buf"
7073
"G_allocate_f_raster_buf"
7174
"G_allocate_null_buf"
7275
"G_allocate_raster_buf"
7376
"G__calloc"
77+
"G_chop"
78+
"G_chrcat"
79+
"G_chrcpy"
80+
"G_chrmov"
81+
"G_colors_count"
7482
"G_col_to_easting"
7583
"G_convert_dirseps_to_host"
84+
"G_create_key_value"
85+
"G_database_projection_name"
86+
"G_date"
7687
"G_define_flag"
7788
"G_define_module"
7889
"G_define_option"
7990
"G_define_standard_option"
91+
"G_easting_to_col"
92+
"G_find_key_value"
93+
"G_find_cell_stat"
94+
"G_fopen_new"
95+
"G_format_easting"
96+
"G_format_northing"
97+
"G_format_resolution"
8098
"G_free"
99+
"G_free_cats"
100+
"G_free_cell_stats"
101+
"G_free_key_value"
81102
"G_free_raster_cats"
103+
"G_fseek"
104+
"G_ftell"
105+
"G_get_cat"
106+
"G_get_c_raster_cat"
107+
"G_get_d_raster_cat"
82108
"G_get_fp_range_min_max"
109+
"G_get_f_raster_cat"
110+
"G_getl"
111+
"G_getl2"
83112
"G_get_range_min_max"
113+
"G_get_raster_cat"
114+
"G_get_raster_row_colors"
84115
"G_get_set_window"
85116
"G_gettext"
86117
"G_get_window"
@@ -93,7 +124,12 @@ SET ( FUNCTIONS
93124
"G_is_c_null_value"
94125
"G_is_d_null_value"
95126
"G_is_f_null_value"
127+
"G__ls"
128+
"G_ls"
129+
"G_ls_format"
96130
"G__malloc"
131+
"G_northing_to_row"
132+
"G__projection_name"
97133
"G_percent"
98134
"G_program_name"
99135
"G_projection"
@@ -102,6 +138,7 @@ SET ( FUNCTIONS
102138
"G_quant_get_limits"
103139
"G_quant_init"
104140
"G_raster_size"
141+
"G_read_key_value_file"
105142
"G__realloc"
106143
"G_row_to_northing"
107144
"G_set_c_null_value"
@@ -110,18 +147,35 @@ SET ( FUNCTIONS
110147
"G_setenv"
111148
"G_set_f_null_value"
112149
"G_set_gisrc_mode"
150+
"G_set_key_value"
113151
"G_set_null_value"
114152
"G_set_raster_cat"
115153
"G_set_raster_cats_title"
116154
"G_set_raster_value_d"
117155
"G_set_window"
156+
"G_store"
157+
"G_strcasecmp"
158+
"G_strcat"
159+
"G_strchg"
160+
"G_strcpy"
161+
"G_strdup"
162+
"G_strend"
118163
"G_strip"
164+
"G_strmov"
165+
"G_strncpy"
166+
"G_str_replace"
167+
"G_strstr"
168+
"G_str_to_lower"
169+
"G_str_to_sql"
170+
"G_str_to_upper"
119171
"G_suppress_masking"
120172
"G_trim_decimal"
121173
"G_update_fp_range"
174+
"G_update_key_value_file"
122175
"G_update_range"
123176
"G_window_cols"
124177
"G_window_rows"
178+
"G_write_key_value_file"
125179
"G_zero"
126180
)
127181

@@ -138,24 +192,36 @@ IF(MSVC)
138192
SET ( FUNCTIONS
139193
${FUNCTIONS}
140194
"G__getenv"
195+
"G_get_gisrc_mode"
141196
)
142197
ENDIF(MSVC)
143198

144199
# List of functions which are implemented in qgsgrassgislib.cpp and
145200
# thus we only need prototype
146201
SET ( FUNCTION_PROTOTYPES
202+
"G_area_of_cell_at_row"
147203
"G_asprintf"
204+
"G_begin_cell_area_calculations"
148205
"G_begin_distance_calculations"
206+
"G_begin_geodesic_distance"
149207
"G_check_input_output_name"
208+
"G_check_overwrite"
150209
"G_close_cell"
151210
"G_command_history"
152211
"G_database_units_to_meters_factor"
153212
"G_debug"
154213
"G_distance"
155214
"G_done_msg"
156215
"G_fatal_error"
216+
"G__file_name"
217+
"G__file_name_misc"
157218
"G_find_cell"
158219
"G_find_cell2"
220+
"G_find_file"
221+
"G_find_file2"
222+
"G_find_file_misc"
223+
"G_find_file2_misc"
224+
"G_fully_qualified_name"
159225
"G_get_cellhd"
160226
"G_get_c_raster_row"
161227
"G_get_c_raster_row_nomask"
@@ -165,6 +231,7 @@ SET ( FUNCTION_PROTOTYPES
165231
"G_get_f_raster_row_nomask"
166232
"G_get_map_row"
167233
"G_get_map_row_nomask"
234+
"G_get_null_value_row"
168235
"G_get_raster_map_type"
169236
"G_get_raster_row"
170237
"G_get_raster_row_nomask"
@@ -191,6 +258,7 @@ SET ( FUNCTION_PROTOTYPES
191258
"G_set_error_routine"
192259
"G_set_quant_rules"
193260
"G_short_history"
261+
"G__temp_element"
194262
"G_tempfile"
195263
"G_vasprintf"
196264
"G_verbose_message"
@@ -214,6 +282,12 @@ FILE(READ "${GRASS_INCLUDE_DIR}/grass/gisdefs.h" HEADER_FILE)
214282
STRING(REGEX REPLACE "(/\\*([^*]|[\r\n]|(\\*+([^*/]|[\r\n])))*\\*+/)" "" HEADER_FILE "${HEADER_FILE}")
215283
STRING(REGEX REPLACE "#[^\r\n]*" "" HEADER_FILE "${HEADER_FILE}")
216284

285+
# Add G_gettext defined in glocale.h
286+
SET ( HEADER_FILE
287+
${HEADER_FILE}
288+
"char *G_gettext(const char *, const char *);"
289+
)
290+
217291
SET ( PROTOTYPES "// Auto generated by cmake, do not edit\n" )
218292
LIST ( APPEND PROTOTYPES "#define GRASS_GISDEFS_H\n" )
219293
LIST ( APPEND PROTOTYPES "extern \"C\"\n" )

‎src/providers/grass/qgsgrassgislib.cpp

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//#include <signal.h>
1717
#include <stdio.h>
1818
#include <stdarg.h>
19+
#include <qmath.h>
1920
#include <QtGlobal>
2021

2122
// If qgsgrassgislibfunctions.h is included on Linux, symbols defined here
@@ -26,10 +27,12 @@
2627
#endif
2728
#include "qgsgrassgislib.h"
2829

30+
#include "qgis.h"
2931
#include "qgslogger.h"
3032
#include "qgsapplication.h"
3133
#include "qgscoordinatereferencesystem.h"
3234
#include "qgsdatasourceuri.h"
35+
#include "qgsgeometry.h"
3336
#include "qgsrectangle.h"
3437
#include "qgsconfig.h"
3538

@@ -231,7 +234,8 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
231234
mExtent = QgsRectangle( mWindow.west, mWindow.south, mWindow.east, mWindow.north );
232235
mRows = mWindow.rows;
233236
mColumns = mWindow.cols;
234-
237+
mXRes = mExtent.width() / mColumns;
238+
mYRes = mExtent.height() / mColumns;
235239

236240
return 0;
237241
}
@@ -321,6 +325,25 @@ char GRASS_LIB_EXPORT *QgsGrassGisLib::G_find_cell2( const char * name, const ch
321325
return qstrdup( ms.toAscii() ); // memory lost
322326
}
323327

328+
char GRASS_LIB_EXPORT *G__file_name( char *path, const char *element, const char *name, const char *mapset )
329+
{
330+
Q_UNUSED( path );
331+
Q_UNUSED( element );
332+
Q_UNUSED( name );
333+
Q_UNUSED( mapset );
334+
return NULL;
335+
}
336+
337+
char *G__file_name_misc( char *path, const char *dir, const char *element, const char *name, const char *mapset )
338+
{
339+
Q_UNUSED( path );
340+
Q_UNUSED( dir );
341+
Q_UNUSED( element );
342+
Q_UNUSED( name );
343+
Q_UNUSED( mapset );
344+
return NULL;
345+
}
346+
324347
char GRASS_LIB_EXPORT *G_find_cell2( const char* name, const char *mapset )
325348
{
326349
return QgsGrassGisLib::instance()->G_find_cell2( name, mapset );
@@ -332,6 +355,40 @@ char GRASS_LIB_EXPORT *G_find_cell( char * name, const char * mapset )
332355
return G_find_cell2( name, mapset );
333356
}
334357

358+
char GRASS_LIB_EXPORT *G_find_file( const char *element, char *name, const char *mapset )
359+
{
360+
Q_UNUSED( element );
361+
Q_UNUSED( name );
362+
Q_UNUSED( mapset );
363+
return NULL;
364+
}
365+
366+
char GRASS_LIB_EXPORT *G_find_file2( const char *element, char *name, const char *mapset )
367+
{
368+
Q_UNUSED( element );
369+
Q_UNUSED( name );
370+
Q_UNUSED( mapset );
371+
return NULL;
372+
}
373+
374+
char GRASS_LIB_EXPORT *G_find_file_misc( const char *dir, const char *element, char *name, const char *mapset )
375+
{
376+
Q_UNUSED( dir );
377+
Q_UNUSED( element );
378+
Q_UNUSED( name );
379+
Q_UNUSED( mapset );
380+
return NULL;
381+
}
382+
383+
char GRASS_LIB_EXPORT *G_find_file_misc2( const char *dir, const char *element, char *name, const char *mapset )
384+
{
385+
Q_UNUSED( dir );
386+
Q_UNUSED( element );
387+
Q_UNUSED( name );
388+
Q_UNUSED( mapset );
389+
return NULL;
390+
}
391+
335392
QgsGrassGisLib::Raster QgsGrassGisLib::raster( QString name )
336393
{
337394
QgsDebugMsg( "name = " + name );
@@ -764,6 +821,24 @@ int GRASS_LIB_EXPORT G_get_map_row_nomask( int fd, CELL * buf, int row )
764821
return G_get_map_row( fd, buf, row );
765822
}
766823

824+
int QgsGrassGisLib::G_get_null_value_row( int fd, char *flags, int row )
825+
{
826+
FCELL *buf = G_allocate_f_raster_buf();
827+
QgsGrassGisLib::instance()->readRasterRow( fd, buf, row, FCELL_TYPE, false );
828+
829+
for ( int i = 0; i < mColumns; i++ )
830+
{
831+
flags[i] = G_is_f_null_value( &buf[i] ) ? 1 : 0;
832+
}
833+
G_free( buf );
834+
return 1;
835+
}
836+
837+
int GRASS_LIB_EXPORT G_get_null_value_row( int fd, char *flags, int row )
838+
{
839+
return QgsGrassGisLib::instance()->G_get_null_value_row( fd, flags, row );
840+
}
841+
767842
int QgsGrassGisLib::G_put_raster_row( int fd, const void *buf, RASTER_MAP_TYPE data_type )
768843
{
769844
Raster rast = mRasters.value( fd );
@@ -895,16 +970,54 @@ double QgsGrassGisLib::G_database_units_to_meters_factor( void )
895970
}
896971
}
897972

973+
double QgsGrassGisLib::G_area_of_cell_at_row( int row )
974+
{
975+
double yMax = mExtent.yMaximum() - row * mYRes;
976+
double yMin = yMax - mYRes;
977+
QgsRectangle rect( mExtent.xMinimum(), yMin, mExtent.xMinimum() + mXRes, yMax );
978+
QgsGeometry* geo = QgsGeometry::fromRect( rect );
979+
double area = mDistanceArea.measure( geo );
980+
delete geo;
981+
if ( !mCrs.geographicFlag() )
982+
{
983+
area *= qPow( G_database_units_to_meters_factor(), 2 );
984+
}
985+
return area;
986+
}
987+
988+
double GRASS_LIB_EXPORT G_area_of_cell_at_row( int row )
989+
{
990+
return QgsGrassGisLib::instance()->G_area_of_cell_at_row( row );
991+
}
992+
898993
double GRASS_LIB_EXPORT G_database_units_to_meters_factor( void )
899994
{
900995
return QgsGrassGisLib::instance()->G_database_units_to_meters_factor();
901996
}
902997

998+
int QgsGrassGisLib::G_begin_cell_area_calculations( void )
999+
{
1000+
if ( mCrs.geographicFlag() ) return 2; // non-planimetric
1001+
return 1; // planimetric
1002+
}
1003+
1004+
int GRASS_LIB_EXPORT G_begin_cell_area_calculations( void )
1005+
{
1006+
return QgsGrassGisLib::instance()->G_begin_cell_area_calculations();
1007+
}
1008+
9031009
int GRASS_LIB_EXPORT G_begin_distance_calculations( void )
9041010
{
9051011
return 1; // nothing to do
9061012
}
9071013

1014+
int GRASS_LIB_EXPORT G_begin_geodesic_distance( double a, double e2 )
1015+
{
1016+
Q_UNUSED( a );
1017+
Q_UNUSED( e2 );
1018+
return 0; // nothing to do
1019+
}
1020+
9081021
// Distance in meters
9091022
double QgsGrassGisLib::G_distance( double e1, double n1, double e2, double n2 )
9101023
{
@@ -993,6 +1106,12 @@ int G_asprintf( char **out, const char *fmt, ... )
9931106
return ret;
9941107
}
9951108

1109+
int GRASS_LIB_EXPORT G__temp_element( char *element )
1110+
{
1111+
Q_UNUSED( element );
1112+
return 0;
1113+
}
1114+
9961115
char GRASS_LIB_EXPORT *G_tempfile( void )
9971116
{
9981117
QTemporaryFile file( "qgis-grass-temp.XXXXXX" );
@@ -1102,3 +1221,15 @@ int GRASS_LIB_EXPORT G_make_aspect_fp_colors( struct Colors *colors, DCELL min,
11021221
return 1; // OK
11031222
}
11041223

1224+
int G_check_overwrite( int argc, char **argv )
1225+
{
1226+
Q_UNUSED( argc );
1227+
Q_UNUSED( argv );
1228+
return 1; // overwrite
1229+
}
1230+
1231+
char *G_fully_qualified_name( const char *name, const char *mapset )
1232+
{
1233+
Q_UNUSED( mapset );
1234+
return G_store( name );
1235+
}

‎src/providers/grass/qgsgrassgislib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
8484
int G_read_fp_range( const char *name, const char *mapset, struct FPRange *drange );
8585

8686
int readRasterRow( int fd, void * buf, int row, RASTER_MAP_TYPE data_type, bool noDataAsZero = false );
87+
int G_get_null_value_row( int fd, char *flags, int row );
8788
int G_put_raster_row( int fd, const void *buf, RASTER_MAP_TYPE data_type );
8889
int G_get_cellhd( const char *name, const char *mapset, struct Cell_head *cellhd );
8990

91+
double G_area_of_cell_at_row( int row );
9092
double G_database_units_to_meters_factor( void );
93+
int G_begin_cell_area_calculations( void );
9194
double G_distance( double e1, double n1, double e2, double n2 );
9295

9396
/** Get QGIS raster type for GRASS raster type */
@@ -131,6 +134,10 @@ class GRASS_LIB_EXPORT QgsGrassGisLib
131134
int mRows;
132135
/** Current region columns */
133136
int mColumns;
137+
/** X resolution */
138+
double mXRes;
139+
/** Y resolution */
140+
double mYRes;
134141
/** Current coordinate reference system */
135142
QgsCoordinateReferenceSystem mCrs;
136143
QgsDistanceArea mDistanceArea;

0 commit comments

Comments
 (0)
Please sign in to comment.