|
41 | 41 | #include <cpl_error.h>
|
42 | 42 |
|
43 | 43 |
|
44 |
| -QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName, |
| 44 | +QgsVectorFileWriter::QgsVectorFileWriter( const QString& vectorFileName, |
45 | 45 | const QString& fileEncoding,
|
46 | 46 | const QgsFieldMap& fields,
|
47 | 47 | QGis::WkbType geometryType,
|
@@ -85,7 +85,7 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
|
85 | 85 | }
|
86 | 86 |
|
87 | 87 | // create the data source
|
88 |
| - mDS = OGR_Dr_CreateDataSource( poDriver, shapefileName.toLocal8Bit().data(), NULL ); |
| 88 | + mDS = OGR_Dr_CreateDataSource( poDriver, vectorFileName.toLocal8Bit().data(), NULL ); |
89 | 89 | if ( mDS == NULL )
|
90 | 90 | {
|
91 | 91 | mError = ErrCreateDataSource;
|
@@ -121,7 +121,7 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
|
121 | 121 | }
|
122 | 122 |
|
123 | 123 | // datasource created, now create the output layer
|
124 |
| - QString layerName = shapefileName.left( shapefileName.indexOf( ".shp", Qt::CaseInsensitive ) ); |
| 124 | + QString layerName = QFileInfo( vectorFileName ).baseName(); |
125 | 125 | OGRwkbGeometryType wkbType = static_cast<OGRwkbGeometryType>( geometryType );
|
126 | 126 | mLayer = OGR_DS_CreateLayer( mDS, QFile::encodeName( layerName ).data(), ogrRef, wkbType, NULL );
|
127 | 127 |
|
@@ -386,7 +386,8 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
|
386 | 386 | bool onlySelected,
|
387 | 387 | QString *errorMessage )
|
388 | 388 | {
|
389 |
| - |
| 389 | + return writeAsVectorFormat( layer, shapefileName, fileEncoding, destCRS, "ESRI Shapefile", onlySelected, errorMessage ); |
| 390 | +#if 0 |
390 | 391 | const QgsCoordinateReferenceSystem* outputCRS;
|
391 | 392 | QgsCoordinateTransform* ct = 0;
|
392 | 393 | int shallTransform = false;
|
@@ -470,6 +471,102 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
|
470 | 471 | delete ct;
|
471 | 472 | }
|
472 | 473 |
|
| 474 | + return NoError; |
| 475 | +#endif //0 |
| 476 | +} |
| 477 | + |
| 478 | +QgsVectorFileWriter::WriterError |
| 479 | +QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer* layer, |
| 480 | + const QString& fileName, |
| 481 | + const QString& fileEncoding, |
| 482 | + const QgsCoordinateReferenceSystem *destCRS, |
| 483 | + const QString& driverName, |
| 484 | + bool onlySelected, |
| 485 | + QString *errorMessage ) |
| 486 | +{ |
| 487 | + const QgsCoordinateReferenceSystem* outputCRS; |
| 488 | + QgsCoordinateTransform* ct = 0; |
| 489 | + int shallTransform = false; |
| 490 | + |
| 491 | + if ( destCRS && destCRS->isValid() ) |
| 492 | + { |
| 493 | + // This means we should transform |
| 494 | + outputCRS = destCRS; |
| 495 | + shallTransform = true; |
| 496 | + } |
| 497 | + else |
| 498 | + { |
| 499 | + // This means we shouldn't transform, use source CRS as output (if defined) |
| 500 | + outputCRS = &layer->srs(); |
| 501 | + } |
| 502 | + QgsVectorFileWriter* writer = |
| 503 | + new QgsVectorFileWriter( fileName, fileEncoding, layer->pendingFields(), layer->wkbType(), outputCRS, driverName ); |
| 504 | + |
| 505 | + // check whether file creation was successful |
| 506 | + WriterError err = writer->hasError(); |
| 507 | + if ( err != NoError ) |
| 508 | + { |
| 509 | + if ( errorMessage ) |
| 510 | + *errorMessage = writer->errorMessage(); |
| 511 | + delete writer; |
| 512 | + return err; |
| 513 | + } |
| 514 | + |
| 515 | + QgsAttributeList allAttr = layer->pendingAllAttributesList(); |
| 516 | + QgsFeature fet; |
| 517 | + |
| 518 | + layer->select( allAttr, QgsRectangle(), true ); |
| 519 | + |
| 520 | + const QgsFeatureIds& ids = layer->selectedFeaturesIds(); |
| 521 | + |
| 522 | + // Create our transform |
| 523 | + if ( destCRS ) |
| 524 | + { |
| 525 | + ct = new QgsCoordinateTransform( layer->srs(), *destCRS ); |
| 526 | + } |
| 527 | + |
| 528 | + // Check for failure |
| 529 | + if ( ct == NULL ) |
| 530 | + { |
| 531 | + shallTransform = false; |
| 532 | + } |
| 533 | + |
| 534 | + // write all features |
| 535 | + while ( layer->nextFeature( fet ) ) |
| 536 | + { |
| 537 | + if ( onlySelected && !ids.contains( fet.id() ) ) |
| 538 | + continue; |
| 539 | + |
| 540 | + if ( shallTransform ) |
| 541 | + { |
| 542 | + try |
| 543 | + { |
| 544 | + fet.geometry()->transform( *ct ); |
| 545 | + } |
| 546 | + catch ( QgsCsException &e ) |
| 547 | + { |
| 548 | + delete ct; |
| 549 | + delete writer; |
| 550 | + |
| 551 | + QString msg = QObject::tr( "Failed to transform a point while drawing a feature of type '%1'. Writing stopped. (Exception: %2)" ) |
| 552 | + .arg( fet.typeName() ).arg( e.what() ); |
| 553 | + QgsLogger::warning( msg ); |
| 554 | + if ( errorMessage ) |
| 555 | + *errorMessage = msg; |
| 556 | + |
| 557 | + return ErrProjection; |
| 558 | + } |
| 559 | + } |
| 560 | + writer->addFeature( fet ); |
| 561 | + } |
| 562 | + |
| 563 | + delete writer; |
| 564 | + |
| 565 | + if ( shallTransform ) |
| 566 | + { |
| 567 | + delete ct; |
| 568 | + } |
| 569 | + |
473 | 570 | return NoError;
|
474 | 571 | }
|
475 | 572 |
|
@@ -498,3 +595,157 @@ bool QgsVectorFileWriter::deleteShapeFile( QString theFileName )
|
498 | 595 |
|
499 | 596 | return ok;
|
500 | 597 | }
|
| 598 | + |
| 599 | +QMap< QString, QString> QgsVectorFileWriter::supportedFiltersAndFormats() |
| 600 | +{ |
| 601 | + QMap<QString, QString> resultMap; |
| 602 | + |
| 603 | + QgsApplication::registerOgrDrivers(); |
| 604 | + int const drvCount = OGRGetDriverCount(); |
| 605 | + |
| 606 | + QString drvName; |
| 607 | + QString filterString; |
| 608 | + for ( int i = 0; i < drvCount; ++i ) |
| 609 | + { |
| 610 | + OGRSFDriverH drv = OGRGetDriver( i ); |
| 611 | + if ( drv ) |
| 612 | + { |
| 613 | + drvName = OGR_Dr_GetName( drv ); |
| 614 | + if ( OGR_Dr_TestCapability( drv, ODrCCreateDataSource ) != 0 ) |
| 615 | + { |
| 616 | + //add driver name and filter to map |
| 617 | + filterString = QgsVectorFileWriter::filterForDriver( drvName ); |
| 618 | + if ( !filterString.isEmpty() ) |
| 619 | + { |
| 620 | + resultMap.insert( filterString, drvName ); |
| 621 | + } |
| 622 | + } |
| 623 | + } |
| 624 | + } |
| 625 | + |
| 626 | + return resultMap; |
| 627 | +} |
| 628 | + |
| 629 | +QString QgsVectorFileWriter::fileFilterString() |
| 630 | +{ |
| 631 | + QString filterString; |
| 632 | + QMap< QString, QString> driverFormatMap = QgsVectorFileWriter::supportedFiltersAndFormats(); |
| 633 | + QMap< QString, QString>::const_iterator it = driverFormatMap.constBegin(); |
| 634 | + for ( ; it != driverFormatMap.constEnd(); ++it ) |
| 635 | + { |
| 636 | + filterString += it.key(); |
| 637 | + } |
| 638 | + return filterString; |
| 639 | +} |
| 640 | + |
| 641 | +QString QgsVectorFileWriter::filterForDriver( const QString& driverName ) |
| 642 | +{ |
| 643 | + QString longName; |
| 644 | + QString glob; |
| 645 | + |
| 646 | + if ( driverName.startsWith( "AVCE00" ) ) |
| 647 | + { |
| 648 | + longName = "Arc/Info ASCII Coverage"; |
| 649 | + glob = "*.e00"; |
| 650 | + } |
| 651 | + else if ( driverName.startsWith( "BNA" ) ) |
| 652 | + { |
| 653 | + longName = "Atlas BNA"; |
| 654 | + glob = "*.bna"; |
| 655 | + } |
| 656 | + else if ( driverName.startsWith( "CSV" ) ) |
| 657 | + { |
| 658 | + longName = "Comma Separated Value"; |
| 659 | + glob = "*.csv"; |
| 660 | + } |
| 661 | + else if ( driverName.startsWith( "ESRI" ) ) |
| 662 | + { |
| 663 | + longName = "ESRI Shapefiles"; |
| 664 | + glob = "*.shp"; |
| 665 | + } |
| 666 | + else if ( driverName.startsWith( "FMEObjects Gateway" ) ) |
| 667 | + { |
| 668 | + longName = "FMEObjects Gateway"; |
| 669 | + glob = "*.fdd"; |
| 670 | + } |
| 671 | + else if ( driverName.startsWith( "GeoJSON" ) ) |
| 672 | + { |
| 673 | + longName = "GeoJSON"; |
| 674 | + glob = "*.geojson"; |
| 675 | + } |
| 676 | + else if ( driverName.startsWith( "GeoRSS" ) ) |
| 677 | + { |
| 678 | + longName = "GeoRSS"; |
| 679 | + glob = "*.xml"; |
| 680 | + } |
| 681 | + else if ( driverName.startsWith( "GML" ) ) |
| 682 | + { |
| 683 | + longName = "Geography Markup Language"; |
| 684 | + glob = "*.gml"; |
| 685 | + } |
| 686 | + else if ( driverName.startsWith( "GMT" ) ) |
| 687 | + { |
| 688 | + longName = "GMT"; |
| 689 | + glob = "*.gmt"; |
| 690 | + } |
| 691 | + else if ( driverName.startsWith( "GPX" ) ) |
| 692 | + { |
| 693 | + longName = "GPX"; |
| 694 | + glob = "*.gpx"; |
| 695 | + } |
| 696 | + else if ( driverName.startsWith( "Interlis 1" ) ) |
| 697 | + { |
| 698 | + longName = "INTERLIS 1"; |
| 699 | + glob = "*.itf *.xml *.ili"; |
| 700 | + } |
| 701 | + else if ( driverName.startsWith( "Interlis 2" ) ) |
| 702 | + { |
| 703 | + longName = "INTERLIS 2"; |
| 704 | + glob = "*.itf *.xml *.ili"; |
| 705 | + } |
| 706 | + else if ( driverName.startsWith( "KML" ) ) |
| 707 | + { |
| 708 | + longName = "KML"; |
| 709 | + glob = "*.kml" ; |
| 710 | + } |
| 711 | + else if ( driverName.startsWith( "MapInfo File" ) ) |
| 712 | + { |
| 713 | + longName = "Mapinfo File"; |
| 714 | + glob = "*.mif *.tab"; |
| 715 | + } |
| 716 | + else if ( driverName.startsWith( "DGN" ) ) |
| 717 | + { |
| 718 | + longName = "Microstation DGN"; |
| 719 | + glob = "*.dgn"; |
| 720 | + } |
| 721 | + else if ( driverName.startsWith( "S57" ) ) |
| 722 | + { |
| 723 | + longName = "S-57 Base file"; |
| 724 | + glob = "*.000"; |
| 725 | + } |
| 726 | + else if ( driverName.startsWith( "SDTS" ) ) |
| 727 | + { |
| 728 | + longName = "Spatial Data Transfer Standard"; |
| 729 | + glob = "*catd.ddf"; |
| 730 | + } |
| 731 | + else if ( driverName.startsWith( "SQLite" ) ) |
| 732 | + { |
| 733 | + longName = "SQLite"; |
| 734 | + glob = "*.sqlite"; |
| 735 | + } |
| 736 | + else if ( driverName.startsWith( "VRT" ) ) |
| 737 | + { |
| 738 | + longName = "VRT - Virtual Datasource "; |
| 739 | + glob = "*.vrt"; |
| 740 | + } |
| 741 | + else if ( driverName.startsWith( "XPlane" ) ) |
| 742 | + { |
| 743 | + longName = "X-Plane/Flighgear"; |
| 744 | + glob = "apt.dat nav.dat fix.dat awy.dat"; |
| 745 | + } |
| 746 | + else |
| 747 | + { |
| 748 | + return QString(); |
| 749 | + } |
| 750 | + return "[OGR] " + longName + " (" + glob.toLower() + " " + glob.toUpper() + ");;"; |
| 751 | +} |
0 commit comments