123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <float.h>
- static int _calc_wkblen (
- GeomType type,
- int num_points,
- int num_parts,
- int num_subparts
- );
- static int _get_wkb_polys (
- char *cp,
- int swap_bytes,
- Geometry *geom
- );
- static int _get_wkb_header (
- char **buf_ptr,
- int *swap_bytes,
- WkbType *wkb_type
- );
- static int _get_wkb_points (
- char *cp,
- int swap_bytes,
- Geometry *geom
- );
- static int _get_wkb_lines (
- char *cp,
- int swap_bytes,
- Geometry *geom
- );
- static int _get_wkb_polys (
- char *cp,
- int swap_bytes,
- Geometry *geom
- );
- void geom_to_wkb (
- Geometry *geom,
- int *max_alloced,
- int *data_len,
- char **binary
- )
- {
- int type;
- char *cp;
- int i, j;
- int tmp_num_subparts;
- int end_subparts;
- int tmp_num_points;
- unsigned char byte_order;
-
- *data_len = _calc_wkblen (geom->type, geom->num_points,
- geom->num_parts, geom->num_subparts);
-
- if (*max_alloced == 0)
- {
- *binary = (char *)malloc (*data_len);
- *max_alloced = *data_len;
- }
- else if (*max_alloced < *data_len)
- {
- *binary = (char *) realloc (*binary, *data_len);
- *max_alloced = *data_len;
- }
- cp = *binary;
- ENDIAN_TEST (i);
- byte_order = (unsigned char) i;
- *cp = byte_order;
- cp++;
- switch (geom->type)
- {
- case geomPoint:
- type = wkbPoint;
- put_integer (&cp, 0, 1, (int *)&type);
- put_xy (&cp, 0, 1, geom->pt);
- break;
- case geomMultiPoint:
- type = wkbMultiPoint;
- put_integer (&cp, 0, 1, (int *)&type);
- put_integer (&cp, 0, 1, &geom->num_points);
- type = wkbPoint;
- for (i = 0; i < geom->num_points; i++)
- {
- *cp = byte_order;
- cp++;
- put_integer (&cp, 0, 1, (int *)&type);
- put_xy (&cp, 0, 1, &geom->pt[i]);
- }
- break;
- case geomLineString:
- type = wkbLineString;
- put_integer (&cp, 0, 1, (int *)&type);
- put_integer (&cp, 0, 1, &geom->num_points);
- put_xy (&cp, 0, geom->num_points, geom->pt);
- break;
- case geomMultiLineString:
- type = wkbMultiLineString;
- put_integer (&cp, 0, 1, (int *)&type);
- put_integer (&cp, 0, 1, &geom->num_parts);
- type = wkbLineString;
- for (i = 0; i < geom->num_parts; i++)
- {
- *cp = byte_order;
- cp++;
- put_integer (&cp, 0, 1, (int *)&type);
- if (i == geom->num_parts - 1)
- tmp_num_points = geom->num_points - geom->offsets[i];
- else
- tmp_num_points = geom->offsets[i+1] - geom->offsets[i];
- put_integer (&cp, 0, 1, &tmp_num_points);
- put_xy (&cp, 0, tmp_num_points, &geom->pt[geom->offsets[i]]);
- }
- break;
- case geomPolygon:
- type = wkbPolygon;
- put_integer (&cp, 0, 1, (int *)&type);
- put_integer (&cp, 0, 1, &geom->num_subparts);
- for (i = 0; i < geom->num_subparts; i++)
- {
- if (i == geom->num_subparts - 1)
- {
- if (NULL != geom->suboffsets)
- {
- tmp_num_points = geom->num_points - geom->suboffsets[i];
- }
- else
- tmp_num_points = geom->num_points;
- }
- else
- tmp_num_points = geom->suboffsets[i+1] - geom->suboffsets[i];
- put_integer (&cp, 0, 1, &tmp_num_points);
- put_xy (&cp, 0, tmp_num_points, &geom->pt[geom->suboffsets[i]]);
- }
- break;
- case geomMultiPolygon:
- type = wkbMultiPolygon;
- put_integer (&cp, 0, 1, (int *)&type);
- put_integer (&cp, 0, 1, &geom->num_parts);
- type = wkbPolygon;
- for (j = 0; j < geom->num_parts; j++)
- {
- *cp = byte_order;
- cp++;
- put_integer (&cp, 0, 1, (int *)&type);
- if (j == geom->num_parts - 1)
- end_subparts = geom->num_subparts;
- else
- end_subparts = geom->offsets[j+1];
- tmp_num_subparts = end_subparts - geom->offsets[j];
- put_integer (&cp, 0, 1, &tmp_num_subparts);
- for (i = geom->offsets[j]; i < end_subparts; i++)
- {
- if (i == geom->num_subparts - 1)
- tmp_num_points = geom->num_points - geom->suboffsets[i];
- else
- tmp_num_points = geom->suboffsets[i+1] - geom->suboffsets[i];
-
- put_integer (&cp, 0, 1, &tmp_num_points);
- put_xy (&cp, 0, tmp_num_points, &geom->pt[geom->suboffsets[i]]);
- }
- }
- break;
- }
- }
- static int _calc_wkblen (
- GeomType type,
- int num_points,
- int num_parts,
- int num_subparts
- )
- {
- int data_len;
-
- data_len = 1 + sizeof (int);
- switch (type)
- {
- case geomPoint:
- data_len += (2 * sizeof (double));
- break;
- case geomMultiPoint:
-
- data_len += sizeof(int);
- data_len += num_points * (1 + sizeof(int) + 2 * sizeof(double));
- break;
- case geomLineString:
-
- data_len += sizeof(int);
- data_len += num_points * (2 * sizeof(double));
- break;
- case geomMultiLineString:
-
- data_len += sizeof(int);
- data_len += num_parts * (1 + 2 * sizeof(int));
- data_len += num_points * (2 * sizeof(double));
- break;
- case geomPolygon:
-
- data_len += sizeof(int);
- data_len += num_subparts * sizeof (int);
- data_len += num_points * (2 * sizeof(double));
- break;
- case geomMultiPolygon:
-
- data_len += sizeof(int);
- data_len += num_parts * (1 + 2 * sizeof (int));
- data_len += num_subparts * sizeof (int);
- data_len += num_points * (2 * sizeof(double));
- break;
- }
- return data_len;
- }
- static int _get_wkb_header (
- char **buf_ptr,
- int *swap_bytes,
- WkbType *wkb_type
- )
- {
- int i;
- ByteOrder byte_order;
-
- switch ((*buf_ptr)[0])
- {
- case 0:
- byte_order = BigEndian;
- break;
- case 1:
- byte_order = LittleEndian;
- break;
- default:
- return GEOM_INVALID_WKB_BYTE_ORDER;
- }
- ENDIAN_TEST (i);
- *swap_bytes = 1 - i;
- (*buf_ptr)++;
- get_integer (buf_ptr, *swap_bytes, 1, (int *) wkb_type);
- return GEOM_SUCCESS;
- }
- int wkb_to_geom (
- char *binary,
- Geometry *geom
- )
- {
- WkbType wkb_type;
- int rc;
- char *cp;
- int swap_bytes;
-
- cp = binary;
- rc = _get_wkb_header (&cp, &swap_bytes, &wkb_type);
- if (rc != GEOM_SUCCESS)
- return rc;
-
- switch (wkb_type)
- {
-
- case wkbPoint:
- geom->type = geomPoint;
- geom->num_points = 1;
-
- rc = geom_allocate(geom, 1);
- if (rc != GEOM_SUCCESS)
- return rc;
-
- get_xy (&cp, swap_bytes, 1, geom->pt);
- break;
- case wkbMultiPoint:
- geom->type = geomMultiPoint;
- _get_wkb_points (cp, swap_bytes, geom);
- break;
- case wkbLineString:
- geom->type = geomLineString;
- _get_wkb_lines (cp, swap_bytes, geom);
- break;
- case wkbMultiLineString:
- geom->type = geomMultiLineString;
- _get_wkb_lines (cp, swap_bytes, geom);
- break;
-
- case wkbPolygon:
- geom->type = geomPolygon;
- _get_wkb_polys (cp, swap_bytes, geom);
- break;
-
- case wkbMultiPolygon:
- geom->type = geomMultiPolygon;
- _get_wkb_polys (cp, swap_bytes, geom);
- break;
- case wkbCollection:
- return GEOM_UNSUPPORTED_WKB_TYPE;
- break;
-
- default:
- return GEOM_INVALID_WKB_TYPE;
- }
- return GEOM_SUCCESS;
- }
- static int _get_wkb_points (
- char *cp,
- int swap_bytes,
- Geometry *geom
- )
- {
- int rc;
- int npoints, i;
- WkbType wkb_type;
- get_integer (&cp, swap_bytes, 1, &npoints);
- if (npoints <= 0)
- return GEOM_TOO_FEW_POINTS;
-
-
- rc = geom_allocate(geom, npoints);
- if (rc != GEOM_SUCCESS)
- return rc;
- geom->num_points = npoints;
-
-
- for (i = 0; i < npoints; i++)
- {
- rc = _get_wkb_header (&cp, &swap_bytes, &wkb_type);
- if (rc != GEOM_SUCCESS)
- return rc;
- if (wkb_type != wkbPoint)
- return GEOM_INVALID_WKB_TYPE;
-
- get_xy (&cp, swap_bytes, 1, &geom->pt[i]);
- }
- return GEOM_SUCCESS;
- }
- static int _get_wkb_lines (
- char *cp,
- int swap_bytes,
- Geometry *geom
- )
- {
- int rc;
- int current_pt = 0;
- int i, npoints, nlines;
- WkbType wkb_type;
- if (geom->type == geomMultiLineString)
- {
-
- get_integer (&cp, swap_bytes, 1, &nlines);
- if (nlines < 1)
- return GEOM_INVALID_NUM_PARTS;
-
- rc = _get_wkb_header (&cp, &swap_bytes, &wkb_type);
- if (rc != GEOM_SUCCESS)
- return rc;
-
- if (wkb_type != wkbLineString)
- return GEOM_INVALID_WKB_TYPE;
- }
- else
- {
- nlines = 1;
- }
- geom->num_points = 0;
- geom->num_parts = nlines;
- geom->num_subparts = 0;
- geom->offsets = malloc (nlines * sizeof(int));
-
-
- for (i = 0; i < nlines; i++)
- {
- geom->offsets[i] = current_pt;
-
- get_integer (&cp, swap_bytes, 1, &npoints);
- if (npoints < 2)
- return GEOM_TOO_FEW_POINTS;
-
- rc = geom_allocate (geom, geom->num_points + npoints);
- if (rc != GEOM_SUCCESS)
- return rc;
- geom->num_points += npoints;
-
- get_xy (&cp, swap_bytes, npoints, &geom->pt[current_pt]);
- current_pt += npoints;
- if (i < nlines - 1)
- {
-
- rc = _get_wkb_header (&cp, &swap_bytes, &wkb_type);
- if (rc != GEOM_SUCCESS)
- return rc;
- if (wkb_type != wkbLineString)
- return GEOM_INVALID_WKB_TYPE;
- }
- }
-
- return GEOM_SUCCESS;
- }
- static int _get_wkb_polys (
- char *cp,
- int swap_bytes,
- Geometry *geom
- )
- {
- int current_pt = 0;
- int rc;
- int i, j, k;
- int npoints, npolys, nrings;
- WkbType wkb_type;
- if (geom->type == geomMultiPolygon)
- {
-
- get_integer (&cp, swap_bytes, 1, &npolys);
- if (npolys < 1)
- return GEOM_INVALID_NUM_PARTS;
-
- rc = _get_wkb_header (&cp, &swap_bytes, &wkb_type);
- if (rc != GEOM_SUCCESS)
- return rc;
-
- if (wkb_type != wkbPolygon)
- return GEOM_INVALID_WKB_TYPE;
- }
- else
- {
- npolys = 1;
- }
-
- geom->num_points = 0;
- geom->num_parts = npolys;
- geom->num_subparts = 0;
- geom->offsets = malloc (npolys * sizeof(int));
- geom->suboffsets = malloc (3 * npolys * sizeof(int));
- geom->sub_allocsize = 3 * npolys;
-
- k = 0;
-
-
- for (i = 0; i < npolys; i++)
- {
- geom->offsets[i] = current_pt;
-
-
- get_integer (&cp, swap_bytes, 1, &nrings);
- if (nrings < 1)
- return GEOM_INVALID_NUM_PARTS;
-
- if (geom->num_subparts + nrings >= geom->sub_allocsize)
- {
- int *newArray;
- geom->sub_allocsize = geom->num_subparts + 2 * nrings;
- newArray = malloc (geom->sub_allocsize * sizeof(int));
- memcpy (newArray, geom->suboffsets,
- geom->num_subparts * sizeof(int));
- free (geom->suboffsets);
- geom->suboffsets = newArray;
- }
- geom->num_subparts += nrings;
-
- for (j = 0; j < nrings; j++)
- {
- geom->suboffsets[k] = current_pt;
- k++;
-
-
- get_integer (&cp, swap_bytes, 1, &npoints);
- if (npoints < 4)
- return GEOM_TOO_FEW_POINTS;
- else
- {
-
- rc = geom_allocate (geom, geom->num_points + npoints);
- if (rc != GEOM_SUCCESS)
- return rc;
- geom->num_points += npoints;
-
- get_xy (&cp, swap_bytes, npoints, &geom->pt[current_pt]);
- current_pt += npoints;
- }
- }
- if (i < npolys - 1)
- {
-
- rc = _get_wkb_header (&cp, &swap_bytes, &wkb_type);
- if (rc != GEOM_SUCCESS)
- return rc;
- if (wkb_type != wkbPolygon)
- return GEOM_INVALID_WKB_TYPE;
- }
- }
-
- return GEOM_SUCCESS;
- }
|