]> granicus.if.org Git - postgis/commitdiff
Fix for DBF files with deleted records. (#29)
authorPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 27 May 2008 03:00:12 +0000 (03:00 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 27 May 2008 03:00:12 +0000 (03:00 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@2783 b70326c6-7e19-0410-871a-916f4a2858ee

ChangeLog
loader/dbfopen.c
loader/shapefil.h
loader/shp2pgsql.c

index f3aa34a87a3b1107266d401eba471c3f58b89d30..a7680a5051c4a40ab6748a63aabdc73976357a62 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2008-06-26 Paul Ramsey <pramsey@cleverelephant.ca>
+
+        * loader/shapefil.h, dbfopen.c, shp2pgsql.c
+          Fix for DBF files with deleted records. (#29)
 
 2008-04-12 Paul Ramsey <pramsey@cleverelephant.ca>
           
index 6e8a1755efbd47358620c77899d1668dc61936b0..337f2eb6ef21fea7079edd9c1077715851783e98 100644 (file)
@@ -718,30 +718,22 @@ DBFAddField(DBFHandle psDBF, const char * pszFieldName,
     return( psDBF->nFields-1 );
 }
 
+
 /************************************************************************/
-/*                          DBFReadAttribute()                          */
+/*                        DBFReadSetup()                                */
 /*                                                                      */
-/*      Read one of the attribute fields of a record.                   */
+/*      Prep a record for reading.                                      */
 /************************************************************************/
 
-static void *DBFReadAttribute(DBFHandle psDBF, int hEntity, int iField,
-                              char chReqType )
-
+int DBFReadSetup(DBFHandle psDBF, int hEntity) 
 {
     int                nRecordOffset;
-    unsigned char      *pabyRec;
-    void       *pReturnField = NULL;
-
-    static double dDoubleField;
 
 /* -------------------------------------------------------------------- */
 /*      Verify selection.                                               */
 /* -------------------------------------------------------------------- */
     if( hEntity < 0 || hEntity >= psDBF->nRecords )
-        return( NULL );
-
-    if( iField < 0 || iField >= psDBF->nFields )
-        return( NULL );
+        return( 0 );
 
 /* -------------------------------------------------------------------- */
 /*     Have we read the record?                                        */
@@ -756,7 +748,7 @@ static void *DBFReadAttribute(DBFHandle psDBF, int hEntity, int iField,
         {
             fprintf( stderr, "fseek(%d) failed on DBF file.\n",
                      nRecordOffset );
-            return NULL;
+            return 0;
         }
 
        if( fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 
@@ -764,25 +756,77 @@ static void *DBFReadAttribute(DBFHandle psDBF, int hEntity, int iField,
         {
             fprintf( stderr, "fread(%d) failed on DBF file.\n",
                      psDBF->nRecordLength );
-            return NULL;
+            return 0;
         }
 
        psDBF->nCurrentRecord = hEntity;
     }
+    
+    return 1;
+
+}
+
+
+/************************************************************************/
+/*                        DBFReadDeleted()                              */
+/*                                                                      */
+/*      Read whether a record is deleted.                               */
+/************************************************************************/
+
+int DBFReadDeleted(DBFHandle psDBF, int hEntity)
+{
+  unsigned char        *pabyRec;
+  unsigned char *pDeleted;
+  
+  if( ! DBFReadSetup( psDBF, hEntity) )
+    return NULL;
+
+  /* get reference to current record */
+  pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
+
+  /* 0x20 => not deleted, 0x24 => deleted */
+  return *pabyRec == 0x20 ? 1 : 0;
 
+}
+
+/************************************************************************/
+/*                          DBFReadAttribute()                          */
+/*                                                                      */
+/*      Read one of the attribute fields of a record.                   */
+/************************************************************************/
+
+static void *DBFReadAttribute(DBFHandle psDBF, int hEntity, int iField,
+                              char chReqType )
+
+{
+    unsigned char      *pabyRec;
+    void       *pReturnField = NULL;
+
+    static double dDoubleField;
+
+/* -------------------------------------------------------------------- */
+/*      Verify selection.                                               */
+/* -------------------------------------------------------------------- */
+    if( iField < 0 || iField >= psDBF->nFields )
+        return( NULL );
+
+    if( ! DBFReadSetup( psDBF, hEntity) )
+      return( NULL );
+
+    /* get reference to current record */
     pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
 
 /* -------------------------------------------------------------------- */
-/*     Ensure our field buffer is large enough to hold this buffer.    */
+/*     Ensure our field buffer is large enough to hold this buffer.          */
 /* -------------------------------------------------------------------- */
     if( psDBF->panFieldSize[iField]+1 > nStringFieldLen )
     {
-       nStringFieldLen = psDBF->panFieldSize[iField]*2 + 10;
-       pszStringField = (char *) SfRealloc(pszStringField,nStringFieldLen);
+           nStringFieldLen = psDBF->panFieldSize[iField]*2 + 10;
+           pszStringField = (char *) SfRealloc(pszStringField,nStringFieldLen);
     }
 
 /* -------------------------------------------------------------------- */
-/*     Extract the requested field.                                    */
+/*     Extract the requested field.                                                            */
 /* -------------------------------------------------------------------- */
     strncpy( pszStringField, 
             ((const char *) pabyRec) + psDBF->panFieldOffset[iField],
@@ -796,9 +840,8 @@ static void *DBFReadAttribute(DBFHandle psDBF, int hEntity, int iField,
 /* -------------------------------------------------------------------- */
     if( chReqType == 'N' )
     {
-        dDoubleField = atof(pszStringField);
-
-       pReturnField = &dDoubleField;
+      dDoubleField = atof(pszStringField);
+      pReturnField = &dDoubleField;
     }
 
 /* -------------------------------------------------------------------- */
index e8fd3be97b920ef44fca1918936044904124dcd2..34908b177fc9fbf69abcee7ce017cc3f4e594bfa 100644 (file)
@@ -456,6 +456,8 @@ const char SHPAPI_CALL1(*)
 int     SHPAPI_CALL
       DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
 
+int SHPAPI_CALL DBFReadDeleted(DBFHandle psDBF, int hEntity);
+
 int SHPAPI_CALL
       DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField, 
                                 int nFieldValue );
index 60ca24726bf5f8b3199987d51c8623be03ee85bd..847aeb72ad9e96bc3e3b830ecb2251cbaa5afc92 100644 (file)
@@ -673,6 +673,12 @@ LoadData(void)
                trans++;
                /* transaction stuff done */
 
+    /*skip the record if it has been deleted*/
+    if(readshape != 1 && DBFReadDeleted(hDBFHandle, j)) {
+      continue; 
+    }
+
+
                /*open the next object */
                if (readshape == 1)
                {