Skip to content

Commit f59a31f

Browse files
author
mhugent
committed
Added code to add Vertices to polygons. Does not work yet, because snapSegment is not implemented for polygons yet
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5266 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ba9bc92 commit f59a31f

File tree

1 file changed

+80
-17
lines changed

1 file changed

+80
-17
lines changed

src/core/qgsgeometry.cpp

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,15 @@ bool QgsGeometry::insertVertexBefore(double x, double y,
470470

471471
case geos::GEOS_POLYGON: // a polygon
472472
{
473-
// TODO
474-
break;
473+
if(insertVertexToPolygon(beforeVertex.back(), x, y))
474+
{
475+
mDirtyWkb = true;
476+
return true;
477+
}
478+
else
479+
{
480+
return false;
481+
}
475482
} // case geos::GEOS_POLYGON
476483

477484
case geos::GEOS_MULTIPOINT: // a collection of points
@@ -915,7 +922,8 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
915922
double *thisx,*thisy;
916923
double *prevx,*prevy;
917924
double testdist;
918-
925+
int closestSegmentIndex;
926+
919927
// Initialise some stuff
920928
beforeVertex.clear();
921929
sqrDist = std::numeric_limits<double>::max();
@@ -925,15 +933,8 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
925933
exportGeosToWkb();
926934
}
927935

928-
// TODO: Convert this to a GEOS comparison not a WKB comparison.
929936
if (mGeometry)
930-
{
931-
// int wkbType;
932-
// double x,y;
933-
// double *thisx,*thisy;
934-
// double *prevx,*prevy;
935-
// double testdist;
936-
937+
{
937938
memcpy(&wkbType, (mGeometry+1), sizeof(int));
938939

939940
switch (wkbType)
@@ -943,7 +944,7 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
943944
return QgsPoint(0,0);
944945

945946
case QGis::WKBLineString:
946-
int closestSegmentIndex = 0;
947+
closestSegmentIndex = 0;
947948
unsigned char* ptr = mGeometry + 5;
948949
int* npoints = (int*) ptr;
949950
ptr += sizeof(int);
@@ -988,8 +989,11 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
988989

989990
break;
990991

991-
// TODO: Other geometry types
992-
992+
//todo: add wkb parsing
993+
case QGis::WKBPolygon:
994+
{
995+
break;
996+
}
993997
} // switch (wkbType)
994998

995999
} // if (mGeometry)
@@ -2064,7 +2068,6 @@ bool QgsGeometry::movePolygonVertex(int atVertex, double x, double y)
20642068

20652069
geos::CoordinateSequence* coordinates = originalpoly->getCoordinates();
20662070
std::vector<int> rings(originalpoly->getNumInteriorRing() + 1); //a vector storing the number of points in each ring
2067-
//todo: consider that the point to be moved could be the starting point/ end point of a ring
20682071
const geos::LineString* outerRing = originalpoly->getExteriorRing();
20692072
int pointcounter = 0;
20702073

@@ -2125,7 +2128,6 @@ bool QgsGeometry::deleteVertexFromPolygon(int atVertex)
21252128

21262129
geos::CoordinateSequence* coordinates = originalpoly->getCoordinates();
21272130
std::vector<int> rings(originalpoly->getNumInteriorRing() + 1); //a vector storing the number of points in each ring
2128-
//todo: consider that the point to be moved could be the starting point/ end point of a ring
21292131
const geos::LineString* outerRing = originalpoly->getExteriorRing();
21302132
int pointcounter = 0;
21312133

@@ -2187,7 +2189,68 @@ bool QgsGeometry::deleteVertexFromPolygon(int atVertex)
21872189

21882190
bool QgsGeometry::insertVertexToPolygon(int beforeVertex, double x, double y)
21892191
{
2190-
return false; //soon
2192+
if(!mGeos)
2193+
{
2194+
return false;
2195+
}
2196+
2197+
geos::Polygon* originalpoly = dynamic_cast<geos::Polygon*>(mGeos);
2198+
if(!originalpoly)
2199+
{
2200+
return false;
2201+
}
2202+
2203+
geos::CoordinateSequence* coordinates = originalpoly->getCoordinates();
2204+
//the sequence where the point will be inserted
2205+
geos::CoordinateSequence* newSequence = new geos::DefaultCoordinateSequence();
2206+
std::vector<int> rings(originalpoly->getNumInteriorRing() + 1); //a vector storing the number of points in each ring
2207+
const geos::LineString* outerRing = originalpoly->getExteriorRing();
2208+
2209+
int coordinateCounter = 0;
2210+
int numRingPoints = 0;
2211+
for(int i = 0; i < outerRing->getNumPoints(); ++i)
2212+
{
2213+
newSequence->add(coordinates->getAt(coordinateCounter), true);
2214+
++coordinateCounter;
2215+
++numRingPoints;
2216+
if(coordinateCounter == beforeVertex)
2217+
{
2218+
++numRingPoints;
2219+
newSequence->add(geos::Coordinate(x, y), true);
2220+
}
2221+
}
2222+
rings[0] = numRingPoints;
2223+
2224+
for(int i = 0; i < originalpoly->getNumInteriorRing(); ++i)
2225+
{
2226+
numRingPoints = 0;
2227+
const geos::LineString* innerRing = originalpoly->getInteriorRingN(i);
2228+
for(int j = 0; j < originalpoly->getNumInteriorRing(); ++j)
2229+
{
2230+
newSequence->add(coordinates->getAt(coordinateCounter), true);
2231+
++numRingPoints;
2232+
++coordinateCounter;
2233+
if(coordinateCounter == beforeVertex)
2234+
{
2235+
++numRingPoints;
2236+
newSequence->add(geos::Coordinate(x, y), true);
2237+
}
2238+
}
2239+
rings[i+1] = numRingPoints;
2240+
}
2241+
geos::Polygon* newPolygon = createPolygonFromCoordSequence(newSequence, rings);
2242+
delete coordinates;
2243+
delete newSequence;
2244+
if(newPolygon)
2245+
{
2246+
delete mGeos;
2247+
mGeos = newPolygon;
2248+
return true;
2249+
}
2250+
else
2251+
{
2252+
return false;
2253+
}
21912254
}
21922255

21932256
geos::Polygon* QgsGeometry::createPolygonFromCoordSequence(const geos::CoordinateSequence* coords, const std::vector<int>& pointsInRings) const

0 commit comments

Comments
 (0)