PostGIS  3.4.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 1614 of file rt_raster.c.

1622 {
1623  int has_z = lwgeom_has_z(lwgeom_in);
1624  int has_m = lwgeom_has_m(lwgeom_in);
1625  LWGEOM *lwgeom;
1626  LWPOINTITERATOR* it;
1627  POINT4D p;
1628  double igt[6] = {0};
1629  rt_errorstate err;
1630  rt_band band = NULL;
1631  double nodatavalue = 0.0;
1632 
1633  /* Get the band reference and read the nodatavalue */
1634  band = rt_raster_get_band(raster, bandnum);
1635  if (!band) {
1636  rterror("unable to read requested band");
1637  return ES_ERROR;
1638  }
1639  rt_band_get_nodata(band, &nodatavalue);
1640 
1641  /* Fluff up geometry to have space for our new dimension */
1642  if (dim == 'z') {
1643  if (has_z)
1644  lwgeom = lwgeom_clone(lwgeom_in);
1645  else if (has_m)
1646  lwgeom = lwgeom_force_4d(lwgeom_in, nodatavalue, nodatavalue);
1647  else
1648  lwgeom = lwgeom_force_3dz(lwgeom_in, nodatavalue);
1649  }
1650  else if (dim == 'm') {
1651  if (has_m)
1652  lwgeom = lwgeom_clone(lwgeom_in);
1653  if (has_z)
1654  lwgeom = lwgeom_force_4d(lwgeom_in, nodatavalue, nodatavalue);
1655  else
1656  lwgeom = lwgeom_force_3dm(lwgeom_in, nodatavalue);
1657  }
1658  else {
1659  rterror("unknown value for dim");
1660  return ES_ERROR;
1661  }
1662 
1663  /* Read every point in the geometry */
1664  it = lwpointiterator_create_rw(lwgeom);
1665  while (lwpointiterator_has_next(it))
1666  {
1667  int nodata;
1668  double xr, yr, value;
1669  lwpointiterator_peek(it, &p);
1670 
1671  /* Convert X/Y world coordinates into raster coordinates */
1672  err = rt_raster_geopoint_to_rasterpoint(raster, p.x, p.y, &xr, &yr, igt);
1673  if (err != ES_NONE) continue;
1674 
1675  /* Read the raster value for this point */
1677  band,
1678  xr, yr,
1679  resample,
1680  &value, &nodata
1681  );
1682 
1683  if (err != ES_NONE) {
1684  value = NAN;
1685  }
1686 
1687  /* Copy in the raster value */
1688  if (dim == 'z')
1689  p.z = value;
1690  if (dim == 'm')
1691  p.m = value;
1692 
1694  }
1696 
1697  if (lwgeom_out)
1698  *lwgeom_out = lwgeom;
1699  return ES_NONE;
1700 }
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:267
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:251
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,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:219
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:1221
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1887
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:852
#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: