pgsql2shp-core.c 57.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://www.postgis.org
 *
 * Copyright (C) 2001-2003 Refractions Research Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public Licence. See the COPYING file.
 *
 **********************************************************************
 *
 * PostGIS to Shapefile converter
 *
 * Original Author: Jeff Lounsbury <jeffloun@refractions.net>
 * Contributions by: Sandro Santilli <strk@keybit.bet>
 * Enhanced by: Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
 *
 **********************************************************************/

#include "../postgis_config.h"

#include "pgsql2shp-core.h"

/* Solaris9 does not provide stdint.h */
/* #include <stdint.h> */
#include <inttypes.h>

#ifdef HAVE_UNISTD_H /* for getpid() and getopt */
#include <unistd.h>
#endif

#ifdef __CYGWIN__
#include <sys/param.h>
#endif

#include "../liblwgeom/liblwgeom.h" /* for LWGEOM struct and funx */
#include "../liblwgeom/lwgeom_log.h" /* for LWDEBUG macros */

/* Maximum DBF field width (according to ARCGIS) */
#define MAX_DBF_FIELD_SIZE 254


/* Prototypes */
static int reverse_points(int num_points, double *x, double *y, double *z, double *m);
static int is_clockwise(int num_points,double *x,double *y,double *z);
static int is_bigendian(void);
static SHPObject *create_point(SHPDUMPERSTATE *state, LWPOINT *lwpoint);
static SHPObject *create_multipoint(SHPDUMPERSTATE *state, LWMPOINT *lwmultipoint);
static SHPObject *create_polygon(SHPDUMPERSTATE *state, LWPOLY *lwpolygon);
static SHPObject *create_multipolygon(SHPDUMPERSTATE *state, LWMPOLY *lwmultipolygon);
static SHPObject *create_linestring(SHPDUMPERSTATE *state, LWLINE *lwlinestring);
static SHPObject *create_multilinestring(SHPDUMPERSTATE *state, LWMLINE *lwmultilinestring);
static char *nullDBFValue(char fieldType);
static int getMaxFieldSize(PGconn *conn, char *schema, char *table, char *fname);
static int getTableInfo(SHPDUMPERSTATE *state);
static int projFileCreate(SHPDUMPERSTATE *state);

/**
 * @brief Make appropriate formatting of a DBF value based on type.
 * Might return untouched input or pointer to static private
 * buffer: use return value right away.
 */
static char * goodDBFValue(char *in, char fieldType);

/** @brief Binary to hexewkb conversion function */
char *convert_bytes_to_hex(uint8_t *ewkb, size_t size);


static SHPObject *
create_point(SHPDUMPERSTATE *state, LWPOINT *lwpoint)
{
	SHPObject *obj;
	POINT4D p4d;

	double *xpts, *ypts, *zpts, *mpts;

	/* Allocate storage for points */
	xpts = malloc(sizeof(double));
	ypts = malloc(sizeof(double));
	zpts = malloc(sizeof(double));
	mpts = malloc(sizeof(double));

	/* Grab the point: note getPoint4d will correctly handle
	the case where the POINTs don't contain Z or M coordinates */
	p4d = getPoint4d(lwpoint->point, 0);

	xpts[0] = p4d.x;
	ypts[0] = p4d.y;
	zpts[0] = p4d.z;
	mpts[0] = p4d.m;

	LWDEBUGF(4, "Point: %g %g %g %g", xpts[0], ypts[0], zpts[0], mpts[0]);

	obj = SHPCreateObject(state->outshptype, -1, 0, NULL, NULL, 1, xpts, ypts, zpts, mpts);

	free(xpts);
	free(ypts);
	free(zpts);
	free(mpts);

	return obj;
}

static SHPObject *
create_multipoint(SHPDUMPERSTATE *state, LWMPOINT *lwmultipoint)
{
	SHPObject *obj;
	POINT4D p4d;
	int i;

	double *xpts, *ypts, *zpts, *mpts;

	/* Allocate storage for points */
	xpts = malloc(sizeof(double) * lwmultipoint->ngeoms);
	ypts = malloc(sizeof(double) * lwmultipoint->ngeoms);
	zpts = malloc(sizeof(double) * lwmultipoint->ngeoms);
	mpts = malloc(sizeof(double) * lwmultipoint->ngeoms);

	/* Grab the points: note getPoint4d will correctly handle
	the case where the POINTs don't contain Z or M coordinates */
	for (i = 0; i < lwmultipoint->ngeoms; i++)
	{
		p4d = getPoint4d(lwmultipoint->geoms[i]->point, 0);

		xpts[i] = p4d.x;
		ypts[i] = p4d.y;
		zpts[i] = p4d.z;
		mpts[i] = p4d.m;

		LWDEBUGF(4, "MultiPoint %d - Point: %g %g %g %g", i, xpts[i], ypts[i], zpts[i], mpts[i]);
	}

	obj = SHPCreateObject(state->outshptype, -1, 0, NULL, NULL, lwmultipoint->ngeoms, xpts, ypts, zpts, mpts);

	free(xpts);
	free(ypts);
	free(zpts);
	free(mpts);

	return obj;
}

static SHPObject *
create_polygon(SHPDUMPERSTATE *state, LWPOLY *lwpolygon)
{
	SHPObject *obj;
	POINT4D p4d;
	int i, j;

	double *xpts, *ypts, *zpts, *mpts;

	int *shpparts, shppointtotal = 0, shppoint = 0;

	/* Allocate storage for ring pointers */
	shpparts = malloc(sizeof(int) * lwpolygon->nrings);

	/* First count through all the points in each ring so we now how much memory is required */
	for (i = 0; i < lwpolygon->nrings; i++)
		shppointtotal += lwpolygon->rings[i]->npoints;

	/* Allocate storage for points */
	xpts = malloc(sizeof(double) * shppointtotal);
	ypts = malloc(sizeof(double) * shppointtotal);
	zpts = malloc(sizeof(double) * shppointtotal);
	mpts = malloc(sizeof(double) * shppointtotal);

	LWDEBUGF(4, "Total number of points: %d", shppointtotal);

	/* Iterate through each ring setting up shpparts to point to the beginning of each ring */
	for (i = 0; i < lwpolygon->nrings; i++)
	{
		/* For each ring, store the integer coordinate offset for the start of each ring */
		shpparts[i] = shppoint;

		for (j = 0; j < lwpolygon->rings[i]->npoints; j++)
		{
			p4d = getPoint4d(lwpolygon->rings[i], j);

			xpts[shppoint] = p4d.x;
			ypts[shppoint] = p4d.y;
			zpts[shppoint] = p4d.z;
			mpts[shppoint] = p4d.m;

			LWDEBUGF(4, "Polygon Ring %d - Point: %g %g %g %g", i, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);

			/* Increment the point counter */
			shppoint++;
		}

		/*
		 * First ring should be clockwise,
		 * other rings should be counter-clockwise
		 */
		if ( i == 0 )
		{
			if ( ! is_clockwise(lwpolygon->rings[i]->npoints,
			                    &xpts[shpparts[i]], &ypts[shpparts[i]], NULL) )
			{
				LWDEBUG(4, "Outer ring not clockwise, forcing clockwise\n");

				reverse_points(lwpolygon->rings[i]->npoints,
				               &xpts[shpparts[i]], &ypts[shpparts[i]],
				               &zpts[shpparts[i]], &mpts[shpparts[i]]);
			}
		}
		else
		{
			if ( is_clockwise(lwpolygon->rings[i]->npoints,
			                  &xpts[shpparts[i]], &ypts[shpparts[i]], NULL) )
			{
				LWDEBUGF(4, "Inner ring %d not counter-clockwise, forcing counter-clockwise\n", i);

				reverse_points(lwpolygon->rings[i]->npoints,
				               &xpts[shpparts[i]], &ypts[shpparts[i]],
				               &zpts[shpparts[i]], &mpts[shpparts[i]]);
			}
		}
	}

	obj = SHPCreateObject(state->outshptype, -1, lwpolygon->nrings, shpparts, NULL, shppointtotal, xpts, ypts, zpts, mpts);

	free(xpts);
	free(ypts);
	free(zpts);
	free(mpts);
	free(shpparts);

	return obj;
}

static SHPObject *
create_multipolygon(SHPDUMPERSTATE *state, LWMPOLY *lwmultipolygon)
{
	SHPObject *obj;
	POINT4D p4d;
	int i, j, k;

	double *xpts, *ypts, *zpts, *mpts;

	int *shpparts, shppointtotal = 0, shppoint = 0, shpringtotal = 0, shpring = 0;

	/* NOTE: Multipolygons are stored in shapefiles as Polygon* shapes with multiple outer rings */

	/* First count through each ring of each polygon so we now know much memory is required */
	for (i = 0; i < lwmultipolygon->ngeoms; i++)
	{
		for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
		{
			shpringtotal++;
			shppointtotal += lwmultipolygon->geoms[i]->rings[j]->npoints;
		}
	}

	/* Allocate storage for ring pointers */
	shpparts = malloc(sizeof(int) * shpringtotal);

	/* Allocate storage for points */
	xpts = malloc(sizeof(double) * shppointtotal);
	ypts = malloc(sizeof(double) * shppointtotal);
	zpts = malloc(sizeof(double) * shppointtotal);
	mpts = malloc(sizeof(double) * shppointtotal);

	LWDEBUGF(4, "Total number of rings: %d   Total number of points: %d", shpringtotal, shppointtotal);

	/* Iterate through each ring of each polygon in turn */
	for (i = 0; i < lwmultipolygon->ngeoms; i++)
	{
		for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
		{
			/* For each ring, store the integer coordinate offset for the start of each ring */
			shpparts[shpring] = shppoint;

			LWDEBUGF(4, "Ring offset: %d", shpring);

			for (k = 0; k < lwmultipolygon->geoms[i]->rings[j]->npoints; k++)
			{
				p4d = getPoint4d(lwmultipolygon->geoms[i]->rings[j], k);

				xpts[shppoint] = p4d.x;
				ypts[shppoint] = p4d.y;
				zpts[shppoint] = p4d.z;
				mpts[shppoint] = p4d.m;

				LWDEBUGF(4, "MultiPolygon %d Polygon Ring %d - Point: %g %g %g %g", i, j, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);

				/* Increment the point counter */
				shppoint++;
			}

			/*
			* First ring should be clockwise,
			* other rings should be counter-clockwise
			*/
			if ( j == 0 )
			{
				if ( ! is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
				                    &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
				{
					LWDEBUG(4, "Outer ring not clockwise, forcing clockwise\n");

					reverse_points(lwmultipolygon->geoms[i]->rings[j]->npoints,
					               &xpts[shpparts[shpring]], &ypts[shpparts[shpring]],
					               &zpts[shpparts[shpring]], &mpts[shpparts[shpring]]);
				}
			}
			else
			{
				if ( is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
				                  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
				{
					LWDEBUGF(4, "Inner ring %d not counter-clockwise, forcing counter-clockwise\n", i);

					reverse_points(lwmultipolygon->geoms[i]->rings[j]->npoints,
					               &xpts[shpparts[shpring]], &ypts[shpparts[shpring]],
					               &zpts[shpparts[shpring]], &mpts[shpparts[shpring]]);
				}
			}

			/* Increment the ring counter */
			shpring++;
		}
	}

	obj = SHPCreateObject(state->outshptype, -1, shpringtotal, shpparts, NULL, shppointtotal, xpts, ypts, zpts, mpts);

	free(xpts);
	free(ypts);
	free(zpts);
	free(mpts);
	free(shpparts);

	return obj;
}

static SHPObject *
create_linestring(SHPDUMPERSTATE *state, LWLINE *lwlinestring)
{
	SHPObject *obj;
	POINT4D p4d;
	int i;

	double *xpts, *ypts, *zpts, *mpts;

	/* Allocate storage for points */
	xpts = malloc(sizeof(double) * lwlinestring->points->npoints);
	ypts = malloc(sizeof(double) * lwlinestring->points->npoints);
	zpts = malloc(sizeof(double) * lwlinestring->points->npoints);
	mpts = malloc(sizeof(double) * lwlinestring->points->npoints);

	/* Grab the points: note getPoint4d will correctly handle
	the case where the POINTs don't contain Z or M coordinates */
	for (i = 0; i < lwlinestring->points->npoints; i++)
	{
		p4d = getPoint4d(lwlinestring->points, i);

		xpts[i] = p4d.x;
		ypts[i] = p4d.y;
		zpts[i] = p4d.z;
		mpts[i] = p4d.m;

		LWDEBUGF(4, "Linestring - Point: %g %g %g %g", i, xpts[i], ypts[i], zpts[i], mpts[i]);
	}

	obj = SHPCreateObject(state->outshptype, -1, 0, NULL, NULL, lwlinestring->points->npoints, xpts, ypts, zpts, mpts);

	free(xpts);
	free(ypts);
	free(zpts);
	free(mpts);

	return obj;
}

static SHPObject *
create_multilinestring(SHPDUMPERSTATE *state, LWMLINE *lwmultilinestring)
{
	SHPObject *obj;
	POINT4D p4d;
	int i, j;

	double *xpts, *ypts, *zpts, *mpts;

	int *shpparts, shppointtotal = 0, shppoint = 0;

	/* Allocate storage for ring pointers */
	shpparts = malloc(sizeof(int) * lwmultilinestring->ngeoms);

	/* First count through all the points in each linestring so we now how much memory is required */
	for (i = 0; i < lwmultilinestring->ngeoms; i++)
		shppointtotal += lwmultilinestring->geoms[i]->points->npoints;

	LWDEBUGF(3, "Total number of points: %d", shppointtotal);

	/* Allocate storage for points */
	xpts = malloc(sizeof(double) * shppointtotal);
	ypts = malloc(sizeof(double) * shppointtotal);
	zpts = malloc(sizeof(double) * shppointtotal);
	mpts = malloc(sizeof(double) * shppointtotal);

	/* Iterate through each linestring setting up shpparts to point to the beginning of each line */
	for (i = 0; i < lwmultilinestring->ngeoms; i++)
	{
		/* For each linestring, store the integer coordinate offset for the start of each line */
		shpparts[i] = shppoint;

		for (j = 0; j < lwmultilinestring->geoms[i]->points->npoints; j++)
		{
			p4d = getPoint4d(lwmultilinestring->geoms[i]->points, j);

			xpts[shppoint] = p4d.x;
			ypts[shppoint] = p4d.y;
			zpts[shppoint] = p4d.z;
			mpts[shppoint] = p4d.m;

			LWDEBUGF(4, "Linestring %d - Point: %g %g %g %g", i, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);

			/* Increment the point counter */
			shppoint++;
		}
	}

	obj = SHPCreateObject(state->outshptype, -1, lwmultilinestring->ngeoms, shpparts, NULL, shppoint, xpts, ypts, zpts, mpts);

	free(xpts);
	free(ypts);
	free(zpts);
	free(mpts);

	return obj;
}



/*Reverse the clockwise-ness of the point list... */
static int
reverse_points(int num_points, double *x, double *y, double *z, double *m)
{

	int i,j;
	double temp;
	j = num_points -1;
	for (i=0; i <num_points; i++)
	{
		if (j <= i)
		{
			break;
		}
		temp = x[j];
		x[j] = x[i];
		x[i] = temp;

		temp = y[j];
		y[j] = y[i];
		y[i] = temp;

		if ( z )
		{
			temp = z[j];
			z[j] = z[i];
			z[i] = temp;
		}

		if ( m )
		{
			temp = m[j];
			m[j] = m[i];
			m[i] = temp;
		}

		j--;
	}
	return 1;
}

/* Return 1 if the points are in clockwise order */
static int
is_clockwise(int num_points, double *x, double *y, double *z)
{
	int i;
	double x_change,y_change,area;
	double *x_new, *y_new; /* the points, translated to the origin
							* for safer accuracy */

	x_new = (double *)malloc(sizeof(double) * num_points);
	y_new = (double *)malloc(sizeof(double) * num_points);
	area=0.0;
	x_change = x[0];
	y_change = y[0];

	for (i=0; i < num_points ; i++)
	{
		x_new[i] = x[i] - x_change;
		y_new[i] = y[i] - y_change;
	}

	for (i=0; i < num_points - 1; i++)
	{
		/* calculate the area	 */
		area += (x[i] * y[i+1]) - (y[i] * x[i+1]);
	}
	if (area > 0 )
	{
		free(x_new);
		free(y_new);
		return 0; /*counter-clockwise */
	}
	else
	{
		free(x_new);
		free(y_new);
		return 1; /*clockwise */
	}
}


/*
 * Return the maximum octet_length from given table.
 * Return -1 on error.
 */
static int
getMaxFieldSize(PGconn *conn, char *schema, char *table, char *fname)
{
	int size;
	char *query;
	PGresult *res;

	/*( this is ugly: don't forget counting the length  */
	/* when changing the fixed query strings ) */

	if ( schema )
	{
		query = (char *)malloc(strlen(fname)+strlen(table)+
		                       strlen(schema)+46);
		sprintf(query,
		        "select max(octet_length(\"%s\"::text)) from \"%s\".\"%s\"",
		        fname, schema, table);
	}
	else
	{
		query = (char *)malloc(strlen(fname)+strlen(table)+46);
		sprintf(query,
		        "select max(octet_length(\"%s\"::text)) from \"%s\"",
		        fname, table);
	}

	LWDEBUGF(4, "maxFieldLenQuery: %s\n", query);

	res = PQexec(conn, query);
	free(query);
	if ( ! res || PQresultStatus(res) != PGRES_TUPLES_OK )
	{
		printf( _("Querying for maximum field length: %s"),
		        PQerrorMessage(conn));
		return -1;
	}

	if (PQntuples(res) <= 0 )
	{
		PQclear(res);
		return -1;
	}
	size = atoi(PQgetvalue(res, 0, 0));
	PQclear(res);
	return size;
}

static int
is_bigendian(void)
{
	int test = 1;

	if ( (((char *)(&test))[0]) == 1)
	{
		return 0; /*NDR (little_endian) */
	}
	else
	{
		return 1; /*XDR (big_endian) */
	}
}

char *
shapetypename(int num)
{
	switch (num)
	{
	case SHPT_NULL:
		return "Null Shape";
	case SHPT_POINT:
		return "Point";
	case SHPT_ARC:
		return "PolyLine";
	case SHPT_POLYGON:
		return "Polygon";
	case SHPT_MULTIPOINT:
		return "MultiPoint";
	case SHPT_POINTZ:
		return "PointZ";
	case SHPT_ARCZ:
		return "PolyLineZ";
	case SHPT_POLYGONZ:
		return "PolygonZ";
	case SHPT_MULTIPOINTZ:
		return "MultiPointZ";
	case SHPT_POINTM:
		return "PointM";
	case SHPT_ARCM:
		return "PolyLineM";
	case SHPT_POLYGONM:
		return "PolygonM";
	case SHPT_MULTIPOINTM:
		return "MultiPointM";
	case SHPT_MULTIPATCH:
		return "MultiPatch";
	default:
		return "Unknown";
	}
}


/* This is taken and adapted from dbfopen.c of shapelib */
static char *
nullDBFValue(char fieldType)
{
	switch (fieldType)
	{
	case FTInteger:
	case FTDouble:
		/* NULL numeric fields have value "****************" */
		return "****************";

	case FTDate:
		/* NULL date fields have value "00000000" */
		return "        ";

	case FTLogical:
		/* NULL boolean fields have value "?" */
		return "?";

	default:
		/* empty string fields are considered NULL */
		return "";
	}
}

/**
 * @brief Make appropriate formatting of a DBF value based on type.
 * 		Might return untouched input or pointer to static private
 * 		buffer: use return value right away.
 */
static char *
goodDBFValue(char *in, char fieldType)
{
	/*
	 * We only work on FTLogical and FTDate.
	 * FTLogical is 1 byte, FTDate is 8 byte (YYYYMMDD)
	 * We allocate space for 9 bytes to take
	 * terminating null into account
	 */
	static char buf[9];

	switch (fieldType)
	{
	case FTLogical:
		buf[0] = toupper(in[0]);
		buf[1]='\0';
		return buf;
	case FTDate:
		buf[0]=in[0]; /* Y */
		buf[1]=in[1]; /* Y */
		buf[2]=in[2]; /* Y */
		buf[3]=in[3]; /* Y */
		buf[4]=in[5]; /* M */
		buf[5]=in[6]; /* M */
		buf[6]=in[8]; /* D */
		buf[7]=in[9]; /* D */
		buf[8]='\0';
		return buf;
	default:
		return in;
	}
}

char *convert_bytes_to_hex(uint8_t *ewkb, size_t size)
{
	int i;
	char *hexewkb;

	/* Convert the byte stream to a hex string using liblwgeom's deparse_hex function */
	hexewkb = malloc(size * 2 + 1);
	for (i=0; i<size; ++i) deparse_hex(ewkb[i], &hexewkb[i * 2]);
	hexewkb[size * 2] = '\0';

	return hexewkb;
}

/**
 * @brief Creates ESRI .prj file for this shp output
 * 		It looks in the spatial_ref_sys table and outputs the srtext field for this data
 * 		If data is a table will use geometry_columns, if a query or view will read SRID from query output.
 *	@warning Will give warning and not output a .prj file if SRID is -1, Unknown, mixed SRIDS or not found in spatial_ref_sys.  The dbf and shp will still be output.
 */
static int
projFileCreate(SHPDUMPERSTATE *state)
{
	FILE	*fp;
	char	*pszFullname, *pszBasename;
709
	int	i;
710 711 712 713 714 715 716 717 718 719 720 721

	char *pszFilename = state->shp_file;
	char *schema = state->schema;
	char *table = state->table;
	char *geo_col_name = state->geo_col_name;

	char *srtext;
	char *query;
	char *esc_schema;
	char *esc_table;
	char *esc_geo_col_name;

722
	int error, result;
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	PGresult *res;
	int size;

	/***********
	*** I'm multiplying by 2 instead of 3 because I am too lazy to figure out how many characters to add
	*** after escaping if any **/
	size = 1000;
	if ( schema )
	{
		size += 3 * strlen(schema);
	}
	size += 1000;
	esc_table = (char *) malloc(3 * strlen(table) + 1);
	esc_geo_col_name = (char *) malloc(3 * strlen(geo_col_name) + 1);
	PQescapeStringConn(state->conn, esc_table, table, strlen(table), &error);
	PQescapeStringConn(state->conn, esc_geo_col_name, geo_col_name, strlen(geo_col_name), &error);

	/** make our address space large enough to hold query with table/schema **/
	query = (char *) malloc(size);
	if ( ! query ) return 0; /* out of virtual memory */

	/**************************************************
	 * Get what kind of spatial ref is the selected geometry field
	 * We first check the geometry_columns table for a match and then if no match do a distinct against the table
	 * NOTE: COALESCE does a short-circuit check returning the faster query result and skipping the second if first returns something
	 *	Escaping quotes in the schema and table in query may not be necessary except to prevent malicious attacks
	 *	or should someone be crazy enough to have quotes or other weird character in their table, column or schema names
	 **************************************************/
	if ( schema )
	{
		esc_schema = (char *) malloc(2 * strlen(schema) + 1);
		PQescapeStringConn(state->conn, esc_schema, schema, strlen(schema), &error);
		sprintf(query, "SELECT COALESCE((SELECT sr.srtext "
		        " FROM  geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid "
		        " WHERE gc.f_table_schema = '%s' AND gc.f_table_name = '%s' AND gc.f_geometry_column = '%s' LIMIT 1),  "
		        " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END As srtext "
		        " FROM \"%s\".\"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)) , ' ') As srtext ",
		        esc_schema, esc_table,esc_geo_col_name, schema, table, geo_col_name);
		free(esc_schema);
	}
	else
	{
		sprintf(query, "SELECT COALESCE((SELECT sr.srtext "
		        " FROM  geometry_columns As gc INNER JOIN spatial_ref_sys sr ON sr.srid = gc.srid "
		        " WHERE gc.f_table_name = '%s' AND gc.f_geometry_column = '%s' AND pg_table_is_visible((gc.f_table_schema || '.' || gc.f_table_name)::regclass) LIMIT 1),  "
		        " (SELECT CASE WHEN COUNT(DISTINCT sr.srid) > 1 THEN 'm' ELSE MAX(sr.srtext) END as srtext "
		        " FROM \"%s\" As g INNER JOIN spatial_ref_sys sr ON sr.srid = ST_SRID((g.\"%s\")::geometry)), ' ') As srtext ",
		        esc_table, esc_geo_col_name, table, geo_col_name);
	}

	LWDEBUGF(3,"%s\n",query);
	free(esc_table);
	free(esc_geo_col_name);

	res = PQexec(state->conn, query);

	if ( ! res || PQresultStatus(res) != PGRES_TUPLES_OK )
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Could not execute prj query: %s"), PQresultErrorMessage(res));
		PQclear(res);
		free(query);
		return SHPDUMPERWARN;
	}

	for (i=0; i < PQntuples(res); i++)
	{
		srtext = PQgetvalue(res, i, 0);
		if (strcmp(srtext,"m") == 0)
		{
			snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Mixed set of spatial references. No prj file will be generated"));
			PQclear(res);
			free(query);
			return SHPDUMPERWARN;
		}
		else
		{
			if (srtext[0] == ' ')
			{
				snprintf(state->message, SHPDUMPERMSGLEN, _("WARNING: Cannot determine spatial reference (empty table or unknown spatial ref). No prj file will be generated."));
				PQclear(res);
				free(query);
				return SHPDUMPERWARN;
			}
			else
			{
				/* -------------------------------------------------------------------- */
				/*	Compute the base (layer) name.  If there is any extension	*/
				/*	on the passed in filename we will strip it off.			*/
				/* -------------------------------------------------------------------- */
				pszBasename = (char *) malloc(strlen(pszFilename)+5);
				strcpy( pszBasename, pszFilename );
				for ( i = strlen(pszBasename)-1;
				        i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
				        && pszBasename[i] != '\\';
				        i-- ) {}

				if ( pszBasename[i] == '.' )
					pszBasename[i] = '\0';

				pszFullname = (char *) malloc(strlen(pszBasename) + 5);
				sprintf( pszFullname, "%s.prj", pszBasename );
				free( pszBasename );


				/* -------------------------------------------------------------------- */
				/*      Create the file.                                                */
				/* -------------------------------------------------------------------- */
				fp = fopen( pszFullname, "wb" );
				if ( fp == NULL )
				{
					return 0;
				}
835 836 837 838 839 840 841 842 843 844 845 846
				{
				    result = fputs (srtext,fp);
                    LWDEBUGF(3, "\n result %d proj SRText is %s .\n", result, srtext);
                    if (result == EOF)
                    {
                        fclose( fp );
                        free( pszFullname );
                        PQclear(res);
                        free(query);
                        return 0;
                    }
				}
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
				fclose( fp );
				free( pszFullname );
			}
		}
	}
	PQclear(res);
	free(query);
	return SHPDUMPEROK;
}


static int 
getTableInfo(SHPDUMPERSTATE *state)
{

	/* Get some more information from the table:
		- count = total number of geometries/geographies in the table

	   and if we have found a suitable geometry column:

		- max = maximum number of dimensions within the geometry/geography column
		- geometrytype = string representing the geometry/geography type, e.g. POINT

	   Since max/geometrytype already require a sequential scan of the table, we may as
	   well get the row count too.
	 */

	PGresult *res;
	char *query;
	int tmpint;


	if (state->geo_col_name)
	{
		/* Include geometry information */
		if (state->schema)
		{
			query = malloc(150 + 4 * strlen(state->geo_col_name) + strlen(state->schema) + strlen(state->table));
	
			sprintf(query, "SELECT count(\"%s\"), max(ST_zmflag(\"%s\"::geometry)), geometrytype(\"%s\"::geometry) FROM \"%s\".\"%s\" GROUP BY geometrytype(\"%s\"::geometry)",
			state->geo_col_name, state->geo_col_name, state->geo_col_name, state->schema, state->table, state->geo_col_name);
		}
		else
		{
			query = malloc(150 + 4 * strlen(state->geo_col_name) + strlen(state->table));
	
			sprintf(query, "SELECT count(\"%s\"), max(ST_zmflag(\"%s\"::geometry)), geometrytype(\"%s\"::geometry) FROM \"%s\" GROUP BY geometrytype(\"%s\"::geometry)",
			state->geo_col_name, state->geo_col_name, state->geo_col_name, state->table, state->geo_col_name);
		}
	}
	else
	{
		/* Otherwise... just a row count will do */
		if (state->schema)
		{
			query = malloc(40 + strlen(state->schema) + strlen(state->table));
			
			sprintf(query, "SELECT count(1) FROM \"%s\".\"%s\"", state->schema, state->table);
		}
		else
		{
			query = malloc(40 + strlen(state->table));

			sprintf(query, "SELECT count(1) FROM \"%s\"", state->table);
		}
	}

	LWDEBUGF(3, "Table metadata query: %s\n", query);

	res = PQexec(state->conn, query);
	free(query);

	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("ERROR: Could not execute table metadata query: %s"), PQresultErrorMessage(res));
		PQclear(res);
		return SHPDUMPERERR;
	}

	/* Make sure we error if the table is empty */
	if (PQntuples(res) == 0)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("ERROR: Could not determine table metadata (empty table)"));
		PQclear(res);
		return SHPDUMPERERR;
	}

	/* If we have a geo* column, get the dimension, type and count information */
	if (state->geo_col_name)
	{
		/* If a table has a geometry column containing mixed types then
		   the metadata query will return multiple rows. We need to cycle
		   through all rows to determine if the type combinations are valid.

		   Note that if we find a combination of a MULTI and non-MULTI geometry
		   of the same type, we always choose MULTI to ensure that everything
		   gets output correctly. The create_* conversion functions are clever
		   enough to up-convert the non-MULTI geometry to a MULTI in this case. */

		int dummy, i;
		uint8_t type = 0;
		int typefound = 0, typemismatch = 0;

		state->rowcount = 0;

		for (i = 0; i < PQntuples(res); i++)
		{
			geometry_type_from_string(PQgetvalue(res, i, 2), &type, &dummy, &dummy);

956 957
			if (!type) continue; /* skip null geometries */

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
			/* We can always set typefound to that of the first column found */
			if (!typefound)
				typefound = type;

			switch (type)
			{
			case MULTIPOINTTYPE:
				if (typefound != MULTIPOINTTYPE && typefound != POINTTYPE)
					typemismatch = 1;
				else
					typefound = MULTIPOINTTYPE;
				break;

			case MULTILINETYPE:
				if (typefound != MULTILINETYPE && typefound != LINETYPE)
					typemismatch = 1;
				else
					typefound = MULTILINETYPE;
				break;					

			case MULTIPOLYGONTYPE:
				if (typefound != MULTIPOLYGONTYPE && typefound != POLYGONTYPE)
					typemismatch = 1;
				else
					typefound = MULTIPOLYGONTYPE;
				break;

			case POINTTYPE:
				if (typefound != POINTTYPE && typefound != MULTIPOINTTYPE)
					typemismatch = 1;
				else if (!lwtype_is_collection(type))
					typefound = POINTTYPE;
				break;

			case LINETYPE:
				if (typefound != LINETYPE && typefound != MULTILINETYPE)
					typemismatch = 1;
				else if (!lwtype_is_collection(type))
					typefound = LINETYPE;
				break;

			case POLYGONTYPE:
				if (typefound != POLYGONTYPE && typefound != MULTIPOLYGONTYPE)
					typemismatch = 1;
				else if (!lwtype_is_collection(type))
					typefound = POLYGONTYPE;
				break;
			}

			/* Update the rowcount for each type */
			state->rowcount += atoi(PQgetvalue(res, i, 0));
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

			/* Set up the dimension output type (note: regardless of how many rows
				 the table metadata query returns, this value will be the same. But
				 we'll choose to use the first value anyway) */
			tmpint = atoi(PQgetvalue(res, i, 1));
			switch (tmpint)
			{
			case 0:
				state->outtype = 's';
				break;
			case 1:
				state->outtype = 'm';
				break;
			default:
				state->outtype = 'z';
				break;
			}

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
		}

		/* Flag an error if the table contains incompatible geometry combinations */
		if (typemismatch)
		{
			snprintf(state->message, SHPDUMPERMSGLEN, _("ERROR: Incompatible mixed geometry types in table"));
			PQclear(res);
			return SHPDUMPERERR;
		}

		/* Set up the shapefile output type based upon the dimension information */
		switch (typefound)
		{
		case POINTTYPE:
			switch(state->outtype)
			{
			case 'z':
				state->outshptype = SHPT_POINTZ;
				break;
	
			case 'm':
				state->outshptype = SHPT_POINTM;
				break;
	
			default:
				state->outshptype = SHPT_POINT;
			}
			break;

		case MULTIPOINTTYPE:
			switch(state->outtype)
			{
			case 'z':
				state->outshptype = SHPT_MULTIPOINTZ;
				break;
	
			case 'm':
				state->outshptype = SHPT_MULTIPOINTM;
				break;
	
			default:
				state->outshptype = SHPT_MULTIPOINT;
			}
			break;

		case LINETYPE:
		case MULTILINETYPE:
			switch(state->outtype)
			{
			case 'z':
				state->outshptype = SHPT_ARCZ;
				break;
	
			case 'm':
				state->outshptype = SHPT_ARCM;
				break;
	
			default:
				state->outshptype = SHPT_ARC;
			}
			break;

		case POLYGONTYPE:
		case MULTIPOLYGONTYPE:
			switch(state->outtype)
			{
			case 'z':
				state->outshptype = SHPT_POLYGONZ;
				break;
	
			case 'm':
				state->outshptype = SHPT_POLYGONM;
				break;
	
			default:
				state->outshptype = SHPT_POLYGON;
			}
			break;
		}
	}
	else
	{
		/* Without a geo* column the total is simply the first (COUNT) column */
		state->rowcount = atoi(PQgetvalue(res, 0, 0));
	}

	/* Dispose of the result set */
	PQclear(res);

	return SHPDUMPEROK;
}


/* Default configuration settings */
void
set_dumper_config_defaults(SHPDUMPERCONFIG *config)
{
	config->conn = malloc(sizeof(SHPCONNECTIONCONFIG));
	config->conn->host = NULL;
	config->conn->port = NULL;
	config->conn->database = NULL;
	config->conn->username = NULL;
	config->conn->password = NULL;

	config->table = NULL;
	config->schema = NULL;
	config->usrquery = NULL;
	config->binary = 0;
	config->shp_file = NULL;
	config->dswitchprovided = 0;
	config->includegid = 0;
	config->unescapedattrs = 0;
	config->geo_col_name = NULL;
	config->keep_fieldname_case = 0;
	config->fetchsize = 100;
	config->column_map_filename = NULL;
}

/* Create a new shapefile state object */
SHPDUMPERSTATE *
ShpDumperCreate(SHPDUMPERCONFIG *config)
{
	SHPDUMPERSTATE *state;

	/* Create a new state object and assign the config to it */
	state = malloc(sizeof(SHPDUMPERSTATE));
	state->config = config;

	/* Set any state defaults */
	state->conn = NULL;
	state->outtype = 's';
	state->geom_oid = 0;
	state->geog_oid = 0;
	state->schema = NULL;
	state->table = NULL;
	state->geo_col_name = NULL;
	state->fetch_query = NULL;
	state->main_scan_query = NULL;
	state->dbffieldnames = NULL;
	state->dbffieldtypes = NULL;
	state->pgfieldnames = NULL;
	state->big_endian = is_bigendian();
1169
	colmap_init(&state->column_map);
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	
	return state;
}

/* Generate the database connection string used by a state */
char *
ShpDumperGetConnectionStringFromConn(SHPCONNECTIONCONFIG *conn)
{
	char *connstring;
	int connlen;
	
	connlen = 64 + 
		(conn->host ? strlen(conn->host) : 0) + (conn->port ? strlen(conn->port) : 0) +
		(conn->username ? strlen(conn->username) : 0) + (conn->password ? strlen(conn->password) : 0) +
		(conn->database ? strlen(conn->database) : 0);

	connstring = malloc(connlen);
	memset(connstring, 0, connlen);

	if (conn->host)
	{
		strcat(connstring, " host=");
		strcat(connstring, conn->host);
	}

	if (conn->port)
	{
		strcat(connstring, " port=");
		strcat(connstring, conn->port);
	}

	if (conn->username)
	{
		strcat(connstring, " user=");
		strcat(connstring, conn->username);
	}

	if (conn->password)
	{	
		strcat(connstring, " password='");
		strcat(connstring, conn->password);
		strcat(connstring, "'");
	}

	if (conn->database)
	{
		strcat(connstring, " dbname=");
		strcat(connstring, conn->database);
	}

1220 1221 1222 1223 1224
	if ( ! getenv("PGCLIENTENCODING") )
	{
		strcat(connstring, " client_encoding=UTF8");
	}

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
	return connstring;
}

/* Connect to the database and identify the version of PostGIS (and any other
capabilities required) */
int
ShpDumperConnectDatabase(SHPDUMPERSTATE *state)
{
	PGresult *res;

	char *connstring, *tmpvalue;

	/* Generate the PostgreSQL connection string */
	connstring = ShpDumperGetConnectionStringFromConn(state->config->conn);

	/* Connect to the database */
	state->conn = PQconnectdb(connstring);
	if (PQstatus(state->conn) == CONNECTION_BAD)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQerrorMessage(state->conn));
		free(connstring);
		return SHPDUMPERERR;
	}

	/* Set datestyle to ISO */
	res = PQexec(state->conn, "SET DATESTYLE='ISO'");
	if (PQresultStatus(res) != PGRES_COMMAND_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
		PQclear(res);
		free(connstring);
		return SHPDUMPERERR;
	}

	PQclear(res);

	/* Retrieve PostGIS major version */
	res = PQexec(state->conn, "SELECT postgis_version()");
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, "%s", PQresultErrorMessage(res));
		PQclear(res);
		free(connstring);
		return SHPDUMPERERR;		
	}

	tmpvalue = PQgetvalue(res, 0, 0);
	state->pgis_major_version = atoi(tmpvalue);

	PQclear(res);

	/* Find the OID for the geometry type */
	res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geometry'");
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geometry oid: %s"), PQresultErrorMessage(res));
		PQclear(res);
		free(connstring);
		return SHPDUMPERERR;		
	}

	if (PQntuples(res) > 0)
	{
		tmpvalue = PQgetvalue(res, 0, 0);
		state->geom_oid = atoi(tmpvalue);
	}
	else
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Geometry type unknown (have you enabled postgis?)"));
		PQclear(res);
		free(connstring);
		return SHPDUMPERERR;
	}

	PQclear(res);

	/* Find the OID for the geography type */
	res = PQexec(state->conn, "SELECT oid FROM pg_type WHERE typname = 'geography'");
	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Error looking up geography oid: %s"), PQresultErrorMessage(res));
		PQclear(res);
		free(connstring);
		return SHPDUMPERERR;		
	}

	if (PQntuples(res) > 0)
	{
		/* Old databases don't have a geography type, so don't fail if we don't find it */
		tmpvalue = PQgetvalue(res, 0, 0);
		state->geog_oid = atoi(tmpvalue);
	}

	PQclear(res);

	free(connstring);

	return SHPDUMPEROK;
}


/* Open the specified table in preparation for extracting rows */
int
ShpDumperOpenTable(SHPDUMPERSTATE *state)
{
	PGresult *res;

	char buf[256];
	char *query;
	int gidfound = 0, i, j, ret, status;


	/* Open the column map if one was specified */
	if (state->config->column_map_filename)
	{
1340 1341 1342
		ret = colmap_read(state->config->column_map_filename,
		                  &state->column_map, state->message, SHPDUMPERMSGLEN);
		if (!ret) return SHPDUMPERERR;
1343 1344 1345 1346 1347
	}
		
	/* If a user-defined query has been specified, create and point the state to our new table */
	if (state->config->usrquery)
	{
1348
		state->table = malloc(20 + 20);		/* string + max long precision */
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
		sprintf(state->table, "__pgsql2shp%lu_tmp_table", (long)getpid());

		query = malloc(32 + strlen(state->table) + strlen(state->config->usrquery));

		sprintf(query, "CREATE TEMP TABLE \"%s\" AS %s", state->table, state->config->usrquery);
		res = PQexec(state->conn, query);
		free(query);

		/* Execute the code to create the table */
		if (PQresultStatus(res) != PGRES_COMMAND_OK)
		{
			snprintf(state->message, SHPDUMPERMSGLEN, _("Error executing user query: %s"), PQresultErrorMessage(res));
			PQclear(res);
			return SHPDUMPERERR;
		}
	}
	else
	{
		/* Simply point the state to copies of the supplied schema and table */
		state->table = strdup(state->config->table);
		if (state->config->schema)
			state->schema = strdup(state->config->schema);
	}


	/* Get the list of columns and their types for the selected table */
	if (state->schema)
	{
		query = malloc(250 + strlen(state->schema) + strlen(state->table));

		sprintf(query, "SELECT a.attname, a.atttypid, "
		        "a.atttypmod, a.attlen FROM "
		        "pg_attribute a, pg_class c, pg_namespace n WHERE "
		        "n.nspname = '%s' AND a.attrelid = c.oid AND "
		        "n.oid = c.relnamespace AND "
		        "a.atttypid != 0 AND "
		        "a.attnum > 0 AND c.relname = '%s'", state->schema, state->table);
	}
	else
	{
		query = malloc(250 + strlen(state->table));

		sprintf(query, "SELECT a.attname, a.atttypid, "
		        "a.atttypmod, a.attlen FROM "
		        "pg_attribute a, pg_class c WHERE "
		        "a.attrelid = c.oid and a.attnum > 0 AND "
		        "a.atttypid != 0 AND "
		        "c.relname = '%s' AND "
		        "pg_catalog.pg_table_is_visible(c.oid)", state->table);
	}

	LWDEBUGF(3, "query is: %s\n", query);

	res = PQexec(state->conn, query);
	free(query);

	if (PQresultStatus(res) != PGRES_TUPLES_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Error querying for attributes: %s"), PQresultErrorMessage(res));
		PQclear(res);
		return SHPDUMPERERR;
	}

	if (!PQntuples(res))
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Table %s does not exist"), state->table);
		PQclear(res);
		return SHPDUMPERERR;
	}

	/* If a shapefile name was specified, use it. Otherwise simply use the table name. */
	if (state->config->shp_file != NULL)
		state->shp_file = state->config->shp_file;
	else
		state->shp_file = state->table;

1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
	/* Create the dbf file: */
	/* If there's a user-specified encoding hanging around, try and use that. */
	/* Otherwise, just use UTF-8 encoding, since that's usually our client encoding. */
	if ( getenv("PGCLIENTENCODING") )
	{
		char *codepage = encoding2codepage(getenv("PGCLIENTENCODING"));
		state->dbf = DBFCreateEx(state->shp_file, codepage);
	}
	else
	{
		state->dbf = DBFCreateEx(state->shp_file, "UTF-8");
	}
		
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
	if (!state->dbf)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Could not create dbf file %s"), state->shp_file);
		return SHPDUMPERERR;
	}

	/*
	 * Scan the result setting fields to be returned in mainscan
	 * query, filling the type_ary, and creating .dbf and .shp files.
	 */
	state->dbffieldnames = malloc(sizeof(char *) * PQntuples(res));
	state->dbffieldtypes = malloc(sizeof(int) * PQntuples(res));
	state->pgfieldnames = malloc(sizeof(char *) * PQntuples(res));
	state->pgfieldlens = malloc(sizeof(int) * PQntuples(res));
	state->pgfieldtypmods = malloc(sizeof(int) * PQntuples(res));
	state->fieldcount = 0;
	int tmpint = 1;

	for (i = 0; i < PQntuples(res); i++)
	{
		char *ptr;

		int pgfieldtype, pgtypmod, pgfieldlen;
		char *pgfieldname;

		int dbffieldtype, dbffieldsize, dbffielddecs;
		char *dbffieldname;

		pgfieldname = PQgetvalue(res, i, 0);
		pgfieldtype = atoi(PQgetvalue(res, i, 1));
		pgtypmod = atoi(PQgetvalue(res, i, 2));
		pgfieldlen = atoi(PQgetvalue(res, i, 3));
		dbffieldtype = -1;
		dbffieldsize = 0;
		dbffielddecs = 0;

		/*
		 * This is a geometry/geography column
		 */
		if (pgfieldtype == state->geom_oid || pgfieldtype == state->geog_oid)
		{
			/* If no geometry/geography column has been found yet... */
			if (!state->geo_col_name)
			{
				/* If either no geo* column name was provided (in which case this is
				   the first match) or we match the provided column name, we have 
				   found our geo* column */
				if (!state->config->geo_col_name || !strcmp(state->config->geo_col_name, pgfieldname))
				{
					dbffieldtype = 9;
	
					state->geo_col_name = strdup(pgfieldname);
				}
			}
		}

		/*
		 * Everything else (non geometries) will be
		 * a DBF attribute.
		 */

		/* Skip gid (if not asked to do otherwise */
		if (!strcmp(pgfieldname, "gid") )
		{
			gidfound = 1;

			if (!state->config->includegid)
				continue;
		}

		/* Unescape any reserved column names */
		ptr = pgfieldname;
		if (!state->config->unescapedattrs)
		{
			if (*ptr == '_')
				ptr += 2;
		}

		/*
		 * This needs special handling since both xmin and _xmin
		 * becomes __xmin when escaped
		 */

		/* Limit dbf field name to 10-digits */
		dbffieldname = malloc(11);
		strncpy(dbffieldname, ptr, 10);
		dbffieldname[10] = '\0';

1526 1527 1528
		/* If a column map file has been passed in,
		 * use this to create the dbf field name from
		 * the PostgreSQL column name */
1529
		{
1530 1531 1532 1533 1534
		  const char *mapped = colmap_dbf_by_pg(&state->column_map, dbffieldname);
		  if (mapped)
		  {
			  strncpy(dbffieldname, mapped, 10);
			  dbffieldname[10] = '\0';
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
			}
		}
			
		/*
		 * make sure the fields all have unique names,
		 */
		tmpint = 1;
		for (j = 0; j < state->fieldcount; j++)
		{
			if (!strncasecmp(dbffieldname, state->dbffieldnames[j], 10))
			{
				sprintf(dbffieldname, "%.7s_%.2d", ptr, tmpint++);
				continue;
			}
		}

		/* make UPPERCASE if keep_fieldname_case = 0 */
		if (!state->config->keep_fieldname_case)
			for (j = 0; j < strlen(dbffieldname); j++)
				dbffieldname[j] = toupper(dbffieldname[j]);

		/* Issue warning if column has been renamed */
		if (strcasecmp(dbffieldname, pgfieldname))
		{
			/* Note: we concatenate all warnings from the main loop as this is useful information */
			snprintf(buf, 256, _("Warning, field %s renamed to %s\n"), pgfieldname, dbffieldname);
			strncat(state->message, buf, SHPDUMPERMSGLEN - strlen(state->message));

			ret = SHPDUMPERWARN;
		}


		/*
		 * Find appropriate type of dbf attributes
		 */

		/* int2 type */
		if (pgfieldtype == 21)
		{
			/*
			 * Longest text representation for
			 * an int2 type (16bit) is 6 bytes
			 * (-32768)
			 */
			dbffieldtype = FTInteger;
			dbffieldsize = 6;
			dbffielddecs = 0;
		}

		/* int4 type */
		else if (pgfieldtype == 23)
		{
			/*
			 * Longest text representation for
			 * an int4 type (32bit) is 11 bytes
			 * (-2147483648)
			 */
			dbffieldtype = FTInteger;
			dbffieldsize = 11;
			dbffielddecs = 0;
		}

		/* int8 type */
		else if (pgfieldtype == 20)
		{
			/*
			 * Longest text representation for
			 * an int8 type (64bit) is 20 bytes
			 * (-9223372036854775808)
			 */
			dbffieldtype = FTInteger;
			dbffieldsize = 19;
			dbffielddecs = 0;
		}

		/*
		 * double or numeric types:
		 *    700: float4
		 *    701: float8
		 *   1700: numeric
		 *
		 *
		 * TODO: stricter handling of sizes
		 */
		else if (pgfieldtype == 700 || pgfieldtype == 701 || pgfieldtype == 1700)
		{
			dbffieldtype = FTDouble;
			dbffieldsize = 32;
			dbffielddecs = 10;
		}

		/*
		 * Boolean field, we use FTLogical
		 */
		else if (pgfieldtype == 16)
		{
			dbffieldtype = FTLogical;
			dbffieldsize = 2;
			dbffielddecs = 0;
		}

		/*
		 * Date field
		 */
		else if (pgfieldtype == 1082)
		{
			dbffieldtype = FTDate;
			dbffieldsize = 8;
			dbffielddecs = 0;
		}

		/*
		 * time, timetz, timestamp, or timestamptz field.
		 */
		else if (pgfieldtype == 1083 || pgfieldtype == 1266 || pgfieldtype == 1114 || pgfieldtype == 1184)
		{
			int secondsize;

			switch (pgtypmod)
			{
			case -1:
				secondsize = 6 + 1;
				break;
			case 0:
				secondsize = 0;
				break;
			default:
				secondsize = pgtypmod + 1;
				break;
			}

			/* We assume the worst case scenario for all of these:
			 * date = '5874897-12-31' = 13
			 * date = '294276-11-20' = 12 (with --enable-interger-datetimes)
			 * time = '00:00:00' = 8
			 * zone = '+01:39:52' = 9 (see Europe/Helsinki around 1915)
			 */

			/* time */
			if (pgfieldtype == 1083)
			{
				dbffieldsize = 8 + secondsize;
			}
			/* timetz */
			else if (pgfieldtype == 1266)
			{
				dbffieldsize = 8 + secondsize + 9;
			}
			/* timestamp */
			else if (pgfieldtype == 1114)
			{
				dbffieldsize = 13 + 1 + 8 + secondsize;
			}
			/* timestamptz */
			else if (pgfieldtype == 1184)
			{
				dbffieldsize = 13 + 1 + 8 + secondsize + 9;
			}

			dbffieldtype = FTString;
			dbffielddecs = 0;
		}

		/*
		 * uuid type 36 bytes (12345678-9012-3456-7890-123456789012)
		 */
		else if (pgfieldtype == 2950)
		{
			dbffieldtype = FTString;
			dbffieldsize = 36;
			dbffielddecs = 0;
		}

		/*
		 * For variable-sized fields we know about, we use
		 * the maximum allowed size.
		  * 1042 is bpchar,  1043 is varchar
		 */
		else if ((pgfieldtype == 1042 || pgfieldtype == 1043) && pgtypmod != -1)
		{
			/*
			 * mod is maximum allowed size, including
			 * header which contains *real* size.
			 */
			dbffieldtype = FTString;
			dbffieldsize = pgtypmod - 4; /* 4 is header size */
			dbffielddecs = 0;
		}

		/* For all other valid non-geometry/geography fields... */
		else if (dbffieldtype == -1)
		{
			/*
			* For types we don't know anything about, all
			* we can do is query the table for the maximum field
			* size.
			*/
			dbffieldsize = getMaxFieldSize(state->conn, state->schema, state->table, pgfieldname);
			if (dbffieldsize == -1)
				return 0;

			if (!dbffieldsize)
				dbffieldsize = 32;

			/* might 0 be a good size ? */

			dbffieldtype = FTString;
			dbffielddecs = 0;

			/* Check to make sure the final field size isn't too large */
			if (dbffieldsize > MAX_DBF_FIELD_SIZE)
			{
				/* Note: we concatenate all warnings from the main loop as this is useful information */
				snprintf(buf, 256, _("Warning: values of field '%s' exceeding maximum dbf field width (%d) "
					"will be truncated.\n"), dbffieldname, MAX_DBF_FIELD_SIZE);
				strncat(state->message, buf, SHPDUMPERMSGLEN - strlen(state->message));
				dbffieldsize = MAX_DBF_FIELD_SIZE;				

				ret = SHPDUMPERWARN;
			}
		}

		LWDEBUGF(3, "DBF FIELD_NAME: %s, SIZE: %d\n", dbffieldname, dbffieldsize);
	
		if (dbffieldtype != 9)
		{
			/* Add the field to the DBF file */
			if (DBFAddField(state->dbf, dbffieldname, dbffieldtype, dbffieldsize, dbffielddecs) == -1)
			{
				snprintf(state->message, SHPDUMPERMSGLEN, _("Error: field %s of type %d could not be created."), dbffieldname, dbffieldtype);

				return SHPDUMPERERR;
			}
	
			/* Add the field information to our field arrays */
			state->dbffieldnames[state->fieldcount] = dbffieldname;
			state->dbffieldtypes[state->fieldcount] = dbffieldtype;
			state->pgfieldnames[state->fieldcount] = pgfieldname;
			state->pgfieldlens[state->fieldcount] = pgfieldlen;
			state->pgfieldtypmods[state->fieldcount] = pgtypmod;
			
			state->fieldcount++;
		}
	}

	/* Now we have generated the field lists, grab some info about the table */
	status = getTableInfo(state);
	if (status == SHPDUMPERERR)
		return SHPDUMPERERR;

	LWDEBUGF(3, "rows: %d\n", state->rowcount);
	LWDEBUGF(3, "shptype: %c\n", state->outtype);
	LWDEBUGF(3, "shpouttype: %d\n", state->outshptype);

	/* If we didn't find a geometry/geography column... */
	if (!state->geo_col_name)
	{
		if (state->config->geo_col_name)
		{
			/* A geo* column was specified, but not found */
			snprintf(state->message, SHPDUMPERMSGLEN, _("%s: no such attribute in table %s"), state->config->geo_col_name, state->table);

			return SHPDUMPERERR;
		}
		else
		{
			/* No geo* column specified so we can only create the DBF section -
			   but let's issue a warning... */
			snprintf(buf, 256, _("No geometry column found.\nThe DBF file will be created but not the shx or shp files.\n"));
			strncat(state->message, buf, SHPDUMPERMSGLEN - strlen(state->message));

			state->shp = NULL;
			
			ret = SHPDUMPERWARN;
		}
	}
	else
	{
		/* Since we have found a geo* column, open the shapefile */
		state->shp = SHPCreate(state->shp_file, state->outshptype);
		if (!state->shp)
		{
			snprintf(state->message, SHPDUMPERMSGLEN, _("Could not open shapefile %s!"), state->shp_file);

			return SHPDUMPERERR;
		}
	}
	

	/* Now we have the complete list of fieldnames, let's generate the SQL query. First let's make sure
	   we reserve enough space for tables with lots of columns */
	j = 0;
	for (i = 0; i < state->fieldcount; i++)
		j += strlen(state->pgfieldnames[i] + 2);	/* Add 2 for leading and trailing quotes */
	
	state->main_scan_query = malloc(1024 + j);
	
	sprintf(state->main_scan_query, "DECLARE cur ");
	if (state->config->binary)
		strcat(state->main_scan_query, "BINARY ");

	strcat(state->main_scan_query, "CURSOR FOR SELECT ");

	for (i = 0; i < state->fieldcount; i++)
	{
		/* Comma-separated column names */
		if (i > 0)
			strcat(state->main_scan_query, ",");
			
		if (state->config->binary)
			sprintf(buf, "\"%s\"::text", state->pgfieldnames[i]);
		else
			sprintf(buf, "\"%s\"", state->pgfieldnames[i]);

		strcat(state->main_scan_query, buf);
	}

	/* If we found a valid geometry/geography column then use it */
	if (state->geo_col_name)
	{
		/* If this is the (only) column, no need for the initial comma */
		if (state->fieldcount > 0)
			strcat(state->main_scan_query, ",");
		
		if (state->big_endian)
		{
			if (state->pgis_major_version > 0)
			{
1863
				sprintf(buf, "ST_asEWKB(ST_SetSRID(\"%s\"::geometry, 0), 'XDR') AS _geoX", state->geo_col_name);
1864 1865 1866
			}
			else
			{
1867
				sprintf(buf, "asbinary(\"%s\"::geometry, 'XDR') AS _geoX",
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
					state->geo_col_name);
			}
		}
		else /* little_endian */
		{
			if (state->pgis_major_version > 0)
			{
				sprintf(buf, "ST_AsEWKB(ST_SetSRID(\"%s\"::geometry, 0), 'NDR') AS _geoX", state->geo_col_name);
			}
			else
			{
				sprintf(buf, "asbinary(\"%s\"::geometry, 'NDR') AS _geoX",
					state->geo_col_name);
			}
		}

		strcat(state->main_scan_query, buf);
	}

	if (state->schema)
	{
		sprintf(buf, " FROM \"%s\".\"%s\"", state->schema, state->table);
	}
	else
	{
		sprintf(buf, " FROM \"%s\"", state->table);
	}

	strcat(state->main_scan_query, buf);

	/* Order by 'gid' (if found) */
	if (gidfound)
	{
		sprintf(buf, " ORDER BY \"gid\"");
		strcat(state->main_scan_query, buf);
	}

	/* Now we've finished with the result set, we can dispose of it */
	PQclear(res);

	LWDEBUGF(3, "FINAL QUERY: %s\n", state->main_scan_query);

	/*
	 * Begin the transaction
	 * (a cursor can only be defined inside a transaction block)
	 */
	res = PQexec(state->conn, "BEGIN");
	if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Error starting transaction: %s"), PQresultErrorMessage(res));
		PQclear(res);
		return SHPDUMPERERR;
	}

	PQclear(res);

	/* Execute the main scan query */
	res = PQexec(state->conn, state->main_scan_query);
	if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Error executing main scan query: %s"), PQresultErrorMessage(res));
		PQclear(res);
		return SHPDUMPERERR;
	}

	PQclear(res);

	/* Setup initial scan state */
	state->currow = 0;
	state->curresrow = 0;
	state->currescount = 0;
	state->fetchres = NULL;

	/* Generate the fetch query */
	state->fetch_query = malloc(256);
	sprintf(state->fetch_query, "FETCH %d FROM cur", state->config->fetchsize);

	return SHPDUMPEROK;
}


/* Append the next row to the output shapefile */
int ShpLoaderGenerateShapeRow(SHPDUMPERSTATE *state)
{
	char *hexewkb = NULL;
	unsigned char *hexewkb_binary = NULL;
	size_t hexewkb_len;
	char *val;
	SHPObject *obj = NULL;
	LWGEOM *lwgeom;

	int i, geocolnum = 0;

	/* If we try to go pass the end of the table, fail immediately */
	if (state->currow > state->rowcount)
	{
		snprintf(state->message, SHPDUMPERMSGLEN, _("Tried to read past end of table!"));
		PQclear(state->fetchres);
		return SHPDUMPERERR;
	}

	/* If we have reached the end of the current batch, fetch a new one */
	if (state->curresrow == state->currescount && state->currow < state->rowcount)
	{
		/* Clear the previous batch results */
		if (state->fetchres)
			PQclear(state->fetchres);

		state->fetchres = PQexec(state->conn, state->fetch_query);
		if (PQresultStatus(state->fetchres) != PGRES_TUPLES_OK)
		{
			snprintf(state->message, SHPDUMPERMSGLEN, _("Error executing fetch query: %s"), PQresultErrorMessage(state->fetchres));
			PQclear(state->fetchres);
			return SHPDUMPERERR;
		}

		state->curresrow = 0;
		state->currescount = PQntuples(state->fetchres);
	}

	/* Grab the id of the geo column if we have one */
	if (state->geo_col_name)
		geocolnum = PQfnumber(state->fetchres, "_geoX");

	/* Process the next record within the batch. First write out all of 
	the non-geo fields */
	for (i = 0; i < state->fieldcount; i++)
	{
		/*
		* Transform NULL numbers to '0'
		* This is because the shapelib
		* won't easly take care of setting
		* nulls unless paying the acquisition
		* of a bug in long integer values
		*/
		if (PQgetisnull(state->fetchres, state->curresrow, i))
		{
			val = nullDBFValue(state->dbffieldtypes[i]);
		}
		else
		{
			val = PQgetvalue(state->fetchres, state->curresrow, i);
			val = goodDBFValue(val, state->dbffieldtypes[i]);
		}

		/* Write it to the DBF file */
		if (!DBFWriteAttributeDirectly(state->dbf, state->currow, i, val))
		{
			snprintf(state->message, SHPDUMPERMSGLEN, _("Error: record %d could not be created"), state->currow);
			PQclear(state->fetchres);
			return SHPDUMPERERR;
		}
	}

	/* Now process the geo field, if present */
	if (state->geo_col_name)
	{
		/* Handle NULL shapes */
		if (PQgetisnull(state->fetchres, state->curresrow, geocolnum))
		{
			obj = SHPCreateSimpleObject(SHPT_NULL, 0, NULL, NULL, NULL);
			if (SHPWriteObject(state->shp, -1, obj) == -1)
			{
				snprintf(state->message, SHPDUMPERMSGLEN, _("Error writing NULL shape for record %d"), state->currow);
				PQclear(state->fetchres);
				SHPDestroyObject(obj);
				return SHPDUMPERERR;
			}
			SHPDestroyObject(obj);
		}
		else
		{
			/* Get the value from the result set */
			val = PQgetvalue(state->fetchres, state->curresrow, geocolnum);

			if (!state->config->binary)
			{
				if (state->pgis_major_version > 0)
				{
					LWDEBUG(4, "PostGIS >= 1.0, non-binary cursor");

					/* Input is bytea encoded text field, so it must be unescaped and
					then converted to hexewkb string */
					hexewkb_binary = PQunescapeBytea((unsigned char *)val, &hexewkb_len);
					hexewkb = convert_bytes_to_hex(hexewkb_binary, hexewkb_len);
				}
				else
				{
					LWDEBUG(4, "PostGIS < 1.0, non-binary cursor");

					/* Input is already hexewkb string, so we can just
					copy directly to hexewkb */
					hexewkb_len = PQgetlength(state->fetchres, state->curresrow, geocolnum);
					hexewkb = malloc(hexewkb_len + 1);
					strncpy(hexewkb, val, hexewkb_len + 1);
				}
			}
			else /* binary */
			{
				LWDEBUG(4, "PostGIS (any version) using binary cursor");

				/* Input is binary field - must convert to hexewkb string */
				hexewkb_len = PQgetlength(state->fetchres, state->curresrow, geocolnum);
				hexewkb = convert_bytes_to_hex((unsigned char *)val, hexewkb_len);
			}

			LWDEBUGF(4, "HexEWKB - length: %d  value: %s", strlen(hexewkb), hexewkb);

			/* Deserialize the LWGEOM */
			lwgeom = lwgeom_from_hexwkb(hexewkb, LW_PARSER_CHECK_NONE);
			if (!lwgeom)
			{
				snprintf(state->message, SHPDUMPERMSGLEN, _("Error parsing HEXEWKB for record %d"), state->currow);
				PQclear(state->fetchres);
				return SHPDUMPERERR;
			}
	
			/* Call the relevant method depending upon the geometry type */
			LWDEBUGF(4, "geomtype: %s\n", lwtype_name(lwgeom->type));
	
			switch (lwgeom->type)
			{
			case POINTTYPE:
				obj = create_point(state, lwgeom_as_lwpoint(lwgeom));
				break;
	
			case MULTIPOINTTYPE:
				obj = create_multipoint(state, lwgeom_as_lwmpoint(lwgeom));
				break;
	
			case POLYGONTYPE:
				obj = create_polygon(state, lwgeom_as_lwpoly(lwgeom));
				break;
	
			case MULTIPOLYGONTYPE:
				obj = create_multipolygon(state, lwgeom_as_lwmpoly(lwgeom));
				break;
	
			case LINETYPE:
				obj = create_linestring(state, lwgeom_as_lwline(lwgeom));
				break;
	
			case MULTILINETYPE:
				obj = create_multilinestring(state, lwgeom_as_lwmline(lwgeom));
				break;
	
			default:
				snprintf(state->message, SHPDUMPERMSGLEN, _("Unknown WKB type (%d) for record %d"), lwgeom->type, state->currow);
				PQclear(state->fetchres);
				SHPDestroyObject(obj);
				return SHPDUMPERERR;
			}
	
			/* Free both the original and geometries */
			lwgeom_free(lwgeom);

			/* Write the shape out to the file */
			if (SHPWriteObject(state->shp, -1, obj) == -1)
			{
				snprintf(state->message, SHPDUMPERMSGLEN, _("Error writing shape %d"), state->currow);
				PQclear(state->fetchres);
				SHPDestroyObject(obj);
				return SHPDUMPERERR;
			}

			SHPDestroyObject(obj);

			/* Free the hexewkb (and temporary bytea unescaped string if used) */
			if (hexewkb) free(hexewkb);
			if (hexewkb_binary) PQfreemem(hexewkb_binary);
		}
	}

	/* Increment ready for next time */
	state->curresrow++;
	state->currow++;

	return SHPDUMPEROK;
}


/* Return a count of the number of rows in the table being dumped */
int
ShpDumperGetRecordCount(SHPDUMPERSTATE *state)
{
	return state->rowcount;
}


/* Close the specified table and flush all files to disk */
int
ShpDumperCloseTable(SHPDUMPERSTATE *state)
{
	int ret = SHPDUMPEROK;

	/* Clear the current batch fetch resource */
	PQclear(state->fetchres);

	/* If a geo column is present, generate the projection file */
	if (state->geo_col_name)
		ret = projFileCreate(state);	

	/* Close the DBF and SHP files */
	if (state->dbf)
		DBFClose(state->dbf);
	if (state->shp)
		SHPClose(state->shp);

	return ret;
}


void
ShpDumperDestroy(SHPDUMPERSTATE *state)
{
	/* Destroy a state object created with ShpDumperConnect */
	int i;

	if (state != NULL)
	{
		/* Disconnect from the database */
		if (state->conn)
			PQfinish(state->conn);

		/* Free the query strings */
		if (state->fetch_query)
			free(state->fetch_query);
		if (state->main_scan_query)
			free(state->main_scan_query);

		/* Free the DBF information fields */
		if (state->dbffieldnames)
		{
			for (i = 0; i < state->fieldcount; i++)
				free(state->dbffieldnames[i]);
			free(state->dbffieldnames);
		}
		
		if (state->dbffieldtypes)
			free(state->dbffieldtypes);
		
		if (state->pgfieldnames)
			free(state->pgfieldnames);

		/* Free any column map fieldnames if specified */
2213
		colmap_clean(&state->column_map);
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
		
		/* Free other names */
		if (state->table)
			free(state->table);
		if (state->schema)
			free(state->schema);
		if (state->geo_col_name)
			free(state->geo_col_name);

		/* Free the state itself */
		free(state);
	}
}