Modified shpindex that reports status

Trevor Banister banister at banister.com
Tue Dec 28 16:43:12 PST 1999


I dug into the source to answer my own question about the grid settings.

I added a status function to shpindex.c so I could see how long the
indexing process would take on some of my large (>500,000) shape files.
Turns out that is will take a few days for some of them :-) Hopefully
the speedup in map rendering will be worth it. Add this to the next
release if you want. 

Is there enough mods and stuff like this that people send in to start a
"contrib" folder in the distribution?

Trevor



/* shpindex.c, from Mapserver 3.3 by Stephen Lime.*/
/* Modified by Trevor Banister */
/* Added verbose mode, prints status every 100 shapes if argv[4] isn't
NULL*/
/* i.e. shpindex myfile 10 10 verbose */

#include "map.h"
#include "mapindex.h"

int main(int argc, char *argv[])
{
  SHPHandle hSHP;
  int nShapeType;
  int nEntities;
  rectObj shpBounds;
  double tmpBounds[4];
  int i;
  int verbose;
  


  char *filename;
  indexObj *idx;

  if(argc<4) {
   fprintf(stdout,"Syntax: shpindex [shpfile] [sx] [sy] [verbose]\n" );
   exit(0);
  }
  
  if (argv[4] != NULL) {
    verbose=1;
  }
  else {
    verbose=0;
  }
  /*
  ** Open shapefile
  */
  if((hSHP = SHPOpen(argv[1], "r" )) == NULL) {
    fprintf(stderr, "Error opening shapefile %s.\n", argv[1]);
    exit(0);
  }

  /* 
  ** Load some information about this shapefile, don't need much
  */
  SHPGetInfo(hSHP, &nEntities, &nShapeType);
  SHPReadBounds(hSHP, -1, tmpBounds);

  shpBounds.minx = tmpBounds[0];
  shpBounds.miny = tmpBounds[1];
  shpBounds.maxx = tmpBounds[2];
  shpBounds.maxy = tmpBounds[3];
  
  /*
  ** Initialize the index
  */
  if((idx = msNewIndex(shpBounds, atoi(argv[2]), atoi(argv[3]),
nEntities)) == NULL) {
    msWriteError(stderr);
    exit(0);
  }

  /* 
  ** For each shape 
  */
  for(i=0;i<nEntities;i++) {
    if (verbose && !(i % 100)) {
      fprintf(stderr, "Working on shape %i of %i\n", i,nEntities); 
    }
    SHPReadBounds(hSHP, i, tmpBounds); /* grab the rectangle for this
shape */

    shpBounds.minx = tmpBounds[0];
    shpBounds.miny = tmpBounds[1];
    shpBounds.maxx = tmpBounds[2];
    shpBounds.maxy = tmpBounds[3];

    if(msInsertRect(idx, i, shpBounds) == -1) { /* add it to the index
*/
      msWriteError(stderr);
      msCloseIndex(idx);
      exit(0);
    }
  }

  filename = (char *)malloc(strlen(argv[1])+strlen(EXTENSION)+1);
  sprintf(filename, "%s%s", argv[1], EXTENSION);
  msWriteIndex(idx, filename);
  msCloseIndex(idx);

  /*
  ** Clean things up
  */
  SHPClose(hSHP);
  free(filename);

  exit(0);
}







More information about the MapServer-users mailing list