*
**********************************************************************
* $Log$
+ * Revision 1.37 2003/11/26 18:54:22 strk
+ * fixed bug in HexDecoder, made WKB parsing the default
+ *
* Revision 1.36 2003/11/26 18:14:11 strk
* binary cursor implemented
*
#define VERBOSE 1
/* Define this to try WKB parsing */
-#undef USE_WKB
+#define USE_WKB
/* Define this to use HEX encoding instead of bytea encoding */
-#undef HEXWKB
+#define HEXWKB
+
+typedef unsigned long int uint32;
+typedef unsigned char byte;
/* Global data */
PGconn *conn;
int is3d;
int binary;
#ifdef USE_WKB
-SHPObject * (*shape_creator)(char *, int);
+SHPObject * (*shape_creator)(byte *, int);
#else
SHPObject * (*shape_creator)(char *, int, SHPHandle, int);
#endif
int is_clockwise(int num_points,double *x,double *y,double *z);
/* WKB functions */
-typedef unsigned long int uint32;
-SHPObject * create_polygon3D_WKB(char *wkb, int shape_id);
-SHPObject * create_polygon2D_WKB(char *wkb, int shape_id);
-SHPObject * create_multipoint2D_WKB(char *wkb, int shape_id);
-SHPObject * create_multipoint3D_WKB(char *wkb, int shape_id);
-SHPObject * create_point2D_WKB(char *wkb, int shape_id);
-SHPObject * create_point3D_WKB(char *wkb, int shape_id);
-SHPObject * create_multiline3D_WKB (char *wkb, int shpid);
-SHPObject * create_multiline2D_WKB (char *wkb, int shpid);
-SHPObject * create_line2D_WKB(char *wkb, int shape_id);
-SHPObject * create_line3D_WKB(char *wkb, int shape_id);
-SHPObject * create_multipolygon2D_WKB(char *wkb, int shpid);
-SHPObject * create_multipolygon3D_WKB(char *wkb, int shpid);
-char getbyte(char *c);
-void skipbyte(char **c);
-char popbyte(char **c);
-int popint(char **c);
-int getint(char *c);
-void skipint(char **c);
-double popdouble(char **c);
-double getdouble(char *c);
-void skipdouble(char **c);
-void dump_wkb(char *wkb);
-char * HexDecode(char *hex);
+SHPObject * create_polygon3D_WKB(byte *wkb, int shape_id);
+SHPObject * create_polygon2D_WKB(byte *wkb, int shape_id);
+SHPObject * create_multipoint2D_WKB(byte *wkb, int shape_id);
+SHPObject * create_multipoint3D_WKB(byte *wkb, int shape_id);
+SHPObject * create_point2D_WKB(byte *wkb, int shape_id);
+SHPObject * create_point3D_WKB(byte *wkb, int shape_id);
+SHPObject * create_multiline3D_WKB (byte *wkb, int shpid);
+SHPObject * create_multiline2D_WKB (byte *wkb, int shpid);
+SHPObject * create_line2D_WKB(byte *wkb, int shape_id);
+SHPObject * create_line3D_WKB(byte *wkb, int shape_id);
+SHPObject * create_multipolygon2D_WKB(byte *wkb, int shpid);
+SHPObject * create_multipolygon3D_WKB(byte *wkb, int shpid);
+byte getbyte(byte *c);
+void skipbyte(byte **c);
+byte popbyte(byte **c);
+uint32 popint(byte **c);
+uint32 getint(byte *c);
+void skipint(byte **c);
+double popdouble(byte **c);
+double getdouble(byte *c);
+void skipdouble(byte **c);
+void dump_wkb(byte *wkb);
+byte * HexDecode(byte *hex);
#define WKB3DOFFSET 0x80000000
}
SHPObject *
-create_multiline3D_WKB (char *wkb, int shape_id)
+create_multiline3D_WKB (byte *wkb, int shape_id)
{
SHPObject *obj;
double *x=NULL, *y=NULL, *z=NULL;
}
SHPObject *
-create_multiline2D_WKB (char *wkb, int shape_id)
+create_multiline2D_WKB (byte *wkb, int shape_id)
{
double *x=NULL, *y=NULL;
int nparts=0, *part_index=NULL, totpoints=0, nlines=0;
}
SHPObject *
-create_line3D_WKB (char *wkb, int shape_id)
+create_line3D_WKB (byte *wkb, int shape_id)
{
double *x=NULL, *y=NULL, *z=NULL;
uint32 npoints=0, pn;
npoints = popint(&wkb);
#if VERBOSE > 2
- printf("Line %d has %d points\n", li, npoints);
+ printf("Line has %lu points\n", npoints);
#endif
x = malloc(sizeof(double)*(npoints));
}
SHPObject *
-create_line2D_WKB (char *wkb, int shape_id)
+create_line2D_WKB (byte *wkb, int shape_id)
{
double *x=NULL, *y=NULL, *z=NULL;
uint32 npoints=0, pn;
npoints = popint(&wkb);
#if VERBOSE > 2
- printf("Line %d has %d points\n", li, npoints);
+ printf("Line has %lu points\n", npoints);
#endif
x = malloc(sizeof(double)*(npoints));
SHPObject *
-create_point3D_WKB(char *wkb, int shape_id)
+create_point3D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
double x, y, z;
}
SHPObject *
-create_point2D_WKB(char *wkb, int shape_id)
+create_point2D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
double x, y;
}
SHPObject *
-create_multipoint3D_WKB(char *wkb, int shape_id)
+create_multipoint3D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
double *x=NULL, *y=NULL, *z=NULL;
}
SHPObject *
-create_multipoint2D_WKB(char *wkb, int shape_id)
+create_multipoint2D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
double *x=NULL, *y=NULL;
}
SHPObject *
-create_polygon2D_WKB(char *wkb, int shape_id)
+create_polygon2D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
int ri, nrings, totpoints=0, *part_index=NULL;
}
SHPObject *
-create_polygon3D_WKB(char *wkb, int shape_id)
+create_polygon3D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
int ri, nrings, totpoints=0, *part_index=NULL;
}
SHPObject *
-create_multipolygon2D_WKB(char *wkb, int shape_id)
+create_multipolygon2D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
uint32 nrings, nparts;
x[totpoints+pn] = popdouble(&wkb);
y[totpoints+pn] = popdouble(&wkb);
#if VERBOSE > 3
- printf("Point%d (%f,%f)\n", pn, x[totpoints+pn], y[totpoints+pn]);
+ printf("Point%lu (%f,%f)\n", pn, x[totpoints+pn], y[totpoints+pn]);
#endif
}
}
SHPObject *
-create_multipolygon3D_WKB(char *wkb, int shape_id)
+create_multipolygon3D_WKB(byte *wkb, int shape_id)
{
SHPObject *obj;
obj = (SHPObject *)malloc(sizeof(SHPObject));
* Input is a NULL-terminated string.
* Output is a binary string.
*/
-char *
-HexDecode(char *hex)
+byte *
+HexDecode(byte *hex)
{
- char *ret, *retptr, *hexptr;
- char byte;
+ byte *ret, *retptr, *hexptr;
+ byte byt;
int len;
len = strlen(hex)/2;
+ ret = (byte *)malloc(len);
+ if ( ! ret ) {
+ fprintf(stderr, "Out of virtual memory\n");
+ exit(1);
+ }
- ret = (char *)malloc(len);
//printf("Decoding %d bytes", len); fflush(stdout);
hexptr = hex; retptr = ret;
//printf("%c", *hexptr);
if ( *hexptr < 58 && *hexptr > 47 )
- byte = (((*hexptr)-48)<<4);
+ byt = (((*hexptr)-48)<<4);
else if ( *hexptr > 64 && *hexptr < 71 )
- byte = (((*hexptr)-65)<<4);
+ byt = (((*hexptr)-55)<<4);
else {
fprintf(stderr, "Malformed WKB\n");
exit(1);
//printf("%c", *hexptr);
if ( *hexptr < 58 && *hexptr > 47 )
- byte |= ((*hexptr)-48);
+ byt |= ((*hexptr)-48);
else if ( *hexptr > 64 && *hexptr < 71 )
- byte |= ((*hexptr)-65);
+ byt |= ((*hexptr)-55);
else {
fprintf(stderr, "Malformed WKB\n");
exit(1);
}
hexptr++;
- //printf("(%d)", byte);
+ //printf("(%d)", byt);
- *retptr = (char)byte;
+ *retptr = (byte)byt;
retptr++;
}
//printf(" Done.\n");
*********************************************************************/
void
-dump_wkb(char *wkb)
+dump_wkb(byte *wkb)
{
int byteOrder;
int type;
}
-void skipbyte(char **c) {
+void skipbyte(byte **c) {
*c+=1;
}
-char getbyte(char *c) {
+byte getbyte(byte *c) {
return *((char*)c);
}
// #define popbyte(x) *x++
-char popbyte(char **c) {
- return *((char*)*c++);
+byte popbyte(byte **c) {
+ return *((byte*)*c++);
}
-int popint(char **c) {
- int ret = *((int*)*c);
+uint32 popint(byte **c) {
+ int ret = *((uint32*)*c);
*c+=4;
return ret;
}
-void skipint(char **c) {
+void skipint(byte **c) {
*c+=4;
}
-int getint(char *c) {
- return *((int*)c);
+uint32 getint(byte *c) {
+ return *((uint32*)c);
}
-double popdouble(char **c) {
- double ret = *((double*)*c);
+double popdouble(byte **c) {
+ double ret;
+ ret = *((double*)*c);
*c+=8;
return ret;
}
-void skipdouble(char **c) {
+void skipdouble(byte **c) {
*c+=8;
}
-double getdouble(char *c) {
+double getdouble(byte *c) {
return *((double*)c);
}