974 double _igt[6] = {0};
987 GEOSGeometry *sgeom = NULL;
988 GEOSGeometry *ngeom = NULL;
996 else if (tolerance > 1.)
1000 while (dbl_run < 10) {
1008 for (i = 0; i < 2; i++) {
1009 if (
FLT_EQ(scale[i], 0)) {
1010 rterror(
"rt_raster_compute_skewed_raster: Scale cannot be zero");
1015 _gt[1] = fabs(scale[i] * tolerance);
1017 _gt[5] = fabs(scale[i] * tolerance);
1030 (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (fabs(scale[0]) / 2.)) / fabs(scale[0]), 1),
1031 (
int) fmax((fabs(extent.
MaxY - extent.
MinY) + (fabs(scale[1]) / 2.)) / fabs(scale[1]), 1)
1035 if (raster == NULL) {
1036 rterror(
"rt_raster_compute_skewed_raster: Could not create output raster");
1055 _gt[2] = skew[0] * tolerance;
1057 _gt[4] = skew[1] * tolerance;
1059 RASTER_DEBUGF(4,
"Initial geotransform: %f, %f, %f, %f, %f, %f",
1060 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1066 rterror(
"rt_raster_compute_skewed_raster: Out of memory allocating extent raster");
1072 if (!GDALInvGeoTransform(_gt, _igt)) {
1073 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1077 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1078 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1082 for (i = 0; i < 2; i++) {
1086 RASTER_DEBUGF(3,
"Shifting along %s axis", i < 1 ?
"X" :
"Y");
1091 if (run > max_run) {
1092 rterror(
"rt_raster_compute_skewed_raster: Could not compute skewed extent due to check preventing infinite loop");
1101 for (j = 0; j < 4; j++) {
1105 _xy[0] = extent.
MinX;
1106 _xy[1] = extent.
MaxY;
1110 _xy[0] = extent.
MinX;
1111 _xy[1] = extent.
MinY;
1115 _xy[0] = extent.
MaxX;
1116 _xy[1] = extent.
MinY;
1120 _xy[0] = extent.
MaxX;
1121 _xy[1] = extent.
MaxY;
1132 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1137 RASTER_DEBUGF(4,
"Point %d at cell %d x %d", j, (
int) _r[0], (
int) _r[1]);
1140 if ((
int) _r[i] < 0) {
1144 if (_dlastpos != j) {
1145 _dlast = (int) _r[i];
1148 else if ((
int) _r[i] < _dlast) {
1149 RASTER_DEBUG(4,
"Point going in wrong direction. Reversing direction");
1165 x = _d[i] * fabs(_r[i]);
1167 y = _d[i] * fabs(_r[i]);
1176 rterror(
"rt_raster_compute_skewed_raster: Could not compute spatial coordinates for raster pixel");
1187 RASTER_DEBUGF(4,
"Shifted geotransform: %f, %f, %f, %f, %f, %f",
1188 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]
1192 if (!GDALInvGeoTransform(_gt, _igt)) {
1193 rterror(
"rt_raster_compute_skewed_raster: Could not compute inverse geotransform matrix");
1197 RASTER_DEBUGF(4,
"Inverse geotransform: %f, %f, %f, %f, %f, %f",
1198 _igt[0], _igt[1], _igt[2], _igt[3], _igt[4], _igt[5]
1215 rterror(
"rt_raster_compute_skewed_raster: Could not compute raster pixel for spatial coordinates");
1220 RASTER_DEBUGF(4,
"geopoint %f x %f at cell %d x %d", extent.
MaxX, extent.
MinY, (
int) _r[0], (
int) _r[1]);
1222 raster->
width = _r[0];
1231 if (npoly == NULL) {
1232 rterror(
"rt_raster_compute_skewed_raster: Could not build extent's geometry for covers test");
1246 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for covers test");
1247 GEOSGeom_destroy(ngeom);
1255 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1256 GEOSGeom_destroy(sgeom);
1259 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test");
1260 GEOSGeom_destroy(ngeom);
1275 raster->
width = (int) ((((
double) raster->
width) * fabs(_gt[1]) + fabs(scale[0] / 2.)) / fabs(scale[0]));
1276 raster->
height = (int) ((((
double) raster->
height) * fabs(_gt[5]) + fabs(scale[1] / 2.)) / fabs(scale[1]));
1277 _gt[1] = fabs(scale[0]);
1278 _gt[5] = -1 * fabs(scale[1]);
1284 for (i = 0; i < 2; i++) {
1294 rterror(
"rt_raster_compute_skewed_raster: Could not build skewed extent's geometry for minimizing dimensions");
1295 GEOSGeom_destroy(ngeom);
1303 covers = GEOSRelatePattern(sgeom, ngeom,
"******FF*");
1304 GEOSGeom_destroy(sgeom);
1307 rterror(
"rt_raster_compute_skewed_raster: Could not run covers test for minimizing dimensions");
1308 GEOSGeom_destroy(ngeom);
1325 GEOSGeom_destroy(ngeom);
Datum covers(PG_FUNCTION_ARGS)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
void lwgeom_free(LWGEOM *geom)
rt_errorstate rt_raster_cell_to_geopoint(rt_raster raster, double xr, double yr, double *xw, double *yw, double *gt)
Convert an xr, yr raster point to an xw, yw point on map.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
LWPOLY * rt_util_envelope_to_lwpoly(rt_envelope ext)
rt_errorstate rt_raster_geopoint_to_cell(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw,yw map point to a xr,yr raster point.
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
void lwgeom_geos_error(const char *fmt,...)
void lwpoly_free(LWPOLY *poly)
void rtinfo(const char *fmt,...)
#define RASTER_DEBUGF(level, msg,...)
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
#define RASTER_DEBUG(level, msg)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.