/** * Read the SRID number from an SRID=<> string */ int wkt_lexer_read_srid(char *str) { char *c = str; long i = 0; int srid; if( ! str ) return SRID_UNKNOWN; c += 5; /* Advance past "SRID=" */ i = strtol(c, NULL, 10); srid = clamp_srid((int)i); /* TODO: warn on explicit UNKNOWN srid ? */ return srid; };
void gserialized_set_srid(GSERIALIZED *s, int32_t srid) { LWDEBUGF(3, "Called with srid = %d", srid); srid = clamp_srid(srid); /* 0 is our internal unknown value. * We'll map back and forth here for now */ if ( srid == SRID_UNKNOWN ) srid = 0; s->srid[0] = (srid & 0x001F0000) >> 16; s->srid[1] = (srid & 0x0000FF00) >> 8; s->srid[2] = (srid & 0x000000FF); }
int32_t gserialized_get_srid(const GSERIALIZED *s) { int32_t srid = 0; srid = srid | (s->srid[0] << 16); srid = srid | (s->srid[1] << 8); srid = srid | s->srid[2]; /* Only the first 21 bits are set. Slide up and back to pull the negative bits down, if we need them. */ srid = (srid<<11)>>11; /* 0 is our internal unknown value. We'll map back and forth here for now */ if ( srid == 0 ) return SRID_UNKNOWN; else return clamp_srid(srid); }
static uint32 gserialized_typmod_in(ArrayType *arr, int is_geography) { uint32 typmod = 0; Datum *elem_values; int n = 0; int i = 0; if (ARR_ELEMTYPE(arr) != CSTRINGOID) ereport(ERROR, (errcode(ERRCODE_ARRAY_ELEMENT_ERROR), errmsg("typmod array must be type cstring[]"))); if (ARR_NDIM(arr) != 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("typmod array must be one-dimensional"))); if (ARR_HASNULL(arr)) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("typmod array must not contain nulls"))); deconstruct_array(arr, CSTRINGOID, -2, false, 'c', /* hardwire cstring representation details */ &elem_values, NULL, &n); /* Set the SRID to the default value first */ if ( is_geography) TYPMOD_SET_SRID(typmod, SRID_DEFAULT); else TYPMOD_SET_SRID(typmod, SRID_UNKNOWN); for (i = 0; i < n; i++) { if ( i == 0 ) /* TYPE */ { char *s = DatumGetCString(elem_values[i]); uint8_t type = 0; int z = 0; int m = 0; if ( geometry_type_from_string(s, &type, &z, &m) == LW_FAILURE ) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid geometry type modifier: %s", s))); } else { TYPMOD_SET_TYPE(typmod, type); if ( z ) TYPMOD_SET_Z(typmod); if ( m ) TYPMOD_SET_M(typmod); } } if ( i == 1 ) /* SRID */ { int srid = pg_atoi(DatumGetCString(elem_values[i]), sizeof(int32), '\0'); srid = clamp_srid(srid); POSTGIS_DEBUGF(3, "srid: %d", srid); if ( srid != SRID_UNKNOWN ) { TYPMOD_SET_SRID(typmod, srid); } } } pfree(elem_values); return typmod; }
/** * GEOMETRY * Generic handling for WKB geometries. The front of every WKB geometry * (including those embedded in collections) is an endian byte, a type * number and an optional srid number. We handle all those here, then pass * to the appropriate handler for the specific type. */ LWGEOM* lwgeom_from_wkb_state(wkb_parse_state *s) { char wkb_little_endian; uint32_t wkb_type; LWDEBUG(4,"Entered function"); /* Fail when handed incorrect starting byte */ wkb_little_endian = byte_from_wkb_state(s); if( wkb_little_endian != 1 && wkb_little_endian != 0 ) { LWDEBUG(4,"Leaving due to bad first byte!"); lwerror("Invalid endian flag value encountered."); return NULL; } /* Check the endianness of our input */ s->swap_bytes = LW_FALSE; if( getMachineEndian() == NDR ) /* Machine arch is little */ { if ( ! wkb_little_endian ) /* Data is big! */ s->swap_bytes = LW_TRUE; } else /* Machine arch is big */ { if ( wkb_little_endian ) /* Data is little! */ s->swap_bytes = LW_TRUE; } /* Read the type number */ wkb_type = integer_from_wkb_state(s); LWDEBUGF(4,"Got WKB type number: 0x%X", wkb_type); lwtype_from_wkb_state(s, wkb_type); /* Read the SRID, if necessary */ if( s->has_srid ) { s->srid = clamp_srid(integer_from_wkb_state(s)); /* TODO: warn on explicit UNKNOWN srid ? */ LWDEBUGF(4,"Got SRID: %u", s->srid); } /* Do the right thing */ switch( s->lwtype ) { case POINTTYPE: return (LWGEOM*)lwpoint_from_wkb_state(s); break; case LINETYPE: return (LWGEOM*)lwline_from_wkb_state(s); break; case CIRCSTRINGTYPE: return (LWGEOM*)lwcircstring_from_wkb_state(s); break; case POLYGONTYPE: return (LWGEOM*)lwpoly_from_wkb_state(s); break; case TRIANGLETYPE: return (LWGEOM*)lwtriangle_from_wkb_state(s); break; case CURVEPOLYTYPE: return (LWGEOM*)lwcurvepoly_from_wkb_state(s); break; case MULTIPOINTTYPE: case MULTILINETYPE: case MULTIPOLYGONTYPE: case COMPOUNDTYPE: case MULTICURVETYPE: case MULTISURFACETYPE: case POLYHEDRALSURFACETYPE: case TINTYPE: case COLLECTIONTYPE: return (LWGEOM*)lwcollection_from_wkb_state(s); break; /* Unknown type! */ default: lwerror("Unsupported geometry type: %s [%d]", lwtype_name(s->lwtype), s->lwtype); } /* Return value to keep compiler happy. */ return NULL; }