Random updates to cell files????

Tom Moore tmoore at pnfi.forestry.ca
Mon Aug 14 08:00:00 EDT 1995


Hello again...

On Friday I was asking about how to quickly update a few
cells in a large raster layer.  After looking at the source to
libgis again I wrote the following hack to allow opening raster layers
for update (permitting successful access to both 'G_get_map_row' 
and 'G_put_map_row_random').  It **seems** to work (but not thoroughly
tested, probably has bugs, and absolutely no guarantees about it).  
The layer to be updated must be uncompressed first.

Tom
------ 


#include "G.h"
#define FCB G__.fileinfo[fd]

int 
G_open_cell_old_update(char *name, char *mapset) {
  int fd, old_fd, n;
  
  /* try to open the file */
  fd = G_open_cell_old(name,mapset);
  if (fd <0) 
    return(fd);

  /* give up if it is compressed (run 'r.compress -u' first) */
  if (FCB.cellhd.compressed) {
    G_close_cell(fd);
    return -1;
  }
  
  /* now try to reopen the file in update mode */
  old_fd = fd;
  close(fd);
  fd = G_open_update("cell",name);
  if (fd < 0) {
    G__.fileinfo[old_fd].open_mode = -1;
    return(fd);
  }

  /* copy the file header if the fd is different */
  if (fd!=old_fd) {
    bcopy(&G__.fileinfo[fd],&G__.fileinfo[old_fd],sizeof(struct fileinfo));
    G__.fileinfo[old_fd].open_mode = -1;
  }

  /* toggle the open_mode flag to RANDOM */
  FCB.open_mode = OPEN_NEW_RANDOM;

  /* allocate a work buffer (if not done already) */
  n = FCB.cellhd.cols * (sizeof(CELL) + 1) + 1;
  if (n > G__.work_buf_size)
    {
      if (G__.work_buf_size <= 0)
        G__.work_buf = (unsigned char *) G_malloc (n);
      else
        G__.work_buf = (unsigned char *) G_realloc(G__.work_buf,n);
      G__.work_buf_size  = n;
    }

  return (fd);
}

void 
G_close_cell_update(int fd) {

  /* set the open_mode flag back to OPEN_OLD before closing the file */
  FCB.open_mode = OPEN_OLD;
  G_close_cell(fd);
}

---- end of sample code ----





More information about the grass-dev mailing list