Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix for #413 - handle encodings correctly in SPIT
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6214 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Dec 8, 2006
1 parent b24e533 commit e49886e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/plugins/spit/qgsshapefile.cpp
Expand Up @@ -28,6 +28,7 @@
#include <QProgressDialog>
#include <QString>
#include <QLabel>
#include <QTextCodec>

#include "qgsdbfbase.h"
#include "cpl_error.h"
Expand All @@ -42,7 +43,7 @@
#endif


QgsShapeFile::QgsShapeFile(QString name){
QgsShapeFile::QgsShapeFile(QString name, QString encoding){
filename = name;
features = 0;
OGRRegisterAll();
Expand All @@ -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(){
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/spit/qgsshapefile.h
Expand Up @@ -26,6 +26,7 @@
#include <ogrsf_frmts.h>

class QProgressDialog;
class QTextCodec;

class OGRLayer;
class OGRDataSource;
Expand All @@ -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();
Expand Down Expand Up @@ -69,6 +70,7 @@ class QgsShapeFile : public QObject
QString filename;
QString geom_type;
QStringList geometries;
QTextCodec* codec;

public slots:
void cancelImport();
Expand Down
28 changes: 25 additions & 3 deletions src/plugins/spit/qgsspit.cpp
Expand Up @@ -25,8 +25,11 @@
#include <QSettings>
#include <QPixmap>
#include <QHeaderView>
#include <QTextCodec>

#include <iostream>

#include "qgsencodingfiledialog.h"

#include "qgspgutil.h"
#include "qgsspit.h"
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit e49886e

Please sign in to comment.