📄 qgspggeoprocessing.cpp
字号:
* buffer plugin will fail for older version of PostGIS. */ // drop the check constraint based on geometry type sql = QString("alter table %1.%2 drop constraint \"enforce_geotype_shape\"") .arg(bb->schema()) .arg(bb->bufferLayerName()); QgsDebugMsg( "SQL: " + sql); result = PQexec(conn, (const char *) sql); if(PQresultStatus(result) == PGRES_COMMAND_OK) { PQclear(result); // check pg version and formulate insert query accordingly result = PQexec(conn,"select version()"); QString versionString = PQgetvalue(result,0,0); QStringList versionParts = QStringList::split(" ", versionString); // second element is the version number QString version = versionParts[1]; if(version < "7.4.0"){ // modify the tableName tableName = uri.table(); } QgsDebugMsg("Table name for PG 7.3 is: " + uri.table() ); // if(PQresultStatus(geoCol) == PGRES_COMMAND_OK) { // do the buffer and insert the features if (objId == "objectid") { sql = QString("insert into %1 (%2) select buffer(%3,%4) from %5") .arg(bb->bufferLayerName()) .arg(bb->geometryColumn()) .arg(geometryCol) .arg(bb->bufferDistance().toDouble()) .arg(tableName); } else { 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); QgsDebugMsg("SQL: " + sql); } result = PQexec(conn, (const char *) sql); PQclear(result); // } QgsDebugMsg("SQL: " + sql); result = PQexec(conn, "end work"); PQclear(result); result = PQexec(conn, "commit;vacuum"); PQclear(result); PQfinish(conn); // QMessageBox::information(0, "Add to Map?", "Do you want to add the layer to the map?"); // add new layer to the map if (bb->addLayerToMap()) { // create the connection string QString newLayerSource = uri.connInfo(); QgsDebugMsg("newLayerSource: " + newLayerSource ); // add the schema.table and geometry column /* newLayerSource += "table=" + bb->schema() + "." + bb->bufferLayerName() + " (" + bb->geometryColumn() + ")"; */ QgsDebugMsg("Adding new layer using " + newLayerSource); // host=localhost dbname=gis_data user=gsherman password= table=public.alaska (the_geom) // Using addVectorLayer requires that be add a table=xxxx to the layer path since // addVectorLayer is generic for all supported layers std::cerr << "Building dataURI string" << std::endl; QString dataURI = newLayerSource + "table=" + bb->schema() + "." + bb->bufferLayerName() + " (" + bb->geometryColumn() + ")\n" + bb->schema() + "." + bb->bufferLayerName() + " (" + bb->geometryColumn() + ")\n" + "postgres"; std::cerr << "Passing to addVectorLayer:\n" << dataURI.toLocal8Bit().data() << std::endl; qI->addVectorLayer(newLayerSource + "table=" + bb->schema() + "." + bb->bufferLayerName() + " (" + bb->geometryColumn() + ")", bb->schema() + "." + bb->bufferLayerName() + " (" + bb->geometryColumn() + ")", "postgres"); } } else { QgsDebugMsg(QString("Status from drop constraint is %1").arg(PQresultStatus(result)) ); QgsDebugMsg(QString("Error message is %1").arg( PQresStatus(PQresultStatus(result))) ); } } else { QgsDebugMsg(QString("Status from add geometry column is %1").arg(PQresultStatus(geoCol)) ); QgsDebugMsg(QString("Error message is %1").arg( PQresStatus(PQresultStatus(geoCol))) ); QMessageBox::critical(0, tr("Unable to add geometry column"), QString(tr("Unable to add geometry column to the output table ") + QString("%1-%2").arg(bb->bufferLayerName()).arg(PQerrorMessage(conn)))); } } else { QMessageBox::critical(0, tr("Unable to create table"), QString(tr("Failed to create the output table ") + QString("%1").arg(bb->bufferLayerName()))); } QApplication::restoreOverrideCursor(); } delete bb; } else { // connection error QString err = tr("Error connecting to the database"); QMessageBox::critical(0, err, PQerrorMessage(conn)); } } else { QMessageBox::critical(0,tr("No GEOS support"), tr("Buffer function requires GEOS support in PostGIS")); } } else { QMessageBox::critical(0, tr("Not a PostgreSQL/PostGIS Layer"), QString("%1").arg(lyr->name()) + tr(" is not a PostgreSQL/PostGIS layer.\n") + tr("Geoprocessing functions are only available for PostgreSQL/PostGIS Layers")); } } else { QMessageBox::warning(0, tr("No Active Layer"), tr("You must select a layer in the legend to buffer")); }}/* Functions for determining available features in postGIS */QString QgsPgGeoprocessing::postgisVersion(PGconn *connection){ PGresult *result = PQexec(connection, "select postgis_version()"); postgisVersionInfo = PQgetvalue(result,0,0); QgsDebugMsg("PostGIS version info: " + postgisVersionInfo); // assume no capabilities geosAvailable = false; gistAvailable = false; projAvailable = false; // parse out the capabilities and store them QStringList postgisParts = QStringList::split(" ", postgisVersionInfo); QStringList geos = postgisParts.grep("GEOS"); if(geos.size() == 1){ geosAvailable = (geos[0].find("=1") > -1); } QStringList gist = postgisParts.grep("STATS"); if(gist.size() == 1){ gistAvailable = (geos[0].find("=1") > -1); } QStringList proj = postgisParts.grep("PROJ"); if(proj.size() == 1){ projAvailable = (proj[0].find("=1") > -1); } return postgisVersionInfo;}bool QgsPgGeoprocessing::hasGEOS(PGconn *connection){ // make sure info is up to date for the current connection postgisVersion(connection); // get geos capability return geosAvailable;}bool QgsPgGeoprocessing::hasGIST(PGconn *connection){ // make sure info is up to date for the current connection postgisVersion(connection); // get gist capability return gistAvailable;}bool QgsPgGeoprocessing::hasPROJ(PGconn *connection){ // make sure info is up to date for the current connection postgisVersion(connection); // get proj4 capability return projAvailable;}// Unload the plugin by cleaning up the GUIvoid QgsPgGeoprocessing::unload(){ // remove the GUI qI->removePluginMenu(tr("&Geoprocessing"),bufferAction); qI->removeToolBarIcon(bufferAction); delete bufferAction;}/** * 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 classQGISEXTERN QgisPlugin * classFactory(QgisInterface * qI){ return new QgsPgGeoprocessing(qI);}// Return the name of the pluginQGISEXTERN QString name(){ return name_;}// Return the descriptionQGISEXTERN QString description(){ return description_;}// Return the type (either UI or MapLayer plugin)QGISEXTERN int type(){ return type_;}// Return the version number for the pluginQGISEXTERN QString version(){ return version_;}// Delete ourselfQGISEXTERN void unload(QgisPlugin * p){ delete p;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -