Changeset: 0e3c2c35c8dc for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=0e3c2c35c8dc Modified Files: geom/lib/libgeom.c geom/lib/libgeom.h geom/monetdb5/geom.c geom/monetdb5/geom.mal geom/sql/40_geom.sql monetdb5/mal/mal_interpreter.c monetdb5/mal/mal_scenario.c monetdb5/mal/mal_session.c sql/backends/monet5/sql.h sql/backends/monet5/sql.mal sql/backends/monet5/sql_cast.c sql/backends/monet5/sql_cast_impl_up_to_int.h sql/backends/monet5/sql_gencode.c sql/backends/monet5/sql_scenario.c sql/common/sql_types.c sql/common/sql_types.h sql/server/rel_updates.c sql/server/sql_parser.y sql/server/sql_scan.c sql/server/sql_symbol.h Branch: geo Log Message:
srid added to wkb. geos2wkb, wkb2geos and wkbFROMSTR adapted to this change diffs (truncated from 807 to 300 lines): diff --git a/geom/lib/libgeom.c b/geom/lib/libgeom.c --- a/geom/lib/libgeom.c +++ b/geom/lib/libgeom.c @@ -110,6 +110,18 @@ getMbrGeos(mbr *res, const GEOSGeom geos return 1; } +GEOSGeom wkb2geos(wkb* geomWKB) { + GEOSGeom geosGeometry; + + if(wkb_isnil(geomWKB)) + return NULL; + + geosGeometry = GEOSGeomFromWKB_buf((unsigned char *)((geomWKB)->data), (geomWKB)->len); + GEOSSetSRID(geosGeometry, (geomWKB)->srid); + + return geosGeometry; +} + /* Function getMbrGeom * A wrapper for getMbrGeos on a geom_geometry. */ @@ -166,6 +178,24 @@ const char* geom_type2str(int t, int fla case wkbGeometryCollection: return "ST_GeometryCollection"; } -} + } return "UKNOWN"; } + + +/* +str geomerty_2_geometry(wkb *res, wkb **geom, int* columnType, int* columnSRID, int* valueSRID) { + +//char* geomStr; +//int len = 0; +//fprintf(stderr, "geometry_2_geometry\n"); +//wkbTOSTR(&geomStr, &len, *geom); +if(*geom != NULL) +fprintf(stderr, "type:%d - wkbTOSTR cannot be seen at this point\n", *columnType); + +if(res == NULL) + fprintf(stderr, "-> "); + + fprintf(stderr, "%d vs %d\n", *columnSRID, *valueSRID); + return "0"; +}*/ diff --git a/geom/lib/libgeom.h b/geom/lib/libgeom.h --- a/geom/lib/libgeom.h +++ b/geom/lib/libgeom.h @@ -111,6 +111,7 @@ libgeom_export const char *geom_type2str typedef struct wkb { int len; + int srid; char data[1]; } wkb; @@ -129,13 +130,14 @@ libgeom_export void libgeom_exit(void); * Returns a GEOSGeom, created from a geom_geometry. * On failure, returns NULL. */ -#define wkb2geos( geom ) \ - wkb_isnil((geom))? NULL: \ - GEOSGeomFromWKB_buf((unsigned char *)((geom)->data), (geom)->len) +//#define wkb2geos( geom ) wkb_isnil((geom))? NULL: GEOSGeomFromWKB_buf((unsigned char *)((geom)->data), (geom)->len) #define wkb_nil geos2wkb(NULL); libgeom_export int wkb_isnil(wkb *wkbp); libgeom_export int getMbrGeos(mbr *mbr, const GEOSGeom geosGeometry); libgeom_export int getMbrGeom(mbr *res, wkb *geom); +libgeom_export GEOSGeom wkb2geos(wkb* geomWKB); + +//libgeom_export str geomerty_2_geometry(wkb *res, wkb **geom, int* columnType, int* columnSRID, int* valueSRID); #endif /* LIBGEOM_H */ diff --git a/geom/monetdb5/geom.c b/geom/monetdb5/geom.c --- a/geom/monetdb5/geom.c +++ b/geom/monetdb5/geom.c @@ -62,8 +62,15 @@ geom_export void geom_epilogue(void); geom_export wkb *wkbNULL(void); geom_export mbr *mbrNULL(void); +/* The WKB we use is the EWKB used also in PostGIS + * because we decided that it is easire to carry around + * the SRID */ + /* gets a GEOSGeometry and creates a WKB */ -geom_export wkb *geos2wkb(GEOSGeom geosGeometry); +geom_export wkb* geos2wkb(GEOSGeom geosGeometry); +/* gets a WKB and creates a GEOSGeometry */ +//geom_export GEOSGeom wkb2geos(wkb* geomWKB); + geom_export int mbrFROMSTR(char *src, int *len, mbr **atom); geom_export int mbrTOSTR(char **dst, int *len, mbr *atom); @@ -103,6 +110,45 @@ geom_export str wkbIsValidReason(char** geom_export str wkbIsValidDetail(char** out, wkb **geom); +geom_export str A_2_B(wkb **res, wkb **geo, int* columnType, int* columnSRID); + +str A_2_B(wkb **res, wkb **geo, int* columnType, int* columnSRID) { + GEOSGeom geosGeometry; + int geoCoordinatesNum = 2; + int valueType = 0; + int valueSRID = 0; + + if(valueSRID != *columnSRID) + throw(MAL, "geom.A_2_B", "Geometry should be expressed in SRID %d and it is expressed in SRID %d\n", *columnSRID, valueSRID); + + /* get the geosGeometry from the wkb */ + geosGeometry = wkb2geos(*geo); + /* get the number of coordinates the geometry has */ + geoCoordinatesNum = GEOSGeom_getCoordinateDimension(geosGeometry); + /* get the type of the geometry */ + valueType = (GEOSGeomTypeId(geosGeometry)+1) << 2; + + if(geoCoordinatesNum > 2) + valueType += (1<<1); + if(geoCoordinatesNum > 3) + valueType += 1; + + //fprintf(stderr, "SRDI: %d, type:%d - coordinates:%d\n", *valueSRID, *valueType, geoCoordinatesNum); + + if(valueType != *columnType) + throw(MAL, "geom.A_2_B", "Geometry should be of type %d and it is of type %d\n", *columnType, valueType); + + /* get the wkb from the geosGeometry */ + *res = geos2wkb(geosGeometry); + + + + //fprintf(stderr, "%d - %d vs %d\n", *columnSRID, *columnType, *valueType); + + return MAL_SUCCEED; +} + + /*check if the geometry has z coordinate*/ void geoHasZ(int* res, int* info) { if(GEOMETRY_HAS_Z(*info)) *res=1; @@ -171,6 +217,45 @@ wkb *wkbNULL(void) { return (&nullval); } +wkb* geos2wkb(GEOSGeom geosGeometry) { + size_t wkbLen = 0; + unsigned char *w = NULL; + wkb *geosWKB; + + if (geosGeometry != NULL){ + GEOS_setWKBOutputDims(GEOSGeom_getCoordinateDimension(geosGeometry)); + w = GEOSGeomToWKB_buf(geosGeometry, &wkbLen); + } + + geosWKB = GDKmalloc(wkb_size(wkbLen)); + if (geosWKB == NULL) + return NULL; + + if (geosGeometry == NULL || w == NULL) { + *geosWKB = *wkbNULL(); + } else { + assert(wkbLen <= GDK_int_max); + geosWKB->len = (int) wkbLen; + geosWKB->srid = GEOSGetSRID(geosGeometry); + memcpy(&geosWKB->data, w, wkbLen); + GEOSFree(w); + } + +return geosWKB; +} + +/*GEOSGeom wkb2geos(wkb* geomWKB) { + GEOSGeom geosGeometry; + + if(wkb_isnil(geom)) + return NULL; + + geosGeometry = GEOSGeomFromWKB_buf((unsigned char *)((geomWKB)->data), (geomWKB)->len); + GEOSSetSRID(geosGeometry, (geomWKB)->srid); + + return geosGeometry; +}*/ + /*str wkbFromString(wkb **w, str *wkt) { int len = 0; char *errbuf; @@ -257,59 +342,73 @@ int mbrTOSTR(char **dst, int *len, mbr * /* FROMSTR: parse string to @1. */ /* return number of parsed characters. */ int wkbFROMSTR(char *src, int srid, int *len, wkb **atom) { - GEOSGeom geometry_FROM_wkt = NULL; /* The geometry object that is parsed from the src string. */ - unsigned char *geometry_TO_wkb = NULL; /* The "well known binary" serialization of the geometry object. */ - size_t wkbLen = 0; /* The length of the wkbSer string. */ - int nil = 0; - int coordinateDimensions = 0; + GEOSGeom geosGeometry = NULL; /* The geometry object that is parsed from the src string. */ + //unsigned char *geometry_TO_wkb = NULL; /* The "well known binary" serialization of the geometry object. */ + //size_t wkbLen = 0; /* The length of the wkbSer string. */ + //int nil = 0; + //int coordinateDimensions = 0; + GEOSWKTReader *WKT_reader; - if (strcmp(src, str_nil) == 0) - nil = 1; + if (strcmp(src, str_nil) == 0) { + **atom = *wkbNULL(); + return 0; + } + //nil = 1; - if (!nil) { - GEOSWKTReader *WKT_reader = GEOSWKTReader_create(); - geometry_FROM_wkt = GEOSWKTReader_read(WKT_reader, src); // GEOSGeomFromWKT(src)) == NULL) { - GEOSWKTReader_destroy(WKT_reader); + //if (!nil) { + WKT_reader = GEOSWKTReader_create(); + geosGeometry = GEOSWKTReader_read(WKT_reader, src); // GEOSGeomFromWKT(src)) == NULL) { + GEOSWKTReader_destroy(WKT_reader); - if(geometry_FROM_wkt == NULL) - goto return_nil; + if(geosGeometry == NULL){ + **atom = *wkbNULL(); + return 0; + } + // goto return_nil; + //} + + //it returns 2 or 3. How can I get 4?? + //coordinateDimensions = GEOSGeom_getCoordinateDimension(geosGeometry); + + if (GEOSGeomTypeId(geosGeometry) == -1) { + GEOSGeom_destroy(geosGeometry); + **atom = *wkbNULL(); + return 0; } - ////it returns 2 or 3. How can I get 4?? - coordinateDimensions = GEOSGeom_getCoordinateDimension(geometry_FROM_wkt); +// if (!nil) { + //add the srid + if(srid == 0 ) + GEOSSetSRID(geosGeometry, 4326); + else //should we check whether the srid exists in spatial_ref_sys? + GEOSSetSRID(geosGeometry, srid); + /* the srid was lost with the transformation of the GEOSGeom to wkb + * so we decided to store it in the wkb */ + + //set the number of dimensions + //GEOS_setWKBOutputDims(coordinateDimensions); - if (!nil && GEOSGeomTypeId(geometry_FROM_wkt) == -1) { - GEOSGeom_destroy(geometry_FROM_wkt); - goto return_nil; - } - - if (!nil) { - //add the srid - if(srid == 0 ) - GEOSSetSRID(geometry_FROM_wkt, 4326); - else //should we check whether the srid exists in spatial_ref_sys? - GEOSSetSRID(geometry_FROM_wkt, srid); - //the srid is lost with the transformation of the GEOSGeom to wkb - - //set the number of dimensions - GEOS_setWKBOutputDims(coordinateDimensions); - - geometry_TO_wkb = GEOSGeomToWKB_buf(geometry_FROM_wkt, &wkbLen); - GEOSGeom_destroy(geometry_FROM_wkt); - } + /* we have a GEOSGeometry will number of coordinates and SRID and we + * want to get the wkb out of it */ + *atom = geos2wkb(geosGeometry); +/* +// geometry_TO_wkb = GEOSGeomToWKB_buf(geometry_FROM_wkt, &wkbLen); +// GEOSGeom_destroy(geometry_FROM_wkt); +// } if (*atom == NULL || *len < (int) wkb_size(wkbLen)) { if (*atom) GDKfree(*atom); *atom = GDKmalloc(*len = (int) wkb_size(wkbLen)); } - if (!geometry_TO_wkb) { - **atom = *wkbNULL(); - } else { - assert(wkbLen <= GDK_int_max); - (*atom)->len = (int) wkbLen; - memcpy(&(*atom)->data, geometry_TO_wkb, wkbLen); - GEOSFree(geometry_TO_wkb); - } +// if (!geometry_TO_wkb) { +// **atom = *wkbNULL(); +// } else { + + assert(wkbLen <= GDK_int_max); + (*atom)->len = (int) wkbLen; + memcpy(&(*atom)->data, geometry_TO_wkb, wkbLen); _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list