Skip to content

Commit

Permalink
Added code to add Vertices to polygons. Does not work yet, because sn…
Browse files Browse the repository at this point in the history
…apSegment is not implemented for polygons yet

git-svn-id: http://svn.osgeo.org/qgis/trunk@5266 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 12, 2006
1 parent d182648 commit 1ea018f
Showing 1 changed file with 80 additions and 17 deletions.
97 changes: 80 additions & 17 deletions src/core/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,15 @@ bool QgsGeometry::insertVertexBefore(double x, double y,

case geos::GEOS_POLYGON: // a polygon
{
// TODO
break;
if(insertVertexToPolygon(beforeVertex.back(), x, y))
{
mDirtyWkb = true;
return true;
}
else
{
return false;
}
} // case geos::GEOS_POLYGON

case geos::GEOS_MULTIPOINT: // a collection of points
Expand Down Expand Up @@ -915,7 +922,8 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
double *thisx,*thisy;
double *prevx,*prevy;
double testdist;

int closestSegmentIndex;

// Initialise some stuff
beforeVertex.clear();
sqrDist = std::numeric_limits<double>::max();
Expand All @@ -925,15 +933,8 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,
exportGeosToWkb();
}

// TODO: Convert this to a GEOS comparison not a WKB comparison.
if (mGeometry)
{
// int wkbType;
// double x,y;
// double *thisx,*thisy;
// double *prevx,*prevy;
// double testdist;

{
memcpy(&wkbType, (mGeometry+1), sizeof(int));

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

case QGis::WKBLineString:
int closestSegmentIndex = 0;
closestSegmentIndex = 0;
unsigned char* ptr = mGeometry + 5;
int* npoints = (int*) ptr;
ptr += sizeof(int);
Expand Down Expand Up @@ -988,8 +989,11 @@ QgsPoint QgsGeometry::closestSegmentWithContext(QgsPoint& point,

break;

// TODO: Other geometry types

//todo: add wkb parsing
case QGis::WKBPolygon:
{
break;
}
} // switch (wkbType)

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

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

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

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

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

bool QgsGeometry::insertVertexToPolygon(int beforeVertex, double x, double y)
{
return false; //soon
if(!mGeos)
{
return false;
}

geos::Polygon* originalpoly = dynamic_cast<geos::Polygon*>(mGeos);
if(!originalpoly)
{
return false;
}

geos::CoordinateSequence* coordinates = originalpoly->getCoordinates();
//the sequence where the point will be inserted
geos::CoordinateSequence* newSequence = new geos::DefaultCoordinateSequence();
std::vector<int> rings(originalpoly->getNumInteriorRing() + 1); //a vector storing the number of points in each ring
const geos::LineString* outerRing = originalpoly->getExteriorRing();

int coordinateCounter = 0;
int numRingPoints = 0;
for(int i = 0; i < outerRing->getNumPoints(); ++i)
{
newSequence->add(coordinates->getAt(coordinateCounter), true);
++coordinateCounter;
++numRingPoints;
if(coordinateCounter == beforeVertex)
{
++numRingPoints;
newSequence->add(geos::Coordinate(x, y), true);
}
}
rings[0] = numRingPoints;

for(int i = 0; i < originalpoly->getNumInteriorRing(); ++i)
{
numRingPoints = 0;
const geos::LineString* innerRing = originalpoly->getInteriorRingN(i);
for(int j = 0; j < originalpoly->getNumInteriorRing(); ++j)
{
newSequence->add(coordinates->getAt(coordinateCounter), true);
++numRingPoints;
++coordinateCounter;
if(coordinateCounter == beforeVertex)
{
++numRingPoints;
newSequence->add(geos::Coordinate(x, y), true);
}
}
rings[i+1] = numRingPoints;
}
geos::Polygon* newPolygon = createPolygonFromCoordSequence(newSequence, rings);
delete coordinates;
delete newSequence;
if(newPolygon)
{
delete mGeos;
mGeos = newPolygon;
return true;
}
else
{
return false;
}
}

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

0 comments on commit 1ea018f

Please sign in to comment.