initial check in of spit source

git-svn-id: http://svn.osgeo.org/qgis/trunk@651 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-01-31 03:33:00 +00:00
parent 6f82ccdf6e
commit 2bb0e3f303
13 changed files with 1736 additions and 2 deletions

View File

@ -20,13 +20,16 @@ lib_LTLIBRARIES = libspitplugin.la
$(UIC) -o $@ -impl $*.h $<
libspitplugin_la_SOURCES = qgsspit.cpp \
qgsspitplugin.cpp \
qgsspitplugin.h \
qgsconnectiondialog.cpp \
qgsshapefile.cpp \
qgsmessageviewer.cpp \
$(spit_UI)\
$(spit_MOC)
$(spit_UI)\
$(spit_MOC)
spit_MOC = qgsconnectiondialogbase.moc.cpp \
qgsspitplugin.moc.cpp \
qgsshapefile.moc.cpp \
qgsmessageviewerbase.moc.cpp \
qgsspitbase.moc.cpp

View File

@ -0,0 +1,91 @@
/***************************************************************************
qgsconnectiondialog.cpp - description
-------------------
begin : Thu Dec 10 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. *
* *
***************************************************************************/
#include <iostream>
#include <qsettings.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qmessagebox.h>
extern "C"
{
#include <libpq-fe.h>
}
#include "qgsconnectiondialog.h"
#include "qgsmessageviewer.h"
QgsConnectionDialog::QgsConnectionDialog (QWidget* parent, QString connName, bool modal, WFlags fl)
: QgsConnectionDialogBase(parent,(const char *)connName,modal,fl)
{
if (!connName.isEmpty()) {
QSettings settings;
QString key = "/Qgis/connections/" + connName;
txtHost->setText(settings.readEntry(key + "/host"));
txtDatabase->setText(settings.readEntry(key + "/database"));
txtUsername->setText(settings.readEntry(key + "/username"));
if(settings.readEntry(key + "/save") == "true"){
txtPassword->setText(settings.readEntry(key + "/password"));
chkStorePassword->setChecked(true);
}
txtName->setText(connName);
}
setFixedSize(QSize(411, 230));
}
QgsConnectionDialog::~QgsConnectionDialog()
{
}
void QgsConnectionDialog::testConnection()
{
QString connInfo = "host=" + txtHost->text() + " dbname=" + txtDatabase->text() +
" user=" + txtUsername->text() + " password=" + txtPassword->text();
PGconn *pd = PQconnectdb((const char *) connInfo);
if (PQstatus(pd) == CONNECTION_OK) {
// Database successfully opened; we can now issue SQL commands.
QMessageBox::information(this, "Test connection", "Connection to " + txtDatabase->text() + " was successfull");
} else {
QMessageBox::information(this, "Test connection", "Connection failed - Check settings and try again ");
}
PQfinish(pd);
}
void QgsConnectionDialog::saveConnection()
{
QSettings settings;
QString baseKey = "/Qgis/connections/";
baseKey += txtName->text();
settings.writeEntry(baseKey + "/host", txtHost->text());
settings.writeEntry(baseKey + "/database", txtDatabase->text());
settings.writeEntry(baseKey + "/username", txtUsername->text());
settings.writeEntry(baseKey + "/password", txtPassword->text());
if(chkStorePassword->isChecked()) settings.writeEntry(baseKey + "/save", "true");
else settings.writeEntry(baseKey + "/save", "false");
accept();
}
void QgsConnectionDialog::helpInfo(){
QString message = "General Interface Help:\n\n";
QgsMessageViewer * e = new QgsMessageViewer(this, "HelpMessage");
e->setMessage(message);
e->exec();
}

View File

@ -0,0 +1,34 @@
/***************************************************************************
qgsconnectiondialog.h - description
-------------------
begin : Thu Dec 10 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. *
* *
***************************************************************************/
#ifndef QGSCONNECTIONDIALOG_H
#define QGSCONNECTIONDIALOG_H
#include "qgsconnectiondialogbase.h"
class QgsConnectionDialog : public QgsConnectionDialogBase
{
public:
QgsConnectionDialog(QWidget* parent = 0, QString connName=QString::null, bool modal = true, WFlags fl = 0);
~QgsConnectionDialog();
void testConnection();
void saveConnection();
void helpInfo();
};
#endif

View File

@ -0,0 +1,302 @@
<!DOCTYPE UI><UI version="3.1" stdsetdef="1">
<class>QgsConnectionDialogBase</class>
<widget class="QDialog">
<property name="name">
<cstring>QgsConnectionDialogBase</cstring>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>411</width>
<height>230</height>
</rect>
</property>
<property name="caption">
<string>Connection Properties</string>
</property>
<widget class="QGroupBox">
<property name="name">
<cstring>connInfoBox</cstring>
</property>
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>284</width>
<height>189</height>
</rect>
</property>
<property name="font">
<font>
<family>Helvetica [Urw]</family>
</font>
</property>
<property name="title">
<string>Connection Information</string>
</property>
<grid>
<property name="name">
<cstring>unnamed</cstring>
</property>
<property name="margin">
<number>11</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<widget class="QLineEdit" row="1" column="1" rowspan="1" colspan="2">
<property name="name">
<cstring>txtHost</cstring>
</property>
</widget>
<widget class="QLabel" row="1" column="0">
<property name="name">
<cstring>lblHost</cstring>
</property>
<property name="font">
<font>
<family>Sans</family>
</font>
</property>
<property name="text">
<string>Host</string>
</property>
</widget>
<widget class="QLineEdit" row="2" column="1" rowspan="1" colspan="2">
<property name="name">
<cstring>txtDatabase</cstring>
</property>
</widget>
<widget class="QLabel" row="2" column="0">
<property name="name">
<cstring>lblDatabase</cstring>
</property>
<property name="font">
<font>
<family>Sans</family>
</font>
</property>
<property name="text">
<string>Database</string>
</property>
</widget>
<widget class="QLineEdit" row="3" column="1">
<property name="name">
<cstring>txtUsername</cstring>
</property>
</widget>
<widget class="QLabel" row="3" column="0">
<property name="name">
<cstring>lblUsername</cstring>
</property>
<property name="font">
<font>
<family>Sans</family>
</font>
</property>
<property name="text">
<string>Username</string>
</property>
</widget>
<widget class="QLineEdit" row="4" column="1">
<property name="name">
<cstring>txtPassword</cstring>
</property>
<property name="echoMode">
<enum>Password</enum>
</property>
</widget>
<widget class="QLabel" row="0" column="0">
<property name="name">
<cstring>lblName</cstring>
</property>
<property name="font">
<font>
<family>Sans</family>
</font>
</property>
<property name="text">
<string>Name</string>
</property>
</widget>
<widget class="QLineEdit" row="0" column="1" rowspan="1" colspan="2">
<property name="name">
<cstring>txtName</cstring>
</property>
<property name="toolTip" stdset="0">
<string>Name of the new connection</string>
</property>
</widget>
<widget class="QLabel" row="4" column="0">
<property name="name">
<cstring>lblPassword</cstring>
</property>
<property name="font">
<font>
<family>Sans</family>
</font>
</property>
<property name="text">
<string>Password</string>
</property>
</widget>
<widget class="QPushButton" row="3" column="2" rowspan="2" colspan="1">
<property name="name">
<cstring>btnTestConnect</cstring>
</property>
<property name="font">
<font>
</font>
</property>
<property name="text">
<string>Test Connect</string>
</property>
</widget>
<widget class="QCheckBox" row="5" column="1" rowspan="1" colspan="2">
<property name="name">
<cstring>chkStorePassword</cstring>
</property>
<property name="font">
<font>
</font>
</property>
<property name="text">
<string>Save Password</string>
</property>
</widget>
</grid>
</widget>
<widget class="QLayoutWidget">
<property name="name">
<cstring>buttonHelp</cstring>
</property>
<property name="geometry">
<rect>
<x>311</x>
<y>21</y>
<width>80</width>
<height>189</height>
</rect>
</property>
<vbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<property name="margin">
<number>0</number>
</property>
<property name="spacing">
<number>6</number>
</property>
<widget class="QPushButton">
<property name="name">
<cstring>btnOk</cstring>
</property>
<property name="font">
<font>
</font>
</property>
<property name="text">
<string>OK</string>
</property>
<property name="accel">
<string></string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton">
<property name="name">
<cstring>btnCancel</cstring>
</property>
<property name="font">
<font>
</font>
</property>
<property name="text">
<string>Cancel</string>
</property>
<property name="accel">
<string></string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton">
<property name="name">
<cstring>btnHelp</cstring>
</property>
<property name="font">
<font>
</font>
</property>
<property name="text">
<string>Help</string>
</property>
<property name="accel">
<string>F1</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
</widget>
<spacer>
<property name="name">
<cstring>Spacer1</cstring>
</property>
<property name="orientation">
<enum>Vertical</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
</spacer>
</vbox>
</widget>
</widget>
<connections>
<connection>
<sender>btnCancel</sender>
<signal>clicked()</signal>
<receiver>QgsConnectionDialogBase</receiver>
<slot>close()</slot>
</connection>
<connection>
<sender>btnTestConnect</sender>
<signal>clicked()</signal>
<receiver>QgsConnectionDialogBase</receiver>
<slot>testConnection()</slot>
</connection>
<connection>
<sender>btnOk</sender>
<signal>clicked()</signal>
<receiver>QgsConnectionDialogBase</receiver>
<slot>saveConnection()</slot>
</connection>
<connection>
<sender>btnHelp</sender>
<signal>clicked()</signal>
<receiver>QgsConnectionDialogBase</receiver>
<slot>helpInfo()</slot>
</connection>
</connections>
<slots>
<slot>testConnection()</slot>
<slot>saveConnection()</slot>
<slot>helpInfo()</slot>
</slots>
<pixmapinproject/>
<layoutdefaults spacing="6" margin="11"/>
</UI>

80
plugins/spit/qgsdbfbase.h Normal file
View File

@ -0,0 +1,80 @@
/***************************************************************************
qgsbdfbase.h - Dbase IV Header
--------------------------------------
Date : 25-Dec-2003
Copyright : (C) 2003 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************
* *
* 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$ */
// Dbase header structure
struct DbaseHeader {
unsigned char valid_dbf;
char year;
char month;
char day;
long num_recs;
short size_hdr;
short size_rec;
char reserved[3];
char lan[13];
char reserved2[4];
};
// Field descriptor array - defines a field and its attributes (type,
// length, etc.
struct FieldDescriptorArray {
char field_name[11];
char field_type;
long field_addr; /* used only in memory */
unsigned char field_length;
unsigned char field_decimal;
char reserved[2];
char work_area;
char lan[2];
char set_fields;
char reserved2[8];
};
// Typedefs
typedef struct FieldDescriptorArray Fda;
typedef struct DbaseHeader DbH;
// Field Array class
class DbaseFieldArray {
public:
DbaseFieldArray(int numberOfFields);
void addField(char *name, char type, unsigned char length,
unsigned char decimal);
int getNumFields();
Fda *getField(int index);
private:
struct FieldDescriptorArray *fda;
unsigned int fieldCount;
int numFields;
};
// Dbase file class (incomplete implementation)
class DbaseFile {
public:
DbaseFile(char *fileName, int numRecords, int recordSize, DbaseFieldArray &fda);
void writeHeader();
void writeFieldDescriptors();
void writeRecord(const char *data);
void closeFile();
struct DbaseHeader header;
private:
char * fileName;
int numRecords;
int recordSize;
DbaseFieldArray fieldArray;
long pos;
bool firstRecord;
};

View File

@ -0,0 +1,24 @@
/***************************************************************************
qgsmessageviewer.cpp - description
-------------------
begin : Tue Dec 23 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. *
* *
***************************************************************************/
#include "qgsmessageviewer.h"
QgsMessageViewer::QgsMessageViewer(QWidget *parent, const char *name): QgsMessageViewerBase(parent, name){}
QgsMessageViewer::~QgsMessageViewer(){}
void QgsMessageViewer::setMessage(QString message){
txtMessage->setText(message);
}

View File

@ -0,0 +1,28 @@
/***************************************************************************
qgsmessageviewer.h - description
-------------------
begin : Tue Dec 23 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. *
* *
***************************************************************************/
#include <qwidget.h>
#include <qstring.h>
#include <qtextedit.h>
#include "qgsmessageviewerbase.h"
class QgsMessageViewer: public QgsMessageViewerBase{
public:
QgsMessageViewer(QWidget *parent=0, const char *name=0);
~QgsMessageViewer();
void setMessage(QString message);
};

View File

@ -0,0 +1,89 @@
<!DOCTYPE UI><UI version="3.1" stdsetdef="1">
<class>QgsMessageViewerBase</class>
<widget class="QDialog">
<property name="name">
<cstring>QgsMessageViewerBase</cstring>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>499</width>
<height>283</height>
</rect>
</property>
<property name="caption">
<string>SPIT Message</string>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<grid>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QTextEdit" row="0" column="0" rowspan="1" colspan="3">
<property name="name">
<cstring>txtMessage</cstring>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" row="1" column="1">
<property name="name">
<cstring>btnClose</cstring>
</property>
<property name="text">
<string>Close</string>
</property>
</widget>
<spacer row="1" column="0">
<property name="name">
<cstring>spacer1</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>191</width>
<height>31</height>
</size>
</property>
</spacer>
<spacer row="1" column="2">
<property name="name">
<cstring>spacer1_2</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>191</width>
<height>31</height>
</size>
</property>
</spacer>
</grid>
</widget>
<connections>
<connection>
<sender>btnClose</sender>
<signal>clicked()</signal>
<receiver>QgsMessageViewerBase</receiver>
<slot>reject()</slot>
</connection>
</connections>
<slots>
<slot>setMessage( QString msg )</slot>
</slots>
<layoutdefaults spacing="6" margin="11"/>
</UI>

340
plugins/spit/qgsspit.cpp Normal file
View File

@ -0,0 +1,340 @@
/***************************************************************************
qgsspit.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. *
* *
***************************************************************************/
#include <qsettings.h>
#include <qlistbox.h>
#include <qtable.h>
#include <qstringlist.h>
#include <qmessagebox.h>
#include <qcombobox.h>
#include <qpushbutton.h>
#include <qspinbox.h>
#include <qcheckbox.h>
#include <qinputdialog.h>
#include <qfiledialog.h>
#include <qprogressdialog.h>
#include <qmemarray.h>
#include <qapplication.h>
extern "C"
{
#include <libpq-fe.h>
}
#include "qgsspit.h"
#include "qgsconnectiondialog.h"
#include "qgsmessageviewer.h"
// Qt implementation of alignment() + changed the numeric types to be shown on the left as well
int QTableItem::alignment() const
{
bool num;
bool ok1 = FALSE, ok2 = FALSE;
(void)txt.toInt( &ok1 );
if ( !ok1 )
(void)txt.toDouble( &ok2 );
num = ok1 || ok2;
return ( num ? AlignLeft : AlignLeft ) | AlignVCenter;
}
QgsSpit::QgsSpit(QWidget *parent, const char *name) : QgsSpitBase(parent, name){
populateConnectionList();
defSrid = -1;
defGeom = "the_geom";
total_features = 0;
setFixedSize(QSize(579, 504));
tblShapefiles->verticalHeader()->hide();
tblShapefiles->adjustColumn(3);
tblShapefiles->setLeftMargin(0);
tblShapefiles->setColumnReadOnly(0, true);
tblShapefiles->setColumnReadOnly(1, true);
tblShapefiles->setColumnReadOnly(2, true);
chkUseDefaultSrid->setChecked(true);
chkUseDefaultGeom->setChecked(true);
useDefaultSrid();
useDefaultGeom();
}
QgsSpit::~QgsSpit(){
}
void QgsSpit::populateConnectionList(){
QSettings settings;
QStringList keys = settings.subkeyList("/Qgis/connections");
QStringList::Iterator it = keys.begin();
cmbConnections->clear();
while (it != keys.end()) {
cmbConnections->insertItem(*it);
++it;
}
}
void QgsSpit::newConnection()
{
QgsConnectionDialog *con = new QgsConnectionDialog(this, "New Connection");
if (con->exec()) {
populateConnectionList();
}
}
void QgsSpit::editConnection()
{
QgsConnectionDialog *con = new QgsConnectionDialog(this, cmbConnections->currentText());
if (con->exec()) {
con->saveConnection();
}
}
void QgsSpit::removeConnection()
{
QSettings settings;
QString key = "/Qgis/connections/" + cmbConnections->currentText();
QString msg = "Are you sure you want to remove the [" + cmbConnections->currentText() + "] connection and all associated settings?";
int result = QMessageBox::information(this, "Confirm Delete", msg, "Yes", "No");
if(result == 0){
settings.removeEntry(key + "/host");
settings.removeEntry(key + "/database");
settings.removeEntry(key + "/username");
settings.removeEntry(key + "/password");
settings.removeEntry(key + "/save");
cmbConnections->removeItem(cmbConnections->currentItem());
}
}
void QgsSpit::addFile()
{
QString error = "";
bool exist;
bool is_error = false;
QStringList files = QFileDialog::getOpenFileNames(
"Shapefiles (*.shp);; All Files (*)", "", this, "add file dialog", "Add Shapefiles" );
for ( QStringList::Iterator it = files.begin(); it != files.end(); ++it){
exist = false;
for(int n=0; n<tblShapefiles->numRows(); n++)
if(tblShapefiles->text(n,0)==*it){
exist = true;
break;
}
if(!exist){
QgsShapeFile * file = new QgsShapeFile(*it);
if(file->is_valid()){
int row = tblShapefiles->numRows();
fileList.push_back(file);
tblShapefiles->insertRows(row);
tblShapefiles->setText(row, 0, *it);
tblShapefiles->setText(row, 1, file->getFeatureClass());
tblShapefiles->setText(row, 2, QString("%1").arg(file->getFeatureCount()));
tblShapefiles->setText(row, 3, file->getTable());
total_features += file->getFeatureCount();
}
else{
error += *it + "\n";
is_error = true;
}
}
}
if(is_error){
error = "The following Shapefile(s) could not be loaded:\n\n" + error;
QgsMessageViewer * e = new QgsMessageViewer(this, "error");
e->setMessage(error);
e->exec();
}
tblShapefiles->adjustColumn(0);
tblShapefiles->adjustColumn(1);
tblShapefiles->adjustColumn(2);
tblShapefiles->adjustColumn(3);
tblShapefiles->setCurrentCell(-1, 0);
}
void QgsSpit::removeFile()
{
std::vector <int> temp;
for(int n=0; n<tblShapefiles->numRows(); n++)
if (tblShapefiles->isRowSelected(n)){
for(std::vector<QgsShapeFile *>::iterator vit = fileList.begin(); vit!=fileList.end(); vit++){
if((*vit)->getName()==tblShapefiles->text(n,0)){
total_features -= (*vit)->getFeatureCount();
fileList.erase(vit);
break;
}
}
temp.push_back(n);
}
QMemArray<int> array(temp.size());
for(int i=0; i<temp.size(); i++)
array[i] = temp[i];
tblShapefiles->removeRows(array);
tblShapefiles->setCurrentCell(-1, 0);
}
void QgsSpit::removeAllFiles(){
QMemArray<int> array(tblShapefiles->numRows());
for(int n=0; n<tblShapefiles->numRows(); n++)
array[n] = n;
fileList.clear();
total_features = 0;
tblShapefiles->removeRows(array);
}
void QgsSpit::useDefaultSrid(){
if(chkUseDefaultSrid->isChecked()) {
defaultSridValue = spinSrid->value();
spinSrid->setValue(defSrid);
spinSrid->setEnabled(false);
}
else {
spinSrid->setEnabled(true);
spinSrid->setValue(defaultSridValue);
}
}
void QgsSpit::useDefaultGeom(){
if(chkUseDefaultGeom->isChecked()) {
defaultGeomValue = txtGeomName->text();
txtGeomName->setText(defGeom);
txtGeomName->setEnabled(false);
}
else {
txtGeomName->setEnabled(true);
txtGeomName->setText(defaultGeomValue);
}
}
void QgsSpit::helpInfo(){
QString message = "General Interface Help:\n\n";
QgsMessageViewer * e = new QgsMessageViewer(this, "HelpMessage");
e->setMessage(message);
e->exec();
}
void QgsSpit::import(){
tblShapefiles->setCurrentCell(-1, 0);
QString connName = cmbConnections->currentText();
bool finished = false;
PGresult *res;
if (!connName.isEmpty()) {
QSettings settings;
QString key = "/Qgis/connections/" + connName;
QString connInfo = "host=" + settings.readEntry(key + "/host") + " dbname=" + settings.readEntry(key + "/database") +
" user=" + settings.readEntry(key + "/username") + " password=" + settings.readEntry(key + "/password");
PGconn *pd = PQconnectdb((const char *) connInfo);
if (PQstatus(pd) == CONNECTION_OK) {
QProgressDialog * pro = new QProgressDialog("Importing files", "Cancel", total_features, this, "Progress", true);
pro->setProgress(0);
pro->setAutoClose(true);
qApp->processEvents();
//pro->setAutoReset(true);
PQexec(pd, "BEGIN");
for(int i=0; i<fileList.size() ; i++){
fileList[i]->setTable(tblShapefiles->text(i, 3));
pro->show();
pro->setLabelText("Importing files\n"+fileList[i]->getName());
int rel_exists1 = 0;
int rel_exists2 = 0;
QMessageBox *del_confirm;
QString query = "SELECT f_table_name FROM geometry_columns WHERE f_table_name=\'"+fileList[i]->getTable()+"\'";
res = PQexec(pd, (const char *)query);
rel_exists1 = PQntuples(res);
PQclear(res);
query = "SELECT relname FROM pg_stat_all_tables WHERE relname=\'"+fileList[i]->getTable()+"\'";
res = PQexec(pd, (const char *)query);
rel_exists2 = PQntuples(res);
PQclear(res);
if(rel_exists1 || rel_exists2){
del_confirm = new QMessageBox("Import Shapefiles - Relation Exists",
"The Shapefile:\n"+fileList[i]->getName()+"\nwill use ["+
QString(fileList[i]->getTable())+"] relation for its data,\nwhich already exists and possibly contains data.\n"+
"To avoid data loss change the \"DB Relation Name\" \nfor this Shapefile in the main dialog file list.\n\n"+
"Do you want to overwrite the ["+fileList[i]->getTable()+"] relation?",
QMessageBox::Warning,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No | QMessageBox::Escape,
QMessageBox::NoButton, this, "Relation Exists");
}
if ((!rel_exists1 && !rel_exists2) || del_confirm->exec() == QMessageBox::Yes){
if(rel_exists1){
query = "SELECT DropGeometryColumn(\'"+QString(settings.readEntry(key + "/database"))+"\', \'"+
fileList[i]->getTable()+"\', \'"+txtGeomName->text()+"')";
res = PQexec(pd, (const char *)query);
PQclear(res);
}
if(rel_exists2){
query = "DROP TABLE " + fileList[i]->getTable();
res = PQexec(pd, (const char *)query);
PQclear(res);
}
if(!fileList[i]->insertLayer(settings.readEntry(key + "/database"), txtGeomName->text(),
QString("%1").arg(spinSrid->value()), pd, pro, finished)){
if(!finished){
pro->close();
QMessageBox::warning(this, "Import Shapefiles",
"Problem inserting features\nOne or more of your shapefiles may be corrupted");
}
finished = true;
break;
}
else if(finished){
pro->close();
break;
}
else{ // if file has been imported, remove it from the list
for(int j=0; j<tblShapefiles->numRows(); j++)
if(tblShapefiles->text(j,0)==QString(fileList[i]->getName())){
tblShapefiles->selectRow(j);
removeFile();
i--;
break;
}
}
}
else{
pro->setProgress(pro->progress()+fileList[i]->getFeatureCount());
}
}
if(finished)
PQexec(pd, "ROLLBACK");
else{
PQexec(pd, "COMMIT");
}
}
else
QMessageBox::warning(this, "Import Shapefiles", "Connection failed - Check settings and try again");
PQfinish(pd);
}
else
QMessageBox::warning(this, "Import Shapefiles", "You need to specify a Connection first");
}

49
plugins/spit/qgsspit.h Normal file
View File

@ -0,0 +1,49 @@
/***************************************************************************
qgsspit.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. *
* *
***************************************************************************/
#include <vector>
#include "qgsspitbase.h"
#include "qgsshapefile.h"
class QgsSpit :public QgsSpitBase{
public:
QgsSpit(QWidget *parent=0, const char *name=0);
~QgsSpit();
void populateConnectionList();
void dbConnect();
QStringList selectedTables();
QString connInfo();
void newConnection();
void editConnection();
void removeConnection();
void addFile();
void removeFile();
void removeAllFiles();
void useDefaultSrid();
void useDefaultGeom();
void helpInfo();
void import();
private:
int total_features;
std::vector <QgsShapeFile *> fileList;
int defSrid;
QString defGeom;
int defaultSridValue;
QString defaultGeomValue;
};

436
plugins/spit/qgsspitbase.ui Normal file
View File

@ -0,0 +1,436 @@
<!DOCTYPE UI><UI version="3.1" stdsetdef="1">
<class>QgsSpitBase</class>
<widget class="QDialog">
<property name="name">
<cstring>QgsSpitBase</cstring>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>579</width>
<height>504</height>
</rect>
</property>
<property name="caption">
<string>SPIT - Shapefile to PostGIS Import Tool</string>
</property>
<property name="sizeGripEnabled">
<bool>false</bool>
</property>
<grid>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QGroupBox" row="0" column="0" rowspan="1" colspan="6">
<property name="name">
<cstring>groupBox8</cstring>
</property>
<property name="title">
<string>PostgreSQL Connections</string>
</property>
<grid>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QComboBox" row="0" column="2" rowspan="1" colspan="3">
<property name="name">
<cstring>cmbConnections</cstring>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
</widget>
<spacer row="0" column="1">
<property name="name">
<cstring>spacer4</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>90</width>
<height>21</height>
</size>
</property>
</spacer>
<widget class="QPushButton" row="1" column="4">
<property name="name">
<cstring>btnRemove</cstring>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
<widget class="QPushButton" row="1" column="3">
<property name="name">
<cstring>btnEdit</cstring>
</property>
<property name="text">
<string>Edit ...</string>
</property>
</widget>
<widget class="QPushButton" row="1" column="2">
<property name="name">
<cstring>btnNew</cstring>
</property>
<property name="text">
<string>New ...</string>
</property>
</widget>
<spacer row="1" column="0" rowspan="1" colspan="2">
<property name="name">
<cstring>spacer5</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>231</width>
<height>21</height>
</size>
</property>
</spacer>
<widget class="QLabel" row="0" column="0">
<property name="name">
<cstring>textLabel1</cstring>
</property>
<property name="text">
<string>Connection</string>
</property>
</widget>
</grid>
</widget>
<widget class="QPushButton" row="2" column="1">
<property name="name">
<cstring>btnImport</cstring>
</property>
<property name="text">
<string>Import</string>
</property>
</widget>
<widget class="QPushButton" row="2" column="2">
<property name="name">
<cstring>btnQuit</cstring>
</property>
<property name="text">
<string>Quit</string>
</property>
</widget>
<widget class="QPushButton" row="2" column="4">
<property name="name">
<cstring>btnHelp</cstring>
</property>
<property name="text">
<string>Help</string>
</property>
</widget>
<spacer row="2" column="5">
<property name="name">
<cstring>spacer1_2_2</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>110</width>
<height>31</height>
</size>
</property>
</spacer>
<spacer row="2" column="0">
<property name="name">
<cstring>spacer1_2</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>110</width>
<height>31</height>
</size>
</property>
</spacer>
<spacer row="2" column="3">
<property name="name">
<cstring>spacer11</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>41</width>
<height>30</height>
</size>
</property>
</spacer>
<widget class="QGroupBox" row="1" column="0" rowspan="1" colspan="6">
<property name="name">
<cstring>groupBox1</cstring>
</property>
<property name="title">
<string>Shapefile List</string>
</property>
<grid>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QPushButton" row="0" column="0">
<property name="name">
<cstring>btnAddFile</cstring>
</property>
<property name="text">
<string>Add ...</string>
</property>
</widget>
<widget class="QPushButton" row="0" column="1">
<property name="name">
<cstring>btnRemoveFile</cstring>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
<widget class="QPushButton" row="0" column="2" rowspan="1" colspan="2">
<property name="name">
<cstring>btnRemoveAll</cstring>
</property>
<property name="text">
<string>Remove All</string>
</property>
</widget>
<widget class="QTable" row="4" column="0" rowspan="1" colspan="6">
<column>
<property name="text">
<string>File Name</string>
</property>
</column>
<column>
<property name="text">
<string>Feature Class</string>
</property>
</column>
<column>
<property name="text">
<string>Features</string>
</property>
</column>
<column>
<property name="text">
<string>DB Relation Name</string>
</property>
</column>
<property name="name">
<cstring>tblShapefiles</cstring>
</property>
<property name="numRows">
<number>0</number>
</property>
<property name="numCols">
<number>4</number>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
<property name="selectionMode">
<enum>MultiRow</enum>
</property>
<property name="focusStyle">
<enum>FollowStyle</enum>
</property>
</widget>
<widget class="QSpinBox" row="2" column="1">
<property name="name">
<cstring>spinSrid</cstring>
</property>
<property name="maxValue">
<number>1000000</number>
</property>
<property name="minValue">
<number>-1</number>
</property>
<property name="value">
<number>-1</number>
</property>
</widget>
<widget class="QCheckBox" row="3" column="1" rowspan="1" colspan="2">
<property name="name">
<cstring>chkUseDefaultSrid</cstring>
</property>
<property name="text">
<string>Use Default</string>
</property>
</widget>
<widget class="QCheckBox" row="3" column="5">
<property name="name">
<cstring>chkUseDefaultGeom</cstring>
</property>
<property name="text">
<string>Use Default</string>
</property>
</widget>
<widget class="QLabel" row="2" column="3" rowspan="1" colspan="2">
<property name="name">
<cstring>textLabel1_3</cstring>
</property>
<property name="text">
<string>Geometry Column Name</string>
</property>
</widget>
<widget class="QLabel" row="2" column="0">
<property name="name">
<cstring>textLabel1_2</cstring>
</property>
<property name="text">
<string>SRID</string>
</property>
</widget>
<spacer row="0" column="4" rowspan="1" colspan="2">
<property name="name">
<cstring>spacer8</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>241</width>
<height>31</height>
</size>
</property>
</spacer>
<widget class="QLineEdit" row="2" column="5">
<property name="name">
<cstring>txtGeomName</cstring>
</property>
</widget>
<widget class="Line" row="1" column="0" rowspan="1" colspan="6">
<property name="name">
<cstring>line1</cstring>
</property>
<property name="frameShape">
<enum>HLine</enum>
</property>
<property name="frameShadow">
<enum>Sunken</enum>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
</widget>
</grid>
</widget>
</grid>
</widget>
<connections>
<connection>
<sender>chkUseDefaultSrid</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>useDefaultSrid()</slot>
</connection>
<connection>
<sender>btnRemoveAll</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>removeAllFiles()</slot>
</connection>
<connection>
<sender>btnRemove</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>removeConnection()</slot>
</connection>
<connection>
<sender>btnAddFile</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>addFile()</slot>
</connection>
<connection>
<sender>btnRemoveFile</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>removeFile()</slot>
</connection>
<connection>
<sender>btnEdit</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>editConnection()</slot>
</connection>
<connection>
<sender>btnQuit</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>close()</slot>
</connection>
<connection>
<sender>btnImport</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>import()</slot>
</connection>
<connection>
<sender>btnNew</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>newConnection()</slot>
</connection>
<connection>
<sender>btnHelp</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>helpInfo()</slot>
</connection>
<connection>
<sender>chkUseDefaultGeom</sender>
<signal>clicked()</signal>
<receiver>QgsSpitBase</receiver>
<slot>useDefaultGeom()</slot>
</connection>
</connections>
<slots>
<slot>import()</slot>
<slot>newConnection()</slot>
<slot>editConnection()</slot>
<slot>removeConnection()</slot>
<slot>addFile()</slot>
<slot>removeFile()</slot>
<slot>removeAllFiles()</slot>
<slot>useDefaultSrid()</slot>
<slot>helpInfo()</slot>
<slot>aboutInfo()</slot>
<slot>useDefaultGeom()</slot>
</slots>
<layoutdefaults spacing="6" margin="11"/>
</UI>

View File

@ -0,0 +1,159 @@
/***************************************************************************
qgsspitplugin.cpp
Shapefile to PostgreSQL Import Tool plugin
-------------------
begin : Jan 30, 2004
copyright : (C) 2004 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* 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$ */
// includes
#include <iostream>
#include <vector>
#include "../../src/qgisapp.h"
#include <qtoolbar.h>
#include <qmenubar.h>
#include <qmessagebox.h>
#include <qpopupmenu.h>
#include <qlineedit.h>
#include <qaction.h>
#include <qapplication.h>
#include <qcursor.h>
#include "qgsspitplugin.h"
#include "qgsspit.h"
// xpm for creating the toolbar icon
#include "icon_spit.xpm"
/**
* Constructor for the plugin. The plugin is passed a pointer to the main app
* and an interface object that provides access to exposed functions in QGIS.
* @param qgis Pointer to the QGIS main window
* @parma _qI Pointer to the QGIS interface object
*/
QgsSpitPlugin::QgsSpitPlugin(QgisApp * qgis, QgisIface * _qI):qgisMainWindow(qgis), qI(_qI)
{
/** Initialize the plugin and set the required attributes */
pName = "SPIT";
pVersion = "Version 0.1";
pDescription = "Shapefile to PostgreSQL/PostGIS Import Tool";
}
QgsSpitPlugin::~QgsSpitPlugin()
{
}
/* Following functions return name, description, version, and type for the plugin */
QString QgsSpitPlugin::name()
{
return pName;
}
QString QgsSpitPlugin::version()
{
return pVersion;
}
QString QgsSpitPlugin::description()
{
return pDescription;
}
int QgsSpitPlugin::type()
{
return QgisPlugin::UI;
}
/*
* Initialize the GUI interface for the plugin
*/
void QgsSpitPlugin::initGui()
{
// add a menu with 2 items
QPopupMenu *pluginMenu = new QPopupMenu(qgisMainWindow);
pluginMenu->insertItem("&Import Shapefiles to PostgreSQL", this, SLOT(spit()));
pluginMenu->insertItem("&Unload SPTI Plugin", this, SLOT(unload()));
menu = ((QMainWindow *) qgisMainWindow)->menuBar();
menuId = menu->insertItem("&Spit", pluginMenu);
// Create the action for tool
QAction *spitAction = new QAction("Import Shapefiles to PostgreSQL", QIconSet(icon_spit), "&SPIT",
0, this, "spit");
// Connect the action to the zoomPrevous slot
connect(spitAction, SIGNAL(activated()), this, SLOT(spit()));
// Add the toolbar
toolBar = new QToolBar((QMainWindow *) qgisMainWindow, "spit");
toolBar->setLabel("SPIT");
// Add the zoom previous tool to the toolbar
spitAction->addTo(toolBar);
}
// Slot called when the shapefile to postgres menu item is activated
void QgsSpitPlugin::spit()
{
QgsSpit *spitDlg = new QgsSpit();
spitDlg->show();
}
// Unload the plugin by cleaning up the GUI
void QgsSpitPlugin::unload()
{
// remove the GUI
menu->removeItem(menuId);
delete toolBar;
}
/**
* Required extern functions needed for every plugin
* These functions can be called prior to creating an instance
* of the plugin class
*/
// Class factory to return a new instance of the plugin class
extern "C" QgisPlugin * classFactory(QgisApp * qgis, QgisIface * qI)
{
return new QgsSpitPlugin(qgis, qI);
}
// Return the name of the plugin
extern "C" QString name()
{
return QString("SPIT - Shapefile to PostgreSQL Import Tool");
}
// Return the description
extern "C" QString description()
{
return QString("Import ESRI Shapefiles to PostgreSQL/PostGIS layer");
}
// Return the type (either UI or MapLayer plugin)
extern "C" int type()
{
return QgisPlugin::UI;
}
// Delete ourself
extern "C" void unload(QgisPlugin * p)
{
delete p;
}

View File

@ -0,0 +1,99 @@
/***************************************************************************
qgsspitplugin.h
Shapefile to PostgreSQL Import Tool plugin
-------------------
begin : Jan 30, 2004
copyright : (C) 2004 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* 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 QGSSPITPLUGIN_H
#define QGSSPITPLUGIN_H
#include "../qgisplugin.h"
#include <qwidget.h>
#include <qmainwindow.h>
extern "C"
{
#include <libpq-fe.h>
}
class QMessageBox;
class QToolBar;
class QMenuBar;
class QPopupMenu;
//#include "qgsworkerclass.h"
#include "../../src/qgisapp.h"
/**
* \class QgsSpitPlugin
* \brief SPIT PostgreSQL/PostGIS plugin for QGIS
*
*/
class QgsSpitPlugin:public QObject, public QgisPlugin
{
Q_OBJECT public:
/**
* Constructor for a plugin. The QgisApp and QgisIface pointers are passed by
* QGIS when it attempts to instantiate the plugin.
* @param qgis Pointer to the QgisApp object
* @param qI Pointer to the QgisIface object.
*/
QgsSpitPlugin(QgisApp * qgis, QgisIface * qI);
/**
* Virtual function to return the name of the plugin. The name will be used when presenting a list
* of installable plugins to the user
*/
virtual QString name();
/**
* Virtual function to return the version of the plugin.
*/
virtual QString version();
/**
* Virtual function to return a description of the plugins functions
*/
virtual QString description();
/**
* Return the plugin type
*/
virtual int type();
//! init the gui
virtual void initGui();
//! Destructor
virtual ~ QgsSpitPlugin();
public slots:
void spit();
//! unload the plugin
void unload();
private:
//! Name of the plugin
QString pName;
//! Version
QString pVersion;
//! Descrption of the plugin
QString pDescription;
//! Plugin type as defined in QgisPlugin::PLUGINTYPE
int ptype;
//! Id of the plugin's menu. Used for unloading
int menuId;
//! Pointer to our toolbar
QToolBar *toolBar;
//! Pointer to our menu
QMenuBar *menu;
//! Pionter to QGIS main application object
QgisApp *qgisMainWindow;
//! Pointer to the QGIS interface object
QgisIface *qI;
};
#endif