/* Write spatial index */ int dig_write_cidx(GVFILE * fp, struct Plus_head *plus) { int i; dig_set_cur_port(&(plus->cidx_port)); dig_rewind(fp); dig_write_cidx_head(fp, plus); /* Write category-type-id for each field */ for (i = 0; i < plus->n_cidx; i++) { int j; struct Cat_index *ci; ci = &(plus->cidx[i]); ci->offset = dig_ftell(fp); /* convert type */ for (j = 0; j < ci->n_cats; j++) ci->cat[j][1] = dig_type_to_store(ci->cat[j][1]); if (0 >= dig__fwrite_port_I((int *)ci->cat, 3 * ci->n_cats, fp)) return (-1); /* Return back */ for (j = 0; j < ci->n_cats; j++) ci->cat[j][1] = dig_type_from_store(ci->cat[j][1]); } dig_write_cidx_head(fp, plus); /* rewrite with offsets */ return 0; }
/*! \brief Read spatial index from sidx file Only needed when old vector is opened in update mode \param fp pointer to struct gvfile \param[in,out] Plus pointer to Plus_head structure \return 0 */ int dig_Rd_spidx(struct gvfile * fp, struct Plus_head *Plus) { G_debug(1, "dig_read_spindx()"); /* free old trees, init new trees */ dig_spidx_free(Plus); dig_spidx_init(Plus); dig_rewind(fp); dig_Rd_spidx_head(fp, Plus); dig_set_cur_port(&(Plus->spidx_port)); /* Nodes */ rtree_load_from_sidx(fp, Plus->Node_spidx_offset, Plus->Node_spidx, Plus->spidx_port.off_t_size); /* Lines */ rtree_load_from_sidx(fp, Plus->Line_spidx_offset, Plus->Line_spidx, Plus->spidx_port.off_t_size); /* Areas */ rtree_load_from_sidx(fp, Plus->Area_spidx_offset, Plus->Area_spidx, Plus->spidx_port.off_t_size); /* Isles */ rtree_load_from_sidx(fp, Plus->Isle_spidx_offset, Plus->Isle_spidx, Plus->spidx_port.off_t_size); /* 3D future : */ /* Faces */ /* Volumes */ /* Holes */ return 0; }
/*! \brief Write spatial index to file \param[out] fp pointer to struct gvfile \param Plus pointer to Plus_head structure \return 0 */ int dig_Wr_spidx(struct gvfile *fp, struct Plus_head *Plus) { G_debug(1, "dig_Wr_spidx()"); dig_set_cur_port(&(Plus->spidx_port)); dig_rewind(fp); dig_Wr_spidx_head(fp, Plus); /* Nodes */ Plus->Node_spidx_offset = rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Node_spidx, Plus->spidx_port.off_t_size); /* Lines */ Plus->Line_spidx_offset = rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Line_spidx, Plus->spidx_port.off_t_size); /* Areas */ Plus->Area_spidx_offset = rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Area_spidx, Plus->spidx_port.off_t_size); /* Isles */ Plus->Isle_spidx_offset = rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Isle_spidx, Plus->spidx_port.off_t_size); /* 3D future : */ /* Faces */ /* Volumes */ /* Holes */ dig_rewind(fp); dig_Wr_spidx_head(fp, Plus); /* rewrite with offsets */ dig_fflush(fp); return 0; }
/*! \brief Read spatial index file \param fp pointer to gvfile structure \param[in,out] plus pointer to Plus_head structure \param head_only non-zero to read only head \return 0 OK \return 1 error */ int dig_read_cidx(struct gvfile * fp, struct Plus_head *plus, int head_only) { int i; G_debug(3, "dig_read_cidx()"); dig_cidx_free(plus); dig_cidx_init(plus); dig_rewind(fp); if (dig_read_cidx_head(fp, plus) == -1) { G_debug(3, "Cannot read cidx head"); return 1; } if (head_only) { plus->cidx_up_to_date = 1; /* OK ? */ return 0; } dig_set_cur_port(&(plus->cidx_port)); /* Read category-type-id for each field */ for (i = 0; i < plus->n_cidx; i++) { int j; struct Cat_index *ci; ci = &(plus->cidx[i]); ci->a_cats = ci->n_cats; ci->cat = G_malloc(ci->a_cats * 3 * sizeof(int)); if (dig_fseek(fp, ci->offset, 0) == -1) return 1; if (0 >= dig__fread_port_I((int *)ci->cat, 3 * ci->n_cats, fp)) return 1; /* convert type */ for (j = 0; j < ci->n_cats; j++) ci->cat[j][1] = dig_type_from_store(ci->cat[j][1]); } plus->cidx_up_to_date = 1; return 0; }
int dig_Wr_Plus_head(struct gvfile * fp, struct Plus_head *ptr) { unsigned char buf[10]; long length = 142; dig_rewind(fp); dig_set_cur_port(&(ptr->port)); /* bytes 1 - 5 */ buf[0] = GV_TOPO_VER_MAJOR; buf[1] = GV_TOPO_VER_MINOR; buf[2] = GV_TOPO_EARLIEST_MAJOR; buf[3] = GV_TOPO_EARLIEST_MINOR; buf[4] = ptr->port.byte_order; if (0 >= dig__fwrite_port_C((char *)buf, 5, fp)) return (-1); /* determine required offset size from coor file size */ if (ptr->coor_size > (off_t)PORT_LONG_MAX) { /* can only happen when sizeof(off_t) == 8 */ ptr->off_t_size = 8; } else ptr->off_t_size = 4; /* add a new field with off_t_size after byte_order? */ /* adjust header size for large files */ if (ptr->off_t_size == 8) { /* 7 offset values and coor file size: add 8 * 4 */ length += 32; } /* bytes 6 - 9 : header size */ if (0 >= dig__fwrite_port_L(&length, 1, fp)) return (0); /* byte 10 : dimension 2D or 3D */ buf[0] = ptr->with_z; if (0 >= dig__fwrite_port_C((char *)buf, 1, fp)) return (0); /* bytes 11 - 58 : bound box */ if (0 >= dig__fwrite_port_D(&(ptr->box.N), 1, fp)) return (-1); if (0 >= dig__fwrite_port_D(&(ptr->box.S), 1, fp)) return (-1); if (0 >= dig__fwrite_port_D(&(ptr->box.E), 1, fp)) return (-1); if (0 >= dig__fwrite_port_D(&(ptr->box.W), 1, fp)) return (-1); if (0 >= dig__fwrite_port_D(&(ptr->box.T), 1, fp)) return (-1); if (0 >= dig__fwrite_port_D(&(ptr->box.B), 1, fp)) return (-1); /* bytes 59 - 86 : number of structures */ if (0 >= dig__fwrite_port_P(&(ptr->n_nodes), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_edges), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_areas), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_isles), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_volumes), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_holes), 1, fp)) return (-1); /* bytes 87 - 110 : number of line types */ if (0 >= dig__fwrite_port_P(&(ptr->n_plines), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_llines), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_blines), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_clines), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_flines), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(ptr->n_klines), 1, fp)) return (-1); /* bytes 111 - 138 : Offset */ if (0 >= dig__fwrite_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fwrite_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fwrite_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fwrite_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fwrite_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fwrite_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fwrite_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size)) return (-1); /* bytes 139 - 142 : Coor size and time */ if (0 >= dig__fwrite_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size)) return (-1); G_debug(2, "topo body offset %"PRI_OFF_T, dig_ftell(fp)); return (0); }
/*! \brief Read Plus_head from file \param fp pointer to gvfile structure \param[in,out] ptr pointer to Plus_head structure \return -1 error \return 0 OK */ int dig_Rd_Plus_head(struct gvfile * fp, struct Plus_head *ptr) { unsigned char buf[5]; int byte_order; dig_rewind(fp); /* bytes 1 - 5 */ if (0 >= dig__fread_port_C((char *)buf, 5, fp)) return (-1); ptr->version.topo.major = buf[0]; ptr->version.topo.minor = buf[1]; ptr->version.topo.back_major = buf[2]; ptr->version.topo.back_minor = buf[3]; byte_order = buf[4]; G_debug(2, "Topo header: file version %d.%d , supported from GRASS version %d.%d", ptr->version.topo.major, ptr->version.topo.minor, ptr->version.topo.back_major, ptr->version.topo.back_minor); G_debug(2, " byte order %d", byte_order); /* check version numbers */ if (ptr->version.topo.major > GV_TOPO_VER_MAJOR || ptr->version.topo.minor > GV_TOPO_VER_MINOR) { /* The file was created by GRASS library with higher version than this one */ if (ptr->version.topo.back_major > GV_TOPO_VER_MAJOR || ptr->version.topo.back_minor > GV_TOPO_VER_MINOR) { /* This version of GRASS lib is lower than the oldest which can read this format */ G_debug(1, "Topology format version %d.%d", ptr->version.topo.major, ptr->version.topo.minor); G_fatal_error (_("This version of GRASS (%d.%d) is too old to read this topology format." " Try to rebuild topology or upgrade GRASS to at least version %d."), GRASS_VERSION_MAJOR, GRASS_VERSION_MINOR, GRASS_VERSION_MAJOR + 1); return (-1); } G_warning(_("Your GRASS version does not fully support topology format %d.%d of the vector." " Consider to rebuild topology or upgrade GRASS."), ptr->version.topo.major, ptr->version.topo.minor); } if (ptr->version.topo.major < GV_TOPO_VER_MAJOR || (ptr->version.topo.major == GV_TOPO_VER_MAJOR && ptr->version.topo.minor < GV_TOPO_VER_MINOR)) { /* The file was created by GRASS library with lower version than this one */ /* This version of GRASS lib can not read this old format */ G_warning(_("Old topology format version %d.%d is not supported by this release." " Try to rebuild topology using v.build or v.build.all module."), ptr->version.topo.major, ptr->version.topo.minor); return (-1); } /* init Port_info structure and set as default */ dig_init_portable(&(ptr->port), byte_order); dig_set_cur_port(&(ptr->port)); /* bytes 6 - 9 : header size */ if (0 >= dig__fread_port_L(&(ptr->head_size), 1, fp)) return (-1); G_debug(2, " header size %ld", ptr->head_size); /* determine required offset size from header size */ /* this is not safe in case new fields get added in later versions */ /* better: add a new field with off_t_size after byte_order? */ if (ptr->head_size >= 142 + 32) /* keep in sync with dig_Wr_Plus_head() */ ptr->off_t_size = 8; else ptr->off_t_size = 4; if (sizeof(off_t) < ptr->off_t_size) { G_warning(_("Vector exceeds supported file size limit")); return (-1); } G_debug(2, "topo off_t size = %d", ptr->off_t_size); /* byte 10 : dimension 2D or 3D */ if (0 >= dig__fread_port_C((char *)buf, 1, fp)) return (-1); ptr->with_z = buf[0]; G_debug(2, " with_z %d", ptr->with_z); /* bytes 11 - 58 : bound box */ if (0 >= dig__fread_port_D(&(ptr->box.N), 1, fp)) return (-1); if (0 >= dig__fread_port_D(&(ptr->box.S), 1, fp)) return (-1); if (0 >= dig__fread_port_D(&(ptr->box.E), 1, fp)) return (-1); if (0 >= dig__fread_port_D(&(ptr->box.W), 1, fp)) return (-1); if (0 >= dig__fread_port_D(&(ptr->box.T), 1, fp)) return (-1); if (0 >= dig__fread_port_D(&(ptr->box.B), 1, fp)) return (-1); /* bytes 59 - 86 : number of structures */ if (0 >= dig__fread_port_P(&(ptr->n_nodes), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_edges), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_lines), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_areas), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_isles), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_volumes), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_holes), 1, fp)) return (-1); /* bytes 87 - 110 : number of line types */ if (0 >= dig__fread_port_P(&(ptr->n_plines), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_llines), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_blines), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_clines), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_flines), 1, fp)) return (-1); if (0 >= dig__fread_port_P(&(ptr->n_klines), 1, fp)) return (-1); /* bytes 111 - 138 : Offset */ if (0 >= dig__fread_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fread_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fread_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fread_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fread_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fread_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size)) return (-1); if (0 >= dig__fread_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size)) return (-1); /* bytes 139 - 142 : Coor size and time */ if (0 >= dig__fread_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size)) return (-1); G_debug(2, " coor size %"PRI_OFF_T, ptr->coor_size); dig_fseek(fp, ptr->head_size, SEEK_SET); return (0); }
int dig_write_cidx_head(GVFILE * fp, struct Plus_head *plus) { int i; unsigned char buf[5]; long length = 9; G_debug(3, "dig_write_cidx_head()"); dig_rewind(fp); dig_set_cur_port(&(plus->cidx_port)); /* Head of header */ /* bytes 1 - 5 */ buf[0] = GV_CIDX_VER_MAJOR; buf[1] = GV_CIDX_VER_MINOR; buf[2] = GV_CIDX_EARLIEST_MAJOR; buf[3] = GV_CIDX_EARLIEST_MINOR; buf[4] = plus->cidx_port.byte_order; if (0 >= dig__fwrite_port_C(buf, 5, fp)) return (-1); /* bytes 6 - 9 : header size */ if (0 >= dig__fwrite_port_L(&length, 1, fp)) return (0); /* Body of header - info about all fields */ /* Number of fields */ if (0 >= dig__fwrite_port_I(&(plus->n_cidx), 1, fp)) return (-1); for (i = 0; i < plus->n_cidx; i++) { int t; struct Cat_index *ci; ci = &(plus->cidx[i]); G_debug(3, "cidx %d head offset: %ld", i, dig_ftell(fp)); /* Field number */ if (0 >= dig__fwrite_port_I(&(ci->field), 1, fp)) return (-1); /* Number of categories */ if (0 >= dig__fwrite_port_I(&(ci->n_cats), 1, fp)) return (-1); /* Number of unique categories */ if (0 >= dig__fwrite_port_I(&(ci->n_ucats), 1, fp)) return (-1); /* Number of types */ if (0 >= dig__fwrite_port_I(&(ci->n_types), 1, fp)) return (-1); /* Types */ for (t = 0; t < ci->n_types; t++) { int wtype; /* type */ wtype = dig_type_to_store(ci->type[t][0]); if (0 >= dig__fwrite_port_I(&wtype, 1, fp)) return (-1); /* number of items */ if (0 >= dig__fwrite_port_I(&(ci->type[t][1]), 1, fp)) return (-1); } /* Offset */ if (0 >= dig__fwrite_port_L(&(ci->offset), 1, fp)) return (0); G_debug(3, "cidx %d offset: %ld", i, ci->offset); } G_debug(3, "cidx body offset %ld", dig_ftell(fp)); return (0); }
/* return: 0 OK, -1 error */ int dig_read_cidx_head(GVFILE * fp, struct Plus_head *plus) { unsigned char buf[5]; int i, byte_order; dig_rewind(fp); /* bytes 1 - 5 */ if (0 >= dig__fread_port_C(buf, 5, fp)) return (-1); plus->cidx_Version_Major = buf[0]; plus->cidx_Version_Minor = buf[1]; plus->cidx_Back_Major = buf[2]; plus->cidx_Back_Minor = buf[3]; byte_order = buf[4]; G_debug(3, "Cidx header: file version %d.%d , supported from GRASS version %d.%d", plus->cidx_Version_Major, plus->cidx_Version_Minor, plus->cidx_Back_Major, plus->cidx_Back_Minor); G_debug(3, " byte order %d", byte_order); /* check version numbers */ if (plus->cidx_Version_Major > GV_CIDX_VER_MAJOR || plus->cidx_Version_Minor > GV_CIDX_VER_MINOR) { /* The file was created by GRASS library with higher version than this one */ if (plus->cidx_Back_Major > GV_CIDX_VER_MAJOR || plus->cidx_Back_Minor > GV_CIDX_VER_MINOR) { /* This version of GRASS lib is lower than the oldest which can read this format */ G_debug(1, "Category index format version %d.%d", plus->cidx_Version_Major, plus->cidx_Version_Minor); G_fatal_error ("This version of GRASS (%d.%d) is too old to read this category index format." " Try to rebuild topology or upgrade GRASS to at least version %d.", GRASS_VERSION_MAJOR, GRASS_VERSION_MINOR, GRASS_VERSION_MAJOR + 1); return (-1); } G_warning ("Your GRASS version does not fully support category index format %d.%d of the vector." " Consider to rebuild topology or upgrade GRASS.", plus->cidx_Version_Major, plus->cidx_Version_Minor); } dig_init_portable(&(plus->cidx_port), byte_order); dig_set_cur_port(&(plus->cidx_port)); /* bytes 6 - 9 : header size */ if (0 >= dig__fread_port_L(&(plus->cidx_head_size), 1, fp)) return (-1); G_debug(3, " header size %ld", plus->cidx_head_size); /* Body of header - info about all fields */ /* Number of fields */ if (0 >= dig__fread_port_I(&(plus->n_cidx), 1, fp)) return (-1); /* alloc space */ plus->a_cidx = plus->n_cidx; plus->cidx = (struct Cat_index *)G_malloc(plus->a_cidx * sizeof(struct Cat_index)); for (i = 0; i < plus->n_cidx; i++) { int t; struct Cat_index *ci; ci = &(plus->cidx[i]); ci->cat = NULL; ci->a_cats = 0; /* Field number */ if (0 >= dig__fread_port_I(&(ci->field), 1, fp)) return (-1); /* Number of categories */ if (0 >= dig__fread_port_I(&(ci->n_cats), 1, fp)) return (-1); /* Number of unique categories */ if (0 >= dig__fread_port_I(&(ci->n_ucats), 1, fp)) return (-1); /* Number of types */ if (0 >= dig__fread_port_I(&(ci->n_types), 1, fp)) return (-1); /* Types */ for (t = 0; t < ci->n_types; t++) { int rtype; /* type */ if (0 >= dig__fread_port_I(&rtype, 1, fp)) return (-1); ci->type[t][0] = dig_type_from_store(rtype); /* number of items */ if (0 >= dig__fread_port_I(&(ci->type[t][1]), 1, fp)) return (-1); } /* Offset */ if (0 >= dig__fread_port_L(&(ci->offset), 1, fp)) return (0); } if (dig_fseek(fp, plus->cidx_head_size, SEEK_SET) == -1) return (-1); return (0); }
/*! \brief Write spatial index header to file \param[in,out] fp pointer to struct gvfile \param ptr pointer to Plus_head structure \return 0 on success \return -1 on error */ int dig_Wr_spidx_head(struct gvfile * fp, struct Plus_head *ptr) { unsigned char buf[6]; long length = 81; /* header length in bytes */ struct RTree *t; size_t size; dig_rewind(fp); dig_set_cur_port(&(ptr->spidx_port)); /* use ptr->off_t_size = 4 if possible */ if (sizeof(off_t) > 4) { size = ptr->Node_spidx->n_nodes * ptr->Node_spidx->nodesize; size += ptr->Line_spidx->n_nodes * ptr->Line_spidx->nodesize; size += ptr->Area_spidx->n_nodes * ptr->Area_spidx->nodesize; size += ptr->Isle_spidx->n_nodes * ptr->Isle_spidx->nodesize; if (size < PORT_INT_MAX) ptr->spidx_port.off_t_size = 4; else ptr->spidx_port.off_t_size = 8; } else ptr->spidx_port.off_t_size = 4; /* bytes 1 - 6 */ buf[0] = GV_SIDX_VER_MAJOR; buf[1] = GV_SIDX_VER_MINOR; buf[2] = GV_SIDX_EARLIEST_MAJOR; buf[3] = GV_SIDX_EARLIEST_MINOR; buf[4] = ptr->spidx_port.byte_order; buf[5] = (unsigned char)ptr->spidx_port.off_t_size; if (0 >= dig__fwrite_port_C((const char *)buf, 6, fp)) return (-1); /* adjust header size for large files */ if (ptr->spidx_port.off_t_size == 4) { if (ptr->off_t_size == 4) length = 113; else if (ptr->off_t_size == 8) length = 117; else G_fatal_error(_("Topology file must be written before spatial index file")); } else if (ptr->spidx_port.off_t_size == 8) { if (ptr->off_t_size == 4) length = 141; else if (ptr->off_t_size == 8) length = 145; else G_fatal_error(_("Topology file must be written before spatial index file")); } /* bytes 7 - 10 : header size */ if (0 >= dig__fwrite_port_L(&length, 1, fp)) return (0); ptr->spidx_head_size = length; /* byte 11 : dimension 2D or 3D */ buf[0] = ptr->spidx_with_z; if (0 >= dig__fwrite_port_C((const char *)buf, 1, fp)) return (-1); /* identical for all spatial indices: */ t = ptr->Node_spidx; /* byte 12 : n dimensions */ if (0 >= dig__fwrite_port_C((const char *)&(t->ndims), 1, fp)) return (-1); /* byte 13 : n sides */ if (0 >= dig__fwrite_port_C((const char *)&(t->nsides), 1, fp)) return (-1); /* bytes 14 - 17 : nodesize */ if (0 >= dig__fwrite_port_I(&(t->nodesize), 1, fp)) return (-1); /* bytes 18 - 21 : nodecard */ if (0 >= dig__fwrite_port_I(&(t->nodecard), 1, fp)) return (-1); /* bytes 22 - 25 : leafcard */ if (0 >= dig__fwrite_port_I(&(t->leafcard), 1, fp)) return (-1); /* bytes 26 - 29 : min node fill */ if (0 >= dig__fwrite_port_I(&(t->min_node_fill), 1, fp)) return (-1); /* bytes 30 - 33 : min leaf fill */ if (0 >= dig__fwrite_port_I(&(t->min_leaf_fill), 1, fp)) return (-1); /* for each spatial index : */ /* Node spatial index */ /* bytes 34 - 37 : n nodes */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 38 - 41 : n leafs */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 42 - 45 : n levels */ if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 46 - 49 (LFS 53) : root node offset */ if (0 >= dig__fwrite_port_O(&(ptr->Node_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* Line spatial index */ t = ptr->Line_spidx; /* bytes 50 - 53 (LFS 54 - 57) : n nodes */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 54 - 57 (LFS 58 - 61) : n leafs */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 58 - 61 (LFS 62 - 65) : n levels */ if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 62 - 65 (LFS 66 - 73) : root node offset */ if (0 >= dig__fwrite_port_O(&(ptr->Line_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* Area spatial index */ t = ptr->Area_spidx; /* bytes 66 - 69 (LFS 74 - 77) : n nodes */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 70 - 73 (LFS 78 - 81) : n leafs */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 74 - 77 (LFS 82 - 85) : n levels */ if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 78 - 81 (LFS 86 - 93) : root node offset */ if (0 >= dig__fwrite_port_O(&(ptr->Area_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* Isle spatial index */ t = ptr->Isle_spidx; /* bytes 82 - 85 (LFS 94 - 97) : n nodes */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 86 - 89 (LFS 98 - 101) : n leafs */ if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 90 - 93 (LFS 102 - 105) : n levels */ if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 94 - 97 (LFS 106 - 113) : root node offset */ if (0 >= dig__fwrite_port_O(&(ptr->Isle_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* 3D future : */ /* Face spatial index */ /* bytes 98 - 101 (LFS 114 - 121) : root node offset */ if (0 >= dig__fwrite_port_O(&(ptr->Face_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* ptr->Face_spidx->rootpos = ptr->Face_spidx_offset; */ /* Volume spatial index */ /* bytes 102 - 105 (LFS 122 - 129) : root node offset */ if (0 >= dig__fwrite_port_O(&(ptr->Volume_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* ptr->Volume_spidx->rootpos = ptr->Volume_spidx_offset; */ /* Hole spatial index */ /* bytes 106 - 109 (LFS 130 - 137) : root node offset */ if (0 >= dig__fwrite_port_O(&(ptr->Hole_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* ptr->Hole_spidx->rootpos = ptr->Hole_spidx_offset; */ G_debug(3, "spidx offset node = %lu line = %lu, area = %lu isle = %lu", (long unsigned)ptr->Node_spidx_offset, (long unsigned)ptr->Line_spidx_offset, (long unsigned)ptr->Area_spidx_offset, (long unsigned)ptr->Isle_spidx_offset); /* coor file size : bytes 110 - 113 (117) (LFS: 138 - 141 (145)) */ if (0 >= dig__fwrite_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size)) return (-1); length = (long unsigned)dig_ftell(fp); G_debug(1, "spidx body offset %lu", length); if (ptr->spidx_head_size != length) G_fatal_error("wrong sidx head length %ld", ptr->spidx_head_size); return (0); }
/*! \brief Read spatial index header from sidx file \param fp pointer to struct gvfile \param[in,out] ptr pointer to Plus_head structure \return 0 on success \return -1 on error */ int dig_Rd_spidx_head(struct gvfile * fp, struct Plus_head *ptr) { unsigned char buf[6]; int byte_order; struct RTree *t; dig_rewind(fp); /* bytes 1 - 6 */ if (0 >= dig__fread_port_C((char *)buf, 6, fp)) return (-1); ptr->version.spidx.major = buf[0]; ptr->version.spidx.minor = buf[1]; ptr->version.spidx.back_major = buf[2]; ptr->version.spidx.back_minor = buf[3]; byte_order = buf[4]; ptr->spidx_port.off_t_size = buf[5]; G_debug(2, "Spidx header: file version %d.%d , supported from GRASS version %d.%d", ptr->version.spidx.major, ptr->version.spidx.minor, ptr->version.spidx.back_major, ptr->version.spidx.back_minor); G_debug(2, " byte order %d", byte_order); /* check version numbers */ if (ptr->version.spidx.major > GV_SIDX_VER_MAJOR || ptr->version.spidx.minor > GV_SIDX_VER_MINOR) { /* The file was created by GRASS library with higher version than this one */ if (ptr->version.spidx.back_major > GV_SIDX_VER_MAJOR || ptr->version.spidx.back_minor > GV_SIDX_VER_MINOR) { /* This version of GRASS lib is lower than the oldest which can read this format */ G_debug(1, "Spatial index format version %d.%d", ptr->version.spidx.major, ptr->version.spidx.minor); G_fatal_error (_("This version of GRASS (%d.%d) is too old to read this spatial index format." " Try to rebuild topology or upgrade GRASS to at least version %d."), GRASS_VERSION_MAJOR, GRASS_VERSION_MINOR, GRASS_VERSION_MAJOR + 1); return (-1); } G_warning(_("Your GRASS version does not fully support " "spatial index format %d.%d of the vector." " Consider to rebuild topology or upgrade GRASS."), ptr->version.spidx.major, ptr->version.spidx.minor); } if (ptr->version.spidx.major < GV_SIDX_VER_MAJOR || (ptr->version.spidx.major == GV_SIDX_VER_MAJOR && ptr->version.spidx.minor < GV_SIDX_VER_MINOR)) { /* The file was created by GRASS library with lower version than this one */ G_fatal_error(_("Spatial index format version %d.%d is not " "supported by this release." " Please rebuild topology."), ptr->version.spidx.major, ptr->version.spidx.minor); return (-1); } /* can this library read the sidx file ? */ if (ptr->spidx_port.off_t_size > (int)sizeof(off_t)) { G_fatal_error("Spatial index was written with LFS but this " "GRASS version does not support LFS. " "Please get a GRASS version with LFS support."); } dig_init_portable(&(ptr->spidx_port), byte_order); dig_set_cur_port(&(ptr->spidx_port)); /* bytes 7 - 10 : header size */ if (0 >= dig__fread_port_L(&(ptr->spidx_head_size), 1, fp)) return (-1); G_debug(2, " header size %ld", ptr->spidx_head_size); /* byte 11 : dimension 2D or 3D */ if (0 >= dig__fread_port_C((char *)buf, 1, fp)) return (-1); ptr->spidx_with_z = buf[0]; G_debug(2, " with_z %d", ptr->spidx_with_z); /* identical for all spatial indices: */ t = ptr->Node_spidx; /* byte 12 : n dimensions */ if (0 >= dig__fread_port_C((char *)&(t->ndims), 1, fp)) return (-1); ptr->Node_spidx->ndims = t->ndims; ptr->Line_spidx->ndims = t->ndims; ptr->Area_spidx->ndims = t->ndims; ptr->Isle_spidx->ndims = t->ndims; /* byte 13 : n sides */ if (0 >= dig__fread_port_C((char *)&(t->nsides), 1, fp)) return (-1); ptr->Node_spidx->nsides = t->nsides; ptr->Line_spidx->nsides = t->nsides; ptr->Area_spidx->nsides = t->nsides; ptr->Isle_spidx->nsides = t->nsides; /* bytes 14 - 17 : nodesize */ if (0 >= dig__fread_port_I(&(t->nodesize), 1, fp)) return (-1); ptr->Node_spidx->nodesize = t->nodesize; ptr->Line_spidx->nodesize = t->nodesize; ptr->Area_spidx->nodesize = t->nodesize; ptr->Isle_spidx->nodesize = t->nodesize; /* bytes 18 - 21 : nodecard */ if (0 >= dig__fread_port_I(&(t->nodecard), 1, fp)) return (-1); ptr->Node_spidx->nodecard = t->nodecard; ptr->Line_spidx->nodecard = t->nodecard; ptr->Area_spidx->nodecard = t->nodecard; ptr->Isle_spidx->nodecard = t->nodecard; /* bytes 22 - 25 : leafcard */ if (0 >= dig__fread_port_I(&(t->leafcard), 1, fp)) return (-1); ptr->Node_spidx->leafcard = t->leafcard; ptr->Line_spidx->leafcard = t->leafcard; ptr->Area_spidx->leafcard = t->leafcard; ptr->Isle_spidx->leafcard = t->leafcard; /* bytes 26 - 29 : min node fill */ if (0 >= dig__fread_port_I(&(t->min_node_fill), 1, fp)) return (-1); ptr->Node_spidx->min_node_fill = t->min_node_fill; ptr->Line_spidx->min_node_fill = t->min_node_fill; ptr->Area_spidx->min_node_fill = t->min_node_fill; ptr->Isle_spidx->min_node_fill = t->min_node_fill; /* bytes 30 - 33 : min leaf fill */ if (0 >= dig__fread_port_I(&(t->min_leaf_fill), 1, fp)) return (-1); ptr->Node_spidx->min_leaf_fill = t->min_leaf_fill; ptr->Line_spidx->min_leaf_fill = t->min_leaf_fill; ptr->Area_spidx->min_leaf_fill = t->min_leaf_fill; ptr->Isle_spidx->min_leaf_fill = t->min_leaf_fill; /* for each spatial index : */ /* Node spatial index */ /* bytes 34 - 37 : n nodes */ if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 38 - 41 : n leafs */ if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 42 - 45 : n levels */ if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 46 - 49 (LFS 53) : root node offset */ if (0 >= dig__fread_port_O(&(ptr->Node_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); t->rootpos = ptr->Node_spidx_offset; /* Line spatial index */ t = ptr->Line_spidx; /* bytes 50 - 53 (LFS 54 - 57) : n nodes */ if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 54 - 57 (LFS 58 - 61) : n leafs */ if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 58 - 61 (LFS 62 - 65) : n levels */ if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 62 - 65 (LFS 66 - 73) : root node offset */ if (0 >= dig__fread_port_O(&(ptr->Line_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); ptr->Line_spidx->rootpos = ptr->Line_spidx_offset; /* Area spatial index */ t = ptr->Area_spidx; /* bytes 66 - 69 (LFS 74 - 77) : n nodes */ if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 70 - 73 (LFS 78 - 81) : n leafs */ if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 74 - 77 (LFS 82 - 85) : n levels */ if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 78 - 81 (LFS 86 - 93) : root node offset */ if (0 >= dig__fread_port_O(&(ptr->Area_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); ptr->Area_spidx->rootpos = ptr->Area_spidx_offset; /* Isle spatial index */ t = ptr->Isle_spidx; /* bytes 82 - 85 (LFS 94 - 97) : n nodes */ if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp)) return (-1); /* bytes 86 - 89 (LFS 98 - 101) : n leafs */ if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp)) return (-1); /* bytes 90 - 93 (LFS 102 - 105) : n levels */ if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp)) return (-1); /* bytes 94 - 97 (LFS 106 - 113) : root node offset */ if (0 >= dig__fread_port_O(&(ptr->Isle_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); ptr->Isle_spidx->rootpos = ptr->Isle_spidx_offset; /* 3D future : */ /* Face spatial index */ /* bytes 98 - 101 (LFS 114 - 121) : root node offset */ if (0 >= dig__fread_port_O(&(ptr->Face_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* ptr->Face_spidx->rootpos = ptr->Face_spidx_offset; */ /* Volume spatial index */ /* bytes 102 - 105 (LFS 122 - 129) : root node offset */ if (0 >= dig__fread_port_O(&(ptr->Volume_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* ptr->Volume_spidx->rootpos = ptr->Volume_spidx_offset; */ /* Hole spatial index */ /* bytes 106 - 109 (LFS 130 - 137) : root node offset */ if (0 >= dig__fread_port_O(&(ptr->Hole_spidx_offset), 1, fp, ptr->spidx_port.off_t_size)) return (-1); /* ptr->Hole_spidx->rootpos = ptr->Hole_spidx_offset; */ /* coor file size : bytes 110 - 113 (117) (LFS: 138 - 145) */ if (ptr->off_t_size == -1) ptr->off_t_size = ptr->spidx_port.off_t_size; if (0 >= dig__fread_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size)) return (-1); G_debug(2, " coor size %lu", (long unsigned)ptr->coor_size); dig_fseek(fp, ptr->spidx_head_size, SEEK_SET); return (0); }
int dig_write_cidx_head(struct gvfile * fp, struct Plus_head *plus) { int i; unsigned char buf[5]; long length = 9; G_debug(3, "dig_write_cidx_head()"); dig_rewind(fp); dig_set_cur_port(&(plus->cidx_port)); /* Head of header */ /* bytes 1 - 5 */ buf[0] = GV_CIDX_VER_MAJOR; buf[1] = GV_CIDX_VER_MINOR; buf[2] = GV_CIDX_EARLIEST_MAJOR; buf[3] = GV_CIDX_EARLIEST_MINOR; buf[4] = plus->cidx_port.byte_order; if (0 >= dig__fwrite_port_C((const char *)buf, 5, fp)) return (-1); /* get required offset size */ if (plus->off_t_size == 0) { /* should not happen, topo is written first */ if (plus->coor_size > (off_t)PORT_LONG_MAX) plus->off_t_size = 8; else plus->off_t_size = 4; } /* bytes 6 - 9 : header size */ if (0 >= dig__fwrite_port_L(&length, 1, fp)) return (0); /* Body of header - info about all fields */ /* Number of fields */ if (0 >= dig__fwrite_port_I(&(plus->n_cidx), 1, fp)) return (-1); for (i = 0; i < plus->n_cidx; i++) { int t; struct Cat_index *ci; ci = &(plus->cidx[i]); G_debug(3, "cidx %d head offset: %"PRI_OFF_T, i, dig_ftell(fp)); /* Field number */ if (0 >= dig__fwrite_port_I(&(ci->field), 1, fp)) return (-1); /* Number of categories */ if (0 >= dig__fwrite_port_I(&(ci->n_cats), 1, fp)) return (-1); /* Number of unique categories */ if (0 >= dig__fwrite_port_I(&(ci->n_ucats), 1, fp)) return (-1); /* Number of types */ if (0 >= dig__fwrite_port_I(&(ci->n_types), 1, fp)) return (-1); /* Types */ for (t = 0; t < ci->n_types; t++) { int wtype; /* type */ wtype = dig_type_to_store(ci->type[t][0]); if (0 >= dig__fwrite_port_I(&wtype, 1, fp)) return (-1); /* number of items */ if (0 >= dig__fwrite_port_I(&(ci->type[t][1]), 1, fp)) return (-1); } /* Offset */ if (0 >= dig__fwrite_port_O(&(ci->offset), 1, fp, plus->off_t_size)) return (0); G_debug(3, "cidx %d offset: %"PRI_OFF_T, i, ci->offset); } G_debug(3, "cidx body offset %"PRI_OFF_T, dig_ftell(fp)); return (0); }