void QgsVectorLayerSaveAsDialog::on_browseCRS_clicked()
{
  QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector();
  if( mCRS >= 0 )
    mySelector->setSelectedCrsId( mCRS );
  mySelector->setMessage( tr( "Select the coordinate reference system for the vector file. "
                              "The data points will be transformed from the layer coordinate reference system." ) );

  if( mySelector->exec() )
  {
    QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
    mCRS = srs.srsid();
    leCRS->setText( srs.description() );
  }

  delete mySelector;
}
示例#2
0
void QgsVectorLayerProperties::on_pbnChangeSpatialRefSys_clicked()
{
  QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector( this );
  mySelector->setMessage();
  mySelector->setSelectedCrsId( layer->crs().srsid() );
  if ( mySelector->exec() )
  {
    QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
    layer->setCrs( srs );
  }
  else
  {
    QApplication::restoreOverrideCursor();
  }
  delete mySelector;

  leSpatialRefSys->setText( layer->crs().authid() + " - " + layer->crs().description() );
  leSpatialRefSys->setCursorPosition( 0 );
}
void QgsLegendLayerFile::saveAsShapefileGeneral( bool saveOnlySelection )
{
  QgsCoordinateReferenceSystem destCRS;

  if ( mLyr.layer()->type() != QgsMapLayer::VectorLayer )
    return;

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

  // get a name for the shapefile
  // Get a file to process, starting at the current directory
  QSettings settings;
  QString filter =  QString( "Shapefiles (*.shp)" );
  QString dirName = settings.value( "/UI/lastShapefileDir", "." ).toString();

  QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( 0,
      tr( "Save layer as..." ),
      dirName,
      filter,
      QString( "UTF-8" ) );
  openFileDialog->setAcceptMode( QFileDialog::AcceptSave );

  // allow for selection of more than one file
  //openFileDialog->setMode(QFileDialog::AnyFile);

  if ( openFileDialog->exec() != QDialog::Accepted )
    return;


  QString encoding = openFileDialog->encoding();
  QString shapefileName = openFileDialog->selectedFiles().first();
  settings.setValue( "/UI/lastShapefileDir", QFileInfo( shapefileName ).absolutePath() );


  if ( shapefileName.isNull() )
    return;

  // add the extension if not present
  if ( shapefileName.indexOf( ".shp" ) == -1 )
  {
    shapefileName += ".shp";
  }

  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." ) +
                          tr( "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
  if ( QFile::exists( shapefileName ) )
  {
    if ( !QgsVectorFileWriter::deleteShapeFile( shapefileName ) )
    {
      return;
    }
  }

  // ok if the file existed it should be deleted now so we can continue...
  QApplication::setOverrideCursor( Qt::WaitCursor );

  QgsVectorFileWriter::WriterError error;
  error = QgsVectorFileWriter::writeAsShapefile( vlayer, shapefileName, encoding, &destCRS, saveOnlySelection );

  QApplication::restoreOverrideCursor();

  switch ( error )
  {
    case QgsVectorFileWriter::NoError:
      QMessageBox::information( 0, tr( "Saving done" ), tr( "Export to Shapefile has been completed" ) );
      break;

    case QgsVectorFileWriter::ErrDriverNotFound:
      QMessageBox::warning( 0, tr( "Driver not found" ), tr( "ESRI Shapefile driver is not available" ) );
      break;

    case QgsVectorFileWriter::ErrCreateDataSource:
      QMessageBox::warning( 0, tr( "Error creating shapefile" ),
                            tr( "The shapefile could not be created (" ) + shapefileName + ")" );
      break;

    case QgsVectorFileWriter::ErrCreateLayer:
      QMessageBox::warning( 0, tr( "Error" ), tr( "Layer creation failed" ) );
      break;
    case QgsVectorFileWriter::ErrAttributeTypeUnsupported:
      QMessageBox::warning( 0, tr( "Error" ),
                            tr( "Layer attribute table contains unsupported datatype(s)" ) );
      break;
  }
}