Fix for #413 - handle encodings correctly in SPIT

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6214 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
wonder 2006-12-08 19:24:11 +00:00
parent b24e533903
commit e49886ec91
3 changed files with 45 additions and 12 deletions

View File

@ -28,6 +28,7 @@
#include <QProgressDialog>
#include <QString>
#include <QLabel>
#include <QTextCodec>
#include "qgsdbfbase.h"
#include "cpl_error.h"
@ -42,7 +43,7 @@
#endif
QgsShapeFile::QgsShapeFile(QString name){
QgsShapeFile::QgsShapeFile(QString name, QString encoding){
filename = name;
features = 0;
OGRRegisterAll();
@ -58,6 +59,10 @@ QgsShapeFile::QgsShapeFile(QString name){
// init the geometry types
geometries << "NULL" << "POINT" << "LINESTRING" << "POLYGON" << "MULTPOINT"
<< "MULTILINESTRING" << "MULTIPOLYGON" << "GEOMETRYCOLLECTION";
codec = QTextCodec::codecForName(encoding.toLocal8Bit().data());
if (!codec)
codec = QTextCodec::codecForLocale();
}
QgsShapeFile::~QgsShapeFile(){
@ -207,7 +212,10 @@ QString QgsShapeFile::getFeatureClass(){
dbf.close();
int numFields = feat->GetFieldCount();
for(int n=0; n<numFields; n++)
column_names.push_back(feat->GetFieldDefnRef(n)->GetNameRef());
{
QString s = codec->toUnicode(feat->GetFieldDefnRef(n)->GetNameRef());
column_names.push_back(s);
}
}else valid = false;
delete feat;
@ -342,21 +350,22 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
query += quotes;
// escape the string value
QString val = feat->GetFieldAsString(n);
char * esc_str = new char[val.length()*2+1];
PQescapeString(esc_str, (const char *)val.lower(), val.length());
QString val = codec->toUnicode(feat->GetFieldAsString(n));
val.replace("'", "''");
//char * esc_str = new char[val.length()*2+1];
//PQescapeString(esc_str, (const char *)val.lower().utf8(), val.length());
// add escaped value to the query
query += esc_str;
query += val; //esc_str;
query += QString(quotes + ", ");
delete[] esc_str;
//delete[] esc_str;
}
query += QString("GeometryFromText(\'")+geometry+QString("\', ")+srid+QString("))");
// std::cerr << query << std::endl;
if(result)
res = PQexec(conn, (const char *)query);
res = PQexec(conn, (const char *)query.utf8());
if(PQresultStatus(res)!=PGRES_COMMAND_OK){
// flag error and send query and error message to stdout on debug
result = false;

View File

@ -26,6 +26,7 @@
#include <ogrsf_frmts.h>
class QProgressDialog;
class QTextCodec;
class OGRLayer;
class OGRDataSource;
@ -40,7 +41,7 @@ class QgsShapeFile : public QObject
Q_OBJECT
public:
QgsShapeFile(QString filename);
QgsShapeFile(QString filename, QString encoding = QString());
~QgsShapeFile();
int getFeatureCount();
QString getFeatureClass();
@ -69,6 +70,7 @@ class QgsShapeFile : public QObject
QString filename;
QString geom_type;
QStringList geometries;
QTextCodec* codec;
public slots:
void cancelImport();

View File

@ -25,9 +25,12 @@
#include <QSettings>
#include <QPixmap>
#include <QHeaderView>
#include <QTextCodec>
#include <iostream>
#include "qgsencodingfiledialog.h"
#include "qgspgutil.h"
#include "qgsspit.h"
#include "qgsconnectiondialog.h"
@ -158,15 +161,23 @@ void QgsSpit::addFile()
bool is_error = false;
QSettings settings("QuantumGIS", "qgis");
QStringList files = QFileDialog::getOpenFileNames(this,
QgsEncodingFileDialog dlg(this,
tr("Add Shapefiles"),
settings.readEntry( "/Plugin-Spit/last_directory" ),
tr("Shapefiles (*.shp);;All files (*.*)") );
tr("Shapefiles (*.shp);;All files (*.*)"),
settings.readEntry( "/Plugin-Spit/last_encoding" ) );
dlg.setMode(QFileDialog::ExistingFiles);
if (dlg.exec() != QDialog::Accepted)
return;
QStringList files = dlg.selectedFiles();
if ( files.size() > 0 )
{
// Save the directory for future use
QFileInfo fi( files[ 0 ] );
settings.writeEntry( "/Plugin-Spit/last_directory", fi.dirPath( true ) );
settings.writeEntry( "/Plugin-Spit/last_encoding", dlg.encoding());
}
// Process the files
for ( QStringList::Iterator it = files.begin(); it != files.end(); ++it )
@ -197,7 +208,7 @@ void QgsSpit::addFile()
if ( !is_error )
{
QgsShapeFile * file = new QgsShapeFile( name );
QgsShapeFile * file = new QgsShapeFile( name, dlg.encoding() );
if ( file->is_valid() )
{
/* XXX getFeatureClass actually does a whole bunch
@ -426,6 +437,17 @@ PGconn* QgsSpit::checkConnection()
QMessageBox::warning( this, tr("Import Shapefiles"), tr("Connection failed - Check settings and try again") );
result = false;
}
int errcode = PQsetClientEncoding(pd, "UNICODE");
#ifdef QGISDEBUG
if(errcode==0)
qWarning("encoding successfully set");
else if(errcode==-1)
qWarning("error in setting encoding");
else
qWarning("undefined return value from encoding setting");
#endif
}
if (result )