int G3d_readRange(const char *name, const char *mapset, struct FPRange *drange) /* adapted from G_read_fp_range */ { int fd; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; char buf[GNAME_MAX + sizeof(G3D_DIRECTORY) + 2], buf2[GMAPSET_MAX + sizeof(G3D_RANGE_ELEMENT) + 2]; char xdr_buf[100]; DCELL dcell1, dcell2; XDR xdr_str; G_init_fp_range(drange); fd = -1; if (G__name_is_fully_qualified(name, xname, xmapset)) { sprintf(buf, "%s/%s", G3D_DIRECTORY, xname); sprintf(buf2, "%s@%s", G3D_RANGE_ELEMENT, xmapset); /* == range@mapset */ } else { sprintf(buf, "%s/%s", G3D_DIRECTORY, name); sprintf(buf2, "%s", G3D_RANGE_ELEMENT); } if (G_find_file2(buf, buf2, mapset)) { fd = G_open_old(buf, buf2, mapset); if (fd < 0) goto error; if (read(fd, xdr_buf, 2 * G3D_XDR_DOUBLE_LENGTH) != 2 * G3D_XDR_DOUBLE_LENGTH) return 2; xdrmem_create(&xdr_str, xdr_buf, (u_int) G3D_XDR_DOUBLE_LENGTH * 2, XDR_DECODE); /* if the f_range file exists, but empty */ if (!xdr_double(&xdr_str, &dcell1) || !xdr_double(&xdr_str, &dcell2)) goto error; G_update_fp_range(dcell1, drange); G_update_fp_range(dcell2, drange); close(fd); return 1; } error: if (fd > 0) close(fd); G_warning("can't read range file for [%s in %s]", name, mapset); return -1; }
int G_rename(const char *element, const char *oldname, const char *newname) { const char *mapset; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; char from[512], to[512]; /* name in mapset legal only if mapset is current mapset */ mapset = G_mapset(); if (G__name_is_fully_qualified(oldname, xname, xmapset) && strcmp(mapset, xmapset)) return -1; if (G__name_is_fully_qualified(newname, xname, xmapset) && strcmp(mapset, xmapset)) return -1; /* if file does not exist return 0 */ if (access(G__file_name(from, element, oldname, mapset), 0) != 0) return 0; G__file_name(to, element, newname, mapset); /* return result of rename */ return G_rename_file(from, to) == 0 ? 1 : -1; }
static int writeRange(const char *name, struct FPRange *range) /* adapted from G_write_fp_range */ { char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; char buf[GNAME_MAX + sizeof(G3D_DIRECTORY) + 2], buf2[GMAPSET_MAX + sizeof(G3D_RANGE_ELEMENT) + 2]; char xdr_buf[100]; int fd; XDR xdr_str; if (G__name_is_fully_qualified(name, xname, xmapset)) { sprintf(buf, "%s/%s", G3D_DIRECTORY, xname); sprintf(buf2, "%s@%s", G3D_RANGE_ELEMENT, xmapset); /* == range@mapset */ } else { sprintf(buf, "%s/%s", G3D_DIRECTORY, name); sprintf(buf2, "%s", G3D_RANGE_ELEMENT); } fd = G_open_new(buf, buf2); if (fd < 0) goto error; if (range->first_time) { /* if range hasn't been updated, write empty file meaning NULLs */ close(fd); return 0; } xdrmem_create(&xdr_str, xdr_buf, (u_int) G3D_XDR_DOUBLE_LENGTH * 2, XDR_ENCODE); if (!xdr_double(&xdr_str, &(range->min))) goto error; if (!xdr_double(&xdr_str, &(range->max))) goto error; write(fd, xdr_buf, G3D_XDR_DOUBLE_LENGTH * 2); close(fd); return 0; error: G_remove(buf, buf2); /* remove the old file with this name */ sprintf(buf, "can't write range file for [%s in %s]", name, G_mapset()); G_warning(buf); return -1; }
int g3_find_dsp_file(char *cell, char *file, char *mset) { char element[100], name[GNAME_MAX], mapset[GMAPSET_MAX], tofind[GNAME_MAX]; if (file == NULL || *file == 0) return 0; strcpy(tofind, file); if (G__name_is_fully_qualified(cell, name, mapset)) sprintf(element, "grid3/%s/dsp", name); else sprintf(element, "grid3/%s/dsp", cell); return G_find_file(element, tofind, mset) != NULL; }
char *G__file_name ( char *path, char *element, char *name, char *mapset) { char xname[512]; char xmapset[512]; char *location = G__location_path(); /* * if a name is given, build a file name * must split the name into name, mapset if it is * in the name@mapset format */ if (name && *name && G__name_is_fully_qualified(name, xname, xmapset)) { strcpy(name, xname); sprintf(path,"%s/%s", location, xmapset); } else if (mapset && *mapset) sprintf(path,"%s/%s", location, mapset); else sprintf(path,"%s/%s", location, G_mapset()); G_free (location); if (element && *element) { strcat (path, "/"); strcat (path, element); } if (name && *name) { strcat (path, "/"); strcat (path, name); } /* * return pointer to users 'path' buffer */ return path; }
/* * If windowName == NULL -> G3D_WINDOW_ELEMENT ("$MAPSET/WIND3") * otherwise G3D_WINDOW_DATABASE ("$MAPSET/windows3d/$NAME") */ static void G3d_getFullWindowPath(char *path, const char *windowName) { char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; if (windowName == NULL) { G__file_name(path, "", G3D_WINDOW_ELEMENT, G_mapset()); return; } while (*windowName == ' ') windowName++; if (strchr(windowName, GRASS_DIRSEP) || strchr(windowName, HOST_DIRSEP)) { sprintf(path, windowName); return; } if (G__name_is_fully_qualified(windowName, xname, xmapset)) { G__file_name(path, G3D_WINDOW_DATABASE, xname, xmapset); return; } G__file_name(path, G3D_WINDOW_DATABASE, windowName, G_mapset()); }
int G__open ( char *element, char *name, char *mapset, int mode) { char path[1024]; char xname[512], xmapset[512], *dummy; int G_fd; G__check_gisinit(); /* READ */ if (mode == 0) { if (G__name_is_fully_qualified (name, xname, xmapset)) { if (strcmp (xmapset, mapset) != 0) { fprintf(stderr, "G__open(r): mapset (%s) doesn't match xmapset (%s)\n", mapset,xmapset); return -1; } name = xname; } if ((dummy = G_find_file (element, name, mapset)) == NULL) return -1; G_free (dummy); G__file_name (path, element, name, mapset); G_fd = open (path, 0); #if defined R_GRASS_INTERFACE && defined __MINGW32_VERSION setmode(G_fd, O_BINARY); #endif /* __MINGW32_VERSION && R_GRASS_INTERFACE */ return G_fd; } /* WRITE */ if (mode == 1 || mode == 2) { if (G__name_is_fully_qualified (name, xname, xmapset)) { if (strcmp (xmapset, G_mapset()) != 0) { fprintf(stderr, "G__open(w): xmapset (%s) != G_mapset() (%s)\n", xmapset,G_mapset()); return -1; } name = xname; } if (G_legal_filename(name) == -1) return -1; G__file_name (path, element, name, G_mapset()); if(mode == 1 || access(path,0) != 0) { G__make_mapset_element (element); close (creat (path, 0666)); } G_fd = open (path, mode); #if defined R_GRASS_INTERFACE && defined __MINGW32_VERSION setmode(G_fd, O_BINARY); #endif /* __MINGW32_VERSION && R_GRASS_INTERFACE */ return G_fd; } return -1; }
int main(int argc, char *argv[]) { char group[INAME_LEN], extension[INAME_LEN]; int order; /* ADDED WITH CRS MODIFICATIONS */ char *ipolname; /* name of interpolation method */ int method; int n, i, m, k = 0; int got_file = 0, target_overwrite = 0; char *overstr; struct Cell_head cellhd; struct Option *grp, /* imagery group */ *val, /* transformation order */ *ifile, /* input files */ *ext, /* extension */ *tres, /* target resolution */ *mem, /* amount of memory for cache */ *interpol; /* interpolation method: nearest neighbor, bilinear, cubic */ struct Flag *c, *a; struct GModule *module; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("imagery, rectify"); module->description = _("Rectifies an image by computing a coordinate " "transformation for each pixel in the image based on the " "control points."); grp = G_define_standard_option(G_OPT_I_GROUP); ifile = G_define_standard_option(G_OPT_R_INPUTS); ifile->required = NO; ext = G_define_option(); ext->key = "extension"; ext->type = TYPE_STRING; ext->required = YES; ext->multiple = NO; ext->description = _("Output raster map(s) suffix"); val = G_define_option(); val->key = "order"; val->type = TYPE_INTEGER; val->required = YES; val->description = _("Rectification polynom order (1-3)"); tres = G_define_option(); tres->key = "res"; tres->type = TYPE_DOUBLE; tres->required = NO; tres->description = _("Target resolution (ignored if -c flag used)"); mem = G_define_option(); mem->key = "memory"; mem->type = TYPE_DOUBLE; mem->key_desc = "memory in MB"; mem->required = NO; mem->answer = "300"; mem->description = _("Amount of memory to use in MB"); ipolname = make_ipol_list(); interpol = G_define_option(); interpol->key = "method"; interpol->type = TYPE_STRING; interpol->required = NO; interpol->answer = "nearest"; interpol->options = ipolname; interpol->description = _("Interpolation method to use"); c = G_define_flag(); c->key = 'c'; c->description = _("Use current region settings in target location (def.=calculate smallest area)"); a = G_define_flag(); a->key = 'a'; a->description = _("Rectify all raster maps in group"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* get the method */ for (method = 0; (ipolname = menu[method].name); method++) if (strcmp(ipolname, interpol->answer) == 0) break; if (!ipolname) G_fatal_error(_("<%s=%s> unknown %s"), interpol->key, interpol->answer, interpol->key); interpolate = menu[method].method; G_strip(grp->answer); strcpy(group, grp->answer); strcpy(extension, ext->answer); order = atoi(val->answer); seg_mb = NULL; if (mem->answer) { if (atoi(mem->answer) > 0) seg_mb = mem->answer; } if (!ifile->answers) a->answer = 1; /* force all */ /* Find out how many files on command line */ if (!a->answer) { for (m = 0; ifile->answers[m]; m++) { k = m; } k++; } if (order < 1 || order > MAXORDER) G_fatal_error(_("Invalid order (%d); please enter 1 to %d"), order, MAXORDER); /* determine the number of files in this group */ if (I_get_group_ref(group, &ref) <= 0) G_fatal_error(_("Group <%s> does not exist"), grp->answer); if (ref.nfiles <= 0) { G_important_message(_("Group <%s> contains no raster maps; run i.group"), grp->answer); exit(EXIT_SUCCESS); } ref_list = (int *)G_malloc(ref.nfiles * sizeof(int)); if (a->answer) { for (n = 0; n < ref.nfiles; n++) { ref_list[n] = 1; } } else { char xname[GNAME_MAX], xmapset[GMAPSET_MAX], *name, *mapset; for (n = 0; n < ref.nfiles; n++) ref_list[n] = 0; for (m = 0; m < k; m++) { got_file = 0; if (G__name_is_fully_qualified(ifile->answers[m], xname, xmapset)) { name = xname; mapset = xmapset; } else { name = ifile->answers[m]; mapset = NULL; } got_file = 0; for (n = 0; n < ref.nfiles; n++) { if (mapset) { if (strcmp(name, ref.file[n].name) == 0 && strcmp(mapset, ref.file[n].mapset) == 0) { got_file = 1; ref_list[n] = 1; break; } } else { if (strcmp(name, ref.file[n].name) == 0) { got_file = 1; ref_list[n] = 1; break; } } } if (got_file == 0) err_exit(ifile->answers[m], group); } } /* read the control points for the group */ get_control_points(group, order); /* get the target */ get_target(group); /* Check the GRASS_OVERWRITE environment variable */ if ((overstr = getenv("GRASS_OVERWRITE"))) /* OK ? */ target_overwrite = atoi(overstr); if (!target_overwrite) { /* check if output exists in target location/mapset */ char result[GNAME_MAX]; select_target_env(); for (i = 0; i < ref.nfiles; i++) { if (!ref_list[i]) continue; strcpy(result, ref.file[i].name); strcat(result, extension); if (G_legal_filename(result) < 0) G_fatal_error(_("Extension <%s> is illegal"), extension); if (G_find_cell(result, G_mapset())) { G_warning(_("The following raster map already exists in")); G_warning(_("target LOCATION %s, MAPSET %s:"), G_location(), G_mapset()); G_warning("<%s>", result); G_fatal_error(_("Orthorectification cancelled.")); } } select_current_env(); } else G_debug(1, "Overwriting OK"); /* do not use current region in target location */ if (!c->answer) { double res = -1; if (tres->answer) { if (!((res = atof(tres->answer)) > 0)) G_warning(_("Target resolution must be > 0, ignored")); } /* Calculate smallest region */ if (a->answer) { if (G_get_cellhd(ref.file[0].name, ref.file[0].mapset, &cellhd) < 0) G_fatal_error(_("Unable to read header of raster map <%s>"), ref.file[0].name); } else { if (G_get_cellhd(ifile->answers[0], ref.file[0].mapset, &cellhd) < 0) G_fatal_error(_("Unable to read header of raster map <%s>"), ifile->answers[0]); } georef_window(&cellhd, &target_window, order, res); } G_verbose_message(_("Using region: N=%f S=%f, E=%f W=%f"), target_window.north, target_window.south, target_window.east, target_window.west); exec_rectify(order, extension, interpol->answer); G_done_msg(" "); exit(EXIT_SUCCESS); }