static void
test_sort_consistency()
{
	// 00 endian (big)
	// 00000000 pcid
	// 00000000 compression
	// 00000002 npoints
	// 0000000800000003000000050006 pt1 (XYZi)
	// 0000000200000001000000040008 pt2 (XYZi)

	// init data
	PCPATCH *pasort;
	char *pastr, *pasortstr;
	uint8_t *wkbsort;
	char *hexbuf = "0000000000000000000000000200000008000000030000000500060000000200000001000000040008";
	size_t hexsize = strlen(hexbuf);
	uint8_t *wkb = bytes_from_hexbytes(hexbuf, hexsize);
	PCPATCH *pa = pc_patch_from_wkb(schema, wkb, hexsize/2);
	PCPOINTLIST *li = pc_pointlist_from_patch(pa);
	const char *X[] = {"X"};

	// sort on X attribute
	pasort = pc_patch_sort(pa, X, 1);

	//chek consistency
	wkbsort = pc_patch_to_wkb(pasort, &hexsize);
	CU_ASSERT_EQUAL(wkb_get_pcid(wkb), wkb_get_pcid(wkbsort));
	CU_ASSERT_EQUAL(wkb_get_npoints(wkb), wkb_get_npoints(wkbsort));
	CU_ASSERT_EQUAL(wkb_get_compression(wkb), wkb_get_compression(wkbsort));

	pastr = pc_patch_to_string(pa);
	CU_ASSERT_STRING_EQUAL(pastr, "{\"pcid\":0,\"pts\":[[0.08,0.03,0.05,6],[0.02,0.01,0.04,8]]}");

	pasortstr = pc_patch_to_string(pasort);
	CU_ASSERT_STRING_EQUAL(pasortstr, "{\"pcid\":0,\"pts\":[[0.02,0.01,0.04,8],[0.08,0.03,0.05,6]]}");

	// free
	pcfree(wkb);
	pcfree(wkbsort);
	pcfree(pastr);
	pcfree(pasortstr);
	pc_patch_free(pasort);
	pc_patch_free(pa);
	pc_pointlist_free(li);
}
PCPATCH *
pc_patch_uncompressed_from_wkb(const PCSCHEMA *s, const uint8_t *wkb, size_t wkbsize)
{
	/*
	byte:     endianness (1 = NDR, 0 = XDR)
	uint32:   pcid (key to POINTCLOUD_SCHEMAS)
	uint32:   compression (0 = no compression, 1 = dimensional, 2 = lazperf)
	uint32:   npoints
	pcpoint[]:  data (interpret relative to pcid)
	*/
	static size_t hdrsz = 1+4+4+4; /* endian + pcid + compression + npoints */
	PCPATCH_UNCOMPRESSED *patch;
	uint8_t *data;
	uint8_t swap_endian = (wkb[0] != machine_endian());
	uint32_t npoints;

	if ( wkb_get_compression(wkb) != PC_NONE )
	{
		pcerror("%s: call with wkb that is not uncompressed", __func__);
		return NULL;
	}

	npoints = wkb_get_npoints(wkb);
	if ( (wkbsize - hdrsz) != (s->size * npoints) )
	{
		pcerror("%s: wkb size and expected data size do not match", __func__);
		return NULL;
	}

	if ( swap_endian )
	{
		data = uncompressed_bytes_flip_endian(wkb+hdrsz, s, npoints);
	}
	else
	{
		data = pcalloc(npoints * s->size);
		memcpy(data, wkb+hdrsz, npoints*s->size);
	}

	patch = pcalloc(sizeof(PCPATCH_UNCOMPRESSED));
	patch->type = PC_NONE;
	patch->readonly = PC_FALSE;
	patch->schema = s;
	patch->npoints = npoints;
	patch->maxpoints = npoints;
	patch->datasize = (wkbsize - hdrsz);
	patch->data = data;
	patch->stats = NULL;

	return (PCPATCH*)patch;
}
PCPATCH *
pc_patch_ght_from_wkb(const PCSCHEMA *schema, const uint8_t *wkb, size_t wkbsize)
{
#ifndef HAVE_LIBGHT
	pcerror("%s: libght support is not enabled", __func__);
	return NULL;
#else
	/*
	byte:     endianness (1 = NDR, 0 = XDR)
	uint32:   pcid (key to POINTCLOUD_SCHEMAS)
	uint32:   compression (0 = no compression, 1 = dimensional, 2 = GHT)
	uint32:   npoints
	uint32:   ghtsize
	uint8[]:  ghtbuffer
	*/
	static size_t hdrsz = 1+4+4+4; /* endian + pcid + compression + npoints */
	PCPATCH_GHT *patch;
	uint8_t swap_endian = (wkb[0] != machine_endian());
	uint32_t npoints;
	size_t ghtsize;
	const uint8_t *buf;

	if ( wkb_get_compression(wkb) != PC_GHT )
	{
		pcerror("%s: call with wkb that is not GHT compressed", __func__);
		return NULL;
	}

	npoints = wkb_get_npoints(wkb);

	patch = pcalloc(sizeof(PCPATCH_GHT));
	patch->type = PC_GHT;
	patch->readonly = PC_FALSE;
	patch->schema = schema;
	patch->npoints = npoints;

	/* Start on the GHT */
	buf = wkb+hdrsz;
	ghtsize = wkb_get_int32(buf, swap_endian);
	buf += 4; /* Move to start of GHT buffer */

	/* Copy in the tree buffer */
	patch->ght = pcalloc(ghtsize);
	patch->ghtsize = ghtsize;
	memcpy(patch->ght, buf, ghtsize);

	return (PCPATCH*)patch;
#endif
}
PCPATCH *
pc_patch_dimensional_from_wkb(const PCSCHEMA *schema, const uint8_t *wkb, size_t wkbsize)
{
	/*
	byte:     endianness (1 = NDR, 0 = XDR)
	uint32:   pcid (key to POINTCLOUD_SCHEMAS)
	uint32:   compression (0 = no compression, 1 = dimensional, 2 = GHT)
	uint32:   npoints
	dimensions[]:  dims (interpret relative to pcid and compressions)
	*/
	static size_t hdrsz = 1+4+4+4; /* endian + pcid + compression + npoints */
	PCPATCH_DIMENSIONAL *patch;
	uint8_t swap_endian = (wkb[0] != machine_endian());
	uint32_t npoints, ndims;
	const uint8_t *buf;
	int i;

	if ( wkb_get_compression(wkb) != PC_DIMENSIONAL )
	{
		pcerror("%s: call with wkb that is not dimensionally compressed", __func__);
		return NULL;
	}

	npoints = wkb_get_npoints(wkb);
	ndims = schema->ndims;

	patch = pcalloc(sizeof(PCPATCH_DIMENSIONAL));
	patch->type = PC_DIMENSIONAL;
	patch->readonly = PC_FALSE;
	patch->schema = schema;
	patch->npoints = npoints;
	patch->bytes = pcalloc(ndims*sizeof(PCBYTES));

	buf = wkb+hdrsz;
	for ( i = 0; i < ndims; i++ )
	{
		PCBYTES *pcb = &(patch->bytes[i]);
		PCDIMENSION *dim = schema->dims[i];
		pc_bytes_deserialize(buf, dim, pcb, PC_FALSE /*readonly*/, swap_endian);
		pcb->npoints = npoints;
		buf += pc_bytes_serialized_size(pcb);
	}

	return (PCPATCH*)patch;
}