Debian Codegraph @

www/geopoly.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The Geopoly Interface To The SQLite R*Tree Module</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 The Geopoly Interface To The SQLite R*Tree Module
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc2"><a href="#geojson">1.1. GeoJSON</a></div>
130 <div class="fancy-toc2"><a href="#binary_storage_format">1.2. Binary storage format</a></div>
131 <div class="fancy-toc1"><a href="#using_the_geopoly_extension">2. Using The Geopoly Extension</a></div>
132 <div class="fancy-toc2"><a href="#queries">2.1. Queries</a></div>
133 <div class="fancy-toc1"><a href="#special_functions">3. Special Functions</a></div>
134 <div class="fancy-toc2"><a href="#the_geopoly_overlap_p1_p2_function">3.1. The geopoly_overlap(P1,P2) Function</a></div>
135 <div class="fancy-toc2"><a href="#the_geopoly_within_p1_p2_function">3.2. The geopoly_within(P1,P2) Function</a></div>
136 <div class="fancy-toc2"><a href="#the_geopoly_area_p_function">3.3. The geopoly_area(P) Function</a></div>
137 <div class="fancy-toc2"><a href="#the_geopoly_blob_p_function">3.4. The geopoly_blob(P) Function</a></div>
138 <div class="fancy-toc2"><a href="#the_geopoly_json_p_function">3.5. The geopoly_json(P) Function</a></div>
139 <div class="fancy-toc2"><a href="#the_geopoly_svg_p_function">3.6. The geopoly_svg(P,...) Function</a></div>
140 <div class="fancy-toc2"><a href="#the_geopoly_bbox_p_and_geopoly_group_bbox_p_functions">3.7. The geopoly_bbox(P) and geopoly_group_bbox(P) Functions</a></div>
141 <div class="fancy-toc2"><a href="#the_geopoly_contains_point_p_x_y_function">3.8. The geopoly_contains_point(P,X,Y) Function</a></div>
142 <div class="fancy-toc2"><a href="#the_geopoly_xform_p_a_b_c_d_e_f_function">3.9. The geopoly_xform(P,A,B,C,D,E,F) Function</a></div>
143 <div class="fancy-toc2"><a href="#the_geopoly_regular_x_y_r_n_function">3.10. The geopoly_regular(X,Y,R,N) Function</a></div>
144 <div class="fancy-toc2"><a href="#the_geopoly_ccw_j_function">3.11. The geopoly_ccw(J) Function</a></div>
145 <div class="fancy-toc1"><a href="#implementation_details">4. Implementation Details</a></div>
146 <div class="fancy-toc2"><a href="#binary_encoding_of_polygons">4.1. Binary Encoding of Polygons</a></div>
147 <div class="fancy-toc2"><a href="#shadow_tables">4.2. Shadow Tables</a></div>
148 </div>
149 </div>
150 <script>
151 function toggle_toc(){
152 var sub = document.getElementById("toc_sub")
153 var mk = document.getElementById("toc_mk")
154 if( sub.style.display!="block" ){
155 sub.style.display = "block";
156 mk.innerHTML = "&#x25bc;";
157 } else {
158 sub.style.display = "none";
159 mk.innerHTML = "&#x25ba;";
160 }
161 }
162 </script>
163 </div>
164
165
166
167
168 <h1 id="overview"><span>1. </span>Overview</h1>
169
170 <p>
171 The Geopoly module is an alternative interface to the <a href="rtree.html">R-Tree extension</a> that uses
172 the <a href="http://geojson.org">GeoJSON</a> notation
173 (<a href="https://tools.ietf.org/html/rfc7946">RFC-7946</a>) to describe two-dimensional
174 polygons. Geopoly includes functions for detecting when one polygon is
175 contained within or overlaps with another, for computing the
176 area enclosed by a polygon, for doing linear transformations of polygons,
177 for rendering polygons as
178 <a href="https://en.wikipedia.org/wiki/Scalable_Vector_Graphics">SVG</a>, and other
179 similar operations.
180
181 </p><p>
182 The source code for Geopoly is included in the <a href="amalgamation.html">amalgamation</a>. However,
183 depending on configuration options and the a particular version of SQLite
184 you are using, the Geopoly extension may or may not be enabled by default.
185 To ensure that Geopoly is enabled for your build,
186 add the <a href="compile.html#enable_geopoly">-DSQLITE_ENABLE_GEOPOLY=1</a> compile-time option.
187
188 </p><p>
189 Geopoly operates on "simple" polygons - that is, polygons for which
190 the boundary does not intersect itself. Geopoly thus extends the capabilities
191 of the <a href="rtree.html">R-Tree extension</a> which can only deal with rectangular areas.
192 On the other hand, the <a href="rtree.html">R-Tree extension</a> is
193 able to handle between 1 and 5 coordinate dimensions, whereas Geopoly is restricted
194 to 2-dimensional shapes only.
195
196 </p><p>
197 Each polygon in the Geopoly module can be associated with an arbitrary
198 number of auxiliary data fields.
199
200 </p><h2 id="geojson"><span>1.1. </span>GeoJSON</h2>
201
202 <p>The <a href="https://tools.ietf.org/html/rfc7946">GeoJSON standard</a> is syntax for
203 exchanging geospatial information using JSON. GeoJSON is a rich standard
204 that can describe nearly any kind of geospatial content.
205
206 </p><p>The Geopoly module only understands
207 a small subset of GeoJSON, but a critical subset.
208 In particular, GeoJSON understands
209 the JSON array of vertexes that describes a simple polygon.
210
211 </p><p>A polygon is defined by its vertexes.
212 Each vertex is a JSON array of two numeric values which are the
213 X and Y coordinates of the vertex.
214 A polygon is a JSON array of at least four of these vertexes,
215 and hence is an array of arrays.
216 The first and last vertex in the array must be the same.
217 The polygon follows the right-hand rule: When tracing a line from
218 one vertex to the next, the area to the right of the line is outside
219 of the polygon and the area to the left is inside the polygon.
220 In other words, the net rotation of the vertexes is counter-clockwise.
221
222 </p><p>
223 For example, the following JSON describes an isosceles triangle, sitting
224 on the X axis and with an area of 0.5:
225
226 </p><div class="codeblock"><pre>&#91;&#91;0,0],&#91;1,0],&#91;0.5,1],&#91;0,0]]
227 </pre></div>
228
229 <p>
230 A triangle has three vertexes, but the GeoJSON description of the triangle
231 has 4 vertexes because the first and last vertex are duplicates.
232
233 </p><h2 id="binary_storage_format"><span>1.2. </span>Binary storage format</h2>
234
235 <p>
236 Internally, Geopoly stores polygons in a binary format - an SQL BLOB.
237 Details of the binary format are given below.
238 All of the Geopoly interfaces are able to accept polygons in either the
239 GeoJSON format or in the binary format.
240
241 </p><h1 id="using_the_geopoly_extension"><span>2. </span>Using The Geopoly Extension</h1>
242
243 <p>
244 A geopoly table is created as follows:
245
246 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE newtab USING geopoly(a,b,c);
247 </pre></div>
248
249 <p>
250 The statement above creates a new geopoly table named "newtab".
251 Every geopoly table contains a built-in integer "rowid" column
252 and a "_shape" column that contains
253 the polygon associated with that row of the table.
254 The example above also defines three auxiliary data columns
255 named "a", "b", and "c" that can store whatever additional
256 information the application needs to associate
257 with each polygon. If there is no need to store auxiliary
258 information, the list of auxiliary columns can be omitted.
259
260 </p><p>
261 Store new polygons in the table using ordinary INSERT statements:
262
263 </p><div class="codeblock"><pre>INSERT INTO newtab(_shape) VALUES('&#91;&#91;0,0],&#91;1,0],&#91;0.5,1],&#91;0,0]]');
264 </pre></div>
265
266 <p>
267 UPDATE and DELETE statements work similarly.
268
269 </p><h2 id="queries"><span>2.1. </span>Queries</h2>
270
271 <p>
272 To query the geopoly table using an indexed geospatial search,
273 use one of the functions geopoly_overlap()
274 or geopoly_within() as a boolean function in the WHERE clause,
275 with the "_shape" column as the first argument to the function.
276 For example:
277
278 </p><div class="codeblock"><pre>SELECT * FROM newtab WHERE geopoly_overlap(_shape, $query_polygon);
279 </pre></div>
280
281 <p>
282 The previous example will return every row for which the _shape
283 overlaps the polygon in the $query_polygon parameter. The
284 geopoly_within() function works similarly, but only returns rows for
285 which the _shape is completely contained within $query_polygon.
286
287 </p><p>
288 Queries (and also DELETE and UPDATE statements) in which the WHERE
289 clause contains a bare geopoly_overlap() or geopoly_within() function
290 make use of the underlying R*Tree data structures for a fast lookup that
291 only has to examine a subset of the rows in the table. The number of
292 rows examines depends, of course, on the size of the $query_polygon.
293 Large $query_polygons will normally need to look at more rows than small
294 ones.
295
296 </p><p>
297 Queries against the rowid of a geopoly table are also very quick, even
298 for tables with a vast number of rows.
299 However, none of the auxiliary data columns are indexes, and so queries
300 against the auxiliary data columns will involve a full table scan.
301
302 </p><h1 id="special_functions"><span>3. </span>Special Functions</h1>
303
304 <p>
305 The geopoly module defines several new SQL functions that are useful for
306 dealing with polygons. All polygon arguments to these functions can be
307 either the GeoJSON format or the internal binary format.
308
309 <a name="goverlap"></a>
310
311 </p><h2 id="the_geopoly_overlap_p1_p2_function"><span>3.1. </span>The geopoly_overlap(P1,P2) Function</h2>
312
313 <p>
314 If P1 and P2 are both polygons, then the geopoly_overlap(P1,P2) function returns
315 a non-zero integer if there is any overlap between P1 and P2, or it returns
316 zero if P1 and P2 completely disjoint.
317 If either P1 or P2 is not a polygon, this routine returns NULL.
318
319 </p><p>
320 The geopoly_overlap(P1,P2) function is special in that the geopoly virtual
321 table knows how to use R*Tree indexes to optimize queries in which the
322 WHERE clause uses geopoly_overlap() as a boolean function. Only the
323 geopoly_overlap(P1,P2) and geopoly_within(P1,P2) functions have this
324 capability.
325
326 <a name="gwithin"></a>
327
328 </p><h2 id="the_geopoly_within_p1_p2_function"><span>3.2. </span>The geopoly_within(P1,P2) Function</h2>
329
330 <p>
331 If P1 and P2 are both polygons, then the geopoly_within(P1,P2) function returns
332 a non-zero integer if P1 is completely contained within P2, or it returns zero
333 if any part of P1 is outside of P2. If P1 and P2 are the same polygon, this routine
334 returns non-zero.
335 If either P1 or P2 is not a polygon, this routine returns NULL.
336
337 </p><p>
338 The geopoly_within(P1,P2) function is special in that the geopoly virtual
339 table knows how to use R*Tree indexes to optimize queries in which the
340 WHERE clause uses geopoly_within() as a boolean function. Only the
341 geopoly_within(P1,P2) and geopoly_overlap(P1,P2) functions have this
342 capability.
343
344 <a name="garea"></a>
345
346 </p><h2 id="the_geopoly_area_p_function"><span>3.3. </span>The geopoly_area(P) Function</h2>
347
348 <p>
349 If P is a polygon, then geopoly_area(P) returns the area enclosed by
350 that polygon. If P is not a polygon, geopoly_area(P) returns NULL.
351
352 <a name="gblob"></a>
353
354 </p><h2 id="the_geopoly_blob_p_function"><span>3.4. </span>The geopoly_blob(P) Function</h2>
355
356 <p>
357 If P is a polygon, then geopoly_blob(P) returns the binary encoding
358 of that polygon as a BLOB.
359 If P is not a polygon, geopoly_blob(P) returns NULL.
360
361 <a name="gjson"></a>
362
363 </p><h2 id="the_geopoly_json_p_function"><span>3.5. </span>The geopoly_json(P) Function</h2>
364
365 <p>
366 If P is a polygon, then geopoly_json(P) returns the GeoJSON representation
367 of that polygon as a TEXT string.
368 If P is not a polygon, geopoly_json(P) returns NULL.
369
370 <a name="gsvg"></a>
371
372 </p><h2 id="the_geopoly_svg_p_function"><span>3.6. </span>The geopoly_svg(P,...) Function</h2>
373
374 <p>
375 If P is a polygon, then geopoly_svg(P,...) returns a text string which is a
376 <a href="https://en.wikipedia.org/wiki/Scalable_Vector_Graphics">Scalable Vector Graphics (SVG)</a>
377 representation of that polygon. If there is more one argument, then second
378 and subsequent arguments are added as attributes to each SVG glyph. For example:
379
380 </p><div class="codeblock"><pre>SELECT geopoly_svg($polygon,'class="poly"','style="fill:blue;"');
381 </pre></div>
382
383 <p>
384 If P is not a polygon, geopoly_svg(P,...) returns NULL.
385
386 </p><p>
387 Note that geopoly uses a traditional right-handed cartesian coordinate system
388 with the origin at the lower left, whereas SVG uses a left-handed coordinate
389 system with the origin at the upper left. The geopoly_svg() routine makes no
390 attempt to transform the coordinate system, so the displayed images are shown
391 in mirror image and rotated. If that is undesirable, the geopoly_xform() routine
392 can be used to transform the output from cartesian to SVG coordinates prior to
393 passing the polygons into geopoly_svg().
394
395 <a name="gbbox"></a>
396
397 </p><h2 id="the_geopoly_bbox_p_and_geopoly_group_bbox_p_functions"><span>3.7. </span>The geopoly_bbox(P) and geopoly_group_bbox(P) Functions</h2>
398
399 <p>
400 If P is a polygon, then geopoly_bbox(P) returns a new polygon that is
401 the smallest (axis-aligned) rectangle completely enclosing P.
402 If P is not a polygon, geopoly_bbox(P) returns NULL.
403
404 </p><p>
405 The geopoly_group_bbox(P) function is an aggregate version of geopoly_bbox(P).
406 The geopoly_group_bbox(P) function returns the smallest rectangle that will
407 enclose all P values seen during aggregation.
408
409 <a name="gpoint"></a>
410
411 </p><h2 id="the_geopoly_contains_point_p_x_y_function"><span>3.8. </span>The geopoly_contains_point(P,X,Y) Function</h2>
412
413 <p>
414 If P is a polygon, then geopoly_contains_point(P,X,Y) returns a
415 non-zero integer if and only
416 if the coordinate X,Y is inside or on the boundary of the polygon P.
417 If P is not a polygon, geopoly_contains_point(P,X,Y) returns NULL.
418
419 <a name="xform"></a>
420
421 </p><h2 id="the_geopoly_xform_p_a_b_c_d_e_f_function"><span>3.9. </span>The geopoly_xform(P,A,B,C,D,E,F) Function</h2>
422
423 <p>
424 The geopoly_xform(P,A,B,C,D,E,F) function returns a new polygon that is an
425 affine transformation of the polygon P and where the transformation
426 is defined by values A,B,C,D,E,F. If P is not a valid polygon, this
427 routine returns NULL.
428
429 </p><p>
430 The transformation converts each vertex of the polygon according to the
431 following formula:
432
433 </p><div class="codeblock"><pre>x1 = A*x0 + B*y0 + E
434 y1 = C*x0 + D*y0 + F
435 </pre></div>
436
437 <p>
438 So, for example, to move a polygon by some amount DX, DY without changing
439 its shape, use:
440
441 </p><div class="codeblock"><pre>geopoly_xform($polygon, 1, 0, 0, 1, $DX, $DY)
442 </pre></div>
443
444 <p>
445 To rotate a polygon by R radians around the point 0, 0:
446
447 </p><div class="codeblock"><pre>geopoly_xform($polygon, cos($R), sin($R), -sin($R), cos($R), 0, 0)
448 </pre></div>
449
450 <p>
451 Note that a transformation that flips the polygon might cause the
452 order of vertexes to be reversed. In other words, the transformation
453 might cause the vertexes to circulate in clockwise order instead of
454 counter-clockwise. This can be corrected by sending the result
455 through the <a href="geopoly.html#ccw">geopoly_ccw()</a> function after transformation.
456
457
458 <a name="regpoly"></a>
459
460 </p><h2 id="the_geopoly_regular_x_y_r_n_function"><span>3.10. </span>The geopoly_regular(X,Y,R,N) Function</h2>
461
462 <p>
463 The geopoly_regular(X,Y,R,N) function returns a convex, simple, regular,
464 equilateral, equiangular polygon with N sides, centered at X,Y, and with
465 a circumradius of R. Or, if R is negative or if N is less than 3, the
466 function returns NULL. The N value is capped at 1000 so that the routine
467 will never render a polygon with more than 1000 sides even if the N value
468 is larger than 1000.
469
470 </p><p>
471 As an example, the following graphic:
472
473 </p><blockquote>
474 <svg width="600" height="300" style="border:1px solid black">
475 <polyline points="140.003,100 80.0019,134.644 80.0019,65.3565 140.003,100" style="fill:none;stroke:red;stroke-width:2"></polyline> <text x="100" y="106" alignment-baseline="central" text-anchor="middle">3</text>
476 <polyline points="240.003,100 200,140.003 159.997,100 200,59.9973 240.003,100" style="fill:none;stroke:orange;stroke-width:2"></polyline> <text x="200" y="106" alignment-baseline="central" text-anchor="middle">4</text>
477 <polyline points="340.003,100 312.358,138.042 267.637,123.511 267.637,76.4893 312.358,61.9583 340.003,100" style="fill:none;stroke:green;stroke-width:2"></polyline> <text x="300" y="106" alignment-baseline="central" text-anchor="middle">5</text>
478 <polyline points="440.003,100 419.998,134.644 380.002,134.644 359.997,100 380.002,65.3565 419.998,65.3565 440.003,100" style="fill:none;stroke:blue;stroke-width:2"></polyline> <text x="400" y="106" alignment-baseline="central" text-anchor="middle">6</text>
479 <polyline points="540.003,100 524.94,131.276 491.101,138.995 463.959,117.353 463.959,82.6471 491.101,61.005 524.94,68.7243 540.003,100" style="fill:none;stroke:purple;stroke-width:2"></polyline> <text x="500" y="106" alignment-baseline="central" text-anchor="middle">7</text>
480 <polyline points="140.003,200 128.286,228.286 100,240.003 71.7143,228.286 59.9973,200 71.7143,171.714 100,159.997 128.286,171.714 140.003,200" style="fill:none;stroke:red;stroke-width:2"></polyline> <text x="100" y="206" alignment-baseline="central" text-anchor="middle">8</text>
481 <polyline points="240.003,200 232.363,223.511 212.358,238.042 187.642,238.042 167.637,223.511 159.997,200 167.637,176.489 187.642,161.958 212.358,161.958 232.363,176.489 240.003,200" style="fill:none;stroke:orange;stroke-width:2"></polyline> <text x="200" y="206" alignment-baseline="central" text-anchor="middle">10</text>
482 <polyline points="340.003,200 334.644,219.998 319.998,234.644 300,240.003 280.002,234.644 265.356,219.998 259.997,200 265.356,180.002 280.002,165.356 300,159.997 319.998,165.356 334.644,180.002 340.003,200" style="fill:none;stroke:green;stroke-width:2"></polyline> <text x="300" y="206" alignment-baseline="central" text-anchor="middle">12</text>
483 <polyline points="440.003,200 436.956,215.305 428.286,228.286 415.305,236.956 400,240.003 384.695,236.956 371.714,228.286 363.044,215.305 359.997,200 363.044,184.695 371.714,171.714 384.695,163.044 400,159.997 415.305,163.044 428.286,171.714 436.956,184.695 440.003,200" style="fill:none;stroke:blue;stroke-width:2"></polyline> <text x="400" y="206" alignment-baseline="central" text-anchor="middle">16</text>
484 <polyline points="540.003,200 538.042,212.358 532.363,223.511 523.511,232.363 512.358,238.042 500,240.003 487.642,238.042 476.489,232.363 467.637,223.511 461.958,212.358 459.997,200 461.958,187.642 467.637,176.489 476.489,167.637 487.642,161.958 500,159.997 512.358,161.958 523.511,167.637 532.363,176.489 538.042,187.642 540.003,200" style="fill:none;stroke:purple;stroke-width:2"></polyline> <text x="500" y="206" alignment-baseline="central" text-anchor="middle">20</text>
485 </svg>
486 </blockquote>
487
488 <p>Was generated by this script:
489
490 </p><div class="codeblock"><pre>SELECT '&lt;svg width="600" height="300">';
491 WITH t1(x,y,n,color) AS (VALUES
492 (100,100,3,'red'),
493 (200,100,4,'orange'),
494 (300,100,5,'green'),
495 (400,100,6,'blue'),
496 (500,100,7,'purple'),
497 (100,200,8,'red'),
498 (200,200,10,'orange'),
499 (300,200,12,'green'),
500 (400,200,16,'blue'),
501 (500,200,20,'purple')
502 )
503 SELECT
504 geopoly_svg(geopoly_regular(x,y,40,n),
505 printf('style="fill:none;stroke:%s;stroke-width:2"',color))
506 || printf(' &lt;text x="%d" y="%d" alignment-baseline="central" text-anchor="middle">%d&lt;/text>',x,y+6,n)
507 FROM t1;
508 SELECT '&lt;/svg>';
509 </pre></div>
510
511 <a name="ccw"></a>
512
513 <h2 id="the_geopoly_ccw_j_function"><span>3.11. </span>The geopoly_ccw(J) Function</h2>
514
515 <p>The geopoly_ccw(J) function returns the polygon J with counter-clockwise (CCW) rotation.
516
517 </p><p>
518 <a href="https://tools.ietf.org/html/rfc7946">RFC-7946</a> requires that polygons use CCW rotation.
519 But the spec also observes that many legacy GeoJSON files do not following the spec and
520 contain polygons with clockwise (CW) rotation. The geopoly_ccw() function is useful for
521 applications that are reading legacy GeoJSON scripts. If the input to geopoly_ccw() is
522 a correctly-formatted polygon, then no changes are made. However, if the circulation of
523 the input polygon is backwards, then geopoly_ccw() reverses the circulation order so that
524 it conforms to the spec and so that it will work correctly with the Geopoly module.
525
526
527
528 </p><h1 id="implementation_details"><span>4. </span>Implementation Details</h1>
529
530 <p>The geopoly module is an extension to the <a href="rtree.html">R-Tree extension</a>. Geopoly
531 uses the same underlying logic and shadow tables as the <a href="rtree.html">R-Tree extension</a>.
532 Geopoly merely presents a different interface, and provides some extra logic
533 to compute polygon decoding, overlap, and containment.
534
535 </p><h2 id="binary_encoding_of_polygons"><span>4.1. </span>Binary Encoding of Polygons</h2>
536
537 <p>
538 Geopoly stores all polygons internally using a binary format. A binary
539 polygon consists of a 4-byte header following by an array of coordinate
540 pairs in which each dimension of each coordinate is a 32-bit floating point
541 number.
542
543 </p><p>
544 The first byte of the header is a flag byte. The least significant bit
545 of the flag byte determines whether the coordinate pairs that follow the
546 header are stored big-endian or little-endian. A value of 0 for the least
547 significant bit means big-endian and a value of 1 means little endian.
548 Other bits of the first byte in the header are reserved for future expansion.
549
550 </p><p>
551 The next three bytes in the header record the number of vertexes in the polygon
552 as a big-endian integer. Thus there is an upper bound of about 16 million
553 vertexes per polygon.
554
555 </p><p>
556 Following the header is the array of coordinate pairs. Each coordinate is
557 a 32-bit floating point number. The use of 32-bit floating point values for
558 coordinates means that any point on the earth's surface can be mapped with
559 a resolution of approximately 2.5 meters. Higher resolutions are of course
560 possible if the map is restricted to a single continent or country.
561 Note that the resolution of coordinates in the geopoly module is similar
562 in magnitude to daily movement of points on the earth's surface due to
563 tidal forces.
564
565 </p><p>
566 The list of coordinates in the binary format contains no redundancy.
567 The last coordinate is not a repeat of the first as it is with GeoJSON.
568 Hence, there is always one fewer coordinate pair in the binary representation of
569 a polygon compared to the GeoJSON representation.
570
571 </p><h2 id="shadow_tables"><span>4.2. </span>Shadow Tables</h2>
572
573 <p>
574 The geopoly module is built on top of the <a href="rtree.html">R-Tree extension</a> and uses the
575 same underlying shadow tables and algorithms. For indexing purposes, each
576 polygon is represented in the shadow tables as a rectangular bounding box.
577 The underlying R-Tree implementation uses bounding boxes to limit the search
578 space. Then the geoploy_overlap() and/or geopoly_within() routines further
579 refine the search to the exact answer.
580 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/geopoly.in?m=7b64a47e71">2023-12-05 14:43:20</a> UTC </small></i></p>
581