diff --git a/plugins/spit/qgsshapefile.cpp b/plugins/spit/qgsshapefile.cpp new file mode 100644 index 00000000000..aec365f91ba --- /dev/null +++ b/plugins/spit/qgsshapefile.cpp @@ -0,0 +1,227 @@ +/*************************************************************************** + qgsshapefile.cpp - description + ------------------- + begin : Fri Dec 19 2003 + copyright : (C) 2003 by Denis Antipov + email : + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +/* $Id$ */ + +#include +#include +#include +#include +#include +#include +#include +extern "C" +{ + #include +} + +#include "qgsdbfbase.h" +#include "cpl_error.h" +#include "qgsshapefile.h" + + +QgsShapeFile::QgsShapeFile(QString name){ + filename = name; + features = 0; + OGRRegisterAll(); + ogrDataSource = OGRSFDriverRegistrar::Open((const char *) filename); + if (ogrDataSource != NULL){ + valid = true; + ogrLayer = ogrDataSource->GetLayer(0); + features = ogrLayer->GetFeatureCount(); + } + else + valid = false; + setDefaultTable(); +} + +QgsShapeFile::~QgsShapeFile(){ + delete ogrLayer; + delete ogrDataSource; + delete filename; + delete geom_type; +} + +int QgsShapeFile::getFeatureCount(){ + return features; +} + +QString QgsShapeFile::getFeatureClass(){ + OGRFeature *feat = ogrLayer->GetNextFeature(); + if(feat){ + OGRGeometry *geom = feat->GetGeometryRef(); + if(geom){ + geom_type = QString(geom->getGeometryName()); + + QString file(filename); + file.replace(file.length()-3, 3, "dbf"); + // open the dbf file + std::ifstream dbf((const char*)file, std::ios::in | std::ios::binary); + // read header + DbaseHeader dbh; + dbf.read((char *)&dbh, sizeof(dbh)); + + Fda fda; + QString str_type = "varchar("; + for(int field_count = 0, bytes_read = sizeof(dbh); bytes_read < dbh.size_hdr-1; field_count++, bytes_read += sizeof(fda)){ + dbf.read((char *)&fda, sizeof(fda)); + switch(fda.field_type){ + case 'N': if((int)fda.field_decimal>0) + column_types.push_back("float"); + else + column_types.push_back("int"); + break; + case 'F': column_types.push_back("float"); + break; + case 'D': column_types.push_back("date"); + break; + case 'C': + str_type= QString("varchar(%1)").arg(fda.field_length); + column_types.push_back(str_type); + break; + case 'L': column_types.push_back("boolean"); + break; + default: + column_types.push_back("varchar(256)"); + break; + } + } + dbf.close(); + int numFields = feat->GetFieldCount(); + for(int n=0; nGetFieldDefnRef(n)->GetNameRef()); + + }else valid = false; + delete feat; + }else valid = false; + + ogrLayer->ResetReading(); + return valid?geom_type:NULL; +} + +bool QgsShapeFile::is_valid(){ + return valid; +} + +QString QgsShapeFile::getName(){ + return filename; +} + +QString QgsShapeFile::getTable(){ + return table_name; +} + +void QgsShapeFile::setTable(QString new_table){ + table_name = new_table; +} + +void QgsShapeFile::setDefaultTable(){ + QString name(filename); + name = name.section('/', -1); + table_name = name.section('.', 0, 0); +} + +bool QgsShapeFile::insertLayer(QString dbname, QString geom_col, QString srid, PGconn * conn, QProgressDialog * pro, bool &fin){ + connect(pro, SIGNAL(cancelled()), this, SLOT(cancelImport())); + import_cancelled = false; + bool result = true; + QString message; + + QString query = "CREATE TABLE "+table_name+"(gid int4, "; + for(int n=0; nGetNextFeature(); + if(feat){ + OGRGeometry *geom = feat->GetGeometryRef(); + if(geom){ + query = "INSERT INTO "+table_name+QString(" VALUES( %1, ").arg(m); + + int num = geom->WkbSize(); + char * geo_temp = new char[num*3]; + geom->exportToWkt(&geo_temp); + QString geometry(geo_temp); + + QString quotes; + for(int n=0; nGetFieldAsString(n); + val.replace("\'","\\\'"); + val.replace("\'","\\\'"); + // add escaped value to the query + query += val; + query += QString(quotes + ", "); + + } + query += QString("GeometryFromText(\'")+geometry+QString("\', ")+srid+QString("))"); + + PQclear(res); + if(result) res = PQexec(conn, (const char *)query); + message = PQresultErrorMessage(res); + if(message != ""){ + // flag error and send query and error message to stdout on debug + result = false; + qWarning(query); + qWarning(PQresultErrorMessage(res)); + } + pro->setProgress(pro->progress()+1); + qApp->processEvents(); + delete[] geo_temp; + } + delete feat; + } + } + ogrLayer->ResetReading(); + PQclear(res); + return result; +} + +void QgsShapeFile::cancelImport(){ + import_cancelled = true; +} diff --git a/plugins/spit/qgsshapefile.h b/plugins/spit/qgsshapefile.h new file mode 100644 index 00000000000..cf23755256e --- /dev/null +++ b/plugins/spit/qgsshapefile.h @@ -0,0 +1,70 @@ +/*************************************************************************** + qgsshapefile.h - description + ------------------- + begin : Fri Dec 19 2003 + copyright : (C) 2003 by Denis Antipov + email : + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +/* $Id$ */ + +#ifndef QGSSHAPEFILE_H +#define QGSSHAPEFILE_H + +#include +#include +#include +#include +#include +extern "C" +{ + #include +} + + +class OGRLayer; +class OGRDataSource; + +class QgsShapeFile : public QObject +{ + Q_OBJECT + public: + + QgsShapeFile(QString filename); + ~QgsShapeFile(); + int getFeatureCount(); + QString getFeatureClass(); + bool insertLayer(QString dbname, QString geom_col, QString srid, struct pg_conn* conn, QProgressDialog * pro, bool &fin); + + bool is_valid(); + QString getName(); + QString getTable(); + void setTable(QString new_table); + void setDefaultTable(); + std::vector column_names; + std::vector column_types; + + + private: + QString table_name; + OGRDataSource *ogrDataSource; + OGRLayer * ogrLayer; + bool import_cancelled; + bool valid; + int features; + QString filename; + QString geom_type; + + public slots: + void cancelImport(); +}; + +#endif