static int input_data(struct interp_params *params, int first_row, int last_row, struct fcell_triple *points, int fdsmooth, int fdinp, int inp_rows, int inp_cols, double zmin, double inp_ns_res, double inp_ew_res) { double x, y, sm; /* input data and smoothing */ int m1, m2; /* loop counters */ static FCELL *cellinp = NULL; /* cell buffer for input data */ static FCELL *cellsmooth = NULL; /* cell buffer for smoothing */ if (!cellinp) cellinp = Rast_allocate_f_buf(); if (!cellsmooth) cellsmooth = Rast_allocate_f_buf(); for (m1 = 0; m1 <= last_row - first_row; m1++) { Rast_get_f_row(fdinp, cellinp, inp_rows - m1 - first_row); if (fdsmooth >= 0) Rast_get_f_row(fdsmooth, cellsmooth, inp_rows - m1 - first_row); y = params->y_orig + (m1 + first_row - 1 + 0.5) * inp_ns_res; for (m2 = 0; m2 < inp_cols; m2++) { x = params->x_orig + (m2 + 0.5) * inp_ew_res; /* * z = cellinp[m2]*params->zmult; */ if (fdsmooth >= 0) sm = (double)cellsmooth[m2]; else sm = 0.01; points[m1 * inp_cols + m2].x = x - params->x_orig; points[m1 * inp_cols + m2].y = y - params->y_orig; if (!Rast_is_f_null_value(cellinp + m2)) { points[m1 * inp_cols + m2].z = cellinp[m2] * params->zmult - zmin; } else { Rast_set_f_null_value(&(points[m1 * inp_cols + m2].z), 1); } /* fprintf (stdout,"sm: %f\n",sm); */ points[m1 * inp_cols + m2].smooth = sm; } } return 1; }
static void init_channel(channel *c) { sprintf(c->name, "%s%s", output, c->suffix); if (Float) { c->fd = Rast_open_fp_new(c->name); c->fbuf = Rast_allocate_f_buf(); } else { c->fd = Rast_open_c_new(c->name); c->buf = Rast_allocate_c_buf(); } c->active = 1; }
/* ************************************************************************* */ void g3d_to_raster(void *map, RASTER3D_Region region, int *fd) { DCELL d1 = 0; FCELL f1 = 0; int x, y, z; int rows, cols, depths, typeIntern, pos = 0; FCELL *fcell = NULL; DCELL *dcell = NULL; rows = region.rows; cols = region.cols; depths = region.depths; G_debug(2, "g3d_to_raster: Writing %i raster maps with %i rows %i cols.", depths, rows, cols); typeIntern = Rast3d_tile_type_map(map); if (typeIntern == FCELL_TYPE) fcell = Rast_allocate_f_buf(); else if (typeIntern == DCELL_TYPE) dcell = Rast_allocate_d_buf(); pos = 0; /*Every Rastermap */ for (z = 0; z < depths; z++) { /*From the bottom to the top */ G_debug(2, "Writing raster map %d of %d", z + 1, depths); for (y = 0; y < rows; y++) { G_percent(y, rows - 1, 10); for (x = 0; x < cols; x++) { if (typeIntern == FCELL_TYPE) { Rast3d_get_value(map, x, y, z, &f1, typeIntern); if (Rast3d_is_null_value_num(&f1, FCELL_TYPE)) Rast_set_null_value(&fcell[x], 1, FCELL_TYPE); else fcell[x] = f1; } else { Rast3d_get_value(map, x, y, z, &d1, typeIntern); if (Rast3d_is_null_value_num(&d1, DCELL_TYPE)) Rast_set_null_value(&dcell[x], 1, DCELL_TYPE); else dcell[x] = d1; } } if (typeIntern == FCELL_TYPE) Rast_put_f_row(fd[pos], fcell); if (typeIntern == DCELL_TYPE) Rast_put_d_row(fd[pos], dcell); } G_debug(2, "Finished writing map %d.", z + 1); pos++; } if (dcell) G_free(dcell); if (fcell) G_free(fcell); }
int main(int argc, char *argv[]) { int m1; struct FPRange range; DCELL cellmin, cellmax; FCELL *cellrow, fcellmin; struct GModule *module; struct { struct Option *input, *elev, *slope, *aspect, *pcurv, *tcurv, *mcurv, *smooth, *maskmap, *zmult, *fi, *segmax, *npmin, *res_ew, *res_ns, *overlap, *theta, *scalex; } parm; struct { struct Flag *deriv, *cprght; } flag; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("resample")); module->description = _("Reinterpolates and optionally computes topographic analysis from " "input raster map to a new raster map (possibly with " "different resolution) using regularized spline with " "tension and smoothing."); parm.input = G_define_standard_option(G_OPT_R_INPUT); parm.res_ew = G_define_option(); parm.res_ew->key = "ew_res"; parm.res_ew->type = TYPE_DOUBLE; parm.res_ew->required = YES; parm.res_ew->description = _("Desired east-west resolution"); parm.res_ns = G_define_option(); parm.res_ns->key = "ns_res"; parm.res_ns->type = TYPE_DOUBLE; parm.res_ns->required = YES; parm.res_ns->description = _("Desired north-south resolution"); parm.elev = G_define_option(); parm.elev->key = "elev"; parm.elev->type = TYPE_STRING; parm.elev->required = NO; parm.elev->gisprompt = "new,cell,raster"; parm.elev->description = _("Output z-file (elevation) map"); parm.elev->guisection = _("Output"); parm.slope = G_define_option(); parm.slope->key = "slope"; parm.slope->type = TYPE_STRING; parm.slope->required = NO; parm.slope->gisprompt = "new,cell,raster"; parm.slope->description = _("Output slope map (or fx)"); parm.slope->guisection = _("Output"); parm.aspect = G_define_option(); parm.aspect->key = "aspect"; parm.aspect->type = TYPE_STRING; parm.aspect->required = NO; parm.aspect->gisprompt = "new,cell,raster"; parm.aspect->description = _("Output aspect map (or fy)"); parm.aspect->guisection = _("Output"); parm.pcurv = G_define_option(); parm.pcurv->key = "pcurv"; parm.pcurv->type = TYPE_STRING; parm.pcurv->required = NO; parm.pcurv->gisprompt = "new,cell,raster"; parm.pcurv->description = _("Output profile curvature map (or fxx)"); parm.pcurv->guisection = _("Output"); parm.tcurv = G_define_option(); parm.tcurv->key = "tcurv"; parm.tcurv->type = TYPE_STRING; parm.tcurv->required = NO; parm.tcurv->gisprompt = "new,cell,raster"; parm.tcurv->description = _("Output tangential curvature map (or fyy)"); parm.tcurv->guisection = _("Output"); parm.mcurv = G_define_option(); parm.mcurv->key = "mcurv"; parm.mcurv->type = TYPE_STRING; parm.mcurv->required = NO; parm.mcurv->gisprompt = "new,cell,raster"; parm.mcurv->description = _("Output mean curvature map (or fxy)"); parm.mcurv->guisection = _("Output"); parm.smooth = G_define_option(); parm.smooth->key = "smooth"; parm.smooth->type = TYPE_STRING; parm.smooth->required = NO; parm.smooth->gisprompt = "old,cell,raster"; parm.smooth->description = _("Name of raster map containing smoothing"); parm.smooth->guisection = _("Settings"); parm.maskmap = G_define_option(); parm.maskmap->key = "maskmap"; parm.maskmap->type = TYPE_STRING; parm.maskmap->required = NO; parm.maskmap->gisprompt = "old,cell,raster"; parm.maskmap->description = _("Name of raster map to be used as mask"); parm.maskmap->guisection = _("Settings"); parm.overlap = G_define_option(); parm.overlap->key = "overlap"; parm.overlap->type = TYPE_INTEGER; parm.overlap->required = NO; parm.overlap->answer = OVERLAP; parm.overlap->description = _("Rows/columns overlap for segmentation"); parm.overlap->guisection = _("Settings"); parm.zmult = G_define_option(); parm.zmult->key = "zmult"; parm.zmult->type = TYPE_DOUBLE; parm.zmult->answer = ZMULT; parm.zmult->required = NO; parm.zmult->description = _("Multiplier for z-values"); parm.zmult->guisection = _("Settings"); parm.fi = G_define_option(); parm.fi->key = "tension"; parm.fi->type = TYPE_DOUBLE; parm.fi->answer = TENSION; parm.fi->required = NO; parm.fi->description = _("Spline tension value"); parm.fi->guisection = _("Settings"); parm.theta = G_define_option(); parm.theta->key = "theta"; parm.theta->type = TYPE_DOUBLE; parm.theta->required = NO; parm.theta->description = _("Anisotropy angle (in degrees)"); parm.theta->guisection = _("Anisotropy"); parm.scalex = G_define_option(); parm.scalex->key = "scalex"; parm.scalex->type = TYPE_DOUBLE; parm.scalex->required = NO; parm.scalex->description = _("Anisotropy scaling factor"); parm.scalex->guisection = _("Anisotropy"); flag.cprght = G_define_flag(); flag.cprght->key = 't'; flag.cprght->description = _("Use dnorm independent tension"); flag.deriv = G_define_flag(); flag.deriv->key = 'd'; flag.deriv->description = _("Output partial derivatives instead of topographic parameters"); flag.deriv->guisection = _("Output"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); G_get_set_window(&winhd); inp_ew_res = winhd.ew_res; inp_ns_res = winhd.ns_res; inp_cols = winhd.cols; inp_rows = winhd.rows; inp_x_orig = winhd.west; inp_y_orig = winhd.south; input = parm.input->answer; smooth = parm.smooth->answer; maskmap = parm.maskmap->answer; elev = parm.elev->answer; slope = parm.slope->answer; aspect = parm.aspect->answer; pcurv = parm.pcurv->answer; tcurv = parm.tcurv->answer; mcurv = parm.mcurv->answer; cond2 = ((pcurv != NULL) || (tcurv != NULL) || (mcurv != NULL)); cond1 = ((slope != NULL) || (aspect != NULL) || cond2); deriv = flag.deriv->answer; dtens = flag.cprght->answer; ertre = 0.1; if (!G_scan_resolution(parm.res_ew->answer, &ew_res, winhd.proj)) G_fatal_error(_("Unable to read ew_res value")); if (!G_scan_resolution(parm.res_ns->answer, &ns_res, winhd.proj)) G_fatal_error(_("Unable to read ns_res value")); if (sscanf(parm.fi->answer, "%lf", &fi) != 1) G_fatal_error(_("Invalid value for tension")); if (sscanf(parm.zmult->answer, "%lf", &zmult) != 1) G_fatal_error(_("Invalid value for zmult")); if (sscanf(parm.overlap->answer, "%d", &overlap) != 1) G_fatal_error(_("Invalid value for overlap")); if (parm.theta->answer) { if (sscanf(parm.theta->answer, "%lf", &theta) != 1) G_fatal_error(_("Invalid value for theta")); } if (parm.scalex->answer) { if (sscanf(parm.scalex->answer, "%lf", &scalex) != 1) G_fatal_error(_("Invalid value for scalex")); if (!parm.theta->answer) G_fatal_error(_("When using anisotropy both theta and scalex must be specified")); } /* * G_set_embedded_null_value_mode(1); */ outhd.ew_res = ew_res; outhd.ns_res = ns_res; outhd.east = winhd.east; outhd.west = winhd.west; outhd.north = winhd.north; outhd.south = winhd.south; outhd.proj = winhd.proj; outhd.zone = winhd.zone; G_adjust_Cell_head(&outhd, 0, 0); ew_res = outhd.ew_res; ns_res = outhd.ns_res; nsizc = outhd.cols; nsizr = outhd.rows; disk = nsizc * nsizr * sizeof(int); az = G_alloc_vector(nsizc + 1); if (cond1) { adx = G_alloc_vector(nsizc + 1); ady = G_alloc_vector(nsizc + 1); if (cond2) { adxx = G_alloc_vector(nsizc + 1); adyy = G_alloc_vector(nsizc + 1); adxy = G_alloc_vector(nsizc + 1); } } if (smooth != NULL) { fdsmooth = Rast_open_old(smooth, ""); Rast_get_cellhd(smooth, "", &smhd); if ((winhd.ew_res != smhd.ew_res) || (winhd.ns_res != smhd.ns_res)) G_fatal_error(_("Map <%s> is the wrong resolution"), smooth); if (Rast_read_fp_range(smooth, "", &range) >= 0) Rast_get_fp_range_min_max(&range, &cellmin, &cellmax); fcellmin = (float)cellmin; if (Rast_is_f_null_value(&fcellmin) || fcellmin < 0.0) G_fatal_error(_("Smoothing values can not be negative or NULL")); } Rast_get_cellhd(input, "", &inphd); if ((winhd.ew_res != inphd.ew_res) || (winhd.ns_res != inphd.ns_res)) G_fatal_error(_("Input map resolution differs from current region resolution!")); fdinp = Rast_open_old(input, ""); sdisk = 0; if (elev != NULL) sdisk += disk; if (slope != NULL) sdisk += disk; if (aspect != NULL) sdisk += disk; if (pcurv != NULL) sdisk += disk; if (tcurv != NULL) sdisk += disk; if (mcurv != NULL) sdisk += disk; G_message(_("Processing all selected output files will require")); if (sdisk > 1024) { if (sdisk > 1024 * 1024) { if (sdisk > 1024 * 1024 * 1024) { G_message(_("%.2f GB of disk space for temp files."), sdisk / (1024. * 1024. * 1024.)); } else G_message(_("%.2f MB of disk space for temp files."), sdisk / (1024. * 1024.)); } else G_message(_("%.2f KB of disk space for temp files."), sdisk / 1024.); } else G_message(_("%d bytes of disk space for temp files."), sdisk); fstar2 = fi * fi / 4.; tfsta2 = fstar2 + fstar2; deltx = winhd.east - winhd.west; delty = winhd.north - winhd.south; xmin = winhd.west; xmax = winhd.east; ymin = winhd.south; ymax = winhd.north; if (smooth != NULL) smc = -9999; else smc = 0.01; if (Rast_read_fp_range(input, "", &range) >= 0) { Rast_get_fp_range_min_max(&range, &cellmin, &cellmax); } else { cellrow = Rast_allocate_f_buf(); for (m1 = 0; m1 < inp_rows; m1++) { Rast_get_f_row(fdinp, cellrow, m1); Rast_row_update_fp_range(cellrow, m1, &range, FCELL_TYPE); } Rast_get_fp_range_min_max(&range, &cellmin, &cellmax); } fcellmin = (float)cellmin; if (Rast_is_f_null_value(&fcellmin)) G_fatal_error(_("Maximum value of a raster map is NULL.")); zmin = (double)cellmin *zmult; zmax = (double)cellmax *zmult; G_debug(1, "zmin=%f, zmax=%f", zmin, zmax); if (fd4 != NULL) fprintf(fd4, "deltx,delty %f %f \n", deltx, delty); create_temp_files(); IL_init_params_2d(¶ms, NULL, 1, 1, zmult, KMIN, KMAX, maskmap, outhd.rows, outhd.cols, az, adx, ady, adxx, adyy, adxy, fi, MAXPOINTS, SCIK1, SCIK2, SCIK3, smc, elev, slope, aspect, pcurv, tcurv, mcurv, dmin, inp_x_orig, inp_y_orig, deriv, theta, scalex, Tmp_fd_z, Tmp_fd_dx, Tmp_fd_dy, Tmp_fd_xx, Tmp_fd_yy, Tmp_fd_xy, NULL, NULL, 0, NULL); /* In the above line, the penultimate argument is supposed to be a * deviations file pointer. None is obvious, so I used NULL. */ /* The 3rd and 4th argument are int-s, elatt and smatt (from the function * definition. The value 1 seemed like a good placeholder... or not. */ IL_init_func_2d(¶ms, IL_grid_calc_2d, IL_matrix_create, IL_check_at_points_2d, IL_secpar_loop_2d, IL_crst, IL_crstg, IL_write_temp_2d); G_message(_("Temporarily changing the region to desired resolution ...")); Rast_set_window(&outhd); bitmask = IL_create_bitmask(¶ms); /* change region to initial region */ G_message(_("Changing back to the original region ...")); Rast_set_window(&winhd); ertot = 0.; cursegm = 0; G_message(_("Percent complete: ")); NPOINT = IL_resample_interp_segments_2d(¶ms, bitmask, zmin, zmax, &zminac, &zmaxac, &gmin, &gmax, &c1min, &c1max, &c2min, &c2max, &ertot, nsizc, &dnorm, overlap, inp_rows, inp_cols, fdsmooth, fdinp, ns_res, ew_res, inp_ns_res, inp_ew_res, dtens); G_message(_("dnorm in mainc after grid before out1= %f"), dnorm); if (NPOINT < 0) { clean(); G_fatal_error(_("split_and_interpolate() failed")); } if (fd4 != NULL) fprintf(fd4, "max. error found = %f \n", ertot); G_free_vector(az); if (cond1) { G_free_vector(adx); G_free_vector(ady); if (cond2) { G_free_vector(adxx); G_free_vector(adyy); G_free_vector(adxy); } } G_message(_("dnorm in mainc after grid before out2= %f"), dnorm); if (IL_resample_output_2d(¶ms, zmin, zmax, zminac, zmaxac, c1min, c1max, c2min, c2max, gmin, gmax, ertot, input, &dnorm, &outhd, &winhd, smooth, NPOINT) < 0) { clean(); G_fatal_error(_("Unable to write raster maps -- try increasing cell size")); } G_free(zero_array_cell); clean(); if (fd4) fclose(fd4); Rast_close(fdinp); if (smooth != NULL) Rast_close(fdsmooth); G_done_msg(" "); exit(EXIT_SUCCESS); }
int calculateF(int fd, struct area_entry *ad, double *result) { FCELL *buf, *buf_sup, *buf_null; FCELL corrCell, precCell, supCell; long npatch, area; long pid, old_pid, new_pid, *pid_corr, *pid_sup, *ltmp; struct pst *pst; long nalloc, incr; int i, j, k; int connected; int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked; struct Cell_head hd; Rast_get_window(&hd); buf_null = Rast_allocate_f_buf(); Rast_set_f_null_value(buf_null, Rast_window_cols()); buf_sup = buf_null; /* initialize patch ids */ pid_corr = G_malloc(Rast_window_cols() * sizeof(long)); pid_sup = G_malloc(Rast_window_cols() * sizeof(long)); for (j = 0; j < Rast_window_cols(); j++) { pid_corr[j] = 0; pid_sup[j] = 0; } /* open mask if needed */ mask_fd = -1; mask_buf = mask_sup = NULL; masked = FALSE; if (ad->mask == 1) { if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0) return RLI_ERRORE; mask_buf = G_malloc(ad->cl * sizeof(int)); if (mask_buf == NULL) { G_fatal_error("malloc mask_buf failed"); return RLI_ERRORE; } mask_sup = G_malloc(ad->cl * sizeof(int)); if (mask_sup == NULL) { G_fatal_error("malloc mask_buf failed"); return RLI_ERRORE; } for (j = 0; j < ad->cl; j++) mask_buf[j] = 0; masked = TRUE; } /* calculate number of patches */ npatch = 0; area = 0; pid = 0; /* patch size and type */ incr = 1024; if (incr > ad->rl) incr = ad->rl; if (incr > ad->cl) incr = ad->cl; if (incr < 2) incr = 2; nalloc = incr; pst = G_malloc(nalloc * sizeof(struct pst)); for (k = 0; k < nalloc; k++) { pst[k].count = 0; } for (i = 0; i < ad->rl; i++) { buf = RLI_get_fcell_raster_row(fd, i + ad->y, ad); if (i > 0) { buf_sup = RLI_get_fcell_raster_row(fd, i - 1 + ad->y, ad); } if (masked) { mask_tmp = mask_sup; mask_sup = mask_buf; mask_buf = mask_tmp; if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) return 0; } ltmp = pid_sup; pid_sup = pid_corr; pid_corr = ltmp; Rast_set_f_null_value(&precCell, 1); connected = 0; for (j = 0; j < ad->cl; j++) { pid_corr[j + ad->x] = 0; corrCell = buf[j + ad->x]; if (masked && (mask_buf[j] == 0)) { Rast_set_f_null_value(&corrCell, 1); } if (Rast_is_f_null_value(&corrCell)) { connected = 0; precCell = corrCell; continue; } area++; supCell = buf_sup[j + ad->x]; if (masked && (mask_sup[j] == 0)) { Rast_set_f_null_value(&supCell, 1); } if (!Rast_is_f_null_value(&precCell) && corrCell == precCell) { pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x]; connected = 1; pst[pid_corr[j + ad->x]].count++; } else { connected = 0; } if (!Rast_is_f_null_value(&supCell) && corrCell == supCell) { if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) { /* connect or merge */ /* after r.clump */ if (connected) { npatch--; if (npatch == 0) { G_fatal_error("npatch == 0 at row %d, col %d", i, j); } } old_pid = pid_corr[j + ad->x]; new_pid = pid_sup[j + ad->x]; pid_corr[j + ad->x] = new_pid; if (old_pid > 0) { /* merge */ /* update left side of the current row */ for (k = 0; k < j; k++) { if (pid_corr[k + ad->x] == old_pid) pid_corr[k + ad->x] = new_pid; } /* update right side of the previous row */ for (k = j + 1; k < ad->cl; k++) { if (pid_sup[k + ad->x] == old_pid) pid_sup[k + ad->x] = new_pid; } pst[new_pid].count += pst[old_pid].count; pst[old_pid].count = 0; if (old_pid == pid) pid--; } else { pst[new_pid].count++; } } connected = 1; } if (!connected) { /* start new patch */ npatch++; pid++; pid_corr[j + ad->x] = pid; if (pid >= nalloc) { pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst)); for (k = nalloc; k < pid + incr; k++) pst[k].count = 0; nalloc = pid + incr; } pst[pid].count = 1; pst[pid].type.t = CELL_TYPE; pst[pid].type.val.c = corrCell; } precCell = corrCell; } } if (npatch > 0) { double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2; double area_p; double cell_size_m; double min, max; /* calculate distance */ G_begin_distance_calculations(); /* EW Dist at North edge */ EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north); /* EW Dist at South Edge */ EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south); /* NS Dist at East edge */ NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south); /* NS Dist at West edge */ NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south); cell_size_m = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) * (((NS_DIST1 + NS_DIST2) / 2) / hd.rows); /* get min and max patch size */ min = 1.0 / 0.0; /* inf */ max = -1.0 / 0.0; /* -inf */ for (old_pid = 1; old_pid <= pid; old_pid++) { if (pst[old_pid].count > 0) { area_p = cell_size_m * pst[old_pid].count / 10000; if (min > area_p) min = area_p; if (max < area_p) max = area_p; } } *result = max - min; } else Rast_set_d_null_value(result, 1); if (masked) { close(mask_fd); G_free(mask_buf); G_free(mask_sup); } G_free(buf_null); G_free(pid_corr); G_free(pid_sup); G_free(pst); return RLI_OK; }
/* ************************************************************************* */ void rast3d_cross_section(void *map,RASTER3D_Region region, int elevfd, int outfd) { int col, row; int rows, cols, depths, typeIntern; FCELL *fcell = NULL; DCELL *dcell = NULL; void *elevrast; void *ptr; int intvalue; float fvalue; double dvalue; double elevation = 0; double north, east; struct Cell_head window; Rast_get_window(&window); rows = region.rows; cols = region.cols; depths = region.depths; /*Typ of the RASTER3D Tile */ typeIntern = Rast3d_tile_type_map(map); /*Allocate mem for the output maps row */ if (typeIntern == FCELL_TYPE) fcell = Rast_allocate_f_buf(); else if (typeIntern == DCELL_TYPE) dcell = Rast_allocate_d_buf(); /*Mem for the input map row */ elevrast = Rast_allocate_buf(globalElevMapType); for (row = 0; row < rows; row++) { G_percent(row, rows - 1, 10); /*Read the input map row */ Rast_get_row(elevfd, elevrast, row, globalElevMapType); for (col = 0, ptr = elevrast; col < cols; col++, ptr = G_incr_void_ptr(ptr, Rast_cell_size(globalElevMapType))) { if (Rast_is_null_value(ptr, globalElevMapType)) { if (typeIntern == FCELL_TYPE) Rast_set_null_value(&fcell[col], 1, FCELL_TYPE); else if (typeIntern == DCELL_TYPE) Rast_set_null_value(&dcell[col], 1, DCELL_TYPE); continue; } /*Read the elevation value */ if (globalElevMapType == CELL_TYPE) { intvalue = *(CELL *) ptr; elevation = intvalue; } else if (globalElevMapType == FCELL_TYPE) { fvalue = *(FCELL *) ptr; elevation = fvalue; } else if (globalElevMapType == DCELL_TYPE) { dvalue = *(DCELL *) ptr; elevation = dvalue; } /* Compute the coordinates */ north = Rast_row_to_northing((double)row + 0.5, &window); east = Rast_col_to_easting((double)col + 0.5, &window); /* Get the voxel value */ if (typeIntern == FCELL_TYPE) Rast3d_get_region_value(map, north, east, elevation, &fcell[col], FCELL_TYPE); if (typeIntern == DCELL_TYPE) Rast3d_get_region_value(map, north, east, elevation, &dcell[col], DCELL_TYPE); } /*Write the data to the output map */ if (typeIntern == FCELL_TYPE) Rast_put_f_row(outfd, fcell); if (typeIntern == DCELL_TYPE) Rast_put_d_row(outfd, dcell); } G_debug(3, "\nDone\n"); /*Free the mem */ if (elevrast) G_free(elevrast); if (dcell) G_free(dcell); if (fcell) G_free(fcell); }
int COGRR1(double x_or, double y_or, double z_or, int n_rows, int n_cols, int n_levs, int n_points, struct quadruple *points, struct point_3d skip_point) /*C C INTERPOLATION BY FUNCTIONAL METHOD : TPS + complete regul. c */ { int secpar_loop(); static double *w2 = NULL; static double *wz2 = NULL; static double *wz1 = NULL; double amaxa; double stepix, stepiy, stepiz, RO, xx, yy, zz, xg, yg, zg, xx2; double wm, dx, dy, dz, dxx, dyy, dxy, dxz, dyz, dzz, h, bmgd1, bmgd2, etar, zcon, r, ww, wz, r2, hcell, zzcell2, etarcell, rcell, wwcell, zzcell; double x_crs,x_crsd,x_crsdd,x_crsdr2; int n1, k1, k2, k, i1, l, l1, n4, n5, m, i; int NGST, LSIZE, ngstc, nszc, ngstr, nszr, ngstl, nszl; int POINT(); int ind, ind1; static int first_time_z = 1; off_t offset, offset1, offset2; int bmask = 1; static FCELL *cell = NULL; int cond1 = (gradient != NULL) || (aspect1 != NULL) || (aspect2 != NULL); int cond2 = (ncurv != NULL) || (gcurv != NULL) || (mcurv != NULL); #define CEULER .57721566 /* C c character*32 fncdsm c normalization c */ offset1 = nsizr * nsizc; stepix = ew_res / dnorm; stepiy = ns_res / dnorm; stepiz = tb_res / dnorm; if (!w2) { if (!(w2 = (double *)G_malloc(sizeof(double) * (KMAX2 + 1)))) { clean(); G_fatal_error(_("Not enough memory for %s"), "w2"); } } if (!wz2) { if (!(wz2 = (double *)G_malloc(sizeof(double) * (KMAX2 + 1)))) { clean(); G_fatal_error(_("Not enough memory for %s"), "wz2"); } } if (!wz1) { if (!(wz1 = (double *)G_malloc(sizeof(double) * (KMAX2 + 1)))) { clean(); G_fatal_error(_("Not enough memory for %s"), "wz1"); } } if (cell == NULL) cell = Rast_allocate_f_buf(); for (i = 1; i <= n_points; i++) { points[i - 1].x = (points[i - 1].x - x_or) / dnorm; points[i - 1].y = (points[i - 1].y - y_or) / dnorm; points[i - 1].z = (points[i - 1].z - z_or) / dnorm; } if (cv) { skip_point.x = (skip_point.x - x_or) / dnorm; skip_point.y = (skip_point.y - y_or) / dnorm; skip_point.z = (skip_point.z - z_or) / dnorm; } n1 = n_points + 1; /* C C GENERATION OF MATRIX C C FIRST COLUMN C */ A[1] = 0.; for (k = 1; k <= n_points; k++) { i1 = k + 1; A[i1] = 1.; } /* C C OTHER COLUMNS C */ RO = rsm; for (k = 1; k <= n_points; k++) { k1 = k * n1 + 1; k2 = k + 1; i1 = k1 + k; if (rsm < 0.) { /*indicates variable smoothing */ A[i1] = points[k - 1].sm; } else { A[i1] = RO; /* constant smoothing */ } for (l = k2; l <= n_points; l++) { xx = points[k - 1].x - points[l - 1].x; yy = points[k - 1].y - points[l - 1].y; zz = points[k - 1].z - points[l - 1].z; r = sqrt(xx * xx + yy * yy + zz * zz); etar = (fi * r) / 2.; if (etar == 0.) { /* printf ("ident. points in segm. \n"); printf ("x[%d]=%lf,x[%d]=%lf,y[%d]=%lf,y[%d]=%lf\n", k - 1, points[k - 1].x, l - 1, points[l - 1].x, k - 1, points[k - 1].y, l - 1, points[l - 1].y); */ } i1 = k1 + l; A[i1] = crs(etar); } } /* C C SYMMETRISATION C */ amaxa = 1.; for (k = 1; k <= n1; k++) { k1 = (k - 1) * n1; k2 = k + 1; for (l = k2; l <= n1; l++) { m = (l - 1) * n1 + k; A[m] = A[k1 + l]; amaxa = amax1(A[m], amaxa); } } /* C RIGHT SIDE C */ n4 = n1 * n1 + 1; A[n4] = 0.; for (l = 1; l <= n_points; l++) { l1 = n4 + l; A[l1] = points[l - 1].w; } n5 = n1 * (n1 + 1); for (i = 1; i <= n5; i++) A[i] = A[i] / amaxa; /* SOLVING OF SYSTEM */ if (LINEQS(n1, n1, 1, &NERROR, &DETERM)) { for (k = 1; k <= n_points; k++) { l = n4 + k; b[k] = A[l]; } b[n_points + 1] = A[n4]; POINT(n_points, points, skip_point); if (cv) return 1; if (devi != NULL && sig1 == 1) return 1; /* C C INTERPOLATION * MOST INNER LOOPS ! C */ NGST = 1; LSIZE = 0; ngstc = (int)(x_or / ew_res + 0.5) + 1; nszc = ngstc + n_cols - 1; ngstr = (int)(y_or / ns_res + 0.5) + 1; nszr = ngstr + n_rows - 1; ngstl = (int)(z_or / tb_res + 0.5) + 1; nszl = ngstl + n_levs - 1; /* fprintf(stderr," Progress percentage for each segment ..." ); */ /*fprintf(stderr,"Before loops,ngstl = %d,nszl =%d\n",ngstl,nszl); */ for (i = ngstl; i <= nszl; i++) { /*fprintf(stderr,"level=%d\n",i); */ /* G_percent(i, nszl, 2); */ offset = offset1 * (i - 1); /* levels offset */ zg = (i - ngstl) * stepiz; for (m = 1; m <= n_points; m++) { wz = zg - points[m - 1].z; wz1[m] = wz; wz2[m] = wz * wz; } for (k = ngstr; k <= nszr; k++) { yg = (k - ngstr) * stepiy; for (m = 1; m <= n_points; m++) { wm = yg - points[m - 1].y; w[m] = wm; w2[m] = wm * wm; } if ((cellinp != NULL) && (cellout != NULL) && (i == ngstl)) Rast_get_f_row(fdcell, cell, n_rows_in - k); for (l = ngstc; l <= nszc; l++) { LSIZE = LSIZE + 1; if (maskmap != NULL) bmask = BM_get(bitmask, l - 1, k - 1); /*bug fix 02/03/00 jh */ xg = (l - ngstc) * stepix; ww = 0.; wwcell = 0.; dx = 0.; dy = 0.; dz = 0.; dxx = 0.; dxy = 0.; dxz = 0.; dyy = 0.; dyz = 0.; dzz = 0.; /* compute everything for area which is not masked out and where cross_input map doesn't have nulls */ if (bmask == 1 && !(cell && Rast_is_f_null_value(&cell[l - 1]))) { h = b[n1]; hcell = b[n1]; for (m = 1; m <= n_points; m++) { xx = xg - points[m - 1].x; xx2 = xx * xx; if ((cellinp != NULL) && (cellout != NULL) && (i == ngstl)) { zcon = (double)(cell[l - 1] * zmult - z_or) - z_orig_in * zmult; /* bug fix 02/03/00 jh */ zcon = zcon / dnorm; zzcell = zcon - points[m - 1].z; zzcell2 = zzcell * zzcell; rcell = sqrt(xx2 + w2[m] + zzcell2); etarcell = (fi * rcell) / 2.; hcell = hcell + b[m] * crs(etarcell); } r2 = xx2 + w2[m] + wz2[m]; r = sqrt(r2); etar = (fi * r) / 2.; crs_full( etar,fi, &x_crs, cond1?&x_crsd:NULL, cond2?&x_crsdr2:NULL, cond2?&x_crsdd:NULL ); h = h + b[m] * x_crs; if(cond1) { bmgd1 = b[m] * x_crsd; dx = dx + bmgd1 * xx; dy = dy + bmgd1 * w[m]; dz = dz + bmgd1 * wz1[m]; } if(cond2) { bmgd2 = b[m] * x_crsdd; bmgd1 = b[m] * x_crsdr2; dyy = dyy + bmgd2 * w2[m] + bmgd1 * w2[m]; dzz = dzz + bmgd2 * wz2[m] + bmgd1 * wz2[m]; dxy = dxy + bmgd2 * xx * w[m] + bmgd1 * xx * w[m]; dxz = dxz + bmgd2 * xx * wz1[m] + bmgd1 * xx * wz1[m]; dyz = dyz + bmgd2 * w[m] * wz1[m] + bmgd1 * w[m] * wz1[m]; } } ww = h + wmin; if ((cellinp != NULL) && (cellout != NULL) && (i == ngstl)) wwcell = hcell + wmin; az[l] = ww; if (first_time_z) { first_time_z = 0; zmaxac = zminac = ww; if ((cellinp != NULL) && (cellout != NULL) && (i == ngstl)) zmaxacell = zminacell = wwcell; } zmaxac = amax1(ww, zmaxac); zminac = amin1(ww, zminac); if ((cellinp != NULL) && (cellout != NULL) && (i == ngstl)) { zmaxacell = amax1(wwcell, zmaxacell); zminacell = amin1(wwcell, zminacell); } if ((ww > wmax + 0.1 * (wmax - wmin)) || (ww < wmin - 0.1 * (wmax - wmin))) { static int once = 0; if (!once) { once = 1; fprintf(stderr, "WARNING:\n"); fprintf(stderr, "Overshoot -- increase in tension suggested.\n"); fprintf(stderr, "Overshoot occurs at (%d,%d,%d) cell\n", l, k, i); fprintf(stderr, "The w-value is %lf, wmin is %lf,wmax is %lf\n", ww, wmin, wmax); } } } /* skip here if you are in masked area, ww should be 0 */ az[l] = ww; adx[l] = dx; ady[l] = dy; adz[l] = dz; /* printf("\n %f", ww); */ adxx[l] = dxx; adxy[l] = dxy; adxz[l] = dxz; adyy[l] = dyy; adyz[l] = dyz; adzz[l] = dzz; if ((gradient != NULL) || (aspect1 != NULL) || (aspect2 != NULL) || (ncurv != NULL) || (gcurv != NULL) || (mcurv != NULL)) if (!(secpar_loop(ngstc, nszc, l))) { clean(); G_fatal_error(_("Secpar_loop failed")); } if ((cellinp != NULL) && (cellout != NULL) && (i == ngstl)) { zero_array_cell[l - 1] = (FCELL) (wwcell); } if (outz != NULL) { zero_array1[l - 1] = (float)(az[l] * sciz); } if (gradient != NULL) { zero_array2[l - 1] = (float)(adx[l]); } if (aspect1 != NULL) { zero_array3[l - 1] = (float)(ady[l]); } if (aspect2 != NULL) { zero_array4[l - 1] = (float)(adz[l]); } if (ncurv != NULL) { zero_array5[l - 1] = (float)(adxx[l]); } if (gcurv != NULL) { zero_array6[l - 1] = (float)(adyy[l]); } if (mcurv != NULL) { zero_array7[l - 1] = (float)(adxy[l]); } } /* columns */ ind = nsizc * (k - 1) + (ngstc - 1); ind1 = ngstc - 1; offset2 = offset + ind; /* rows*cols offset */ if ((cellinp != NULL) && (cellout != NULL) && (i == ngstl)) { G_fseek(Tmp_fd_cell, ((off_t)ind * sizeof(FCELL)), 0); if (! (fwrite (zero_array_cell + ind1, sizeof(FCELL), nszc - ngstc + 1, Tmp_fd_cell))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } if (outz != NULL) { G_fseek(Tmp_fd_z, (off_t)(offset2 * sizeof(float)), 0); if (! (fwrite (zero_array1 + ind1, sizeof(float), nszc - ngstc + 1, Tmp_fd_z))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } if (gradient != NULL) { G_fseek(Tmp_fd_dx, (off_t)(offset2 * sizeof(float)), 0); if (! (fwrite (zero_array2 + ind1, sizeof(float), nszc - ngstc + 1, Tmp_fd_dx))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } if (aspect1 != NULL) { G_fseek(Tmp_fd_dy, (off_t)(offset2 * sizeof(float)), 0); if (! (fwrite (zero_array3 + ind1, sizeof(float), nszc - ngstc + 1, Tmp_fd_dy))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } if (aspect2 != NULL) { G_fseek(Tmp_fd_dz, (off_t)(offset2 * sizeof(float)), 0); if (! (fwrite (zero_array4 + ind1, sizeof(float), nszc - ngstc + 1, Tmp_fd_dz))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } if (ncurv != NULL) { G_fseek(Tmp_fd_xx, (off_t)(offset2 * sizeof(float)), 0); if (! (fwrite (zero_array5 + ind1, sizeof(float), nszc - ngstc + 1, Tmp_fd_xx))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } if (gcurv != NULL) { G_fseek(Tmp_fd_yy, (off_t)(offset2 * sizeof(float)), 0); if (! (fwrite (zero_array6 + ind1, sizeof(float), nszc - ngstc + 1, Tmp_fd_yy))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } if (mcurv != NULL) { G_fseek(Tmp_fd_xy, (off_t)(offset2 * sizeof(float)), 0); if (! (fwrite (zero_array7 + ind1, sizeof(float), nszc - ngstc + 1, Tmp_fd_xy))) { clean(); G_fatal_error (_("Not enough disk space--cannot write files")); } } } } } /* falls here if LINEQS() returns 0 */ /* total++; */ /*fprintf(stderr,"wminac=%lf,wmaxac=%lf\n",zminac,zmaxac); */ return 1; }
int OUTGR() { void *cf1, *cf2, *cf3, *cf4, *cf5, *cf6, *cf7; int read_val; FCELL *cell; float *data; int i, iarc, cnt; int bmask = 1; int x, y; float value; if ((cellinp != NULL) && (cellout != NULL)) { cell = Rast_allocate_f_buf(); for (i = 0; i < nsizr; i++) { /* seek to the right row */ G_fseek (Tmp_fd_cell, ((off_t)(nsizr - 1 - i) * nsizc * sizeof(FCELL)), 0); fread(cell, sizeof(FCELL), nsizc, Tmp_fd_cell); Rast_put_f_row(fdcout, cell); } } /*** Initialize output g3d region ***/ current_region.bottom = z_orig_in; current_region.top = nsizl * tb_res_in + z_orig_in; if (!(data = (float *)G_malloc(sizeof(float) * nsizr * nsizc * nsizl))) { clean(); G_fatal_error(_("Out of memory")); } /*** Write elevation results ***/ if (outz != NULL) { cf1 = Rast3d_open_new_opt_tile_size(outz, RASTER3D_USE_CACHE_DEFAULT, ¤t_region, FCELL_TYPE, 32); if (cf1 == NULL) { clean(); G_fatal_error(_("Unable to open %s for writing"), outz); } /* seek to the beginning */ G_fseek(Tmp_fd_z, 0L, 0); /* Read data in from temp file */ read_val = fread(data, sizeof(float), nsizr * nsizc * nsizl, Tmp_fd_z); if (read_val < 0) { clean(); G_fatal_error(_("Unable to read data from temp file")); } cnt = 0; for (iarc = 0; iarc < nsizl; iarc++) { for (y = nsizr - 1; y >= 0; y--) { /* changed by AV */ for (x = 0; x < nsizc; x++) { if (maskmap != NULL) bmask = BM_get(bitmask, x, nsizr - y - 1); else bmask = 1; value = data[cnt]; if (!bmask) Rast3d_set_null_value(&value, 1, FCELL_TYPE); if (Rast3d_put_float(cf1, x, y, iarc, value) == 0) { clean(); G_fatal_error( _("Error writing cell (%d,%d,%d) with value %f"), x, y, iarc, value); } cnt++; } } } /* Close the file */ if (Rast3d_close(cf1) == 0) { clean(); G_fatal_error(_("Error closing output file %s"), outz); } else G_message(_("3D raster map <%s> created"), outz); } /*** Write out the gradient results ***/ if (gradient != NULL) { cf2 = Rast3d_open_new_opt_tile_size(gradient, RASTER3D_USE_CACHE_DEFAULT, ¤t_region, FCELL_TYPE, 32); if (cf2 == NULL) { clean(); G_fatal_error(_("Unable to open %s for writing"), gradient); } /* seek to the beginning */ G_fseek(Tmp_fd_dx, 0L, 0); /* Read data in from temp file */ read_val = fread(data, sizeof(float), nsizr * nsizc * nsizl, Tmp_fd_dx); if (read_val < 0) { clean(); G_fatal_error(_("Unable to read data from temp file")); } cnt = 0; for (iarc = 0; iarc < nsizl; iarc++) { for (y = nsizr - 1; y >= 0; y--) { /* changed by AV */ for (x = 0; x < nsizc; x++) { if (maskmap != NULL) bmask = BM_get(bitmask, x, nsizr - y - 1); else bmask = 1; value = data[cnt]; if (!bmask) Rast3d_set_null_value(&value, 1, FCELL_TYPE); if (Rast3d_put_float(cf2, x, y, iarc, value) == 0) { clean(); G_fatal_error( _("Error writing cell (%d,%d,%d) with value %f"), x, y, iarc, value); } cnt++; } } } /* Close the file */ if (Rast3d_close(cf2) == 0) { clean(); G_fatal_error(_("Error closing output file %s"), gradient); } else G_message(_("3D raster map <%s> created"), gradient); } /*** Write out aspect1 results ***/ if (aspect1 != NULL) { cf3 = Rast3d_open_new_opt_tile_size(aspect1, RASTER3D_USE_CACHE_DEFAULT, ¤t_region, FCELL_TYPE, 32); if (cf3 == NULL) { clean(); G_fatal_error(_("Unable to open %s for writing"), aspect1); } /* seek to the beginning */ G_fseek(Tmp_fd_dy, 0L, 0); /* Read data in from temp file */ read_val = fread(data, sizeof(float), nsizr * nsizc * nsizl, Tmp_fd_dy); if (read_val < 0) { clean(); G_fatal_error(_("Unable to read data from temp file")); } cnt = 0; for (iarc = 0; iarc < nsizl; iarc++) { for (y = nsizr - 1; y >= 0; y--) { /* changed by AV */ for (x = 0; x < nsizc; x++) { if (maskmap != NULL) bmask = BM_get(bitmask, x, nsizr - y - 1); else bmask = 1; value = data[cnt] * 180 / M_PI; if (!bmask) Rast3d_set_null_value(&value, 1, FCELL_TYPE); if (Rast3d_put_float(cf3, x, y, iarc, value) == 0) { clean(); G_fatal_error( _("Error writing cell (%d,%d,%d) with value %f"), x, y, iarc, value); } cnt++; } } } /* Close the file */ if (Rast3d_close(cf3) == 0) { clean(); G_fatal_error(_("Error closing output file %s"), aspect1); } else G_message(_("3D raster map <%s> created"), aspect1); } /*** Write out aspect2 results ***/ if (aspect2 != NULL) { cf4 = Rast3d_open_new_opt_tile_size(aspect2, RASTER3D_USE_CACHE_DEFAULT, ¤t_region, FCELL_TYPE, 32); if (cf4 == NULL) { clean(); G_fatal_error(_("Unable to open %s for writing"), aspect2); } /* seek to the beginning */ G_fseek(Tmp_fd_dz, 0L, 0); /* Read data in from temp file */ read_val = fread(data, sizeof(float), nsizr * nsizc * nsizl, Tmp_fd_dz); if (read_val < 0) { clean(); G_fatal_error(_("Unable to read data from temp file")); } cnt = 0; for (iarc = 0; iarc < nsizl; iarc++) { for (y = nsizr - 1; y >= 0; y--) { /* changed by AV */ for (x = 0; x < nsizc; x++) { if (maskmap != NULL) bmask = BM_get(bitmask, x, nsizr - y - 1); else bmask = 1; value = data[cnt] * 180 / M_PI; if (!bmask) Rast3d_set_null_value(&value, 1, FCELL_TYPE); if (Rast3d_put_float(cf4, x, y, iarc, value) == 0) { clean(); G_fatal_error( _("Error writing cell (%d,%d,%d) with value %f"), x, y, iarc, value); } cnt++; } } } /* Close the file */ if (Rast3d_close(cf4) == 0) { clean(); G_fatal_error(_("Error closing output file %s"), aspect2); } else G_message(_("3D raster map <%s> created"), aspect2); } /*** Write out ncurv results ***/ if (ncurv != NULL) { cf5 = Rast3d_open_new_opt_tile_size(ncurv, RASTER3D_USE_CACHE_DEFAULT, ¤t_region, FCELL_TYPE, 32); if (cf5 == NULL) { clean(); G_fatal_error(_("Unable to open %s for writing"), ncurv); } /* seek to the beginning */ G_fseek(Tmp_fd_xx, 0L, 0); /* Read data in from temp file */ read_val = fread(data, sizeof(float), nsizr * nsizc * nsizl, Tmp_fd_xx); if (read_val < 0) { clean(); G_fatal_error(_("Unable to read data from temp file")); } cnt = 0; for (iarc = 0; iarc < nsizl; iarc++) { for (y = nsizr - 1; y >= 0; y--) { /* changed by AV */ for (x = 0; x < nsizc; x++) { if (maskmap != NULL) bmask = BM_get(bitmask, x, nsizr - y - 1); else bmask = 1; value = data[cnt]; if (!bmask) Rast3d_set_null_value(&value, 1, FCELL_TYPE); if (Rast3d_put_float(cf5, x, y, iarc, value) == 0) { clean(); G_fatal_error( _("Error writing cell (%d,%d,%d) with value %f"), x, y, iarc, value); } cnt++; } } } /* Close the file */ if (Rast3d_close(cf5) == 0) { clean(); G_fatal_error(_("Error closing output file %s"), ncurv); } else G_message(_("3D raster map <%s> created"), ncurv); } /*** Write out gcurv results ***/ if (gcurv != NULL) { cf6 = Rast3d_open_new_opt_tile_size(gcurv, RASTER3D_USE_CACHE_DEFAULT, ¤t_region, FCELL_TYPE, 32); if (cf6 == NULL) { clean(); G_fatal_error(_("Unable to open %s for writing"), gcurv); } /* seek to the beginning */ G_fseek(Tmp_fd_yy, 0L, 0); /* Read data in from temp file */ read_val = fread(data, sizeof(float), nsizr * nsizc * nsizl, Tmp_fd_yy); if (read_val < 0) { clean(); G_fatal_error(_("Unable to read data from temp file")); } cnt = 0; for (iarc = 0; iarc < nsizl; iarc++) { for (y = nsizr - 1; y >= 0; y--) { /* changed by AV */ for (x = 0; x < nsizc; x++) { if (maskmap != NULL) bmask = BM_get(bitmask, x, nsizr - y - 1); else bmask = 1; value = data[cnt]; if (!bmask) Rast3d_set_null_value(&value, 1, FCELL_TYPE); if (Rast3d_put_float(cf6, x, y, iarc, value) == 0) { clean(); G_fatal_error( _("Error writing cell (%d,%d,%d) with value %f"), x, y, iarc, value); } cnt++; } } } /* Close the file */ if (Rast3d_close(cf6) == 0) { clean(); G_fatal_error(_("Error closing output file %s"), gcurv); } else G_message(_("3D raster map <%s> created"), gcurv); } /*** Write mcurv results ***/ if (mcurv != NULL) { cf7 = Rast3d_open_new_opt_tile_size(mcurv, RASTER3D_USE_CACHE_DEFAULT, ¤t_region, FCELL_TYPE, 32); if (cf7 == NULL) { clean(); G_fatal_error(_("Unable to open %s for writing"), mcurv); } /* seek to the beginning */ G_fseek(Tmp_fd_xy, 0L, 0); /* Read data in from temp file */ read_val = fread(data, sizeof(float), nsizr * nsizc * nsizl, Tmp_fd_xy); if (read_val < 0) { clean(); G_fatal_error(_("Unable to read data from temp file")); } cnt = 0; for (iarc = 0; iarc < nsizl; iarc++) { for (y = nsizr - 1; y >= 0; y--) { /* changed by AV */ for (x = 0; x < nsizc; x++) { if (maskmap != NULL) bmask = BM_get(bitmask, x, nsizr - y - 1); else bmask = 1; value = data[cnt]; if (!bmask) Rast3d_set_null_value(&value, 1, FCELL_TYPE); if (Rast3d_put_float(cf7, x, y, iarc, value) == 0) { clean(); G_fatal_error( _("Error writing cell (%d,%d,%d) with value %f"), x, y, iarc, value); } cnt++; } } } /* Close the file */ if (Rast3d_close(cf7) == 0) { clean(); G_fatal_error(_("Error closing output file %s"), mcurv); } else G_message(_("3D raster map <%s> created"), mcurv); } G_free(data); return 1; }
int extract_points(int z_flag) { struct line_pnts *points = Vect_new_line_struct(); CELL *cellbuf; FCELL *fcellbuf; DCELL *dcellbuf; int row, col; double x, y; int count; switch (data_type) { case CELL_TYPE: cellbuf = Rast_allocate_c_buf(); break; case FCELL_TYPE: fcellbuf = Rast_allocate_f_buf(); break; case DCELL_TYPE: dcellbuf = Rast_allocate_d_buf(); break; } G_message(_("Extracting points...")); count = 1; for (row = 0; row < cell_head.rows; row++) { G_percent(row, n_rows, 2); y = Rast_row_to_northing((double)(row + .5), &cell_head); switch (data_type) { case CELL_TYPE: Rast_get_c_row(input_fd, cellbuf, row); break; case FCELL_TYPE: Rast_get_f_row(input_fd, fcellbuf, row); break; case DCELL_TYPE: Rast_get_d_row(input_fd, dcellbuf, row); break; } for (col = 0; col < cell_head.cols; col++) { int cat, val; double dval; x = Rast_col_to_easting((double)(col + .5), &cell_head); switch (data_type) { case CELL_TYPE: if (Rast_is_c_null_value(cellbuf + col)) continue; val = cellbuf[col]; dval = val; break; case FCELL_TYPE: if (Rast_is_f_null_value(fcellbuf + col)) continue; dval = fcellbuf[col]; break; case DCELL_TYPE: if (Rast_is_d_null_value(dcellbuf + col)) continue; dval = dcellbuf[col]; break; } /* value_flag is used only for CELL type */ cat = (value_flag) ? val : count; Vect_reset_line(points); Vect_reset_cats(Cats); Vect_cat_set(Cats, 1, cat); Vect_append_point(points, x, y, dval); Vect_write_line(&Map, GV_POINT, points, Cats); if ((driver != NULL) && !value_flag) { insert_value(cat, val, dval); } count++; } } G_percent(row, n_rows, 2); switch (data_type) { case CELL_TYPE: G_free(cellbuf); break; case FCELL_TYPE: G_free(fcellbuf); break; case DCELL_TYPE: G_free(dcellbuf); break; } Vect_destroy_line_struct(points); return (1); }
int calculateF(int fd, area_des ad, struct Cell_head hd, double *result) { FCELL *buf; FCELL *buf_sup; FCELL corrCell; FCELL precCell; FCELL supCell; int i, j; int mask_fd = -1, *mask_buf; int ris = 0; int masked = FALSE; long npatch = 0; long tot = 0; long zero = 0; long uno = 1; long idCorr = 0; long lastId = 0; long doppi = 0; long *mask_patch_sup; long *mask_patch_corr; double indice = 0; double area = 0; /*if all cells are null area=0 */ double areaCorrect = 0; double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2; avlID_tree albero = NULL; avlID_table *array; /* open mask if needed */ if (ad->mask == 1) { if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0) return RLI_ERRORE; mask_buf = G_malloc(ad->cl * sizeof(int)); if (mask_buf == NULL) { G_fatal_error("malloc mask_buf failed"); return RLI_ERRORE; } masked = TRUE; } mask_patch_sup = G_malloc(ad->cl * sizeof(long)); if (mask_patch_sup == NULL) { G_fatal_error("malloc mask_patch_sup failed"); return RLI_ERRORE; } mask_patch_corr = G_malloc(ad->cl * sizeof(long)); if (mask_patch_corr == NULL) { G_fatal_error("malloc mask_patch_corr failed"); return RLI_ERRORE; } buf_sup = Rast_allocate_f_buf(); if (buf_sup == NULL) { G_fatal_error("malloc buf_sup failed"); return RLI_ERRORE; } buf = Rast_allocate_f_buf(); if (buf == NULL) { G_fatal_error("malloc buf failed"); return RLI_ERRORE; } Rast_set_f_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */ for (i = 0; i < ad->cl; i++) { mask_patch_sup[i] = 0; mask_patch_corr[i] = 0; } /*for each raster row */ for (j = 0; j < ad->rl; j++) { if (j > 0) { buf_sup = RLI_get_fcell_raster_row(fd, j - 1 + ad->y, ad); } buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad); if (masked) { if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) { G_fatal_error("mask read failed"); return RLI_ERRORE; } } Rast_set_f_null_value(&precCell, 1); for (i = 0; i < ad->cl; i++) { /*for each cell in the row */ corrCell = buf[i + ad->x]; if (((masked) && (mask_buf[i + ad->x] == 0))) { Rast_set_f_null_value(&corrCell, 1); } if (!(Rast_is_null_value(&corrCell, FCELL_TYPE))) { area++; if (i > 0) precCell = buf[i - 1 + ad->x]; if (j == 0) Rast_set_f_null_value(&supCell, 1); else supCell = buf_sup[i + ad->x]; if (corrCell != precCell) { if (corrCell != supCell) { /*new patch */ if (idCorr == 0) { /*first patch */ lastId = 1; idCorr = 1; mask_patch_corr[i] = idCorr; } else { /*not first patch */ /* put in the tree previous value */ if (albero == NULL) { albero = avlID_make(idCorr, uno); if (albero == NULL) { G_fatal_error("avlID_make error"); return RLI_ERRORE; } npatch++; } else { /*tree not empty */ ris = avlID_add(&albero, idCorr, uno); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error ("avlID_add unknown error"); return RLI_ERRORE; } } } lastId++; idCorr = lastId; mask_patch_corr[i] = idCorr; } } else { /*current cell and upper cell are equal */ if ((corrCell == precCell) && (mask_patch_sup[i] != mask_patch_corr[i - 1])) { long r = 0; long del = mask_patch_sup[i]; r = avlID_sub(&albero, del); if (r == 0) { G_fatal_error("avlID_sub error"); return RLI_ERRORE; } /*Remove one patch because it makes part of a patch already found */ ris = avlID_add(&albero, idCorr, uno); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error("avlID_add unknown error"); return RLI_ERRORE; } } r = i; while (i < ad->cl) { if (mask_patch_sup[r] == del) { mask_patch_sup[r] = idCorr; } else { r = ad->cl + 1; } } } if (albero == NULL) { albero = avlID_make(idCorr, uno); if (albero == NULL) { G_fatal_error("avlID_make error"); return RLI_ERRORE; } npatch++; } else { /*the tree (albero) isn't null */ ris = avlID_add(&albero, idCorr, uno); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error("avlID_add unknown error"); return RLI_ERRORE; } } } idCorr = mask_patch_sup[i]; mask_patch_corr[i] = idCorr; } } else { /*current cell and previous cell are equal */ if ((corrCell == supCell) && (mask_patch_sup[i] != mask_patch_corr[i - 1])) { int l; mask_patch_corr[i] = mask_patch_sup[i]; l = i - 1; while (l >= 0) { if (mask_patch_corr[l] == idCorr) { mask_patch_corr[l] = mask_patch_sup[i]; l--; } else { l = (-1); } } lastId--; idCorr = mask_patch_sup[i]; } else { mask_patch_corr[i] = idCorr; } } } else { /*null cell or cell not to consider */ mask_patch_corr[i] = 0; } } mask_patch_sup = mask_patch_corr; } if (area != 0) { if (albero == NULL) { albero = avlID_make(idCorr, uno); if (albero == NULL) { G_fatal_error("avlID_make error"); return RLI_ERRORE; } npatch++; } else { ris = avlID_add(&albero, idCorr, uno); switch (ris) { case AVL_ERR: { G_fatal_error("avlID_add error"); return RLI_ERRORE; } case AVL_ADD: { npatch++; break; } case AVL_PRES: { break; } default: { G_fatal_error("avlID_add unknown error"); return RLI_ERRORE; } } } array = G_malloc(npatch * sizeof(avlID_tableRow)); if (array == NULL) { G_fatal_error("malloc array failed"); return RLI_ERRORE; } tot = avlID_to_array(albero, zero, array); if (tot != npatch) { G_warning ("avlID_to_array unaspected value. the result could be wrong"); return RLI_ERRORE; } for (i = 0; i < npatch; i++) { if (array[i]->tot == 0) doppi++; } npatch = npatch - doppi; /*calculate distance */ G_begin_distance_calculations(); /* EW Dist at North edge */ EW_DIST1 = G_distance(hd.east, hd.north, hd.west, hd.north); /* EW Dist at South Edge */ EW_DIST2 = G_distance(hd.east, hd.south, hd.west, hd.south); /* NS Dist at East edge */ NS_DIST1 = G_distance(hd.east, hd.north, hd.east, hd.south); /* NS Dist at West edge */ NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south); areaCorrect = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) * (((NS_DIST1 + NS_DIST2) / 2) / hd.rows) * (area); indice = areaCorrect / npatch; G_free(array); } else indice = (double)(0); *result = indice; if (masked) G_free(mask_buf); G_free(mask_patch_corr); return RLI_OK; }