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 14, 2012
1 parent 46016e2 commit 49102e2
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 1 deletion.
74 changes: 74 additions & 0 deletions src/providers/grass/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,55 @@ SET ( FUNCTIONS
"G_add_f_raster_color_rule"
"G_add_raster_color_rule"
"G_adjust_Cell_head"
"G_adjust_easting"
"G_adjust_east_longitude"
"G_align_window"
"G__allocate_null_bits"
"G_allocate_cell_buf"
"G_allocate_c_raster_buf"
"G_allocate_d_raster_buf"
"G_allocate_f_raster_buf"
"G_allocate_null_buf"
"G_allocate_raster_buf"
"G__calloc"
"G_chop"
"G_chrcat"
"G_chrcpy"
"G_chrmov"
"G_colors_count"
"G_col_to_easting"
"G_convert_dirseps_to_host"
"G_create_key_value"
"G_database_projection_name"
"G_date"
"G_define_flag"
"G_define_module"
"G_define_option"
"G_define_standard_option"
"G_easting_to_col"
"G_find_key_value"
"G_find_cell_stat"
"G_fopen_new"
"G_format_easting"
"G_format_northing"
"G_format_resolution"
"G_free"
"G_free_cats"
"G_free_cell_stats"
"G_free_key_value"
"G_free_raster_cats"
"G_fseek"
"G_ftell"
"G_get_cat"
"G_get_c_raster_cat"
"G_get_d_raster_cat"
"G_get_fp_range_min_max"
"G_get_f_raster_cat"
"G_getl"
"G_getl2"
"G_get_range_min_max"
"G_get_raster_cat"
"G_get_raster_row_colors"
"G_get_set_window"
"G_gettext"
"G_get_window"
Expand All @@ -93,7 +124,12 @@ SET ( FUNCTIONS
"G_is_c_null_value"
"G_is_d_null_value"
"G_is_f_null_value"
"G__ls"
"G_ls"
"G_ls_format"
"G__malloc"
"G_northing_to_row"
"G__projection_name"
"G_percent"
"G_program_name"
"G_projection"
Expand All @@ -102,6 +138,7 @@ SET ( FUNCTIONS
"G_quant_get_limits"
"G_quant_init"
"G_raster_size"
"G_read_key_value_file"
"G__realloc"
"G_row_to_northing"
"G_set_c_null_value"
Expand All @@ -110,18 +147,35 @@ SET ( FUNCTIONS
"G_setenv"
"G_set_f_null_value"
"G_set_gisrc_mode"
"G_set_key_value"
"G_set_null_value"
"G_set_raster_cat"
"G_set_raster_cats_title"
"G_set_raster_value_d"
"G_set_window"
"G_store"
"G_strcasecmp"
"G_strcat"
"G_strchg"
"G_strcpy"
"G_strdup"
"G_strend"
"G_strip"
"G_strmov"
"G_strncpy"
"G_str_replace"
"G_strstr"
"G_str_to_lower"
"G_str_to_sql"
"G_str_to_upper"
"G_suppress_masking"
"G_trim_decimal"
"G_update_fp_range"
"G_update_key_value_file"
"G_update_range"
"G_window_cols"
"G_window_rows"
"G_write_key_value_file"
"G_zero"
)

Expand All @@ -138,24 +192,36 @@ IF(MSVC)
SET ( FUNCTIONS
${FUNCTIONS}
"G__getenv"
"G_get_gisrc_mode"
)
ENDIF(MSVC)

# List of functions which are implemented in qgsgrassgislib.cpp and
# thus we only need prototype
SET ( FUNCTION_PROTOTYPES
"G_area_of_cell_at_row"
"G_asprintf"
"G_begin_cell_area_calculations"
"G_begin_distance_calculations"
"G_begin_geodesic_distance"
"G_check_input_output_name"
"G_check_overwrite"
"G_close_cell"
"G_command_history"
"G_database_units_to_meters_factor"
"G_debug"
"G_distance"
"G_done_msg"
"G_fatal_error"
"G__file_name"
"G__file_name_misc"
"G_find_cell"
"G_find_cell2"
"G_find_file"
"G_find_file2"
"G_find_file_misc"
"G_find_file2_misc"
"G_fully_qualified_name"
"G_get_cellhd"
"G_get_c_raster_row"
"G_get_c_raster_row_nomask"
Expand All @@ -165,6 +231,7 @@ SET ( FUNCTION_PROTOTYPES
"G_get_f_raster_row_nomask"
"G_get_map_row"
"G_get_map_row_nomask"
"G_get_null_value_row"
"G_get_raster_map_type"
"G_get_raster_row"
"G_get_raster_row_nomask"
Expand All @@ -191,6 +258,7 @@ SET ( FUNCTION_PROTOTYPES
"G_set_error_routine"
"G_set_quant_rules"
"G_short_history"
"G__temp_element"
"G_tempfile"
"G_vasprintf"
"G_verbose_message"
Expand All @@ -214,6 +282,12 @@ FILE(READ "${GRASS_INCLUDE_DIR}/grass/gisdefs.h" HEADER_FILE)
STRING(REGEX REPLACE "(/\\*([^*]|[\r\n]|(\\*+([^*/]|[\r\n])))*\\*+/)" "" HEADER_FILE "${HEADER_FILE}")
STRING(REGEX REPLACE "#[^\r\n]*" "" HEADER_FILE "${HEADER_FILE}")

# Add G_gettext defined in glocale.h
SET ( HEADER_FILE
${HEADER_FILE}
"char *G_gettext(const char *, const char *);"
)

SET ( PROTOTYPES "// Auto generated by cmake, do not edit\n" )
LIST ( APPEND PROTOTYPES "#define GRASS_GISDEFS_H\n" )
LIST ( APPEND PROTOTYPES "extern \"C\"\n" )
Expand Down
133 changes: 132 additions & 1 deletion src/providers/grass/qgsgrassgislib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//#include <signal.h>
#include <stdio.h>
#include <stdarg.h>
#include <qmath.h>
#include <QtGlobal>

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

#include "qgis.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsdatasourceuri.h"
#include "qgsgeometry.h"
#include "qgsrectangle.h"
#include "qgsconfig.h"

Expand Down Expand Up @@ -231,7 +234,8 @@ int GRASS_LIB_EXPORT QgsGrassGisLib::G__gisinit( const char * version, const cha
mExtent = QgsRectangle( mWindow.west, mWindow.south, mWindow.east, mWindow.north );
mRows = mWindow.rows;
mColumns = mWindow.cols;

mXRes = mExtent.width() / mColumns;
mYRes = mExtent.height() / mColumns;

return 0;
}
Expand Down Expand Up @@ -321,6 +325,25 @@ char GRASS_LIB_EXPORT *QgsGrassGisLib::G_find_cell2( const char * name, const ch
return qstrdup( ms.toAscii() ); // memory lost
}

char GRASS_LIB_EXPORT *G__file_name( char *path, const char *element, const char *name, const char *mapset )
{
Q_UNUSED( path );
Q_UNUSED( element );
Q_UNUSED( name );
Q_UNUSED( mapset );
return NULL;
}

char *G__file_name_misc( char *path, const char *dir, const char *element, const char *name, const char *mapset )
{
Q_UNUSED( path );
Q_UNUSED( dir );
Q_UNUSED( element );
Q_UNUSED( name );
Q_UNUSED( mapset );
return NULL;
}

char GRASS_LIB_EXPORT *G_find_cell2( const char* name, const char *mapset )
{
return QgsGrassGisLib::instance()->G_find_cell2( name, mapset );
Expand All @@ -332,6 +355,40 @@ char GRASS_LIB_EXPORT *G_find_cell( char * name, const char * mapset )
return G_find_cell2( name, mapset );
}

char GRASS_LIB_EXPORT *G_find_file( const char *element, char *name, const char *mapset )
{
Q_UNUSED( element );
Q_UNUSED( name );
Q_UNUSED( mapset );
return NULL;
}

char GRASS_LIB_EXPORT *G_find_file2( const char *element, char *name, const char *mapset )
{
Q_UNUSED( element );
Q_UNUSED( name );
Q_UNUSED( mapset );
return NULL;
}

char GRASS_LIB_EXPORT *G_find_file_misc( const char *dir, const char *element, char *name, const char *mapset )
{
Q_UNUSED( dir );
Q_UNUSED( element );
Q_UNUSED( name );
Q_UNUSED( mapset );
return NULL;
}

char GRASS_LIB_EXPORT *G_find_file_misc2( const char *dir, const char *element, char *name, const char *mapset )
{
Q_UNUSED( dir );
Q_UNUSED( element );
Q_UNUSED( name );
Q_UNUSED( mapset );
return NULL;
}

QgsGrassGisLib::Raster QgsGrassGisLib::raster( QString name )
{
QgsDebugMsg( "name = " + name );
Expand Down Expand Up @@ -764,6 +821,24 @@ int GRASS_LIB_EXPORT G_get_map_row_nomask( int fd, CELL * buf, int row )
return G_get_map_row( fd, buf, row );
}

int QgsGrassGisLib::G_get_null_value_row( int fd, char *flags, int row )
{
FCELL *buf = G_allocate_f_raster_buf();
QgsGrassGisLib::instance()->readRasterRow( fd, buf, row, FCELL_TYPE, false );

for ( int i = 0; i < mColumns; i++ )
{
flags[i] = G_is_f_null_value( &buf[i] ) ? 1 : 0;
}
G_free( buf );
return 1;
}

int GRASS_LIB_EXPORT G_get_null_value_row( int fd, char *flags, int row )
{
return QgsGrassGisLib::instance()->G_get_null_value_row( fd, flags, row );
}

int QgsGrassGisLib::G_put_raster_row( int fd, const void *buf, RASTER_MAP_TYPE data_type )
{
Raster rast = mRasters.value( fd );
Expand Down Expand Up @@ -895,16 +970,54 @@ double QgsGrassGisLib::G_database_units_to_meters_factor( void )
}
}

double QgsGrassGisLib::G_area_of_cell_at_row( int row )
{
double yMax = mExtent.yMaximum() - row * mYRes;
double yMin = yMax - mYRes;
QgsRectangle rect( mExtent.xMinimum(), yMin, mExtent.xMinimum() + mXRes, yMax );
QgsGeometry* geo = QgsGeometry::fromRect( rect );
double area = mDistanceArea.measure( geo );
delete geo;
if ( !mCrs.geographicFlag() )
{
area *= qPow( G_database_units_to_meters_factor(), 2 );
}
return area;
}

double GRASS_LIB_EXPORT G_area_of_cell_at_row( int row )
{
return QgsGrassGisLib::instance()->G_area_of_cell_at_row( row );
}

double GRASS_LIB_EXPORT G_database_units_to_meters_factor( void )
{
return QgsGrassGisLib::instance()->G_database_units_to_meters_factor();
}

int QgsGrassGisLib::G_begin_cell_area_calculations( void )
{
if ( mCrs.geographicFlag() ) return 2; // non-planimetric
return 1; // planimetric
}

int GRASS_LIB_EXPORT G_begin_cell_area_calculations( void )
{
return QgsGrassGisLib::instance()->G_begin_cell_area_calculations();
}

int GRASS_LIB_EXPORT G_begin_distance_calculations( void )
{
return 1; // nothing to do
}

int GRASS_LIB_EXPORT G_begin_geodesic_distance( double a, double e2 )
{
Q_UNUSED( a );
Q_UNUSED( e2 );
return 0; // nothing to do
}

// Distance in meters
double QgsGrassGisLib::G_distance( double e1, double n1, double e2, double n2 )
{
Expand Down Expand Up @@ -993,6 +1106,12 @@ int G_asprintf( char **out, const char *fmt, ... )
return ret;
}

int GRASS_LIB_EXPORT G__temp_element( char *element )
{
Q_UNUSED( element );
return 0;
}

char GRASS_LIB_EXPORT *G_tempfile( void )
{
QTemporaryFile file( "qgis-grass-temp.XXXXXX" );
Expand Down Expand Up @@ -1102,3 +1221,15 @@ 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 )
{
Q_UNUSED( argc );
Q_UNUSED( argv );
return 1; // overwrite
}

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

0 comments on commit 49102e2

Please sign in to comment.