/** Function initializing closestpoint calculations. */ LWGEOM * lw_dist2d_distancepoint(LWGEOM *lw1, LWGEOM *lw2,int srid,int mode) { double x,y; DISTPTS thedl; double initdistance = MAXFLOAT; LWGEOM *result; thedl.mode = mode; thedl.distance= initdistance; thedl.tolerance = 0; LWDEBUG(2, "lw_dist2d_distancepoint is called"); if (!lw_dist2d_comp( lw1,lw2,&thedl)) { /*should never get here. all cases ought to be error handled earlier*/ lwerror("Some unspecified error."); result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); } if (thedl.distance == initdistance) { LWDEBUG(3, "didn't find geometries to measure between, returning null"); result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); } else { x=thedl.p1.x; y=thedl.p1.y; result = (LWGEOM *)lwpoint_make2d(srid, x, y); } return result; }
Datum point_from_geohash(PG_FUNCTION_ARGS) { GBOX *box = NULL; LWPOINT *point = NULL; GSERIALIZED *result = NULL; text *geohash_input = NULL; char *geohash = NULL; double lon, lat; int precision = -1; if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); } if (!PG_ARGISNULL(1)) { precision = PG_GETARG_INT32(1); } geohash_input = PG_GETARG_TEXT_P(0); geohash = text2cstring(geohash_input); box = parse_geohash(geohash, precision); lon = box->xmin + (box->xmax - box->xmin) / 2; lat = box->ymin + (box->ymax - box->ymin) / 2; point = lwpoint_make2d(SRID_UNKNOWN, lon, lat); result = geometry_serialize((LWGEOM *) point); lwfree(box); PG_RETURN_POINTER(result); }
/** Function initializing shortestline and longestline calculations. */ LWGEOM * lw_dist2d_distanceline(LWGEOM *lw1, LWGEOM *lw2,int srid,int mode) { double x1,x2,y1,y2; double initdistance = ( mode == DIST_MIN ? MAXFLOAT : -1.0); DISTPTS thedl; LWPOINT *lwpoints[2]; LWGEOM *result; thedl.mode = mode; thedl.distance = initdistance; thedl.tolerance = 0.0; LWDEBUG(2, "lw_dist2d_distanceline is called"); if (!lw_dist2d_comp( lw1,lw2,&thedl)) { /*should never get here. all cases ought to be error handled earlier*/ lwerror("Some unspecified error."); result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); } /*if thedl.distance is unchanged there where only empty geometries input*/ if (thedl.distance == initdistance) { LWDEBUG(3, "didn't find geometries to measure between, returning null"); result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); } else { x1=thedl.p1.x; y1=thedl.p1.y; x2=thedl.p2.x; y2=thedl.p2.y; lwpoints[0] = lwpoint_make2d(srid, x1, y1); lwpoints[1] = lwpoint_make2d(srid, x2, y2); result = (LWGEOM *)lwline_from_lwpointarray(srid, 2, lwpoints); } return result; }
Datum BOX2D_to_LWGEOM(PG_FUNCTION_ARGS) { GBOX *box = (GBOX *)PG_GETARG_POINTER(0); POINTARRAY *pa = ptarray_construct_empty(0, 0, 5); POINT4D pt; GSERIALIZED *result; /* * Alter BOX2D cast so that a valid geometry is always * returned depending upon the size of the BOX2D. The * code makes the following assumptions: * - If the BOX2D is a single point then return a * POINT geometry * - If the BOX2D represents either a horizontal or * vertical line, return a LINESTRING geometry * - Otherwise return a POLYGON */ if ( (box->xmin == box->xmax) && (box->ymin == box->ymax) ) { /* Construct and serialize point */ LWPOINT *point = lwpoint_make2d(SRID_UNKNOWN, box->xmin, box->ymin); result = geometry_serialize(lwpoint_as_lwgeom(point)); lwpoint_free(point); } else if ( (box->xmin == box->xmax) || (box->ymin == box->ymax) ) { LWLINE *line; /* Assign coordinates to point array */ pt.x = box->xmin; pt.y = box->ymin; ptarray_append_point(pa, &pt, LW_TRUE); pt.x = box->xmax; pt.y = box->ymax; ptarray_append_point(pa, &pt, LW_TRUE); /* Construct and serialize linestring */ line = lwline_construct(SRID_UNKNOWN, NULL, pa); result = geometry_serialize(lwline_as_lwgeom(line)); lwline_free(line); } else { LWPOLY *poly; POINTARRAY **ppa = lwalloc(sizeof(POINTARRAY*)); /* Assign coordinates to point array */ pt.x = box->xmin; pt.y = box->ymin; ptarray_append_point(pa, &pt, LW_TRUE); pt.x = box->xmin; pt.y = box->ymax; ptarray_append_point(pa, &pt, LW_TRUE); pt.x = box->xmax; pt.y = box->ymax; ptarray_append_point(pa, &pt, LW_TRUE); pt.x = box->xmax; pt.y = box->ymin; ptarray_append_point(pa, &pt, LW_TRUE); pt.x = box->xmin; pt.y = box->ymin; ptarray_append_point(pa, &pt, LW_TRUE); /* Construct polygon */ ppa[0] = pa; poly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, ppa); result = geometry_serialize(lwpoly_as_lwgeom(poly)); lwpoly_free(poly); } PG_RETURN_POINTER(result); }