Skip to content

Commit

Permalink
Also added a wkb method for adding points. Implementation for multipo…
Browse files Browse the repository at this point in the history
…lygon will follow soon

git-svn-id: http://svn.osgeo.org/qgis/trunk@5352 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 23, 2006
1 parent 8577f1d commit 56ad7fb
Showing 1 changed file with 123 additions and 3 deletions.
126 changes: 123 additions & 3 deletions src/core/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ QgsPoint QgsGeometry::closestVertex(const QgsPoint& point) const
}



bool QgsGeometry::insertVertexBefore(double x, double y,
int beforeVertex,
const geos::CoordinateSequence* old_sequence,
Expand Down Expand Up @@ -413,7 +414,7 @@ bool QgsGeometry::insertVertexBefore(double x, double y,
return inserted;
}


#if 0
bool QgsGeometry::insertVertexBefore(double x, double y,
QgsGeometryVertexIndex beforeVertex)
{
Expand Down Expand Up @@ -510,9 +511,8 @@ bool QgsGeometry::insertVertexBefore(double x, double y,
} // if (mGeos)

return FALSE;


}
#endif

bool QgsGeometry::moveVertexAt(double x, double y, QgsGeometryVertexIndex atVertex)
{
Expand Down Expand Up @@ -900,7 +900,127 @@ bool QgsGeometry::deleteVertexAt(QgsGeometryVertexIndex atVertex)
}
}

bool QgsGeometry::insertVertexBefore(double x, double y, QgsGeometryVertexIndex beforeVertex)
{
int vertexnr = beforeVertex.back();
bool success = false;

if(mDirtyWkb)
{
exportGeosToWkb();
}

//create a new geometry buffer for the modified geometry
unsigned char* newbuffer = new unsigned char[mGeometrySize+2*sizeof(double)];
memcpy(newbuffer, mGeometry, 1+sizeof(int)); //endian and type are the same

int pointindex = 0;
unsigned int wkbType;
unsigned char* ptr = mGeometry+1;
memcpy(&wkbType, ptr, sizeof(wkbType));
ptr += sizeof(wkbType);
unsigned char* newBufferPtr = newbuffer+1+sizeof(int);

switch(wkbType)
{
case QGis::WKBPoint://cannot insert a vertex before another one on point types
case QGis::WKBMultiPoint:
break;
case QGis::WKBLineString:
{
int* nPoints = (int*)ptr;
if(vertexnr > *nPoints || vertexnr < 0)
{
break;
}
int newNPoints = (*nPoints)+1;
memcpy(newBufferPtr, &newNPoints, sizeof(int));
newBufferPtr += sizeof(int);
ptr += sizeof(int);

for(int pointnr = 0; pointnr < *nPoints; ++pointnr)
{
memcpy(newBufferPtr, ptr, sizeof(double));//x
memcpy(newBufferPtr+sizeof(double), ptr+sizeof(double), sizeof(double));//x
ptr += 2*sizeof(double);
newBufferPtr += 2*sizeof(double);
++pointindex;
if(pointindex == vertexnr)
{
memcpy(newBufferPtr, &x, sizeof(double));
memcpy(newBufferPtr+sizeof(double), &y, sizeof(double));
newBufferPtr += 2*sizeof(double);
success = true;
}
}
break;
}
case QGis::WKBMultiLineString:
{
break;
}
case QGis::WKBPolygon:
{
int* nRings = (int*)ptr;
int* nPoints = 0; //number of points in a ring
ptr += sizeof(int);
memcpy(newBufferPtr, nRings, sizeof(int));
newBufferPtr += sizeof(int);
int pointindex = 0;

for(int ringnr = 0; ringnr < *nRings; ++ringnr)
{
nPoints = (int*)ptr;
int newNPoints;
if(vertexnr >= pointindex && vertexnr < (pointindex + (*nPoints)))//point is in this ring
{
newNPoints = (*nPoints)+1;
}
else
{
newNPoints = *nPoints;
}
memcpy(newBufferPtr, &newNPoints, sizeof(double));
newBufferPtr += sizeof(int);
ptr += sizeof(int);

for(int pointnr = 0; pointnr < *nPoints; ++pointnr)
{
memcpy(newBufferPtr, ptr, sizeof(double));//x
memcpy(newBufferPtr+sizeof(double), ptr+sizeof(double), sizeof(double));//y
ptr += 2*sizeof(double);
newBufferPtr += 2*sizeof(double);
++pointindex;
if(pointindex == vertexnr)
{
memcpy(newBufferPtr, &x, sizeof(double));
memcpy(newBufferPtr+sizeof(double), &y, sizeof(double));
newBufferPtr += 2*sizeof(double);
success = true;
}
}
}
break;
}
case QGis::WKBMultiPolygon:
{
break;
}
}

if(success)
{
delete mGeometry;
mGeometry = newbuffer;
mGeometrySize += 2*sizeof(double);
return true;
}
else
{
delete newbuffer;
return false;
}
}

#if 0
bool QgsGeometry::moveVertexAt(double x, double y,
Expand Down

0 comments on commit 56ad7fb

Please sign in to comment.