PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ rt_raster_copy_to_geometry()

rt_errorstate rt_raster_copy_to_geometry ( rt_raster  raster,
uint32_t  bandnum,
char  dim,
rt_resample_type  resample,
const LWGEOM lwgeom_in,
LWGEOM **  lwgeom_out 
)

Copy values from a raster to the points on a geometry using the requested interpolation type.

and selected interpolation.

Parameters
raster: the raster to read for values
bandnum: the band number to read from
dim: the geometry dimension to copy values into 'Z' or 'M'
resample: algorithm for reading raster (nearest or bilinear)
lwgeom_in: the input geometry
lwgeom_out: pointer for the output geometry
Returns
ES_ERROR on error, otherwise ES_NONE

Definition at line 1492 of file rt_raster.c.

1500 {
1501  int has_z = lwgeom_has_z(lwgeom_in);
1502  int has_m = lwgeom_has_m(lwgeom_in);
1503  LWGEOM *lwgeom;
1504  LWPOINTITERATOR* it;
1505  POINT4D p;
1506  double igt[6] = {0};
1507  rt_errorstate err;
1508  rt_band band = NULL;
1509  double nodatavalue = 0.0;
1510 
1511  /* Get the band reference and read the nodatavalue */
1512  band = rt_raster_get_band(raster, bandnum);
1513  if (!band) {
1514  rterror("unable to read requested band");
1515  return ES_ERROR;
1516  }
1517  rt_band_get_nodata(band, &nodatavalue);
1518 
1519  /* Fluff up geometry to have space for our new dimension */
1520  if (dim == 'z') {
1521  if (has_z)
1522  lwgeom = lwgeom_clone(lwgeom_in);
1523  else if (has_m)
1524  lwgeom = lwgeom_force_4d(lwgeom_in, nodatavalue, nodatavalue);
1525  else
1526  lwgeom = lwgeom_force_3dz(lwgeom_in, nodatavalue);
1527  }
1528  else if (dim == 'm') {
1529  if (has_m)
1530  lwgeom = lwgeom_clone(lwgeom_in);
1531  if (has_z)
1532  lwgeom = lwgeom_force_4d(lwgeom_in, nodatavalue, nodatavalue);
1533  else
1534  lwgeom = lwgeom_force_3dm(lwgeom_in, nodatavalue);
1535  }
1536  else {
1537  rterror("unknown value for dim");
1538  return ES_ERROR;
1539  }
1540 
1541  /* Read every point in the geometry */
1542  it = lwpointiterator_create_rw(lwgeom);
1543  while (lwpointiterator_has_next(it))
1544  {
1545  int nodata;
1546  double xr, yr, value;
1547  lwpointiterator_peek(it, &p);
1548 
1549  /* Convert X/Y world coordinates into raster coordinates */
1550  err = rt_raster_geopoint_to_rasterpoint(raster, p.x, p.y, &xr, &yr, igt);
1551  if (err != ES_NONE) continue;
1552 
1553  /* Read the raster value for this point */
1555  band,
1556  xr, yr,
1557  resample,
1558  &value, &nodata
1559  );
1560 
1561  if (err != ES_NONE) {
1562  value = NAN;
1563  }
1564 
1565  /* Copy in the raster value */
1566  if (dim == 'z')
1567  p.z = value;
1568  if (dim == 'm')
1569  p.m = value;
1570 
1572  }
1574 
1575  if (lwgeom_out)
1576  *lwgeom_out = lwgeom;
1577  return ES_NONE;
1578 }
int lwpointiterator_peek(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assigns the next point in the iterator to p.
Definition: lwiterator.c:193
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
Definition: lwiterator.c:268
LWGEOM * lwgeom_force_3dm(const LWGEOM *geom, double mval)
Definition: lwgeom.c:805
LWGEOM * lwgeom_force_4d(const LWGEOM *geom, double zval, double mval)
Definition: lwgeom.c:811
LWGEOM * lwgeom_force_3dz(const LWGEOM *geom, double zval)
Definition: lwgeom.c:799
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
int lwpointiterator_modify_next(LWPOINTITERATOR *s, const POINT4D *p)
Attempts to replace the next point int the iterator with p, and advances the iterator to the next poi...
Definition: lwiterator.c:224
LWPOINTITERATOR * lwpointiterator_create_rw(LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM* Supports modification of coordinates during iterat...
Definition: lwiterator.c:252
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:491
int lwpointiterator_has_next(LWPOINTITERATOR *s)
Returns LW_TRUE if there is another point available in the iterator.
Definition: lwiterator.c:202
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:941
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
rt_errorstate
Enum definitions.
Definition: librtcore.h:181
@ ES_NONE
Definition: librtcore.h:182
@ ES_ERROR
Definition: librtcore.h:183
rt_errorstate rt_band_get_pixel_resample(rt_band band, double xr, double yr, rt_resample_type resample, double *r_value, int *r_nodata)
Retrieve a point value from the raster using a world coordinate and selected resampling method.
Definition: rt_band.c:1372
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:2038
int value
Definition: genraster.py:62
band
Definition: ovdump.py:58
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
rt_errorstate rt_raster_geopoint_to_rasterpoint(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.
Definition: rt_raster.c:730
#define NAN
Definition: rt_raster.c:37
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:385
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414

References ovdump::band, ES_ERROR, ES_NONE, lwgeom_clone(), lwgeom_force_3dm(), lwgeom_force_3dz(), lwgeom_force_4d(), lwgeom_has_m(), lwgeom_has_z(), lwpointiterator_create_rw(), lwpointiterator_destroy(), lwpointiterator_has_next(), lwpointiterator_modify_next(), lwpointiterator_peek(), POINT4D::m, NAN, rtrowdump::raster, rt_band_get_nodata(), rt_band_get_pixel_resample(), rt_raster_geopoint_to_rasterpoint(), rt_raster_get_band(), rterror(), genraster::value, POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by RASTER_getGeometryValues().

Here is the call graph for this function:
Here is the caller graph for this function: