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); }
int dig_Wr_P_line(struct Plus_head *Plus, int n, struct gvfile * fp) { int n_edges = 0; char ch; struct P_line *ptr; G_debug(4, "dig_Wr_P_line() line = %d", n); ptr = Plus->Line[n]; /* if NULL i.e. dead write just 0 instead of type */ if (ptr == NULL) { G_debug(4, " line is dead -> write 0 only"); ch = 0; if (0 >= dig__fwrite_port_C(&ch, 1, fp)) return (-1); return 0; } /* type */ ch = (char)dig_type_to_store(ptr->type); G_debug(5, " line type %d -> %d", ptr->type, ch); if (0 >= dig__fwrite_port_C(&ch, 1, fp)) return (-1); /* offset */ if (0 >= dig__fwrite_port_O(&(ptr->offset), 1, fp, Plus->off_t_size)) return (-1); if (!ptr->topo) return (0); /* nothing else for points */ /* centroids */ if (ptr->type & GV_CENTROID) { struct P_topo_c *topo = (struct P_topo_c *)ptr->topo; if (0 >= dig__fwrite_port_P(&(topo->area), 1, fp)) return (-1); } /* lines */ else if (ptr->type & GV_LINE) { struct P_topo_l *topo = (struct P_topo_l *)ptr->topo; if (0 >= dig__fwrite_port_P(&(topo->N1), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(topo->N2), 1, fp)) return (-1); } /* boundaries */ else if (ptr->type & GV_BOUNDARY) { struct P_topo_b *topo = (struct P_topo_b *)ptr->topo; if (0 >= dig__fwrite_port_P(&(topo->N1), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(topo->N2), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(topo->left), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(topo->right), 1, fp)) return (-1); } /* faces */ else if ((ptr->type & GV_FACE) && Plus->with_z) { /* reserved for face */ struct P_topo_f *topo = (struct P_topo_f *)ptr->topo; if (0 >= dig__fwrite_port_I(&n_edges, 1, fp)) return (-1); /* here will be list of edges */ /* left / right volume / hole */ if (0 >= dig__fwrite_port_P(&(topo->left), 1, fp)) return (-1); if (0 >= dig__fwrite_port_P(&(topo->right), 1, fp)) return (-1); } /* kernels */ else if ((ptr->type & GV_KERNEL) && Plus->with_z) { /* reserved for kernel (volume number) */ struct P_topo_k *topo = (struct P_topo_k *)ptr->topo; /* volume */ if (0 >= dig__fwrite_port_P(&(topo->volume), 1, fp)) 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); }
static off_t rtree_write_from_file(struct gvfile *fp, off_t startpos, struct RTree *t, int off_t_size) { off_t nextfreepos = startpos; int sidx_nodesize, sidx_leafsize; struct RTree_Node *n; int i, j, writeout, maxcard; static struct spidxstack *s = NULL; int top = 0; if (!s) { s = G_malloc(MAXLEVEL * sizeof(struct spidxstack)); for (i = 0; i < MAXLEVEL; i++) { s[i].sn.branch = G_malloc(MAXCARD * sizeof(struct RTree_Branch)); for (j = 0; j < MAXCARD; j++) { s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal)); } } } /* write pending changes to file */ RTreeFlushBuffer(t); /* should be foolproof */ sidx_nodesize = (int)(2 * PORT_INT + t->nodecard * (off_t_size + NUMSIDES * PORT_DOUBLE)); sidx_leafsize = (int)(2 * PORT_INT + t->leafcard * (off_t_size + NUMSIDES * PORT_DOUBLE)); /* stack size of t->rootlevel + 1 would be enough because of * depth-first post-order traversal: * only one node per level on stack at any given time */ /* add root node position to stack */ s[top].branch_id = i = 0; RTreeReadNode(&s[top].sn, t->rootpos, t); /* depth-first postorder traversal * all children of a node are visitied and written out first * when a child is written out, its position in file is stored in pos[] for * the parent node and written out with the parent node */ /* root node is written out last and its position returned */ while (top >= 0) { n = &(s[top].sn); writeout = 1; /* this is an internal node in the RTree * all its children are processed first, * before it is written out to the sidx file */ if (s[top].sn.level > 0) { for (i = s[top].branch_id; i < t->nodecard; i++) { s[top].pos[i] = 0; if (n->branch[i].child.pos >= 0) { s[top++].branch_id = i + 1; RTreeReadNode(&s[top].sn, n->branch[i].child.pos, t); s[top].branch_id = 0; writeout = 0; break; } } if (writeout) { /* nothing else found, ready to write out */ s[top].branch_id = t->nodecard; } } if (writeout) { /* write node to sidx file */ if (G_ftell(fp->file) != nextfreepos) G_fatal_error("Unable to write spatial index. " "Wrong node position (%"PRI_OFF_T") in file (should be %"PRI_OFF_T").", G_ftell(fp->file), nextfreepos); /* write with dig__fwrite_port_* fns */ dig__fwrite_port_I(&(s[top].sn.count), 1, fp); dig__fwrite_port_I(&(s[top].sn.level), 1, fp); maxcard = s[top].sn.level ? t->nodecard : t->leafcard; for (j = 0; j < maxcard; j++) { dig__fwrite_port_D(s[top].sn.branch[j].rect.boundary, NUMSIDES, fp); /* leaf node: vector object IDs are stored in child.id */ if (s[top].sn.level == 0) s[top].pos[j] = (off_t) s[top].sn.branch[j].child.id; dig__fwrite_port_O(&(s[top].pos[j]), 1, fp, off_t_size); } top--; /* update corresponding child position of parent node * this node is only updated if its level is > 0, i.e. * this is an internal node * children of internal nodes do not have an ID, instead * they hold the position in file of the next nodes down the tree */ if (top >= 0) { s[top].pos[s[top].branch_id - 1] = nextfreepos; nextfreepos += (s[top + 1].sn.level ? sidx_nodesize : sidx_leafsize); } } } close(t->fd); return nextfreepos; }
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); }