/*! \brief Lowest level open routine. Opens the file <i>name</i> in <i>element</i> ("cell", etc.) in mapset <i>mapset</i> according to the i/o <i>mode</i>. - mode = 0 (read) will look for <i>name</i> in <i>mapset</i> and open the file for read only the file must exist - mode = 1 (write) will create an empty file <i>name</i> in the current mapset and open the file for write only <i>mapset</i> ignored - mode = 2 (read and write) will open a file in the current mapset for reading and writing creating a new file if necessary <i>mapset</i> ignored \param element database element name \param name map file name \param mapset mapset containing map <i>name</i> \param mode r/w mode 0=read, 1=write, 2=read/write \return open file descriptor (int) \return -1 could not open */ static int G__open(const char *element, const char *name, const char *mapset, int mode) { char path[GPATH_MAX]; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; G__check_gisinit(); /* READ */ if (mode == 0) { if (G_name_is_fully_qualified(name, xname, xmapset)) { if (*mapset && strcmp(xmapset, mapset) != 0) { G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"), mapset, xmapset); return -1; } name = xname; mapset = xmapset; } else if (!mapset || !*mapset) mapset = G_find_file2(element, name, mapset); if (!mapset) return -1; G_file_name(path, element, name, mapset); return open(path, 0); } /* WRITE */ if (mode == 1 || mode == 2) { mapset = G_mapset(); if (G_name_is_fully_qualified(name, xname, xmapset)) { if (strcmp(xmapset, mapset) != 0) { G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"), xmapset, mapset); return -1; } name = xname; } if (*name && G_legal_filename(name) == -1) return -1; G_file_name(path, element, name, mapset); if (mode == 1 || access(path, 0) != 0) { G__make_mapset_element(element); close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666)); } return open(path, mode); } return -1; }
void print_projinfo(int shell, const char *force_epsg) { int i; char path[GPATH_MAX]; if (check_xy(shell)) return; if (!shell) fprintf(stdout, "-PROJ_INFO-------------------------------------------------\n"); for (i = 0; i < projinfo->nitems; i++) { if (shell) fprintf(stdout, "%s=%s\n", projinfo->key[i], projinfo->value[i]); else fprintf(stdout, "%-11s: %s\n", projinfo->key[i], projinfo->value[i]); } /* EPSG code is preserved for historical metadata interest only: the contents of this file are not used by pj_*() routines at all */ G_file_name(path, "", "PROJ_EPSG", "PERMANENT"); if (access(path, F_OK) == 0) { const char *epsg_value, *epsg_key; struct Key_Value *in_epsg_key; if (force_epsg) { epsg_key = "epsg"; epsg_value = force_epsg; } else { in_epsg_key = G_read_key_value_file(path); epsg_key = in_epsg_key->key[0]; epsg_value = in_epsg_key->value[0]; } if (!shell) { fprintf(stdout, "-PROJ_EPSG-------------------------------------------------\n"); fprintf(stdout, "%-11s: %s\n", epsg_key, epsg_value); } else fprintf(stdout, "%s=%s\n", epsg_key, epsg_value); if (!force_epsg) G_free_key_value(in_epsg_key); } if (!shell) fprintf(stdout, "-PROJ_UNITS------------------------------------------------\n"); for (i = 0; i < projunits->nitems; i++) { if (shell) fprintf(stdout, "%s=%s\n", projunits->key[i], projunits->value[i]); else fprintf(stdout, "%-11s: %s\n", projunits->key[i], projunits->value[i]); } return; }
int I_list_cameras(int full) { char *element; char buf[1024]; char title[50]; FILE *ls, *temp; int any; if (tempfile == NULL) tempfile = G_tempfile(); element = "camera"; G__make_mapset_element(element); temp = fopen(tempfile, "w"); if (temp == NULL) G_fatal_error("can't open any temp files"); fprintf(temp, "Available cameras\n"); fprintf(temp, "---------------------------------\n"); any = 0; strcpy(buf, "cd "); G_file_name(buf + strlen(buf), element, "", G_mapset()); strcat(buf, ";ls"); if (!full) strcat(buf, " -C"); if (ls = popen(buf, "r")) { while (G_getl(buf, sizeof buf, ls)) { any = 1; fprintf(temp, "%s", buf); if (full) { I_get_cam_title(buf, title, sizeof title); if (*title) fprintf(temp, " (%s)", title); fprintf(temp, "\n"); } else fprintf(temp, "\n"); } pclose(ls); } if (!any) fprintf(temp, "no camera files available\n"); fprintf(temp, "---------------------------------\n"); fclose(temp); sprintf(buf, "$GRASS_PAGER %s", tempfile); G_system(buf); unlink(tempfile); fprintf(stderr, "hit RETURN to continue -->"); G_gets(buf); return 0; }
int G__make_location(const char *location_name, struct Cell_head *wind, struct Key_Value *proj_info, struct Key_Value *proj_units, FILE * report_file) { char path[GPATH_MAX]; /* Try to create the location directory, under the gisdbase. */ sprintf(path, "%s/%s", G_gisdbase(), location_name); if (G_mkdir(path) != 0) return -1; /* Make the PERMANENT mapset. */ sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT"); if (G_mkdir(path) != 0) return -1; /* make these the new current location and mapset */ G__setenv("LOCATION_NAME", location_name); G__setenv("MAPSET", "PERMANENT"); /* Create the default, and current window files */ G__put_window(wind, "", "DEFAULT_WIND"); G__put_window(wind, "", "WIND"); /* Write out the PROJ_INFO, and PROJ_UNITS if available. */ if (proj_info != NULL) { G_file_name(path, "", "PROJ_INFO", "PERMANENT"); G_write_key_value_file(path, proj_info); } if (proj_units != NULL) { G_file_name(path, "", "PROJ_UNITS", "PERMANENT"); G_write_key_value_file(path, proj_units); } return 0; }
void modify_projinfo() { const char *mapset = G_mapset(); struct Cell_head old_cellhd; if (strcmp(mapset, "PERMANENT") != 0) G_fatal_error(_("You must select the PERMANENT mapset before updating the " "current location's projection (current mapset is <%s>)."), mapset); /* Read projection information from current location first */ G_get_default_window(&old_cellhd); char path[GPATH_MAX]; /* Write out the PROJ_INFO, and PROJ_UNITS if available. */ if (projinfo != NULL) { G_file_name(path, "", "PROJ_INFO", "PERMANENT"); G_write_key_value_file(path, projinfo); } if (projunits != NULL) { G_file_name(path, "", "PROJ_UNITS", "PERMANENT"); G_write_key_value_file(path, projunits); } if ((old_cellhd.zone != cellhd.zone) || (old_cellhd.proj != cellhd.proj)) { /* Recreate the default, and current window files if projection * number or zone have changed */ G__put_window(&cellhd, "", "DEFAULT_WIND"); G__put_window(&cellhd, "", "WIND"); G_message(_("Default region was updated to the new projection, but if you have " "multiple mapsets `g.region -d` should be run in each to update the " "region from the default")); } G_important_message(_("Projection information updated")); }
/* * If windowName == NULL -> RASTER3D_WINDOW_ELEMENT ("$MAPSET/WIND3") * otherwise RASTER3D_WINDOW_DATABASE ("$MAPSET/windows3d/$NAME") */ static void Rast3d_getFullWindowPath(char *path, const char *windowName) { char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; if (windowName == NULL) { G_file_name(path, "", RASTER3D_WINDOW_ELEMENT, G_mapset()); return; } while (*windowName == ' ') windowName++; if (strchr(windowName, GRASS_DIRSEP) || strchr(windowName, HOST_DIRSEP)) { sprintf(path, "%s", windowName); return; } if (G_name_is_fully_qualified(windowName, xname, xmapset)) { G_file_name(path, RASTER3D_WINDOW_DATABASE, xname, xmapset); return; } G_file_name(path, RASTER3D_WINDOW_DATABASE, windowName, G_mapset()); }
/*! \brief Check for user mapset permission \param mapset mapset name \return 1 mapset exists, and user has permission \return 0 mapset exists, BUT user denied permission \return -1 mapset does not exist */ int G__mapset_permissions(const char *mapset) { char path[GPATH_MAX]; struct stat info; G_file_name(path, "", "", mapset); if (G_stat(path, &info) != 0) return -1; if (!S_ISDIR(info.st_mode)) return -1; if (!check_owner(&info)) return 0; return 1; }
/*! * \brief Create tempfile from process id. * * See G_tempfile(). * * \param pid * \return pointer to string path */ char *G__tempfile(int pid) { char path[GPATH_MAX]; char name[GNAME_MAX]; char element[100]; if (pid <= 0) pid = getpid(); G__temp_element(element); G_init_tempfile(); do { int uniq = G_counter_next(&unique); sprintf(name, "%d.%d", pid, uniq); G_file_name(path, element, name, G_mapset()); } while (access(path, F_OK) == 0); return G_store(path); }
/*! \brief Read category index from cidx file if exists \param Map pointer to Map_info structure \param head_only read only header of the file \return 0 on success \return 1 if file does not exist \return -1 error, file exists but cannot be read */ int Vect_cidx_open(struct Map_info *Map, int head_only) { int ret; char file_path[GPATH_MAX], *path; struct gvfile fp; struct Plus_head *Plus; G_debug(2, "Vect_cidx_open(): name = %s mapset= %s", Map->name, Map->mapset); Plus = &(Map->plus); path = Vect__get_path(Map); G_file_name(file_path, path, GV_CIDX_ELEMENT, Map->mapset); if (access(file_path, F_OK) != 0) { /* does not exist */ G_free(path); return 1; } dig_file_init(&fp); fp.file = G_fopen_old(path, GV_CIDX_ELEMENT, Map->mapset); G_free(path); if (fp.file == NULL) { /* category index file is not available */ G_warning(_("Unable to open category index file for vector map <%s>"), Vect_get_full_name(Map)); return -1; } /* load category index to memory */ ret = dig_read_cidx(&fp, Plus, head_only); fclose(fp.file); if (ret == 1) { G_debug(3, "Cannot read cidx"); return -1; } return 0; }
/*! \brief Create element in the current mapset. Make the specified element in the current mapset will check for the existence of the element and do nothing if it is found so this routine can be called even if the element already exists. \param element element to be created in mapset \return 0 ? \return ? */ int G__make_mapset_element(const char *p_element) { char path[GPATH_MAX]; char *p; const char *element; element = p_element; if (*element == 0) return 0; G_file_name(p = path, NULL, NULL, G_mapset()); while (*p) p++; /* add trailing slash if missing */ --p; if (*p++ != '/') { *p++ = '/'; *p = 0; } /* now append element, one directory at a time, to path */ while (1) { if (*element == '/' || *element == 0) { *p = 0; if (access(path, 0) != 0) { /* directory not yet created */ if (G_mkdir(path) != 0) G_fatal_error(_("Unable to make mapset element %s (%s): %s"), p_element, path, strerror(errno)); } if (access(path, 0) != 0) /* directory not accessible */ G_fatal_error(_("Unable to access mapset element %s (%s): %s"), p_element, path, strerror(errno)); if (*element == 0) return 1; } *p++ = *element++; } }
static void read_gdal_options(void) { FILE *fp; struct Key_Value *key_val; const char *p; fp = G_fopen_old("", "GDAL", G_mapset()); if (!fp) G_fatal_error(_("Unable to open GDAL file")); key_val = G_fread_key_value(fp); fclose(fp); p = G_find_key_value("directory", key_val); if (!p) p = "gdal"; if (*p == '/') { st->opts.dir = G_store(p); } else { char path[GPATH_MAX]; G_file_name(path, p, "", G_mapset()); st->opts.dir = G_store(path); if (access(path, 0) != 0) G_make_mapset_element(p); } p = G_find_key_value("extension", key_val); st->opts.ext = G_store(p ? p : ""); p = G_find_key_value("format", key_val); st->opts.format = G_store(p ? p : "GTiff"); p = G_find_key_value("options", key_val); st->opts.options = p ? G_tokenize(p, ",") : NULL; G_free_key_value(key_val); }
/*! * \brief get ellipsoid parameters * * This routine returns the semi-major axis <b>a</b> (in meters) and * the eccentricity squared <b>e2</b> for the ellipsoid associated * with the database. If there is no ellipsoid explicitly associated * with the database, it returns the values for the WGS 84 ellipsoid. * * \param[out] a semi-major axis * \param[out] e2 eccentricity squared * * \return 1 success * \return 0 default values used */ int G_get_ellipsoid_parameters(double *a, double *e2) { int stat; char ipath[GPATH_MAX]; struct Key_Value *proj_keys; proj_keys = NULL; G_file_name(ipath, "", PROJECTION_FILE, PERMANENT); if (access(ipath, 0) != 0) { *a = 6378137.0; *e2 = .006694385; return 0; } proj_keys = G_read_key_value_file(ipath); stat = get_ellipsoid_parameters(proj_keys, a, e2); G_free_key_value(proj_keys); return stat; }
static const char *find_file( int misc, const char *dir, const char *element, const char *name, const char *mapset) { char path[GPATH_MAX]; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; const char *pname, *pmapset; int n; if (*name == 0) return NULL; *path = 0; /* * if name is in the fully qualified format, split it into * name, mapset (overrides what was in mapset) */ if (G_name_is_fully_qualified(name, xname, xmapset)) { pname = xname; pmapset = xmapset; } else { pname = name; pmapset = mapset; } if (strcmp(element, "vector") == 0 && strcasecmp(pmapset, "ogr") == 0) { /* don't check for virtual OGR mapset */ return G_store(pmapset); } /* * reject illegal names and mapsets */ if (G_legal_filename(pname) == -1) return NULL; if (pmapset && *pmapset && G_legal_filename(pmapset) == -1) return NULL; /* * if no specific mapset is to be searched * then search all mapsets in the mapset search list */ if (pmapset == NULL || *pmapset == 0) { int cnt = 0; const char *pselmapset = NULL; for (n = 0; (pmapset = G__mapset_name(n)); n++) { if (misc) G_file_name_misc(path, dir, element, pname, pmapset); else G_file_name(path, element, pname, pmapset); if (access(path, 0) == 0) { if (!pselmapset) pselmapset = pmapset; else G_warning(_("'%s/%s' was found in more mapsets (also found in <%s>)"), element, pname, pmapset); cnt++; } } if (cnt > 0) { /* If the same name exists in more mapsets and print a warning */ if (cnt > 1) G_warning(_("Using <%s@%s>"), pname, pselmapset); return G_store(pselmapset); } } /* * otherwise just look for the file in the specified mapset. * since the name may have been qualified, mapset may point * to the xmapset, so we must should it to * permanent storage via G_store(). */ else { if (misc) G_file_name_misc(path, dir, element, pname, pmapset); else G_file_name(path, element, pname, pmapset); if (access(path, 0) == 0) return G_store(pmapset); } return NULL; }
int main(int argc, char *argv[]) { struct GModule *module; int Out_proj; int out_stat; int old_zone, old_proj; int i; int stat; char cmnd2[500]; char proj_out[20], proj_name[50], set_name[20]; char path[1024], buffa[1024], buffb[1024], answer[200], answer1[200]; char answer2[200], buff[1024]; char tmp_buff[20], *buf; struct Key_Value *old_proj_keys, *out_proj_keys, *in_unit_keys; double aa, e2; double f; FILE *FPROJ; int exist = 0; char spheroid[100]; int j, k, sph_check; struct Cell_head cellhd; char datum[100], dat_ellps[100], dat_params[100]; struct proj_parm *proj_parms; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("projection")); module->description = _("Interactively reset the location's projection settings."); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (strcmp(G_mapset(), "PERMANENT") != 0) G_fatal_error(_("You must be in the PERMANENT mapset to run g.setproj")); /*** * no longer necessary, table is a static struct * init_unit_table(); ***/ sprintf(set_name, "PERMANENT"); G_file_name(path, "", PROJECTION_FILE, set_name); /* get the output projection parameters, if existing */ /* Check for ownership here */ stat = G__mapset_permissions(set_name); if (stat == 0) { G_fatal_error(_("PERMANENT: permission denied")); } G_get_default_window(&cellhd); if (-1 == G_set_window(&cellhd)) G_fatal_error(_("Current region cannot be set")); if (G_get_set_window(&cellhd) == -1) G_fatal_error(_("Retrieving and setting region failed")); Out_proj = cellhd.proj; old_zone = cellhd.zone; old_proj = cellhd.proj; if (access(path, 0) == 0) { exist = 1; FPROJ = fopen(path, "r"); old_proj_keys = G_fread_key_value(FPROJ); fclose(FPROJ); buf = G_find_key_value("name", old_proj_keys); fprintf(stderr, "\nWARNING: A projection file already exists for this location\n(Filename '%s')\n", path); fprintf(stderr, "\nThis file contains all the parameters for the location's projection:\n %s\n", buf); fprintf(stderr, "\n Overriding this information implies that the old projection parameters\n" " were incorrect. If you change the parameters, all existing data will\n" " be interpreted differently by the projection software.\n%c%c%c", 7, 7, 7); fprintf(stderr, " GRASS will not re-project your data automatically.\n\n"); if (!G_yes (_("Would you still like to change some of the parameters?"), 0)) { G_message(_("The projection information will not be updated")); leave(SP_NOCHANGE); } } out_proj_keys = G_create_key_value(); if (exist) { buf = G_find_key_value("zone", old_proj_keys); if (buf != NULL) sscanf(buf, "%d", &zone); if (zone != old_zone) { G_warning(_("Zone in default geographic region definition: %d\n" " is different from zone in PROJ_INFO file: %d"), old_zone, zone); old_zone = zone; } } switch (Out_proj) { case 0: /* No projection/units */ if (!exist) { /* leap frog over code, and just make sure we remove the file */ G_warning(_("XY-location cannot be projected")); goto write_file; break; } case PROJECTION_UTM: if (!exist) { sprintf(proj_name, "%s", G__projection_name(PROJECTION_UTM)); sprintf(proj_out, "utm"); break; } case PROJECTION_SP: if (!exist) { sprintf(proj_name, "%s", G__projection_name(PROJECTION_SP)); sprintf(proj_out, "stp"); break; } case PROJECTION_LL: if (!exist) { sprintf(proj_name, "%s", G__projection_name(PROJECTION_LL)); sprintf(proj_out, "ll"); break; } case PROJECTION_OTHER: if (G_ask_proj_name(proj_out, proj_name) < 0) leave(SP_NOCHANGE); if (G_strcasecmp(proj_out, "LL") == 0) Out_proj = PROJECTION_LL; else if (G_strcasecmp(proj_out, "UTM") == 0) Out_proj = PROJECTION_UTM; else if (G_strcasecmp(proj_out, "STP") == 0) Out_proj = PROJECTION_SP; break; default: G_fatal_error(_("Unknown projection")); } cellhd.proj = Out_proj; proj_parms = get_proj_parms(proj_out); if (!proj_parms) G_fatal_error(_("Projection %s is not specified in the file 'proj-parms.table'"), proj_out); G_set_key_value("name", proj_name, out_proj_keys); sph_check = 0; if (G_yes (_("Do you wish to specify a geodetic datum for this location?"), 1)) { char lbuf[100], lbufa[100]; if (exist && (G_get_datumparams_from_projinfo(old_proj_keys, lbuf, lbufa) == 2)) { G_strip(lbuf); if ((i = G_get_datum_by_name(lbuf)) > 0) { G_message(_("The current datum is %s (%s)"), G_datum_name(i), G_datum_description(i)); if (G_yes (_("Do you wish to change the datum (or datum transformation parameters)?"), 0)) sph_check = ask_datum(datum, dat_ellps, dat_params); else { sprintf(datum, lbuf); sprintf(dat_params, lbufa); sprintf(dat_ellps, G_datum_ellipsoid(i)); sph_check = 1; G_message(_("The datum information has not been changed")); } } else sph_check = ask_datum(datum, dat_ellps, dat_params); } else sph_check = ask_datum(datum, dat_ellps, dat_params); } if (sph_check > 0) { char *paramkey, *paramvalue; /* write out key/value pairs to out_proj_keys */ if (G_strcasecmp(datum, "custom") != 0) G_set_key_value("datum", datum, out_proj_keys); /* G_set_key_value("datumparams", dat_params, out_proj_keys); */ paramkey = strtok(dat_params, "="); paramvalue = dat_params + strlen(paramkey) + 1; G_set_key_value(paramkey, paramvalue, out_proj_keys); sprintf(spheroid, "%s", dat_ellps); } else { /***************** GET spheroid **************************/ if (Out_proj != PROJECTION_SP) { /* some projections have * fixed spheroids */ if (G_strcasecmp(proj_out, "ALSK") == 0 || G_strcasecmp(proj_out, "GS48") == 0 || G_strcasecmp(proj_out, "GS50") == 0) { sprintf(spheroid, "%s", "clark66"); G_set_key_value("ellps", spheroid, out_proj_keys); sph_check = 1; } else if (G_strcasecmp(proj_out, "LABRD") == 0 || G_strcasecmp(proj_out, "NZMG") == 0) { sprintf(spheroid, "%s", "international"); G_set_key_value("ellps", spheroid, out_proj_keys); sph_check = 1; } else if (G_strcasecmp(proj_out, "SOMERC") == 0) { sprintf(spheroid, "%s", "bessel"); G_set_key_value("ellps", spheroid, out_proj_keys); sph_check = 1; } else if (G_strcasecmp(proj_out, "OB_TRAN") == 0) { /* Hard coded to use "Equidistant Cylincrical" * until g.setproj has been changed to run * recurively, to allow input of options for * a second projection, MHu991010 */ G_set_key_value("o_proj", "eqc", out_proj_keys); sph_check = 2; } else { if (exist && (buf = G_find_key_value("ellps", old_proj_keys)) != NULL) { strcpy(spheroid, buf); G_strip(spheroid); if (G_get_spheroid_by_name(spheroid, &aa, &e2, &f)) { /* if legal ellips. exist, ask wether or not to change it */ G_message(_("The current ellipsoid is %s"), spheroid); if (G_yes (_("Do you want to change ellipsoid parameter?"), 0)) sph_check = G_ask_ellipse_name(spheroid); else { G_message(_("The ellipse information has not been changed")); sph_check = 1; } } /* the val is legal */ else sph_check = G_ask_ellipse_name(spheroid); } else sph_check = G_ask_ellipse_name(spheroid); } } if (sph_check > 0) { if (sph_check == 2) { /* ask radius */ if (exist) { buf = G_find_key_value("a", old_proj_keys); if ((buf != NULL) && (sscanf(buf, "%lf", &radius) == 1)) { G_message(_("The radius is currently %f"), radius); if (G_yes(_("Do you want to change the radius?"), 0)) radius = prompt_num_double(_("Enter radius for the sphere in meters"), RADIUS_DEF, 1); } } else radius = prompt_num_double(_("Enter radius for the sphere in meters"), RADIUS_DEF, 1); } /* end ask radius */ } } /*** END get spheroid ***/ /* create the PROJ_INFO & PROJ_UNITS files, if required */ if (G_strcasecmp(proj_out, "LL") == 0) ; else if (G_strcasecmp(proj_out, "STP") == 0) get_stp_proj(buffb); else if (sph_check != 2) { G_strip(spheroid); if (G_get_spheroid_by_name(spheroid, &aa, &e2, &f) == 0) G_fatal_error(_("Invalid input ellipsoid")); } write_file: /* ** NOTE the program will (hopefully) never exit abnormally ** after this point. Thus we know the file will be completely ** written out once it is opened for write */ if (exist) { sprintf(buff, "%s~", path); G_rename_file(path, buff); } if (Out_proj == 0) goto write_units; /* ** Include MISC parameters for PROJ_INFO */ if (G_strcasecmp(proj_out, "STP") == 0) { for (i = 0; i < strlen(buffb); i++) if (buffb[i] == ' ') buffb[i] = '\t'; sprintf(cmnd2, "%s\t\n", buffb); for (i = 0; i < strlen(cmnd2); i++) { j = k = 0; if (cmnd2[i] == '+') { while (cmnd2[++i] != '=') buffa[j++] = cmnd2[i]; buffa[j] = 0; while (cmnd2[++i] != '\t' && cmnd2[i] != '\n' && cmnd2[i] != 0) buffb[k++] = cmnd2[i]; buffb[k] = 0; G_set_key_value(buffa, buffb, out_proj_keys); } } } else if (G_strcasecmp(proj_out, "LL") == 0) { G_set_key_value("proj", "ll", out_proj_keys); G_set_key_value("ellps", spheroid, out_proj_keys); } else { if (sph_check != 2) { G_set_key_value("proj", proj_out, out_proj_keys); G_set_key_value("ellps", spheroid, out_proj_keys); sprintf(tmp_buff, "%.10f", aa); G_set_key_value("a", tmp_buff, out_proj_keys); sprintf(tmp_buff, "%.10f", e2); G_set_key_value("es", tmp_buff, out_proj_keys); sprintf(tmp_buff, "%.10f", f); G_set_key_value("f", tmp_buff, out_proj_keys); } else { G_set_key_value("proj", proj_out, out_proj_keys); /* G_set_key_value ("ellps", "sphere", out_proj_keys); */ sprintf(tmp_buff, "%.10f", radius); G_set_key_value("a", tmp_buff, out_proj_keys); G_set_key_value("es", "0.0", out_proj_keys); G_set_key_value("f", "0.0", out_proj_keys); } for (i = 0;; i++) { struct proj_parm *parm = &proj_parms[i]; struct proj_desc *desc; if (!parm->name) break; desc = get_proj_desc(parm->name); if (!desc) break; if (parm->ask) { if (G_strcasecmp(desc->type, "bool") == 0) { if (G_yes((char *)desc->desc, 0)) { G_set_key_value(desc->key, "defined", out_proj_keys); if (G_strcasecmp(parm->name, "SOUTH") == 0) cellhd.zone = -abs(cellhd.zone); } } else if (G_strcasecmp(desc->type, "lat") == 0) { double val; while (!get_LL_stuff(parm, desc, 1, &val)) ; sprintf(tmp_buff, "%.10f", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "lon") == 0) { double val; while (!get_LL_stuff(parm, desc, 0, &val)) ; sprintf(tmp_buff, "%.10f", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "float") == 0) { double val; while (!get_double(parm, desc, &val)) ; sprintf(tmp_buff, "%.10f", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "int") == 0) { int val; while (!get_int(parm, desc, &val)) ; sprintf(tmp_buff, "%d", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "zone") == 0) { if ((Out_proj == PROJECTION_UTM) && (old_zone != 0)) { G_message(_("The UTM zone is now set to %d"), old_zone); if (!G_yes (_("Do you want to change the UTM zone?"), 0)) { G_message(_("UTM zone information has not been updated")); zone = old_zone; break; } else { G_message(_("But if you change zone, all the existing " "data will be interpreted by projection software. " "GRASS will not automatically re-project or even " "change the headers for existing maps.")); if (!G_yes (_("Would you still like to change the UTM zone?"), 0)) { zone = old_zone; break; } } } /* UTM */ while (!get_zone()) ; sprintf(tmp_buff, "%d", zone); G_set_key_value("zone", tmp_buff, out_proj_keys); cellhd.zone = zone; } } else if (parm->def_exists) { /* don't ask, use the default */ if (G_strcasecmp(desc->type, "float") == 0 || G_strcasecmp(desc->type, "lat") == 0 || G_strcasecmp(desc->type, "lon") == 0) { sprintf(tmp_buff, "%.10f", parm->deflt); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "int") == 0) { sprintf(tmp_buff, "%d", (int)parm->deflt); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } } } /* for OPTIONS */ } /* create the PROJ_INFO & PROJ_UNITS files, if required */ G_write_key_value_file(path, out_proj_keys, &out_stat); if (out_stat != 0) { G_fatal_error(_("Error writing PROJ_INFO file <%s>"), path); } G_free_key_value(out_proj_keys); if (exist) G_free_key_value(old_proj_keys); write_units: G_file_name(path, "", UNIT_FILE, set_name); /* if we got this far, the user ** already affirmed to write over old info ** so if units file is here, remove it. */ if (access(path, 0) == 0) { sprintf(buff, "%s~", path); G_rename_file(path, buff); } if (Out_proj == 0) leave(0); { in_unit_keys = G_create_key_value(); switch (Out_proj) { case PROJECTION_UTM: G_set_key_value("unit", "meter", in_unit_keys); G_set_key_value("units", "meters", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); break; case PROJECTION_SP: for (;;) { do { fprintf(stderr, "\nSpecify the correct units to use:\n"); fprintf(stderr, "Enter the corresponding number\n"); fprintf(stderr, "1.\tUS Survey Foot (Default for State Plane 1927)\n"); fprintf(stderr, "2.\tInternational Foot\n"); fprintf(stderr, "3.\tMeter\n"); fprintf(stderr, ">"); } while (!G_gets(answer)); G_strip(answer); if (strcmp(answer, "1") == 0) { G_set_key_value("unit", "USfoot", in_unit_keys); G_set_key_value("units", "USfeet", in_unit_keys); G_set_key_value("meters", "0.30480060960121920243", in_unit_keys); break; } else if (strcmp(answer, "2") == 0) { G_set_key_value("unit", "foot", in_unit_keys); G_set_key_value("units", "feet", in_unit_keys); G_set_key_value("meters", "0.3048", in_unit_keys); break; } else if (strcmp(answer, "3") == 0) { G_set_key_value("unit", "meter", in_unit_keys); G_set_key_value("units", "meters", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); break; } else fprintf(stderr, "\nInvalid Entry (number 1 - 3)\n"); } break; case PROJECTION_LL: G_set_key_value("unit", "degree", in_unit_keys); G_set_key_value("units", "degrees", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); break; default: if (G_strcasecmp(proj_out, "LL") != 0) { fprintf(stderr, _("Enter plural form of units [meters]: ")); G_gets(answer); if (strlen(answer) == 0) { G_set_key_value("unit", "meter", in_unit_keys); G_set_key_value("units", "meters", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); } else { const struct proj_unit *unit; G_strip(answer); unit = get_proj_unit(answer); if (unit) { #ifdef FOO if (G_strcasecmp(proj_out, "STP") == 0 && !strcmp(answer, "feet")) { fprintf(stderr, "%cPROJECTION 99 State Plane cannot be in FEET.\n", 7); remove(path); /* remove file */ leave(SP_FATAL); } #endif G_set_key_value("unit", unit->unit, in_unit_keys); G_set_key_value("units", unit->units, in_unit_keys); sprintf(buffb, "%.10f", unit->fact); G_set_key_value("meters", buffb, in_unit_keys); } else { double unit_fact; while (1) { fprintf(stderr, _("Enter singular for unit: ")); G_gets(answer1); G_strip(answer1); if (strlen(answer1) > 0) break; } while (1) { fprintf(stderr, _("Enter conversion factor from %s to meters: "), answer); G_gets(answer2); G_strip(answer2); if (! (strlen(answer2) == 0 || (1 != sscanf(answer2, "%lf", &unit_fact)))) break; } G_set_key_value("unit", answer1, in_unit_keys); G_set_key_value("units", answer, in_unit_keys); sprintf(buffb, "%.10f", unit_fact); G_set_key_value("meters", buffb, in_unit_keys); } } } else { G_set_key_value("unit", "degree", in_unit_keys); G_set_key_value("units", "degrees", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); } } /* switch */ G_write_key_value_file(path, in_unit_keys, &out_stat); if (out_stat != 0) G_fatal_error(_("Error writing into UNITS output file <%s>"), path); G_free_key_value(in_unit_keys); } /* if */ if (G__put_window(&cellhd, "", "DEFAULT_WIND") < 0) G_fatal_error(_("Unable to write to DEFAULT_WIND region file")); fprintf(stderr, _("\nProjection information has been recorded for this location\n\n")); if ((old_zone != zone) | (old_proj != cellhd.proj)) { G_message(_("The geographic region information in WIND is now obsolete")); G_message(_("Run g.region -d to update it")); } leave(0); }
int save_table(int t) { int i, j, ncols, nrows, ret, field, rec; char name[2000], fname[20], element[100]; DBFHandle dbf; ROW *rows; VALUE *val; int dbftype, width, decimals; G_debug(2, "save_table %d", t); /* Note: because if driver is killed during the time the table is written, the process * is not completed and DATA ARE LOST. To minimize this, data are first written * to temporary file and then this file is renamed to 'database/table.dbf'. * Hopefully both file are on the same disk/partition */ if (!(db.tables[t].alive) || !(db.tables[t].updated)) return DB_OK; /* Construct our temp name because shapelib doesn't like '.' in name */ G__temp_element(element); sprintf(fname, "%d.dbf", getpid()); G_file_name(name, element, fname, G_mapset()); G_debug(2, "Write table to tempfile: '%s'", name); dbf = DBFCreate(name); if (dbf == NULL) return DB_FAILED; ncols = db.tables[t].ncols; rows = db.tables[t].rows; nrows = db.tables[t].nrows; for (i = 0; i < ncols; i++) { switch (db.tables[t].cols[i].type) { case DBF_INT: dbftype = FTInteger; break; case DBF_CHAR: dbftype = FTString; break; case DBF_DOUBLE: dbftype = FTDouble; break; } width = db.tables[t].cols[i].width; decimals = db.tables[t].cols[i].decimals; DBFAddField(dbf, db.tables[t].cols[i].name, dbftype, width, decimals); } G_debug(2, "Write %d rows", nrows); rec = 0; for (i = 0; i < nrows; i++) { if (rows[i].alive == FALSE) continue; for (j = 0; j < ncols; j++) { field = j; val = &(rows[i].values[j]); if (val->is_null) { DBFWriteNULLAttribute(dbf, rec, field); } else { switch (db.tables[t].cols[j].type) { case DBF_INT: ret = DBFWriteIntegerAttribute(dbf, rec, field, val->i); break; case DBF_CHAR: if (val->c != NULL) ret = DBFWriteStringAttribute(dbf, rec, field, val->c); else ret = DBFWriteStringAttribute(dbf, rec, field, ""); break; case DBF_DOUBLE: ret = DBFWriteDoubleAttribute(dbf, rec, field, val->d); break; } } } rec++; } G_debug(2, "Written %d records", rec); DBFClose(dbf); /* Copy */ if (G_rename_file(name, db.tables[t].file)) { append_error("Cannot move %s\nto %s\n", name, db.tables[t].file); return DB_FAILED; }; return DB_OK; }
/*! \brief Save feature index file for vector map \param Map pointer to Map_info structure \param offset pointer to Format_info_offset struct (see Format_info_ogr and Format_info_pg struct for implementation issues) \return 1 on success \return 0 on error */ int Vect_save_fidx(struct Map_info *Map, struct Format_info_offset *offset) { #ifdef HAVE_OGR char fname[GPATH_MAX], elem[GPATH_MAX]; char buf[5]; long length; struct gvfile fp; struct Port_info port; if (strcmp(Map->mapset, G_mapset()) != 0 || Map->support_updated == FALSE || Map->plus.built != GV_BUILD_ALL) return 1; length = 9; sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name); G_file_name(fname, elem, GV_FIDX_ELEMENT, Map->mapset); G_debug(4, "Open fidx: %s", fname); dig_file_init(&fp); fp.file = fopen(fname, "w"); if (fp.file == NULL) { G_warning(_("Unable to open fidx file for write <%s>"), fname); return 0; } dig_init_portable(&port, dig__byte_order_out()); dig_set_cur_port(&port); /* Header */ /* bytes 1 - 5 */ buf[0] = 5; buf[1] = 0; buf[2] = 5; buf[3] = 0; buf[4] = (char)dig__byte_order_out(); if (0 >= dig__fwrite_port_C(buf, 5, &fp)) return 0; /* bytes 6 - 9 : header size */ if (0 >= dig__fwrite_port_L(&length, 1, &fp)) return 0; /* Body */ /* number of records */ if (0 >= dig__fwrite_port_I(&(offset->array_num), 1, &fp)) return 0; /* offsets */ if (0 >= dig__fwrite_port_I(offset->array, offset->array_num, &fp)) return 0; G_debug(3, "Vect_save_fidx(): offset_num = %d", offset->array_num); fclose(fp.file); return 1; #else G_fatal_error(_("GRASS is not compiled with OGR support")); return 0; #endif }
/*! \brief Copy a map including attribute tables Old vector is deleted \param in input vector map name \param mapset mapset name \param out output vector map name \return -1 error \return 0 success */ int Vect_copy(const char *in, const char *mapset, const char *out) { int i, n, ret, type; struct Map_info In, Out; struct field_info *Fi, *Fin; char old_path[GPATH_MAX], new_path[GPATH_MAX], buf[GPATH_MAX]; const char *files[] = { GV_FRMT_ELEMENT, GV_COOR_ELEMENT, GV_HEAD_ELEMENT, GV_HIST_ELEMENT, GV_TOPO_ELEMENT, GV_SIDX_ELEMENT, GV_CIDX_ELEMENT, NULL }; const char *inmapset; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; dbDriver *driver; G_debug(2, "Copy vector '%s' in '%s' to '%s'", in, mapset, out); /* check for [A-Za-z][A-Za-z0-9_]* in name */ if (Vect_legal_filename(out) < 0) G_fatal_error(_("Vector map name is not SQL compliant")); inmapset = G_find_vector2(in, mapset); if (!inmapset) { G_warning(_("Unable to find vector map <%s> in <%s>"), in, mapset); return -1; } mapset = inmapset; /* remove mapset from fully qualified name, confuses G_file_name() */ if (G_name_is_fully_qualified(in, xname, xmapset)) { in = xname; } /* Delete old vector if it exists */ if (G_find_vector2(out, G_mapset())) { G_warning(_("Vector map <%s> already exists and will be overwritten"), out); ret = Vect_delete(out); if (ret != 0) { G_warning(_("Unable to delete vector map <%s>"), out); return -1; } } /* Copy the directory */ G__make_mapset_element(GV_DIRECTORY); sprintf(buf, "%s/%s", GV_DIRECTORY, out); G__make_mapset_element(buf); i = 0; while (files[i]) { sprintf(buf, "%s/%s", in, files[i]); G_file_name(old_path, GV_DIRECTORY, buf, mapset); sprintf(buf, "%s/%s", out, files[i]); G_file_name(new_path, GV_DIRECTORY, buf, G_mapset()); if (access(old_path, F_OK) == 0) { /* file exists? */ G_debug(2, "copy %s to %s", old_path, new_path); if (copy_file(old_path, new_path)) { G_warning(_("Unable to copy vector map <%s> to <%s>"), old_path, new_path); } } i++; } G_file_name(old_path, GV_DIRECTORY, in, mapset); G_file_name(new_path, GV_DIRECTORY, out, G_mapset()); /* Open input */ Vect_set_open_level(1); Vect_open_old_head(&In, in, mapset); if (In.format != GV_FORMAT_NATIVE) { /* Done */ Vect_close(&In); return 0; } /* Open output */ Vect_set_open_level(1); Vect_open_update_head(&Out, out, G_mapset()); /* Copy tables */ n = Vect_get_num_dblinks(&In); type = GV_1TABLE; if (n > 1) type = GV_MTABLE; for (i = 0; i < n; i++) { Fi = Vect_get_dblink(&In, i); if (Fi == NULL) { G_warning(_("Database connection not defined for layer %d"), In.dblnk->field[i].number); Vect_close(&In); Vect_close(&Out); return -1; } Fin = Vect_default_field_info(&Out, Fi->number, Fi->name, type); G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'", Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database, Fin->table); Vect_map_add_dblink(&Out, Fi->number, Fi->name, Fin->table, Fi->key, Fin->database, Fin->driver); ret = db_copy_table(Fi->driver, Fi->database, Fi->table, Fin->driver, Vect_subst_var(Fin->database, &Out), Fin->table); if (ret == DB_FAILED) { G_warning(_("Unable to copy table <%s>"), Fin->table); Vect_close(&In); Vect_close(&Out); return -1; } driver = db_start_driver_open_database(Fin->driver, Vect_subst_var(Fin->database, &Out)); if (driver == NULL) { G_warning(_("Unable to open database <%s> by driver <%s>"), Fin->database, Fin->driver); } else { if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK) G_warning(_("Unable to create index for table <%s>, key <%s>"), Fi->table, Fi->key); db_close_database_shutdown_driver(driver); } } Vect_close(&In); Vect_close(&Out); return 0; }
int main(int argc, char *argv[]) { char file[GPATH_MAX], name[GNAME_MAX]; const char *search_mapset, *mapset; struct GModule *module; struct Option *elem_opt; struct Option *mapset_opt; struct Option *file_opt; struct Flag *n_flag, *l_flag; module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("map management")); G_add_keyword(_("data base files")); module->description = _("Searches for GRASS data base files " "and sets variables for the shell."); G_gisinit(argv[0]); /* Define the different options */ elem_opt = G_define_option(); elem_opt->key = "element"; elem_opt->type = TYPE_STRING; elem_opt->required = YES; elem_opt->description = _("Name of an element"); file_opt = G_define_option(); file_opt->key = "file"; file_opt->type = TYPE_STRING; file_opt->required = YES; file_opt->description = _("Name of an existing map"); mapset_opt = G_define_option(); mapset_opt->key = "mapset"; mapset_opt->type = TYPE_STRING; mapset_opt->required = NO; mapset_opt->label = _("Name of a mapset (default: search path)"); mapset_opt->description = _("'.' for current mapset"); n_flag = G_define_flag(); n_flag->key = 'n'; n_flag->description = _("Don't add quotes"); l_flag = G_define_flag(); l_flag->key = 'l'; l_flag->description = _("List available elements and exit"); l_flag->suppress_required = YES; if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (l_flag->answer) { list_elements(); return EXIT_SUCCESS; } search_mapset = mapset_opt->answer; if (!search_mapset) { search_mapset = G_store(""); } if (strcmp(".", search_mapset) == 0) search_mapset = G_mapset(); if (mapset_opt->answer && strlen(mapset_opt->answer) > 0) { char **map_mapset = G_tokenize(file_opt->answer, "@"); if (G_number_of_tokens(map_mapset) > 1) { if (strcmp(map_mapset[1], mapset_opt->answer)) G_fatal_error(_("Parameter 'file' contains reference to <%s> mapset, " "but mapset parameter <%s> does not correspond"), map_mapset[1], mapset_opt->answer); else strcpy(name, file_opt->answer); } if (G_number_of_tokens(map_mapset) == 1) strcpy(name, file_opt->answer); G_free_tokens(map_mapset); } else strcpy(name, file_opt->answer); mapset = G_find_file2(elem_opt->answer, name, search_mapset); if (mapset) { char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; const char *qchar = n_flag->answer ? "" : "'"; const char *qual = G_fully_qualified_name(name, mapset); G_unqualified_name(name, mapset, xname, xmapset); G_file_name(file, elem_opt->answer, name, mapset); fprintf(stdout, "name=%s%s%s\n", qchar, xname, qchar); fprintf(stdout, "mapset=%s%s%s\n", qchar, xmapset, qchar); fprintf(stdout, "fullname=%s%s%s\n", qchar, qual, qchar); fprintf(stdout, "file=%s%s%s\n", qchar, file, qchar); return EXIT_SUCCESS; } else { fprintf(stdout, "name=\n"); fprintf(stdout, "mapset=\n"); fprintf(stdout, "fullname=\n"); fprintf(stdout, "file=\n"); } return EXIT_FAILURE; }
int main(int argc, char *argv[]) { char path[GPATH_MAX]; int perms; /* full mapset permissions */ int group, other; /* bool. want group/other permission */ struct Option *group_opt, *other_opt; struct GModule *module; /* init the GRASS library */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("map management")); G_add_keyword(_("permission")); module->label = _("Controls access to the current mapset for other users on the system."); module->description = _("If no option given, prints current status."); group_opt = G_define_option(); group_opt->key = "group"; group_opt->type = TYPE_STRING; group_opt->required = NO; group_opt->options = "grant,revoke"; group_opt->description = _("Access for group"); group_opt->guisection = _("Settings"); other_opt = G_define_option(); other_opt->key = "other"; other_opt->type = TYPE_STRING; other_opt->required = NO; other_opt->options = "grant,revoke"; other_opt->description = _("Access for others"); other_opt->guisection = _("Settings"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); #ifdef __MINGW32__ G_fatal_error(_("UNIX filesystem access controls are not supported by MS-Windows")); #endif /* get the unix file name for the mapset directory */ G_file_name(path, "", "", G_mapset()); /* this part is until PERMANENT no longer holds DEFAULT_WIND and MYNAME */ if (strcmp(G_mapset(), "PERMANENT") == 0) G_fatal_error(_("Access to the PERMANENT mapset must be open, nothing changed")); /* get the current permissions */ if (get_perms(path, &perms, &group, &other) < 0) G_fatal_error(_("Unable to determine mapset permissions")); if (group_opt->answer) { if (group_opt->answer[0] == 'g') group = 1; else group = 0; } if (other_opt->answer) { if (other_opt->answer[0] == 'g') other = 1; else other = 0; } set_perms(path, perms, group, other); exit(EXIT_SUCCESS); }