|
21 | 21 | #include <limits>
|
22 | 22 |
|
23 | 23 | #include "qgsvectordataprovider.h"
|
| 24 | +#include "qgscircularstringv2.h" |
| 25 | +#include "qgscompoundcurvev2.h" |
24 | 26 | #include "qgsfeature.h"
|
25 | 27 | #include "qgsfeatureiterator.h"
|
26 | 28 | #include "qgsfeaturerequest.h"
|
27 | 29 | #include "qgsfield.h"
|
| 30 | +#include "qgsgeometry.h" |
| 31 | +#include "qgsgeometrycollectionv2.h" |
| 32 | +#include "qgsgeometryfactory.h" |
28 | 33 | #include "qgslogger.h"
|
29 | 34 | #include "qgsmessagelog.h"
|
30 | 35 |
|
@@ -582,4 +587,103 @@ QSet<QString> QgsVectorDataProvider::layerDependencies() const
|
582 | 587 | return QSet<QString>();
|
583 | 588 | }
|
584 | 589 |
|
| 590 | +QgsGeometry* QgsVectorDataProvider::convertToProviderType( const QgsGeometry& geom ) const |
| 591 | +{ |
| 592 | + if ( geom.isEmpty() ) |
| 593 | + { |
| 594 | + return nullptr; |
| 595 | + } |
| 596 | + |
| 597 | + QgsAbstractGeometryV2* geometry = geom.geometry(); |
| 598 | + if ( !geometry ) |
| 599 | + { |
| 600 | + return nullptr; |
| 601 | + } |
| 602 | + |
| 603 | + QgsWKBTypes::Type providerGeomType = QgsWKBTypes::Type( geometryType() ); |
| 604 | + |
| 605 | + //geom is already in the provider geometry type |
| 606 | + if ( geometry->wkbType() == providerGeomType ) |
| 607 | + { |
| 608 | + return nullptr; |
| 609 | + } |
| 610 | + |
| 611 | + QgsAbstractGeometryV2* outputGeom = nullptr; |
| 612 | + |
| 613 | + //convert compoundcurve to circularstring (possible if compoundcurve consists of one circular string) |
| 614 | + if ( QgsWKBTypes::flatType( providerGeomType ) == QgsWKBTypes::CircularString && QgsWKBTypes::flatType( geometry->wkbType() ) == QgsWKBTypes::CompoundCurve ) |
| 615 | + { |
| 616 | + QgsCompoundCurveV2* compoundCurve = static_cast<QgsCompoundCurveV2*>( geometry ); |
| 617 | + if ( compoundCurve ) |
| 618 | + { |
| 619 | + if ( compoundCurve->nCurves() == 1 ) |
| 620 | + { |
| 621 | + const QgsCircularStringV2* circularString = dynamic_cast<const QgsCircularStringV2*>( compoundCurve->curveAt( 0 ) ); |
| 622 | + if ( circularString ) |
| 623 | + { |
| 624 | + outputGeom = circularString->clone(); |
| 625 | + } |
| 626 | + } |
| 627 | + } |
| 628 | + } |
| 629 | + |
| 630 | + //convert to multitype if necessary |
| 631 | + if ( QgsWKBTypes::isMultiType( providerGeomType ) && !QgsWKBTypes::isMultiType( geometry->wkbType() ) ) |
| 632 | + { |
| 633 | + outputGeom = QgsGeometryFactory::geomFromWkbType( providerGeomType ); |
| 634 | + QgsGeometryCollectionV2* geomCollection = dynamic_cast<QgsGeometryCollectionV2*>( outputGeom ); |
| 635 | + if ( geomCollection ) |
| 636 | + { |
| 637 | + geomCollection->addGeometry( geometry->clone() ); |
| 638 | + } |
| 639 | + } |
| 640 | + |
| 641 | + //convert to curved type if necessary |
| 642 | + if ( !QgsWKBTypes::isCurvedType( geometry->wkbType() ) && QgsWKBTypes::isCurvedType( providerGeomType ) ) |
| 643 | + { |
| 644 | + QgsAbstractGeometryV2* curveGeom = outputGeom ? outputGeom->toCurveType() : geometry->toCurveType(); |
| 645 | + if ( curveGeom ) |
| 646 | + { |
| 647 | + delete outputGeom; |
| 648 | + outputGeom = curveGeom; |
| 649 | + } |
| 650 | + } |
| 651 | + |
| 652 | + //convert to linear type from curved type |
| 653 | + if ( QgsWKBTypes::isCurvedType( geometry->wkbType() ) && !QgsWKBTypes::isCurvedType( providerGeomType ) ) |
| 654 | + { |
| 655 | + QgsAbstractGeometryV2* segmentizedGeom = nullptr; |
| 656 | + segmentizedGeom = outputGeom ? outputGeom->segmentize() : geometry->segmentize(); |
| 657 | + if ( segmentizedGeom ) |
| 658 | + { |
| 659 | + delete outputGeom; |
| 660 | + outputGeom = segmentizedGeom; |
| 661 | + } |
| 662 | + } |
| 663 | + |
| 664 | + //set z/m types |
| 665 | + if ( QgsWKBTypes::hasZ( providerGeomType ) ) |
| 666 | + { |
| 667 | + if ( !outputGeom ) |
| 668 | + { |
| 669 | + outputGeom = geometry->clone(); |
| 670 | + } |
| 671 | + outputGeom->addZValue(); |
| 672 | + } |
| 673 | + if ( QgsWKBTypes::hasM( providerGeomType ) ) |
| 674 | + { |
| 675 | + if ( !outputGeom ) |
| 676 | + { |
| 677 | + outputGeom = geometry->clone(); |
| 678 | + } |
| 679 | + outputGeom->addMValue(); |
| 680 | + } |
| 681 | + |
| 682 | + if ( outputGeom ) |
| 683 | + { |
| 684 | + return new QgsGeometry( outputGeom ); |
| 685 | + } |
| 686 | + return nullptr; |
| 687 | +} |
| 688 | + |
585 | 689 | QStringList QgsVectorDataProvider::smEncodings;
|
0 commit comments