18 #include <sys/types.h> 67 if (
sizeof(off_t) > 4) {
99 G_fatal_error(
_(
"Topology file must be written before spatial index file"));
107 G_fatal_error(
_(
"Topology file must be written before spatial index file"));
239 G_debug(3,
"spidx offset node = %lu line = %lu, area = %lu isle = %lu",
250 G_debug(1,
"spidx body offset %lu", length);
269 unsigned char buf[6];
286 "Spidx header: file version %d.%d , supported from GRASS version %d.%d",
290 G_debug(2,
" byte order %d", byte_order);
300 G_debug(1,
"Spatial index format version %d.%d",
303 (
_(
"This version of GRASS (%d.%d) is too old to read this spatial index format." 304 " Try to rebuild topology or upgrade GRASS to at least version %d."),
309 G_warning(
_(
"Your GRASS version does not fully support " 310 "spatial index format %d.%d of the vector." 311 " Consider to rebuild topology or upgrade GRASS."),
319 "supported by this release." 320 " Please rebuild topology."),
328 "GRASS version does not support LFS. " 329 "Please get a GRASS version with LFS support.");
514 static int rtree_dump_node(FILE *,
struct RTree_Node *n,
int);
526 static int rtree_dump_branch(FILE * fp,
struct RTree_Branch *
b,
int with_z,
534 fprintf(fp,
" id = %d ", b->
child.
id);
540 rtree_dump_node(fp, b->
child.
ptr, with_z);
554 int rtree_dump_node(FILE * fp,
struct RTree_Node *n,
int with_z)
563 fprintf(fp,
"Node level=%d count=%d\n", n->
level, n->
count);
568 fprintf(fp,
" Branch %d", i);
569 rtree_dump_branch(fp, &n->
branch[i], with_z, n->
level);
575 fprintf(fp,
" Branch %d", i);
576 rtree_dump_branch(fp, &n->
branch[i], with_z, n->
level);
583 static int rtree_dump_node_file(FILE *, off_t,
int,
struct RTree *);
595 static int rtree_dump_branch_file(FILE * fp,
struct RTree_Branch *b,
int with_z,
596 int level,
struct RTree *
t)
603 fprintf(fp,
" id = %d ", b->
child.
id);
609 rtree_dump_node_file(fp, b->
child.
pos, with_z, t);
624 int rtree_dump_node_file(FILE * fp, off_t pos,
int with_z,
struct RTree *t)
639 fprintf(fp,
"Node level=%d count=%d\n", n->
level, n->
count);
644 fprintf(fp,
" Branch %d", i);
645 rtree_dump_branch_file(fp, &(n->
branch[i]), with_z, n->
level, t);
651 fprintf(fp,
" Branch %d", i);
652 rtree_dump_branch_file(fp, &(n->
branch[i]), with_z, n->
level, t);
694 static off_t rtree_write_from_memory(
struct gvfile *fp, off_t startpos,
695 struct RTree *t,
int off_t_size)
697 off_t nextfreepos = startpos;
698 int sidx_nodesize, sidx_leafsize;
700 int i, j, writeout, maxcard;
715 s[top].branch_id = i = 0;
725 if (s[top].sn ==
NULL)
732 if (s[top].sn->level > 0) {
733 for (i = s[top].branch_id; i < t->
nodecard; i++) {
736 s[top++].branch_id = i + 1;
738 s[top].branch_id = 0;
759 for (j = 0; j < maxcard; j++) {
763 if (s[top].sn->level == 0)
764 s[top].pos[j] = (off_t) s[top].sn->branch[j].child.id;
775 s[top].pos[s[top].branch_id - 1] = nextfreepos;
776 nextfreepos += (s[top + 1].sn->level ? sidx_nodesize : sidx_leafsize);
800 static off_t rtree_write_from_file(
struct gvfile *fp, off_t startpos,
801 struct RTree *t,
int off_t_size)
803 off_t nextfreepos = startpos;
804 int sidx_nodesize, sidx_leafsize;
806 int i, j, writeout, maxcard;
807 static struct spidxstack *s =
NULL;
814 for (j = 0; j <
MAXCARD; j++) {
834 s[top].branch_id = i = 0;
849 if (s[top].sn.level > 0) {
850 for (i = s[top].branch_id; i < t->
nodecard; i++) {
853 s[top++].branch_id = i + 1;
855 s[top].branch_id = 0;
876 for (j = 0; j < maxcard; j++) {
880 if (s[top].sn.level == 0)
881 s[top].pos[j] = (off_t) s[top].sn.branch[j].child.id;
892 s[top].pos[s[top].branch_id - 1] = nextfreepos;
893 nextfreepos += (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++) {
955 for (j = 0; j < maxcard; j++) {
959 if (s[top].sn.level == 0) {
960 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
963 s[top].sn.branch[j].child.ptr =
NULL;
967 s[top].branch_id = i = 0;
978 if (s[top].sn.level > 0) {
979 for (i = s[top].branch_id; i < t->
nodecard; i++) {
980 if (s[top].pos[i] > 0) {
981 s[top++].branch_id = i + 1;
987 for (j = 0; j < maxcard; j++) {
996 if (s[top].sn.level == 0) {
997 s[top].sn.branch[j].child.id =
1001 s[top].sn.branch[j].child.ptr =
NULL;
1004 s[top].branch_id = 0;
1008 else if (last->pos[i] < 0)
1030 s[top].sn.branch[s[top].branch_id - 1].child.ptr = newnode;
1050 static void rtree_load_to_file(
struct gvfile *fp, off_t rootpos,
1051 struct RTree *t,
int off_t_size)
1053 off_t newnode_pos = -1;
1054 int i, j, loadnode, maxcard;
1055 struct spidxstack *last;
1056 static struct spidxstack *s =
NULL;
1063 for (j = 0; j <
MAXCARD; j++) {
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;
1112 for (j = 0; j < maxcard; j++) {
1121 if (s[top].sn.level == 0) {
1122 s[top].sn.branch[j].child.id =
1126 s[top].sn.branch[j].child.pos = -1;
1129 s[top].branch_id = 0;
1133 else if (last->pos[i] < 0)
1154 s[top].sn.branch[s[top].branch_id - 1].child.pos = newnode_pos;
1162 static void rtree_load_from_sidx(
struct gvfile *fp, off_t rootpos,
1163 struct RTree *t,
int off_t_size)
1166 return rtree_load_to_file(fp, rootpos, t, off_t_size);
1168 return rtree_load_to_memory(fp, rootpos, t, off_t_size);
1231 G_debug(1,
"dig_read_spindx()");
1275 fprintf(fp,
"Nodes\n");
1284 fprintf(fp,
"Lines\n");
1293 fprintf(fp,
"Areas\n");
1302 fprintf(fp,
"Isles\n");
1315 static void rtree_read_node(
struct NodeBuffer *nb,
1327 for (i = 0; i < maxcard; i++) {
1360 rtree_read_node(&(t->
nb[
level][which]), nodepos, t, Plus);
1400 int hitCount = 0, found;
1403 struct spidxpstack s[MAXLEVEL];
1423 for (j = 0; j < maxcard; j++) {
1429 if (s[top].sn.level == 0) {
1430 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
1433 s[top].sn.branch[j].child.pos = s[top].pos[j];
1438 s[top].branch_id = i = 0;
1441 level = s[top].sn->level;
1444 for (i = s[top].branch_id; i < t->
nodecard; i++) {
1445 lastpos = s[top].sn->branch[i].child.pos;
1448 s[top++].branch_id = i + 1;
1449 s[top].sn = rtree_get_node(lastpos, level - 1, t, Plus);
1459 for (j = 0; j < maxcard; j++) {
1465 if (s[top].sn.level == 0) {
1466 s[top].sn.branch[j].child.id = (int)s[top].pos[j];
1469 s[top].sn.branch[j].child.pos = s[top].pos[j];
1473 s[top].branch_id = 0;
1485 for (i = 0; i < t->
leafcard; i++) {
1486 if (s[top].sn->branch[i].child.id &&
1490 if (!shcb((
int)s[top].sn->branch[i].child.id,
1491 &s[top].sn->branch[i].rect, cbarg)) {
struct Version_info spidx
Version info for spatial index file.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
off_t coor_size
Size of coor file.
void dig_rewind(struct gvfile *file)
Rewind file position.
size_t RTreeReadNode(struct RTree_Node *n, off_t nodepos, struct RTree *t)
int SearchHitCallback(int id, const struct RTree_Rect *rect, void *arg)
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
int dig__fwrite_port_I(const int *, size_t, struct gvfile *)
Write integers to the Portable Vector Format.
off_t Isle_spidx_offset
Offset of isles in sidx file.
int back_major
Earliest version that can use this data format (major)
int dig__fwrite_port_C(const char *, size_t, struct gvfile *)
Write chars to the Portable Vector Format.
int dig__fread_port_I(int *, size_t, struct gvfile *)
Read integers from the Portable Vector Format.
int spidx_with_z
2D/3D spatial index
int with_z
2D/3D vector data
int dig__fread_port_C(char *, size_t, struct gvfile *)
Read chars from the Portable Vector Format.
#define MAXLEVEL
Maximum verbosity level.
void RTreeCopyNode(struct RTree_Node *n1, struct RTree_Node *n2, struct RTree *t)
struct RTree * Isle_spidx
Isles spatial index.
int off_t_size
Size of off_t data type.
struct RTree * Node_spidx
Node spatial index.
void G_free(void *)
Free allocated memory.
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__fread_port_L(long *, size_t, struct gvfile *)
Read longs from the Portable Vector Format.
#define GRASS_VERSION_MINOR
struct RTree * Line_spidx
Line spatial index.
#define GV_SIDX_EARLIEST_MINOR
#define GV_SIDX_VER_MINOR
off_t dig_ftell(struct gvfile *file)
Get struct gvfile position.
int dig__fwrite_port_O(const off_t *, size_t, struct gvfile *, size_t)
Write off_ts to the Portable Vector Format.
off_t Area_spidx_offset
Offset of areas in sidx file.
#define PORT_DOUBLE
Sizes of types used in portable format (different names used in Vlib/ and diglib/ for the same thing)...
#define GRASS_VERSION_MAJOR
int dig_fflush(struct gvfile *file)
Flush struct gvfile.
void dig_spidx_free(struct Plus_head *)
Free spatial index (nodes, lines, areas, isles)
void RTreeFlushBuffer(struct RTree *t)
int off_t_size
Offset size.
struct gvfile spidx_fp
Spatial index file pointer.
#define GV_SIDX_EARLIEST_MAJOR
Basic topology-related info.
int dig_Wr_spidx(struct gvfile *fp, struct Plus_head *Plus)
Write spatial index to file.
#define assert(condition)
int major
Current version (major)
struct RTree * Area_spidx
Area spatial index.
int dig_Rd_spidx_head(struct gvfile *fp, struct Plus_head *ptr)
Read spatial index header from sidx file.
struct RTree_Branch * branch
int dig_dump_spidx(FILE *fp, const struct Plus_head *Plus)
Dump spatial index.
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
long spidx_head_size
Spatial index header size.
struct RTree_Node * RTreeAllocNode(struct RTree *t, int level)
off_t RTreeGetNodePos(struct RTree *t)
int dig_Wr_spidx_head(struct gvfile *fp, struct Plus_head *ptr)
Write spatial index header to file.
off_t Line_spidx_offset
Offset of lines in sidx file.
int byte_order
File byte order.
off_t G_ftell(FILE *)
Get the current file position of the stream.
int dig__fread_port_O(off_t *, size_t, struct gvfile *, size_t)
Read off_ts from the Portable Vector Format.
#define GV_SIDX_VER_MAJOR
FILE * file
File descriptor.
int dig_spidx_init(struct Plus_head *)
Initit spatial index (nodes, lines, areas, isles)
void G_warning(const char *,...) __attribute__((format(printf
off_t Volume_spidx_offset
Offset of volumes in sidx file.
struct Port_info spidx_port
Portability information for spatial index.
off_t Node_spidx_offset
Offset of nodes in sidx file.
size_t RTreeWriteNode(struct RTree_Node *n, struct RTree *t)
int minor
Current version (minor)
int dig_set_cur_port(struct Port_info *)
Set current Port_info structure.
int dig__fwrite_port_L(const long *, size_t, struct gvfile *)
Write longs to the Portable Vector Format.
int back_minor
Earliest version that can use this data format (minor)
int RTreeOverlap(struct RTree_Rect *r, struct RTree_Rect *s, struct RTree *t)
off_t Hole_spidx_offset
Offset of holes in sidx file.
int G_debug(int, const char *,...) __attribute__((format(printf
int dig_fseek(struct gvfile *file, off_t offset, int whence)
Set struct gvfile position.
struct Plus_head::@9 version
Backward compatibility version info.
int dig__fwrite_port_D(const double *, size_t, struct gvfile *)
Write doubles to the Portable Vector Format.
off_t Face_spidx_offset
Offset of faces in sidx file.
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...
int dig__fread_port_D(double *, size_t, struct gvfile *)
Read doubles from the Portable Vector Format.