A polygon clipping method based on R-tree spatial index optimization
By adopting the polygon clipping method based on R-tree spatial index in the GIS system, the problem of hole point detection and exclusion when dealing with complex polygons is solved, and efficient and accurate polygon clipping effect is achieved.
Patent Information
- Application Number
- CN202510105843.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-23
- Publication Date
- 2025-05-06
- Estimated Expiration
- 2045-01-23
AI Technical Summary
In the prior art, when processing complex polygons containing holes, it is difficult to efficiently cut and eliminate hole points, resulting in low data processing efficiency and inaccurate results.
The optimized polygon clipping method based on R-tree spatial index is adopted to generate hole points, convert the polygon boundary into line features, and use the R-tree index to quickly locate and eliminate polygons containing holes.
It significantly improves the speed and accuracy of polygon cropping, reduces resource consumption, and can more accurately identify and eliminate polygons containing holes, adapting to more complex data processing needs.
Smart Images

Figure CN119513224B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of GIS (Geographic Information System) processing technology, and in particular to a polygon clipping method based on R-tree spatial index optimization. Background Art
[0002] Geographic Information System (GIS) technology plays a vital role in many fields such as urban planning, resource management, and environmental research. Among them, polygon clipping and optimization is a basic and frequently performed operation in GIS data processing. Traditional polygon clipping methods usually rely on complex geometric operations, which are inefficient when processing large amounts of data, especially when processing complex polygons containing holes, and often face a series of technical challenges, which directly affects the efficiency of data processing and the accuracy of the results.
[0003] When generating random points inside a polygon, traditional methods have difficulty ensuring that all generated points are strictly inside the polygon, especially when the polygon has a complex geometry or contains holes. This may cause the generated points to fall outside the polygon or inside the hole, affecting the correctness of subsequent analysis. When a polygon clipping operation encounters a target with holes, how to quickly locate and exclude these hole points becomes a difficult problem, especially when the data set is large and the hole distribution is complex. Traditional methods are often time-consuming and resource-intensive. Moreover, as the size of GIS data sets continues to increase, how to improve processing speed and reduce resource consumption while ensuring clipping accuracy is a major challenge facing modern GIS systems.
[0004] In 1984, Guttman et al. proposed a tree data structure for spatial access methods in the paper "R-Trees: A Dynamic Index Structure for SpatialSearching", referred to as R-tree. It is used to index multidimensional information, such as geographic coordinates, rectangles or polygons. Ivan Sutherland and GW Hodgman proposed "Reentrant Polygon Clipping" in 1974. This algorithm checks whether a polygon intersects with the clipping window (usually a rectangle) edge by edge, and then calculates the new polygon after clipping. One limitation of the Sutherland-Hodgman algorithm is that if there is a complex intersection between the original polygon and the clipping window, such as the polygon passes through the window multiple times, or the polygon itself has holes, the performance of the algorithm will become worse. Wang Jiechen et al. proposed in the paper "An Effective Complex Polygon Clipping Algorithm" published in 2010, which explores how to use the scan line idea and trapezoidal segmentation technology to clip polygons. However, no detailed solution is given for hole detection and exclusion. In 2016, Wang Haitao et al. published "Implementation and Improvement of Polygon Clipping Algorithm", which described a polygon clipping method that reduced the number of intersection judgments and storage, but its focus was on clipping the intersection of two polygons, without detailing how to effectively deal with the problem of holes inside polygons. In 2018, Kang Yaolong et al. published "Clipping Algorithm of Vector Graphics in Non-Self-Intersecting Polygons", which improved the traditional vector graphics clipping algorithm by reducing redundant calculations in loops, reusing objects, and multi-threaded control, but did not optimize the problem of holes inside polygons.
[0005] Although the above existing technologies have improved the efficiency of polygon clipping to a certain extent, they all have the common defect of failing to effectively handle hole points, which limits their application scope and performance when processing complex geospatial data. Summary of the invention
[0006] The technical problem to be solved by the present invention is to overcome the deficiencies of the prior art and provide an efficient, accurate and reliable polygon clipping method based on R-tree spatial index optimization, which is particularly optimized for the detection and exclusion of hole points to improve clipping accuracy and processing speed.
[0007] The technical solution adopted by the present invention is a polygon clipping method based on R-tree spatial index optimization, which comprises the following steps:
[0008] a. Loading data: loading the original polygonal surface feature data and clipping feature data to be clipped, wherein the clipping feature data includes line feature data and polygonal surface feature data;
[0009] b. Generate hole points: Identify and extract the location points of all holes from the polygonal surface feature data. The specific steps are as follows: when processing the original polygonal feature data to be clipped, traverse each polygon one by one, check whether each polygon contains a hollow area, and define the hollow area as a hole. For polygons containing holes, record the boundary points of all holes, and calculate a minimum rectangular bounding box that can completely surround the holes based on these boundary points. Then, randomly generate hole points in this rectangular bounding box, and use the ray method to ensure that the generated points are located in the holes, and ensure that each hole has a corresponding random point. Repeat this process until all holes have successfully generated corresponding hole points;
[0010] c. Convert polygonal boundaries to line features: merge all line features and then convert them into polygonal surface features;
[0011] d. Use the R-tree index to exclude polygons with holes, output and save the results.
[0012] Assume that the hole is considered as a different polygon, and the bounding box of the polygon is a rectangle, which completely contains the polygon, and the four sides of the bounding box are aligned with the minimum x coordinate minx, the minimum y coordinate miny, the maximum x coordinate maxx and the maximum y coordinate maxy of the polygon respectively, and the boundary value of the bounding box is calculated by the coordinates of all vertices of the polygon:
[0013] , , , ,
[0014] in, and are the x-coordinates and y-coordinates of all vertices of the polygon, i ranges from 1 to n, and n is a natural number;
[0015] Generating random points within the bounding box is represented as:
[0016] Randomly generate x coordinates:
[0017] Randomly generate y coordinates:
[0018] Among them, rand is a random number generated uniformly in the interval [0, 1];
[0019] Use the ray method to determine whether a point is inside the polygon: draw a horizontal ray parallel to the x-axis from the point to be tested, and count the number of intersections between the ray and the polygon boundary. If the number of intersections is an odd number, the point is inside the polygon, and the polygon is judged to contain a hole; if the number of intersections is an even number, the point is outside the polygon, and the polygon is judged to contain no holes.
[0020] The specific steps of step c are: converting the original polygonal surface feature data to be clipped and the clipping feature data into line feature data, then merging the original line feature and the polygonal boundary line feature into one line feature, and finally, converting the merged line feature into a new polygon set.
[0021] The specific steps to convert area features into line features are:
[0022] Suppose a polygon P, whose boundary consists of vertices (x1,y1), (x2,y2),…, (x n ,y n ) are connected in sequence,
[0023] The boundary line B of the polygon forms a closed polyline, which is expressed as:
[0024] ,
[0025] Merging all line features into a single geometric object, there are multiple line features L1, L2, ..., L m , the merged line feature L is expressed as:
[0026] ,
[0027] Connect a set of line features L and form a closed path, that is, construct a surface feature polygon Q, where n and m are both natural numbers.
[0028] In the step d, the specific steps of excluding polygons containing holes are: creating a spatial index, then checking whether the polygon contains hole points, and finally filtering the polygons containing holes.
[0029] The specific steps of creating a spatial index, then checking whether the polygon contains holes, and finally filtering the polygons containing holes are as follows: suppose a set of hole points , each point P i There is a bounding box B i :
[0030] ,
[0031] Create an R-tree index, which is used to store the bounding boxes of the hole points, where i ranges from 1 to n, and n is a natural number.
[0032] For each polygon Q, check its bounding box B Q Whether it intersects with the bounding box of any hole point. If the polygon contains any hole point, it is considered to contain a hole, and finally the polygon containing the hole is filtered out.
[0033] In the step d, the final polygon clipping result is saved as a new geospatial data file in ESRI Shapefile format.
[0034] The beneficial effects of the present invention are:
[0035] 1. Enhanced efficiency: By using the R-tree index, this method can significantly improve the speed of clipping operations, especially when processing large-scale geospatial data sets. The R-tree index reduces unnecessary geometric comparisons through spatial approximation and hierarchical structure, making it possible to quickly locate and exclude irrelevant geometric objects during polygon clipping, thereby greatly improving clipping efficiency.
[0036] 2. Improved accuracy: By accurately generating random points inside polygons and automatically detecting hole points, this method can more accurately identify and exclude polygons containing holes, avoiding misjudgments or omissions that may occur in traditional methods, and ensuring the geometric integrity and accuracy of the clipping results.
[0037] 3. Complexity reduction: This method simplifies the polygon clipping process by converting polygon boundaries into line features and further merging line features. This line feature conversion and merging strategy reduces the difficulty of processing complex polygons while maintaining the accuracy of clipping operations.
[0038] 4. Reduced resource consumption: Due to the use of optimized spatial indexing technology and precise random point generation algorithm, this method is more economical in memory usage and computing resources than traditional methods. This is especially important for processing large-scale data sets because it avoids performance bottlenecks caused by resource limitations.
[0039] Overall, the method of the present invention combines multiple methods such as spatial indexing, random point generation, polygon conversion and merging, which significantly improves the efficiency and accuracy of geographic data processing. Compared with previous technologies, this technical solution is more flexible, efficient, and reliable, and can adapt to more complex data processing needs, bringing significant improvements and advantages to the field of geographic information systems and spatial analysis. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 This is a flow chart of the polygon clipping method based on R-tree index optimization;
[0041] Figure 2 It is a visualization of the polygonal surface feature data to be clipped;
[0042] Figure 3 It is a visualization of the clipping feature data;
[0043] Figure 4 It is a visualization of the final result data over a large range;
[0044] Figure 5 It is a visualization of the polygon feature data to be clipped;
[0045] Figure 6 It is a visualization of the clipping feature data;
[0046] Figure 7 It is a visualization of polygonal feature data containing holes;
[0047] Figure 8 Use hole points to mark the hole map;
[0048] Fig. 9 is a visualization of the final result data;
[0049] Fig.10 This is a simplified diagram of the hole point definition process;
[0050] Fig.11 It is a simple schematic diagram of the position of a point determined by the ray method. DETAILED DESCRIPTION
[0051] The method of the present invention comprises the following steps:
[0052] a. Loading data: loading the original polygonal surface feature data and clipping feature data to be clipped, wherein the clipping feature data includes line feature data and polygonal surface feature data;
[0053] b. Generate hole points: Identify and extract the location points of all holes from the polygonal surface feature data;
[0054] c. Convert polygonal boundaries to line features: merge all line features and then convert them into polygonal surface features;
[0055] d. Use the R-tree index to exclude polygons with holes, output and save the results.
[0056] Specifically, the specific steps of step b are as follows: when processing the original polygonal feature data to be clipped, traverse each polygon one by one, check whether each polygon contains a hollow area, define the hollow area as a hole, and for polygons containing holes, record the boundary points of all holes, and calculate a minimum rectangular bounding box that can completely surround the holes based on these boundary points, then randomly generate hole points in this rectangular bounding box, and ensure that the generated points are located in the holes through the ray method, and ensure that each hole has a corresponding random point, and repeat this process until all holes successfully generate corresponding hole points. Fig.10 As shown in the figure, assume that area L is a hole, generate a minimum circumscribed rectangle (rectangular area M) outside area L, and randomly generate a point within the rectangular area M. Use the ray method to ensure that the generated point is within area L. We call this point a hole point. In the figure, the elliptical area L is a hole, the rectangle M connected to the outside of area L is the minimum rectangular bounding box surrounding the hole, and N is a randomly generated hole point. Fig.11 As shown, the ray method is used to ensure that the randomly generated hole points are located inside the hole.
[0057] Assume that the hole is considered as a different polygon, and the bounding box of the polygon is a rectangle, which completely contains the polygon, and the four sides of the bounding box are aligned with the minimum x coordinate minx, the minimum y coordinate miny, the maximum x coordinate maxx and the maximum y coordinate maxy of the polygon respectively, and the boundary value of the bounding box is calculated by the coordinates of all vertices of the polygon:
[0058] , , , ,
[0059] in, and are the x-coordinates and y-coordinates of all vertices of the polygon, i ranges from 1 to n, and n is a natural number;
[0060] Generating random points within the bounding box is represented as:
[0061] Randomly generate x coordinates:
[0062] Randomly generate y coordinates:
[0063] Among them, rand is a random number generated uniformly in the interval [0, 1];
[0064] like Fig.11As shown, G is a point inside the hole, and H is a point outside the hole; use the ray method to determine whether a point is inside the polygon: draw a horizontal ray parallel to the x-axis from the point to be tested, and count the number of intersections between the ray and the polygon boundary. If the number of intersections is an odd number, the point is inside the polygon, and the polygon is judged to contain a hole; if the number of intersections is an even number, the point is outside the polygon, and the polygon is judged to not contain a hole.
[0065] The specific steps of step c are: converting the original polygonal surface feature data to be clipped and the clipping feature data into line feature data, then merging the original line feature and the polygonal boundary line feature into one line feature, and finally, converting the merged line feature into a new polygon set.
[0066] The specific steps to convert area features into line features are:
[0067] Suppose a polygon P, whose boundary consists of vertices (x1,y1), (x2,y2),…, (x n ,y n ) are connected in sequence,
[0068] The boundary line B of the polygon forms a closed polyline, which is expressed as:
[0069]
[0070] Merging all line features into a single geometric object, there are multiple line features L1, L2, ..., L m , the merged line feature L is expressed as:
[0071] ,
[0072] Connect a set of line features L and form a closed path, that is, construct a surface feature polygon Q, where n and m are both natural numbers.
[0073] In the step d, the specific steps of excluding polygons containing holes are: creating a spatial index, then checking whether the polygon contains hole points, and finally filtering the polygons containing holes.
[0074] The specific steps of creating a spatial index, then checking whether the polygon contains holes, and finally filtering the polygons containing holes are as follows: suppose a set of hole points , each point P i There is a bounding box B i :
[0075]
[0076] Create an R-tree index, which is used to store the bounding boxes of the hole points, where i ranges from 1 to n, and n is a natural number;
[0077] For each polygon Q, check its bounding box B Q Whether it intersects with the bounding box of any hole point. If the polygon contains any hole point, it is considered to contain a hole, and finally the polygon containing the hole is filtered out.
[0078] In the step d, the final polygon clipping result is saved as a new geospatial data file in ESRI Shapefile format.
[0079] like Figures 1 to 9 As shown, the present invention is described in detail below in conjunction with specific embodiments.
[0080] Step a: First, two core tasks need to be handled: one is to read and prepare the area feature dataset to be clipped, and the other is to import the clipping features, that is, line features or area features. In this embodiment, line features are selected as clipping features.
[0081] First, we focus on the face feature file to be clipped, which is a collection of 989 independent face features, each of which represents a closed area in geographic space. In order to intuitively understand the data distribution and structure, we performed a preliminary visualization to reveal the global characteristics of the dataset, such as Figure 2 The local zoom visualization result is shown in Figure 5 shown.
[0082] For the clipping feature file, it consists of 17,814 line features. The large number of clipping features indicates the complexity and precision of the clipping operation. Each line feature may participate in the clipping process and affect the final shape and range of the surface feature. The visualization result is as follows Figure 3 As shown, the local zoom visualization result is as follows Figure 6 shown.
[0083] Step b: Traverse the 989 face feature data to be clipped, and check whether there are holes in the traversal process. If there is a hole, the hole is regarded as a new polygon, and a point is randomly generated inside the polygon. This point is called a hole point. The hole point is used to mark the holes in the face feature data, and a total of 1473 hole points are generated. In this operation, a total of 1473 hole points are generated, which means that at least 1473 holes are found in 989 face features.
[0084] Step c: Extract the boundary lines of 989 area features to be clipped and convert them into line features to be clipped. Merge the extracted 989 line features to be clipped and the other 17,814 clipping line features into the same dataset. This is usually done by merging two line feature layers into a new layer to ensure that all line segments are included in a unified data structure. Find closed paths composed of line segments that define the boundaries of polygons and construct new polygons through closed lines. That is, geometric polygonization. The local zoom-in visualization result is as follows Figure 7 shown.
[0085] Step d: R-tree, as a multidimensional index structure designed for spatial data, can effectively manage the location information of spatial objects by building a hierarchical index tree. In this case, we first established an R-tree index for 1473 hole points. This process is essentially to organize and store the bounding box of each hole point to form a hierarchical structure based on spatial location. For 12998 surface features, we took advantage of the R-tree index to quickly locate the surface features that potentially contain hole points through the intersection of bounding boxes. The core of this strategy is that it can quickly narrow the scope of accurate spatial inclusion tests and avoid detailed geometric comparisons of all surface features with hole points one by one. Specifically, for each surface feature, the R-tree index is first used to find the set of hole points that may intersect with its bounding box. Subsequently, only these limited candidate hole points are subjected to detailed inclusion tests to determine whether they are actually inside the surface feature. This series of steps ensures that only those surface features that actually contain hole points will be identified and then excluded from the subsequent polygon clipping process. The local zoomed-in visualization results of 12,998 polygon feature data with holes and 1,473 hole points are shown in the figure below. Figure 8 shown.
[0086] The last step is to save the final polygon clipping result as a new geospatial data file in ESRI Shapefile format to ensure that the final polygon clipping result is consistent with the coordinate system of the original data. The visualization result is as follows: Figure 4 As shown, the local zoom visualization result is as follows Fig. 9 shown.
[0087] Finally, it should be emphasized that the above is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various changes and modifications. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A polygon clipping method based on R-tree spatial index optimization, characterized in that: The method comprises the following steps: a. Loading data: loading the original polygonal surface feature data and clipping feature data to be clipped, wherein the clipping feature data includes line feature data and polygonal surface feature data; b. Generate hole points: Identify and extract the location points of all holes from the polygonal surface feature data. The specific steps are as follows: when processing the original polygonal feature data to be clipped, traverse each polygon one by one, check whether each polygon contains a hollow area, and define the hollow area as a hole. For polygons containing holes, record the boundary points of all holes, and calculate a minimum rectangular bounding box that can completely surround the holes based on these boundary points. Then, randomly generate hole points in this rectangular bounding box, and use the ray method to ensure that the generated points are located in the holes, and ensure that each hole has a corresponding random point. Repeat this process until all holes have successfully generated corresponding hole points; c. Convert polygonal boundaries to line features: merge all line features and then convert them into polygonal surface features; d. Use the R-tree index to exclude polygons with holes, output and save the results; Assume that the hole is considered as a different polygon, and the bounding box of the polygon is a rectangle, which completely contains the polygon, and the four sides of the bounding box are aligned with the minimum x coordinate minx, the minimum y coordinate miny, the maximum x coordinate maxx and the maximum y coordinate maxy of the polygon respectively, and the boundary value of the bounding box is calculated by the coordinates of all vertices of the polygon: , , , , in, and are the x- and y-coordinates of all the vertices of the polygon, i The value ranges from 1 to n, where n is a natural number; Generating random points within the bounding box is represented as: Randomly generate x coordinates: , Randomly generate y coordinates: , Among them, rand is a random number generated uniformly in the interval [0, 1]; Use the ray method to determine whether a point is inside the polygon: draw a horizontal ray parallel to the x-axis from the point to be tested, and count the number of intersections between the ray and the polygon boundary. If the number of intersections is an odd number, the point is inside the polygon, and the polygon is judged to contain a hole; if the number of intersections is an even number, the point is outside the polygon, and the polygon is judged to contain no holes.
2. The polygon clipping method based on R-tree spatial index optimization according to claim 1, characterized in that: The specific steps of step c are: converting the original polygonal surface feature data to be clipped and the clipping feature data into line feature data, then merging the original line feature and the polygonal boundary line feature into one line feature, and finally, converting the merged line feature into a new polygon set.
3. The polygon clipping method based on R-tree spatial index optimization according to claim 2, characterized in that: The specific steps to convert area features into line features are: Set polygon P , whose boundary is composed of vertices ( x 1 ,y 1 ), x 2 ,y 2 ),…,( x n ,y n ) are connected in sequence, The boundary line B of the polygon forms a closed polyline, which is expressed as: , Merge all line features into a single geometric object. L 1 ,L 2 ,…,L m , the merged line feature L is expressed as: , Connect a set of line features L and form a closed path, that is, construct a polygon Q, where: n , m All are natural numbers.
4. The polygon clipping method based on R-tree spatial index optimization according to claim 1, characterized in that: In the step d, the specific steps of excluding polygons containing holes are: creating a spatial index, then checking whether the polygon contains hole points, and finally filtering the polygons containing holes.
5. The polygon clipping method based on R-tree spatial index optimization according to claim 4, characterized in that: The specific steps of creating a spatial index, then checking whether the polygon contains holes, and finally filtering the polygons containing holes are as follows: suppose a set of hole points , each point P i There is a bounding box B i : , Create an R-tree index to store the bounding boxes of the hole points, where i The value ranges from 1 to n, where n is a natural number. For each polygon Q, check its bounding box B Q Whether it intersects with the bounding box of any hole point. If the polygon contains any hole point, it is considered to contain a hole, and finally the polygon containing the hole is filtered out.
6. The polygon clipping method based on R-tree spatial index optimization according to claim 1, characterized in that: In the step d, the final polygon clipping result is saved as a new geospatial data file in ESRI Shapefile format.
Citation Information
Patent Citations
High-performance intersection method for cutting polygons
CN112819223A