/*! \brief Save topology file for vector map \param Map pointer to Map_info structure \return 1 on success \return 0 on error */ int Vect_save_topo(struct Map_info *Map) { struct Plus_head *plus; char path[GPATH_MAX]; struct gvfile fp; G_debug(1, "Vect_save_topo()"); /* write out all the accumulated info to the plus file */ plus = &(Map->plus); dig_file_init(&fp); Vect__get_path(path, Map); fp.file = G_fopen_new(path, GV_TOPO_ELEMENT); if (fp.file == NULL) { G_warning(_("Unable to create topo file for vector map <%s>"), Map->name); return 0; } /* set portable info */ dig_init_portable(&(plus->port), dig__byte_order_out()); if (0 > dig_write_plus_file(&fp, plus)) { G_warning(_("Error writing out topo file")); return 0; } fclose(fp.file); return 1; }
/*! \brief Save category index to binary file (cidx) \param Map pointer to Map_info structure \return 0 on success \return 1 on error */ int Vect_cidx_save(struct Map_info *Map) { struct Plus_head *plus; char *path; struct gvfile fp; G_debug(2, "Vect_cidx_save()"); check_status(Map); plus = &(Map->plus); dig_file_init(&fp); path = Vect__get_path(Map); fp.file = G_fopen_new(path, GV_CIDX_ELEMENT); G_free(path); if (fp.file == NULL) { G_warning(_("Unable to create category index file for vector map <%s>"), Vect_get_name(Map)); return 1; } /* set portable info */ dig_init_portable(&(plus->cidx_port), dig__byte_order_out()); if (0 > dig_write_cidx(&fp, plus)) { G_warning(_("Error writing out category index file")); return 1; } fclose(fp.file); return 0; }
/** \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; }
/*! \brief Save spatial index file for vector map \param Map vector map \return 1 on success \return 0 on error */ int Vect_save_sidx(struct Map_info *Map) { struct Plus_head *plus; char file_path[GPATH_MAX]; G_debug(1, "Vect_save_spatial_index()"); plus = &(Map->plus); if (!plus->Spidx_built) { G_warning(_("Spatial index not available, can not be saved")); return 0; } /* new or update mode ? */ if (plus->Spidx_new == TRUE) { /* write out rtrees to sidx file */ Vect__get_element_path(file_path, Map, GV_SIDX_ELEMENT); G_debug(1, "Open sidx: %s", file_path); dig_file_init(&(plus->spidx_fp)); plus->spidx_fp.file = fopen(file_path, "w+"); if (plus->spidx_fp.file == NULL) { G_warning(_("Unable to create spatial index file for vector map <%s>"), Vect_get_name(Map)); return 0; } /* set portable info */ dig_init_portable(&(plus->spidx_port), dig__byte_order_out()); if (0 > dig_Wr_spidx(&(plus->spidx_fp), plus)) { G_warning(_("Error writing out spatial index file")); return 0; } Map->plus.Spidx_new = FALSE; } fclose(Map->plus.spidx_fp.file); Map->plus.Spidx_built = FALSE; return 1; }
/*! \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 }