Convert to using public ght api

This commit is contained in:
Paul Ramsey 2013-05-03 15:24:56 -07:00
parent 309dddf4c8
commit 4d2187af38
7 changed files with 140 additions and 75 deletions

View File

@ -30,5 +30,6 @@ More Functions
- PC_FilterLessThan(patch, dimension, value) returns patch
- PC_FilterGreaterThan(patch, dimension, value) returns patch
- PC_FilterBetween(patch, dimension, valuemin, valuemax) returns patch
- PC_FilterPolygon(patch, wkb) returns patch
- PC_Filter(patch, dimension, expression) returns patch
- PC_Get(pcpatch, dimname) returns Array(numeric)

View File

@ -184,8 +184,11 @@ typedef struct
const PCSCHEMA *schema;
uint32_t npoints; /* How many points we have */
double xmin, xmax, ymin, ymax;
size_t ghtsize;
#ifdef HAVE_LIBGHT
GhtTree *ght;
GhtTreePtr ght;
#else
uint8_t *ght
#endif
} PCPATCH_GHT;
@ -316,6 +319,12 @@ double pc_point_get_x(const PCPOINT *pt);
/** Returns Y coordinate */
double pc_point_get_y(const PCPOINT *pt);
/** Set the X coordinate */
double pc_point_set_x(PCPOINT *pt, double val);
/** Set the Y coordinate */
double pc_point_set_y(PCPOINT *pt, double val);
/** Create a new readwrite PCPOINT from a hex byte array */
PCPOINT* pc_point_from_wkb(const PCSCHEMA *s, uint8_t *wkb, size_t wkbsize);

View File

@ -167,6 +167,7 @@ int pc_patch_uncompressed_add_point(PCPATCH_UNCOMPRESSED *c, const PCPOINT *p);
/* GHT PATCHES */
PCPATCH_GHT* pc_patch_ght_from_uncompressed(const PCPATCH_UNCOMPRESSED *pa);
PCPATCH_GHT* pc_patch_ght_from_pointlist(const PCPOINTLIST *pdl);
PCPATCH_UNCOMPRESSED* pc_patch_uncompressed_from_ght(const PCPATCH_GHT *pght);

View File

@ -109,9 +109,9 @@ void pc_set_ght_handlers(pc_allocator allocator, pc_reallocator reallocator,
pc_message_handler info_handler, pc_message_handler warn_handler)
{
#ifdef HAVE_LIBGHT
ght_set_handlers((GhtAllocator)allocator, (GhtReallocator)reallocator,
(GhtDeallocator)deallocator, (GhtMessageHandler)error_handler,
(GhtMessageHandler)info_handler, (GhtMessageHandler)warn_handler);
ght_set_handlers((void *)allocator, (void *)reallocator,
(void *)deallocator, (void *)error_handler,
(void *)info_handler, (void *)warn_handler);
#endif
return;
}

View File

@ -78,41 +78,28 @@ ght_type_from_pc_type(const int pctype)
}
}
static GhtDimension *
ght_dimension_from_pc_dimension(const PCDIMENSION *pcdim)
static GhtDimensionPtr
ght_dimension_from_pc_dimension(const PCDIMENSION *d)
{
int i;
GhtDimension *dim;
ght_dimension_new(&dim);
if ( pcdim->name )
{
dim->name = pcstrdup(pcdim->name);
}
if ( pcdim->description )
{
dim->description = pcstrdup(pcdim->description);
}
dim->scale = pcdim->scale;
dim->offset = pcdim->offset;
dim->type = ght_type_from_pc_type(pcdim->interpretation);
GhtDimensionPtr dim;
GhtType type = ght_type_from_pc_type(d->interpretation);
ght_dimension_new_from_parameters(d->name, d->description, type, d->scale, d->offset, &dim);
return dim;
}
static GhtSchema *
static GhtSchemaPtr
ght_schema_from_pc_schema(const PCSCHEMA *pcschema)
{
int i;
GhtSchema *schema;
GhtSchemaPtr schema;
ght_schema_new(&schema);
for ( i = 0; i < pcschema->ndims; i++ )
{
GhtDimension *dim = ght_dimension_from_pc_dimension(pcschema->dims[i]);
GhtDimensionPtr dim = ght_dimension_from_pc_dimension(pcschema->dims[i]);
ght_schema_add_dimension(schema, dim);
}
@ -120,16 +107,6 @@ ght_schema_from_pc_schema(const PCSCHEMA *pcschema)
}
#endif /* HAVE_LIBGHT */
void
pc_init_ght_handlers()
{
#ifdef HAVE_LIBGHT
#else
return;
#endif
}
PCPATCH_GHT *
pc_patch_ght_from_pointlist(const PCPOINTLIST *pdl)
{
@ -149,10 +126,10 @@ pc_patch_ght_from_uncompressed(const PCPATCH_UNCOMPRESSED *pa)
int i, j;
int pointcount = 0;
GhtSchema *schema;
GhtTree *tree;
GhtSchemaPtr schema;
GhtTreePtr tree;
GhtCoordinate coord;
GhtNode *node;
GhtNodePtr node;
GhtErr err;
PCPOINT pt;
PCDIMENSION *xdim, *ydim;
@ -183,12 +160,14 @@ pc_patch_ght_from_uncompressed(const PCPATCH_UNCOMPRESSED *pa)
/* TODO, make resolution configurable from the schema */
if ( ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node) == GHT_OK )
{
unsigned int num_dims;
ght_schema_get_num_dimensions(schema, &num_dims);
/* Add attributes to the node */
for ( j = 0; j < schema->num_dims; j++ )
for ( j = 0; j < num_dims; j++ )
{
PCDIMENSION *dim;
GhtDimension *ghtdim;
GhtAttribute *attr;
GhtDimensionPtr ghtdim;
GhtAttributePtr attr;
double val;
/* Don't add X or Y as attributes, they are already embodied in the hash */
@ -225,6 +204,7 @@ pc_patch_ght_from_uncompressed(const PCPATCH_UNCOMPRESSED *pa)
/* Compact the tree */
if ( ght_tree_compact_attributes(tree) == GHT_OK )
{
GhtWriterPtr writer;
paght = pcalloc(sizeof(PCPATCH_GHT));
paght->type = PC_GHT;
paght->readonly = PC_FALSE;
@ -234,15 +214,18 @@ pc_patch_ght_from_uncompressed(const PCPATCH_UNCOMPRESSED *pa)
paght->xmax = pa->xmax;
paght->ymin = pa->ymin;
paght->ymax = pa->ymax;
paght->ght = tree;
/* Convert the tree to a memory buffer */
ght_writer_new_mem(&writer);
ght_tree_write(tree, writer);
ght_writer_get_size(writer, &(paght->ghtsize));
paght->ght = pcalloc(paght->ghtsize);
ght_writer_get_bytes(writer, paght->ght);
ght_writer_free(writer);
}
else
{
ght_tree_free(tree);
}
ght_tree_free(tree);
return paght;
#endif
}
@ -266,56 +249,114 @@ pc_patch_ght_free(PCPATCH_GHT *paght)
#endif
}
#if 0
/* Done */
PCPATCH_UNCOMPRESSED *
pc_patch_uncompressed_from_ght(const PCPATCH_GHT *pdl)
static GhtTreePtr
ght_tree_from_pc_patch(const PCPATCH_GHT *paght)
{
GhtTreePtr tree;
GhtReaderPtr reader;
GhtSchemaPtr ghtschema;
ghtschema = ght_schema_from_pc_schema(paght->schema);
if ( ! ghtschema )
return NULL;
if ( GHT_OK != ght_reader_new_mem(paght->ght, paght->ghtsize, ghtschema, &reader) )
return NULL;
if ( GHT_OK != ght_tree_read(reader, &tree) )
return NULL;
return tree;
}
PCPATCH_UNCOMPRESSED *
pc_patch_uncompressed_from_ght(const PCPATCH_GHT *paght)
{
#ifndef HAVE_LIBGHT
pcerror("%s: libght support is not enabled", __func__);
return NULL;
#else
int i, j, npoints;
PCPATCH_UNCOMPRESSED *patch;
PCPATCH_DIMENSIONAL *pdl_uncompressed;
PCPOINT point;
const PCSCHEMA *schema;
uint8_t *buf;
GhtNodeListPtr nodelist;
GhtCoordinate coord;
GhtNodePtr node;
GhtTreePtr tree;
GhtHash *hash;
GhtAttributePtr attr;
npoints = pdl->npoints;
schema = pdl->schema;
/* Build a structured tree from the tree serialization */
if ( ! paght || ! paght->ght ) return NULL;
tree = ght_tree_from_pc_patch(paght);
if ( ! tree ) return NULL;
/* Convert tree to nodelist */
ght_nodelist_new(paght->npoints, &nodelist);
ght_tree_to_nodelist(tree, nodelist);
/* Allocate uncompressed patch */
ght_nodelist_get_num_nodes(nodelist, &npoints);
schema = paght->schema;
patch = pcalloc(sizeof(PCPATCH_UNCOMPRESSED));
patch->schema = schema;
patch->npoints = npoints;
patch->maxpoints = npoints;
patch->readonly = PC_FALSE;
patch->type = PC_NONE;
patch->xmin = pdl->xmin;
patch->xmax = pdl->xmax;
patch->ymin = pdl->ymin;
patch->ymax = pdl->ymax;
patch->datasize = schema->size * pdl->npoints;
patch->xmin = paght->xmin;
patch->xmax = paght->xmax;
patch->ymin = paght->ymin;
patch->ymax = paght->ymax;
patch->datasize = schema->size * npoints;
patch->data = pcalloc(patch->datasize);
buf = patch->data;
/* Can only read from uncompressed dimensions */
pdl_uncompressed = pc_patch_dimensional_decompress(pdl);
/* Set up utility point */
point.schema = schema;
point.readonly = PC_FALSE;
point.data = patch->data;
/* Process each point... */
for ( i = 0; i < npoints; i++ )
{
for ( j = 0; j < schema->ndims; j++ )
double val;
/* Read and set X and Y */
ght_nodelist_get_node(nodelist, i, &node);
ght_node_get_coordinate(node, &coord);
pc_point_set_x(&point, coord.x);
pc_point_set_y(&point, coord.y);
/* Read and set all the attributes */
ght_node_get_attributes(node, &attr);
while ( attr )
{
PCDIMENSION *dim = pc_schema_get_dimension(schema, j);
uint8_t *in = pdl_uncompressed->bytes[j].bytes + dim->size * i;
uint8_t *out = buf + dim->byteoffset;
memcpy(out, in, dim->size);
}
buf += schema->size;
GhtDimensionPtr dim;
const char *name;
ght_attribute_get_value(attr, &val);
ght_attribute_get_dimension(attr, &dim);
ght_dimension_get_name(dim, &name);
pc_point_set_double_by_name(&point, name, val);
ght_attribute_get_next(attr, &attr);
}
point.data += schema->size;
}
pc_patch_dimensional_free(pdl_uncompressed);
/* Done w/ nodelist and tree */
ght_nodelist_free_deep(nodelist);
ght_tree_free(tree);
/* Done */
return patch;
#endif
}
#if 0
char *
pc_patch_ght_to_string(const PCPATCH_GHT *pa)
{

View File

@ -168,6 +168,18 @@ pc_point_get_y(const PCPOINT *pt)
return d;
}
double
pc_point_set_x(PCPOINT *pt, double val)
{
return pc_point_set_double_by_index(pt, pt->schema->x_position, val);
}
double
pc_point_set_y(PCPOINT *pt, double val)
{
return pc_point_set_double_by_index(pt, pt->schema->y_position, val);
}
char *
pc_point_to_string(const PCPOINT *pt)
{

View File

@ -19,6 +19,7 @@ Datum pcpatch_from_pcpoint_array(PG_FUNCTION_ARGS);
Datum pcpatch_from_pcpatch_array(PG_FUNCTION_ARGS);
Datum pcpatch_uncompress(PG_FUNCTION_ARGS);
Datum pcpatch_numpoints(PG_FUNCTION_ARGS);
Datum pcpatch_compression(PG_FUNCTION_ARGS);
Datum pcpatch_intersects(PG_FUNCTION_ARGS);
Datum pcpatch_size(PG_FUNCTION_ARGS);
Datum pcpoint_size(PG_FUNCTION_ARGS);