Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Apply patch #2452 (save as vector)
git-svn-id: http://svn.osgeo.org/qgis/trunk@13072 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Mar 18, 2010
1 parent 116dfc8 commit d14a5e6
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 12 deletions.
21 changes: 20 additions & 1 deletion python/core/qgsvectorfilewriter.sip
Expand Up @@ -31,13 +31,32 @@ public:
const QgsCoordinateReferenceSystem*,
bool onlySelected = FALSE);

/** Write contents of vector layer to an (OGR supported) vector formt
@note: this method was added in version 1.5*/
static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
const QString& fileName,
const QString& fileEncoding,
const QgsCoordinateReferenceSystem *destCRS,
const QString& driverName = "ESRI Shapefile",
bool onlySelected = FALSE,
QString *errorMessage = 0 );

/** create shapefile and initialize it */
QgsVectorFileWriter(const QString& shapefileName,
QgsVectorFileWriter(const QString& vectorFileName,
const QString& fileEncoding,
const QMap<int, QgsField>& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* srs,
const QString& driverName = "ESRI Shapefile" );

/**Returns map with format filter string as key and OGR format key as value*/
static QMap< QString, QString> supportedFiltersAndFormats();

/**Returns filter string that can be used for dialogs*/
static QString fileFilterString();

/**Creates a filter for an OGR driver key*/
static QString filterForDriver( const QString& driverName );

/** checks whether there were any errors in constructor */
WriterError hasError();
Expand Down
94 changes: 89 additions & 5 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -423,14 +423,14 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu, QAction* toggleEditingActio
}
}

// save as shapefile
theMenu.addAction( tr( "Save as shapefile..." ), this, SLOT( saveAsShapefile() ) );
// save as vector file
theMenu.addAction( tr( "Save as..." ), this, SLOT( saveAsVectorFile() ) );

// save selection as shapefile
QAction* saveSelectionAction = theMenu.addAction( tr( "Save selection as shapefile..." ), this, SLOT( saveSelectionAsShapefile() ) );
// save selection as vector file
QAction* saveSelectionAsAction = theMenu.addAction( tr( "Save selection as..." ), this, SLOT( saveSelectionAsVectorFile() ) );
if ( vlayer->selectedFeatureCount() == 0 )
{
saveSelectionAction->setEnabled( false );
saveSelectionAsAction->setEnabled( false );
}

theMenu.addSeparator();
Expand Down Expand Up @@ -460,6 +460,16 @@ void QgsLegendLayer::saveSelectionAsShapefile()
saveAsShapefileGeneral( TRUE );
}

void QgsLegendLayer::saveAsVectorFile()
{
saveAsVectorFileGeneral( false );
}

void QgsLegendLayer::saveSelectionAsVectorFile()
{
saveAsVectorFileGeneral( true );
}

//////////

void QgsLegendLayer::setVisible( bool visible )
Expand Down Expand Up @@ -492,7 +502,81 @@ void QgsLegendLayer::showInOverview()
legend()->updateOverview();
}

void QgsLegendLayer::saveAsVectorFileGeneral( bool saveOnlySelection )
{
if ( mLyr.layer()->type() != QgsMapLayer::VectorLayer )
return;

QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( mLyr.layer() );

//get output name and format
QSettings settings;
QString filter = QString( "Shapefiles (*.shp)" );
QString dirName = settings.value( "/UI/lastVectorfileDir", "." ).toString();
QString filterString = QgsVectorFileWriter::fileFilterString();
QString selectedFilter = settings.value( "/UI/lastVectorFilter", "[OGR] ESRI Shapefiles (*.shp *.SHP)" ).toString();
QString outputFile = QFileDialog::getSaveFileName( 0, tr( "Save layer as..." ), dirName, filterString, &selectedFilter );
if ( outputFile.isNull() )
{
return; //cancelled
}

settings.setValue( "/UI/lastVectorfileDir", QFileInfo( outputFile ).absolutePath() );
settings.setValue( "/UI/lastVectorFilter", selectedFilter );

QMap< QString, QString> filterDriverMap = QgsVectorFileWriter::supportedFiltersAndFormats();
QMap< QString, QString>::const_iterator it = filterDriverMap.find( selectedFilter + ";;" );
if ( it == filterDriverMap.constEnd() )
{
return; //unknown format
}

QString driverKey = *it;

//output CRS
QgsCoordinateReferenceSystem destCRS = vlayer->srs();
// Find out if we have projections enabled or not
if ( QgisApp::instance()->mapCanvas()->mapRenderer()->hasCrsTransformEnabled() )
{
destCRS = QgisApp::instance()->mapCanvas()->mapRenderer()->destinationSrs();
}

QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector();
mySelector->setSelectedCrsId( destCRS.srsid() );
mySelector->setMessage( tr( "Select the coordinate reference system for the saved shapefile. "
"The data points will be transformed from the layer coordinate reference system." ) );

if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
destCRS = srs;
// destCRS->createFromId(mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId)
}
else
{
// Aborted CS selection, don't save.
delete mySelector;
return;
}
delete mySelector;

// overwrite the file - user will already have been prompted
// to verify they want to overwrite by the file dialog above
// might not even exists in the given case.
if ( driverKey == "ESRI Shapefile" )
{
// add the extension if not present
if ( !outputFile.endsWith( ".shp", Qt::CaseInsensitive ) )
{
outputFile += ".shp";
}
QgsVectorFileWriter::deleteShapeFile( outputFile );
}

QString errorMessage;
QgsVectorFileWriter::WriterError error;
error = QgsVectorFileWriter::writeAsVectorFormat( vlayer, outputFile, "utf-8", &destCRS, driverKey, saveOnlySelection, &errorMessage );
}


void QgsLegendLayer::saveAsShapefileGeneral( bool saveOnlySelection )
Expand Down
5 changes: 5 additions & 0 deletions src/app/legend/qgslegendlayer.h
Expand Up @@ -87,6 +87,9 @@ class QgsLegendLayer : public QgsLegendItem
void saveAsShapefile();
void saveSelectionAsShapefile();

void saveAsVectorFile();
void saveSelectionAsVectorFile();

/**update the layer's icon to show whether is in editing mode or in overview */
void updateIcon();

Expand All @@ -111,6 +114,8 @@ class QgsLegendLayer : public QgsLegendItem
/**Save as shapefile (called from saveAsShapefile and saveSelectionAsShapefile)*/
void saveAsShapefileGeneral( bool saveOnlySelection );

void saveAsVectorFileGeneral( bool saveOnlySelection );

private:
/** Helper method to make the font bold from all ctors.
* Not to be confused with setFont() which is inherited
Expand Down

0 comments on commit d14a5e6

Please sign in to comment.