[postgis-users] importing shape files nightmare!

strk at refractions.net strk at refractions.net
Thu Sep 15 03:36:35 PDT 2005


Attached is a patch to bring shp2pgsql.c from Revision 1.91 
to Revision 1.96 (the one shipped with PostGIS 1.0.4).

If you don't have the sources that's ... well ... sad :(
You can still download latest release sources (1.0.4),
run make in the loader/ directory and replace your dumper/loader
with the newly created ones.

.. you can read by the end of the patch all changes
in shp2pgsql since 1.91 ...

--strk;

On Thu, Sep 15, 2005 at 12:28:25PM +0200, christomec wrote:
> I get:
> 
> RCSID: $Id: shp2pgsql.c,v 1.91 2005/07/04 09:47:03 strk Exp $
> 
> Is there a way of just fixing the shp2pgsql without having to re-emerging
> postgis?
> 
> > On Thu, Sep 15, 2005 at 11:58:29AM +0200, strk at refractions.net wrote:
> >> On Thu, Sep 15, 2005 at 11:52:08AM +0200, christomec wrote:
> >> > So is POSTGIS="1.1.0CVS" is in actual fact 1.0.3?
> >>
> >> Might be, the 1.1 and 1.0 are two different branches in CVS,
> >> and 1.1.0CVS is there since about 1.0.0 is out... there's no
> >> easy way to tell which version of the code 1.1.0CVS is.
> >>
> >> Since this is a bug with pgsql2shp what you should look at
> >> is RCSID from it. It is shown running pgsql2shp w/out arguments.
> >
> > Oops.. sorry, it's shp2pgsql (the loader), NOT pgsql2shp (the dumper).
> > The RCSID is still shown when called w/out args
> >
> > --strk;
> > _______________________________________________
> > postgis-users mailing list
> > postgis-users at postgis.refractions.net
> > http://postgis.refractions.net/mailman/listinfo/postgis-users
> >
> > !DSPAM:432946b7323981650317341!
> >
> >
> >
> 
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
-------------- next part --------------
Index: shp2pgsql.c
===================================================================
RCS file: /home/cvs/postgis/postgis/loader/shp2pgsql.c,v
retrieving revision 1.91
retrieving revision 1.96
diff -U2 -r1.91 -r1.96
--- shp2pgsql.c	4 Jul 2005 09:47:03 -0000	1.91
+++ shp2pgsql.c	29 Aug 2005 22:36:25 -0000	1.96
@@ -1,4 +1,4 @@
 /**********************************************************************
- * $Id: shp2pgsql.c,v 1.91 2005/07/04 09:47:03 strk Exp $
+ * $Id: shp2pgsql.c,v 1.96 2005/08/29 22:36:25 strk Exp $
  *
  * PostGIS - Spatial Types for PostgreSQL
@@ -131,5 +131,5 @@
 
 static char rcsid[] =
-  "$Id: shp2pgsql.c,v 1.91 2005/07/04 09:47:03 strk Exp $";
+  "$Id: shp2pgsql.c,v 1.96 2005/08/29 22:36:25 strk Exp $";
 
 void *safe_malloc(size_t size)
@@ -436,4 +436,5 @@
 {
 	int j;
+	SHPObject *obj=NULL;
 
 	hSHPHandle = SHPOpen( shp_file, "rb" );
@@ -451,7 +452,15 @@
 
 	/* Check we have at least a not-null geometry */
-	j=num_entities;
-	while( ( obj == NULL || obj->nVertices == 0 ) && j--)
+	for (j=0; j<num_entities; j++)
+	{
 		obj = SHPReadObject(hSHPHandle,j);
+		if ( obj && obj->nVertices > 0 ) {
+			SHPDestroyObject(obj);
+			break;
+		}
+		SHPDestroyObject(obj);
+		obj=NULL;
+	}
+
 	if ( obj == NULL) 
 	{
@@ -763,5 +772,4 @@
 		else printf("NULL);\n");
 
-		SHPDestroyObject(obj);
 		return;
 	}
@@ -818,5 +826,4 @@
 		else printf("NULL);\n");
 
-		SHPDestroyObject(obj);
 		return;
 	}
@@ -1157,6 +1164,6 @@
 {
 	unsigned int u;
-	if (dump_format) printf("SRID=%s;POINT(",sr_id);
-	else printf("GeometryFromText('POINT (");
+	if (dump_format) printf("SRID=%s;%s(", sr_id, pgtype);
+	else printf("GeometryFromText('%s(", pgtype);
 
 	for (u=0;u<obj->nVertices; u++){
@@ -1416,9 +1423,10 @@
 	char *buf = malloc(256*ring->n);
 	int i;
+
+	buf[0] = '\0';
 	for (i=0; i<ring->n; i++)
 	{
-		if (i) sprintf(buf, "%s,", buf);
-		sprintf(buf, "%s%g %g",
-			buf,
+		if (i) strcat(buf, ",");
+		sprintf(buf+strlen(buf), "%g %g",
 			ring->list[i].x,
 			ring->list[i].y);
@@ -1517,9 +1525,17 @@
 GetFieldsSpec(void)
 {
+/*
+ * Shapefile (dbf) field name are at most 10chars + 1 NULL.
+ * Postgresql field names are at most 63 bytes + 1 NULL.
+ */
+#define MAXFIELDNAMELEN 64
 	int field_precision, field_width;
 	int j, z;
-	char  name[64];
-	char  name2[64];
+	char  name[MAXFIELDNAMELEN];
+	char  name2[MAXFIELDNAMELEN];
 	DBFFieldType type = -1;
+#ifdef USE_ICONV
+	char *utf8str;
+#endif
 
 	num_fields = DBFGetFieldCount( hDBFHandle );
@@ -1529,5 +1545,5 @@
 	widths = malloc(num_fields*sizeof(int));
 	precisions = malloc(num_fields*sizeof(int));
-	col_names = malloc((num_fields+2) * sizeof(char) * 32);
+	col_names = malloc((num_fields+2) * sizeof(char) * MAXFIELDNAMELEN);
 	strcpy(col_names, "(" );
 
@@ -1542,30 +1558,27 @@
 		precisions[j] = field_precision;
 
+#ifdef USE_ICONV
+		if ( encoding )
+		{
+			utf8str = utf8(encoding, name);
+			if ( ! utf8str ) exit(1);
+			strcpy(name, utf8str);
+			free(utf8str);
+		}
+#endif
+
+
 		/*
 		 * Make field names lowercase unless asked to
 		 * keep identifiers case.
 		 */
-		if ( ! quoteidentifiers ) {
-			for(z=0; z<strlen(name) ;z++)
-				name[z] = tolower(name[z]);
-		}
+		if ( ! quoteidentifiers ) LowerCase(name);
 
 		/*
 		 * Escape names starting with the
-		 * escape char (_)
+		 * escape char (_), those named 'gid'
+		 * or after pgsql reserved attribute names
 		 */
-		if( name[0]=='_' )
-		{
-			strcpy(name2+2, name);
-			name2[0] = '_';
-			name2[1] = '_';
-			strcpy(name, name2);
-		}
-
-		/*
-		 * Escape attributes named 'gid'
-		 * and pgsql reserved attribute names
-		 */
-		else if(
+		if( name[0]=='_' ||
 			! strcmp(name,"gid") ||
 			! strcmp(name, "tableoid") ||
@@ -1575,6 +1588,6 @@
 			! strcmp(name, "primary") ||
 			! strcmp(name, "oid") ||
-			! strcmp(name, "ctid")
-		) {
+			! strcmp(name, "ctid") )
+		{
 			strcpy(name2+2, name);
 			name2[0] = '_';
@@ -1587,16 +1600,20 @@
 			if(strcmp(field_names[z],name)==0){
 				strcat(name,"__");
-				sprintf(name,"%s%i",name,j);
+				sprintf(name+strlen(name),"%i",j);
 				break;
 			}
 		}	
 
-
-		field_names[j] = malloc ( strlen(name)+3 );
+		field_names[j] = malloc (strlen(name)+1);
 		strcpy(field_names[j], name);
 
-		sprintf(col_names, "%s\"%s\",", col_names, name);
-	}
-	sprintf(col_names, "%s\"%s\")", col_names, geom);
+		/*sprintf(col_names, "%s\"%s\",", col_names, name);*/
+		strcat(col_names, "\"");
+		strcat(col_names, name);
+		strcat(col_names, "\",");
+	}
+	/*sprintf(col_names, "%s\"%s\")", col_names, geom);*/
+	strcat(col_names, geom);
+	strcat(col_names, ")");
 }
 
@@ -1647,4 +1664,20 @@
 /**********************************************************************
  * $Log: shp2pgsql.c,v $
+ * Revision 1.96  2005/08/29 22:36:25  strk
+ * Removed premature object destruction in InsertLineString{WKT,} causing segfault
+ *
+ * Revision 1.95  2005/08/29 11:48:33  strk
+ * Fixed sprintf() calls to avoid overlapping memory,
+ * reworked not-null objects existance check to reduce startup costs.
+ *
+ * Revision 1.94  2005/07/27 02:47:14  strk
+ * Support for multibyte field names in loader
+ *
+ * Revision 1.93  2005/07/27 02:35:50  strk
+ * Minor cleanups in loader
+ *
+ * Revision 1.92  2005/07/27 02:07:01  strk
+ * Fixed handling of POINT types as WKT (-w) in loader
+ *
  * Revision 1.91  2005/07/04 09:47:03  strk
  * Added conservative iconv detection code


More information about the postgis-users mailing list