int main(int argc, char* argv[]) { //this variable collects all user options MultiviewOptions options; parse_args(argc, argv, &options); record_args(options); #ifdef INTERPOLATE_RESULT viewsheds = (Nvis*) malloc((options->NVIEWSHEDS+10)*sizeof(Nvis)); assert(viewsheds); #endif //read input raster printf("reading input grid %s ", options.input_name); Grid *ingrid = read_grid_from_arcascii_file(options.input_name); assert(ingrid); printf("..done\n"); //number of rows and columns in the grid int nrows, ncols; nrows = ingrid->hd->nrows; ncols = ingrid->hd->ncols; printf("grid: rows = %d, cols = %d\n", nrows, ncols); if (options.NVIEWSHEDS ==0) options.NVIEWSHEDS = nrows * ncols; //create an output grid Grid* outgrid = create_empty_grid(); assert(outgrid); //outgrid->hd = create_empty_header(); //the header is allocated in create_empty_grid() copy_header(outgrid->hd, *(ingrid->hd)); alloc_grid_data(outgrid); /* **************************************** */ /* INITIALIZE EVENT LIST */ /* **************************************** */ /*allocate the eventlist to hold the maximum number of events possible*/ Event* eventList; eventList = (Event*) malloc(ncols * nrows * 3 * sizeof(Event)); assert(eventList); /*initialize the eventList with the info common to all viewpoints */ long nevents; Rtimer initTime; rt_start(initTime); nevents = init_event_list(eventList, ingrid ); printf("nb events = %ld\n", nevents); rt_stop(initTime); print_init_timings(initTime); /* ****************************** */ /* compute the viewshed of the i % DO_EVERY point */ /* ****************************** */ assert(options.NVIEWSHEDS > 0); int DO_EVERY = nrows * ncols/ options.NVIEWSHEDS; /* start going through the data and considering each point, in turn, as a viewshed */ if (options.SWEEP_MODE == SWEEP_DISTRIBUTE) { assert(options.BASECASE_THRESHOLD >0 && options.NUM_SECTORS >0); compute_multiviewshed_distribution(options, DO_EVERY, ingrid, outgrid, nevents, eventList); } else { compute_multiviewshed_radial(options, DO_EVERY, ingrid, outgrid, nevents, eventList); } /* ****************************** */ /*all sweeping and computing done - clean up */ free(eventList); //write output grid to file save_grid_to_arcascii_file(outgrid, options.output_name); //clean up destroy_grid(ingrid); destroy_grid(outgrid); #ifdef INTERPOLATE_RESULT //for NVIEWSHEDS small, the resulting map is basically all empty; //the interpolate function extends each non-empty output viewshed //value to a ball centered at that point interpolate_raster(outgrid, output_name); #endif exit(0); }
/* ---------------------------------------------------------------------- */ int main(int argc, char *argv[]) { struct GModule *module; Rtimer rtTotal; char buf[BUFSIZ]; /* initialize GIS library */ G_gisinit(argv[0]); module = G_define_module(); #ifdef ELEV_SHORT module->description = _("Flow computation for massive grids (integer version)."); #endif #ifdef ELEV_FLOAT module->description = _("Flow computation for massive grids (float version)."); #endif G_add_keyword(_("raster")); G_add_keyword(_("hydrology")); /* read user options; fill in global <opt> */ opt = (userOptions*)malloc(sizeof(userOptions)); assert(opt); region = (struct Cell_head*)malloc(sizeof(struct Cell_head)); assert(region); parse_args(argc, argv); /* get the current region and dimensions */ G_get_set_window(region); check_args(); int nr = Rast_window_rows(); int nc = Rast_window_cols(); if ((nr > dimension_type_max) || (nc > dimension_type_max)) { G_fatal_error(_("[nrows=%d, ncols=%d] dimension_type overflow -- " "change dimension_type and recompile"), nr, nc); } else { nrows = (dimension_type)nr; ncols = (dimension_type)nc; } G_verbose_message( _("Region size is %d x %d"), nrows, ncols); /* check STREAM path (the place where intermediate STREAMs are placed) */ sprintf(buf, "%s=%s",STREAM_TMPDIR, opt->streamdir); /* don't pass an automatic variable; putenv() isn't guaranteed to make a copy */ putenv(G_store(buf)); if (getenv(STREAM_TMPDIR) == NULL) { fprintf(stderr, "%s:", STREAM_TMPDIR); G_fatal_error("not set"); } else { fprintf(stderr, "STREAM temporary files in %s ", getenv(STREAM_TMPDIR)); fprintf(stderr, "(THESE INTERMEDIATE STREAMS WILL NOT BE DELETED IN CASE OF ABNORMAL TERMINATION OF THE PROGRAM. TO SAVE SPACE PLEASE DELETE THESE FILES MANUALLY!)\n"); } /* open the stats file */ stats = new statsRecorder(opt->stats); record_args(argc, argv); { char buf[BUFSIZ]; long grid_size = nrows * ncols; *stats << "region size = " << formatNumber(buf, grid_size) << " elts " << "(" << nrows << " rows x " << ncols << " cols)\n"; stats->flush(); } /* set up STREAM memory manager */ size_t mm_size = (size_t) opt->mem << 20; /* opt->mem is in MB */ MM_manager.set_memory_limit(mm_size); if (opt->verbose) { MM_manager.warn_memory_limit(); } else { MM_manager.ignore_memory_limit(); } MM_manager.print_limit_mode(); /* initialize nodata */ nodataType::init(); *stats << "internal nodata value: " << nodataType::ELEVATION_NODATA << endl; /* start timing -- after parse_args, which are interactive */ rt_start(rtTotal); #ifndef JUMP2FLOW /* read elevation into a stream */ AMI_STREAM<elevation_type> *elstr=NULL; long nodata_count; elstr = cell2stream<elevation_type>(opt->elev_grid, elevation_type_max, &nodata_count); /* print the largest interm file that will be generated */ printMaxSortSize(nodata_count); /* -------------------------------------------------- */ /* compute flow direction and filled elevation (and watersheds) */ AMI_STREAM<direction_type> *dirstr=NULL; AMI_STREAM<elevation_type> *filledstr=NULL; AMI_STREAM<waterWindowBaseType> *flowStream=NULL; AMI_STREAM<labelElevType> *labeledWater = NULL; flowStream=computeFlowDirections(elstr, filledstr, dirstr, labeledWater); delete elstr; /* write streams to GRASS raster maps */ stream2_CELL(dirstr, nrows, ncols, opt->dir_grid); delete dirstr; #ifdef ELEV_SHORT stream2_CELL(filledstr, nrows, ncols, opt->filled_grid); #else stream2_CELL(filledstr, nrows, ncols, opt->filled_grid,true); #endif delete filledstr; stream2_CELL(labeledWater, nrows, ncols, labelElevTypePrintLabel(), opt->watershed_grid); setSinkWatershedColorTable(opt->watershed_grid); delete labeledWater; #else AMI_STREAM<waterWindowBaseType> *flowStream; char path[GPATH_MAX]; sprintf(path, "%s/flowStream", streamdir->answer); flowStream = new AMI_STREAM<waterWindowBaseType>(path); fprintf(stderr, "flowStream opened: len=%d\n", flowStream->stream_len()); fprintf(stderr, "jumping to flow accumulation computation\n"); #endif /* -------------------------------------------------- */ /* compute flow accumulation (and tci) */ AMI_STREAM<sweepOutput> *outstr=NULL; computeFlowAccumulation(flowStream, outstr); /* delete flowStream -- deleted inside */ /* write output stream to GRASS raster maps */ #ifdef OUTPUT_TCI stream2_FCELL(outstr, nrows, ncols, printAccumulation(), printTci(), opt->flowaccu_grid, opt->tci_grid); #else stream2_FCELL(outstr, nrows, ncols, printAccumulation(), opt->flowaccu_grid); #endif setFlowAccuColorTable(opt->flowaccu_grid); delete outstr; rt_stop(rtTotal); stats->recordTime("Total running time: ", rtTotal); stats->timestamp("end"); G_done_msg(" "); /* free the globals */ free(region); free(opt); delete stats; return 0; }