From e0beec3af0c9c40b8776c5fcc1a2f59a1bd93a67 Mon Sep 17 00:00:00 2001 From: Alvaro Huarte Date: Mon, 20 Jan 2014 18:05:30 +0100 Subject: [PATCH 1/2] #5357: Add OGR_G_SetPointCount and OGR_G_SetPoints functions to API C --- gdal/ogr/ogr_api.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++ gdal/ogr/ogr_api.h | 5 +++ 2 files changed, 104 insertions(+) diff --git a/gdal/ogr/ogr_api.cpp b/gdal/ogr/ogr_api.cpp index 45985cf2d355..2c72c895eb6a 100644 --- a/gdal/ogr/ogr_api.cpp +++ b/gdal/ogr/ogr_api.cpp @@ -68,6 +68,38 @@ int OGR_G_GetPointCount( OGRGeometryH hGeom ) } } +/************************************************************************/ +/* OGR_G_SetPointCount() */ +/************************************************************************/ +/** + * \brief Set number of points in a geometry. + * + * This method primary exists to preset the number of points in a linestring + * geometry before setPoint() is used to assign them to avoid reallocating + * the array larger with each call to addPoint(). + * + * @param nNewPointCount the new number of points for geometry. + */ + +void OGR_G_SetPointCount( OGRGeometryH hGeom, int nNewPointCount ) + +{ + VALIDATE_POINTER0( hGeom, "OGR_G_SetPointCount", 0 ); + + switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) ) + { + case wkbLineString: + { + OGRLineString *poLine = (OGRLineString *) hGeom; + poLine->setNumPoints( nNewPointCount ); + break; + } + default: + CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation"); + break; + } +} + /************************************************************************/ /* OGR_G_GetX() */ /************************************************************************/ @@ -329,6 +361,73 @@ void OGR_G_GetPoint( OGRGeometryH hGeom, int i, } } +/************************************************************************/ +/* OGR_G_SetPoint() */ +/************************************************************************/ +/** + * \brief Assign all points in a point or a line string geometry. + * + * This method clear any existing points assigned to this geometry, + * and assigns a whole new set. + * + * @param hGeom handle to the geometry to set the coordinates. + * @param nPointsIn number of points being passed in padfX and padfY. + * @param padfX list of X coordinates of points being assigned. + * @param nXStride the number of bytes between 2 elements of pabyX. + * @param padfY list of Y coordinates of points being assigned. + * @param nYStride the number of bytes between 2 elements of pabyY. + * @param padfZ list of Z coordinates of points being assigned (defaults to NULL for 2D objects). + * @param nZStride the number of bytes between 2 elements of pabyZ. + */ + +void CPL_DLL OGR_G_SetPoints( OGRGeometryH hGeom, int nPointsIn, + void* pabyX, int nXStride, + void* pabyY, int nYStride, + void* pabyZ, int nZStride ) + +{ + VALIDATE_POINTER0( hGeom, "OGR_G_SetPoints" ); + + switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) ) + { + case wkbPoint: + { + ((OGRPoint *) hGeom)->setX( pabyX ? *( (double *)pabyX ) : 0.0 ); + ((OGRPoint *) hGeom)->setY( pabyY ? *( (double *)pabyY ) : 0.0 ); + ((OGRPoint *) hGeom)->setZ( pabyZ ? *( (double *)pabyZ ) : 0.0 ); + break; + } + case wkbLineString: + { + OGRLineString* poLine = (OGRLineString *) hGeom; + + if( nXStride == 0 && nYStride == 0 && nZStride == 0 ) + { + poLine->setPoints( nPointsIn, (double *)pabyX, (double *)pabyY, (double *)pabyZ ); + } + else + { + double x, y, z; + x = y = z = 0; + poLine->setNumPoints( nPointsIn ); + + for (int i = 0; i < nPointsIn; ++i) + { + if( pabyX ) x = *(double*)((char*)pabyX + i * nXStride); + if( pabyY ) y = *(double*)((char*)pabyY + i * nYStride); + if( pabyZ ) z = *(double*)((char*)pabyZ + i * nZStride); + + poLine->setPoint( i, x, y, z ); + } + } + break; + } + default: + CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation"); + break; + } +} + /************************************************************************/ /* OGR_G_SetPoint() */ /************************************************************************/ diff --git a/gdal/ogr/ogr_api.h b/gdal/ogr/ogr_api.h index 97b77cfbbbd2..3a0d9387ce8d 100644 --- a/gdal/ogr/ogr_api.h +++ b/gdal/ogr/ogr_api.h @@ -189,12 +189,17 @@ double CPL_DLL OGR_G_GetY( OGRGeometryH, int ); double CPL_DLL OGR_G_GetZ( OGRGeometryH, int ); void CPL_DLL OGR_G_GetPoint( OGRGeometryH, int iPoint, double *, double *, double * ); +void CPL_DLL OGR_G_SetPointCount( OGRGeometryH hGeom, int nNewPointCount ); void CPL_DLL OGR_G_SetPoint( OGRGeometryH, int iPoint, double, double, double ); void CPL_DLL OGR_G_SetPoint_2D( OGRGeometryH, int iPoint, double, double ); void CPL_DLL OGR_G_AddPoint( OGRGeometryH, double, double, double ); void CPL_DLL OGR_G_AddPoint_2D( OGRGeometryH, double, double ); +void CPL_DLL OGR_G_SetPoints( OGRGeometryH hGeom, int nPointsIn, + void* pabyX, int nXStride, + void* pabyY, int nYStride, + void* pabyZ, int nZStride ); /* Methods for getting/setting rings and members collections */ From 18c736b736f6cf710b04269d87c648f6f5a1b839 Mon Sep 17 00:00:00 2001 From: Alvaro Huarte Date: Mon, 20 Jan 2014 18:22:24 +0100 Subject: [PATCH 2/2] #5357: fix VALIDATE_POINTER0 macro --- gdal/ogr/ogr_api.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdal/ogr/ogr_api.cpp b/gdal/ogr/ogr_api.cpp index 2c72c895eb6a..e32e95d71b41 100644 --- a/gdal/ogr/ogr_api.cpp +++ b/gdal/ogr/ogr_api.cpp @@ -84,7 +84,7 @@ int OGR_G_GetPointCount( OGRGeometryH hGeom ) void OGR_G_SetPointCount( OGRGeometryH hGeom, int nNewPointCount ) { - VALIDATE_POINTER0( hGeom, "OGR_G_SetPointCount", 0 ); + VALIDATE_POINTER0( hGeom, "OGR_G_SetPointCount" ); switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) ) {