static void _get_envelope(GEOSGeometry* geometry,Rect* rect) { unsigned int num_pnt; GEOSGeometry* env; const GEOSGeometry* ring; const GEOSCoordSequence* seq; double x,y; unsigned int loop=0; assert(rect != 0); assert(geometry != 0); rect->minX = rect->minY = rect->maxX = rect->maxY = 0; if(GEOSGeomTypeId(geometry) == GEOS_POINT) { GEOSGeomGetX(geometry,&x); GEOSGeomGetY(geometry,&y); rect->minX = rect->maxX = x; rect->minY = rect->maxY = y; return; } env = GEOSEnvelope(geometry); ring = GEOSGetExteriorRing(env); seq = GEOSGeom_getCoordSeq(ring); GEOSCoordSeq_getSize(seq,&num_pnt); if(num_pnt == 0 || num_pnt > 5)return; GEOSCoordSeq_getX(seq,0,&x); GEOSCoordSeq_getY(seq,0,&y); rect->minX = rect->maxX = x; rect->minY = rect->maxY = y; for(loop=1;loop<num_pnt;loop++) { GEOSCoordSeq_getX(seq,loop,&x); GEOSCoordSeq_getY(seq,loop,&y); if(x > rect->maxX) rect->maxX = x; if(x < rect->minX) rect->minX = x; if(y > rect->maxY) rect->maxY = y; if(y < rect->minY) rect->minY = y; } GEOSGeom_destroy(env); }
void geo_y(sqlite3_context *context,int argc,sqlite3_value **argv) { if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_BLOB) { GEOSGeometry* geometry; const void* data = sqlite3_value_blob(argv[0]); size_t data_size = sqlite3_value_bytes(argv[0]); _init_geos(); geometry = _geo_from_wkb((const unsigned char*)data,data_size); if(geometry != 0) { if(GEOSGeomTypeId(geometry) == GEOS_POINT) { double y; GEOSGeomGetY(geometry,&y); sqlite3_result_double(context,y); } } GEOSGeom_destroy(geometry); finishGEOS(); } }
static size_t points_to_geojson(GEOSGeom geom, char *output, int precision) { uint32_t i; char *ptr; char x[BUFSIZE+1]; char y[BUFSIZE+1]; char z[BUFSIZE+1]; uint32_t npoints = 0; numPointsGeometry(&npoints, geom); assert ( precision <= OUT_MAX_DOUBLE_PRECISION ); x[BUFSIZE] = '\0'; y[BUFSIZE] = '\0'; z[BUFSIZE] = '\0'; ptr = output; if ( GEOS_getWKBOutputDims(geom) == 2) { for (i=0; i<npoints; i++) { GEOSGeom point = (GEOSGeom) GEOSGetGeometryN(geom, i); double pt_x, pt_y; GEOSGeomGetX(point, &pt_x); GEOSGeomGetY(point, &pt_y); print_double(pt_x, precision, x, BUFSIZE); trim_trailing_zeros(x); print_double(pt_y, precision, y, BUFSIZE); trim_trailing_zeros(y); if ( i ) ptr += sprintf(ptr, ","); ptr += sprintf(ptr, "[%s,%s]", x, y); } } else { for (i=0; i<npoints; i++) { GEOSGeom point = (GEOSGeom) GEOSGetGeometryN(geom, i); double pt_x, pt_y, pt_z; GEOSGeomGetX(point, &pt_x); GEOSGeomGetY(point, &pt_y); GEOSGeomGetZ(point, &pt_z); print_double(pt_x, precision, x, BUFSIZE); trim_trailing_zeros(x); print_double(pt_y, precision, y, BUFSIZE); trim_trailing_zeros(y); print_double(pt_z, precision, z, BUFSIZE); trim_trailing_zeros(z); if ( i ) ptr += sprintf(ptr, ","); ptr += sprintf(ptr, "[%s,%s,%s]", x, y, z); } } return (ptr-output); }