Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS) { PG_LWGEOM *geom; int32 SRID; PG_LWGEOM *result = NULL; geom = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1( LWGEOMFromWKB, PG_GETARG_DATUM(0))); if ( pglwgeom_getSRID(geom) != -1 || TYPE_GETZM(geom->type) != 0 ) { elog(WARNING, "OGC WKB expected, EWKB provided - use GeometryFromEWKB() for this"); } /* read user-requested SRID if any */ if ( PG_NARGS() > 1 ) { SRID = PG_GETARG_INT32(1); if ( SRID != pglwgeom_getSRID(geom) ) { result = pglwgeom_setSRID(geom, SRID); pfree(geom); } } if ( ! result ) result = geom; PG_RETURN_POINTER(result); }
/* * Construct a LWCIRCSTRING from a LWMPOINT */ LWCIRCSTRING * lwcircstring_from_lwmpoint(int SRID, LWMPOINT *mpoint) { unsigned int i; POINTARRAY *pa; char zmflag = TYPE_GETZM(mpoint->type); size_t ptsize, size; uchar *newpoints, *ptr; if (zmflag == 0) ptsize = 2 * sizeof(double); else if (zmflag == 3) ptsize = 4 * sizeof(double); else ptsize = 3 * sizeof(double); /* Allocate space for output points */ size = ptsize * mpoint->ngeoms; newpoints = lwalloc(size); memset(newpoints, 0, size); ptr = newpoints; for (i = 0; i < mpoint->ngeoms; i++) { memcpy(ptr, getPoint_internal(mpoint->geoms[i]->point, 0), ptsize); ptr += ptsize; } pa = pointArray_construct(newpoints, zmflag&2, zmflag&1, mpoint->ngeoms); LWDEBUGF(3, "lwcurve_from_lwmpoint: constructed pointarray for %d points, %d zmflag", mpoint->ngeoms, zmflag); return lwcircstring_construct(SRID, NULL, pa); }
/* * Convert this point into its serialize form writing it into * the given buffer, and returning number of bytes written into * the given int pointer. * result's first char will be the 8bit type. See serialized form doc */ void lwpoint_serialize_buf(LWPOINT *point, uchar *buf, size_t *retsize) { int size=1; char hasSRID; uchar *loc; int ptsize = pointArray_ptsize(point->point); if ( TYPE_GETZM(point->type) != TYPE_GETZM(point->point->dims) ) lwerror("Dimensions mismatch in lwpoint"); LWDEBUGF(2, "lwpoint_serialize_buf(%p, %p) called", point, buf); /*printLWPOINT(point); */ hasSRID = (point->SRID != -1); if (hasSRID) size +=4; /*4 byte SRID */ if (point->bbox) size += sizeof(BOX2DFLOAT4); /* bvol */ size += sizeof(double)*TYPE_NDIMS(point->type); buf[0] = (uchar) lwgeom_makeType_full( TYPE_HASZ(point->type), TYPE_HASM(point->type), hasSRID, POINTTYPE, point->bbox?1:0); loc = buf+1; if (point->bbox) { memcpy(loc, point->bbox, sizeof(BOX2DFLOAT4)); loc += sizeof(BOX2DFLOAT4); } if (hasSRID) { memcpy(loc, &point->SRID, sizeof(int32)); loc += 4; } /* copy in points */ memcpy(loc, getPoint_internal(point->point, 0), ptsize); if (retsize) *retsize = size; }
Datum LWGEOM_from_text(PG_FUNCTION_ARGS) { text *wkttext = PG_GETARG_TEXT_P(0); char *wkt, fc; size_t size; LWGEOM_PARSER_RESULT lwg_parser_result; PG_LWGEOM *geom_result = NULL; LWGEOM *lwgeom; int result; POSTGIS_DEBUG(2, "LWGEOM_from_text"); size = VARSIZE(wkttext)-VARHDRSZ; POSTGIS_DEBUGF(3, "size: %d", (int)size); if ( size < 10 ) { lwerror("Invalid OGC WKT (too short)"); PG_RETURN_NULL(); } fc=*(VARDATA(wkttext)); wkt = lwalloc(size+1); memcpy(wkt, VARDATA(wkttext), size); wkt[size]='\0'; POSTGIS_DEBUGF(3, "wkt: [%s]", wkt); result = serialized_lwgeom_from_ewkt(&lwg_parser_result, wkt, PARSER_CHECK_ALL); if (result) PG_PARSER_ERROR(lwg_parser_result); lwgeom = lwgeom_deserialize(lwg_parser_result.serialized_lwgeom); if ( lwgeom->SRID != -1 || TYPE_GETZM(lwgeom->type) != 0 ) { elog(WARNING, "OGC WKT expected, EWKT provided - use GeomFromEWKT() for this"); } /* read user-requested SRID if any */ if ( PG_NARGS() > 1 ) lwgeom->SRID = PG_GETARG_INT32(1); geom_result = pglwgeom_serialize(lwgeom); lwgeom_release(lwgeom); PG_RETURN_POINTER(geom_result); }
/* * convert this circularstring into its serialized form writing it into * the given buffer, and returning number of bytes written into * the given int pointer. * result's first char will be the 8bit type. See serialized form doc */ void lwcircstring_serialize_buf(LWCIRCSTRING *curve, uchar *buf, size_t *retsize) { char hasSRID; uchar *loc; int ptsize; size_t size; LWDEBUGF(2, "lwcircstring_serialize_buf(%p, %p, %p) called", curve, buf, retsize); if (curve == NULL) { lwerror("lwcircstring_serialize:: given null curve"); return; } if (TYPE_GETZM(curve->type) != TYPE_GETZM(curve->points->dims)) { lwerror("Dimensions mismatch in lwcircstring"); return; } ptsize = pointArray_ptsize(curve->points); hasSRID = (curve->SRID != -1); buf[0] = (uchar)lwgeom_makeType_full( TYPE_HASZ(curve->type), TYPE_HASM(curve->type), hasSRID, CIRCSTRINGTYPE, curve->bbox ? 1 : 0); loc = buf+1; LWDEBUGF(3, "lwcircstring_serialize_buf added type (%d)", curve->type); if (curve->bbox) { memcpy(loc, curve->bbox, sizeof(BOX2DFLOAT4)); loc += sizeof(BOX2DFLOAT4); LWDEBUG(3, "lwcircstring_serialize_buf added BBOX"); } if (hasSRID) { memcpy(loc, &curve->SRID, sizeof(int32)); loc += sizeof(int32); LWDEBUG(3, "lwcircstring_serialize_buf added SRID"); } memcpy(loc, &curve->points->npoints, sizeof(uint32)); loc += sizeof(uint32); LWDEBUGF(3, "lwcircstring_serialize_buf added npoints (%d)", curve->points->npoints); /* copy in points */ size = curve->points->npoints * ptsize; memcpy(loc, getPoint_internal(curve->points, 0), size); loc += size; LWDEBUGF(3, "lwcircstring_serialize_buf copied serialized_pointlist (%d bytes)", ptsize * curve->points->npoints); if (retsize) *retsize = loc-buf; LWDEBUGF(3, "lwcircstring_serialize_buf returning (loc: %p, size: %d)", loc, loc-buf); }
/* * convert this line into its serialize form writing it into * the given buffer, and returning number of bytes written into * the given int pointer. * result's first char will be the 8bit type. See serialized form doc */ void lwline_serialize_buf(LWLINE *line, uchar *buf, size_t *retsize) { char hasSRID; uchar *loc; int ptsize; size_t size; LWDEBUGF(2, "lwline_serialize_buf(%p, %p, %p) called", line, buf, retsize); if (line == NULL) lwerror("lwline_serialize:: given null line"); if ( TYPE_GETZM(line->type) != TYPE_GETZM(line->points->dims) ) lwerror("Dimensions mismatch in lwline"); ptsize = pointArray_ptsize(line->points); hasSRID = (line->SRID != -1); buf[0] = (uchar) lwgeom_makeType_full( TYPE_HASZ(line->type), TYPE_HASM(line->type), hasSRID, LINETYPE, line->bbox ? 1 : 0); loc = buf+1; LWDEBUGF(3, "lwline_serialize_buf added type (%d)", line->type); if (line->bbox) { memcpy(loc, line->bbox, sizeof(BOX2DFLOAT4)); loc += sizeof(BOX2DFLOAT4); LWDEBUG(3, "lwline_serialize_buf added BBOX"); } if (hasSRID) { memcpy(loc, &line->SRID, sizeof(int32)); loc += sizeof(int32); LWDEBUG(3, "lwline_serialize_buf added SRID"); } memcpy(loc, &line->points->npoints, sizeof(uint32)); loc += sizeof(uint32); LWDEBUGF(3, "lwline_serialize_buf added npoints (%d)", line->points->npoints); /*copy in points */ size = line->points->npoints*ptsize; memcpy(loc, getPoint_internal(line->points, 0), size); loc += size; LWDEBUGF(3, "lwline_serialize_buf copied serialized_pointlist (%d bytes)", ptsize * line->points->npoints); if (retsize) *retsize = loc-buf; /*printBYTES((uchar *)result, loc-buf); */ LWDEBUGF(3, "lwline_serialize_buf returning (loc: %p, size: %d)", loc, loc-buf); }