コード例 #1
0
ファイル: open_nat.c プロジェクト: imincik/pkg-grass
/**
   \brief Open/Create new vector map.

   \param Map pointer to vector map
   \param name map name
   \param with_z 2D or 3D (unused?)


   \return 0 success
   \return -1 error 
*/
int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
{
    char buf[1000];
    struct stat info;

    G_debug(1, "V1_open_new_nat(): name = %s", name);

    sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);

    /* Set the 'coor' file version */
    Map->head.Version_Major = GV_COOR_VER_MAJOR;
    Map->head.Version_Minor = GV_COOR_VER_MINOR;
    Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
    Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;

    /* TODO open better */
    dig_file_init(&(Map->dig_fp));
    Map->dig_fp.file = G_fopen_new(buf, GRASS_VECT_COOR_ELEMENT);
    if (Map->dig_fp.file == NULL)
	return (-1);
    fclose(Map->dig_fp.file);

    dig_file_init(&(Map->dig_fp));
    Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
    if (Map->dig_fp.file == NULL)
	return (-1);

    /* check to see if dig_plus file exists and if so, remove it */
    G__file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
    if (stat(name_buf, &info) == 0)	/* file exists? */
	unlink(name_buf);

    G__file_name(name_buf, buf, GRASS_VECT_COOR_ELEMENT, G_mapset());

    Map->head.size = 0;
    Map->head.head_size = GV_COOR_HEAD_SIZE;
    Vect__write_head(Map);

    /* set conversion matrices */
    dig_init_portable(&(Map->head.port), dig__byte_order_out());

    if (!(dig__write_head(Map)))
	return (-1);

    return 0;
}
コード例 #2
0
ファイル: make_loc.c プロジェクト: imincik/pkg-grass
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];
    int out_stat;

    /* 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, &out_stat);
	if (out_stat != 0)
	    return -2;
    }

    if (proj_units != NULL) {
	G__file_name(path, "", "PROJ_UNITS", "PERMANENT");
	G_write_key_value_file(path, proj_units, &out_stat);
	if (out_stat != 0)
	    return -2;
    }

    return 0;
}
コード例 #3
0
ファイル: proj3.c プロジェクト: cran/GRASS
static int lookup(char *file, char *key, char *value, int len)
{
    char path[1024];

/*
    G__file_name (path, "", file, G_mapset());
    if (access(path,0) == 0)
	return G_lookup_key_value_from_file(path, key, value, len) == 1;
*/
    G__file_name (path, "", file, "PERMANENT");
    return G_lookup_key_value_from_file(path, key, value, len) == 1;
}
コード例 #4
0
ファイル: g3dwindowio.c プロジェクト: imincik/pkg-grass
/*
 * 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());
}
コード例 #5
0
ファイル: rename.c プロジェクト: imincik/pkg-grass
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;
}
コード例 #6
0
ファイル: ls_elev.c プロジェクト: imincik/pkg-grass
int I_list_elev(int full)
{
    char *element;
    char buf[1024];
    FILE *ls, *temp;
    int any;

    if (tempfile == NULL)
	tempfile = G_tempfile();

    element = "cell";
    G__make_mapset_element(element);

    temp = fopen(tempfile, "w");
    if (temp == NULL)
	G_fatal_error("can't open any temp files");
    fprintf(temp, "Available raster maps:\n");
    fprintf(temp, "---------------------------------\n");

    any = 0;
    strcpy(buf, "cd ");
    G__file_name(buf + strlen(buf), element, " ", " ");
    strcat(buf, ";ls");
    strcat(buf, " -C");
    if ((ls = popen(buf, "r"))) {
	while (G_getl(buf, sizeof buf, ls)) {
	    any = 1;
	    fprintf(temp, "%s", buf);
	    fprintf(temp, "\n");
	}
	pclose(ls);
    }
    if (!any)
	fprintf(temp, "no raster maps 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);

/******/
    G_list_element("cell", "cell", G_mapset(), NULL);


    return 0;
}
コード例 #7
0
ファイル: myname.c プロジェクト: imincik/pkg-grass
char *G_myname(void)
{
    static char name[GNAME_MAX];
    char path[GPATH_MAX];
    FILE *fd;
    int ok;

    ok = 0;

    G__file_name(path, "", "MYNAME", "PERMANENT");
    if ((fd = fopen(path, "r"))) {
	ok = G_getl(name, sizeof name, fd);
	fclose(fd);
    }
    if (!ok)
	strcpy(name, _("This location has no description."));

    return name;
}
コード例 #8
0
ファイル: closecell.c プロジェクト: imincik/pkg-grass
static int close_new(int fd, int ok)
{
    struct fileinfo *fcb = &G__.fileinfo[fd];
    int stat;
    struct Categories cats;
    struct History hist;
    char path[GPATH_MAX];
    CELL cell_min, cell_max;
    int row, i, open_mode;

    if (ok) {
	switch (fcb->open_mode) {
	case OPEN_NEW_COMPRESSED:
	    G_debug(1, "close %s compressed", fcb->name);
	    break;
	case OPEN_NEW_UNCOMPRESSED:
	    G_debug(1, "close %s uncompressed", fcb->name);
	    break;
	case OPEN_NEW_RANDOM:
	    G_debug(1, "close %s random", fcb->name);
	    break;
	}

	if (fcb->open_mode != OPEN_NEW_RANDOM &&
	    fcb->cur_row < fcb->cellhd.rows) {
	    G_zero_raster_buf(fcb->data, fcb->map_type);
	    for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
		G_put_raster_row(fd, fcb->data, fcb->map_type);
	    G_free(fcb->data);
	    fcb->data = NULL;
	}

	/* create path : full null file name */
	G__make_mapset_element_misc("cell_misc", fcb->name);
	G__file_name_misc(path, "cell_misc", NULL_FILE, fcb->name,
			  G_mapset());
	remove(path);

	if (fcb->null_cur_row > 0) {
	    /* if temporary NULL file exists, write it into cell_misc/name/null */
	    int null_fd;

	    null_fd = G__open_null_write(fd);
	    if (null_fd <= 0)
		return -1;
	    if (null_fd < 1)
		return -1;

	    /* first finish writing null file */
	    /* write out the rows stored in memory */
	    for (row = fcb->min_null_row; row < fcb->null_cur_row; row++)
		G__write_null_bits(null_fd,
				   fcb->NULL_ROWS[row - fcb->min_null_row],
				   row, fcb->cellhd.cols, fd);

	    /* write missing rows */
	    if (fcb->open_mode != OPEN_NEW_RANDOM
		&& fcb->null_cur_row < fcb->cellhd.rows) {
		G__init_null_bits(fcb->null_work_buf, fcb->cellhd.cols);
		for (row = fcb->null_cur_row; row < fcb->cellhd.rows; row++)
		    G__write_null_bits(null_fd, fcb->null_work_buf, row,
				       fcb->cellhd.cols, fd);
	    }
	    close(null_fd);

	    if (rename(fcb->null_temp_name, path)) {
		G_warning(_("closecell: can't move %s\nto null file %s"),
			  fcb->null_temp_name, path);
		stat = -1;
	    }
	    else {
		remove(fcb->null_temp_name);
	    }
	}
	else {
	    remove(fcb->null_temp_name);
	    remove(path);
	}			/* null_cur_row > 0 */

	if (fcb->open_mode == OPEN_NEW_COMPRESSED) {	/* auto compression */
	    fcb->row_ptr[fcb->cellhd.rows] = lseek(fd, 0L, SEEK_CUR);
	    G__write_row_ptrs(fd);
	}

	if (fcb->map_type != CELL_TYPE) {	/* floating point map */
	    int cell_fd;

	    if (G__write_fp_format(fd) != 0) {
		G_warning(_("Error writing floating point format file for map %s"),
			  fcb->name);
		stat = -1;
	    }

	    /* now write 0-length cell file */
	    G__make_mapset_element("cell");
	    cell_fd =
		creat(G__file_name(path, "cell", fcb->name, fcb->mapset),
		      0666);
	    close(cell_fd);
	    strcpy(CELL_DIR, "fcell");
	}
	else {
	    /* remove fcell/name file */
	    G__file_name(path, "fcell", fcb->name, fcb->mapset);
	    remove(path);
	    /* remove cell_misc/name/f_format */
	    G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
			      fcb->mapset);
	    remove(path);
	    strcpy(CELL_DIR, "cell");
	    close(fd);
	}
    }				/* ok */
    /* NOW CLOSE THE FILE DESCRIPTOR */

    close(fd);
    /* remember open_mode */
    open_mode = fcb->open_mode;
    fcb->open_mode = -1;

    if (fcb->data != NULL)
	G_free(fcb->data);

    if (fcb->null_temp_name != NULL) {
	G_free(fcb->null_temp_name);
	fcb->null_temp_name = NULL;
    }

    /* if the cell file was written to a temporary file
     * move this temporary file into the cell file
     * if the move fails, tell the user, but go ahead and create
     * the support files
     */
    stat = 1;
    if (ok && (fcb->temp_name != NULL)) {
	G__file_name(path, CELL_DIR, fcb->name, fcb->mapset);
	remove(path);
	if (rename(fcb->temp_name, path)) {
	    G_warning(_("closecell: can't move %s\nto cell file %s"),
		      fcb->temp_name, path);
	    stat = -1;
	}
	else {
	    remove(fcb->temp_name);
	}
    }

    if (fcb->temp_name != NULL) {
	G_free(fcb->temp_name);
    }

    if (ok) {
	/* remove color table */
	G_remove_colors(fcb->name, "");

	/* create a history file */
	G_short_history(fcb->name, "raster", &hist);
	G_write_history(fcb->name, &hist);

	/* write the range */
	if (fcb->map_type == CELL_TYPE) {
	    G_write_range(fcb->name, &fcb->range);
	    G__remove_fp_range(fcb->name);
	}
	/*NOTE: int range for floating point maps is not written out */
	else {			/* if(fcb->map_type != CELL_TYPE) */

	    G_write_fp_range(fcb->name, &fcb->fp_range);
	    G_construct_default_range(&fcb->range);
	    /* this range will be used to add default rule to quant structure */
	}

	if (fcb->map_type != CELL_TYPE)
	    fcb->cellhd.format = -1;
	else			/* CELL map */
	    fcb->cellhd.format = fcb->nbytes - 1;

	/* write header file */
	G_put_cellhd(fcb->name, &fcb->cellhd);

	/* if map is floating point write the quant rules, otherwise remove f_quant */
	if (fcb->map_type != CELL_TYPE) {
	    /* DEFAULT RANGE QUANT
	       G_get_fp_range_min_max(&fcb->fp_range, &dcell_min, &dcell_max);
	       if(!G_is_d_null_value(&dcell_min) && !G_is_d_null_value(&dcell_max))
	       {
	       G_get_range_min_max(&fcb->range, &cell_min, &cell_max);
	       G_quant_add_rule(&fcb->quant, dcell_min, dcell_max, 
	       cell_min, cell_max);
	       }
	     */
	    G_quant_round(&fcb->quant);
	    if (G_write_quant(fcb->name, fcb->mapset, &fcb->quant) < 0)
		G_warning(_("unable to write quant file!"));
	}
	else {
	    /* remove cell_misc/name/f_quant */
	    G__file_name_misc(path, "cell_misc", QUANT_FILE, fcb->name,
			      fcb->mapset);
	    remove(path);
	}

	/* create empty cats file */
	G_get_range_min_max(&fcb->range, &cell_min, &cell_max);
	if (G_is_c_null_value(&cell_max))
	    cell_max = 0;
	G_init_cats(cell_max, (char *)NULL, &cats);
	G_write_cats(fcb->name, &cats);
	G_free_cats(&cats);

	/* write the histogram */
	/* only works for integer maps */
	if ((fcb->map_type == CELL_TYPE)
	    && (fcb->want_histogram)) {
	    G_write_histogram_cs(fcb->name, &fcb->statf);
	    G_free_cell_stats(&fcb->statf);
	}
	else {
	    G_remove_histogram(fcb->name);
	}
    }				/* OK */

    G_free(fcb->name);
    G_free(fcb->mapset);

    for (i = 0; i < NULL_ROWS_INMEM; i++)
	G_free(fcb->NULL_ROWS[i]);
    G_free(fcb->null_work_buf);

    if (fcb->map_type != CELL_TYPE)
	G_quant_free(&fcb->quant);

    return stat;
}
コード例 #9
0
ファイル: open.c プロジェクト: cran/GRASS
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;
}
コード例 #10
0
ファイル: main.c プロジェクト: imincik/pkg-grass
int main(int argc, char *argv[])
{
    char file[GPATH_MAX], name[GNAME_MAX], *mapset;
    char *search_mapset;
    struct GModule *module;
    struct Option *elem_opt;
    struct Option *mapset_opt;
    struct Option *file_opt;
    struct Flag *n_flag;

    module = G_define_module();
    module->keywords = _("general, map management");
    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->description = _("Name of a mapset");
    mapset_opt->answer = "";

    n_flag = G_define_flag();
    n_flag->key = 'n';
    n_flag->description = _("Don't add quotes");

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

    search_mapset = mapset_opt->answer;
    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) {
	const char *qchar = n_flag->answer ? "" : "'";
	const char *qual = G_fully_qualified_name(name, mapset);
	G__file_name(file, elem_opt->answer, name, mapset);
	fprintf(stdout, "name=%s%s%s\n", qchar, name, qchar);
	fprintf(stdout, "mapset=%s%s%s\n", qchar, mapset, qchar);
	fprintf(stdout, "fullname=%s%s%s\n", qchar, qual, qchar);
	fprintf(stdout, "file=%s%s%s\n", qchar, file, qchar);
    }
    else {
	fprintf(stdout, "name=\n");
	fprintf(stdout, "mapset=\n");
	fprintf(stdout, "fullname=\n");
	fprintf(stdout, "file=\n");
    }
    
    exit(mapset == NULL);
}
コード例 #11
0
ファイル: close.c プロジェクト: imincik/pkg-grass
/*!
   \brief Close vector data file

   \param Map vector map to be closed

   \return 0 on success
   \return non-zero on error
 */
int Vect_close(struct Map_info *Map)
{
    struct Coor_info CInfo;

    G_debug(1,
	    "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
	    Map->name, Map->mapset, Map->format, Map->level);

    /* Store support files if in write mode on level 2 */
    if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
	Map->plus.built == GV_BUILD_ALL) {
	char buf[GPATH_MAX];
	char file_path[GPATH_MAX];
	struct stat info;

	/* Delete old support files if available */
	sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);

	G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
	if (stat(file_path, &info) == 0)	/* file exists? */
	    unlink(file_path);

	G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
	if (stat(file_path, &info) == 0)	/* file exists? */
	    unlink(file_path);

	G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
	if (stat(file_path, &info) == 0)	/* file exists? */
	    unlink(file_path);

	Vect_coor_info(Map, &CInfo);
	Map->plus.coor_size = CInfo.size;
	Map->plus.coor_mtime = CInfo.mtime;

	Vect_save_topo(Map);

	/* Spatial index is not saved */
	/* Vect_save_spatial_index ( Map ); */

	Vect_cidx_save(Map);

#ifdef HAVE_OGR
	if (Map->format == GV_FORMAT_OGR)
	    V2_close_ogr(Map);
#endif
    }

    if (Map->level == 2 && Map->plus.release_support) {
	G_debug(1, "free topology");
	dig_free_plus(&(Map->plus));

	if (!Map->head_only) {
	    G_debug(1, "free spatial index");
	    dig_spidx_free(&(Map->plus));
	}

	G_debug(1, "free category index");
	dig_cidx_free(&(Map->plus));

    }

    if (Map->format == GV_FORMAT_NATIVE) {
	G_debug(1, "close history file");
	if (Map->hist_fp != NULL)
	    fclose(Map->hist_fp);
    }

    /* Close level 1 files / data sources if not head_only */
    if (!Map->head_only) {
	if (((*Close_array[Map->format][1]) (Map)) != 0) {
	    G_warning(_("Unable to close vector <%s>"),
		      Vect_get_full_name(Map));
	    return 1;
	}
    }

    G_free((void *)Map->name);
    Map->name = NULL;
    G_free((void *)Map->mapset);
    Map->mapset = NULL;

    Map->open = VECT_CLOSED_CODE;

    return 0;
}