Skip to content

Commit 5a4a7db

Browse files
author
mhugent
committed
addPoint should now work also for polygons
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5323 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 44789bc commit 5a4a7db

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

src/core/qgsgeometry.cpp

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,31 @@ bool QgsGeometry::vertexAt(double &x, double &y,
721721
}
722722
case QGis::WKBPolygon:
723723
{
724-
// TODO
725-
return false;
726-
break;
724+
unsigned char *ptr;
725+
int *nRings;
726+
int *nPoints = 0;
727+
ptr = mGeometry+5;
728+
nRings = (int*)ptr;
729+
ptr += sizeof(int);
730+
int pointindex = 0;
731+
for(int ringnr = 0; ringnr < *nRings; ++ringnr)
732+
{
733+
nPoints = (int*)ptr;
734+
ptr += sizeof(int);
735+
for(int pointnr = 0; pointnr < *nPoints; ++pointnr)
736+
{
737+
if(pointindex == atVertex.back())
738+
{
739+
memcpy(&x, ptr, sizeof(double));
740+
ptr += sizeof(double);
741+
memcpy(&y, ptr, sizeof(double));
742+
break;
743+
}
744+
ptr += 2*sizeof(double);
745+
++pointindex;
746+
}
747+
}
748+
return false;
727749
}
728750
case QGis::WKBMultiPoint:
729751
{
@@ -910,12 +932,6 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
910932
QgsGeometryVertexIndex& beforeVertex,
911933
double& sqrDist)
912934
{
913-
914-
#ifdef QGISDEBUG
915-
// std::cout << "QgsGeometry::closestSegment: Entered"
916-
// << "." << std::endl;
917-
#endif
918-
919935
QgsPoint minDistPoint;
920936

921937
int wkbType;
@@ -957,43 +973,57 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
957973
prevx = thisx;
958974
prevy = thisy;
959975
}
960-
961976
thisx = (double*) ptr;
962977
ptr += sizeof(double);
963978
thisy = (double*) ptr;
964979

965980
if (index > 0)
966981
{
967-
if (
968-
(
969-
testdist = distanceSquaredPointToSegment(point,
970-
prevx, prevy,
971-
thisx, thisy,
972-
minDistPoint) // TODO: save minDistPoint into something meaningful.
973-
)
974-
< sqrDist )
982+
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, minDistPoint)) < sqrDist )
975983
{
976-
#ifdef QGISDEBUG
977-
// std::cout << "QgsGeometry::closestSegment: testDist "
978-
// << testdist << ", sqrDist"
979-
// << sqrDist
980-
// << "." << std::endl;
981-
#endif
982984
closestSegmentIndex = index;
983985
sqrDist = testdist;
984986
}
985987
}
986-
987988
ptr += sizeof(double);
988989
}
989-
990990
beforeVertex.push_back(closestSegmentIndex);
991-
992991
break;
993992
}
994-
//todo: add wkb parsing
995993
case QGis::WKBPolygon:
996994
{
995+
closestSegmentIndex = 0;
996+
int index = 0;
997+
unsigned char* ptr = mGeometry + 5;
998+
int* nrings = (int*)ptr;
999+
int* npoints = 0; //number of points in a ring
1000+
ptr += sizeof(int);
1001+
for(int ringnr = 0; ringnr < *nrings; ++ringnr)//loop over rings
1002+
{
1003+
npoints = (int*)ptr;
1004+
ptr += sizeof(int);
1005+
prevx = 0;
1006+
prevy = 0;
1007+
for(int pointnr = 0; pointnr < *npoints; ++pointnr)//loop over points in a ring
1008+
{
1009+
thisx = (double*)ptr;
1010+
ptr += sizeof(double);
1011+
thisy = (double*)ptr;
1012+
ptr += sizeof(double);
1013+
if(prevx && prevy)
1014+
{
1015+
if((testdist = distanceSquaredPointToSegment(point, prevx, prevy, thisx, thisy, minDistPoint)) < sqrDist )
1016+
{
1017+
closestSegmentIndex = index;
1018+
sqrDist = testdist;
1019+
}
1020+
}
1021+
prevx = thisx;
1022+
prevy = thisy;
1023+
++index;
1024+
}
1025+
}
1026+
beforeVertex.push_back(closestSegmentIndex);
9971027
break;
9981028
}
9991029
} // switch (wkbType)

0 commit comments

Comments
 (0)