Пример #1
0
void G_read_datum_table(void)
{
    FILE *fd;
    char file[GPATH_MAX];
    char buf[1024];
    int line;

    if (G_is_initialized(&table.initialized))
	return;

    sprintf(file, "%s%s", G_gisbase(), DATUMTABLE);

    fd = fopen(file, "r");
    if (!fd) {
	G_warning(_("unable to open datum table file: %s"), file);
	G_initialize_done(&table.initialized);
	return;
    }

    for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
	char name[100], descr[100], ellps[100];
	struct datum *t;

	G_strip(buf);
	if (*buf == '\0' || *buf == '#')
	    continue;

	if (table.count >= table.size) {
	    table.size += 50;
	    table.datums = G_realloc(table.datums, table.size * sizeof(struct datum));
	}

	t = &table.datums[table.count];

	if (sscanf(buf, "%s \"%99[^\"]\" %s dx=%lf dy=%lf dz=%lf",
		   name, descr, ellps, &t->dx, &t->dy, &t->dz) != 6) {
	    G_warning(_("error in datum table file, line %d"), line);
	    continue;
	}

	t->name = G_store(name);
	t->descr = G_store(descr);
	t->ellps = G_store(ellps);

	table.count++;
    }

    qsort(table.datums, table.count, sizeof(struct datum), compare_table_names);

    G_initialize_done(&table.initialized);
}
Пример #2
0
/*!
 * \brief Get user's home directory
 *
 * Returns a pointer to a string which is the full path name of the
 * user's home directory.
 *
 * \return pointer to string
 * \return NULL on error
 */
const char *G__home(void)
{
    static int initialized;
    static const char *home = 0;

    if (G_is_initialized(&initialized))
	return home;

#ifdef __MINGW32__
    {
	char buf[GPATH_MAX];

	/* TODO: we should probably check if the dir exists */
	home = getenv("USERPROFILE");

	if (!home) {
	    sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));

	    if (strlen(buf) >= 0)
		home = G_store(buf);
	}

	if (!home)
	    home = getenv("HOME");
    }
#else
    home = getenv("HOME");
#endif
    G_initialize_done(&initialized);
    return home;
}
Пример #3
0
void G_init_locale(void)
{
    static int initialized;
    const char *gisbase;

    if (G_is_initialized(&initialized))
        return;

    setlocale(LC_CTYPE, "");

#if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
#ifdef LC_MESSAGES
    setlocale(LC_MESSAGES, "");
#endif

    gisbase = getenv("GISBASE");
    if (gisbase && *gisbase) {
        char localedir[GPATH_MAX];

        strcpy(localedir, gisbase);
        strcat(localedir, "/locale");

        bindtextdomain("grasslibs", localedir);
        bindtextdomain("grassmods", localedir);
    }
#endif

    G_initialize_done(&initialized);
}
Пример #4
0
/*!
 * \brief Current date and time.
 *
 * Returns a pointer to a string which is the current date and
 * time. The format is the same as that produced by the UNIX
 * <tt>date</tt> command.
 *
 * \return pointer to a string holding date/time
 */
const char *G_date(void)
{
    static int initialized;
    static char *date;
    time_t clock;
    struct tm *local;
    char *tdate;
    char *d;

    if (G_is_initialized(&initialized))
	return date;

    time(&clock);

    local = localtime(&clock);
    tdate = asctime(local);
    for (d = tdate; *d; d++)
	if (*d == '\n')
	    *d = 0;

    date = G_store(tdate);

    G_initialize_done(&initialized);

    return date;
}
Пример #5
0
void Rast__init(void)
{
    if (G_is_initialized(&initialized))
	return;
    init();
    G_initialize_done(&initialized);
}
Пример #6
0
/*!
  \brief Initialize environment for creating tempfiles.
*/
void G_init_tempfile(void)
{
    if (G_is_initialized(&initialized))
	return;

    G_init_counter(&unique, 0);

    G_initialize_done(&initialized);
}
Пример #7
0
static void init(void)
{
    if (G_is_initialized(&initialized))
	return;

    proj_info = G_get_projinfo();
    proj_units = G_get_projunits();
    proj_epsg = G_get_projepsg();

    G_initialize_done(&initialized);
}
Пример #8
0
/*!
  \brief Initialization

  Register all GDAL drivers.
*/
void Rast_init_gdal(void)
{
#ifdef GDAL_LINK
    static int initialized;

    if (G_is_initialized(&initialized))
	return;

    init_gdal();
    (*pGDALAllRegister) ();
    G_initialize_done(&initialized);
#endif
}
Пример #9
0
int G_verbose(void)
{
    const char *verstr;		/* string for GRASS_VERBOSE content */

    if (G_is_initialized(&initialized))
	return verbose;

    /* verbose not defined -> get it from env. */
    verstr = getenv("GRASS_VERBOSE");
    verbose = verstr ? atoi(verstr) : STDLEVEL;

    G_initialize_done(&initialized);

    return verbose;
}
Пример #10
0
static int read_env(int loc)
{

    FILE *fd;

    if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
	return 0;		/* don't use file for GISRC */

    if (G_is_initialized(&st->init[loc]))
	return 1;

    if ((fd = open_env("r", loc))) {
        parse_env(fd, loc);
        fclose(fd);
    }

    G_initialize_done(&st->init[loc]);
    return 0;
}
Пример #11
0
const char *G__machine_name(void)
{
    static int initialized;
    static char name[128];

    if (G_is_initialized(&initialized))
	return name;

#if defined(HAVE_GETHOSTNAME)
    gethostname(name, sizeof(name));
    name[sizeof(name) - 1] = 0;	/* make sure NUL terminated */
#elif defined(HAVE_SYS_UTSNAME_H)
    {
	struct utsname attname;
	uname(&attname);
	strcpy(name, attname.nodename);
    }
#else
    strcpy(name, "unknown");
#endif

    G_initialize_done(&initialized);
    return name;
}
Пример #12
0
/*!
  \brief Read ellipsoid table

  \param fatal non-zero value for G_fatal_error(), otherwise
  G_warning() is used

  \return 1 on sucess
  \return 0 on error
*/
int G_read_ellipsoid_table(int fatal)
{
    FILE *fd;
    char file[GPATH_MAX];
    char buf[1024];
    char badlines[256];
    int line;
    int err;

    if (G_is_initialized(&table.initialized))
	return 1;

    sprintf(file, "%s/etc/proj/ellipse.table", G_gisbase());
    fd = fopen(file, "r");

    if (fd == NULL) {
	(fatal ? G_fatal_error : G_warning)(_("Unable to open ellipsoid table file <%s>"), file);
	G_initialize_done(&table.initialized);
	return 0;
    }

    err = 0;
    *badlines = 0;
    for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
	char name[100], descr[100], buf1[100], buf2[100];
	struct ellipse *e;

	G_strip(buf);
	if (*buf == 0 || *buf == '#')
	    continue;

	if (sscanf(buf, "%s  \"%99[^\"]\" %s %s", name, descr, buf1, buf2) != 4) {
	    err++;
	    sprintf(buf, " %d", line);
	    if (*badlines)
		strcat(badlines, ",");
	    strcat(badlines, buf);
	    continue;
	}

	if (table.count >= table.size) {
	    table.size += 60;
	    table.ellipses = G_realloc(table.ellipses, table.size * sizeof(struct ellipse));
	}

	e = &table.ellipses[table.count];

	e->name = G_store(name);
	e->descr = G_store(descr);

	if (get_a_e2_f(buf1, buf2, &e->a, &e->e2, &e->f) ||
	    get_a_e2_f(buf2, buf1, &e->a, &e->e2, &e->f))
	    table.count++;
	else {
	    err++;
	    sprintf(buf, " %d", line);
	    if (*badlines)
		strcat(badlines, ",");
	    strcat(badlines, buf);
	    continue;
	}
    }

    fclose(fd);

    if (!err) {
	/* over correct typed version */
	qsort(table.ellipses, table.count, sizeof(struct ellipse), compare_ellipse_names);
	G_initialize_done(&table.initialized);
	return 1;
    }

    (fatal ? G_fatal_error : G_warning)(
	_n(
	("Line%s of ellipsoid table file <%s> is invalid"),
        ("Lines%s of ellipsoid table file <%s> are invalid"),
        err), 
	badlines, file);

    G_initialize_done(&table.initialized);

    return 0;
}
Пример #13
0
/*!
  \brief Create GDAL settings for given raster map

  \param name map name
  \param map_type map type (CELL, FCELL, DCELL)

  \return pointer to allocated GDAL_link structure
  \return NULL on error
*/
struct GDAL_link *Rast_create_gdal_link(const char *name,
					RASTER_MAP_TYPE map_type)
{
#ifdef GDAL_LINK
    char path[GPATH_MAX];
    GDALDriverH driver;
    double transform[6];
    struct GDAL_link *gdal;
    FILE *fp;
    struct Key_Value *key_val;
    char buf[32];

    Rast__init_window();

    Rast_init_gdal();

    if (!G_is_initialized(&st->initialized)) {
	read_gdal_options();
	st->projinfo = G_get_projinfo();
	st->projunits = G_get_projunits();
#if 0
	/* We cannot use GPJ_grass_to_wkt() here because that would create a
	   circular dependency between libgis and libgproj */
	if (st->projinfo && st->projunits)
	    st->srswkt = GPJ_grass_to_wkt(st->projinfo, st->projunits);
#endif
	G_initialize_done(&st->initialized);
    }

    gdal = G_calloc(1, sizeof(struct GDAL_link));

    sprintf(path, "%s/%s%s", st->opts.dir, name, st->opts.ext);
    gdal->filename = G_store(path);
    gdal->band_num = 1;
    gdal->hflip = 0;
    gdal->vflip = 0;

    switch (map_type) {
    case CELL_TYPE:
	switch (R__.nbytes) {
	case 1:
	    gdal->type = GDT_Byte;
	    gdal->null_val = (DCELL) 0xFF;
	    break;
	case 2:
	    gdal->type = GDT_UInt16;
	    gdal->null_val = (DCELL) 0xFFFF;
	    break;
	case 3:
	case 4:
	    gdal->type = GDT_Int32;
	    gdal->null_val = (DCELL) 0x80000000U;
	    break;
	}
	break;
    case FCELL_TYPE:
	gdal->type = GDT_Float32;
	Rast_set_d_null_value(&gdal->null_val, 1);
	break;
    case DCELL_TYPE:
	gdal->type = GDT_Float64;
	Rast_set_d_null_value(&gdal->null_val, 1);
	break;
    default:
	G_fatal_error(_("Invalid map type <%d>"), map_type);
	break;
    }

    driver = (*pGDALGetDriverByName) (st->opts.format);
    if (!driver)
	G_fatal_error(_("Unable to get <%s> driver"), st->opts.format);

    /* Does driver support GDALCreate ? */
    if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATE, NULL)) {
	gdal->data =
	    (*pGDALCreate)(driver, gdal->filename,
			   R__.wr_window.cols, R__.wr_window.rows,
			   1, gdal->type, st->opts.options);
	if (!gdal->data)
	    G_fatal_error(_("Unable to create <%s> dataset using <%s> driver"),
			  name, st->opts.format);
    }
    /* If not - create MEM driver for intermediate dataset. 
     * Check if raster can be created at all (with GDALCreateCopy) */
    else if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATECOPY, NULL)) {
	GDALDriverH mem_driver;

	G_message(_("Driver <%s> does not support direct writing. "
		    "Using MEM driver for intermediate dataset."),
		  st->opts.format);

	mem_driver = (*pGDALGetDriverByName) ("MEM");
	if (!mem_driver)
	    G_fatal_error(_("Unable to get in-memory raster driver"));

	gdal->data =
	    (*pGDALCreate)(mem_driver, "",
			   R__.wr_window.cols, R__.wr_window.rows,
			   1, gdal->type, st->opts.options);
	if (!gdal->data)
	    G_fatal_error(_("Unable to create <%s> dataset using memory driver"),
			  name);
    }
    else
	G_fatal_error(_("Driver <%s> does not support creating rasters"),
		      st->opts.format);

    gdal->band = (*pGDALGetRasterBand) (gdal->data, gdal->band_num);

    (*pGDALSetRasterNoDataValue) (gdal->band, gdal->null_val);

    /* Set Geo Transform  */
    transform[0] = R__.wr_window.west;
    transform[1] = R__.wr_window.ew_res;
    transform[2] = 0.0;
    transform[3] = R__.wr_window.north;
    transform[4] = 0.0;
    transform[5] = -R__.wr_window.ns_res;

    if ((*pGDALSetGeoTransform) (gdal->data, transform) >= CE_Failure)
	G_warning(_("Unable to set geo transform"));

    if (st->srswkt)
	if ((*pGDALSetProjection) (gdal->data, st->srswkt) == CE_Failure)
	    G_warning(_("Unable to set projection"));

    fp = G_fopen_new_misc("cell_misc", "gdal", name);
    if (!fp)
	G_fatal_error(_("Unable to create cell_misc/%s/gdal file"), name);

    key_val = G_create_key_value();

    G_set_key_value("file", gdal->filename, key_val);

    sprintf(buf, "%d", gdal->band_num);
    G_set_key_value("band", buf, key_val);

    sprintf(buf, "%.22g", gdal->null_val);
    G_set_key_value("null", buf, key_val);

    sprintf(buf, "%d", gdal->type);
    G_set_key_value("type", buf, key_val);

    if (G_fwrite_key_value(fp, key_val) < 0)
	G_fatal_error(_("Error writing cell_misc/%s/gdal file"), name);

    G_free_key_value(key_val);

    fclose(fp);

    return gdal;
#else
    return NULL;
#endif
}