dataPreprocessing/prepareShpFiles.c
int restructureFeatures(SHPHandle shpIn,SHPHandle shpOut,
			DBFHandle dbfIn,DBFHandle dbfOut)
{
  int bOk = 1;         /* for return */
  int inputCount = 0;  /* number of features in the input file */
  int shapeType = 0;   /* what kind of geographic feature? */
  int in = 0;      /* count features read */
  int out = 0;     /* count features written */
  int part = 0;    /* count parts within a feature */
  SHPObject * readShp = NULL;
  SHPObject * newShp = NULL;
  SHPGetInfo(shpIn, &inputCount,&shapeType,NULL,NULL);
  for (in = 0; (in < inputCount) && (bOk > 0); in++)
     {
     readShp = SHPReadObject(shpIn,in);
     if (bVerbose)
        printf("Read shape %d\n",in);
     if (readShp == NULL)
       {
       bOk = 0;
       break;
       }
     if (bVerbose)
       printf("Shape %d has %d part(s), %d vertices\n",in,readShp->nParts,
	      readShp->nVertices);
     if (readShp->nParts == 1) /* just copy */
       {
       newShp = SHPCreateSimpleObject(SHPT_ARC,readShp->nVertices,
				      readShp->padfX,readShp->padfY,NULL);
       if (newShp == NULL)
	 {
	 bOk = 0;
	 break;
	 }
       out = SHPWriteObject(shpOut,-1,newShp);
       bOk = copyAttributes(dbfIn,dbfOut,in,out);
       SHPDestroyObject(newShp);
       }
     else
       {
       for (part = 0; part < readShp->nParts; part++)
	   {
	   int p = 0;   /* loop counter for points */
	   double * pX = NULL;
	   double * pY = NULL;
	   int vCount = 0;  /* count of vertices in this part */
	   int start = readShp->panPartStart[part];
	   int end;
	   if (part == readShp->nParts - 1) /* last part */
	     end = readShp->nVertices - 1;
	   else
	     end = readShp->panPartStart[part+1] - 1;
	   vCount = end - start + 1; 
	   if (bVerbose)
	     printf("\tShape %d: part %d has %d vertices (%d to %d)\n",
		      in,part,vCount,start,end);
	   /* create coordinate arrays for this part */
	   pX = calloc(vCount,sizeof(double));
	   pY = calloc(vCount,sizeof(double));
	   if ((pX == NULL) || (pY == NULL))
	     {
	     bOk = 0;
	     break;
	     }
	   /* copy coordinates */
	   for (p = 0; p < vCount; p++)
	     {
	     pX[p] = readShp->padfX[start+p];
	     pY[p] = readShp->padfY[start+p];
	     }  
	   newShp = SHPCreateSimpleObject(SHPT_ARC,vCount,pX,pY,NULL);
	   if (newShp == NULL)
	     {
	     bOk = 0;
	     break;
	     }
	   out = SHPWriteObject(shpOut,-1,newShp);
	   bOk = copyAttributes(dbfIn,dbfOut,in,out);
	   SHPDestroyObject(newShp);
	   free(pX);
	   free(pY);
	   }
       }
     }  
  if (bVerbose)
    {
    printf("Input file had %d features - output file has %d features\n",
	   in,out);
    }
  return bOk;
}
/* Copy and transform data from the input coordinate and attribute 
 * files to the output. If a feature has a single part, just do a copy.
 * If a feature has multiple parts, break it into multiple single
 * part features.
 *    shpIn       File handle for input coordinate file
 *    shpOut      File handle for output coordinate file
 *    dbfIn       File handle for input DBF
 *    dbfOut      File handle for output DBF
 * Returns 1 for success, 0 for error.
 */
Copy and transform data from the input coordinate and attribute
files to the output. If a feature has a single part, just do a copy.
If a feature has multiple parts, break it into multiple single
part features.
shpIn       File handle for input coordinate file
shpOut      File handle for output coordinate file
dbfIn       File handle for input DBF
dbfOut      File handle for output DBF
Returns 1 for success, 0 for error.
Copy and transform data from the input coordinate and attribute files to the output . If a feature has a single part just do a copy . If a feature has multiple parts break it into multiple single part features . shpIn File handle for input coordinate file shpOut File handle for output coordinate file dbfIn File handle for input DBF dbfOut File handle for output DBF Returns 1 for success 0 for error .
int restructureFeatures(SHPHandle shpIn,SHPHandle shpOut,
			DBFHandle dbfIn,DBFHandle dbfOut)
{
  int bOk = 1;         
  int inputCount = 0;  
  int shapeType = 0;   
  int in = 0;      
  int out = 0;     
  int part = 0;    
  SHPObject * readShp = NULL;
  SHPObject * newShp = NULL;
  SHPGetInfo(shpIn, &inputCount,&shapeType,NULL,NULL);
  for (in = 0; (in < inputCount) && (bOk > 0); in++)
     {
     readShp = SHPReadObject(shpIn,in);
     if (bVerbose)
        printf("Read shape %d\n",in);
     if (readShp == NULL)
       {
       bOk = 0;
       break;
       }
     if (bVerbose)
       printf("Shape %d has %d part(s), %d vertices\n",in,readShp->nParts,
	      readShp->nVertices);
     if (readShp->nParts == 1) 
       {
       newShp = SHPCreateSimpleObject(SHPT_ARC,readShp->nVertices,
				      readShp->padfX,readShp->padfY,NULL);
       if (newShp == NULL)
	 {
	 bOk = 0;
	 break;
	 }
       out = SHPWriteObject(shpOut,-1,newShp);
       bOk = copyAttributes(dbfIn,dbfOut,in,out);
       SHPDestroyObject(newShp);
       }
     else
       {
       for (part = 0; part < readShp->nParts; part++)
	   {
	   int p = 0;   
	   double * pX = NULL;
	   double * pY = NULL;
	   int vCount = 0;  
	   int start = readShp->panPartStart[part];
	   int end;
	   if (part == readShp->nParts - 1) 
	     end = readShp->nVertices - 1;
	   else
	     end = readShp->panPartStart[part+1] - 1;
	   vCount = end - start + 1; 
	   if (bVerbose)
	     printf("\tShape %d: part %d has %d vertices (%d to %d)\n",
		      in,part,vCount,start,end);
	   pX = calloc(vCount,sizeof(double));
	   pY = calloc(vCount,sizeof(double));
	   if ((pX == NULL) || (pY == NULL))
	     {
	     bOk = 0;
	     break;
	     }
	   for (p = 0; p < vCount; p++)
	     {
	     pX[p] = readShp->padfX[start+p];
	     pY[p] = readShp->padfY[start+p];
	     }  
	   newShp = SHPCreateSimpleObject(SHPT_ARC,vCount,pX,pY,NULL);
	   if (newShp == NULL)
	     {
	     bOk = 0;
	     break;
	     }
	   out = SHPWriteObject(shpOut,-1,newShp);
	   bOk = copyAttributes(dbfIn,dbfOut,in,out);
	   SHPDestroyObject(newShp);
	   free(pX);
	   free(pY);
	   }
       }
     }  
  if (bVerbose)
    {
    printf("Input file had %d features - output file has %d features\n",
	   in,out);
    }
  return bOk;
}
int restructureFeatures ( SHPHandle shpIn , SHPHandle shpOut , DBFHandle dbfIn , DBFHandle dbfOut ) { int bOk = 1 ; int inputCount = 0 ; int shapeType = 0 ; int in = 0 ; int out = 0 ; int part = 0 ; SHPObject * readShp = NULL ; SHPObject * newShp = NULL ; SHPGetInfo ( shpIn , & inputCount , & shapeType , NULL , NULL ) ; for ( in = 0 ; ( in < inputCount ) && ( bOk > 0 ) ; in ++ ) { readShp = SHPReadObject ( shpIn , in ) ; if ( bVerbose ) printf ( " \n " , in ) ; if ( readShp == NULL ) { bOk = 0 ; break ; } if ( bVerbose ) printf ( " \n " , in , readShp -> nParts , readShp -> nVertices ) ; if ( readShp -> nParts == 1 ) { newShp = SHPCreateSimpleObject ( SHPT_ARC , readShp -> nVertices , readShp -> padfX , readShp -> padfY , NULL ) ; if ( newShp == NULL ) { bOk = 0 ; break ; } out = SHPWriteObject ( shpOut , -1 , newShp ) ; bOk = copyAttributes ( dbfIn , dbfOut , in , out ) ; SHPDestroyObject ( newShp ) ; } else { for ( part = 0 ; part < readShp -> nParts ; part ++ ) { int p = 0 ; double * pX = NULL ; double * pY = NULL ; int vCount = 0 ; int start = readShp -> panPartStart [ part ] ; int end ; if ( part == readShp -> nParts - 1 ) end = readShp -> nVertices - 1 ; else end = readShp -> panPartStart [ part + 1 ] - 1 ; vCount = end - start + 1 ; if ( bVerbose ) printf ( " \t \n " , in , part , vCount , start , end ) ; pX = calloc ( vCount , sizeof ( double ) ) ; pY = calloc ( vCount , sizeof ( double ) ) ; if ( ( pX == NULL ) || ( pY == NULL ) ) { bOk = 0 ; break ; } for ( p = 0 ; p < vCount ; p ++ ) { pX [ p ] = readShp -> padfX [ start + p ] ; pY [ p ] = readShp -> padfY [ start + p ] ; } newShp = SHPCreateSimpleObject ( SHPT_ARC , vCount , pX , pY , NULL ) ; if ( newShp == NULL ) { bOk = 0 ; break ; } out = SHPWriteObject ( shpOut , -1 , newShp ) ; bOk = copyAttributes ( dbfIn , dbfOut , in , out ) ; SHPDestroyObject ( newShp ) ; free ( pX ) ; free ( pY ) ; } } } if ( bVerbose ) { printf ( " \n " , in , out ) ; } return bOk ; }
Copy and transform data from the input coordinate and attribute
 files to the output.
Copy and transform data from the input coordinate and attribute files to the output .
/* for return */ /* number of features in the input file */ /* what kind of geographic feature? */ /* count features read */ /* count features written */ /* count parts within a feature */ /* just copy */ /* loop counter for points */ /* count of vertices in this part */ /* last part */ /* create coordinate arrays for this part */ /* copy coordinates */