@@ -649,13 +649,6 @@ bool QgsGeometry::deleteVertexAt(QgsGeometryVertexIndex atVertex)
649649bool QgsGeometry::vertexAt (double &x, double &y,
650650 QgsGeometryVertexIndex atVertex) const
651651{
652- #ifdef QGISDEBUG
653- std::cout << " QgsGeometry::vertexAt: Entered with "
654- // << "atVertex " << atVertex << ", atRing " << atRing << ", atItem"
655- // << " " << atItem
656- << " ." << std::endl;
657- #endif
658-
659652 if (mGeos )// try to find the vertex from the Geos geometry (it present)
660653 {
661654 geos::CoordinateSequence* cs = mGeos ->getCoordinates ();
@@ -672,59 +665,50 @@ bool QgsGeometry::vertexAt(double &x, double &y,
672665 else if (mGeometry )
673666 {
674667 int wkbType;
668+ unsigned char * ptr;
675669
676670 memcpy (&wkbType, (mGeometry +1 ), sizeof (int ));
677671 switch (wkbType)
678672 {
679673 case QGis::WKBPoint:
680674 {
681- // TODO
682- return FALSE ;
675+ if (atVertex.back () == 0 )
676+ {
677+ ptr = mGeometry +1 +sizeof (int );
678+ memcpy (&x, ptr, sizeof (double ));
679+ ptr += sizeof (double );
680+ memcpy (&y, ptr, sizeof (double ));
681+ }
682+ else
683+ {
684+ return FALSE ;
685+ }
683686 break ;
684687 }
685688 case QGis::WKBLineString:
686689 {
687- unsigned char *ptr;
688690 int *nPoints;
689-
690691 // get number of points in the line
691- ptr = mGeometry + 5 ; // now at mGeometry.numPoints
692+ ptr = mGeometry + 1 + sizeof ( int ) ; // now at mGeometry.numPoints
692693 nPoints = (int *) ptr;
693694
694- #ifdef QGISDEBUG
695- std::cout << " QgsGeometry::vertexAt: Number of points in WKBLineString is " << *nPoints
696- << " ." << std::endl;
697- #endif
698695 // return error if underflow
699- if (0 > atVertex.back ())
700- {
701- return FALSE ;
702- }
703- // return error if overflow
704- if (*nPoints <= atVertex.back ())
696+ if (0 > atVertex.back () || *nPoints <= atVertex.back ())
705697 {
706698 return FALSE ;
707699 }
708-
709700 // copy the vertex coordinates
710- ptr = mGeometry + 9 + (atVertex.back () * 16 );
711- memcpy (&x, ptr, 8 );
712- ptr += 8 ;
713- memcpy (&y, ptr, 8 );
714-
715- #ifdef QGISDEBUG
716- std::cout << " QgsGeometry::vertexAt: Point is (" << x << " , " << y << " )"
717- << " ." << std::endl;
718- #endif
719-
701+ ptr = mGeometry + 9 + (atVertex.back () * 2 *sizeof (double ));
702+ memcpy (&x, ptr, sizeof (double ));
703+ ptr += sizeof (double );
704+ memcpy (&y, ptr, sizeof (double ));
720705 break ;
721706 }
722707 case QGis::WKBPolygon:
723708 {
724- unsigned char *ptr;
725709 int *nRings;
726710 int *nPoints = 0 ;
727- ptr = mGeometry +5 ;
711+ ptr = mGeometry +1 + sizeof ( int ) ;
728712 nRings = (int *)ptr;
729713 ptr += sizeof (int );
730714 int pointindex = 0 ;
@@ -749,23 +733,74 @@ bool QgsGeometry::vertexAt(double &x, double &y,
749733 }
750734 case QGis::WKBMultiPoint:
751735 {
752- // TODO
753- return false ;
736+ ptr = mGeometry +1 +sizeof (int );
737+ int * nPoints = (int *)ptr;
738+ if (atVertex.back () < 0 || atVertex.back () >= *nPoints)
739+ {
740+ return false ;
741+ }
742+ ptr += atVertex.back ()*2 *sizeof (double );
743+ memcpy (&x, ptr, sizeof (double ));
744+ ptr += sizeof (double );
745+ memcpy (&y, ptr, sizeof (double ));
754746 break ;
755- }
756-
747+ }
757748 case QGis::WKBMultiLineString:
758749 {
759- // TODO
750+ ptr = mGeometry +1 +sizeof (int );
751+ int * nLines = (int *)ptr;
752+ int * nPoints = 0 ; // number of points in a line
753+ int pointindex = 0 ; // global point counter
754+ ptr += sizeof (int );
755+ for (int linenr = 0 ; linenr < *nLines; ++linenr)
756+ {
757+ nPoints = (int *)ptr;
758+ ptr += sizeof (int );
759+ for (int pointnr = 0 ; pointnr < *nPoints; ++pointnr)
760+ {
761+ if (pointindex == atVertex.back ())
762+ {
763+ memcpy (&x, ptr, sizeof (double ));
764+ ptr += sizeof (double );
765+ memcpy (&y, ptr, sizeof (double ));
766+ break ;
767+ }
768+ ptr += 2 *sizeof (double );
769+ ++pointindex;
770+ }
771+ }
760772 return false ;
761- break ;
762773 }
763-
764774 case QGis::WKBMultiPolygon:
765775 {
766- // TODO
776+ ptr = mGeometry +1 +sizeof (int );
777+ int * nRings = 0 ;// number of rings in a polygon
778+ int * nPoints = 0 ;// number of points in a ring
779+ int pointindex = 0 ; // global point counter
780+ int * nPolygons = (int *)ptr;
781+ ptr += sizeof (int );
782+ for (int polynr = 0 ; polynr < *nPolygons; ++polynr)
783+ {
784+ nRings = (int *)ptr;
785+ ptr += sizeof (int );
786+ for (int ringnr = 0 ; ringnr < *nRings; ++ringnr)
787+ {
788+ nPoints = (int *)ptr;
789+ for (int pointnr = 0 ; pointnr < *nPoints; ++pointnr)
790+ {
791+ if (pointindex == atVertex.back ())
792+ {
793+ memcpy (&x, ptr, sizeof (double ));
794+ ptr += sizeof (double );
795+ memcpy (&y, ptr, sizeof (double ));
796+ break ;
797+ }
798+ ++pointindex;
799+ ptr += 2 *sizeof (double );
800+ }
801+ }
802+ }
767803 return false ;
768- break ;
769804 }
770805 default :
771806#ifdef QGISDEBUG
0 commit comments