Ejemplo n.º 1
0
static void
gpkgMakePointZ (double x, double y, double z, int srid, unsigned char **result,
		unsigned int *size)
{
/* build a Blob encoded Geometry representing a POINT */
    unsigned char *ptr;
    int endian_arch = gaiaEndianArch ();
/* computing the Blob size and then allocating it */
    *size = GEOPACKAGE_HEADER_LEN + GEOPACKAGE_3D_ENVELOPE_LEN;
    *size += GEOPACKAGE_WKB_HEADER_LEN;
    *size += (sizeof (double) * 3);	/* [x,y,z] coords */
    *result = malloc (*size);
    if (*result == NULL)
      {
	  return;
      }
    memset (*result, 0xD9, *size);	/* just a flag value */
    ptr = *result;
/* setting the Blob value */
    *ptr = GEOPACKAGE_MAGIC1;
    *(ptr + 1) = GEOPACKAGE_MAGIC2;
    *(ptr + 2) = GEOPACKAGE_VERSION;
    *(ptr + 3) = GEOPACKAGE_FLAGS_3D_LITTLEENDIAN;
    gaiaExport32 (ptr + 4, srid, 1, endian_arch);	/* the SRID */
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN, x, 1, endian_arch);	/* MBR - minimum X */
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + sizeof (double), x, 1, endian_arch);	/* MBR - maximum x */
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + 2 * sizeof (double), y, 1, endian_arch);	/* MBR - minimum Y */
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + 3 * sizeof (double), y, 1, endian_arch);	/* MBR - maximum Y */
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + 4 * sizeof (double), z, 1, endian_arch);	/* MBR - maximum Z */
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + 5 * sizeof (double), z, 1, endian_arch);	/* MBR - maximum Z */
    *(ptr + GEOPACKAGE_HEADER_LEN + GEOPACKAGE_3D_ENVELOPE_LEN) =
	GEOPACKAGE_WKB_LITTLEENDIAN;
    gaiaExport32 (ptr + GEOPACKAGE_HEADER_LEN + GEOPACKAGE_3D_ENVELOPE_LEN + 1,
		  GEOPACKAGE_WKB_POINTZ, 1, endian_arch);
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + GEOPACKAGE_3D_ENVELOPE_LEN +
		  GEOPACKAGE_WKB_HEADER_LEN, x, 1, endian_arch);
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + GEOPACKAGE_3D_ENVELOPE_LEN +
		  GEOPACKAGE_WKB_HEADER_LEN + sizeof (double), y, 1,
		  endian_arch);
    gaiaExport64 (ptr + GEOPACKAGE_HEADER_LEN + GEOPACKAGE_3D_ENVELOPE_LEN +
		  GEOPACKAGE_WKB_HEADER_LEN + (2 * sizeof (double)), z, 1,
		  endian_arch);
}
Ejemplo n.º 2
0
int main (int argc, char *argv[])
{
    unsigned char buf_in[8];
    unsigned char buf_out[8];
    short shrt_val;
    int int_val;
    sqlite3_int64 i64_val;
    float flt_val;
    double dbl_val;

/* testing short values [16 bit] */
    buf_in[0] = 0xdf;
    buf_in[1] = 0xfd;
    shrt_val = gaiaImport16(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport16(buf_out, shrt_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xfd) {
	fprintf(stderr, "endian INT16 (1): got 0x%02x%02x, expected 0xdffd\n", 
            buf_out[0], buf_out[1]);
	return -1;
    }
    shrt_val = gaiaImport16(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport16(buf_out, shrt_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xfd) {
	fprintf(stderr, "endian INT16 (2): got 0x%02x%02x, expected 0xdffd\n", 
            buf_out[0], buf_out[1]);
	return -2;
    }
    shrt_val = gaiaImport16(buf_in, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport16(buf_out, shrt_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xdf) {
	fprintf(stderr, "endian INT16 (3): got 0x%02x%02x, expected 0xfddf\n", 
            buf_out[0], buf_out[1]);
	return -3;
    }
    shrt_val = gaiaImport16(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport16(buf_out, shrt_val, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xdf) {
	fprintf(stderr, "endian INT16 (4): got 0x%02x%02x, expected 0xfddf\n", 
            buf_out[0], buf_out[1]);
	return -4;
    }
    shrt_val = gaiaImport16(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport16(buf_out, shrt_val, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xfd) {
	fprintf(stderr, "endian 16 bit (5): got 0x%02x%02x, expected 0xdffd\n", 
            buf_out[0], buf_out[1]);
	return -5;
    }
    shrt_val = gaiaImport16(buf_in, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport16(buf_out, shrt_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xdf) {
	fprintf(stderr, "endian INT16 (6): got 0x%02x%02x, expected 0xfddf\n", 
            buf_out[0], buf_out[1]);
	return -6;
    }
    
/* testing int values [32 bit] */
    buf_in[0] = 0xdf;
    buf_in[1] = 0xec;
    buf_in[2] = 0xce;
    buf_in[3] = 0xfd;
    int_val = gaiaImport32(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport32(buf_out, int_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xce || buf_out[3] != 0xfd) {
	fprintf(stderr, "endian INT32 (1): got 0x%02x%02x%02x%02x, expected 0xdfeccefd\n", 
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -7;
    }
    int_val = gaiaImport32(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport32(buf_out, int_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xce || buf_out[3] != 0xfd) {
	fprintf(stderr, "endian INT32 (2): got 0x%02x%02x%02x%02x, expected 0xdfeccefd\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -8;
    }
    int_val = gaiaImport32(buf_in, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport32(buf_out, int_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian INT32 (3): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -9;
    }
    int_val = gaiaImport32(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport32(buf_out, int_val, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian INT32 (4): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -10;
    }
    int_val = gaiaImport32(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport32(buf_out, int_val, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian INT32 (5): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -11;
    }
    int_val = gaiaImport32(buf_in, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport32(buf_out, int_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian INT32 (6): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -12;
    }
    
/* testing long values [64 bit] */
    buf_in[0] = 0xdf;
    buf_in[1] = 0xec;
    buf_in[2] = 0xa8;
    buf_in[3] = 0x63;
    buf_in[4] = 0x36;
    buf_in[5] = 0x8a;
    buf_in[6] = 0xce;
    buf_in[7] = 0xfd;
    i64_val = gaiaImportI64(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExportI64(buf_out, i64_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xa8 || buf_out[3] != 0x63 ||
        buf_out[4] != 0x36 || buf_out[5] != 0x8a || buf_out[6] != 0xce || buf_out[7] != 0xfd) {
	fprintf(stderr, "endian INT64 (1): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xdfeca863368acefd\n", 
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -13;
    }
    i64_val = gaiaImportI64(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExportI64(buf_out, i64_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xa8 || buf_out[3] != 0x63 ||
        buf_out[4] != 0x36 || buf_out[5] != 0x8a || buf_out[6] != 0xce || buf_out[7] != 0xfd) {
	fprintf(stderr, "endian INT64 (2): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xdfeca863368acefd\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -14;
    }
    i64_val = gaiaImportI64(buf_in, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExportI64(buf_out, i64_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian INT64 (3): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -15;
    }
    i64_val = gaiaImportI64(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExportI64(buf_out, i64_val, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian INT64 (4): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -16;
    }
    i64_val = gaiaImportI64(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExportI64(buf_out, i64_val, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian INT64 (5): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -17;
    }
    i64_val = gaiaImportI64(buf_in, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExportI64(buf_out, i64_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian INT64 (6): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -18;
    }
    
/* testing float values */
    buf_in[0] = 0xdf;
    buf_in[1] = 0xec;
    buf_in[2] = 0xce;
    buf_in[3] = 0xfd;
    flt_val = gaiaImportF32(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExportF32(buf_out, flt_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xce || buf_out[3] != 0xfd) {
	fprintf(stderr, "endian FLOAT (1): got 0x%02x%02x%02x%02x, expected 0xdfeccefd\n", 
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -19;
    }
    flt_val = gaiaImportF32(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExportF32(buf_out, flt_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xce || buf_out[3] != 0xfd) {
	fprintf(stderr, "endian FLOAT (2): got 0x%02x%02x%02x%02x, expected 0xdfeccefd\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -20;
    }
    flt_val = gaiaImportF32(buf_in, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExportF32(buf_out, flt_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian FLOAT (3): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -21;
    }
    flt_val = gaiaImportF32(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExportF32(buf_out, flt_val, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian FLOAT (4): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -22;
    }
    flt_val = gaiaImportF32(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExportF32(buf_out, flt_val, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian FLOAT (5): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -23;
    }
    flt_val = gaiaImportF32(buf_in, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExportF32(buf_out, flt_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0xec || buf_out[3] != 0xdf) {
	fprintf(stderr, "endian FLOAT (6): got 0x%02x%02x%02x%02x, expected 0xfdceecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3]);
	return -24;
    }
        
/* testing double values */
    buf_in[0] = 0xdf;
    buf_in[1] = 0xec;
    buf_in[2] = 0xa8;
    buf_in[3] = 0x63;
    buf_in[4] = 0x36;
    buf_in[5] = 0x8a;
    buf_in[6] = 0xce;
    buf_in[7] = 0xfd;
    dbl_val = gaiaImport64(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport64(buf_out, dbl_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xa8 || buf_out[3] != 0x63 ||
        buf_out[4] != 0x36 || buf_out[5] != 0x8a || buf_out[6] != 0xce || buf_out[7] != 0xfd) {
	fprintf(stderr, "endian DOUBLE (1): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xdfeca863368acefd\n", 
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -25;
    }
    dbl_val = gaiaImport64(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport64(buf_out, dbl_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xdf || buf_out[1] != 0xec || buf_out[2] != 0xa8 || buf_out[3] != 0x63 ||
        buf_out[4] != 0x36 || buf_out[5] != 0x8a || buf_out[6] != 0xce || buf_out[7] != 0xfd) {
	fprintf(stderr, "endian DOUBLE (2): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xdfeca863368acefd\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -26;
    }
    dbl_val = gaiaImport64(buf_in, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport64(buf_out, dbl_val, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian DOUBLE (3): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -27;
    }
    dbl_val = gaiaImport64(buf_in, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport64(buf_out, dbl_val, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian DOUBLE (4): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -28;
    }
    dbl_val = gaiaImport64(buf_in, LITTLE_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    gaiaExport64(buf_out, dbl_val, BIG_ENDIAN_ENCODED, LITTLE_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian DOUBLE (5): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -29;
    }
    dbl_val = gaiaImport64(buf_in, LITTLE_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    gaiaExport64(buf_out, dbl_val, BIG_ENDIAN_ENCODED, BIG_ENDIAN_ARCH);
    if (buf_out[0] != 0xfd || buf_out[1] != 0xce || buf_out[2] != 0x8a || buf_out[3] != 0x36 ||
        buf_out[4] != 0x63 || buf_out[5] != 0xa8 || buf_out[6] != 0xec || buf_out[7] != 0xdf) {
	fprintf(stderr, "endian DOUBLE (6): got 0x%02x%02x%02x%02x%02x%02x%02x%02x, expected 0xfdce8a3663a8ecdf\n",
            buf_out[0], buf_out[1], buf_out[2], buf_out[3], buf_out[4], buf_out[5], 
            buf_out[6], buf_out[7]);
	return -30;
    }
    
    return 0;
}
Ejemplo n.º 3
0
bool MyFrame::CreateNetwork(Network * p_graph, wxString & table,
                            wxString & from, wxString & to, wxString & geometry,
                            wxString & name, bool aStarSupported,
                            double aStarCoeff)
{
//
// creates the NETWORK-DATA table 
//
  int ret;
  wxString sql;
  char xsql[1024];
  char *errMsg = NULL;
  unsigned char *auxbuf = new unsigned char[MAX_BLOCK];
  unsigned char *buf = new unsigned char[MAX_BLOCK];
  unsigned char *out;
  sqlite3_stmt *stmt;
  int i;
  int size;
  int endian_arch = gaiaEndianArch();
  NetNode *pN;
  int pk = 0;
  int nodes_cnt = 0;
  int len;
  bool net_data_exists = false;
  bool net_exists = false;
  bool delete_existing = false;
  char xname[1024];
  wxString data_table = table + wxT("_net_data");
  wxString net_table = table + wxT("_net");
  net_data_exists = TableAlreadyExists(data_table);
  net_exists = TableAlreadyExists(net_table);
  if (net_data_exists == true || net_exists == true)
    {
      // asking permission to overwrite existing tables
      wxString msg;
      if (net_data_exists == true)
        msg += wxT("A table named '") + data_table + wxT("' already exists\n");
      if (net_exists == true)
        msg += wxT("A table named '") + net_table + wxT("' already exists\n");
      msg += wxT("\nDo you allow DROPping existing table(s) ?");
      wxMessageDialog confirm(this, msg, wxT("Confirm overwrite"),
                              wxYES_NO | wxICON_QUESTION);
      ret = confirm.ShowModal();
      if (ret == wxID_YES)
        delete_existing = true;
    }
  ::wxBeginBusyCursor();
  for (i = 0; i < p_graph->GetNumNodes(); i++)
    {
      // setting the internal index to each Node 
      pN = p_graph->GetSortedNode(i);
      pN->SetInternalIndex(i);
    }
// starts a transaction 
  ret = sqlite3_exec(SqliteHandle, "BEGIN", NULL, NULL, &errMsg);
  if (ret != SQLITE_OK)
    {
      wxMessageBox(wxT("BEGIN error: ") + wxString::FromUTF8(errMsg),
                   wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
      sqlite3_free(errMsg);
      goto abort;
    }
  if (delete_existing == true)
    {
      strcpy(xname, net_table.ToUTF8());
      DoubleQuotedSql(xname);
      sql = wxT("DROP TABLE IF EXISTS ") + wxString::FromUTF8(xname);
      strcpy(xsql, sql.ToUTF8());
      ret = sqlite3_exec(SqliteHandle, xsql, NULL, NULL, &errMsg);
      if (ret != SQLITE_OK)
        {
          wxMessageBox(wxT("DROP TABLE error: ") + wxString::FromUTF8(errMsg),
                       wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
          sqlite3_free(errMsg);
          goto abort;
        }
      strcpy(xname, data_table.ToUTF8());
      DoubleQuotedSql(xname);
      sql = wxT("DROP TABLE IF EXISTS ") + wxString::FromUTF8(xname);
      strcpy(xsql, sql.ToUTF8());
      ret = sqlite3_exec(SqliteHandle, xsql, NULL, NULL, &errMsg);
      if (ret != SQLITE_OK)
        {
          wxMessageBox(wxT("DROP TABLE error: ") + wxString::FromUTF8(errMsg),
                       wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
          sqlite3_free(errMsg);
          goto abort;
        }
    }
// creating the NETWORK-DATA table 
  strcpy(xname, data_table.ToUTF8());
  DoubleQuotedSql(xname);
  sql = wxT("CREATE TABLE ") + wxString::FromUTF8(xname);
  sql += wxT(" (Id INTEGER PRIMARY KEY, NetworkData BLOB NOT NULL)");
  strcpy(xsql, sql.ToUTF8());
  ret = sqlite3_exec(SqliteHandle, xsql, NULL, NULL, &errMsg);
  if (ret != SQLITE_OK)
    {
      wxMessageBox(wxT("CREATE TABLE error: ") + wxString::FromUTF8(errMsg),
                   wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
      sqlite3_free(errMsg);
      goto abort;
    }
// preparing the SQL statement
  strcpy(xname, data_table.ToUTF8());
  DoubleQuotedSql(xname);
  sql =
    wxT("INSERT INTO ") + wxString::FromUTF8(xname) +
    wxT(" (Id, NetworkData) VALUES (?, ?)");
  strcpy(xsql, sql.ToUTF8());
  ret = sqlite3_prepare_v2(SqliteHandle, xsql, strlen(xsql), &stmt, NULL);
  if (ret != SQLITE_OK)
    {
      wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle));
      wxMessageBox(wxT("INSERT error: ") + err, wxT("spatialite_gui"),
                   wxOK | wxICON_ERROR, this);
      goto abort;
    }
  if (pk == 0)
    {
      // preparing the HEADER block 
      out = buf;
      if (aStarSupported)
        *out++ = GAIA_NET64_A_STAR_START;
      else
        *out++ = GAIA_NET64_START;
      *out++ = GAIA_NET_HEADER;
      gaiaExport32(out, p_graph->GetNumNodes(), 1, endian_arch);  // how many Nodes are there
      out += 4;
      if (p_graph->IsNodeCode() == true)
        *out++ = GAIA_NET_CODE; // Nodes are identified by a TEXT code 
      else
        *out++ = GAIA_NET_ID;   // Nodes are identified by an INTEGER id 
      if (p_graph->IsNodeCode() == true)
        *out++ = p_graph->GetMaxCodeLength(); // max TEXT code length
      else
        *out++ = 0x00;
      // inserting the main Table name 
      *out++ = GAIA_NET_TABLE;
      len = table.Len() + 1;
      gaiaExport16(out, len, 1, endian_arch); // the Table Name length, including last '\0'
      out += 2;
      memset(out, '\0', len);
      strcpy((char *) out, table.ToUTF8());
      out += len;
      // inserting the NodeFrom column name 
      *out++ = GAIA_NET_FROM;
      len = from.Len() + 1;
      gaiaExport16(out, len, 1, endian_arch); // the NodeFrom column Name length, including last '\0'
      out += 2;
      memset(out, '\0', len);
      strcpy((char *) out, from.ToUTF8());
      out += len;
      // inserting the NodeTo column name
      *out++ = GAIA_NET_TO;
      len = to.Len() + 1;
      gaiaExport16(out, len, 1, endian_arch); // the NodeTo column Name length, including last '\0'
      out += 2;
      memset(out, '\0', len);
      strcpy((char *) out, to.ToUTF8());
      out += len;
      // inserting the Geometry column name
      *out++ = GAIA_NET_GEOM;
      len = geometry.Len() + 1;
      gaiaExport16(out, len, 1, endian_arch); // the Geometry column Name length, including last '\0'
      out += 2;
      memset(out, '\0', len);
      strcpy((char *) out, geometry.ToUTF8());
      out += len;
      // inserting the Name column name - may be empty
      *out++ = GAIA_NET_NAME;
      if (name.Len() == 0)
        len = 1;
      else
        len = name.Len() + 1;
      gaiaExport16(out, len, 1, endian_arch); // the Name column Name length, including last '\0'
      out += 2;
      memset(out, '\0', len);
      if (name.Len() > 0)
        strcpy((char *) out, name.ToUTF8());
      out += len;
      if (aStarSupported)
        {
          // inserting the A* Heuristic Coeff
          *out++ = GAIA_NET_A_STAR_COEFF;
          gaiaExport64(out, aStarCoeff, 1, endian_arch);
          out += 8;
        }
      *out++ = GAIA_NET_END;
      // INSERTing the Header block 
      sqlite3_reset(stmt);
      sqlite3_clear_bindings(stmt);
      sqlite3_bind_int64(stmt, 1, pk);
      sqlite3_bind_blob(stmt, 2, buf, out - buf, SQLITE_STATIC);
      ret = sqlite3_step(stmt);
      if (ret == SQLITE_DONE || ret == SQLITE_ROW)
        ;
      else
        {
          wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle));
          wxMessageBox(wxT("sqlite3_step error: ") + err, wxT("spatialite_gui"),
                       wxOK | wxICON_ERROR, this);
          sqlite3_finalize(stmt);
          goto abort;
        }
      pk++;
      // preparing a new block 
      out = buf;
      *out++ = GAIA_NET_BLOCK;
      gaiaExport16(out, 0, 1, endian_arch); // how many Nodes are into this block 
      out += 2;
      nodes_cnt = 0;
    }
  for (i = 0; i < p_graph->GetNumNodes(); i++)
    {
      // looping on each Node 
      pN = p_graph->GetSortedNode(i);
      OutputNetNode(auxbuf, &size, i, p_graph->IsNodeCode(),
                    p_graph->GetMaxCodeLength(), pN, endian_arch,
                    aStarSupported);
      if (size >= (MAX_BLOCK - (out - buf)))
        {
          // inserting the last block 
          gaiaExport16(buf + 1, nodes_cnt, 1, endian_arch); // how many Nodes are into this block
          sqlite3_reset(stmt);
          sqlite3_clear_bindings(stmt);
          sqlite3_bind_int64(stmt, 1, pk);
          sqlite3_bind_blob(stmt, 2, buf, out - buf, SQLITE_STATIC);
          ret = sqlite3_step(stmt);
          if (ret == SQLITE_DONE || ret == SQLITE_ROW)
            ;
          else
            {
              wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle));
              wxMessageBox(wxT("sqlite3_step error: ") + err,
                           wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
              sqlite3_finalize(stmt);
              goto abort;
            }
          pk++;
          // preparing a new block 
          out = buf;
          *out++ = GAIA_NET_BLOCK;
          gaiaExport16(out, 0, 1, endian_arch); // how many Nodes are into this block
          out += 2;
          nodes_cnt = 0;
        }
      // inserting the current Node into the block 
      nodes_cnt++;
      memcpy(out, auxbuf, size);
      out += size;
    }
  if (nodes_cnt)
    {
      // inserting the last block
      gaiaExport16(buf + 1, nodes_cnt, 1, endian_arch); // how many Nodes are into this block
      sqlite3_reset(stmt);
      sqlite3_clear_bindings(stmt);
      sqlite3_bind_int64(stmt, 1, pk);
      sqlite3_bind_blob(stmt, 2, buf, out - buf, SQLITE_STATIC);
      ret = sqlite3_step(stmt);
      if (ret == SQLITE_DONE || ret == SQLITE_ROW)
        ;
      else
        {
          wxString err = wxString::FromUTF8(sqlite3_errmsg(SqliteHandle));
          wxMessageBox(wxT("sqlite3_step error: ") + err, wxT("spatialite_gui"),
                       wxOK | wxICON_ERROR, this);
          sqlite3_finalize(stmt);
          goto abort;
        }
    }
  sqlite3_finalize(stmt);
// creating the VirtualNetwork NET-table
  strcpy(xname, net_table.ToUTF8());
  DoubleQuotedSql(xname);
  sql = wxT("CREATE VIRTUAL TABLE ") + wxString::FromUTF8(xname);
  sql += wxT(" USING VirtualNetwork(");
  strcpy(xname, data_table.ToUTF8());
  DoubleQuotedSql(xname);
  sql += wxString::FromUTF8(xname) + wxT(")");
  strcpy(xsql, sql.ToUTF8());
  ret = sqlite3_exec(SqliteHandle, xsql, NULL, NULL, &errMsg);
  if (ret != SQLITE_OK)
    {
      wxMessageBox(wxT("CREATE VIRTUAL TABLE error: ") +
                   wxString::FromUTF8(errMsg), wxT("spatialite_gui"),
                   wxOK | wxICON_ERROR, this);
      sqlite3_free(errMsg);
      goto abort;
    }
// commits the transaction 
  ret = sqlite3_exec(SqliteHandle, "COMMIT", NULL, NULL, &errMsg);
  if (ret != SQLITE_OK)
    {
      wxMessageBox(wxT("COMMIT error: ") + wxString::FromUTF8(errMsg),
                   wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
      sqlite3_free(errMsg);
      goto abort;
    }
  if (buf)
    delete[]buf;
  if (auxbuf)
    delete[]auxbuf;
  ::wxEndBusyCursor();
  return true;
abort:
  ::wxEndBusyCursor();
  if (buf)
    delete[]buf;
  if (auxbuf)
    delete[]auxbuf;
  return true;
  return false;
}