PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum RASTER_worldToRasterCoord ( PG_FUNCTION_ARGS  )

Definition at line 13203 of file rt_pg.c.

References ES_NONE, FALSE, FLT_NEQ, POSTGIS_RT_DEBUG, rtrowdump::raster, result, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_geopoint_to_cell(), rt_raster_get_x_skew(), rt_raster_get_y_skew(), and TRUE.

13204 {
13205  rt_pgraster *pgraster = NULL;
13206  rt_raster raster = NULL;
13207  int i = 0;
13208  double cw[2] = {0};
13209  double _cr[2] = {0};
13210  int cr[2] = {0};
13211  bool skewed = false;
13212 
13213  TupleDesc tupdesc;
13214  int values_length = 2;
13215  Datum values[values_length];
13216  bool nulls[values_length];
13217  HeapTuple tuple;
13218  Datum result;
13219 
13220  POSTGIS_RT_DEBUG(3, "RASTER_worldToRasterCoord: Starting");
13221 
13222  /* pgraster is null, return null */
13223  if (PG_ARGISNULL(0))
13224  PG_RETURN_NULL();
13225  pgraster = (rt_pgraster *) PG_DETOAST_DATUM_SLICE(PG_GETARG_DATUM(0), 0, sizeof(struct rt_raster_serialized_t));
13226 
13227  /* raster */
13228  raster = rt_raster_deserialize(pgraster, TRUE);
13229  if (!raster) {
13230  PG_FREE_IF_COPY(pgraster, 0);
13231  elog(ERROR, "RASTER_worldToRasterCoord: Could not deserialize raster");
13232  PG_RETURN_NULL();
13233  }
13234 
13235  /* raster skewed? */
13236  skewed = FLT_NEQ(rt_raster_get_x_skew(raster), 0) ? true : false;
13237  if (!skewed)
13238  skewed = FLT_NEQ(rt_raster_get_y_skew(raster), 0) ? true : false;
13239 
13240  /* longitude and latitude */
13241  for (i = 1; i <= 2; i++) {
13242  if (PG_ARGISNULL(i)) {
13243  /* if skewed, parameter is required */
13244  if (skewed) {
13245  elog(NOTICE, "Latitude and longitude required for computing pixel row and column of a rotated raster");
13246  rt_raster_destroy(raster);
13247  PG_FREE_IF_COPY(pgraster, 0);
13248  PG_RETURN_NULL();
13249  }
13250 
13251  continue;
13252  }
13253 
13254  cw[i - 1] = PG_GETARG_FLOAT8(i);
13255  }
13256 
13257  /* return pixel row and column values are 0-based */
13259  raster,
13260  cw[0], cw[1],
13261  &(_cr[0]), &(_cr[1]),
13262  NULL
13263  ) != ES_NONE) {
13264  rt_raster_destroy(raster);
13265  PG_FREE_IF_COPY(pgraster, 0);
13266  elog(ERROR, "RASTER_worldToRasterCoord: Could not compute pixel row and column from longitude and latitude");
13267  PG_RETURN_NULL();
13268  }
13269  rt_raster_destroy(raster);
13270  PG_FREE_IF_COPY(pgraster, 0);
13271 
13272  /* force to integer and add one to make 1-based */
13273  cr[0] = ((int) _cr[0]) + 1;
13274  cr[1] = ((int) _cr[1]) + 1;
13275 
13276  /* Build a tuple descriptor for our result type */
13277  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
13278  ereport(ERROR, (
13279  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13280  errmsg(
13281  "function returning record called in context "
13282  "that cannot accept type record"
13283  )
13284  ));
13285  }
13286 
13287  BlessTupleDesc(tupdesc);
13288 
13289  values[0] = Int32GetDatum(cr[0]);
13290  values[1] = Int32GetDatum(cr[1]);
13291 
13292  memset(nulls, FALSE, sizeof(bool) * values_length);
13293 
13294  /* build a tuple */
13295  tuple = heap_form_tuple(tupdesc, values, nulls);
13296 
13297  /* make the tuple into a datum */
13298  result = HeapTupleGetDatum(tuple);
13299 
13300  PG_RETURN_DATUM(result);
13301 }
#define FLT_NEQ(x, y)
Definition: rt_api.h:2158
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_api.c:5486
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_api.c:5495
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:123
char ** result
Definition: liblwgeom.h:218
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rt_pg.h:58
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
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.
Definition: rt_api.c:6105
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_api.c:8350
#define TRUE
Definition: dbfopen.c:170

Here is the call graph for this function: