Example #1
0
int main(int argc, char *argv[])
{
    /* variables   */
    CELL *data_buf, *clump_buf;
    CELL i, max;
    int row, col, rows, cols;
    int out_mode, use_MASK, *n, *e;
    long int *count;
    int fd_data, fd_clump;
    const char *datamap, *clumpmap, *site_list;
    const char *clump_mapset;
    double avg, vol, total_vol, east, north, *sum;
    struct Cell_head window;
    struct Map_info *fd_sites = NULL;
    Site *mysite;
    Site_head site_info;
    struct GModule *module;
    struct Option *opt1, *opt2, *opt3;
    struct Flag *flag1;

    /* Initialize GIS */
    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("raster"));
    G_add_keyword(_("volume"));
    module->description =
	_("Calculates the volume of data \"clumps\", "
	  "and (optionally) produces a GRASS vector points map "
	  "containing the calculated centroids of these clumps.");

    opt1 = G_define_standard_option(G_OPT_R_INPUT);
    opt1->description =
	_("Existing raster map representing data that will be summed within clumps");

    opt2 = G_define_standard_option(G_OPT_R_INPUT);
    opt2->key = "clump";
    opt2->required = NO;
    opt2->description =
	_("Existing raster map, preferably the output of r.clump");

    opt3 = G_define_standard_option(G_OPT_V_OUTPUT);
    opt3->key = "centroids";
    opt3->required = NO;
    opt3->description = _("Vector points map to contain clump centroids");

    flag1 = G_define_flag();
    flag1->key = 'f';
    flag1->description = _("Generate unformatted report");

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    /* get current window */
    G_get_window(&window);

    /* initialize */
    out_mode = 1;		/* assume full output text */
    mysite = G_site_new_struct(CELL_TYPE, 2, 0, 4);

    /* get arguments */
    datamap = opt1->answer;

    if (opt2->answer)
	clumpmap = opt2->answer;
    else
	clumpmap = "";

    if (opt3->answer)
	site_list = opt3->answer;
    else
	site_list = "";

    out_mode = (!flag1->answer);

    if (*datamap == 0)
	G_fatal_error(_("No data map specified"));

    /*
     * See if MASK or a separate "clumpmap" layer is to be used-- it must(!)
     * be one of those two choices.
     */
    use_MASK = 0;
    if (*clumpmap == '\0') {
	clumpmap = "MASK";
	use_MASK = 1;
    }
    fd_data = Rast_open_old(datamap, "");
    if (use_MASK)
	clump_mapset = G_mapset();
    else
	clump_mapset = "";

    fd_clump = Rast_open_old(clumpmap, clump_mapset);

    /* initialize sites file (for centroids) if needed */
    if (*site_list) {
	fd_sites = G_fopen_sites_new(site_list);
	if (fd_sites == NULL)
	    G_fatal_error(_("Unable to open centroids vector points map"));
    }

    /* initialize data accumulation arrays */
    max = Rast_get_max_c_cat(clumpmap, clump_mapset);

    sum = (double *)G_malloc((max + 1) * sizeof(double));
    count = (long int *)G_malloc((max + 1) * sizeof(long int));

    for (i = 0; i <= max; i++) {
	sum[i] = 0;
	count[i] = 0;
    }

    data_buf = Rast_allocate_c_buf();
    clump_buf = Rast_allocate_c_buf();

    /* get window size */
    rows = window.rows;
    cols = window.cols;

    /* now get the data -- first pass */
    G_message("Complete ...");
    for (row = 0; row < rows; row++) {
	G_percent(row, rows, 2);
	Rast_get_c_row(fd_data, data_buf, row);
	Rast_get_c_row(fd_clump, clump_buf, row);
	for (col = 0; col < cols; col++) {
	    i = clump_buf[col];
	    if (i > max)
		G_fatal_error(
		    "Row=%d Col=%d Cat=%d in clump map [%s]; max=%d.\n"
		    "Cat value > max returned by Rast_get_max_c_cat.",
		    row, col, i, clumpmap, max);
	    if (i < 1)
		continue;	/* ignore zeros and negs */
	    count[i]++;
	    sum[i] += data_buf[col];
	}
    }
    G_percent(row, rows, 2);
    /* free some buffer space */
    G_free(data_buf);
    G_free(clump_buf);

    /* data lists for centroids of clumps */
    e = (int *)G_malloc((max + 1) * sizeof(int));
    n = (int *)G_malloc((max + 1) * sizeof(int));

    i = centroids(fd_clump, e, n, 1, max);

    /* got everything, now do output */
    if (*site_list) {
	char desc[GNAME_MAX * 2 + 40];

	site_info.form = NULL;
	site_info.time = NULL;
	site_info.stime = NULL;
	sprintf(desc, "from %s on map %s using clumps from %s",
		argv[0], datamap, clumpmap);
	site_info.desc = G_store(desc);
	site_info.name = G_store(site_list);
	site_info.labels =
	    G_store("centroid east|centroid north|#cat vol avg t n");
	G_site_put_head(fd_sites, &site_info);
    }
    if (out_mode) {
	fprintf(stdout, "Volume report on data from %s", datamap);
	fprintf(stdout, " using clumps on %s map\n\n", clumpmap);
	fprintf(stdout,
		" Cat    Average   Data   # Cells        Centroid             Total\n");
	fprintf(stdout,
		"Number  in clump  Total  in clump   Easting   Northing       Volume\n\n");
    }
    total_vol = 0.0;

    for (i = 1; i <= max; i++) {
	if (count[i]) {
	    avg = sum[i] / (double)count[i];
	    vol = sum[i] * window.ew_res * window.ns_res;
	    total_vol += vol;
	    east = window.west + (e[i] + 0.5) * window.ew_res;
	    north = window.north - (n[i] + 0.5) * window.ns_res;
	    if (*site_list) {
		mysite->east = east;
		mysite->north = north;
		mysite->ccat = i;
		mysite->dbl_att[0] = vol;
		mysite->dbl_att[1] = avg;
		mysite->dbl_att[2] = sum[i];
		mysite->dbl_att[3] = (double)count[i];
		/*       "%-1.2f|%-1.2f|#%5d v=%-1.2f a=%-1.2f t=%-1.0f n=%ld\n", */
		/* east, north, i, vol, avg, sum[i], count[i]); */
		G_site_put(fd_sites, mysite);
	    }
	    if (out_mode)
		fprintf(stdout,
			"%5d%10.2f%10.0f %7ld  %10.2f  %10.2f %16.2f\n", i,
			avg, sum[i], count[i], east, north, vol);
	    else
		fprintf(stdout, "%d:%.2f:%.0f:%ld:%.2f:%.2f:%.2f\n",
			i, avg, sum[i], count[i], east, north, vol);
	}
    }
    if (total_vol > 0.0 && out_mode)
	fprintf(stdout, "%58s %14.2f", "Total Volume =", total_vol);
    fprintf(stdout, "\n");
    exit(EXIT_SUCCESS);
}				/* end of main() */
Example #2
0
int main(int argc, char *argv[])
{
    char *me;
    char *output, *input;
    char *fs;
    int dims, i, has_cat;
    struct GModule *module;
    FILE *in_fd, *out_fd;
    Site *site;
    Site_head shead;
    struct TimeStamp ts;
    struct
    {
	struct Option *input, *output, *dims, *fs, *date;
    } parm;

    G_gisinit(me = argv[0]);

    module = G_define_module();
    G_add_keyword(_("sites"));
    module->description =
	"Convert an ASCII listing of site locations "
	"into a GRASS site list file.";

    parm.output = G_define_option();
    parm.output->key = "output";
    parm.output->type = TYPE_STRING;
    parm.output->required = YES;
    parm.output->description = "vector map to be created";
    parm.output->gisprompt = "any,vector,vector";

    parm.input = G_define_option();
    parm.input->key = "input";
    parm.input->type = TYPE_STRING;
    parm.input->required = NO;
    parm.input->description = "unix file containing sites";

    parm.dims = G_define_option();
    parm.dims->key = "d";
    parm.dims->type = TYPE_INTEGER;
    parm.dims->required = NO;
    parm.dims->description = "number of dimensions (default=2)";

    parm.fs = G_define_option();
    parm.fs->key = "fs";
    parm.fs->key_desc = "character|space|tab";
    parm.fs->type = TYPE_STRING;
    parm.fs->required = NO;
    parm.fs->description = "input field separator";
    parm.fs->answer = "space";

    parm.date = G_define_option();
    parm.date->key = "date";
    parm.date->key_desc = "timestamp";
    parm.date->required = NO;
    parm.date->type = TYPE_STRING;
    parm.date->description = "datetime or datetime1/datetime2";

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    if ((input = parm.input->answer)) {
	in_fd = fopen(input, "r");
	if (NULL == in_fd) {
	    fprintf(stderr, "%s - ", me);
	    perror(input);
	    exit(1);
	}
    }
    else
	in_fd = stdin;

    output = parm.output->answer;
    shead.name = G_store(parm.output->answer);
    shead.desc = G_store(G_recreate_command());
    shead.form = shead.labels = shead.stime = (char *)NULL;

    /* add here time parameter */
    if (parm.date->answer) {
	if (1 == G_scan_timestamp(&ts, parm.date->answer))
	    shead.time = &ts;
	else
	    G_fatal_error("Invalid timestamp");
    }
    else
	shead.time = (struct TimeStamp *)NULL;

    dims = 2;
    loop = 1;			/* added 11/99 MNeteler */

    if (parm.dims->answer != NULL)
	if ((i = sscanf(parm.dims->answer, "%d", &dims)) != 1)
	    G_fatal_error("error scanning number of dimensions");
    if (dims < 2)
	G_fatal_error("number of dimensions must be greater than 1");

    if (strlen(parm.fs->answer) < 1)
	G_fatal_error("field separator cannot be empty");
    else {
	fs = parm.fs->answer;
	if (strcmp(fs, "space") == 0)
	    fs = NULL;
	else if (strcmp(fs, "tab") == 0)
	    fs = NULL;
    }

    out_fd = G_fopen_sites_new(output);
    if (out_fd == NULL)
	G_fatal_error("can't create sites file [%s].", output);

    G_site_put_head(out_fd, &shead);

    while ((site = get_site(in_fd, dims, fs, &has_cat)))
	G_site_put(out_fd, site);

    G_sites_close(out_fd);
    exit(0);
}
Example #3
0
// 
// Refine each tile individually, write it to disk, and free it from
// memory. This way only one tile and boundary arrays are in memory at
// one time.
//
void refineTin(double e, short delaunay, TIN *tin,char *path,
	       char *siteFileName, char *vectFileName, short useNodata){

  TIN_TILE *tt;
  printf("refining..\n"); fflush(stdout);
  
  // write tin file headers
  if(path != NULL)
    writeTin(tin,path,1);

#ifdef __GRASS__
  // Write site file headers
  //
  FILE *sitesFile = NULL;
  struct Map_info Map;
    
  if(siteFileName != NULL){
    char errbuf[100];
    if ((sitesFile = G_fopen_sites_new(siteFileName)) == NULL){
      sprintf(errbuf,"Not able to open sitesfile for [%s]\n", siteFileName);
      G_fatal_error(errbuf);
    }
    writeSitesHeader(sitesFile,siteFileName);
    assert(sitesFile); 
  }  
  if (vectFileName != NULL) {
    // Write vector file headers
    //
    // Create new digit file
    if ((Vect_open_new (&Map, vectFileName)) < 0){
      G_fatal_error("Creating new vector file.\n") ;
    }
    
    // Write vector header
    set_default_head_info (&(Map.head));
  }
#endif
  
  // Skip the dummy head
  tt = tin->tt->next;
  while(tt->next != NULL){
    refineTile(tt,e,delaunay,useNodata);
    tin->numTris += tt->numTris;
    tin->numPoints += tt->numPoints;
#ifdef __GRASS__
    if(siteFileName != NULL) {
      writeSitesTile(tt,sitesFile, siteFileName);
      assert(sitesFile);
    }
    if(vectFileName != NULL) {
      writeVectorTile(&Map,tt);
    }
#endif
    if(path != NULL)
      writeTinTile(tt,path,1);
    
    // Go to next tile
    tt = tt->next;
  }
  // If there is only one tile then get info from it
  if(tin->tt == tt && tt->next != NULL){ // Fix me ?
    tin->numTris += tt->numTris;
    tin->numPoints += tt->numPoints;
  }

#ifdef __GRASS__
  if (siteFileName != NULL) {
    assert(sitesFile);
    fclose(sitesFile);
  }
  if (vectFileName != NULL) {
    Vect_close (&Map);
  }
#endif
  
  tinGlobal = tin;
  printf("done refining\n"); fflush(stdout);
}