]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add the geopoly_within() SQL function.
authordrh <drh@noemail.net>
Fri, 11 May 2018 16:50:57 +0000 (16:50 +0000)
committerdrh <drh@noemail.net>
Fri, 11 May 2018 16:50:57 +0000 (16:50 +0000)
FossilOrigin-Name: 927d52a93c82ae408cb1651fd1326772f0be7442b6673e89f20567e633d39af3

ext/misc/geopoly.c
manifest
manifest.uuid

index dde61f7bfb9d9925224763651bb63f9568ff0a1a..f9de51c807cff2550fbbffce45682d6f3108c180 100644 (file)
@@ -367,6 +367,84 @@ static void geopolyAreaFunc(
   }            
 }
 
+/*
+** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
+** Returns:
+**
+**    +2  x0,y0 is on the line segement
+**
+**    +1  x0,y0 is beneath line segment
+**
+**    0   x0,y0 is not on or beneath the line segment or the line segment
+**        is vertical and x0,y0 is not on the line segment
+**
+** The left-most coordinate min(x1,x2) is not considered to be part of
+** the line segment for the purposes of this analysis.
+*/
+static int pointBeneathLine(
+  double x0, double y0,
+  double x1, double y1,
+  double x2, double y2
+){
+  double y;
+  if( x0==x1 && y0==y1 ) return 2;
+  if( x1<x2 ){
+    if( x0<=x1 || x0>x2 ) return 0;
+  }else if( x1>x2 ){
+    if( x0<=x2 || x0>x1 ) return 0;
+  }else{
+    /* Vertical line segment */
+    if( x0!=x1 ) return 0;
+    if( y0<y1 && y0<y2 ) return 0;
+    if( y0>y1 && y0>y2 ) return 0;
+    return 2;
+  }
+  y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
+  if( y0==y ) return 2;
+  if( y0<y ) return 1;
+  return 0;
+}
+
+/*
+** SQL function:    geopoly_within(P,X,Y)
+**
+** Return +2 if point X,Y is within polygon P.
+** Return +1 if point X,Y is on the polygon boundary.
+** Return 0 if point X,Y is outside the polygon
+*/
+static void geopolyWithinFunc(
+  sqlite3_context *context,
+  int argc,
+  sqlite3_value **argv
+){
+  GeoPoly *p = geopolyFuncParam(context, argv[0]);
+  double x0 = sqlite3_value_double(argv[1]);
+  double y0 = sqlite3_value_double(argv[2]);
+  if( p ){
+    int v = 0;
+    int cnt = 0;
+    int ii;
+    for(ii=0; ii<p->nVertex-1; ii++){
+      v = pointBeneathLine(x0,y0,p->a[ii*2],p->a[ii*2+1],
+                                 p->a[ii*2+2],p->a[ii*2+3]);
+      if( v==2 ) break;
+      cnt += v;
+    }
+    if( v!=2 ){
+      v = pointBeneathLine(x0,y0,p->a[ii*2],p->a[ii*2+1],
+                                 p->a[0],p->a[1]);
+    }
+    if( v==2 ){
+      sqlite3_result_int(context, 1);
+    }else if( ((v+cnt)&1)==0 ){
+      sqlite3_result_int(context, 0);
+    }else{
+      sqlite3_result_int(context, 2);
+    }
+    sqlite3_free(p);
+  }            
+}
+
 
 #ifdef _WIN32
 __declspec(dllexport)
@@ -382,9 +460,10 @@ int sqlite3_geopoly_init(
     int nArg;
     const char *zName;
   } aFunc[] = {
-     { geopolyAreaFunc,          1,    "geopoly_area"  },
-     { geopolyBlobFunc,          1,    "geopoly_blob"  },
-     { geopolyJsonFunc,          1,    "geopoly_json"  },
+     { geopolyAreaFunc,          1,    "geopoly_area"   },
+     { geopolyBlobFunc,          1,    "geopoly_blob"   },
+     { geopolyJsonFunc,          1,    "geopoly_json"   },
+     { geopolyWithinFunc,        3,    "geopoly_within" },
   };
   int i;
   SQLITE_EXTENSION_INIT2(pApi);
index 842ca9f89dfe725260d99ba20b330dabc3a0389e..34154bc8709e738c76b03adc325a2973a5270c84 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\sgeopoly_read()\sSQL\sfunction\sto\sthe\sgeopoly.c\sextension.
-D 2018-05-11T15:38:03.123
+C Add\sthe\sgeopoly_within()\sSQL\sfunction.
+D 2018-05-11T16:50:57.975
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in bfc40f350586923e0419d2ea4b559c37ec10ee4b6e210e08c14401f8e340f0da
@@ -279,7 +279,7 @@ F ext/misc/dbdump.c 69ef1be5b210538f77dfcc6fcb55b4b5f5e98b1e0bcfd67d818711e10761
 F ext/misc/eval.c 6ea9b22a5fa0dd973b67ca4e53555be177bc0b7b263aadf1024429457c82c0e3
 F ext/misc/fileio.c 48c7751c78fc4cdd29d8c862fd2f3f98bbfefa2a3cf1ca1496df4bf02eb8cded
 F ext/misc/fuzzer.c 7c64b8197bb77b7d64eff7cac7848870235d4c25
-F ext/misc/geopoly.c 1e0cf4b36dda931335ac593c405b00faaccb67c442b765eb1a5a501e69d29222
+F ext/misc/geopoly.c fafe3ded3326a538f3887ef477424f83d26861492a8da42ebd255ffe83108bda
 F ext/misc/ieee754.c f190d0cc5182529acb15babd177781be1ac1718c
 F ext/misc/json1.c dbe086615b9546c156bf32b9378fc09383b58bd17513b866cfd24c1e15281984
 F ext/misc/memvfs.c ab36f49e02ebcdf85a1e08dc4d8599ea8f343e073ac9e0bca18a98b7e1ec9567
@@ -1729,7 +1729,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P e76f676c120cd503e4a7b7cc636250ace68f4eafbbe304a8fcf503614e1b9ad0
-R e59fb5b809946c39215a5cc2b908f045
+P b37625e8e469f8856b19acabe9a6628322cba2e76d478da18caff4d9613ab5e6
+R 54999e7a5484c9e8c0a96460cd1ccdca
 U drh
-Z ee1d0fae34c255e3ce33f1c92519c313
+Z 11b849f7588c67b562fa6e2b0287c003
index ea196df688786bf16f547b4375917299636a0c20..d42f174dd2d752898e46950c3ff32467d9f5007d 100644 (file)
@@ -1 +1 @@
-b37625e8e469f8856b19acabe9a6628322cba2e76d478da18caff4d9613ab5e6
\ No newline at end of file
+927d52a93c82ae408cb1651fd1326772f0be7442b6673e89f20567e633d39af3
\ No newline at end of file