[postgis-devel] shp2pgsql patch

strk at refractions.net strk at refractions.net
Fri Apr 1 08:05:50 PST 2005


Markus, I would move the whole block you're wrapping
into a separate function (loadData?). 
Also I noted a spurious line in current (original) code at
line 546. Can you confirm we can drop that line ?
(I might be affected by MCA spring hillness ;)

--strk;

On Fri, Apr 01, 2005 at 06:02:31PM +0200, Markus Schaber wrote:
> Hi,
> 
> Here's the first bugfix: Drop the sequence number increasing.
> 
> Markus
> -- 
> Markus Schaber - http://schabi.de/
> 
> But hey!, Standard compliance is something we should strive for, so
> let's try and change the Standard :-) [Alexandre Oliva, GCC Developer]

> Index: README.shp2pgsql
> ===================================================================
> RCS file: /home/cvs/postgis/postgis/loader/README.shp2pgsql,v
> retrieving revision 1.3
> diff -u -r1.3 README.shp2pgsql
> --- README.shp2pgsql	4 May 2002 22:44:04 -0000	1.3
> +++ README.shp2pgsql	1 Apr 2005 16:02:47 -0000
> @@ -43,14 +43,18 @@
>    -a    Append mode. Do not delete the target table or try to create
>          a new table, simple insert the data into the existing table.
>          A table will have to exist for this to work, it is usually
> -        used after a create mode as been run once.(mutually exclusive
> -	with -c and -d)
> +        used after a create mode as been run once or after -p. (mutually
> +        exclusive with -c, -d and -p)
>    -c    Create mode. This is the default mode is no other is specified.
>  	Create a new table and upload the data into that table.
> -	(mutually exclusive with -a and -d)
> +	(mutually exclusive with -a, -d and -p)
>    -d    Delete mode. Delete the database table named <tablename>, then
>  	create a new one with that name before uploading the data into
> -	the new empty database table.(mutually exclusive with -a and -c)
> +	the new empty database table. (mutually exclusive with -a, -c 
> +        and -p)
> +  -p    Prepare mode. Read the table schema from the shape file and 
> +        create the new table, but do not insert any data. (mutually
> +        exclusive with -a, -c and -d)
>  
>    -D Dump. When inserting the data into the table use 'dump' format.
>  	Dump format is used by PostgreSQL for large data dumps and 
> Index: shp2pgsql.c
> ===================================================================
> RCS file: /home/cvs/postgis/postgis/loader/shp2pgsql.c,v
> retrieving revision 1.83
> diff -u -r1.83 shp2pgsql.c
> --- shp2pgsql.c	15 Mar 2005 12:24:40 -0000	1.83
> +++ shp2pgsql.c	1 Apr 2005 16:02:48 -0000
> @@ -528,9 +528,9 @@
>  	printf("BEGIN;\n");
>  
>  	//if opt is 'a' do nothing, go straight to making inserts
> -	if(opt == 'c' || opt == 'd') create_table();
> +	if(opt == 'c' || opt == 'd' || opt == 'p') create_table();
>  
> -	if (dump_format){
> +	if (dump_format && opt != 'p'){
>  		if ( schema )
>  		{
>  			printf("COPY \"%s\".\"%s\" %s FROM stdin;\n",
> @@ -546,113 +546,116 @@
>  	obj = SHPReadObject(hSHPHandle,0);
>  	
>  
> -/**************************************************************
> - * 
> - *   MAIN SHAPE OBJECTS SCAN
> - * 
> - **************************************************************/
> -	for (j=0;j<num_entities; j++)
> -	{
> -		//wrap a transaction block around each 250 inserts...
> -		if ( ! dump_format )
> -		{
> -			if (trans == 250) {
> -				trans=0;
> -				printf("END;\n");
> -				printf("BEGIN;\n");
> -			}
> -		}
> -		trans++;
> -		// transaction stuff done
> -
> -		//open the next object
> -		obj = SHPReadObject(hSHPHandle,j);
> -
> -		if (!dump_format)
> -		{
> -			if ( schema )
> -			{
> -				printf("INSERT INTO \"%s\".\"%s\" %s VALUES (",
> -                                        schema, table, col_names);
> -			}
> -			else
> -			{
> -		  		printf("INSERT INTO \"%s\" %s VALUES (",
> -					table, col_names);
> -			}
> -			if(opt != 'a')
> -			{
> -				printf("'%d',", j);
> -			}
> -		}
> -		else
> -		{
> -			if(opt != 'a')
> -			{
> -				printf("%d\t", j);
> -			}
> -		}
> -		Insert_attributes(hDBFHandle,j);
> -
> -		// ---------- NULL SHAPE -----------------
> -		if (obj->nVertices == 0)
> -		{
> -			if (dump_format) printf("\\N\n");
> -			else printf("NULL);\n");
> -			SHPDestroyObject(obj);	
> -			continue;
> -		}
> -
> -		switch (obj->nSHPType)
> -		{
> -			case SHPT_POLYGON:
> -			case SHPT_POLYGONM:
> -			case SHPT_POLYGONZ:
> -				InsertPolygon();
> -				break;
> -
> -			case SHPT_POINT:
> -			case SHPT_POINTM:
> -			case SHPT_POINTZ:
> -				InsertPoint();
> -				break;
> -
> -			case SHPT_MULTIPOINT:
> -			case SHPT_MULTIPOINTM:
> -			case SHPT_MULTIPOINTZ:
> -				InsertMultiPoint();
> -				break;
> -
> -			case SHPT_ARC:
> -			case SHPT_ARCM:
> -			case SHPT_ARCZ:
> -				InsertLineString(j);
> -				break;
> -
> -			default:
> -				printf ("\n\n**** Type is NOT SUPPORTED, type id = %d ****\n\n",
> -					obj->nSHPType);
> -				break;
> -
> -		}
> -		
> -		SHPDestroyObject(obj);	
> -
> -	} // END of MAIN SHAPE OBJECT LOOP
> -
> -
> -	if ((dump_format) ) {
> -		printf("\\.\n");
> -
> -	} 
> -
> -	free(col_names);
> +        if (opt != 'p') { /*only if we do not have prepare mode*/
> +        /**************************************************************
> +         * 
> +         *   MAIN SHAPE OBJECTS SCAN
> +         * 
> +         **************************************************************/
> +	        for (j=0;j<num_entities; j++)
> +	        {
> +		        //wrap a transaction block around each 250 inserts...
> +		        if ( ! dump_format )
> +		        {
> +			        if (trans == 250) {
> +				        trans=0;
> +				        printf("END;\n");
> +				        printf("BEGIN;\n");
> +			        }
> +		        }
> +		        trans++;
> +		        // transaction stuff done
> +
> +		        //open the next object
> +		        obj = SHPReadObject(hSHPHandle,j);
> +
> +		        if (!dump_format)
> +		        {
> +			        if ( schema )
> +			        {
> +				        printf("INSERT INTO \"%s\".\"%s\" %s VALUES (",
> +                                                schema, table, col_names);
> +			        }
> +			        else
> +			        {
> +		  		        printf("INSERT INTO \"%s\" %s VALUES (",
> +					        table, col_names);
> +			        }
> +			        if(opt != 'a')
> +			        {
> +				        printf("'%d',", j);
> +			        }
> +		        }
> +		        else
> +		        {
> +			        if(opt != 'a')
> +			        {
> +				        printf("%d\t", j);
> +			        }
> +		        }
> +		        Insert_attributes(hDBFHandle,j);
> +
> +		        // ---------- NULL SHAPE -----------------
> +		        if (obj->nVertices == 0)
> +		        {
> +			        if (dump_format) printf("\\N\n");
> +			        else printf("NULL);\n");
> +			        SHPDestroyObject(obj);	
> +			        continue;
> +		        }
> +
> +		        switch (obj->nSHPType)
> +		        {
> +			        case SHPT_POLYGON:
> +			        case SHPT_POLYGONM:
> +			        case SHPT_POLYGONZ:
> +				        InsertPolygon();
> +				        break;
> +
> +			        case SHPT_POINT:
> +			        case SHPT_POINTM:
> +			        case SHPT_POINTZ:
> +				        InsertPoint();
> +				        break;
> +
> +			        case SHPT_MULTIPOINT:
> +			        case SHPT_MULTIPOINTM:
> +			        case SHPT_MULTIPOINTZ:
> +				        InsertMultiPoint();
> +				        break;
> +
> +			        case SHPT_ARC:
> +			        case SHPT_ARCM:
> +			        case SHPT_ARCZ:
> +				        InsertLineString(j);
> +				        break;
> +
> +			        default:
> +				        printf ("\n\n**** Type is NOT SUPPORTED, type id = %d ****\n\n",
> +					        obj->nSHPType);
> +				        break;
> +
> +		        }
> +
> +		        SHPDestroyObject(obj);	
> +
> +	        } // END of MAIN SHAPE OBJECT LOOP
> +
> +
> +	        if ((dump_format) ) {
> +		        printf("\\.\n");
> +
> +	        } 
> +
> +        } /* end of if (opt != 'p') */
> +        
> +        free(col_names);
>  	if(opt != 'a')
>  	{
>  		if ( schema )
>  		{
>  			printf("\nALTER TABLE ONLY \"%s\".\"%s\" ADD CONSTRAINT \"%s_pkey\" PRIMARY KEY (gid);\n",schema,table,table);
> -			if(j > 1)
> +			if(j > 1 && opt != 'p')
>  			{
>  				printf("SELECT setval ('\"%s\".\"%s_gid_seq\"', %i, true);\n", schema, table, j-1);
>  			}
> @@ -660,7 +663,7 @@
>  		else
>  		{
>  			printf("\nALTER TABLE ONLY \"%s\" ADD CONSTRAINT \"%s_pkey\" PRIMARY KEY (gid);\n",table,table);
> -			if(j > 1){
> +			if(j > 1 && opt != 'p'){
>  				printf("SELECT setval ('\"%s_gid_seq\"', %i, true);\n", table, j-1);
>  			}
>  		}
> @@ -775,13 +778,14 @@
>  	fprintf(stderr, "OPTIONS:\n");
>  	fprintf(stderr, "  -s <srid>  Set the SRID field. If not specified it defaults to -1.\n");
>  	fprintf(stderr, "\n");
> -	fprintf(stderr, "  (-d|a|c) These are mutually exclusive options:\n");
> -	fprintf(stderr, "      -d  Drops the table , then recreates it and populates\n");
> +	fprintf(stderr, "  (-d|a|c|p) These are mutually exclusive options:\n");
> +	fprintf(stderr, "      -d  Drops the table, then recreates it and populates\n");
>  	fprintf(stderr, "          it with current shape file data.\n");
>  	fprintf(stderr, "      -a  Appends shape file into current table, must be\n");
>  	fprintf(stderr, "          exactly the same table schema.\n");
>  	fprintf(stderr, "      -c  Creates a new table and populates it, this is the\n");
>  	fprintf(stderr, "          default if you do not specify any options.\n");
> +	fprintf(stderr, "      -p  Only creates the table.");
>  	fprintf(stderr, "\n");
>  	fprintf(stderr, "  -g <geometry_column> Specify the name of the geometry column\n");
>  	fprintf(stderr, "     (mostly useful in append mode).\n");
> @@ -1104,7 +1108,7 @@
>  	int curindex=0;
>  	char  *ptr;
>  
> -	while ((c = getopt(ARGC, ARGV, "kcdaDs:g:iW:")) != EOF){
> +	while ((c = getopt(ARGC, ARGV, "kcdapDs:g:iW:")) != EOF){
>                 switch (c) {
>                 case 'c':
>                      if (opt == ' ')
> @@ -1124,6 +1128,12 @@
>                      else
>                           return 0;
>                      break;
> +	       case 'p':
> +                    if (opt == ' ')
> +                         opt ='p';
> +                    else
> +                         return 0;
> +                    break;
>  	       case 'D':
>  		    dump_format =1;
>                      break;

> _______________________________________________
> postgis-devel mailing list
> postgis-devel at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-devel




More information about the postgis-devel mailing list