Skip to content

Commit 83d6e0c

Browse files
author
jef
committed
[FEATURE] allow specification of CRS when creating shape files (also fixes #2123)
git-svn-id: http://svn.osgeo.org/qgis/trunk@12259 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent cbceb45 commit 83d6e0c

File tree

7 files changed

+258
-184
lines changed

7 files changed

+258
-184
lines changed

i18n/qgis_de.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14768,8 +14768,8 @@ p, li { white-space: pre-wrap; }
1476814768
</message>
1476914769
<message>
1477014770
<location filename="../src/ui/qgsnewvectorlayerdialogbase.ui" line="245"/>
14771-
<source>Removed selected attribute</source>
14772-
<translation>Gewähltes Attribute gelöscht</translation>
14771+
<source>Remove selected attribute</source>
14772+
<translation type="unfinished">Gewähltes Attribut löschen</translation>
1477314773
</message>
1477414774
</context>
1477514775
<context>

src/app/qgisapp.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,11 +1802,11 @@ void QgisApp::setupConnections()
18021802
connect( mActionRedo, SIGNAL( triggered() ), mUndoWidget, SLOT( redo() ) );
18031803
connect( mUndoWidget, SIGNAL( undoStackChanged() ), this, SLOT( updateUndoActions() ) );
18041804

1805+
// Monitor change of project path
18051806
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ),
18061807
this, SLOT( projectChanged( const QDomDocument & ) ) );
18071808
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ),
18081809
this, SLOT( projectChanged( QDomDocument & ) ) );
1809-
18101810
}
18111811

18121812
void QgisApp::createCanvas()
@@ -3209,16 +3209,15 @@ void QgisApp::newVectorLayer()
32093209
return;
32103210
}
32113211

3212-
QGis::WkbType geometrytype;
3213-
QString fileformat;
3214-
32153212
QgsNewVectorLayerDialog geomDialog( this );
32163213
if ( geomDialog.exec() == QDialog::Rejected )
32173214
{
32183215
return;
32193216
}
3220-
geometrytype = geomDialog.selectedType();
3221-
fileformat = geomDialog.selectedFileFormat();
3217+
3218+
QGis::WkbType geometrytype = geomDialog.selectedType();
3219+
QString fileformat = geomDialog.selectedFileFormat();
3220+
int crsId = geomDialog.selectedCrsId();
32223221
QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );
32233222

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

32883287
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
3289-
const std::list<std::pair<QString, QString> >& );
3288+
const std::list<std::pair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
32903289
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
32913290
if ( createEmptyDataSource )
32923291
{
3293-
#if 0
3294-
if ( geometrytype == QGis::WKBPoint )
3295-
{
3296-
createEmptyDataSource( fileName, fileformat, enc, QGis::WKBPoint, attributes );
3297-
}
3298-
else if ( geometrytype == QGis::WKBLineString )
3299-
{
3300-
createEmptyDataSource( fileName, fileformat, enc, QGis::WKBLineString, attributes );
3301-
}
3302-
else if ( geometrytype == QGis::WKBPolygon )
3303-
{
3304-
createEmptyDataSource( fileName, fileformat, enc, QGis::WKBPolygon, attributes );
3305-
}
3306-
#endif
33073292
if ( geometrytype != QGis::WKBUnknown )
33083293
{
3309-
createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes );
3294+
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
3295+
createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs );
33103296
}
33113297
else
33123298
{

src/app/qgsnewvectorlayerdialog.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
#include "qgsapplication.h"
2121
#include "qgisapp.h" // <- for theme icons
2222
#include "qgslogger.h"
23+
#include "qgscoordinatereferencesystem.h"
24+
#include "qgsgenericprojectionselector.h"
2325
#include <QPushButton>
2426

27+
2528
QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl )
2629
: QDialog( parent, fl )
2730
{
@@ -48,6 +51,12 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl
4851
}
4952
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
5053
mOkButton->setEnabled( false );
54+
55+
QgsCoordinateReferenceSystem srs;
56+
srs.validate();
57+
58+
mCrsId = srs.srsid();
59+
leSpatialRefSys->setText( srs.toProj4() );
5160
}
5261

5362
QgsNewVectorLayerDialog::~QgsNewVectorLayerDialog()
@@ -76,6 +85,11 @@ QGis::WkbType QgsNewVectorLayerDialog::selectedType() const
7685
return QGis::WKBUnknown;
7786
}
7887

88+
int QgsNewVectorLayerDialog::selectedCrsId() const
89+
{
90+
return mCrsId;
91+
}
92+
7993
void QgsNewVectorLayerDialog::on_mAddAttributeButton_clicked()
8094
{
8195
QString myName = mNameEdit->text();
@@ -100,6 +114,23 @@ void QgsNewVectorLayerDialog::on_mRemoveAttributeButton_clicked()
100114
}
101115
}
102116

117+
void QgsNewVectorLayerDialog::on_pbnChangeSpatialRefSys_clicked()
118+
{
119+
QgsGenericProjectionSelector *mySelector = new QgsGenericProjectionSelector( this );
120+
mySelector->setMessage();
121+
mySelector->setSelectedCrsId( pbnChangeSpatialRefSys->text().toInt() );
122+
if ( mySelector->exec() )
123+
{
124+
mCrsId = mySelector->selectedCrsId();
125+
leSpatialRefSys->setText( mySelector->selectedProj4String() );
126+
}
127+
else
128+
{
129+
QApplication::restoreOverrideCursor();
130+
}
131+
delete mySelector;
132+
}
133+
103134
void QgsNewVectorLayerDialog::attributes( std::list<std::pair<QString, QString> >& at ) const
104135
{
105136
QTreeWidgetItemIterator it( mAttributeView );

src/app/qgsnewvectorlayerdialog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ class QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVectorLayerDial
3737
void attributes( std::list<std::pair<QString, QString> >& at ) const;
3838
/**Returns the file format for storage*/
3939
QString selectedFileFormat() const;
40+
/**Returns the selected crs id*/
41+
int selectedCrsId() const;
4042

4143
protected slots:
4244
void on_mAddAttributeButton_clicked();
4345
void on_mRemoveAttributeButton_clicked();
4446
void on_mTypeBox_currentIndexChanged( int index );
47+
void on_pbnChangeSpatialRefSys_clicked();
4548
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
4649

4750
private:
4851
QPushButton *mOkButton;
52+
int mCrsId;
4953
};
5054

5155
#endif //qgsnewvectorlayerdialog_H

src/core/qgsvectorfilewriter.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,27 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
9595
}
9696

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

102102
if ( srs )
103103
{
104+
if ( shapefileName != layerName )
105+
{
106+
QFile prjFile( layerName + ".prj" );
107+
if ( prjFile.open( QIODevice::WriteOnly ) )
108+
{
109+
QTextStream prjStream( &prjFile );
110+
prjStream << srs->toWkt().toLocal8Bit().data() << endl;
111+
prjFile.close();
112+
}
113+
else
114+
{
115+
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
116+
}
117+
}
118+
104119
OSRDestroySpatialReference( ogrRef );
105120
}
106121

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

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

397-
// Ohh, a great Hack-fest starts!
398-
// Overwrite the .prj file created by QGsVectorFileWrite().
399-
// This might break progrmas that relies on ESRI style .prf-files.
400-
// The 'CT-params' (e.g. +towgs84) does not get stripped in this way
401-
QRegExp regExp( ".shp$" );
402-
QString prjName = shapefileName;
403-
prjName.replace( regExp, QString( "" ) );
404-
prjName.append( QString( ".prj" ) );
405-
QFile prjFile( prjName );
406-
407-
if ( !prjFile.open( QIODevice::WriteOnly ) )
408-
{
409-
QgsDebugMsg( "Couldn't open file " + prjName );
410-
return NoError; // For now
411-
}
412-
413-
QTextStream prjStream( & prjFile );
414-
prjStream << destCRS->toWkt() << endl;
415-
prjFile.close();
416-
417412
return NoError;
418413
}
419414

src/providers/ogr/qgsogrprovider.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,8 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
14531453
const QString& format,
14541454
const QString& encoding,
14551455
QGis::WkbType vectortype,
1456-
const std::list<std::pair<QString, QString> >& attributes )
1456+
const std::list<std::pair<QString, QString> > &attributes,
1457+
const QgsCoordinateReferenceSystem *srs = NULL )
14571458
{
14581459
QgsDebugMsg( QString( "Creating empty vector layer with format: %1" ).arg( format ) );
14591460
OGRSFDriverH driver;
@@ -1473,8 +1474,17 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
14731474

14741475
//consider spatial reference system
14751476
OGRSpatialReferenceH reference = NULL;
1477+
14761478
QgsCoordinateReferenceSystem mySpatialRefSys;
1477-
mySpatialRefSys.validate();
1479+
if ( srs )
1480+
{
1481+
mySpatialRefSys = *srs;
1482+
}
1483+
else
1484+
{
1485+
mySpatialRefSys.validate();
1486+
}
1487+
14781488
QString myWkt = mySpatialRefSys.toWkt();
14791489

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

15771587
OGR_DS_Destroy( dataSource );
15781588

1589+
if ( uri.endsWith( ".shp", Qt::CaseInsensitive ) )
1590+
{
1591+
QString layerName = uri.left( uri.length() - 4 );
1592+
QFile prjFile( layerName + ".prj" );
1593+
if ( prjFile.open( QIODevice::WriteOnly ) )
1594+
{
1595+
QTextStream prjStream( &prjFile );
1596+
prjStream << myWkt.toLocal8Bit().data() << endl;
1597+
prjFile.close();
1598+
}
1599+
else
1600+
{
1601+
QgsDebugMsg( "Couldn't open file " + layerName + ".prj" );
1602+
}
1603+
}
1604+
15791605
QgsDebugMsg( QString( "GDAL Version number %1" ).arg( GDAL_VERSION_NUM ) );
15801606
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1310
15811607
if ( reference )

0 commit comments

Comments
 (0)