19 #include <sys/types.h>
65 if (
sizeof(off_t) > 4) {
100 _(
"Topology file must be written before spatial index file"));
109 _(
"Topology file must be written before spatial index file"));
234 G_debug(3,
"spidx offset node = %lu line = %lu, area = %lu isle = %lu",
245 G_debug(1,
"spidx body offset %lu", length);
264 unsigned char buf[6];
282 "Spidx header: file version %d.%d , supported from GRASS version %d.%d",
286 G_debug(2,
" byte order %d", byte_order);
299 G_debug(1,
"Spatial index format version %d.%d",
301 G_fatal_error(
_(
"This version of GRASS (%d.%d) is too old to read "
302 "this spatial index format."
303 " Try to rebuild topology or upgrade GRASS to at "
304 "least version %d."),
310 G_warning(
_(
"Your GRASS version does not fully support "
311 "spatial index format %d.%d of the vector."
312 " Consider to rebuild topology or upgrade GRASS."),
321 "supported by this release."
322 " Please rebuild topology."),
330 "GRASS version does not support LFS. "
331 "Please get a GRASS version with LFS support.");
509 static int rtree_dump_node(FILE *,
struct RTree_Node *n,
int);
521 static int rtree_dump_branch(FILE *fp,
struct RTree_Branch *
b,
int with_z,
529 fprintf(fp,
" id = %d ",
b->child.id);
531 fprintf(fp,
" %f %f %f %f %f %f\n",
r->boundary[0],
r->boundary[1],
532 r->boundary[2],
r->boundary[3],
r->boundary[4],
r->boundary[5]);
535 rtree_dump_node(fp,
b->child.ptr, with_z);
549 int rtree_dump_node(FILE *fp,
struct RTree_Node *n,
int with_z)
558 fprintf(fp,
"Node level=%d count=%d\n", n->
level, n->
count);
563 fprintf(fp,
" Branch %d", i);
564 rtree_dump_branch(fp, &n->
branch[i], with_z, n->
level);
570 fprintf(fp,
" Branch %d", i);
571 rtree_dump_branch(fp, &n->
branch[i], with_z, n->
level);
578 static int rtree_dump_node_file(FILE *, off_t,
int,
struct RTree *);
590 static int rtree_dump_branch_file(FILE *fp,
struct RTree_Branch *
b,
int with_z,
591 int level,
struct RTree *
t)
598 fprintf(fp,
" id = %d ",
b->child.id);
600 fprintf(fp,
" %f %f %f %f %f %f\n",
r->boundary[0],
r->boundary[1],
601 r->boundary[2],
r->boundary[3],
r->boundary[4],
r->boundary[5]);
604 rtree_dump_node_file(fp,
b->child.pos, with_z,
t);
619 int rtree_dump_node_file(FILE *fp, off_t pos,
int with_z,
struct RTree *
t)
634 fprintf(fp,
"Node level=%d count=%d\n", n->
level, n->
count);
639 fprintf(fp,
" Branch %d", i);
640 rtree_dump_branch_file(fp, &(n->
branch[i]), with_z, n->
level,
647 fprintf(fp,
" Branch %d", i);
648 rtree_dump_branch_file(fp, &(n->
branch[i]), with_z, n->
level,
690 static off_t rtree_write_from_memory(
struct gvfile *fp, off_t startpos,
691 struct RTree *
t,
int off_t_size)
693 off_t nextfreepos = startpos;
694 int sidx_nodesize, sidx_leafsize;
696 int i, j, writeout, maxcard;
701 sidx_nodesize = (int)(2 *
PORT_INT +
703 sidx_leafsize = (int)(2 *
PORT_INT +
711 s[top].branch_id = i = 0;
721 if (s[top].sn ==
NULL)
728 if (s[top].sn->level > 0) {
729 for (i = s[top].branch_id; i <
t->nodecard; i++) {
732 s[top++].branch_id = i + 1;
734 s[top].branch_id = 0;
741 s[top].branch_id =
t->nodecard;
748 "Wrong node position (%" PRId64
749 ") in file (should be %" PRId64
").",
755 maxcard = s[top].sn->level ?
t->nodecard :
t->leafcard;
756 for (j = 0; j < maxcard; j++) {
760 if (s[top].sn->level == 0)
761 s[top].pos[j] = (off_t)s[top].sn->branch[j].child.id;
772 s[top].pos[s[top].branch_id - 1] = nextfreepos;
774 (s[top + 1].sn->level ? sidx_nodesize : sidx_leafsize);
797 static off_t rtree_write_from_file(
struct gvfile *fp, off_t startpos,
798 struct RTree *
t,
int off_t_size)
800 off_t nextfreepos = startpos;
801 int sidx_nodesize, sidx_leafsize;
803 int i, j, writeout, maxcard;
804 static struct spidxstack *s =
NULL;
811 for (j = 0; j <
MAXCARD; j++) {
812 s[i].sn.branch[j].rect.boundary =
822 sidx_nodesize = (int)(2 *
PORT_INT +
824 sidx_leafsize = (int)(2 *
PORT_INT +
832 s[top].branch_id = i = 0;
847 if (s[top].sn.level > 0) {
848 for (i = s[top].branch_id; i <
t->nodecard; i++) {
851 s[top++].branch_id = i + 1;
853 s[top].branch_id = 0;
860 s[top].branch_id =
t->nodecard;
867 "Wrong node position (%" PRId64
868 ") in file (should be %" PRId64
").",
874 maxcard = s[top].sn.level ?
t->nodecard :
t->leafcard;
875 for (j = 0; j < maxcard; j++) {
879 if (s[top].sn.level == 0)
880 s[top].pos[j] = (off_t)s[top].sn.branch[j].child.id;
891 s[top].pos[s[top].branch_id - 1] = nextfreepos;
893 (s[top + 1].sn.level ? sidx_nodesize : sidx_leafsize);
904 static off_t rtree_write_to_sidx(
struct gvfile *fp, off_t startpos,
905 struct RTree *
t,
int off_t_size)
908 return rtree_write_from_file(fp, startpos,
t, off_t_size);
910 return rtree_write_from_memory(fp, startpos,
t, off_t_size);
925 static void rtree_load_to_memory(
struct gvfile *fp, off_t rootpos,
926 struct RTree *
t,
int off_t_size)
929 int i, j, loadnode, maxcard;
930 struct spidxstack *last;
931 static struct spidxstack *s =
NULL;
938 for (j = 0; j <
MAXCARD; j++) {
939 s[i].sn.branch[j].rect.boundary =
955 maxcard = s[top].sn.level ?
t->nodecard :
t->leafcard;
956 for (j = 0; j < maxcard; j++) {
960 if (s[top].sn.level == 0) {
961 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
964 s[top].sn.branch[j].child.ptr =
NULL;
968 s[top].branch_id = i = 0;
979 if (s[top].sn.level > 0) {
980 for (i = s[top].branch_id; i <
t->nodecard; i++) {
981 if (s[top].pos[i] > 0) {
982 s[top++].branch_id = i + 1;
987 maxcard = s[top].sn.level ?
t->nodecard :
t->leafcard;
988 for (j = 0; j < maxcard; j++) {
996 if (s[top].sn.level == 0) {
997 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
1000 s[top].sn.branch[j].child.ptr =
NULL;
1003 s[top].branch_id = 0;
1007 else if (last->pos[i] < 0)
1012 s[top].branch_id =
t->nodecard;
1029 s[top].sn.branch[s[top].branch_id - 1].child.ptr = newnode;
1049 static void rtree_load_to_file(
struct gvfile *fp, off_t rootpos,
1050 struct RTree *
t,
int off_t_size)
1052 off_t newnode_pos = -1;
1053 int i, j, loadnode, maxcard;
1054 struct spidxstack *last;
1055 static struct spidxstack *s =
NULL;
1062 for (j = 0; j <
MAXCARD; j++) {
1063 s[i].sn.branch[j].rect.boundary =
1079 maxcard =
t->rootlevel ?
t->nodecard :
t->leafcard;
1080 for (j = 0; j < maxcard; j++) {
1084 if (s[top].sn.level == 0) {
1085 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
1088 s[top].sn.branch[j].child.pos = -1;
1092 s[top].branch_id = i = 0;
1103 if (s[top].sn.level > 0) {
1104 for (i = s[top].branch_id; i <
t->nodecard; i++) {
1105 if (s[top].pos[i] > 0) {
1106 s[top++].branch_id = i + 1;
1111 maxcard = s[top].sn.level ?
t->nodecard :
t->leafcard;
1112 for (j = 0; j < maxcard; j++) {
1120 if (s[top].sn.level == 0) {
1121 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
1124 s[top].sn.branch[j].child.pos = -1;
1127 s[top].branch_id = 0;
1131 else if (last->pos[i] < 0)
1136 s[top].branch_id =
t->nodecard;
1152 s[top].sn.branch[s[top].branch_id - 1].child.pos = newnode_pos;
1157 t->rootpos = newnode_pos;
1160 static void rtree_load_from_sidx(
struct gvfile *fp, off_t rootpos,
1161 struct RTree *
t,
int off_t_size)
1164 rtree_load_to_file(fp, rootpos,
t, off_t_size);
1166 rtree_load_to_memory(fp, rootpos,
t, off_t_size);
1225 G_debug(1,
"dig_read_spindx()");
1269 fprintf(fp,
"Nodes\n");
1278 fprintf(fp,
"Lines\n");
1287 fprintf(fp,
"Areas\n");
1296 fprintf(fp,
"Isles\n");
1309 static void rtree_read_node(
struct NodeBuffer *nb, off_t nodepos,
1320 maxcard = nb->
n.
level ?
t->nodecard :
t->leafcard;
1321 for (i = 0; i < maxcard; i++) {
1343 while (
t->nb[
level][
t->used[
level][i]].pos != nodepos &&
1349 which =
t->used[
level][i];
1351 if (
t->nb[
level][which].pos != nodepos) {
1352 rtree_read_node(&(
t->nb[
level][which]), nodepos,
t, Plus);
1353 t->nb[
level][which].pos = nodepos;
1361 t->used[
level][0] = which;
1367 t->used[
level][0] = which;
1371 return &(
t->nb[
level][which].n);
1390 int hitCount = 0, found;
1407 s[top].sn = rtree_get_node(
t->rootpos,
t->rootlevel,
t, Plus);
1413 maxcard =
t->rootlevel ?
t->nodecard :
t->leafcard;
1414 for (j = 0; j < maxcard; j++) {
1420 if (s[top].sn.level == 0) {
1421 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
1424 s[top].sn.branch[j].child.pos = s[top].pos[j];
1429 s[top].branch_id = i = 0;
1432 level = s[top].sn->level;
1435 for (i = s[top].branch_id; i <
t->nodecard; i++) {
1436 lastpos = s[top].sn->branch[i].child.pos;
1439 s[top++].branch_id = i + 1;
1440 s[top].sn = rtree_get_node(lastpos, level - 1,
t, Plus);
1449 maxcard = s[top].sn.level ?
t->nodecard :
t->leafcard;
1450 for (j = 0; j < maxcard; j++) {
1456 if (s[top].sn.level == 0) {
1457 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
1460 s[top].sn.branch[j].child.pos = s[top].pos[j];
1464 s[top].branch_id = 0;
1471 s[top].branch_id =
t->nodecard;
1476 for (i = 0; i <
t->leafcard; i++) {
1477 if (s[top].sn->branch[i].child.id &&
1481 if (!shcb((
int)s[top].sn->branch[i].child.id,
1482 &s[top].sn->branch[i].rect, cbarg)) {
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
off_t G_ftell(FILE *)
Get the current file position of the stream.
int G_debug(int, const char *,...) __attribute__((format(printf
#define GV_SIDX_VER_MINOR
#define PORT_DOUBLE
Sizes of types used in portable format (different names used in Vlib/ and diglib/ for the same thing)
#define GV_SIDX_VER_MAJOR
#define GV_SIDX_EARLIEST_MAJOR
#define GV_SIDX_EARLIEST_MINOR
int dig__fread_port_D(double *, size_t, struct gvfile *)
Read doubles from the Portable Vector Format.
int dig__fread_port_O(off_t *, size_t, struct gvfile *, size_t)
Read off_ts from the Portable Vector Format.
int dig__fwrite_port_C(const char *, size_t, struct gvfile *)
Write chars to the Portable Vector Format.
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
int dig__fread_port_L(long *, size_t, struct gvfile *)
Read longs from the Portable Vector Format.
int dig__fwrite_port_L(const long *, size_t, struct gvfile *)
Write longs to the Portable Vector Format.
int dig__fwrite_port_I(const int *, size_t, struct gvfile *)
Write integers to the Portable Vector Format.
off_t dig_ftell(struct gvfile *file)
Get struct gvfile position.
int dig_set_cur_port(struct Port_info *)
Set current Port_info structure.
int dig__fwrite_port_D(const double *, size_t, struct gvfile *)
Write doubles to the Portable Vector Format.
void dig_rewind(struct gvfile *file)
Rewind file position.
int dig__fread_port_C(char *, size_t, struct gvfile *)
Read chars from the Portable Vector Format.
int dig__fread_port_I(int *, size_t, struct gvfile *)
Read integers from the Portable Vector Format.
int dig_fseek(struct gvfile *file, off_t offset, int whence)
Set struct gvfile position.
int dig_fflush(struct gvfile *file)
Flush struct gvfile.
void dig_spidx_free(struct Plus_head *)
Free spatial index (nodes, lines, areas, isles)
int dig_spidx_init(struct Plus_head *)
Initit spatial index (nodes, lines, areas, isles)
int dig__fwrite_port_O(const off_t *, size_t, struct gvfile *, size_t)
Write off_ts to the Portable Vector Format.
off_t RTreeGetNodePos(struct RTree *t)
size_t RTreeReadNode(struct RTree_Node *n, off_t nodepos, struct RTree *t)
void RTreeFlushBuffer(struct RTree *t)
size_t RTreeWriteNode(struct RTree_Node *n, struct RTree *t)
#define assert(condition)
void RTreeCopyNode(struct RTree_Node *n1, struct RTree_Node *n2, struct RTree *t)
struct RTree_Node * RTreeAllocNode(struct RTree *t, int level)
int RTreeOverlap(struct RTree_Rect *r, struct RTree_Rect *s, struct RTree *t)
int SearchHitCallback(int id, const struct RTree_Rect *rect, void *arg)
int dig_Wr_spidx_head(struct gvfile *fp, struct Plus_head *ptr)
Write spatial index header to file.
int dig_Wr_spidx(struct gvfile *fp, struct Plus_head *Plus)
Write spatial index to file.
int dig_Rd_spidx(struct gvfile *fp, struct Plus_head *Plus)
Read spatial index from sidx file Only needed when old vector is opened in update mode.
int dig_Rd_spidx_head(struct gvfile *fp, struct Plus_head *ptr)
Read spatial index header from sidx file.
int dig_dump_spidx(FILE *fp, const struct Plus_head *Plus)
Dump spatial index.
int rtree_search(struct RTree *t, struct RTree_Rect *r, SearchHitCallback shcb, void *cbarg, struct Plus_head *Plus)
Search spatial index file Can't use regular RTreeSearch() here because sidx must be read with dig__fr...
Basic topology-related info.
struct gvfile spidx_fp
Spatial index file pointer.
off_t Area_spidx_offset
Offset of areas in sidx file.
int with_z
2D/3D vector data
off_t coor_size
Size of coor file.
off_t Isle_spidx_offset
Offset of isles in sidx file.
struct Plus_head::@9 version
Backward compatibility version info.
off_t Hole_spidx_offset
Offset of holes in sidx file.
struct RTree * Isle_spidx
Isles spatial index.
off_t Face_spidx_offset
Offset of faces in sidx file.
int off_t_size
Offset size.
struct RTree * Area_spidx
Area spatial index.
off_t Volume_spidx_offset
Offset of volumes in sidx file.
struct RTree * Line_spidx
Line spatial index.
int spidx_with_z
2D/3D spatial index
long spidx_head_size
Spatial index header size.
struct Port_info spidx_port
Portability information for spatial index.
off_t Line_spidx_offset
Offset of lines in sidx file.
struct RTree * Node_spidx
Node spatial index.
off_t Node_spidx_offset
Offset of nodes in sidx file.
struct Version_info spidx
Version info for spatial index file.
int off_t_size
Size of off_t data type.
int byte_order
File byte order.
struct RTree_Branch * branch
int minor
Current version (minor)
int back_major
Earliest version that can use this data format (major)
int back_minor
Earliest version that can use this data format (minor)
int major
Current version (major)
FILE * file
File descriptor.
#define MAXLEVEL
Maximum verbosity level.
#define GRASS_VERSION_MINOR
#define GRASS_VERSION_MAJOR