File: pc_point.c

package info (click to toggle)
pgpointcloud 1.2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,892 kB
  • sloc: sql: 40,767; ansic: 11,045; xml: 935; makefile: 297; cpp: 282; perl: 248; python: 178; sh: 92
file content (395 lines) | stat: -rw-r--r-- 8,724 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***********************************************************************
 * pc_point.c
 *
 *  Pointclound point handling. Create, get and set values from the
 *  basic PCPOINT structure.
 *
 *  PgSQL Pointcloud is free and open source software provided
 *  by the Government of Canada
 *  Copyright (c) 2013 Natural Resources Canada
 *
 ***********************************************************************/

#include "pc_api_internal.h"
#include "stringbuffer.h"

PCPOINT *pc_point_make(const PCSCHEMA *s)
{
  size_t sz;
  PCPOINT *pt;

  if (!s)
  {
    pcerror("null schema passed into pc_point_make");
    return NULL;
  }

  /* Width of the data area */
  sz = s->size;
  if (!sz)
  {
    pcerror("invalid size calculation in pc_point_make");
    return NULL;
  }

  /* Make our own data area */
  pt = pcalloc(sizeof(PCPOINT));
  pt->data = pcalloc(sz);

  /* Set up basic info */
  pt->schema = s;
  pt->readonly = PC_FALSE;
  return pt;
};

PCPOINT *pc_point_from_data(const PCSCHEMA *s, const uint8_t *data)
{
  PCPOINT *pt;

  if (!s)
  {
    pcerror("null schema passed into pc_point_from_data");
    return NULL;
  }

  /* Reference the external data */
  pt = pcalloc(sizeof(PCPOINT));
  pt->data = (uint8_t *)data;

  /* Set up basic info */
  pt->schema = s;
  pt->readonly = PC_TRUE;
  return pt;
}

void pc_point_free(PCPOINT *pt)
{
  if (!pt->readonly)
  {
    pcfree(pt->data);
  }
  pcfree(pt);
}

int pc_point_get_double(const PCPOINT *pt, const PCDIMENSION *dim, double *val)
{
  uint8_t *ptr;
  double d;

  if (!dim)
    return PC_FAILURE;

  /* Read raw value from byte buffer */
  ptr = pt->data + dim->byteoffset;
  d = pc_double_from_ptr(ptr, dim->interpretation);
  d = pc_value_scale_offset(d, dim);

  *val = d;
  return PC_SUCCESS;
}

int pc_point_get_double_by_name(const PCPOINT *pt, const char *name,
                                double *val)
{
  PCDIMENSION *dim;
  dim = pc_schema_get_dimension_by_name(pt->schema, name);
  return pc_point_get_double(pt, dim, val);
}

int pc_point_get_double_by_index(const PCPOINT *pt, uint32_t idx, double *val)
{
  PCDIMENSION *dim;
  dim = pc_schema_get_dimension(pt->schema, idx);
  return pc_point_get_double(pt, dim, val);
}

int pc_point_set_double(PCPOINT *pt, const PCDIMENSION *dim, double val)
{
  uint8_t *ptr;

  if (!dim)
    return PC_FAILURE;

  /* Remove scale and offsets */
  val = pc_value_unscale_unoffset(val, dim);

  /* Get pointer into byte buffer */
  ptr = pt->data + dim->byteoffset;

  return pc_double_to_ptr(ptr, dim->interpretation, val);
}

int pc_point_set_double_by_index(PCPOINT *pt, uint32_t idx, double val)
{
  PCDIMENSION *dim;
  dim = pc_schema_get_dimension(pt->schema, idx);
  return pc_point_set_double(pt, dim, val);
}

int pc_point_set_double_by_name(PCPOINT *pt, const char *name, double val)
{
  PCDIMENSION *dim;
  dim = pc_schema_get_dimension_by_name(pt->schema, name);
  return pc_point_set_double(pt, dim, val);
}

int pc_point_get_x(const PCPOINT *pt, double *val)
{
  return pc_point_get_double(pt, pt->schema->xdim, val);
}

int pc_point_get_y(const PCPOINT *pt, double *val)
{
  return pc_point_get_double(pt, pt->schema->ydim, val);
}

int pc_point_get_z(const PCPOINT *pt, double *val)
{
  return pc_point_get_double(pt, pt->schema->zdim, val);
}

int pc_point_get_m(const PCPOINT *pt, double *val)
{
  return pc_point_get_double(pt, pt->schema->mdim, val);
}

int pc_point_set_x(PCPOINT *pt, double val)
{
  return pc_point_set_double(pt, pt->schema->xdim, val);
}

int pc_point_set_y(PCPOINT *pt, double val)
{
  return pc_point_set_double(pt, pt->schema->ydim, val);
}

int pc_point_set_z(PCPOINT *pt, double val)
{
  return pc_point_set_double(pt, pt->schema->zdim, val);
}

int pc_point_set_m(PCPOINT *pt, double val)
{
  return pc_point_set_double(pt, pt->schema->mdim, val);
}

char *pc_point_to_string(const PCPOINT *pt)
{
  /* { "pcid":1, "values":[<dim1>, <dim2>, <dim3>, <dim4>] }*/
  stringbuffer_t *sb = stringbuffer_create();
  char *str;
  int i;

  stringbuffer_aprintf(sb, "{\"pcid\":%d,\"pt\":[", pt->schema->pcid);
  for (i = 0; i < pt->schema->ndims; i++)
  {
    double d;
    if (!pc_point_get_double_by_index(pt, i, &d))
    {
      pcerror("pc_point_to_string: unable to read double at position %d", i);
    }
    if (i)
    {
      stringbuffer_append(sb, ",");
    }
    stringbuffer_aprintf(sb, "%g", d);
  }
  stringbuffer_append(sb, "]}");
  str = stringbuffer_getstringcopy(sb);
  stringbuffer_destroy(sb);
  return str;
}

PCPOINT *pc_point_from_double_array(const PCSCHEMA *s, double *array,
                                    uint32_t offset, uint32_t stride)
{
  int i;
  PCPOINT *pt;

  if (!s)
  {
    pcerror("null schema passed into pc_point_from_double_array");
    return NULL;
  }

  if (stride != s->ndims)
  {
    pcerror("number of elements in schema and array do not match in "
            "pc_point_from_double_array");
    return NULL;
  }

  /* Reference the external data */
  pt = pcalloc(sizeof(PCPOINT));
  pt->data = pcalloc(s->size);
  pt->schema = s;
  pt->readonly = PC_FALSE;

  for (i = 0; i < stride; i++)
  {
    if (PC_FAILURE == pc_point_set_double_by_index(pt, i, array[offset + i]))
    {
      pcerror("failed to write value into dimension %d in "
              "pc_point_from_double_array",
              i);
      return NULL;
    }
  }

  return pt;
}

PCPOINT *pc_point_from_wkb(const PCSCHEMA *schema, uint8_t *wkb, size_t wkblen)
{
  /*
  byte:     endianness (1 = NDR, 0 = XDR)
  uint32:   pcid (key to POINTCLOUD_SCHEMAS)
  uchar[]:  data (interpret relative to pcid)
  */
  const size_t hdrsz = 1 + 4; /* endian + pcid */
  uint8_t wkb_endian;
  uint8_t *data;
  PCPOINT *pt;

  if (!wkblen)
  {
    pcerror("pc_point_from_wkb: zero length wkb");
  }

  wkb_endian = wkb[0];

  if ((wkblen - hdrsz) != schema->size)
  {
    pcerror("pc_point_from_wkb: wkb size inconsistent with schema size");
  }

  if (wkb_endian != machine_endian())
  {
    /* uncompressed_bytes_flip_endian creates a flipped copy */
    data = uncompressed_bytes_flip_endian(wkb + hdrsz, schema, 1);
  }
  else
  {
    data = pcalloc(schema->size);
    memcpy(data, wkb + hdrsz, wkblen - hdrsz);
  }

  pt = pc_point_from_data(schema, data);
  pt->readonly = PC_FALSE;
  return pt;
}

uint8_t *pc_point_to_wkb(const PCPOINT *pt, size_t *wkbsize)
{
  /*
  byte:     endianness (1 = NDR, 0 = XDR)
  uint32:   pcid (key to POINTCLOUD_SCHEMAS)
  uchar[]:  data (interpret relative to pcid)
  */
  char endian = machine_endian();
  size_t size = 1 + 4 + pt->schema->size;
  uint8_t *wkb = pcalloc(size);
  wkb[0] = endian;                             /* Write endian flag */
  memcpy(wkb + 1, &(pt->schema->pcid), 4);     /* Write PCID */
  memcpy(wkb + 5, pt->data, pt->schema->size); /* Write data */
  if (wkbsize)
    *wkbsize = size;
  return wkb;
}

uint8_t *pc_point_to_geometry_wkb(const PCPOINT *pt, size_t *wkbsize)
{
  static uint32_t srid_mask = 0x20000000;
  static uint32_t m_mask = 0x40000000;
  static uint32_t z_mask = 0x80000000;
  uint32_t wkbtype = 1;        /* WKB POINT */
  size_t size = 1 + 4 + 8 + 8; /* endian + type + dblX, + dblY */
  uint8_t *wkb, *ptr;
  uint32_t srid = pt->schema->srid;
  double x, y, z, m;
  int has_x = pc_point_get_x(pt, &x) == PC_SUCCESS;
  int has_y = pc_point_get_y(pt, &y) == PC_SUCCESS;
  int has_z = pc_point_get_z(pt, &z) == PC_SUCCESS;
  int has_m = pc_point_get_m(pt, &m) == PC_SUCCESS;

  if (!(has_x && has_y))
    return NULL;

  if (srid)
  {
    wkbtype |= srid_mask;
    size += 4;
  }

  if (has_z)
  {
    wkbtype |= z_mask;
    size += 8;
  }

  if (has_m)
  {
    wkbtype |= m_mask;
    size += 8;
  }

  wkb = pcalloc(size);
  ptr = wkb;

  ptr[0] = machine_endian(); /* Endian flag */
  ptr += 1;

  memcpy(ptr, &wkbtype, 4); /* WKB type */
  ptr += 4;

  if (srid != 0)
  {
    memcpy(ptr, &srid, 4); /* SRID */
    ptr += 4;
  }

  memcpy(ptr, &x, 8); /* X */
  ptr += 8;

  memcpy(ptr, &y, 8); /* Y */
  ptr += 8;

  if (has_z)
  {
    memcpy(ptr, &z, 8); /* Z */
    ptr += 8;
  }

  if (has_m)
  {
    memcpy(ptr, &m, 8); /* M */
    ptr += 8;
  }

  if (wkbsize)
    *wkbsize = size;
  return wkb;
}

/**
 * @brief this function convert a PCPOINT to an array of double containing
 *        all the dimension values of this point
 *
 * @param a pointer to the point to convert to double
 *
 * @return a pointer to an array of double containing all the dimensions
 *         of the point expressed as double precision
 *
 */
double *pc_point_to_double_array(const PCPOINT *p)
{
  int i;
  double *a = (double *)pcalloc(p->schema->ndims * sizeof(double));

  for (i = 0; i < p->schema->ndims; ++i)
  {
    pc_point_get_double_by_index(p, i, &(a[i]));
  }

  return a;
}