This is an automated email from the git hooks/post-receive script. sebastic pushed a commit to branch experimental-2.2 in repository gdal-grass.
commit 2757bea279be2968b8ed96c19ef165867cec3256 Author: Bas Couwenberg <sebas...@xs4all.nl> Date: Fri Apr 14 17:22:32 2017 +0200 Imported Upstream version 2.2.0~beta1 --- README | 18 +- VERSION | 2 +- aclocal.m4 | 16 +- configure.in | 2 +- grass57dataset.cpp | 462 +++++++++++++------------- ogrgrass.h | 137 ++++---- ogrgrassdatasource.cpp | 75 ++--- ogrgrassdriver.cpp | 33 +- ogrgrasslayer.cpp | 886 ++++++++++++++++++++++++------------------------- 9 files changed, 772 insertions(+), 859 deletions(-) diff --git a/README b/README index cd545cd..dfd9f44 100644 --- a/README +++ b/README @@ -2,11 +2,11 @@ Standalone GRASS Drivers for GDAL and OGR ========================================= This package contains standalone drivers for GRASS raster and vector -files that can be built after GDAL has been built and installed as an -"autoload" driver. +files that can be built after GDAL has been built and installed as an +"autoload" driver. This is particularly useful in resolving problems with GRASS depending -on GDAL, but GDAL with GRASS support depending on GRASS. With this +on GDAL, but GDAL with GRASS support depending on GRASS. With this package you can configure and install GDAL normally (--without-grass), then build and install GRASS normally and finally build and install this driver. @@ -52,12 +52,12 @@ libraries not found? Answer: -Your problem is likely to be solved by editing /etc/ld.so.conf to -include the locations of proj, gdal, grass, and geos. Specifically, -the full path to both gdal-config and geos-config, and the full paths +Your problem is likely to be solved by editing /etc/ld.so.conf to +include the locations of proj, gdal, grass, and geos. Specifically, +the full path to both gdal-config and geos-config, and the full paths to the library locations of proj (often /usr/local/lib) and grass (/ -usr/local/grass-6.0.1/lib). After editing ld.so.conf, run ldconfig, +usr/local/grass-6.0.1/lib). After editing ld.so.conf, run ldconfig, and you should be good to go. -I ran into this problem this weekend (and posted for help to this -list), so it seems to be a pretty common issue. +I ran into this problem this weekend (and posted for help to this +list), so it seems to be a pretty common issue. diff --git a/VERSION b/VERSION index ac2cdeb..ccbccc3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.1.3 +2.2.0 diff --git a/aclocal.m4 b/aclocal.m4 index 7bffcf4..ec692bd 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -6,14 +6,14 @@ AC_DEFUN(AC_COMPILER_LOCALHACK, echo 'int main() { int i = 1; if( *((unsigned char *) &i) == 0 ) printf( "BIGENDIAN"); return 0; }' >> conftest.c ${CC} $CPPFLAGS $EXTRA_INCLUDES -o conftest conftest.c 2> comp.out COMP_CHECK=`grep "system directory" comp.out | grep /usr/local/include` - if test -z "$COMP_CHECK" ; then + if test -z "$COMP_CHECK" ; then AC_MSG_RESULT([no, everything is ok]) else AC_MSG_RESULT([yes, stripping extras]) CXXFLAGS=`echo "$CXXFLAGS " | sed "s/-I\/usr\/local\/include //"` CFLAGS=`echo "$CFLAGS " | sed "s/-I\/usr\/local\/include //"` EXTRA_INCLUDES=`echo "$EXTRA_INCLUDES " | sed "s/-I\/usr\/local\/include //"` - fi + fi rm -f comp.out ]) @@ -61,7 +61,7 @@ AC_DEFUN(AC_COMPILER_PIC, dnl dnl Try to find something to link shared libraries with. Use "c++ -shared" dnl in preference to "ld -shared" because it will link in required c++ -dnl run time support for us. +dnl run time support for us. dnl AC_DEFUN(AC_LD_SHARED, [ @@ -84,9 +84,9 @@ AC_DEFUN(AC_LD_SHARED, if test "$with_ld_shared" != "" ; then if test "$with_ld_shared" = "no" ; then - echo "user disabled shared library support." + echo "user disabled shared library support." else - echo "using user supplied .so link command ... $with_ld_shared" + echo "using user supplied .so link command ... $with_ld_shared" fi LD_SHARED="$with_ld_shared" fi @@ -113,7 +113,7 @@ AC_DEFUN(AC_LD_SHARED, fi fi - dnl Test special MacOS (Darwin) case. + dnl Test special MacOS (Darwin) case. if test ! -z "`uname | grep Darwin`" \ -a "$LD_SHARED" = "/bin/true" \ @@ -157,7 +157,7 @@ AC_DEFUN(AC_LD_SHARED, else echo "checking for ${CXX} -shared ... no(2)" fi - else + else if test "$LD_SHARED" = "/bin/true" ; then echo "checking for ${CXX} -shared ... no(1)" fi @@ -195,7 +195,7 @@ AC_DEFUN(AC_LD_SHARED, fi fi - rm -f conftest* libconftest* + rm -f conftest* libconftest* AC_SUBST(LD_SHARED,$LD_SHARED) AC_SUBST(SO_EXT,$SO_EXT) diff --git a/configure.in b/configure.in index ff96173..c9bf951 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ dnl *************************************************************************** -dnl $Id: configure.in 37161 2017-01-17 07:34:30Z rouault $ +dnl $Id: configure.in 37160 2017-01-17 07:34:22Z rouault $ dnl dnl Project: GDAL GRASS Plugin dnl Purpose: Configure source file. diff --git a/grass57dataset.cpp b/grass57dataset.cpp index f7442d4..fc9e926 100644 --- a/grass57dataset.cpp +++ b/grass57dataset.cpp @@ -1,5 +1,4 @@ /****************************************************************************** - * $Id: grass57dataset.cpp 33893 2016-04-04 14:08:28Z rouault $ * * Project: GRASS Driver * Purpose: Implement GRASS raster read/write support @@ -52,22 +51,24 @@ extern "C" { #include <grass/gis.h> #if GRASS_VERSION_MAJOR >= 7 -char *GPJ_grass_to_wkt(const struct Key_Value *, - const struct Key_Value *, - int, int); +char *GPJ_grass_to_wkt( const struct Key_Value *, + const struct Key_Value *, + int, int ); #else -char *GPJ_grass_to_wkt(struct Key_Value *, - struct Key_Value *, - int, int); +char *GPJ_grass_to_wkt( struct Key_Value *, + struct Key_Value *, + int, int ); #endif } #define GRASS_MAX_COLORS 100000 // what is the right value -CPL_CVSID("$Id: grass57dataset.cpp 33893 2016-04-04 14:08:28Z rouault $"); +CPL_CVSID("$Id: grass57dataset.cpp 37964 2017-04-11 18:04:50Z rouault $"); #if GRASS_VERSION_MAJOR >= 7 #define G_get_cellhd Rast_get_cellhd +#define G_get_window Rast_get_window +#define G_set_window Rast_set_window #define G_raster_map_type Rast_map_type #define G_read_fp_range Rast_read_fp_range #define G_get_fp_range_min_max Rast_get_fp_range_min_max @@ -96,7 +97,7 @@ CPL_CVSID("$Id: grass57dataset.cpp 33893 2016-04-04 14:08:28Z rouault $"); /* Grass2CPLErrorHook() */ /************************************************************************/ -int Grass2CPLErrorHook( char * pszMessage, int bFatal ) +static int Grass2CPLErrorHook( char * pszMessage, int bFatal ) { if( !bFatal ) @@ -110,7 +111,7 @@ int Grass2CPLErrorHook( char * pszMessage, int bFatal ) /************************************************************************/ /* ==================================================================== */ -/* GRASSDataset */ +/* GRASSDataset */ /* ==================================================================== */ /************************************************************************/ @@ -120,22 +121,22 @@ class GRASSDataset : public GDALDataset { friend class GRASSRasterBand; - char *pszGisdbase; - char *pszLocation; /* LOCATION_NAME */ - char *pszElement; /* cellhd or group */ + char *pszGisdbase; + char *pszLocation; /* LOCATION_NAME */ + char *pszElement; /* cellhd or group */ struct Cell_head sCellInfo; /* raster region */ - char *pszProjection; + char *pszProjection; - double adfGeoTransform[6]; + double adfGeoTransform[6]; public: GRASSDataset(); ~GRASSDataset(); - virtual const char *GetProjectionRef(void); - virtual CPLErr GetGeoTransform( double * ); + virtual const char *GetProjectionRef(void) override; + virtual CPLErr GetGeoTransform( double * ) override; static GDALDataset *Open( GDALOpenInfo * ); @@ -155,7 +156,7 @@ class GRASSRasterBand : public GDALRasterBand char *pszCellName; char *pszMapset; - int hCell; + int hCell; int nGRSType; // GRASS raster type: CELL_TYPE, FCELL_TYPE, DCELL_TYPE bool nativeNulls; // use GRASS native NULL values @@ -164,11 +165,11 @@ class GRASSRasterBand : public GDALRasterBand struct Cell_head sOpenWindow; /* the region when the raster was opened */ - int bHaveMinMax; - double dfCellMin; - double dfCellMax; + int bHaveMinMax; + double dfCellMin; + double dfCellMax; - double dfNoData; + double dfNoData; bool valid; @@ -178,42 +179,40 @@ class GRASSRasterBand : public GDALRasterBand const char *, const char * ); virtual ~GRASSRasterBand(); - virtual CPLErr IReadBlock( int, int, void * ); + virtual CPLErr IReadBlock( int, int, void * ) override; virtual CPLErr IRasterIO ( GDALRWFlag, int, int, int, int, void *, int, int, GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace, - GDALRasterIOExtraArg* psExtraArg); - virtual GDALColorInterp GetColorInterpretation(); - virtual GDALColorTable *GetColorTable(); - virtual double GetMinimum( int *pbSuccess = NULL ); - virtual double GetMaximum( int *pbSuccess = NULL ); - virtual double GetNoDataValue( int *pbSuccess = NULL ); + GDALRasterIOExtraArg* psExtraArg) override; + virtual GDALColorInterp GetColorInterpretation() override; + virtual GDALColorTable *GetColorTable() override; + virtual double GetMinimum( int *pbSuccess = NULL ) override; + virtual double GetMaximum( int *pbSuccess = NULL ) override; + virtual double GetNoDataValue( int *pbSuccess = NULL ) override; private: CPLErr ResetReading( struct Cell_head * ); - }; - /************************************************************************/ /* GRASSRasterBand() */ /************************************************************************/ -GRASSRasterBand::GRASSRasterBand( GRASSDataset *poDS, int nBand, - const char * pszMapset, - const char * pszCellName ) +GRASSRasterBand::GRASSRasterBand( GRASSDataset *poDSIn, int nBandIn, + const char * pszMapsetIn, + const char * pszCellNameIn ) { - struct Cell_head sCellInfo; + struct Cell_head sCellInfo; // Note: GISDBASE, LOCATION_NAME ans MAPSET was set in GRASSDataset::Open - this->poDS = poDS; - this->nBand = nBand; + this->poDS = poDSIn; + this->nBand = nBandIn; this->valid = false; - this->pszCellName = G_store ( (char *) pszCellName ); - this->pszMapset = G_store ( (char *) pszMapset ); + this->pszCellName = G_store ( (char *) pszCellNameIn ); + this->pszMapset = G_store ( (char *) pszMapsetIn ); G_get_cellhd( (char *) pszCellName, (char *) pszMapset, &sCellInfo ); nGRSType = G_raster_map_type( (char *) pszCellName, (char *) pszMapset ); @@ -244,66 +243,66 @@ GRASSRasterBand::GRASSRasterBand( GRASSDataset *poDS, int nBand, // full raster values range is used -> no checks if shorter type could be used if( nGRSType == CELL_TYPE ) { - if ( sCellInfo.format == 0 ) { // 1 byte / cell -> possible range 0,255 - if ( bHaveMinMax && dfCellMin > 0 ) { + if ( sCellInfo.format == 0 ) { // 1 byte / cell -> possible range 0,255 + if ( bHaveMinMax && dfCellMin > 0 ) { this->eDataType = GDT_Byte; - dfNoData = 0.0; - } else if ( bHaveMinMax && dfCellMax < 255 ) { + dfNoData = 0.0; + } else if ( bHaveMinMax && dfCellMax < 255 ) { this->eDataType = GDT_Byte; - dfNoData = 255.0; - } else { // maximum is not known or full range is used - this->eDataType = GDT_UInt16; - dfNoData = 256.0; - } - nativeNulls = false; - } else if ( sCellInfo.format == 1 ) { // 2 bytes / cell -> possible range 0,65535 - if ( bHaveMinMax && dfCellMin > 0 ) { - this->eDataType = GDT_UInt16; - dfNoData = 0.0; - } else if ( bHaveMinMax && dfCellMax < 65535 ) { + dfNoData = 255.0; + } else { // maximum is not known or full range is used + this->eDataType = GDT_UInt16; + dfNoData = 256.0; + } + nativeNulls = false; + } else if ( sCellInfo.format == 1 ) { // 2 bytes / cell -> possible range 0,65535 + if ( bHaveMinMax && dfCellMin > 0 ) { + this->eDataType = GDT_UInt16; + dfNoData = 0.0; + } else if ( bHaveMinMax && dfCellMax < 65535 ) { this->eDataType = GDT_UInt16; - dfNoData = 65535; - } else { // maximum is not known or full range is used - CELL cval; - this->eDataType = GDT_Int32; - G_set_c_null_value ( &cval, 1); - dfNoData = (double) cval; - nativeNulls = true; - } - nativeNulls = false; - } else { // 3-4 bytes - CELL cval; - this->eDataType = GDT_Int32; - G_set_c_null_value ( &cval, 1); - dfNoData = (double) cval; - nativeNulls = true; - } + dfNoData = 65535; + } else { // maximum is not known or full range is used + CELL cval; + this->eDataType = GDT_Int32; + G_set_c_null_value ( &cval, 1); + dfNoData = (double) cval; + nativeNulls = true; + } + nativeNulls = false; + } else { // 3-4 bytes + CELL cval; + this->eDataType = GDT_Int32; + G_set_c_null_value ( &cval, 1); + dfNoData = (double) cval; + nativeNulls = true; + } } else if( nGRSType == FCELL_TYPE ) { - FCELL fval; + FCELL fval; this->eDataType = GDT_Float32; - G_set_f_null_value ( &fval, 1); - dfNoData = (double) fval; - nativeNulls = true; + G_set_f_null_value ( &fval, 1); + dfNoData = (double) fval; + nativeNulls = true; } else if( nGRSType == DCELL_TYPE ) { - DCELL dval; + DCELL dval; this->eDataType = GDT_Float64; - G_set_d_null_value ( &dval, 1); - dfNoData = (double) dval; - nativeNulls = true; + G_set_d_null_value ( &dval, 1); + dfNoData = (double) dval; + nativeNulls = true; } - nBlockXSize = poDS->nRasterXSize;; + nBlockXSize = poDSIn->nRasterXSize; nBlockYSize = 1; - G_set_window( &(((GRASSDataset *)poDS)->sCellInfo) ); + G_set_window( &(poDSIn->sCellInfo) ); if ( (hCell = G_open_cell_old((char *) pszCellName, (char *) pszMapset)) < 0 ) { - CPLError( CE_Warning, CPLE_AppDefined, "GRASS: Cannot open raster '%s'", pszCellName ); - return; + CPLError( CE_Warning, CPLE_AppDefined, "GRASS: Cannot open raster '%s'", pszCellName ); + return; } - G_copy((void *) &sOpenWindow, (void *) &(((GRASSDataset *)poDS)->sCellInfo), sizeof(struct Cell_head)); + G_copy((void *) &sOpenWindow, (void *) &(poDSIn->sCellInfo), sizeof(struct Cell_head)); /* -------------------------------------------------------------------- */ /* Do we have a color table? */ @@ -311,34 +310,34 @@ GRASSRasterBand::GRASSRasterBand( GRASSDataset *poDS, int nBand, poCT = NULL; if( G_read_colors( (char *) pszCellName, (char *) pszMapset, &sGrassColors ) == 1 ) { - int maxcolor; - CELL min, max; + int maxcolor; + CELL min, max; - G_get_color_range ( &min, &max, &sGrassColors); + G_get_color_range ( &min, &max, &sGrassColors); if ( bHaveMinMax ) { - if ( max < dfCellMax ) { - maxcolor = max; + if ( max < dfCellMax ) { + maxcolor = max; } else { - maxcolor = (int) ceil ( dfCellMax ); - } - if ( maxcolor > GRASS_MAX_COLORS ) { - maxcolor = GRASS_MAX_COLORS; + maxcolor = (int) ceil ( dfCellMax ); + } + if ( maxcolor > GRASS_MAX_COLORS ) { + maxcolor = GRASS_MAX_COLORS; CPLDebug( "GRASS", "Too many values, color table cut to %d entries.", maxcolor ); - } - } else { - if ( max < GRASS_MAX_COLORS ) { - maxcolor = max; + } + } else { + if ( max < GRASS_MAX_COLORS ) { + maxcolor = max; } else { - maxcolor = GRASS_MAX_COLORS; + maxcolor = GRASS_MAX_COLORS; CPLDebug( "GRASS", "Too many values, color table set to %d entries.", maxcolor ); - } + } } poCT = new GDALColorTable(); for( int iColor = 0; iColor <= maxcolor; iColor++ ) { - int nRed, nGreen, nBlue; + int nRed, nGreen, nBlue; GDALColorEntry sColor; #if GRASS_VERSION_MAJOR >= 7 @@ -424,33 +423,32 @@ CPLErr GRASSRasterBand::ResetReading ( struct Cell_head *sNewWindow ) /* Check if the window has changed */ if ( sNewWindow->north != sOpenWindow.north || sNewWindow->south != sOpenWindow.south || - sNewWindow->east != sOpenWindow.east || sNewWindow->west != sOpenWindow.west || - sNewWindow->ew_res != sOpenWindow.ew_res || sNewWindow->ns_res != sOpenWindow.ns_res || - sNewWindow->rows != sOpenWindow.rows || sNewWindow->cols != sOpenWindow.cols ) + sNewWindow->east != sOpenWindow.east || sNewWindow->west != sOpenWindow.west || + sNewWindow->ew_res != sOpenWindow.ew_res || sNewWindow->ns_res != sOpenWindow.ns_res || + sNewWindow->rows != sOpenWindow.rows || sNewWindow->cols != sOpenWindow.cols ) { - if( hCell >= 0 ) { + if( hCell >= 0 ) { G_close_cell( hCell ); - hCell = -1; - } + hCell = -1; + } - /* Set window */ - G_set_window( sNewWindow ); + /* Set window */ + G_set_window( sNewWindow ); - /* Open raster */ - G__setenv( "GISDBASE", ((GRASSDataset *)poDS)->pszGisdbase ); - G__setenv( "LOCATION_NAME", ((GRASSDataset *)poDS)->pszLocation ); - G__setenv( "MAPSET", pszMapset); - G_reset_mapsets(); - G_add_mapset_to_search_path ( pszMapset ); + /* Open raster */ + G__setenv( "GISDBASE", ((GRASSDataset *)poDS)->pszGisdbase ); + G__setenv( "LOCATION_NAME", ((GRASSDataset *)poDS)->pszLocation ); + G__setenv( "MAPSET", pszMapset); + G_reset_mapsets(); + G_add_mapset_to_search_path ( pszMapset ); - if ( (hCell = G_open_cell_old( pszCellName, pszMapset)) < 0 ) { - CPLError( CE_Warning, CPLE_AppDefined, "GRASS: Cannot open raster '%s'", pszCellName ); + if ( (hCell = G_open_cell_old( pszCellName, pszMapset)) < 0 ) { + CPLError( CE_Warning, CPLE_AppDefined, "GRASS: Cannot open raster '%s'", pszCellName ); this->valid = false; - return CE_Failure; - } - - G_copy((void *) &sOpenWindow, (void *) sNewWindow, sizeof(struct Cell_head)); + return CE_Failure; + } + G_copy((void *) &sOpenWindow, (void *) sNewWindow, sizeof(struct Cell_head)); } else { @@ -470,7 +468,6 @@ CPLErr GRASSRasterBand::ResetReading ( struct Cell_head *sNewWindow ) } } - return CE_None; } @@ -479,7 +476,7 @@ CPLErr GRASSRasterBand::ResetReading ( struct Cell_head *sNewWindow ) /* */ /************************************************************************/ -CPLErr GRASSRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, +CPLErr GRASSRasterBand::IReadBlock( int /*nBlockXOff*/, int nBlockYOff, void *pImage ) { @@ -491,29 +488,32 @@ CPLErr GRASSRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, } if ( eDataType == GDT_Byte || eDataType == GDT_UInt16 ) { - CELL *cbuf; - - cbuf = G_allocate_c_raster_buf(); - G_get_c_raster_row ( hCell, cbuf, nBlockYOff ); - - /* Reset NULLs */ - for ( int col = 0; col < nBlockXSize; col++ ) { - if ( G_is_c_null_value(&(cbuf[col])) ) - cbuf[col] = (CELL) dfNoData; - } + CELL *cbuf = G_allocate_c_raster_buf(); + G_get_c_raster_row ( hCell, cbuf, nBlockYOff ); - GDALCopyWords ( (void *) cbuf, GDT_Int32, sizeof(CELL), - pImage, eDataType, GDALGetDataTypeSize(eDataType)/8, - nBlockXSize ); + /* Reset NULLs */ + for ( int col = 0; col < nBlockXSize; col++ ) { + if ( G_is_c_null_value(&(cbuf[col])) ) + cbuf[col] = (CELL) dfNoData; + } - G_free ( cbuf ); + GDALCopyWords ( (void *) cbuf, GDT_Int32, sizeof(CELL), + pImage, eDataType, GDALGetDataTypeSize(eDataType)/8, + nBlockXSize ); - } else if ( eDataType == GDT_Int32 ) { - G_get_c_raster_row ( hCell, (CELL *) pImage, nBlockYOff ); - } else if ( eDataType == GDT_Float32 ) { - G_get_f_raster_row ( hCell, (FCELL *) pImage, nBlockYOff ); - } else if ( eDataType == GDT_Float64 ) { - G_get_d_raster_row ( hCell, (DCELL *) pImage, nBlockYOff ); + G_free ( cbuf ); + } + else if ( eDataType == GDT_Int32 ) + { + G_get_c_raster_row ( hCell, (CELL *) pImage, nBlockYOff ); + } + else if ( eDataType == GDT_Float32 ) + { + G_get_f_raster_row ( hCell, (FCELL *) pImage, nBlockYOff ); + } + else if ( eDataType == GDT_Float64 ) + { + G_get_d_raster_row ( hCell, (DCELL *) pImage, nBlockYOff ); } return CE_None; @@ -525,12 +525,12 @@ CPLErr GRASSRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, /************************************************************************/ CPLErr GRASSRasterBand::IRasterIO ( GDALRWFlag eRWFlag, - int nXOff, int nYOff, int nXSize, int nYSize, - void * pData, int nBufXSize, int nBufYSize, - GDALDataType eBufType, - GSpacing nPixelSpace, - GSpacing nLineSpace, - GDALRasterIOExtraArg* psExtraArg ) + int nXOff, int nYOff, int nXSize, int nYSize, + void * pData, int nBufXSize, int nBufYSize, + GDALDataType eBufType, + GSpacing nPixelSpace, + GSpacing nLineSpace, + GDALRasterIOExtraArg* /*psExtraArg*/ ) { /* GRASS library does that, we have only calculate and reset the region in map units * and if the region has changed, reopen the raster */ @@ -539,6 +539,7 @@ CPLErr GRASSRasterBand::IRasterIO ( GDALRWFlag eRWFlag, struct Cell_head sWindow; struct Cell_head *psDsWindow; + if( eRWFlag != GF_Read ) return CE_Failure; if ( ! this->valid ) return CE_Failure; psDsWindow = &(((GRASSDataset *)poDS)->sCellInfo); @@ -569,63 +570,63 @@ CPLErr GRASSRasterBand::IRasterIO ( GDALRWFlag eRWFlag, /* Reset space if default (0) */ if ( nPixelSpace == 0 ) - nPixelSpace = GDALGetDataTypeSize ( eBufType ) / 8; + nPixelSpace = GDALGetDataTypeSize ( eBufType ) / 8; if ( nLineSpace == 0 ) - nLineSpace = nBufXSize * nPixelSpace; + nLineSpace = nBufXSize * nPixelSpace; if ( nGRSType == CELL_TYPE && ( !nativeNulls || eBufType != GDT_Int32 || sizeof(CELL) != 4 || - nPixelSpace != sizeof(CELL) ) ) + nPixelSpace != sizeof(CELL) ) ) { - cbuf = G_allocate_c_raster_buf(); + cbuf = G_allocate_c_raster_buf(); } else if( nGRSType == FCELL_TYPE && ( eBufType != GDT_Float32 || nPixelSpace != sizeof(FCELL) ) ) { - fbuf = G_allocate_f_raster_buf(); + fbuf = G_allocate_f_raster_buf(); } else if( nGRSType == DCELL_TYPE && ( eBufType != GDT_Float64 || nPixelSpace != sizeof(DCELL) ) ) { - dbuf = G_allocate_d_raster_buf(); + dbuf = G_allocate_d_raster_buf(); } else { - direct = true; + direct = true; } for ( int row = 0; row < nBufYSize; row++ ) { char *pnt = (char *)pData + row * nLineSpace; - if ( nGRSType == CELL_TYPE ) { - if ( direct ) { - G_get_c_raster_row ( hCell, (CELL *) pnt, row ); - } else { - G_get_c_raster_row ( hCell, cbuf, row ); - - /* Reset NULLs */ - for ( int col = 0; col < nBufXSize; col++ ) { - if ( G_is_c_null_value(&(cbuf[col])) ) - cbuf[col] = (CELL) dfNoData; - } - - GDALCopyWords ( (void *) cbuf, GDT_Int32, sizeof(CELL), - (void *) pnt, eBufType, nPixelSpace, - nBufXSize ); - } - } else if( nGRSType == FCELL_TYPE ) { - if ( direct ) { - G_get_f_raster_row ( hCell, (FCELL *) pnt, row ); - } else { - G_get_f_raster_row ( hCell, fbuf, row ); - - GDALCopyWords ( (void *) fbuf, GDT_Float32, sizeof(FCELL), - (void *) pnt, eBufType, nPixelSpace, - nBufXSize ); - } - } else if( nGRSType == DCELL_TYPE ) { - if ( direct ) { - G_get_d_raster_row ( hCell, (DCELL *) pnt, row ); - } else { - G_get_d_raster_row ( hCell, dbuf, row ); - - GDALCopyWords ( (void *) dbuf, GDT_Float64, sizeof(DCELL), - (void *) pnt, eBufType, nPixelSpace, - nBufXSize ); - } - } + if ( nGRSType == CELL_TYPE ) { + if ( direct ) { + G_get_c_raster_row ( hCell, (CELL *) pnt, row ); + } else { + G_get_c_raster_row ( hCell, cbuf, row ); + + /* Reset NULLs */ + for ( int col = 0; col < nBufXSize; col++ ) { + if ( G_is_c_null_value(&(cbuf[col])) ) + cbuf[col] = (CELL) dfNoData; + } + + GDALCopyWords ( (void *) cbuf, GDT_Int32, sizeof(CELL), + (void *) pnt, eBufType, nPixelSpace, + nBufXSize ); + } + } else if( nGRSType == FCELL_TYPE ) { + if ( direct ) { + G_get_f_raster_row ( hCell, (FCELL *) pnt, row ); + } else { + G_get_f_raster_row ( hCell, fbuf, row ); + + GDALCopyWords ( (void *) fbuf, GDT_Float32, sizeof(FCELL), + (void *) pnt, eBufType, nPixelSpace, + nBufXSize ); + } + } else if( nGRSType == DCELL_TYPE ) { + if ( direct ) { + G_get_d_raster_row ( hCell, (DCELL *) pnt, row ); + } else { + G_get_d_raster_row ( hCell, dbuf, row ); + + GDALCopyWords ( (void *) dbuf, GDT_Float64, sizeof(DCELL), + (void *) pnt, eBufType, nPixelSpace, + nBufXSize ); + } + } } if ( cbuf ) G_free ( cbuf ); @@ -719,7 +720,6 @@ double GRASSRasterBand::GetNoDataValue( int *pbSuccess ) /* ==================================================================== */ /************************************************************************/ - /************************************************************************/ /* GRASSDataset() */ /************************************************************************/ @@ -734,6 +734,9 @@ GRASSDataset::GRASSDataset() adfGeoTransform[3] = 0.0; adfGeoTransform[4] = 0.0; adfGeoTransform[5] = 1.0; + pszGisdbase = NULL; + pszLocation = NULL; + pszElement = NULL; } /************************************************************************/ @@ -744,13 +747,13 @@ GRASSDataset::~GRASSDataset() { if ( pszGisdbase ) - G_free ( pszGisdbase ); + G_free ( pszGisdbase ); if ( pszLocation ) G_free ( pszLocation ); if ( pszElement ) - G_free ( pszElement ); + G_free ( pszElement ); G_free( pszProjection ); } @@ -788,31 +791,37 @@ CPLErr GRASSDataset::GetGeoTransform( double * padfGeoTransform ) /* false - failed */ /************************************************************************/ bool GRASSDataset::SplitPath( char *path, char **gisdbase, char **location, - char **mapset, char **element, char **name ) + char **mapset, char **element, char **name ) { - char *p, *ptr[5], *tmp; - int i = 0; + char *p; + char *ptr[5]; + char *tmp; + int i = 0; - *gisdbase = *location = *mapset = *element = *name = NULL; + *gisdbase = NULL; + *location = NULL; + *mapset = NULL; + *element = NULL; + *name = NULL; if ( !path || strlen(path) == 0 ) - return false; + return false; tmp = G_store ( path ); while ( (p = strrchr(tmp,'/')) != NULL && i < 4 ) { - *p = '\0'; + *p = '\0'; - if ( strlen(p+1) == 0 ) /* repeated '/' */ - continue; + if ( strlen(p+1) == 0 ) /* repeated '/' */ + continue; - ptr[i++] = p+1; + ptr[i++] = p+1; } /* Note: empty GISDBASE == 0 is not accepted (relative path) */ if ( i != 4 ) { G_free ( tmp ); - return false; + return false; } *gisdbase = G_store ( tmp ); @@ -838,8 +847,8 @@ typedef int (*GrassErrorHandler)(char *, int); GDALDataset *GRASSDataset::Open( GDALOpenInfo * poOpenInfo ) { - char *pszGisdb = NULL, *pszLoc = NULL; - char *pszMapset = NULL, *pszElem = NULL, *pszName = NULL; + char *pszGisdb = NULL, *pszLoc = NULL; + char *pszMapset = NULL, *pszElem = NULL, *pszName = NULL; char **papszCells = NULL; char **papszMapsets = NULL; @@ -862,7 +871,6 @@ GDALDataset *GRASSDataset::Open( GDALOpenInfo * poOpenInfo ) // Set error function G_set_error_routine ( (GrassErrorHandler) Grass2CPLErrorHook ); - // GISBASE is path to the directory where GRASS is installed, if ( !getenv( "GISBASE" ) ) { static char* gisbaseEnv = NULL; @@ -880,19 +888,19 @@ GDALDataset *GRASSDataset::Open( GDALOpenInfo * poOpenInfo ) if ( !SplitPath( poOpenInfo->pszFilename, &pszGisdb, &pszLoc, &pszMapset, &pszElem, &pszName) ) { - return NULL; + return NULL; } /* -------------------------------------------------------------------- */ /* Check element name */ /* -------------------------------------------------------------------- */ if ( strcmp(pszElem,"cellhd") != 0 && strcmp(pszElem,"group") != 0 ) { - G_free(pszGisdb); + G_free(pszGisdb); G_free(pszLoc); G_free(pszMapset); G_free(pszElem); G_free(pszName); - return NULL; + return NULL; } /* -------------------------------------------------------------------- */ @@ -911,12 +919,12 @@ GDALDataset *GRASSDataset::Open( GDALOpenInfo * poOpenInfo ) if ( strcmp(pszElem,"cellhd") == 0 ) { if ( G_find_file2("cell", pszName, pszMapset) == NULL ) { - G_free(pszGisdb); G_free(pszLoc); G_free(pszMapset); G_free(pszElem); G_free(pszName); - return NULL; - } + G_free(pszGisdb); G_free(pszLoc); G_free(pszMapset); G_free(pszElem); G_free(pszName); + return NULL; + } - papszMapsets = CSLAddString( papszMapsets, pszMapset ); - papszCells = CSLAddString( papszCells, pszName ); + papszMapsets = CSLAddString( papszMapsets, pszMapset ); + papszCells = CSLAddString( papszCells, pszName ); } /* -------------------------------------------------------------------- */ /* Check if this is a valid GRASS imagery group. */ @@ -926,12 +934,12 @@ GDALDataset *GRASSDataset::Open( GDALOpenInfo * poOpenInfo ) I_init_group_ref( &ref ); if ( I_get_group_ref( pszName, &ref ) == 0 ) { - G_free(pszGisdb); G_free(pszLoc); G_free(pszMapset); G_free(pszElem); G_free(pszName); - return NULL; - } + G_free(pszGisdb); G_free(pszLoc); G_free(pszMapset); G_free(pszElem); G_free(pszName); + return NULL; + } for( int iRef = 0; iRef < ref.nfiles; iRef++ ) - { + { papszCells = CSLAddString( papszCells, ref.file[iRef].name ); papszMapsets = CSLAddString( papszMapsets, ref.file[iRef].mapset ); G_add_mapset_to_search_path ( ref.file[iRef].mapset ); @@ -946,9 +954,7 @@ GDALDataset *GRASSDataset::Open( GDALOpenInfo * poOpenInfo ) /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ - GRASSDataset *poDS; - - poDS = new GRASSDataset(); + GRASSDataset*poDS = new GRASSDataset(); /* notdef: should only allow read access to an existing cell, right? */ poDS->eAccess = poOpenInfo->eAccess; @@ -997,14 +1003,14 @@ GDALDataset *GRASSDataset::Open( GDALOpenInfo * poOpenInfo ) /* -------------------------------------------------------------------- */ for( int iBand = 0; papszCells[iBand] != NULL; iBand++ ) { - GRASSRasterBand *rb = new GRASSRasterBand( poDS, iBand+1, papszMapsets[iBand], + GRASSRasterBand *rb = new GRASSRasterBand( poDS, iBand+1, papszMapsets[iBand], papszCells[iBand] ); - if ( !rb->valid ) { - CPLError( CE_Warning, CPLE_AppDefined, "GRASS: Cannot open raster band %d", iBand); - delete rb; - delete poDS; - return NULL; + if ( !rb->valid ) { + CPLError( CE_Warning, CPLE_AppDefined, "GRASS: Cannot open raster band %d", iBand); + delete rb; + delete poDS; + return NULL; } poDS->SetBand( iBand+1, rb ); diff --git a/ogrgrass.h b/ogrgrass.h index 3cda766..b98e358 100644 --- a/ogrgrass.h +++ b/ogrgrass.h @@ -1,5 +1,5 @@ /****************************************************************************** - * $Id: ogrgrass.h 33713 2016-03-12 17:41:57Z goatbar $ + * $Id: ogrgrass.h 36501 2016-11-25 14:09:24Z rouault $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Private definitions for OGR/GRASS driver. @@ -50,81 +50,76 @@ extern "C" { class OGRGRASSLayer : public OGRLayer { public: - OGRGRASSLayer( int layer, struct Map_info * map ); - ~OGRGRASSLayer(); + OGRGRASSLayer( int layer, struct Map_info * map ); + virtual ~OGRGRASSLayer(); // Layer info - OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; } - GIntBig GetFeatureCount( int ); - OGRErr GetExtent(OGREnvelope *psExtent, int bForce); - virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent, int bForce) + OGRFeatureDefn * GetLayerDefn() override { return poFeatureDefn; } + GIntBig GetFeatureCount( int ) override; + OGRErr GetExtent(OGREnvelope *psExtent, int bForce) override; + virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent, int bForce) override { return OGRLayer::GetExtent(iGeomField, psExtent, bForce); } - virtual OGRSpatialReference *GetSpatialRef(); - int TestCapability( const char * ); + virtual OGRSpatialReference *GetSpatialRef() override; + int TestCapability( const char * ) override; // Reading - void ResetReading(); - virtual OGRErr SetNextByIndex( GIntBig nIndex ); - OGRFeature * GetNextFeature(); - OGRFeature *GetFeature( GIntBig nFeatureId ); + void ResetReading() override; + virtual OGRErr SetNextByIndex( GIntBig nIndex ) override; + OGRFeature * GetNextFeature() override; + OGRFeature *GetFeature( GIntBig nFeatureId ) override; // Filters - virtual OGRErr SetAttributeFilter( const char *query ); - virtual void SetSpatialFilter( OGRGeometry * poGeomIn ); - virtual void SetSpatialFilter( int iGeomField, OGRGeometry *poGeom ) + virtual OGRErr SetAttributeFilter( const char *query ) override; + virtual void SetSpatialFilter( OGRGeometry * poGeomIn ) override; + virtual void SetSpatialFilter( int iGeomField, OGRGeometry *poGeom ) override { OGRLayer::SetSpatialFilter(iGeomField, poGeom); } - // Write access, not supported: - virtual OGRErr CreateField( OGRFieldDefn *poField, int bApproxOK = TRUE ); - OGRErr ISetFeature( OGRFeature *poFeature ); - OGRErr ICreateFeature( OGRFeature *poFeature ); - private: - char *pszName; + char *pszName; OGRSpatialReference *poSRS; - OGRFeatureDefn *poFeatureDefn; - char *pszQuery; // Attribute filter string + OGRFeatureDefn *poFeatureDefn; + char *pszQuery; // Attribute filter string - int iNextId; - int nTotalCount; - int iLayer; // Layer number - int iLayerIndex; // Layer index (in GRASS category index) - int iCatField; // Field where category (key) is stored - int nFields; - int *paFeatureIndex; // Array of indexes to category index array + int iNextId; + int nTotalCount; + int iLayer; // Layer number + int iLayerIndex; // Layer index (in GRASS category index) + int iCatField; // Field where category (key) is stored + int nFields; + int *paFeatureIndex; // Array of indexes to category index array // Vector map - struct Map_info *poMap; + struct Map_info *poMap; struct field_info *poLink; // Database connection - bool bHaveAttributes; + bool bHaveAttributes; - dbString *poDbString; - dbDriver *poDriver; - dbCursor *poCursor; + dbString *poDbString; + dbDriver *poDriver; + dbCursor *poCursor; - bool bCursorOpened; // Sequential database cursor opened - int iCurrentCat; // Current category in select cursor + bool bCursorOpened; // Sequential database cursor opened + int iCurrentCat; // Current category in select cursor - struct line_pnts *poPoints; - struct line_cats *poCats; + struct line_pnts *poPoints; + struct line_cats *poCats; - bool StartDbDriver (); - bool StopDbDriver (); + bool StartDbDriver (); + bool StopDbDriver (); - OGRGeometry *GetFeatureGeometry ( long nFeatureId, int *cat ); - bool SetAttributes ( OGRFeature *feature, dbTable *table ); + OGRGeometry *GetFeatureGeometry ( long nFeatureId, int *cat ); + bool SetAttributes ( OGRFeature *feature, dbTable *table ); // Features matching spatial filter for ALL features/elements in GRASS - char *paSpatialMatch; - bool SetSpatialMatch(); + char *paSpatialMatch; + bool SetSpatialMatch(); // Features matching attribute filter for ALL features/elements in GRASS - char *paQueryMatch; - bool OpenSequentialCursor(); - bool ResetSequentialCursor(); - bool SetQueryMatch(); + char *paQueryMatch; + bool OpenSequentialCursor(); + bool ResetSequentialCursor(); + bool SetQueryMatch(); }; /************************************************************************/ @@ -134,33 +129,26 @@ class OGRGRASSDataSource : public OGRDataSource { public: OGRGRASSDataSource(); - ~OGRGRASSDataSource(); + virtual ~OGRGRASSDataSource(); int Open( const char *, int bUpdate, int bTestOpen, int bSingleNewFile = FALSE ); - const char *GetName() { return pszName; } - int GetLayerCount() { return nLayers; } - OGRLayer *GetLayer( int ); - - int TestCapability( const char * ); - - // Not implemented (returns NULL): - virtual OGRLayer *ICreateLayer( const char *, - OGRSpatialReference * = NULL, - OGRwkbGeometryType = wkbUnknown, - char ** = NULL ); + const char *GetName() override { return pszName; } + int GetLayerCount() override { return nLayers; } + OGRLayer *GetLayer( int ) override; + int TestCapability( const char * ) override; private: OGRGRASSLayer **papoLayers; - char *pszName; // Date source name - char *pszGisdbase; // GISBASE - char *pszLocation; // location name - char *pszMapset; // mapset name - char *pszMap; // name of vector map + char *pszName; // Date source name + char *pszGisdbase; // GISBASE + char *pszLocation; // location name + char *pszMapset; // mapset name + char *pszMap; // name of vector map - struct Map_info map; + struct Map_info map; int nLayers; int bOpened; @@ -174,17 +162,12 @@ class OGRGRASSDataSource : public OGRDataSource class OGRGRASSDriver : public OGRSFDriver { public: - ~OGRGRASSDriver(); - - const char *GetName(); - OGRDataSource *Open( const char *, int ); + virtual ~OGRGRASSDriver(); - int TestCapability( const char * ); + const char *GetName() override; + OGRDataSource *Open( const char *, int ) override; - // Not implemented (return error/NULL): - virtual OGRDataSource *CreateDataSource( const char *pszName, - char ** = NULL ); - OGRErr DeleteDataSource( const char *pszDataSource ); + int TestCapability( const char * ) override; }; #endif /* ndef OGRGRASS_H_INCLUDED */ diff --git a/ogrgrassdatasource.cpp b/ogrgrassdatasource.cpp index e454f0a..df184b9 100644 --- a/ogrgrassdatasource.cpp +++ b/ogrgrassdatasource.cpp @@ -1,5 +1,4 @@ /****************************************************************************** - * $Id: ogrgrassdatasource.cpp 33713 2016-03-12 17:41:57Z goatbar $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements OGRGRASSDataSource class. @@ -32,7 +31,7 @@ #include "cpl_conv.h" #include "cpl_string.h" -CPL_CVSID("$Id: ogrgrassdatasource.cpp 33713 2016-03-12 17:41:57Z goatbar $"); +CPL_CVSID("$Id: ogrgrassdatasource.cpp 36368 2016-11-21 01:47:49Z rouault $"); #if GRASS_VERSION_MAJOR >= 7 #define G__setenv G_setenv_nogisrc @@ -41,7 +40,7 @@ CPL_CVSID("$Id: ogrgrassdatasource.cpp 33713 2016-03-12 17:41:57Z goatbar $"); /************************************************************************/ /* Grass2CPLErrorHook() */ /************************************************************************/ -int Grass2OGRErrorHook( char * pszMessage, int bFatal ) +static int Grass2OGRErrorHook( char * pszMessage, int bFatal ) { if( !bFatal ) CPLError( CE_Warning, CPLE_AppDefined, "GRASS warning: %s", pszMessage ); @@ -95,8 +94,8 @@ typedef int (*GrassErrorHandler)(const char *, int); typedef int (*GrassErrorHandler)(char *, int); #endif -int OGRGRASSDataSource::Open( const char * pszNewName, int bUpdate, - int bTestOpen, int bSingleNewFileIn ) +int OGRGRASSDataSource::Open( const char * pszNewName, int /*bUpdate*/, + int bTestOpen, int /*bSingleNewFileIn*/ ) { VSIStatBuf stat; @@ -110,11 +109,11 @@ int OGRGRASSDataSource::Open( const char * pszNewName, int bUpdate, if ( strstr(pszName,"vector") == NULL || strstr(pszName,"head") == NULL ) { if( !bTestOpen ) - { + { CPLError( CE_Failure, CPLE_AppDefined, "%s is not GRASS vector, access failed.\n", pszName ); - } - return FALSE; + } + return FALSE; } /* -------------------------------------------------------------------- */ @@ -123,10 +122,10 @@ int OGRGRASSDataSource::Open( const char * pszNewName, int bUpdate, if( CPLStat( pszName, &stat ) != 0 || !VSI_ISREG(stat.st_mode) ) { if( !bTestOpen ) - { + { CPLError( CE_Failure, CPLE_AppDefined, "%s is not GRASS vector, access failed.\n", pszName ); - } + } return FALSE; } @@ -135,15 +134,15 @@ int OGRGRASSDataSource::Open( const char * pszNewName, int bUpdate, /* Parse datasource name */ /* -------------------------------------------------------------------- */ if ( !SplitPath(pszName, &pszGisdbase, &pszLocation, - &pszMapset, &pszMap) ) + &pszMapset, &pszMap) ) { if( !bTestOpen ) - { + { CPLError( CE_Failure, CPLE_AppDefined, "%s is not GRASS datasource name, access failed.\n", - pszName ); - } - return FALSE; + pszName ); + } + return FALSE; } CPLDebug ( "GRASS", "Gisdbase: %s", pszGisdbase ); @@ -202,7 +201,7 @@ int OGRGRASSDataSource::Open( const char * pszNewName, int bUpdate, if ( level < 2 ) { CPLError( CE_Failure, CPLE_AppDefined, "Cannot open GRASS vector %s on level 2.\n", pszName ); - return FALSE; + return FALSE; } CPLDebug ( "GRASS", "Num lines = %d", Vect_get_num_lines(&map) ); @@ -214,15 +213,13 @@ int OGRGRASSDataSource::Open( const char * pszNewName, int bUpdate, CPLDebug ( "GRASS", "Num layers = %d", ncidx ); for ( int i = 0; i < ncidx; i++ ) { - // Create the layer object - OGRGRASSLayer *poLayer; - - poLayer = new OGRGRASSLayer( i, &map ); + // Create the layer object + OGRGRASSLayer *poLayer = new OGRGRASSLayer( i, &map ); // Add layer to data source layer list - papoLayers = (OGRGRASSLayer **) - CPLRealloc( papoLayers, sizeof(OGRGRASSLayer *) * (nLayers+1) ); - papoLayers[nLayers++] = poLayer; + papoLayers = (OGRGRASSLayer **) + CPLRealloc( papoLayers, sizeof(OGRGRASSLayer *) * (nLayers+1) ); + papoLayers[nLayers++] = poLayer; } bOpened = TRUE; @@ -231,25 +228,9 @@ int OGRGRASSDataSource::Open( const char * pszNewName, int bUpdate, } /************************************************************************/ -/* ICreateLayer() */ -/************************************************************************/ -OGRLayer * -OGRGRASSDataSource::ICreateLayer( const char * pszLayerName, - OGRSpatialReference *poSRS, - OGRwkbGeometryType eType, - char ** papszOptions ) - -{ - CPLError( CE_Failure, CPLE_NoWriteAccess, - "CreateLayer is not supported by GRASS driver" ); - - return NULL; -} - -/************************************************************************/ /* TestCapability() */ /************************************************************************/ -int OGRGRASSDataSource::TestCapability( const char * pszCap ) +int OGRGRASSDataSource::TestCapability( const char * /* pszCap*/ ) { return FALSE; } @@ -275,7 +256,7 @@ OGRLayer *OGRGRASSDataSource::GetLayer( int iLayer ) /* false - failed */ /************************************************************************/ bool OGRGRASSDataSource::SplitPath( char *path, char **gisdbase, - char **location, char **mapset, char **map ) + char **location, char **mapset, char **map ) { char *p, *ptr[5], *tmp; int i = 0; @@ -285,23 +266,23 @@ bool OGRGRASSDataSource::SplitPath( char *path, char **gisdbase, *gisdbase = *location = *mapset = *map = NULL; if ( !path || strlen(path) == 0 ) - return false; + return false; tmp = G_store ( path ); while ( (p = strrchr(tmp,'/')) != NULL && i < 5 ) { - *p = '\0'; + *p = '\0'; - if ( strlen(p+1) == 0 ) /* repeated '/' */ - continue; + if ( strlen(p+1) == 0 ) /* repeated '/' */ + continue; - ptr[i++] = p+1; + ptr[i++] = p+1; } /* Note: empty GISDBASE == 0 is not accepted (relative path) */ if ( i != 5 ) { free ( tmp ); - return false; + return false; } if ( strcmp(ptr[0],"head") != 0 || strcmp(ptr[2],"vector") != 0 ) { diff --git a/ogrgrassdriver.cpp b/ogrgrassdriver.cpp index f1a9521..c09d45d 100644 --- a/ogrgrassdriver.cpp +++ b/ogrgrassdriver.cpp @@ -1,5 +1,4 @@ /****************************************************************************** - * $Id: ogrgrassdriver.cpp 32110 2015-12-10 17:19:40Z goatbar $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements OGRGRASSDriver class. @@ -31,7 +30,7 @@ #include "cpl_conv.h" #include "cpl_string.h" -CPL_CVSID("$Id: ogrgrassdriver.cpp 32110 2015-12-10 17:19:40Z goatbar $"); +CPL_CVSID("$Id: ogrgrassdriver.cpp 36368 2016-11-21 01:47:49Z rouault $"); /************************************************************************/ /* ~OGRGRASSDriver() */ @@ -54,9 +53,7 @@ const char *OGRGRASSDriver::GetName() OGRDataSource *OGRGRASSDriver::Open( const char * pszFilename, int bUpdate ) { - OGRGRASSDataSource *poDS; - - poDS = new OGRGRASSDataSource(); + OGRGRASSDataSource *poDS = new OGRGRASSDataSource(); if( !poDS->Open( pszFilename, bUpdate, TRUE ) ) { @@ -70,32 +67,9 @@ OGRDataSource *OGRGRASSDriver::Open( const char * pszFilename, } /************************************************************************/ -/* CreateDataSource() */ -/************************************************************************/ -OGRDataSource *OGRGRASSDriver::CreateDataSource( const char * pszName, - char **papszOptions ) -{ - CPLError( CE_Failure, CPLE_AppDefined, - "CreateDataSource is not supported by GRASS driver.\n" ); - - return NULL; -} - -/************************************************************************/ -/* DeleteDataSource() */ -/************************************************************************/ -OGRErr OGRGRASSDriver::DeleteDataSource( const char *pszDataSource ) -{ - CPLError( CE_Failure, CPLE_AppDefined, - "DeleteDataSource is not supported by GRASS driver" ); - - return OGRERR_FAILURE; -} - -/************************************************************************/ /* TestCapability() */ /************************************************************************/ -int OGRGRASSDriver::TestCapability( const char * pszCap ) +int OGRGRASSDriver::TestCapability( const char * /*pszCap*/ ) { return FALSE; } @@ -120,4 +94,3 @@ void RegisterOGRGRASS() OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( poDriver ); } - diff --git a/ogrgrasslayer.cpp b/ogrgrasslayer.cpp index a2be8ea..ea2030e 100644 --- a/ogrgrasslayer.cpp +++ b/ogrgrasslayer.cpp @@ -1,5 +1,4 @@ /****************************************************************************** - * $Id: ogrgrasslayer.cpp 33713 2016-03-12 17:41:57Z goatbar $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements OGRGRASSLayer class. @@ -32,7 +31,7 @@ #include "ogrgrass.h" #include "cpl_conv.h" -CPL_CVSID("$Id: ogrgrasslayer.cpp 33713 2016-03-12 17:41:57Z goatbar $"); +CPL_CVSID("$Id: ogrgrasslayer.cpp 36368 2016-11-21 01:47:49Z rouault $"); /************************************************************************/ /* OGRGRASSLayer() */ @@ -50,6 +49,7 @@ OGRGRASSLayer::OGRGRASSLayer( int layerIndex, struct Map_info * map ) pszQuery = NULL; paQueryMatch = NULL; paSpatialMatch = NULL; + iCurrentCat = 0; iLayer = Vect_cidx_get_field_number ( poMap, iLayerIndex); CPLDebug ( "GRASS", "iLayer = %d", iLayer ); @@ -59,13 +59,13 @@ OGRGRASSLayer::OGRGRASSLayer( int layerIndex, struct Map_info * map ) // Layer name if ( poLink && poLink->name ) { - pszName = CPLStrdup( poLink->name ); + pszName = CPLStrdup( poLink->name ); } else { - char buf[20]; - sprintf ( buf, "%d", iLayer ); - pszName = CPLStrdup( buf ); + char buf[20]; + snprintf ( buf, sizeof(buf), "%d", iLayer ); + pszName = CPLStrdup( buf ); } // Because we don't represent centroids as any simple feature, we have to scan @@ -78,12 +78,12 @@ OGRGRASSLayer::OGRGRASSLayer( int layerIndex, struct Map_info * map ) int cnt = 0; for ( int i = 0; i < n; i++ ) { - int cat,type, id; + int cat,type, id; - Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, i, &cat, &type, &id ); + Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, i, &cat, &type, &id ); - if ( !( type & (GV_POINT|GV_LINES|GV_AREA) ) ) continue; - paFeatureIndex[cnt++] = i; + if ( !( type & (GV_POINT|GV_LINES|GV_AREA) ) ) continue; + paFeatureIndex[cnt++] = i; } poFeatureDefn = new OGRFeatureDefn( pszName ); @@ -94,10 +94,10 @@ OGRGRASSLayer::OGRGRASSLayer( int layerIndex, struct Map_info * map ) int nTypes = Vect_cidx_get_num_types_by_index ( poMap, iLayerIndex ); int types = 0; for ( int i = 0; i < nTypes; i++ ) { - int type, count; - Vect_cidx_get_type_count_by_index ( poMap, iLayerIndex, i, &type, &count); - if ( !(type & (GV_POINT|GV_LINES|GV_AREA) ) ) continue; - types |= type; + int type, count; + Vect_cidx_get_type_count_by_index ( poMap, iLayerIndex, i, &type, &count); + if ( !(type & (GV_POINT|GV_LINES|GV_AREA) ) ) continue; + types |= type; CPLDebug ( "GRASS", "type = %d types = %d", type, types ); } @@ -131,72 +131,71 @@ OGRGRASSLayer::OGRGRASSLayer( int layerIndex, struct Map_info * map ) db_init_string ( poDbString ); if ( poLink ) { - if ( StartDbDriver() ) - { - db_set_string ( poDbString, poLink->table ); - dbTable *table; - if ( db_describe_table ( poDriver, poDbString, &table) == DB_OK ) - { - nFields = db_get_table_number_of_columns ( table ); - iCatField = -1; - for ( int i = 0; i < nFields; i++) - { - dbColumn *column = db_get_table_column ( table, i ); - int ctype = db_sqltype_to_Ctype ( db_get_column_sqltype(column) ); - - OGRFieldType ogrFtype = OFTInteger; - switch ( ctype ) { - case DB_C_TYPE_INT: - ogrFtype = OFTInteger; - break; - case DB_C_TYPE_DOUBLE: - ogrFtype = OFTReal; - break; - case DB_C_TYPE_STRING: - ogrFtype = OFTString; - break; - case DB_C_TYPE_DATETIME: - ogrFtype = OFTDateTime; - break; - } - - CPLDebug ( "GRASS", "column = %s type = %d", - db_get_column_name(column), ctype ); - - OGRFieldDefn oField ( db_get_column_name(column), ogrFtype ); - poFeatureDefn->AddFieldDefn( &oField ); - - if ( G_strcasecmp(db_get_column_name(column),poLink->key) == 0 ) - { - iCatField = i; - } - } - if ( iCatField >= 0 ) - { - bHaveAttributes = true; - } - else - { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot find key field" ); - db_close_database_shutdown_driver ( poDriver ); - poDriver = NULL; + if ( StartDbDriver() ) + { + db_set_string ( poDbString, poLink->table ); + dbTable *table = NULL; + if ( db_describe_table ( poDriver, poDbString, &table) == DB_OK ) + { + nFields = db_get_table_number_of_columns ( table ); + iCatField = -1; + for ( int i = 0; i < nFields; i++) + { + dbColumn *column = db_get_table_column ( table, i ); + int ctype = db_sqltype_to_Ctype ( db_get_column_sqltype(column) ); + + OGRFieldType ogrFtype = OFTInteger; + switch ( ctype ) { + case DB_C_TYPE_INT: + ogrFtype = OFTInteger; + break; + case DB_C_TYPE_DOUBLE: + ogrFtype = OFTReal; + break; + case DB_C_TYPE_STRING: + ogrFtype = OFTString; + break; + case DB_C_TYPE_DATETIME: + ogrFtype = OFTDateTime; + break; + } + + CPLDebug ( "GRASS", "column = %s type = %d", + db_get_column_name(column), ctype ); + + OGRFieldDefn oField ( db_get_column_name(column), ogrFtype ); + poFeatureDefn->AddFieldDefn( &oField ); + + if ( G_strcasecmp(db_get_column_name(column),poLink->key) == 0 ) + { + iCatField = i; + } + } + if ( iCatField >= 0 ) + { + bHaveAttributes = true; + } + else + { + CPLError( CE_Failure, CPLE_AppDefined, "Cannot find key field" ); + db_close_database_shutdown_driver ( poDriver ); + poDriver = NULL; } } - else + else { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot describe table %s", - poLink->table ); - + CPLError( CE_Failure, CPLE_AppDefined, "Cannot describe table %s", + poLink->table ); } - db_close_database_shutdown_driver ( poDriver ); - poDriver = NULL; + db_close_database_shutdown_driver ( poDriver ); + poDriver = NULL; } } if ( !bHaveAttributes && iLayer > 0 ) // Because features in layer 0 have no cats { - OGRFieldDefn oField("cat", OFTInteger); - poFeatureDefn->AddFieldDefn( &oField ); + OGRFieldDefn oField("cat", OFTInteger); + poFeatureDefn->AddFieldDefn( &oField ); } if ( getenv("GISBASE") ) // We have some projection info in GISBASE @@ -208,13 +207,13 @@ OGRGRASSLayer::OGRGRASSLayer( int layerIndex, struct Map_info * map ) // where those variables are set projinfo = G_get_projinfo(); - projunits = G_get_projunits(); + projunits = G_get_projunits(); - char *srsWkt = GPJ_grass_to_wkt ( projinfo, projunits, 0, 0); + char *srsWkt = GPJ_grass_to_wkt ( projinfo, projunits, 0, 0); if ( srsWkt ) { - poSRS = new OGRSpatialReference ( srsWkt ); - G_free ( srsWkt ); + poSRS = new OGRSpatialReference ( srsWkt ); + G_free ( srsWkt ); } G_free_key_value(projinfo); @@ -229,12 +228,12 @@ OGRGRASSLayer::~OGRGRASSLayer() { if ( bCursorOpened ) { - db_close_cursor ( poCursor); + db_close_cursor ( poCursor); } if ( poDriver ) { - StopDbDriver(); + StopDbDriver(); } if ( pszName ) CPLFree ( pszName ); @@ -271,16 +270,16 @@ bool OGRGRASSLayer::StartDbDriver() if ( !poLink ) { - return false; + return false; } poDriver = db_start_driver_open_database ( poLink->driver, poLink->database ); if ( poDriver == NULL) { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot open database %s by driver %s, " - "check if GISBASE environment variable is set, the driver is available " - " and the database is accessible.", poLink->driver, poLink->database ); - return false; + CPLError( CE_Failure, CPLE_AppDefined, "Cannot open database %s by driver %s, " + "check if GISBASE environment variable is set, the driver is available " + " and the database is accessible.", poLink->driver, poLink->database ); + return false; } return true; } @@ -292,8 +291,8 @@ bool OGRGRASSLayer::StopDbDriver() { if ( !poDriver ) { - CPLError( CE_Failure, CPLE_AppDefined, "Driver is not started" ); - return true; // I think that true is OK here + CPLError( CE_Failure, CPLE_AppDefined, "Driver is not started" ); + return true; // I think that true is OK here } // TODO!!!: Because of bug in GRASS library it is impossible @@ -341,25 +340,25 @@ OGRErr OGRGRASSLayer::SetNextByIndex( GIntBig nIndex ) { if( m_poFilterGeom != NULL || m_poAttrQuery != NULL ) { - iNextId = 0; - int count = 0; + iNextId = 0; + int count = 0; - while ( true ) { - if( iNextId >= nTotalCount ) break; - if ( count == nIndex ) break; + while ( true ) { + if( iNextId >= nTotalCount ) break; + if ( count == nIndex ) break; - // Attributes - if( pszQuery != NULL && !paQueryMatch[iNextId] ) { - iNextId++; - continue; + // Attributes + if( pszQuery != NULL && !paQueryMatch[iNextId] ) { + iNextId++; + continue; } - // Spatial - if( m_poFilterGeom && !paSpatialMatch[iNextId] ) { - iNextId++; - continue; - } - count++; + // Spatial + if( m_poFilterGeom && !paSpatialMatch[iNextId] ) { + iNextId++; + continue; + } + count++; } } @@ -376,16 +375,16 @@ OGRErr OGRGRASSLayer::SetAttributeFilter( const char *query ) CPLDebug ( "GRASS", "SetAttributeFilter: %s", query ); if ( query == NULL ) { - // Release old if any - if ( pszQuery ) { - CPLFree ( pszQuery ); - pszQuery = NULL; - } - if ( paQueryMatch ) { - CPLFree ( paQueryMatch ); - paQueryMatch = NULL; - } - return OGRERR_NONE; + // Release old if any + if ( pszQuery ) { + CPLFree ( pszQuery ); + pszQuery = NULL; + } + if ( paQueryMatch ) { + CPLFree ( paQueryMatch ); + paQueryMatch = NULL; + } + return OGRERR_NONE; } paQueryMatch = (char *) CPLMalloc ( nTotalCount ); @@ -396,53 +395,53 @@ OGRErr OGRGRASSLayer::SetAttributeFilter( const char *query ) if ( bHaveAttributes ) { - if ( !poDriver ) - { - StartDbDriver(); - } - - if ( poDriver ) - { - if ( bCursorOpened ) - { - db_close_cursor ( poCursor ); - bCursorOpened = false; - } - OpenSequentialCursor(); - if ( bCursorOpened ) - { - SetQueryMatch(); - db_close_cursor ( poCursor ); - bCursorOpened = false; - } - else - { - CPLFree ( pszQuery ); - pszQuery = NULL; - return OGRERR_FAILURE; - } - db_close_database_shutdown_driver ( poDriver ); - poDriver = NULL; - } - else - { - CPLFree ( pszQuery ); - pszQuery = NULL; - return OGRERR_FAILURE; - } + if ( !poDriver ) + { + StartDbDriver(); + } + + if ( poDriver ) + { + if ( bCursorOpened ) + { + db_close_cursor ( poCursor ); + bCursorOpened = false; + } + OpenSequentialCursor(); + if ( bCursorOpened ) + { + SetQueryMatch(); + db_close_cursor ( poCursor ); + bCursorOpened = false; + } + else + { + CPLFree ( pszQuery ); + pszQuery = NULL; + return OGRERR_FAILURE; + } + db_close_database_shutdown_driver ( poDriver ); + poDriver = NULL; + } + else + { + CPLFree ( pszQuery ); + pszQuery = NULL; + return OGRERR_FAILURE; + } } else { - // Use OGR to evaluate category match - for ( int i = 0; i < nTotalCount; i++ ) - { - OGRFeature *feature = GetFeature(i); - CPLDebug ( "GRASS", "i = %d eval = %d", i, m_poAttrQuery->Evaluate ( feature ) ); - if ( m_poAttrQuery->Evaluate ( feature ) ) - { - paQueryMatch[i] = 1; - } - } + // Use OGR to evaluate category match + for ( int i = 0; i < nTotalCount; i++ ) + { + OGRFeature *feature = GetFeature(i); + CPLDebug ( "GRASS", "i = %d eval = %d", i, m_poAttrQuery->Evaluate ( feature ) ); + if ( m_poAttrQuery->Evaluate ( feature ) ) + { + paQueryMatch[i] = 1; + } + } } return OGRERR_NONE; @@ -459,8 +458,8 @@ bool OGRGRASSLayer::SetQueryMatch() // this method is called immediately after OpenSequentialCursor() if ( !bCursorOpened ) { - CPLError( CE_Failure, CPLE_AppDefined, "Cursor is not opened."); - return false; + CPLError( CE_Failure, CPLE_AppDefined, "Cursor is not opened."); + return false; } int more; @@ -470,58 +469,58 @@ bool OGRGRASSLayer::SetQueryMatch() int ncats = Vect_cidx_get_num_cats_by_index ( poMap, iLayerIndex ); dbTable *table = db_get_cursor_table ( poCursor ); while ( true ) { - if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) - { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot fetch attributes."); - return false; - } - if ( !more ) break; - - dbColumn *column = db_get_table_column ( table, iCatField ); - dbValue *value = db_get_column_value ( column ); - int cat = db_get_value_int ( value ); - - // NOTE: because of bug in GRASS library it is impossible to use - // Vect_cidx_find_next - - // Go through category index until first record of current category - // is found or a category > current is found - int cidxcat, type, id; - while ( cidx < ncats ) { - Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, cidx, - &cidxcat, &type, &id ); - - if ( cidxcat < cat ) { - cidx++; - continue; - } - if ( cidxcat > cat ) break; // Not found - - // We have the category we want, check type - if ( !(type & (GV_POINT|GV_LINES|GV_AREA)) ) - { - cidx++; - continue; - } - - // Both category and type match -> find feature and set it on - while ( true ) { - if ( fidx > nTotalCount || paFeatureIndex[fidx] > cidx ) { - // should not happen - break; - } - - if ( paFeatureIndex[fidx] == cidx ) { - paQueryMatch[fidx] = 1; - fidx++; - break; - } - fidx++; - } - cidx++; - } - - if ( id < 0 ) continue; // not found + if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) + { + CPLError( CE_Failure, CPLE_AppDefined, "Cannot fetch attributes."); + return false; + } + if ( !more ) break; + + dbColumn *column = db_get_table_column ( table, iCatField ); + dbValue *value = db_get_column_value ( column ); + int cat = db_get_value_int ( value ); + + // NOTE: because of bug in GRASS library it is impossible to use + // Vect_cidx_find_next + + // Go through category index until first record of current category + // is found or a category > current is found + int cidxcat, type, id; + while ( cidx < ncats ) { + Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, cidx, + &cidxcat, &type, &id ); + + if ( cidxcat < cat ) { + cidx++; + continue; + } + if ( cidxcat > cat ) break; // Not found + + // We have the category we want, check type + if ( !(type & (GV_POINT|GV_LINES|GV_AREA)) ) + { + cidx++; + continue; + } + + // Both category and type match -> find feature and set it on + while ( true ) { + if ( fidx > nTotalCount || paFeatureIndex[fidx] > cidx ) { + // should not happen + break; + } + + if ( paFeatureIndex[fidx] == cidx ) { + paQueryMatch[fidx] = 1; + fidx++; + break; + } + fidx++; + } + cidx++; + } + + if ( id < 0 ) continue; // not found } return true; @@ -536,41 +535,41 @@ bool OGRGRASSLayer::OpenSequentialCursor() if ( !poDriver ) { - CPLError( CE_Failure, CPLE_AppDefined, "Driver not opened."); - return false; + CPLError( CE_Failure, CPLE_AppDefined, "Driver not opened."); + return false; } if ( bCursorOpened ) { - db_close_cursor ( poCursor ); - bCursorOpened = false; + db_close_cursor ( poCursor ); + bCursorOpened = false; } char buf[2000]; - sprintf ( buf, "SELECT * FROM %s ", poLink->table ); + snprintf ( buf, sizeof(buf), "SELECT * FROM %s ", poLink->table ); db_set_string ( poDbString, buf); if ( pszQuery ) { - sprintf ( buf, "WHERE %s ", pszQuery ); - db_append_string ( poDbString, buf); + snprintf ( buf, sizeof(buf), "WHERE %s ", pszQuery ); + db_append_string ( poDbString, buf); } - sprintf ( buf, "ORDER BY %s", poLink->key); + snprintf ( buf, sizeof(buf), "ORDER BY %s", poLink->key); db_append_string ( poDbString, buf); CPLDebug ( "GRASS", "Query: %s", db_get_string(poDbString) ); if ( db_open_select_cursor ( poDriver, poDbString, - poCursor, DB_SCROLL) == DB_OK ) + poCursor, DB_SCROLL) == DB_OK ) { - iCurrentCat = -1; - bCursorOpened = true; - CPLDebug ( "GRASS", "num rows = %d", db_get_num_rows ( poCursor ) ); + iCurrentCat = -1; + bCursorOpened = true; + CPLDebug ( "GRASS", "num rows = %d", db_get_num_rows ( poCursor ) ); } else { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot open cursor."); - return false; + CPLError( CE_Failure, CPLE_AppDefined, "Cannot open cursor."); + return false; } return true; } @@ -585,13 +584,13 @@ bool OGRGRASSLayer::ResetSequentialCursor() int more; if( db_fetch ( poCursor, DB_FIRST, &more) != DB_OK ) { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot reset cursor."); - return false; + CPLError( CE_Failure, CPLE_AppDefined, "Cannot reset cursor."); + return false; } if( db_fetch ( poCursor, DB_PREVIOUS, &more) != DB_OK ) { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot reset cursor."); - return false; + CPLError( CE_Failure, CPLE_AppDefined, "Cannot reset cursor."); + return false; } return true; } @@ -606,12 +605,12 @@ void OGRGRASSLayer::SetSpatialFilter( OGRGeometry * poGeomIn ) OGRLayer::SetSpatialFilter ( poGeomIn ); if ( poGeomIn == NULL ) { - // Release old if any - if ( paSpatialMatch ) { - CPLFree ( paSpatialMatch ); - paSpatialMatch = NULL; - } - return; + // Release old if any + if ( paSpatialMatch ) { + CPLFree ( paSpatialMatch ); + paSpatialMatch = NULL; + } + return; } SetSpatialMatch(); @@ -626,51 +625,50 @@ bool OGRGRASSLayer::SetSpatialMatch() if ( !paSpatialMatch ) { - paSpatialMatch = (char *) CPLMalloc ( nTotalCount ); + paSpatialMatch = (char *) CPLMalloc ( nTotalCount ); } memset ( paSpatialMatch, 0x0, nTotalCount ); - OGRGeometry *geom; OGRLineString *lstring = new OGRLineString(); lstring->setNumPoints ( 5 ); - geom = lstring; + OGRGeometry *geom = lstring; for ( int i = 0; i < nTotalCount; i++ ) { - int cidx = paFeatureIndex[i]; + int cidx = paFeatureIndex[i]; - int cat, type, id; + int cat, type, id; - Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, cidx, &cat, &type, &id ); + Vect_cidx_get_cat_by_index ( poMap, iLayerIndex, cidx, &cat, &type, &id ); #if GRASS_VERSION_MAJOR >= 7 struct bound_box box; #else - BOUND_BOX box; + BOUND_BOX box; #endif - switch ( type ) - { - case GV_POINT: - case GV_LINE: - case GV_BOUNDARY: - Vect_get_line_box ( poMap, id, &box ); - break; - - case GV_AREA: - Vect_get_area_box ( poMap, id, &box ); - break; - } - - lstring->setPoint( 0, box.W, box.N, 0. ); - lstring->setPoint( 1, box.W, box.S, 0. ); - lstring->setPoint( 2, box.E, box.S, 0. ); - lstring->setPoint( 3, box.E, box.N, 0. ); - lstring->setPoint( 4, box.W, box.N, 0. ); - - if ( FilterGeometry(geom) ) { - CPLDebug ( "GRASS", "Feature %d in filter", i ); - paSpatialMatch[i] = 1; - } + switch ( type ) + { + case GV_POINT: + case GV_LINE: + case GV_BOUNDARY: + Vect_get_line_box ( poMap, id, &box ); + break; + + case GV_AREA: + Vect_get_area_box ( poMap, id, &box ); + break; + } + + lstring->setPoint( 0, box.W, box.N, 0. ); + lstring->setPoint( 1, box.W, box.S, 0. ); + lstring->setPoint( 2, box.E, box.S, 0. ); + lstring->setPoint( 3, box.E, box.N, 0. ); + lstring->setPoint( 4, box.W, box.N, 0. ); + + if ( FilterGeometry(geom) ) { + CPLDebug ( "GRASS", "Feature %d in filter", i ); + paSpatialMatch[i] = 1; + } } delete lstring; return true; @@ -688,36 +686,36 @@ OGRFeature *OGRGRASSLayer::GetNextFeature() // Get next iNextId while ( true ) { - if( iNextId >= nTotalCount ) // No more features - { - // Close cursor / driver if opened - if ( bCursorOpened ) - { - db_close_cursor ( poCursor); - bCursorOpened = false; - } - if ( poDriver ) - { - db_close_database_shutdown_driver ( poDriver ); - poDriver = NULL; - } - - return NULL; - } - - // Attributes - if( pszQuery != NULL && !paQueryMatch[iNextId] ) { - iNextId++; - continue; - } - - // Spatial - if( m_poFilterGeom && !paSpatialMatch[iNextId] ) { - iNextId++; - continue; - } - - break; // Attributes & spatial filter match + if( iNextId >= nTotalCount ) // No more features + { + // Close cursor / driver if opened + if ( bCursorOpened ) + { + db_close_cursor ( poCursor); + bCursorOpened = false; + } + if ( poDriver ) + { + db_close_database_shutdown_driver ( poDriver ); + poDriver = NULL; + } + + return NULL; + } + + // Attributes + if( pszQuery != NULL && !paQueryMatch[iNextId] ) { + iNextId++; + continue; + } + + // Spatial + if( m_poFilterGeom && !paSpatialMatch[iNextId] ) { + iNextId++; + continue; + } + + break; // Attributes & spatial filter match } OGRGeometry *poOGR = GetFeatureGeometry ( iNextId, &cat ); @@ -731,51 +729,51 @@ OGRFeature *OGRGRASSLayer::GetNextFeature() CPLDebug ( "GRASS", "bHaveAttributes = %d", bHaveAttributes ); if ( bHaveAttributes ) { - if ( !poDriver ) - { - StartDbDriver(); - } - if ( poDriver ) { - if ( !bCursorOpened ) - { - OpenSequentialCursor(); - } - if ( bCursorOpened ) - { - dbTable *table = db_get_cursor_table ( poCursor ); - if ( iCurrentCat < cat ) - { - while ( true ) { - int more; - if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "Cannot fetch attributes."); - break; - } - if ( !more ) break; - - dbColumn *column = db_get_table_column ( table, iCatField ); - dbValue *value = db_get_column_value ( column ); - iCurrentCat = db_get_value_int ( value ); - - if ( iCurrentCat >= cat ) break; - } - } - if ( cat == iCurrentCat ) - { - SetAttributes ( poFeature, table ); - } - else - { - CPLError( CE_Failure, CPLE_AppDefined, "Attributes not found."); - } - } - } + if ( !poDriver ) + { + StartDbDriver(); + } + if ( poDriver ) { + if ( !bCursorOpened ) + { + OpenSequentialCursor(); + } + if ( bCursorOpened ) + { + dbTable *table = db_get_cursor_table ( poCursor ); + if ( iCurrentCat < cat ) + { + while ( true ) { + int more; + if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) + { + CPLError( CE_Failure, CPLE_AppDefined, + "Cannot fetch attributes."); + break; + } + if ( !more ) break; + + dbColumn *column = db_get_table_column ( table, iCatField ); + dbValue *value = db_get_column_value ( column ); + iCurrentCat = db_get_value_int ( value ); + + if ( iCurrentCat >= cat ) break; + } + } + if ( cat == iCurrentCat ) + { + SetAttributes ( poFeature, table ); + } + else + { + CPLError( CE_Failure, CPLE_AppDefined, "Attributes not found."); + } + } + } } else if ( iLayer > 0 ) // Add category { - poFeature->SetField( 0, cat ); + poFeature->SetField( 0, cat ); } m_nFeaturesRead++; @@ -787,7 +785,8 @@ OGRFeature *OGRGRASSLayer::GetNextFeature() OGRFeature *OGRGRASSLayer::GetFeature( GIntBig nFeatureId ) { - CPLDebug ( "GRASS", "OGRGRASSLayer::GetFeature nFeatureId = %ld", nFeatureId ); + CPLDebug ( "GRASS", "OGRGRASSLayer::GetFeature nFeatureId = " CPL_FRMT_GIB, + nFeatureId ); int cat; OGRFeature *poFeature = NULL; @@ -801,57 +800,57 @@ OGRFeature *OGRGRASSLayer::GetFeature( GIntBig nFeatureId ) // Get attributes if ( bHaveAttributes && !poDriver ) { - StartDbDriver(); + StartDbDriver(); } if ( poDriver ) { - if ( bCursorOpened ) - { - db_close_cursor ( poCursor); - bCursorOpened = false; - } - CPLDebug ( "GRASS", "Open cursor for key = %d", cat ); - char buf[2000]; - sprintf ( buf, "SELECT * FROM %s WHERE %s = %d", - poLink->table, poLink->key, cat ); - db_set_string ( poDbString, buf); - if ( db_open_select_cursor ( poDriver, poDbString, - poCursor, DB_SEQUENTIAL) == DB_OK ) - { - iCurrentCat = cat; // Not important - bCursorOpened = true; - } - else - { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot open cursor."); - } - - if ( bCursorOpened ) - { - int more; - if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) - { - CPLError( CE_Failure, CPLE_AppDefined, "Cannot fetch attributes."); - } - else - { - if ( !more ) - { - CPLError( CE_Failure, CPLE_AppDefined, "Attributes not found."); - } - else - { - dbTable *table = db_get_cursor_table ( poCursor ); - SetAttributes ( poFeature, table ); - } - } - db_close_cursor ( poCursor); - bCursorOpened = false; - } + if ( bCursorOpened ) + { + db_close_cursor ( poCursor); + bCursorOpened = false; + } + CPLDebug ( "GRASS", "Open cursor for key = %d", cat ); + char buf[2000]; + snprintf ( buf, sizeof(buf), "SELECT * FROM %s WHERE %s = %d", + poLink->table, poLink->key, cat ); + db_set_string ( poDbString, buf); + if ( db_open_select_cursor ( poDriver, poDbString, + poCursor, DB_SEQUENTIAL) == DB_OK ) + { + iCurrentCat = cat; // Not important + bCursorOpened = true; + } + else + { + CPLError( CE_Failure, CPLE_AppDefined, "Cannot open cursor."); + } + + if ( bCursorOpened ) + { + int more; + if( db_fetch ( poCursor, DB_NEXT, &more) != DB_OK ) + { + CPLError( CE_Failure, CPLE_AppDefined, "Cannot fetch attributes."); + } + else + { + if ( !more ) + { + CPLError( CE_Failure, CPLE_AppDefined, "Attributes not found."); + } + else + { + dbTable *table = db_get_cursor_table ( poCursor ); + SetAttributes ( poFeature, table ); + } + } + db_close_cursor ( poCursor); + bCursorOpened = false; + } } else if ( iLayer > 0 ) // Add category { - poFeature->SetField( 0, cat ); + poFeature->SetField( 0, cat ); } m_nFeaturesRead++; @@ -876,9 +875,9 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry ( long nFeatureId, int *cat ) int bIs3D = Vect_is_3d(poMap); switch ( type ) { - case GV_POINT: + case GV_POINT: { - Vect_read_line ( poMap, poPoints, poCats, id); + Vect_read_line ( poMap, poPoints, poCats, id); if (bIs3D) poOGR = new OGRPoint( poPoints->x[0], poPoints->y[0], poPoints->z[0] ); else @@ -886,11 +885,11 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry ( long nFeatureId, int *cat ) } break; - case GV_LINE: - case GV_BOUNDARY: + case GV_LINE: + case GV_BOUNDARY: { - Vect_read_line ( poMap, poPoints, poCats, id); - OGRLineString *poOGRLine = new OGRLineString(); + Vect_read_line ( poMap, poPoints, poCats, id); + OGRLineString *poOGRLine = new OGRLineString(); if (bIs3D) poOGRLine->setPoints( poPoints->n_points, poPoints->x, poPoints->y, poPoints->z ); @@ -902,15 +901,13 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry ( long nFeatureId, int *cat ) } break; - case GV_AREA: + case GV_AREA: { - Vect_get_area_points ( poMap, id, poPoints ); + Vect_get_area_points ( poMap, id, poPoints ); - OGRPolygon *poOGRPoly; - poOGRPoly = new OGRPolygon(); + OGRPolygon *poOGRPoly = new OGRPolygon(); - OGRLinearRing *poRing; - poRing = new OGRLinearRing(); + OGRLinearRing *poRing = new OGRLinearRing(); if (bIs3D) poRing->setPoints( poPoints->n_points, poPoints->x, poPoints->y, poPoints->z ); @@ -918,15 +915,15 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry ( long nFeatureId, int *cat ) poRing->setPoints( poPoints->n_points, poPoints->x, poPoints->y ); - poOGRPoly->addRingDirectly( poRing ); + poOGRPoly->addRingDirectly( poRing ); - // Islands - int nisles = Vect_get_area_num_isles ( poMap, id ); - for ( int i = 0; i < nisles; i++ ) { - int isle = Vect_get_area_isle ( poMap, id, i ); - Vect_get_isle_points ( poMap, isle, poPoints ); + // Islands + int nisles = Vect_get_area_num_isles ( poMap, id ); + for ( int i = 0; i < nisles; i++ ) { + int isle = Vect_get_area_isle ( poMap, id, i ); + Vect_get_isle_points ( poMap, isle, poPoints ); - poRing = new OGRLinearRing(); + poRing = new OGRLinearRing(); if (bIs3D) poRing->setPoints( poPoints->n_points, poPoints->x, poPoints->y, poPoints->z ); @@ -934,17 +931,17 @@ OGRGeometry *OGRGRASSLayer::GetFeatureGeometry ( long nFeatureId, int *cat ) poRing->setPoints( poPoints->n_points, poPoints->x, poPoints->y ); - poOGRPoly->addRingDirectly( poRing ); - } + poOGRPoly->addRingDirectly( poRing ); + } - poOGR = poOGRPoly; + poOGR = poOGRPoly; } break; - default: // Should not happen + default: // Should not happen { - CPLError( CE_Failure, CPLE_AppDefined, "Unknown GRASS feature type."); - return NULL; + CPLError( CE_Failure, CPLE_AppDefined, "Unknown GRASS feature type."); + return NULL; } } @@ -960,50 +957,34 @@ bool OGRGRASSLayer::SetAttributes ( OGRFeature *poFeature, dbTable *table ) for ( int i = 0; i < nFields; i++) { - dbColumn *column = db_get_table_column ( table, i ); - dbValue *value = db_get_column_value ( column ); - - int ctype = db_sqltype_to_Ctype ( db_get_column_sqltype(column) ); - - if ( !db_test_value_isnull(value) ) - { - switch ( ctype ) { - case DB_C_TYPE_INT: - poFeature->SetField( i, db_get_value_int ( value )); - break; - case DB_C_TYPE_DOUBLE: - poFeature->SetField( i, db_get_value_double ( value )); - break; - case DB_C_TYPE_STRING: - poFeature->SetField( i, db_get_value_string ( value )); - break; - case DB_C_TYPE_DATETIME: - db_convert_column_value_to_string ( column, poDbString ); - poFeature->SetField( i, db_get_string ( poDbString )); - break; - } - } - - db_convert_column_value_to_string ( column, poDbString ); - //CPLDebug ( "GRASS", "val = %s", db_get_string ( poDbString )); - } - return true; -} + dbColumn *column = db_get_table_column ( table, i ); + dbValue *value = db_get_column_value ( column ); -/************************************************************************/ -/* ISetFeature() */ -/************************************************************************/ -OGRErr OGRGRASSLayer::ISetFeature( OGRFeature *poFeature ) -{ - return OGRERR_FAILURE; -} + int ctype = db_sqltype_to_Ctype ( db_get_column_sqltype(column) ); -/************************************************************************/ -/* ICreateFeature() */ -/************************************************************************/ -OGRErr OGRGRASSLayer::ICreateFeature( OGRFeature *poFeature ) -{ - return OGRERR_FAILURE; + if ( !db_test_value_isnull(value) ) + { + switch ( ctype ) { + case DB_C_TYPE_INT: + poFeature->SetField( i, db_get_value_int ( value )); + break; + case DB_C_TYPE_DOUBLE: + poFeature->SetField( i, db_get_value_double ( value )); + break; + case DB_C_TYPE_STRING: + poFeature->SetField( i, db_get_value_string ( value )); + break; + case DB_C_TYPE_DATETIME: + db_convert_column_value_to_string ( column, poDbString ); + poFeature->SetField( i, db_get_string ( poDbString )); + break; + } + } + + db_convert_column_value_to_string ( column, poDbString ); + // CPLDebug ( "GRASS", "val = %s", db_get_string ( poDbString )); + } + return true; } /************************************************************************/ @@ -1031,7 +1012,7 @@ GIntBig OGRGRASSLayer::GetFeatureCount( int bForce ) /* */ /* Returns OGRERR_NONE/OGRRERR_FAILURE. */ /************************************************************************/ -OGRErr OGRGRASSLayer::GetExtent (OGREnvelope *psExtent, int bForce) +OGRErr OGRGRASSLayer::GetExtent (OGREnvelope *psExtent, int /*bForce*/) { #if GRASS_VERSION_MAJOR >= 7 struct bound_box box; @@ -1074,17 +1055,6 @@ int OGRGRASSLayer::TestCapability( const char * pszCap ) } /************************************************************************/ -/* CreateField() */ -/************************************************************************/ -OGRErr OGRGRASSLayer::CreateField( OGRFieldDefn *poField, int bApproxOK ) -{ - CPLError( CE_Failure, CPLE_NotSupported, - "Can't create fields on a GRASS layer.\n"); - - return OGRERR_FAILURE; -} - -/************************************************************************/ /* GetSpatialRef() */ /************************************************************************/ OGRSpatialReference *OGRGRASSLayer::GetSpatialRef() -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/gdal-grass.git _______________________________________________ Pkg-grass-devel mailing list Pkg-grass-devel@lists.alioth.debian.org http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-grass-devel