The principle and realization of quadtree spatial indexing _ Quadtree

Source: Internet
Author: User

Today is still in the holiday, this will be previously written in the school four Fork tree things out and share with you.

The basic idea of quadtree indexing is to classify the geographical space recursively into different levels of tree structure. It divides the known range of space into four equal subspaces, so that recursion goes on until the level of the tree reaches a certain depth or when a requirement is met to stop the segmentation. The structure of Quadtree is relatively simple, and when spatial data objects are distributed more evenly, it has high spatial data insertion and query efficiency, so quadtree is one of the most common spatial indexes in GIS. The structure of the conventional quadtree is shown in the diagram, the geospatial objects are stored on the leaf nodes, and the intermediate nodes and the root nodes do not store the geospatial objects.

Four Fork Tree sketch map

Quadtree is more efficient for region query. However, if the spatial object distribution is uneven, with the continuous insertion of geo-spatial objects, the level of quadtree will continue to deepen, will form a serious unbalanced four-fork tree, then each query depth will be greatly increased, resulting in a sharp decline in query efficiency.

This section describes an improved four-fork tree indexing structure. The quadtree structure is a tree-like hierarchical structure which is divided from top to bottom. The traditional four-fork tree index has the following disadvantages:

(1) Space entities can only be stored in the leaf node, the middle node and the root node can not store space entity information, with the continuous insertion of space objects, will eventually lead to four-tree tree hierarchy is deep, in the Spatial Data window query efficiency will be relatively low.

(2) The same geographical entity is most likely to be stored in multiple nodes in the process of splitting the quadtree, which results in a waste of index storage space.

(3) Because geo-spatial objects may be unevenly distributed, this will result in the normal quadtree generating a very unbalanced tree, which can also cause the imbalance of tree structure and the waste of storage space.

The corresponding improvement method stores the geographic entity information in the smallest rectangular node that contains it completely, does not store in its parent node, each geographical entity only stores once in the tree, avoids the waste of the storage space. First, the full four-fork tree is generated to avoid the need to reallocate the memory when the geographic entity is inserted, to speed up the insertion, and finally to free up the empty node's memory space. The improved four-fork tree structure is shown in the following figure. The depth of the quadtree generally takes an empirical value of 4-7 between the best.

Graph improved four-fork tree structure

In order to maintain the consistency of spatial index and spatial data stored in a file or database, the following data structure is designed to support the operation of Quadtree.

(1) Four sub-region identification

Defines a planar area of four sub-region index number, the upper right is the first quadrant 0, the upper left is the second Quadrant 1, the lower left is the third Quadrant 2, the lower right is the fourth Quadrant 3.

typedef enum

{

ur = 0,//ur first quadrant

UL = 1,//UL is the second quadrant

LL = 2,/ll be the third quadrant

LR = 3//LR is the fourth quadrant

}quadrantenum;

(2) Spatial object data structure

Spatial object data structure is approximate to geo-spatial object, in spatial index, quite a few of them adopt MBR as approximation.

/* Space object MBR Information * *

typedef struct SHPMBRINFO

{

int NID; Space object ID Number

Maprect Box; Space object MBR range coordinates

}shpmbrinfo;

Nid is the identification number of the space object, and box is the smallest outsourced rectangle (MBR) of the space object.

(3) Four-fork tree node data structure

The Quadtree node is the main component of the quadtree structure, which is mainly used in the identification number and MBR of the storage space object, and is also the main part of the Quadtree algorithm operation.

/* Four fork tree node type structure * *

typedef struct QUADNODE

{

Maprect Box; The rectangular area that the node represents

int nshpcount; The number of all space objects that a node contains

shpmbrinfo* Pshapeobj; Array of space object pointers

int nchildcount; Number of child nodes

Quadnode *children[4]; Four children pointing to a node

}quadnode;

box is the smallest outsourcing rectangle that represents the corresponding region of Quadtree, and the minimum outsourced rectangle of the node in the upper layer contains the smallest outsourced rectangular area; Nshpcount represents the number of space objects contained in this node; Pshapeobj represents the first address of a space object's storage address, The space object of the same node is stored continuously in memory; The Nchildcount represents the number of child nodes owned by the node; Children is an array pointing to the children's node pointers.

The theory is all about the same, the following is affixed to my C language implementation version code.

The header file is as follows:


#ifndef __quadtree_h_59cae94a_e937_42ad_aa27_794e467715bb__ #define __quadtree_h_59cae94a_e937_42ad_aa27_    794e467715bb__ * A rectangular area of the Quadrant Division:: UL (1) |    UR (0)----------|-----------LL (2) |

LR (3) The following enumeration of the quadrant type */typedef enum {UR = 0, UL = 1, LL = 2, LR = 3}quadrantenum;		/* Space object MBR information */typedef struct SHPMBRINFO {int nID;	Space object ID number Maprect Box;

space object MBR range coordinate}shpmbrinfo;			/* Four fork tree node type structure * * typedef struct QUADNODE {maprect Box;		The rectangular region represented by the node is int nshpcount;		The number of all space objects contained in the node shpmbrinfo* pshapeobj;		space object pointer array int nchildcount;		Number of child nodes Quadnode *children[4];

Four children pointing to a node}quadnode;
	/* Four fork tree type structure */typedef struct QUADTREE_T {Quadnode *root;           int depth;


	The deep}quadtree of the quadtree;

	Initialization of Quadtree node Quadnode *initquadnode ();

	Layers create a four-fork tree method (full four fork tree) void Createquadtree (int depth,geolayer *polayer,quadtree* pquadtree);

	Create each branch void createquadbranch (int depth,maprect &rect,quadnode** node); Constructing quadtree spatial index void Buildquadtree (GEOLAYER*POlayer,quadtree* pquadtree);

	Quadtree index query (rectangular query) void Searchquadtree (quadnode* node,maprect &queryRect,vector<int>& itemsearched); Quadtree index query (rectangular query) parallel query void Searchquadtreepara (vector<quadnode*> resnodes,maprect &queryrect,vector<int

	>& itemsearched);

	Quadtree query (point query) void Ptsearchqtree (quadnode* node,double cx,double cy,vector<int>& itemsearched);

	Inserts the specified space object into the four-fork tree void Insert (long key,maprect &itemrect,quadnode* pnode);

	Inserts the specified space object into the four-forked tree void Insertquad (long key,maprect &itemrect,quadnode* pnode);

	Inserts the specified space object into the four-forked tree void InsertQuad2 (long key,maprect &itemrect,quadnode* pnode);

	Determine if a node is a leaf node bool Isquadleaf (quadnode* node);

	Remove redundant nodes bool Delfalsenode (quadnode* node);

	Quadtree traversal (all elements) void Traversalquadtree (quadnode* quadtree,vector<int>& Resvec);

	Quadtree traversal (all nodes) void Traversalquadtree (quadnode* quadtree,vector<quadnode*>& arrnode);

	Frees the memory space of the tree void Releasequadtree (quadnode** quadtree); CalculationThe size of the byte that the Quadtree occupies is a long calbytequadtree (quadnode* quadtree,long& nsize); #endif



from:http://blog.csdn.net/zhouxuguang236/article/details/12312099

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.