Skip to content

Commit

Permalink
[FEATURE] allow specification of CRS when creating shape files (also f…
Browse files Browse the repository at this point in the history
…ixes #2123)

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12259 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 25, 2009
1 parent 11d80a3 commit 5d40608
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 184 deletions.
4 changes: 2 additions & 2 deletions i18n/qgis_de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14768,8 +14768,8 @@ p, li { white-space: pre-wrap; }
</message>
<message>
<location filename="../src/ui/qgsnewvectorlayerdialogbase.ui" line="245"/>
<source>Removed selected attribute</source>
<translation>Gewähltes Attribute gelöscht</translation>
<source>Remove selected attribute</source>
<translation type="unfinished">Gewähltes Attribut löschen</translation>
</message>
</context>
<context>
Expand Down
30 changes: 8 additions & 22 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1802,11 +1802,11 @@ void QgisApp::setupConnections()
connect( mActionRedo, SIGNAL( triggered() ), mUndoWidget, SLOT( redo() ) );
connect( mUndoWidget, SIGNAL( undoStackChanged() ), this, SLOT( updateUndoActions() ) );

// Monitor change of project path
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ),
this, SLOT( projectChanged( const QDomDocument & ) ) );
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ),
this, SLOT( projectChanged( QDomDocument & ) ) );

}

void QgisApp::createCanvas()
Expand Down Expand Up @@ -3209,16 +3209,15 @@ void QgisApp::newVectorLayer()
return;
}

QGis::WkbType geometrytype;
QString fileformat;

QgsNewVectorLayerDialog geomDialog( this );
if ( geomDialog.exec() == QDialog::Rejected )
{
return;
}
geometrytype = geomDialog.selectedType();
fileformat = geomDialog.selectedFileFormat();

QGis::WkbType geometrytype = geomDialog.selectedType();
QString fileformat = geomDialog.selectedFileFormat();
int crsId = geomDialog.selectedCrsId();
QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );

std::list<std::pair<QString, QString> > attributes;
Expand Down Expand Up @@ -3286,27 +3285,14 @@ void QgisApp::newVectorLayer()
QgsDebugMsg( "ogr provider loaded" );

typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
const std::list<std::pair<QString, QString> >& );
const std::list<std::pair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
if ( createEmptyDataSource )
{
#if 0
if ( geometrytype == QGis::WKBPoint )
{
createEmptyDataSource( fileName, fileformat, enc, QGis::WKBPoint, attributes );
}
else if ( geometrytype == QGis::WKBLineString )
{
createEmptyDataSource( fileName, fileformat, enc, QGis::WKBLineString, attributes );
}
else if ( geometrytype == QGis::WKBPolygon )
{
createEmptyDataSource( fileName, fileformat, enc, QGis::WKBPolygon, attributes );
}
#endif
if ( geometrytype != QGis::WKBUnknown )
{
createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes );
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs );
}
else
{
Expand Down
31 changes: 31 additions & 0 deletions src/app/qgsnewvectorlayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
#include "qgsapplication.h"
#include "qgisapp.h" // <- for theme icons
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsgenericprojectionselector.h"
#include <QPushButton>


QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl )
: QDialog( parent, fl )
{
Expand All @@ -48,6 +51,12 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl
}
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
mOkButton->setEnabled( false );

QgsCoordinateReferenceSystem srs;
srs.validate();

mCrsId = srs.srsid();
leSpatialRefSys->setText( srs.toProj4() );
}

QgsNewVectorLayerDialog::~QgsNewVectorLayerDialog()
Expand Down Expand Up @@ -76,6 +85,11 @@ QGis::WkbType QgsNewVectorLayerDialog::selectedType() const
return QGis::WKBUnknown;
}

int QgsNewVectorLayerDialog::selectedCrsId() const
{
return mCrsId;
}

void QgsNewVectorLayerDialog::on_mAddAttributeButton_clicked()
{
QString myName = mNameEdit->text();
Expand All @@ -100,6 +114,23 @@ void QgsNewVectorLayerDialog::on_mRemoveAttributeButton_clicked()
}
}

void QgsNewVectorLayerDialog::on_pbnChangeSpatialRefSys_clicked()
{
QgsGenericProjectionSelector *mySelector = new QgsGenericProjectionSelector( this );
mySelector->setMessage();
mySelector->setSelectedCrsId( pbnChangeSpatialRefSys->text().toInt() );
if ( mySelector->exec() )
{
mCrsId = mySelector->selectedCrsId();
leSpatialRefSys->setText( mySelector->selectedProj4String() );
}
else
{
QApplication::restoreOverrideCursor();
}
delete mySelector;
}

void QgsNewVectorLayerDialog::attributes( std::list<std::pair<QString, QString> >& at ) const
{
QTreeWidgetItemIterator it( mAttributeView );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsnewvectorlayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ class QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVectorLayerDial
void attributes( std::list<std::pair<QString, QString> >& at ) const;
/**Returns the file format for storage*/
QString selectedFileFormat() const;
/**Returns the selected crs id*/
int selectedCrsId() const;

protected slots:
void on_mAddAttributeButton_clicked();
void on_mRemoveAttributeButton_clicked();
void on_mTypeBox_currentIndexChanged( int index );
void on_pbnChangeSpatialRefSys_clicked();
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }

private:
QPushButton *mOkButton;
int mCrsId;
};

#endif //qgsnewvectorlayerdialog_H
41 changes: 18 additions & 23 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,27 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
}

// datasource created, now create the output layer
QString layerName = shapefileName.left( shapefileName.indexOf( ".shp" ) );
QString layerName = shapefileName.left( shapefileName.lastIndexOf( ".shp", Qt::CaseInsensitive ) );
OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType>( geometryType );
mLayer = OGR_DS_CreateLayer( mDS, QFile::encodeName( layerName ).data(), ogrRef, wkbType, NULL );

if ( srs )
{
if ( shapefileName != layerName )
{
QFile prjFile( layerName + ".prj" );
if ( prjFile.open( QIODevice::WriteOnly ) )
{
QTextStream prjStream( &prjFile );
prjStream << srs->toWkt().toLocal8Bit().data() << endl;
prjFile.close();
}
else
{
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
}
}

OSRDestroySpatialReference( ogrRef );
}

Expand Down Expand Up @@ -344,8 +359,8 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
// This means we shouldn't transform, use source CRS as output (if defined)
outputCRS = &layer->srs();
}
QgsVectorFileWriter* writer = new QgsVectorFileWriter( shapefileName,
fileEncoding, provider->fields(), provider->geometryType(), outputCRS );
QgsVectorFileWriter* writer =
new QgsVectorFileWriter( shapefileName, fileEncoding, provider->fields(), provider->geometryType(), outputCRS );

// check whether file creation was successful
WriterError err = writer->hasError();
Expand Down Expand Up @@ -394,26 +409,6 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
delete ct;
}

// Ohh, a great Hack-fest starts!
// Overwrite the .prj file created by QGsVectorFileWrite().
// This might break progrmas that relies on ESRI style .prf-files.
// The 'CT-params' (e.g. +towgs84) does not get stripped in this way
QRegExp regExp( ".shp$" );
QString prjName = shapefileName;
prjName.replace( regExp, QString( "" ) );
prjName.append( QString( ".prj" ) );
QFile prjFile( prjName );

if ( !prjFile.open( QIODevice::WriteOnly ) )
{
QgsDebugMsg( "Couldn't open file " + prjName );
return NoError; // For now
}

QTextStream prjStream( & prjFile );
prjStream << destCRS->toWkt() << endl;
prjFile.close();

return NoError;
}

Expand Down
30 changes: 28 additions & 2 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,8 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
const QString& format,
const QString& encoding,
QGis::WkbType vectortype,
const std::list<std::pair<QString, QString> >& attributes )
const std::list<std::pair<QString, QString> > &attributes,
const QgsCoordinateReferenceSystem *srs = NULL )
{
QgsDebugMsg( QString( "Creating empty vector layer with format: %1" ).arg( format ) );
OGRSFDriverH driver;
Expand All @@ -1473,8 +1474,17 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,

//consider spatial reference system
OGRSpatialReferenceH reference = NULL;

QgsCoordinateReferenceSystem mySpatialRefSys;
mySpatialRefSys.validate();
if ( srs )
{
mySpatialRefSys = *srs;
}
else
{
mySpatialRefSys.validate();
}

QString myWkt = mySpatialRefSys.toWkt();

if ( !myWkt.isNull() && myWkt.length() != 0 )
Expand Down Expand Up @@ -1576,6 +1586,22 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,

OGR_DS_Destroy( dataSource );

if ( uri.endsWith( ".shp", Qt::CaseInsensitive ) )
{
QString layerName = uri.left( uri.length() - 4 );
QFile prjFile( layerName + ".prj" );
if ( prjFile.open( QIODevice::WriteOnly ) )
{
QTextStream prjStream( &prjFile );
prjStream << myWkt.toLocal8Bit().data() << endl;
prjFile.close();
}
else
{
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
}
}

QgsDebugMsg( QString( "GDAL Version number %1" ).arg( GDAL_VERSION_NUM ) );
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1310
if ( reference )
Expand Down
Loading

0 comments on commit 5d40608

Please sign in to comment.