LWPOLY* lwpoly_grid(const LWPOLY *poly, const gridspec *grid) { LWPOLY *opoly; int ri; #if 0 /* * TODO: control this assertion * it is assumed that, since the grid size will be a pixel, * a visible ring should show at least a white pixel inside, * thus, for a square, that would be grid_xsize*grid_ysize */ double minvisiblearea = grid->xsize * grid->ysize; #endif LWDEBUGF(3, "lwpoly_grid: applying grid to polygon with %d rings", poly->nrings); opoly = lwpoly_construct_empty(poly->srid, lwgeom_has_z((LWGEOM*)poly), lwgeom_has_m((LWGEOM*)poly)); for (ri=0; ri<poly->nrings; ri++) { POINTARRAY *ring = poly->rings[ri]; POINTARRAY *newring; newring = ptarray_grid(ring, grid); /* Skip ring if not composed by at least 4 pts (3 segments) */ if ( newring->npoints < 4 ) { ptarray_free(newring); LWDEBUGF(3, "grid_polygon3d: ring%d skipped ( <4 pts )", ri); if ( ri ) continue; else break; /* this is the external ring, no need to work on holes */ } if ( ! lwpoly_add_ring(opoly, newring) ) { lwerror("lwpoly_grid, memory error"); return NULL; } } LWDEBUGF(3, "lwpoly_grid: simplified polygon with %d rings", opoly->nrings); if ( ! opoly->nrings ) { lwpoly_free(opoly); return NULL; } return opoly; }
LWPOINT * lwpoint_grid(LWPOINT *point, gridspec *grid) { LWPOINT *opoint; POINTARRAY *opa; opa = ptarray_grid(point->point, grid); /* TODO: grid bounding box ? */ opoint = lwpoint_construct(point->srid, NULL, opa); POSTGIS_DEBUG(2, "lwpoint_grid called"); return opoint; }
LWCIRCSTRING * lwcirc_grid(LWCIRCSTRING *line, gridspec *grid) { LWCIRCSTRING *oline; POINTARRAY *opa; opa = ptarray_grid(line->points, grid); /* Skip line3d with less then 2 points */ if ( opa->npoints < 2 ) return NULL; /* TODO: grid bounding box... */ oline = lwcircstring_construct(line->srid, NULL, opa); return oline; }
LWPOLY * lwpoly_grid(LWPOLY *poly, gridspec *grid) { LWPOLY *opoly; int ri; POINTARRAY **newrings = NULL; int nrings = 0; double minvisiblearea; /* * TODO: control this assertion * it is assumed that, since the grid size will be a pixel, * a visible ring should show at least a white pixel inside, * thus, for a square, that would be grid_xsize*grid_ysize */ minvisiblearea = grid->xsize * grid->ysize; nrings = 0; POSTGIS_DEBUGF(3, "grid_polygon3d: applying grid to polygon with %d rings", poly->nrings); for (ri=0; ri<poly->nrings; ri++) { POINTARRAY *ring = poly->rings[ri]; POINTARRAY *newring; #if POSTGIS_DEBUG_LEVEL >= 4 POINT2D p1, p2; getPoint2d_p(ring, 0, &p1); getPoint2d_p(ring, ring->npoints-1, &p2); if ( ! SAMEPOINT(&p1, &p2) ) POSTGIS_DEBUG(4, "Before gridding: first point != last point"); #endif newring = ptarray_grid(ring, grid); /* Skip ring if not composed by at least 4 pts (3 segments) */ if ( newring->npoints < 4 ) { pfree(newring); POSTGIS_DEBUGF(3, "grid_polygon3d: ring%d skipped ( <4 pts )", ri); if ( ri ) continue; else break; /* this is the external ring, no need to work on holes */ } #if POSTGIS_DEBUG_LEVEL >= 4 getPoint2d_p(newring, 0, &p1); getPoint2d_p(newring, newring->npoints-1, &p2); if ( ! SAMEPOINT(&p1, &p2) ) POSTGIS_DEBUG(4, "After gridding: first point != last point"); #endif POSTGIS_DEBUGF(3, "grid_polygon3d: ring%d simplified from %d to %d points", ri, ring->npoints, newring->npoints); /* * Add ring to simplified ring array * (TODO: dinamic allocation of pts_per_ring) */ if ( ! nrings ) { newrings = palloc(sizeof(POINTARRAY *)); } else { newrings = repalloc(newrings, sizeof(POINTARRAY *)*(nrings+1)); } if ( ! newrings ) { elog(ERROR, "Out of virtual memory"); return NULL; } newrings[nrings++] = newring; } POSTGIS_DEBUGF(3, "grid_polygon3d: simplified polygon with %d rings", nrings); if ( ! nrings ) return NULL; opoly = lwpoly_construct(poly->srid, NULL, nrings, newrings); return opoly; }