Skip to content
Permalink
Browse files

add support for UTF8 in GDAL/OGR 1.8 (fixes #2551)

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15148 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Feb 9, 2011
1 parent 63f69c5 commit c7af95b7b7e828e1e360ee79f250c3244f3b3ee9
@@ -19,6 +19,11 @@
#include "cpl_string.h"
#include <QProgressDialog>

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
#else
#define TO8(x) (x).toLocal8Bit().constData()
#endif

QgsNineCellFilter::QgsNineCellFilter( const QString& inputFile, const QString& outputFile, const QString& outputFormat ): \
mInputFile( inputFile ), mOutputFile( outputFile ), mOutputFormat( outputFormat ), mCellSizeX( -1 ), mCellSizeY( -1 ), mInputNodataValue( -1 ), mOutputNodataValue( -1 )
@@ -191,7 +196,7 @@ int QgsNineCellFilter::processRaster( QProgressDialog* p )

GDALDatasetH QgsNineCellFilter::openInputFile( int& nCellsX, int& nCellsY )
{
GDALDatasetH inputDataset = GDALOpen( mInputFile.toLocal8Bit().data(), GA_ReadOnly );
GDALDatasetH inputDataset = GDALOpen( TO8( mInputFile ), GA_ReadOnly );
if ( inputDataset != NULL )
{
nCellsX = GDALGetRasterXSize( inputDataset );
@@ -24,6 +24,12 @@

#include "gdalwarper.h"

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
#else
#define TO8(x) (x).toLocal8Bit().constData()
#endif

QgsRasterCalculator::QgsRasterCalculator( const QString& formulaString, const QString& outputFile, const QString& outputFormat,
const QgsRectangle& outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry>& rasterEntries ): mFormulaString( formulaString ), mOutputFile( outputFile ), mOutputFormat( outputFormat ),
mOutputRectangle( outputExtent ), mNumOutputColumns( nOutputColumns ), mNumOutputRows( nOutputRows ), mRasterEntries( rasterEntries )
@@ -59,7 +65,7 @@ int QgsRasterCalculator::processCalculation( QProgressDialog* p )
{
return 2;
}
GDALDatasetH inputDataset = GDALOpen( it->raster->source().toLocal8Bit().data(), GA_ReadOnly );
GDALDatasetH inputDataset = GDALOpen( TO8( it->raster->source() ), GA_ReadOnly );
if ( inputDataset == NULL )
{
return 2;
@@ -61,8 +61,22 @@ class ANALYSIS_EXPORT QgsRasterCalculator
GDALDatasetH openOutputFile( GDALDriverH outputDriver );

/**Reads raster pixels from a dataset/band
@param targetGeotransformation transformation parameters of the requested raster array (not necessarily the same as the transform of the source dataset */
void readRasterPart( double* targetGeotransform, int xOffset, int yOffset, int nCols, int nRows, double* sourceTransform, GDALRasterBandH sourceBand, float* rasterBuffer );
@param targetGeotransform transformation parameters of the requested raster array
(not necessarily the same as the transform of the source dataset)
@param xOffset x offset
@param yOffset y offset
@param nCols number of columns
@param nRows number of rows
@param sourceTransform source transformation
@param sourceBand source band
@param rasterBuffer raster buffer
*/
void readRasterPart( double* targetGeotransform,
int xOffset, int yOffset,
int nCols, int nRows,
double* sourceTransform,
GDALRasterBandH sourceBand,
float* rasterBuffer );

/**Compares two geotransformations (six parameter double arrays*/
bool transformationsEqual( double* t1, double* t2 ) const;
@@ -23,6 +23,12 @@
#include "cpl_string.h"
#include <QProgressDialog>

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
#else
#define TO8(x) (x).toLocal8Bit().constData()
#endif

QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand )
: mRasterFilePath( rasterFile )
, mRasterBand( rasterBand )
@@ -60,7 +66,7 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )

//open the raster layer and the raster band
GDALAllRegister();
GDALDatasetH inputDataset = GDALOpen( mRasterFilePath.toLocal8Bit().data(), GA_ReadOnly );
GDALDatasetH inputDataset = GDALOpen( TO8( mRasterFilePath ), GA_ReadOnly );
if ( inputDataset == NULL )
{
return 3;
@@ -29,6 +29,11 @@
#include <ogr_api.h>
#include <cpl_error.h>

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8F(x) (x).toUtf8().constData()
#else
#define TO8F(x) QFile::encodeName( x ).constData()
#endif

QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString& connType, const QString& connName, Qt::WFlags fl )
: QDialog( parent, fl ),
@@ -83,7 +88,7 @@ void QgsNewOgrConnection::testConnection()
OGRDataSourceH poDS;
OGRSFDriverH pahDriver;
CPLErrorReset();
poDS = OGROpen( QFile::encodeName( uri ).constData(), false, &pahDriver );
poDS = OGROpen( TO8F( uri ), false, &pahDriver );
if ( poDS == NULL )
{
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
@@ -74,6 +74,11 @@ email : tim at linfiniti.com
// doubles can take for the current system. (Yes, 20 was arbitrary.)
#define TINY_VALUE std::numeric_limits<double>::epsilon() * 20

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8F(x) (x).toUtf8().constData()
#else
#define TO8F(x) QFile::encodeName( x ).constData()
#endif

QgsRasterLayer::QgsRasterLayer(
QString const & path,
@@ -452,7 +457,7 @@ bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
CPLErrorReset();

//open the file using gdal making sure we have handled locale properly
myDataset = GDALOpen( QFile::encodeName( theFileNameQString ).constData(), GA_ReadOnly );
myDataset = GDALOpen( TO8F( theFileNameQString ), GA_ReadOnly );
if ( myDataset == NULL )
{
if ( CPLGetLastErrorNo() != CPLE_OpenFailed )
@@ -1002,12 +1007,12 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami

//close the gdal dataset and reopen it in read / write mode
GDALClose( mGdalDataset );
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_Update );
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_Update );

// if the dataset couldn't be opened in read / write mode, tell the user
if ( !mGdalBaseDataset )
{
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_ReadOnly );
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
mGdalDataset = mGdalBaseDataset;
return "ERROR_WRITE_FORMAT";
@@ -1077,7 +1082,7 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
//something bad happenend
//QString myString = QString (CPLGetLastError());
GDALClose( mGdalBaseDataset );
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_ReadOnly );
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
mGdalDataset = mGdalBaseDataset;

@@ -1104,7 +1109,7 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
{
//close the gdal dataset and reopen it in read only mode
GDALClose( mGdalBaseDataset );
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
mGdalBaseDataset = GDALOpen( TO8F( mDataSource ), GA_ReadOnly );
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
mGdalDataset = mGdalBaseDataset;
}
@@ -5299,7 +5304,7 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
mGdalDataset = NULL;

//open the dataset making sure we handle char encoding of locale properly
mGdalBaseDataset = GDALOpen( QFile::encodeName( theFilename ).constData(), GA_ReadOnly );
mGdalBaseDataset = GDALOpen( TO8F( theFilename ), GA_ReadOnly );

if ( mGdalBaseDataset == NULL )
{
@@ -76,6 +76,12 @@
#include "qgsidwinterpolator.h"
#include "qgstininterpolator.h"

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
#else
#define TO8(x) (x).toLocal8Bit().constData()
#endif

QgsSLDParser::QgsSLDParser( QDomDocument* doc ): QgsConfigParser(), mXMLDoc( doc )
{
mSLDNamespace = "http://www.opengis.net/sld";
@@ -1279,15 +1285,6 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty
GDALRasterBandH hBand;
GDALDatasetH hSrcDS;

QByteArray pszSrcData = rasterLayer->source().toLocal8Bit();
QByteArray pszDstData = tmpFileName.toLocal8Bit();
QByteArray pszElevData = propertyName.toLocal8Bit();

const char *pszSrcFilename = pszSrcData.data();
const char *pszDstFilename = pszDstData.data();
const char *pszElevAttrib = pszElevData.data();
const char *pszFormat = "ESRI Shapefile";

int numberOfLevels = 0;
double currentLevel = 0.0;

@@ -1315,7 +1312,7 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty

int b3D = FALSE, bNoDataSet = FALSE, bIgnoreNoData = FALSE;

hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly );
hSrcDS = GDALOpen( TO8( rasterLayer->source() ), GA_ReadOnly );
if ( hSrcDS == NULL )
exit( 2 );

@@ -1344,19 +1341,18 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty
/* Create the outputfile. */
/* -------------------------------------------------------------------- */
OGRDataSourceH hDS;
OGRSFDriverH hDriver = OGRGetDriverByName( pszFormat );
OGRSFDriverH hDriver = OGRGetDriverByName( "ESRI Shapefile" );
OGRFieldDefnH hFld;
OGRLayerH hLayer;
int nElevField = -1;

if ( hDriver == NULL )
{
fprintf( FCGI_stderr, "Unable to find format driver named %s.\n",
pszFormat );
fprintf( FCGI_stderr, "Unable to find format driver named 'ESRI Shapefile'.\n" );
exit( 10 );
}

hDS = OGR_Dr_CreateDataSource( hDriver, pszDstFilename, NULL );
hDS = OGR_Dr_CreateDataSource( hDriver, TO8( tmpFileName ), NULL );
if ( hDS == NULL )
exit( 1 );

@@ -1371,9 +1367,9 @@ QgsVectorLayer* QgsSLDParser::contourLayerFromRaster( const QDomElement& userSty
OGR_L_CreateField( hLayer, hFld, FALSE );
OGR_Fld_Destroy( hFld );

if ( pszElevAttrib )
if ( !propertyName.isEmpty() )
{
hFld = OGR_Fld_Create( pszElevAttrib, OFTReal );
hFld = OGR_Fld_Create( TO8( propertyName ), OFTReal );
OGR_Fld_SetWidth( hFld, 12 );
OGR_Fld_SetPrecision( hFld, 3 );
OGR_L_CreateField( hLayer, hFld, FALSE );
@@ -31,6 +31,12 @@
#include "qgsimagewarper.h"
#include "qgsgeoreftransform.h"

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8F(x) (x).toUtf8().constData()
#else
#define TO8F(x) QFile::encodeName( x ).constData()
#endif

bool QgsImageWarper::mWarpCanceled = false;

QgsImageWarper::QgsImageWarper( QWidget *theParent )
@@ -44,7 +50,7 @@ bool QgsImageWarper::openSrcDSAndGetWarpOpt( const QString &input, const Resampl
{
// Open input file
GDALAllRegister();
hSrcDS = GDALOpen( QFile::encodeName( input ).constData(), GA_ReadOnly );
hSrcDS = GDALOpen( TO8F( input ), GA_ReadOnly );
if ( hSrcDS == NULL ) return false;

// Setup warp options.
@@ -32,6 +32,12 @@

#include <ogr_api.h>

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
#else
#define TO8(x) (x).toLocal8Bit().constData()
#endif

Dialog::Dialog( QWidget* parent, Qt::WFlags fl )
: QDialog( parent, fl )
{
@@ -158,7 +164,7 @@ void Dialog::populateLayers( QString const& url )
{
comboSrcLayer->clear();

OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
OGRDataSourceH ds = OGROpen( TO8( url ), 0, 0 );
if ( 0 != ds )
{
QString lyrName;
@@ -199,7 +205,7 @@ bool Dialog::testConnection( QString const& url )
{
bool success = false;

OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
OGRDataSourceH ds = OGROpen( TO8( url ), 0, 0 );
if ( 0 != ds )
{
success = true;
@@ -23,6 +23,14 @@
#include <ogr_api.h>
#include <cpl_error.h>

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
#define FROM8(x) QString::fromUtf8(x)
#else
#define TO8(x) (x).toLocal8Bit().constData()
#define FROM8(x) QString::fromLocal8Bit(x)
#endif

Translator::Translator()
: mDstUpdate( false ), mDstLayerOverwrite( true )
{
@@ -125,7 +133,7 @@ bool Translator::translate()
// TODO: Support translation of all layers from input data source
//for (int i = 0; i < OGR_DS_GetLayerCount(); ++i)

OGRLayerH srcLayer = OGR_DS_GetLayerByName( srcDs, mSrcLayer.toAscii().constData() );
OGRLayerH srcLayer = OGR_DS_GetLayerByName( srcDs, TO8( mSrcLayer ) );
if ( 0 == srcLayer )
{
QgsDebugMsg( "Can not find layer: " + mSrcLayer );
@@ -201,13 +209,12 @@ bool Translator::translateLayer( OGRDataSourceH srcDs, OGRLayerH srcLayer, OGRDa
// TODO: Implement SRS transformation
OGRSpatialReferenceH dstLayerSrs = OGR_L_GetSpatialRef( srcLayer );

dstLayer = OGR_DS_CreateLayer( dstDs, mDstLayer.toAscii().constData(),
dstLayerSrs, geomType, 0 );
dstLayer = OGR_DS_CreateLayer( dstDs, TO8( mDstLayer ), dstLayerSrs, geomType, 0 );
}

if ( 0 == dstLayer )
{
qWarning() << QString("Layer %1 not found and CreateLayer failed [OGR: %2]\n" ).arg( mDstLayer ).arg( CPLGetLastErrorMsg() );
qWarning() << QString( "Layer %1 not found and CreateLayer failed [OGR: %2]\n" ).arg( mDstLayer ).arg( CPLGetLastErrorMsg() );
return false;
}

@@ -379,7 +386,8 @@ OGRLayerH Translator::findLayer( OGRDataSourceH ds, QString const& name, int& in

OGRDataSourceH Translator::openDataSource( QString const& url, bool readOnly )
{
OGRDataSourceH ds = OGROpen( url.toAscii().constData(), !readOnly, 0 );
OGRDataSourceH ds = OGROpen( TO8( url ), !readOnly, 0 );

if ( 0 == ds )
{
QgsDebugMsg( "Failed to open: " + url );
@@ -410,7 +418,7 @@ OGRDataSourceH Translator::openDataTarget( QString const& url, bool update )
// Create the output data source
//
// TODO: Add support for creation options
ds = OGR_Dr_CreateDataSource( drv, url.toAscii().constData(), 0 );
ds = OGR_Dr_CreateDataSource( drv, TO8( url ), 0 );
if ( 0 == ds )
{
QgsDebugMsg( "Failed to open: " + url );
@@ -26,6 +26,12 @@

#include "qgsvectorlayer.h"

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
#else
#define TO8(x) (x).toLocal8Bit().constData()
#endif

QgsOracleSelectGeoraster::QgsOracleSelectGeoraster( QWidget* parent,
QgisInterface* iface,
Qt::WFlags fl ) : QDialog( parent, fl ), mIface( iface )
@@ -187,7 +193,7 @@ void QgsOracleSelectGeoraster::showSelection( const QString & line )
* Try to open georaster dataset
*/

hDS = GDALOpenShared( identification.toAscii(), eAccess );
hDS = GDALOpenShared( TO8( identification ), eAccess );

buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
if ( hDS == NULL )

0 comments on commit c7af95b

Please sign in to comment.
You can’t perform that action at this time.