From: Sandro Santilli Date: Tue, 21 Dec 2004 15:17:30 +0000 (+0000) Subject: Made setSRID(geom, -1) actually *remove* srid from LWGEOM. X-Git-Tag: pgis_1_0_0RC1~131 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=de8eb920ebda92d7969efb8e04bf83a4a7327933;p=postgis Made setSRID(geom, -1) actually *remove* srid from LWGEOM. git-svn-id: http://svn.osgeo.org/postgis/trunk@1173 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/lwgeom/lwgeom_api.c b/lwgeom/lwgeom_api.c index 1c9da6544..2cfa3eb64 100644 --- a/lwgeom/lwgeom_api.c +++ b/lwgeom/lwgeom_api.c @@ -1750,42 +1750,77 @@ lwgeom_setSRID(PG_LWGEOM *lwgeom, int32 newSRID) if (lwgeom_hasSRID(type)) { - //we create a new one and copy the SRID in - result = lwalloc(len); - memcpy(result, lwgeom, len); - memcpy(result->data+bbox_offset, &newSRID,4); + if ( newSRID != -1 ) { + //we create a new one and copy the SRID in + result = lwalloc(len); + memcpy(result, lwgeom, len); + memcpy(result->data+bbox_offset, &newSRID,4); + } else { + //we create a new one dropping the SRID + result = lwalloc(len-4); + result->size = len-4; + result->type = lwgeom_makeType_full( + TYPE_HASZ(type), TYPE_HASM(type), + 0, lwgeom_getType(type), + lwgeom_hasBBOX(type)); + loc_new = result->data; + loc_old = lwgeom->data; + len_left = len-4-1; + + // handle bbox (if there) + if (lwgeom_hasBBOX(type)) + { + memcpy(loc_new, loc_old, sizeof(BOX2DFLOAT4)); + loc_new += sizeof(BOX2DFLOAT4); + loc_old += sizeof(BOX2DFLOAT4); + len_left -= sizeof(BOX2DFLOAT4); + } + + // skip SRID, copy the remaining + loc_old += 4; + len_left -= 4; + memcpy(loc_new, loc_old, len_left); + + } + } - else // need to add one + else { - len_new = len + 4;//+4 for SRID - result = lwalloc(len_new); - memcpy(result, &len_new, 4); // size copy in - result->type = lwgeom_makeType_full( - TYPE_HASZ(type), TYPE_HASM(type), - 1, lwgeom_getType(type),lwgeom_hasBBOX(type)); - - loc_new = result->data; - loc_old = lwgeom->data; + // just copy input, already w/out a SRID + if ( newSRID == -1 ) { + result = lwalloc(len); + memcpy(result, lwgeom, len); + } + // need to add one + else { + len_new = len + 4;//+4 for SRID + result = lwalloc(len_new); + memcpy(result, &len_new, 4); // size copy in + result->type = lwgeom_makeType_full( + TYPE_HASZ(type), TYPE_HASM(type), + 1, lwgeom_getType(type),lwgeom_hasBBOX(type)); - len_left = len -4-1;// old length - size - type + loc_new = result->data; + loc_old = lwgeom->data; - // handle bbox (if there) + len_left = len -4-1;// old length - size - type - if (lwgeom_hasBBOX(type)) - { - memcpy(loc_new, loc_old, sizeof(BOX2DFLOAT4)) ;//copy in bbox - loc_new += sizeof(BOX2DFLOAT4); - loc_old += sizeof(BOX2DFLOAT4); - len_left -= sizeof(BOX2DFLOAT4); - } + // handle bbox (if there) - //put in SRID + if (lwgeom_hasBBOX(type)) + { + memcpy(loc_new, loc_old, sizeof(BOX2DFLOAT4)) ;//copy in bbox + loc_new += sizeof(BOX2DFLOAT4); + loc_old += sizeof(BOX2DFLOAT4); + len_left -= sizeof(BOX2DFLOAT4); + } - memcpy(loc_new, &newSRID,4); - loc_new +=4; - memcpy(loc_new, loc_old, len_left); + //put in SRID - // TODO: add SRID presence flag in type. + memcpy(loc_new, &newSRID,4); + loc_new +=4; + memcpy(loc_new, loc_old, len_left); + } } return result; }