[postgis-users] [PATCH] shp2pgsql and ''::numeric

strk strk at keybit.net
Mon Sep 29 05:13:05 PDT 2003


I fixed some bugs in shp2pgsql.c:

	- "\t" always preceeded the first value of a dump_format query 
	  if NULL

	- field values where quoted with (") in dump_format when
	  called with -k ( did I introduce that? )
	  
	- Appropriate calls to DBF[..]ReadAttributes based on
	  cached attribute types.

	- Assured that *all* shapes are NULL before exiting with
	  an error ( I did not check that NULL shapes in the midle
	  of the shapefiles are handled, but previous code did
	  not check that either ... )

As a side effect - trying to make it more readable - I reduced some
lines of code and changed the format a bit. I'm sorry if this causes
an harder merging process...

pramsey wrote:
> It is also worth pointing out that the int/bigint determination code 
> seems to be deciding on bigint an awful lot. bigint shouldn't be used 
> unless the field definition is number(9,0) or higher.
> 
> strk wrote:
> > shp2pgsql (as of latest cvs version) 
> > generates empty strings as the values for numeric fields.
> > 
> > If you could use DBFReadAttribute instead of DBFReadStringAttribute
> > you might get a better control over values.
> > 
> > What I think is that the field value is the number 0, so
> > DBFIsAttributeNULL returns false while DBFReadStringAttribute
> > returns '' ( the empty string! ). 
> > 
> > Can you please check this (it requires duplicating the type
> > detecting code used at table creation time ( or maybe cache
> > that info).
> > 
> > NOTE that the problem emerge only for numerics valued 0.
> > TIA
> > --strk;
> > 
> > _______________________________________________
> > postgis-users mailing list
> > postgis-users at postgis.refractions.net
> > http://postgis.refractions.net/mailman/listinfo/postgis-users
> 
> -- 
>        __
>       /
>       | Paul Ramsey
>       | Refractions Research
>       | Email: pramsey at refractions.net
>       | Phone: (250) 885-0632
>       \_
> 
> 
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
-------------- next part --------------
--- shp2pgsql.c.000	Sat Sep 27 03:04:29 2003
+++ shp2pgsql.c	Mon Sep 29 13:58:53 2003
@@ -89,4 +89,5 @@
 char    opt;
 char    *col_names;
+DBFFieldType *types;
 
 int Insert_attributes(DBFHandle hDBFHandle, int row);
@@ -465,43 +466,58 @@
 //Insert the attributes from the correct row of dbf file
 
-int Insert_attributes(DBFHandle hDBFHandle, int row){
-	int i,num_fields;
-
-	num_fields = DBFGetFieldCount( hDBFHandle );
-		
-	
-	for( i = 0; i < num_fields; i++ ){
-	         if(DBFIsAttributeNULL( hDBFHandle, row, i)){
-		        if(dump_format){
-		               printf("\t\\N");
-		        }else{
-				if(i == 0){
-			               printf("NULL");
-				}else{
-			               printf(",NULL");
-				}
-		        }
-		 }else{
-			if (dump_format){
-
-				if ( quoteidentifiers ){
-					if(i == 0){
-						printf("\"%s\"",make_good_string((char*)DBFReadStringAttribute( hDBFHandle,row, i )) );
-					}else{
-						printf("\t\"%s\"",make_good_string((char*)DBFReadStringAttribute( hDBFHandle,row, i )) );
-					}
-				}else{
-					if(i == 0){
-						printf("%s",make_good_string((char*)DBFReadStringAttribute( hDBFHandle,row, i )) );
-					}else{
-						printf("\t%s",make_good_string((char*)DBFReadStringAttribute( hDBFHandle,row, i )) );
-					}
-				}
-			}else{
-				if(i == 0){
-					printf("'%s'",protect_quotes_string((char*)DBFReadStringAttribute(hDBFHandle, row, i )) );
-				}else{
-					printf(",'%s'",protect_quotes_string((char*)DBFReadStringAttribute(hDBFHandle, row, i )) );
-				}
+int
+Insert_attributes(DBFHandle hDBFHandle, int row)
+{
+   int i,num_fields;
+   char val[1024];
+
+   num_fields = DBFGetFieldCount( hDBFHandle );
+   for( i = 0; i < num_fields; i++ )
+   {
+      if(DBFIsAttributeNULL( hDBFHandle, row, i))
+      {
+         if(dump_format)
+         {
+				if(i) printf("\t");
+            printf("\\N");
+         }
+         else
+         {
+				if(i) printf(",");
+			   printf("NULL");
+		   }
+      }
+
+      else /* Attribute NOT NULL */
+      {
+         switch (types[i]) 
+         {
+            case FTString:
+               if ( -1 == snprintf(val, 1024, "%s",
+                     DBFReadStringAttribute(hDBFHandle, row, i)) )
+               {
+                  fprintf(stderr, "Warning: field %d name trucated\n", i);
+                  val[1023] = '\0';
+               }
+               break;
+            case FTInteger:
+               sprintf(val, "%d", DBFReadIntegerAttribute(hDBFHandle, row, i));
+               break;
+            case FTDouble:
+               sprintf(val, "%f", DBFReadDoubleAttribute(hDBFHandle, row, i));
+               break;
+            default:
+               fprintf(stderr,
+                  "Error: field %d has invalid or unknown field type (%d)\n",
+                  i, types[i]);
+               exit(1);
+         }
+ 
+			if (dump_format) {
+            if ( i ) printf("\t");
+				printf("%s",make_good_string(val));
+			} else {
+            if ( i ) printf(",");
+				printf("'%s'",protect_quotes_string(val));
 			}
 		 }
@@ -699,7 +715,9 @@
 		num_records = DBFGetRecordCount(hDBFHandle);
 		names = malloc((num_fields + 1)*sizeof(char*));
+      types = (DBFFieldType *)malloc((num_fields + 1)*sizeof(char*));
 		for(j=0;j<num_fields;j++){
 			type = DBFGetFieldInfo(hDBFHandle, j, name, &field_width, &field_precision); 
 			names[j] = malloc ( strlen(name)+1);
+			types[j] = type;
 			strcpy(names[j], name);
 			for(z=0; z < j ; z++){
@@ -756,10 +774,13 @@
 
 	SHPGetInfo( hSHPHandle, &num_entities, &phnshapetype, &padminbound[0], &padmaxbound[0]);
-	if(obj == NULL){
-		obj = 	SHPReadObject(hSHPHandle,0);
-		if(obj == NULL){
-			printf("file exists but contains null shapes");
-			exit(-1);
-		}
+   j=num_entities;
+   while(obj == NULL && j--)
+   {
+		obj = SHPReadObject(hSHPHandle,j);
+   }
+   if ( obj == NULL) 
+   {
+      fprintf(stderr, "Shapefile contains %d NULL object(s)\n", num_entities);
+		exit(-1);
 	}
 


More information about the postgis-users mailing list