more progress towards a buffer function

git-svn-id: http://svn.osgeo.org/qgis/trunk@565 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-01-23 07:37:32 +00:00
parent 5583b3fd0b
commit 8c3e5fc65b
5 changed files with 223 additions and 80 deletions

View File

@ -7,7 +7,9 @@
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# $Id
#
# $Id$
lib_LTLIBRARIES = libpggeoprocessingplugin.la
%.moc.cpp: %.h
$(MOC) -o $@ $<

View File

@ -22,6 +22,32 @@ QString QgsDlgPgBuffer::bufferLayerName(){
bool QgsDlgPgBuffer::addLayerToMap(){
return chkAddToMap->isChecked();
}
QString QgsDlgPgBuffer::geometryColumn(){
return txtGeometryColumn->text();
}
QString QgsDlgPgBuffer::srid(){
return txtSrid->text();
}
QString QgsDlgPgBuffer::objectIdColumn(){
return cmbFields->currentText();
}
QString QgsDlgPgBuffer::schema(){
return cmbSchema->currentText();
}
void QgsDlgPgBuffer::addFieldItem(QString field){
cmbFields->insertItem(field);
}
void QgsDlgPgBuffer::addSchema(QString schema){
cmbSchema->insertItem(schema);
}
void QgsDlgPgBuffer::setSrid(QString srid){
txtSrid->setText(srid);
}
void QgsDlgPgBuffer::setBufferLayerName(QString name){
txtBufferedLayerName->setText(name);
}
void QgsDlgPgBuffer::setGeometryColumn(QString name){
txtGeometryColumn->setText(name);
}

View File

@ -10,7 +10,15 @@ public:
void setBufferLabel(QString &lbl);
QString bufferDistance();
QString bufferLayerName();
QString objectIdColumn();
QString geometryColumn();
QString srid();
QString schema();
bool addLayerToMap();
void addFieldItem(QString field);
void addSchema(QString schema);
void setSrid(QString srid);
void setBufferLayerName(QString name);
void setGeometryColumn(QString name);
};
#endif // QGSDLGPGBUFFER_H

View File

@ -8,8 +8,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>516</width>
<height>275</height>
<width>520</width>
<height>294</height>
</rect>
</property>
<property name="caption">
@ -22,7 +22,7 @@
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QLabel" row="0" column="0" rowspan="1" colspan="2">
<widget class="QLabel" row="0" column="0" rowspan="1" colspan="3">
<property name="name">
<cstring>lblBufferInfo</cstring>
</property>
@ -30,33 +30,7 @@
<string>Buffer the features in layer</string>
</property>
</widget>
<widget class="QLabel" row="2" column="0" rowspan="1" colspan="2">
<property name="name">
<cstring>textLabel3</cstring>
</property>
<property name="text">
<string>Table name for the buffered layer</string>
</property>
</widget>
<widget class="QLineEdit" row="1" column="2" rowspan="1" colspan="2">
<property name="name">
<cstring>txtBufferDistance</cstring>
</property>
</widget>
<widget class="QLabel" row="1" column="0" rowspan="1" colspan="2">
<property name="name">
<cstring>textLabel2</cstring>
</property>
<property name="text">
<string>Buffer distance in map units</string>
</property>
</widget>
<widget class="QLineEdit" row="2" column="2" rowspan="1" colspan="3">
<property name="name">
<cstring>txtBufferedLayerName</cstring>
</property>
</widget>
<spacer row="1" column="4">
<spacer row="1" column="5" rowspan="1" colspan="3">
<property name="name">
<cstring>spacer2</cstring>
</property>
@ -73,15 +47,28 @@
</size>
</property>
</spacer>
<widget class="QLabel" row="3" column="0" rowspan="1" colspan="5">
<widget class="QLineEdit" row="1" column="3" rowspan="1" colspan="2">
<property name="name">
<cstring>textLabel1</cstring>
</property>
<property name="text">
<string>Use the following field as feature id (must be unique)</string>
<cstring>txtBufferDistance</cstring>
</property>
</widget>
<widget class="QLayoutWidget" row="6" column="0" rowspan="1" colspan="5">
<widget class="QLabel" row="1" column="0" rowspan="1" colspan="3">
<property name="name">
<cstring>textLabel2</cstring>
</property>
<property name="text">
<string>Buffer distance in map units</string>
</property>
</widget>
<widget class="QCheckBox" row="6" column="0" rowspan="1" colspan="5">
<property name="name">
<cstring>chkAddToMap</cstring>
</property>
<property name="text">
<string>Add the buffered layer to the map</string>
</property>
</widget>
<widget class="QLayoutWidget" row="7" column="0" rowspan="1" colspan="8">
<property name="name">
<cstring>Layout1</cstring>
</property>
@ -159,15 +146,55 @@
</widget>
</hbox>
</widget>
<widget class="QCheckBox" row="5" column="0" rowspan="1" colspan="4">
<spacer row="3" column="7">
<property name="name">
<cstring>chkAddToMap</cstring>
<cstring>spacer5</cstring>
</property>
<property name="text">
<string>Add the buffered layer to the map</string>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>43</width>
<height>28</height>
</size>
</property>
</spacer>
<widget class="QLineEdit" row="2" column="3" rowspan="1" colspan="5">
<property name="name">
<cstring>txtBufferedLayerName</cstring>
</property>
</widget>
<widget class="QComboBox" row="4" column="1" rowspan="1" colspan="2">
<widget class="QLabel" row="2" column="0" rowspan="1" colspan="3">
<property name="name">
<cstring>textLabel3</cstring>
</property>
<property name="text">
<string>Table name for the buffered layer</string>
</property>
</widget>
<widget class="QComboBox" row="3" column="1" rowspan="1" colspan="3">
<item>
<property name="text">
<string>public</string>
</property>
</item>
<property name="name">
<cstring>cmbSchema</cstring>
</property>
</widget>
<widget class="QLabel" row="3" column="0">
<property name="name">
<cstring>textLabel1_2</cstring>
</property>
<property name="text">
<string>Schema</string>
</property>
</widget>
<widget class="QComboBox" row="5" column="3" rowspan="1" colspan="4">
<item>
<property name="text">
<string>Create unique object id</string>
@ -177,40 +204,40 @@
<cstring>cmbFields</cstring>
</property>
</widget>
<spacer row="4" column="3" rowspan="1" colspan="2">
<widget class="QLabel" row="5" column="0" rowspan="1" colspan="3">
<property name="name">
<cstring>spacer4</cstring>
<cstring>textLabel1</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
<property name="text">
<string>Unique field to use as feature id</string>
</property>
<property name="sizeType">
<enum>Fixed</enum>
</property>
<property name="sizeHint">
<size>
<width>258</width>
<height>24</height>
</size>
</property>
</spacer>
<spacer row="4" column="0">
</widget>
<widget class="QLineEdit" row="3" column="6">
<property name="name">
<cstring>spacer3</cstring>
<cstring>txtSrid</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</widget>
<widget class="QLabel" row="3" column="4" rowspan="1" colspan="2">
<property name="name">
<cstring>textLabel2_2</cstring>
</property>
<property name="sizeType">
<enum>Fixed</enum>
<property name="text">
<string>Spatial Ref. Id</string>
</property>
<property name="sizeHint">
<size>
<width>44</width>
<height>19</height>
</size>
</widget>
<widget class="QLabel" row="4" column="0" rowspan="1" colspan="2">
<property name="name">
<cstring>textLabel2_2_2</cstring>
</property>
</spacer>
<property name="text">
<string>Geometry column</string>
</property>
</widget>
<widget class="QLineEdit" row="4" column="2" rowspan="1" colspan="2">
<property name="name">
<cstring>txtGeometryColumn</cstring>
</property>
</widget>
</grid>
</widget>
<connections>

View File

@ -123,43 +123,123 @@ void QgsPgGeoprocessing::buffer()
QStringList tables = connStrings.grep("table=");
QString table = tables[0];
QString tableName = table.mid(table.find("=") + 1);
// get the schema
QString schema = tableName.left(tableName.find("."));
// get the database name
QStringList dbnames = connStrings.grep("dbname=");
QString dbname = dbnames[0];
dbname = dbname.mid(dbname.find("=") + 1);
// get the user name
QStringList userNames = connStrings.grep("user=");
QString user = userNames[0];
user = user.mid(user.find("=") + 1);
// show dialog to fetch buffer distrance, new layer name, and option to
QgsDlgPgBuffer *bb = new QgsDlgPgBuffer();
// set the label
QString lbl = tr("Buffer features in layer %1").arg(tableName);
bb->setBufferLabel(lbl);
// set the fields
// set a default output table name
bb->setBufferLayerName(tableName.mid(tableName.find(".")+1) + "_buffer");
// set the fields on the dialog box drop-down
QgsDataProvider *dp = lyr->getDataProvider();
std::vector<QgsField> flds = dp->fields();
for(int i=0; i < flds.size(); i++){
bb->addFieldItem(flds[i].getName());
// check the field type -- if its int we can use it
if(flds[i].getType().find("int") > -1){
bb->addFieldItem(flds[i].getName());
}
}
if (bb->exec()) {
// connect to the database
// connect to the database
PGconn *conn = PQconnectdb((const char *) connInfo);
if (PQstatus(conn) == CONNECTION_OK) {
// get some info from the source layer so we can duplicate it in the new layer
QString sql = "select * from geometry_columns";
// populate the schema drop-down
QString schemaSql = QString("select nspname from pg_namespace,pg_user where nspowner = usesysid and usename = '%1'").arg(user);
PGresult *schemas = PQexec(conn, (const char *) schemaSql);
if(PQresultStatus(schemas) == PGRES_TUPLES_OK){
// add the schemas to the drop-down, otherwise just public (the
// default) will show up
for(int i=0; i < PQntuples(schemas); i++){
bb->addSchema(PQgetvalue(schemas,i,0));
}
}
PQclear(schemas);
// query the geometry_columns table to get the srid and use it as default
QString sridSql = QString("select srid,f_geometry_column from geometry_columns where f_table_schema='%1' and f_table_name='%2'")
.arg(schema)
.arg(tableName.mid(tableName.find(".")+1));
QMessageBox::information(0,"SRID SQL",sridSql);
QString geometryCol;
PGresult *sridq = PQexec(conn,(const char *)sridSql);
if(PQresultStatus(sridq) == PGRES_TUPLES_OK){
bb->setSrid(PQgetvalue(sridq,0,0));
geometryCol = PQgetvalue(sridq,0,1);
bb->setGeometryColumn(geometryCol);
}else{
bb->setSrid("-1");
}
PQclear(sridq);
// exec the dialog and process if user selects ok
if (bb->exec()) {
// determine what column to use as the obj id
QString objId = bb->objectIdColumn();
QString objIdType = "int";
QString objIdValue;
if(objId == "Create unique object id"){
objId = "objectid";
objIdType = "serial";
objIdValue = "0";
}else{
objIdValue = objId;
}
// first create the new table
sql = QString("create table %1 (objectid int)").arg(bb->bufferLayerName());
QString sql = QString("create table %1.%2 (%3 %4)")
.arg(bb->schema())
.arg(bb->bufferLayerName())
.arg(objId)
.arg(objIdType);
PGresult *result = PQexec(conn, (const char *) sql);
if (PQresultStatus(result) == PGRES_COMMAND_OK) {
// add the geometry column
sql = "select addgeometrycolumn(%1,%2,%3,%4,%5";
// add new layer to the map
//<db_name>, <table_name>, <column_name>, <srid>, <type>, <dimension>
sql = QString("select addgeometrycolumn('%1','%2.%4','%4',%5,'%6',%7)")
.arg(dbname)
.arg(bb->schema())
.arg(bb->bufferLayerName())
.arg(bb->geometryColumn())
.arg(bb->srid())
.arg("POLYGON")
.arg("2");
QMessageBox::information(0,"AddGeomCol",sql);
PGresult *geoCol = PQexec(conn, (const char *)sql);
if(PQresultStatus(geoCol) == PGRES_COMMAND_OK) {
// do the buffer and insert the features
sql = QString("insert into %1 select %2, buffer(%3,%4) from %5")
.arg(bb->bufferLayerName())
.arg(objIdValue)
.arg(geometryCol)
.arg(bb->bufferDistance().toDouble())
.arg(tableName);
PQexec(conn, (const char *)sql);
}
// add new layer to the map
if(bb->addLayerToMap()){
// create the connection string
QString conn = "dbname=%1 host";
}
} else {
QMessageBox::critical(0, "Unable to create table",
QString("Failed to create the output table %1").arg(bb->bufferLayerName()));
}
} else {
}
} else {
// connection error
QString err = tr("Error connecting to the database");
QMessageBox::critical(0, err, PQerrorMessage(conn));
}
}
} else {
QMessageBox::critical(0, "Not a PostgreSQL/PosGIS Layer",
QString