Hex Artifact Content
Not logged in

Artifact fe1d6e12c2f98dff23f9df9372afc23f745b50df:


0000: 2f 2a 0a 0a 20 73 70 61 74 69 61 6c 69 74 65 2e  /*.. spatialite.
0010: 63 20 2d 2d 20 53 51 4c 69 74 65 33 20 73 70 61  c -- SQLite3 spa
0020: 74 69 61 6c 20 65 78 74 65 6e 73 69 6f 6e 0a 0a  tial extension..
0030: 20 76 65 72 73 69 6f 6e 20 34 2e 30 2c 20 32 30   version 4.0, 20
0040: 31 32 20 41 75 67 75 73 74 20 36 0a 0a 20 41 75  12 August 6.. Au
0050: 74 68 6f 72 3a 20 53 61 6e 64 72 6f 20 46 75 72  thor: Sandro Fur
0060: 69 65 72 69 20 61 2e 66 75 72 69 65 72 69 40 6c  ieri a.furieri@l
0070: 71 74 2e 69 74 0a 0a 20 2d 2d 2d 2d 2d 2d 2d 2d  qt.it.. --------
0080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
00a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
00b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
00c0: 2d 2d 2d 2d 2d 2d 0a 20 0a 20 56 65 72 73 69 6f  ------. . Versio
00d0: 6e 3a 20 4d 50 4c 20 31 2e 31 2f 47 50 4c 20 32  n: MPL 1.1/GPL 2
00e0: 2e 30 2f 4c 47 50 4c 20 32 2e 31 0a 20 0a 20 54  .0/LGPL 2.1. . T
00f0: 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
0100: 68 69 73 20 66 69 6c 65 20 61 72 65 20 73 75 62  his file are sub
0110: 6a 65 63 74 20 74 6f 20 74 68 65 20 4d 6f 7a 69  ject to the Mozi
0120: 6c 6c 61 20 50 75 62 6c 69 63 20 4c 69 63 65 6e  lla Public Licen
0130: 73 65 20 56 65 72 73 69 6f 6e 0a 20 31 2e 31 20  se Version. 1.1 
0140: 28 74 68 65 20 22 4c 69 63 65 6e 73 65 22 29 3b  (the "License");
0150: 20 79 6f 75 20 6d 61 79 20 6e 6f 74 20 75 73 65   you may not use
0160: 20 74 68 69 73 20 66 69 6c 65 20 65 78 63 65 70   this file excep
0170: 74 20 69 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20  t in compliance 
0180: 77 69 74 68 0a 20 74 68 65 20 4c 69 63 65 6e 73  with. the Licens
0190: 65 2e 20 59 6f 75 20 6d 61 79 20 6f 62 74 61 69  e. You may obtai
01a0: 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  n a copy of the 
01b0: 4c 69 63 65 6e 73 65 20 61 74 0a 20 68 74 74 70  License at. http
01c0: 3a 2f 2f 77 77 77 2e 6d 6f 7a 69 6c 6c 61 2e 6f  ://www.mozilla.o
01d0: 72 67 2f 4d 50 4c 2f 0a 20 0a 53 6f 66 74 77 61  rg/MPL/. .Softwa
01e0: 72 65 20 64 69 73 74 72 69 62 75 74 65 64 20 75  re distributed u
01f0: 6e 64 65 72 20 74 68 65 20 4c 69 63 65 6e 73 65  nder the License
0200: 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20   is distributed 
0210: 6f 6e 20 61 6e 20 22 41 53 20 49 53 22 20 62 61  on an "AS IS" ba
0220: 73 69 73 2c 0a 57 49 54 48 4f 55 54 20 57 41 52  sis,.WITHOUT WAR
0230: 52 41 4e 54 59 20 4f 46 20 41 4e 59 20 4b 49 4e  RANTY OF ANY KIN
0240: 44 2c 20 65 69 74 68 65 72 20 65 78 70 72 65 73  D, either expres
0250: 73 20 6f 72 20 69 6d 70 6c 69 65 64 2e 20 53 65  s or implied. Se
0260: 65 20 74 68 65 20 4c 69 63 65 6e 73 65 0a 66 6f  e the License.fo
0270: 72 20 74 68 65 20 73 70 65 63 69 66 69 63 20 6c  r the specific l
0280: 61 6e 67 75 61 67 65 20 67 6f 76 65 72 6e 69 6e  anguage governin
0290: 67 20 72 69 67 68 74 73 20 61 6e 64 20 6c 69 6d  g rights and lim
02a0: 69 74 61 74 69 6f 6e 73 20 75 6e 64 65 72 20 74  itations under t
02b0: 68 65 0a 4c 69 63 65 6e 73 65 2e 0a 0a 54 68 65  he.License...The
02c0: 20 4f 72 69 67 69 6e 61 6c 20 43 6f 64 65 20 69   Original Code i
02d0: 73 20 74 68 65 20 53 70 61 74 69 61 4c 69 74 65  s the SpatiaLite
02e0: 20 6c 69 62 72 61 72 79 0a 0a 54 68 65 20 49 6e   library..The In
02f0: 69 74 69 61 6c 20 44 65 76 65 6c 6f 70 65 72 20  itial Developer 
0300: 6f 66 20 74 68 65 20 4f 72 69 67 69 6e 61 6c 20  of the Original 
0310: 43 6f 64 65 20 69 73 20 41 6c 65 73 73 61 6e 64  Code is Alessand
0320: 72 6f 20 46 75 72 69 65 72 69 0a 20 0a 50 6f 72  ro Furieri. .Por
0330: 74 69 6f 6e 73 20 63 72 65 61 74 65 64 20 62 79  tions created by
0340: 20 74 68 65 20 49 6e 69 74 69 61 6c 20 44 65 76   the Initial Dev
0350: 65 6c 6f 70 65 72 20 61 72 65 20 43 6f 70 79 72  eloper are Copyr
0360: 69 67 68 74 20 28 43 29 20 32 30 30 38 2d 32 30  ight (C) 2008-20
0370: 31 32 0a 74 68 65 20 49 6e 69 74 69 61 6c 20 44  12.the Initial D
0380: 65 76 65 6c 6f 70 65 72 2e 20 41 6c 6c 20 52 69  eveloper. All Ri
0390: 67 68 74 73 20 52 65 73 65 72 76 65 64 2e 0a 0a  ghts Reserved...
03a0: 43 6f 6e 74 72 69 62 75 74 6f 72 28 73 29 3a 0a  Contributor(s):.
03b0: 50 65 70 69 6a 6e 20 56 61 6e 20 45 65 63 6b 68  Pepijn Van Eeckh
03c0: 6f 75 64 74 20 3c 70 65 70 69 6a 6e 76 61 6e 65  oudt <pepijnvane
03d0: 65 63 6b 68 6f 75 64 74 40 6c 75 63 69 61 64 2e  eckhoudt@luciad.
03e0: 63 6f 6d 3e 0a 28 69 6d 70 6c 65 6d 65 6e 74 69  com>.(implementi
03f0: 6e 67 20 41 6e 64 72 6f 69 64 20 73 75 70 70 6f  ng Android suppo
0400: 72 74 29 0a 0a 41 6c 74 65 72 6e 61 74 69 76 65  rt)..Alternative
0410: 6c 79 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ly, the contents
0420: 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 6d 61   of this file ma
0430: 79 20 62 65 20 75 73 65 64 20 75 6e 64 65 72 20  y be used under 
0440: 74 68 65 20 74 65 72 6d 73 20 6f 66 0a 65 69 74  the terms of.eit
0450: 68 65 72 20 74 68 65 20 47 4e 55 20 47 65 6e 65  her the GNU Gene
0460: 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 6e  ral Public Licen
0470: 73 65 20 56 65 72 73 69 6f 6e 20 32 20 6f 72 20  se Version 2 or 
0480: 6c 61 74 65 72 20 28 74 68 65 20 22 47 50 4c 22  later (the "GPL"
0490: 29 2c 20 6f 72 0a 74 68 65 20 47 4e 55 20 4c 65  ), or.the GNU Le
04a0: 73 73 65 72 20 47 65 6e 65 72 61 6c 20 50 75 62  sser General Pub
04b0: 6c 69 63 20 4c 69 63 65 6e 73 65 20 56 65 72 73  lic License Vers
04c0: 69 6f 6e 20 32 2e 31 20 6f 72 20 6c 61 74 65 72  ion 2.1 or later
04d0: 20 28 74 68 65 20 22 4c 47 50 4c 22 29 2c 0a 69   (the "LGPL"),.i
04e0: 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
04f0: 20 70 72 6f 76 69 73 69 6f 6e 73 20 6f 66 20 74   provisions of t
0500: 68 65 20 47 50 4c 20 6f 72 20 74 68 65 20 4c 47  he GPL or the LG
0510: 50 4c 20 61 72 65 20 61 70 70 6c 69 63 61 62 6c  PL are applicabl
0520: 65 20 69 6e 73 74 65 61 64 0a 6f 66 20 74 68 6f  e instead.of tho
0530: 73 65 20 61 62 6f 76 65 2e 20 49 66 20 79 6f 75  se above. If you
0540: 20 77 69 73 68 20 74 6f 20 61 6c 6c 6f 77 20 75   wish to allow u
0550: 73 65 20 6f 66 20 79 6f 75 72 20 76 65 72 73 69  se of your versi
0560: 6f 6e 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20  on of this file 
0570: 6f 6e 6c 79 0a 75 6e 64 65 72 20 74 68 65 20 74  only.under the t
0580: 65 72 6d 73 20 6f 66 20 65 69 74 68 65 72 20 74  erms of either t
0590: 68 65 20 47 50 4c 20 6f 72 20 74 68 65 20 4c 47  he GPL or the LG
05a0: 50 4c 2c 20 61 6e 64 20 6e 6f 74 20 74 6f 20 61  PL, and not to a
05b0: 6c 6c 6f 77 20 6f 74 68 65 72 73 20 74 6f 0a 75  llow others to.u
05c0: 73 65 20 79 6f 75 72 20 76 65 72 73 69 6f 6e 20  se your version 
05d0: 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64  of this file und
05e0: 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20  er the terms of 
05f0: 74 68 65 20 4d 50 4c 2c 20 69 6e 64 69 63 61 74  the MPL, indicat
0600: 65 20 79 6f 75 72 0a 64 65 63 69 73 69 6f 6e 20  e your.decision 
0610: 62 79 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20  by deleting the 
0620: 70 72 6f 76 69 73 69 6f 6e 73 20 61 62 6f 76 65  provisions above
0630: 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74 68 65   and replace the
0640: 6d 20 77 69 74 68 20 74 68 65 20 6e 6f 74 69 63  m with the notic
0650: 65 0a 61 6e 64 20 6f 74 68 65 72 20 70 72 6f 76  e.and other prov
0660: 69 73 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20  isions required 
0670: 62 79 20 74 68 65 20 47 50 4c 20 6f 72 20 74 68  by the GPL or th
0680: 65 20 4c 47 50 4c 2e 20 49 66 20 79 6f 75 20 64  e LGPL. If you d
0690: 6f 20 6e 6f 74 20 64 65 6c 65 74 65 0a 74 68 65  o not delete.the
06a0: 20 70 72 6f 76 69 73 69 6f 6e 73 20 61 62 6f 76   provisions abov
06b0: 65 2c 20 61 20 72 65 63 69 70 69 65 6e 74 20 6d  e, a recipient m
06c0: 61 79 20 75 73 65 20 79 6f 75 72 20 76 65 72 73  ay use your vers
06d0: 69 6f 6e 20 6f 66 20 74 68 69 73 20 66 69 6c 65  ion of this file
06e0: 20 75 6e 64 65 72 0a 74 68 65 20 74 65 72 6d 73   under.the terms
06f0: 20 6f 66 20 61 6e 79 20 6f 6e 65 20 6f 66 20 74   of any one of t
0700: 68 65 20 4d 50 4c 2c 20 74 68 65 20 47 50 4c 20  he MPL, the GPL 
0710: 6f 72 20 74 68 65 20 4c 47 50 4c 2e 0a 20 0a 2a  or the LGPL.. .*
0720: 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  /..#include <sys
0730: 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75  /types.h>.#inclu
0740: 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69  de <stdlib.h>.#i
0750: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e  nclude <stdio.h>
0760: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e  .#include <strin
0770: 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 6d  g.h>.#include <m
0780: 61 74 68 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  ath.h>.#include 
0790: 3c 66 6c 6f 61 74 2e 68 3e 0a 23 69 6e 63 6c 75  <float.h>.#inclu
07a0: 64 65 20 3c 6c 6f 63 61 6c 65 2e 68 3e 0a 0a 23  de <locale.h>..#
07b0: 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e  include "config.
07c0: 68 22 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  h"..#if defined(
07d0: 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e  _WIN32) || defin
07e0: 65 64 28 57 49 4e 33 32 29 0a 23 69 6e 63 6c 75  ed(WIN32).#inclu
07f0: 64 65 20 3c 69 6f 2e 68 3e 0a 23 64 65 66 69 6e  de <io.h>.#defin
0800: 65 20 69 73 61 74 74 79 09 5f 69 73 61 74 74 79  e isatty._isatty
0810: 0a 23 65 6c 73 65 0a 23 69 6e 63 6c 75 64 65 20  .#else.#include 
0820: 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 65 6e 64 69  <unistd.h>.#endi
0830: 66 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 73 70 61  f..#include <spa
0840: 74 69 61 6c 69 74 65 2f 73 71 6c 69 74 65 2e 68  tialite/sqlite.h
0850: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 70 61 74  >.#include <spat
0860: 69 61 6c 69 74 65 2f 64 65 62 75 67 2e 68 3e 0a  ialite/debug.h>.
0870: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 70 61 74 69  .#include <spati
0880: 61 6c 69 74 65 2f 67 61 69 61 61 75 78 2e 68 3e  alite/gaiaaux.h>
0890: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 70 61 74 69  .#include <spati
08a0: 61 6c 69 74 65 2f 67 61 69 61 67 65 6f 2e 68 3e  alite/gaiageo.h>
08b0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 70 61 74 69  .#include <spati
08c0: 61 6c 69 74 65 2f 67 61 69 61 65 78 69 66 2e 68  alite/gaiaexif.h
08d0: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 70 61 74  >.#include <spat
08e0: 69 61 6c 69 74 65 2f 73 70 61 74 69 61 6c 69 74  ialite/spatialit
08f0: 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  e.h>.#include <s
0900: 70 61 74 69 61 6c 69 74 65 2e 68 3e 0a 23 69 6e  patialite.h>.#in
0910: 63 6c 75 64 65 20 3c 73 70 61 74 69 61 6c 69 74  clude <spatialit
0920: 65 5f 70 72 69 76 61 74 65 2e 68 3e 0a 0a 23 69  e_private.h>..#i
0930: 66 6e 64 65 66 20 4f 4d 49 54 5f 47 45 4f 53 09  fndef OMIT_GEOS.
0940: 09 2f 2a 20 69 6e 63 6c 75 64 69 6e 67 20 47 45  ./* including GE
0950: 4f 53 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c  OS */.#include <
0960: 67 65 6f 73 5f 63 2e 68 3e 0a 23 65 6e 64 69 66  geos_c.h>.#endif
0970: 0a 0a 23 69 66 6e 64 65 66 20 4f 4d 49 54 5f 50  ..#ifndef OMIT_P
0980: 52 4f 4a 09 09 2f 2a 20 69 6e 63 6c 75 64 69 6e  ROJ../* includin
0990: 67 20 50 52 4f 4a 2e 34 20 2a 2f 0a 23 69 6e 63  g PROJ.4 */.#inc
09a0: 6c 75 64 65 20 3c 70 72 6f 6a 5f 61 70 69 2e 68  lude <proj_api.h
09b0: 3e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  >.#endif..#ifdef
09c0: 20 5f 57 49 4e 33 32 0a 23 64 65 66 69 6e 65 20   _WIN32.#define 
09d0: 73 74 72 63 61 73 65 63 6d 70 09 5f 73 74 72 69  strcasecmp._stri
09e0: 63 6d 70 0a 23 65 6e 64 69 66 20 2f 2a 20 6e 6f  cmp.#endif /* no
09f0: 74 20 57 49 4e 33 32 20 2a 2f 0a 0a 23 64 65 66  t WIN32 */..#def
0a00: 69 6e 65 20 47 41 49 41 5f 55 4e 55 53 45 44 28  ine GAIA_UNUSED(
0a10: 29 20 69 66 20 28 61 72 67 63 20 7c 7c 20 61 72  ) if (argc || ar
0a20: 67 76 29 20 61 72 67 63 20 3d 20 61 72 67 63 3b  gv) argc = argc;
0a30: 0a 0a 23 69 66 6e 64 65 66 20 4f 4d 49 54 5f 47  ..#ifndef OMIT_G
0a40: 45 4f 43 41 4c 4c 42 41 43 4b 53 09 2f 2a 20 73  EOCALLBACKS./* s
0a50: 75 70 70 6f 72 74 69 6e 67 20 52 54 72 65 65 20  upporting RTree 
0a60: 67 65 6f 6d 65 74 72 79 20 63 61 6c 6c 62 61 63  geometry callbac
0a70: 6b 73 20 2a 2f 0a 73 74 72 75 63 74 20 67 61 69  ks */.struct gai
0a80: 61 5f 72 74 72 65 65 5f 6d 62 72 0a 7b 0a 2f 2a  a_rtree_mbr.{./*
0a90: 20 61 20 73 74 72 75 63 74 20 75 73 65 64 20 62   a struct used b
0aa0: 79 20 52 2a 54 72 65 65 20 47 65 6f 6d 65 74 72  y R*Tree Geometr
0ab0: 79 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69  yCallback functi
0ac0: 6f 6e 73 20 5b 4d 42 52 5d 20 2a 2f 0a 20 20 20  ons [MBR] */.   
0ad0: 20 64 6f 75 62 6c 65 20 6d 69 6e 78 3b 0a 20 20   double minx;.  
0ae0: 20 20 64 6f 75 62 6c 65 20 6d 69 6e 79 3b 0a 20    double miny;. 
0af0: 20 20 20 64 6f 75 62 6c 65 20 6d 61 78 78 3b 0a     double maxx;.
0b00: 20 20 20 20 64 6f 75 62 6c 65 20 6d 61 78 79 3b      double maxy;
0b10: 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 65 6e  .};.#endif /* en
0b20: 64 20 52 54 72 65 65 20 67 65 6f 6d 65 74 72 79  d RTree geometry
0b30: 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 0a 53   callbacks */..S
0b40: 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f  QLITE_EXTENSION_
0b50: 49 4e 49 54 31 20 73 74 72 75 63 74 20 73 70 61  INIT1 struct spa
0b60: 74 69 61 6c 5f 69 6e 64 65 78 5f 73 74 72 0a 7b  tial_index_str.{
0b70: 0a 2f 2a 20 61 20 73 74 72 75 63 74 20 74 6f 20  ./* a struct to 
0b80: 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6c 69 6e 6b  implement a link
0b90: 65 64 20 6c 69 73 74 20 6f 66 20 73 70 61 74 69  ed list of spati
0ba0: 61 6c 2d 69 6e 64 65 78 65 73 20 2a 2f 0a 20 20  al-indexes */.  
0bb0: 20 20 63 68 61 72 20 56 61 6c 69 64 52 74 72 65    char ValidRtre
0bc0: 65 3b 0a 20 20 20 20 63 68 61 72 20 56 61 6c 69  e;.    char Vali
0bd0: 64 43 61 63 68 65 3b 0a 20 20 20 20 63 68 61 72  dCache;.    char
0be0: 20 2a 54 61 62 6c 65 4e 61 6d 65 3b 0a 20 20 20   *TableName;.   
0bf0: 20 63 68 61 72 20 2a 43 6f 6c 75 6d 6e 4e 61 6d   char *ColumnNam
0c00: 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73 70  e;.    struct sp
0c10: 61 74 69 61 6c 5f 69 6e 64 65 78 5f 73 74 72 20  atial_index_str 
0c20: 2a 4e 65 78 74 3b 0a 7d 3b 0a 0a 73 74 72 75 63  *Next;.};..struc
0c30: 74 20 73 74 64 64 65 76 5f 73 74 72 0a 7b 0a 2f  t stddev_str.{./
0c40: 2a 20 61 20 73 74 72 75 63 74 20 74 6f 20 69 6d  * a struct to im
0c50: 70 6c 65 6d 65 6e 74 20 53 74 61 6e 64 61 72 64  plement Standard
0c60: 56 61 72 69 61 74 69 6f 6e 20 61 6e 64 20 56 61  Variation and Va
0c70: 72 69 61 6e 63 65 20 61 67 67 72 65 67 61 74 65  riance aggregate
0c80: 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20   functions */.  
0c90: 20 20 69 6e 74 20 63 6c 65 61 6e 65 64 3b 0a 20    int cleaned;. 
0ca0: 20 20 20 64 6f 75 62 6c 65 20 6d 65 61 6e 3b 0a     double mean;.
0cb0: 20 20 20 20 64 6f 75 62 6c 65 20 71 75 6f 74 3b      double quot;
0cc0: 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f 75 6e  .    double coun
0cd0: 74 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 66 64  t;.};..struct fd
0ce0: 6f 5f 74 61 62 6c 65 0a 7b 0a 2f 2a 20 61 20 73  o_table.{./* a s
0cf0: 74 72 75 63 74 20 74 6f 20 69 6d 70 6c 65 6d 65  truct to impleme
0d00: 6e 74 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74  nt a linked-list
0d10: 20 66 6f 72 20 46 44 4f 2d 4f 52 47 20 74 61 62   for FDO-ORG tab
0d20: 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 20 20  le names */.    
0d30: 63 68 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20  char *table;.   
0d40: 20 73 74 72 75 63 74 20 66 64 6f 5f 74 61 62 6c   struct fdo_tabl
0d50: 65 20 2a 6e 65 78 74 3b 0a 7d 3b 0a 0a 73 74 61  e *next;.};..sta
0d60: 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 73 70  tic void.fnct_sp
0d70: 61 74 69 61 6c 69 74 65 5f 76 65 72 73 69 6f 6e  atialite_version
0d80: 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
0d90: 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
0da0: 20 61 72 67 63 2c 0a 09 09 09 20 73 71 6c 69 74   argc,.... sqlit
0db0: 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
0dc0: 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
0dd0: 69 6f 6e 3a 0a 2f 20 73 70 61 74 69 61 6c 69 74  ion:./ spatialit
0de0: 65 5f 76 65 72 73 69 6f 6e 28 29 0a 2f 0a 2f 20  e_version()././ 
0df0: 72 65 74 75 72 6e 20 61 20 74 65 78 74 20 73 74  return a text st
0e00: 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 69 6e  ring representin
0e10: 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 70  g the current Sp
0e20: 61 74 69 61 4c 69 74 65 20 76 65 72 73 69 6f 6e  atiaLite version
0e30: 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  .*/.    int len;
0e40: 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
0e50: 2a 70 5f 72 65 73 75 6c 74 20 3d 20 73 70 61 74  *p_result = spat
0e60: 69 61 6c 69 74 65 5f 76 65 72 73 69 6f 6e 20 28  ialite_version (
0e70: 29 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  );.    GAIA_UNUS
0e80: 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
0e90: 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
0ea0: 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 20 28 70   len = strlen (p
0eb0: 5f 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 73 71  _result);.    sq
0ec0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
0ed0: 74 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  t (context, p_re
0ee0: 73 75 6c 74 2c 20 6c 65 6e 2c 20 53 51 4c 49 54  sult, len, SQLIT
0ef0: 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a  E_TRANSIENT);.}.
0f00: 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
0f10: 74 5f 67 65 6f 73 5f 76 65 72 73 69 6f 6e 20 28  t_geos_version (
0f20: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
0f30: 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
0f40: 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
0f50: 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
0f60: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
0f70: 20 67 65 6f 73 5f 76 65 72 73 69 6f 6e 28 29 0a   geos_version().
0f80: 2f 0a 2f 20 72 65 74 75 72 6e 20 61 20 74 65 78  /./ return a tex
0f90: 74 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  t string represe
0fa0: 6e 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e  nting the curren
0fb0: 74 20 47 45 4f 53 20 76 65 72 73 69 6f 6e 0a 2f  t GEOS version./
0fc0: 20 6f 72 20 4e 55 4c 4c 20 69 66 20 47 45 4f 53   or NULL if GEOS
0fd0: 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e   is currently un
0fe0: 73 75 70 70 6f 72 74 65 64 0a 2a 2f 0a 0a 23 69  supported.*/..#i
0ff0: 66 6e 64 65 66 20 4f 4d 49 54 5f 47 45 4f 53 09  fndef OMIT_GEOS.
1000: 09 2f 2a 20 47 45 4f 53 20 76 65 72 73 69 6f 6e  ./* GEOS version
1010: 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b   */.    int len;
1020: 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1030: 2a 70 5f 72 65 73 75 6c 74 20 3d 20 47 45 4f 53  *p_result = GEOS
1040: 76 65 72 73 69 6f 6e 20 28 29 3b 0a 20 20 20 20  version ();.    
1050: 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
1060: 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
1070: 4e 45 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20  NE */.    len = 
1080: 73 74 72 6c 65 6e 20 28 70 5f 72 65 73 75 6c 74  strlen (p_result
1090: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
10a0: 65 73 75 6c 74 5f 74 65 78 74 20 28 63 6f 6e 74  esult_text (cont
10b0: 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c  ext, p_result, l
10c0: 65 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  en, SQLITE_TRANS
10d0: 49 45 4e 54 29 3b 0a 23 65 6c 73 65 0a 20 20 20  IENT);.#else.   
10e0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
10f0: 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
1100: 23 65 6e 64 69 66 0a 7d 0a 0a 73 74 61 74 69 63  #endif.}..static
1110: 20 76 6f 69 64 0a 66 6e 63 74 5f 70 72 6f 6a 34   void.fnct_proj4
1120: 5f 76 65 72 73 69 6f 6e 20 28 73 71 6c 69 74 65  _version (sqlite
1130: 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
1140: 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
1150: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
1160: 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
1170: 75 6e 63 74 69 6f 6e 3a 0a 2f 20 70 72 6f 6a 34  unction:./ proj4
1180: 5f 76 65 72 73 69 6f 6e 28 29 0a 2f 0a 2f 20 72  _version()././ r
1190: 65 74 75 72 6e 20 61 20 74 65 78 74 20 73 74 72  eturn a text str
11a0: 69 6e 67 20 72 65 70 72 65 73 65 6e 74 69 6e 67  ing representing
11b0: 20 74 68 65 20 63 75 72 72 65 6e 74 20 50 52 4f   the current PRO
11c0: 4a 2e 34 20 76 65 72 73 69 6f 6e 0a 2f 20 6f 72  J.4 version./ or
11d0: 20 4e 55 4c 4c 20 69 66 20 50 52 4f 4a 2e 34 20   NULL if PROJ.4 
11e0: 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 73  is currently uns
11f0: 75 70 70 6f 72 74 65 64 0a 2a 2f 0a 0a 23 69 66  upported.*/..#if
1200: 6e 64 65 66 20 4f 4d 49 54 5f 50 52 4f 4a 09 09  ndef OMIT_PROJ..
1210: 2f 2a 20 50 52 4f 4a 2e 34 20 76 65 72 73 69 6f  /* PROJ.4 versio
1220: 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e  n */.    int len
1230: 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
1240: 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 70 6a 5f   *p_result = pj_
1250: 67 65 74 5f 72 65 6c 65 61 73 65 20 28 29 3b 0a  get_release ();.
1260: 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
1270: 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
1280: 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 6c 65  L_LINE */.    le
1290: 6e 20 3d 20 73 74 72 6c 65 6e 20 28 70 5f 72 65  n = strlen (p_re
12a0: 73 75 6c 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  sult);.    sqlit
12b0: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 20 28  e3_result_text (
12c0: 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
12d0: 74 2c 20 6c 65 6e 2c 20 53 51 4c 49 54 45 5f 54  t, len, SQLITE_T
12e0: 52 41 4e 53 49 45 4e 54 29 3b 0a 23 65 6c 73 65  RANSIENT);.#else
12f0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
1300: 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
1310: 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 73 74  t);.#endif.}..st
1320: 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 68  atic void.fnct_h
1330: 61 73 5f 70 72 6f 6a 20 28 73 71 6c 69 74 65 33  as_proj (sqlite3
1340: 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
1350: 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
1360: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
1370: 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
1380: 6e 63 74 69 6f 6e 3a 0a 2f 20 48 61 73 50 72 6f  nction:./ HasPro
1390: 6a 28 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 20 31  j()././ return 1
13a0: 20 69 66 20 62 75 69 6c 74 20 69 6e 63 6c 75 64   if built includ
13b0: 69 6e 67 20 50 72 6f 6a 2e 34 3b 20 6f 74 68 65  ing Proj.4; othe
13c0: 72 77 69 73 65 20 30 0a 2a 2f 0a 20 20 20 20 47  rwise 0.*/.    G
13d0: 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
13e0: 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
13f0: 45 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 4d 49  E */.#ifndef OMI
1400: 54 5f 50 52 4f 4a 09 09 2f 2a 20 50 52 4f 4a 2e  T_PROJ../* PROJ.
1410: 34 20 69 73 20 73 75 70 70 6f 72 74 65 64 20 2a  4 is supported *
1420: 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  /.    sqlite3_re
1430: 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
1440: 74 2c 20 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20  t, 1);.#else.   
1450: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1460: 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29  int (context, 0)
1470: 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 73 74 61 74  ;.#endif.}..stat
1480: 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 68 61 73  ic void.fnct_has
1490: 5f 67 65 6f 73 20 28 73 71 6c 69 74 65 33 5f 63  _geos (sqlite3_c
14a0: 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
14b0: 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
14c0: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
14d0: 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
14e0: 74 69 6f 6e 3a 0a 2f 20 48 61 73 47 65 6f 73 28  tion:./ HasGeos(
14f0: 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 20 31 20 69  )././ return 1 i
1500: 66 20 62 75 69 6c 74 20 69 6e 63 6c 75 64 69 6e  f built includin
1510: 67 20 47 45 4f 53 3b 20 6f 74 68 65 72 77 69 73  g GEOS; otherwis
1520: 65 20 30 0a 2a 2f 0a 20 20 20 20 47 41 49 41 5f  e 0.*/.    GAIA_
1530: 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
1540: 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
1550: 0a 23 69 66 6e 64 65 66 20 4f 4d 49 54 5f 47 45  .#ifndef OMIT_GE
1560: 4f 53 09 09 2f 2a 20 47 45 4f 53 20 69 73 20 73  OS../* GEOS is s
1570: 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 20  upported */.    
1580: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
1590: 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b  nt (context, 1);
15a0: 0a 23 65 6c 73 65 0a 20 20 20 20 73 71 6c 69 74  .#else.    sqlit
15b0: 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
15c0: 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 23 65 6e 64  ontext, 0);.#end
15d0: 69 66 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  if.}..static voi
15e0: 64 0a 66 6e 63 74 5f 68 61 73 5f 67 65 6f 73 5f  d.fnct_has_geos_
15f0: 61 64 76 61 6e 63 65 64 20 28 73 71 6c 69 74 65  advanced (sqlite
1600: 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
1610: 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09  ext, int argc,..
1620: 09 09 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  ..sqlite3_value 
1630: 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
1640: 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 48 61  L function:./ Ha
1650: 73 47 65 6f 73 41 64 76 61 6e 63 65 64 28 29 0a  sGeosAdvanced().
1660: 2f 0a 2f 20 72 65 74 75 72 6e 20 31 20 69 66 20  /./ return 1 if 
1670: 62 75 69 6c 74 20 69 6e 63 6c 75 64 69 6e 67 20  built including 
1680: 47 45 4f 53 3b 20 6f 74 68 65 72 77 69 73 65 20  GEOS; otherwise 
1690: 30 0a 2a 2f 0a 20 20 20 20 47 41 49 41 5f 55 4e  0.*/.    GAIA_UN
16a0: 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
16b0: 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 23  V_EXCL_LINE */.#
16c0: 69 66 64 65 66 20 47 45 4f 53 5f 41 44 56 41 4e  ifdef GEOS_ADVAN
16d0: 43 45 44 09 09 2f 2a 20 47 45 4f 53 2d 41 44 56  CED../* GEOS-ADV
16e0: 41 4e 43 45 44 20 69 73 20 73 75 70 70 6f 72 74  ANCED is support
16f0: 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ed */.    sqlite
1700: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
1710: 6e 74 65 78 74 2c 20 31 29 3b 0a 23 65 6c 73 65  ntext, 1);.#else
1720: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
1730: 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
1740: 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  , 0);.#endif.}..
1750: 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
1760: 5f 68 61 73 5f 69 63 6f 6e 76 20 28 73 71 6c 69  _has_iconv (sqli
1770: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
1780: 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
1790: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
17a0: 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
17b0: 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 48 61 73   function:./ Has
17c0: 49 63 6f 6e 76 28 29 0a 2f 0a 2f 20 72 65 74 75  Iconv()././ retu
17d0: 72 6e 20 31 20 69 66 20 62 75 69 6c 74 20 69 6e  rn 1 if built in
17e0: 63 6c 75 64 69 6e 67 20 49 43 4f 4e 56 3b 20 6f  cluding ICONV; o
17f0: 74 68 65 72 77 69 73 65 20 30 0a 2a 2f 0a 20 20  therwise 0.*/.  
1800: 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
1810: 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
1820: 4c 49 4e 45 20 2a 2f 0a 23 69 66 6e 64 65 66 20  LINE */.#ifndef 
1830: 4f 4d 49 54 5f 49 43 4f 4e 56 09 09 2f 2a 20 49  OMIT_ICONV../* I
1840: 43 4f 4e 56 20 69 73 20 73 75 70 70 6f 72 74 65  CONV is supporte
1850: 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  d */.    sqlite3
1860: 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
1870: 74 65 78 74 2c 20 31 29 3b 0a 23 65 6c 73 65 0a  text, 1);.#else.
1880: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1890: 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
18a0: 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 73   0);.#endif.}..s
18b0: 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
18c0: 68 61 73 5f 6d 61 74 68 5f 73 71 6c 20 28 73 71  has_math_sql (sq
18d0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
18e0: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
18f0: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
1900: 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
1910: 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 48  QL function:./ H
1920: 61 73 4d 61 74 68 53 71 6c 28 29 0a 2f 0a 2f 20  asMathSql()././ 
1930: 72 65 74 75 72 6e 20 31 20 69 66 20 62 75 69 6c  return 1 if buil
1940: 74 20 69 6e 63 6c 75 64 69 6e 67 20 4d 41 54 48  t including MATH
1950: 53 51 4c 3b 20 6f 74 68 65 72 77 69 73 65 20 30  SQL; otherwise 0
1960: 0a 2a 2f 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  .*/.    GAIA_UNU
1970: 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
1980: 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 23 69  _EXCL_LINE */.#i
1990: 66 6e 64 65 66 20 4f 4d 49 54 5f 4d 41 54 48 53  fndef OMIT_MATHS
19a0: 51 4c 09 09 2f 2a 20 4d 41 54 48 53 51 4c 20 69  QL../* MATHSQL i
19b0: 73 20 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20  s supported */. 
19c0: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
19d0: 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
19e0: 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 73 71  1);.#else.    sq
19f0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
1a00: 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 23   (context, 0);.#
1a10: 65 6e 64 69 66 0a 7d 0a 0a 73 74 61 74 69 63 20  endif.}..static 
1a20: 76 6f 69 64 0a 66 6e 63 74 5f 68 61 73 5f 67 65  void.fnct_has_ge
1a30: 6f 5f 63 61 6c 6c 62 61 63 6b 73 20 28 73 71 6c  o_callbacks (sql
1a40: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
1a50: 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
1a60: 2c 0a 09 09 09 73 71 6c 69 74 65 33 5f 76 61 6c  ,....sqlite3_val
1a70: 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
1a80: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
1a90: 20 48 61 73 47 65 6f 43 61 6c 6c 62 61 63 6b 73   HasGeoCallbacks
1aa0: 28 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 20 31 20  ()././ return 1 
1ab0: 69 66 20 62 75 69 6c 74 20 65 6e 61 62 6c 69 6e  if built enablin
1ac0: 67 20 47 45 4f 43 41 4c 4c 42 41 43 4b 53 3b 20  g GEOCALLBACKS; 
1ad0: 6f 74 68 65 72 77 69 73 65 20 30 0a 2a 2f 0a 20  otherwise 0.*/. 
1ae0: 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
1af0: 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
1b00: 5f 4c 49 4e 45 20 2a 2f 0a 23 69 66 6e 64 65 66  _LINE */.#ifndef
1b10: 20 4f 4d 49 54 5f 47 45 4f 43 41 4c 4c 42 41 43   OMIT_GEOCALLBAC
1b20: 4b 53 09 2f 2a 20 47 45 4f 2d 43 41 4c 4c 42 41  KS./* GEO-CALLBA
1b30: 43 4b 53 20 61 72 65 20 73 75 70 70 6f 72 74 65  CKS are supporte
1b40: 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  d */.    sqlite3
1b50: 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
1b60: 74 65 78 74 2c 20 31 29 3b 0a 23 65 6c 73 65 0a  text, 1);.#else.
1b70: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1b80: 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
1b90: 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 73   0);.#endif.}..s
1ba0: 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
1bb0: 68 61 73 5f 66 72 65 65 58 4c 20 28 73 71 6c 69  has_freeXL (sqli
1bc0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
1bd0: 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
1be0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1bf0: 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
1c00: 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 48 61 73   function:./ Has
1c10: 46 72 65 65 58 4c 28 29 0a 2f 0a 2f 20 72 65 74  FreeXL()././ ret
1c20: 75 72 6e 20 31 20 69 66 20 62 75 69 6c 74 20 69  urn 1 if built i
1c30: 6e 63 6c 75 64 69 6e 67 20 46 72 65 65 58 4c 3b  ncluding FreeXL;
1c40: 20 6f 74 68 65 72 77 69 73 65 20 30 0a 2a 2f 0a   otherwise 0.*/.
1c50: 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
1c60: 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
1c70: 4c 5f 4c 49 4e 45 20 2a 2f 0a 23 69 66 6e 64 65  L_LINE */.#ifnde
1c80: 66 20 4f 4d 49 54 5f 46 52 45 45 58 4c 09 09 2f  f OMIT_FREEXL../
1c90: 2a 20 46 72 65 65 58 4c 20 69 73 20 73 75 70 70  * FreeXL is supp
1ca0: 6f 72 74 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c  orted */.    sql
1cb0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
1cc0: 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 23 65  (context, 1);.#e
1cd0: 6c 73 65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  lse.    sqlite3_
1ce0: 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
1cf0: 65 78 74 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a  ext, 0);.#endif.
1d00: 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
1d10: 6e 63 74 5f 68 61 73 5f 65 70 73 67 20 28 73 71  nct_has_epsg (sq
1d20: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
1d30: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
1d40: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
1d50: 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
1d60: 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 48  QL function:./ H
1d70: 61 73 45 70 73 67 28 29 0a 2f 0a 2f 20 72 65 74  asEpsg()././ ret
1d80: 75 72 6e 20 31 20 69 66 20 62 75 69 6c 74 20 69  urn 1 if built i
1d90: 6e 63 6c 75 64 69 6e 67 20 45 50 53 47 3b 20 6f  ncluding EPSG; o
1da0: 74 68 65 72 77 69 73 65 20 30 0a 2a 2f 0a 20 20  therwise 0.*/.  
1db0: 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
1dc0: 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
1dd0: 4c 49 4e 45 20 2a 2f 0a 23 69 66 6e 64 65 66 20  LINE */.#ifndef 
1de0: 4f 4d 49 54 5f 45 50 53 47 09 09 2f 2a 20 45 50  OMIT_EPSG../* EP
1df0: 53 47 20 69 73 20 73 75 70 70 6f 72 74 65 64 20  SG is supported 
1e00: 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  */.    sqlite3_r
1e10: 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
1e20: 78 74 2c 20 31 29 3b 0a 23 65 6c 73 65 0a 20 20  xt, 1);.#else.  
1e30: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1e40: 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
1e50: 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 73 74 61  );.#endif.}..sta
1e60: 74 69 63 20 76 6f 69 64 0a 63 6c 65 61 6e 5f 73  tic void.clean_s
1e70: 71 6c 5f 73 74 72 69 6e 67 20 28 63 68 61 72 20  ql_string (char 
1e80: 2a 62 75 66 29 0a 7b 0a 2f 2a 20 77 65 6c 6c 2d  *buf).{./* well-
1e90: 66 6f 72 6d 61 74 74 69 6e 67 20 61 20 73 74 72  formatting a str
1ea0: 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 61  ing to be used a
1eb0: 73 20 61 6e 20 53 51 4c 20 73 74 72 69 6e 67 2d  s an SQL string-
1ec0: 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 63 68 61  value */.    cha
1ed0: 72 20 74 6d 70 5b 31 30 32 34 5d 3b 0a 20 20 20  r tmp[1024];.   
1ee0: 20 63 68 61 72 20 2a 69 6e 20 3d 20 74 6d 70 3b   char *in = tmp;
1ef0: 0a 20 20 20 20 63 68 61 72 20 2a 6f 75 74 20 3d  .    char *out =
1f00: 20 62 75 66 3b 0a 20 20 20 20 73 74 72 63 70 79   buf;.    strcpy
1f10: 20 28 74 6d 70 2c 20 62 75 66 29 3b 0a 20 20 20   (tmp, buf);.   
1f20: 20 77 68 69 6c 65 20 28 2a 69 6e 20 21 3d 20 27   while (*in != '
1f30: 5c 30 27 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  \0').      {..  
1f40: 69 66 20 28 2a 69 6e 20 3d 3d 20 27 5c 27 27 29  if (*in == '\'')
1f50: 0a 09 20 20 20 20 20 20 2a 6f 75 74 2b 2b 20 3d  ..      *out++ =
1f60: 20 27 5c 27 27 3b 0a 09 20 20 2a 6f 75 74 2b 2b   '\'';..  *out++
1f70: 20 3d 20 2a 69 6e 2b 2b 3b 0a 20 20 20 20 20 20   = *in++;.      
1f80: 7d 0a 20 20 20 20 2a 6f 75 74 20 3d 20 27 5c 30  }.    *out = '\0
1f90: 27 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  ';.}..static voi
1fa0: 64 0a 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f  d.double_quoted_
1fb0: 73 71 6c 20 28 63 68 61 72 20 2a 62 75 66 29 0a  sql (char *buf).
1fc0: 7b 0a 2f 2a 20 77 65 6c 6c 2d 66 6f 72 6d 61 74  {./* well-format
1fd0: 74 69 6e 67 20 61 20 73 74 72 69 6e 67 20 74 6f  ting a string to
1fe0: 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 20 53   be used as an S
1ff0: 51 4c 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63  QL name */.    c
2000: 68 61 72 20 74 6d 70 5b 31 30 32 34 5d 3b 0a 20  har tmp[1024];. 
2010: 20 20 20 63 68 61 72 20 2a 69 6e 20 3d 20 74 6d     char *in = tm
2020: 70 3b 0a 20 20 20 20 63 68 61 72 20 2a 6f 75 74  p;.    char *out
2030: 20 3d 20 62 75 66 3b 0a 20 20 20 20 73 74 72 63   = buf;.    strc
2040: 70 79 20 28 74 6d 70 2c 20 62 75 66 29 3b 0a 20  py (tmp, buf);. 
2050: 20 20 20 2a 6f 75 74 2b 2b 20 3d 20 27 22 27 3b     *out++ = '"';
2060: 0a 20 20 20 20 77 68 69 6c 65 20 28 2a 69 6e 20  .    while (*in 
2070: 21 3d 20 27 5c 30 27 29 0a 20 20 20 20 20 20 7b  != '\0').      {
2080: 0a 09 20 20 69 66 20 28 2a 69 6e 20 3d 3d 20 27  ..  if (*in == '
2090: 22 27 29 0a 09 20 20 20 20 20 20 2a 6f 75 74 2b  "')..      *out+
20a0: 2b 20 3d 20 27 22 27 3b 0a 09 20 20 2a 6f 75 74  + = '"';..  *out
20b0: 2b 2b 20 3d 20 2a 69 6e 2b 2b 3b 0a 20 20 20 20  ++ = *in++;.    
20c0: 20 20 7d 0a 20 20 20 20 2a 6f 75 74 2b 2b 20 3d    }.    *out++ =
20d0: 20 27 22 27 3b 0a 20 20 20 20 2a 6f 75 74 20 3d   '"';.    *out =
20e0: 20 27 5c 30 27 3b 0a 7d 0a 0a 73 74 61 74 69 63   '\0';.}..static
20f0: 20 76 6f 69 64 0a 66 6e 63 74 5f 47 65 6f 6d 65   void.fnct_Geome
2100: 74 72 79 43 6f 6e 73 74 72 61 69 6e 74 73 20 28  tryConstraints (
2110: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2120: 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
2130: 72 67 63 2c 0a 09 09 09 20 20 73 71 6c 69 74 65  rgc,....  sqlite
2140: 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
2150: 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
2160: 6f 6e 3a 0a 2f 20 47 65 6f 6d 65 74 72 79 43 6f  on:./ GeometryCo
2170: 6e 73 74 72 61 69 6e 74 73 28 42 4c 4f 42 65 6e  nstraints(BLOBen
2180: 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c 20  coded geometry, 
2190: 67 65 6f 6d 65 74 72 79 2d 74 79 70 65 2c 20 73  geometry-type, s
21a0: 72 69 64 29 0a 2f 20 47 65 6f 6d 65 74 72 79 43  rid)./ GeometryC
21b0: 6f 6e 73 74 72 61 69 6e 74 73 28 42 4c 4f 42 65  onstraints(BLOBe
21c0: 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c  ncoded geometry,
21d0: 20 67 65 6f 6d 65 74 72 79 2d 74 79 70 65 2c 20   geometry-type, 
21e0: 73 72 69 64 2c 20 64 69 6d 65 6e 73 69 6f 6e 73  srid, dimensions
21f0: 29 0a 2f 0a 2f 20 63 68 65 63 6b 73 20 67 65 6f  )././ checks geo
2200: 6d 65 74 72 79 20 63 6f 6e 73 74 72 61 69 6e 74  metry constraint
2210: 73 2c 20 72 65 74 75 72 6e 69 6e 67 3a 0a 2f 0a  s, returning:./.
2220: 2f 20 2d 31 20 2d 20 69 66 20 73 6f 6d 65 20 65  / -1 - if some e
2230: 72 72 6f 72 20 6f 63 63 75 72 72 65 64 0a 2f 20  rror occurred./ 
2240: 31 20 2d 20 69 66 20 67 65 6f 6d 65 74 72 79 20  1 - if geometry 
2250: 63 6f 6e 73 74 72 61 69 6e 74 73 20 76 61 6c 69  constraints vali
2260: 64 61 74 69 6f 6e 20 70 61 73 73 65 73 0a 2f 20  dation passes./ 
2270: 30 20 2d 20 69 66 20 67 65 6f 6d 65 74 72 79 20  0 - if geometry 
2280: 63 6f 6e 73 74 72 61 69 6e 74 73 20 76 61 6c 69  constraints vali
2290: 64 61 74 69 6f 6e 20 66 61 69 6c 73 0a 2f 0a 2a  dation fails./.*
22a0: 2f 0a 20 20 20 20 69 6e 74 20 6c 69 74 74 6c 65  /.    int little
22b0: 5f 65 6e 64 69 61 6e 3b 0a 20 20 20 20 69 6e 74  _endian;.    int
22c0: 20 65 6e 64 69 61 6e 5f 61 72 63 68 20 3d 20 67   endian_arch = g
22d0: 61 69 61 45 6e 64 69 61 6e 41 72 63 68 20 28 29  aiaEndianArch ()
22e0: 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
22f0: 68 61 72 20 2a 70 5f 62 6c 6f 62 20 3d 20 4e 55  har *p_blob = NU
2300: 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  LL;.    int n_by
2310: 74 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  tes = 0;.    int
2320: 20 73 72 69 64 3b 0a 20 20 20 20 69 6e 74 20 67   srid;.    int g
2330: 65 6f 6d 5f 73 72 69 64 20 3d 20 2d 31 3b 0a 20  eom_srid = -1;. 
2340: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74     const char *t
2350: 79 70 65 3b 0a 20 20 20 20 69 6e 74 20 78 74 79  ype;.    int xty
2360: 70 65 3b 0a 20 20 20 20 69 6e 74 20 67 65 6f 6d  pe;.    int geom
2370: 5f 74 79 70 65 20 3d 20 2d 31 3b 0a 20 20 20 20  _type = -1;.    
2380: 69 6e 74 20 67 65 6f 6d 5f 6e 6f 72 6d 61 6c 69  int geom_normali
2390: 7a 65 64 5f 74 79 70 65 3b 0a 20 20 20 20 63 6f  zed_type;.    co
23a0: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
23b0: 72 20 2a 64 69 6d 65 6e 73 69 6f 6e 73 3b 0a 20  r *dimensions;. 
23c0: 20 20 20 69 6e 74 20 64 69 6d 73 20 3d 20 47 41     int dims = GA
23d0: 49 41 5f 58 59 3b 0a 20 20 20 20 69 6e 74 20 72  IA_XY;.    int r
23e0: 65 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  et;.    GAIA_UNU
23f0: 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
2400: 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
2410: 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
2420: 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
2430: 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) == SQLITE_BLO
2440: 42 0a 09 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61  B..|| sqlite3_va
2450: 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
2460: 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 4e 55 4c  ]) == SQLITE_NUL
2470: 4c 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a 20  L)..;.    else. 
2480: 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
2490: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
24a0: 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72  ntext, -1);..  r
24b0: 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
24c0: 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
24d0: 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
24e0: 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 54 45  1]) == SQLITE_TE
24f0: 58 54 29 0a 09 74 79 70 65 20 3d 20 28 63 6f 6e  XT)..type = (con
2500: 73 74 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  st char *) sqlit
2510: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61  e3_value_text (a
2520: 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[1]);.    els
2530: 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
2540: 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
2550: 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
2560: 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
2570: 20 2f 2a 20 63 75 72 72 65 6e 74 20 6d 65 74 61   /* current meta
2580: 64 61 74 61 20 73 74 79 6c 65 20 3e 3d 20 76 2e  data style >= v.
2590: 34 2e 30 2e 30 20 2a 2f 0a 09 20 20 74 79 70 65  4.0.0 */..  type
25a0: 20 3d 20 22 55 4e 4b 4e 4f 57 4e 22 3b 0a 09 20   = "UNKNOWN";.. 
25b0: 20 73 77 69 74 63 68 20 28 73 71 6c 69 74 65 33   switch (sqlite3
25c0: 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76  _value_int (argv
25d0: 5b 31 5d 29 29 0a 09 20 20 20 20 7b 0a 09 20 20  [1]))..    {..  
25e0: 20 20 63 61 73 65 20 30 3a 0a 09 09 74 79 70 65    case 0:...type
25f0: 20 3d 20 22 47 45 4f 4d 45 54 52 59 22 3b 0a 09   = "GEOMETRY";..
2600: 09 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59 3b  .dims = GAIA_XY;
2610: 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
2620: 61 73 65 20 31 3a 0a 09 09 74 79 70 65 20 3d 20  ase 1:...type = 
2630: 22 50 4f 49 4e 54 22 3b 0a 09 09 64 69 6d 73 20  "POINT";...dims 
2640: 3d 20 47 41 49 41 5f 58 59 3b 0a 09 09 62 72 65  = GAIA_XY;...bre
2650: 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 32 3a  ak;..    case 2:
2660: 0a 09 09 74 79 70 65 20 3d 20 22 4c 49 4e 45 53  ...type = "LINES
2670: 54 52 49 4e 47 22 3b 0a 09 09 64 69 6d 73 20 3d  TRING";...dims =
2680: 20 47 41 49 41 5f 58 59 3b 0a 09 09 62 72 65 61   GAIA_XY;...brea
2690: 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 33 3a 0a  k;..    case 3:.
26a0: 09 09 74 79 70 65 20 3d 20 22 50 4f 4c 59 47 4f  ..type = "POLYGO
26b0: 4e 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49  N";...dims = GAI
26c0: 41 5f 58 59 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  A_XY;...break;..
26d0: 20 20 20 20 63 61 73 65 20 34 3a 0a 09 09 74 79      case 4:...ty
26e0: 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f 49 4e 54  pe = "MULTIPOINT
26f0: 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49 41  ";...dims = GAIA
2700: 5f 58 59 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20  _XY;...break;.. 
2710: 20 20 20 63 61 73 65 20 35 3a 0a 09 09 74 79 70     case 5:...typ
2720: 65 20 3d 20 22 4d 55 4c 54 49 4c 49 4e 45 53 54  e = "MULTILINEST
2730: 52 49 4e 47 22 3b 0a 09 09 64 69 6d 73 20 3d 20  RING";...dims = 
2740: 47 41 49 41 5f 58 59 3b 0a 09 09 62 72 65 61 6b  GAIA_XY;...break
2750: 3b 0a 09 20 20 20 20 63 61 73 65 20 36 3a 0a 09  ;..    case 6:..
2760: 09 74 79 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f  .type = "MULTIPO
2770: 4c 59 47 4f 4e 22 3b 0a 09 09 64 69 6d 73 20 3d  LYGON";...dims =
2780: 20 47 41 49 41 5f 58 59 3b 0a 09 09 62 72 65 61   GAIA_XY;...brea
2790: 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 37 3a 0a  k;..    case 7:.
27a0: 09 09 74 79 70 65 20 3d 20 22 47 45 4f 4d 45 54  ..type = "GEOMET
27b0: 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 22 3b 0a 09  RYCOLLECTION";..
27c0: 09 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59 3b  .dims = GAIA_XY;
27d0: 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
27e0: 61 73 65 20 31 30 30 30 3a 0a 09 09 74 79 70 65  ase 1000:...type
27f0: 20 3d 20 22 47 45 4f 4d 45 54 52 59 22 3b 0a 09   = "GEOMETRY";..
2800: 09 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59 5f  .dims = GAIA_XY_
2810: 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  Z;...break;..   
2820: 20 63 61 73 65 20 31 30 30 31 3a 0a 09 09 74 79   case 1001:...ty
2830: 70 65 20 3d 20 22 50 4f 49 4e 54 22 3b 0a 09 09  pe = "POINT";...
2840: 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59 5f 5a  dims = GAIA_XY_Z
2850: 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
2860: 63 61 73 65 20 31 30 30 32 3a 0a 09 09 74 79 70  case 1002:...typ
2870: 65 20 3d 20 22 4c 49 4e 45 53 54 52 49 4e 47 22  e = "LINESTRING"
2880: 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49 41 5f  ;...dims = GAIA_
2890: 58 59 5f 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  XY_Z;...break;..
28a0: 20 20 20 20 63 61 73 65 20 31 30 30 33 3a 0a 09      case 1003:..
28b0: 09 74 79 70 65 20 3d 20 22 50 4f 4c 59 47 4f 4e  .type = "POLYGON
28c0: 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49 41  ";...dims = GAIA
28d0: 5f 58 59 5f 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a  _XY_Z;...break;.
28e0: 09 20 20 20 20 63 61 73 65 20 31 30 30 34 3a 0a  .    case 1004:.
28f0: 09 09 74 79 70 65 20 3d 20 22 4d 55 4c 54 49 50  ..type = "MULTIP
2900: 4f 49 4e 54 22 3b 0a 09 09 64 69 6d 73 20 3d 20  OINT";...dims = 
2910: 47 41 49 41 5f 58 59 5f 5a 3b 0a 09 09 62 72 65  GAIA_XY_Z;...bre
2920: 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 31 30  ak;..    case 10
2930: 30 35 3a 0a 09 09 74 79 70 65 20 3d 20 22 4d 55  05:...type = "MU
2940: 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 22 3b 0a  LTILINESTRING";.
2950: 09 09 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59  ..dims = GAIA_XY
2960: 5f 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  _Z;...break;..  
2970: 20 20 63 61 73 65 20 31 30 30 36 3a 0a 09 09 74    case 1006:...t
2980: 79 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f 4c 59  ype = "MULTIPOLY
2990: 47 4f 4e 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47  GON";...dims = G
29a0: 41 49 41 5f 58 59 5f 5a 3b 0a 09 09 62 72 65 61  AIA_XY_Z;...brea
29b0: 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 31 30 30  k;..    case 100
29c0: 37 3a 0a 09 09 74 79 70 65 20 3d 20 22 47 45 4f  7:...type = "GEO
29d0: 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 22  METRYCOLLECTION"
29e0: 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49 41 5f  ;...dims = GAIA_
29f0: 58 59 5f 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  XY_Z;...break;..
2a00: 20 20 20 20 63 61 73 65 20 32 30 30 30 3a 0a 09      case 2000:..
2a10: 09 74 79 70 65 20 3d 20 22 47 45 4f 4d 45 54 52  .type = "GEOMETR
2a20: 59 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49  Y";...dims = GAI
2a30: 41 5f 58 59 5f 4d 3b 0a 09 09 62 72 65 61 6b 3b  A_XY_M;...break;
2a40: 0a 09 20 20 20 20 63 61 73 65 20 32 30 30 31 3a  ..    case 2001:
2a50: 0a 09 09 74 79 70 65 20 3d 20 22 50 4f 49 4e 54  ...type = "POINT
2a60: 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49 41  ";...dims = GAIA
2a70: 5f 58 59 5f 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a  _XY_M;...break;.
2a80: 09 20 20 20 20 63 61 73 65 20 32 30 30 32 3a 0a  .    case 2002:.
2a90: 09 09 74 79 70 65 20 3d 20 22 4c 49 4e 45 53 54  ..type = "LINEST
2aa0: 52 49 4e 47 22 3b 0a 09 09 64 69 6d 73 20 3d 20  RING";...dims = 
2ab0: 47 41 49 41 5f 58 59 5f 4d 3b 0a 09 09 62 72 65  GAIA_XY_M;...bre
2ac0: 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 32 30  ak;..    case 20
2ad0: 30 33 3a 0a 09 09 74 79 70 65 20 3d 20 22 50 4f  03:...type = "PO
2ae0: 4c 59 47 4f 4e 22 3b 0a 09 09 64 69 6d 73 20 3d  LYGON";...dims =
2af0: 20 47 41 49 41 5f 58 59 5f 4d 3b 0a 09 09 62 72   GAIA_XY_M;...br
2b00: 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 32  eak;..    case 2
2b10: 30 30 34 3a 0a 09 09 74 79 70 65 20 3d 20 22 4d  004:...type = "M
2b20: 55 4c 54 49 50 4f 49 4e 54 22 3b 0a 09 09 64 69  ULTIPOINT";...di
2b30: 6d 73 20 3d 20 47 41 49 41 5f 58 59 5f 4d 3b 0a  ms = GAIA_XY_M;.
2b40: 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
2b50: 73 65 20 32 30 30 35 3a 0a 09 09 74 79 70 65 20  se 2005:...type 
2b60: 3d 20 22 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49  = "MULTILINESTRI
2b70: 4e 47 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41  NG";...dims = GA
2b80: 49 41 5f 58 59 5f 4d 3b 0a 09 09 62 72 65 61 6b  IA_XY_M;...break
2b90: 3b 0a 09 20 20 20 20 63 61 73 65 20 32 30 30 36  ;..    case 2006
2ba0: 3a 0a 09 09 74 79 70 65 20 3d 20 22 4d 55 4c 54  :...type = "MULT
2bb0: 49 50 4f 4c 59 47 4f 4e 22 3b 0a 09 09 64 69 6d  IPOLYGON";...dim
2bc0: 73 20 3d 20 47 41 49 41 5f 58 59 5f 4d 3b 0a 09  s = GAIA_XY_M;..
2bd0: 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
2be0: 65 20 32 30 30 37 3a 0a 09 09 74 79 70 65 20 3d  e 2007:...type =
2bf0: 20 22 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43   "GEOMETRYCOLLEC
2c00: 54 49 4f 4e 22 3b 0a 09 09 64 69 6d 73 20 3d 20  TION";...dims = 
2c10: 47 41 49 41 5f 58 59 5f 4d 3b 0a 09 09 62 72 65  GAIA_XY_M;...bre
2c20: 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 33 30  ak;..    case 30
2c30: 30 30 3a 0a 09 09 74 79 70 65 20 3d 20 22 47 45  00:...type = "GE
2c40: 4f 4d 45 54 52 59 22 3b 0a 09 09 64 69 6d 73 20  OMETRY";...dims 
2c50: 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3b 0a 09  = GAIA_XY_Z_M;..
2c60: 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
2c70: 65 20 33 30 30 31 3a 0a 09 09 74 79 70 65 20 3d  e 3001:...type =
2c80: 20 22 50 4f 49 4e 54 22 3b 0a 09 09 64 69 6d 73   "POINT";...dims
2c90: 20 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3b 0a   = GAIA_XY_Z_M;.
2ca0: 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
2cb0: 73 65 20 33 30 30 32 3a 0a 09 09 74 79 70 65 20  se 3002:...type 
2cc0: 3d 20 22 4c 49 4e 45 53 54 52 49 4e 47 22 3b 0a  = "LINESTRING";.
2cd0: 09 09 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59  ..dims = GAIA_XY
2ce0: 5f 5a 5f 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  _Z_M;...break;..
2cf0: 20 20 20 20 63 61 73 65 20 33 30 30 33 3a 0a 09      case 3003:..
2d00: 09 74 79 70 65 20 3d 20 22 50 4f 4c 59 47 4f 4e  .type = "POLYGON
2d10: 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49 41  ";...dims = GAIA
2d20: 5f 58 59 5f 5a 5f 4d 3b 0a 09 09 62 72 65 61 6b  _XY_Z_M;...break
2d30: 3b 0a 09 20 20 20 20 63 61 73 65 20 33 30 30 34  ;..    case 3004
2d40: 3a 0a 09 09 74 79 70 65 20 3d 20 22 4d 55 4c 54  :...type = "MULT
2d50: 49 50 4f 49 4e 54 22 3b 0a 09 09 64 69 6d 73 20  IPOINT";...dims 
2d60: 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3b 0a 09  = GAIA_XY_Z_M;..
2d70: 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
2d80: 65 20 33 30 30 35 3a 0a 09 09 74 79 70 65 20 3d  e 3005:...type =
2d90: 20 22 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e   "MULTILINESTRIN
2da0: 47 22 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41 49  G";...dims = GAI
2db0: 41 5f 58 59 5f 5a 5f 4d 3b 0a 09 09 62 72 65 61  A_XY_Z_M;...brea
2dc0: 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 33 30 30  k;..    case 300
2dd0: 36 3a 0a 09 09 74 79 70 65 20 3d 20 22 4d 55 4c  6:...type = "MUL
2de0: 54 49 50 4f 4c 59 47 4f 4e 22 3b 0a 09 09 64 69  TIPOLYGON";...di
2df0: 6d 73 20 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d  ms = GAIA_XY_Z_M
2e00: 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
2e10: 63 61 73 65 20 33 30 30 37 3a 0a 09 09 74 79 70  case 3007:...typ
2e20: 65 20 3d 20 22 47 45 4f 4d 45 54 52 59 43 4f 4c  e = "GEOMETRYCOL
2e30: 4c 45 43 54 49 4f 4e 22 3b 0a 09 09 64 69 6d 73  LECTION";...dims
2e40: 20 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3b 0a   = GAIA_XY_Z_M;.
2e50: 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 7d 3b  ..break;..    };
2e60: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
2e70: 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
2e80: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
2e90: 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09  (context, -1);..
2ea0: 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
2eb0: 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
2ec0: 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
2ed0: 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[2]) == SQLITE
2ee0: 5f 49 4e 54 45 47 45 52 29 0a 09 73 72 69 64 20  _INTEGER)..srid 
2ef0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2f00: 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20  int (argv[2]);. 
2f10: 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
2f20: 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2f30: 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
2f40: 2d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  -1);..  return;.
2f50: 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
2f60: 61 72 67 63 20 3d 3d 20 34 29 0a 20 20 20 20 20  argc == 4).     
2f70: 20 7b 0a 09 20 20 2f 2a 20 65 78 70 6c 69 63 69   {..  /* explici
2f80: 74 20 64 69 6d 65 6e 73 69 6f 6e 73 20 2d 20 73  t dimensions - s
2f90: 75 70 70 6f 72 74 69 6e 67 20 58 59 5a 4d 20 2a  upporting XYZM *
2fa0: 2f 0a 09 20 20 64 69 6d 65 6e 73 69 6f 6e 73 20  /..  dimensions 
2fb0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2fc0: 74 65 78 74 20 28 61 72 67 76 5b 33 5d 29 3b 0a  text (argv[3]);.
2fd0: 09 20 20 69 66 20 28 73 74 72 63 61 73 65 63 6d  .  if (strcasecm
2fe0: 70 20 28 28 63 68 61 72 20 2a 29 20 64 69 6d 65  p ((char *) dime
2ff0: 6e 73 69 6f 6e 73 2c 20 22 58 59 5a 22 29 20 3d  nsions, "XYZ") =
3000: 3d 20 30 29 0a 09 20 20 20 20 20 20 64 69 6d 73  = 0)..      dims
3010: 20 3d 20 47 41 49 41 5f 58 59 5f 5a 3b 0a 09 20   = GAIA_XY_Z;.. 
3020: 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 61 73   else if (strcas
3030: 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20 64  ecmp ((char *) d
3040: 69 6d 65 6e 73 69 6f 6e 73 2c 20 22 58 59 4d 22  imensions, "XYM"
3050: 29 20 3d 3d 20 30 29 0a 09 20 20 20 20 20 20 64  ) == 0)..      d
3060: 69 6d 73 20 3d 20 47 41 49 41 5f 58 59 5f 4d 3b  ims = GAIA_XY_M;
3070: 0a 09 20 20 65 6c 73 65 20 69 66 20 28 73 74 72  ..  else if (str
3080: 63 61 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a  casecmp ((char *
3090: 29 20 64 69 6d 65 6e 73 69 6f 6e 73 2c 20 22 58  ) dimensions, "X
30a0: 59 5a 4d 22 29 20 3d 3d 20 30 29 0a 09 20 20 20  YZM") == 0)..   
30b0: 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41 5f 58     dims = GAIA_X
30c0: 59 5f 5a 5f 4d 3b 0a 09 20 20 65 6c 73 65 0a 09  Y_Z_M;..  else..
30d0: 20 20 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49        dims = GAI
30e0: 41 5f 58 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20  A_XY;.      }.  
30f0: 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
3100: 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
3110: 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) == SQLITE_BLO
3120: 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 70 5f  B).      {..  p_
3130: 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
3140: 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
3150: 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
3160: 76 5b 30 5d 29 3b 0a 09 20 20 6e 5f 62 79 74 65  v[0]);..  n_byte
3170: 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s = sqlite3_valu
3180: 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d  e_bytes (argv[0]
3190: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  );.      }.    i
31a0: 66 20 28 70 5f 62 6c 6f 62 29 0a 20 20 20 20 20  f (p_blob).     
31b0: 20 7b 0a 09 20 20 2f 2a 20 71 75 69 63 6b 20 47   {..  /* quick G
31c0: 65 6f 6d 65 74 72 79 20 76 61 6c 69 64 61 74 69  eometry validati
31d0: 6f 6e 20 2a 2f 0a 09 20 20 69 66 20 28 6e 5f 62  on */..  if (n_b
31e0: 79 74 65 73 20 3c 20 34 35 29 0a 09 20 20 20 20  ytes < 45)..    
31f0: 20 20 67 6f 74 6f 20 69 6c 6c 65 67 61 6c 5f 67    goto illegal_g
3200: 65 6f 6d 65 74 72 79 3b 09 2f 2a 20 63 61 6e 6e  eometry;./* cann
3210: 6f 74 20 62 65 20 61 6e 20 69 6e 74 65 72 6e 61  ot be an interna
3220: 6c 20 42 4c 4f 42 20 57 4b 42 20 67 65 6f 6d 65  l BLOB WKB geome
3230: 74 72 79 20 2a 2f 0a 09 20 20 69 66 20 28 2a 28  try */..  if (*(
3240: 70 5f 62 6c 6f 62 20 2b 20 30 29 20 21 3d 20 47  p_blob + 0) != G
3250: 41 49 41 5f 4d 41 52 4b 5f 53 54 41 52 54 29 0a  AIA_MARK_START).
3260: 09 20 20 20 20 20 20 67 6f 74 6f 20 69 6c 6c 65  .      goto ille
3270: 67 61 6c 5f 67 65 6f 6d 65 74 72 79 3b 09 2f 2a  gal_geometry;./*
3280: 20 66 61 69 6c 65 64 20 74 6f 20 72 65 63 6f 67   failed to recog
3290: 6e 69 7a 65 20 53 54 41 52 54 20 73 69 67 6e 61  nize START signa
32a0: 74 75 72 65 20 2a 2f 0a 09 20 20 69 66 20 28 2a  ture */..  if (*
32b0: 28 70 5f 62 6c 6f 62 20 2b 20 28 6e 5f 62 79 74  (p_blob + (n_byt
32c0: 65 73 20 2d 20 31 29 29 20 21 3d 20 47 41 49 41  es - 1)) != GAIA
32d0: 5f 4d 41 52 4b 5f 45 4e 44 29 0a 09 20 20 20 20  _MARK_END)..    
32e0: 20 20 67 6f 74 6f 20 69 6c 6c 65 67 61 6c 5f 67    goto illegal_g
32f0: 65 6f 6d 65 74 72 79 3b 09 2f 2a 20 66 61 69 6c  eometry;./* fail
3300: 65 64 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20  ed to recognize 
3310: 45 4e 44 20 73 69 67 6e 61 74 75 72 65 20 2a 2f  END signature */
3320: 0a 09 20 20 69 66 20 28 2a 28 70 5f 62 6c 6f 62  ..  if (*(p_blob
3330: 20 2b 20 33 38 29 20 21 3d 20 47 41 49 41 5f 4d   + 38) != GAIA_M
3340: 41 52 4b 5f 4d 42 52 29 0a 09 20 20 20 20 20 20  ARK_MBR)..      
3350: 67 6f 74 6f 20 69 6c 6c 65 67 61 6c 5f 67 65 6f  goto illegal_geo
3360: 6d 65 74 72 79 3b 09 2f 2a 20 66 61 69 6c 65 64  metry;./* failed
3370: 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20 4d 42   to recognize MB
3380: 52 20 73 69 67 6e 61 74 75 72 65 20 2a 2f 0a 09  R signature */..
3390: 20 20 69 66 20 28 2a 28 70 5f 62 6c 6f 62 20 2b    if (*(p_blob +
33a0: 20 31 29 20 3d 3d 20 47 41 49 41 5f 4c 49 54 54   1) == GAIA_LITT
33b0: 4c 45 5f 45 4e 44 49 41 4e 29 0a 09 20 20 20 20  LE_ENDIAN)..    
33c0: 20 20 6c 69 74 74 6c 65 5f 65 6e 64 69 61 6e 20    little_endian 
33d0: 3d 20 31 3b 0a 09 20 20 65 6c 73 65 20 69 66 20  = 1;..  else if 
33e0: 28 2a 28 70 5f 62 6c 6f 62 20 2b 20 31 29 20 3d  (*(p_blob + 1) =
33f0: 3d 20 47 41 49 41 5f 42 49 47 5f 45 4e 44 49 41  = GAIA_BIG_ENDIA
3400: 4e 29 0a 09 20 20 20 20 20 20 6c 69 74 74 6c 65  N)..      little
3410: 5f 65 6e 64 69 61 6e 20 3d 20 30 3b 0a 09 20 20  _endian = 0;..  
3420: 65 6c 73 65 0a 09 20 20 20 20 20 20 67 6f 74 6f  else..      goto
3430: 20 69 6c 6c 65 67 61 6c 5f 67 65 6f 6d 65 74 72   illegal_geometr
3440: 79 3b 09 2f 2a 20 75 6e 6b 6e 6f 77 6e 20 65 6e  y;./* unknown en
3450: 63 6f 64 69 6e 67 3b 20 6e 65 69 74 68 65 72 20  coding; neither 
3460: 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6e 6f  little-endian no
3470: 72 20 62 69 67 2d 65 6e 64 69 61 6e 20 2a 2f 0a  r big-endian */.
3480: 09 20 20 67 65 6f 6d 5f 74 79 70 65 20 3d 20 67  .  geom_type = g
3490: 61 69 61 49 6d 70 6f 72 74 33 32 20 28 70 5f 62  aiaImport32 (p_b
34a0: 6c 6f 62 20 2b 20 33 39 2c 20 6c 69 74 74 6c 65  lob + 39, little
34b0: 5f 65 6e 64 69 61 6e 2c 20 65 6e 64 69 61 6e 5f  _endian, endian_
34c0: 61 72 63 68 29 3b 0a 09 20 20 67 65 6f 6d 5f 73  arch);..  geom_s
34d0: 72 69 64 20 3d 20 67 61 69 61 49 6d 70 6f 72 74  rid = gaiaImport
34e0: 33 32 20 28 70 5f 62 6c 6f 62 20 2b 20 32 2c 20  32 (p_blob + 2, 
34f0: 6c 69 74 74 6c 65 5f 65 6e 64 69 61 6e 2c 20 65  little_endian, e
3500: 6e 64 69 61 6e 5f 61 72 63 68 29 3b 0a 09 20 20  ndian_arch);..  
3510: 67 6f 74 6f 20 76 61 6c 69 64 5f 67 65 6f 6d 65  goto valid_geome
3520: 74 72 79 3b 0a 09 69 6c 6c 65 67 61 6c 5f 67 65  try;..illegal_ge
3530: 6f 6d 65 74 72 79 3a 0a 09 20 20 73 71 6c 69 74  ometry:..  sqlit
3540: 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
3550: 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20  ontext, -1);..  
3560: 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
3570: 20 20 76 61 6c 69 64 5f 67 65 6f 6d 65 74 72 79    valid_geometry
3580: 3a 0a 20 20 20 20 78 74 79 70 65 20 3d 20 47 41  :.    xtype = GA
3590: 49 41 5f 55 4e 4b 4e 4f 57 4e 3b 0a 20 20 20 20  IA_UNKNOWN;.    
35a0: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28  if (strcasecmp (
35b0: 28 63 68 61 72 20 2a 29 20 74 79 70 65 2c 20 22  (char *) type, "
35c0: 50 4f 49 4e 54 22 29 20 3d 3d 20 30 29 0a 20 20  POINT") == 0).  
35d0: 20 20 20 20 7b 0a 09 20 20 73 77 69 74 63 68 20      {..  switch 
35e0: 28 64 69 6d 73 29 0a 09 20 20 20 20 7b 0a 09 20  (dims)..    {.. 
35f0: 20 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f     case GAIA_XY_
3600: 5a 3a 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49  Z:...xtype = GAI
3610: 41 5f 50 4f 49 4e 54 5a 3b 0a 09 09 62 72 65 61  A_POINTZ;...brea
3620: 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49  k;..    case GAI
3630: 41 5f 58 59 5f 4d 3a 0a 09 09 78 74 79 70 65 20  A_XY_M:...xtype 
3640: 3d 20 47 41 49 41 5f 50 4f 49 4e 54 4d 3b 0a 09  = GAIA_POINTM;..
3650: 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
3660: 65 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09  e GAIA_XY_Z_M:..
3670: 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50 4f  .xtype = GAIA_PO
3680: 49 4e 54 5a 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a  INTZM;...break;.
3690: 09 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 09 09  .    default:...
36a0: 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50 4f 49  xtype = GAIA_POI
36b0: 4e 54 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  NT;...break;..  
36c0: 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20    };.      }.   
36d0: 20 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20   if (strcasecmp 
36e0: 28 28 63 68 61 72 20 2a 29 20 74 79 70 65 2c 20  ((char *) type, 
36f0: 22 4c 49 4e 45 53 54 52 49 4e 47 22 29 20 3d 3d  "LINESTRING") ==
3700: 20 30 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73   0).      {..  s
3710: 77 69 74 63 68 20 28 64 69 6d 73 29 0a 09 20 20  witch (dims)..  
3720: 20 20 7b 0a 09 20 20 20 20 63 61 73 65 20 47 41    {..    case GA
3730: 49 41 5f 58 59 5f 5a 3a 0a 09 09 78 74 79 70 65  IA_XY_Z:...xtype
3740: 20 3d 20 47 41 49 41 5f 4c 49 4e 45 53 54 52 49   = GAIA_LINESTRI
3750: 4e 47 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20  NGZ;...break;.. 
3760: 20 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f     case GAIA_XY_
3770: 4d 3a 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49  M:...xtype = GAI
3780: 41 5f 4c 49 4e 45 53 54 52 49 4e 47 4d 3b 0a 09  A_LINESTRINGM;..
3790: 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
37a0: 65 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09  e GAIA_XY_Z_M:..
37b0: 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 4c 49  .xtype = GAIA_LI
37c0: 4e 45 53 54 52 49 4e 47 5a 4d 3b 0a 09 09 62 72  NESTRINGZM;...br
37d0: 65 61 6b 3b 0a 09 20 20 20 20 64 65 66 61 75 6c  eak;..    defaul
37e0: 74 3a 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49  t:...xtype = GAI
37f0: 41 5f 4c 49 4e 45 53 54 52 49 4e 47 3b 0a 09 09  A_LINESTRING;...
3800: 62 72 65 61 6b 3b 0a 09 20 20 20 20 7d 3b 0a 20  break;..    };. 
3810: 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73       }.    if (s
3820: 74 72 63 61 73 65 63 6d 70 20 28 28 63 68 61 72  trcasecmp ((char
3830: 20 2a 29 20 74 79 70 65 2c 20 22 50 4f 4c 59 47   *) type, "POLYG
3840: 4f 4e 22 29 20 3d 3d 20 30 29 0a 20 20 20 20 20  ON") == 0).     
3850: 20 7b 0a 09 20 20 73 77 69 74 63 68 20 28 64 69   {..  switch (di
3860: 6d 73 29 0a 09 20 20 20 20 7b 0a 09 20 20 20 20  ms)..    {..    
3870: 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a  case GAIA_XY_Z:.
3880: 09 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50  ..xtype = GAIA_P
3890: 4f 4c 59 47 4f 4e 5a 3b 0a 09 09 62 72 65 61 6b  OLYGONZ;...break
38a0: 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  ;..    case GAIA
38b0: 5f 58 59 5f 4d 3a 0a 09 09 78 74 79 70 65 20 3d  _XY_M:...xtype =
38c0: 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 4d 3b 0a   GAIA_POLYGONM;.
38d0: 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
38e0: 73 65 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a  se GAIA_XY_Z_M:.
38f0: 09 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50  ..xtype = GAIA_P
3900: 4f 4c 59 47 4f 4e 5a 4d 3b 0a 09 09 62 72 65 61  OLYGONZM;...brea
3910: 6b 3b 0a 09 20 20 20 20 64 65 66 61 75 6c 74 3a  k;..    default:
3920: 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f  ...xtype = GAIA_
3930: 50 4f 4c 59 47 4f 4e 3b 0a 09 09 62 72 65 61 6b  POLYGON;...break
3940: 3b 0a 09 20 20 20 20 7d 3b 0a 20 20 20 20 20 20  ;..    };.      
3950: 7d 0a 20 20 20 20 69 66 20 28 73 74 72 63 61 73  }.    if (strcas
3960: 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20 74  ecmp ((char *) t
3970: 79 70 65 2c 20 22 4d 55 4c 54 49 50 4f 49 4e 54  ype, "MULTIPOINT
3980: 22 29 20 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b  ") == 0).      {
3990: 0a 09 20 20 73 77 69 74 63 68 20 28 64 69 6d 73  ..  switch (dims
39a0: 29 0a 09 20 20 20 20 7b 0a 09 20 20 20 20 63 61  )..    {..    ca
39b0: 73 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09 09  se GAIA_XY_Z:...
39c0: 78 74 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c  xtype = GAIA_MUL
39d0: 54 49 50 4f 49 4e 54 5a 3b 0a 09 09 62 72 65 61  TIPOINTZ;...brea
39e0: 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49  k;..    case GAI
39f0: 41 5f 58 59 5f 4d 3a 0a 09 09 78 74 79 70 65 20  A_XY_M:...xtype 
3a00: 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e  = GAIA_MULTIPOIN
3a10: 54 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  TM;...break;..  
3a20: 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a    case GAIA_XY_Z
3a30: 5f 4d 3a 0a 09 09 78 74 79 70 65 20 3d 20 47 41  _M:...xtype = GA
3a40: 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 5a 4d 3b  IA_MULTIPOINTZM;
3a50: 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 64  ...break;..    d
3a60: 65 66 61 75 6c 74 3a 0a 09 09 78 74 79 70 65 20  efault:...xtype 
3a70: 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e  = GAIA_MULTIPOIN
3a80: 54 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  T;...break;..   
3a90: 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   };.      }.    
3aa0: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28  if (strcasecmp (
3ab0: 28 63 68 61 72 20 2a 29 20 74 79 70 65 2c 20 22  (char *) type, "
3ac0: 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 22  MULTILINESTRING"
3ad0: 29 20 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a  ) == 0).      {.
3ae0: 09 20 20 73 77 69 74 63 68 20 28 64 69 6d 73 29  .  switch (dims)
3af0: 0a 09 20 20 20 20 7b 0a 09 20 20 20 20 63 61 73  ..    {..    cas
3b00: 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09 09 78  e GAIA_XY_Z:...x
3b10: 74 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54  type = GAIA_MULT
3b20: 49 4c 49 4e 45 53 54 52 49 4e 47 5a 3b 0a 09 09  ILINESTRINGZ;...
3b30: 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
3b40: 20 47 41 49 41 5f 58 59 5f 4d 3a 0a 09 09 78 74   GAIA_XY_M:...xt
3b50: 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49  ype = GAIA_MULTI
3b60: 4c 49 4e 45 53 54 52 49 4e 47 4d 3b 0a 09 09 62  LINESTRINGM;...b
3b70: 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
3b80: 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09 09 78  GAIA_XY_Z_M:...x
3b90: 74 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54  type = GAIA_MULT
3ba0: 49 4c 49 4e 45 53 54 52 49 4e 47 5a 4d 3b 0a 09  ILINESTRINGZM;..
3bb0: 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 64 65 66  .break;..    def
3bc0: 61 75 6c 74 3a 0a 09 09 78 74 79 70 65 20 3d 20  ault:...xtype = 
3bd0: 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54  GAIA_MULTILINEST
3be0: 52 49 4e 47 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  RING;...break;..
3bf0: 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20      };.      }. 
3c00: 20 20 20 69 66 20 28 73 74 72 63 61 73 65 63 6d     if (strcasecm
3c10: 70 20 28 28 63 68 61 72 20 2a 29 20 74 79 70 65  p ((char *) type
3c20: 2c 20 22 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 22  , "MULTIPOLYGON"
3c30: 29 20 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a  ) == 0).      {.
3c40: 09 20 20 73 77 69 74 63 68 20 28 64 69 6d 73 29  .  switch (dims)
3c50: 0a 09 20 20 20 20 7b 0a 09 20 20 20 20 63 61 73  ..    {..    cas
3c60: 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09 09 78  e GAIA_XY_Z:...x
3c70: 74 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54  type = GAIA_MULT
3c80: 49 50 4f 4c 59 47 4f 4e 5a 3b 0a 09 09 62 72 65  IPOLYGONZ;...bre
3c90: 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
3ca0: 49 41 5f 58 59 5f 4d 3a 0a 09 09 78 74 79 70 65  IA_XY_M:...xtype
3cb0: 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c   = GAIA_MULTIPOL
3cc0: 59 47 4f 4e 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a  YGONM;...break;.
3cd0: 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 58  .    case GAIA_X
3ce0: 59 5f 5a 5f 4d 3a 0a 09 09 78 74 79 70 65 20 3d  Y_Z_M:...xtype =
3cf0: 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47   GAIA_MULTIPOLYG
3d00: 4f 4e 5a 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  ONZM;...break;..
3d10: 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 09 09 78      default:...x
3d20: 74 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54  type = GAIA_MULT
3d30: 49 50 4f 4c 59 47 4f 4e 3b 0a 09 09 62 72 65 61  IPOLYGON;...brea
3d40: 6b 3b 0a 09 20 20 20 20 7d 3b 0a 20 20 20 20 20  k;..    };.     
3d50: 20 7d 0a 20 20 20 20 69 66 20 28 73 74 72 63 61   }.    if (strca
3d60: 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20  secmp ((char *) 
3d70: 74 79 70 65 2c 20 22 47 45 4f 4d 45 54 52 59 43  type, "GEOMETRYC
3d80: 4f 4c 4c 45 43 54 49 4f 4e 22 29 20 3d 3d 20 30  OLLECTION") == 0
3d90: 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 77 69  ).      {..  swi
3da0: 74 63 68 20 28 64 69 6d 73 29 0a 09 20 20 20 20  tch (dims)..    
3db0: 7b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  {..    case GAIA
3dc0: 5f 58 59 5f 5a 3a 0a 09 09 78 74 79 70 65 20 3d  _XY_Z:...xtype =
3dd0: 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f   GAIA_GEOMETRYCO
3de0: 4c 4c 45 43 54 49 4f 4e 5a 3b 0a 09 09 62 72 65  LLECTIONZ;...bre
3df0: 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
3e00: 49 41 5f 58 59 5f 4d 3a 0a 09 09 78 74 79 70 65  IA_XY_M:...xtype
3e10: 20 3d 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59   = GAIA_GEOMETRY
3e20: 43 4f 4c 4c 45 43 54 49 4f 4e 4d 3b 0a 09 09 62  COLLECTIONM;...b
3e30: 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
3e40: 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09 09 78  GAIA_XY_Z_M:...x
3e50: 74 79 70 65 20 3d 20 47 41 49 41 5f 47 45 4f 4d  type = GAIA_GEOM
3e60: 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 5a 4d  ETRYCOLLECTIONZM
3e70: 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
3e80: 64 65 66 61 75 6c 74 3a 0a 09 09 78 74 79 70 65  default:...xtype
3e90: 20 3d 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59   = GAIA_GEOMETRY
3ea0: 43 4f 4c 4c 45 43 54 49 4f 4e 3b 0a 09 09 62 72  COLLECTION;...br
3eb0: 65 61 6b 3b 0a 09 20 20 20 20 7d 3b 0a 20 20 20  eak;..    };.   
3ec0: 20 20 20 7d 0a 20 20 20 20 73 77 69 74 63 68 20     }.    switch 
3ed0: 28 67 65 6f 6d 5f 74 79 70 65 29 0a 20 20 20 20  (geom_type).    
3ee0: 20 20 7b 0a 09 20 20 2f 2a 20 61 64 6a 75 73 74    {..  /* adjust
3ef0: 69 6e 67 20 43 4f 4d 50 52 45 53 53 45 44 20 47  ing COMPRESSED G
3f00: 65 6f 6d 65 74 72 69 65 73 20 2a 2f 0a 20 20 20  eometries */.   
3f10: 20 20 20 63 61 73 65 20 47 41 49 41 5f 43 4f 4d     case GAIA_COM
3f20: 50 52 45 53 53 45 44 5f 4c 49 4e 45 53 54 52 49  PRESSED_LINESTRI
3f30: 4e 47 3a 0a 09 20 20 67 65 6f 6d 5f 6e 6f 72 6d  NG:..  geom_norm
3f40: 61 6c 69 7a 65 64 5f 74 79 70 65 20 3d 20 47 41  alized_type = GA
3f50: 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 3b 0a 09  IA_LINESTRING;..
3f60: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3f70: 61 73 65 20 47 41 49 41 5f 43 4f 4d 50 52 45 53  ase GAIA_COMPRES
3f80: 53 45 44 5f 4c 49 4e 45 53 54 52 49 4e 47 5a 3a  SED_LINESTRINGZ:
3f90: 0a 09 20 20 67 65 6f 6d 5f 6e 6f 72 6d 61 6c 69  ..  geom_normali
3fa0: 7a 65 64 5f 74 79 70 65 20 3d 20 47 41 49 41 5f  zed_type = GAIA_
3fb0: 4c 49 4e 45 53 54 52 49 4e 47 5a 3b 0a 09 20 20  LINESTRINGZ;..  
3fc0: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3fd0: 65 20 47 41 49 41 5f 43 4f 4d 50 52 45 53 53 45  e GAIA_COMPRESSE
3fe0: 44 5f 4c 49 4e 45 53 54 52 49 4e 47 4d 3a 0a 09  D_LINESTRINGM:..
3ff0: 20 20 67 65 6f 6d 5f 6e 6f 72 6d 61 6c 69 7a 65    geom_normalize
4000: 64 5f 74 79 70 65 20 3d 20 47 41 49 41 5f 4c 49  d_type = GAIA_LI
4010: 4e 45 53 54 52 49 4e 47 4d 3b 0a 09 20 20 62 72  NESTRINGM;..  br
4020: 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
4030: 47 41 49 41 5f 43 4f 4d 50 52 45 53 53 45 44 5f  GAIA_COMPRESSED_
4040: 4c 49 4e 45 53 54 52 49 4e 47 5a 4d 3a 0a 09 20  LINESTRINGZM:.. 
4050: 20 67 65 6f 6d 5f 6e 6f 72 6d 61 6c 69 7a 65 64   geom_normalized
4060: 5f 74 79 70 65 20 3d 20 47 41 49 41 5f 4c 49 4e  _type = GAIA_LIN
4070: 45 53 54 52 49 4e 47 5a 4d 3b 0a 09 20 20 62 72  ESTRINGZM;..  br
4080: 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
4090: 47 41 49 41 5f 43 4f 4d 50 52 45 53 53 45 44 5f  GAIA_COMPRESSED_
40a0: 50 4f 4c 59 47 4f 4e 3a 0a 09 20 20 67 65 6f 6d  POLYGON:..  geom
40b0: 5f 6e 6f 72 6d 61 6c 69 7a 65 64 5f 74 79 70 65  _normalized_type
40c0: 20 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 3b   = GAIA_POLYGON;
40d0: 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  ..  break;.     
40e0: 20 63 61 73 65 20 47 41 49 41 5f 43 4f 4d 50 52   case GAIA_COMPR
40f0: 45 53 53 45 44 5f 50 4f 4c 59 47 4f 4e 5a 3a 0a  ESSED_POLYGONZ:.
4100: 09 20 20 67 65 6f 6d 5f 6e 6f 72 6d 61 6c 69 7a  .  geom_normaliz
4110: 65 64 5f 74 79 70 65 20 3d 20 47 41 49 41 5f 50  ed_type = GAIA_P
4120: 4f 4c 59 47 4f 4e 5a 3b 0a 09 20 20 62 72 65 61  OLYGONZ;..  brea
4130: 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 47 41  k;.      case GA
4140: 49 41 5f 43 4f 4d 50 52 45 53 53 45 44 5f 50 4f  IA_COMPRESSED_PO
4150: 4c 59 47 4f 4e 4d 3a 0a 09 20 20 67 65 6f 6d 5f  LYGONM:..  geom_
4160: 6e 6f 72 6d 61 6c 69 7a 65 64 5f 74 79 70 65 20  normalized_type 
4170: 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 4d 3b  = GAIA_POLYGONM;
4180: 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  ..  break;.     
4190: 20 63 61 73 65 20 47 41 49 41 5f 43 4f 4d 50 52   case GAIA_COMPR
41a0: 45 53 53 45 44 5f 50 4f 4c 59 47 4f 4e 5a 4d 3a  ESSED_POLYGONZM:
41b0: 0a 09 20 20 67 65 6f 6d 5f 6e 6f 72 6d 61 6c 69  ..  geom_normali
41c0: 7a 65 64 5f 74 79 70 65 20 3d 20 47 41 49 41 5f  zed_type = GAIA_
41d0: 50 4f 4c 59 47 4f 4e 5a 4d 3b 0a 09 20 20 62 72  POLYGONZM;..  br
41e0: 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75  eak;.      defau
41f0: 6c 74 3a 0a 09 20 20 67 65 6f 6d 5f 6e 6f 72 6d  lt:..  geom_norm
4200: 61 6c 69 7a 65 64 5f 74 79 70 65 20 3d 20 67 65  alized_type = ge
4210: 6f 6d 5f 74 79 70 65 3b 0a 09 20 20 62 72 65 61  om_type;..  brea
4220: 6b 3b 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20  k;.      };.    
4230: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28  if (strcasecmp (
4240: 28 63 68 61 72 20 2a 29 20 74 79 70 65 2c 20 22  (char *) type, "
4250: 47 45 4f 4d 45 54 52 59 22 29 20 3d 3d 20 30 29  GEOMETRY") == 0)
4260: 0a 09 78 74 79 70 65 20 3d 20 2d 31 3b 0a 20 20  ..xtype = -1;.  
4270: 20 20 69 66 20 28 78 74 79 70 65 20 3d 3d 20 47    if (xtype == G
4280: 41 49 41 5f 55 4e 4b 4e 4f 57 4e 29 0a 09 73 71  AIA_UNKNOWN)..sq
4290: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
42a0: 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a   (context, -1);.
42b0: 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
42c0: 0a 09 20 20 72 65 74 20 3d 20 31 3b 0a 09 20 20  ..  ret = 1;..  
42d0: 69 66 20 28 70 5f 62 6c 6f 62 29 0a 09 20 20 20  if (p_blob)..   
42e0: 20 7b 0a 09 09 2f 2a 20 73 6b 69 70 70 69 6e 67   {.../* skipping
42f0: 20 4e 55 4c 4c 20 47 65 6f 6d 65 74 72 79 3b 20   NULL Geometry; 
4300: 74 68 69 73 20 69 73 20 61 73 73 75 6d 65 64 20  this is assumed 
4310: 74 6f 20 62 65 20 61 6c 77 61 79 73 20 67 6f 6f  to be always goo
4320: 64 20 2a 2f 0a 09 09 69 66 20 28 67 65 6f 6d 5f  d */...if (geom_
4330: 73 72 69 64 20 21 3d 20 73 72 69 64 29 0a 09 09  srid != srid)...
4340: 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 09 09 69      ret = 0;...i
4350: 66 20 28 78 74 79 70 65 20 3d 3d 20 2d 31 29 0a  f (xtype == -1).
4360: 09 09 20 20 20 20 3b 0a 09 09 65 6c 73 65 20 69  ..    ;...else i
4370: 66 20 28 78 74 79 70 65 20 21 3d 20 67 65 6f 6d  f (xtype != geom
4380: 5f 6e 6f 72 6d 61 6c 69 7a 65 64 5f 74 79 70 65  _normalized_type
4390: 29 0a 09 09 20 20 20 20 72 65 74 20 3d 20 30 3b  )...    ret = 0;
43a0: 0a 09 20 20 20 20 7d 0a 09 20 20 73 71 6c 69 74  ..    }..  sqlit
43b0: 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
43c0: 6f 6e 74 65 78 74 2c 20 72 65 74 29 3b 0a 20 20  ontext, ret);.  
43d0: 20 20 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20      }.}..static 
43e0: 76 6f 69 64 0a 66 6e 63 74 5f 52 54 72 65 65 41  void.fnct_RTreeA
43f0: 6c 69 67 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f  lign (sqlite3_co
4400: 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
4410: 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
4420: 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
4430: 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
4440: 69 6f 6e 3a 0a 2f 20 52 54 72 65 65 41 6c 69 67  ion:./ RTreeAlig
4450: 6e 28 52 54 72 65 65 2d 74 61 62 6c 65 2d 6e 61  n(RTree-table-na
4460: 6d 65 2c 20 50 4b 49 44 2d 76 61 6c 75 65 2c 20  me, PKID-value, 
4470: 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d  BLOBencoded geom
4480: 65 74 72 79 29 0a 2f 0a 2f 20 61 74 74 65 6d 70  etry)././ attemp
4490: 74 73 20 74 6f 20 75 70 64 61 74 65 20 74 68 65  ts to update the
44a0: 20 61 73 73 6f 63 69 61 74 65 64 20 52 2a 54 72   associated R*Tr
44b0: 65 65 2c 20 72 65 74 75 72 6e 69 6e 67 3a 0a 2f  ee, returning:./
44c0: 0a 2f 20 2d 31 20 2d 20 69 66 20 73 6f 6d 65 20  ./ -1 - if some 
44d0: 69 6e 76 61 6c 69 64 20 61 72 67 20 77 61 73 20  invalid arg was 
44e0: 70 61 73 73 65 64 0a 2f 20 31 20 2d 20 73 75 63  passed./ 1 - suc
44f0: 63 65 73 66 75 6c 6c 20 75 70 64 61 74 65 0a 2f  cesfull update./
4500: 20 30 20 2d 20 75 70 64 61 74 65 20 66 61 69 6c   0 - update fail
4510: 75 72 65 0a 2f 0a 2a 2f 0a 20 20 20 20 75 6e 73  ure./.*/.    uns
4520: 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
4530: 6f 62 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  ob = NULL;.    i
4540: 6e 74 20 6e 5f 62 79 74 65 73 20 3d 20 30 3b 0a  nt n_bytes = 0;.
4550: 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
4560: 34 20 70 6b 69 64 3b 0a 20 20 20 20 63 6f 6e 73  4 pkid;.    cons
4570: 74 20 63 68 61 72 20 2a 72 74 72 65 65 5f 74 61  t char *rtree_ta
4580: 62 6c 65 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ble;.    gaiaGeo
4590: 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d 20 3d 20  mCollPtr geom = 
45a0: 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 65  NULL;.    int re
45b0: 74 3b 0a 20 20 20 20 63 68 61 72 20 74 61 62 6c  t;.    char tabl
45c0: 65 5f 6e 61 6d 65 5b 31 30 32 34 5d 3b 0a 20 20  e_name[1024];.  
45d0: 20 20 63 68 61 72 20 73 71 6c 5b 34 31 39 32 5d    char sql[4192]
45e0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73  ;.    sqlite3 *s
45f0: 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f  qlite = sqlite3_
4600: 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
4610: 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  e (context);.   
4620: 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
4630: 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
4640: 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
4650: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
4660: 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53  e (argv[0]) == S
4670: 51 4c 49 54 45 5f 54 45 58 54 29 0a 09 72 74 72  QLITE_TEXT)..rtr
4680: 65 65 5f 74 61 62 6c 65 20 3d 20 28 63 6f 6e 73  ee_table = (cons
4690: 74 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  t char *) sqlite
46a0: 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72  3_value_text (ar
46b0: 67 76 5b 30 5d 29 3b 0a 20 20 20 20 65 6c 73 65  gv[0]);.    else
46c0: 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
46d0: 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
46e0: 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20  context, -1);.. 
46f0: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
4700: 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
4710: 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
4720: 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[1]) == SQLITE_
4730: 49 4e 54 45 47 45 52 29 0a 09 70 6b 69 64 20 3d  INTEGER)..pkid =
4740: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
4750: 6e 74 36 34 20 28 61 72 67 76 5b 31 5d 29 3b 0a  nt64 (argv[1]);.
4760: 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
4770: 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
4780: 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
4790: 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b   -1);..  return;
47a0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
47b0: 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
47c0: 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d 3d  ype (argv[2]) ==
47d0: 20 53 51 4c 49 54 45 5f 42 4c 4f 42 0a 09 7c 7c   SQLITE_BLOB..||
47e0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
47f0: 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d 3d  ype (argv[2]) ==
4800: 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 3b   SQLITE_NULL)..;
4810: 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
4820: 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
4830: 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
4840: 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e  , -1);..  return
4850: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
4860: 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
4870: 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d  type (argv[2]) =
4880: 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
4890: 20 20 20 20 20 7b 0a 09 20 20 70 5f 62 6c 6f 62       {..  p_blob
48a0: 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
48b0: 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
48c0: 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 32 5d  ue_blob (argv[2]
48d0: 29 3b 0a 09 20 20 6e 5f 62 79 74 65 73 20 3d 20  );..  n_bytes = 
48e0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
48f0: 74 65 73 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09  tes (argv[2]);..
4900: 20 20 67 65 6f 6d 20 3d 20 67 61 69 61 46 72 6f    geom = gaiaFro
4910: 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
4920: 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
4930: 74 65 73 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  tes);.      }.. 
4940: 20 20 20 69 66 20 28 67 65 6f 6d 20 3d 3d 20 4e     if (geom == N
4950: 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  ULL).      {..  
4960: 2f 2a 20 4e 55 4c 4c 20 67 65 6f 6d 65 74 72 79  /* NULL geometry
4970: 3a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20  : nothing to do 
4980: 2a 2f 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  */..  sqlite3_re
4990: 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
49a0: 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  t, 1);.      }. 
49b0: 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
49c0: 09 20 20 2f 2a 20 49 4e 53 45 52 54 69 6e 67 20  .  /* INSERTing 
49d0: 69 6e 74 6f 20 74 68 65 20 52 2a 54 72 65 65 20  into the R*Tree 
49e0: 2a 2f 0a 09 20 20 73 74 72 63 70 79 20 28 74 61  */..  strcpy (ta
49f0: 62 6c 65 5f 6e 61 6d 65 2c 20 72 74 72 65 65 5f  ble_name, rtree_
4a00: 74 61 62 6c 65 29 3b 0a 09 20 20 69 66 20 28 2a  table);..  if (*
4a10: 28 74 61 62 6c 65 5f 6e 61 6d 65 20 2b 20 30 29  (table_name + 0)
4a20: 20 3d 3d 20 27 22 27 0a 09 20 20 20 20 20 20 26   == '"'..      &
4a30: 26 20 2a 28 74 61 62 6c 65 5f 6e 61 6d 65 20 2b  & *(table_name +
4a40: 20 73 74 72 6c 65 6e 20 28 74 61 62 6c 65 5f 6e   strlen (table_n
4a50: 61 6d 65 29 20 2d 20 31 29 20 3d 3d 20 27 22 27  ame) - 1) == '"'
4a60: 29 0a 09 20 20 20 20 20 20 3b 09 09 09 2f 2a 20  )..      ;.../* 
4a70: 65 61 72 6c 69 65 72 20 76 65 72 73 69 6f 6e 73  earlier versions
4a80: 20 6d 61 79 20 70 61 73 73 20 61 6e 20 61 6c 72   may pass an alr
4a90: 65 61 64 79 20 71 75 6f 74 65 64 20 6e 61 6d 65  eady quoted name
4aa0: 20 2a 2f 0a 09 20 20 65 6c 73 65 0a 09 20 20 20   */..  else..   
4ab0: 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
4ac0: 5f 73 71 6c 20 28 74 61 62 6c 65 5f 6e 61 6d 65  _sql (table_name
4ad0: 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  );.#if defined(_
4ae0: 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65  WIN32) || define
4af0: 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f 29 0a 2f  d(__MINGW32__)./
4b00: 2a 20 43 41 56 45 41 54 3a 20 4d 24 20 72 75 6e  * CAVEAT: M$ run
4b10: 74 69 6d 65 20 64 6f 65 73 6e 27 74 20 73 75 70  time doesn't sup
4b20: 70 6f 72 74 73 20 25 6c 6c 64 20 66 6f 72 20 36  ports %lld for 6
4b30: 34 20 62 69 74 73 20 2a 2f 0a 09 20 20 73 70 72  4 bits */..  spr
4b40: 69 6e 74 66 20 28 73 71 6c 2c 20 22 49 4e 53 45  intf (sql, "INSE
4b50: 52 54 20 49 4e 54 4f 20 25 73 20 28 70 6b 69 64  RT INTO %s (pkid
4b60: 2c 20 78 6d 69 6e 2c 20 79 6d 69 6e 2c 20 78 6d  , xmin, ymin, xm
4b70: 61 78 2c 20 79 6d 61 78 29 20 22 0a 09 09 20 20  ax, ymax) "...  
4b80: 20 22 56 41 4c 55 45 53 20 28 25 49 36 34 64 2c   "VALUES (%I64d,
4b90: 20 25 31 2e 31 32 66 2c 20 25 31 2e 31 32 66 2c   %1.12f, %1.12f,
4ba0: 20 25 31 2e 31 32 66 2c 20 25 31 2e 31 32 66 29   %1.12f, %1.12f)
4bb0: 22 2c 0a 09 09 20 20 20 74 61 62 6c 65 5f 6e 61  ",...   table_na
4bc0: 6d 65 2c 20 70 6b 69 64 2c 20 67 65 6f 6d 2d 3e  me, pkid, geom->
4bd0: 4d 69 6e 58 2c 20 67 65 6f 6d 2d 3e 4d 69 6e 59  MinX, geom->MinY
4be0: 2c 20 67 65 6f 6d 2d 3e 4d 61 78 58 2c 0a 09 09  , geom->MaxX,...
4bf0: 20 20 20 67 65 6f 6d 2d 3e 4d 61 78 59 29 3b 0a     geom->MaxY);.
4c00: 23 65 6c 73 65 0a 09 20 20 73 70 72 69 6e 74 66  #else..  sprintf
4c10: 20 28 73 71 6c 2c 20 22 49 4e 53 45 52 54 20 49   (sql, "INSERT I
4c20: 4e 54 4f 20 25 73 20 28 70 6b 69 64 2c 20 78 6d  NTO %s (pkid, xm
4c30: 69 6e 2c 20 79 6d 69 6e 2c 20 78 6d 61 78 2c 20  in, ymin, xmax, 
4c40: 79 6d 61 78 29 20 22 0a 09 09 20 20 20 22 56 41  ymax) "...   "VA
4c50: 4c 55 45 53 20 28 25 6c 6c 64 2c 20 25 31 2e 31  LUES (%lld, %1.1
4c60: 32 66 2c 20 25 31 2e 31 32 66 2c 20 25 31 2e 31  2f, %1.12f, %1.1
4c70: 32 66 2c 20 25 31 2e 31 32 66 29 22 2c 0a 09 09  2f, %1.12f)",...
4c80: 20 20 20 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 70     table_name, p
4c90: 6b 69 64 2c 20 67 65 6f 6d 2d 3e 4d 69 6e 58 2c  kid, geom->MinX,
4ca0: 20 67 65 6f 6d 2d 3e 4d 69 6e 59 2c 20 67 65 6f   geom->MinY, geo
4cb0: 6d 2d 3e 4d 61 78 58 2c 0a 09 09 20 20 20 67 65  m->MaxX,...   ge
4cc0: 6f 6d 2d 3e 4d 61 78 59 29 3b 0a 23 65 6e 64 69  om->MaxY);.#endi
4cd0: 66 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f  f..  gaiaFreeGeo
4ce0: 6d 43 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a 09 20  mColl (geom);.. 
4cf0: 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65   ret = sqlite3_e
4d00: 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c  xec (sqlite, sql
4d10: 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 4e 55  , NULL, NULL, NU
4d20: 4c 4c 29 3b 0a 09 20 20 69 66 20 28 72 65 74 20  LL);..  if (ret 
4d30: 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 20  != SQLITE_OK).. 
4d40: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
4d50: 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
4d60: 2c 20 30 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20  , 0);..  else.. 
4d70: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
4d80: 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
4d90: 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a  , 1);.      }.}.
4da0: 0a 53 50 41 54 49 41 4c 49 54 45 5f 50 52 49 56  .SPATIALITE_PRIV
4db0: 41 54 45 20 69 6e 74 0a 63 68 65 63 6b 53 70 61  ATE int.checkSpa
4dc0: 74 69 61 6c 4d 65 74 61 44 61 74 61 20 28 63 6f  tialMetaData (co
4dd0: 6e 73 74 20 76 6f 69 64 20 2a 68 61 6e 64 6c 65  nst void *handle
4de0: 29 0a 7b 0a 2f 2a 20 69 6e 74 65 72 6e 61 6c 20  ).{./* internal 
4df0: 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e  utility function
4e00: 3a 0a 2f 0a 2f 20 66 6f 72 20 46 44 4f 2d 4f 47  :././ for FDO-OG
4e10: 52 20 69 6e 74 65 72 6f 70 65 72 61 62 69 6c 69  R interoperabili
4e20: 74 79 20 61 6e 64 20 63 72 6f 73 73 2d 76 65 72  ty and cross-ver
4e30: 73 69 6f 6e 20 73 65 61 6d 6c 65 73 73 20 63 6f  sion seamless co
4e40: 6d 70 61 74 69 62 69 6c 69 74 79 3a 0a 2f 20 74  mpatibility:./ t
4e50: 65 73 74 73 20 74 68 65 20 53 70 61 74 69 61 6c  ests the Spatial
4e60: 4d 65 74 61 64 61 74 61 20 74 79 70 65 2c 20 72  Metadata type, r
4e70: 65 74 75 72 6e 69 6e 67 3a 0a 2f 0a 2f 20 30 20  eturning:././ 0 
4e80: 2d 20 69 66 20 6e 6f 20 76 61 6c 69 64 20 53 70  - if no valid Sp
4e90: 61 74 69 61 6c 4d 65 74 61 44 61 74 61 20 77 68  atialMetaData wh
4ea0: 65 72 65 20 66 6f 75 6e 64 0a 2f 20 31 20 2d 20  ere found./ 1 - 
4eb0: 69 66 20 53 70 61 74 69 61 4c 69 74 65 2d 6c 69  if SpatiaLite-li
4ec0: 6b 65 20 28 6c 65 67 61 63 79 29 20 53 70 61 74  ke (legacy) Spat
4ed0: 69 61 6c 4d 65 74 61 64 61 74 61 20 77 68 65 72  ialMetadata wher
4ee0: 65 20 66 6f 75 6e 64 0a 2f 20 32 20 2d 20 69 66  e found./ 2 - if
4ef0: 20 46 44 4f 2d 4f 47 52 2d 6c 69 6b 65 20 53 70   FDO-OGR-like Sp
4f00: 61 74 69 61 6c 4d 65 74 61 64 61 74 61 20 77 68  atialMetadata wh
4f10: 65 72 65 20 66 6f 75 6e 64 0a 2f 20 33 20 2d 20  ere found./ 3 - 
4f20: 69 66 20 53 70 61 74 69 61 4c 69 74 65 2d 6c 69  if SpatiaLite-li
4f30: 6b 65 20 28 63 75 72 72 65 6e 74 29 20 53 70 61  ke (current) Spa
4f40: 74 69 61 6c 4d 65 74 61 64 61 74 61 20 77 68 65  tialMetadata whe
4f50: 72 65 20 66 6f 75 6e 64 0a 2f 0a 2a 2f 0a 20 20  re found./.*/.  
4f60: 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74    sqlite3 *sqlit
4f70: 65 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 20  e = (sqlite3 *) 
4f80: 68 61 6e 64 6c 65 3b 0a 20 20 20 20 69 6e 74 20  handle;.    int 
4f90: 73 70 61 74 69 61 6c 69 74 65 5f 6c 65 67 61 63  spatialite_legac
4fa0: 79 5f 72 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e  y_rs = 0;.    in
4fb0: 74 20 73 70 61 74 69 61 6c 69 74 65 5f 72 73 20  t spatialite_rs 
4fc0: 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 66 64 6f  = 0;.    int fdo
4fd0: 5f 72 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  _rs = 0;.    int
4fe0: 20 73 70 61 74 69 61 6c 69 74 65 5f 6c 65 67 61   spatialite_lega
4ff0: 63 79 5f 67 63 20 3d 20 30 3b 0a 20 20 20 20 69  cy_gc = 0;.    i
5000: 6e 74 20 73 70 61 74 69 61 6c 69 74 65 5f 67 63  nt spatialite_gc
5010: 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 66 64   = 0;.    int fd
5020: 6f 5f 67 63 20 3d 20 30 3b 0a 20 20 20 20 69 6e  o_gc = 0;.    in
5030: 74 20 72 73 5f 73 72 69 64 20 3d 20 30 3b 0a 20  t rs_srid = 0;. 
5040: 20 20 20 69 6e 74 20 61 75 74 68 5f 6e 61 6d 65     int auth_name
5050: 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 61 75   = 0;.    int au
5060: 74 68 5f 73 72 69 64 20 3d 20 30 3b 0a 20 20 20  th_srid = 0;.   
5070: 20 69 6e 74 20 73 72 74 65 78 74 20 3d 20 30 3b   int srtext = 0;
5080: 0a 20 20 20 20 69 6e 74 20 72 65 66 5f 73 79 73  .    int ref_sys
5090: 5f 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 69  _name = 0;.    i
50a0: 6e 74 20 70 72 6f 6a 34 74 65 78 74 20 3d 20 30  nt proj4text = 0
50b0: 3b 0a 20 20 20 20 69 6e 74 20 66 5f 74 61 62 6c  ;.    int f_tabl
50c0: 65 5f 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  e_name = 0;.    
50d0: 69 6e 74 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63  int f_geometry_c
50e0: 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 20 20 69  olumn = 0;.    i
50f0: 6e 74 20 67 65 6f 6d 65 74 72 79 5f 74 79 70 65  nt geometry_type
5100: 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 63 6f   = 0;.    int co
5110: 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 20 3d 20  ord_dimension = 
5120: 30 3b 0a 20 20 20 20 69 6e 74 20 67 63 5f 73 72  0;.    int gc_sr
5130: 69 64 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  id = 0;.    int 
5140: 67 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61 74 20  geometry_format 
5150: 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 74 79 70  = 0;.    int typ
5160: 65 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 73  e = 0;.    int s
5170: 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 65 6e 61  patial_index_ena
5180: 62 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 63 68  bled = 0;.    ch
5190: 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b 0a 20 20  ar sql[1024];.  
51a0: 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 63    int ret;.    c
51b0: 6f 6e 73 74 20 63 68 61 72 20 2a 6e 61 6d 65 3b  onst char *name;
51c0: 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
51d0: 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 73 3b 0a  char **results;.
51e0: 20 20 20 20 69 6e 74 20 72 6f 77 73 3b 0a 20 20      int rows;.  
51f0: 20 20 69 6e 74 20 63 6f 6c 75 6d 6e 73 3b 0a 2f    int columns;./
5200: 2a 20 63 68 65 63 6b 69 6e 67 20 74 68 65 20 47  * checking the G
5210: 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e 53 20  EOMETRY_COLUMNS 
5220: 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 73 74 72  table */.    str
5230: 63 70 79 20 28 73 71 6c 2c 20 22 50 52 41 47 4d  cpy (sql, "PRAGM
5240: 41 20 74 61 62 6c 65 5f 69 6e 66 6f 28 67 65 6f  A table_info(geo
5250: 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 29 22 29  metry_columns)")
5260: 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
5270: 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 28 73  te3_get_table (s
5280: 71 6c 69 74 65 2c 20 73 71 6c 2c 20 26 72 65 73  qlite, sql, &res
5290: 75 6c 74 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f  ults, &rows, &co
52a0: 6c 75 6d 6e 73 2c 20 4e 55 4c 4c 29 3b 0a 20 20  lumns, NULL);.  
52b0: 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
52c0: 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 75 6e  ITE_OK)..goto un
52d0: 6b 6e 6f 77 6e 3b 0a 20 20 20 20 69 66 20 28 72  known;.    if (r
52e0: 6f 77 73 20 3c 20 31 29 0a 09 3b 0a 20 20 20 20  ows < 1)..;.    
52f0: 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
5300: 66 6f 72 20 28 69 20 3d 20 31 3b 20 69 20 3c 3d  for (i = 1; i <=
5310: 20 72 6f 77 73 3b 20 69 2b 2b 29 0a 09 20 20 20   rows; i++)..   
5320: 20 7b 0a 09 09 6e 61 6d 65 20 3d 20 72 65 73 75   {...name = resu
5330: 6c 74 73 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73  lts[(i * columns
5340: 29 20 2b 20 31 5d 3b 0a 09 09 69 66 20 28 73 74  ) + 1];...if (st
5350: 72 63 61 73 65 63 6d 70 20 28 6e 61 6d 65 2c 20  rcasecmp (name, 
5360: 22 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 22 29 20  "f_table_name") 
5370: 3d 3d 20 30 29 0a 09 09 20 20 20 20 66 5f 74 61  == 0)...    f_ta
5380: 62 6c 65 5f 6e 61 6d 65 20 3d 20 31 3b 0a 09 09  ble_name = 1;...
5390: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28  if (strcasecmp (
53a0: 6e 61 6d 65 2c 20 22 66 5f 67 65 6f 6d 65 74 72  name, "f_geometr
53b0: 79 5f 63 6f 6c 75 6d 6e 22 29 20 3d 3d 20 30 29  y_column") == 0)
53c0: 0a 09 09 20 20 20 20 66 5f 67 65 6f 6d 65 74 72  ...    f_geometr
53d0: 79 5f 63 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 09 09  y_column = 1;...
53e0: 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28  if (strcasecmp (
53f0: 6e 61 6d 65 2c 20 22 67 65 6f 6d 65 74 72 79 5f  name, "geometry_
5400: 74 79 70 65 22 29 20 3d 3d 20 30 29 0a 09 09 20  type") == 0)... 
5410: 20 20 20 67 65 6f 6d 65 74 72 79 5f 74 79 70 65     geometry_type
5420: 20 3d 20 31 3b 0a 09 09 69 66 20 28 73 74 72 63   = 1;...if (strc
5430: 61 73 65 63 6d 70 20 28 6e 61 6d 65 2c 20 22 63  asecmp (name, "c
5440: 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 22 29  oord_dimension")
5450: 20 3d 3d 20 30 29 0a 09 09 20 20 20 20 63 6f 6f   == 0)...    coo
5460: 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 20 3d 20 31  rd_dimension = 1
5470: 3b 0a 09 09 69 66 20 28 73 74 72 63 61 73 65 63  ;...if (strcasec
5480: 6d 70 20 28 6e 61 6d 65 2c 20 22 73 72 69 64 22  mp (name, "srid"
5490: 29 20 3d 3d 20 30 29 0a 09 09 20 20 20 20 67 63  ) == 0)...    gc
54a0: 5f 73 72 69 64 20 3d 20 31 3b 0a 09 09 69 66 20  _srid = 1;...if 
54b0: 28 73 74 72 63 61 73 65 63 6d 70 20 28 6e 61 6d  (strcasecmp (nam
54c0: 65 2c 20 22 67 65 6f 6d 65 74 72 79 5f 66 6f 72  e, "geometry_for
54d0: 6d 61 74 22 29 20 3d 3d 20 30 29 0a 09 09 20 20  mat") == 0)...  
54e0: 20 20 67 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61    geometry_forma
54f0: 74 20 3d 20 31 3b 0a 09 09 69 66 20 28 73 74 72  t = 1;...if (str
5500: 63 61 73 65 63 6d 70 20 28 6e 61 6d 65 2c 20 22  casecmp (name, "
5510: 74 79 70 65 22 29 20 3d 3d 20 30 29 0a 09 09 20  type") == 0)... 
5520: 20 20 20 74 79 70 65 20 3d 20 31 3b 0a 09 09 69     type = 1;...i
5530: 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 6e  f (strcasecmp (n
5540: 61 6d 65 2c 20 22 73 70 61 74 69 61 6c 5f 69 6e  ame, "spatial_in
5550: 64 65 78 5f 65 6e 61 62 6c 65 64 22 29 20 3d 3d  dex_enabled") ==
5560: 20 30 29 0a 09 09 20 20 20 20 73 70 61 74 69 61   0)...    spatia
5570: 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 20  l_index_enabled 
5580: 3d 20 31 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20  = 1;..    }.    
5590: 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
55a0: 66 72 65 65 5f 74 61 62 6c 65 20 28 72 65 73 75  free_table (resu
55b0: 6c 74 73 29 3b 0a 20 20 20 20 69 66 20 28 66 5f  lts);.    if (f_
55c0: 74 61 62 6c 65 5f 6e 61 6d 65 20 26 26 20 66 5f  table_name && f_
55d0: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 20  geometry_column 
55e0: 26 26 20 74 79 70 65 20 26 26 20 63 6f 6f 72 64  && type && coord
55f0: 5f 64 69 6d 65 6e 73 69 6f 6e 20 26 26 20 67 63  _dimension && gc
5600: 5f 73 72 69 64 0a 09 26 26 20 73 70 61 74 69 61  _srid..&& spatia
5610: 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 29  l_index_enabled)
5620: 0a 09 73 70 61 74 69 61 6c 69 74 65 5f 6c 65 67  ..spatialite_leg
5630: 61 63 79 5f 67 63 20 3d 20 31 3b 0a 20 20 20 20  acy_gc = 1;.    
5640: 69 66 20 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  if (f_table_name
5650: 20 26 26 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63   && f_geometry_c
5660: 6f 6c 75 6d 6e 20 26 26 20 67 65 6f 6d 65 74 72  olumn && geometr
5670: 79 5f 74 79 70 65 20 26 26 20 63 6f 6f 72 64 5f  y_type && coord_
5680: 64 69 6d 65 6e 73 69 6f 6e 0a 09 26 26 20 67 63  dimension..&& gc
5690: 5f 73 72 69 64 20 26 26 20 73 70 61 74 69 61 6c  _srid && spatial
56a0: 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 29 0a  _index_enabled).
56b0: 09 73 70 61 74 69 61 6c 69 74 65 5f 67 63 20 3d  .spatialite_gc =
56c0: 20 31 3b 0a 20 20 20 20 69 66 20 28 66 5f 74 61   1;.    if (f_ta
56d0: 62 6c 65 5f 6e 61 6d 65 20 26 26 20 66 5f 67 65  ble_name && f_ge
56e0: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 20 26 26  ometry_column &&
56f0: 20 67 65 6f 6d 65 74 72 79 5f 74 79 70 65 20 26   geometry_type &
5700: 26 20 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f  & coord_dimensio
5710: 6e 0a 09 26 26 20 67 63 5f 73 72 69 64 20 26 26  n..&& gc_srid &&
5720: 20 67 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61 74   geometry_format
5730: 29 0a 09 66 64 6f 5f 67 63 20 3d 20 31 3b 0a 2f  )..fdo_gc = 1;./
5740: 2a 20 63 68 65 63 6b 69 6e 67 20 74 68 65 20 53  * checking the S
5750: 50 41 54 49 41 4c 5f 52 45 46 5f 53 59 53 20 74  PATIAL_REF_SYS t
5760: 61 62 6c 65 20 2a 2f 0a 20 20 20 20 73 74 72 63  able */.    strc
5770: 70 79 20 28 73 71 6c 2c 20 22 50 52 41 47 4d 41  py (sql, "PRAGMA
5780: 20 74 61 62 6c 65 5f 69 6e 66 6f 28 73 70 61 74   table_info(spat
5790: 69 61 6c 5f 72 65 66 5f 73 79 73 29 22 29 3b 0a  ial_ref_sys)");.
57a0: 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65      ret = sqlite
57b0: 33 5f 67 65 74 5f 74 61 62 6c 65 20 28 73 71 6c  3_get_table (sql
57c0: 69 74 65 2c 20 73 71 6c 2c 20 26 72 65 73 75 6c  ite, sql, &resul
57d0: 74 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f 6c 75  ts, &rows, &colu
57e0: 6d 6e 73 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  mns, NULL);.    
57f0: 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
5800: 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 75 6e 6b 6e  E_OK)..goto unkn
5810: 6f 77 6e 3b 0a 20 20 20 20 69 66 20 28 72 6f 77  own;.    if (row
5820: 73 20 3c 20 31 29 0a 09 3b 0a 20 20 20 20 65 6c  s < 1)..;.    el
5830: 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 66 6f  se.      {..  fo
5840: 72 20 28 69 20 3d 20 31 3b 20 69 20 3c 3d 20 72  r (i = 1; i <= r
5850: 6f 77 73 3b 20 69 2b 2b 29 0a 09 20 20 20 20 7b  ows; i++)..    {
5860: 0a 09 09 6e 61 6d 65 20 3d 20 72 65 73 75 6c 74  ...name = result
5870: 73 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 20  s[(i * columns) 
5880: 2b 20 31 5d 3b 0a 09 09 69 66 20 28 73 74 72 63  + 1];...if (strc
5890: 61 73 65 63 6d 70 20 28 6e 61 6d 65 2c 20 22 73  asecmp (name, "s
58a0: 72 69 64 22 29 20 3d 3d 20 30 29 0a 09 09 20 20  rid") == 0)...  
58b0: 20 20 72 73 5f 73 72 69 64 20 3d 20 31 3b 0a 09    rs_srid = 1;..
58c0: 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20  .if (strcasecmp 
58d0: 28 6e 61 6d 65 2c 20 22 61 75 74 68 5f 6e 61 6d  (name, "auth_nam
58e0: 65 22 29 20 3d 3d 20 30 29 0a 09 09 20 20 20 20  e") == 0)...    
58f0: 61 75 74 68 5f 6e 61 6d 65 20 3d 20 31 3b 0a 09  auth_name = 1;..
5900: 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20  .if (strcasecmp 
5910: 28 6e 61 6d 65 2c 20 22 61 75 74 68 5f 73 72 69  (name, "auth_sri
5920: 64 22 29 20 3d 3d 20 30 29 0a 09 09 20 20 20 20  d") == 0)...    
5930: 61 75 74 68 5f 73 72 69 64 20 3d 20 31 3b 0a 09  auth_srid = 1;..
5940: 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20  .if (strcasecmp 
5950: 28 6e 61 6d 65 2c 20 22 73 72 74 65 78 74 22 29  (name, "srtext")
5960: 20 3d 3d 20 30 29 0a 09 09 20 20 20 20 73 72 74   == 0)...    srt
5970: 65 78 74 20 3d 20 31 3b 0a 09 09 69 66 20 28 73  ext = 1;...if (s
5980: 74 72 63 61 73 65 63 6d 70 20 28 6e 61 6d 65 2c  trcasecmp (name,
5990: 20 22 72 65 66 5f 73 79 73 5f 6e 61 6d 65 22 29   "ref_sys_name")
59a0: 20 3d 3d 20 30 29 0a 09 09 20 20 20 20 72 65 66   == 0)...    ref
59b0: 5f 73 79 73 5f 6e 61 6d 65 20 3d 20 31 3b 0a 09  _sys_name = 1;..
59c0: 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20  .if (strcasecmp 
59d0: 28 6e 61 6d 65 2c 20 22 70 72 6f 6a 34 74 65 78  (name, "proj4tex
59e0: 74 22 29 20 3d 3d 20 30 29 0a 09 09 20 20 20 20  t") == 0)...    
59f0: 70 72 6f 6a 34 74 65 78 74 20 3d 20 31 3b 0a 09  proj4text = 1;..
5a00: 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20  .if (strcasecmp 
5a10: 28 6e 61 6d 65 2c 20 22 73 72 74 65 78 74 22 29  (name, "srtext")
5a20: 20 3d 3d 20 30 29 0a 09 09 20 20 20 20 73 72 74   == 0)...    srt
5a30: 65 78 74 20 3d 20 31 3b 0a 09 20 20 20 20 7d 0a  ext = 1;..    }.
5a40: 20 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69        }.    sqli
5a50: 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 28  te3_free_table (
5a60: 72 65 73 75 6c 74 73 29 3b 0a 20 20 20 20 69 66  results);.    if
5a70: 20 28 72 73 5f 73 72 69 64 20 26 26 20 61 75 74   (rs_srid && aut
5a80: 68 5f 6e 61 6d 65 20 26 26 20 61 75 74 68 5f 73  h_name && auth_s
5a90: 72 69 64 20 26 26 20 72 65 66 5f 73 79 73 5f 6e  rid && ref_sys_n
5aa0: 61 6d 65 20 26 26 20 70 72 6f 6a 34 74 65 78 74  ame && proj4text
5ab0: 0a 09 26 26 20 73 72 74 65 78 74 29 0a 09 73 70  ..&& srtext)..sp
5ac0: 61 74 69 61 6c 69 74 65 5f 72 73 20 3d 20 31 3b  atialite_rs = 1;
5ad0: 0a 20 20 20 20 69 66 20 28 72 73 5f 73 72 69 64  .    if (rs_srid
5ae0: 20 26 26 20 61 75 74 68 5f 6e 61 6d 65 20 26 26   && auth_name &&
5af0: 20 61 75 74 68 5f 73 72 69 64 20 26 26 20 72 65   auth_srid && re
5b00: 66 5f 73 79 73 5f 6e 61 6d 65 20 26 26 20 70 72  f_sys_name && pr
5b10: 6f 6a 34 74 65 78 74 29 0a 09 73 70 61 74 69 61  oj4text)..spatia
5b20: 6c 69 74 65 5f 6c 65 67 61 63 79 5f 72 73 20 3d  lite_legacy_rs =
5b30: 20 31 3b 0a 20 20 20 20 69 66 20 28 72 73 5f 73   1;.    if (rs_s
5b40: 72 69 64 20 26 26 20 61 75 74 68 5f 6e 61 6d 65  rid && auth_name
5b50: 20 26 26 20 61 75 74 68 5f 73 72 69 64 20 26 26   && auth_srid &&
5b60: 20 73 72 74 65 78 74 29 0a 09 66 64 6f 5f 72 73   srtext)..fdo_rs
5b70: 20 3d 20 31 3b 0a 2f 2a 20 76 65 72 69 66 79 69   = 1;./* verifyi
5b80: 6e 67 20 74 68 65 20 4d 65 74 61 44 61 74 61 20  ng the MetaData 
5b90: 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 69 66  format */.    if
5ba0: 20 28 73 70 61 74 69 61 6c 69 74 65 5f 6c 65 67   (spatialite_leg
5bb0: 61 63 79 5f 67 63 20 26 26 20 73 70 61 74 69 61  acy_gc && spatia
5bc0: 6c 69 74 65 5f 6c 65 67 61 63 79 5f 72 73 29 0a  lite_legacy_rs).
5bd0: 09 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 69  .return 1;.    i
5be0: 66 20 28 66 64 6f 5f 67 63 20 26 26 20 66 64 6f  f (fdo_gc && fdo
5bf0: 5f 72 73 29 0a 09 72 65 74 75 72 6e 20 32 3b 0a  _rs)..return 2;.
5c00: 20 20 20 20 69 66 20 28 73 70 61 74 69 61 6c 69      if (spatiali
5c10: 74 65 5f 67 63 20 26 26 20 73 70 61 74 69 61 6c  te_gc && spatial
5c20: 69 74 65 5f 72 73 29 0a 09 72 65 74 75 72 6e 20  ite_rs)..return 
5c30: 33 3b 0a 20 20 75 6e 6b 6e 6f 77 6e 3a 0a 20 20  3;.  unknown:.  
5c40: 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73    return 0;.}..s
5c50: 74 61 74 69 63 20 76 6f 69 64 0a 61 64 64 5f 66  tatic void.add_f
5c60: 64 6f 5f 74 61 62 6c 65 20 28 73 74 72 75 63 74  do_table (struct
5c70: 20 66 64 6f 5f 74 61 62 6c 65 20 2a 2a 66 69 72   fdo_table **fir
5c80: 73 74 2c 20 73 74 72 75 63 74 20 66 64 6f 5f 74  st, struct fdo_t
5c90: 61 62 6c 65 20 2a 2a 6c 61 73 74 2c 0a 09 20 20  able **last,..  
5ca0: 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
5cb0: 2a 74 61 62 6c 65 2c 20 69 6e 74 20 6c 65 6e 29  *table, int len)
5cc0: 0a 7b 0a 2f 2a 20 61 64 64 73 20 61 6e 20 46 44  .{./* adds an FD
5cd0: 4f 2d 4f 47 52 20 73 74 79 6c 65 64 20 47 65 6f  O-OGR styled Geo
5ce0: 6d 65 74 72 79 20 54 61 62 6c 65 20 74 6f 20 63  metry Table to c
5cf0: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 69 6e  orresponding lin
5d00: 6b 65 64 20 6c 69 73 74 20 2a 2f 0a 20 20 20 20  ked list */.    
5d10: 73 74 72 75 63 74 20 66 64 6f 5f 74 61 62 6c 65  struct fdo_table
5d20: 20 2a 70 20 3d 20 6d 61 6c 6c 6f 63 20 28 73 69   *p = malloc (si
5d30: 7a 65 6f 66 20 28 73 74 72 75 63 74 20 66 64 6f  zeof (struct fdo
5d40: 5f 74 61 62 6c 65 29 29 3b 0a 20 20 20 20 70 2d  _table));.    p-
5d50: 3e 74 61 62 6c 65 20 3d 20 6d 61 6c 6c 6f 63 20  >table = malloc 
5d60: 28 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 20 20 73  (len + 1);.    s
5d70: 74 72 63 70 79 20 28 70 2d 3e 74 61 62 6c 65 2c  trcpy (p->table,
5d80: 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 70 2d 3e   table);.    p->
5d90: 6e 65 78 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  next = NULL;.   
5da0: 20 69 66 20 28 21 28 2a 66 69 72 73 74 29 29 0a   if (!(*first)).
5db0: 09 28 2a 66 69 72 73 74 29 20 3d 20 70 3b 0a 20  .(*first) = p;. 
5dc0: 20 20 20 69 66 20 28 28 2a 6c 61 73 74 29 29 0a     if ((*last)).
5dd0: 09 28 2a 6c 61 73 74 29 2d 3e 6e 65 78 74 20 3d  .(*last)->next =
5de0: 20 70 3b 0a 20 20 20 20 28 2a 6c 61 73 74 29 20   p;.    (*last) 
5df0: 3d 20 70 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  = p;.}..static v
5e00: 6f 69 64 0a 66 72 65 65 5f 66 64 6f 5f 74 61 62  oid.free_fdo_tab
5e10: 6c 65 73 20 28 73 74 72 75 63 74 20 66 64 6f 5f  les (struct fdo_
5e20: 74 61 62 6c 65 20 2a 66 69 72 73 74 29 0a 7b 0a  table *first).{.
5e30: 2f 2a 20 6d 65 6d 6f 72 79 20 63 6c 65 61 6e 75  /* memory cleanu
5e40: 70 3b 20 64 65 73 74 72 6f 79 69 6e 67 20 74 68  p; destroying th
5e50: 65 20 46 44 4f 2d 4f 47 52 20 74 61 62 6c 65 73  e FDO-OGR tables
5e60: 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 2a 2f 0a   linked list */.
5e70: 20 20 20 20 73 74 72 75 63 74 20 66 64 6f 5f 74      struct fdo_t
5e80: 61 62 6c 65 20 2a 70 3b 0a 20 20 20 20 73 74 72  able *p;.    str
5e90: 75 63 74 20 66 64 6f 5f 74 61 62 6c 65 20 2a 70  uct fdo_table *p
5ea0: 6e 3b 0a 20 20 20 20 70 20 3d 20 66 69 72 73 74  n;.    p = first
5eb0: 3b 0a 20 20 20 20 77 68 69 6c 65 20 28 70 29 0a  ;.    while (p).
5ec0: 20 20 20 20 20 20 7b 0a 09 20 20 70 6e 20 3d 20        {..  pn = 
5ed0: 70 2d 3e 6e 65 78 74 3b 0a 09 20 20 69 66 20 28  p->next;..  if (
5ee0: 70 2d 3e 74 61 62 6c 65 29 0a 09 20 20 20 20 20  p->table)..     
5ef0: 20 66 72 65 65 20 28 70 2d 3e 74 61 62 6c 65 29   free (p->table)
5f00: 3b 0a 09 20 20 66 72 65 65 20 28 70 29 3b 0a 09  ;..  free (p);..
5f10: 20 20 70 20 3d 20 70 6e 3b 0a 20 20 20 20 20 20    p = pn;.      
5f20: 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  }.}..static void
5f30: 0a 66 6e 63 74 5f 41 75 74 6f 46 44 4f 53 74 61  .fnct_AutoFDOSta
5f40: 72 74 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  rt (sqlite3_cont
5f50: 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
5f60: 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
5f70: 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
5f80: 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
5f90: 6e 3a 0a 2f 20 41 75 74 6f 46 44 4f 53 74 61 72  n:./ AutoFDOStar
5fa0: 74 28 76 6f 69 64 29 0a 2f 0a 2f 20 66 6f 72 20  t(void)././ for 
5fb0: 46 44 4f 2d 4f 47 52 20 69 6e 74 65 72 6f 70 65  FDO-OGR interope
5fc0: 72 61 62 69 6c 69 74 79 3a 0a 2f 20 74 65 73 74  rability:./ test
5fd0: 73 20 74 68 65 20 53 70 61 74 69 61 6c 4d 65 74  s the SpatialMet
5fe0: 61 64 61 74 61 20 74 79 70 65 2c 20 74 68 65 6e  adata type, then
5ff0: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2f   automatically./
6000: 20 63 72 65 61 74 69 6e 67 20 61 20 56 69 72 74   creating a Virt
6010: 75 61 6c 46 44 4f 20 74 61 62 6c 65 20 66 6f 72  ualFDO table for
6020: 20 65 61 63 68 20 46 44 4f 2d 4f 47 52 20 6d 61   each FDO-OGR ma
6030: 69 6e 20 74 61 62 6c 65 20 0a 2f 20 64 65 63 6c  in table ./ decl
6040: 61 72 65 64 20 77 69 74 68 69 6e 20 46 44 4f 2d  ared within FDO-
6050: 73 74 79 6c 65 64 20 53 70 61 74 69 61 6c 4d 65  styled SpatialMe
6060: 74 61 64 61 74 61 0a 2f 0a 2a 2f 0a 20 20 20 20  tadata./.*/.    
6070: 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 6f 6e  int ret;.    con
6080: 73 74 20 63 68 61 72 20 2a 6e 61 6d 65 3b 0a 20  st char *name;. 
6090: 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68     int i;.    ch
60a0: 61 72 20 2a 2a 72 65 73 75 6c 74 73 3b 0a 20 20  ar **results;.  
60b0: 20 20 69 6e 74 20 72 6f 77 73 3b 0a 20 20 20 20    int rows;.    
60c0: 69 6e 74 20 63 6f 6c 75 6d 6e 73 3b 0a 20 20 20  int columns;.   
60d0: 20 63 68 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b   char sql[1024];
60e0: 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 20 3d  .    int count =
60f0: 20 30 3b 0a 20 20 20 20 73 74 72 75 63 74 20 66   0;.    struct f
6100: 64 6f 5f 74 61 62 6c 65 20 2a 66 69 72 73 74 20  do_table *first 
6110: 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 73 74 72 75  = NULL;.    stru
6120: 63 74 20 66 64 6f 5f 74 61 62 6c 65 20 2a 6c 61  ct fdo_table *la
6130: 73 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 73  st = NULL;.    s
6140: 74 72 75 63 74 20 66 64 6f 5f 74 61 62 6c 65 20  truct fdo_table 
6150: 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  *p;.    int len;
6160: 0a 20 20 20 20 63 68 61 72 20 78 6e 61 6d 65 5b  .    char xname[
6170: 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
6180: 78 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20  xtable[1024];.  
6190: 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74    sqlite3 *sqlit
61a0: 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  e = sqlite3_cont
61b0: 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28 63  ext_db_handle (c
61c0: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41 49  ontext);.    GAI
61d0: 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
61e0: 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
61f0: 2a 2f 0a 20 20 20 20 69 66 20 28 63 68 65 63 6b  */.    if (check
6200: 53 70 61 74 69 61 6c 4d 65 74 61 44 61 74 61 20  SpatialMetaData 
6210: 28 73 71 6c 69 74 65 29 20 3d 3d 20 32 29 0a 20  (sqlite) == 2). 
6220: 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 6f 6b 2c       {..  /* ok,
6230: 20 63 72 65 61 74 69 6e 67 20 56 69 72 74 75 61   creating Virtua
6240: 6c 46 44 4f 20 74 61 62 6c 65 73 20 2a 2f 0a 09  lFDO tables */..
6250: 20 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22    strcpy (sql, "
6260: 53 45 4c 45 43 54 20 44 49 53 54 49 4e 43 54 20  SELECT DISTINCT 
6270: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 46 52 4f  f_table_name FRO
6280: 4d 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  M geometry_colum
6290: 6e 73 22 29 3b 0a 09 20 20 72 65 74 20 3d 0a 09  ns");..  ret =..
62a0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 67 65        sqlite3_ge
62b0: 74 5f 74 61 62 6c 65 20 28 73 71 6c 69 74 65 2c  t_table (sqlite,
62c0: 20 73 71 6c 2c 20 26 72 65 73 75 6c 74 73 2c 20   sql, &results, 
62d0: 26 72 6f 77 73 2c 20 26 63 6f 6c 75 6d 6e 73 2c  &rows, &columns,
62e0: 20 4e 55 4c 4c 29 3b 0a 09 20 20 69 66 20 28 72   NULL);..  if (r
62f0: 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29  et != SQLITE_OK)
6300: 0a 09 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72  ..      goto err
6310: 6f 72 3b 0a 09 20 20 69 66 20 28 72 6f 77 73 20  or;..  if (rows 
6320: 3c 20 31 29 0a 09 20 20 20 20 20 20 3b 0a 09 20  < 1)..      ;.. 
6330: 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 66   else..    {...f
6340: 6f 72 20 28 69 20 3d 20 31 3b 20 69 20 3c 3d 20  or (i = 1; i <= 
6350: 72 6f 77 73 3b 20 69 2b 2b 29 0a 09 09 20 20 7b  rows; i++)...  {
6360: 0a 09 09 20 20 20 20 20 20 6e 61 6d 65 20 3d 20  ...      name = 
6370: 72 65 73 75 6c 74 73 5b 28 69 20 2a 20 63 6f 6c  results[(i * col
6380: 75 6d 6e 73 29 20 2b 20 30 5d 3b 0a 09 09 20 20  umns) + 0];...  
6390: 20 20 20 20 69 66 20 28 6e 61 6d 65 29 0a 09 09      if (name)...
63a0: 09 7b 0a 09 09 09 20 20 20 20 6c 65 6e 20 3d 20  .{....    len = 
63b0: 73 74 72 6c 65 6e 20 28 6e 61 6d 65 29 3b 0a 09  strlen (name);..
63c0: 09 09 20 20 20 20 61 64 64 5f 66 64 6f 5f 74 61  ..    add_fdo_ta
63d0: 62 6c 65 20 28 26 66 69 72 73 74 2c 20 26 6c 61  ble (&first, &la
63e0: 73 74 2c 20 6e 61 6d 65 2c 20 6c 65 6e 29 3b 0a  st, name, len);.
63f0: 09 09 09 7d 0a 09 09 20 20 7d 0a 09 20 20 20 20  ...}...  }..    
6400: 7d 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65  }..  sqlite3_fre
6410: 65 5f 74 61 62 6c 65 20 28 72 65 73 75 6c 74 73  e_table (results
6420: 29 3b 0a 09 20 20 70 20 3d 20 66 69 72 73 74 3b  );..  p = first;
6430: 0a 09 20 20 77 68 69 6c 65 20 28 70 29 0a 09 20  ..  while (p).. 
6440: 20 20 20 7b 0a 09 09 2f 2a 20 64 65 73 74 72 6f     {.../* destro
6450: 79 69 6e 67 20 74 68 65 20 56 69 72 74 75 61 6c  ying the Virtual
6460: 46 44 4f 20 74 61 62 6c 65 20 5b 69 66 20 65 78  FDO table [if ex
6470: 69 73 74 69 6e 67 5d 20 2a 2f 0a 09 09 73 70 72  isting] */...spr
6480: 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 66 64  intf (xname, "fd
6490: 6f 5f 25 73 22 2c 20 70 2d 3e 74 61 62 6c 65 29  o_%s", p->table)
64a0: 3b 0a 09 09 64 6f 75 62 6c 65 5f 71 75 6f 74 65  ;...double_quote
64b0: 64 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 09  d_sql (xname);..
64c0: 09 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22  .sprintf (sql, "
64d0: 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58  DROP TABLE IF EX
64e0: 49 53 54 53 20 25 73 22 2c 20 78 6e 61 6d 65 29  ISTS %s", xname)
64f0: 3b 0a 09 09 72 65 74 20 3d 20 73 71 6c 69 74 65  ;...ret = sqlite
6500: 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20  3_exec (sqlite, 
6510: 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c  sql, NULL, NULL,
6520: 20 4e 55 4c 4c 29 3b 0a 09 09 69 66 20 28 72 65   NULL);...if (re
6530: 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
6540: 09 09 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ..    goto error
6550: 3b 0a 09 09 2f 2a 20 63 72 65 61 74 69 6e 67 20  ;.../* creating 
6560: 74 68 65 20 56 69 72 74 75 61 6c 46 44 4f 20 74  the VirtualFDO t
6570: 61 62 6c 65 20 2a 2f 0a 09 09 73 74 72 63 70 79  able */...strcpy
6580: 20 28 78 74 61 62 6c 65 2c 20 70 2d 3e 74 61 62   (xtable, p->tab
6590: 6c 65 29 3b 0a 09 09 64 6f 75 62 6c 65 5f 71 75  le);...double_qu
65a0: 6f 74 65 64 5f 73 71 6c 20 28 78 74 61 62 6c 65  oted_sql (xtable
65b0: 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 73 71  );...sprintf (sq
65c0: 6c 2c 20 22 43 52 45 41 54 45 20 56 49 52 54 55  l, "CREATE VIRTU
65d0: 41 4c 20 54 41 42 4c 45 20 25 73 20 55 53 49 4e  AL TABLE %s USIN
65e0: 47 20 56 69 72 74 75 61 6c 46 44 4f 28 25 73 29  G VirtualFDO(%s)
65f0: 22 2c 0a 09 09 09 20 78 6e 61 6d 65 2c 20 78 74  ",.... xname, xt
6600: 61 62 6c 65 29 3b 0a 09 09 72 65 74 20 3d 20 73  able);...ret = s
6610: 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
6620: 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
6630: 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09 09 69  NULL, NULL);...i
6640: 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
6650: 5f 4f 4b 29 0a 09 09 20 20 20 20 67 6f 74 6f 20  _OK)...    goto 
6660: 65 72 72 6f 72 3b 0a 09 09 63 6f 75 6e 74 2b 2b  error;...count++
6670: 3b 0a 09 09 70 20 3d 20 70 2d 3e 6e 65 78 74 3b  ;...p = p->next;
6680: 0a 09 20 20 20 20 7d 0a 09 65 72 72 6f 72 3a 0a  ..    }..error:.
6690: 09 20 20 66 72 65 65 5f 66 64 6f 5f 74 61 62 6c  .  free_fdo_tabl
66a0: 65 73 20 28 66 69 72 73 74 29 3b 0a 09 20 20 73  es (first);..  s
66b0: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
66c0: 74 20 28 63 6f 6e 74 65 78 74 2c 20 63 6f 75 6e  t (context, coun
66d0: 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
66e0: 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74       }.    sqlit
66f0: 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
6700: 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20  ontext, 0);.    
6710: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69  return;.}..stati
6720: 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41 75 74 6f  c void.fnct_Auto
6730: 46 44 4f 53 74 6f 70 20 28 73 71 6c 69 74 65 33  FDOStop (sqlite3
6740: 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
6750: 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
6760: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
6770: 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
6780: 6e 63 74 69 6f 6e 3a 0a 2f 20 41 75 74 6f 46 44  nction:./ AutoFD
6790: 4f 53 74 6f 70 28 76 6f 69 64 29 0a 2f 0a 2f 20  OStop(void)././ 
67a0: 66 6f 72 20 46 44 4f 2d 4f 47 52 20 69 6e 74 65  for FDO-OGR inte
67b0: 72 6f 70 65 72 61 62 69 6c 69 74 79 3a 0a 2f 20  roperability:./ 
67c0: 74 65 73 74 73 20 74 68 65 20 53 70 61 74 69 61  tests the Spatia
67d0: 6c 4d 65 74 61 64 61 74 61 20 74 79 70 65 2c 20  lMetadata type, 
67e0: 74 68 65 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c  then automatical
67f0: 6c 79 0a 2f 20 72 65 6d 6f 76 65 73 20 61 6e 79  ly./ removes any
6800: 20 56 69 72 74 75 61 6c 46 44 4f 20 74 61 62 6c   VirtualFDO tabl
6810: 65 20 0a 2f 0a 2a 2f 0a 20 20 20 20 69 6e 74 20  e ./.*/.    int 
6820: 72 65 74 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ret;.    const c
6830: 68 61 72 20 2a 6e 61 6d 65 3b 0a 20 20 20 20 69  har *name;.    i
6840: 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 2a  nt i;.    char *
6850: 2a 72 65 73 75 6c 74 73 3b 0a 20 20 20 20 69 6e  *results;.    in
6860: 74 20 72 6f 77 73 3b 0a 20 20 20 20 69 6e 74 20  t rows;.    int 
6870: 63 6f 6c 75 6d 6e 73 3b 0a 20 20 20 20 63 68 61  columns;.    cha
6880: 72 20 73 71 6c 5b 31 30 32 34 5d 3b 0a 20 20 20  r sql[1024];.   
6890: 20 69 6e 74 20 63 6f 75 6e 74 20 3d 20 30 3b 0a   int count = 0;.
68a0: 20 20 20 20 73 74 72 75 63 74 20 66 64 6f 5f 74      struct fdo_t
68b0: 61 62 6c 65 20 2a 66 69 72 73 74 20 3d 20 4e 55  able *first = NU
68c0: 4c 4c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 66  LL;.    struct f
68d0: 64 6f 5f 74 61 62 6c 65 20 2a 6c 61 73 74 20 3d  do_table *last =
68e0: 20 4e 55 4c 4c 3b 0a 20 20 20 20 73 74 72 75 63   NULL;.    struc
68f0: 74 20 66 64 6f 5f 74 61 62 6c 65 20 2a 70 3b 0a  t fdo_table *p;.
6900: 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
6910: 20 63 68 61 72 20 78 6e 61 6d 65 5b 31 30 32 34   char xname[1024
6920: 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ];.    sqlite3 *
6930: 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65 33  sqlite = sqlite3
6940: 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
6950: 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  le (context);.  
6960: 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
6970: 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
6980: 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
6990: 63 68 65 63 6b 53 70 61 74 69 61 6c 4d 65 74 61  checkSpatialMeta
69a0: 44 61 74 61 20 28 73 71 6c 69 74 65 29 20 3d 3d  Data (sqlite) ==
69b0: 20 32 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f   2).      {..  /
69c0: 2a 20 6f 6b 2c 20 63 72 65 61 74 69 6e 67 20 56  * ok, creating V
69d0: 69 72 74 75 61 6c 46 44 4f 20 74 61 62 6c 65 73  irtualFDO tables
69e0: 20 2a 2f 0a 09 20 20 73 74 72 63 70 79 20 28 73   */..  strcpy (s
69f0: 71 6c 2c 20 22 53 45 4c 45 43 54 20 44 49 53 54  ql, "SELECT DIST
6a00: 49 4e 43 54 20 66 5f 74 61 62 6c 65 5f 6e 61 6d  INCT f_table_nam
6a10: 65 20 46 52 4f 4d 20 67 65 6f 6d 65 74 72 79 5f  e FROM geometry_
6a20: 63 6f 6c 75 6d 6e 73 22 29 3b 0a 09 20 20 72 65  columns");..  re
6a30: 74 20 3d 0a 09 20 20 20 20 20 20 73 71 6c 69 74  t =..      sqlit
6a40: 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 28 73 71  e3_get_table (sq
6a50: 6c 69 74 65 2c 20 73 71 6c 2c 20 26 72 65 73 75  lite, sql, &resu
6a60: 6c 74 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f 6c  lts, &rows, &col
6a70: 75 6d 6e 73 2c 20 4e 55 4c 4c 29 3b 0a 09 20 20  umns, NULL);..  
6a80: 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
6a90: 45 5f 4f 4b 29 0a 09 20 20 20 20 20 20 67 6f 74  E_OK)..      got
6aa0: 6f 20 65 72 72 6f 72 3b 0a 09 20 20 69 66 20 28  o error;..  if (
6ab0: 72 6f 77 73 20 3c 20 31 29 0a 09 20 20 20 20 20  rows < 1)..     
6ac0: 20 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20   ;..  else..    
6ad0: 7b 0a 09 09 66 6f 72 20 28 69 20 3d 20 31 3b 20  {...for (i = 1; 
6ae0: 69 20 3c 3d 20 72 6f 77 73 3b 20 69 2b 2b 29 0a  i <= rows; i++).
6af0: 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 6e 61  ..  {...      na
6b00: 6d 65 20 3d 20 72 65 73 75 6c 74 73 5b 28 69 20  me = results[(i 
6b10: 2a 20 63 6f 6c 75 6d 6e 73 29 20 2b 20 30 5d 3b  * columns) + 0];
6b20: 0a 09 09 20 20 20 20 20 20 69 66 20 28 6e 61 6d  ...      if (nam
6b30: 65 29 0a 09 09 09 7b 0a 09 09 09 20 20 20 20 6c  e)....{....    l
6b40: 65 6e 20 3d 20 73 74 72 6c 65 6e 20 28 6e 61 6d  en = strlen (nam
6b50: 65 29 3b 0a 09 09 09 20 20 20 20 61 64 64 5f 66  e);....    add_f
6b60: 64 6f 5f 74 61 62 6c 65 20 28 26 66 69 72 73 74  do_table (&first
6b70: 2c 20 26 6c 61 73 74 2c 20 6e 61 6d 65 2c 20 6c  , &last, name, l
6b80: 65 6e 29 3b 0a 09 09 09 7d 0a 09 09 20 20 7d 0a  en);....}...  }.
6b90: 09 20 20 20 20 7d 0a 09 20 20 73 71 6c 69 74 65  .    }..  sqlite
6ba0: 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 28 72 65  3_free_table (re
6bb0: 73 75 6c 74 73 29 3b 0a 09 20 20 70 20 3d 20 66  sults);..  p = f
6bc0: 69 72 73 74 3b 0a 09 20 20 77 68 69 6c 65 20 28  irst;..  while (
6bd0: 70 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 64  p)..    {.../* d
6be0: 65 73 74 72 6f 79 69 6e 67 20 74 68 65 20 56 69  estroying the Vi
6bf0: 72 74 75 61 6c 46 44 4f 20 74 61 62 6c 65 20 5b  rtualFDO table [
6c00: 69 66 20 65 78 69 73 74 69 6e 67 5d 20 2a 2f 0a  if existing] */.
6c10: 09 09 73 70 72 69 6e 74 66 20 28 78 6e 61 6d 65  ..sprintf (xname
6c20: 2c 20 22 66 64 6f 5f 25 73 22 2c 20 70 2d 3e 74  , "fdo_%s", p->t
6c30: 61 62 6c 65 29 3b 0a 09 09 64 6f 75 62 6c 65 5f  able);...double_
6c40: 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d  quoted_sql (xnam
6c50: 65 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 73  e);...sprintf (s
6c60: 71 6c 2c 20 22 44 52 4f 50 20 54 41 42 4c 45 20  ql, "DROP TABLE 
6c70: 49 46 20 45 58 49 53 54 53 20 25 73 22 2c 20 78  IF EXISTS %s", x
6c80: 6e 61 6d 65 29 3b 0a 09 09 72 65 74 20 3d 20 73  name);...ret = s
6c90: 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
6ca0: 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
6cb0: 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 09 09 69  NULL, NULL);...i
6cc0: 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
6cd0: 5f 4f 4b 29 0a 09 09 20 20 20 20 67 6f 74 6f 20  _OK)...    goto 
6ce0: 65 72 72 6f 72 3b 0a 09 09 63 6f 75 6e 74 2b 2b  error;...count++
6cf0: 3b 0a 09 09 70 20 3d 20 70 2d 3e 6e 65 78 74 3b  ;...p = p->next;
6d00: 0a 09 20 20 20 20 7d 0a 09 65 72 72 6f 72 3a 0a  ..    }..error:.
6d10: 09 20 20 66 72 65 65 5f 66 64 6f 5f 74 61 62 6c  .  free_fdo_tabl
6d20: 65 73 20 28 66 69 72 73 74 29 3b 0a 09 20 20 73  es (first);..  s
6d30: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
6d40: 74 20 28 63 6f 6e 74 65 78 74 2c 20 63 6f 75 6e  t (context, coun
6d50: 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
6d60: 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74       }.    sqlit
6d70: 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
6d80: 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20  ontext, 0);.    
6d90: 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69  return;.}..stati
6da0: 63 20 69 6e 74 0a 74 65 73 74 53 70 61 74 69 61  c int.testSpatia
6db0: 4c 69 74 65 48 69 73 74 6f 72 79 20 28 73 71 6c  LiteHistory (sql
6dc0: 69 74 65 33 20 2a 20 73 71 6c 69 74 65 29 0a 7b  ite3 * sqlite).{
6dd0: 0a 2f 2a 20 69 6e 74 65 72 6e 61 6c 20 75 74 69  ./* internal uti
6de0: 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f  lity function:./
6df0: 0a 2f 20 63 68 65 63 6b 73 20 69 66 20 74 68 65  ./ checks if the
6e00: 20 53 50 41 54 49 41 4c 49 54 45 5f 48 49 53 54   SPATIALITE_HIST
6e10: 4f 52 59 20 74 61 62 6c 65 20 61 6c 72 65 61 64  ORY table alread
6e20: 79 20 65 78 69 73 74 73 0a 2f 0a 2a 2f 0a 20 20  y exists./.*/.  
6e30: 20 20 69 6e 74 20 65 76 65 6e 74 5f 69 64 20 3d    int event_id =
6e40: 20 30 3b 0a 20 20 20 20 69 6e 74 20 74 61 62 6c   0;.    int tabl
6e50: 65 5f 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  e_name = 0;.    
6e60: 69 6e 74 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  int geometry_col
6e70: 75 6d 6e 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  umn = 0;.    int
6e80: 20 65 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20   event = 0;.    
6e90: 69 6e 74 20 74 69 6d 65 73 74 61 6d 70 20 3d 20  int timestamp = 
6ea0: 30 3b 0a 20 20 20 20 69 6e 74 20 76 65 72 5f 73  0;.    int ver_s
6eb0: 71 6c 69 74 65 20 3d 20 30 3b 0a 20 20 20 20 69  qlite = 0;.    i
6ec0: 6e 74 20 76 65 72 5f 73 70 6c 69 74 65 20 3d 20  nt ver_splite = 
6ed0: 30 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 5b  0;.    char sql[
6ee0: 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72  1024];.    int r
6ef0: 65 74 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  et;.    const ch
6f00: 61 72 20 2a 6e 61 6d 65 3b 0a 20 20 20 20 69 6e  ar *name;.    in
6f10: 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 2a 2a  t i;.    char **
6f20: 72 65 73 75 6c 74 73 3b 0a 20 20 20 20 69 6e 74  results;.    int
6f30: 20 72 6f 77 73 3b 0a 20 20 20 20 69 6e 74 20 63   rows;.    int c
6f40: 6f 6c 75 6d 6e 73 3b 0a 2f 2a 20 63 68 65 63 6b  olumns;./* check
6f50: 69 6e 67 20 74 68 65 20 53 50 41 54 49 41 4c 49  ing the SPATIALI
6f60: 54 45 5f 48 49 53 54 4f 52 59 20 74 61 62 6c 65  TE_HISTORY table
6f70: 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20 28   */.    strcpy (
6f80: 73 71 6c 2c 20 22 50 52 41 47 4d 41 20 74 61 62  sql, "PRAGMA tab
6f90: 6c 65 5f 69 6e 66 6f 28 73 70 61 74 69 61 6c 69  le_info(spatiali
6fa0: 74 65 5f 68 69 73 74 6f 72 79 29 22 29 3b 0a 20  te_history)");. 
6fb0: 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33     ret = sqlite3
6fc0: 5f 67 65 74 5f 74 61 62 6c 65 20 28 73 71 6c 69  _get_table (sqli
6fd0: 74 65 2c 20 73 71 6c 2c 20 26 72 65 73 75 6c 74  te, sql, &result
6fe0: 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f 6c 75 6d  s, &rows, &colum
6ff0: 6e 73 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 69  ns, NULL);.    i
7000: 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
7010: 5f 4f 4b 29 0a 09 72 65 74 75 72 6e 20 30 3b 0a  _OK)..return 0;.
7020: 20 20 20 20 69 66 20 28 72 6f 77 73 20 3c 20 31      if (rows < 1
7030: 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  )..;.    else.  
7040: 20 20 20 20 7b 0a 09 20 20 66 6f 72 20 28 69 20      {..  for (i 
7050: 3d 20 31 3b 20 69 20 3c 3d 20 72 6f 77 73 3b 20  = 1; i <= rows; 
7060: 69 2b 2b 29 0a 09 20 20 20 20 7b 0a 09 09 6e 61  i++)..    {...na
7070: 6d 65 20 3d 20 72 65 73 75 6c 74 73 5b 28 69 20  me = results[(i 
7080: 2a 20 63 6f 6c 75 6d 6e 73 29 20 2b 20 31 5d 3b  * columns) + 1];
7090: 0a 09 09 69 66 20 28 73 74 72 63 61 73 65 63 6d  ...if (strcasecm
70a0: 70 20 28 6e 61 6d 65 2c 20 22 65 76 65 6e 74 5f  p (name, "event_
70b0: 69 64 22 29 20 3d 3d 20 30 29 0a 09 09 20 20 20  id") == 0)...   
70c0: 20 65 76 65 6e 74 5f 69 64 20 3d 20 31 3b 0a 09   event_id = 1;..
70d0: 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20  .if (strcasecmp 
70e0: 28 6e 61 6d 65 2c 20 22 74 61 62 6c 65 5f 6e 61  (name, "table_na
70f0: 6d 65 22 29 20 3d 3d 20 30 29 0a 09 09 20 20 20  me") == 0)...   
7100: 20 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 31 3b   table_name = 1;
7110: 0a 09 09 69 66 20 28 73 74 72 63 61 73 65 63 6d  ...if (strcasecm
7120: 70 20 28 6e 61 6d 65 2c 20 22 67 65 6f 6d 65 74  p (name, "geomet
7130: 72 79 5f 63 6f 6c 75 6d 6e 22 29 20 3d 3d 20 30  ry_column") == 0
7140: 29 0a 09 09 20 20 20 20 67 65 6f 6d 65 74 72 79  )...    geometry
7150: 5f 63 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 09 09 69  _column = 1;...i
7160: 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 6e  f (strcasecmp (n
7170: 61 6d 65 2c 20 22 65 76 65 6e 74 22 29 20 3d 3d  ame, "event") ==
7180: 20 30 29 0a 09 09 20 20 20 20 65 76 65 6e 74 20   0)...    event 
7190: 3d 20 31 3b 0a 09 09 69 66 20 28 73 74 72 63 61  = 1;...if (strca
71a0: 73 65 63 6d 70 20 28 6e 61 6d 65 2c 20 22 74 69  secmp (name, "ti
71b0: 6d 65 73 74 61 6d 70 22 29 20 3d 3d 20 30 29 0a  mestamp") == 0).
71c0: 09 09 20 20 20 20 74 69 6d 65 73 74 61 6d 70 20  ..    timestamp 
71d0: 3d 20 31 3b 0a 09 09 69 66 20 28 73 74 72 63 61  = 1;...if (strca
71e0: 73 65 63 6d 70 20 28 6e 61 6d 65 2c 20 22 76 65  secmp (name, "ve
71f0: 72 5f 73 71 6c 69 74 65 22 29 20 3d 3d 20 30 29  r_sqlite") == 0)
7200: 0a 09 09 20 20 20 20 76 65 72 5f 73 71 6c 69 74  ...    ver_sqlit
7210: 65 20 3d 20 31 3b 0a 09 09 69 66 20 28 73 74 72  e = 1;...if (str
7220: 63 61 73 65 63 6d 70 20 28 6e 61 6d 65 2c 20 22  casecmp (name, "
7230: 76 65 72 5f 73 70 6c 69 74 65 22 29 20 3d 3d 20  ver_splite") == 
7240: 30 29 0a 09 09 20 20 20 20 76 65 72 5f 73 70 6c  0)...    ver_spl
7250: 69 74 65 20 3d 20 31 3b 0a 09 20 20 20 20 7d 0a  ite = 1;..    }.
7260: 20 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69        }.    sqli
7270: 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 28  te3_free_table (
7280: 72 65 73 75 6c 74 73 29 3b 0a 20 20 20 20 69 66  results);.    if
7290: 20 28 65 76 65 6e 74 5f 69 64 20 26 26 20 74 61   (event_id && ta
72a0: 62 6c 65 5f 6e 61 6d 65 20 26 26 20 67 65 6f 6d  ble_name && geom
72b0: 65 74 72 79 5f 63 6f 6c 75 6d 6e 20 26 26 20 65  etry_column && e
72c0: 76 65 6e 74 20 26 26 20 74 69 6d 65 73 74 61 6d  vent && timestam
72d0: 70 0a 09 26 26 20 76 65 72 5f 73 71 6c 69 74 65  p..&& ver_sqlite
72e0: 20 26 26 20 76 65 72 5f 73 70 6c 69 74 65 29 0a   && ver_splite).
72f0: 09 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 72  .return 1;.    r
7300: 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61 74  eturn 0;.}..stat
7310: 69 63 20 69 6e 74 0a 63 68 65 63 6b 53 70 61 74  ic int.checkSpat
7320: 69 61 4c 69 74 65 48 69 73 74 6f 72 79 20 28 73  iaLiteHistory (s
7330: 71 6c 69 74 65 33 20 2a 20 73 71 6c 69 74 65 29  qlite3 * sqlite)
7340: 0a 7b 0a 2f 2a 20 69 6e 74 65 72 6e 61 6c 20 75  .{./* internal u
7350: 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 3a  tility function:
7360: 0a 2f 0a 2f 20 63 68 65 63 6b 73 20 69 66 20 74  ././ checks if t
7370: 68 65 20 53 50 41 54 49 41 4c 49 54 45 5f 48 49  he SPATIALITE_HI
7380: 53 54 4f 52 59 20 74 61 62 6c 65 20 61 6c 72 65  STORY table alre
7390: 61 64 79 20 65 78 69 73 74 73 0a 2f 20 69 66 20  ady exists./ if 
73a0: 6e 6f 74 2c 20 73 75 63 68 20 74 61 62 6c 65 20  not, such table 
73b0: 77 69 6c 6c 20 74 68 65 6e 20 62 65 20 63 72 65  will then be cre
73c0: 61 74 65 64 0a 2f 0a 2a 2f 0a 20 20 20 20 63 68  ated./.*/.    ch
73d0: 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b 0a 20 20  ar sql[1024];.  
73e0: 20 20 63 68 61 72 20 2a 65 72 72 4d 73 67 20 3d    char *errMsg =
73f0: 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72   NULL;.    int r
7400: 65 74 3b 0a 0a 20 20 20 20 69 66 20 28 74 65 73  et;..    if (tes
7410: 74 53 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f  tSpatiaLiteHisto
7420: 72 79 20 28 73 71 6c 69 74 65 29 29 0a 09 72 65  ry (sqlite))..re
7430: 74 75 72 6e 20 31 3b 0a 0a 2f 2a 20 63 72 65 61  turn 1;../* crea
7440: 74 69 6e 67 20 74 68 65 20 53 50 41 54 49 41 4c  ting the SPATIAL
7450: 49 54 45 5f 48 49 53 54 4f 52 59 20 74 61 62 6c  ITE_HISTORY tabl
7460: 65 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20  e */.    strcpy 
7470: 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54 41  (sql, "CREATE TA
7480: 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 53 54  BLE IF NOT EXIST
7490: 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  S ");.    strcat
74a0: 20 28 73 71 6c 2c 20 22 73 70 61 74 69 61 6c 69   (sql, "spatiali
74b0: 74 65 5f 68 69 73 74 6f 72 79 20 28 5c 6e 22 29  te_history (\n")
74c0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
74d0: 6c 2c 20 22 65 76 65 6e 74 5f 69 64 20 49 4e 54  l, "event_id INT
74e0: 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 20 50 52  EGER NOT NULL PR
74f0: 49 4d 41 52 59 20 4b 45 59 20 41 55 54 4f 49 4e  IMARY KEY AUTOIN
7500: 43 52 45 4d 45 4e 54 2c 5c 6e 22 29 3b 0a 20 20  CREMENT,\n");.  
7510: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
7520: 74 61 62 6c 65 5f 6e 61 6d 65 20 54 45 58 54 20  table_name TEXT 
7530: 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20  NOT NULL,\n");. 
7540: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
7550: 22 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  "geometry_column
7560: 20 54 45 58 54 2c 5c 6e 22 29 3b 0a 20 20 20 20   TEXT,\n");.    
7570: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 65 76  strcat (sql, "ev
7580: 65 6e 74 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c  ent TEXT NOT NUL
7590: 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  L,\n");.    strc
75a0: 61 74 20 28 73 71 6c 2c 20 22 74 69 6d 65 73 74  at (sql, "timest
75b0: 61 6d 70 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c  amp TEXT NOT NUL
75c0: 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  L,\n");.    strc
75d0: 61 74 20 28 73 71 6c 2c 20 22 76 65 72 5f 73 71  at (sql, "ver_sq
75e0: 6c 69 74 65 20 54 45 58 54 20 4e 4f 54 20 4e 55  lite TEXT NOT NU
75f0: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
7600: 63 61 74 20 28 73 71 6c 2c 20 22 76 65 72 5f 73  cat (sql, "ver_s
7610: 70 6c 69 74 65 20 54 45 58 54 20 4e 4f 54 20 4e  plite TEXT NOT N
7620: 55 4c 4c 29 22 29 3b 0a 20 20 20 20 72 65 74 20  ULL)");.    ret 
7630: 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
7640: 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c  sqlite, sql, NUL
7650: 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
7660: 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
7670: 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 72 65  = SQLITE_OK)..re
7680: 74 75 72 6e 20 30 3b 0a 0a 20 20 20 20 69 66 20  turn 0;..    if 
7690: 28 74 65 73 74 53 70 61 74 69 61 4c 69 74 65 48  (testSpatiaLiteH
76a0: 69 73 74 6f 72 79 20 28 73 71 6c 69 74 65 29 29  istory (sqlite))
76b0: 0a 09 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ..return 1;.    
76c0: 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61  return 0;.}..sta
76d0: 74 69 63 20 76 6f 69 64 0a 75 70 64 61 74 65 53  tic void.updateS
76e0: 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f 72 79  patiaLiteHistory
76f0: 20 28 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69   (sqlite3 * sqli
7700: 74 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  te, const char *
7710: 74 61 62 6c 65 2c 0a 09 09 09 20 63 6f 6e 73 74  table,.... const
7720: 20 63 68 61 72 20 2a 67 65 6f 6d 2c 20 63 6f 6e   char *geom, con
7730: 73 74 20 63 68 61 72 20 2a 6f 70 65 72 61 74 69  st char *operati
7740: 6f 6e 29 0a 7b 0a 2f 2a 20 69 6e 73 65 72 74 69  on).{./* inserti
7750: 6e 67 20 61 20 72 6f 77 20 69 6e 20 53 50 41 54  ng a row in SPAT
7760: 49 41 4c 49 54 45 5f 48 49 53 54 4f 52 59 20 2a  IALITE_HISTORY *
7770: 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c 5b 32  /.    char sql[2
7780: 30 34 38 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65  048];.    sqlite
7790: 33 5f 73 74 6d 74 20 2a 73 74 6d 74 20 3d 20 4e  3_stmt *stmt = N
77a0: 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 65 74  ULL;.    int ret
77b0: 3b 0a 0a 20 20 20 20 69 66 20 28 63 68 65 63 6b  ;..    if (check
77c0: 53 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f 72  SpatiaLiteHistor
77d0: 79 20 28 73 71 6c 69 74 65 29 20 3d 3d 20 30 29  y (sqlite) == 0)
77e0: 0a 09 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 73  ..return;..    s
77f0: 74 72 63 70 79 20 28 73 71 6c 2c 20 22 49 4e 53  trcpy (sql, "INS
7800: 45 52 54 20 49 4e 54 4f 20 73 70 61 74 69 61 6c  ERT INTO spatial
7810: 69 74 65 5f 68 69 73 74 6f 72 79 20 22 29 3b 0a  ite_history ");.
7820: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
7830: 20 22 28 65 76 65 6e 74 5f 69 64 2c 20 74 61 62   "(event_id, tab
7840: 6c 65 5f 6e 61 6d 65 2c 20 67 65 6f 6d 65 74 72  le_name, geometr
7850: 79 5f 63 6f 6c 75 6d 6e 2c 20 65 76 65 6e 74 2c  y_column, event,
7860: 20 74 69 6d 65 73 74 61 6d 70 2c 20 22 29 3b 0a   timestamp, ");.
7870: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
7880: 20 22 76 65 72 5f 73 71 6c 69 74 65 2c 20 76 65   "ver_sqlite, ve
7890: 72 5f 73 70 6c 69 74 65 29 20 22 29 3b 0a 20 20  r_splite) ");.  
78a0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 0a 09    strcat (sql,..
78b0: 20 20 20 20 22 56 41 4c 55 45 53 20 28 4e 55 4c      "VALUES (NUL
78c0: 4c 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 44 61 74 65  L, ?, ?, ?, Date
78d0: 54 69 6d 65 28 27 6e 6f 77 27 29 2c 20 73 71 6c  Time('now'), sql
78e0: 69 74 65 5f 76 65 72 73 69 6f 6e 28 29 2c 20 73  ite_version(), s
78f0: 70 61 74 69 61 6c 69 74 65 5f 76 65 72 73 69 6f  patialite_versio
7900: 6e 28 29 29 22 29 3b 0a 20 20 20 20 72 65 74 20  n())");.    ret 
7910: 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
7920: 65 5f 76 32 20 28 73 71 6c 69 74 65 2c 20 73 71  e_v2 (sqlite, sq
7930: 6c 2c 20 73 74 72 6c 65 6e 20 28 73 71 6c 29 2c  l, strlen (sql),
7940: 20 26 73 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20   &stmt, NULL);. 
7950: 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
7960: 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b  LITE_OK).      {
7970: 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
7980: 20 28 22 53 51 4c 20 65 72 72 6f 72 3a 20 25 73   ("SQL error: %s
7990: 5c 6e 25 73 5c 6e 22 2c 20 73 71 6c 2c 20 73 71  \n%s\n", sql, sq
79a0: 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71  lite3_errmsg (sq
79b0: 6c 69 74 65 29 29 3b 0a 09 20 20 67 6f 74 6f 20  lite));..  goto 
79c0: 73 74 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20  stop;.      }.  
79d0: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 20    sqlite3_reset 
79e0: 28 73 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69  (stmt);.    sqli
79f0: 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e  te3_clear_bindin
7a00: 67 73 20 28 73 74 6d 74 29 3b 0a 20 20 20 20 73  gs (stmt);.    s
7a10: 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
7a20: 20 28 73 74 6d 74 2c 20 31 2c 20 74 61 62 6c 65   (stmt, 1, table
7a30: 2c 20 73 74 72 6c 65 6e 20 28 74 61 62 6c 65 29  , strlen (table)
7a40: 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
7a50: 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 6d 29  ;.    if (!geom)
7a60: 0a 09 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  ..sqlite3_bind_n
7a70: 75 6c 6c 20 28 73 74 6d 74 2c 20 32 29 3b 0a 20  ull (stmt, 2);. 
7a80: 20 20 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33     else..sqlite3
7a90: 5f 62 69 6e 64 5f 74 65 78 74 20 28 73 74 6d 74  _bind_text (stmt
7aa0: 2c 20 32 2c 20 67 65 6f 6d 2c 20 73 74 72 6c 65  , 2, geom, strle
7ab0: 6e 20 28 67 65 6f 6d 29 2c 20 53 51 4c 49 54 45  n (geom), SQLITE
7ac0: 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
7ad0: 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20  lite3_bind_text 
7ae0: 28 73 74 6d 74 2c 20 33 2c 20 6f 70 65 72 61 74  (stmt, 3, operat
7af0: 69 6f 6e 2c 20 73 74 72 6c 65 6e 20 28 6f 70 65  ion, strlen (ope
7b00: 72 61 74 69 6f 6e 29 2c 20 53 51 4c 49 54 45 5f  ration), SQLITE_
7b10: 53 54 41 54 49 43 29 3b 0a 20 20 20 20 72 65 74  STATIC);.    ret
7b20: 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 20   = sqlite3_step 
7b30: 28 73 74 6d 74 29 3b 0a 20 20 20 20 69 66 20 28  (stmt);.    if (
7b40: 72 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 44 4f  ret == SQLITE_DO
7b50: 4e 45 20 7c 7c 20 72 65 74 20 3d 3d 20 53 51 4c  NE || ret == SQL
7b60: 49 54 45 5f 52 4f 57 29 0a 09 67 6f 74 6f 20 73  ITE_ROW)..goto s
7b70: 74 6f 70 3b 0a 20 20 20 20 73 70 61 74 69 61 6c  top;.    spatial
7b80: 69 74 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f  ite_e ("SQL erro
7b90: 72 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 69 74 65  r: %s\n", sqlite
7ba0: 33 5f 65 72 72 6d 73 67 20 28 73 71 6c 69 74 65  3_errmsg (sqlite
7bb0: 29 29 3b 0a 0a 20 20 73 74 6f 70 3a 0a 20 20 20  ));..  stop:.   
7bc0: 20 69 66 20 28 73 74 6d 74 29 0a 09 73 71 6c 69   if (stmt)..sqli
7bd0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73 74  te3_finalize (st
7be0: 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  mt);.}..static i
7bf0: 6e 74 0a 63 72 65 61 74 65 41 64 76 61 6e 63 65  nt.createAdvance
7c00: 64 4d 65 74 61 44 61 74 61 20 28 73 71 6c 69 74  dMetaData (sqlit
7c10: 65 33 20 2a 20 73 71 6c 69 74 65 29 0a 7b 0a 2f  e3 * sqlite).{./
7c20: 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 61  * creating the a
7c30: 64 76 61 6e 63 65 64 20 4d 65 74 61 44 61 74 61  dvanced MetaData
7c40: 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 63   tables */.    c
7c50: 68 61 72 20 73 71 6c 5b 34 31 38 36 5d 3b 0a 20  har sql[4186];. 
7c60: 20 20 20 63 68 61 72 20 2a 65 72 72 4d 73 67 20     char *errMsg 
7c70: 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20  = NULL;.    int 
7c80: 72 65 74 3b 0a 2f 2a 20 63 72 65 61 74 69 6e 67  ret;./* creating
7c90: 20 74 68 65 20 56 49 45 57 53 5f 47 45 4f 4d 45   the VIEWS_GEOME
7ca0: 54 52 59 5f 43 4f 4c 55 4d 4e 53 20 74 61 62 6c  TRY_COLUMNS tabl
7cb0: 65 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20  e */.    strcpy 
7cc0: 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54 41  (sql, "CREATE TA
7cd0: 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 53 54  BLE IF NOT EXIST
7ce0: 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  S ");.    strcat
7cf0: 20 28 73 71 6c 2c 20 22 76 69 65 77 73 5f 67 65   (sql, "views_ge
7d00: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20 28  ometry_columns (
7d10: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
7d20: 20 28 73 71 6c 2c 20 22 76 69 65 77 5f 6e 61 6d   (sql, "view_nam
7d30: 65 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c  e TEXT NOT NULL,
7d40: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
7d50: 20 28 73 71 6c 2c 20 22 76 69 65 77 5f 67 65 6f   (sql, "view_geo
7d60: 6d 65 74 72 79 20 54 45 58 54 20 4e 4f 54 20 4e  metry TEXT NOT N
7d70: 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  ULL,\n");.    st
7d80: 72 63 61 74 20 28 73 71 6c 2c 20 22 76 69 65 77  rcat (sql, "view
7d90: 5f 72 6f 77 69 64 20 54 45 58 54 20 4e 4f 54 20  _rowid TEXT NOT 
7da0: 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  NULL,\n");.    s
7db0: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66 5f 74  trcat (sql, "f_t
7dc0: 61 62 6c 65 5f 6e 61 6d 65 20 54 45 58 54 20 4e  able_name TEXT N
7dd0: 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20  OT NULL,\n");.  
7de0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
7df0: 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  f_geometry_colum
7e00: 6e 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c  n TEXT NOT NULL,
7e10: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
7e20: 20 28 73 71 6c 2c 20 22 72 65 61 64 5f 6f 6e 6c   (sql, "read_onl
7e30: 79 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55  y INTEGER NOT NU
7e40: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
7e50: 63 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54  cat (sql, "CONST
7e60: 52 41 49 4e 54 20 70 6b 5f 67 65 6f 6d 5f 63 6f  RAINT pk_geom_co
7e70: 6c 73 5f 76 69 65 77 73 20 50 52 49 4d 41 52 59  ls_views PRIMARY
7e80: 20 4b 45 59 20 22 29 3b 0a 20 20 20 20 73 74 72   KEY ");.    str
7e90: 63 61 74 20 28 73 71 6c 2c 20 22 28 76 69 65 77  cat (sql, "(view
7ea0: 5f 6e 61 6d 65 2c 20 76 69 65 77 5f 67 65 6f 6d  _name, view_geom
7eb0: 65 74 72 79 29 2c 5c 6e 22 29 3b 0a 20 20 20 20  etry),\n");.    
7ec0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43 4f  strcat (sql, "CO
7ed0: 4e 53 54 52 41 49 4e 54 20 66 6b 5f 76 69 65 77  NSTRAINT fk_view
7ee0: 73 5f 67 65 6f 6d 5f 63 6f 6c 73 20 46 4f 52 45  s_geom_cols FORE
7ef0: 49 47 4e 20 4b 45 59 20 22 29 3b 0a 20 20 20 20  IGN KEY ");.    
7f00: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28 66  strcat (sql, "(f
7f10: 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67  _table_name, f_g
7f20: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20  eometry_column) 
7f30: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
7f40: 73 71 6c 2c 20 22 52 45 46 45 52 45 4e 43 45 53  sql, "REFERENCES
7f50: 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e   geometry_column
7f60: 73 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  s ");.    strcat
7f70: 20 28 73 71 6c 2c 20 22 28 66 5f 74 61 62 6c 65   (sql, "(f_table
7f80: 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72  _name, f_geometr
7f90: 79 5f 63 6f 6c 75 6d 6e 29 20 22 29 3b 0a 20 20  y_column) ");.  
7fa0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
7fb0: 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41 44  ON DELETE CASCAD
7fc0: 45 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  E,\n");.    strc
7fd0: 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52  at (sql, "CONSTR
7fe0: 41 49 4e 54 20 63 6b 5f 76 77 5f 72 64 6f 6e 6c  AINT ck_vw_rdonl
7ff0: 79 20 43 48 45 43 4b 20 28 72 65 61 64 5f 6f 6e  y CHECK (read_on
8000: 6c 79 20 49 4e 20 22 29 3b 0a 20 20 20 20 73 74  ly IN ");.    st
8010: 72 63 61 74 20 28 73 71 6c 2c 20 22 28 30 2c 31  rcat (sql, "(0,1
8020: 29 29 29 22 29 3b 0a 20 20 20 20 72 65 74 20 3d  )))");.    ret =
8030: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73   sqlite3_exec (s
8040: 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c  qlite, sql, NULL
8050: 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29  , NULL, &errMsg)
8060: 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d  ;.    if (ret !=
8070: 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20   SQLITE_OK).    
8080: 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
8090: 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f 72 3a  e_e ("SQL error:
80a0: 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 2c   %s: %s\n", sql,
80b0: 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71 6c   errMsg);..  sql
80c0: 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73  ite3_free (errMs
80d0: 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b  g);..  return 0;
80e0: 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65 61  .      }./* crea
80f0: 74 69 6e 67 20 61 6e 20 49 4e 44 45 58 20 73 75  ting an INDEX su
8100: 70 70 6f 72 74 69 6e 67 20 74 68 65 20 47 45 4f  pporting the GEO
8110: 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e 53 20 46 4b  METRY_COLUMNS FK
8120: 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20 28   */.    strcpy (
8130: 73 71 6c 2c 20 22 43 52 45 41 54 45 20 49 4e 44  sql, "CREATE IND
8140: 45 58 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53  EX IF NOT EXISTS
8150: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
8160: 28 73 71 6c 2c 20 22 69 64 78 5f 76 69 65 77 73  (sql, "idx_views
8170: 6a 6f 69 6e 20 4f 4e 20 76 69 65 77 73 5f 67 65  join ON views_ge
8180: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5c 6e  ometry_columns\n
8190: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
81a0: 73 71 6c 2c 20 22 28 66 5f 74 61 62 6c 65 5f 6e  sql, "(f_table_n
81b0: 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79 5f  ame, f_geometry_
81c0: 63 6f 6c 75 6d 6e 29 22 29 3b 0a 20 20 20 20 72  column)");.    r
81d0: 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
81e0: 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  c (sqlite, sql, 
81f0: 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
8200: 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  Msg);.    if (re
8210: 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
8220: 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
8230: 61 6c 69 74 65 5f 65 20 28 22 53 51 4c 20 65 72  alite_e ("SQL er
8240: 72 6f 72 3a 20 25 73 3a 20 25 73 5c 6e 22 2c 20  ror: %s: %s\n", 
8250: 73 71 6c 2c 20 65 72 72 4d 73 67 29 3b 0a 09 20  sql, errMsg);.. 
8260: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65   sqlite3_free (e
8270: 72 72 4d 73 67 29 3b 0a 09 20 20 72 65 74 75 72  rrMsg);..  retur
8280: 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 2f 2a 20  n 0;.      }./* 
8290: 63 72 65 61 74 69 6e 67 20 74 68 65 20 56 49 52  creating the VIR
82a0: 54 53 5f 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55  TS_GEOMETRY_COLU
82b0: 4d 4e 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  MNS table */.   
82c0: 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43   strcpy (sql, "C
82d0: 52 45 41 54 45 20 54 41 42 4c 45 20 49 46 20 4e  REATE TABLE IF N
82e0: 4f 54 20 45 58 49 53 54 53 20 22 29 3b 0a 20 20  OT EXISTS ");.  
82f0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
8300: 76 69 72 74 73 5f 67 65 6f 6d 65 74 72 79 5f 63  virts_geometry_c
8310: 6f 6c 75 6d 6e 73 20 28 5c 6e 22 29 3b 0a 20 20  olumns (\n");.  
8320: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
8330: 76 69 72 74 5f 6e 61 6d 65 20 54 45 58 54 20 4e  virt_name TEXT N
8340: 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20  OT NULL,\n");.  
8350: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
8360: 76 69 72 74 5f 67 65 6f 6d 65 74 72 79 20 54 45  virt_geometry TE
8370: 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29  XT NOT NULL,\n")
8380: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
8390: 6c 2c 20 22 67 65 6f 6d 65 74 72 79 5f 74 79 70  l, "geometry_typ
83a0: 65 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55  e INTEGER NOT NU
83b0: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
83c0: 63 61 74 20 28 73 71 6c 2c 20 22 63 6f 6f 72 64  cat (sql, "coord
83d0: 5f 64 69 6d 65 6e 73 69 6f 6e 20 49 4e 54 45 47  _dimension INTEG
83e0: 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29  ER NOT NULL,\n")
83f0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
8400: 6c 2c 20 22 73 72 69 64 20 49 4e 54 45 47 45 52  l, "srid INTEGER
8410: 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a   NOT NULL,\n");.
8420: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
8430: 20 22 43 4f 4e 53 54 52 41 49 4e 54 20 70 6b 5f   "CONSTRAINT pk_
8440: 67 65 6f 6d 5f 63 6f 6c 73 5f 76 69 72 74 73 20  geom_cols_virts 
8450: 50 52 49 4d 41 52 59 20 4b 45 59 20 22 29 3b 0a  PRIMARY KEY ");.
8460: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
8470: 20 22 28 76 69 72 74 5f 6e 61 6d 65 2c 20 76 69   "(virt_name, vi
8480: 72 74 5f 67 65 6f 6d 65 74 72 79 29 2c 5c 6e 22  rt_geometry),\n"
8490: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
84a0: 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54 20  ql, "CONSTRAINT 
84b0: 66 6b 5f 76 67 63 5f 73 72 69 64 20 46 4f 52 45  fk_vgc_srid FORE
84c0: 49 47 4e 20 4b 45 59 20 22 29 3b 0a 20 20 20 20  IGN KEY ");.    
84d0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28 73  strcat (sql, "(s
84e0: 72 69 64 29 20 52 45 46 45 52 45 4e 43 45 53 20  rid) REFERENCES 
84f0: 73 70 61 74 69 61 6c 5f 72 65 66 5f 73 79 73 20  spatial_ref_sys 
8500: 28 73 72 69 64 29 2c 5c 6e 22 29 3b 0a 20 20 20  (srid),\n");.   
8510: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43   strcat (sql, "C
8520: 4f 4e 53 54 52 41 49 4e 54 20 63 6b 5f 76 67 63  ONSTRAINT ck_vgc
8530: 5f 74 79 70 65 20 43 48 45 43 4b 20 28 67 65 6f  _type CHECK (geo
8540: 6d 65 74 72 79 5f 74 79 70 65 20 49 4e 20 22 29  metry_type IN ")
8550: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
8560: 6c 2c 20 22 28 31 2c 32 2c 33 2c 34 2c 35 2c 36  l, "(1,2,3,4,5,6
8570: 2c 31 30 30 31 2c 31 30 30 32 2c 31 30 30 33 2c  ,1001,1002,1003,
8580: 31 30 30 34 2c 31 30 30 35 2c 31 30 30 36 2c 22  1004,1005,1006,"
8590: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
85a0: 71 6c 2c 20 22 32 30 30 31 2c 32 30 30 32 2c 32  ql, "2001,2002,2
85b0: 30 30 33 2c 32 30 30 34 2c 32 30 30 35 2c 32 30  003,2004,2005,20
85c0: 30 36 2c 33 30 30 31 2c 33 30 30 32 2c 22 29 3b  06,3001,3002,");
85d0: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
85e0: 2c 20 22 33 30 30 33 2c 33 30 30 34 2c 33 30 30  , "3003,3004,300
85f0: 35 2c 33 30 30 36 29 29 2c 5c 6e 22 29 3b 0a 20  5,3006)),\n");. 
8600: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
8610: 22 43 4f 4e 53 54 52 41 49 4e 54 20 63 6b 5f 76  "CONSTRAINT ck_v
8620: 67 63 5f 64 69 6d 73 20 43 48 45 43 4b 20 28 63  gc_dims CHECK (c
8630: 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 20 49  oord_dimension I
8640: 4e 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  N ");.    strcat
8650: 20 28 73 71 6c 2c 20 22 28 32 2c 33 2c 34 29 29   (sql, "(2,3,4))
8660: 29 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73  )");.    ret = s
8670: 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
8680: 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
8690: 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a  NULL, &errMsg);.
86a0: 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53      if (ret != S
86b0: 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20  QLITE_OK).      
86c0: 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
86d0: 65 20 28 22 53 51 4c 20 65 72 72 6f 72 3a 20 25  e ("SQL error: %
86e0: 73 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 2c 20 65  s: %s\n", sql, e
86f0: 72 72 4d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74  rrMsg);..  sqlit
8700: 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73 67 29  e3_free (errMsg)
8710: 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  ;..  return 0;. 
8720: 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65 61 74 69       }./* creati
8730: 6e 67 20 61 6e 20 49 4e 44 45 58 20 73 75 70 70  ng an INDEX supp
8740: 6f 72 74 69 6e 67 20 74 68 65 20 53 50 41 54 49  orting the SPATI
8750: 41 4c 5f 52 45 46 5f 53 59 53 20 46 4b 20 2a 2f  AL_REF_SYS FK */
8760: 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
8770: 2c 20 22 43 52 45 41 54 45 20 49 4e 44 45 58 20  , "CREATE INDEX 
8780: 49 46 20 4e 4f 54 20 45 58 49 53 54 53 20 22 29  IF NOT EXISTS ")
8790: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
87a0: 6c 2c 20 22 69 64 78 5f 76 69 72 74 73 73 72 69  l, "idx_virtssri
87b0: 64 20 4f 4e 20 76 69 72 74 73 5f 67 65 6f 6d 65  d ON virts_geome
87c0: 74 72 79 5f 63 6f 6c 75 6d 6e 73 5c 6e 22 29 3b  try_columns\n");
87d0: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
87e0: 2c 20 22 28 73 72 69 64 29 22 29 3b 0a 20 20 20  , "(srid)");.   
87f0: 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65   ret = sqlite3_e
8800: 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c  xec (sqlite, sql
8810: 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
8820: 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28  rrMsg);.    if (
8830: 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b  ret != SQLITE_OK
8840: 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61  ).      {..  spa
8850: 74 69 61 6c 69 74 65 5f 65 20 28 22 53 51 4c 20  tialite_e ("SQL 
8860: 65 72 72 6f 72 3a 20 25 73 3a 20 25 73 5c 6e 22  error: %s: %s\n"
8870: 2c 20 73 71 6c 2c 20 65 72 72 4d 73 67 29 3b 0a  , sql, errMsg);.
8880: 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20  .  sqlite3_free 
8890: 28 65 72 72 4d 73 67 29 3b 0a 09 20 20 72 65 74  (errMsg);..  ret
88a0: 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 2f  urn 0;.      }./
88b0: 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 47  * creating the G
88c0: 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e 53 5f  EOMETRY_COLUMNS_
88d0: 53 54 41 54 49 53 54 49 43 53 20 74 61 62 6c 65  STATISTICS table
88e0: 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20 28   */.    strcpy (
88f0: 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54 41 42  sql, "CREATE TAB
8900: 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53  LE IF NOT EXISTS
8910: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
8920: 28 73 71 6c 2c 20 22 67 65 6f 6d 65 74 72 79 5f  (sql, "geometry_
8930: 63 6f 6c 75 6d 6e 73 5f 73 74 61 74 69 73 74 69  columns_statisti
8940: 63 73 20 28 5c 6e 22 29 3b 0a 20 20 20 20 73 74  cs (\n");.    st
8950: 72 63 61 74 20 28 73 71 6c 2c 20 22 66 5f 74 61  rcat (sql, "f_ta
8960: 62 6c 65 5f 6e 61 6d 65 20 54 45 58 54 20 4e 4f  ble_name TEXT NO
8970: 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20  T NULL,\n");.   
8980: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66   strcat (sql, "f
8990: 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
89a0: 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c   TEXT NOT NULL,\
89b0: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
89c0: 28 73 71 6c 2c 20 22 6c 61 73 74 5f 76 65 72 69  (sql, "last_veri
89d0: 66 69 65 64 20 54 49 4d 45 53 54 41 4d 50 2c 5c  fied TIMESTAMP,\
89e0: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
89f0: 28 73 71 6c 2c 20 22 72 6f 77 5f 63 6f 75 6e 74  (sql, "row_count
8a00: 20 49 4e 54 45 47 45 52 2c 5c 6e 22 29 3b 0a 20   INTEGER,\n");. 
8a10: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
8a20: 22 65 78 74 65 6e 74 5f 6d 69 6e 5f 78 20 44 4f  "extent_min_x DO
8a30: 55 42 4c 45 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  UBLE,\n");.    s
8a40: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 65 78 74  trcat (sql, "ext
8a50: 65 6e 74 5f 6d 69 6e 5f 79 20 44 4f 55 42 4c 45  ent_min_y DOUBLE
8a60: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
8a70: 74 20 28 73 71 6c 2c 20 22 65 78 74 65 6e 74 5f  t (sql, "extent_
8a80: 6d 61 78 5f 78 20 44 4f 55 42 4c 45 2c 5c 6e 22  max_x DOUBLE,\n"
8a90: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
8aa0: 71 6c 2c 20 22 65 78 74 65 6e 74 5f 6d 61 78 5f  ql, "extent_max_
8ab0: 79 20 44 4f 55 42 4c 45 2c 5c 6e 22 29 3b 0a 20  y DOUBLE,\n");. 
8ac0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
8ad0: 22 43 4f 4e 53 54 52 41 49 4e 54 20 70 6b 5f 67  "CONSTRAINT pk_g
8ae0: 63 5f 73 74 61 74 69 73 74 69 63 73 20 50 52 49  c_statistics PRI
8af0: 4d 41 52 59 20 4b 45 59 20 22 29 3b 0a 20 20 20  MARY KEY ");.   
8b00: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28   strcat (sql, "(
8b10: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f  f_table_name, f_
8b20: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29  geometry_column)
8b30: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
8b40: 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41  t (sql, "CONSTRA
8b50: 49 4e 54 20 66 6b 5f 67 63 5f 73 74 61 74 69 73  INT fk_gc_statis
8b60: 74 69 63 73 20 46 4f 52 45 49 47 4e 20 4b 45 59  tics FOREIGN KEY
8b70: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
8b80: 28 73 71 6c 2c 20 22 28 66 5f 74 61 62 6c 65 5f  (sql, "(f_table_
8b90: 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79  name, f_geometry
8ba0: 5f 63 6f 6c 75 6d 6e 29 20 52 45 46 45 52 45 4e  _column) REFEREN
8bb0: 43 45 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63  CES ");.    strc
8bc0: 61 74 20 28 73 71 6c 2c 20 22 67 65 6f 6d 65 74  at (sql, "geomet
8bd0: 72 79 5f 63 6f 6c 75 6d 6e 73 20 28 66 5f 74 61  ry_columns (f_ta
8be0: 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d  ble_name, f_geom
8bf0: 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 22 29 3b  etry_column) ");
8c00: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
8c10: 2c 20 22 4f 4e 20 44 45 4c 45 54 45 20 43 41 53  , "ON DELETE CAS
8c20: 43 41 44 45 29 22 29 3b 0a 20 20 20 20 72 65 74  CADE)");.    ret
8c30: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
8c40: 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
8c50: 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
8c60: 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
8c70: 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20  != SQLITE_OK).  
8c80: 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c      {..  spatial
8c90: 69 74 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f  ite_e ("SQL erro
8ca0: 72 3a 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71  r: %s: %s\n", sq
8cb0: 6c 2c 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73  l, errMsg);..  s
8cc0: 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72  qlite3_free (err
8cd0: 4d 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20  Msg);..  return 
8ce0: 30 3b 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72  0;.      }./* cr
8cf0: 65 61 74 69 6e 67 20 74 68 65 20 56 49 45 57 53  eating the VIEWS
8d00: 5f 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e  _GEOMETRY_COLUMN
8d10: 53 5f 53 54 41 54 49 53 54 49 43 53 20 74 61 62  S_STATISTICS tab
8d20: 6c 65 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79  le */.    strcpy
8d30: 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54   (sql, "CREATE T
8d40: 41 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 53  ABLE IF NOT EXIS
8d50: 54 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  TS ");.    strca
8d60: 74 20 28 73 71 6c 2c 20 22 76 69 65 77 73 5f 67  t (sql, "views_g
8d70: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f  eometry_columns_
8d80: 73 74 61 74 69 73 74 69 63 73 20 28 5c 6e 22 29  statistics (\n")
8d90: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
8da0: 6c 2c 20 22 76 69 65 77 5f 6e 61 6d 65 20 54 45  l, "view_name TE
8db0: 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29  XT NOT NULL,\n")
8dc0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
8dd0: 6c 2c 20 22 76 69 65 77 5f 67 65 6f 6d 65 74 72  l, "view_geometr
8de0: 79 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c  y TEXT NOT NULL,
8df0: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
8e00: 20 28 73 71 6c 2c 20 22 6c 61 73 74 5f 76 65 72   (sql, "last_ver
8e10: 69 66 69 65 64 20 54 49 4d 45 53 54 41 4d 50 2c  ified TIMESTAMP,
8e20: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
8e30: 20 28 73 71 6c 2c 20 22 72 6f 77 5f 63 6f 75 6e   (sql, "row_coun
8e40: 74 20 49 4e 54 45 47 45 52 2c 5c 6e 22 29 3b 0a  t INTEGER,\n");.
8e50: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
8e60: 20 22 65 78 74 65 6e 74 5f 6d 69 6e 5f 78 20 44   "extent_min_x D
8e70: 4f 55 42 4c 45 2c 5c 6e 22 29 3b 0a 20 20 20 20  OUBLE,\n");.    
8e80: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 65 78  strcat (sql, "ex
8e90: 74 65 6e 74 5f 6d 69 6e 5f 79 20 44 4f 55 42 4c  tent_min_y DOUBL
8ea0: 45 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  E,\n");.    strc
8eb0: 61 74 20 28 73 71 6c 2c 20 22 65 78 74 65 6e 74  at (sql, "extent
8ec0: 5f 6d 61 78 5f 78 20 44 4f 55 42 4c 45 2c 5c 6e  _max_x DOUBLE,\n
8ed0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
8ee0: 73 71 6c 2c 20 22 65 78 74 65 6e 74 5f 6d 61 78  sql, "extent_max
8ef0: 5f 79 20 44 4f 55 42 4c 45 2c 5c 6e 22 29 3b 0a  _y DOUBLE,\n");.
8f00: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
8f10: 20 22 43 4f 4e 53 54 52 41 49 4e 54 20 70 6b 5f   "CONSTRAINT pk_
8f20: 76 77 67 63 5f 73 74 61 74 69 73 74 69 63 73 20  vwgc_statistics 
8f30: 50 52 49 4d 41 52 59 20 4b 45 59 20 22 29 3b 0a  PRIMARY KEY ");.
8f40: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
8f50: 20 22 28 76 69 65 77 5f 6e 61 6d 65 2c 20 76 69   "(view_name, vi
8f60: 65 77 5f 67 65 6f 6d 65 74 72 79 29 2c 5c 6e 22  ew_geometry),\n"
8f70: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
8f80: 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54 20  ql, "CONSTRAINT 
8f90: 66 6b 5f 76 77 67 63 5f 73 74 61 74 69 73 74 69  fk_vwgc_statisti
8fa0: 63 73 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 22  cs FOREIGN KEY "
8fb0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
8fc0: 71 6c 2c 20 22 28 76 69 65 77 5f 6e 61 6d 65 2c  ql, "(view_name,
8fd0: 20 76 69 65 77 5f 67 65 6f 6d 65 74 72 79 29 20   view_geometry) 
8fe0: 52 45 46 45 52 45 4e 43 45 53 20 22 29 3b 0a 20  REFERENCES ");. 
8ff0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
9000: 22 76 69 65 77 73 5f 67 65 6f 6d 65 74 72 79 5f  "views_geometry_
9010: 63 6f 6c 75 6d 6e 73 20 28 76 69 65 77 5f 6e 61  columns (view_na
9020: 6d 65 2c 20 76 69 65 77 5f 67 65 6f 6d 65 74 72  me, view_geometr
9030: 79 29 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  y) ");.    strca
9040: 74 20 28 73 71 6c 2c 20 22 4f 4e 20 44 45 4c 45  t (sql, "ON DELE
9050: 54 45 20 43 41 53 43 41 44 45 29 22 29 3b 0a 20  TE CASCADE)");. 
9060: 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33     ret = sqlite3
9070: 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73  _exec (sqlite, s
9080: 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  ql, NULL, NULL, 
9090: 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  &errMsg);.    if
90a0: 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
90b0: 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OK).      {..  s
90c0: 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 53 51  patialite_e ("SQ
90d0: 4c 20 65 72 72 6f 72 3a 20 25 73 3a 20 25 73 5c  L error: %s: %s\
90e0: 6e 22 2c 20 73 71 6c 2c 20 65 72 72 4d 73 67 29  n", sql, errMsg)
90f0: 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
9100: 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20 72  e (errMsg);..  r
9110: 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
9120: 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65  ./* creating the
9130: 20 56 49 52 54 53 5f 47 45 4f 4d 45 54 52 59 5f   VIRTS_GEOMETRY_
9140: 43 4f 4c 55 4d 4e 53 5f 53 54 41 54 49 53 54 49  COLUMNS_STATISTI
9150: 43 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  CS table */.    
9160: 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52  strcpy (sql, "CR
9170: 45 41 54 45 20 54 41 42 4c 45 20 49 46 20 4e 4f  EATE TABLE IF NO
9180: 54 20 45 58 49 53 54 53 20 22 29 3b 0a 20 20 20  T EXISTS ");.   
9190: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 76   strcat (sql, "v
91a0: 69 72 74 73 5f 67 65 6f 6d 65 74 72 79 5f 63 6f  irts_geometry_co
91b0: 6c 75 6d 6e 73 5f 73 74 61 74 69 73 74 69 63 73  lumns_statistics
91c0: 20 28 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63   (\n");.    strc
91d0: 61 74 20 28 73 71 6c 2c 20 22 76 69 72 74 5f 6e  at (sql, "virt_n
91e0: 61 6d 65 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c  ame TEXT NOT NUL
91f0: 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  L,\n");.    strc
9200: 61 74 20 28 73 71 6c 2c 20 22 76 69 72 74 5f 67  at (sql, "virt_g
9210: 65 6f 6d 65 74 72 79 20 54 45 58 54 20 4e 4f 54  eometry TEXT NOT
9220: 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20   NULL,\n");.    
9230: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6c 61  strcat (sql, "la
9240: 73 74 5f 76 65 72 69 66 69 65 64 20 54 49 4d 45  st_verified TIME
9250: 53 54 41 4d 50 2c 5c 6e 22 29 3b 0a 20 20 20 20  STAMP,\n");.    
9260: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 72 6f  strcat (sql, "ro
9270: 77 5f 63 6f 75 6e 74 20 49 4e 54 45 47 45 52 2c  w_count INTEGER,
9280: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
9290: 20 28 73 71 6c 2c 20 22 65 78 74 65 6e 74 5f 6d   (sql, "extent_m
92a0: 69 6e 5f 78 20 44 4f 55 42 4c 45 2c 5c 6e 22 29  in_x DOUBLE,\n")
92b0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
92c0: 6c 2c 20 22 65 78 74 65 6e 74 5f 6d 69 6e 5f 79  l, "extent_min_y
92d0: 20 44 4f 55 42 4c 45 2c 5c 6e 22 29 3b 0a 20 20   DOUBLE,\n");.  
92e0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
92f0: 65 78 74 65 6e 74 5f 6d 61 78 5f 78 20 44 4f 55  extent_max_x DOU
9300: 42 4c 45 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  BLE,\n");.    st
9310: 72 63 61 74 20 28 73 71 6c 2c 20 22 65 78 74 65  rcat (sql, "exte
9320: 6e 74 5f 6d 61 78 5f 79 20 44 4f 55 42 4c 45 2c  nt_max_y DOUBLE,
9330: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
9340: 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49   (sql, "CONSTRAI
9350: 4e 54 20 70 6b 5f 76 72 74 67 63 5f 73 74 61 74  NT pk_vrtgc_stat
9360: 69 73 74 69 63 73 20 50 52 49 4d 41 52 59 20 4b  istics PRIMARY K
9370: 45 59 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  EY ");.    strca
9380: 74 20 28 73 71 6c 2c 20 22 28 76 69 72 74 5f 6e  t (sql, "(virt_n
9390: 61 6d 65 2c 20 76 69 72 74 5f 67 65 6f 6d 65 74  ame, virt_geomet
93a0: 72 79 29 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  ry),\n");.    st
93b0: 72 63 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53  rcat (sql, "CONS
93c0: 54 52 41 49 4e 54 20 66 6b 5f 76 72 74 67 63 5f  TRAINT fk_vrtgc_
93d0: 73 74 61 74 69 73 74 69 63 73 20 46 4f 52 45 49  statistics FOREI
93e0: 47 4e 20 4b 45 59 20 22 29 3b 0a 20 20 20 20 73  GN KEY ");.    s
93f0: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28 76 69  trcat (sql, "(vi
9400: 72 74 5f 6e 61 6d 65 2c 20 76 69 72 74 5f 67 65  rt_name, virt_ge
9410: 6f 6d 65 74 72 79 29 20 52 45 46 45 52 45 4e 43  ometry) REFERENC
9420: 45 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ES ");.    strca
9430: 74 20 28 73 71 6c 2c 20 22 76 69 72 74 73 5f 67  t (sql, "virts_g
9440: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20  eometry_columns 
9450: 28 76 69 72 74 5f 6e 61 6d 65 2c 20 76 69 72 74  (virt_name, virt
9460: 5f 67 65 6f 6d 65 74 72 79 29 20 22 29 3b 0a 20  _geometry) ");. 
9470: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
9480: 22 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41  "ON DELETE CASCA
9490: 44 45 29 22 29 3b 0a 20 20 20 20 72 65 74 20 3d  DE)");.    ret =
94a0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73   sqlite3_exec (s
94b0: 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c  qlite, sql, NULL
94c0: 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29  , NULL, &errMsg)
94d0: 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d  ;.    if (ret !=
94e0: 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20   SQLITE_OK).    
94f0: 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
9500: 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f 72 3a  e_e ("SQL error:
9510: 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 2c   %s: %s\n", sql,
9520: 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71 6c   errMsg);..  sql
9530: 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73  ite3_free (errMs
9540: 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b  g);..  return 0;
9550: 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65 61  .      }./* crea
9560: 74 69 6e 67 20 74 68 65 20 47 45 4f 4d 45 54 52  ting the GEOMETR
9570: 59 5f 43 4f 4c 55 4d 4e 53 5f 46 49 45 4c 44 5f  Y_COLUMNS_FIELD_
9580: 49 4e 46 4f 53 20 74 61 62 6c 65 20 2a 2f 0a 20  INFOS table */. 
9590: 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20     strcpy (sql, 
95a0: 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 49 46  "CREATE TABLE IF
95b0: 20 4e 4f 54 20 45 58 49 53 54 53 20 22 29 3b 0a   NOT EXISTS ");.
95c0: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
95d0: 20 22 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d   "geometry_colum
95e0: 6e 73 5f 66 69 65 6c 64 5f 69 6e 66 6f 73 20 28  ns_field_infos (
95f0: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
9600: 20 28 73 71 6c 2c 20 22 66 5f 74 61 62 6c 65 5f   (sql, "f_table_
9610: 6e 61 6d 65 20 54 45 58 54 20 4e 4f 54 20 4e 55  name TEXT NOT NU
9620: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
9630: 63 61 74 20 28 73 71 6c 2c 20 22 66 5f 67 65 6f  cat (sql, "f_geo
9640: 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 20 54 45 58  metry_column TEX
9650: 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b  T NOT NULL,\n");
9660: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
9670: 2c 20 22 6f 72 64 69 6e 61 6c 20 49 4e 54 45 47  , "ordinal INTEG
9680: 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29  ER NOT NULL,\n")
9690: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
96a0: 6c 2c 20 22 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20  l, "column_name 
96b0: 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  TEXT NOT NULL,\n
96c0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
96d0: 73 71 6c 2c 20 22 6e 75 6c 6c 5f 76 61 6c 75 65  sql, "null_value
96e0: 73 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55  s INTEGER NOT NU
96f0: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
9700: 63 61 74 20 28 73 71 6c 2c 20 22 69 6e 74 65 67  cat (sql, "integ
9710: 65 72 5f 76 61 6c 75 65 73 20 49 4e 54 45 47 45  er_values INTEGE
9720: 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b  R NOT NULL,\n");
9730: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
9740: 2c 20 22 64 6f 75 62 6c 65 5f 76 61 6c 75 65 73  , "double_values
9750: 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c   INTEGER NOT NUL
9760: 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  L,\n");.    strc
9770: 61 74 20 28 73 71 6c 2c 20 22 74 65 78 74 5f 76  at (sql, "text_v
9780: 61 6c 75 65 73 20 49 4e 54 45 47 45 52 20 4e 4f  alues INTEGER NO
9790: 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20  T NULL,\n");.   
97a0: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 62   strcat (sql, "b
97b0: 6c 6f 62 5f 76 61 6c 75 65 73 20 49 4e 54 45 47  lob_values INTEG
97c0: 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29  ER NOT NULL,\n")
97d0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
97e0: 6c 2c 20 22 6d 61 78 5f 73 69 7a 65 20 49 4e 54  l, "max_size INT
97f0: 45 47 45 52 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  EGER,\n");.    s
9800: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e  trcat (sql, "CON
9810: 53 54 52 41 49 4e 54 20 70 6b 5f 67 63 66 6c 64  STRAINT pk_gcfld
9820: 5f 69 6e 66 6f 73 20 50 52 49 4d 41 52 59 20 4b  _infos PRIMARY K
9830: 45 59 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  EY ");.    strca
9840: 74 20 28 73 71 6c 2c 20 22 28 66 5f 74 61 62 6c  t (sql, "(f_tabl
9850: 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74  e_name, f_geomet
9860: 72 79 5f 63 6f 6c 75 6d 6e 2c 20 6f 72 64 69 6e  ry_column, ordin
9870: 61 6c 2c 20 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 29  al, column_name)
9880: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
9890: 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41  t (sql, "CONSTRA
98a0: 49 4e 54 20 66 6b 5f 67 63 66 6c 64 5f 69 6e 66  INT fk_gcfld_inf
98b0: 6f 73 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 22  os FOREIGN KEY "
98c0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
98d0: 71 6c 2c 20 22 28 66 5f 74 61 62 6c 65 5f 6e 61  ql, "(f_table_na
98e0: 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63  me, f_geometry_c
98f0: 6f 6c 75 6d 6e 29 20 52 45 46 45 52 45 4e 43 45  olumn) REFERENCE
9900: 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  S ");.    strcat
9910: 20 28 73 71 6c 2c 20 22 67 65 6f 6d 65 74 72 79   (sql, "geometry
9920: 5f 63 6f 6c 75 6d 6e 73 20 28 66 5f 74 61 62 6c  _columns (f_tabl
9930: 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74  e_name, f_geomet
9940: 72 79 5f 63 6f 6c 75 6d 6e 29 20 22 29 3b 0a 20  ry_column) ");. 
9950: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
9960: 22 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41  "ON DELETE CASCA
9970: 44 45 29 22 29 3b 0a 20 20 20 20 72 65 74 20 3d  DE)");.    ret =
9980: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73   sqlite3_exec (s
9990: 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c  qlite, sql, NULL
99a0: 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29  , NULL, &errMsg)
99b0: 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d  ;.    if (ret !=
99c0: 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20   SQLITE_OK).    
99d0: 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
99e0: 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f 72 3a  e_e ("SQL error:
99f0: 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 2c   %s: %s\n", sql,
9a00: 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71 6c   errMsg);..  sql
9a10: 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73  ite3_free (errMs
9a20: 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b  g);..  return 0;
9a30: 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65 61  .      }./* crea
9a40: 74 69 6e 67 20 74 68 65 20 56 49 45 57 53 5f 43  ting the VIEWS_C
9a50: 4f 4c 55 4d 4e 53 5f 46 49 45 4c 44 5f 49 4e 46  OLUMNS_FIELD_INF
9a60: 4f 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  OS table */.    
9a70: 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52  strcpy (sql, "CR
9a80: 45 41 54 45 20 54 41 42 4c 45 20 49 46 20 4e 4f  EATE TABLE IF NO
9a90: 54 20 45 58 49 53 54 53 20 22 29 3b 0a 20 20 20  T EXISTS ");.   
9aa0: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 76   strcat (sql, "v
9ab0: 69 65 77 73 5f 67 65 6f 6d 65 74 72 79 5f 63 6f  iews_geometry_co
9ac0: 6c 75 6d 6e 73 5f 66 69 65 6c 64 5f 69 6e 66 6f  lumns_field_info
9ad0: 73 20 28 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  s (\n");.    str
9ae0: 63 61 74 20 28 73 71 6c 2c 20 22 76 69 65 77 5f  cat (sql, "view_
9af0: 6e 61 6d 65 20 54 45 58 54 20 4e 4f 54 20 4e 55  name TEXT NOT NU
9b00: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
9b10: 63 61 74 20 28 73 71 6c 2c 20 22 76 69 65 77 5f  cat (sql, "view_
9b20: 67 65 6f 6d 65 74 72 79 20 54 45 58 54 20 4e 4f  geometry TEXT NO
9b30: 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20  T NULL,\n");.   
9b40: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6f   strcat (sql, "o
9b50: 72 64 69 6e 61 6c 20 49 4e 54 45 47 45 52 20 4e  rdinal INTEGER N
9b60: 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20  OT NULL,\n");.  
9b70: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
9b80: 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20 54 45 58 54  column_name TEXT
9b90: 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a   NOT NULL,\n");.
9ba0: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
9bb0: 20 22 6e 75 6c 6c 5f 76 61 6c 75 65 73 20 49 4e   "null_values IN
9bc0: 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c  TEGER NOT NULL,\
9bd0: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
9be0: 28 73 71 6c 2c 20 22 69 6e 74 65 67 65 72 5f 76  (sql, "integer_v
9bf0: 61 6c 75 65 73 20 49 4e 54 45 47 45 52 20 4e 4f  alues INTEGER NO
9c00: 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20  T NULL,\n");.   
9c10: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 64   strcat (sql, "d
9c20: 6f 75 62 6c 65 5f 76 61 6c 75 65 73 20 49 4e 54  ouble_values INT
9c30: 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  EGER NOT NULL,\n
9c40: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
9c50: 73 71 6c 2c 20 22 74 65 78 74 5f 76 61 6c 75 65  sql, "text_value
9c60: 73 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55  s INTEGER NOT NU
9c70: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
9c80: 63 61 74 20 28 73 71 6c 2c 20 22 62 6c 6f 62 5f  cat (sql, "blob_
9c90: 76 61 6c 75 65 73 20 49 4e 54 45 47 45 52 20 4e  values INTEGER N
9ca0: 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20  OT NULL,\n");.  
9cb0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
9cc0: 6d 61 78 5f 73 69 7a 65 20 49 4e 54 45 47 45 52  max_size INTEGER
9cd0: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
9ce0: 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41  t (sql, "CONSTRA
9cf0: 49 4e 54 20 70 6b 5f 76 77 67 63 66 6c 64 5f 69  INT pk_vwgcfld_i
9d00: 6e 66 6f 73 20 50 52 49 4d 41 52 59 20 4b 45 59  nfos PRIMARY KEY
9d10: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
9d20: 28 73 71 6c 2c 20 22 28 76 69 65 77 5f 6e 61 6d  (sql, "(view_nam
9d30: 65 2c 20 76 69 65 77 5f 67 65 6f 6d 65 74 72 79  e, view_geometry
9d40: 2c 20 6f 72 64 69 6e 61 6c 2c 20 63 6f 6c 75 6d  , ordinal, colum
9d50: 6e 5f 6e 61 6d 65 29 2c 5c 6e 22 29 3b 0a 20 20  n_name),\n");.  
9d60: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
9d70: 43 4f 4e 53 54 52 41 49 4e 54 20 66 6b 5f 76 77  CONSTRAINT fk_vw
9d80: 67 63 66 6c 64 5f 69 6e 66 6f 73 20 46 4f 52 45  gcfld_infos FORE
9d90: 49 47 4e 20 4b 45 59 20 22 29 3b 0a 20 20 20 20  IGN KEY ");.    
9da0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28 76  strcat (sql, "(v
9db0: 69 65 77 5f 6e 61 6d 65 2c 20 76 69 65 77 5f 67  iew_name, view_g
9dc0: 65 6f 6d 65 74 72 79 29 20 52 45 46 45 52 45 4e  eometry) REFEREN
9dd0: 43 45 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63  CES ");.    strc
9de0: 61 74 20 28 73 71 6c 2c 20 22 76 69 65 77 73 5f  at (sql, "views_
9df0: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73  geometry_columns
9e00: 20 28 76 69 65 77 5f 6e 61 6d 65 2c 20 76 69 65   (view_name, vie
9e10: 77 5f 67 65 6f 6d 65 74 72 79 29 20 22 29 3b 0a  w_geometry) ");.
9e20: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
9e30: 20 22 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43   "ON DELETE CASC
9e40: 41 44 45 29 22 29 3b 0a 20 20 20 20 72 65 74 20  ADE)");.    ret 
9e50: 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
9e60: 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c  sqlite, sql, NUL
9e70: 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
9e80: 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
9e90: 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
9ea0: 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
9eb0: 74 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f 72  te_e ("SQL error
9ec0: 3a 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71 6c  : %s: %s\n", sql
9ed0: 2c 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71  , errMsg);..  sq
9ee0: 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d  lite3_free (errM
9ef0: 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30  sg);..  return 0
9f00: 3b 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65  ;.      }./* cre
9f10: 61 74 69 6e 67 20 74 68 65 20 56 49 52 54 53 5f  ating the VIRTS_
9f20: 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e 53  GEOMETRY_COLUMNS
9f30: 5f 46 49 45 4c 44 5f 49 4e 46 4f 53 20 74 61 62  _FIELD_INFOS tab
9f40: 6c 65 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79  le */.    strcpy
9f50: 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54   (sql, "CREATE T
9f60: 41 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 53  ABLE IF NOT EXIS
9f70: 54 53 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  TS ");.    strca
9f80: 74 20 28 73 71 6c 2c 20 22 76 69 72 74 73 5f 67  t (sql, "virts_g
9f90: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f  eometry_columns_
9fa0: 66 69 65 6c 64 5f 69 6e 66 6f 73 20 28 5c 6e 22  field_infos (\n"
9fb0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
9fc0: 71 6c 2c 20 22 76 69 72 74 5f 6e 61 6d 65 20 54  ql, "virt_name T
9fd0: 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22  EXT NOT NULL,\n"
9fe0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
9ff0: 71 6c 2c 20 22 76 69 72 74 5f 67 65 6f 6d 65 74  ql, "virt_geomet
a000: 72 79 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c  ry TEXT NOT NULL
a010: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
a020: 74 20 28 73 71 6c 2c 20 22 6f 72 64 69 6e 61 6c  t (sql, "ordinal
a030: 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c   INTEGER NOT NUL
a040: 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  L,\n");.    strc
a050: 61 74 20 28 73 71 6c 2c 20 22 63 6f 6c 75 6d 6e  at (sql, "column
a060: 5f 6e 61 6d 65 20 54 45 58 54 20 4e 4f 54 20 4e  _name TEXT NOT N
a070: 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  ULL,\n");.    st
a080: 72 63 61 74 20 28 73 71 6c 2c 20 22 6e 75 6c 6c  rcat (sql, "null
a090: 5f 76 61 6c 75 65 73 20 49 4e 54 45 47 45 52 20  _values INTEGER 
a0a0: 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20  NOT NULL,\n");. 
a0b0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
a0c0: 22 69 6e 74 65 67 65 72 5f 76 61 6c 75 65 73 20  "integer_values 
a0d0: 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c  INTEGER NOT NULL
a0e0: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
a0f0: 74 20 28 73 71 6c 2c 20 22 64 6f 75 62 6c 65 5f  t (sql, "double_
a100: 76 61 6c 75 65 73 20 49 4e 54 45 47 45 52 20 4e  values INTEGER N
a110: 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20  OT NULL,\n");.  
a120: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
a130: 74 65 78 74 5f 76 61 6c 75 65 73 20 49 4e 54 45  text_values INTE
a140: 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22  GER NOT NULL,\n"
a150: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
a160: 71 6c 2c 20 22 62 6c 6f 62 5f 76 61 6c 75 65 73  ql, "blob_values
a170: 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c   INTEGER NOT NUL
a180: 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  L,\n");.    strc
a190: 61 74 20 28 73 71 6c 2c 20 22 6d 61 78 5f 73 69  at (sql, "max_si
a1a0: 7a 65 20 49 4e 54 45 47 45 52 2c 5c 6e 22 29 3b  ze INTEGER,\n");
a1b0: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
a1c0: 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54 20 70 6b  , "CONSTRAINT pk
a1d0: 5f 76 72 74 67 63 66 6c 64 5f 69 6e 66 6f 73 20  _vrtgcfld_infos 
a1e0: 50 52 49 4d 41 52 59 20 4b 45 59 20 22 29 3b 0a  PRIMARY KEY ");.
a1f0: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
a200: 20 22 28 76 69 72 74 5f 6e 61 6d 65 2c 20 76 69   "(virt_name, vi
a210: 72 74 5f 67 65 6f 6d 65 74 72 79 2c 20 6f 72 64  rt_geometry, ord
a220: 69 6e 61 6c 2c 20 63 6f 6c 75 6d 6e 5f 6e 61 6d  inal, column_nam
a230: 65 29 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  e),\n");.    str
a240: 63 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54  cat (sql, "CONST
a250: 52 41 49 4e 54 20 66 6b 5f 76 72 74 67 63 66 6c  RAINT fk_vrtgcfl
a260: 64 5f 69 6e 66 6f 73 20 46 4f 52 45 49 47 4e 20  d_infos FOREIGN 
a270: 4b 45 59 20 22 29 3b 0a 20 20 20 20 73 74 72 63  KEY ");.    strc
a280: 61 74 20 28 73 71 6c 2c 20 22 28 76 69 72 74 5f  at (sql, "(virt_
a290: 6e 61 6d 65 2c 20 76 69 72 74 5f 67 65 6f 6d 65  name, virt_geome
a2a0: 74 72 79 29 20 52 45 46 45 52 45 4e 43 45 53 20  try) REFERENCES 
a2b0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
a2c0: 73 71 6c 2c 20 22 76 69 72 74 73 5f 67 65 6f 6d  sql, "virts_geom
a2d0: 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20 28 76 69  etry_columns (vi
a2e0: 72 74 5f 6e 61 6d 65 2c 20 76 69 72 74 5f 67 65  rt_name, virt_ge
a2f0: 6f 6d 65 74 72 79 29 20 22 29 3b 0a 20 20 20 20  ometry) ");.    
a300: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4f 4e  strcat (sql, "ON
a310: 20 44 45 4c 45 54 45 20 43 41 53 43 41 44 45 29   DELETE CASCADE)
a320: 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71  ");.    ret = sq
a330: 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
a340: 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e  te, sql, NULL, N
a350: 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20  ULL, &errMsg);. 
a360: 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
a370: 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b  LITE_OK).      {
a380: 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
a390: 20 28 22 53 51 4c 20 65 72 72 6f 72 3a 20 25 73   ("SQL error: %s
a3a0: 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 2c 20 65 72  : %s\n", sql, er
a3b0: 72 4d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65  rMsg);..  sqlite
a3c0: 33 5f 66 72 65 65 20 28 65 72 72 4d 73 67 29 3b  3_free (errMsg);
a3d0: 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ..  return 0;.  
a3e0: 20 20 20 20 7d 0a 2f 2a 20 63 72 65 61 74 69 6e      }./* creatin
a3f0: 67 20 74 68 65 20 56 49 52 54 53 5f 47 45 4f 4d  g the VIRTS_GEOM
a400: 45 54 52 59 5f 43 4f 4c 55 4d 4e 53 5f 54 49 4d  ETRY_COLUMNS_TIM
a410: 45 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 73  E table */.    s
a420: 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45  trcpy (sql, "CRE
a430: 41 54 45 20 54 41 42 4c 45 20 49 46 20 4e 4f 54  ATE TABLE IF NOT
a440: 20 45 58 49 53 54 53 20 22 29 3b 0a 20 20 20 20   EXISTS ");.    
a450: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 67 65  strcat (sql, "ge
a460: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 74  ometry_columns_t
a470: 69 6d 65 20 28 5c 6e 22 29 3b 0a 20 20 20 20 73  ime (\n");.    s
a480: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66 5f 74  trcat (sql, "f_t
a490: 61 62 6c 65 5f 6e 61 6d 65 20 54 45 58 54 20 4e  able_name TEXT N
a4a0: 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20  OT NULL,\n");.  
a4b0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
a4c0: 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  f_geometry_colum
a4d0: 6e 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c  n TEXT NOT NULL,
a4e0: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
a4f0: 20 28 73 71 6c 2c 0a 09 20 20 20 20 22 6c 61 73   (sql,..    "las
a500: 74 5f 69 6e 73 65 72 74 20 54 49 4d 45 53 54 41  t_insert TIMESTA
a510: 4d 50 20 4e 4f 54 20 4e 55 4c 4c 20 44 45 46 41  MP NOT NULL DEFA
a520: 55 4c 54 20 27 30 30 30 30 2d 30 31 2d 30 31 20  ULT '0000-01-01 
a530: 30 30 3a 30 30 3a 30 30 27 2c 5c 6e 22 29 3b 0a  00:00:00',\n");.
a540: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
a550: 0a 09 20 20 20 20 22 6c 61 73 74 5f 75 70 64 61  ..    "last_upda
a560: 74 65 20 54 49 4d 45 53 54 41 4d 50 20 4e 4f 54  te TIMESTAMP NOT
a570: 20 4e 55 4c 4c 20 44 45 46 41 55 4c 54 20 27 30   NULL DEFAULT '0
a580: 30 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a  000-01-01 00:00:
a590: 30 30 27 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  00',\n");.    st
a5a0: 72 63 61 74 20 28 73 71 6c 2c 0a 09 20 20 20 20  rcat (sql,..    
a5b0: 22 6c 61 73 74 5f 64 65 6c 65 74 65 20 54 49 4d  "last_delete TIM
a5c0: 45 53 54 41 4d 50 20 4e 4f 54 20 4e 55 4c 4c 20  ESTAMP NOT NULL 
a5d0: 44 45 46 41 55 4c 54 20 27 30 30 30 30 2d 30 31  DEFAULT '0000-01
a5e0: 2d 30 31 20 30 30 3a 30 30 3a 30 30 27 2c 5c 6e  -01 00:00:00',\n
a5f0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
a600: 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54  sql, "CONSTRAINT
a610: 20 70 6b 5f 67 63 5f 74 69 6d 65 20 50 52 49 4d   pk_gc_time PRIM
a620: 41 52 59 20 4b 45 59 20 22 29 3b 0a 20 20 20 20  ARY KEY ");.    
a630: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28 66  strcat (sql, "(f
a640: 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67  _table_name, f_g
a650: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 2c  eometry_column),
a660: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
a670: 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49   (sql, "CONSTRAI
a680: 4e 54 20 66 6b 5f 67 63 5f 74 69 6d 65 20 46 4f  NT fk_gc_time FO
a690: 52 45 49 47 4e 20 4b 45 59 20 22 29 3b 0a 20 20  REIGN KEY ");.  
a6a0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
a6b0: 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66  (f_table_name, f
a6c0: 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
a6d0: 29 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ) ");.    strcat
a6e0: 20 28 73 71 6c 2c 20 22 52 45 46 45 52 45 4e 43   (sql, "REFERENC
a6f0: 45 53 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  ES geometry_colu
a700: 6d 6e 73 20 22 29 3b 0a 20 20 20 20 73 74 72 63  mns ");.    strc
a710: 61 74 20 28 73 71 6c 2c 20 22 28 66 5f 74 61 62  at (sql, "(f_tab
a720: 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65  le_name, f_geome
a730: 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 22 29 3b 0a  try_column) ");.
a740: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
a750: 20 22 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43   "ON DELETE CASC
a760: 41 44 45 29 22 29 3b 0a 20 20 20 20 72 65 74 20  ADE)");.    ret 
a770: 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
a780: 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c  sqlite, sql, NUL
a790: 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
a7a0: 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
a7b0: 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
a7c0: 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
a7d0: 74 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f 72  te_e ("SQL error
a7e0: 3a 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71 6c  : %s: %s\n", sql
a7f0: 2c 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71  , errMsg);..  sq
a800: 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d  lite3_free (errM
a810: 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30  sg);..  return 0
a820: 3b 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65  ;.      }./* cre
a830: 61 74 69 6e 67 20 74 68 65 20 47 45 4f 4d 45 54  ating the GEOMET
a840: 52 59 5f 43 4f 4c 55 4d 4e 53 5f 41 55 54 48 20  RY_COLUMNS_AUTH 
a850: 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 73 74 72  table */.    str
a860: 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45 41 54  cpy (sql, "CREAT
a870: 45 20 54 41 42 4c 45 20 49 46 20 4e 4f 54 20 45  E TABLE IF NOT E
a880: 58 49 53 54 53 20 22 29 3b 0a 20 20 20 20 73 74  XISTS ");.    st
a890: 72 63 61 74 20 28 73 71 6c 2c 20 22 67 65 6f 6d  rcat (sql, "geom
a8a0: 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 61 75 74  etry_columns_aut
a8b0: 68 20 28 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  h (\n");.    str
a8c0: 63 61 74 20 28 73 71 6c 2c 20 22 66 5f 74 61 62  cat (sql, "f_tab
a8d0: 6c 65 5f 6e 61 6d 65 20 54 45 58 54 20 4e 4f 54  le_name TEXT NOT
a8e0: 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20   NULL,\n");.    
a8f0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66 5f  strcat (sql, "f_
a900: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 20  geometry_column 
a910: 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  TEXT NOT NULL,\n
a920: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
a930: 73 71 6c 2c 20 22 72 65 61 64 5f 6f 6e 6c 79 20  sql, "read_only 
a940: 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c  INTEGER NOT NULL
a950: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
a960: 74 20 28 73 71 6c 2c 20 22 68 69 64 64 65 6e 20  t (sql, "hidden 
a970: 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c  INTEGER NOT NULL
a980: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
a990: 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41  t (sql, "CONSTRA
a9a0: 49 4e 54 20 70 6b 5f 67 63 5f 61 75 74 68 20 50  INT pk_gc_auth P
a9b0: 52 49 4d 41 52 59 20 4b 45 59 20 22 29 3b 0a 20  RIMARY KEY ");. 
a9c0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
a9d0: 22 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20  "(f_table_name, 
a9e0: 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  f_geometry_colum
a9f0: 6e 29 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  n),\n");.    str
aa00: 63 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54  cat (sql, "CONST
aa10: 52 41 49 4e 54 20 66 6b 5f 67 63 5f 61 75 74 68  RAINT fk_gc_auth
aa20: 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 22 29 3b   FOREIGN KEY ");
aa30: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
aa40: 2c 20 22 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  , "(f_table_name
aa50: 2c 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  , f_geometry_col
aa60: 75 6d 6e 29 20 22 29 3b 0a 20 20 20 20 73 74 72  umn) ");.    str
aa70: 63 61 74 20 28 73 71 6c 2c 20 22 52 45 46 45 52  cat (sql, "REFER
aa80: 45 4e 43 45 53 20 67 65 6f 6d 65 74 72 79 5f 63  ENCES geometry_c
aa90: 6f 6c 75 6d 6e 73 20 22 29 3b 0a 20 20 20 20 73  olumns ");.    s
aaa0: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28 66 5f  trcat (sql, "(f_
aab0: 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65  table_name, f_ge
aac0: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 22  ometry_column) "
aad0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
aae0: 71 6c 2c 20 22 4f 4e 20 44 45 4c 45 54 45 20 43  ql, "ON DELETE C
aaf0: 41 53 43 41 44 45 2c 5c 6e 22 29 3b 0a 20 20 20  ASCADE,\n");.   
ab00: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43   strcat (sql, "C
ab10: 4f 4e 53 54 52 41 49 4e 54 20 63 6b 5f 67 63 5f  ONSTRAINT ck_gc_
ab20: 72 6f 6e 6c 79 20 43 48 45 43 4b 20 28 72 65 61  ronly CHECK (rea
ab30: 64 5f 6f 6e 6c 79 20 49 4e 20 22 29 3b 0a 20 20  d_only IN ");.  
ab40: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
ab50: 28 30 2c 31 29 29 2c 5c 6e 22 29 3b 0a 20 20 20  (0,1)),\n");.   
ab60: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43   strcat (sql, "C
ab70: 4f 4e 53 54 52 41 49 4e 54 20 63 6b 5f 67 63 5f  ONSTRAINT ck_gc_
ab80: 68 69 64 64 65 6e 20 43 48 45 43 4b 20 28 68 69  hidden CHECK (hi
ab90: 64 64 65 6e 20 49 4e 20 22 29 3b 0a 20 20 20 20  dden IN ");.    
aba0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28 30  strcat (sql, "(0
abb0: 2c 31 29 29 29 22 29 3b 0a 20 20 20 20 72 65 74  ,1)))");.    ret
abc0: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
abd0: 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
abe0: 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
abf0: 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
ac00: 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20  != SQLITE_OK).  
ac10: 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c      {..  spatial
ac20: 69 74 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f  ite_e ("SQL erro
ac30: 72 3a 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71  r: %s: %s\n", sq
ac40: 6c 2c 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73  l, errMsg);..  s
ac50: 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72  qlite3_free (err
ac60: 4d 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20  Msg);..  return 
ac70: 30 3b 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72  0;.      }./* cr
ac80: 65 61 74 69 6e 67 20 74 68 65 20 56 49 45 57 53  eating the VIEWS
ac90: 5f 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e  _GEOMETRY_COLUMN
aca0: 53 5f 41 55 54 48 20 74 61 62 6c 65 20 2a 2f 0a  S_AUTH table */.
acb0: 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 2c      strcpy (sql,
acc0: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 49   "CREATE TABLE I
acd0: 46 20 4e 4f 54 20 45 58 49 53 54 53 20 22 29 3b  F NOT EXISTS ");
ace0: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
acf0: 2c 20 22 76 69 65 77 73 5f 67 65 6f 6d 65 74 72  , "views_geometr
ad00: 79 5f 63 6f 6c 75 6d 6e 73 5f 61 75 74 68 20 28  y_columns_auth (
ad10: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
ad20: 20 28 73 71 6c 2c 20 22 76 69 65 77 5f 6e 61 6d   (sql, "view_nam
ad30: 65 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c  e TEXT NOT NULL,
ad40: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
ad50: 20 28 73 71 6c 2c 20 22 76 69 65 77 5f 67 65 6f   (sql, "view_geo
ad60: 6d 65 74 72 79 20 54 45 58 54 20 4e 4f 54 20 4e  metry TEXT NOT N
ad70: 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  ULL,\n");.    st
ad80: 72 63 61 74 20 28 73 71 6c 2c 20 22 68 69 64 64  rcat (sql, "hidd
ad90: 65 6e 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e  en INTEGER NOT N
ada0: 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  ULL,\n");.    st
adb0: 72 63 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53  rcat (sql, "CONS
adc0: 54 52 41 49 4e 54 20 70 6b 5f 76 77 67 63 5f 61  TRAINT pk_vwgc_a
add0: 75 74 68 20 50 52 49 4d 41 52 59 20 4b 45 59 20  uth PRIMARY KEY 
ade0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
adf0: 73 71 6c 2c 20 22 28 76 69 65 77 5f 6e 61 6d 65  sql, "(view_name
ae00: 2c 20 76 69 65 77 5f 67 65 6f 6d 65 74 72 79 29  , view_geometry)
ae10: 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
ae20: 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41  t (sql, "CONSTRA
ae30: 49 4e 54 20 66 6b 5f 76 77 67 63 5f 61 75 74 68  INT fk_vwgc_auth
ae40: 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 22 29 3b   FOREIGN KEY ");
ae50: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
ae60: 2c 20 22 28 76 69 65 77 5f 6e 61 6d 65 2c 20 76  , "(view_name, v
ae70: 69 65 77 5f 67 65 6f 6d 65 74 72 79 29 20 22 29  iew_geometry) ")
ae80: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
ae90: 6c 2c 20 22 52 45 46 45 52 45 4e 43 45 53 20 76  l, "REFERENCES v
aea0: 69 65 77 73 5f 67 65 6f 6d 65 74 72 79 5f 63 6f  iews_geometry_co
aeb0: 6c 75 6d 6e 73 20 22 29 3b 0a 20 20 20 20 73 74  lumns ");.    st
aec0: 72 63 61 74 20 28 73 71 6c 2c 20 22 28 76 69 65  rcat (sql, "(vie
aed0: 77 5f 6e 61 6d 65 2c 20 76 69 65 77 5f 67 65 6f  w_name, view_geo
aee0: 6d 65 74 72 79 29 20 22 29 3b 0a 20 20 20 20 73  metry) ");.    s
aef0: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4f 4e 20  trcat (sql, "ON 
af00: 44 45 4c 45 54 45 20 43 41 53 43 41 44 45 2c 5c  DELETE CASCADE,\
af10: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
af20: 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e  (sql, "CONSTRAIN
af30: 54 20 63 6b 5f 76 77 67 63 5f 68 69 64 64 65 6e  T ck_vwgc_hidden
af40: 20 43 48 45 43 4b 20 28 68 69 64 64 65 6e 20 49   CHECK (hidden I
af50: 4e 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  N ");.    strcat
af60: 20 28 73 71 6c 2c 20 22 28 30 2c 31 29 29 29 22   (sql, "(0,1)))"
af70: 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c  );.    ret = sql
af80: 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74  ite3_exec (sqlit
af90: 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55  e, sql, NULL, NU
afa0: 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20  LL, &errMsg);.  
afb0: 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
afc0: 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a  ITE_OK).      {.
afd0: 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20  .  spatialite_e 
afe0: 28 22 53 51 4c 20 65 72 72 6f 72 3a 20 25 73 3a  ("SQL error: %s:
aff0: 20 25 73 5c 6e 22 2c 20 73 71 6c 2c 20 65 72 72   %s\n", sql, err
b000: 4d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  Msg);..  sqlite3
b010: 5f 66 72 65 65 20 28 65 72 72 4d 73 67 29 3b 0a  _free (errMsg);.
b020: 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20  .  return 0;.   
b030: 20 20 20 7d 0a 2f 2a 20 63 72 65 61 74 69 6e 67     }./* creating
b040: 20 74 68 65 20 56 49 52 54 53 5f 47 45 4f 4d 45   the VIRTS_GEOME
b050: 54 52 59 5f 43 4f 4c 55 4d 4e 53 5f 41 55 54 48  TRY_COLUMNS_AUTH
b060: 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 73 74   table */.    st
b070: 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45 41  rcpy (sql, "CREA
b080: 54 45 20 54 41 42 4c 45 20 49 46 20 4e 4f 54 20  TE TABLE IF NOT 
b090: 45 58 49 53 54 53 20 22 29 3b 0a 20 20 20 20 73  EXISTS ");.    s
b0a0: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 76 69 72  trcat (sql, "vir
b0b0: 74 73 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  ts_geometry_colu
b0c0: 6d 6e 73 5f 61 75 74 68 20 28 5c 6e 22 29 3b 0a  mns_auth (\n");.
b0d0: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
b0e0: 20 22 76 69 72 74 5f 6e 61 6d 65 20 54 45 58 54   "virt_name TEXT
b0f0: 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a   NOT NULL,\n");.
b100: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
b110: 20 22 76 69 72 74 5f 67 65 6f 6d 65 74 72 79 20   "virt_geometry 
b120: 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  TEXT NOT NULL,\n
b130: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
b140: 73 71 6c 2c 20 22 68 69 64 64 65 6e 20 49 4e 54  sql, "hidden INT
b150: 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  EGER NOT NULL,\n
b160: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
b170: 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54  sql, "CONSTRAINT
b180: 20 70 6b 5f 76 72 74 67 63 5f 61 75 74 68 20 50   pk_vrtgc_auth P
b190: 52 49 4d 41 52 59 20 4b 45 59 20 22 29 3b 0a 20  RIMARY KEY ");. 
b1a0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
b1b0: 22 28 76 69 72 74 5f 6e 61 6d 65 2c 20 76 69 72  "(virt_name, vir
b1c0: 74 5f 67 65 6f 6d 65 74 72 79 29 2c 5c 6e 22 29  t_geometry),\n")
b1d0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
b1e0: 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54 20 66  l, "CONSTRAINT f
b1f0: 6b 5f 76 72 74 67 63 5f 61 75 74 68 20 46 4f 52  k_vrtgc_auth FOR
b200: 45 49 47 4e 20 4b 45 59 20 22 29 3b 0a 20 20 20  EIGN KEY ");.   
b210: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28   strcat (sql, "(
b220: 76 69 72 74 5f 6e 61 6d 65 2c 20 76 69 72 74 5f  virt_name, virt_
b230: 67 65 6f 6d 65 74 72 79 29 20 22 29 3b 0a 20 20  geometry) ");.  
b240: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
b250: 52 45 46 45 52 45 4e 43 45 53 20 76 69 72 74 73  REFERENCES virts
b260: 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
b270: 73 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  s ");.    strcat
b280: 20 28 73 71 6c 2c 20 22 28 76 69 72 74 5f 6e 61   (sql, "(virt_na
b290: 6d 65 2c 20 76 69 72 74 5f 67 65 6f 6d 65 74 72  me, virt_geometr
b2a0: 79 29 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  y) ");.    strca
b2b0: 74 20 28 73 71 6c 2c 20 22 4f 4e 20 44 45 4c 45  t (sql, "ON DELE
b2c0: 54 45 20 43 41 53 43 41 44 45 2c 5c 6e 22 29 3b  TE CASCADE,\n");
b2d0: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
b2e0: 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54 20 63 6b  , "CONSTRAINT ck
b2f0: 5f 76 72 74 67 63 5f 68 69 64 64 65 6e 20 43 48  _vrtgc_hidden CH
b300: 45 43 4b 20 28 68 69 64 64 65 6e 20 49 4e 20 22  ECK (hidden IN "
b310: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
b320: 71 6c 2c 20 22 28 30 2c 31 29 29 29 22 29 3b 0a  ql, "(0,1)))");.
b330: 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65      ret = sqlite
b340: 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20  3_exec (sqlite, 
b350: 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c  sql, NULL, NULL,
b360: 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69   &errMsg);.    i
b370: 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
b380: 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  _OK).      {..  
b390: 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 53  spatialite_e ("S
b3a0: 51 4c 20 65 72 72 6f 72 3a 20 25 73 3a 20 25 73  QL error: %s: %s
b3b0: 5c 6e 22 2c 20 73 71 6c 2c 20 65 72 72 4d 73 67  \n", sql, errMsg
b3c0: 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
b3d0: 65 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20  ee (errMsg);..  
b3e0: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
b3f0: 7d 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68  }./* creating th
b400: 65 20 56 45 43 54 4f 52 5f 4c 41 59 45 52 53 20  e VECTOR_LAYERS 
b410: 76 69 65 77 20 2a 2f 0a 20 20 20 20 73 74 72 63  view */.    strc
b420: 70 79 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45  py (sql, "CREATE
b430: 20 56 49 45 57 20 76 65 63 74 6f 72 5f 6c 61 79   VIEW vector_lay
b440: 65 72 73 20 41 53 5c 6e 22 29 3b 0a 20 20 20 20  ers AS\n");.    
b450: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45  strcat (sql, "SE
b460: 4c 45 43 54 20 27 53 70 61 74 69 61 6c 54 61 62  LECT 'SpatialTab
b470: 6c 65 27 20 41 53 20 6c 61 79 65 72 5f 74 79 70  le' AS layer_typ
b480: 65 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  e, ");.    strca
b490: 74 20 28 73 71 6c 2c 20 22 66 5f 74 61 62 6c 65  t (sql, "f_table
b4a0: 5f 6e 61 6d 65 20 41 53 20 74 61 62 6c 65 5f 6e  _name AS table_n
b4b0: 61 6d 65 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  ame, ");.    str
b4c0: 63 61 74 20 28 73 71 6c 2c 20 22 66 5f 67 65 6f  cat (sql, "f_geo
b4d0: 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 20 41 53 20  metry_column AS 
b4e0: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 2c  geometry_column,
b4f0: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
b500: 28 73 71 6c 2c 20 22 67 65 6f 6d 65 74 72 79 5f  (sql, "geometry_
b510: 74 79 70 65 20 41 53 20 67 65 6f 6d 65 74 72 79  type AS geometry
b520: 5f 74 79 70 65 2c 20 22 29 3b 0a 20 20 20 20 73  _type, ");.    s
b530: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 63 6f 6f  trcat (sql, "coo
b540: 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 20 41 53 20  rd_dimension AS 
b550: 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 2c  coord_dimension,
b560: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
b570: 28 73 71 6c 2c 20 22 73 72 69 64 20 41 53 20 73  (sql, "srid AS s
b580: 72 69 64 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  rid, ");.    str
b590: 63 61 74 20 28 73 71 6c 2c 20 22 73 70 61 74 69  cat (sql, "spati
b5a0: 61 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64  al_index_enabled
b5b0: 20 41 53 20 73 70 61 74 69 61 6c 5f 69 6e 64 65   AS spatial_inde
b5c0: 78 5f 65 6e 61 62 6c 65 64 5c 6e 22 29 3b 0a 20  x_enabled\n");. 
b5d0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
b5e0: 22 46 52 4f 4d 20 67 65 6f 6d 65 74 72 79 5f 63  "FROM geometry_c
b5f0: 6f 6c 75 6d 6e 73 5c 6e 22 29 3b 0a 20 20 20 20  olumns\n");.    
b600: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 55 4e  strcat (sql, "UN
b610: 49 4f 4e 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  ION\n");.    str
b620: 63 61 74 20 28 73 71 6c 2c 20 22 53 45 4c 45 43  cat (sql, "SELEC
b630: 54 20 27 53 70 61 74 69 61 6c 56 69 65 77 27 20  T 'SpatialView' 
b640: 41 53 20 6c 61 79 65 72 5f 74 79 70 65 2c 20 22  AS layer_type, "
b650: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
b660: 71 6c 2c 20 22 61 2e 76 69 65 77 5f 6e 61 6d 65  ql, "a.view_name
b670: 20 41 53 20 74 61 62 6c 65 5f 6e 61 6d 65 2c 20   AS table_name, 
b680: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
b690: 73 71 6c 2c 20 22 61 2e 76 69 65 77 5f 67 65 6f  sql, "a.view_geo
b6a0: 6d 65 74 72 79 20 41 53 20 67 65 6f 6d 65 74 72  metry AS geometr
b6b0: 79 5f 63 6f 6c 75 6d 6e 2c 20 22 29 3b 0a 20 20  y_column, ");.  
b6c0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
b6d0: 62 2e 67 65 6f 6d 65 74 72 79 5f 74 79 70 65 20  b.geometry_type 
b6e0: 41 53 20 67 65 6f 6d 65 74 72 79 5f 74 79 70 65  AS geometry_type
b6f0: 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
b700: 20 28 73 71 6c 2c 20 22 62 2e 63 6f 6f 72 64 5f   (sql, "b.coord_
b710: 64 69 6d 65 6e 73 69 6f 6e 20 41 53 20 63 6f 6f  dimension AS coo
b720: 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 2c 20 22 29  rd_dimension, ")
b730: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
b740: 6c 2c 20 22 62 2e 73 72 69 64 20 41 53 20 73 72  l, "b.srid AS sr
b750: 69 64 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63  id, ");.    strc
b760: 61 74 20 28 73 71 6c 2c 20 22 62 2e 73 70 61 74  at (sql, "b.spat
b770: 69 61 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65  ial_index_enable
b780: 64 20 41 53 20 73 70 61 74 69 61 6c 5f 69 6e 64  d AS spatial_ind
b790: 65 78 5f 65 6e 61 62 6c 65 64 5c 6e 22 29 3b 0a  ex_enabled\n");.
b7a0: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
b7b0: 20 22 46 52 4f 4d 20 76 69 65 77 73 5f 67 65 6f   "FROM views_geo
b7c0: 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20 41 53  metry_columns AS
b7d0: 20 61 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63   a\n");.    strc
b7e0: 61 74 20 28 73 71 6c 2c 20 22 4c 45 46 54 20 4a  at (sql, "LEFT J
b7f0: 4f 49 4e 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  OIN geometry_col
b800: 75 6d 6e 73 20 41 53 20 62 20 4f 4e 20 28 22 29  umns AS b ON (")
b810: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
b820: 6c 2c 20 22 55 70 70 65 72 28 61 2e 66 5f 74 61  l, "Upper(a.f_ta
b830: 62 6c 65 5f 6e 61 6d 65 29 20 3d 20 55 70 70 65  ble_name) = Uppe
b840: 72 28 62 2e 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  r(b.f_table_name
b850: 29 20 41 4e 44 20 22 29 3b 0a 20 20 20 20 73 74  ) AND ");.    st
b860: 72 63 61 74 20 28 73 71 6c 2c 20 22 55 70 70 65  rcat (sql, "Uppe
b870: 72 28 61 2e 66 5f 67 65 6f 6d 65 74 72 79 5f 63  r(a.f_geometry_c
b880: 6f 6c 75 6d 6e 29 20 3d 20 55 70 70 65 72 28 62  olumn) = Upper(b
b890: 2e 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  .f_geometry_colu
b8a0: 6d 6e 29 29 5c 6e 22 29 3b 0a 20 20 20 20 73 74  mn))\n");.    st
b8b0: 72 63 61 74 20 28 73 71 6c 2c 20 22 55 4e 49 4f  rcat (sql, "UNIO
b8c0: 4e 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  N\n");.    strca
b8d0: 74 20 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20  t (sql, "SELECT 
b8e0: 27 56 69 72 74 75 61 6c 53 68 61 70 65 27 20 41  'VirtualShape' A
b8f0: 53 20 6c 61 79 65 72 5f 74 79 70 65 2c 20 22 29  S layer_type, ")
b900: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
b910: 6c 2c 20 22 76 69 72 74 5f 6e 61 6d 65 20 41 53  l, "virt_name AS
b920: 20 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 22 29 3b   table_name, ");
b930: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
b940: 2c 20 22 76 69 72 74 5f 67 65 6f 6d 65 74 72 79  , "virt_geometry
b950: 20 41 53 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c   AS geometry_col
b960: 75 6d 6e 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  umn, ");.    str
b970: 63 61 74 20 28 73 71 6c 2c 20 22 67 65 6f 6d 65  cat (sql, "geome
b980: 74 72 79 5f 74 79 70 65 20 41 53 20 67 65 6f 6d  try_type AS geom
b990: 65 74 72 79 5f 74 79 70 65 2c 20 22 29 3b 0a 20  etry_type, ");. 
b9a0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
b9b0: 22 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e  "coord_dimension
b9c0: 20 41 53 20 63 6f 6f 72 64 5f 64 69 6d 65 6e 73   AS coord_dimens
b9d0: 69 6f 6e 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  ion, ");.    str
b9e0: 63 61 74 20 28 73 71 6c 2c 20 22 73 72 69 64 20  cat (sql, "srid 
b9f0: 41 53 20 73 72 69 64 2c 20 22 29 3b 0a 20 20 20  AS srid, ");.   
ba00: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 30   strcat (sql, "0
ba10: 20 41 53 20 73 70 61 74 69 61 6c 5f 69 6e 64 65   AS spatial_inde
ba20: 78 5f 65 6e 61 62 6c 65 64 5c 6e 22 29 3b 0a 20  x_enabled\n");. 
ba30: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
ba40: 22 46 52 4f 4d 20 76 69 72 74 73 5f 67 65 6f 6d  "FROM virts_geom
ba50: 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 22 29 3b 0a  etry_columns");.
ba60: 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65      ret = sqlite
ba70: 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20  3_exec (sqlite, 
ba80: 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c  sql, NULL, NULL,
ba90: 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69   &errMsg);.    i
baa0: 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
bab0: 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  _OK).      {..  
bac0: 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 53  spatialite_e ("S
bad0: 51 4c 20 65 72 72 6f 72 3a 20 25 73 3a 20 25 73  QL error: %s: %s
bae0: 5c 6e 22 2c 20 73 71 6c 2c 20 65 72 72 4d 73 67  \n", sql, errMsg
baf0: 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
bb00: 65 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20  ee (errMsg);..  
bb10: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
bb20: 7d 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68  }./* creating th
bb30: 65 20 56 45 43 54 4f 52 5f 4c 41 59 45 52 53 5f  e VECTOR_LAYERS_
bb40: 41 55 54 48 20 76 69 65 77 20 2a 2f 0a 20 20 20  AUTH view */.   
bb50: 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43   strcpy (sql, "C
bb60: 52 45 41 54 45 20 56 49 45 57 20 76 65 63 74 6f  REATE VIEW vecto
bb70: 72 5f 6c 61 79 65 72 73 5f 61 75 74 68 20 41 53  r_layers_auth AS
bb80: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
bb90: 20 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 27   (sql, "SELECT '
bba0: 53 70 61 74 69 61 6c 54 61 62 6c 65 27 20 41 53  SpatialTable' AS
bbb0: 20 6c 61 79 65 72 5f 74 79 70 65 2c 20 22 29 3b   layer_type, ");
bbc0: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
bbd0: 2c 20 22 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20  , "f_table_name 
bbe0: 41 53 20 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 22  AS table_name, "
bbf0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
bc00: 71 6c 2c 20 22 66 5f 67 65 6f 6d 65 74 72 79 5f  ql, "f_geometry_
bc10: 63 6f 6c 75 6d 6e 20 41 53 20 67 65 6f 6d 65 74  column AS geomet
bc20: 72 79 5f 63 6f 6c 75 6d 6e 2c 20 22 29 3b 0a 20  ry_column, ");. 
bc30: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
bc40: 22 72 65 61 64 5f 6f 6e 6c 79 20 41 53 20 72 65  "read_only AS re
bc50: 61 64 5f 6f 6e 6c 79 2c 20 22 29 3b 0a 20 20 20  ad_only, ");.   
bc60: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 68   strcat (sql, "h
bc70: 69 64 64 65 6e 20 41 53 20 68 69 64 64 65 6e 5c  idden AS hidden\
bc80: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
bc90: 28 73 71 6c 2c 20 22 46 52 4f 4d 20 67 65 6f 6d  (sql, "FROM geom
bca0: 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 61 75 74  etry_columns_aut
bcb0: 68 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  h\n");.    strca
bcc0: 74 20 28 73 71 6c 2c 20 22 55 4e 49 4f 4e 5c 6e  t (sql, "UNION\n
bcd0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
bce0: 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 27 53 70  sql, "SELECT 'Sp
bcf0: 61 74 69 61 6c 56 69 65 77 27 20 41 53 20 6c 61  atialView' AS la
bd00: 79 65 72 5f 74 79 70 65 2c 20 22 29 3b 0a 20 20  yer_type, ");.  
bd10: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
bd20: 61 2e 76 69 65 77 5f 6e 61 6d 65 20 41 53 20 74  a.view_name AS t
bd30: 61 62 6c 65 5f 6e 61 6d 65 2c 20 22 29 3b 0a 20  able_name, ");. 
bd40: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
bd50: 22 61 2e 76 69 65 77 5f 67 65 6f 6d 65 74 72 79  "a.view_geometry
bd60: 20 41 53 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c   AS geometry_col
bd70: 75 6d 6e 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  umn, ");.    str
bd80: 63 61 74 20 28 73 71 6c 2c 20 22 62 2e 72 65 61  cat (sql, "b.rea
bd90: 64 5f 6f 6e 6c 79 20 41 53 20 72 65 61 64 5f 6f  d_only AS read_o
bda0: 6e 6c 79 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  nly, ");.    str
bdb0: 63 61 74 20 28 73 71 6c 2c 20 22 61 2e 68 69 64  cat (sql, "a.hid
bdc0: 64 65 6e 20 41 53 20 68 69 64 64 65 6e 5c 6e 22  den AS hidden\n"
bdd0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
bde0: 71 6c 2c 20 22 46 52 4f 4d 20 76 69 65 77 73 5f  ql, "FROM views_
bdf0: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73  geometry_columns
be00: 5f 61 75 74 68 20 41 53 20 61 5c 6e 22 29 3b 0a  _auth AS a\n");.
be10: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
be20: 20 22 4a 4f 49 4e 20 76 69 65 77 73 5f 67 65 6f   "JOIN views_geo
be30: 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20 41 53  metry_columns AS
be40: 20 62 20 4f 4e 20 28 22 29 3b 0a 20 20 20 20 73   b ON (");.    s
be50: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 55 70 70  trcat (sql, "Upp
be60: 65 72 28 61 2e 76 69 65 77 5f 6e 61 6d 65 29 20  er(a.view_name) 
be70: 3d 20 55 70 70 65 72 28 62 2e 76 69 65 77 5f 6e  = Upper(b.view_n
be80: 61 6d 65 29 20 41 4e 44 20 22 29 3b 0a 20 20 20  ame) AND ");.   
be90: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 55   strcat (sql, "U
bea0: 70 70 65 72 28 61 2e 76 69 65 77 5f 67 65 6f 6d  pper(a.view_geom
beb0: 65 74 72 79 29 20 3d 20 55 70 70 65 72 28 62 2e  etry) = Upper(b.
bec0: 76 69 65 77 5f 67 65 6f 6d 65 74 72 79 29 29 5c  view_geometry))\
bed0: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
bee0: 28 73 71 6c 2c 20 22 55 4e 49 4f 4e 5c 6e 22 29  (sql, "UNION\n")
bef0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
bf00: 6c 2c 20 22 53 45 4c 45 43 54 20 27 56 69 72 74  l, "SELECT 'Virt
bf10: 75 61 6c 53 68 61 70 65 27 20 41 53 20 6c 61 79  ualShape' AS lay
bf20: 65 72 5f 74 79 70 65 2c 20 22 29 3b 0a 20 20 20  er_type, ");.   
bf30: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 76   strcat (sql, "v
bf40: 69 72 74 5f 6e 61 6d 65 20 41 53 20 74 61 62 6c  irt_name AS tabl
bf50: 65 5f 6e 61 6d 65 2c 20 22 29 3b 0a 20 20 20 20  e_name, ");.    
bf60: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 76 69  strcat (sql, "vi
bf70: 72 74 5f 67 65 6f 6d 65 74 72 79 20 41 53 20 67  rt_geometry AS g
bf80: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 2c 20  eometry_column, 
bf90: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
bfa0: 73 71 6c 2c 20 22 31 20 41 53 20 72 65 61 64 5f  sql, "1 AS read_
bfb0: 6f 6e 6c 79 2c 20 22 29 3b 0a 20 20 20 20 73 74  only, ");.    st
bfc0: 72 63 61 74 20 28 73 71 6c 2c 20 22 68 69 64 64  rcat (sql, "hidd
bfd0: 65 6e 20 41 53 20 68 69 64 64 65 6e 5c 6e 22 29  en AS hidden\n")
bfe0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
bff0: 6c 2c 20 22 46 52 4f 4d 20 76 69 72 74 73 5f 67  l, "FROM virts_g
c000: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f  eometry_columns_
c010: 61 75 74 68 22 29 3b 0a 20 20 20 20 72 65 74 20  auth");.    ret 
c020: 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
c030: 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c  sqlite, sql, NUL
c040: 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
c050: 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
c060: 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
c070: 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
c080: 74 65 5f 65 20 28 22 53 51 4c 20 65 72 72 6f 72  te_e ("SQL error
c090: 3a 20 25 73 3a 20 25 73 5c 6e 22 2c 20 73 71 6c  : %s: %s\n", sql
c0a0: 2c 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71  , errMsg);..  sq
c0b0: 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d  lite3_free (errM
c0c0: 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30  sg);..  return 0
c0d0: 3b 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65  ;.      }./* cre
c0e0: 61 74 69 6e 67 20 74 68 65 20 56 45 43 54 4f 52  ating the VECTOR
c0f0: 5f 4c 41 59 45 52 53 5f 53 54 41 54 49 53 54 49  _LAYERS_STATISTI
c100: 43 53 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 73  CS view */.    s
c110: 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45  trcpy (sql, "CRE
c120: 41 54 45 20 56 49 45 57 20 76 65 63 74 6f 72 5f  ATE VIEW vector_
c130: 6c 61 79 65 72 73 5f 73 74 61 74 69 73 74 69 63  layers_statistic
c140: 73 20 41 53 5c 6e 22 29 3b 0a 20 20 20 20 73 74  s AS\n");.    st
c150: 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45 4c 45  rcat (sql, "SELE
c160: 43 54 20 27 53 70 61 74 69 61 6c 54 61 62 6c 65  CT 'SpatialTable
c170: 27 20 41 53 20 6c 61 79 65 72 5f 74 79 70 65 2c  ' AS layer_type,
c180: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
c190: 28 73 71 6c 2c 20 22 66 5f 74 61 62 6c 65 5f 6e  (sql, "f_table_n
c1a0: 61 6d 65 20 41 53 20 74 61 62 6c 65 5f 6e 61 6d  ame AS table_nam
c1b0: 65 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  e, ");.    strca
c1c0: 74 20 28 73 71 6c 2c 20 22 66 5f 67 65 6f 6d 65  t (sql, "f_geome
c1d0: 74 72 79 5f 63 6f 6c 75 6d 6e 20 41 53 20 67 65  try_column AS ge
c1e0: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 2c 20 22  ometry_column, "
c1f0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
c200: 71 6c 2c 20 22 6c 61 73 74 5f 76 65 72 69 66 69  ql, "last_verifi
c210: 65 64 20 41 53 20 6c 61 73 74 5f 76 65 72 69 66  ed AS last_verif
c220: 69 65 64 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  ied, ");.    str
c230: 63 61 74 20 28 73 71 6c 2c 20 22 72 6f 77 5f 63  cat (sql, "row_c
c240: 6f 75 6e 74 20 41 53 20 72 6f 77 5f 63 6f 75 6e  ount AS row_coun
c250: 74 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  t, ");.    strca
c260: 74 20 28 73 71 6c 2c 20 22 65 78 74 65 6e 74 5f  t (sql, "extent_
c270: 6d 69 6e 5f 78 20 41 53 20 65 78 74 65 6e 74 5f  min_x AS extent_
c280: 6d 69 6e 5f 78 2c 20 22 29 3b 0a 20 20 20 20 73  min_x, ");.    s
c290: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 65 78 74  trcat (sql, "ext
c2a0: 65 6e 74 5f 6d 69 6e 5f 79 20 41 53 20 65 78 74  ent_min_y AS ext
c2b0: 65 6e 74 5f 6d 69 6e 5f 79 2c 20 22 29 3b 0a 20  ent_min_y, ");. 
c2c0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
c2d0: 22 65 78 74 65 6e 74 5f 6d 61 78 5f 78 20 41 53  "extent_max_x AS
c2e0: 20 65 78 74 65 6e 74 5f 6d 61 78 5f 78 2c 20 22   extent_max_x, "
c2f0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
c300: 71 6c 2c 20 22 65 78 74 65 6e 74 5f 6d 61 78 5f  ql, "extent_max_
c310: 79 20 41 53 20 65 78 74 65 6e 74 5f 6d 61 78 5f  y AS extent_max_
c320: 79 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  y\n");.    strca
c330: 74 20 28 73 71 6c 2c 20 22 46 52 4f 4d 20 67 65  t (sql, "FROM ge
c340: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 73  ometry_columns_s
c350: 74 61 74 69 73 74 69 63 73 5c 6e 22 29 3b 0a 20  tatistics\n");. 
c360: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
c370: 22 55 4e 49 4f 4e 5c 6e 22 29 3b 0a 20 20 20 20  "UNION\n");.    
c380: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45  strcat (sql, "SE
c390: 4c 45 43 54 20 27 53 70 61 74 69 61 6c 56 69 65  LECT 'SpatialVie
c3a0: 77 27 20 41 53 20 6c 61 79 65 72 5f 74 79 70 65  w' AS layer_type
c3b0: 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
c3c0: 20 28 73 71 6c 2c 20 22 76 69 65 77 5f 6e 61 6d   (sql, "view_nam
c3d0: 65 20 41 53 20 74 61 62 6c 65 5f 6e 61 6d 65 2c  e AS table_name,
c3e0: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
c3f0: 28 73 71 6c 2c 20 22 76 69 65 77 5f 67 65 6f 6d  (sql, "view_geom
c400: 65 74 72 79 20 41 53 20 67 65 6f 6d 65 74 72 79  etry AS geometry
c410: 5f 63 6f 6c 75 6d 6e 2c 20 22 29 3b 0a 20 20 20  _column, ");.   
c420: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6c   strcat (sql, "l
c430: 61 73 74 5f 76 65 72 69 66 69 65 64 20 41 53 20  ast_verified AS 
c440: 6c 61 73 74 5f 76 65 72 69 66 69 65 64 2c 20 22  last_verified, "
c450: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
c460: 71 6c 2c 20 22 72 6f 77 5f 63 6f 75 6e 74 20 41  ql, "row_count A
c470: 53 20 72 6f 77 5f 63 6f 75 6e 74 2c 20 22 29 3b  S row_count, ");
c480: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
c490: 2c 20 22 65 78 74 65 6e 74 5f 6d 69 6e 5f 78 20  , "extent_min_x 
c4a0: 41 53 20 65 78 74 65 6e 74 5f 6d 69 6e 5f 78 2c  AS extent_min_x,
c4b0: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
c4c0: 28 73 71 6c 2c 20 22 65 78 74 65 6e 74 5f 6d 69  (sql, "extent_mi
c4d0: 6e 5f 79 20 41 53 20 65 78 74 65 6e 74 5f 6d 69  n_y AS extent_mi
c4e0: 6e 5f 79 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  n_y, ");.    str
c4f0: 63 61 74 20 28 73 71 6c 2c 20 22 65 78 74 65 6e  cat (sql, "exten
c500: 74 5f 6d 61 78 5f 78 20 41 53 20 65 78 74 65 6e  t_max_x AS exten
c510: 74 5f 6d 61 78 5f 78 2c 20 22 29 3b 0a 20 20 20  t_max_x, ");.   
c520: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 65   strcat (sql, "e
c530: 78 74 65 6e 74 5f 6d 61 78 5f 79 20 41 53 20 65  xtent_max_y AS e
c540: 78 74 65 6e 74 5f 6d 61 78 5f 79 5c 6e 22 29 3b  xtent_max_y\n");
c550: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
c560: 2c 20 22 46 52 4f 4d 20 76 69 65 77 73 5f 67 65  , "FROM views_ge
c570: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 73  ometry_columns_s
c580: 74 61 74 69 73 74 69 63 73 5c 6e 22 29 3b 0a 20  tatistics\n");. 
c590: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
c5a0: 22 55 4e 49 4f 4e 5c 6e 22 29 3b 0a 20 20 20 20  "UNION\n");.    
c5b0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45  strcat (sql, "SE
c5c0: 4c 45 43 54 20 27 56 69 72 74 75 61 6c 53 68 61  LECT 'VirtualSha
c5d0: 70 65 27 20 41 53 20 6c 61 79 65 72 5f 74 79 70  pe' AS layer_typ
c5e0: 65 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  e, ");.    strca
c5f0: 74 20 28 73 71 6c 2c 20 22 76 69 72 74 5f 6e 61  t (sql, "virt_na
c600: 6d 65 20 41 53 20 74 61 62 6c 65 5f 6e 61 6d 65  me AS table_name
c610: 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
c620: 20 28 73 71 6c 2c 20 22 76 69 72 74 5f 67 65 6f   (sql, "virt_geo
c630: 6d 65 74 72 79 20 41 53 20 67 65 6f 6d 65 74 72  metry AS geometr
c640: 79 5f 63 6f 6c 75 6d 6e 2c 20 22 29 3b 0a 20 20  y_column, ");.  
c650: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
c660: 6c 61 73 74 5f 76 65 72 69 66 69 65 64 20 41 53  last_verified AS
c670: 20 6c 61 73 74 5f 76 65 72 69 66 69 65 64 2c 20   last_verified, 
c680: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
c690: 73 71 6c 2c 20 22 72 6f 77 5f 63 6f 75 6e 74 20  sql, "row_count 
c6a0: 41 53 20 72 6f 77 5f 63 6f 75 6e 74 2c 20 22 29  AS row_count, ")
c6b0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
c6c0: 6c 2c 20 22 65 78 74 65 6e 74 5f 6d 69 6e 5f 78  l, "extent_min_x
c6d0: 20 41 53 20 65 78 74 65 6e 74 5f 6d 69 6e 5f 78   AS extent_min_x
c6e0: 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
c6f0: 20 28 73 71 6c 2c 20 22 65 78 74 65 6e 74 5f 6d   (sql, "extent_m
c700: 69 6e 5f 79 20 41 53 20 65 78 74 65 6e 74 5f 6d  in_y AS extent_m
c710: 69 6e 5f 79 2c 20 22 29 3b 0a 20 20 20 20 73 74  in_y, ");.    st
c720: 72 63 61 74 20 28 73 71 6c 2c 20 22 65 78 74 65  rcat (sql, "exte
c730: 6e 74 5f 6d 61 78 5f 78 20 41 53 20 65 78 74 65  nt_max_x AS exte
c740: 6e 74 5f 6d 61 78 5f 78 2c 20 22 29 3b 0a 20 20  nt_max_x, ");.  
c750: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
c760: 65 78 74 65 6e 74 5f 6d 61 78 5f 79 20 41 53 20  extent_max_y AS 
c770: 65 78 74 65 6e 74 5f 6d 61 78 5f 79 5c 6e 22 29  extent_max_y\n")
c780: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
c790: 6c 2c 20 22 46 52 4f 4d 20 76 69 72 74 73 5f 67  l, "FROM virts_g
c7a0: 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f  eometry_columns_
c7b0: 73 74 61 74 69 73 74 69 63 73 22 29 3b 0a 20 20  statistics");.  
c7c0: 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
c7d0: 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
c7e0: 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
c7f0: 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20  errMsg);.    if 
c800: 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
c810: 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70  K).      {..  sp
c820: 61 74 69 61 6c 69 74 65 5f 65 20 28 22 53 51 4c  atialite_e ("SQL
c830: 20 65 72 72 6f 72 3a 20 25 73 3a 20 25 73 5c 6e   error: %s: %s\n
c840: 22 2c 20 73 71 6c 2c 20 65 72 72 4d 73 67 29 3b  ", sql, errMsg);
c850: 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
c860: 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20 72 65   (errMsg);..  re
c870: 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
c880: 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  /* creating the 
c890: 56 45 43 54 4f 52 5f 4c 41 59 45 52 53 5f 46 49  VECTOR_LAYERS_FI
c8a0: 45 4c 44 5f 49 4e 46 4f 53 20 76 69 65 77 20 2a  ELD_INFOS view *
c8b0: 2f 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  /.    strcpy (sq
c8c0: 6c 2c 20 22 43 52 45 41 54 45 20 56 49 45 57 20  l, "CREATE VIEW 
c8d0: 76 65 63 74 6f 72 5f 6c 61 79 65 72 73 5f 66 69  vector_layers_fi
c8e0: 65 6c 64 5f 69 6e 66 6f 73 20 41 53 5c 6e 22 29  eld_infos AS\n")
c8f0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
c900: 6c 2c 20 22 53 45 4c 45 43 54 20 27 53 70 61 74  l, "SELECT 'Spat
c910: 69 61 6c 54 61 62 6c 65 27 20 41 53 20 6c 61 79  ialTable' AS lay
c920: 65 72 5f 74 79 70 65 2c 20 22 29 3b 0a 20 20 20  er_type, ");.   
c930: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66   strcat (sql, "f
c940: 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 41 53 20 74  _table_name AS t
c950: 61 62 6c 65 5f 6e 61 6d 65 2c 20 22 29 3b 0a 20  able_name, ");. 
c960: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
c970: 22 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  "f_geometry_colu
c980: 6d 6e 20 41 53 20 67 65 6f 6d 65 74 72 79 5f 63  mn AS geometry_c
c990: 6f 6c 75 6d 6e 2c 20 22 29 3b 0a 20 20 20 20 73  olumn, ");.    s
c9a0: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6f 72 64  trcat (sql, "ord
c9b0: 69 6e 61 6c 20 41 53 20 6f 72 64 69 6e 61 6c 2c  inal AS ordinal,
c9c0: 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20   ");.    strcat 
c9d0: 28 73 71 6c 2c 20 22 63 6f 6c 75 6d 6e 5f 6e 61  (sql, "column_na
c9e0: 6d 65 20 41 53 20 63 6f 6c 75 6d 6e 5f 6e 61 6d  me AS column_nam
c9f0: 65 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  e, ");.    strca
ca00: 74 20 28 73 71 6c 2c 20 22 6e 75 6c 6c 5f 76 61  t (sql, "null_va
ca10: 6c 75 65 73 20 41 53 20 6e 75 6c 6c 5f 76 61 6c  lues AS null_val
ca20: 75 65 73 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  ues, ");.    str
ca30: 63 61 74 20 28 73 71 6c 2c 20 22 69 6e 74 65 67  cat (sql, "integ
ca40: 65 72 5f 76 61 6c 75 65 73 20 41 53 20 69 6e 74  er_values AS int
ca50: 65 67 65 72 5f 76 61 6c 75 65 73 2c 20 22 29 3b  eger_values, ");
ca60: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
ca70: 2c 20 22 64 6f 75 62 6c 65 5f 76 61 6c 75 65 73  , "double_values
ca80: 20 41 53 20 64 6f 75 62 6c 65 5f 76 61 6c 75 65   AS double_value
ca90: 73 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  s, ");.    strca
caa0: 74 20 28 73 71 6c 2c 20 22 74 65 78 74 5f 76 61  t (sql, "text_va
cab0: 6c 75 65 73 20 41 53 20 74 65 78 74 5f 76 61 6c  lues AS text_val
cac0: 75 65 73 2c 20 22 29 3b 0a 20 20 20 20 73 74 72  ues, ");.    str
cad0: 63 61 74 20 28 73 71 6c 2c 20 22 62 6c 6f 62 5f  cat (sql, "blob_
cae0: 76 61 6c 75 65 73 20 41 53 20 62 6c 6f 62 5f 76  values AS blob_v
caf0: 61 6c 75 65 73 2c 20 22 29 3b 0a 20 20 20 20 73  alues, ");.    s
cb00: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6d 61 78  trcat (sql, "max
cb10: 5f 73 69 7a 65 20 41 53 20 6d 61 78 5f 73 69 7a  _size AS max_siz
cb20: 65 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  e\n");.    strca
cb30: 74 20 28 73 71 6c 2c 20 22 46 52 4f 4d 20 67 65  t (sql, "FROM ge
cb40: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 66  ometry_columns_f
cb50: 69 65 6c 64 5f 69 6e 66 6f 73 5c 6e 22 29 3b 0a  ield_infos\n");.
cb60: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
cb70: 20 22 55 4e 49 4f 4e 5c 6e 22 29 3b 0a 20 20 20   "UNION\n");.   
cb80: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 53   strcat (sql, "S
cb90: 45 4c 45 43 54 20 27 53 70 61 74 69 61 6c 56 69  ELECT 'SpatialVi
cba0: 65 77 27 20 41 53 20 6c 61 79 65 72 5f 74 79 70  ew' AS layer_typ
cbb0: 65 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  e, ");.    strca
cbc0: 74 20 28 73 71 6c 2c 20 22 76 69 65 77 5f 6e 61  t (sql, "view_na
cbd0: 6d 65 20 41 53 20 74 61 62 6c 65 5f 6e 61 6d 65  me AS table_name
cbe0: 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
cbf0: 20 28 73 71 6c 2c 20 22 76 69 65 77 5f 67 65 6f   (sql, "view_geo
cc00: 6d 65 74 72 79 20 41 53 20 67 65 6f 6d 65 74 72  metry AS geometr
cc10: 79 5f 63 6f 6c 75 6d 6e 2c 20 22 29 3b 0a 20 20  y_column, ");.  
cc20: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
cc30: 6f 72 64 69 6e 61 6c 20 41 53 20 6f 72 64 69 6e  ordinal AS ordin
cc40: 61 6c 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63  al, ");.    strc
cc50: 61 74 20 28 73 71 6c 2c 20 22 63 6f 6c 75 6d 6e  at (sql, "column
cc60: 5f 6e 61 6d 65 20 41 53 20 63 6f 6c 75 6d 6e 5f  _name AS column_
cc70: 6e 61 6d 65 2c 20 22 29 3b 0a 20 20 20 20 73 74  name, ");.    st
cc80: 72 63 61 74 20 28 73 71 6c 2c 20 22 6e 75 6c 6c  rcat (sql, "null
cc90: 5f 76 61 6c 75 65 73 20 41 53 20 6e 75 6c 6c 5f  _values AS null_
cca0: 76 61 6c 75 65 73 2c 20 22 29 3b 0a 20 20 20 20  values, ");.    
ccb0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 69 6e  strcat (sql, "in
ccc0: 74 65 67 65 72 5f 76 61 6c 75 65 73 20 41 53 20  teger_values AS 
ccd0: 69 6e 74 65 67 65 72 5f 76 61 6c 75 65 73 2c 20  integer_values, 
cce0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
ccf0: 73 71 6c 2c 20 22 64 6f 75 62 6c 65 5f 76 61 6c  sql, "double_val
cd00: 75 65 73 20 41 53 20 64 6f 75 62 6c 65 5f 76 61  ues AS double_va
cd10: 6c 75 65 73 2c 20 22 29 3b 0a 20 20 20 20 73 74  lues, ");.    st
cd20: 72 63 61 74 20 28 73 71 6c 2c 20 22 74 65 78 74  rcat (sql, "text
cd30: 5f 76 61 6c 75 65 73 20 41 53 20 74 65 78 74 5f  _values AS text_
cd40: 76 61 6c 75 65 73 2c 20 22 29 3b 0a 20 20 20 20  values, ");.    
cd50: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 62 6c  strcat (sql, "bl
cd60: 6f 62 5f 76 61 6c 75 65 73 20 41 53 20 62 6c 6f  ob_values AS blo
cd70: 62 5f 76 61 6c 75 65 73 2c 20 22 29 3b 0a 20 20  b_values, ");.  
cd80: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
cd90: 6d 61 78 5f 73 69 7a 65 20 41 53 20 6d 61 78 5f  max_size AS max_
cda0: 73 69 7a 65 5c 6e 22 29 3b 0a 20 20 20 20 73 74  size\n");.    st
cdb0: 72 63 61 74 20 28 73 71 6c 2c 20 22 46 52 4f 4d  rcat (sql, "FROM
cdc0: 20 76 69 65 77 73 5f 67 65 6f 6d 65 74 72 79 5f   views_geometry_
cdd0: 63 6f 6c 75 6d 6e 73 5f 66 69 65 6c 64 5f 69 6e  columns_field_in
cde0: 66 6f 73 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  fos\n");.    str
cdf0: 63 61 74 20 28 73 71 6c 2c 20 22 55 4e 49 4f 4e  cat (sql, "UNION
ce00: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
ce10: 20 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 27   (sql, "SELECT '
ce20: 56 69 72 74 75 61 6c 53 68 61 70 65 27 20 41 53  VirtualShape' AS
ce30: 20 6c 61 79 65 72 5f 74 79 70 65 2c 20 22 29 3b   layer_type, ");
ce40: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
ce50: 2c 20 22 76 69 72 74 5f 6e 61 6d 65 20 41 53 20  , "virt_name AS 
ce60: 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 22 29 3b 0a  table_name, ");.
ce70: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
ce80: 20 22 76 69 72 74 5f 67 65 6f 6d 65 74 72 79 20   "virt_geometry 
ce90: 41 53 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  AS geometry_colu
cea0: 6d 6e 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63  mn, ");.    strc
ceb0: 61 74 20 28 73 71 6c 2c 20 22 6f 72 64 69 6e 61  at (sql, "ordina
cec0: 6c 20 41 53 20 6f 72 64 69 6e 61 6c 2c 20 22 29  l AS ordinal, ")
ced0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
cee0: 6c 2c 20 22 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20  l, "column_name 
cef0: 41 53 20 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 2c 20  AS column_name, 
cf00: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
cf10: 73 71 6c 2c 20 22 6e 75 6c 6c 5f 76 61 6c 75 65  sql, "null_value
cf20: 73 20 41 53 20 6e 75 6c 6c 5f 76 61 6c 75 65 73  s AS null_values
cf30: 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
cf40: 20 28 73 71 6c 2c 20 22 69 6e 74 65 67 65 72 5f   (sql, "integer_
cf50: 76 61 6c 75 65 73 20 41 53 20 69 6e 74 65 67 65  values AS intege
cf60: 72 5f 76 61 6c 75 65 73 2c 20 22 29 3b 0a 20 20  r_values, ");.  
cf70: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
cf80: 64 6f 75 62 6c 65 5f 76 61 6c 75 65 73 20 41 53  double_values AS
cf90: 20 64 6f 75 62 6c 65 5f 76 61 6c 75 65 73 2c 20   double_values, 
cfa0: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
cfb0: 73 71 6c 2c 20 22 74 65 78 74 5f 76 61 6c 75 65  sql, "text_value
cfc0: 73 20 41 53 20 74 65 78 74 5f 76 61 6c 75 65 73  s AS text_values
cfd0: 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
cfe0: 20 28 73 71 6c 2c 20 22 62 6c 6f 62 5f 76 61 6c   (sql, "blob_val
cff0: 75 65 73 20 41 53 20 62 6c 6f 62 5f 76 61 6c 75  ues AS blob_valu
d000: 65 73 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63  es, ");.    strc
d010: 61 74 20 28 73 71 6c 2c 20 22 6d 61 78 5f 73 69  at (sql, "max_si
d020: 7a 65 20 41 53 20 6d 61 78 5f 73 69 7a 65 5c 6e  ze AS max_size\n
d030: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
d040: 73 71 6c 2c 20 22 46 52 4f 4d 20 76 69 72 74 73  sql, "FROM virts
d050: 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
d060: 73 5f 66 69 65 6c 64 5f 69 6e 66 6f 73 22 29 3b  s_field_infos");
d070: 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74  .    ret = sqlit
d080: 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c  e3_exec (sqlite,
d090: 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c   sql, NULL, NULL
d0a0: 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20  , &errMsg);.    
d0b0: 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
d0c0: 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20  E_OK).      {.. 
d0d0: 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22   spatialite_e ("
d0e0: 53 51 4c 20 65 72 72 6f 72 3a 20 25 73 3a 20 25  SQL error: %s: %
d0f0: 73 5c 6e 22 2c 20 73 71 6c 2c 20 65 72 72 4d 73  s\n", sql, errMs
d100: 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66  g);..  sqlite3_f
d110: 72 65 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 20  ree (errMsg);.. 
d120: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
d130: 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b   }.    return 1;
d140: 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
d150: 66 6e 63 74 5f 43 68 65 63 6b 53 70 61 74 69 61  fnct_CheckSpatia
d160: 6c 4d 65 74 61 44 61 74 61 20 28 73 71 6c 69 74  lMetaData (sqlit
d170: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
d180: 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a  text, int argc,.
d190: 09 09 09 20 20 20 73 71 6c 69 74 65 33 5f 76 61  ...   sqlite3_va
d1a0: 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
d1b0: 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
d1c0: 2f 20 43 68 65 63 6b 53 70 61 74 69 61 6c 4d 65  / CheckSpatialMe
d1d0: 74 61 44 61 74 61 28 76 6f 69 64 29 0a 2f 0a 2f  taData(void)././
d1e0: 20 66 6f 72 20 46 44 4f 2d 4f 47 52 20 69 6e 74   for FDO-OGR int
d1f0: 65 72 6f 70 65 72 61 62 69 6c 69 74 79 3a 0a 2f  eroperability:./
d200: 20 74 65 73 74 73 20 74 68 65 20 53 70 61 74 69   tests the Spati
d210: 61 6c 4d 65 74 61 64 61 74 61 20 74 79 70 65 2c  alMetadata type,
d220: 20 72 65 74 75 72 6e 69 6e 67 3a 0a 2f 0a 2f 20   returning:././ 
d230: 30 20 2d 20 69 66 20 6e 6f 20 76 61 6c 69 64 20  0 - if no valid 
d240: 53 70 61 74 69 61 6c 4d 65 74 61 44 61 74 61 20  SpatialMetaData 
d250: 77 68 65 72 65 20 66 6f 75 6e 64 0a 2f 20 31 20  where found./ 1 
d260: 2d 20 69 66 20 53 70 61 74 69 61 4c 69 74 65 2d  - if SpatiaLite-
d270: 6c 65 67 61 63 79 20 53 70 61 74 69 61 6c 4d 65  legacy SpatialMe
d280: 74 61 64 61 74 61 20 77 68 65 72 65 20 66 6f 75  tadata where fou
d290: 6e 64 0a 2f 20 32 2d 20 69 66 20 46 44 4f 2d 4f  nd./ 2- if FDO-O
d2a0: 47 52 2d 6c 69 6b 65 20 53 70 61 74 69 61 6c 4d  GR-like SpatialM
d2b0: 65 74 61 64 61 74 61 20 77 68 65 72 65 20 66 6f  etadata where fo
d2c0: 75 6e 64 0a 2f 20 33 20 2d 20 69 66 20 53 70 61  und./ 3 - if Spa
d2d0: 74 69 61 4c 69 74 65 2d 63 75 72 72 65 6e 74 20  tiaLite-current 
d2e0: 53 70 61 74 69 61 6c 4d 65 74 61 64 61 74 61 20  SpatialMetadata 
d2f0: 77 68 65 72 65 20 66 6f 75 6e 64 0a 2f 0a 2a 2f  where found./.*/
d300: 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71  .    sqlite3 *sq
d310: 6c 69 74 65 3b 0a 20 20 20 20 69 6e 74 20 72 65  lite;.    int re
d320: 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  t;.    GAIA_UNUS
d330: 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
d340: 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
d350: 20 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65   sqlite = sqlite
d360: 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
d370: 64 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  dle (context);. 
d380: 20 20 20 72 65 74 20 3d 20 63 68 65 63 6b 53 70     ret = checkSp
d390: 61 74 69 61 6c 4d 65 74 61 44 61 74 61 20 28 73  atialMetaData (s
d3a0: 71 6c 69 74 65 29 3b 0a 20 20 20 20 69 66 20 28  qlite);.    if (
d3b0: 72 65 74 20 3d 3d 20 33 29 0a 20 20 20 20 20 20  ret == 3).      
d3c0: 7b 0a 09 20 20 2f 2a 20 74 72 79 69 6e 67 20 74  {..  /* trying t
d3d0: 6f 20 63 72 65 61 74 65 20 74 68 65 20 61 64 76  o create the adv
d3e0: 61 6e 63 65 64 20 6d 65 74 61 64 61 74 61 20 74  anced metadata t
d3f0: 61 62 6c 65 73 20 3e 3d 20 76 2e 34 2e 30 2e 30  ables >= v.4.0.0
d400: 20 2a 2f 0a 09 20 20 63 72 65 61 74 65 41 64 76   */..  createAdv
d410: 61 6e 63 65 64 4d 65 74 61 44 61 74 61 20 28 73  ancedMetaData (s
d420: 71 6c 69 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a  qlite);.      }.
d430: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
d440: 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
d450: 20 72 65 74 29 3b 0a 20 20 20 20 72 65 74 75 72   ret);.    retur
d460: 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  n;.}..static voi
d470: 64 0a 66 6e 63 74 5f 49 6e 69 74 53 70 61 74 69  d.fnct_InitSpati
d480: 61 6c 4d 65 74 61 44 61 74 61 20 28 73 71 6c 69  alMetaData (sqli
d490: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
d4a0: 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
d4b0: 0a 09 09 09 20 20 73 71 6c 69 74 65 33 5f 76 61  ....  sqlite3_va
d4c0: 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
d4d0: 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
d4e0: 2f 20 49 6e 69 74 53 70 61 74 69 61 6c 4d 65 74  / InitSpatialMet
d4f0: 61 44 61 74 61 28 5b 74 65 78 74 20 6d 6f 64 65  aData([text mode
d500: 5d 29 0a 2f 0a 2f 20 63 72 65 61 74 65 73 20 74  ])././ creates t
d510: 68 65 20 53 50 41 54 49 41 4c 5f 52 45 46 5f 53  he SPATIAL_REF_S
d520: 59 53 20 61 6e 64 20 47 45 4f 4d 45 54 52 59 5f  YS and GEOMETRY_
d530: 43 4f 4c 55 4d 4e 53 20 74 61 62 6c 65 73 0a 2f  COLUMNS tables./
d540: 20 72 65 74 75 72 6e 73 20 31 20 6f 6e 20 73 75   returns 1 on su
d550: 63 63 65 73 73 0a 2f 20 30 20 6f 6e 20 66 61 69  ccess./ 0 on fai
d560: 6c 75 72 65 0a 2a 2f 0a 20 20 20 20 63 68 61 72  lure.*/.    char
d570: 20 73 71 6c 5b 38 31 39 32 5d 3b 0a 20 20 20 20   sql[8192];.    
d580: 63 68 61 72 20 2a 65 72 72 4d 73 67 20 3d 20 4e  char *errMsg = N
d590: 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 65 74  ULL;.    int ret
d5a0: 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
d5b0: 20 2a 78 6d 6f 64 65 3b 0a 20 20 20 20 69 6e 74   *xmode;.    int
d5c0: 20 6d 6f 64 65 20 3d 20 47 41 49 41 5f 45 50 53   mode = GAIA_EPS
d5d0: 47 5f 41 4e 59 3b 0a 20 20 20 20 73 71 6c 69 74  G_ANY;.    sqlit
d5e0: 65 33 20 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c  e3 *sqlite = sql
d5f0: 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
d600: 68 61 6e 64 6c 65 20 28 63 6f 6e 74 65 78 74 29  handle (context)
d610: 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
d620: 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
d630: 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
d640: 69 66 20 28 61 72 67 63 20 3d 3d 20 31 29 0a 20  if (argc == 1). 
d650: 20 20 20 20 20 7b 0a 09 20 20 69 66 20 28 73 71       {..  if (sq
d660: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
d670: 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
d680: 4c 49 54 45 5f 54 45 58 54 29 0a 09 20 20 20 20  LITE_TEXT)..    
d690: 7b 0a 09 09 73 70 61 74 69 61 6c 69 74 65 5f 65  {...spatialite_e
d6a0: 0a 09 09 20 20 20 20 28 22 49 6e 69 74 53 70 61  ...    ("InitSpa
d6b0: 74 69 61 6c 4d 65 74 61 44 61 74 61 28 29 20 65  tialMetaData() e
d6c0: 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 31  rror: argument 1
d6d0: 20 5b 6d 6f 64 65 5d 20 69 73 20 6e 6f 74 20 6f   [mode] is not o
d6e0: 66 20 74 68 65 20 53 74 72 69 6e 67 20 74 79 70  f the String typ
d6f0: 65 5c 6e 22 29 3b 0a 09 09 73 71 6c 69 74 65 33  e\n");...sqlite3
d700: 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
d710: 74 65 78 74 2c 20 30 29 3b 0a 09 09 72 65 74 75  text, 0);...retu
d720: 72 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20 78 6d  rn;..    }..  xm
d730: 6f 64 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ode = (const cha
d740: 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
d750: 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d  ue_text (argv[0]
d760: 29 3b 0a 09 20 20 69 66 20 28 73 74 72 63 61 73  );..  if (strcas
d770: 65 63 6d 70 20 28 78 6d 6f 64 65 2c 20 22 4e 4f  ecmp (xmode, "NO
d780: 4e 45 22 29 20 3d 3d 20 30 0a 09 20 20 20 20 20  NE") == 0..     
d790: 20 7c 7c 20 73 74 72 63 61 73 65 63 6d 70 20 28   || strcasecmp (
d7a0: 78 6d 6f 64 65 2c 20 22 45 4d 50 54 59 22 29 20  xmode, "EMPTY") 
d7b0: 3d 3d 20 30 29 0a 09 20 20 20 20 20 20 6d 6f 64  == 0)..      mod
d7c0: 65 20 3d 20 47 41 49 41 5f 45 50 53 47 5f 4e 4f  e = GAIA_EPSG_NO
d7d0: 4e 45 3b 0a 09 20 20 69 66 20 28 73 74 72 63 61  NE;..  if (strca
d7e0: 73 65 63 6d 70 20 28 78 6d 6f 64 65 2c 20 22 57  secmp (xmode, "W
d7f0: 47 53 38 34 22 29 20 3d 3d 20 30 0a 09 20 20 20  GS84") == 0..   
d800: 20 20 20 7c 7c 20 73 74 72 63 61 73 65 63 6d 70     || strcasecmp
d810: 20 28 78 6d 6f 64 65 2c 20 22 57 47 53 38 34 5f   (xmode, "WGS84_
d820: 4f 4e 4c 59 22 29 20 3d 3d 20 30 29 0a 09 20 20  ONLY") == 0)..  
d830: 20 20 20 20 6d 6f 64 65 20 3d 20 47 41 49 41 5f      mode = GAIA_
d840: 45 50 53 47 5f 57 47 53 38 34 5f 4f 4e 4c 59 3b  EPSG_WGS84_ONLY;
d850: 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 63 72 65 61  .      }./* crea
d860: 74 69 6e 67 20 74 68 65 20 53 50 41 54 49 41 4c  ting the SPATIAL
d870: 5f 52 45 46 5f 53 59 53 20 74 61 62 6c 65 20 2a  _REF_SYS table *
d880: 2f 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  /.    strcpy (sq
d890: 6c 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c 45  l, "CREATE TABLE
d8a0: 20 73 70 61 74 69 61 6c 5f 72 65 66 5f 73 79 73   spatial_ref_sys
d8b0: 20 28 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63   (\n");.    strc
d8c0: 61 74 20 28 73 71 6c 2c 20 22 73 72 69 64 20 49  at (sql, "srid I
d8d0: 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 20  NTEGER NOT NULL 
d8e0: 50 52 49 4d 41 52 59 20 4b 45 59 2c 5c 6e 22 29  PRIMARY KEY,\n")
d8f0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
d900: 6c 2c 20 22 61 75 74 68 5f 6e 61 6d 65 20 54 45  l, "auth_name TE
d910: 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29  XT NOT NULL,\n")
d920: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
d930: 6c 2c 20 22 61 75 74 68 5f 73 72 69 64 20 49 4e  l, "auth_srid IN
d940: 54 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c  TEGER NOT NULL,\
d950: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
d960: 28 73 71 6c 2c 20 22 72 65 66 5f 73 79 73 5f 6e  (sql, "ref_sys_n
d970: 61 6d 65 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c  ame TEXT NOT NUL
d980: 4c 20 44 45 46 41 55 4c 54 20 27 55 6e 6b 6e 6f  L DEFAULT 'Unkno
d990: 77 6e 27 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  wn',\n");.    st
d9a0: 72 63 61 74 20 28 73 71 6c 2c 20 22 70 72 6f 6a  rcat (sql, "proj
d9b0: 34 74 65 78 74 20 54 45 58 54 20 4e 4f 54 20 4e  4text TEXT NOT N
d9c0: 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  ULL,\n");.    st
d9d0: 72 63 61 74 20 28 73 71 6c 2c 20 22 73 72 74 65  rcat (sql, "srte
d9e0: 78 74 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c  xt TEXT NOT NULL
d9f0: 20 44 45 46 41 55 4c 54 20 27 55 6e 64 65 66 69   DEFAULT 'Undefi
da00: 6e 65 64 27 29 22 29 3b 0a 20 20 20 20 72 65 74  ned')");.    ret
da10: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
da20: 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
da30: 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
da40: 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
da50: 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67  != SQLITE_OK)..g
da60: 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 73  oto error;.    s
da70: 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45  trcpy (sql, "CRE
da80: 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58  ATE UNIQUE INDEX
da90: 20 69 64 78 5f 73 70 61 74 69 61 6c 5f 72 65 66   idx_spatial_ref
daa0: 5f 73 79 73 20 5c 6e 22 29 3b 0a 20 20 20 20 73  _sys \n");.    s
dab0: 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4f 4e 20  trcat (sql, "ON 
dac0: 73 70 61 74 69 61 6c 5f 72 65 66 5f 73 79 73 20  spatial_ref_sys 
dad0: 28 61 75 74 68 5f 73 72 69 64 2c 20 61 75 74 68  (auth_srid, auth
dae0: 5f 6e 61 6d 65 29 22 29 3b 0a 20 20 20 20 72 65  _name)");.    re
daf0: 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
db00: 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
db10: 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d  ULL, NULL, &errM
db20: 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74  sg);.    if (ret
db30: 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09   != SQLITE_OK)..
db40: 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20  goto error;.    
db50: 75 70 64 61 74 65 53 70 61 74 69 61 4c 69 74 65  updateSpatiaLite
db60: 48 69 73 74 6f 72 79 20 28 73 71 6c 69 74 65 2c  History (sqlite,
db70: 20 22 73 70 61 74 69 61 6c 5f 72 65 66 5f 73 79   "spatial_ref_sy
db80: 73 22 2c 20 4e 55 4c 4c 2c 0a 09 09 09 20 20 20  s", NULL,....   
db90: 20 20 22 74 61 62 6c 65 20 73 75 63 63 65 73 73    "table success
dba0: 66 75 6c 6c 79 20 63 72 65 61 74 65 64 22 29 3b  fully created");
dbb0: 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65  ./* creating the
dbc0: 20 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e   GEOMETRY_COLUMN
dbd0: 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 73 74   table */.    st
dbe0: 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45 41  rcpy (sql, "CREA
dbf0: 54 45 20 54 41 42 4c 45 20 67 65 6f 6d 65 74 72  TE TABLE geometr
dc00: 79 5f 63 6f 6c 75 6d 6e 73 20 28 5c 6e 22 29 3b  y_columns (\n");
dc10: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
dc20: 2c 20 22 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20  , "f_table_name 
dc30: 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  TEXT NOT NULL,\n
dc40: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
dc50: 73 71 6c 2c 20 22 66 5f 67 65 6f 6d 65 74 72 79  sql, "f_geometry
dc60: 5f 63 6f 6c 75 6d 6e 20 54 45 58 54 20 4e 4f 54  _column TEXT NOT
dc70: 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20   NULL,\n");.    
dc80: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 67 65  strcat (sql, "ge
dc90: 6f 6d 65 74 72 79 5f 74 79 70 65 20 49 4e 54 45  ometry_type INTE
dca0: 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22  GER NOT NULL,\n"
dcb0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
dcc0: 71 6c 2c 20 22 63 6f 6f 72 64 5f 64 69 6d 65 6e  ql, "coord_dimen
dcd0: 73 69 6f 6e 20 49 4e 54 45 47 45 52 20 4e 4f 54  sion INTEGER NOT
dce0: 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20   NULL,\n");.    
dcf0: 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 73 72  strcat (sql, "sr
dd00: 69 64 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e  id INTEGER NOT N
dd10: 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74  ULL,\n");.    st
dd20: 72 63 61 74 20 28 73 71 6c 2c 20 22 73 70 61 74  rcat (sql, "spat
dd30: 69 61 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65  ial_index_enable
dd40: 64 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55  d INTEGER NOT NU
dd50: 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
dd60: 63 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54  cat (sql, "CONST
dd70: 52 41 49 4e 54 20 70 6b 5f 67 65 6f 6d 5f 63 6f  RAINT pk_geom_co
dd80: 6c 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20 22  ls PRIMARY KEY "
dd90: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
dda0: 71 6c 2c 20 22 28 66 5f 74 61 62 6c 65 5f 6e 61  ql, "(f_table_na
ddb0: 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63  me, f_geometry_c
ddc0: 6f 6c 75 6d 6e 29 2c 5c 6e 22 29 3b 0a 20 20 20  olumn),\n");.   
ddd0: 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43   strcat (sql, "C
dde0: 4f 4e 53 54 52 41 49 4e 54 20 66 6b 5f 67 63 5f  ONSTRAINT fk_gc_
ddf0: 73 72 73 20 46 4f 52 45 49 47 4e 20 4b 45 59 20  srs FOREIGN KEY 
de00: 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
de10: 73 71 6c 2c 20 22 28 73 72 69 64 29 20 52 45 46  sql, "(srid) REF
de20: 45 52 45 4e 43 45 53 20 73 70 61 74 69 61 6c 5f  ERENCES spatial_
de30: 72 65 66 5f 73 79 73 20 28 73 72 69 64 29 2c 5c  ref_sys (srid),\
de40: 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
de50: 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e  (sql, "CONSTRAIN
de60: 54 20 63 6b 5f 67 63 5f 74 79 70 65 20 43 48 45  T ck_gc_type CHE
de70: 43 4b 20 28 67 65 6f 6d 65 74 72 79 5f 74 79 70  CK (geometry_typ
de80: 65 20 49 4e 20 22 29 3b 0a 20 20 20 20 73 74 72  e IN ");.    str
de90: 63 61 74 20 28 73 71 6c 2c 20 22 28 30 2c 31 2c  cat (sql, "(0,1,
dea0: 32 2c 33 2c 34 2c 35 2c 36 2c 37 2c 31 30 30 30  2,3,4,5,6,7,1000
deb0: 2c 31 30 30 31 2c 31 30 30 32 2c 31 30 30 33 2c  ,1001,1002,1003,
dec0: 31 30 30 34 2c 31 30 30 35 2c 31 30 30 36 2c 22  1004,1005,1006,"
ded0: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
dee0: 71 6c 2c 20 22 31 30 30 37 2c 32 30 30 30 2c 32  ql, "1007,2000,2
def0: 30 30 31 2c 32 30 30 32 2c 32 30 30 33 2c 32 30  001,2002,2003,20
df00: 30 34 2c 32 30 30 35 2c 32 30 30 36 2c 32 30 30  04,2005,2006,200
df10: 37 2c 33 30 30 30 2c 33 30 30 31 2c 22 29 3b 0a  7,3000,3001,");.
df20: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
df30: 20 22 33 30 30 32 2c 33 30 30 33 2c 33 30 30 34   "3002,3003,3004
df40: 2c 33 30 30 35 2c 33 30 30 36 2c 33 30 30 37 29  ,3005,3006,3007)
df50: 29 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  ),\n");.    strc
df60: 61 74 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52  at (sql, "CONSTR
df70: 41 49 4e 54 20 63 6b 5f 67 63 5f 64 69 6d 73 20  AINT ck_gc_dims 
df80: 43 48 45 43 4b 20 28 63 6f 6f 72 64 5f 64 69 6d  CHECK (coord_dim
df90: 65 6e 73 69 6f 6e 20 49 4e 20 22 29 3b 0a 20 20  ension IN ");.  
dfa0: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
dfb0: 28 32 2c 33 2c 34 29 29 2c 5c 6e 22 29 3b 0a 20  (2,3,4)),\n");. 
dfc0: 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
dfd0: 22 43 4f 4e 53 54 52 41 49 4e 54 20 63 6b 5f 67  "CONSTRAINT ck_g
dfe0: 63 5f 72 74 72 65 65 20 43 48 45 43 4b 20 22 29  c_rtree CHECK ")
dff0: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
e000: 6c 2c 20 22 28 73 70 61 74 69 61 6c 5f 69 6e 64  l, "(spatial_ind
e010: 65 78 5f 65 6e 61 62 6c 65 64 20 49 4e 20 28 30  ex_enabled IN (0
e020: 2c 31 2c 32 29 29 29 22 29 3b 0a 20 20 20 20 72  ,1,2)))");.    r
e030: 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
e040: 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  c (sqlite, sql, 
e050: 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
e060: 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  Msg);.    if (re
e070: 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
e080: 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20  .goto error;.   
e090: 20 75 70 64 61 74 65 53 70 61 74 69 61 4c 69 74   updateSpatiaLit
e0a0: 65 48 69 73 74 6f 72 79 20 28 73 71 6c 69 74 65  eHistory (sqlite
e0b0: 2c 20 22 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  , "geometry_colu
e0c0: 6d 6e 73 22 2c 20 4e 55 4c 4c 2c 0a 09 09 09 20  mns", NULL,.... 
e0d0: 20 20 20 20 22 74 61 62 6c 65 20 73 75 63 63 65      "table succe
e0e0: 73 73 66 75 6c 6c 79 20 63 72 65 61 74 65 64 22  ssfully created"
e0f0: 29 3b 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 61  );./* creating a
e100: 6e 20 49 4e 44 45 58 20 63 6f 72 72 65 73 70 6f  n INDEX correspo
e110: 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 53 52 49  nding to the SRI
e120: 44 20 46 4b 20 2a 2f 0a 20 20 20 20 73 74 72 63  D FK */.    strc
e130: 70 79 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45  py (sql, "CREATE
e140: 20 49 4e 44 45 58 20 69 64 78 5f 73 72 69 64 5f   INDEX idx_srid_
e150: 67 65 6f 63 6f 6c 73 20 4f 4e 20 67 65 6f 6d 65  geocols ON geome
e160: 74 72 79 5f 63 6f 6c 75 6d 6e 73 5c 6e 22 29 3b  try_columns\n");
e170: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
e180: 2c 20 22 28 73 72 69 64 29 20 22 29 3b 0a 20 20  , "(srid) ");.  
e190: 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
e1a0: 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
e1b0: 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
e1c0: 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20  errMsg);.    if 
e1d0: 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
e1e0: 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a  K)..goto error;.
e1f0: 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  /* creating the 
e200: 47 45 4f 4d 5f 43 4f 4c 53 5f 52 45 46 5f 53 59  GEOM_COLS_REF_SY
e210: 53 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 73 74  S view */.    st
e220: 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45 41  rcpy (sql, "CREA
e230: 54 45 20 56 49 45 57 20 67 65 6f 6d 5f 63 6f 6c  TE VIEW geom_col
e240: 73 5f 72 65 66 5f 73 79 73 20 41 53 5c 6e 22 29  s_ref_sys AS\n")
e250: 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
e260: 6c 2c 20 22 53 45 4c 45 43 54 20 66 5f 74 61 62  l, "SELECT f_tab
e270: 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65  le_name, f_geome
e280: 74 72 79 5f 63 6f 6c 75 6d 6e 2c 20 67 65 6f 6d  try_column, geom
e290: 65 74 72 79 5f 74 79 70 65 2c 5c 6e 22 29 3b 0a  etry_type,\n");.
e2a0: 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
e2b0: 20 22 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f   "coord_dimensio
e2c0: 6e 2c 20 73 70 61 74 69 61 6c 5f 72 65 66 5f 73  n, spatial_ref_s
e2d0: 79 73 2e 73 72 69 64 20 41 53 20 73 72 69 64 2c  ys.srid AS srid,
e2e0: 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
e2f0: 20 28 73 71 6c 2c 20 22 61 75 74 68 5f 6e 61 6d   (sql, "auth_nam
e300: 65 2c 20 61 75 74 68 5f 73 72 69 64 2c 20 72 65  e, auth_srid, re
e310: 66 5f 73 79 73 5f 6e 61 6d 65 2c 20 70 72 6f 6a  f_sys_name, proj
e320: 34 74 65 78 74 2c 20 73 72 74 65 78 74 5c 6e 22  4text, srtext\n"
e330: 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
e340: 71 6c 2c 20 22 46 52 4f 4d 20 67 65 6f 6d 65 74  ql, "FROM geomet
e350: 72 79 5f 63 6f 6c 75 6d 6e 73 2c 20 73 70 61 74  ry_columns, spat
e360: 69 61 6c 5f 72 65 66 5f 73 79 73 5c 6e 22 29 3b  ial_ref_sys\n");
e370: 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
e380: 2c 20 22 57 48 45 52 45 20 67 65 6f 6d 65 74 72  , "WHERE geometr
e390: 79 5f 63 6f 6c 75 6d 6e 73 2e 73 72 69 64 20 3d  y_columns.srid =
e3a0: 20 73 70 61 74 69 61 6c 5f 72 65 66 5f 73 79 73   spatial_ref_sys
e3b0: 2e 73 72 69 64 22 29 3b 0a 20 20 20 20 72 65 74  .srid");.    ret
e3c0: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
e3d0: 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
e3e0: 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
e3f0: 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
e400: 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67  != SQLITE_OK)..g
e410: 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 69  oto error;.    i
e420: 66 20 28 21 63 72 65 61 74 65 41 64 76 61 6e 63  f (!createAdvanc
e430: 65 64 4d 65 74 61 44 61 74 61 20 28 73 71 6c 69  edMetaData (sqli
e440: 74 65 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72  te))..goto error
e450: 3b 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68  ;./* creating th
e460: 65 20 53 70 61 74 69 61 6c 49 6e 64 65 78 20 56  e SpatialIndex V
e470: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 2a 2f 0a  IRTUAL TABLE */.
e480: 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 2c      strcpy (sql,
e490: 20 22 43 52 45 41 54 45 20 56 49 52 54 55 41 4c   "CREATE VIRTUAL
e4a0: 20 54 41 42 4c 45 20 53 70 61 74 69 61 6c 49 6e   TABLE SpatialIn
e4b0: 64 65 78 20 22 29 3b 0a 20 20 20 20 73 74 72 63  dex ");.    strc
e4c0: 61 74 20 28 73 71 6c 2c 20 22 55 53 49 4e 47 20  at (sql, "USING 
e4d0: 56 69 72 74 75 61 6c 53 70 61 74 69 61 6c 49 6e  VirtualSpatialIn
e4e0: 64 65 78 28 29 22 29 3b 0a 20 20 20 20 72 65 74  dex()");.    ret
e4f0: 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
e500: 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
e510: 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
e520: 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
e530: 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67  != SQLITE_OK)..g
e540: 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 69  oto error;.    i
e550: 66 20 28 73 70 61 74 69 61 6c 5f 72 65 66 5f 73  f (spatial_ref_s
e560: 79 73 5f 69 6e 69 74 32 20 28 73 71 6c 69 74 65  ys_init2 (sqlite
e570: 2c 20 6d 6f 64 65 2c 20 30 29 29 0a 20 20 20 20  , mode, 0)).    
e580: 20 20 7b 0a 09 20 20 69 66 20 28 6d 6f 64 65 20    {..  if (mode 
e590: 3d 3d 20 47 41 49 41 5f 45 50 53 47 5f 4e 4f 4e  == GAIA_EPSG_NON
e5a0: 45 29 0a 09 20 20 20 20 20 20 75 70 64 61 74 65  E)..      update
e5b0: 53 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f 72  SpatiaLiteHistor
e5c0: 79 20 28 73 71 6c 69 74 65 2c 20 22 73 70 61 74  y (sqlite, "spat
e5d0: 69 61 6c 5f 72 65 66 5f 73 79 73 22 2c 20 4e 55  ial_ref_sys", NU
e5e0: 4c 4c 2c 0a 09 09 09 09 20 20 20 20 20 20 20 22  LL,.....       "
e5f0: 74 61 62 6c 65 20 73 75 63 63 65 73 73 66 75 6c  table successful
e600: 6c 79 20 63 72 65 61 74 65 64 20 5b 65 6d 70 74  ly created [empt
e610: 79 5d 22 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20  y]");..  else.. 
e620: 20 20 20 20 20 75 70 64 61 74 65 53 70 61 74 69       updateSpati
e630: 61 4c 69 74 65 48 69 73 74 6f 72 79 20 28 73 71  aLiteHistory (sq
e640: 6c 69 74 65 2c 20 22 73 70 61 74 69 61 6c 5f 72  lite, "spatial_r
e650: 65 66 5f 73 79 73 22 2c 20 4e 55 4c 4c 2c 0a 09  ef_sys", NULL,..
e660: 09 09 09 20 20 20 20 20 20 20 22 74 61 62 6c 65  ...       "table
e670: 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 70 6f   successfully po
e680: 70 75 6c 61 74 65 64 22 29 3b 0a 20 20 20 20 20  pulated");.     
e690: 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72   }.    sqlite3_r
e6a0: 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
e6b0: 78 74 2c 20 31 29 3b 0a 20 20 20 20 72 65 74 75  xt, 1);.    retu
e6c0: 72 6e 3b 0a 20 20 65 72 72 6f 72 3a 0a 20 20 20  rn;.  error:.   
e6d0: 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22   spatialite_e ("
e6e0: 20 49 6e 69 74 53 70 61 74 69 61 4d 65 74 61 44   InitSpatiaMetaD
e6f0: 61 74 61 28 29 20 65 72 72 6f 72 3a 5c 22 25 73  ata() error:\"%s
e700: 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b 0a  \"\n", errMsg);.
e710: 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
e720: 20 28 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73   (errMsg);.    s
e730: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
e740: 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  t (context, 0);.
e750: 20 20 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73      return;.}..s
e760: 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
e770: 49 6e 73 65 72 74 45 70 73 67 53 72 69 64 20 28  InsertEpsgSrid (
e780: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
e790: 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
e7a0: 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
e7b0: 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
e7c0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
e7d0: 20 49 6e 73 65 72 74 45 70 73 67 53 72 69 64 28   InsertEpsgSrid(
e7e0: 69 6e 74 20 73 72 69 64 29 0a 2f 0a 2f 20 72 65  int srid)././ re
e7f0: 74 75 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65  turns 1 on succe
e800: 73 73 3a 20 30 20 6f 6e 20 66 61 69 6c 75 72 65  ss: 0 on failure
e810: 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 73 72 69 64  .*/.    int srid
e820: 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20  ;.    int ret;. 
e830: 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69     sqlite3 *sqli
e840: 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  te = sqlite3_con
e850: 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28  text_db_handle (
e860: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41  context);.    GA
e870: 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
e880: 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
e890: 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
e8a0: 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
e8b0: 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49  argv[0]) == SQLI
e8c0: 54 45 5f 49 4e 54 45 47 45 52 29 0a 09 73 72 69  TE_INTEGER)..sri
e8d0: 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
e8e0: 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b  e_int (argv[0]);
e8f0: 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
e900: 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
e910: 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
e920: 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  , 0);..  return;
e930: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74  .      }.    ret
e940: 20 3d 20 69 6e 73 65 72 74 5f 65 70 73 67 5f 73   = insert_epsg_s
e950: 72 69 64 20 28 73 71 6c 69 74 65 2c 20 73 72 69  rid (sqlite, sri
e960: 64 29 3b 0a 20 20 20 20 69 66 20 28 21 72 65 74  d);.    if (!ret
e970: 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
e980: 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
e990: 30 29 3b 0a 20 20 20 20 65 6c 73 65 0a 09 73 71  0);.    else..sq
e9a0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
e9b0: 20 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 7d   (context, 1);.}
e9c0: 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 72 65 63  ..static int.rec
e9d0: 6f 76 65 72 47 65 6f 6d 43 6f 6c 75 6d 6e 20 28  overGeomColumn (
e9e0: 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69 74 65  sqlite3 * sqlite
e9f0: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 61  , const char *ta
ea00: 62 6c 65 2c 0a 09 09 20 20 20 63 6f 6e 73 74 20  ble,...   const 
ea10: 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 2c 20 69 6e  char *column, in
ea20: 74 20 78 74 79 70 65 2c 20 69 6e 74 20 64 69 6d  t xtype, int dim
ea30: 73 2c 20 69 6e 74 20 73 72 69 64 2c 0a 09 09 20  s, int srid,... 
ea40: 20 20 63 68 61 72 20 2a 78 78 63 6f 6c 75 6d 6e    char *xxcolumn
ea50: 29 0a 7b 0a 2f 2a 20 63 68 65 63 6b 73 20 69 66  ).{./* checks if
ea60: 20 54 41 42 4c 45 2e 43 4f 4c 55 4d 4e 20 65 78   TABLE.COLUMN ex
ea70: 69 73 74 73 20 61 6e 64 20 68 61 73 20 74 68 65  ists and has the
ea80: 20 72 65 71 75 69 72 65 64 20 66 65 61 74 75 72   required featur
ea90: 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 6b  es */.    int ok
eaa0: 20 3d 20 31 3b 0a 20 20 20 20 63 68 61 72 20 73   = 1;.    char s
eab0: 71 6c 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e  ql[1024];.    in
eac0: 74 20 74 79 70 65 3b 0a 20 20 20 20 73 71 6c 69  t type;.    sqli
ead0: 74 65 33 5f 73 74 6d 74 20 2a 73 74 6d 74 3b 0a  te3_stmt *stmt;.
eae0: 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
eaf0: 50 74 72 20 67 65 6f 6d 3b 0a 20 20 20 20 63 6f  Ptr geom;.    co
eb00: 6e 73 74 20 76 6f 69 64 20 2a 62 6c 6f 62 5f 76  nst void *blob_v
eb10: 61 6c 75 65 3b 0a 20 20 20 20 69 6e 74 20 6c 65  alue;.    int le
eb20: 6e 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a  n;.    int ret;.
eb30: 20 20 20 20 69 6e 74 20 69 5f 63 6f 6c 3b 0a 20      int i_col;. 
eb40: 20 20 20 63 68 61 72 20 78 63 6f 6c 75 6d 6e 5b     char xcolumn[
eb50: 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
eb60: 78 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20  xtable[1024];.  
eb70: 20 20 69 6e 74 20 69 73 5f 6e 75 6c 6c 61 62 6c    int is_nullabl
eb80: 65 20 3d 20 31 3b 0a 20 20 20 20 63 68 61 72 20  e = 1;.    char 
eb90: 2a 2a 72 65 73 75 6c 74 73 3b 0a 20 20 20 20 69  **results;.    i
eba0: 6e 74 20 72 6f 77 73 3b 0a 20 20 20 20 69 6e 74  nt rows;.    int
ebb0: 20 63 6f 6c 75 6d 6e 73 3b 0a 20 20 20 20 69 6e   columns;.    in
ebc0: 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 2a 65  t i;.    char *e
ebd0: 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 0a 2f  rrMsg = NULL;../
ebe0: 2a 20 74 65 73 74 69 6e 67 20 69 66 20 4e 4f 54  * testing if NOT
ebf0: 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 73 74 72   NULL */.    str
ec00: 63 70 79 20 28 78 74 61 62 6c 65 2c 20 28 63 68  cpy (xtable, (ch
ec10: 61 72 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20  ar *) table);.  
ec20: 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f    double_quoted_
ec30: 73 71 6c 20 28 78 74 61 62 6c 65 29 3b 0a 20 20  sql (xtable);.  
ec40: 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
ec50: 22 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e  "PRAGMA table_in
ec60: 66 6f 28 25 73 29 22 2c 20 78 74 61 62 6c 65 29  fo(%s)", xtable)
ec70: 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
ec80: 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 28 73  te3_get_table (s
ec90: 71 6c 69 74 65 2c 20 73 71 6c 2c 20 26 72 65 73  qlite, sql, &res
eca0: 75 6c 74 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f  ults, &rows, &co
ecb0: 6c 75 6d 6e 73 2c 20 26 65 72 72 4d 73 67 29 3b  lumns, &errMsg);
ecc0: 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
ecd0: 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
ece0: 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
ecf0: 5f 65 20 28 22 72 65 63 6f 76 65 72 47 65 6f 6d  _e ("recoverGeom
ed00: 43 6f 6c 75 6d 6e 3a 20 65 72 72 6f 72 3a 20 5c  Column: error: \
ed10: 22 25 73 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67  "%s\"\n", errMsg
ed20: 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
ed30: 65 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20  ee (errMsg);..  
ed40: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
ed50: 7d 0a 20 20 20 20 66 6f 72 20 28 69 20 3d 20 31  }.    for (i = 1
ed60: 3b 20 69 20 3c 3d 20 72 6f 77 73 3b 20 69 2b 2b  ; i <= rows; i++
ed70: 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20  ).      {..  if 
ed80: 28 73 74 72 63 61 73 65 63 6d 70 20 28 28 63 68  (strcasecmp ((ch
ed90: 61 72 20 2a 29 20 63 6f 6c 75 6d 6e 2c 20 72 65  ar *) column, re
eda0: 73 75 6c 74 73 5b 28 69 20 2a 20 63 6f 6c 75 6d  sults[(i * colum
edb0: 6e 73 29 20 2b 20 31 5d 29 20 3d 3d 20 30 29 0a  ns) + 1]) == 0).
edc0: 09 20 20 20 20 7b 0a 09 09 73 74 72 63 70 79 20  .    {...strcpy 
edd0: 28 78 78 63 6f 6c 75 6d 6e 2c 20 72 65 73 75 6c  (xxcolumn, resul
ede0: 74 73 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29  ts[(i * columns)
edf0: 20 2b 20 31 5d 29 3b 0a 09 09 69 66 20 28 61 74   + 1]);...if (at
ee00: 6f 69 20 28 72 65 73 75 6c 74 73 5b 28 69 20 2a  oi (results[(i *
ee10: 20 63 6f 6c 75 6d 6e 73 29 20 2b 20 32 5d 29 20   columns) + 2]) 
ee20: 21 3d 20 30 29 0a 09 09 20 20 20 20 69 73 5f 6e  != 0)...    is_n
ee30: 75 6c 6c 61 62 6c 65 20 3d 20 30 3b 0a 09 20 20  ullable = 0;..  
ee40: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
ee50: 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
ee60: 6c 65 20 28 72 65 73 75 6c 74 73 29 3b 0a 0a 20  le (results);.. 
ee70: 20 20 20 73 74 72 63 70 79 20 28 78 63 6f 6c 75     strcpy (xcolu
ee80: 6d 6e 2c 20 28 63 68 61 72 20 2a 29 20 63 6f 6c  mn, (char *) col
ee90: 75 6d 6e 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65  umn);.    double
eea0: 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 63 6f  _quoted_sql (xco
eeb0: 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 70 72 69 6e  lumn);.    sprin
eec0: 74 66 20 28 73 71 6c 2c 20 22 53 45 4c 45 43 54  tf (sql, "SELECT
eed0: 20 25 73 20 46 52 4f 4d 20 25 73 22 2c 20 78 63   %s FROM %s", xc
eee0: 6f 6c 75 6d 6e 2c 20 78 74 61 62 6c 65 29 3b 0a  olumn, xtable);.
eef0: 2f 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 53 51 4c  /* compiling SQL
ef00: 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
ef10: 65 6e 74 20 2a 2f 0a 20 20 20 20 72 65 74 20 3d  ent */.    ret =
ef20: 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
ef30: 5f 76 32 20 28 73 71 6c 69 74 65 2c 20 73 71 6c  _v2 (sqlite, sql
ef40: 2c 20 73 74 72 6c 65 6e 20 28 73 71 6c 29 2c 20  , strlen (sql), 
ef50: 26 73 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20  &stmt, NULL);.  
ef60: 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
ef70: 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a  ITE_OK).      {.
ef80: 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20  .  spatialite_e 
ef90: 28 22 72 65 63 6f 76 65 72 47 65 6f 6d 43 6f 6c  ("recoverGeomCol
efa0: 75 6d 6e 3a 20 65 72 72 6f 72 20 25 64 20 5c 22  umn: error %d \"
efb0: 25 73 5c 22 5c 6e 22 2c 0a 09 09 09 73 71 6c 69  %s\"\n",....sqli
efc0: 74 65 33 5f 65 72 72 63 6f 64 65 20 28 73 71 6c  te3_errcode (sql
efd0: 69 74 65 29 2c 20 73 71 6c 69 74 65 33 5f 65 72  ite), sqlite3_er
efe0: 72 6d 73 67 20 28 73 71 6c 69 74 65 29 29 3b 0a  rmsg (sqlite));.
eff0: 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20  .  return 0;.   
f000: 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 20 28     }.    while (
f010: 31 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a  1).      {..  /*
f020: 20 73 63 72 6f 6c 6c 69 6e 67 20 74 68 65 20 72   scrolling the r
f030: 65 73 75 6c 74 20 73 65 74 20 72 6f 77 73 20 2a  esult set rows *
f040: 2f 0a 09 20 20 72 65 74 20 3d 20 73 71 6c 69 74  /..  ret = sqlit
f050: 65 33 5f 73 74 65 70 20 28 73 74 6d 74 29 3b 0a  e3_step (stmt);.
f060: 09 20 20 69 66 20 28 72 65 74 20 3d 3d 20 53 51  .  if (ret == SQ
f070: 4c 49 54 45 5f 44 4f 4e 45 29 0a 09 20 20 20 20  LITE_DONE)..    
f080: 20 20 62 72 65 61 6b 3b 09 09 2f 2a 20 65 6e 64    break;../* end
f090: 20 6f 66 20 72 65 73 75 6c 74 20 73 65 74 20 2a   of result set *
f0a0: 2f 0a 09 20 20 69 66 20 28 72 65 74 20 3d 3d 20  /..  if (ret == 
f0b0: 53 51 4c 49 54 45 5f 52 4f 57 29 0a 09 20 20 20  SQLITE_ROW)..   
f0c0: 20 7b 0a 09 09 2f 2a 20 63 68 65 63 6b 69 6e 67   {.../* checking
f0d0: 20 47 65 6f 6d 65 74 72 79 20 66 65 61 74 75 72   Geometry featur
f0e0: 65 73 20 2a 2f 0a 09 09 67 65 6f 6d 20 3d 20 4e  es */...geom = N
f0f0: 55 4c 4c 3b 0a 09 09 66 6f 72 20 28 69 5f 63 6f  ULL;...for (i_co
f100: 6c 20 3d 20 30 3b 20 69 5f 63 6f 6c 20 3c 20 73  l = 0; i_col < s
f110: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
f120: 75 6e 74 20 28 73 74 6d 74 29 3b 20 69 5f 63 6f  unt (stmt); i_co
f130: 6c 2b 2b 29 0a 09 09 20 20 7b 0a 09 09 20 20 20  l++)...  {...   
f140: 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 63     if (sqlite3_c
f150: 6f 6c 75 6d 6e 5f 74 79 70 65 20 28 73 74 6d 74  olumn_type (stmt
f160: 2c 20 69 5f 63 6f 6c 29 20 3d 3d 20 53 51 4c 49  , i_col) == SQLI
f170: 54 45 5f 4e 55 4c 4c 29 0a 09 09 09 7b 0a 09 09  TE_NULL)....{...
f180: 09 20 20 20 20 2f 2a 20 66 6f 75 6e 64 20 61 20  .    /* found a 
f190: 4e 55 4c 4c 20 67 65 6f 6d 65 74 72 79 20 2a 2f  NULL geometry */
f1a0: 0a 09 09 09 20 20 20 20 69 66 20 28 21 69 73 5f  ....    if (!is_
f1b0: 6e 75 6c 6c 61 62 6c 65 29 0a 09 09 09 09 6f 6b  nullable).....ok
f1c0: 20 3d 20 30 3b 0a 09 09 09 7d 0a 09 09 20 20 20   = 0;....}...   
f1d0: 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69     else if (sqli
f1e0: 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 20  te3_column_type 
f1f0: 28 73 74 6d 74 2c 20 69 5f 63 6f 6c 29 20 21 3d  (stmt, i_col) !=
f200: 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 09 09   SQLITE_BLOB)...
f210: 09 20 20 6f 6b 20 3d 20 30 3b 0a 09 09 20 20 20  .  ok = 0;...   
f220: 20 20 20 65 6c 73 65 0a 09 09 09 7b 0a 09 09 09     else....{....
f230: 20 20 20 20 62 6c 6f 62 5f 76 61 6c 75 65 20 3d      blob_value =
f240: 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
f250: 62 6c 6f 62 20 28 73 74 6d 74 2c 20 69 5f 63 6f  blob (stmt, i_co
f260: 6c 29 3b 0a 09 09 09 20 20 20 20 6c 65 6e 20 3d  l);....    len =
f270: 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
f280: 62 79 74 65 73 20 28 73 74 6d 74 2c 20 69 5f 63  bytes (stmt, i_c
f290: 6f 6c 29 3b 0a 09 09 09 20 20 20 20 67 65 6f 6d  ol);....    geom
f2a0: 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69   = gaiaFromSpati
f2b0: 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 62 6c  aLiteBlobWkb (bl
f2c0: 6f 62 5f 76 61 6c 75 65 2c 20 6c 65 6e 29 3b 0a  ob_value, len);.
f2d0: 09 09 09 20 20 20 20 69 66 20 28 21 67 65 6f 6d  ...    if (!geom
f2e0: 29 0a 09 09 09 09 6f 6b 20 3d 20 30 3b 0a 09 09  ).....ok = 0;...
f2f0: 09 20 20 20 20 65 6c 73 65 0a 09 09 09 20 20 20  .    else....   
f300: 20 20 20 7b 0a 09 09 09 09 20 20 69 66 20 28 67     {.....  if (g
f310: 65 6f 6d 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f  eom->DimensionMo
f320: 64 65 6c 20 21 3d 20 64 69 6d 73 29 0a 09 09 09  del != dims)....
f330: 09 20 20 20 20 20 20 6f 6b 20 3d 20 30 3b 0a 09  .      ok = 0;..
f340: 09 09 09 20 20 69 66 20 28 67 65 6f 6d 2d 3e 53  ...  if (geom->S
f350: 72 69 64 20 21 3d 20 73 72 69 64 29 0a 09 09 09  rid != srid)....
f360: 09 20 20 20 20 20 20 6f 6b 20 3d 20 30 3b 0a 09  .      ok = 0;..
f370: 09 09 09 20 20 74 79 70 65 20 3d 20 67 61 69 61  ...  type = gaia
f380: 47 65 6f 6d 65 74 72 79 54 79 70 65 20 28 67 65  GeometryType (ge
f390: 6f 6d 29 3b 0a 09 09 09 09 20 20 69 66 20 28 78  om);.....  if (x
f3a0: 74 79 70 65 20 3d 3d 20 2d 31 29 0a 09 09 09 09  type == -1).....
f3b0: 20 20 20 20 20 20 3b 09 2f 2a 20 47 45 4f 4d 45        ;./* GEOME
f3c0: 54 52 59 20 2a 2f 0a 09 09 09 09 20 20 65 6c 73  TRY */.....  els
f3d0: 65 0a 09 09 09 09 20 20 20 20 7b 0a 09 09 09 09  e.....    {.....
f3e0: 09 69 66 20 28 78 74 79 70 65 20 3d 3d 20 74 79  .if (xtype == ty
f3f0: 70 65 29 0a 09 09 09 09 09 20 20 20 20 3b 0a 09  pe)......    ;..
f400: 09 09 09 09 65 6c 73 65 0a 09 09 09 09 09 20 20  ....else......  
f410: 20 20 6f 6b 20 3d 20 30 3b 0a 09 09 09 09 20 20    ok = 0;.....  
f420: 20 20 7d 0a 09 09 09 09 20 20 67 61 69 61 46 72    }.....  gaiaFr
f430: 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d  eeGeomColl (geom
f440: 29 3b 0a 09 09 09 20 20 20 20 20 20 7d 0a 09 09  );....      }...
f450: 09 7d 0a 09 09 20 20 7d 0a 09 20 20 20 20 7d 0a  .}...  }..    }.
f460: 09 20 20 69 66 20 28 21 6f 6b 29 0a 09 20 20 20  .  if (!ok)..   
f470: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
f480: 7d 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  }.    ret = sqli
f490: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73 74  te3_finalize (st
f4a0: 6d 74 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74  mt);.    if (ret
f4b0: 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20   != SQLITE_OK). 
f4c0: 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61       {..  spatia
f4d0: 6c 69 74 65 5f 65 20 28 22 72 65 63 6f 76 65 72  lite_e ("recover
f4e0: 47 65 6f 6d 43 6f 6c 75 6d 6e 3a 20 65 72 72 6f  GeomColumn: erro
f4f0: 72 20 25 64 20 5c 22 25 73 5c 22 5c 6e 22 2c 0a  r %d \"%s\"\n",.
f500: 09 09 09 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  ...sqlite3_errco
f510: 64 65 20 28 73 71 6c 69 74 65 29 2c 20 73 71 6c  de (sqlite), sql
f520: 69 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71 6c  ite3_errmsg (sql
f530: 69 74 65 29 29 3b 0a 09 20 20 72 65 74 75 72 6e  ite));..  return
f540: 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
f550: 72 65 74 75 72 6e 20 6f 6b 3b 0a 7d 0a 0a 73 74  return ok;.}..st
f560: 61 74 69 63 20 76 6f 69 64 0a 62 75 69 6c 64 53  atic void.buildS
f570: 70 61 74 69 61 6c 49 6e 64 65 78 20 28 73 71 6c  patialIndex (sql
f580: 69 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63  ite3 * sqlite, c
f590: 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
f5a0: 61 72 20 2a 74 61 62 6c 65 2c 0a 09 09 20 20 20  ar *table,...   
f5b0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 5f  const char *col_
f5c0: 6e 61 6d 65 29 0a 7b 0a 2f 2a 20 6c 6f 61 64 69  name).{./* loadi
f5d0: 6e 67 20 61 20 53 70 61 74 69 61 6c 49 6e 64 65  ng a SpatialInde
f5e0: 78 20 5b 52 54 72 65 65 5d 20 2a 2f 0a 20 20 20  x [RTree] */.   
f5f0: 20 63 68 61 72 20 73 71 6c 5b 32 30 34 38 5d 3b   char sql[2048];
f600: 0a 20 20 20 20 63 68 61 72 20 73 71 6c 32 5b 31  .    char sql2[1
f610: 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a  024];.    char *
f620: 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20  errMsg = NULL;. 
f630: 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20     int ret;.    
f640: 63 68 61 72 20 78 6e 61 6d 65 5b 31 30 32 34 5d  char xname[1024]
f650: 3b 0a 20 20 20 20 63 68 61 72 20 78 74 61 62 6c  ;.    char xtabl
f660: 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 73 70 72  e[1024];.    spr
f670: 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 69 64  intf (xname, "id
f680: 78 5f 25 73 5f 25 73 22 2c 20 74 61 62 6c 65 2c  x_%s_%s", table,
f690: 20 63 6f 6c 5f 6e 61 6d 65 29 3b 0a 20 20 20 20   col_name);.    
f6a0: 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
f6b0: 6c 20 28 78 6e 61 6d 65 29 3b 0a 20 20 20 20 73  l (xname);.    s
f6c0: 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 49 4e  printf (sql, "IN
f6d0: 53 45 52 54 20 49 4e 54 4f 20 25 73 20 28 70 6b  SERT INTO %s (pk
f6e0: 69 64 2c 20 78 6d 69 6e 2c 20 78 6d 61 78 2c 20  id, xmin, xmax, 
f6f0: 79 6d 69 6e 2c 20 79 6d 61 78 29 20 22 2c 20 78  ymin, ymax) ", x
f700: 6e 61 6d 65 29 3b 0a 20 20 20 20 73 74 72 63 70  name);.    strcp
f710: 79 20 28 78 6e 61 6d 65 2c 20 63 6f 6c 5f 6e 61  y (xname, col_na
f720: 6d 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  me);.    double_
f730: 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d  quoted_sql (xnam
f740: 65 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28  e);.    strcpy (
f750: 78 74 61 62 6c 65 2c 20 28 63 68 61 72 20 2a 29  xtable, (char *)
f760: 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 64 6f 75   table);.    dou
f770: 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
f780: 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72  xtable);.    spr
f790: 69 6e 74 66 20 28 73 71 6c 32 2c 0a 09 20 20 20  intf (sql2,..   
f7a0: 20 20 22 53 45 4c 45 43 54 20 52 4f 57 49 44 2c    "SELECT ROWID,
f7b0: 20 4d 62 72 4d 69 6e 58 28 25 73 29 2c 20 4d 62   MbrMinX(%s), Mb
f7c0: 72 4d 61 78 58 28 25 73 29 2c 20 4d 62 72 4d 69  rMaxX(%s), MbrMi
f7d0: 6e 59 28 25 73 29 2c 20 4d 62 72 4d 61 78 59 28  nY(%s), MbrMaxY(
f7e0: 25 73 29 20 46 52 4f 4d 20 25 73 22 2c 0a 09 20  %s) FROM %s",.. 
f7f0: 20 20 20 20 78 6e 61 6d 65 2c 20 78 6e 61 6d 65      xname, xname
f800: 2c 20 78 6e 61 6d 65 2c 20 78 6e 61 6d 65 2c 20  , xname, xname, 
f810: 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  xtable);.    str
f820: 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
f830: 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
f840: 6c 32 2c 20 22 20 57 48 45 52 45 20 4d 62 72 4d  l2, " WHERE MbrM
f850: 69 6e 58 28 25 73 29 20 49 53 20 4e 4f 54 20 4e  inX(%s) IS NOT N
f860: 55 4c 4c 22 2c 20 78 6e 61 6d 65 29 3b 0a 20 20  ULL", xname);.  
f870: 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
f880: 71 6c 32 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  ql2);.    ret = 
f890: 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71  sqlite3_exec (sq
f8a0: 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c  lite, sql, NULL,
f8b0: 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b   NULL, &errMsg);
f8c0: 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
f8d0: 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
f8e0: 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
f8f0: 5f 65 20 28 22 62 75 69 6c 64 53 70 61 74 69 61  _e ("buildSpatia
f900: 6c 49 6e 64 65 78 20 65 72 72 6f 72 3a 20 5c 22  lIndex error: \"
f910: 25 73 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67 29  %s\"\n", errMsg)
f920: 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
f930: 65 20 28 65 72 72 4d 73 67 29 3b 0a 20 20 20 20  e (errMsg);.    
f940: 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f    }.}..static vo
f950: 69 64 0a 75 70 64 61 74 65 47 65 6f 6d 65 74 72  id.updateGeometr
f960: 79 54 72 69 67 67 65 72 73 20 28 73 71 6c 69 74  yTriggers (sqlit
f970: 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e  e3 * sqlite, con
f980: 73 74 20 63 68 61 72 20 2a 74 61 62 6c 65 2c 20  st char *table, 
f990: 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75  const char *colu
f9a0: 6d 6e 29 0a 7b 0a 2f 2a 20 75 70 64 61 74 65 73  mn).{./* updates
f9b0: 20 74 72 69 67 67 65 72 73 20 66 6f 72 20 73 6f   triggers for so
f9c0: 6d 65 20 53 70 61 74 69 61 6c 20 43 6f 6c 75 6d  me Spatial Colum
f9d0: 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71  n */.    char sq
f9e0: 6c 5b 32 35 36 5d 3b 0a 20 20 20 20 63 68 61 72  l[256];.    char
f9f0: 20 74 72 69 67 67 65 72 5b 34 30 39 36 5d 3b 0a   trigger[4096];.
fa00: 20 20 20 20 63 68 61 72 20 2a 2a 72 65 73 75 6c      char **resul
fa10: 74 73 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b  ts;.    int ret;
fa20: 0a 20 20 20 20 69 6e 74 20 72 6f 77 73 3b 0a 20  .    int rows;. 
fa30: 20 20 20 69 6e 74 20 63 6f 6c 75 6d 6e 73 3b 0a     int columns;.
fa40: 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63      int i;.    c
fa50: 68 61 72 20 74 62 6c 6e 61 6d 65 5b 32 35 36 5d  har tblname[256]
fa60: 3b 0a 20 20 20 20 63 68 61 72 20 63 6f 6c 6e 61  ;.    char colna
fa70: 6d 65 5b 32 35 36 5d 3b 0a 20 20 20 20 63 68 61  me[256];.    cha
fa80: 72 20 63 6f 6c 5f 69 6e 64 65 78 5b 33 32 5d 3b  r col_index[32];
fa90: 0a 20 20 20 20 63 68 61 72 20 63 6f 6c 5f 64 69  .    char col_di
faa0: 6d 73 5b 36 34 5d 3b 0a 20 20 20 20 69 6e 74 20  ms[64];.    int 
fab0: 69 6e 64 65 78 3b 0a 20 20 20 20 69 6e 74 20 63  index;.    int c
fac0: 61 63 68 65 64 3b 0a 20 20 20 20 69 6e 74 20 64  ached;.    int d
fad0: 69 6d 73 3b 0a 20 20 20 20 63 68 61 72 20 2a 74  ims;.    char *t
fae0: 78 74 5f 64 69 6d 73 3b 0a 20 20 20 20 69 6e 74  xt_dims;.    int
faf0: 20 6c 65 6e 3b 0a 20 20 20 20 63 68 61 72 20 2a   len;.    char *
fb00: 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20  errMsg = NULL;. 
fb10: 20 20 20 63 68 61 72 20 64 75 6d 6d 79 5b 35 31     char dummy[51
fb20: 32 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c  2];.    char sql
fb30: 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20  table[1024];.   
fb40: 20 63 68 61 72 20 73 71 6c 63 6f 6c 75 6d 6e 5b   char sqlcolumn[
fb50: 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
fb60: 78 6e 61 6d 65 5b 31 30 32 34 5d 3b 0a 20 20 20  xname[1024];.   
fb70: 20 63 68 61 72 20 78 63 6f 6c 6e 61 6d 65 5b 31   char xcolname[1
fb80: 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 78  024];.    char x
fb90: 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20  table[1024];.   
fba0: 20 63 68 61 72 20 78 69 6e 64 65 78 5b 31 30 32   char xindex[102
fbb0: 34 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73  4];.    struct s
fbc0: 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 73 74 72  patial_index_str
fbd0: 20 2a 66 69 72 73 74 5f 69 64 78 20 3d 20 4e 55   *first_idx = NU
fbe0: 4c 4c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73  LL;.    struct s
fbf0: 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 73 74 72  patial_index_str
fc00: 20 2a 6c 61 73 74 5f 69 64 78 20 3d 20 4e 55 4c   *last_idx = NUL
fc10: 4c 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73 70  L;.    struct sp
fc20: 61 74 69 61 6c 5f 69 6e 64 65 78 5f 73 74 72 20  atial_index_str 
fc30: 2a 63 75 72 72 5f 69 64 78 3b 0a 20 20 20 20 73  *curr_idx;.    s
fc40: 74 72 75 63 74 20 73 70 61 74 69 61 6c 5f 69 6e  truct spatial_in
fc50: 64 65 78 5f 73 74 72 20 2a 6e 65 78 74 5f 69 64  dex_str *next_id
fc60: 78 3b 0a 20 20 20 20 69 6e 74 20 6d 65 74 61 64  x;.    int metad
fc70: 61 74 61 5f 76 65 72 73 69 6f 6e 20 3d 20 63 68  ata_version = ch
fc80: 65 63 6b 53 70 61 74 69 61 6c 4d 65 74 61 44 61  eckSpatialMetaDa
fc90: 74 61 20 28 73 71 6c 69 74 65 29 3b 0a 20 20 20  ta (sqlite);.   
fca0: 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c   strcpy (sqltabl
fcb0: 65 2c 20 28 63 68 61 72 20 2a 29 20 74 61 62 6c  e, (char *) tabl
fcc0: 65 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71  e);.    clean_sq
fcd0: 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74 61 62  l_string (sqltab
fce0: 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20  le);.    strcpy 
fcf0: 28 73 71 6c 63 6f 6c 75 6d 6e 2c 20 28 63 68 61  (sqlcolumn, (cha
fd00: 72 20 2a 29 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20  r *) column);.  
fd10: 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69    clean_sql_stri
fd20: 6e 67 20 28 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a  ng (sqlcolumn);.
fd30: 20 20 20 20 69 66 20 28 6d 65 74 61 64 61 74 61      if (metadata
fd40: 5f 76 65 72 73 69 6f 6e 20 3d 3d 20 33 29 0a 20  _version == 3). 
fd50: 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 63 75 72       {..  /* cur
fd60: 72 65 6e 74 20 6d 65 74 61 64 61 74 61 20 73 74  rent metadata st
fd70: 79 6c 65 20 3e 3d 20 76 2e 34 2e 30 2e 30 20 2a  yle >= v.4.0.0 *
fd80: 2f 0a 09 20 20 73 70 72 69 6e 74 66 20 28 73 71  /..  sprintf (sq
fd90: 6c 2c 0a 09 09 20 20 20 22 53 45 4c 45 43 54 20  l,...   "SELECT 
fda0: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f  f_table_name, f_
fdb0: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 2c  geometry_column,
fdc0: 20 73 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 65   spatial_index_e
fdd0: 6e 61 62 6c 65 64 20 22 0a 09 09 20 20 20 22 46  nabled "...   "F
fde0: 52 4f 4d 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  ROM geometry_col
fdf0: 75 6d 6e 73 20 57 48 45 52 45 20 55 70 70 65 72  umns WHERE Upper
fe00: 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 20 3d  (f_table_name) =
fe10: 20 55 70 70 65 72 28 27 25 73 27 29 20 22 0a 09   Upper('%s') "..
fe20: 09 20 20 20 22 41 4e 44 20 55 70 70 65 72 28 66  .   "AND Upper(f
fe30: 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
fe40: 29 20 3d 20 55 70 70 65 72 28 27 25 73 27 29 22  ) = Upper('%s')"
fe50: 2c 20 73 71 6c 74 61 62 6c 65 2c 0a 09 09 20 20  , sqltable,...  
fe60: 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20   sqlcolumn);.   
fe70: 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20     }.    else.  
fe80: 20 20 20 20 7b 0a 09 20 20 2f 2a 20 6c 65 67 61      {..  /* lega
fe90: 63 79 20 6d 65 74 61 64 61 74 61 20 73 74 79 6c  cy metadata styl
fea0: 65 20 3c 3d 20 76 2e 33 2e 31 2e 30 20 2a 2f 0a  e <= v.3.1.0 */.
feb0: 09 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c  .  sprintf (sql,
fec0: 0a 09 09 20 20 20 22 53 45 4c 45 43 54 20 66 5f  ...   "SELECT f_
fed0: 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65  table_name, f_ge
fee0: 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 2c 20 73  ometry_column, s
fef0: 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 65 6e 61  patial_index_ena
ff00: 62 6c 65 64 2c 20 63 6f 6f 72 64 5f 64 69 6d 65  bled, coord_dime
ff10: 6e 73 69 6f 6e 20 22 0a 09 09 20 20 20 22 46 52  nsion "...   "FR
ff20: 4f 4d 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  OM geometry_colu
ff30: 6d 6e 73 20 57 48 45 52 45 20 55 70 70 65 72 28  mns WHERE Upper(
ff40: 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 20 3d 20  f_table_name) = 
ff50: 55 70 70 65 72 28 27 25 73 27 29 20 22 0a 09 09  Upper('%s') "...
ff60: 20 20 20 22 41 4e 44 20 55 70 70 65 72 28 66 5f     "AND Upper(f_
ff70: 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29  geometry_column)
ff80: 20 3d 20 55 70 70 65 72 28 27 25 73 27 29 22 2c   = Upper('%s')",
ff90: 20 73 71 6c 74 61 62 6c 65 2c 0a 09 09 20 20 20   sqltable,...   
ffa0: 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  sqlcolumn);.    
ffb0: 20 20 7d 0a 20 20 20 20 72 65 74 20 3d 20 73 71    }.    ret = sq
ffc0: 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20  lite3_get_table 
ffd0: 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 26 72  (sqlite, sql, &r
ffe0: 65 73 75 6c 74 73 2c 20 26 72 6f 77 73 2c 20 26  esults, &rows, &
fff0: 63 6f 6c 75 6d 6e 73 2c 20 26 65 72 72 4d 73 67  columns, &errMsg
10000 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
10010 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
10020 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
10030 74 65 5f 65 20 28 22 75 70 64 61 74 65 54 61 62  te_e ("updateTab
10040 6c 65 54 72 69 67 67 65 72 73 3a 20 5c 22 25 73  leTriggers: \"%s
10050 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b 0a  \"\n", errMsg);.
10060 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20  .  sqlite3_free 
10070 28 65 72 72 4d 73 67 29 3b 0a 09 20 20 72 65 74  (errMsg);..  ret
10080 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
10090 20 66 6f 72 20 28 69 20 3d 20 31 3b 20 69 20 3c   for (i = 1; i <
100a0 3d 20 72 6f 77 73 3b 20 69 2b 2b 29 0a 20 20 20  = rows; i++).   
100b0 20 20 20 7b 0a 09 20 20 2f 2a 20 70 72 65 70 61     {..  /* prepa
100c0 72 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72  ring the trigger
100d0 73 20 2a 2f 0a 09 20 20 73 74 72 63 70 79 20 28  s */..  strcpy (
100e0 74 62 6c 6e 61 6d 65 2c 20 72 65 73 75 6c 74 73  tblname, results
100f0 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 5d 29  [(i * columns)])
10100 3b 0a 09 20 20 73 74 72 63 70 79 20 28 63 6f 6c  ;..  strcpy (col
10110 6e 61 6d 65 2c 20 72 65 73 75 6c 74 73 5b 28 69  name, results[(i
10120 20 2a 20 63 6f 6c 75 6d 6e 73 29 20 2b 20 31 5d   * columns) + 1]
10130 29 3b 0a 09 20 20 73 74 72 63 70 79 20 28 63 6f  );..  strcpy (co
10140 6c 5f 69 6e 64 65 78 2c 20 72 65 73 75 6c 74 73  l_index, results
10150 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 20 2b  [(i * columns) +
10160 20 32 5d 29 3b 0a 09 20 20 69 66 20 28 6d 65 74   2]);..  if (met
10170 61 64 61 74 61 5f 76 65 72 73 69 6f 6e 20 3d 3d  adata_version ==
10180 20 31 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20   1)..    {.../* 
10190 6c 65 67 61 63 79 20 6d 65 74 61 64 61 74 61 20  legacy metadata 
101a0 73 74 79 6c 65 20 3c 3d 20 76 2e 33 2e 31 2e 30  style <= v.3.1.0
101b0 20 2a 2f 0a 09 09 73 74 72 63 70 79 20 28 63 6f   */...strcpy (co
101c0 6c 5f 64 69 6d 73 2c 20 72 65 73 75 6c 74 73 5b  l_dims, results[
101d0 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 20 2b 20  (i * columns) + 
101e0 33 5d 29 3b 0a 09 09 64 69 6d 73 20 3d 20 47 41  3]);...dims = GA
101f0 49 41 5f 58 59 3b 0a 09 09 69 66 20 28 73 74 72  IA_XY;...if (str
10200 63 61 73 65 63 6d 70 20 28 63 6f 6c 5f 64 69 6d  casecmp (col_dim
10210 73 2c 20 22 58 59 5a 22 29 20 3d 3d 20 30 29 0a  s, "XYZ") == 0).
10220 09 09 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49  ..    dims = GAI
10230 41 5f 58 59 5f 5a 3b 0a 09 09 69 66 20 28 73 74  A_XY_Z;...if (st
10240 72 63 61 73 65 63 6d 70 20 28 63 6f 6c 5f 64 69  rcasecmp (col_di
10250 6d 73 2c 20 22 58 59 4d 22 29 20 3d 3d 20 30 29  ms, "XYM") == 0)
10260 0a 09 09 20 20 20 20 64 69 6d 73 20 3d 20 47 41  ...    dims = GA
10270 49 41 5f 58 59 5f 4d 3b 0a 09 09 69 66 20 28 73  IA_XY_M;...if (s
10280 74 72 63 61 73 65 63 6d 70 20 28 63 6f 6c 5f 64  trcasecmp (col_d
10290 69 6d 73 2c 20 22 58 59 5a 4d 22 29 20 3d 3d 20  ims, "XYZM") == 
102a0 30 29 0a 09 09 20 20 20 20 64 69 6d 73 20 3d 20  0)...    dims = 
102b0 47 41 49 41 5f 58 59 5f 5a 5f 4d 3b 0a 09 09 73  GAIA_XY_Z_M;...s
102c0 77 69 74 63 68 20 28 64 69 6d 73 29 0a 09 09 20  witch (dims)... 
102d0 20 7b 0a 09 09 20 20 63 61 73 65 20 47 41 49 41   {...  case GAIA
102e0 5f 58 59 5f 5a 3a 0a 09 09 20 20 20 20 20 20 74  _XY_Z:...      t
102f0 78 74 5f 64 69 6d 73 20 3d 20 22 58 59 5a 22 3b  xt_dims = "XYZ";
10300 0a 09 09 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ...      break;.
10310 09 09 20 20 63 61 73 65 20 47 41 49 41 5f 58 59  ..  case GAIA_XY
10320 5f 4d 3a 0a 09 09 20 20 20 20 20 20 74 78 74 5f  _M:...      txt_
10330 64 69 6d 73 20 3d 20 22 58 59 4d 22 3b 0a 09 09  dims = "XYM";...
10340 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 09 09 20        break;... 
10350 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a 5f   case GAIA_XY_Z_
10360 4d 3a 0a 09 09 20 20 20 20 20 20 74 78 74 5f 64  M:...      txt_d
10370 69 6d 73 20 3d 20 22 58 59 5a 4d 22 3b 0a 09 09  ims = "XYZM";...
10380 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 09 09 20        break;... 
10390 20 64 65 66 61 75 6c 74 3a 0a 09 09 20 20 20 20   default:...    
103a0 20 20 74 78 74 5f 64 69 6d 73 20 3d 20 22 58 59    txt_dims = "XY
103b0 22 3b 0a 09 09 20 20 20 20 20 20 62 72 65 61 6b  ";...      break
103c0 3b 0a 09 09 20 20 7d 3b 0a 09 20 20 20 20 7d 0a  ;...  };..    }.
103d0 09 20 20 69 66 20 28 61 74 6f 69 20 28 63 6f 6c  .  if (atoi (col
103e0 5f 69 6e 64 65 78 29 20 3d 3d 20 31 29 0a 09 20  _index) == 1).. 
103f0 20 20 20 20 20 69 6e 64 65 78 20 3d 20 31 3b 0a       index = 1;.
10400 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20 69  .  else..      i
10410 6e 64 65 78 20 3d 20 30 3b 0a 09 20 20 69 66 20  ndex = 0;..  if 
10420 28 61 74 6f 69 20 28 63 6f 6c 5f 69 6e 64 65 78  (atoi (col_index
10430 29 20 3d 3d 20 32 29 0a 09 20 20 20 20 20 20 63  ) == 2)..      c
10440 61 63 68 65 64 20 3d 20 31 3b 0a 09 20 20 65 6c  ached = 1;..  el
10450 73 65 0a 09 20 20 20 20 20 20 63 61 63 68 65 64  se..      cached
10460 20 3d 20 30 3b 0a 0a 09 20 20 2f 2a 20 74 72 79   = 0;...  /* try
10470 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 6f 6c  ing to delete ol
10480 64 20 76 65 72 73 69 6f 6e 73 20 5b 76 32 2e 30  d versions [v2.0
10490 2c 20 76 32 2e 32 5d 20 74 72 69 67 67 65 72 73  , v2.2] triggers
104a0 5b 69 66 20 61 6e 79 5d 20 2a 2f 0a 09 20 20 73  [if any] */..  s
104b0 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c  trcpy (sqltable,
104c0 20 28 63 68 61 72 20 2a 29 20 74 62 6c 6e 61 6d   (char *) tblnam
104d0 65 29 3b 0a 09 20 20 63 6c 65 61 6e 5f 73 71 6c  e);..  clean_sql
104e0 5f 73 74 72 69 6e 67 20 28 73 71 6c 74 61 62 6c  _string (sqltabl
104f0 65 29 3b 0a 09 20 20 73 74 72 63 70 79 20 28 73  e);..  strcpy (s
10500 71 6c 63 6f 6c 75 6d 6e 2c 20 28 63 68 61 72 20  qlcolumn, (char 
10510 2a 29 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20  *) colname);..  
10520 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67  clean_sql_string
10530 20 28 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 09 20   (sqlcolumn);.. 
10540 20 73 70 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c   sprintf (xname,
10550 20 22 67 74 69 5f 25 73 5f 25 73 22 2c 20 74 62   "gti_%s_%s", tb
10560 6c 6e 61 6d 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b  lname, colname);
10570 0a 09 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65  ..  double_quote
10580 64 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 09  d_sql (xname);..
10590 20 20 73 70 72 69 6e 74 66 20 28 74 72 69 67 67    sprintf (trigg
105a0 65 72 2c 20 22 44 52 4f 50 20 54 52 49 47 47 45  er, "DROP TRIGGE
105b0 52 20 49 46 20 45 58 49 53 54 53 20 25 73 22 2c  R IF EXISTS %s",
105c0 20 78 6e 61 6d 65 29 3b 0a 09 20 20 72 65 74 20   xname);..  ret 
105d0 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
105e0 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72 2c  sqlite, trigger,
105f0 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72   NULL, NULL, &er
10600 72 4d 73 67 29 3b 0a 09 20 20 69 66 20 28 72 65  rMsg);..  if (re
10610 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
10620 09 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  .      goto erro
10630 72 3b 0a 09 20 20 73 70 72 69 6e 74 66 20 28 78  r;..  sprintf (x
10640 6e 61 6d 65 2c 20 22 67 74 75 5f 25 73 5f 25 73  name, "gtu_%s_%s
10650 22 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f 6c 6e  ", tblname, coln
10660 61 6d 65 29 3b 0a 09 20 20 64 6f 75 62 6c 65 5f  ame);..  double_
10670 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d  quoted_sql (xnam
10680 65 29 3b 0a 09 20 20 73 70 72 69 6e 74 66 20 28  e);..  sprintf (
10690 74 72 69 67 67 65 72 2c 20 22 44 52 4f 50 20 54  trigger, "DROP T
106a0 52 49 47 47 45 52 20 49 46 20 45 58 49 53 54 53  RIGGER IF EXISTS
106b0 20 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a 09 20   %s", xname);.. 
106c0 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65   ret = sqlite3_e
106d0 78 65 63 20 28 73 71 6c 69 74 65 2c 20 74 72 69  xec (sqlite, tri
106e0 67 67 65 72 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c  gger, NULL, NULL
106f0 2c 20 26 65 72 72 4d 73 67 29 3b 0a 09 20 20 69  , &errMsg);..  i
10700 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
10710 5f 4f 4b 29 0a 09 20 20 20 20 20 20 67 6f 74 6f  _OK)..      goto
10720 20 65 72 72 6f 72 3b 0a 09 20 20 73 70 72 69 6e   error;..  sprin
10730 74 66 20 28 78 6e 61 6d 65 2c 20 22 67 73 69 5f  tf (xname, "gsi_
10740 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c  %s_%s", tblname,
10750 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20 64 6f   colname);..  do
10760 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
10770 28 78 6e 61 6d 65 29 3b 0a 09 20 20 73 70 72 69  (xname);..  spri
10780 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20 22 44  ntf (trigger, "D
10790 52 4f 50 20 54 52 49 47 47 45 52 20 49 46 20 45  ROP TRIGGER IF E
107a0 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61 6d 65  XISTS %s", xname
107b0 29 3b 0a 09 20 20 72 65 74 20 3d 20 73 71 6c 69  );..  ret = sqli
107c0 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
107d0 2c 20 74 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c  , trigger, NULL,
107e0 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b   NULL, &errMsg);
107f0 0a 09 20 20 69 66 20 28 72 65 74 20 21 3d 20 53  ..  if (ret != S
10800 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20 20 20 20  QLITE_OK)..     
10810 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 09 20 20   goto error;..  
10820 73 70 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20  sprintf (xname, 
10830 22 67 73 75 5f 25 73 5f 25 73 22 2c 20 74 62 6c  "gsu_%s_%s", tbl
10840 6e 61 6d 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a  name, colname);.
10850 09 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64  .  double_quoted
10860 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 20  _sql (xname);.. 
10870 20 73 70 72 69 6e 74 66 20 28 74 72 69 67 67 65   sprintf (trigge
10880 72 2c 20 22 44 52 4f 50 20 54 52 49 47 47 45 52  r, "DROP TRIGGER
10890 20 49 46 20 45 58 49 53 54 53 20 25 73 22 2c 20   IF EXISTS %s", 
108a0 78 6e 61 6d 65 29 3b 0a 09 20 20 72 65 74 20 3d  xname);..  ret =
108b0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73   sqlite3_exec (s
108c0 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72 2c 20  qlite, trigger, 
108d0 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
108e0 4d 73 67 29 3b 0a 09 20 20 69 66 20 28 72 65 74  Msg);..  if (ret
108f0 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09   != SQLITE_OK)..
10900 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72        goto error
10910 3b 0a 09 20 20 2f 2a 20 65 6e 64 20 64 65 6c 65  ;..  /* end dele
10920 74 69 6f 6e 20 6f 6c 64 20 76 65 72 73 69 6f 6e  tion old version
10930 73 20 5b 76 32 2e 30 2c 20 76 32 2e 32 5d 20 74  s [v2.0, v2.2] t
10940 72 69 67 67 65 72 73 5b 69 66 20 61 6e 79 5d 20  riggers[if any] 
10950 2a 2f 0a 0a 09 20 20 2f 2a 20 64 65 6c 65 74 69  */...  /* deleti
10960 6e 67 20 74 68 65 20 6f 6c 64 20 49 4e 53 45 52  ng the old INSER
10970 54 20 74 72 69 67 67 65 72 20 54 59 50 45 20 5b  T trigger TYPE [
10980 69 66 20 61 6e 79 5d 20 2a 2f 0a 09 20 20 73 70  if any] */..  sp
10990 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67  rintf (xname, "g
109a0 67 69 5f 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61  gi_%s_%s", tblna
109b0 6d 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20  me, colname);.. 
109c0 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
109d0 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 20 20 73  ql (xname);..  s
109e0 70 72 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c  printf (trigger,
109f0 20 22 44 52 4f 50 20 54 52 49 47 47 45 52 20 49   "DROP TRIGGER I
10a00 46 20 45 58 49 53 54 53 20 25 73 22 2c 20 78 6e  F EXISTS %s", xn
10a10 61 6d 65 29 3b 0a 09 20 20 72 65 74 20 3d 20 73  ame);..  ret = s
10a20 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
10a30 69 74 65 2c 20 74 72 69 67 67 65 72 2c 20 4e 55  ite, trigger, NU
10a40 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
10a50 67 29 3b 0a 09 20 20 69 66 20 28 72 65 74 20 21  g);..  if (ret !
10a60 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20  = SQLITE_OK)..  
10a70 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a      goto error;.
10a80 09 20 20 2f 2a 20 69 6e 73 65 72 74 69 6e 67 20  .  /* inserting 
10a90 74 68 65 20 6e 65 77 20 49 4e 53 45 52 54 20 74  the new INSERT t
10aa0 72 69 67 67 65 72 20 54 59 50 45 20 2a 2f 0a 09  rigger TYPE */..
10ab0 20 20 73 74 72 63 70 79 20 28 78 74 61 62 6c 65    strcpy (xtable
10ac0 2c 20 74 62 6c 6e 61 6d 65 29 3b 0a 09 20 20 64  , tblname);..  d
10ad0 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c  ouble_quoted_sql
10ae0 20 28 78 74 61 62 6c 65 29 3b 0a 09 20 20 73 74   (xtable);..  st
10af0 72 63 70 79 20 28 78 63 6f 6c 6e 61 6d 65 2c 20  rcpy (xcolname, 
10b00 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20 64 6f 75  colname);..  dou
10b10 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
10b20 78 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20 73 70  xcolname);..  sp
10b30 72 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20  rintf (trigger, 
10b40 22 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20  "CREATE TRIGGER 
10b50 25 73 20 42 45 46 4f 52 45 20 49 4e 53 45 52 54  %s BEFORE INSERT
10b60 20 4f 4e 20 25 73 5c 6e 22 2c 20 78 6e 61 6d 65   ON %s\n", xname
10b70 2c 0a 09 09 20 20 20 78 74 61 62 6c 65 29 3b 0a  ,...   xtable);.
10b80 09 20 20 73 74 72 63 61 74 20 28 74 72 69 67 67  .  strcat (trigg
10b90 65 72 2c 20 22 46 4f 52 20 45 41 43 48 20 52 4f  er, "FOR EACH RO
10ba0 57 20 42 45 47 49 4e 5c 6e 22 29 3b 0a 09 20 20  W BEGIN\n");..  
10bb0 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 0a  sprintf (dummy,.
10bc0 09 09 20 20 20 22 53 45 4c 45 43 54 20 52 41 49  ..   "SELECT RAI
10bd0 53 45 28 52 4f 4c 4c 42 41 43 4b 2c 20 27 25 73  SE(ROLLBACK, '%s
10be0 2e 25 73 20 76 69 6f 6c 61 74 65 73 20 47 65 6f  .%s violates Geo
10bf0 6d 65 74 72 79 20 63 6f 6e 73 74 72 61 69 6e 74  metry constraint
10c00 20 5b 67 65 6f 6d 2d 74 79 70 65 20 6f 72 20 53   [geom-type or S
10c10 52 49 44 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 5d  RID not allowed]
10c20 27 29 5c 6e 22 2c 0a 09 09 20 20 20 73 71 6c 74  ')\n",...   sqlt
10c30 61 62 6c 65 2c 20 73 71 6c 63 6f 6c 75 6d 6e 29  able, sqlcolumn)
10c40 3b 0a 09 20 20 73 74 72 63 61 74 20 28 74 72 69  ;..  strcat (tri
10c50 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 20  gger, dummy);.. 
10c60 20 69 66 20 28 6d 65 74 61 64 61 74 61 5f 76 65   if (metadata_ve
10c70 72 73 69 6f 6e 20 3d 3d 20 33 29 0a 09 20 20 20  rsion == 3)..   
10c80 20 7b 0a 09 09 2f 2a 20 63 75 72 72 65 6e 74 20   {.../* current 
10c90 6d 65 74 61 64 61 74 61 20 73 74 79 6c 65 20 3e  metadata style >
10ca0 3d 20 76 2e 34 2e 30 2e 30 20 2a 2f 0a 09 09 73  = v.4.0.0 */...s
10cb0 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 0a  trcat (trigger,.
10cc0 09 09 09 22 57 48 45 52 45 20 28 53 45 4c 45 43  ..."WHERE (SELEC
10cd0 54 20 67 65 6f 6d 65 74 72 79 5f 74 79 70 65 20  T geometry_type 
10ce0 46 52 4f 4d 20 67 65 6f 6d 65 74 72 79 5f 63 6f  FROM geometry_co
10cf0 6c 75 6d 6e 73 5c 6e 22 29 3b 0a 09 09 73 70 72  lumns\n");...spr
10d00 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22 57 48  intf (dummy, "WH
10d10 45 52 45 20 55 70 70 65 72 28 66 5f 74 61 62 6c  ERE Upper(f_tabl
10d20 65 5f 6e 61 6d 65 29 20 3d 20 55 70 70 65 72 28  e_name) = Upper(
10d30 27 25 73 27 29 20 41 4e 44 20 22 2c 0a 09 09 09  '%s') AND ",....
10d40 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09 09 73 74   sqltable);...st
10d50 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 64  rcat (trigger, d
10d60 75 6d 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74 66  ummy);...sprintf
10d70 20 28 64 75 6d 6d 79 2c 20 22 55 70 70 65 72 28   (dummy, "Upper(
10d80 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  f_geometry_colum
10d90 6e 29 20 3d 20 55 70 70 65 72 28 27 25 73 27 29  n) = Upper('%s')
10da0 5c 6e 22 2c 0a 09 09 09 20 73 71 6c 63 6f 6c 75  \n",.... sqlcolu
10db0 6d 6e 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74  mn);...strcat (t
10dc0 72 69 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a  rigger, dummy);.
10dd0 09 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79  ..sprintf (dummy
10de0 2c 0a 09 09 09 20 22 41 4e 44 20 47 65 6f 6d 65  ,.... "AND Geome
10df0 74 72 79 43 6f 6e 73 74 72 61 69 6e 74 73 28 4e  tryConstraints(N
10e00 45 57 2e 25 73 2c 20 67 65 6f 6d 65 74 72 79 5f  EW.%s, geometry_
10e10 74 79 70 65 2c 20 73 72 69 64 29 20 3d 20 31 29  type, srid) = 1)
10e20 20 49 53 20 4e 55 4c 4c 3b 5c 6e 22 2c 0a 09 09   IS NULL;\n",...
10e30 09 20 78 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20  . xcolname);..  
10e40 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20    }..  else..   
10e50 20 7b 0a 09 09 2f 2a 20 6c 65 67 61 63 79 20 6d   {.../* legacy m
10e60 65 74 61 64 61 74 61 20 73 74 79 6c 65 20 3c 3d  etadata style <=
10e70 20 76 2e 33 2e 31 2e 30 20 2a 2f 0a 09 09 73 74   v.3.1.0 */...st
10e80 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 22  rcat (trigger, "
10e90 57 48 45 52 45 20 28 53 45 4c 45 43 54 20 74 79  WHERE (SELECT ty
10ea0 70 65 20 46 52 4f 4d 20 67 65 6f 6d 65 74 72 79  pe FROM geometry
10eb0 5f 63 6f 6c 75 6d 6e 73 5c 6e 22 29 3b 0a 09 09  _columns\n");...
10ec0 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 0a  sprintf (dummy,.
10ed0 09 09 09 20 22 57 48 45 52 45 20 66 5f 74 61 62  ... "WHERE f_tab
10ee0 6c 65 5f 6e 61 6d 65 20 3d 20 27 25 73 27 20 41  le_name = '%s' A
10ef0 4e 44 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f  ND f_geometry_co
10f00 6c 75 6d 6e 20 3d 20 27 25 73 27 5c 6e 22 2c 0a  lumn = '%s'\n",.
10f10 09 09 09 20 73 71 6c 74 61 62 6c 65 2c 20 73 71  ... sqltable, sq
10f20 6c 63 6f 6c 75 6d 6e 29 3b 0a 09 09 73 74 72 63  lcolumn);...strc
10f30 61 74 20 28 74 72 69 67 67 65 72 2c 20 64 75 6d  at (trigger, dum
10f40 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28  my);...sprintf (
10f50 64 75 6d 6d 79 2c 0a 09 09 09 20 22 41 4e 44 20  dummy,.... "AND 
10f60 47 65 6f 6d 65 74 72 79 43 6f 6e 73 74 72 61 69  GeometryConstrai
10f70 6e 74 73 28 4e 45 57 2e 25 73 2c 20 74 79 70 65  nts(NEW.%s, type
10f80 2c 20 73 72 69 64 2c 20 27 25 73 27 29 20 3d 20  , srid, '%s') = 
10f90 31 29 20 49 53 20 4e 55 4c 4c 3b 5c 6e 22 2c 0a  1) IS NULL;\n",.
10fa0 09 09 09 20 78 63 6f 6c 6e 61 6d 65 2c 20 74 78  ... xcolname, tx
10fb0 74 5f 64 69 6d 73 29 3b 0a 09 20 20 20 20 7d 0a  t_dims);..    }.
10fc0 09 20 20 73 74 72 63 61 74 20 28 74 72 69 67 67  .  strcat (trigg
10fd0 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 20 20 73  er, dummy);..  s
10fe0 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20  trcat (trigger, 
10ff0 22 45 4e 44 3b 22 29 3b 0a 09 20 20 72 65 74 20  "END;");..  ret 
11000 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
11010 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72 2c  sqlite, trigger,
11020 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72   NULL, NULL, &er
11030 72 4d 73 67 29 3b 0a 09 20 20 69 66 20 28 72 65  rMsg);..  if (re
11040 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
11050 09 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  .      goto erro
11060 72 3b 0a 09 20 20 2f 2a 20 64 65 6c 65 74 69 6e  r;..  /* deletin
11070 67 20 74 68 65 20 6f 6c 64 20 55 50 44 41 54 45  g the old UPDATE
11080 20 74 72 69 67 67 65 72 20 54 59 50 45 20 5b 69   trigger TYPE [i
11090 66 20 61 6e 79 5d 20 2a 2f 0a 09 20 20 73 70 72  f any] */..  spr
110a0 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67 67  intf (xname, "gg
110b0 75 5f 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d  u_%s_%s", tblnam
110c0 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20  e, colname);..  
110d0 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
110e0 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 20 20 73 70  l (xname);..  sp
110f0 72 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20  rintf (trigger, 
11100 22 44 52 4f 50 20 54 52 49 47 47 45 52 20 49 46  "DROP TRIGGER IF
11110 20 45 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61   EXISTS %s", xna
11120 6d 65 29 3b 0a 09 20 20 72 65 74 20 3d 20 73 71  me);..  ret = sq
11130 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
11140 74 65 2c 20 74 72 69 67 67 65 72 2c 20 4e 55 4c  te, trigger, NUL
11150 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
11160 29 3b 0a 09 20 20 69 66 20 28 72 65 74 20 21 3d  );..  if (ret !=
11170 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20 20   SQLITE_OK)..   
11180 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 09     goto error;..
11190 20 20 2f 2a 20 69 6e 73 65 72 74 69 6e 67 20 74    /* inserting t
111a0 68 65 20 6e 65 77 20 55 50 44 41 54 45 20 74 72  he new UPDATE tr
111b0 69 67 67 65 72 20 54 59 50 45 20 2a 2f 0a 09 20  igger TYPE */.. 
111c0 20 73 70 72 69 6e 74 66 20 28 74 72 69 67 67 65   sprintf (trigge
111d0 72 2c 20 22 43 52 45 41 54 45 20 54 52 49 47 47  r, "CREATE TRIGG
111e0 45 52 20 25 73 20 42 45 46 4f 52 45 20 55 50 44  ER %s BEFORE UPD
111f0 41 54 45 20 4f 4e 20 25 73 5c 6e 22 2c 20 78 6e  ATE ON %s\n", xn
11200 61 6d 65 2c 0a 09 09 20 20 20 78 74 61 62 6c 65  ame,...   xtable
11210 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 74 72  );..  strcat (tr
11220 69 67 67 65 72 2c 20 22 46 4f 52 20 45 41 43 48  igger, "FOR EACH
11230 20 52 4f 57 20 42 45 47 49 4e 5c 6e 22 29 3b 0a   ROW BEGIN\n");.
11240 09 20 20 73 70 72 69 6e 74 66 20 28 64 75 6d 6d  .  sprintf (dumm
11250 79 2c 0a 09 09 20 20 20 22 53 45 4c 45 43 54 20  y,...   "SELECT 
11260 52 41 49 53 45 28 52 4f 4c 4c 42 41 43 4b 2c 20  RAISE(ROLLBACK, 
11270 27 25 73 2e 25 73 20 76 69 6f 6c 61 74 65 73 20  '%s.%s violates 
11280 47 65 6f 6d 65 74 72 79 20 63 6f 6e 73 74 72 61  Geometry constra
11290 69 6e 74 20 5b 67 65 6f 6d 2d 74 79 70 65 20 6f  int [geom-type o
112a0 72 20 53 52 49 44 20 6e 6f 74 20 61 6c 6c 6f 77  r SRID not allow
112b0 65 64 5d 27 29 5c 6e 22 2c 0a 09 09 20 20 20 73  ed]')\n",...   s
112c0 71 6c 74 61 62 6c 65 2c 20 73 71 6c 63 6f 6c 75  qltable, sqlcolu
112d0 6d 6e 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28  mn);..  strcat (
112e0 74 72 69 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b  trigger, dummy);
112f0 0a 09 20 20 69 66 20 28 6d 65 74 61 64 61 74 61  ..  if (metadata
11300 5f 76 65 72 73 69 6f 6e 20 3d 3d 20 33 29 0a 09  _version == 3)..
11310 20 20 20 20 7b 0a 09 09 2f 2a 20 63 75 72 72 65      {.../* curre
11320 6e 74 20 6d 65 74 61 64 61 74 61 20 73 74 79 6c  nt metadata styl
11330 65 20 3e 3d 20 76 2e 34 2e 30 2e 30 20 2a 2f 0a  e >= v.4.0.0 */.
11340 09 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65  ..strcat (trigge
11350 72 2c 0a 09 09 09 22 57 48 45 52 45 20 28 53 45  r,...."WHERE (SE
11360 4c 45 43 54 20 67 65 6f 6d 65 74 72 79 5f 74 79  LECT geometry_ty
11370 70 65 20 46 52 4f 4d 20 67 65 6f 6d 65 74 72 79  pe FROM geometry
11380 5f 63 6f 6c 75 6d 6e 73 5c 6e 22 29 3b 0a 09 09  _columns\n");...
11390 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20  sprintf (dummy, 
113a0 22 57 48 45 52 45 20 55 70 70 65 72 28 66 5f 74  "WHERE Upper(f_t
113b0 61 62 6c 65 5f 6e 61 6d 65 29 20 3d 20 55 70 70  able_name) = Upp
113c0 65 72 28 27 25 73 27 29 20 41 4e 44 20 22 2c 0a  er('%s') AND ",.
113d0 09 09 09 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09  ... sqltable);..
113e0 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65 72  .strcat (trigger
113f0 2c 20 64 75 6d 6d 79 29 3b 0a 09 09 73 70 72 69  , dummy);...spri
11400 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22 55 70 70  ntf (dummy, "Upp
11410 65 72 28 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f  er(f_geometry_co
11420 6c 75 6d 6e 29 20 3d 20 55 70 70 65 72 28 27 25  lumn) = Upper('%
11430 73 27 29 5c 6e 22 2c 0a 09 09 09 20 73 71 6c 63  s')\n",.... sqlc
11440 6f 6c 75 6d 6e 29 3b 0a 09 09 73 74 72 63 61 74  olumn);...strcat
11450 20 28 74 72 69 67 67 65 72 2c 20 64 75 6d 6d 79   (trigger, dummy
11460 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 64 75  );...sprintf (du
11470 6d 6d 79 2c 0a 09 09 09 20 22 41 4e 44 20 47 65  mmy,.... "AND Ge
11480 6f 6d 65 74 72 79 43 6f 6e 73 74 72 61 69 6e 74  ometryConstraint
11490 73 28 4e 45 57 2e 25 73 2c 20 67 65 6f 6d 65 74  s(NEW.%s, geomet
114a0 72 79 5f 74 79 70 65 2c 20 73 72 69 64 29 20 3d  ry_type, srid) =
114b0 20 31 29 20 49 53 20 4e 55 4c 4c 3b 5c 6e 22 2c   1) IS NULL;\n",
114c0 0a 09 09 09 20 78 63 6f 6c 6e 61 6d 65 29 3b 0a  .... xcolname);.
114d0 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09  .    }..  else..
114e0 20 20 20 20 7b 0a 09 09 2f 2a 20 6c 65 67 61 63      {.../* legac
114f0 79 20 6d 65 74 61 64 61 74 61 20 73 74 79 6c 65  y metadata style
11500 20 3c 3d 20 76 2e 33 2e 31 2e 30 20 2a 2f 0a 09   <= v.3.1.0 */..
11510 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65 72  .strcat (trigger
11520 2c 20 22 57 48 45 52 45 20 28 53 45 4c 45 43 54  , "WHERE (SELECT
11530 20 74 79 70 65 20 46 52 4f 4d 20 67 65 6f 6d 65   type FROM geome
11540 74 72 79 5f 63 6f 6c 75 6d 6e 73 5c 6e 22 29 3b  try_columns\n");
11550 0a 09 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d  ...sprintf (dumm
11560 79 2c 0a 09 09 09 20 22 57 48 45 52 45 20 66 5f  y,.... "WHERE f_
11570 74 61 62 6c 65 5f 6e 61 6d 65 20 3d 20 27 25 73  table_name = '%s
11580 27 20 41 4e 44 20 66 5f 67 65 6f 6d 65 74 72 79  ' AND f_geometry
11590 5f 63 6f 6c 75 6d 6e 20 3d 20 27 25 73 27 5c 6e  _column = '%s'\n
115a0 22 2c 0a 09 09 09 20 73 71 6c 74 61 62 6c 65 2c  ",.... sqltable,
115b0 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 09 09 73   sqlcolumn);...s
115c0 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20  trcat (trigger, 
115d0 64 75 6d 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74  dummy);...sprint
115e0 66 20 28 64 75 6d 6d 79 2c 0a 09 09 09 20 22 41  f (dummy,.... "A
115f0 4e 44 20 47 65 6f 6d 65 74 72 79 43 6f 6e 73 74  ND GeometryConst
11600 72 61 69 6e 74 73 28 4e 45 57 2e 25 73 2c 20 74  raints(NEW.%s, t
11610 79 70 65 2c 20 73 72 69 64 2c 20 27 25 73 27 29  ype, srid, '%s')
11620 20 3d 20 31 29 20 49 53 20 4e 55 4c 4c 3b 5c 6e   = 1) IS NULL;\n
11630 22 2c 0a 09 09 09 20 78 63 6f 6c 6e 61 6d 65 2c  ",.... xcolname,
11640 20 74 78 74 5f 64 69 6d 73 29 3b 0a 09 20 20 20   txt_dims);..   
11650 20 7d 0a 09 20 20 73 74 72 63 61 74 20 28 74 72   }..  strcat (tr
11660 69 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09  igger, dummy);..
11670 20 20 73 74 72 63 61 74 20 28 74 72 69 67 67 65    strcat (trigge
11680 72 2c 20 22 45 4e 44 3b 22 29 3b 0a 09 20 20 72  r, "END;");..  r
11690 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
116a0 63 20 28 73 71 6c 69 74 65 2c 20 74 72 69 67 67  c (sqlite, trigg
116b0 65 72 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  er, NULL, NULL, 
116c0 26 65 72 72 4d 73 67 29 3b 0a 09 20 20 69 66 20  &errMsg);..  if 
116d0 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
116e0 4b 29 0a 09 20 20 20 20 20 20 67 6f 74 6f 20 65  K)..      goto e
116f0 72 72 6f 72 3b 0a 09 20 20 69 66 20 28 6d 65 74  rror;..  if (met
11700 61 64 61 74 61 5f 76 65 72 73 69 6f 6e 20 3d 3d  adata_version ==
11710 20 33 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20   3)..    {.../* 
11720 63 75 72 72 65 6e 74 20 6d 65 74 61 64 61 74 61  current metadata
11730 20 73 74 79 6c 65 20 3e 3d 20 76 2e 34 2e 30 2e   style >= v.4.0.
11740 30 20 2a 2f 0a 0a 09 09 2f 2a 20 64 65 6c 65 74  0 */..../* delet
11750 69 6e 67 20 74 68 65 20 6f 6c 64 20 55 50 44 41  ing the old UPDA
11760 54 45 20 28 74 69 6d 65 73 74 61 6d 70 29 20 74  TE (timestamp) t
11770 72 69 67 67 65 72 20 5b 69 66 20 61 6e 79 5d 20  rigger [if any] 
11780 2a 2f 0a 09 09 73 70 72 69 6e 74 66 20 28 78 6e  */...sprintf (xn
11790 61 6d 65 2c 20 22 74 6d 75 5f 25 73 5f 25 73 22  ame, "tmu_%s_%s"
117a0 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f 6c 6e 61  , tblname, colna
117b0 6d 65 29 3b 0a 09 09 64 6f 75 62 6c 65 5f 71 75  me);...double_qu
117c0 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d 65 29  oted_sql (xname)
117d0 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 74 72 69  ;...sprintf (tri
117e0 67 67 65 72 2c 20 22 44 52 4f 50 20 54 52 49 47  gger, "DROP TRIG
117f0 47 45 52 20 49 46 20 45 58 49 53 54 53 20 25 73  GER IF EXISTS %s
11800 22 2c 20 78 6e 61 6d 65 29 3b 0a 09 09 72 65 74  ", xname);...ret
11810 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
11820 28 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72  (sqlite, trigger
11830 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
11840 72 72 4d 73 67 29 3b 0a 09 09 69 66 20 28 72 65  rrMsg);...if (re
11850 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
11860 09 09 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ..    goto error
11870 3b 0a 09 09 2f 2a 20 69 6e 73 65 72 74 69 6e 67  ;.../* inserting
11880 20 74 68 65 20 6e 65 77 20 55 50 44 41 54 45 20   the new UPDATE 
11890 28 74 69 6d 65 73 74 61 6d 70 29 20 74 72 69 67  (timestamp) trig
118a0 67 65 72 20 2a 2f 0a 09 09 73 70 72 69 6e 74 66  ger */...sprintf
118b0 20 28 78 6e 61 6d 65 2c 20 22 74 6d 75 5f 25 73   (xname, "tmu_%s
118c0 5f 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c 20 63  _%s", tblname, c
118d0 6f 6c 6e 61 6d 65 29 3b 0a 09 09 64 6f 75 62 6c  olname);...doubl
118e0 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e  e_quoted_sql (xn
118f0 61 6d 65 29 3b 0a 09 09 73 70 72 69 6e 74 66 20  ame);...sprintf 
11900 28 74 72 69 67 67 65 72 2c 20 22 43 52 45 41 54  (trigger, "CREAT
11910 45 20 54 52 49 47 47 45 52 20 25 73 20 41 46 54  E TRIGGER %s AFT
11920 45 52 20 55 50 44 41 54 45 20 4f 4e 20 25 73 5c  ER UPDATE ON %s\
11930 6e 22 2c 0a 09 09 09 20 78 6e 61 6d 65 2c 20 78  n",.... xname, x
11940 74 61 62 6c 65 29 3b 0a 09 09 73 74 72 63 61 74  table);...strcat
11950 20 28 74 72 69 67 67 65 72 2c 20 22 46 4f 52 20   (trigger, "FOR 
11960 45 41 43 48 20 52 4f 57 20 42 45 47 49 4e 5c 6e  EACH ROW BEGIN\n
11970 22 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72  ");...strcat (tr
11980 69 67 67 65 72 2c 0a 09 09 09 22 55 50 44 41 54  igger,...."UPDAT
11990 45 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  E geometry_colum
119a0 6e 73 5f 74 69 6d 65 20 53 45 54 20 6c 61 73 74  ns_time SET last
119b0 5f 75 70 64 61 74 65 20 3d 20 64 61 74 65 74 69  _update = dateti
119c0 6d 65 28 27 6e 6f 77 27 29 5c 6e 22 29 3b 0a 09  me('now')\n");..
119d0 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c  .sprintf (dummy,
119e0 20 22 57 48 45 52 45 20 55 70 70 65 72 28 66 5f   "WHERE Upper(f_
119f0 74 61 62 6c 65 5f 6e 61 6d 65 29 20 3d 20 55 70  table_name) = Up
11a00 70 65 72 28 27 25 73 27 29 20 41 4e 44 20 22 2c  per('%s') AND ",
11a10 0a 09 09 09 20 73 71 6c 74 61 62 6c 65 29 3b 0a  .... sqltable);.
11a20 09 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65  ..strcat (trigge
11a30 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 09 73 70 72  r, dummy);...spr
11a40 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22 55 70  intf (dummy, "Up
11a50 70 65 72 28 66 5f 67 65 6f 6d 65 74 72 79 5f 63  per(f_geometry_c
11a60 6f 6c 75 6d 6e 29 20 3d 20 55 70 70 65 72 28 27  olumn) = Upper('
11a70 25 73 27 29 3b 5c 6e 22 2c 0a 09 09 09 20 73 71  %s');\n",.... sq
11a80 6c 63 6f 6c 75 6d 6e 29 3b 0a 09 09 73 74 72 63  lcolumn);...strc
11a90 61 74 20 28 74 72 69 67 67 65 72 2c 20 64 75 6d  at (trigger, dum
11aa0 6d 79 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74  my);...strcat (t
11ab0 72 69 67 67 65 72 2c 20 22 45 4e 44 3b 22 29 3b  rigger, "END;");
11ac0 0a 09 09 72 65 74 20 3d 20 73 71 6c 69 74 65 33  ...ret = sqlite3
11ad0 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 74  _exec (sqlite, t
11ae0 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c 20 4e 55  rigger, NULL, NU
11af0 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 09 09  LL, &errMsg);...
11b00 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
11b10 45 5f 4f 4b 29 0a 09 09 20 20 20 20 67 6f 74 6f  E_OK)...    goto
11b20 20 65 72 72 6f 72 3b 0a 0a 09 09 2f 2a 20 64 65   error;..../* de
11b30 6c 65 74 69 6e 67 20 74 68 65 20 6f 6c 64 20 49  leting the old I
11b40 4e 53 45 52 54 20 28 74 69 6d 65 73 74 61 6d 70  NSERT (timestamp
11b50 29 20 74 72 69 67 67 65 72 20 5b 69 66 20 61 6e  ) trigger [if an
11b60 79 5d 20 2a 2f 0a 09 09 73 70 72 69 6e 74 66 20  y] */...sprintf 
11b70 28 78 6e 61 6d 65 2c 20 22 74 6d 69 5f 25 73 5f  (xname, "tmi_%s_
11b80 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f  %s", tblname, co
11b90 6c 6e 61 6d 65 29 3b 0a 09 09 64 6f 75 62 6c 65  lname);...double
11ba0 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61  _quoted_sql (xna
11bb0 6d 65 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28  me);...sprintf (
11bc0 74 72 69 67 67 65 72 2c 20 22 44 52 4f 50 20 54  trigger, "DROP T
11bd0 52 49 47 47 45 52 20 49 46 20 45 58 49 53 54 53  RIGGER IF EXISTS
11be0 20 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a 09 09   %s", xname);...
11bf0 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78  ret = sqlite3_ex
11c00 65 63 20 28 73 71 6c 69 74 65 2c 20 74 72 69 67  ec (sqlite, trig
11c10 67 65 72 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c  ger, NULL, NULL,
11c20 20 26 65 72 72 4d 73 67 29 3b 0a 09 09 69 66 20   &errMsg);...if 
11c30 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
11c40 4b 29 0a 09 09 20 20 20 20 67 6f 74 6f 20 65 72  K)...    goto er
11c50 72 6f 72 3b 0a 09 09 2f 2a 20 69 6e 73 65 72 74  ror;.../* insert
11c60 69 6e 67 20 74 68 65 20 6e 65 77 20 49 4e 53 45  ing the new INSE
11c70 52 54 20 28 74 69 6d 65 73 74 61 6d 70 29 20 74  RT (timestamp) t
11c80 72 69 67 67 65 72 20 2a 2f 0a 09 09 73 70 72 69  rigger */...spri
11c90 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 74 6d 69  ntf (xname, "tmi
11ca0 5f 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d 65  _%s_%s", tblname
11cb0 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 64 6f  , colname);...do
11cc0 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
11cd0 28 78 6e 61 6d 65 29 3b 0a 09 09 73 70 72 69 6e  (xname);...sprin
11ce0 74 66 20 28 74 72 69 67 67 65 72 2c 20 22 43 52  tf (trigger, "CR
11cf0 45 41 54 45 20 54 52 49 47 47 45 52 20 25 73 20  EATE TRIGGER %s 
11d00 41 46 54 45 52 20 49 4e 53 45 52 54 20 4f 4e 20  AFTER INSERT ON 
11d10 25 73 5c 6e 22 2c 0a 09 09 09 20 78 6e 61 6d 65  %s\n",.... xname
11d20 2c 20 78 74 61 62 6c 65 29 3b 0a 09 09 73 74 72  , xtable);...str
11d30 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 22 46  cat (trigger, "F
11d40 4f 52 20 45 41 43 48 20 52 4f 57 20 42 45 47 49  OR EACH ROW BEGI
11d50 4e 5c 6e 22 29 3b 0a 09 09 73 74 72 63 61 74 20  N\n");...strcat 
11d60 28 74 72 69 67 67 65 72 2c 0a 09 09 09 22 55 50  (trigger,...."UP
11d70 44 41 54 45 20 67 65 6f 6d 65 74 72 79 5f 63 6f  DATE geometry_co
11d80 6c 75 6d 6e 73 5f 74 69 6d 65 20 53 45 54 20 6c  lumns_time SET l
11d90 61 73 74 5f 69 6e 73 65 72 74 20 3d 20 64 61 74  ast_insert = dat
11da0 65 74 69 6d 65 28 27 6e 6f 77 27 29 5c 6e 22 29  etime('now')\n")
11db0 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 64 75 6d  ;...sprintf (dum
11dc0 6d 79 2c 20 22 57 48 45 52 45 20 55 70 70 65 72  my, "WHERE Upper
11dd0 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 20 3d  (f_table_name) =
11de0 20 55 70 70 65 72 28 27 25 73 27 29 20 41 4e 44   Upper('%s') AND
11df0 20 22 2c 0a 09 09 09 20 73 71 6c 74 61 62 6c 65   ",.... sqltable
11e00 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72 69  );...strcat (tri
11e10 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 09  gger, dummy);...
11e20 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20  sprintf (dummy, 
11e30 22 55 70 70 65 72 28 66 5f 67 65 6f 6d 65 74 72  "Upper(f_geometr
11e40 79 5f 63 6f 6c 75 6d 6e 29 20 3d 20 55 70 70 65  y_column) = Uppe
11e50 72 28 27 25 73 27 29 3b 5c 6e 22 2c 0a 09 09 09  r('%s');\n",....
11e60 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 09 09 73   sqlcolumn);...s
11e70 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20  trcat (trigger, 
11e80 64 75 6d 6d 79 29 3b 0a 09 09 73 74 72 63 61 74  dummy);...strcat
11e90 20 28 74 72 69 67 67 65 72 2c 20 22 45 4e 44 3b   (trigger, "END;
11ea0 22 29 3b 0a 09 09 72 65 74 20 3d 20 73 71 6c 69  ");...ret = sqli
11eb0 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
11ec0 2c 20 74 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c  , trigger, NULL,
11ed0 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b   NULL, &errMsg);
11ee0 0a 09 09 69 66 20 28 72 65 74 20 21 3d 20 53 51  ...if (ret != SQ
11ef0 4c 49 54 45 5f 4f 4b 29 0a 09 09 20 20 20 20 67  LITE_OK)...    g
11f00 6f 74 6f 20 65 72 72 6f 72 3b 0a 0a 09 09 2f 2a  oto error;..../*
11f10 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6f 6c   deleting the ol
11f20 64 20 44 45 4c 45 54 45 20 28 74 69 6d 65 73 74  d DELETE (timest
11f30 61 6d 70 29 20 74 72 69 67 67 65 72 20 5b 69 66  amp) trigger [if
11f40 20 61 6e 79 5d 20 2a 2f 0a 09 09 73 70 72 69 6e   any] */...sprin
11f50 74 66 20 28 78 6e 61 6d 65 2c 20 22 74 6d 64 5f  tf (xname, "tmd_
11f60 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c  %s_%s", tblname,
11f70 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 64 6f 75   colname);...dou
11f80 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
11f90 78 6e 61 6d 65 29 3b 0a 09 09 73 70 72 69 6e 74  xname);...sprint
11fa0 66 20 28 74 72 69 67 67 65 72 2c 20 22 44 52 4f  f (trigger, "DRO
11fb0 50 20 54 52 49 47 47 45 52 20 49 46 20 45 58 49  P TRIGGER IF EXI
11fc0 53 54 53 20 25 73 22 2c 20 78 6e 61 6d 65 29 3b  STS %s", xname);
11fd0 0a 09 09 72 65 74 20 3d 20 73 71 6c 69 74 65 33  ...ret = sqlite3
11fe0 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 74  _exec (sqlite, t
11ff0 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c 20 4e 55  rigger, NULL, NU
12000 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 09 09  LL, &errMsg);...
12010 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
12020 45 5f 4f 4b 29 0a 09 09 20 20 20 20 67 6f 74 6f  E_OK)...    goto
12030 20 65 72 72 6f 72 3b 0a 09 09 2f 2a 20 69 6e 73   error;.../* ins
12040 65 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20 44  erting the new D
12050 45 4c 45 54 45 20 28 74 69 6d 65 73 74 61 6d 70  ELETE (timestamp
12060 29 20 74 72 69 67 67 65 72 20 2a 2f 0a 09 09 73  ) trigger */...s
12070 70 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22  printf (xname, "
12080 74 6d 64 5f 25 73 5f 25 73 22 2c 20 74 62 6c 6e  tmd_%s_%s", tbln
12090 61 6d 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09  ame, colname);..
120a0 09 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73  .double_quoted_s
120b0 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 09 73 70  ql (xname);...sp
120c0 72 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20  rintf (trigger, 
120d0 22 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20  "CREATE TRIGGER 
120e0 25 73 20 41 46 54 45 52 20 44 45 4c 45 54 45 20  %s AFTER DELETE 
120f0 4f 4e 20 25 73 5c 6e 22 2c 0a 09 09 09 20 78 6e  ON %s\n",.... xn
12100 61 6d 65 2c 20 78 74 61 62 6c 65 29 3b 0a 09 09  ame, xtable);...
12110 73 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c  strcat (trigger,
12120 20 22 46 4f 52 20 45 41 43 48 20 52 4f 57 20 42   "FOR EACH ROW B
12130 45 47 49 4e 5c 6e 22 29 3b 0a 09 09 73 74 72 63  EGIN\n");...strc
12140 61 74 20 28 74 72 69 67 67 65 72 2c 0a 09 09 09  at (trigger,....
12150 22 55 50 44 41 54 45 20 67 65 6f 6d 65 74 72 79  "UPDATE geometry
12160 5f 63 6f 6c 75 6d 6e 73 5f 74 69 6d 65 20 53 45  _columns_time SE
12170 54 20 6c 61 73 74 5f 64 65 6c 65 74 65 20 3d 20  T last_delete = 
12180 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 5c  datetime('now')\
12190 6e 22 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28  n");...sprintf (
121a0 64 75 6d 6d 79 2c 20 22 57 48 45 52 45 20 55 70  dummy, "WHERE Up
121b0 70 65 72 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  per(f_table_name
121c0 29 20 3d 20 55 70 70 65 72 28 27 25 73 27 29 20  ) = Upper('%s') 
121d0 41 4e 44 20 22 2c 0a 09 09 09 20 73 71 6c 74 61  AND ",.... sqlta
121e0 62 6c 65 29 3b 0a 09 09 73 74 72 63 61 74 20 28  ble);...strcat (
121f0 74 72 69 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b  trigger, dummy);
12200 0a 09 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d  ...sprintf (dumm
12210 79 2c 20 22 55 70 70 65 72 28 66 5f 67 65 6f 6d  y, "Upper(f_geom
12220 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 3d 20 55  etry_column) = U
12230 70 70 65 72 28 27 25 73 27 29 3b 5c 6e 22 2c 0a  pper('%s');\n",.
12240 09 09 09 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a  ... sqlcolumn);.
12250 09 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65  ..strcat (trigge
12260 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 09 73 74 72  r, dummy);...str
12270 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 22 45  cat (trigger, "E
12280 4e 44 3b 22 29 3b 0a 09 09 72 65 74 20 3d 20 73  ND;");...ret = s
12290 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
122a0 69 74 65 2c 20 74 72 69 67 67 65 72 2c 20 4e 55  ite, trigger, NU
122b0 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
122c0 67 29 3b 0a 09 09 69 66 20 28 72 65 74 20 21 3d  g);...if (ret !=
122d0 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 09 20 20   SQLITE_OK)...  
122e0 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 09 20    goto error;.. 
122f0 20 20 20 7d 0a 09 20 20 2f 2a 20 69 6e 73 65 72     }..  /* inser
12300 74 69 6e 67 20 53 70 61 74 69 61 6c 49 6e 64 65  ting SpatialInde
12310 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  x information in
12320 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69  to the linked li
12330 73 74 20 2a 2f 0a 09 20 20 63 75 72 72 5f 69 64  st */..  curr_id
12340 78 20 3d 20 6d 61 6c 6c 6f 63 20 28 73 69 7a 65  x = malloc (size
12350 6f 66 20 28 73 74 72 75 63 74 20 73 70 61 74 69  of (struct spati
12360 61 6c 5f 69 6e 64 65 78 5f 73 74 72 29 29 3b 0a  al_index_str));.
12370 09 20 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 20  .  len = strlen 
12380 28 74 62 6c 6e 61 6d 65 29 3b 0a 09 20 20 63 75  (tblname);..  cu
12390 72 72 5f 69 64 78 2d 3e 54 61 62 6c 65 4e 61 6d  rr_idx->TableNam
123a0 65 20 3d 20 6d 61 6c 6c 6f 63 20 28 6c 65 6e 20  e = malloc (len 
123b0 2b 20 31 29 3b 0a 09 20 20 73 74 72 63 70 79 20  + 1);..  strcpy 
123c0 28 63 75 72 72 5f 69 64 78 2d 3e 54 61 62 6c 65  (curr_idx->Table
123d0 4e 61 6d 65 2c 20 74 62 6c 6e 61 6d 65 29 3b 0a  Name, tblname);.
123e0 09 20 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 20  .  len = strlen 
123f0 28 28 63 68 61 72 20 2a 29 20 63 6f 6c 6e 61 6d  ((char *) colnam
12400 65 29 3b 0a 09 20 20 63 75 72 72 5f 69 64 78 2d  e);..  curr_idx-
12410 3e 43 6f 6c 75 6d 6e 4e 61 6d 65 20 3d 20 6d 61  >ColumnName = ma
12420 6c 6c 6f 63 20 28 6c 65 6e 20 2b 20 31 29 3b 0a  lloc (len + 1);.
12430 09 20 20 73 74 72 63 70 79 20 28 63 75 72 72 5f  .  strcpy (curr_
12440 69 64 78 2d 3e 43 6f 6c 75 6d 6e 4e 61 6d 65 2c  idx->ColumnName,
12450 20 28 63 68 61 72 20 2a 29 20 63 6f 6c 6e 61 6d   (char *) colnam
12460 65 29 3b 0a 09 20 20 63 75 72 72 5f 69 64 78 2d  e);..  curr_idx-
12470 3e 56 61 6c 69 64 52 74 72 65 65 20 3d 20 28 63  >ValidRtree = (c
12480 68 61 72 29 20 69 6e 64 65 78 3b 0a 09 20 20 63  har) index;..  c
12490 75 72 72 5f 69 64 78 2d 3e 56 61 6c 69 64 43 61  urr_idx->ValidCa
124a0 63 68 65 20 3d 20 28 63 68 61 72 29 20 63 61 63  che = (char) cac
124b0 68 65 64 3b 0a 09 20 20 63 75 72 72 5f 69 64 78  hed;..  curr_idx
124c0 2d 3e 4e 65 78 74 20 3d 20 4e 55 4c 4c 3b 0a 09  ->Next = NULL;..
124d0 20 20 69 66 20 28 21 66 69 72 73 74 5f 69 64 78    if (!first_idx
124e0 29 0a 09 20 20 20 20 20 20 66 69 72 73 74 5f 69  )..      first_i
124f0 64 78 20 3d 20 63 75 72 72 5f 69 64 78 3b 0a 09  dx = curr_idx;..
12500 20 20 69 66 20 28 6c 61 73 74 5f 69 64 78 29 0a    if (last_idx).
12510 09 20 20 20 20 20 20 6c 61 73 74 5f 69 64 78 2d  .      last_idx-
12520 3e 4e 65 78 74 20 3d 20 63 75 72 72 5f 69 64 78  >Next = curr_idx
12530 3b 0a 09 20 20 6c 61 73 74 5f 69 64 78 20 3d 20  ;..  last_idx = 
12540 63 75 72 72 5f 69 64 78 3b 0a 09 20 20 2f 2a 20  curr_idx;..  /* 
12550 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6f 6c 64  deleting the old
12560 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 20   INSERT trigger 
12570 53 50 41 54 49 41 4c 5f 49 4e 44 45 58 20 5b 69  SPATIAL_INDEX [i
12580 66 20 61 6e 79 5d 20 2a 2f 0a 09 20 20 73 70 72  f any] */..  spr
12590 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67 69  intf (xname, "gi
125a0 69 5f 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d  i_%s_%s", tblnam
125b0 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20  e, colname);..  
125c0 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
125d0 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 20 20 73 70  l (xname);..  sp
125e0 72 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20  rintf (trigger, 
125f0 22 44 52 4f 50 20 54 52 49 47 47 45 52 20 49 46  "DROP TRIGGER IF
12600 20 45 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61   EXISTS %s", xna
12610 6d 65 29 3b 0a 09 20 20 72 65 74 20 3d 20 73 71  me);..  ret = sq
12620 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
12630 74 65 2c 20 74 72 69 67 67 65 72 2c 20 4e 55 4c  te, trigger, NUL
12640 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
12650 29 3b 0a 09 20 20 69 66 20 28 72 65 74 20 21 3d  );..  if (ret !=
12660 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20 20   SQLITE_OK)..   
12670 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 09     goto error;..
12680 20 20 69 66 20 28 69 6e 64 65 78 29 0a 09 20 20    if (index)..  
12690 20 20 7b 0a 09 09 2f 2a 20 69 6e 73 65 72 74 69    {.../* inserti
126a0 6e 67 20 74 68 65 20 6e 65 77 20 49 4e 53 45 52  ng the new INSER
126b0 54 20 74 72 69 67 67 65 72 20 53 52 49 44 20 2a  T trigger SRID *
126c0 2f 0a 09 09 73 70 72 69 6e 74 66 20 28 78 69 6e  /...sprintf (xin
126d0 64 65 78 2c 20 22 69 64 78 5f 25 73 5f 25 73 22  dex, "idx_%s_%s"
126e0 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f 6c 6e 61  , tblname, colna
126f0 6d 65 29 3b 0a 09 09 64 6f 75 62 6c 65 5f 71 75  me);...double_qu
12700 6f 74 65 64 5f 73 71 6c 20 28 78 69 6e 64 65 78  oted_sql (xindex
12710 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 74 72  );...sprintf (tr
12720 69 67 67 65 72 2c 20 22 43 52 45 41 54 45 20 54  igger, "CREATE T
12730 52 49 47 47 45 52 20 25 73 20 41 46 54 45 52 20  RIGGER %s AFTER 
12740 49 4e 53 45 52 54 20 4f 4e 20 25 73 5c 6e 22 2c  INSERT ON %s\n",
12750 0a 09 09 09 20 78 6e 61 6d 65 2c 20 78 74 61 62  .... xname, xtab
12760 6c 65 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74  le);...strcat (t
12770 72 69 67 67 65 72 2c 20 22 46 4f 52 20 45 41 43  rigger, "FOR EAC
12780 48 20 52 4f 57 20 42 45 47 49 4e 5c 6e 22 29 3b  H ROW BEGIN\n");
12790 0a 09 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d  ...sprintf (dumm
127a0 79 2c 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20  y, "DELETE FROM 
127b0 25 73 20 57 48 45 52 45 20 70 6b 69 64 3d 4e 45  %s WHERE pkid=NE
127c0 57 2e 52 4f 57 49 44 3b 5c 6e 22 2c 0a 09 09 09  W.ROWID;\n",....
127d0 20 78 69 6e 64 65 78 29 3b 0a 09 09 73 74 72 63   xindex);...strc
127e0 61 74 20 28 74 72 69 67 67 65 72 2c 20 64 75 6d  at (trigger, dum
127f0 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28  my);...sprintf (
12800 78 69 6e 64 65 78 2c 20 22 69 64 78 5f 25 73 5f  xindex, "idx_%s_
12810 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f  %s", tblname, co
12820 6c 6e 61 6d 65 29 3b 0a 09 09 63 6c 65 61 6e 5f  lname);...clean_
12830 73 71 6c 5f 73 74 72 69 6e 67 20 28 78 69 6e 64  sql_string (xind
12840 65 78 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28  ex);...sprintf (
12850 64 75 6d 6d 79 2c 20 22 53 45 4c 45 43 54 20 52  dummy, "SELECT R
12860 54 72 65 65 41 6c 69 67 6e 28 27 25 73 27 2c 20  TreeAlign('%s', 
12870 4e 45 57 2e 52 4f 57 49 44 2c 20 4e 45 57 2e 25  NEW.ROWID, NEW.%
12880 73 29 3b 22 2c 0a 09 09 09 20 78 69 6e 64 65 78  s);",.... xindex
12890 2c 20 78 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 73  , xcolname);...s
128a0 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20  trcat (trigger, 
128b0 64 75 6d 6d 79 29 3b 0a 09 09 73 74 72 63 61 74  dummy);...strcat
128c0 20 28 74 72 69 67 67 65 72 2c 20 22 45 4e 44 3b   (trigger, "END;
128d0 22 29 3b 0a 09 09 72 65 74 20 3d 20 73 71 6c 69  ");...ret = sqli
128e0 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
128f0 2c 20 74 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c  , trigger, NULL,
12900 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b   NULL, &errMsg);
12910 0a 09 09 69 66 20 28 72 65 74 20 21 3d 20 53 51  ...if (ret != SQ
12920 4c 49 54 45 5f 4f 4b 29 0a 09 09 20 20 20 20 67  LITE_OK)...    g
12930 6f 74 6f 20 65 72 72 6f 72 3b 0a 09 20 20 20 20  oto error;..    
12940 7d 0a 09 20 20 2f 2a 20 64 65 6c 65 74 69 6e 67  }..  /* deleting
12950 20 74 68 65 20 6f 6c 64 20 55 50 44 41 54 45 20   the old UPDATE 
12960 74 72 69 67 67 65 72 20 53 50 41 54 49 41 4c 5f  trigger SPATIAL_
12970 49 4e 44 45 58 20 5b 69 66 20 61 6e 79 5d 20 2a  INDEX [if any] *
12980 2f 0a 09 20 20 73 70 72 69 6e 74 66 20 28 78 6e  /..  sprintf (xn
12990 61 6d 65 2c 20 22 67 69 75 5f 25 73 5f 25 73 22  ame, "giu_%s_%s"
129a0 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f 6c 6e 61  , tblname, colna
129b0 6d 65 29 3b 0a 09 20 20 64 6f 75 62 6c 65 5f 71  me);..  double_q
129c0 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d 65  uoted_sql (xname
129d0 29 3b 0a 09 20 20 73 70 72 69 6e 74 66 20 28 74  );..  sprintf (t
129e0 72 69 67 67 65 72 2c 20 22 44 52 4f 50 20 54 52  rigger, "DROP TR
129f0 49 47 47 45 52 20 49 46 20 45 58 49 53 54 53 20  IGGER IF EXISTS 
12a00 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a 09 20 20  %s", xname);..  
12a10 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78  ret = sqlite3_ex
12a20 65 63 20 28 73 71 6c 69 74 65 2c 20 74 72 69 67  ec (sqlite, trig
12a30 67 65 72 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c  ger, NULL, NULL,
12a40 20 26 65 72 72 4d 73 67 29 3b 0a 09 20 20 69 66   &errMsg);..  if
12a50 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
12a60 4f 4b 29 0a 09 20 20 20 20 20 20 67 6f 74 6f 20  OK)..      goto 
12a70 65 72 72 6f 72 3b 0a 09 20 20 69 66 20 28 69 6e  error;..  if (in
12a80 64 65 78 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a  dex)..    {.../*
12a90 20 69 6e 73 65 72 74 69 6e 67 20 74 68 65 20 6e   inserting the n
12aa0 65 77 20 55 50 44 41 54 45 20 74 72 69 67 67 65  ew UPDATE trigge
12ab0 72 20 53 52 49 44 20 2a 2f 0a 09 09 73 70 72 69  r SRID */...spri
12ac0 6e 74 66 20 28 78 69 6e 64 65 78 2c 20 22 69 64  ntf (xindex, "id
12ad0 78 5f 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d  x_%s_%s", tblnam
12ae0 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 64  e, colname);...d
12af0 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c  ouble_quoted_sql
12b00 20 28 78 69 6e 64 65 78 29 3b 0a 09 09 73 70 72   (xindex);...spr
12b10 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20 22  intf (trigger, "
12b20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 25  CREATE TRIGGER %
12b30 73 20 41 46 54 45 52 20 55 50 44 41 54 45 20 4f  s AFTER UPDATE O
12b40 4e 20 25 73 5c 6e 22 2c 0a 09 09 09 20 78 6e 61  N %s\n",.... xna
12b50 6d 65 2c 20 78 74 61 62 6c 65 29 3b 0a 09 09 73  me, xtable);...s
12b60 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20  trcat (trigger, 
12b70 22 46 4f 52 20 45 41 43 48 20 52 4f 57 20 42 45  "FOR EACH ROW BE
12b80 47 49 4e 5c 6e 22 29 3b 0a 09 09 73 70 72 69 6e  GIN\n");...sprin
12b90 74 66 20 28 64 75 6d 6d 79 2c 20 22 44 45 4c 45  tf (dummy, "DELE
12ba0 54 45 20 46 52 4f 4d 20 25 73 20 57 48 45 52 45  TE FROM %s WHERE
12bb0 20 70 6b 69 64 3d 4e 45 57 2e 52 4f 57 49 44 3b   pkid=NEW.ROWID;
12bc0 5c 6e 22 2c 0a 09 09 09 20 78 69 6e 64 65 78 29  \n",.... xindex)
12bd0 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72 69 67  ;...strcat (trig
12be0 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 09 73  ger, dummy);...s
12bf0 70 72 69 6e 74 66 20 28 78 69 6e 64 65 78 2c 20  printf (xindex, 
12c00 22 69 64 78 5f 25 73 5f 25 73 22 2c 20 74 62 6c  "idx_%s_%s", tbl
12c10 6e 61 6d 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a  name, colname);.
12c20 09 09 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69  ..clean_sql_stri
12c30 6e 67 20 28 78 69 6e 64 65 78 29 3b 0a 09 09 73  ng (xindex);...s
12c40 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22  printf (dummy, "
12c50 53 45 4c 45 43 54 20 52 54 72 65 65 41 6c 69 67  SELECT RTreeAlig
12c60 6e 28 27 25 73 27 2c 20 4e 45 57 2e 52 4f 57 49  n('%s', NEW.ROWI
12c70 44 2c 20 4e 45 57 2e 25 73 29 3b 22 2c 0a 09 09  D, NEW.%s);",...
12c80 09 20 78 69 6e 64 65 78 2c 20 78 63 6f 6c 6e 61  . xindex, xcolna
12c90 6d 65 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74  me);...strcat (t
12ca0 72 69 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a  rigger, dummy);.
12cb0 09 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65  ..strcat (trigge
12cc0 72 2c 20 22 45 4e 44 3b 22 29 3b 0a 09 09 72 65  r, "END;");...re
12cd0 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
12ce0 20 28 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65   (sqlite, trigge
12cf0 72 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  r, NULL, NULL, &
12d00 65 72 72 4d 73 67 29 3b 0a 09 09 69 66 20 28 72  errMsg);...if (r
12d10 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29  et != SQLITE_OK)
12d20 0a 09 09 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  ...    goto erro
12d30 72 3b 0a 09 20 20 20 20 7d 0a 09 20 20 2f 2a 20  r;..    }..  /* 
12d40 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6f 6c 64  deleting the old
12d50 20 55 50 44 41 54 45 20 74 72 69 67 67 65 72 20   UPDATE trigger 
12d60 53 50 41 54 49 41 4c 5f 49 4e 44 45 58 20 5b 69  SPATIAL_INDEX [i
12d70 66 20 61 6e 79 5d 20 2a 2f 0a 09 20 20 73 70 72  f any] */..  spr
12d80 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67 69  intf (xname, "gi
12d90 64 5f 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d  d_%s_%s", tblnam
12da0 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 20 20  e, colname);..  
12db0 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
12dc0 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 20 20 73 70  l (xname);..  sp
12dd0 72 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20  rintf (trigger, 
12de0 22 44 52 4f 50 20 54 52 49 47 47 45 52 20 49 46  "DROP TRIGGER IF
12df0 20 45 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61   EXISTS %s", xna
12e00 6d 65 29 3b 0a 09 20 20 72 65 74 20 3d 20 73 71  me);..  ret = sq
12e10 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
12e20 74 65 2c 20 74 72 69 67 67 65 72 2c 20 4e 55 4c  te, trigger, NUL
12e30 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
12e40 29 3b 0a 09 20 20 69 66 20 28 72 65 74 20 21 3d  );..  if (ret !=
12e50 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20 20   SQLITE_OK)..   
12e60 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 09     goto error;..
12e70 20 20 69 66 20 28 69 6e 64 65 78 29 0a 09 20 20    if (index)..  
12e80 20 20 7b 0a 09 09 2f 2a 20 69 6e 73 65 72 74 69    {.../* inserti
12e90 6e 67 20 74 68 65 20 6e 65 77 20 44 45 4c 45 54  ng the new DELET
12ea0 45 20 74 72 69 67 67 65 72 20 53 52 49 44 20 2a  E trigger SRID *
12eb0 2f 0a 09 09 73 70 72 69 6e 74 66 20 28 78 69 6e  /...sprintf (xin
12ec0 64 65 78 2c 20 22 69 64 78 5f 25 73 5f 25 73 22  dex, "idx_%s_%s"
12ed0 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f 6c 6e 61  , tblname, colna
12ee0 6d 65 29 3b 0a 09 09 64 6f 75 62 6c 65 5f 71 75  me);...double_qu
12ef0 6f 74 65 64 5f 73 71 6c 20 28 78 69 6e 64 65 78  oted_sql (xindex
12f00 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 74 72  );...sprintf (tr
12f10 69 67 67 65 72 2c 20 22 43 52 45 41 54 45 20 54  igger, "CREATE T
12f20 52 49 47 47 45 52 20 25 73 20 41 46 54 45 52 20  RIGGER %s AFTER 
12f30 44 45 4c 45 54 45 20 4f 4e 20 25 73 5c 6e 22 2c  DELETE ON %s\n",
12f40 0a 09 09 09 20 78 6e 61 6d 65 2c 20 78 74 61 62  .... xname, xtab
12f50 6c 65 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74  le);...strcat (t
12f60 72 69 67 67 65 72 2c 20 22 46 4f 52 20 45 41 43  rigger, "FOR EAC
12f70 48 20 52 4f 57 20 42 45 47 49 4e 5c 6e 22 29 3b  H ROW BEGIN\n");
12f80 0a 09 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d  ...sprintf (dumm
12f90 79 2c 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20  y, "DELETE FROM 
12fa0 25 73 20 57 48 45 52 45 20 70 6b 69 64 20 3d 20  %s WHERE pkid = 
12fb0 4f 4c 44 2e 52 4f 57 49 44 3b 5c 6e 22 2c 0a 09  OLD.ROWID;\n",..
12fc0 09 09 20 78 69 6e 64 65 78 29 3b 0a 09 09 73 74  .. xindex);...st
12fd0 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 64  rcat (trigger, d
12fe0 75 6d 6d 79 29 3b 0a 09 09 73 74 72 63 61 74 20  ummy);...strcat 
12ff0 28 74 72 69 67 67 65 72 2c 20 22 45 4e 44 3b 22  (trigger, "END;"
13000 29 3b 0a 09 09 72 65 74 20 3d 20 73 71 6c 69 74  );...ret = sqlit
13010 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c  e3_exec (sqlite,
13020 20 74 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c 20   trigger, NULL, 
13030 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a  NULL, &errMsg);.
13040 09 09 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c  ..if (ret != SQL
13050 49 54 45 5f 4f 4b 29 0a 09 09 20 20 20 20 67 6f  ITE_OK)...    go
13060 74 6f 20 65 72 72 6f 72 3b 0a 09 20 20 20 20 7d  to error;..    }
13070 0a 09 20 20 2f 2a 20 64 65 6c 65 74 69 6e 67 20  ..  /* deleting 
13080 74 68 65 20 6f 6c 64 20 49 4e 53 45 52 54 20 74  the old INSERT t
13090 72 69 67 67 65 72 20 4d 42 52 5f 43 41 43 48 45  rigger MBR_CACHE
130a0 20 5b 69 66 20 61 6e 79 5d 20 2a 2f 0a 09 20 20   [if any] */..  
130b0 73 70 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20  sprintf (xname, 
130c0 22 67 63 69 5f 25 73 5f 25 73 22 2c 20 74 62 6c  "gci_%s_%s", tbl
130d0 6e 61 6d 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a  name, colname);.
130e0 09 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64  .  double_quoted
130f0 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 20  _sql (xname);.. 
13100 20 73 70 72 69 6e 74 66 20 28 74 72 69 67 67 65   sprintf (trigge
13110 72 2c 20 22 44 52 4f 50 20 54 52 49 47 47 45 52  r, "DROP TRIGGER
13120 20 49 46 20 45 58 49 53 54 53 20 25 73 22 2c 20   IF EXISTS %s", 
13130 78 6e 61 6d 65 29 3b 0a 09 20 20 72 65 74 20 3d  xname);..  ret =
13140 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73   sqlite3_exec (s
13150 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72 2c 20  qlite, trigger, 
13160 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
13170 4d 73 67 29 3b 0a 09 20 20 69 66 20 28 72 65 74  Msg);..  if (ret
13180 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09   != SQLITE_OK)..
13190 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72        goto error
131a0 3b 0a 09 20 20 69 66 20 28 63 61 63 68 65 64 29  ;..  if (cached)
131b0 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 69 6e 73  ..    {.../* ins
131c0 65 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20 49  erting the new I
131d0 4e 53 45 52 54 20 74 72 69 67 67 65 72 20 53 52  NSERT trigger SR
131e0 49 44 20 2a 2f 0a 09 09 73 70 72 69 6e 74 66 20  ID */...sprintf 
131f0 28 78 69 6e 64 65 78 2c 20 22 63 61 63 68 65 5f  (xindex, "cache_
13200 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c  %s_%s", tblname,
13210 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 64 6f 75   colname);...dou
13220 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
13230 78 69 6e 64 65 78 29 3b 0a 09 09 73 70 72 69 6e  xindex);...sprin
13240 74 66 20 28 74 72 69 67 67 65 72 2c 20 22 43 52  tf (trigger, "CR
13250 45 41 54 45 20 54 52 49 47 47 45 52 20 25 73 20  EATE TRIGGER %s 
13260 41 46 54 45 52 20 49 4e 53 45 52 54 20 4f 4e 20  AFTER INSERT ON 
13270 25 73 5c 6e 22 2c 0a 09 09 09 20 78 6e 61 6d 65  %s\n",.... xname
13280 2c 20 78 74 61 62 6c 65 29 3b 0a 09 09 73 74 72  , xtable);...str
13290 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 22 46  cat (trigger, "F
132a0 4f 52 20 45 41 43 48 20 52 4f 57 20 42 45 47 49  OR EACH ROW BEGI
132b0 4e 5c 6e 22 29 3b 0a 09 09 73 70 72 69 6e 74 66  N\n");...sprintf
132c0 20 28 64 75 6d 6d 79 2c 0a 09 09 09 20 22 49 4e   (dummy,.... "IN
132d0 53 45 52 54 20 49 4e 54 4f 20 25 73 20 28 72 6f  SERT INTO %s (ro
132e0 77 69 64 2c 20 6d 62 72 29 20 56 41 4c 55 45 53  wid, mbr) VALUES
132f0 20 28 4e 45 57 2e 52 4f 57 49 44 2c 5c 6e 42 75   (NEW.ROWID,\nBu
13300 69 6c 64 4d 62 72 46 69 6c 74 65 72 28 22 2c 0a  ildMbrFilter(",.
13310 09 09 09 20 78 69 6e 64 65 78 29 3b 0a 09 09 73  ... xindex);...s
13320 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20  trcat (trigger, 
13330 64 75 6d 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74  dummy);...sprint
13340 66 20 28 64 75 6d 6d 79 2c 20 22 4d 62 72 4d 69  f (dummy, "MbrMi
13350 6e 58 28 4e 45 57 2e 25 73 29 2c 20 22 2c 20 78  nX(NEW.%s), ", x
13360 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 73 74 72 63  colname);...strc
13370 61 74 20 28 74 72 69 67 67 65 72 2c 20 64 75 6d  at (trigger, dum
13380 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74 66 20 28  my);...sprintf (
13390 64 75 6d 6d 79 2c 20 22 4d 62 72 4d 69 6e 59 28  dummy, "MbrMinY(
133a0 4e 45 57 2e 25 73 29 2c 20 22 2c 20 78 63 6f 6c  NEW.%s), ", xcol
133b0 6e 61 6d 65 29 3b 0a 09 09 73 74 72 63 61 74 20  name);...strcat 
133c0 28 74 72 69 67 67 65 72 2c 20 64 75 6d 6d 79 29  (trigger, dummy)
133d0 3b 0a 09 09 73 70 72 69 6e 74 66 20 28 64 75 6d  ;...sprintf (dum
133e0 6d 79 2c 20 22 4d 62 72 4d 61 78 58 28 4e 45 57  my, "MbrMaxX(NEW
133f0 2e 25 73 29 2c 20 22 2c 20 78 63 6f 6c 6e 61 6d  .%s), ", xcolnam
13400 65 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72  e);...strcat (tr
13410 69 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09  igger, dummy);..
13420 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c  .sprintf (dummy,
13430 20 22 4d 62 72 4d 61 78 59 28 4e 45 57 2e 25 73   "MbrMaxY(NEW.%s
13440 29 29 29 3b 5c 6e 22 2c 20 78 63 6f 6c 6e 61 6d  )));\n", xcolnam
13450 65 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72  e);...strcat (tr
13460 69 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09  igger, dummy);..
13470 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65 72  .strcat (trigger
13480 2c 20 22 45 4e 44 3b 22 29 3b 0a 09 09 72 65 74  , "END;");...ret
13490 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
134a0 28 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72  (sqlite, trigger
134b0 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
134c0 72 72 4d 73 67 29 3b 0a 09 09 69 66 20 28 72 65  rrMsg);...if (re
134d0 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
134e0 09 09 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ..    goto error
134f0 3b 0a 09 20 20 20 20 7d 0a 09 20 20 2f 2a 20 64  ;..    }..  /* d
13500 65 6c 65 74 69 6e 67 20 74 68 65 20 6f 6c 64 20  eleting the old 
13510 55 50 44 41 54 45 20 74 72 69 67 67 65 72 20 4d  UPDATE trigger M
13520 42 52 5f 43 41 43 48 45 20 5b 69 66 20 61 6e 79  BR_CACHE [if any
13530 5d 20 2a 2f 0a 09 20 20 73 70 72 69 6e 74 66 20  ] */..  sprintf 
13540 28 78 6e 61 6d 65 2c 20 22 67 63 75 5f 25 73 5f  (xname, "gcu_%s_
13550 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c 20 63 6f  %s", tblname, co
13560 6c 6e 61 6d 65 29 3b 0a 09 20 20 64 6f 75 62 6c  lname);..  doubl
13570 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e  e_quoted_sql (xn
13580 61 6d 65 29 3b 0a 09 20 20 73 70 72 69 6e 74 66  ame);..  sprintf
13590 20 28 74 72 69 67 67 65 72 2c 20 22 44 52 4f 50   (trigger, "DROP
135a0 20 54 52 49 47 47 45 52 20 49 46 20 45 58 49 53   TRIGGER IF EXIS
135b0 54 53 20 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a  TS %s", xname);.
135c0 09 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33  .  ret = sqlite3
135d0 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 74  _exec (sqlite, t
135e0 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c 20 4e 55  rigger, NULL, NU
135f0 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 09 20  LL, &errMsg);.. 
13600 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
13610 54 45 5f 4f 4b 29 0a 09 20 20 20 20 20 20 67 6f  TE_OK)..      go
13620 74 6f 20 65 72 72 6f 72 3b 0a 09 20 20 69 66 20  to error;..  if 
13630 28 63 61 63 68 65 64 29 0a 09 20 20 20 20 7b 0a  (cached)..    {.
13640 09 09 2f 2a 20 69 6e 73 65 72 74 69 6e 67 20 74  ../* inserting t
13650 68 65 20 6e 65 77 20 55 50 44 41 54 45 20 74 72  he new UPDATE tr
13660 69 67 67 65 72 20 53 52 49 44 20 2a 2f 0a 09 09  igger SRID */...
13670 73 70 72 69 6e 74 66 20 28 78 69 6e 64 65 78 2c  sprintf (xindex,
13680 20 22 63 61 63 68 65 5f 25 73 5f 25 73 22 2c 20   "cache_%s_%s", 
13690 74 62 6c 6e 61 6d 65 2c 20 63 6f 6c 6e 61 6d 65  tblname, colname
136a0 29 3b 0a 09 09 64 6f 75 62 6c 65 5f 71 75 6f 74  );...double_quot
136b0 65 64 5f 73 71 6c 20 28 78 69 6e 64 65 78 29 3b  ed_sql (xindex);
136c0 0a 09 09 73 70 72 69 6e 74 66 20 28 74 72 69 67  ...sprintf (trig
136d0 67 65 72 2c 20 22 43 52 45 41 54 45 20 54 52 49  ger, "CREATE TRI
136e0 47 47 45 52 20 25 73 20 41 46 54 45 52 20 55 50  GGER %s AFTER UP
136f0 44 41 54 45 20 4f 4e 20 25 73 5c 6e 22 2c 0a 09  DATE ON %s\n",..
13700 09 09 20 78 6e 61 6d 65 2c 20 78 74 61 62 6c 65  .. xname, xtable
13710 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72 69  );...strcat (tri
13720 67 67 65 72 2c 20 22 46 4f 52 20 45 41 43 48 20  gger, "FOR EACH 
13730 52 4f 57 20 42 45 47 49 4e 5c 6e 22 29 3b 0a 09  ROW BEGIN\n");..
13740 09 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c  .sprintf (dummy,
13750 20 22 55 50 44 41 54 45 20 25 73 20 53 45 54 20   "UPDATE %s SET 
13760 22 2c 20 78 69 6e 64 65 78 29 3b 0a 09 09 73 74  ", xindex);...st
13770 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 64  rcat (trigger, d
13780 75 6d 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74 66  ummy);...sprintf
13790 20 28 64 75 6d 6d 79 2c 20 22 6d 62 72 20 3d 20   (dummy, "mbr = 
137a0 42 75 69 6c 64 4d 62 72 46 69 6c 74 65 72 28 4d  BuildMbrFilter(M
137b0 62 72 4d 69 6e 58 28 4e 45 57 2e 25 73 29 2c 20  brMinX(NEW.%s), 
137c0 22 2c 0a 09 09 09 20 78 63 6f 6c 6e 61 6d 65 29  ",.... xcolname)
137d0 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72 69 67  ;...strcat (trig
137e0 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 09 73  ger, dummy);...s
137f0 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22  printf (dummy, "
13800 4d 62 72 4d 69 6e 59 28 4e 45 57 2e 25 73 29 2c  MbrMinY(NEW.%s),
13810 20 22 2c 20 78 63 6f 6c 6e 61 6d 65 29 3b 0a 09   ", xcolname);..
13820 09 73 74 72 63 61 74 20 28 74 72 69 67 67 65 72  .strcat (trigger
13830 2c 20 64 75 6d 6d 79 29 3b 0a 09 09 73 70 72 69  , dummy);...spri
13840 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22 4d 62 72  ntf (dummy, "Mbr
13850 4d 61 78 58 28 4e 45 57 2e 25 73 29 2c 20 22 2c  MaxX(NEW.%s), ",
13860 20 78 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 73 74   xcolname);...st
13870 72 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 64  rcat (trigger, d
13880 75 6d 6d 79 29 3b 0a 09 09 73 70 72 69 6e 74 66  ummy);...sprintf
13890 20 28 64 75 6d 6d 79 2c 20 22 4d 62 72 4d 61 78   (dummy, "MbrMax
138a0 59 28 4e 45 57 2e 25 73 29 29 5c 6e 22 2c 20 78  Y(NEW.%s))\n", x
138b0 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 73 74 72 63  colname);...strc
138c0 61 74 20 28 74 72 69 67 67 65 72 2c 20 64 75 6d  at (trigger, dum
138d0 6d 79 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74  my);...strcat (t
138e0 72 69 67 67 65 72 2c 20 22 57 48 45 52 45 20 72  rigger, "WHERE r
138f0 6f 77 69 64 20 3d 20 4e 45 57 2e 52 4f 57 49 44  owid = NEW.ROWID
13900 3b 5c 6e 22 29 3b 0a 09 09 73 74 72 63 61 74 20  ;\n");...strcat 
13910 28 74 72 69 67 67 65 72 2c 20 22 45 4e 44 3b 22  (trigger, "END;"
13920 29 3b 0a 09 09 72 65 74 20 3d 20 73 71 6c 69 74  );...ret = sqlit
13930 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c  e3_exec (sqlite,
13940 20 74 72 69 67 67 65 72 2c 20 4e 55 4c 4c 2c 20   trigger, NULL, 
13950 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a  NULL, &errMsg);.
13960 09 09 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c  ..if (ret != SQL
13970 49 54 45 5f 4f 4b 29 0a 09 09 20 20 20 20 67 6f  ITE_OK)...    go
13980 74 6f 20 65 72 72 6f 72 3b 0a 09 20 20 20 20 7d  to error;..    }
13990 0a 09 20 20 2f 2a 20 64 65 6c 65 74 69 6e 67 20  ..  /* deleting 
139a0 74 68 65 20 6f 6c 64 20 55 50 44 41 54 45 20 74  the old UPDATE t
139b0 72 69 67 67 65 72 20 4d 42 52 5f 43 41 43 48 45  rigger MBR_CACHE
139c0 20 5b 69 66 20 61 6e 79 5d 20 2a 2f 0a 09 20 20   [if any] */..  
139d0 73 70 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20  sprintf (xname, 
139e0 22 67 63 64 5f 25 73 5f 25 73 22 2c 20 74 62 6c  "gcd_%s_%s", tbl
139f0 6e 61 6d 65 2c 20 63 6f 6c 6e 61 6d 65 29 3b 0a  name, colname);.
13a00 09 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64  .  double_quoted
13a10 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 09 20  _sql (xname);.. 
13a20 20 73 70 72 69 6e 74 66 20 28 74 72 69 67 67 65   sprintf (trigge
13a30 72 2c 20 22 44 52 4f 50 20 54 52 49 47 47 45 52  r, "DROP TRIGGER
13a40 20 49 46 20 45 58 49 53 54 53 20 25 73 22 2c 20   IF EXISTS %s", 
13a50 78 6e 61 6d 65 29 3b 0a 09 20 20 72 65 74 20 3d  xname);..  ret =
13a60 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73   sqlite3_exec (s
13a70 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72 2c 20  qlite, trigger, 
13a80 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
13a90 4d 73 67 29 3b 0a 09 20 20 69 66 20 28 72 65 74  Msg);..  if (ret
13aa0 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09   != SQLITE_OK)..
13ab0 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72        goto error
13ac0 3b 0a 09 20 20 69 66 20 28 63 61 63 68 65 64 29  ;..  if (cached)
13ad0 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 69 6e 73  ..    {.../* ins
13ae0 65 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20 44  erting the new D
13af0 45 4c 45 54 45 20 74 72 69 67 67 65 72 20 53 52  ELETE trigger SR
13b00 49 44 20 2a 2f 0a 09 09 73 70 72 69 6e 74 66 20  ID */...sprintf 
13b10 28 78 69 6e 64 65 78 2c 20 22 63 61 63 68 65 5f  (xindex, "cache_
13b20 25 73 5f 25 73 22 2c 20 74 62 6c 6e 61 6d 65 2c  %s_%s", tblname,
13b30 20 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 64 6f 75   colname);...dou
13b40 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
13b50 78 69 6e 64 65 78 29 3b 0a 09 09 73 70 72 69 6e  xindex);...sprin
13b60 74 66 20 28 74 72 69 67 67 65 72 2c 20 22 43 52  tf (trigger, "CR
13b70 45 41 54 45 20 54 52 49 47 47 45 52 20 25 73 20  EATE TRIGGER %s 
13b80 41 46 54 45 52 20 44 45 4c 45 54 45 20 4f 4e 20  AFTER DELETE ON 
13b90 25 73 5c 6e 22 2c 0a 09 09 09 20 78 6e 61 6d 65  %s\n",.... xname
13ba0 2c 20 78 74 61 62 6c 65 29 3b 0a 09 09 73 74 72  , xtable);...str
13bb0 63 61 74 20 28 74 72 69 67 67 65 72 2c 20 22 46  cat (trigger, "F
13bc0 4f 52 20 45 41 43 48 20 52 4f 57 20 42 45 47 49  OR EACH ROW BEGI
13bd0 4e 5c 6e 22 29 3b 0a 09 09 73 70 72 69 6e 74 66  N\n");...sprintf
13be0 20 28 64 75 6d 6d 79 2c 20 22 44 45 4c 45 54 45   (dummy, "DELETE
13bf0 20 46 52 4f 4d 20 25 73 20 57 48 45 52 45 20 72   FROM %s WHERE r
13c00 6f 77 69 64 20 3d 20 4f 4c 44 2e 52 4f 57 49 44  owid = OLD.ROWID
13c10 3b 5c 6e 22 2c 0a 09 09 09 20 78 69 6e 64 65 78  ;\n",.... xindex
13c20 29 3b 0a 09 09 73 74 72 63 61 74 20 28 74 72 69  );...strcat (tri
13c30 67 67 65 72 2c 20 64 75 6d 6d 79 29 3b 0a 09 09  gger, dummy);...
13c40 73 74 72 63 61 74 20 28 74 72 69 67 67 65 72 2c  strcat (trigger,
13c50 20 22 45 4e 44 3b 22 29 3b 0a 09 09 72 65 74 20   "END;");...ret 
13c60 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
13c70 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72 2c  sqlite, trigger,
13c80 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72   NULL, NULL, &er
13c90 72 4d 73 67 29 3b 0a 09 09 69 66 20 28 72 65 74  rMsg);...if (ret
13ca0 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09   != SQLITE_OK)..
13cb0 09 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b  .    goto error;
13cc0 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a  ..    }.      }.
13cd0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
13ce0 5f 74 61 62 6c 65 20 28 72 65 73 75 6c 74 73 29  _table (results)
13cf0 3b 0a 2f 2a 20 6e 6f 77 20 77 65 27 6c 6c 20 61  ;./* now we'll a
13d00 64 6a 75 73 74 20 61 6e 79 20 72 65 6c 61 74 65  djust any relate
13d10 64 20 53 70 61 74 69 61 6c 49 6e 64 65 78 20 61  d SpatialIndex a
13d20 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  s required */.  
13d30 20 20 63 75 72 72 5f 69 64 78 20 3d 20 66 69 72    curr_idx = fir
13d40 73 74 5f 69 64 78 3b 0a 20 20 20 20 77 68 69 6c  st_idx;.    whil
13d50 65 20 28 63 75 72 72 5f 69 64 78 29 0a 20 20 20  e (curr_idx).   
13d60 20 20 20 7b 0a 09 20 20 69 66 20 28 63 75 72 72     {..  if (curr
13d70 5f 69 64 78 2d 3e 56 61 6c 69 64 52 74 72 65 65  _idx->ValidRtree
13d80 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75  )..    {.../* bu
13d90 69 6c 64 69 6e 67 20 52 54 72 65 65 20 53 70 61  ilding RTree Spa
13da0 74 69 61 6c 49 6e 64 65 78 20 2a 2f 0a 09 09 73  tialIndex */...s
13db0 70 72 69 6e 74 66 20 28 78 69 6e 64 65 78 2c 20  printf (xindex, 
13dc0 22 69 64 78 5f 25 73 5f 25 73 22 2c 20 63 75 72  "idx_%s_%s", cur
13dd0 72 5f 69 64 78 2d 3e 54 61 62 6c 65 4e 61 6d 65  r_idx->TableName
13de0 2c 0a 09 09 09 20 63 75 72 72 5f 69 64 78 2d 3e  ,.... curr_idx->
13df0 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a 09 09 64  ColumnName);...d
13e00 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c  ouble_quoted_sql
13e10 20 28 78 69 6e 64 65 78 29 3b 0a 09 09 73 70 72   (xindex);...spr
13e20 69 6e 74 66 20 28 74 72 69 67 67 65 72 2c 20 22  intf (trigger, "
13e30 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
13e40 41 42 4c 45 20 25 73 20 55 53 49 4e 47 20 72 74  ABLE %s USING rt
13e50 72 65 65 28 5c 6e 22 2c 0a 09 09 09 20 78 69 6e  ree(\n",.... xin
13e60 64 65 78 29 3b 0a 09 09 73 74 72 63 61 74 20 28  dex);...strcat (
13e70 74 72 69 67 67 65 72 2c 20 22 70 6b 69 64 2c 20  trigger, "pkid, 
13e80 78 6d 69 6e 2c 20 78 6d 61 78 2c 20 79 6d 69 6e  xmin, xmax, ymin
13e90 2c 20 79 6d 61 78 29 22 29 3b 0a 09 09 72 65 74  , ymax)");...ret
13ea0 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
13eb0 28 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72  (sqlite, trigger
13ec0 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
13ed0 72 72 4d 73 67 29 3b 0a 09 09 69 66 20 28 72 65  rrMsg);...if (re
13ee0 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
13ef0 09 09 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ..    goto error
13f00 3b 0a 09 09 62 75 69 6c 64 53 70 61 74 69 61 6c  ;...buildSpatial
13f10 49 6e 64 65 78 20 28 73 71 6c 69 74 65 2c 0a 09  Index (sqlite,..
13f20 09 09 09 20 20 20 28 75 6e 73 69 67 6e 65 64 20  ...   (unsigned 
13f30 63 68 61 72 20 2a 29 20 28 63 75 72 72 5f 69 64  char *) (curr_id
13f40 78 2d 3e 54 61 62 6c 65 4e 61 6d 65 29 2c 0a 09  x->TableName),..
13f50 09 09 09 20 20 20 63 75 72 72 5f 69 64 78 2d 3e  ...   curr_idx->
13f60 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a 09 20 20  ColumnName);..  
13f70 20 20 7d 0a 09 20 20 69 66 20 28 63 75 72 72 5f    }..  if (curr_
13f80 69 64 78 2d 3e 56 61 6c 69 64 43 61 63 68 65 29  idx->ValidCache)
13f90 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75 69  ..    {.../* bui
13fa0 6c 64 69 6e 67 20 4d 62 72 43 61 63 68 65 20 53  lding MbrCache S
13fb0 70 61 74 69 61 6c 49 6e 64 65 78 20 2a 2f 0a 09  patialIndex */..
13fc0 09 73 70 72 69 6e 74 66 20 28 78 69 6e 64 65 78  .sprintf (xindex
13fd0 2c 20 22 63 61 63 68 65 5f 25 73 5f 25 73 22 2c  , "cache_%s_%s",
13fe0 20 63 75 72 72 5f 69 64 78 2d 3e 54 61 62 6c 65   curr_idx->Table
13ff0 4e 61 6d 65 2c 0a 09 09 09 20 63 75 72 72 5f 69  Name,.... curr_i
14000 64 78 2d 3e 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b  dx->ColumnName);
14010 0a 09 09 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64  ...double_quoted
14020 5f 73 71 6c 20 28 78 69 6e 64 65 78 29 3b 0a 09  _sql (xindex);..
14030 09 73 74 72 63 70 79 20 28 78 74 61 62 6c 65 2c  .strcpy (xtable,
14040 20 63 75 72 72 5f 69 64 78 2d 3e 54 61 62 6c 65   curr_idx->Table
14050 4e 61 6d 65 29 3b 0a 09 09 64 6f 75 62 6c 65 5f  Name);...double_
14060 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 74 61 62  quoted_sql (xtab
14070 6c 65 29 3b 0a 09 09 73 74 72 63 70 79 20 28 78  le);...strcpy (x
14080 63 6f 6c 6e 61 6d 65 2c 20 63 75 72 72 5f 69 64  colname, curr_id
14090 78 2d 3e 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a  x->ColumnName);.
140a0 09 09 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f  ..double_quoted_
140b0 73 71 6c 20 28 78 63 6f 6c 6e 61 6d 65 29 3b 0a  sql (xcolname);.
140c0 09 09 73 70 72 69 6e 74 66 20 28 74 72 69 67 67  ..sprintf (trigg
140d0 65 72 2c 0a 09 09 09 20 22 43 52 45 41 54 45 20  er,.... "CREATE 
140e0 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 25 73  VIRTUAL TABLE %s
140f0 20 55 53 49 4e 47 20 4d 62 72 43 61 63 68 65 28   USING MbrCache(
14100 25 73 2c 20 25 73 29 5c 6e 22 2c 0a 09 09 09 20  %s, %s)\n",.... 
14110 78 69 6e 64 65 78 2c 20 78 74 61 62 6c 65 2c 20  xindex, xtable, 
14120 78 63 6f 6c 6e 61 6d 65 29 3b 0a 09 09 72 65 74  xcolname);...ret
14130 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
14140 28 73 71 6c 69 74 65 2c 20 74 72 69 67 67 65 72  (sqlite, trigger
14150 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
14160 72 72 4d 73 67 29 3b 0a 09 09 69 66 20 28 72 65  rrMsg);...if (re
14170 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
14180 09 09 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ..    goto error
14190 3b 0a 09 20 20 20 20 7d 0a 09 20 20 63 75 72 72  ;..    }..  curr
141a0 5f 69 64 78 20 3d 20 63 75 72 72 5f 69 64 78 2d  _idx = curr_idx-
141b0 3e 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  >Next;.      }. 
141c0 20 20 20 67 6f 74 6f 20 69 6e 64 65 78 5f 63 6c     goto index_cl
141d0 65 61 6e 75 70 3b 0a 20 20 65 72 72 6f 72 3a 0a  eanup;.  error:.
141e0 20 20 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65      spatialite_e
141f0 20 28 22 75 70 64 61 74 65 54 61 62 6c 65 54 72   ("updateTableTr
14200 69 67 67 65 72 73 3a 20 5c 22 25 73 5c 22 5c 6e  iggers: \"%s\"\n
14210 22 2c 20 65 72 72 4d 73 67 29 3b 0a 20 20 20 20  ", errMsg);.    
14220 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72  sqlite3_free (er
14230 72 4d 73 67 29 3b 0a 20 20 69 6e 64 65 78 5f 63  rMsg);.  index_c
14240 6c 65 61 6e 75 70 3a 0a 20 20 20 20 63 75 72 72  leanup:.    curr
14250 5f 69 64 78 20 3d 20 66 69 72 73 74 5f 69 64 78  _idx = first_idx
14260 3b 0a 20 20 20 20 77 68 69 6c 65 20 28 63 75 72  ;.    while (cur
14270 72 5f 69 64 78 29 0a 20 20 20 20 20 20 7b 0a 09  r_idx).      {..
14280 20 20 6e 65 78 74 5f 69 64 78 20 3d 20 63 75 72    next_idx = cur
14290 72 5f 69 64 78 2d 3e 4e 65 78 74 3b 0a 09 20 20  r_idx->Next;..  
142a0 69 66 20 28 63 75 72 72 5f 69 64 78 2d 3e 54 61  if (curr_idx->Ta
142b0 62 6c 65 4e 61 6d 65 29 0a 09 20 20 20 20 20 20  bleName)..      
142c0 66 72 65 65 20 28 63 75 72 72 5f 69 64 78 2d 3e  free (curr_idx->
142d0 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 09 20 20 69  TableName);..  i
142e0 66 20 28 63 75 72 72 5f 69 64 78 2d 3e 43 6f 6c  f (curr_idx->Col
142f0 75 6d 6e 4e 61 6d 65 29 0a 09 20 20 20 20 20 20  umnName)..      
14300 66 72 65 65 20 28 63 75 72 72 5f 69 64 78 2d 3e  free (curr_idx->
14310 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a 09 20 20  ColumnName);..  
14320 66 72 65 65 20 28 63 75 72 72 5f 69 64 78 29 3b  free (curr_idx);
14330 0a 09 20 20 63 75 72 72 5f 69 64 78 20 3d 20 6e  ..  curr_idx = n
14340 65 78 74 5f 69 64 78 3b 0a 20 20 20 20 20 20 7d  ext_idx;.      }
14350 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
14360 66 6e 63 74 5f 41 64 64 47 65 6f 6d 65 74 72 79  fnct_AddGeometry
14370 43 6f 6c 75 6d 6e 20 28 73 71 6c 69 74 65 33 5f  Column (sqlite3_
14380 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
14390 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 09  t, int argc,....
143a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
143b0 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20   argv).{./* SQL 
143c0 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 41 64 64 47  function:./ AddG
143d0 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 74 61  eometryColumn(ta
143e0 62 6c 65 2c 20 63 6f 6c 75 6d 6e 2c 20 73 72 69  ble, column, sri
143f0 64 2c 20 74 79 70 65 20 2c 20 64 69 6d 65 6e 73  d, type , dimens
14400 69 6f 6e 20 20 5b 20 20 2c 20 6e 6f 74 2d 6e 75  ion  [  , not-nu
14410 6c 6c 20 5d 20 20 29 0a 2f 0a 2f 20 63 72 65 61  ll ]  )././ crea
14420 74 65 73 20 61 20 6e 65 77 20 43 4f 4c 55 4d 4e  tes a new COLUMN
14430 20 6f 66 20 67 69 76 65 6e 20 54 59 50 45 20 69   of given TYPE i
14440 6e 74 6f 20 54 41 42 4c 45 0a 2f 20 72 65 74 75  nto TABLE./ retu
14450 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65 73 73  rns 1 on success
14460 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 0a  ./ 0 on failure.
14470 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  */.    const cha
14480 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20 63 6f  r *table;.    co
14490 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d 6e  nst char *column
144a0 3b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  ;.    const unsi
144b0 67 6e 65 64 20 63 68 61 72 20 2a 74 79 70 65 3b  gned char *type;
144c0 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  .    const unsig
144d0 6e 65 64 20 63 68 61 72 20 2a 74 78 74 5f 64 69  ned char *txt_di
144e0 6d 73 3b 0a 20 20 20 20 69 6e 74 20 78 74 79 70  ms;.    int xtyp
144f0 65 3b 0a 20 20 20 20 69 6e 74 20 73 72 69 64 20  e;.    int srid 
14500 3d 20 2d 31 3b 0a 20 20 20 20 69 6e 74 20 64 69  = -1;.    int di
14510 6d 65 6e 73 69 6f 6e 20 3d 20 32 3b 0a 20 20 20  mension = 2;.   
14520 20 69 6e 74 20 64 69 6d 73 20 3d 20 2d 31 3b 0a   int dims = -1;.
14530 20 20 20 20 63 68 61 72 20 64 75 6d 6d 79 5b 33      char dummy[3
14540 32 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c  2];.    char sql
14550 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72  [1024];.    char
14560 20 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b   *errMsg = NULL;
14570 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20  .    int ret;.  
14580 20 20 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 73    char **results
14590 3b 0a 20 20 20 20 69 6e 74 20 72 6f 77 73 3b 0a  ;.    int rows;.
145a0 20 20 20 20 69 6e 74 20 63 6f 6c 75 6d 6e 73 3b      int columns;
145b0 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
145c0 63 68 61 72 20 74 62 6c 6e 61 6d 65 5b 32 35 36  char tblname[256
145d0 5d 3b 0a 20 20 20 20 63 68 61 72 20 78 74 61 62  ];.    char xtab
145e0 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68  le[1024];.    ch
145f0 61 72 20 78 63 6f 6c 75 6d 6e 5b 31 30 32 34 5d  ar xcolumn[1024]
14600 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 74 61  ;.    char sqlta
14610 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63  ble[1024];.    c
14620 68 61 72 20 73 71 6c 63 6f 6c 75 6d 6e 5b 31 30  har sqlcolumn[10
14630 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 6f 74  24];.    int not
14640 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 69 6e  Null = 0;.    in
14650 74 20 6d 65 74 61 64 61 74 61 5f 76 65 72 73 69  t metadata_versi
14660 6f 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  on;.    sqlite3 
14670 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65  *sqlite = sqlite
14680 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
14690 64 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  dle (context);. 
146a0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
146b0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
146c0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 54 45  0]) != SQLITE_TE
146d0 58 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  XT).      {..  s
146e0 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20  patialite_e..   
146f0 20 20 20 28 22 41 64 64 47 65 6f 6d 65 74 72 79     ("AddGeometry
14700 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20  Column() error: 
14710 61 72 67 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c  argument 1 [tabl
14720 65 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f  e_name] is not o
14730 66 20 74 68 65 20 53 74 72 69 6e 67 20 74 79 70  f the String typ
14740 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65  e\n");..  sqlite
14750 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
14760 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
14770 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
14780 20 20 74 61 62 6c 65 20 3d 20 28 63 6f 6e 73 74    table = (const
14790 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
147a0 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67  _value_text (arg
147b0 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 73  v[0]);.    if (s
147c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
147d0 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53  e (argv[1]) != S
147e0 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20  QLITE_TEXT).    
147f0 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
14800 65 5f 65 0a 09 20 20 20 20 20 20 28 22 41 64 64  e_e..      ("Add
14810 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29  GeometryColumn()
14820 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74   error: argument
14830 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 5d   2 [column_name]
14840 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 53   is not of the S
14850 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a  tring type\n");.
14860 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
14870 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
14880 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  0);..  return;. 
14890 20 20 20 20 20 7d 0a 20 20 20 20 63 6f 6c 75 6d       }.    colum
148a0 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  n = (const char 
148b0 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
148c0 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d 29 3b  _text (argv[1]);
148d0 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
148e0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
148f0 76 5b 32 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[2]) != SQLITE_
14900 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b  INTEGER).      {
14910 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
14920 0a 09 20 20 20 20 20 20 28 22 41 64 64 47 65 6f  ..      ("AddGeo
14930 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72  metryColumn() er
14940 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 33 20  ror: argument 3 
14950 5b 53 52 49 44 5d 20 69 73 20 6e 6f 74 20 6f 66  [SRID] is not of
14960 20 74 68 65 20 49 6e 74 65 67 65 72 20 74 79 70   the Integer typ
14970 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65  e\n");..  sqlite
14980 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
14990 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
149a0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
149b0 20 20 73 72 69 64 20 3d 20 73 71 6c 69 74 65 33    srid = sqlite3
149c0 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76  _value_int (argv
149d0 5b 32 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71  [2]);.    if (sq
149e0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
149f0 20 28 61 72 67 76 5b 33 5d 29 20 21 3d 20 53 51   (argv[3]) != SQ
14a00 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20  LITE_TEXT).     
14a10 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
14a20 5f 65 0a 09 20 20 20 20 20 20 28 22 41 64 64 47  _e..      ("AddG
14a30 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20  eometryColumn() 
14a40 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20  error: argument 
14a50 34 20 5b 67 65 6f 6d 65 74 72 79 5f 74 79 70 65  4 [geometry_type
14a60 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20  ] is not of the 
14a70 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b  String type\n");
14a80 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
14a90 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
14aa0 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a   0);..  return;.
14ab0 20 20 20 20 20 20 7d 0a 20 20 20 20 74 79 70 65        }.    type
14ac0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
14ad0 5f 74 65 78 74 20 28 61 72 67 76 5b 33 5d 29 3b  _text (argv[3]);
14ae0 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
14af0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
14b00 76 5b 34 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[4]) == SQLITE_
14b10 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b  INTEGER).      {
14b20 0a 09 20 20 64 69 6d 65 6e 73 69 6f 6e 20 3d 20  ..  dimension = 
14b30 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
14b40 74 20 28 61 72 67 76 5b 34 5d 29 3b 0a 09 20 20  t (argv[4]);..  
14b50 69 66 20 28 64 69 6d 65 6e 73 69 6f 6e 20 3d 3d  if (dimension ==
14b60 20 32 29 0a 09 20 20 20 20 20 20 64 69 6d 73 20   2)..      dims 
14b70 3d 20 47 41 49 41 5f 58 59 3b 0a 09 20 20 69 66  = GAIA_XY;..  if
14b80 20 28 64 69 6d 65 6e 73 69 6f 6e 20 3d 3d 20 33   (dimension == 3
14b90 29 0a 09 20 20 20 20 20 20 64 69 6d 73 20 3d 20  )..      dims = 
14ba0 47 41 49 41 5f 58 59 5f 5a 3b 0a 20 20 20 20 20  GAIA_XY_Z;.     
14bb0 20 7d 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28   }.    else if (
14bc0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
14bd0 70 65 20 28 61 72 67 76 5b 34 5d 29 20 3d 3d 20  pe (argv[4]) == 
14be0 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20  SQLITE_TEXT).   
14bf0 20 20 20 7b 0a 09 20 20 74 78 74 5f 64 69 6d 73     {..  txt_dims
14c00 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
14c10 5f 74 65 78 74 20 28 61 72 67 76 5b 34 5d 29 3b  _text (argv[4]);
14c20 0a 09 20 20 69 66 20 28 73 74 72 63 61 73 65 63  ..  if (strcasec
14c30 6d 70 20 28 28 63 68 61 72 20 2a 29 20 74 78 74  mp ((char *) txt
14c40 5f 64 69 6d 73 2c 20 22 58 59 22 29 20 3d 3d 20  _dims, "XY") == 
14c50 30 29 0a 09 20 20 20 20 20 20 64 69 6d 73 20 3d  0)..      dims =
14c60 20 47 41 49 41 5f 58 59 3b 0a 09 20 20 69 66 20   GAIA_XY;..  if 
14c70 28 73 74 72 63 61 73 65 63 6d 70 20 28 28 63 68  (strcasecmp ((ch
14c80 61 72 20 2a 29 20 74 78 74 5f 64 69 6d 73 2c 20  ar *) txt_dims, 
14c90 22 58 59 5a 22 29 20 3d 3d 20 30 29 0a 09 20 20  "XYZ") == 0)..  
14ca0 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41 5f      dims = GAIA_
14cb0 58 59 5f 5a 3b 0a 09 20 20 69 66 20 28 73 74 72  XY_Z;..  if (str
14cc0 63 61 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a  casecmp ((char *
14cd0 29 20 74 78 74 5f 64 69 6d 73 2c 20 22 58 59 4d  ) txt_dims, "XYM
14ce0 22 29 20 3d 3d 20 30 29 0a 09 20 20 20 20 20 20  ") == 0)..      
14cf0 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59 5f 4d  dims = GAIA_XY_M
14d00 3b 0a 09 20 20 69 66 20 28 73 74 72 63 61 73 65  ;..  if (strcase
14d10 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20 74 78  cmp ((char *) tx
14d20 74 5f 64 69 6d 73 2c 20 22 58 59 5a 4d 22 29 20  t_dims, "XYZM") 
14d30 3d 3d 20 30 29 0a 09 20 20 20 20 20 20 64 69 6d  == 0)..      dim
14d40 73 20 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3b  s = GAIA_XY_Z_M;
14d50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
14d60 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61  e.      {..  spa
14d70 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20  tialite_e..     
14d80 20 28 22 41 64 64 47 65 6f 6d 65 74 72 79 43 6f   ("AddGeometryCo
14d90 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61 72  lumn() error: ar
14da0 67 75 6d 65 6e 74 20 35 20 5b 64 69 6d 65 6e 73  gument 5 [dimens
14db0 69 6f 6e 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74  ion] is not of t
14dc0 68 65 20 49 6e 74 65 67 65 72 20 6f 72 20 54 65  he Integer or Te
14dd0 78 74 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20  xt type\n");..  
14de0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
14df0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  nt (context, 0);
14e00 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
14e10 20 20 7d 0a 20 20 20 20 69 66 20 28 61 72 67 63    }.    if (argc
14e20 20 3e 20 35 29 0a 20 20 20 20 20 20 7b 0a 09 20   > 5).      {.. 
14e30 20 2f 2a 20 6f 70 74 69 6f 6e 61 6c 20 4e 4f 54   /* optional NOT
14e40 20 4e 55 4c 4c 20 61 72 67 20 2a 2f 0a 09 20 20   NULL arg */..  
14e50 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
14e60 65 5f 74 79 70 65 20 28 61 72 67 76 5b 35 5d 29  e_type (argv[5])
14e70 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   != SQLITE_INTEG
14e80 45 52 29 0a 09 20 20 20 20 7b 0a 09 09 73 70 61  ER)..    {...spa
14e90 74 69 61 6c 69 74 65 5f 65 0a 09 09 20 20 20 20  tialite_e...    
14ea0 28 22 41 64 64 47 65 6f 6d 65 74 72 79 43 6f 6c  ("AddGeometryCol
14eb0 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67  umn() error: arg
14ec0 75 6d 65 6e 74 20 36 20 5b 6e 6f 74 20 6e 75 6c  ument 6 [not nul
14ed0 6c 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65  l] is not of the
14ee0 20 49 6e 74 65 67 65 72 20 74 79 70 65 5c 6e 22   Integer type\n"
14ef0 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73  );...sqlite3_res
14f00 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
14f10 2c 20 30 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a  , 0);...return;.
14f20 09 20 20 20 20 7d 0a 09 20 20 6e 6f 74 4e 75 6c  .    }..  notNul
14f30 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
14f40 65 5f 69 6e 74 20 28 61 72 67 76 5b 35 5d 29 3b  e_int (argv[5]);
14f50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 78 74 79  .      }.    xty
14f60 70 65 20 3d 20 47 41 49 41 5f 55 4e 4b 4e 4f 57  pe = GAIA_UNKNOW
14f70 4e 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63 61  N;.    if (strca
14f80 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20  secmp ((char *) 
14f90 74 79 70 65 2c 20 22 50 4f 49 4e 54 22 29 20 3d  type, "POINT") =
14fa0 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20 47 41  = 0)..xtype = GA
14fb0 49 41 5f 50 4f 49 4e 54 3b 0a 20 20 20 20 69 66  IA_POINT;.    if
14fc0 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 28 63   (strcasecmp ((c
14fd0 68 61 72 20 2a 29 20 74 79 70 65 2c 20 22 4c 49  har *) type, "LI
14fe0 4e 45 53 54 52 49 4e 47 22 29 20 3d 3d 20 30 29  NESTRING") == 0)
14ff0 0a 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 4c  ..xtype = GAIA_L
15000 49 4e 45 53 54 52 49 4e 47 3b 0a 20 20 20 20 69  INESTRING;.    i
15010 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 28  f (strcasecmp ((
15020 63 68 61 72 20 2a 29 20 74 79 70 65 2c 20 22 50  char *) type, "P
15030 4f 4c 59 47 4f 4e 22 29 20 3d 3d 20 30 29 0a 09  OLYGON") == 0)..
15040 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50 4f 4c  xtype = GAIA_POL
15050 59 47 4f 4e 3b 0a 20 20 20 20 69 66 20 28 73 74  YGON;.    if (st
15060 72 63 61 73 65 63 6d 70 20 28 28 63 68 61 72 20  rcasecmp ((char 
15070 2a 29 20 74 79 70 65 2c 20 22 4d 55 4c 54 49 50  *) type, "MULTIP
15080 4f 49 4e 54 22 29 20 3d 3d 20 30 29 0a 09 78 74  OINT") == 0)..xt
15090 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49  ype = GAIA_MULTI
150a0 50 4f 49 4e 54 3b 0a 20 20 20 20 69 66 20 28 73  POINT;.    if (s
150b0 74 72 63 61 73 65 63 6d 70 20 28 28 63 68 61 72  trcasecmp ((char
150c0 20 2a 29 20 74 79 70 65 2c 20 22 4d 55 4c 54 49   *) type, "MULTI
150d0 4c 49 4e 45 53 54 52 49 4e 47 22 29 20 3d 3d 20  LINESTRING") == 
150e0 30 29 0a 09 78 74 79 70 65 20 3d 20 47 41 49 41  0)..xtype = GAIA
150f0 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47  _MULTILINESTRING
15100 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63 61 73  ;.    if (strcas
15110 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20 74  ecmp ((char *) t
15120 79 70 65 2c 20 22 4d 55 4c 54 49 50 4f 4c 59 47  ype, "MULTIPOLYG
15130 4f 4e 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70  ON") == 0)..xtyp
15140 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f  e = GAIA_MULTIPO
15150 4c 59 47 4f 4e 3b 0a 20 20 20 20 69 66 20 28 73  LYGON;.    if (s
15160 74 72 63 61 73 65 63 6d 70 20 28 28 63 68 61 72  trcasecmp ((char
15170 20 2a 29 20 74 79 70 65 2c 20 22 47 45 4f 4d 45   *) type, "GEOME
15180 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 22 29 20  TRYCOLLECTION") 
15190 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20 47  == 0)..xtype = G
151a0 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c  AIA_GEOMETRYCOLL
151b0 45 43 54 49 4f 4e 3b 0a 20 20 20 20 69 66 20 28  ECTION;.    if (
151c0 73 74 72 63 61 73 65 63 6d 70 20 28 28 63 68 61  strcasecmp ((cha
151d0 72 20 2a 29 20 74 79 70 65 2c 20 22 47 45 4f 4d  r *) type, "GEOM
151e0 45 54 52 59 22 29 20 3d 3d 20 30 29 0a 09 78 74  ETRY") == 0)..xt
151f0 79 70 65 20 3d 20 2d 31 3b 0a 20 20 20 20 69 66  ype = -1;.    if
15200 20 28 78 74 79 70 65 20 3d 3d 20 47 41 49 41 5f   (xtype == GAIA_
15210 55 4e 4b 4e 4f 57 4e 29 0a 20 20 20 20 20 20 7b  UNKNOWN).      {
15220 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
15230 0a 09 20 20 20 20 20 20 28 22 41 64 64 47 65 6f  ..      ("AddGeo
15240 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72  metryColumn() er
15250 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 34 20  ror: argument 4 
15260 5b 67 65 6f 6d 65 74 72 79 5f 74 79 70 65 5d 20  [geometry_type] 
15270 68 61 73 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76  has an illegal v
15280 61 6c 75 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c  alue\n");..  sql
15290 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
152a0 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20  (context, 0);.. 
152b0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
152c0 0a 20 20 20 20 69 66 20 28 64 69 6d 73 20 3d 3d  .    if (dims ==
152d0 20 47 41 49 41 5f 58 59 20 7c 7c 20 64 69 6d 73   GAIA_XY || dims
152e0 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 20 7c 7c   == GAIA_XY_Z ||
152f0 20 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59   dims == GAIA_XY
15300 5f 4d 0a 09 7c 7c 20 64 69 6d 73 20 3d 3d 20 47  _M..|| dims == G
15310 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 3b 0a 20  AIA_XY_Z_M)..;. 
15320 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
15330 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a  .  spatialite_e.
15340 09 20 20 20 20 20 20 28 22 41 64 64 47 65 6f 6d  .      ("AddGeom
15350 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72  etryColumn() err
15360 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 35 20 5b  or: argument 5 [
15370 64 69 6d 65 6e 73 69 6f 6e 5d 20 49 4c 4c 45 47  dimension] ILLEG
15380 41 4c 20 56 41 4c 55 45 5c 6e 22 29 3b 0a 09 20  AL VALUE\n");.. 
15390 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
153a0 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29  int (context, 0)
153b0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
153c0 20 20 20 7d 0a 2f 2a 20 63 68 65 63 6b 69 6e 67     }./* checking
153d0 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 65 78   if the table ex
153e0 69 73 74 73 20 2a 2f 0a 20 20 20 20 73 74 72 63  ists */.    strc
153f0 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61  py (sqltable, ta
15400 62 6c 65 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f  ble);.    clean_
15410 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74  sql_string (sqlt
15420 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 70  able);.    strcp
15430 79 20 28 73 71 6c 63 6f 6c 75 6d 6e 2c 20 63 6f  y (sqlcolumn, co
15440 6c 75 6d 6e 29 3b 0a 20 20 20 20 63 6c 65 61 6e  lumn);.    clean
15450 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c  _sql_string (sql
15460 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 70 72  column);.    spr
15470 69 6e 74 66 20 28 73 71 6c 2c 0a 09 20 20 20 20  intf (sql,..    
15480 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 20 46 52   "SELECT name FR
15490 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
154a0 20 57 48 45 52 45 20 74 79 70 65 20 3d 20 27 74   WHERE type = 't
154b0 61 62 6c 65 27 20 41 4e 44 20 55 70 70 65 72 28  able' AND Upper(
154c0 6e 61 6d 65 29 20 3d 20 55 70 70 65 72 28 27 25  name) = Upper('%
154d0 73 27 29 22 2c 0a 09 20 20 20 20 20 73 71 6c 74  s')",..     sqlt
154e0 61 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 20 3d  able);.    ret =
154f0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
15500 6c 65 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c  le (sqlite, sql,
15510 20 26 72 65 73 75 6c 74 73 2c 20 26 72 6f 77 73   &results, &rows
15520 2c 20 26 63 6f 6c 75 6d 6e 73 2c 20 26 65 72 72  , &columns, &err
15530 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  Msg);.    if (re
15540 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
15550 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
15560 61 6c 69 74 65 5f 65 20 28 22 41 64 64 47 65 6f  alite_e ("AddGeo
15570 6d 65 74 72 79 43 6f 6c 75 6d 6e 3a 20 5c 22 25  metryColumn: \"%
15580 73 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b  s\"\n", errMsg);
15590 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
155a0 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20 72 65   (errMsg);..  re
155b0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
155c0 20 20 2a 74 62 6c 6e 61 6d 65 20 3d 20 27 5c 30    *tblname = '\0
155d0 27 3b 0a 20 20 20 20 66 6f 72 20 28 69 20 3d 20  ';.    for (i = 
155e0 31 3b 20 69 20 3c 3d 20 72 6f 77 73 3b 20 69 2b  1; i <= rows; i+
155f0 2b 29 0a 09 73 74 72 63 70 79 20 28 74 62 6c 6e  +)..strcpy (tbln
15600 61 6d 65 2c 20 72 65 73 75 6c 74 73 5b 28 69 20  ame, results[(i 
15610 2a 20 63 6f 6c 75 6d 6e 73 29 5d 29 3b 0a 20 20  * columns)]);.  
15620 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74    sqlite3_free_t
15630 61 62 6c 65 20 28 72 65 73 75 6c 74 73 29 3b 0a  able (results);.
15640 20 20 20 20 69 66 20 28 2a 74 62 6c 6e 61 6d 65      if (*tblname
15650 20 3d 3d 20 27 5c 30 27 29 0a 20 20 20 20 20 20   == '\0').      
15660 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
15670 65 0a 09 20 20 20 20 20 20 28 22 41 64 64 47 65  e..      ("AddGe
15680 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65  ometryColumn() e
15690 72 72 6f 72 3a 20 74 61 62 6c 65 20 27 25 73 27  rror: table '%s'
156a0 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 5c   does not exist\
156b0 6e 22 2c 20 74 61 62 6c 65 29 3b 0a 09 20 20 73  n", table);..  s
156c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
156d0 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  t (context, 0);.
156e0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
156f0 20 7d 0a 20 20 20 20 6d 65 74 61 64 61 74 61 5f   }.    metadata_
15700 76 65 72 73 69 6f 6e 20 3d 20 63 68 65 63 6b 53  version = checkS
15710 70 61 74 69 61 6c 4d 65 74 61 44 61 74 61 20 28  patialMetaData (
15720 73 71 6c 69 74 65 29 3b 0a 20 20 20 20 69 66 20  sqlite);.    if 
15730 28 6d 65 74 61 64 61 74 61 5f 76 65 72 73 69 6f  (metadata_versio
15740 6e 20 3d 3d 20 31 20 7c 7c 20 6d 65 74 61 64 61  n == 1 || metada
15750 74 61 5f 76 65 72 73 69 6f 6e 20 3d 3d 20 33 29  ta_version == 3)
15760 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  ..;.    else.   
15770 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
15780 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 41 64  te_e..      ("Ad
15790 64 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28  dGeometryColumn(
157a0 29 20 65 72 72 6f 72 3a 20 75 6e 65 78 70 65 63  ) error: unexpec
157b0 74 65 64 20 6d 65 74 61 64 61 74 61 20 6c 61 79  ted metadata lay
157c0 6f 75 74 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69  out\n");..  sqli
157d0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
157e0 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20  context, 0);..  
157f0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
15800 2f 2a 20 74 72 79 69 6e 67 20 74 6f 20 61 64 64  /* trying to add
15810 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20   the column */. 
15820 20 20 20 73 74 72 63 70 79 20 28 78 74 61 62 6c     strcpy (xtabl
15830 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 64  e, table);.    d
15840 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c  ouble_quoted_sql
15850 20 28 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 73   (xtable);.    s
15860 74 72 63 70 79 20 28 78 63 6f 6c 75 6d 6e 2c 20  trcpy (xcolumn, 
15870 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 64 6f 75  column);.    dou
15880 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
15890 78 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 74  xcolumn);.    st
158a0 72 63 70 79 20 28 73 71 6c 2c 20 22 41 4c 54 45  rcpy (sql, "ALTE
158b0 52 20 54 41 42 4c 45 20 22 29 3b 0a 20 20 20 20  R TABLE ");.    
158c0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 78 74 61  strcat (sql, xta
158d0 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ble);.    strcat
158e0 20 28 73 71 6c 2c 20 22 20 41 44 44 20 43 4f 4c   (sql, " ADD COL
158f0 55 4d 4e 20 22 29 3b 0a 20 20 20 20 73 74 72 63  UMN ");.    strc
15900 61 74 20 28 73 71 6c 2c 20 78 63 6f 6c 75 6d 6e  at (sql, xcolumn
15910 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
15920 71 6c 2c 20 22 20 22 29 3b 0a 20 20 20 20 73 77  ql, " ");.    sw
15930 69 74 63 68 20 28 78 74 79 70 65 29 0a 20 20 20  itch (xtype).   
15940 20 20 20 7b 0a 20 20 20 20 20 20 63 61 73 65 20     {.      case 
15950 47 41 49 41 5f 50 4f 49 4e 54 3a 0a 09 20 20 73  GAIA_POINT:..  s
15960 74 72 63 61 74 20 28 73 71 6c 2c 20 22 50 4f 49  trcat (sql, "POI
15970 4e 54 22 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a  NT");..  break;.
15980 20 20 20 20 20 20 63 61 73 65 20 47 41 49 41 5f        case GAIA_
15990 4c 49 4e 45 53 54 52 49 4e 47 3a 0a 09 20 20 73  LINESTRING:..  s
159a0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4c 49 4e  trcat (sql, "LIN
159b0 45 53 54 52 49 4e 47 22 29 3b 0a 09 20 20 62 72  ESTRING");..  br
159c0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
159d0 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 3a 0a 09 20  GAIA_POLYGON:.. 
159e0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 50   strcat (sql, "P
159f0 4f 4c 59 47 4f 4e 22 29 3b 0a 09 20 20 62 72 65  OLYGON");..  bre
15a00 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 47  ak;.      case G
15a10 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 3a 0a  AIA_MULTIPOINT:.
15a20 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20  .  strcat (sql, 
15a30 22 4d 55 4c 54 49 50 4f 49 4e 54 22 29 3b 0a 09  "MULTIPOINT");..
15a40 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
15a50 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49  ase GAIA_MULTILI
15a60 4e 45 53 54 52 49 4e 47 3a 0a 09 20 20 73 74 72  NESTRING:..  str
15a70 63 61 74 20 28 73 71 6c 2c 20 22 4d 55 4c 54 49  cat (sql, "MULTI
15a80 4c 49 4e 45 53 54 52 49 4e 47 22 29 3b 0a 09 20  LINESTRING");.. 
15a90 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
15aa0 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c  se GAIA_MULTIPOL
15ab0 59 47 4f 4e 3a 0a 09 20 20 73 74 72 63 61 74 20  YGON:..  strcat 
15ac0 28 73 71 6c 2c 20 22 4d 55 4c 54 49 50 4f 4c 59  (sql, "MULTIPOLY
15ad0 47 4f 4e 22 29 3b 0a 09 20 20 62 72 65 61 6b 3b  GON");..  break;
15ae0 0a 20 20 20 20 20 20 63 61 73 65 20 47 41 49 41  .      case GAIA
15af0 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54  _GEOMETRYCOLLECT
15b00 49 4f 4e 3a 0a 09 20 20 73 74 72 63 61 74 20 28  ION:..  strcat (
15b10 73 71 6c 2c 20 22 47 45 4f 4d 45 54 52 59 43 4f  sql, "GEOMETRYCO
15b20 4c 4c 45 43 54 49 4f 4e 22 29 3b 0a 09 20 20 62  LLECTION");..  b
15b30 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
15b40 20 2d 31 3a 0a 09 20 20 73 74 72 63 61 74 20 28   -1:..  strcat (
15b50 73 71 6c 2c 20 22 47 45 4f 4d 45 54 52 59 22 29  sql, "GEOMETRY")
15b60 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;..  break;.    
15b70 20 20 7d 3b 0a 20 20 20 20 69 66 20 28 6e 6f 74    };.    if (not
15b80 4e 75 6c 6c 29 0a 20 20 20 20 20 20 7b 0a 09 20  Null).      {.. 
15b90 20 2f 2a 20 61 64 64 69 6e 67 20 61 20 4e 4f 54   /* adding a NOT
15ba0 20 4e 55 4c 4c 20 63 6c 61 75 73 65 20 2a 2f 0a   NULL clause */.
15bb0 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20  .  strcat (sql, 
15bc0 22 20 4e 4f 54 20 4e 55 4c 4c 20 44 45 46 41 55  " NOT NULL DEFAU
15bd0 4c 54 20 27 27 22 29 3b 0a 20 20 20 20 20 20 7d  LT ''");.      }
15be0 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74  .    ret = sqlit
15bf0 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c  e3_exec (sqlite,
15c00 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c   sql, NULL, NULL
15c10 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20  , &errMsg);.    
15c20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
15c30 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f  E_OK)..goto erro
15c40 72 3b 0a 2f 2a 6f 6b 2c 20 69 6e 73 65 72 74 69  r;./*ok, inserti
15c50 6e 67 20 69 6e 74 6f 20 67 65 6f 6d 65 74 72 79  ng into geometry
15c60 5f 63 6f 6c 75 6d 6e 73 20 5b 53 70 61 74 69 61  _columns [Spatia
15c70 6c 20 4d 65 74 61 64 61 74 61 5d 20 2a 2f 0a 20  l Metadata] */. 
15c80 20 20 20 69 66 20 28 6d 65 74 61 64 61 74 61 5f     if (metadata_
15c90 76 65 72 73 69 6f 6e 20 3d 3d 20 31 29 0a 20 20  version == 1).  
15ca0 20 20 20 20 7b 0a 09 20 20 2f 2a 20 6c 65 67 61      {..  /* lega
15cb0 63 79 20 6d 65 74 61 64 61 74 61 20 73 74 79 6c  cy metadata styl
15cc0 65 20 3c 3d 20 76 2e 33 2e 31 2e 30 20 2a 2f 0a  e <= v.3.1.0 */.
15cd0 09 20 20 73 74 72 63 70 79 20 28 73 71 6c 2c 0a  .  strcpy (sql,.
15ce0 09 09 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  ..  "INSERT INTO
15cf0 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e   geometry_column
15d00 73 20 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c  s (f_table_name,
15d10 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75   f_geometry_colu
15d20 6d 6e 2c 20 74 79 70 65 2c 20 22 29 3b 0a 09 20  mn, type, ");.. 
15d30 20 73 74 72 63 61 74 20 28 73 71 6c 2c 0a 09 09   strcat (sql,...
15d40 20 20 22 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69    "coord_dimensi
15d50 6f 6e 2c 20 73 72 69 64 2c 20 73 70 61 74 69 61  on, srid, spatia
15d60 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 29  l_index_enabled)
15d70 20 56 41 4c 55 45 53 20 28 22 29 3b 0a 09 20 20   VALUES (");..  
15d80 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 22  strcat (sql, "'"
15d90 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
15da0 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09 20  l, sqltable);.. 
15db0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27   strcat (sql, "'
15dc0 2c 20 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74  , '");..  strcat
15dd0 20 28 73 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e   (sql, sqlcolumn
15de0 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
15df0 6c 2c 20 22 27 2c 20 27 22 29 3b 0a 09 20 20 73  l, "', '");..  s
15e00 77 69 74 63 68 20 28 78 74 79 70 65 29 0a 09 20  witch (xtype).. 
15e10 20 20 20 7b 0a 09 20 20 20 20 63 61 73 65 20 47     {..    case G
15e20 41 49 41 5f 50 4f 49 4e 54 3a 0a 09 09 73 74 72  AIA_POINT:...str
15e30 63 61 74 20 28 73 71 6c 2c 20 22 50 4f 49 4e 54  cat (sql, "POINT
15e40 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  ");...break;..  
15e50 20 20 63 61 73 65 20 47 41 49 41 5f 4c 49 4e 45    case GAIA_LINE
15e60 53 54 52 49 4e 47 3a 0a 09 09 73 74 72 63 61 74  STRING:...strcat
15e70 20 28 73 71 6c 2c 20 22 4c 49 4e 45 53 54 52 49   (sql, "LINESTRI
15e80 4e 47 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  NG");...break;..
15e90 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f      case GAIA_PO
15ea0 4c 59 47 4f 4e 3a 0a 09 09 73 74 72 63 61 74 20  LYGON:...strcat 
15eb0 28 73 71 6c 2c 20 22 50 4f 4c 59 47 4f 4e 22 29  (sql, "POLYGON")
15ec0 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
15ed0 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 50  case GAIA_MULTIP
15ee0 4f 49 4e 54 3a 0a 09 09 73 74 72 63 61 74 20 28  OINT:...strcat (
15ef0 73 71 6c 2c 20 22 4d 55 4c 54 49 50 4f 49 4e 54  sql, "MULTIPOINT
15f00 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  ");...break;..  
15f10 20 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54    case GAIA_MULT
15f20 49 4c 49 4e 45 53 54 52 49 4e 47 3a 0a 09 09 73  ILINESTRING:...s
15f30 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4d 55 4c  trcat (sql, "MUL
15f40 54 49 4c 49 4e 45 53 54 52 49 4e 47 22 29 3b 0a  TILINESTRING");.
15f50 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
15f60 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c  se GAIA_MULTIPOL
15f70 59 47 4f 4e 3a 0a 09 09 73 74 72 63 61 74 20 28  YGON:...strcat (
15f80 73 71 6c 2c 20 22 4d 55 4c 54 49 50 4f 4c 59 47  sql, "MULTIPOLYG
15f90 4f 4e 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  ON");...break;..
15fa0 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 47 45      case GAIA_GE
15fb0 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e  OMETRYCOLLECTION
15fc0 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c  :...strcat (sql,
15fd0 20 22 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43   "GEOMETRYCOLLEC
15fe0 54 49 4f 4e 22 29 3b 0a 09 09 62 72 65 61 6b 3b  TION");...break;
15ff0 0a 09 20 20 20 20 63 61 73 65 20 2d 31 3a 0a 09  ..    case -1:..
16000 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 47  .strcat (sql, "G
16010 45 4f 4d 45 54 52 59 22 29 3b 0a 09 09 62 72 65  EOMETRY");...bre
16020 61 6b 3b 0a 09 20 20 20 20 7d 3b 0a 09 20 20 73  ak;..    };..  s
16030 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 2c 20  trcat (sql, "', 
16040 27 22 29 3b 0a 09 20 20 73 77 69 74 63 68 20 28  '");..  switch (
16050 64 69 6d 73 29 0a 09 20 20 20 20 7b 0a 09 20 20  dims)..    {..  
16060 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 3a 0a    case GAIA_XY:.
16070 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22  ..strcat (sql, "
16080 58 59 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  XY");...break;..
16090 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 58 59      case GAIA_XY
160a0 5f 5a 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71  _Z:...strcat (sq
160b0 6c 2c 20 22 58 59 5a 22 29 3b 0a 09 09 62 72 65  l, "XYZ");...bre
160c0 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
160d0 49 41 5f 58 59 5f 4d 3a 0a 09 09 73 74 72 63 61  IA_XY_M:...strca
160e0 74 20 28 73 71 6c 2c 20 22 58 59 4d 22 29 3b 0a  t (sql, "XYM");.
160f0 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
16100 73 65 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a  se GAIA_XY_Z_M:.
16110 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22  ..strcat (sql, "
16120 58 59 5a 4d 22 29 3b 0a 09 09 62 72 65 61 6b 3b  XYZM");...break;
16130 0a 09 20 20 20 20 7d 3b 0a 09 20 20 73 74 72 63  ..    };..  strc
16140 61 74 20 28 73 71 6c 2c 20 22 27 2c 20 22 29 3b  at (sql, "', ");
16150 0a 09 20 20 69 66 20 28 73 72 69 64 20 3c 3d 20  ..  if (srid <= 
16160 30 29 0a 09 20 20 20 20 20 20 73 74 72 63 61 74  0)..      strcat
16170 20 28 73 71 6c 2c 20 22 2d 31 22 29 3b 0a 09 20   (sql, "-1");.. 
16180 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 73   else..    {...s
16190 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22  printf (dummy, "
161a0 25 64 22 2c 20 73 72 69 64 29 3b 0a 09 09 73 74  %d", srid);...st
161b0 72 63 61 74 20 28 73 71 6c 2c 20 64 75 6d 6d 79  rcat (sql, dummy
161c0 29 3b 0a 09 20 20 20 20 7d 0a 09 20 20 73 74 72  );..    }..  str
161d0 63 61 74 20 28 73 71 6c 2c 20 22 2c 20 30 29 22  cat (sql, ", 0)"
161e0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65  );.      }.    e
161f0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f  lse.      {..  /
16200 2a 20 63 75 72 72 65 6e 74 20 6d 65 74 61 64 61  * current metada
16210 74 61 20 73 74 79 6c 65 20 3e 3d 20 76 2e 34 2e  ta style >= v.4.
16220 30 2e 30 20 2a 2f 0a 09 20 20 73 74 72 63 70 79  0.0 */..  strcpy
16230 20 28 73 71 6c 2c 0a 09 09 20 20 22 49 4e 53 45   (sql,...  "INSE
16240 52 54 20 49 4e 54 4f 20 67 65 6f 6d 65 74 72 79  RT INTO geometry
16250 5f 63 6f 6c 75 6d 6e 73 20 28 66 5f 74 61 62 6c  _columns (f_tabl
16260 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74  e_name, f_geomet
16270 72 79 5f 63 6f 6c 75 6d 6e 2c 20 67 65 6f 6d 65  ry_column, geome
16280 74 72 79 5f 74 79 70 65 2c 20 22 29 3b 0a 09 20  try_type, ");.. 
16290 20 73 74 72 63 61 74 20 28 73 71 6c 2c 0a 09 09   strcat (sql,...
162a0 20 20 22 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69    "coord_dimensi
162b0 6f 6e 2c 20 73 72 69 64 2c 20 73 70 61 74 69 61  on, srid, spatia
162c0 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 29  l_index_enabled)
162d0 20 56 41 4c 55 45 53 20 28 22 29 3b 0a 09 20 20   VALUES (");..  
162e0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 22  strcat (sql, "'"
162f0 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
16300 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09 20  l, sqltable);.. 
16310 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27   strcat (sql, "'
16320 2c 20 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74  , '");..  strcat
16330 20 28 73 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e   (sql, sqlcolumn
16340 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
16350 6c 2c 20 22 27 2c 20 22 29 3b 0a 09 20 20 73 77  l, "', ");..  sw
16360 69 74 63 68 20 28 78 74 79 70 65 29 0a 09 20 20  itch (xtype)..  
16370 20 20 7b 0a 09 20 20 20 20 63 61 73 65 20 47 41    {..    case GA
16380 49 41 5f 50 4f 49 4e 54 3a 0a 09 09 69 66 20 28  IA_POINT:...if (
16390 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f  dims == GAIA_XY_
163a0 5a 29 0a 09 09 20 20 20 20 73 74 72 63 61 74 20  Z)...    strcat 
163b0 28 73 71 6c 2c 20 22 31 30 30 31 2c 20 22 29 3b  (sql, "1001, ");
163c0 0a 09 09 65 6c 73 65 20 69 66 20 28 64 69 6d 73  ...else if (dims
163d0 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09   == GAIA_XY_M)..
163e0 09 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
163f0 2c 20 22 32 30 30 31 2c 20 22 29 3b 0a 09 09 65  , "2001, ");...e
16400 6c 73 65 20 69 66 20 28 64 69 6d 73 20 3d 3d 20  lse if (dims == 
16410 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 20  GAIA_XY_Z_M)... 
16420 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
16430 22 33 30 30 31 2c 20 22 29 3b 0a 09 09 65 6c 73  "3001, ");...els
16440 65 0a 09 09 20 20 20 20 73 74 72 63 61 74 20 28  e...    strcat (
16450 73 71 6c 2c 20 22 31 2c 20 22 29 3b 0a 09 09 62  sql, "1, ");...b
16460 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
16470 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 3a  GAIA_LINESTRING:
16480 0a 09 09 69 66 20 28 64 69 6d 73 20 3d 3d 20 47  ...if (dims == G
16490 41 49 41 5f 58 59 5f 5a 29 0a 09 09 20 20 20 20  AIA_XY_Z)...    
164a0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 31 30  strcat (sql, "10
164b0 30 32 2c 20 22 29 3b 0a 09 09 65 6c 73 65 20 69  02, ");...else i
164c0 66 20 28 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f  f (dims == GAIA_
164d0 58 59 5f 4d 29 0a 09 09 20 20 20 20 73 74 72 63  XY_M)...    strc
164e0 61 74 20 28 73 71 6c 2c 20 22 32 30 30 32 2c 20  at (sql, "2002, 
164f0 22 29 3b 0a 09 09 65 6c 73 65 20 69 66 20 28 64  ");...else if (d
16500 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a  ims == GAIA_XY_Z
16510 5f 4d 29 0a 09 09 20 20 20 20 73 74 72 63 61 74  _M)...    strcat
16520 20 28 73 71 6c 2c 20 22 33 30 30 32 2c 20 22 29   (sql, "3002, ")
16530 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 73  ;...else...    s
16540 74 72 63 61 74 20 28 73 71 6c 2c 20 22 32 2c 20  trcat (sql, "2, 
16550 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  ");...break;..  
16560 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f 4c 59    case GAIA_POLY
16570 47 4f 4e 3a 0a 09 09 69 66 20 28 64 69 6d 73 20  GON:...if (dims 
16580 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09 09  == GAIA_XY_Z)...
16590 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
165a0 20 22 31 30 30 33 2c 20 22 29 3b 0a 09 09 65 6c   "1003, ");...el
165b0 73 65 20 69 66 20 28 64 69 6d 73 20 3d 3d 20 47  se if (dims == G
165c0 41 49 41 5f 58 59 5f 4d 29 0a 09 09 20 20 20 20  AIA_XY_M)...    
165d0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 32 30  strcat (sql, "20
165e0 30 33 2c 20 22 29 3b 0a 09 09 65 6c 73 65 20 69  03, ");...else i
165f0 66 20 28 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f  f (dims == GAIA_
16600 58 59 5f 5a 5f 4d 29 0a 09 09 20 20 20 20 73 74  XY_Z_M)...    st
16610 72 63 61 74 20 28 73 71 6c 2c 20 22 33 30 30 33  rcat (sql, "3003
16620 2c 20 22 29 3b 0a 09 09 65 6c 73 65 0a 09 09 20  , ");...else... 
16630 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
16640 22 33 2c 20 22 29 3b 0a 09 09 62 72 65 61 6b 3b  "3, ");...break;
16650 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
16660 4d 55 4c 54 49 50 4f 49 4e 54 3a 0a 09 09 69 66  MULTIPOINT:...if
16670 20 28 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58   (dims == GAIA_X
16680 59 5f 5a 29 0a 09 09 20 20 20 20 73 74 72 63 61  Y_Z)...    strca
16690 74 20 28 73 71 6c 2c 20 22 31 30 30 34 2c 20 22  t (sql, "1004, "
166a0 29 3b 0a 09 09 65 6c 73 65 20 69 66 20 28 64 69  );...else if (di
166b0 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 29  ms == GAIA_XY_M)
166c0 0a 09 09 20 20 20 20 73 74 72 63 61 74 20 28 73  ...    strcat (s
166d0 71 6c 2c 20 22 32 30 30 34 2c 20 22 29 3b 0a 09  ql, "2004, ");..
166e0 09 65 6c 73 65 20 69 66 20 28 64 69 6d 73 20 3d  .else if (dims =
166f0 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09  = GAIA_XY_Z_M)..
16700 09 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
16710 2c 20 22 33 30 30 34 2c 20 22 29 3b 0a 09 09 65  , "3004, ");...e
16720 6c 73 65 0a 09 09 20 20 20 20 73 74 72 63 61 74  lse...    strcat
16730 20 28 73 71 6c 2c 20 22 34 2c 20 22 29 3b 0a 09   (sql, "4, ");..
16740 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
16750 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45  e GAIA_MULTILINE
16760 53 54 52 49 4e 47 3a 0a 09 09 69 66 20 28 64 69  STRING:...if (di
16770 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29  ms == GAIA_XY_Z)
16780 0a 09 09 20 20 20 20 73 74 72 63 61 74 20 28 73  ...    strcat (s
16790 71 6c 2c 20 22 31 30 30 35 2c 20 22 29 3b 0a 09  ql, "1005, ");..
167a0 09 65 6c 73 65 20 69 66 20 28 64 69 6d 73 20 3d  .else if (dims =
167b0 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 09 20  = GAIA_XY_M)... 
167c0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
167d0 22 32 30 30 35 2c 20 22 29 3b 0a 09 09 65 6c 73  "2005, ");...els
167e0 65 20 69 66 20 28 64 69 6d 73 20 3d 3d 20 47 41  e if (dims == GA
167f0 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 20 20 20  IA_XY_Z_M)...   
16800 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 33   strcat (sql, "3
16810 30 30 35 2c 20 22 29 3b 0a 09 09 65 6c 73 65 0a  005, ");...else.
16820 09 09 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ..    strcat (sq
16830 6c 2c 20 22 35 2c 20 22 29 3b 0a 09 09 62 72 65  l, "5, ");...bre
16840 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
16850 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 3a  IA_MULTIPOLYGON:
16860 0a 09 09 69 66 20 28 64 69 6d 73 20 3d 3d 20 47  ...if (dims == G
16870 41 49 41 5f 58 59 5f 5a 29 0a 09 09 20 20 20 20  AIA_XY_Z)...    
16880 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 31 30  strcat (sql, "10
16890 30 36 2c 20 22 29 3b 0a 09 09 65 6c 73 65 20 69  06, ");...else i
168a0 66 20 28 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f  f (dims == GAIA_
168b0 58 59 5f 4d 29 0a 09 09 20 20 20 20 73 74 72 63  XY_M)...    strc
168c0 61 74 20 28 73 71 6c 2c 20 22 32 30 30 36 2c 20  at (sql, "2006, 
168d0 22 29 3b 0a 09 09 65 6c 73 65 20 69 66 20 28 64  ");...else if (d
168e0 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a  ims == GAIA_XY_Z
168f0 5f 4d 29 0a 09 09 20 20 20 20 73 74 72 63 61 74  _M)...    strcat
16900 20 28 73 71 6c 2c 20 22 33 30 30 36 2c 20 22 29   (sql, "3006, ")
16910 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 73  ;...else...    s
16920 74 72 63 61 74 20 28 73 71 6c 2c 20 22 36 2c 20  trcat (sql, "6, 
16930 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  ");...break;..  
16940 20 20 63 61 73 65 20 47 41 49 41 5f 47 45 4f 4d    case GAIA_GEOM
16950 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 3a 0a  ETRYCOLLECTION:.
16960 09 09 69 66 20 28 64 69 6d 73 20 3d 3d 20 47 41  ..if (dims == GA
16970 49 41 5f 58 59 5f 5a 29 0a 09 09 20 20 20 20 73  IA_XY_Z)...    s
16980 74 72 63 61 74 20 28 73 71 6c 2c 20 22 31 30 30  trcat (sql, "100
16990 37 2c 20 22 29 3b 0a 09 09 65 6c 73 65 20 69 66  7, ");...else if
169a0 20 28 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58   (dims == GAIA_X
169b0 59 5f 4d 29 0a 09 09 20 20 20 20 73 74 72 63 61  Y_M)...    strca
169c0 74 20 28 73 71 6c 2c 20 22 32 30 30 37 2c 20 22  t (sql, "2007, "
169d0 29 3b 0a 09 09 65 6c 73 65 20 69 66 20 28 64 69  );...else if (di
169e0 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f  ms == GAIA_XY_Z_
169f0 4d 29 0a 09 09 20 20 20 20 73 74 72 63 61 74 20  M)...    strcat 
16a00 28 73 71 6c 2c 20 22 33 30 30 37 2c 20 22 29 3b  (sql, "3007, ");
16a10 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 73 74  ...else...    st
16a20 72 63 61 74 20 28 73 71 6c 2c 20 22 37 2c 20 22  rcat (sql, "7, "
16a30 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  );...break;..   
16a40 20 63 61 73 65 20 2d 31 3a 0a 09 09 69 66 20 28   case -1:...if (
16a50 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f  dims == GAIA_XY_
16a60 5a 29 0a 09 09 20 20 20 20 73 74 72 63 61 74 20  Z)...    strcat 
16a70 28 73 71 6c 2c 20 22 31 30 30 30 2c 20 22 29 3b  (sql, "1000, ");
16a80 0a 09 09 65 6c 73 65 20 69 66 20 28 64 69 6d 73  ...else if (dims
16a90 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09   == GAIA_XY_M)..
16aa0 09 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
16ab0 2c 20 22 32 30 30 30 2c 20 22 29 3b 0a 09 09 65  , "2000, ");...e
16ac0 6c 73 65 20 69 66 20 28 64 69 6d 73 20 3d 3d 20  lse if (dims == 
16ad0 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 20  GAIA_XY_Z_M)... 
16ae0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
16af0 22 33 30 30 30 2c 20 22 29 3b 0a 09 09 65 6c 73  "3000, ");...els
16b00 65 0a 09 09 20 20 20 20 73 74 72 63 61 74 20 28  e...    strcat (
16b10 73 71 6c 2c 20 22 30 2c 20 22 29 3b 0a 09 09 62  sql, "0, ");...b
16b20 72 65 61 6b 3b 0a 09 20 20 20 20 7d 3b 0a 09 20  reak;..    };.. 
16b30 20 73 77 69 74 63 68 20 28 64 69 6d 73 29 0a 09   switch (dims)..
16b40 20 20 20 20 7b 0a 09 20 20 20 20 63 61 73 65 20      {..    case 
16b50 47 41 49 41 5f 58 59 3a 0a 09 09 73 74 72 63 61  GAIA_XY:...strca
16b60 74 20 28 73 71 6c 2c 20 22 32 22 29 3b 0a 09 09  t (sql, "2");...
16b70 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
16b80 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09 20 20 20   GAIA_XY_Z:..   
16b90 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 4d 3a   case GAIA_XY_M:
16ba0 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  ...strcat (sql, 
16bb0 22 33 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  "3");...break;..
16bc0 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 58 59      case GAIA_XY
16bd0 5f 5a 5f 4d 3a 0a 09 09 73 74 72 63 61 74 20 28  _Z_M:...strcat (
16be0 73 71 6c 2c 20 22 34 22 29 3b 0a 09 09 62 72 65  sql, "4");...bre
16bf0 61 6b 3b 0a 09 20 20 20 20 7d 3b 0a 09 20 20 73  ak;..    };..  s
16c00 74 72 63 61 74 20 28 73 71 6c 2c 20 22 2c 20 22  trcat (sql, ", "
16c10 29 3b 0a 09 20 20 73 70 72 69 6e 74 66 20 28 64  );..  sprintf (d
16c20 75 6d 6d 79 2c 20 22 25 64 22 2c 20 73 72 69 64  ummy, "%d", srid
16c30 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
16c40 6c 2c 20 64 75 6d 6d 79 29 3b 0a 09 20 20 73 74  l, dummy);..  st
16c50 72 63 61 74 20 28 73 71 6c 2c 20 22 2c 20 30 29  rcat (sql, ", 0)
16c60 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ");.      }.    
16c70 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78  ret = sqlite3_ex
16c80 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c  ec (sqlite, sql,
16c90 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72   NULL, NULL, &er
16ca0 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72  rMsg);.    if (r
16cb0 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29  et != SQLITE_OK)
16cc0 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20  ..goto error;.  
16cd0 20 20 69 66 20 28 6d 65 74 61 64 61 74 61 5f 76    if (metadata_v
16ce0 65 72 73 69 6f 6e 20 3d 3d 20 33 29 0a 20 20 20  ersion == 3).   
16cf0 20 20 20 7b 0a 09 20 20 2f 2a 20 63 75 72 72 65     {..  /* curre
16d00 6e 74 20 6d 65 74 61 64 61 74 61 20 73 74 79 6c  nt metadata styl
16d10 65 20 3e 3d 20 76 2e 34 2e 30 2e 30 20 2a 2f 0a  e >= v.4.0.0 */.
16d20 0a 09 20 20 2f 2a 20 69 6e 73 65 72 74 69 6e 67  ..  /* inserting
16d30 20 61 20 72 6f 77 20 69 6e 74 6f 20 47 45 4f 4d   a row into GEOM
16d40 45 54 52 59 5f 43 4f 4c 55 4d 4e 53 5f 41 55 54  ETRY_COLUMNS_AUT
16d50 48 20 2a 2f 0a 09 20 20 73 74 72 63 70 79 20 28  H */..  strcpy (
16d60 73 71 6c 2c 0a 09 09 20 20 22 49 4e 53 45 52 54  sql,...  "INSERT
16d70 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f   OR REPLACE INTO
16d80 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e   geometry_column
16d90 73 5f 61 75 74 68 20 28 66 5f 74 61 62 6c 65 5f  s_auth (f_table_
16da0 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79  name, f_geometry
16db0 5f 63 6f 6c 75 6d 6e 2c 20 22 29 3b 0a 09 20 20  _column, ");..  
16dc0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 72 65  strcat (sql, "re
16dd0 61 64 5f 6f 6e 6c 79 2c 20 68 69 64 64 65 6e 29  ad_only, hidden)
16de0 20 56 41 4c 55 45 53 20 28 22 29 3b 0a 09 20 20   VALUES (");..  
16df0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 22  strcat (sql, "'"
16e00 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
16e10 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09 20  l, sqltable);.. 
16e20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27   strcat (sql, "'
16e30 2c 20 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74  , '");..  strcat
16e40 20 28 73 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e   (sql, sqlcolumn
16e50 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
16e60 6c 2c 20 22 27 2c 20 30 2c 20 30 29 22 29 3b 0a  l, "', 0, 0)");.
16e70 09 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33  .  ret = sqlite3
16e80 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73  _exec (sqlite, s
16e90 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  ql, NULL, NULL, 
16ea0 26 65 72 72 4d 73 67 29 3b 0a 09 20 20 69 66 20  &errMsg);..  if 
16eb0 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
16ec0 4b 29 0a 09 20 20 20 20 20 20 67 6f 74 6f 20 65  K)..      goto e
16ed0 72 72 6f 72 3b 0a 09 20 20 2f 2a 20 69 6e 73 65  rror;..  /* inse
16ee0 72 74 69 6e 67 20 61 20 72 6f 77 20 69 6e 74 6f  rting a row into
16ef0 20 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e   GEOMETRY_COLUMN
16f00 53 5f 53 54 41 54 49 53 54 49 43 53 20 2a 2f 0a  S_STATISTICS */.
16f10 09 20 20 73 74 72 63 70 79 20 28 73 71 6c 2c 0a  .  strcpy (sql,.
16f20 09 09 20 20 22 49 4e 53 45 52 54 20 4f 52 20 52  ..  "INSERT OR R
16f30 45 50 4c 41 43 45 20 49 4e 54 4f 20 67 65 6f 6d  EPLACE INTO geom
16f40 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 73 74 61  etry_columns_sta
16f50 74 69 73 74 69 63 73 20 28 66 5f 74 61 62 6c 65  tistics (f_table
16f60 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72  _name, f_geometr
16f70 79 5f 63 6f 6c 75 6d 6e 29 20 22 29 3b 0a 09 20  y_column) ");.. 
16f80 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 56   strcat (sql, "V
16f90 41 4c 55 45 53 20 28 22 29 3b 0a 09 20 20 73 74  ALUES (");..  st
16fa0 72 63 61 74 20 28 73 71 6c 2c 20 22 27 22 29 3b  rcat (sql, "'");
16fb0 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c  ..  strcat (sql,
16fc0 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09 20 20 73   sqltable);..  s
16fd0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 2c 20  trcat (sql, "', 
16fe0 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28  '");..  strcat (
16ff0 73 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b  sql, sqlcolumn);
17000 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c  ..  strcat (sql,
17010 20 22 27 29 22 29 3b 0a 09 20 20 72 65 74 20 3d   "')");..  ret =
17020 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73   sqlite3_exec (s
17030 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c  qlite, sql, NULL
17040 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29  , NULL, &errMsg)
17050 3b 0a 09 20 20 69 66 20 28 72 65 74 20 21 3d 20  ;..  if (ret != 
17060 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20 20 20  SQLITE_OK)..    
17070 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 09 20    goto error;.. 
17080 20 2f 2a 20 69 6e 73 65 72 74 69 6e 67 20 61 20   /* inserting a 
17090 72 6f 77 20 69 6e 74 6f 20 47 45 4f 4d 45 54 52  row into GEOMETR
170a0 59 5f 43 4f 4c 55 4d 4e 53 5f 54 49 4d 45 20 2a  Y_COLUMNS_TIME *
170b0 2f 0a 09 20 20 73 74 72 63 70 79 20 28 73 71 6c  /..  strcpy (sql
170c0 2c 0a 09 09 20 20 22 49 4e 53 45 52 54 20 4f 52  ,...  "INSERT OR
170d0 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 67 65   REPLACE INTO ge
170e0 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 74  ometry_columns_t
170f0 69 6d 65 20 28 66 5f 74 61 62 6c 65 5f 6e 61 6d  ime (f_table_nam
17100 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f  e, f_geometry_co
17110 6c 75 6d 6e 29 20 22 29 3b 0a 09 20 20 73 74 72  lumn) ");..  str
17120 63 61 74 20 28 73 71 6c 2c 20 22 56 41 4c 55 45  cat (sql, "VALUE
17130 53 20 28 22 29 3b 0a 09 20 20 73 74 72 63 61 74  S (");..  strcat
17140 20 28 73 71 6c 2c 20 22 27 22 29 3b 0a 09 20 20   (sql, "'");..  
17150 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c  strcat (sql, sql
17160 74 61 62 6c 65 29 3b 0a 09 20 20 73 74 72 63 61  table);..  strca
17170 74 20 28 73 71 6c 2c 20 22 27 2c 20 27 22 29 3b  t (sql, "', '");
17180 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c  ..  strcat (sql,
17190 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 09 20 20   sqlcolumn);..  
171a0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 29  strcat (sql, "')
171b0 22 29 3b 0a 09 20 20 72 65 74 20 3d 20 73 71 6c  ");..  ret = sql
171c0 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74  ite3_exec (sqlit
171d0 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55  e, sql, NULL, NU
171e0 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 09 20  LL, &errMsg);.. 
171f0 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
17200 54 45 5f 4f 4b 29 0a 09 20 20 20 20 20 20 67 6f  TE_OK)..      go
17210 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to error;.      
17220 7d 0a 20 20 20 20 75 70 64 61 74 65 47 65 6f 6d  }.    updateGeom
17230 65 74 72 79 54 72 69 67 67 65 72 73 20 28 73 71  etryTriggers (sq
17240 6c 69 74 65 2c 20 74 61 62 6c 65 2c 20 63 6f 6c  lite, table, col
17250 75 6d 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  umn);.    sqlite
17260 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
17270 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 20 20 73  ntext, 1);.    s
17280 74 72 63 70 79 20 28 73 71 6c 2c 20 22 47 65 6f  trcpy (sql, "Geo
17290 6d 65 74 72 79 20 5b 22 29 3b 0a 20 20 20 20 73  metry [");.    s
172a0 77 69 74 63 68 20 28 78 74 79 70 65 29 0a 20 20  witch (xtype).  
172b0 20 20 20 20 7b 0a 20 20 20 20 20 20 63 61 73 65      {.      case
172c0 20 47 41 49 41 5f 50 4f 49 4e 54 3a 0a 09 20 20   GAIA_POINT:..  
172d0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 50 4f  strcat (sql, "PO
172e0 49 4e 54 22 29 3b 0a 09 20 20 62 72 65 61 6b 3b  INT");..  break;
172f0 0a 20 20 20 20 20 20 63 61 73 65 20 47 41 49 41  .      case GAIA
17300 5f 4c 49 4e 45 53 54 52 49 4e 47 3a 0a 09 20 20  _LINESTRING:..  
17310 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4c 49  strcat (sql, "LI
17320 4e 45 53 54 52 49 4e 47 22 29 3b 0a 09 20 20 62  NESTRING");..  b
17330 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
17340 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 3a 0a 09   GAIA_POLYGON:..
17350 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
17360 50 4f 4c 59 47 4f 4e 22 29 3b 0a 09 20 20 62 72  POLYGON");..  br
17370 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
17380 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 3a  GAIA_MULTIPOINT:
17390 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c  ..  strcat (sql,
173a0 20 22 4d 55 4c 54 49 50 4f 49 4e 54 22 29 3b 0a   "MULTIPOINT");.
173b0 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  .  break;.      
173c0 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c  case GAIA_MULTIL
173d0 49 4e 45 53 54 52 49 4e 47 3a 0a 09 20 20 73 74  INESTRING:..  st
173e0 72 63 61 74 20 28 73 71 6c 2c 20 22 4d 55 4c 54  rcat (sql, "MULT
173f0 49 4c 49 4e 45 53 54 52 49 4e 47 22 29 3b 0a 09  ILINESTRING");..
17400 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
17410 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f  ase GAIA_MULTIPO
17420 4c 59 47 4f 4e 3a 0a 09 20 20 73 74 72 63 61 74  LYGON:..  strcat
17430 20 28 73 71 6c 2c 20 22 4d 55 4c 54 49 50 4f 4c   (sql, "MULTIPOL
17440 59 47 4f 4e 22 29 3b 0a 09 20 20 62 72 65 61 6b  YGON");..  break
17450 3b 0a 20 20 20 20 20 20 63 61 73 65 20 47 41 49  ;.      case GAI
17460 41 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43  A_GEOMETRYCOLLEC
17470 54 49 4f 4e 3a 0a 09 20 20 73 74 72 63 61 74 20  TION:..  strcat 
17480 28 73 71 6c 2c 20 22 47 45 4f 4d 45 54 52 59 43  (sql, "GEOMETRYC
17490 4f 4c 4c 45 43 54 49 4f 4e 22 29 3b 0a 09 20 20  OLLECTION");..  
174a0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
174b0 65 20 2d 31 3a 0a 09 20 20 73 74 72 63 61 74 20  e -1:..  strcat 
174c0 28 73 71 6c 2c 20 22 47 45 4f 4d 45 54 52 59 22  (sql, "GEOMETRY"
174d0 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20  );..  break;.   
174e0 20 20 20 7d 3b 0a 20 20 20 20 73 74 72 63 61 74     };.    strcat
174f0 20 28 73 71 6c 2c 20 22 2c 22 29 3b 0a 20 20 20   (sql, ",");.   
17500 20 73 77 69 74 63 68 20 28 64 69 6d 73 29 0a 20   switch (dims). 
17510 20 20 20 20 20 7b 0a 20 20 20 20 20 20 63 61 73       {.      cas
17520 65 20 47 41 49 41 5f 58 59 3a 0a 09 20 20 73 74  e GAIA_XY:..  st
17530 72 63 61 74 20 28 73 71 6c 2c 20 22 58 59 22 29  rcat (sql, "XY")
17540 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;..  break;.    
17550 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a    case GAIA_XY_Z
17560 3a 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c  :..  strcat (sql
17570 2c 20 22 58 59 5a 22 29 3b 0a 09 20 20 62 72 65  , "XYZ");..  bre
17580 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 47  ak;.      case G
17590 41 49 41 5f 58 59 5f 4d 3a 0a 09 20 20 73 74 72  AIA_XY_M:..  str
175a0 63 61 74 20 28 73 71 6c 2c 20 22 58 59 4d 22 29  cat (sql, "XYM")
175b0 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;..  break;.    
175c0 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a    case GAIA_XY_Z
175d0 5f 4d 3a 0a 09 20 20 73 74 72 63 61 74 20 28 73  _M:..  strcat (s
175e0 71 6c 2c 20 22 58 59 5a 4d 22 29 3b 0a 09 20 20  ql, "XYZM");..  
175f0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 3b 0a  break;.      };.
17600 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
17610 63 6f 6c 75 6d 6e 2c 20 22 2c 53 52 49 44 3d 25  column, ",SRID=%
17620 64 22 2c 20 28 73 72 69 64 20 3c 3d 20 30 29 20  d", (srid <= 0) 
17630 3f 20 2d 31 20 3a 20 73 72 69 64 29 3b 0a 20 20  ? -1 : srid);.  
17640 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
17650 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73  qlcolumn);.    s
17660 74 72 63 61 74 20 28 73 71 6c 2c 20 22 5d 20 73  trcat (sql, "] s
17670 75 63 63 65 73 73 66 75 6c 6c 79 20 63 72 65 61  uccessfully crea
17680 74 65 64 22 29 3b 0a 20 20 20 20 75 70 64 61 74  ted");.    updat
17690 65 53 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f  eSpatiaLiteHisto
176a0 72 79 20 28 73 71 6c 69 74 65 2c 20 28 63 6f 6e  ry (sqlite, (con
176b0 73 74 20 63 68 61 72 20 2a 29 20 74 61 62 6c 65  st char *) table
176c0 2c 0a 09 09 09 20 20 20 20 20 28 63 6f 6e 73 74  ,....     (const
176d0 20 63 68 61 72 20 2a 29 20 63 6f 6c 75 6d 6e 2c   char *) column,
176e0 20 73 71 6c 29 3b 0a 20 20 20 20 72 65 74 75 72   sql);.    retur
176f0 6e 3b 0a 20 20 65 72 72 6f 72 3a 0a 20 20 20 20  n;.  error:.    
17700 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 41  spatialite_e ("A
17710 64 64 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e  ddGeometryColumn
17720 28 29 20 65 72 72 6f 72 3a 20 5c 22 25 73 5c 22  () error: \"%s\"
17730 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b 0a 20 20  \n", errMsg);.  
17740 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28    sqlite3_free (
17750 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c  errMsg);.    sql
17760 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
17770 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
17780 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61    return;.}..sta
17790 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 52 65  tic void.fnct_Re
177a0 63 6f 76 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c  coverGeometryCol
177b0 75 6d 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  umn (sqlite3_con
177c0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
177d0 69 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 20 20  int argc,....   
177e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
177f0 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
17800 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 52 65 63   function:./ Rec
17810 6f 76 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75  overGeometryColu
17820 6d 6e 28 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e  mn(table, column
17830 2c 20 73 72 69 64 2c 20 74 79 70 65 20 2c 20 64  , srid, type , d
17840 69 6d 65 6e 73 69 6f 6e 20 29 0a 2f 0a 2f 20 63  imension )././ c
17850 68 65 63 6b 73 20 69 66 20 61 6e 20 65 78 69 73  hecks if an exis
17860 74 69 6e 67 20 54 41 42 4c 45 2e 43 4f 4c 55 4d  ting TABLE.COLUM
17870 4e 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20  N satisfies the 
17880 72 65 71 75 69 72 65 64 20 67 65 6f 6d 65 74 72  required geometr
17890 69 63 20 66 65 61 74 75 72 65 73 0a 2f 20 69 66  ic features./ if
178a0 20 79 65 73 20 61 64 64 73 20 69 74 20 74 6f 20   yes adds it to 
178b0 53 70 61 74 69 61 6c 4d 65 74 61 44 61 74 61 20  SpatialMetaData 
178c0 61 6e 64 20 65 6e 61 62 6c 69 6e 67 20 74 72 69  and enabling tri
178d0 67 67 65 72 73 0a 2f 20 72 65 74 75 72 6e 73 20  ggers./ returns 
178e0 31 20 6f 6e 20 73 75 63 63 65 73 73 0a 2f 20 30  1 on success./ 0
178f0 20 6f 6e 20 66 61 69 6c 75 72 65 0a 2a 2f 0a 20   on failure.*/. 
17900 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74     const char *t
17910 61 62 6c 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  able;.    const 
17920 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 3b 0a 20 20  char *column;.  
17930 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
17940 20 63 68 61 72 20 2a 74 79 70 65 3b 0a 20 20 20   char *type;.   
17950 20 69 6e 74 20 78 74 79 70 65 3b 0a 20 20 20 20   int xtype;.    
17960 69 6e 74 20 78 78 74 79 70 65 3b 0a 20 20 20 20  int xxtype;.    
17970 69 6e 74 20 73 72 69 64 20 3d 20 2d 31 3b 0a 20  int srid = -1;. 
17980 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
17990 64 20 63 68 61 72 20 2a 74 78 74 5f 64 69 6d 73  d char *txt_dims
179a0 3b 0a 20 20 20 20 69 6e 74 20 64 69 6d 65 6e 73  ;.    int dimens
179b0 69 6f 6e 20 3d 20 32 3b 0a 20 20 20 20 69 6e 74  ion = 2;.    int
179c0 20 64 69 6d 73 20 3d 20 2d 31 3b 0a 20 20 20 20   dims = -1;.    
179d0 63 68 61 72 20 64 75 6d 6d 79 5b 33 32 5d 3b 0a  char dummy[32];.
179e0 20 20 20 20 63 68 61 72 20 73 71 6c 5b 31 30 32      char sql[102
179f0 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72  4];.    char *er
17a00 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  rMsg = NULL;.   
17a10 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 68   int ret;.    ch
17a20 61 72 20 2a 2a 72 65 73 75 6c 74 73 3b 0a 20 20  ar **results;.  
17a30 20 20 69 6e 74 20 72 6f 77 73 3b 0a 20 20 20 20    int rows;.    
17a40 69 6e 74 20 63 6f 6c 75 6d 6e 73 3b 0a 20 20 20  int columns;.   
17a50 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72   int i;.    char
17a60 20 73 71 6c 74 61 62 6c 65 5b 31 30 32 34 5d 3b   sqltable[1024];
17a70 0a 20 20 20 20 63 68 61 72 20 73 71 6c 63 6f 6c  .    char sqlcol
17a80 75 6d 6e 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63  umn[1024];.    c
17a90 68 61 72 20 78 74 61 62 6c 65 5b 31 30 32 34 5d  har xtable[1024]
17aa0 3b 0a 20 20 20 20 63 68 61 72 20 78 63 6f 6c 75  ;.    char xcolu
17ab0 6d 6e 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e  mn[1024];.    in
17ac0 74 20 6d 65 74 61 64 61 74 61 5f 76 65 72 73 69  t metadata_versi
17ad0 6f 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  on;.    sqlite3 
17ae0 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65  *sqlite = sqlite
17af0 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
17b00 64 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  dle (context);. 
17b10 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
17b20 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
17b30 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
17b40 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
17b50 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
17b60 20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20   SQLITE_TEXT).  
17b70 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c      {..  spatial
17b80 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 52  ite_e..      ("R
17b90 65 63 6f 76 65 72 47 65 6f 6d 65 74 72 79 43 6f  ecoverGeometryCo
17ba0 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61 72  lumn() error: ar
17bb0 67 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c 65 5f  gument 1 [table_
17bc0 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20  name] is not of 
17bd0 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c  the String type\
17be0 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  n");..  sqlite3_
17bf0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
17c00 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75  ext, 0);..  retu
17c10 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
17c20 74 61 62 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63  table = (const c
17c30 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
17c40 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b  alue_text (argv[
17c50 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c  0]);.    if (sql
17c60 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
17c70 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c  (argv[1]) != SQL
17c80 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20  ITE_TEXT).      
17c90 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
17ca0 65 0a 09 20 20 20 20 20 20 28 22 52 65 63 6f 76  e..      ("Recov
17cb0 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e  erGeometryColumn
17cc0 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65  () error: argume
17cd0 6e 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61 6d  nt 2 [column_nam
17ce0 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65  e] is not of the
17cf0 20 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29   String type\n")
17d00 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ;..  sqlite3_res
17d10 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
17d20 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  , 0);..  return;
17d30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 63 6f 6c  .      }.    col
17d40 75 6d 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  umn = (const cha
17d50 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
17d60 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d  ue_text (argv[1]
17d70 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  );.    if (sqlit
17d80 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
17d90 72 67 76 5b 32 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[2]) != SQLIT
17da0 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
17db0 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
17dc0 5f 65 0a 09 20 20 20 20 20 20 28 22 52 65 63 6f  _e..      ("Reco
17dd0 76 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d  verGeometryColum
17de0 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d  n() error: argum
17df0 65 6e 74 20 33 20 5b 53 52 49 44 5d 20 69 73 20  ent 3 [SRID] is 
17e00 6e 6f 74 20 6f 66 20 74 68 65 20 49 6e 74 65 67  not of the Integ
17e10 65 72 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20  er type\n");..  
17e20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
17e30 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  nt (context, 0);
17e40 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
17e50 20 20 7d 0a 20 20 20 20 73 72 69 64 20 3d 20 73    }.    srid = s
17e60 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
17e70 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20   (argv[2]);.    
17e80 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
17e90 65 5f 74 79 70 65 20 28 61 72 67 76 5b 33 5d 29  e_type (argv[3])
17ea0 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29   != SQLITE_TEXT)
17eb0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
17ec0 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20  ialite_e..      
17ed0 28 22 52 65 63 6f 76 65 72 47 65 6f 6d 65 74 72  ("RecoverGeometr
17ee0 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a  yColumn() error:
17ef0 20 61 72 67 75 6d 65 6e 74 20 34 20 5b 67 65 6f   argument 4 [geo
17f00 6d 65 74 72 79 5f 74 79 70 65 5d 20 69 73 20 6e  metry_type] is n
17f10 6f 74 20 6f 66 20 74 68 65 20 53 74 72 69 6e 67  ot of the String
17f20 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20 73 71   type\n");..  sq
17f30 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
17f40 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
17f50 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
17f60 7d 0a 20 20 20 20 74 79 70 65 20 3d 20 73 71 6c  }.    type = sql
17f70 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
17f80 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 69  (argv[3]);.    i
17f90 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
17fa0 5f 74 79 70 65 20 28 61 72 67 76 5b 34 5d 29 20  _type (argv[4]) 
17fb0 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
17fc0 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 64 69  R).      {..  di
17fd0 6d 65 6e 73 69 6f 6e 20 3d 20 73 71 6c 69 74 65  mension = sqlite
17fe0 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
17ff0 76 5b 34 5d 29 3b 0a 09 20 20 69 66 20 28 64 69  v[4]);..  if (di
18000 6d 65 6e 73 69 6f 6e 20 3d 3d 20 32 29 0a 09 20  mension == 2).. 
18010 20 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41       dims = GAIA
18020 5f 58 59 3b 0a 09 20 20 69 66 20 28 64 69 6d 65  _XY;..  if (dime
18030 6e 73 69 6f 6e 20 3d 3d 20 33 29 0a 09 20 20 20  nsion == 3)..   
18040 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41 5f 58     dims = GAIA_X
18050 59 5f 5a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Y_Z;.      }.   
18060 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
18070 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
18080 67 76 5b 34 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[4]) == SQLITE
18090 5f 54 45 58 54 29 0a 20 20 20 20 20 20 7b 0a 09  _TEXT).      {..
180a0 20 20 74 78 74 5f 64 69 6d 73 20 3d 20 73 71 6c    txt_dims = sql
180b0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
180c0 28 61 72 67 76 5b 34 5d 29 3b 0a 09 20 20 69 66  (argv[4]);..  if
180d0 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 28 63   (strcasecmp ((c
180e0 68 61 72 20 2a 29 20 74 78 74 5f 64 69 6d 73 2c  har *) txt_dims,
180f0 20 22 58 59 22 29 20 3d 3d 20 30 29 0a 09 20 20   "XY") == 0)..  
18100 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41 5f      dims = GAIA_
18110 58 59 3b 0a 09 20 20 69 66 20 28 73 74 72 63 61  XY;..  if (strca
18120 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20  secmp ((char *) 
18130 74 78 74 5f 64 69 6d 73 2c 20 22 58 59 5a 22 29  txt_dims, "XYZ")
18140 20 3d 3d 20 30 29 0a 09 20 20 20 20 20 20 64 69   == 0)..      di
18150 6d 73 20 3d 20 47 41 49 41 5f 58 59 5f 5a 3b 0a  ms = GAIA_XY_Z;.
18160 09 20 20 69 66 20 28 73 74 72 63 61 73 65 63 6d  .  if (strcasecm
18170 70 20 28 28 63 68 61 72 20 2a 29 20 74 78 74 5f  p ((char *) txt_
18180 64 69 6d 73 2c 20 22 58 59 4d 22 29 20 3d 3d 20  dims, "XYM") == 
18190 30 29 0a 09 20 20 20 20 20 20 64 69 6d 73 20 3d  0)..      dims =
181a0 20 47 41 49 41 5f 58 59 5f 4d 3b 0a 09 20 20 69   GAIA_XY_M;..  i
181b0 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 28  f (strcasecmp ((
181c0 63 68 61 72 20 2a 29 20 74 78 74 5f 64 69 6d 73  char *) txt_dims
181d0 2c 20 22 58 59 5a 4d 22 29 20 3d 3d 20 30 29 0a  , "XYZM") == 0).
181e0 09 20 20 20 20 20 20 64 69 6d 73 20 3d 20 47 41  .      dims = GA
181f0 49 41 5f 58 59 5f 5a 5f 4d 3b 0a 20 20 20 20 20  IA_XY_Z_M;.     
18200 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20   }.    else.    
18210 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
18220 65 5f 65 0a 09 20 20 20 20 20 20 28 22 52 65 63  e_e..      ("Rec
18230 6f 76 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75  overGeometryColu
18240 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75  mn() error: argu
18250 6d 65 6e 74 20 35 20 5b 64 69 6d 65 6e 73 69 6f  ment 5 [dimensio
18260 6e 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65  n] is not of the
18270 20 49 6e 74 65 67 65 72 20 6f 72 20 54 65 78 74   Integer or Text
18280 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20 73 71   type\n");..  sq
18290 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
182a0 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
182b0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
182c0 7d 0a 20 20 20 20 78 74 79 70 65 20 3d 20 47 41  }.    xtype = GA
182d0 49 41 5f 55 4e 4b 4e 4f 57 4e 3b 0a 20 20 20 20  IA_UNKNOWN;.    
182e0 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28  if (strcasecmp (
182f0 28 63 68 61 72 20 2a 29 20 74 79 70 65 2c 20 22  (char *) type, "
18300 50 4f 49 4e 54 22 29 20 3d 3d 20 30 29 0a 09 78  POINT") == 0)..x
18310 74 79 70 65 20 3d 20 47 41 49 41 5f 50 4f 49 4e  type = GAIA_POIN
18320 54 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63 61  T;.    if (strca
18330 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20  secmp ((char *) 
18340 74 79 70 65 2c 20 22 4c 49 4e 45 53 54 52 49 4e  type, "LINESTRIN
18350 47 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70 65  G") == 0)..xtype
18360 20 3d 20 47 41 49 41 5f 4c 49 4e 45 53 54 52 49   = GAIA_LINESTRI
18370 4e 47 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63  NG;.    if (strc
18380 61 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29  asecmp ((char *)
18390 20 74 79 70 65 2c 20 22 50 4f 4c 59 47 4f 4e 22   type, "POLYGON"
183a0 29 20 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d  ) == 0)..xtype =
183b0 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 3b 0a 20   GAIA_POLYGON;. 
183c0 20 20 20 69 66 20 28 73 74 72 63 61 73 65 63 6d     if (strcasecm
183d0 70 20 28 28 63 68 61 72 20 2a 29 20 74 79 70 65  p ((char *) type
183e0 2c 20 22 4d 55 4c 54 49 50 4f 49 4e 54 22 29 20  , "MULTIPOINT") 
183f0 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20 47  == 0)..xtype = G
18400 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 3b 0a  AIA_MULTIPOINT;.
18410 20 20 20 20 69 66 20 28 73 74 72 63 61 73 65 63      if (strcasec
18420 6d 70 20 28 28 63 68 61 72 20 2a 29 20 74 79 70  mp ((char *) typ
18430 65 2c 20 22 4d 55 4c 54 49 4c 49 4e 45 53 54 52  e, "MULTILINESTR
18440 49 4e 47 22 29 20 3d 3d 20 30 29 0a 09 78 74 79  ING") == 0)..xty
18450 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49 4c  pe = GAIA_MULTIL
18460 49 4e 45 53 54 52 49 4e 47 3b 0a 20 20 20 20 69  INESTRING;.    i
18470 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 28  f (strcasecmp ((
18480 63 68 61 72 20 2a 29 20 74 79 70 65 2c 20 22 4d  char *) type, "M
18490 55 4c 54 49 50 4f 4c 59 47 4f 4e 22 29 20 3d 3d  ULTIPOLYGON") ==
184a0 20 30 29 0a 09 78 74 79 70 65 20 3d 20 47 41 49   0)..xtype = GAI
184b0 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 3b 0a  A_MULTIPOLYGON;.
184c0 20 20 20 20 69 66 20 28 73 74 72 63 61 73 65 63      if (strcasec
184d0 6d 70 20 28 28 63 68 61 72 20 2a 29 20 74 79 70  mp ((char *) typ
184e0 65 2c 20 22 47 45 4f 4d 45 54 52 59 43 4f 4c 4c  e, "GEOMETRYCOLL
184f0 45 43 54 49 4f 4e 22 29 20 3d 3d 20 30 29 0a 09  ECTION") == 0)..
18500 78 74 79 70 65 20 3d 20 47 41 49 41 5f 47 45 4f  xtype = GAIA_GEO
18510 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 3b  METRYCOLLECTION;
18520 0a 20 20 20 20 69 66 20 28 73 74 72 63 61 73 65  .    if (strcase
18530 63 6d 70 20 28 28 63 68 61 72 20 2a 29 20 74 79  cmp ((char *) ty
18540 70 65 2c 20 22 47 45 4f 4d 45 54 52 59 22 29 20  pe, "GEOMETRY") 
18550 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20 2d  == 0)..xtype = -
18560 31 3b 0a 20 20 20 20 69 66 20 28 78 74 79 70 65  1;.    if (xtype
18570 20 3d 3d 20 47 41 49 41 5f 55 4e 4b 4e 4f 57 4e   == GAIA_UNKNOWN
18580 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61  ).      {..  spa
18590 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20  tialite_e..     
185a0 20 28 22 52 65 63 6f 76 65 72 47 65 6f 6d 65 74   ("RecoverGeomet
185b0 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72  ryColumn() error
185c0 3a 20 61 72 67 75 6d 65 6e 74 20 34 20 5b 67 65  : argument 4 [ge
185d0 6f 6d 65 74 72 79 5f 74 79 70 65 5d 20 68 61 73  ometry_type] has
185e0 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c 75   an illegal valu
185f0 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65  e\n");..  sqlite
18600 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
18610 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
18620 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
18630 20 20 69 66 20 28 64 69 6d 73 20 3d 3d 20 47 41    if (dims == GA
18640 49 41 5f 58 59 20 7c 7c 20 64 69 6d 73 20 3d 3d  IA_XY || dims ==
18650 20 47 41 49 41 5f 58 59 5f 5a 20 7c 7c 20 64 69   GAIA_XY_Z || di
18660 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 0a  ms == GAIA_XY_M.
18670 09 7c 7c 20 64 69 6d 73 20 3d 3d 20 47 41 49 41  .|| dims == GAIA
18680 5f 58 59 5f 5a 5f 4d 29 0a 09 3b 0a 20 20 20 20  _XY_Z_M)..;.    
18690 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
186a0 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20  spatialite_e..  
186b0 20 20 20 20 28 22 52 65 63 6f 76 65 72 47 65 6f      ("RecoverGeo
186c0 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72  metryColumn() er
186d0 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 35 20  ror: argument 5 
186e0 5b 64 69 6d 65 6e 73 69 6f 6e 5d 20 49 4c 4c 45  [dimension] ILLE
186f0 47 41 4c 20 56 41 4c 55 45 5c 6e 22 29 3b 0a 09  GAL VALUE\n");..
18700 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
18710 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
18720 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
18730 20 20 20 20 7d 0a 20 20 20 20 6d 65 74 61 64 61      }.    metada
18740 74 61 5f 76 65 72 73 69 6f 6e 20 3d 20 63 68 65  ta_version = che
18750 63 6b 53 70 61 74 69 61 6c 4d 65 74 61 44 61 74  ckSpatialMetaDat
18760 61 20 28 73 71 6c 69 74 65 29 3b 0a 20 20 20 20  a (sqlite);.    
18770 69 66 20 28 6d 65 74 61 64 61 74 61 5f 76 65 72  if (metadata_ver
18780 73 69 6f 6e 20 3d 3d 20 31 20 7c 7c 20 6d 65 74  sion == 1 || met
18790 61 64 61 74 61 5f 76 65 72 73 69 6f 6e 20 3d 3d  adata_version ==
187a0 20 33 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a   3)..;.    else.
187b0 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
187c0 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28  alite_e..      (
187d0 22 52 65 63 6f 76 65 72 47 65 6f 6d 65 74 72 79  "RecoverGeometry
187e0 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20  Column() error: 
187f0 75 6e 65 78 70 65 63 74 65 64 20 6d 65 74 61 64  unexpected metad
18800 61 74 61 20 6c 61 79 6f 75 74 5c 6e 22 29 3b 0a  ata layout\n");.
18810 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
18820 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
18830 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  0);..  return;. 
18840 20 20 20 20 20 7d 0a 2f 2a 20 63 68 65 63 6b 69       }./* checki
18850 6e 67 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ng if the table 
18860 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 73 74  exists */.    st
18870 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20  rcpy (sqltable, 
18880 74 61 62 6c 65 29 3b 0a 20 20 20 20 63 6c 65 61  table);.    clea
18890 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71  n_sql_string (sq
188a0 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72  ltable);.    spr
188b0 69 6e 74 66 20 28 73 71 6c 2c 0a 09 20 20 20 20  intf (sql,..    
188c0 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 20 46 52   "SELECT name FR
188d0 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
188e0 20 57 48 45 52 45 20 74 79 70 65 20 3d 20 27 74   WHERE type = 't
188f0 61 62 6c 65 27 20 41 4e 44 20 55 70 70 65 72 28  able' AND Upper(
18900 6e 61 6d 65 29 20 3d 20 55 70 70 65 72 28 27 25  name) = Upper('%
18910 73 27 29 22 2c 0a 09 20 20 20 20 20 73 71 6c 74  s')",..     sqlt
18920 61 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 20 3d  able);.    ret =
18930 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
18940 6c 65 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c  le (sqlite, sql,
18950 20 26 72 65 73 75 6c 74 73 2c 20 26 72 6f 77 73   &results, &rows
18960 2c 20 26 63 6f 6c 75 6d 6e 73 2c 20 26 65 72 72  , &columns, &err
18970 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  Msg);.    if (re
18980 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
18990 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
189a0 61 6c 69 74 65 5f 65 20 28 22 52 65 63 6f 76 65  alite_e ("Recove
189b0 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 3a  rGeometryColumn:
189c0 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 65 72 72 4d   \"%s\"\n", errM
189d0 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  sg);..  sqlite3_
189e0 66 72 65 65 20 28 65 72 72 4d 73 67 29 3b 0a 09  free (errMsg);..
189f0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
18a00 7d 0a 20 20 20 20 2a 78 74 61 62 6c 65 20 3d 20  }.    *xtable = 
18a10 27 5c 30 27 3b 0a 20 20 20 20 66 6f 72 20 28 69  '\0';.    for (i
18a20 20 3d 20 31 3b 20 69 20 3c 3d 20 72 6f 77 73 3b   = 1; i <= rows;
18a30 20 69 2b 2b 29 0a 20 20 20 20 20 20 7b 0a 09 20   i++).      {.. 
18a40 20 2f 2a 20 72 65 74 72 69 65 76 69 6e 67 20 74   /* retrieving t
18a50 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61  he real table na
18a60 6d 65 20 2a 2f 0a 09 20 20 73 74 72 63 70 79 20  me */..  strcpy 
18a70 28 78 74 61 62 6c 65 2c 20 72 65 73 75 6c 74 73  (xtable, results
18a80 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 5d 29  [(i * columns)])
18a90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 73 71  ;.      }.    sq
18aa0 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
18ab0 20 28 72 65 73 75 6c 74 73 29 3b 0a 20 20 20 20   (results);.    
18ac0 69 66 20 28 2a 78 74 61 62 6c 65 20 3d 3d 20 27  if (*xtable == '
18ad0 5c 30 27 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  \0').      {..  
18ae0 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20  spatialite_e..  
18af0 20 20 20 20 28 22 52 65 63 6f 76 65 72 47 65 6f      ("RecoverGeo
18b00 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72  metryColumn() er
18b10 72 6f 72 3a 20 74 61 62 6c 65 20 27 25 73 27 20  ror: table '%s' 
18b20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 5c 6e  does not exist\n
18b30 22 2c 0a 09 20 20 20 20 20 20 20 74 61 62 6c 65  ",..       table
18b40 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
18b50 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
18b60 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e  t, 0);..  return
18b70 3b 0a 20 20 20 20 20 20 7d 0a 2f 2a 20 61 64 6a  ;.      }./* adj
18b80 75 73 74 69 6e 67 20 74 68 65 20 61 63 74 75 61  usting the actua
18b90 6c 20 47 65 6f 6d 65 74 72 79 54 79 70 65 20 2a  l GeometryType *
18ba0 2f 0a 20 20 20 20 78 78 74 79 70 65 20 3d 20 78  /.    xxtype = x
18bb0 74 79 70 65 3b 0a 20 20 20 20 78 74 79 70 65 20  type;.    xtype 
18bc0 3d 20 47 41 49 41 5f 55 4e 4b 4e 4f 57 4e 3b 0a  = GAIA_UNKNOWN;.
18bd0 20 20 20 20 69 66 20 28 78 78 74 79 70 65 20 3d      if (xxtype =
18be0 3d 20 47 41 49 41 5f 50 4f 49 4e 54 29 0a 20 20  = GAIA_POINT).  
18bf0 20 20 20 20 7b 0a 09 20 20 73 77 69 74 63 68 20      {..  switch 
18c00 28 64 69 6d 73 29 0a 09 20 20 20 20 7b 0a 09 20  (dims)..    {.. 
18c10 20 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f     case GAIA_XY_
18c20 5a 3a 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49  Z:...xtype = GAI
18c30 41 5f 50 4f 49 4e 54 5a 3b 0a 09 09 62 72 65 61  A_POINTZ;...brea
18c40 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49  k;..    case GAI
18c50 41 5f 58 59 5f 4d 3a 0a 09 09 78 74 79 70 65 20  A_XY_M:...xtype 
18c60 3d 20 47 41 49 41 5f 50 4f 49 4e 54 4d 3b 0a 09  = GAIA_POINTM;..
18c70 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
18c80 65 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09  e GAIA_XY_Z_M:..
18c90 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50 4f  .xtype = GAIA_PO
18ca0 49 4e 54 5a 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a  INTZM;...break;.
18cb0 09 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 09 09  .    default:...
18cc0 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50 4f 49  xtype = GAIA_POI
18cd0 4e 54 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  NT;...break;..  
18ce0 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20    };.      }.   
18cf0 20 69 66 20 28 78 78 74 79 70 65 20 3d 3d 20 47   if (xxtype == G
18d00 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 29 0a  AIA_LINESTRING).
18d10 20 20 20 20 20 20 7b 0a 09 20 20 73 77 69 74 63        {..  switc
18d20 68 20 28 64 69 6d 73 29 0a 09 20 20 20 20 7b 0a  h (dims)..    {.
18d30 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 58  .    case GAIA_X
18d40 59 5f 5a 3a 0a 09 09 78 74 79 70 65 20 3d 20 47  Y_Z:...xtype = G
18d50 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 5a 3b  AIA_LINESTRINGZ;
18d60 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
18d70 61 73 65 20 47 41 49 41 5f 58 59 5f 4d 3a 0a 09  ase GAIA_XY_M:..
18d80 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 4c 49  .xtype = GAIA_LI
18d90 4e 45 53 54 52 49 4e 47 4d 3b 0a 09 09 62 72 65  NESTRINGM;...bre
18da0 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
18db0 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09 09 78 74 79  IA_XY_Z_M:...xty
18dc0 70 65 20 3d 20 47 41 49 41 5f 4c 49 4e 45 53 54  pe = GAIA_LINEST
18dd0 52 49 4e 47 5a 4d 3b 0a 09 09 62 72 65 61 6b 3b  RINGZM;...break;
18de0 0a 09 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 09  ..    default:..
18df0 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 4c 49  .xtype = GAIA_LI
18e00 4e 45 53 54 52 49 4e 47 3b 0a 09 09 62 72 65 61  NESTRING;...brea
18e10 6b 3b 0a 09 20 20 20 20 7d 3b 0a 20 20 20 20 20  k;..    };.     
18e20 20 7d 0a 20 20 20 20 69 66 20 28 78 78 74 79 70   }.    if (xxtyp
18e30 65 20 3d 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f  e == GAIA_POLYGO
18e40 4e 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 77  N).      {..  sw
18e50 69 74 63 68 20 28 64 69 6d 73 29 0a 09 20 20 20  itch (dims)..   
18e60 20 7b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49   {..    case GAI
18e70 41 5f 58 59 5f 5a 3a 0a 09 09 78 74 79 70 65 20  A_XY_Z:...xtype 
18e80 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 5a 3b  = GAIA_POLYGONZ;
18e90 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
18ea0 61 73 65 20 47 41 49 41 5f 58 59 5f 4d 3a 0a 09  ase GAIA_XY_M:..
18eb0 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 50 4f  .xtype = GAIA_PO
18ec0 4c 59 47 4f 4e 4d 3b 0a 09 09 62 72 65 61 6b 3b  LYGONM;...break;
18ed0 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
18ee0 58 59 5f 5a 5f 4d 3a 0a 09 09 78 74 79 70 65 20  XY_Z_M:...xtype 
18ef0 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 5a 4d  = GAIA_POLYGONZM
18f00 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
18f10 64 65 66 61 75 6c 74 3a 0a 09 09 78 74 79 70 65  default:...xtype
18f20 20 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 3b   = GAIA_POLYGON;
18f30 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 7d  ...break;..    }
18f40 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
18f50 20 28 78 78 74 79 70 65 20 3d 3d 20 47 41 49 41   (xxtype == GAIA
18f60 5f 4d 55 4c 54 49 50 4f 49 4e 54 29 0a 20 20 20  _MULTIPOINT).   
18f70 20 20 20 7b 0a 09 20 20 73 77 69 74 63 68 20 28     {..  switch (
18f80 64 69 6d 73 29 0a 09 20 20 20 20 7b 0a 09 20 20  dims)..    {..  
18f90 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a    case GAIA_XY_Z
18fa0 3a 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49 41  :...xtype = GAIA
18fb0 5f 4d 55 4c 54 49 50 4f 49 4e 54 5a 3b 0a 09 09  _MULTIPOINTZ;...
18fc0 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
18fd0 20 47 41 49 41 5f 58 59 5f 4d 3a 0a 09 09 78 74   GAIA_XY_M:...xt
18fe0 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49  ype = GAIA_MULTI
18ff0 50 4f 49 4e 54 4d 3b 0a 09 09 62 72 65 61 6b 3b  POINTM;...break;
19000 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
19010 58 59 5f 5a 5f 4d 3a 0a 09 09 78 74 79 70 65 20  XY_Z_M:...xtype 
19020 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e  = GAIA_MULTIPOIN
19030 54 5a 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20  TZM;...break;.. 
19040 20 20 20 64 65 66 61 75 6c 74 3a 0a 09 09 78 74     default:...xt
19050 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49  ype = GAIA_MULTI
19060 50 4f 49 4e 54 3b 0a 09 09 62 72 65 61 6b 3b 0a  POINT;...break;.
19070 09 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a  .    };.      }.
19080 20 20 20 20 69 66 20 28 78 78 74 79 70 65 20 3d      if (xxtype =
19090 3d 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45  = GAIA_MULTILINE
190a0 53 54 52 49 4e 47 29 0a 20 20 20 20 20 20 7b 0a  STRING).      {.
190b0 09 20 20 73 77 69 74 63 68 20 28 64 69 6d 73 29  .  switch (dims)
190c0 0a 09 20 20 20 20 7b 0a 09 20 20 20 20 63 61 73  ..    {..    cas
190d0 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09 09 78  e GAIA_XY_Z:...x
190e0 74 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54  type = GAIA_MULT
190f0 49 4c 49 4e 45 53 54 52 49 4e 47 5a 3b 0a 09 09  ILINESTRINGZ;...
19100 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
19110 20 47 41 49 41 5f 58 59 5f 4d 3a 0a 09 09 78 74   GAIA_XY_M:...xt
19120 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49  ype = GAIA_MULTI
19130 4c 49 4e 45 53 54 52 49 4e 47 4d 3b 0a 09 09 62  LINESTRINGM;...b
19140 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
19150 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09 09 78  GAIA_XY_Z_M:...x
19160 74 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54  type = GAIA_MULT
19170 49 4c 49 4e 45 53 54 52 49 4e 47 5a 4d 3b 0a 09  ILINESTRINGZM;..
19180 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 64 65 66  .break;..    def
19190 61 75 6c 74 3a 0a 09 09 78 74 79 70 65 20 3d 20  ault:...xtype = 
191a0 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54  GAIA_MULTILINEST
191b0 52 49 4e 47 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  RING;...break;..
191c0 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20      };.      }. 
191d0 20 20 20 69 66 20 28 78 78 74 79 70 65 20 3d 3d     if (xxtype ==
191e0 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47   GAIA_MULTIPOLYG
191f0 4f 4e 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  ON).      {..  s
19200 77 69 74 63 68 20 28 64 69 6d 73 29 0a 09 20 20  witch (dims)..  
19210 20 20 7b 0a 09 20 20 20 20 63 61 73 65 20 47 41    {..    case GA
19220 49 41 5f 58 59 5f 5a 3a 0a 09 09 78 74 79 70 65  IA_XY_Z:...xtype
19230 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c   = GAIA_MULTIPOL
19240 59 47 4f 4e 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a  YGONZ;...break;.
19250 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 58  .    case GAIA_X
19260 59 5f 4d 3a 0a 09 09 78 74 79 70 65 20 3d 20 47  Y_M:...xtype = G
19270 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e  AIA_MULTIPOLYGON
19280 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  M;...break;..   
19290 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a 5f   case GAIA_XY_Z_
192a0 4d 3a 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49  M:...xtype = GAI
192b0 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 5a 4d  A_MULTIPOLYGONZM
192c0 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
192d0 64 65 66 61 75 6c 74 3a 0a 09 09 78 74 79 70 65  default:...xtype
192e0 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c   = GAIA_MULTIPOL
192f0 59 47 4f 4e 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  YGON;...break;..
19300 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20      };.      }. 
19310 20 20 20 69 66 20 28 78 78 74 79 70 65 20 3d 3d     if (xxtype ==
19320 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f   GAIA_GEOMETRYCO
19330 4c 4c 45 43 54 49 4f 4e 29 0a 20 20 20 20 20 20  LLECTION).      
19340 7b 0a 09 20 20 73 77 69 74 63 68 20 28 64 69 6d  {..  switch (dim
19350 73 29 0a 09 20 20 20 20 7b 0a 09 20 20 20 20 63  s)..    {..    c
19360 61 73 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09  ase GAIA_XY_Z:..
19370 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f 47 45  .xtype = GAIA_GE
19380 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e  OMETRYCOLLECTION
19390 5a 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  Z;...break;..   
193a0 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 4d 3a   case GAIA_XY_M:
193b0 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f  ...xtype = GAIA_
193c0 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
193d0 4f 4e 4d 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20  ONM;...break;.. 
193e0 20 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f     case GAIA_XY_
193f0 5a 5f 4d 3a 0a 09 09 78 74 79 70 65 20 3d 20 47  Z_M:...xtype = G
19400 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c  AIA_GEOMETRYCOLL
19410 45 43 54 49 4f 4e 5a 4d 3b 0a 09 09 62 72 65 61  ECTIONZM;...brea
19420 6b 3b 0a 09 20 20 20 20 64 65 66 61 75 6c 74 3a  k;..    default:
19430 0a 09 09 78 74 79 70 65 20 3d 20 47 41 49 41 5f  ...xtype = GAIA_
19440 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
19450 4f 4e 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  ON;...break;..  
19460 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20    };.      }.   
19470 20 69 66 20 28 78 78 74 79 70 65 20 3d 3d 20 2d   if (xxtype == -
19480 31 29 0a 09 78 74 79 70 65 20 3d 20 2d 31 3b 09  1)..xtype = -1;.
19490 09 2f 2a 20 47 45 4f 4d 45 54 52 59 20 2a 2f 0a  ./* GEOMETRY */.
194a0 20 20 20 20 69 66 20 28 21 72 65 63 6f 76 65 72      if (!recover
194b0 47 65 6f 6d 43 6f 6c 75 6d 6e 20 28 73 71 6c 69  GeomColumn (sqli
194c0 74 65 2c 20 78 74 61 62 6c 65 2c 20 63 6f 6c 75  te, xtable, colu
194d0 6d 6e 2c 20 78 74 79 70 65 2c 20 64 69 6d 73 2c  mn, xtype, dims,
194e0 20 73 72 69 64 2c 20 78 63 6f 6c 75 6d 6e 29 29   srid, xcolumn))
194f0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
19500 69 61 6c 69 74 65 5f 65 20 28 22 52 65 63 6f 76  ialite_e ("Recov
19510 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e  erGeometryColumn
19520 28 29 3a 20 76 61 6c 69 64 61 74 69 6f 6e 20 66  (): validation f
19530 61 69 6c 65 64 5c 6e 22 29 3b 0a 09 20 20 73 71  ailed\n");..  sq
19540 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
19550 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
19560 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
19570 7d 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  }.    strcpy (sq
19580 6c 74 61 62 6c 65 2c 20 78 74 61 62 6c 65 29 3b  ltable, xtable);
19590 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73  .    clean_sql_s
195a0 74 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29  tring (sqltable)
195b0 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
195c0 6c 63 6f 6c 75 6d 6e 2c 20 78 63 6f 6c 75 6d 6e  lcolumn, xcolumn
195d0 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c  );.    clean_sql
195e0 5f 73 74 72 69 6e 67 20 28 73 71 6c 63 6f 6c 75  _string (sqlcolu
195f0 6d 6e 29 3b 0a 2f 2a 20 64 65 6c 65 74 69 6e 67  mn);./* deleting
19600 20 61 6e 79 77 61 79 20 61 6e 79 20 70 72 65 76   anyway any prev
19610 69 6f 75 73 20 64 65 66 69 6e 69 74 69 6f 6e 20  ious definition 
19620 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20 28 73  */.    strcpy (s
19630 71 6c 2c 20 22 44 45 4c 45 54 45 20 46 52 4f 4d  ql, "DELETE FROM
19640 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e   geometry_column
19650 73 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  s ");.    strcat
19660 20 28 73 71 6c 2c 20 22 57 48 45 52 45 20 55 70   (sql, "WHERE Up
19670 70 65 72 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  per(f_table_name
19680 29 20 3d 20 55 70 70 65 72 28 27 22 29 3b 0a 20  ) = Upper('");. 
19690 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
196a0 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
196b0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 29 20  trcat (sql, "') 
196c0 41 4e 44 20 55 70 70 65 72 28 66 5f 67 65 6f 6d  AND Upper(f_geom
196d0 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 3d 20 55  etry_column) = U
196e0 70 70 65 72 28 27 22 29 3b 0a 20 20 20 20 73 74  pper('");.    st
196f0 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 63 6f  rcat (sql, sqlco
19700 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 74 72 63 61  lumn);.    strca
19710 74 20 28 73 71 6c 2c 20 22 27 29 22 29 3b 0a 20  t (sql, "')");. 
19720 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33     ret = sqlite3
19730 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73  _exec (sqlite, s
19740 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  ql, NULL, NULL, 
19750 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  &errMsg);.    if
19760 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
19770 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b  OK)..goto error;
19780 0a 0a 20 20 20 20 69 66 20 28 6d 65 74 61 64 61  ..    if (metada
19790 74 61 5f 76 65 72 73 69 6f 6e 20 3d 3d 20 31 29  ta_version == 1)
197a0 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 6c  .      {..  /* l
197b0 65 67 61 63 79 20 6d 65 74 61 64 61 74 61 20 73  egacy metadata s
197c0 74 79 6c 65 20 3c 3d 20 76 2e 33 2e 31 2e 30 20  tyle <= v.3.1.0 
197d0 2a 2f 0a 09 20 20 73 74 72 63 70 79 20 28 73 71  */..  strcpy (sq
197e0 6c 2c 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  l, "INSERT INTO 
197f0 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73  geometry_columns
19800 20 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20   (f_table_name, 
19810 22 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73  ");..  strcat (s
19820 71 6c 2c 20 22 66 5f 67 65 6f 6d 65 74 72 79 5f  ql, "f_geometry_
19830 63 6f 6c 75 6d 6e 2c 20 74 79 70 65 2c 20 63 6f  column, type, co
19840 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 2c 20 73  ord_dimension, s
19850 72 69 64 2c 20 22 29 3b 0a 09 20 20 73 74 72 63  rid, ");..  strc
19860 61 74 20 28 73 71 6c 2c 20 22 73 70 61 74 69 61  at (sql, "spatia
19870 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 29  l_index_enabled)
19880 20 56 41 4c 55 45 53 20 28 22 29 3b 0a 09 20 20   VALUES (");..  
19890 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 22  strcat (sql, "'"
198a0 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
198b0 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09 20  l, sqltable);.. 
198c0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27   strcat (sql, "'
198d0 2c 20 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74  , '");..  strcat
198e0 20 28 73 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e   (sql, sqlcolumn
198f0 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
19900 6c 2c 20 22 27 2c 20 27 22 29 3b 0a 09 20 20 73  l, "', '");..  s
19910 77 69 74 63 68 20 28 78 74 79 70 65 29 0a 09 20  witch (xtype).. 
19920 20 20 20 7b 0a 09 20 20 20 20 63 61 73 65 20 47     {..    case G
19930 41 49 41 5f 50 4f 49 4e 54 3a 0a 09 20 20 20 20  AIA_POINT:..    
19940 63 61 73 65 20 47 41 49 41 5f 50 4f 49 4e 54 5a  case GAIA_POINTZ
19950 3a 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  :..    case GAIA
19960 5f 50 4f 49 4e 54 4d 3a 0a 09 20 20 20 20 63 61  _POINTM:..    ca
19970 73 65 20 47 41 49 41 5f 50 4f 49 4e 54 5a 4d 3a  se GAIA_POINTZM:
19980 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  ...strcat (sql, 
19990 22 50 4f 49 4e 54 22 29 3b 0a 09 09 62 72 65 61  "POINT");...brea
199a0 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49  k;..    case GAI
199b0 41 5f 4c 49 4e 45 53 54 52 49 4e 47 3a 0a 09 20  A_LINESTRING:.. 
199c0 20 20 20 63 61 73 65 20 47 41 49 41 5f 4c 49 4e     case GAIA_LIN
199d0 45 53 54 52 49 4e 47 5a 3a 0a 09 20 20 20 20 63  ESTRINGZ:..    c
199e0 61 73 65 20 47 41 49 41 5f 4c 49 4e 45 53 54 52  ase GAIA_LINESTR
199f0 49 4e 47 4d 3a 0a 09 20 20 20 20 63 61 73 65 20  INGM:..    case 
19a00 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 5a  GAIA_LINESTRINGZ
19a10 4d 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c  M:...strcat (sql
19a20 2c 20 22 4c 49 4e 45 53 54 52 49 4e 47 22 29 3b  , "LINESTRING");
19a30 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
19a40 61 73 65 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e  ase GAIA_POLYGON
19a50 3a 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  :..    case GAIA
19a60 5f 50 4f 4c 59 47 4f 4e 5a 3a 0a 09 20 20 20 20  _POLYGONZ:..    
19a70 63 61 73 65 20 47 41 49 41 5f 50 4f 4c 59 47 4f  case GAIA_POLYGO
19a80 4e 4d 3a 0a 09 20 20 20 20 63 61 73 65 20 47 41  NM:..    case GA
19a90 49 41 5f 50 4f 4c 59 47 4f 4e 5a 4d 3a 0a 09 09  IA_POLYGONZM:...
19aa0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 50 4f  strcat (sql, "PO
19ab0 4c 59 47 4f 4e 22 29 3b 0a 09 09 62 72 65 61 6b  LYGON");...break
19ac0 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  ;..    case GAIA
19ad0 5f 4d 55 4c 54 49 50 4f 49 4e 54 3a 0a 09 20 20  _MULTIPOINT:..  
19ae0 20 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54    case GAIA_MULT
19af0 49 50 4f 49 4e 54 5a 3a 0a 09 20 20 20 20 63 61  IPOINTZ:..    ca
19b00 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49  se GAIA_MULTIPOI
19b10 4e 54 4d 3a 0a 09 20 20 20 20 63 61 73 65 20 47  NTM:..    case G
19b20 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 5a 4d  AIA_MULTIPOINTZM
19b30 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c  :...strcat (sql,
19b40 20 22 4d 55 4c 54 49 50 4f 49 4e 54 22 29 3b 0a   "MULTIPOINT");.
19b50 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
19b60 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e  se GAIA_MULTILIN
19b70 45 53 54 52 49 4e 47 3a 0a 09 20 20 20 20 63 61  ESTRING:..    ca
19b80 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e  se GAIA_MULTILIN
19b90 45 53 54 52 49 4e 47 5a 3a 0a 09 20 20 20 20 63  ESTRINGZ:..    c
19ba0 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49  ase GAIA_MULTILI
19bb0 4e 45 53 54 52 49 4e 47 4d 3a 0a 09 20 20 20 20  NESTRINGM:..    
19bc0 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c  case GAIA_MULTIL
19bd0 49 4e 45 53 54 52 49 4e 47 5a 4d 3a 0a 09 09 73  INESTRINGZM:...s
19be0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4d 55 4c  trcat (sql, "MUL
19bf0 54 49 4c 49 4e 45 53 54 52 49 4e 47 22 29 3b 0a  TILINESTRING");.
19c00 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
19c10 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c  se GAIA_MULTIPOL
19c20 59 47 4f 4e 3a 0a 09 20 20 20 20 63 61 73 65 20  YGON:..    case 
19c30 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f  GAIA_MULTIPOLYGO
19c40 4e 5a 3a 0a 09 20 20 20 20 63 61 73 65 20 47 41  NZ:..    case GA
19c50 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 4d  IA_MULTIPOLYGONM
19c60 3a 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  :..    case GAIA
19c70 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 5a 4d 3a  _MULTIPOLYGONZM:
19c80 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  ...strcat (sql, 
19c90 22 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 22 29 3b  "MULTIPOLYGON");
19ca0 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
19cb0 61 73 65 20 47 41 49 41 5f 47 45 4f 4d 45 54 52  ase GAIA_GEOMETR
19cc0 59 43 4f 4c 4c 45 43 54 49 4f 4e 3a 0a 09 20 20  YCOLLECTION:..  
19cd0 20 20 63 61 73 65 20 47 41 49 41 5f 47 45 4f 4d    case GAIA_GEOM
19ce0 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 5a 3a  ETRYCOLLECTIONZ:
19cf0 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
19d00 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
19d10 4f 4e 4d 3a 0a 09 20 20 20 20 63 61 73 65 20 47  ONM:..    case G
19d20 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c  AIA_GEOMETRYCOLL
19d30 45 43 54 49 4f 4e 5a 4d 3a 0a 09 09 73 74 72 63  ECTIONZM:...strc
19d40 61 74 20 28 73 71 6c 2c 20 22 47 45 4f 4d 45 54  at (sql, "GEOMET
19d50 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 22 29 3b 0a  RYCOLLECTION");.
19d60 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
19d70 73 65 20 2d 31 3a 0a 09 09 73 74 72 63 61 74 20  se -1:...strcat 
19d80 28 73 71 6c 2c 20 22 47 45 4f 4d 45 54 52 59 22  (sql, "GEOMETRY"
19d90 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  );...break;..   
19da0 20 7d 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73   };..  strcat (s
19db0 71 6c 2c 20 22 27 2c 20 27 22 29 3b 0a 09 20 20  ql, "', '");..  
19dc0 73 77 69 74 63 68 20 28 64 69 6d 73 29 0a 09 20  switch (dims).. 
19dd0 20 20 20 7b 0a 09 20 20 20 20 63 61 73 65 20 47     {..    case G
19de0 41 49 41 5f 58 59 3a 0a 09 09 73 74 72 63 61 74  AIA_XY:...strcat
19df0 20 28 73 71 6c 2c 20 22 58 59 22 29 3b 0a 09 09   (sql, "XY");...
19e00 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
19e10 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09 09 73 74   GAIA_XY_Z:...st
19e20 72 63 61 74 20 28 73 71 6c 2c 20 22 58 59 5a 22  rcat (sql, "XYZ"
19e30 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  );...break;..   
19e40 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 4d 3a   case GAIA_XY_M:
19e50 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  ...strcat (sql, 
19e60 22 58 59 4d 22 29 3b 0a 09 09 62 72 65 61 6b 3b  "XYM");...break;
19e70 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
19e80 58 59 5f 5a 5f 4d 3a 0a 09 09 73 74 72 63 61 74  XY_Z_M:...strcat
19e90 20 28 73 71 6c 2c 20 22 58 59 5a 4d 22 29 3b 0a   (sql, "XYZM");.
19ea0 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 7d 3b  ..break;..    };
19eb0 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c  ..  strcat (sql,
19ec0 20 22 27 2c 20 22 29 3b 0a 20 20 20 20 20 20 7d   "', ");.      }
19ed0 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
19ee0 7b 0a 09 20 20 2f 2a 20 63 75 72 72 65 6e 74 20  {..  /* current 
19ef0 6d 65 74 61 64 61 74 61 20 73 74 79 6c 65 20 3e  metadata style >
19f00 3d 20 76 2e 34 2e 30 2e 30 20 2a 2f 0a 09 20 20  = v.4.0.0 */..  
19f10 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 49 4e  strcpy (sql, "IN
19f20 53 45 52 54 20 49 4e 54 4f 20 67 65 6f 6d 65 74  SERT INTO geomet
19f30 72 79 5f 63 6f 6c 75 6d 6e 73 20 28 66 5f 74 61  ry_columns (f_ta
19f40 62 6c 65 5f 6e 61 6d 65 2c 20 22 29 3b 0a 09 20  ble_name, ");.. 
19f50 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66   strcat (sql, "f
19f60 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
19f70 2c 20 67 65 6f 6d 65 74 72 79 5f 74 79 70 65 2c  , geometry_type,
19f80 20 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e   coord_dimension
19f90 2c 20 22 29 3b 0a 09 20 20 73 74 72 63 61 74 20  , ");..  strcat 
19fa0 28 73 71 6c 2c 20 22 73 72 69 64 2c 20 73 70 61  (sql, "srid, spa
19fb0 74 69 61 6c 5f 69 6e 64 65 78 5f 65 6e 61 62 6c  tial_index_enabl
19fc0 65 64 29 20 56 41 4c 55 45 53 20 28 22 29 3b 0a  ed) VALUES (");.
19fd0 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20  .  strcat (sql, 
19fe0 22 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74 20  "'");..  strcat 
19ff0 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b  (sql, sqltable);
1a000 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c  ..  strcat (sql,
1a010 20 22 27 2c 20 27 22 29 3b 0a 09 20 20 73 74 72   "', '");..  str
1a020 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 63 6f 6c  cat (sql, sqlcol
1a030 75 6d 6e 29 3b 0a 09 20 20 73 74 72 63 61 74 20  umn);..  strcat 
1a040 28 73 71 6c 2c 20 22 27 2c 20 22 29 3b 0a 09 20  (sql, "', ");.. 
1a050 20 73 77 69 74 63 68 20 28 78 74 79 70 65 29 0a   switch (xtype).
1a060 09 20 20 20 20 7b 0a 09 20 20 20 20 63 61 73 65  .    {..    case
1a070 20 47 41 49 41 5f 50 4f 49 4e 54 3a 0a 09 09 73   GAIA_POINT:...s
1a080 74 72 63 61 74 20 28 73 71 6c 2c 20 22 31 2c 20  trcat (sql, "1, 
1a090 32 2c 20 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a  2, ");...break;.
1a0a0 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 50  .    case GAIA_P
1a0b0 4f 49 4e 54 5a 3a 0a 09 09 73 74 72 63 61 74 20  OINTZ:...strcat 
1a0c0 28 73 71 6c 2c 20 22 31 30 30 31 2c 20 33 2c 20  (sql, "1001, 3, 
1a0d0 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  ");...break;..  
1a0e0 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f 49 4e    case GAIA_POIN
1a0f0 54 4d 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71  TM:...strcat (sq
1a100 6c 2c 20 22 32 30 30 31 2c 20 33 2c 20 22 29 3b  l, "2001, 3, ");
1a110 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
1a120 61 73 65 20 47 41 49 41 5f 50 4f 49 4e 54 5a 4d  ase GAIA_POINTZM
1a130 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c  :...strcat (sql,
1a140 20 22 33 30 30 31 2c 20 34 2c 20 22 29 3b 0a 09   "3001, 4, ");..
1a150 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
1a160 65 20 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e  e GAIA_LINESTRIN
1a170 47 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c  G:...strcat (sql
1a180 2c 20 22 32 2c 20 32 2c 20 22 29 3b 0a 09 09 62  , "2, 2, ");...b
1a190 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
1a1a0 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 5a  GAIA_LINESTRINGZ
1a1b0 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c  :...strcat (sql,
1a1c0 20 22 32 30 30 32 2c 20 33 2c 20 22 29 3b 0a 09   "2002, 3, ");..
1a1d0 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
1a1e0 65 20 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e  e GAIA_LINESTRIN
1a1f0 47 4d 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71  GM:...strcat (sq
1a200 6c 2c 20 22 32 30 30 32 2c 20 33 2c 20 22 29 3b  l, "2002, 3, ");
1a210 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
1a220 61 73 65 20 47 41 49 41 5f 4c 49 4e 45 53 54 52  ase GAIA_LINESTR
1a230 49 4e 47 5a 4d 3a 0a 09 09 73 74 72 63 61 74 20  INGZM:...strcat 
1a240 28 73 71 6c 2c 20 22 33 30 30 32 2c 20 34 2c 20  (sql, "3002, 4, 
1a250 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  ");...break;..  
1a260 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f 4c 59    case GAIA_POLY
1a270 47 4f 4e 3a 0a 09 09 73 74 72 63 61 74 20 28 73  GON:...strcat (s
1a280 71 6c 2c 20 22 33 2c 20 32 2c 20 22 29 3b 0a 09  ql, "3, 2, ");..
1a290 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
1a2a0 65 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 5a 3a  e GAIA_POLYGONZ:
1a2b0 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  ...strcat (sql, 
1a2c0 22 31 30 30 33 2c 20 33 2c 20 22 29 3b 0a 09 09  "1003, 3, ");...
1a2d0 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
1a2e0 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 4d 3a 0a   GAIA_POLYGONM:.
1a2f0 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22  ..strcat (sql, "
1a300 32 30 30 33 2c 20 33 2c 20 22 29 3b 0a 09 09 62  2003, 3, ");...b
1a310 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
1a320 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 5a 4d 3a 0a  GAIA_POLYGONZM:.
1a330 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22  ..strcat (sql, "
1a340 33 30 30 33 2c 20 34 2c 20 22 29 3b 0a 09 09 62  3003, 4, ");...b
1a350 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
1a360 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 3a  GAIA_MULTIPOINT:
1a370 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  ...strcat (sql, 
1a380 22 34 2c 20 32 2c 20 22 29 3b 0a 09 09 62 72 65  "4, 2, ");...bre
1a390 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
1a3a0 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 5a 3a 0a  IA_MULTIPOINTZ:.
1a3b0 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22  ..strcat (sql, "
1a3c0 31 30 30 34 2c 20 33 2c 20 22 29 3b 0a 09 09 62  1004, 3, ");...b
1a3d0 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
1a3e0 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 4d  GAIA_MULTIPOINTM
1a3f0 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c  :...strcat (sql,
1a400 20 22 32 30 30 34 2c 20 33 2c 20 22 29 3b 0a 09   "2004, 3, ");..
1a410 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
1a420 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e  e GAIA_MULTIPOIN
1a430 54 5a 4d 3a 0a 09 09 73 74 72 63 61 74 20 28 73  TZM:...strcat (s
1a440 71 6c 2c 20 22 33 30 30 34 2c 20 34 2c 20 22 29  ql, "3004, 4, ")
1a450 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
1a460 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c  case GAIA_MULTIL
1a470 49 4e 45 53 54 52 49 4e 47 3a 0a 09 09 73 74 72  INESTRING:...str
1a480 63 61 74 20 28 73 71 6c 2c 20 22 35 2c 20 32 2c  cat (sql, "5, 2,
1a490 20 22 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20   ");...break;.. 
1a4a0 20 20 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c     case GAIA_MUL
1a4b0 54 49 4c 49 4e 45 53 54 52 49 4e 47 5a 3a 0a 09  TILINESTRINGZ:..
1a4c0 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 31  .strcat (sql, "1
1a4d0 30 30 35 2c 20 33 2c 20 22 29 3b 0a 09 09 62 72  005, 3, ");...br
1a4e0 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47  eak;..    case G
1a4f0 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52  AIA_MULTILINESTR
1a500 49 4e 47 4d 3a 0a 09 09 73 74 72 63 61 74 20 28  INGM:...strcat (
1a510 73 71 6c 2c 20 22 32 30 30 35 2c 20 33 2c 20 22  sql, "2005, 3, "
1a520 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  );...break;..   
1a530 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49   case GAIA_MULTI
1a540 4c 49 4e 45 53 54 52 49 4e 47 5a 4d 3a 0a 09 09  LINESTRINGZM:...
1a550 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 33 30  strcat (sql, "30
1a560 30 35 2c 20 34 2c 20 22 29 3b 0a 09 09 62 72 65  05, 4, ");...bre
1a570 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
1a580 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 3a  IA_MULTIPOLYGON:
1a590 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  ...strcat (sql, 
1a5a0 22 36 2c 20 32 2c 20 22 29 3b 0a 09 09 62 72 65  "6, 2, ");...bre
1a5b0 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
1a5c0 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 5a  IA_MULTIPOLYGONZ
1a5d0 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c  :...strcat (sql,
1a5e0 20 22 31 30 30 36 2c 20 33 2c 20 22 29 3b 0a 09   "1006, 3, ");..
1a5f0 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
1a600 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59  e GAIA_MULTIPOLY
1a610 47 4f 4e 4d 3a 0a 09 09 73 74 72 63 61 74 20 28  GONM:...strcat (
1a620 73 71 6c 2c 20 22 32 30 30 36 2c 20 33 2c 20 22  sql, "2006, 3, "
1a630 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  );...break;..   
1a640 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49   case GAIA_MULTI
1a650 50 4f 4c 59 47 4f 4e 5a 4d 3a 0a 09 09 73 74 72  POLYGONZM:...str
1a660 63 61 74 20 28 73 71 6c 2c 20 22 33 30 30 36 2c  cat (sql, "3006,
1a670 20 34 2c 20 22 29 3b 0a 09 09 62 72 65 61 6b 3b   4, ");...break;
1a680 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
1a690 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
1a6a0 4f 4e 3a 0a 09 09 73 74 72 63 61 74 20 28 73 71  ON:...strcat (sq
1a6b0 6c 2c 20 22 37 2c 20 32 2c 20 22 29 3b 0a 09 09  l, "7, 2, ");...
1a6c0 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
1a6d0 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f   GAIA_GEOMETRYCO
1a6e0 4c 4c 45 43 54 49 4f 4e 5a 3a 0a 09 09 73 74 72  LLECTIONZ:...str
1a6f0 63 61 74 20 28 73 71 6c 2c 20 22 31 30 30 37 2c  cat (sql, "1007,
1a700 20 33 2c 20 22 29 3b 0a 09 09 62 72 65 61 6b 3b   3, ");...break;
1a710 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
1a720 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
1a730 4f 4e 4d 3a 0a 09 09 73 74 72 63 61 74 20 28 73  ONM:...strcat (s
1a740 71 6c 2c 20 22 32 30 30 37 2c 20 33 2c 20 22 29  ql, "2007, 3, ")
1a750 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
1a760 63 61 73 65 20 47 41 49 41 5f 47 45 4f 4d 45 54  case GAIA_GEOMET
1a770 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 5a 4d 3a 0a  RYCOLLECTIONZM:.
1a780 09 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22  ..strcat (sql, "
1a790 33 30 30 37 2c 20 34 2c 20 22 29 3b 0a 09 09 62  3007, 4, ");...b
1a7a0 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
1a7b0 2d 31 3a 0a 09 09 73 77 69 74 63 68 20 28 64 69  -1:...switch (di
1a7c0 6d 73 29 0a 09 09 20 20 7b 0a 09 09 20 20 63 61  ms)...  {...  ca
1a7d0 73 65 20 47 41 49 41 5f 58 59 3a 0a 09 09 20 20  se GAIA_XY:...  
1a7e0 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
1a7f0 20 22 30 2c 20 32 2c 20 22 29 3b 0a 09 09 20 20   "0, 2, ");...  
1a800 20 20 20 20 62 72 65 61 6b 3b 0a 09 09 20 20 63      break;...  c
1a810 61 73 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09  ase GAIA_XY_Z:..
1a820 09 20 20 20 20 20 20 73 74 72 63 61 74 20 28 73  .      strcat (s
1a830 71 6c 2c 20 22 31 30 30 30 2c 20 32 2c 20 22 29  ql, "1000, 2, ")
1a840 3b 0a 09 09 20 20 20 20 20 20 62 72 65 61 6b 3b  ;...      break;
1a850 0a 09 09 20 20 63 61 73 65 20 47 41 49 41 5f 58  ...  case GAIA_X
1a860 59 5f 4d 3a 0a 09 09 20 20 20 20 20 20 73 74 72  Y_M:...      str
1a870 63 61 74 20 28 73 71 6c 2c 20 22 32 30 30 30 2c  cat (sql, "2000,
1a880 20 32 2c 20 22 29 3b 0a 09 09 20 20 20 20 20 20   2, ");...      
1a890 62 72 65 61 6b 3b 0a 09 09 20 20 63 61 73 65 20  break;...  case 
1a8a0 47 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09 09 20  GAIA_XY_Z_M:... 
1a8b0 20 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c       strcat (sql
1a8c0 2c 20 22 33 30 30 30 2c 20 32 2c 20 22 29 3b 0a  , "3000, 2, ");.
1a8d0 09 09 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 09  ..      break;..
1a8e0 09 20 20 7d 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  .  };...break;..
1a8f0 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 7d 0a 20      };.      }. 
1a900 20 20 20 69 66 20 28 73 72 69 64 20 3c 3d 20 30     if (srid <= 0
1a910 29 0a 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  )..strcat (sql, 
1a920 22 2d 31 22 29 3b 0a 20 20 20 20 65 6c 73 65 0a  "-1");.    else.
1a930 20 20 20 20 20 20 7b 0a 09 20 20 73 70 72 69 6e        {..  sprin
1a940 74 66 20 28 64 75 6d 6d 79 2c 20 22 25 64 22 2c  tf (dummy, "%d",
1a950 20 73 72 69 64 29 3b 0a 09 20 20 73 74 72 63 61   srid);..  strca
1a960 74 20 28 73 71 6c 2c 20 64 75 6d 6d 79 29 3b 0a  t (sql, dummy);.
1a970 20 20 20 20 20 20 7d 0a 20 20 20 20 73 74 72 63        }.    strc
1a980 61 74 20 28 73 71 6c 2c 20 22 2c 20 30 29 22 29  at (sql, ", 0)")
1a990 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
1a9a0 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
1a9b0 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c  , sql, NULL, NUL
1a9c0 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20  L, &errMsg);.   
1a9d0 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
1a9e0 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72  TE_OK)..goto err
1a9f0 6f 72 3b 0a 20 20 20 20 69 66 20 28 6d 65 74 61  or;.    if (meta
1aa00 64 61 74 61 5f 76 65 72 73 69 6f 6e 20 3d 3d 20  data_version == 
1aa10 33 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a  3).      {..  /*
1aa20 20 63 75 72 72 65 6e 74 20 6d 65 74 61 64 61 74   current metadat
1aa30 61 20 73 74 79 6c 65 20 3e 3d 20 76 2e 34 2e 30  a style >= v.4.0
1aa40 2e 30 20 2a 2f 0a 0a 09 20 20 2f 2a 20 69 6e 73  .0 */...  /* ins
1aa50 65 72 74 69 6e 67 20 61 20 72 6f 77 20 69 6e 74  erting a row int
1aa60 6f 20 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d  o GEOMETRY_COLUM
1aa70 4e 53 5f 41 55 54 48 20 2a 2f 0a 09 20 20 73 74  NS_AUTH */..  st
1aa80 72 63 70 79 20 28 73 71 6c 2c 0a 09 09 20 20 22  rcpy (sql,...  "
1aa90 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
1aaa0 45 20 49 4e 54 4f 20 67 65 6f 6d 65 74 72 79 5f  E INTO geometry_
1aab0 63 6f 6c 75 6d 6e 73 5f 61 75 74 68 20 28 66 5f  columns_auth (f_
1aac0 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65  table_name, f_ge
1aad0 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 2c 20 22  ometry_column, "
1aae0 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
1aaf0 6c 2c 20 22 72 65 61 64 5f 6f 6e 6c 79 2c 20 68  l, "read_only, h
1ab00 69 64 64 65 6e 29 20 56 41 4c 55 45 53 20 28 22  idden) VALUES ("
1ab10 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
1ab20 6c 2c 20 22 27 22 29 3b 0a 09 20 20 73 74 72 63  l, "'");..  strc
1ab30 61 74 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c  at (sql, sqltabl
1ab40 65 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73  e);..  strcat (s
1ab50 71 6c 2c 20 22 27 2c 20 27 22 29 3b 0a 09 20 20  ql, "', '");..  
1ab60 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c  strcat (sql, sql
1ab70 63 6f 6c 75 6d 6e 29 3b 0a 09 20 20 73 74 72 63  column);..  strc
1ab80 61 74 20 28 73 71 6c 2c 20 22 27 2c 20 30 2c 20  at (sql, "', 0, 
1ab90 30 29 22 29 3b 0a 09 20 20 72 65 74 20 3d 20 73  0)");..  ret = s
1aba0 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
1abb0 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
1abc0 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a  NULL, &errMsg);.
1abd0 09 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51  .  if (ret != SQ
1abe0 4c 49 54 45 5f 4f 4b 29 0a 09 20 20 20 20 20 20  LITE_OK)..      
1abf0 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 09 20 20 2f  goto error;..  /
1ac00 2a 20 69 6e 73 65 72 74 69 6e 67 20 61 20 72 6f  * inserting a ro
1ac10 77 20 69 6e 74 6f 20 47 45 4f 4d 45 54 52 59 5f  w into GEOMETRY_
1ac20 43 4f 4c 55 4d 4e 53 5f 53 54 41 54 49 53 54 49  COLUMNS_STATISTI
1ac30 43 53 20 2a 2f 0a 09 20 20 73 74 72 63 70 79 20  CS */..  strcpy 
1ac40 28 73 71 6c 2c 0a 09 09 20 20 22 49 4e 53 45 52  (sql,...  "INSER
1ac50 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54  T OR REPLACE INT
1ac60 4f 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  O geometry_colum
1ac70 6e 73 5f 73 74 61 74 69 73 74 69 63 73 20 28 66  ns_statistics (f
1ac80 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67  _table_name, f_g
1ac90 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20  eometry_column) 
1aca0 22 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73  ");..  strcat (s
1acb0 71 6c 2c 20 22 56 41 4c 55 45 53 20 28 22 29 3b  ql, "VALUES (");
1acc0 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c  ..  strcat (sql,
1acd0 20 22 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74   "'");..  strcat
1ace0 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c 65 29   (sql, sqltable)
1acf0 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c  ;..  strcat (sql
1ad00 2c 20 22 27 2c 20 27 22 29 3b 0a 09 20 20 73 74  , "', '");..  st
1ad10 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 63 6f  rcat (sql, sqlco
1ad20 6c 75 6d 6e 29 3b 0a 09 20 20 73 74 72 63 61 74  lumn);..  strcat
1ad30 20 28 73 71 6c 2c 20 22 27 29 22 29 3b 0a 09 20   (sql, "')");.. 
1ad40 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65   ret = sqlite3_e
1ad50 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c  xec (sqlite, sql
1ad60 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
1ad70 72 72 4d 73 67 29 3b 0a 09 20 20 69 66 20 28 72  rrMsg);..  if (r
1ad80 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29  et != SQLITE_OK)
1ad90 0a 09 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72  ..      goto err
1ada0 6f 72 3b 0a 09 20 20 2f 2a 20 69 6e 73 65 72 74  or;..  /* insert
1adb0 69 6e 67 20 61 20 72 6f 77 20 69 6e 74 6f 20 47  ing a row into G
1adc0 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e 53 5f  EOMETRY_COLUMNS_
1add0 54 49 4d 45 20 2a 2f 0a 09 20 20 73 74 72 63 70  TIME */..  strcp
1ade0 79 20 28 73 71 6c 2c 0a 09 09 20 20 22 49 4e 53  y (sql,...  "INS
1adf0 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49  ERT OR REPLACE I
1ae00 4e 54 4f 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  NTO geometry_col
1ae10 75 6d 6e 73 5f 74 69 6d 65 20 28 66 5f 74 61 62  umns_time (f_tab
1ae20 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d 65  le_name, f_geome
1ae30 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 22 29 3b 0a  try_column) ");.
1ae40 09 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20  .  strcat (sql, 
1ae50 22 56 41 4c 55 45 53 20 28 22 29 3b 0a 09 20 20  "VALUES (");..  
1ae60 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 22  strcat (sql, "'"
1ae70 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
1ae80 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 09 20  l, sqltable);.. 
1ae90 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27   strcat (sql, "'
1aea0 2c 20 27 22 29 3b 0a 09 20 20 73 74 72 63 61 74  , '");..  strcat
1aeb0 20 28 73 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e   (sql, sqlcolumn
1aec0 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  );..  strcat (sq
1aed0 6c 2c 20 22 27 29 22 29 3b 0a 09 20 20 72 65 74  l, "')");..  ret
1aee0 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
1aef0 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
1af00 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
1af10 67 29 3b 0a 09 20 20 69 66 20 28 72 65 74 20 21  g);..  if (ret !
1af20 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20  = SQLITE_OK)..  
1af30 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a      goto error;.
1af40 20 20 20 20 20 20 7d 0a 20 20 20 20 75 70 64 61        }.    upda
1af50 74 65 47 65 6f 6d 65 74 72 79 54 72 69 67 67 65  teGeometryTrigge
1af60 72 73 20 28 73 71 6c 69 74 65 2c 20 78 74 61 62  rs (sqlite, xtab
1af70 6c 65 2c 20 78 63 6f 6c 75 6d 6e 29 3b 0a 20 20  le, xcolumn);.  
1af80 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1af90 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31  _int (context, 1
1afa0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73  );.    strcpy (s
1afb0 71 6c 2c 20 22 47 65 6f 6d 65 74 72 79 20 5b 22  ql, "Geometry ["
1afc0 29 3b 0a 20 20 20 20 73 77 69 74 63 68 20 28 78  );.    switch (x
1afd0 74 79 70 65 29 0a 20 20 20 20 20 20 7b 0a 20 20  type).      {.  
1afe0 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f      case GAIA_PO
1aff0 49 4e 54 3a 0a 09 20 20 73 74 72 63 61 74 20 28  INT:..  strcat (
1b000 73 71 6c 2c 20 22 50 4f 49 4e 54 22 29 3b 0a 09  sql, "POINT");..
1b010 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1b020 61 73 65 20 47 41 49 41 5f 4c 49 4e 45 53 54 52  ase GAIA_LINESTR
1b030 49 4e 47 3a 0a 09 20 20 73 74 72 63 61 74 20 28  ING:..  strcat (
1b040 73 71 6c 2c 20 22 4c 49 4e 45 53 54 52 49 4e 47  sql, "LINESTRING
1b050 22 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20  ");..  break;.  
1b060 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f      case GAIA_PO
1b070 4c 59 47 4f 4e 3a 0a 09 20 20 73 74 72 63 61 74  LYGON:..  strcat
1b080 20 28 73 71 6c 2c 20 22 50 4f 4c 59 47 4f 4e 22   (sql, "POLYGON"
1b090 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20  );..  break;.   
1b0a0 20 20 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c     case GAIA_MUL
1b0b0 54 49 50 4f 49 4e 54 3a 0a 09 20 20 73 74 72 63  TIPOINT:..  strc
1b0c0 61 74 20 28 73 71 6c 2c 20 22 4d 55 4c 54 49 50  at (sql, "MULTIP
1b0d0 4f 49 4e 54 22 29 3b 0a 09 20 20 62 72 65 61 6b  OINT");..  break
1b0e0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 47 41 49  ;.      case GAI
1b0f0 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e  A_MULTILINESTRIN
1b100 47 3a 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  G:..  strcat (sq
1b110 6c 2c 20 22 4d 55 4c 54 49 4c 49 4e 45 53 54 52  l, "MULTILINESTR
1b120 49 4e 47 22 29 3b 0a 09 20 20 62 72 65 61 6b 3b  ING");..  break;
1b130 0a 20 20 20 20 20 20 63 61 73 65 20 47 41 49 41  .      case GAIA
1b140 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 3a 0a 09  _MULTIPOLYGON:..
1b150 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
1b160 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 22 29 3b 0a  MULTIPOLYGON");.
1b170 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  .  break;.      
1b180 63 61 73 65 20 47 41 49 41 5f 47 45 4f 4d 45 54  case GAIA_GEOMET
1b190 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 3a 0a 09 20  RYCOLLECTION:.. 
1b1a0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 47   strcat (sql, "G
1b1b0 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f  EOMETRYCOLLECTIO
1b1c0 4e 22 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20  N");..  break;. 
1b1d0 20 20 20 20 20 63 61 73 65 20 2d 31 3a 0a 09 20       case -1:.. 
1b1e0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 47   strcat (sql, "G
1b1f0 45 4f 4d 45 54 52 59 22 29 3b 0a 09 20 20 62 72  EOMETRY");..  br
1b200 65 61 6b 3b 0a 20 20 20 20 20 20 7d 3b 0a 20 20  eak;.      };.  
1b210 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
1b220 2c 22 29 3b 0a 20 20 20 20 73 77 69 74 63 68 20  ,");.    switch 
1b230 28 64 69 6d 73 29 0a 20 20 20 20 20 20 7b 0a 20  (dims).      {. 
1b240 20 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 58       case GAIA_X
1b250 59 3a 0a 09 20 20 73 74 72 63 61 74 20 28 73 71  Y:..  strcat (sq
1b260 6c 2c 20 22 58 59 22 29 3b 0a 09 20 20 62 72 65  l, "XY");..  bre
1b270 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 47  ak;.      case G
1b280 41 49 41 5f 58 59 5f 5a 3a 0a 09 20 20 73 74 72  AIA_XY_Z:..  str
1b290 63 61 74 20 28 73 71 6c 2c 20 22 58 59 5a 22 29  cat (sql, "XYZ")
1b2a0 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;..  break;.    
1b2b0 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 4d    case GAIA_XY_M
1b2c0 3a 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c  :..  strcat (sql
1b2d0 2c 20 22 58 59 4d 22 29 3b 0a 09 20 20 62 72 65  , "XYM");..  bre
1b2e0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 47  ak;.      case G
1b2f0 41 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09 20 20 73  AIA_XY_Z_M:..  s
1b300 74 72 63 61 74 20 28 73 71 6c 2c 20 22 58 59 5a  trcat (sql, "XYZ
1b310 4d 22 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20  M");..  break;. 
1b320 20 20 20 20 20 7d 3b 0a 20 20 20 20 73 70 72 69       };.    spri
1b330 6e 74 66 20 28 73 71 6c 63 6f 6c 75 6d 6e 2c 20  ntf (sqlcolumn, 
1b340 22 2c 53 52 49 44 3d 25 64 22 2c 20 28 73 72 69  ",SRID=%d", (sri
1b350 64 20 3c 3d 20 30 29 20 3f 20 2d 31 20 3a 20 73  d <= 0) ? -1 : s
1b360 72 69 64 29 3b 0a 20 20 20 20 73 74 72 63 61 74  rid);.    strcat
1b370 20 28 73 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e   (sql, sqlcolumn
1b380 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
1b390 71 6c 2c 20 22 5d 20 73 75 63 63 65 73 73 66 75  ql, "] successfu
1b3a0 6c 6c 79 20 72 65 63 6f 76 65 72 65 64 22 29 3b  lly recovered");
1b3b0 0a 20 20 20 20 75 70 64 61 74 65 53 70 61 74 69  .    updateSpati
1b3c0 61 4c 69 74 65 48 69 73 74 6f 72 79 20 28 73 71  aLiteHistory (sq
1b3d0 6c 69 74 65 2c 20 28 63 6f 6e 73 74 20 63 68 61  lite, (const cha
1b3e0 72 20 2a 29 20 78 74 61 62 6c 65 2c 0a 09 09 09  r *) xtable,....
1b3f0 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72       (const char
1b400 20 2a 29 20 78 63 6f 6c 75 6d 6e 2c 20 73 71 6c   *) xcolumn, sql
1b410 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
1b420 20 65 72 72 6f 72 3a 0a 20 20 20 20 73 70 61 74   error:.    spat
1b430 69 61 6c 69 74 65 5f 65 20 28 22 52 65 63 6f 76  ialite_e ("Recov
1b440 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e  erGeometryColumn
1b450 28 29 20 65 72 72 6f 72 3a 20 5c 22 25 73 5c 22  () error: \"%s\"
1b460 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b 0a 20 20  \n", errMsg);.  
1b470 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28    sqlite3_free (
1b480 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c  errMsg);.    sql
1b490 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
1b4a0 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
1b4b0 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61    return;.}..sta
1b4c0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 44 69  tic void.fnct_Di
1b4d0 73 63 61 72 64 47 65 6f 6d 65 74 72 79 43 6f 6c  scardGeometryCol
1b4e0 75 6d 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  umn (sqlite3_con
1b4f0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
1b500 69 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 20 20  int argc,....   
1b510 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1b520 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
1b530 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 44 69 73   function:./ Dis
1b540 63 61 72 64 47 65 6f 6d 65 74 72 79 43 6f 6c 75  cardGeometryColu
1b550 6d 6e 28 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e  mn(table, column
1b560 29 0a 2f 0a 2f 20 72 65 6d 6f 76 65 73 20 54 41  )././ removes TA
1b570 42 4c 45 2e 43 4f 4c 55 4d 4e 20 66 72 6f 6d 20  BLE.COLUMN from 
1b580 74 68 65 20 53 70 61 74 69 61 6c 20 4d 65 74 61  the Spatial Meta
1b590 44 61 74 61 20 5b 74 68 75 73 20 64 69 73 61 62  Data [thus disab
1b5a0 6c 69 6e 67 20 74 72 69 67 67 65 72 73 20 74 6f  ling triggers to
1b5b0 6f 5d 0a 2f 20 72 65 74 75 72 6e 73 20 31 20 6f  o]./ returns 1 o
1b5c0 6e 20 73 75 63 63 65 73 73 0a 2f 20 30 20 6f 6e  n success./ 0 on
1b5d0 20 66 61 69 6c 75 72 65 0a 2a 2f 0a 20 20 20 20   failure.*/.    
1b5e0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1b5f0 68 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20  har *table;.    
1b600 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1b610 68 61 72 20 2a 63 6f 6c 75 6d 6e 3b 0a 20 20 20  har *column;.   
1b620 20 63 68 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b   char sql[1024];
1b630 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72 4d 73  .    char *errMs
1b640 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  g = NULL;.    in
1b650 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61 72 20  t ret;.    char 
1b660 78 6e 61 6d 65 5b 31 30 32 34 5d 3b 0a 20 20 20  xname[1024];.   
1b670 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31   char sqltable[1
1b680 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 73  024];.    char s
1b690 71 6c 63 6f 6c 75 6d 6e 5b 31 30 32 34 5d 3b 0a  qlcolumn[1024];.
1b6a0 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c      sqlite3 *sql
1b6b0 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ite = sqlite3_co
1b6c0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20  ntext_db_handle 
1b6d0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47  (context);.    G
1b6e0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
1b6f0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
1b700 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
1b710 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
1b720 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
1b730 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20  ITE_TEXT).      
1b740 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
1b750 65 0a 09 20 20 20 20 20 20 28 22 44 69 73 63 61  e..      ("Disca
1b760 72 64 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e  rdGeometryColumn
1b770 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65  () error: argume
1b780 6e 74 20 31 20 5b 74 61 62 6c 65 5f 6e 61 6d 65  nt 1 [table_name
1b790 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20  ] is not of the 
1b7a0 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b  String type\n");
1b7b0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
1b7c0 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
1b7d0 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a   0);..  return;.
1b7e0 20 20 20 20 20 20 7d 0a 20 20 20 20 74 61 62 6c        }.    tabl
1b7f0 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
1b800 65 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d 29  e_text (argv[0])
1b810 3b 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  ;.    if (sqlite
1b820 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
1b830 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[1]) != SQLITE
1b840 5f 54 45 58 54 29 0a 20 20 20 20 20 20 7b 0a 09  _TEXT).      {..
1b850 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09    spatialite_e..
1b860 20 20 20 20 20 20 28 22 44 69 73 63 61 72 64 47        ("DiscardG
1b870 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20  eometryColumn() 
1b880 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20  error: argument 
1b890 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 5d 20  2 [column_name] 
1b8a0 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 53 74  is not of the St
1b8b0 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a 09  ring type\n");..
1b8c0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1b8d0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
1b8e0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
1b8f0 20 20 20 20 7d 0a 20 20 20 20 63 6f 6c 75 6d 6e      }.    column
1b900 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1b910 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d 29 3b  _text (argv[1]);
1b920 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
1b930 74 61 62 6c 65 2c 20 28 63 68 61 72 20 2a 29 20  table, (char *) 
1b940 74 61 62 6c 65 29 3b 0a 20 20 20 20 63 6c 65 61  table);.    clea
1b950 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71  n_sql_string (sq
1b960 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
1b970 63 70 79 20 28 73 71 6c 63 6f 6c 75 6d 6e 2c 20  cpy (sqlcolumn, 
1b980 28 63 68 61 72 20 2a 29 20 63 6f 6c 75 6d 6e 29  (char *) column)
1b990 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f  ;.    clean_sql_
1b9a0 73 74 72 69 6e 67 20 28 73 71 6c 63 6f 6c 75 6d  string (sqlcolum
1b9b0 6e 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  n);.    sprintf 
1b9c0 28 73 71 6c 2c 0a 09 20 20 20 20 20 22 44 45 4c  (sql,..     "DEL
1b9d0 45 54 45 20 46 52 4f 4d 20 67 65 6f 6d 65 74 72  ETE FROM geometr
1b9e0 79 5f 63 6f 6c 75 6d 6e 73 20 57 48 45 52 45 20  y_columns WHERE 
1b9f0 55 70 70 65 72 28 66 5f 74 61 62 6c 65 5f 6e 61  Upper(f_table_na
1ba00 6d 65 29 20 3d 20 55 70 70 65 72 28 27 25 73 27  me) = Upper('%s'
1ba10 29 20 41 4e 44 20 55 70 70 65 72 28 66 5f 67 65  ) AND Upper(f_ge
1ba20 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 3d  ometry_column) =
1ba30 20 55 70 70 65 72 28 27 25 73 27 29 22 2c 0a 09   Upper('%s')",..
1ba40 20 20 20 20 20 73 71 6c 74 61 62 6c 65 2c 20 73       sqltable, s
1ba50 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 72  qlcolumn);.    r
1ba60 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
1ba70 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  c (sqlite, sql, 
1ba80 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
1ba90 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  Msg);.    if (re
1baa0 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
1bab0 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 2f 2a 20  .goto error;./* 
1bac0 72 65 6d 6f 76 69 6e 67 20 74 72 69 67 67 65 72  removing trigger
1bad0 73 20 74 6f 6f 20 2a 2f 0a 20 20 20 20 73 70 72  s too */.    spr
1bae0 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67 67  intf (xname, "gg
1baf0 69 5f 25 73 5f 25 73 22 2c 20 28 63 68 61 72 20  i_%s_%s", (char 
1bb00 2a 29 20 74 61 62 6c 65 2c 20 28 63 68 61 72 20  *) table, (char 
1bb10 2a 29 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  *) column);.    
1bb20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
1bb30 6c 20 28 78 6e 61 6d 65 29 3b 0a 20 20 20 20 73  l (xname);.    s
1bb40 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 44 52  printf (sql, "DR
1bb50 4f 50 20 54 52 49 47 47 45 52 20 49 46 20 45 58  OP TRIGGER IF EX
1bb60 49 53 54 53 20 25 73 22 2c 20 78 6e 61 6d 65 29  ISTS %s", xname)
1bb70 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
1bb80 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
1bb90 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c  , sql, NULL, NUL
1bba0 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20  L, &errMsg);.   
1bbb0 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
1bbc0 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72  TE_OK)..goto err
1bbd0 6f 72 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  or;.    sprintf 
1bbe0 28 78 6e 61 6d 65 2c 20 22 67 67 75 5f 25 73 5f  (xname, "ggu_%s_
1bbf0 25 73 22 2c 20 28 63 68 61 72 20 2a 29 20 74 61  %s", (char *) ta
1bc00 62 6c 65 2c 20 28 63 68 61 72 20 2a 29 20 63 6f  ble, (char *) co
1bc10 6c 75 6d 6e 29 3b 0a 20 20 20 20 64 6f 75 62 6c  lumn);.    doubl
1bc20 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e  e_quoted_sql (xn
1bc30 61 6d 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ame);.    sprint
1bc40 66 20 28 73 71 6c 2c 20 22 44 52 4f 50 20 54 52  f (sql, "DROP TR
1bc50 49 47 47 45 52 20 49 46 20 45 58 49 53 54 53 20  IGGER IF EXISTS 
1bc60 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a 20 20 20  %s", xname);.   
1bc70 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65   ret = sqlite3_e
1bc80 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c  xec (sqlite, sql
1bc90 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
1bca0 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28  rrMsg);.    if (
1bcb0 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b  ret != SQLITE_OK
1bcc0 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20  )..goto error;. 
1bcd0 20 20 20 73 70 72 69 6e 74 66 20 28 78 6e 61 6d     sprintf (xnam
1bce0 65 2c 20 22 67 69 69 5f 25 73 5f 25 73 22 2c 20  e, "gii_%s_%s", 
1bcf0 28 63 68 61 72 20 2a 29 20 74 61 62 6c 65 2c 20  (char *) table, 
1bd00 28 63 68 61 72 20 2a 29 20 63 6f 6c 75 6d 6e 29  (char *) column)
1bd10 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f  ;.    double_quo
1bd20 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b  ted_sql (xname);
1bd30 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
1bd40 6c 2c 20 22 44 52 4f 50 20 54 52 49 47 47 45 52  l, "DROP TRIGGER
1bd50 20 49 46 20 45 58 49 53 54 53 20 25 73 22 2c 20   IF EXISTS %s", 
1bd60 78 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 20  xname);.    ret 
1bd70 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
1bd80 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c  sqlite, sql, NUL
1bd90 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
1bda0 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
1bdb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f  = SQLITE_OK)..go
1bdc0 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 73 70  to error;.    sp
1bdd0 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67  rintf (xname, "g
1bde0 69 75 5f 25 73 5f 25 73 22 2c 20 28 63 68 61 72  iu_%s_%s", (char
1bdf0 20 2a 29 20 74 61 62 6c 65 2c 20 28 63 68 61 72   *) table, (char
1be00 20 2a 29 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20   *) column);.   
1be10 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
1be20 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 20 20 20 20  ql (xname);.    
1be30 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 44  sprintf (sql, "D
1be40 52 4f 50 20 54 52 49 47 47 45 52 20 49 46 20 45  ROP TRIGGER IF E
1be50 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61 6d 65  XISTS %s", xname
1be60 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c  );.    ret = sql
1be70 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74  ite3_exec (sqlit
1be80 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55  e, sql, NULL, NU
1be90 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20  LL, &errMsg);.  
1bea0 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
1beb0 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72  ITE_OK)..goto er
1bec0 72 6f 72 3b 0a 20 20 20 20 73 70 72 69 6e 74 66  ror;.    sprintf
1bed0 20 28 78 6e 61 6d 65 2c 20 22 67 69 64 5f 25 73   (xname, "gid_%s
1bee0 5f 25 73 22 2c 20 28 63 68 61 72 20 2a 29 20 74  _%s", (char *) t
1bef0 61 62 6c 65 2c 20 28 63 68 61 72 20 2a 29 20 63  able, (char *) c
1bf00 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 64 6f 75 62  olumn);.    doub
1bf10 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78  le_quoted_sql (x
1bf20 6e 61 6d 65 29 3b 0a 20 20 20 20 73 70 72 69 6e  name);.    sprin
1bf30 74 66 20 28 73 71 6c 2c 20 22 44 52 4f 50 20 54  tf (sql, "DROP T
1bf40 52 49 47 47 45 52 20 49 46 20 45 58 49 53 54 53  RIGGER IF EXISTS
1bf50 20 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a 20 20   %s", xname);.  
1bf60 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
1bf70 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
1bf80 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
1bf90 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20  errMsg);.    if 
1bfa0 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
1bfb0 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a  K)..goto error;.
1bfc0 20 20 20 20 73 70 72 69 6e 74 66 20 28 78 6e 61      sprintf (xna
1bfd0 6d 65 2c 20 22 67 63 69 5f 25 73 5f 25 73 22 2c  me, "gci_%s_%s",
1bfe0 20 28 63 68 61 72 20 2a 29 20 74 61 62 6c 65 2c   (char *) table,
1bff0 20 28 63 68 61 72 20 2a 29 20 63 6f 6c 75 6d 6e   (char *) column
1c000 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75  );.    double_qu
1c010 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d 65 29  oted_sql (xname)
1c020 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73  ;.    sprintf (s
1c030 71 6c 2c 20 22 44 52 4f 50 20 54 52 49 47 47 45  ql, "DROP TRIGGE
1c040 52 20 49 46 20 45 58 49 53 54 53 20 25 73 22 2c  R IF EXISTS %s",
1c050 20 78 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74   xname);.    ret
1c060 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
1c070 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
1c080 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
1c090 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
1c0a0 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67  != SQLITE_OK)..g
1c0b0 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 73  oto error;.    s
1c0c0 70 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22  printf (xname, "
1c0d0 67 63 75 5f 25 73 5f 25 73 22 2c 20 28 63 68 61  gcu_%s_%s", (cha
1c0e0 72 20 2a 29 20 74 61 62 6c 65 2c 20 28 63 68 61  r *) table, (cha
1c0f0 72 20 2a 29 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20  r *) column);.  
1c100 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f    double_quoted_
1c110 73 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 20 20 20  sql (xname);.   
1c120 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22   sprintf (sql, "
1c130 44 52 4f 50 20 54 52 49 47 47 45 52 20 49 46 20  DROP TRIGGER IF 
1c140 45 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61 6d  EXISTS %s", xnam
1c150 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71  e);.    ret = sq
1c160 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
1c170 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e  te, sql, NULL, N
1c180 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20  ULL, &errMsg);. 
1c190 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
1c1a0 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65  LITE_OK)..goto e
1c1b0 72 72 6f 72 3b 0a 20 20 20 20 73 70 72 69 6e 74  rror;.    sprint
1c1c0 66 20 28 78 6e 61 6d 65 2c 20 22 67 63 64 5f 25  f (xname, "gcd_%
1c1d0 73 5f 25 73 22 2c 20 28 63 68 61 72 20 2a 29 20  s_%s", (char *) 
1c1e0 74 61 62 6c 65 2c 20 28 63 68 61 72 20 2a 29 20  table, (char *) 
1c1f0 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 64 6f 75  column);.    dou
1c200 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
1c210 78 6e 61 6d 65 29 3b 0a 20 20 20 20 73 70 72 69  xname);.    spri
1c220 6e 74 66 20 28 73 71 6c 2c 20 22 44 52 4f 50 20  ntf (sql, "DROP 
1c230 54 52 49 47 47 45 52 20 49 46 20 45 58 49 53 54  TRIGGER IF EXIST
1c240 53 20 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a 20  S %s", xname);. 
1c250 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33     ret = sqlite3
1c260 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73  _exec (sqlite, s
1c270 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  ql, NULL, NULL, 
1c280 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  &errMsg);.    if
1c290 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
1c2a0 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b  OK)..goto error;
1c2b0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 78 6e  .    sprintf (xn
1c2c0 61 6d 65 2c 20 22 74 6d 69 5f 25 73 5f 25 73 22  ame, "tmi_%s_%s"
1c2d0 2c 20 28 63 68 61 72 20 2a 29 20 74 61 62 6c 65  , (char *) table
1c2e0 2c 20 28 63 68 61 72 20 2a 29 20 63 6f 6c 75 6d  , (char *) colum
1c2f0 6e 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  n);.    double_q
1c300 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d 65  uoted_sql (xname
1c310 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  );.    sprintf (
1c320 73 71 6c 2c 20 22 44 52 4f 50 20 54 52 49 47 47  sql, "DROP TRIGG
1c330 45 52 20 49 46 20 45 58 49 53 54 53 20 25 73 22  ER IF EXISTS %s"
1c340 2c 20 78 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65  , xname);.    re
1c350 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
1c360 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
1c370 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d  ULL, NULL, &errM
1c380 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74  sg);.    if (ret
1c390 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09   != SQLITE_OK)..
1c3a0 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20  goto error;.    
1c3b0 73 70 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20  sprintf (xname, 
1c3c0 22 74 6d 75 5f 25 73 5f 25 73 22 2c 20 28 63 68  "tmu_%s_%s", (ch
1c3d0 61 72 20 2a 29 20 74 61 62 6c 65 2c 20 28 63 68  ar *) table, (ch
1c3e0 61 72 20 2a 29 20 63 6f 6c 75 6d 6e 29 3b 0a 20  ar *) column);. 
1c3f0 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
1c400 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 20 20  _sql (xname);.  
1c410 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
1c420 22 44 52 4f 50 20 54 52 49 47 47 45 52 20 49 46  "DROP TRIGGER IF
1c430 20 45 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61   EXISTS %s", xna
1c440 6d 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73  me);.    ret = s
1c450 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
1c460 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
1c470 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a  NULL, &errMsg);.
1c480 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53      if (ret != S
1c490 51 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20  QLITE_OK)..goto 
1c4a0 65 72 72 6f 72 3b 0a 20 20 20 20 73 70 72 69 6e  error;.    sprin
1c4b0 74 66 20 28 78 6e 61 6d 65 2c 20 22 74 6d 64 5f  tf (xname, "tmd_
1c4c0 25 73 5f 25 73 22 2c 20 28 63 68 61 72 20 2a 29  %s_%s", (char *)
1c4d0 20 74 61 62 6c 65 2c 20 28 63 68 61 72 20 2a 29   table, (char *)
1c4e0 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 64 6f   column);.    do
1c4f0 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
1c500 28 78 6e 61 6d 65 29 3b 0a 20 20 20 20 73 70 72  (xname);.    spr
1c510 69 6e 74 66 20 28 73 71 6c 2c 20 22 44 52 4f 50  intf (sql, "DROP
1c520 20 54 52 49 47 47 45 52 20 49 46 20 45 58 49 53   TRIGGER IF EXIS
1c530 54 53 20 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a  TS %s", xname);.
1c540 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65      ret = sqlite
1c550 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20  3_exec (sqlite, 
1c560 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c  sql, NULL, NULL,
1c570 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69   &errMsg);.    i
1c580 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
1c590 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72  _OK)..goto error
1c5a0 3b 0a 0a 20 20 20 20 2f 2a 20 74 72 79 69 6e 67  ;..    /* trying
1c5b0 20 74 6f 20 64 65 6c 65 74 65 20 6f 6c 64 20 76   to delete old v
1c5c0 65 72 73 69 6f 6e 73 20 5b 76 32 2e 30 2c 20 76  ersions [v2.0, v
1c5d0 32 2e 32 5d 20 74 72 69 67 67 65 72 73 5b 69 66  2.2] triggers[if
1c5e0 20 61 6e 79 5d 20 2a 2f 0a 20 20 20 20 73 70 72   any] */.    spr
1c5f0 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67 74  intf (xname, "gt
1c600 69 5f 25 73 5f 25 73 22 2c 20 28 63 68 61 72 20  i_%s_%s", (char 
1c610 2a 29 20 74 61 62 6c 65 2c 20 28 63 68 61 72 20  *) table, (char 
1c620 2a 29 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  *) column);.    
1c630 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
1c640 6c 20 28 78 6e 61 6d 65 29 3b 0a 20 20 20 20 73  l (xname);.    s
1c650 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 44 52  printf (sql, "DR
1c660 4f 50 20 54 52 49 47 47 45 52 20 49 46 20 45 58  OP TRIGGER IF EX
1c670 49 53 54 53 20 25 73 22 2c 20 78 6e 61 6d 65 29  ISTS %s", xname)
1c680 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
1c690 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
1c6a0 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c  , sql, NULL, NUL
1c6b0 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20  L, &errMsg);.   
1c6c0 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
1c6d0 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72  TE_OK)..goto err
1c6e0 6f 72 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  or;.    sprintf 
1c6f0 28 78 6e 61 6d 65 2c 20 22 67 74 75 5f 25 73 5f  (xname, "gtu_%s_
1c700 25 73 22 2c 20 28 63 68 61 72 20 2a 29 20 74 61  %s", (char *) ta
1c710 62 6c 65 2c 20 28 63 68 61 72 20 2a 29 20 63 6f  ble, (char *) co
1c720 6c 75 6d 6e 29 3b 0a 20 20 20 20 64 6f 75 62 6c  lumn);.    doubl
1c730 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 6e  e_quoted_sql (xn
1c740 61 6d 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ame);.    sprint
1c750 66 20 28 73 71 6c 2c 20 22 44 52 4f 50 20 54 52  f (sql, "DROP TR
1c760 49 47 47 45 52 20 49 46 20 45 58 49 53 54 53 20  IGGER IF EXISTS 
1c770 25 73 22 2c 20 78 6e 61 6d 65 29 3b 0a 20 20 20  %s", xname);.   
1c780 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65   ret = sqlite3_e
1c790 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c  xec (sqlite, sql
1c7a0 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65  , NULL, NULL, &e
1c7b0 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28  rrMsg);.    if (
1c7c0 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b  ret != SQLITE_OK
1c7d0 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20  )..goto error;. 
1c7e0 20 20 20 73 70 72 69 6e 74 66 20 28 78 6e 61 6d     sprintf (xnam
1c7f0 65 2c 20 22 67 73 69 5f 25 73 5f 25 73 22 2c 20  e, "gsi_%s_%s", 
1c800 28 63 68 61 72 20 2a 29 20 74 61 62 6c 65 2c 20  (char *) table, 
1c810 28 63 68 61 72 20 2a 29 20 63 6f 6c 75 6d 6e 29  (char *) column)
1c820 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f  ;.    double_quo
1c830 74 65 64 5f 73 71 6c 20 28 78 6e 61 6d 65 29 3b  ted_sql (xname);
1c840 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
1c850 6c 2c 20 22 44 52 4f 50 20 54 52 49 47 47 45 52  l, "DROP TRIGGER
1c860 20 49 46 20 45 58 49 53 54 53 20 25 73 22 2c 20   IF EXISTS %s", 
1c870 78 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 20  xname);.    ret 
1c880 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
1c890 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c  sqlite, sql, NUL
1c8a0 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
1c8b0 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
1c8c0 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f  = SQLITE_OK)..go
1c8d0 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 73 70  to error;.    sp
1c8e0 72 69 6e 74 66 20 28 78 6e 61 6d 65 2c 20 22 67  rintf (xname, "g
1c8f0 73 75 5f 25 73 5f 25 73 22 2c 20 28 63 68 61 72  su_%s_%s", (char
1c900 20 2a 29 20 74 61 62 6c 65 2c 20 28 63 68 61 72   *) table, (char
1c910 20 2a 29 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20   *) column);.   
1c920 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
1c930 71 6c 20 28 78 6e 61 6d 65 29 3b 0a 20 20 20 20  ql (xname);.    
1c940 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 44  sprintf (sql, "D
1c950 52 4f 50 20 54 52 49 47 47 45 52 20 49 46 20 45  ROP TRIGGER IF E
1c960 58 49 53 54 53 20 25 73 22 2c 20 78 6e 61 6d 65  XISTS %s", xname
1c970 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c  );.    ret = sql
1c980 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74  ite3_exec (sqlit
1c990 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55  e, sql, NULL, NU
1c9a0 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20  LL, &errMsg);.  
1c9b0 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
1c9c0 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72  ITE_OK)..goto er
1c9d0 72 6f 72 3b 0a 20 20 20 20 2f 2a 20 65 6e 64 20  ror;.    /* end 
1c9e0 64 65 6c 65 74 69 6f 6e 20 6f 6c 64 20 76 65 72  deletion old ver
1c9f0 73 69 6f 6e 73 20 5b 76 32 2e 30 2c 20 76 32 2e  sions [v2.0, v2.
1ca00 32 5d 20 74 72 69 67 67 65 72 73 5b 69 66 20 61  2] triggers[if a
1ca10 6e 79 5d 20 2a 2f 0a 0a 20 20 20 20 73 71 6c 69  ny] */..    sqli
1ca20 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
1ca30 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 20  context, 1);.   
1ca40 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 47   strcpy (sql, "G
1ca50 65 6f 6d 65 74 72 79 20 73 75 63 63 65 73 73 66  eometry successf
1ca60 75 6c 6c 79 20 64 69 73 63 61 72 64 65 64 22 29  ully discarded")
1ca70 3b 0a 20 20 20 20 75 70 64 61 74 65 53 70 61 74  ;.    updateSpat
1ca80 69 61 4c 69 74 65 48 69 73 74 6f 72 79 20 28 73  iaLiteHistory (s
1ca90 71 6c 69 74 65 2c 20 28 63 6f 6e 73 74 20 63 68  qlite, (const ch
1caa0 61 72 20 2a 29 20 74 61 62 6c 65 2c 0a 09 09 09  ar *) table,....
1cab0 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72       (const char
1cac0 20 2a 29 20 63 6f 6c 75 6d 6e 2c 20 73 71 6c 29   *) column, sql)
1cad0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
1cae0 65 72 72 6f 72 3a 0a 20 20 20 20 73 70 61 74 69  error:.    spati
1caf0 61 6c 69 74 65 5f 65 20 28 22 44 69 73 63 61 72  alite_e ("Discar
1cb00 64 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28  dGeometryColumn(
1cb10 29 20 65 72 72 6f 72 3a 20 5c 22 25 73 5c 22 5c  ) error: \"%s\"\
1cb20 6e 22 2c 20 65 72 72 4d 73 67 29 3b 0a 20 20 20  n", errMsg);.   
1cb30 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65   sqlite3_free (e
1cb40 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69  rrMsg);.    sqli
1cb50 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
1cb60 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20  context, 0);.   
1cb70 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74   return;.}..stat
1cb80 69 63 20 69 6e 74 0a 72 65 67 69 73 74 65 72 56  ic int.registerV
1cb90 69 72 74 75 61 6c 20 28 73 71 6c 69 74 65 33 20  irtual (sqlite3 
1cba0 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73 74 20  * sqlite, const 
1cbb0 63 68 61 72 20 2a 74 61 62 6c 65 29 0a 7b 0a 2f  char *table).{./
1cbc0 2a 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  * attempting to 
1cbd0 72 65 67 69 73 74 65 72 20 61 20 56 69 72 74 75  register a Virtu
1cbe0 61 6c 47 65 6f 6d 65 74 72 79 20 2a 2f 0a 20 20  alGeometry */.  
1cbf0 20 20 63 68 61 72 20 73 71 6c 5b 38 31 39 32 5d    char sql[8192]
1cc00 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 32 5b  ;.    char sql2[
1cc10 38 31 39 32 5d 3b 0a 20 20 20 20 63 68 61 72 20  8192];.    char 
1cc20 78 74 61 62 6c 65 5b 34 30 39 36 5d 3b 0a 20 20  xtable[4096];.  
1cc30 20 20 63 68 61 72 20 67 74 79 70 65 5b 36 34 5d    char gtype[64]
1cc40 3b 0a 20 20 20 20 69 6e 74 20 78 74 79 70 65 20  ;.    int xtype 
1cc50 3d 20 2d 31 3b 0a 20 20 20 20 69 6e 74 20 73 72  = -1;.    int sr
1cc60 69 64 3b 0a 20 20 20 20 63 68 61 72 20 2a 2a 72  id;.    char **r
1cc70 65 73 75 6c 74 73 3b 0a 20 20 20 20 69 6e 74 20  esults;.    int 
1cc80 72 65 74 3b 0a 20 20 20 20 69 6e 74 20 72 6f 77  ret;.    int row
1cc90 73 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6c 75 6d  s;.    int colum
1cca0 6e 73 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  ns;.    int i;. 
1ccb0 20 20 20 63 68 61 72 20 2a 65 72 72 4d 73 67 20     char *errMsg 
1ccc0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20  = NULL;.    int 
1ccd0 6f 6b 5f 76 69 72 74 5f 6e 61 6d 65 20 3d 20 30  ok_virt_name = 0
1cce0 3b 0a 20 20 20 20 69 6e 74 20 6f 6b 5f 76 69 72  ;.    int ok_vir
1ccf0 74 5f 67 65 6f 6d 65 74 72 79 20 3d 20 30 3b 0a  t_geometry = 0;.
1cd00 20 20 20 20 69 6e 74 20 6f 6b 5f 73 72 69 64 20      int ok_srid 
1cd10 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6f 6b 5f  = 0;.    int ok_
1cd20 67 65 6f 6d 65 74 72 79 5f 74 79 70 65 20 3d 20  geometry_type = 
1cd30 30 3b 0a 20 20 20 20 69 6e 74 20 6f 6b 5f 74 79  0;.    int ok_ty
1cd40 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  pe = 0;.    int 
1cd50 6f 6b 5f 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69  ok_coord_dimensi
1cd60 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  on = 0;.    int 
1cd70 78 64 69 6d 73 3b 0a 0a 2f 2a 20 74 65 73 74 69  xdims;../* testi
1cd80 6e 67 20 74 68 65 20 6c 61 79 6f 75 74 20 6f 66  ng the layout of
1cd90 20 76 69 72 74 73 5f 67 65 6f 6d 65 74 72 79 5f   virts_geometry_
1cda0 63 6f 6c 75 6d 6e 73 20 74 61 62 6c 65 20 2a 2f  columns table */
1cdb0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
1cdc0 6c 2c 20 22 50 52 41 47 4d 41 20 74 61 62 6c 65  l, "PRAGMA table
1cdd0 5f 69 6e 66 6f 28 76 69 72 74 73 5f 67 65 6f 6d  _info(virts_geom
1cde0 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 29 22 29 3b  etry_columns)");
1cdf0 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74  .    ret = sqlit
1ce00 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 28 73 71  e3_get_table (sq
1ce10 6c 69 74 65 2c 20 73 71 6c 2c 20 26 72 65 73 75  lite, sql, &resu
1ce20 6c 74 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f 6c  lts, &rows, &col
1ce30 75 6d 6e 73 2c 20 26 65 72 72 4d 73 67 29 3b 0a  umns, &errMsg);.
1ce40 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53      if (ret != S
1ce50 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20  QLITE_OK).      
1ce60 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
1ce70 65 20 28 22 52 65 67 69 73 74 65 72 56 69 72 74  e ("RegisterVirt
1ce80 75 61 6c 47 65 6f 6d 65 74 72 79 28 29 20 65 72  ualGeometry() er
1ce90 72 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 20  ror: \"%s\"\n", 
1cea0 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71 6c 69  errMsg);..  sqli
1ceb0 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73 67  te3_free (errMsg
1cec0 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a  );..  return 0;.
1ced0 20 20 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 20        }.    for 
1cee0 28 69 20 3d 20 31 3b 20 69 20 3c 3d 20 72 6f 77  (i = 1; i <= row
1cef0 73 3b 20 69 2b 2b 29 0a 20 20 20 20 20 20 7b 0a  s; i++).      {.
1cf00 09 20 20 69 66 20 28 73 74 72 63 61 73 65 63 6d  .  if (strcasecm
1cf10 70 20 28 22 76 69 72 74 5f 6e 61 6d 65 22 2c 20  p ("virt_name", 
1cf20 72 65 73 75 6c 74 73 5b 28 69 20 2a 20 63 6f 6c  results[(i * col
1cf30 75 6d 6e 73 29 20 2b 20 31 5d 29 20 3d 3d 20 30  umns) + 1]) == 0
1cf40 29 0a 09 20 20 20 20 20 20 6f 6b 5f 76 69 72 74  )..      ok_virt
1cf50 5f 6e 61 6d 65 20 3d 20 31 3b 0a 09 20 20 69 66  _name = 1;..  if
1cf60 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 22 76   (strcasecmp ("v
1cf70 69 72 74 5f 67 65 6f 6d 65 74 72 79 22 2c 20 72  irt_geometry", r
1cf80 65 73 75 6c 74 73 5b 28 69 20 2a 20 63 6f 6c 75  esults[(i * colu
1cf90 6d 6e 73 29 20 2b 20 31 5d 29 20 3d 3d 20 30 29  mns) + 1]) == 0)
1cfa0 0a 09 20 20 20 20 20 20 6f 6b 5f 76 69 72 74 5f  ..      ok_virt_
1cfb0 67 65 6f 6d 65 74 72 79 20 3d 20 31 3b 0a 09 20  geometry = 1;.. 
1cfc0 20 69 66 20 28 73 74 72 63 61 73 65 63 6d 70 20   if (strcasecmp 
1cfd0 28 22 73 72 69 64 22 2c 20 72 65 73 75 6c 74 73  ("srid", results
1cfe0 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 20 2b  [(i * columns) +
1cff0 20 31 5d 29 20 3d 3d 20 30 29 0a 09 20 20 20 20   1]) == 0)..    
1d000 20 20 6f 6b 5f 73 72 69 64 20 3d 20 31 3b 0a 09    ok_srid = 1;..
1d010 20 20 69 66 20 28 73 74 72 63 61 73 65 63 6d 70    if (strcasecmp
1d020 20 28 22 67 65 6f 6d 65 74 72 79 5f 74 79 70 65   ("geometry_type
1d030 22 2c 20 72 65 73 75 6c 74 73 5b 28 69 20 2a 20  ", results[(i * 
1d040 63 6f 6c 75 6d 6e 73 29 20 2b 20 31 5d 29 20 3d  columns) + 1]) =
1d050 3d 20 30 29 0a 09 20 20 20 20 20 20 6f 6b 5f 67  = 0)..      ok_g
1d060 65 6f 6d 65 74 72 79 5f 74 79 70 65 20 3d 20 31  eometry_type = 1
1d070 3b 0a 09 20 20 69 66 20 28 73 74 72 63 61 73 65  ;..  if (strcase
1d080 63 6d 70 20 28 22 74 79 70 65 22 2c 20 72 65 73  cmp ("type", res
1d090 75 6c 74 73 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e  ults[(i * column
1d0a0 73 29 20 2b 20 31 5d 29 20 3d 3d 20 30 29 0a 09  s) + 1]) == 0)..
1d0b0 20 20 20 20 20 20 6f 6b 5f 74 79 70 65 20 3d 20        ok_type = 
1d0c0 31 3b 0a 09 20 20 69 66 20 28 73 74 72 63 61 73  1;..  if (strcas
1d0d0 65 63 6d 70 20 28 22 63 6f 6f 72 64 5f 64 69 6d  ecmp ("coord_dim
1d0e0 65 6e 73 69 6f 6e 22 2c 20 72 65 73 75 6c 74 73  ension", results
1d0f0 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 20 2b  [(i * columns) +
1d100 20 31 5d 29 20 3d 3d 20 30 29 0a 09 20 20 20 20   1]) == 0)..    
1d110 20 20 6f 6b 5f 63 6f 6f 72 64 5f 64 69 6d 65 6e    ok_coord_dimen
1d120 73 69 6f 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20  sion = 1;.      
1d130 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
1d140 65 65 5f 74 61 62 6c 65 20 28 72 65 73 75 6c 74  ee_table (result
1d150 73 29 3b 0a 0a 20 20 20 20 69 66 20 28 6f 6b 5f  s);..    if (ok_
1d160 76 69 72 74 5f 6e 61 6d 65 20 26 26 20 6f 6b 5f  virt_name && ok_
1d170 76 69 72 74 5f 67 65 6f 6d 65 74 72 79 20 26 26  virt_geometry &&
1d180 20 6f 6b 5f 73 72 69 64 20 26 26 20 6f 6b 5f 67   ok_srid && ok_g
1d190 65 6f 6d 65 74 72 79 5f 74 79 70 65 0a 09 26 26  eometry_type..&&
1d1a0 20 6f 6b 5f 63 6f 6f 72 64 5f 64 69 6d 65 6e 73   ok_coord_dimens
1d1b0 69 6f 6e 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65  ion)..;.    else
1d1c0 20 69 66 20 28 6f 6b 5f 76 69 72 74 5f 6e 61 6d   if (ok_virt_nam
1d1d0 65 20 26 26 20 6f 6b 5f 76 69 72 74 5f 67 65 6f  e && ok_virt_geo
1d1e0 6d 65 74 72 79 20 26 26 20 6f 6b 5f 73 72 69 64  metry && ok_srid
1d1f0 20 26 26 20 6f 6b 5f 74 79 70 65 29 0a 09 3b 0a   && ok_type)..;.
1d200 20 20 20 20 65 6c 73 65 0a 09 72 65 74 75 72 6e      else..return
1d210 20 30 3b 0a 0a 2f 2a 20 64 65 74 65 72 6d 69 6e   0;../* determin
1d220 69 6e 67 20 47 65 6f 6d 65 74 72 79 20 54 79 70  ing Geometry Typ
1d230 65 20 61 6e 64 20 64 69 6d 73 20 2a 2f 0a 20 20  e and dims */.  
1d240 20 20 73 74 72 63 70 79 20 28 78 74 61 62 6c 65    strcpy (xtable
1d250 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 64 6f  , table);.    do
1d260 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
1d270 28 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 70  (xtable);.    sp
1d280 72 69 6e 74 66 20 28 73 71 6c 2c 0a 09 20 20 20  rintf (sql,..   
1d290 20 20 22 53 45 4c 45 43 54 20 44 49 53 54 49 4e    "SELECT DISTIN
1d2a0 43 54 20 53 54 5f 47 65 6f 6d 65 74 72 79 54 79  CT ST_GeometryTy
1d2b0 70 65 28 47 65 6f 6d 65 74 72 79 29 2c 20 53 54  pe(Geometry), ST
1d2c0 5f 53 72 69 64 28 47 65 6f 6d 65 74 72 79 29 20  _Srid(Geometry) 
1d2d0 46 52 4f 4d 20 25 73 22 2c 0a 09 20 20 20 20 20  FROM %s",..     
1d2e0 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 72 65 74  xtable);.    ret
1d2f0 20 3d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74   = sqlite3_get_t
1d300 61 62 6c 65 20 28 73 71 6c 69 74 65 2c 20 73 71  able (sqlite, sq
1d310 6c 2c 20 26 72 65 73 75 6c 74 73 2c 20 26 72 6f  l, &results, &ro
1d320 77 73 2c 20 26 63 6f 6c 75 6d 6e 73 2c 20 26 65  ws, &columns, &e
1d330 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20 28  rrMsg);.    if (
1d340 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b  ret != SQLITE_OK
1d350 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61  ).      {..  spa
1d360 74 69 61 6c 69 74 65 5f 65 20 28 22 52 65 67 69  tialite_e ("Regi
1d370 73 74 65 72 56 69 72 74 75 61 6c 47 65 6f 6d 65  sterVirtualGeome
1d380 74 72 79 28 29 20 65 72 72 6f 72 3a 20 5c 22 25  try() error: \"%
1d390 73 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b  s\"\n", errMsg);
1d3a0 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
1d3b0 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20 72 65   (errMsg);..  re
1d3c0 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
1d3d0 20 20 20 20 66 6f 72 20 28 69 20 3d 20 31 3b 20      for (i = 1; 
1d3e0 69 20 3c 3d 20 72 6f 77 73 3b 20 69 2b 2b 29 0a  i <= rows; i++).
1d3f0 20 20 20 20 20 20 7b 0a 09 20 20 73 74 72 63 70        {..  strcp
1d400 79 20 28 67 74 79 70 65 2c 20 72 65 73 75 6c 74  y (gtype, result
1d410 73 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 5d  s[(i * columns)]
1d420 29 3b 0a 09 20 20 73 72 69 64 20 3d 20 61 74 6f  );..  srid = ato
1d430 69 20 28 72 65 73 75 6c 74 73 5b 28 69 20 2a 20  i (results[(i * 
1d440 63 6f 6c 75 6d 6e 73 29 20 2b 20 31 5d 29 3b 0a  columns) + 1]);.
1d450 20 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69        }.    sqli
1d460 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 28  te3_free_table (
1d470 72 65 73 75 6c 74 73 29 3b 0a 0a 2f 2a 20 6e 6f  results);../* no
1d480 72 6d 61 6c 69 7a 65 64 20 47 65 6f 6d 65 74 72  rmalized Geometr
1d490 79 20 74 79 70 65 20 2a 2f 0a 20 20 20 20 69 66  y type */.    if
1d4a0 20 28 73 74 72 63 6d 70 20 28 67 74 79 70 65 2c   (strcmp (gtype,
1d4b0 20 22 50 4f 49 4e 54 22 29 20 3d 3d 20 30 29 0a   "POINT") == 0).
1d4c0 09 78 74 79 70 65 20 3d 20 31 3b 0a 20 20 20 20  .xtype = 1;.    
1d4d0 69 66 20 28 73 74 72 63 6d 70 20 28 67 74 79 70  if (strcmp (gtyp
1d4e0 65 2c 20 22 50 4f 49 4e 54 20 5a 22 29 20 3d 3d  e, "POINT Z") ==
1d4f0 20 30 29 0a 09 78 74 79 70 65 20 3d 20 31 30 30   0)..xtype = 100
1d500 31 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63 6d  1;.    if (strcm
1d510 70 20 28 67 74 79 70 65 2c 20 22 50 4f 49 4e 54  p (gtype, "POINT
1d520 20 4d 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70   M") == 0)..xtyp
1d530 65 20 3d 20 32 30 30 31 3b 0a 20 20 20 20 69 66  e = 2001;.    if
1d540 20 28 73 74 72 63 6d 70 20 28 67 74 79 70 65 2c   (strcmp (gtype,
1d550 20 22 50 4f 49 4e 54 20 5a 4d 22 29 20 3d 3d 20   "POINT ZM") == 
1d560 30 29 0a 09 78 74 79 70 65 20 3d 20 33 30 30 31  0)..xtype = 3001
1d570 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63 6d 70  ;.    if (strcmp
1d580 20 28 67 74 79 70 65 2c 20 22 4c 49 4e 45 53 54   (gtype, "LINEST
1d590 52 49 4e 47 22 29 20 3d 3d 20 30 29 0a 09 78 74  RING") == 0)..xt
1d5a0 79 70 65 20 3d 20 32 3b 0a 20 20 20 20 69 66 20  ype = 2;.    if 
1d5b0 28 73 74 72 63 6d 70 20 28 67 74 79 70 65 2c 20  (strcmp (gtype, 
1d5c0 22 4c 49 4e 45 53 54 52 49 4e 47 20 5a 22 29 20  "LINESTRING Z") 
1d5d0 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20 31  == 0)..xtype = 1
1d5e0 30 30 32 3b 0a 20 20 20 20 69 66 20 28 73 74 72  002;.    if (str
1d5f0 63 6d 70 20 28 67 74 79 70 65 2c 20 22 4c 49 4e  cmp (gtype, "LIN
1d600 45 53 54 52 49 4e 47 20 4d 22 29 20 3d 3d 20 30  ESTRING M") == 0
1d610 29 0a 09 78 74 79 70 65 20 3d 20 32 30 30 32 3b  )..xtype = 2002;
1d620 0a 20 20 20 20 69 66 20 28 73 74 72 63 6d 70 20  .    if (strcmp 
1d630 28 67 74 79 70 65 2c 20 22 4c 49 4e 45 53 54 52  (gtype, "LINESTR
1d640 49 4e 47 20 5a 4d 22 29 20 3d 3d 20 30 29 0a 09  ING ZM") == 0)..
1d650 78 74 79 70 65 20 3d 20 33 30 30 32 3b 0a 20 20  xtype = 3002;.  
1d660 20 20 69 66 20 28 73 74 72 63 6d 70 20 28 67 74    if (strcmp (gt
1d670 79 70 65 2c 20 22 50 4f 4c 59 47 4f 4e 22 29 20  ype, "POLYGON") 
1d680 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20 33  == 0)..xtype = 3
1d690 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63 6d 70  ;.    if (strcmp
1d6a0 20 28 67 74 79 70 65 2c 20 22 50 4f 4c 59 47 4f   (gtype, "POLYGO
1d6b0 4e 20 5a 22 29 20 3d 3d 20 30 29 0a 09 78 74 79  N Z") == 0)..xty
1d6c0 70 65 20 3d 20 31 30 30 33 3b 0a 20 20 20 20 69  pe = 1003;.    i
1d6d0 66 20 28 73 74 72 63 6d 70 20 28 67 74 79 70 65  f (strcmp (gtype
1d6e0 2c 20 22 50 4f 4c 59 47 4f 4e 20 4d 22 29 20 3d  , "POLYGON M") =
1d6f0 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20 32 30  = 0)..xtype = 20
1d700 30 33 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63  03;.    if (strc
1d710 6d 70 20 28 67 74 79 70 65 2c 20 22 50 4f 4c 59  mp (gtype, "POLY
1d720 47 4f 4e 20 5a 4d 22 29 20 3d 3d 20 30 29 0a 09  GON ZM") == 0)..
1d730 78 74 79 70 65 20 3d 20 33 30 30 33 3b 0a 20 20  xtype = 3003;.  
1d740 20 20 69 66 20 28 73 74 72 63 6d 70 20 28 67 74    if (strcmp (gt
1d750 79 70 65 2c 20 22 4d 55 4c 54 49 50 4f 49 4e 54  ype, "MULTIPOINT
1d760 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70 65 20  ") == 0)..xtype 
1d770 3d 20 34 3b 0a 20 20 20 20 69 66 20 28 73 74 72  = 4;.    if (str
1d780 63 6d 70 20 28 67 74 79 70 65 2c 20 22 4d 55 4c  cmp (gtype, "MUL
1d790 54 49 50 4f 49 4e 54 20 5a 22 29 20 3d 3d 20 30  TIPOINT Z") == 0
1d7a0 29 0a 09 78 74 79 70 65 20 3d 20 31 30 30 34 3b  )..xtype = 1004;
1d7b0 0a 20 20 20 20 69 66 20 28 73 74 72 63 6d 70 20  .    if (strcmp 
1d7c0 28 67 74 79 70 65 2c 20 22 4d 55 4c 54 49 50 4f  (gtype, "MULTIPO
1d7d0 49 4e 54 20 4d 22 29 20 3d 3d 20 30 29 0a 09 78  INT M") == 0)..x
1d7e0 74 79 70 65 20 3d 20 32 30 30 34 3b 0a 20 20 20  type = 2004;.   
1d7f0 20 69 66 20 28 73 74 72 63 6d 70 20 28 67 74 79   if (strcmp (gty
1d800 70 65 2c 20 22 4d 55 4c 54 49 50 4f 49 4e 54 20  pe, "MULTIPOINT 
1d810 5a 4d 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70  ZM") == 0)..xtyp
1d820 65 20 3d 20 33 30 30 34 3b 0a 20 20 20 20 69 66  e = 3004;.    if
1d830 20 28 73 74 72 63 6d 70 20 28 67 74 79 70 65 2c   (strcmp (gtype,
1d840 20 22 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e   "MULTILINESTRIN
1d850 47 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70 65  G") == 0)..xtype
1d860 20 3d 20 35 3b 0a 20 20 20 20 69 66 20 28 73 74   = 5;.    if (st
1d870 72 63 6d 70 20 28 67 74 79 70 65 2c 20 22 4d 55  rcmp (gtype, "MU
1d880 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 20 5a 22  LTILINESTRING Z"
1d890 29 20 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d  ) == 0)..xtype =
1d8a0 20 31 30 30 35 3b 0a 20 20 20 20 69 66 20 28 73   1005;.    if (s
1d8b0 74 72 63 6d 70 20 28 67 74 79 70 65 2c 20 22 4d  trcmp (gtype, "M
1d8c0 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 20 4d  ULTILINESTRING M
1d8d0 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70 65 20  ") == 0)..xtype 
1d8e0 3d 20 32 30 30 35 3b 0a 20 20 20 20 69 66 20 28  = 2005;.    if (
1d8f0 73 74 72 63 6d 70 20 28 67 74 79 70 65 2c 20 22  strcmp (gtype, "
1d900 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 20  MULTILINESTRING 
1d910 5a 4d 22 29 20 3d 3d 20 30 29 0a 09 78 74 79 70  ZM") == 0)..xtyp
1d920 65 20 3d 20 33 30 30 35 3b 0a 20 20 20 20 69 66  e = 3005;.    if
1d930 20 28 73 74 72 63 6d 70 20 28 67 74 79 70 65 2c   (strcmp (gtype,
1d940 20 22 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 22 29   "MULTIPOLYGON")
1d950 20 3d 3d 20 30 29 0a 09 78 74 79 70 65 20 3d 20   == 0)..xtype = 
1d960 36 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63 6d  6;.    if (strcm
1d970 70 20 28 67 74 79 70 65 2c 20 22 4d 55 4c 54 49  p (gtype, "MULTI
1d980 50 4f 4c 59 47 4f 4e 20 5a 22 29 20 3d 3d 20 30  POLYGON Z") == 0
1d990 29 0a 09 78 74 79 70 65 20 3d 20 31 30 30 36 3b  )..xtype = 1006;
1d9a0 0a 20 20 20 20 69 66 20 28 73 74 72 63 6d 70 20  .    if (strcmp 
1d9b0 28 67 74 79 70 65 2c 20 22 4d 55 4c 54 49 50 4f  (gtype, "MULTIPO
1d9c0 4c 59 47 4f 4e 20 4d 22 29 20 3d 3d 20 30 29 0a  LYGON M") == 0).
1d9d0 09 78 74 79 70 65 20 3d 20 32 30 30 36 3b 0a 20  .xtype = 2006;. 
1d9e0 20 20 20 69 66 20 28 73 74 72 63 6d 70 20 28 67     if (strcmp (g
1d9f0 74 79 70 65 2c 20 22 4d 55 4c 54 49 50 4f 4c 59  type, "MULTIPOLY
1da00 47 4f 4e 20 5a 4d 22 29 20 3d 3d 20 30 29 0a 09  GON ZM") == 0)..
1da10 78 74 79 70 65 20 3d 20 33 30 30 36 3b 0a 0a 2f  xtype = 3006;../
1da20 2a 20 75 70 64 61 74 69 6e 67 20 6d 65 74 61 64  * updating metad
1da30 61 74 61 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20  ata tables */.  
1da40 20 20 78 64 69 6d 73 20 3d 20 2d 31 3b 0a 20 20    xdims = -1;.  
1da50 20 20 73 77 69 74 63 68 20 28 78 74 79 70 65 29    switch (xtype)
1da60 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 63  .      {.      c
1da70 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73  ase 1:.      cas
1da80 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 2:.      case 
1da90 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a  3:.      case 4:
1daa0 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 0a 20  .      case 5:. 
1dab0 20 20 20 20 20 63 61 73 65 20 36 3a 0a 09 20 20       case 6:..  
1dac0 78 64 69 6d 73 20 3d 20 32 3b 0a 09 20 20 62 72  xdims = 2;..  br
1dad0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
1dae0 31 30 30 31 3a 0a 20 20 20 20 20 20 63 61 73 65  1001:.      case
1daf0 20 31 30 30 32 3a 0a 20 20 20 20 20 20 63 61 73   1002:.      cas
1db00 65 20 31 30 30 33 3a 0a 20 20 20 20 20 20 63 61  e 1003:.      ca
1db10 73 65 20 31 30 30 34 3a 0a 20 20 20 20 20 20 63  se 1004:.      c
1db20 61 73 65 20 31 30 30 35 3a 0a 20 20 20 20 20 20  ase 1005:.      
1db30 63 61 73 65 20 31 30 30 36 3a 0a 20 20 20 20 20  case 1006:.     
1db40 20 63 61 73 65 20 32 30 30 31 3a 0a 20 20 20 20   case 2001:.    
1db50 20 20 63 61 73 65 20 32 30 30 32 3a 0a 20 20 20    case 2002:.   
1db60 20 20 20 63 61 73 65 20 32 30 30 33 3a 0a 20 20     case 2003:.  
1db70 20 20 20 20 63 61 73 65 20 32 30 30 34 3a 0a 20      case 2004:. 
1db80 20 20 20 20 20 63 61 73 65 20 32 30 30 35 3a 0a       case 2005:.
1db90 20 20 20 20 20 20 63 61 73 65 20 32 30 30 36 3a        case 2006:
1dba0 0a 09 20 20 78 64 69 6d 73 20 3d 20 33 3b 0a 09  ..  xdims = 3;..
1dbb0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1dbc0 61 73 65 20 33 30 30 31 3a 0a 20 20 20 20 20 20  ase 3001:.      
1dbd0 63 61 73 65 20 33 30 30 32 3a 0a 20 20 20 20 20  case 3002:.     
1dbe0 20 63 61 73 65 20 33 30 30 33 3a 0a 20 20 20 20   case 3003:.    
1dbf0 20 20 63 61 73 65 20 33 30 30 34 3a 0a 20 20 20    case 3004:.   
1dc00 20 20 20 63 61 73 65 20 33 30 30 35 3a 0a 20 20     case 3005:.  
1dc10 20 20 20 20 63 61 73 65 20 33 30 30 36 3a 0a 09      case 3006:..
1dc20 20 20 78 64 69 6d 73 20 3d 20 34 3b 0a 09 20 20    xdims = 4;..  
1dc30 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 3b 0a  break;.      };.
1dc40 20 20 20 20 73 74 72 63 70 79 20 28 78 74 61 62      strcpy (xtab
1dc50 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20 20  le, table);.    
1dc60 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67  clean_sql_string
1dc70 20 28 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 69   (xtable);.    i
1dc80 66 20 28 6f 6b 5f 67 65 6f 6d 65 74 72 79 5f 74  f (ok_geometry_t
1dc90 79 70 65 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  ype).      {..  
1dca0 2f 2a 20 68 61 73 20 74 68 65 20 22 67 65 6f 6d  /* has the "geom
1dcb0 65 74 72 79 5f 74 79 70 65 22 20 63 6f 6c 75 6d  etry_type" colum
1dcc0 6e 20 2a 2f 0a 09 20 20 73 74 72 63 70 79 20 28  n */..  strcpy (
1dcd0 73 71 6c 2c 20 22 49 4e 53 45 52 54 20 4f 52 20  sql, "INSERT OR 
1dce0 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 76 69 72  REPLACE INTO vir
1dcf0 74 73 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  ts_geometry_colu
1dd00 6d 6e 73 20 22 29 3b 0a 09 20 20 73 74 72 63 61  mns ");..  strca
1dd10 74 20 28 73 71 6c 2c 0a 09 09 20 20 22 28 76 69  t (sql,...  "(vi
1dd20 72 74 5f 6e 61 6d 65 2c 20 76 69 72 74 5f 67 65  rt_name, virt_ge
1dd30 6f 6d 65 74 72 79 2c 20 67 65 6f 6d 65 74 72 79  ometry, geometry
1dd40 5f 74 79 70 65 2c 20 63 6f 6f 72 64 5f 64 69 6d  _type, coord_dim
1dd50 65 6e 73 69 6f 6e 2c 20 73 72 69 64 29 20 22 29  ension, srid) ")
1dd60 3b 0a 09 20 20 73 70 72 69 6e 74 66 20 28 73 71  ;..  sprintf (sq
1dd70 6c 32 2c 20 22 56 41 4c 55 45 53 20 28 27 25 73  l2, "VALUES ('%s
1dd80 27 2c 20 27 47 65 6f 6d 65 74 72 79 27 2c 20 25  ', 'Geometry', %
1dd90 64 2c 20 25 64 2c 20 25 64 29 22 2c 20 78 74 61  d, %d, %d)", xta
1dda0 62 6c 65 2c 20 78 74 79 70 65 2c 0a 09 09 20 20  ble, xtype,...  
1ddb0 20 78 64 69 6d 73 2c 20 73 72 69 64 29 3b 0a 09   xdims, srid);..
1ddc0 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
1ddd0 71 6c 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ql2);.      }.  
1dde0 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
1ddf0 20 20 2f 2a 20 68 61 73 20 74 68 65 20 22 74 79    /* has the "ty
1de00 70 65 22 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 09 20  pe" column */.. 
1de10 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 78 67 74   const char *xgt
1de20 79 70 65 20 3d 20 22 55 4e 4b 4e 4f 57 4e 22 3b  ype = "UNKNOWN";
1de30 0a 09 20 20 73 77 69 74 63 68 20 28 78 74 79 70  ..  switch (xtyp
1de40 65 29 0a 09 20 20 20 20 7b 0a 09 20 20 20 20 63  e)..    {..    c
1de50 61 73 65 20 31 3a 0a 09 20 20 20 20 63 61 73 65  ase 1:..    case
1de60 20 31 30 30 31 3a 0a 09 20 20 20 20 63 61 73 65   1001:..    case
1de70 20 32 30 30 31 3a 0a 09 20 20 20 20 63 61 73 65   2001:..    case
1de80 20 33 30 30 31 3a 0a 09 09 78 67 74 79 70 65 20   3001:...xgtype 
1de90 3d 20 22 50 4f 49 4e 54 22 3b 0a 09 09 62 72 65  = "POINT";...bre
1dea0 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 32 3a  ak;..    case 2:
1deb0 0a 09 20 20 20 20 63 61 73 65 20 31 30 30 32 3a  ..    case 1002:
1dec0 0a 09 20 20 20 20 63 61 73 65 20 32 30 30 32 3a  ..    case 2002:
1ded0 0a 09 20 20 20 20 63 61 73 65 20 33 30 30 32 3a  ..    case 3002:
1dee0 0a 09 09 78 67 74 79 70 65 20 3d 20 22 4c 49 4e  ...xgtype = "LIN
1def0 45 53 54 52 49 4e 47 22 3b 0a 09 09 62 72 65 61  ESTRING";...brea
1df00 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 33 3a 0a  k;..    case 3:.
1df10 09 20 20 20 20 63 61 73 65 20 31 30 30 33 3a 0a  .    case 1003:.
1df20 09 20 20 20 20 63 61 73 65 20 32 30 30 33 3a 0a  .    case 2003:.
1df30 09 20 20 20 20 63 61 73 65 20 33 30 30 33 3a 0a  .    case 3003:.
1df40 09 09 78 67 74 79 70 65 20 3d 20 22 50 4f 4c 59  ..xgtype = "POLY
1df50 47 4f 4e 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  GON";...break;..
1df60 20 20 20 20 63 61 73 65 20 34 3a 0a 09 20 20 20      case 4:..   
1df70 20 63 61 73 65 20 31 30 30 34 3a 0a 09 20 20 20   case 1004:..   
1df80 20 63 61 73 65 20 32 30 30 34 3a 0a 09 20 20 20   case 2004:..   
1df90 20 63 61 73 65 20 33 30 30 34 3a 0a 09 09 78 67   case 3004:...xg
1dfa0 74 79 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f 49  type = "MULTIPOI
1dfb0 4e 54 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20  NT";...break;.. 
1dfc0 20 20 20 63 61 73 65 20 35 3a 0a 09 20 20 20 20     case 5:..    
1dfd0 63 61 73 65 20 31 30 30 35 3a 0a 09 20 20 20 20  case 1005:..    
1dfe0 63 61 73 65 20 32 30 30 35 3a 0a 09 20 20 20 20  case 2005:..    
1dff0 63 61 73 65 20 33 30 30 35 3a 0a 09 09 78 67 74  case 3005:...xgt
1e000 79 70 65 20 3d 20 22 4d 55 4c 54 49 4c 49 4e 45  ype = "MULTILINE
1e010 53 54 52 49 4e 47 22 3b 0a 09 09 62 72 65 61 6b  STRING";...break
1e020 3b 0a 09 20 20 20 20 63 61 73 65 20 36 3a 0a 09  ;..    case 6:..
1e030 20 20 20 20 63 61 73 65 20 31 30 30 36 3a 0a 09      case 1006:..
1e040 20 20 20 20 63 61 73 65 20 32 30 30 36 3a 0a 09      case 2006:..
1e050 20 20 20 20 63 61 73 65 20 33 30 30 36 3a 0a 09      case 3006:..
1e060 09 78 67 74 79 70 65 20 3d 20 22 4d 55 4c 54 49  .xgtype = "MULTI
1e070 50 4f 4c 59 47 4f 4e 22 3b 0a 09 09 62 72 65 61  POLYGON";...brea
1e080 6b 3b 0a 09 20 20 20 20 7d 3b 0a 09 20 20 73 74  k;..    };..  st
1e090 72 63 70 79 20 28 73 71 6c 2c 20 22 49 4e 53 45  rcpy (sql, "INSE
1e0a0 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e  RT OR REPLACE IN
1e0b0 54 4f 20 76 69 72 74 73 5f 67 65 6f 6d 65 74 72  TO virts_geometr
1e0c0 79 5f 63 6f 6c 75 6d 6e 73 20 22 29 3b 0a 09 20  y_columns ");.. 
1e0d0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28   strcat (sql, "(
1e0e0 76 69 72 74 5f 6e 61 6d 65 2c 20 76 69 72 74 5f  virt_name, virt_
1e0f0 67 65 6f 6d 65 74 72 79 2c 20 74 79 70 65 2c 20  geometry, type, 
1e100 73 72 69 64 29 20 22 29 3b 0a 09 20 20 73 70 72  srid) ");..  spr
1e110 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 56 41 4c  intf (sql2, "VAL
1e120 55 45 53 20 28 27 25 73 27 2c 20 27 47 65 6f 6d  UES ('%s', 'Geom
1e130 65 74 72 79 27 2c 20 27 25 73 27 2c 20 25 64 29  etry', '%s', %d)
1e140 22 2c 20 78 74 61 62 6c 65 2c 20 78 67 74 79 70  ", xtable, xgtyp
1e150 65 2c 0a 09 09 20 20 20 73 72 69 64 29 3b 0a 09  e,...   srid);..
1e160 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
1e170 71 6c 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ql2);.      }.  
1e180 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
1e190 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
1e1a0 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
1e1b0 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20  errMsg);.    if 
1e1c0 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
1e1d0 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70  K).      {..  sp
1e1e0 61 74 69 61 6c 69 74 65 5f 65 20 28 22 52 65 67  atialite_e ("Reg
1e1f0 69 73 74 65 72 56 69 72 74 75 61 6c 47 65 6f 6d  isterVirtualGeom
1e200 65 74 72 79 28 29 20 65 72 72 6f 72 3a 20 5c 22  etry() error: \"
1e210 25 73 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67 29  %s\"\n", errMsg)
1e220 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
1e230 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20 72  e (errMsg);..  r
1e240 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
1e250 0a 20 20 20 20 69 66 20 28 63 68 65 63 6b 53 70  .    if (checkSp
1e260 61 74 69 61 6c 4d 65 74 61 44 61 74 61 20 28 73  atialMetaData (s
1e270 71 6c 69 74 65 29 20 3d 3d 20 33 29 0a 20 20 20  qlite) == 3).   
1e280 20 20 20 7b 0a 09 20 20 2f 2a 20 63 75 72 72 65     {..  /* curre
1e290 6e 74 20 6d 65 74 61 64 61 74 61 20 73 74 79 6c  nt metadata styl
1e2a0 65 20 3e 3d 20 76 2e 34 2e 30 2e 30 20 2a 2f 0a  e >= v.4.0.0 */.
1e2b0 0a 09 20 20 2f 2a 20 69 6e 73 65 72 74 69 6e 67  ..  /* inserting
1e2c0 20 61 20 72 6f 77 20 69 6e 74 6f 20 56 49 52 54   a row into VIRT
1e2d0 53 5f 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d  S_GEOMETRY_COLUM
1e2e0 4e 53 5f 41 55 54 48 20 2a 2f 0a 09 20 20 73 74  NS_AUTH */..  st
1e2f0 72 63 70 79 20 28 73 71 6c 2c 0a 09 09 20 20 22  rcpy (sql,...  "
1e300 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
1e310 45 20 49 4e 54 4f 20 76 69 72 74 73 5f 67 65 6f  E INTO virts_geo
1e320 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 61 75  metry_columns_au
1e330 74 68 20 28 76 69 72 74 5f 6e 61 6d 65 2c 20 76  th (virt_name, v
1e340 69 72 74 5f 67 65 6f 6d 65 74 72 79 2c 20 22 29  irt_geometry, ")
1e350 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73 71 6c  ;..  strcat (sql
1e360 2c 20 22 68 69 64 64 65 6e 29 20 56 41 4c 55 45  , "hidden) VALUE
1e370 53 20 28 22 29 3b 0a 09 20 20 73 74 72 63 61 74  S (");..  strcat
1e380 20 28 73 71 6c 2c 20 22 27 22 29 3b 0a 09 20 20   (sql, "'");..  
1e390 73 74 72 63 61 74 20 28 73 71 6c 2c 20 78 74 61  strcat (sql, xta
1e3a0 62 6c 65 29 3b 0a 09 20 20 73 74 72 63 61 74 20  ble);..  strcat 
1e3b0 28 73 71 6c 2c 20 22 27 2c 20 27 47 65 6f 6d 65  (sql, "', 'Geome
1e3c0 74 72 79 27 2c 20 30 29 22 29 3b 0a 09 20 20 72  try', 0)");..  r
1e3d0 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
1e3e0 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  c (sqlite, sql, 
1e3f0 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
1e400 4d 73 67 29 3b 0a 09 20 20 69 66 20 28 72 65 74  Msg);..  if (ret
1e410 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09   != SQLITE_OK)..
1e420 20 20 20 20 7b 0a 09 09 73 70 61 74 69 61 6c 69      {...spatiali
1e430 74 65 5f 65 20 28 22 52 65 67 69 73 74 65 72 56  te_e ("RegisterV
1e440 69 72 74 75 61 6c 47 65 6f 6d 65 74 72 79 28 29  irtualGeometry()
1e450 20 65 72 72 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e   error: \"%s\"\n
1e460 22 2c 0a 09 09 09 20 20 20 20 20 20 65 72 72 4d  ",....      errM
1e470 73 67 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 66  sg);...sqlite3_f
1e480 72 65 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 09  ree (errMsg);...
1e490 72 65 74 75 72 6e 20 30 3b 0a 09 20 20 20 20 7d  return 0;..    }
1e4a0 0a 09 20 20 2f 2a 20 69 6e 73 65 72 74 69 6e 67  ..  /* inserting
1e4b0 20 61 20 72 6f 77 20 69 6e 74 6f 20 47 45 4f 4d   a row into GEOM
1e4c0 45 54 52 59 5f 43 4f 4c 55 4d 4e 53 5f 53 54 41  ETRY_COLUMNS_STA
1e4d0 54 49 53 54 49 43 53 20 2a 2f 0a 09 20 20 73 74  TISTICS */..  st
1e4e0 72 63 70 79 20 28 73 71 6c 2c 0a 09 09 20 20 22  rcpy (sql,...  "
1e4f0 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
1e500 45 20 49 4e 54 4f 20 76 69 72 74 73 5f 67 65 6f  E INTO virts_geo
1e510 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 5f 73 74  metry_columns_st
1e520 61 74 69 73 74 69 63 73 20 28 76 69 72 74 5f 6e  atistics (virt_n
1e530 61 6d 65 2c 20 76 69 72 74 5f 67 65 6f 6d 65 74  ame, virt_geomet
1e540 72 79 29 20 22 29 3b 0a 09 20 20 73 74 72 63 61  ry) ");..  strca
1e550 74 20 28 73 71 6c 2c 20 22 56 41 4c 55 45 53 20  t (sql, "VALUES 
1e560 28 22 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28  (");..  strcat (
1e570 73 71 6c 2c 20 22 27 22 29 3b 0a 09 20 20 73 74  sql, "'");..  st
1e580 72 63 61 74 20 28 73 71 6c 2c 20 78 74 61 62 6c  rcat (sql, xtabl
1e590 65 29 3b 0a 09 20 20 73 74 72 63 61 74 20 28 73  e);..  strcat (s
1e5a0 71 6c 2c 20 22 27 2c 20 27 47 65 6f 6d 65 74 72  ql, "', 'Geometr
1e5b0 79 27 29 22 29 3b 0a 09 20 20 72 65 74 20 3d 20  y')");..  ret = 
1e5c0 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71  sqlite3_exec (sq
1e5d0 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c  lite, sql, NULL,
1e5e0 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b   NULL, &errMsg);
1e5f0 0a 09 20 20 69 66 20 28 72 65 74 20 21 3d 20 53  ..  if (ret != S
1e600 51 4c 49 54 45 5f 4f 4b 29 0a 09 20 20 20 20 7b  QLITE_OK)..    {
1e610 0a 09 09 73 70 61 74 69 61 6c 69 74 65 5f 65 20  ...spatialite_e 
1e620 28 22 52 65 67 69 73 74 65 72 56 69 72 74 75 61  ("RegisterVirtua
1e630 6c 47 65 6f 6d 65 74 72 79 28 29 20 65 72 72 6f  lGeometry() erro
1e640 72 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 0a 09 09  r: \"%s\"\n",...
1e650 09 20 20 20 20 20 20 65 72 72 4d 73 67 29 3b 0a  .      errMsg);.
1e660 09 09 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28  ..sqlite3_free (
1e670 65 72 72 4d 73 67 29 3b 0a 09 09 72 65 74 75 72  errMsg);...retur
1e680 6e 20 30 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20  n 0;..    }.    
1e690 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31    }.    return 1
1e6a0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
1e6b0 0a 66 6e 63 74 5f 52 65 67 69 73 74 65 72 56 69  .fnct_RegisterVi
1e6c0 72 74 75 61 6c 47 65 6f 6d 65 74 72 79 20 28 73  rtualGeometry (s
1e6d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1e6e0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
1e6f0 67 63 2c 0a 09 09 09 20 20 20 20 20 20 73 71 6c  gc,....      sql
1e700 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
1e710 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
1e720 63 74 69 6f 6e 3a 0a 2f 20 52 65 67 69 73 74 65  ction:./ Registe
1e730 72 56 69 72 74 75 61 6c 47 65 6f 6d 65 74 72 79  rVirtualGeometry
1e740 28 74 61 62 6c 65 29 0a 2f 0a 2f 20 69 6e 73 65  (table)././ inse
1e750 72 74 2f 75 70 64 61 74 65 73 20 54 41 42 4c 45  rt/updates TABLE
1e760 2e 43 4f 4c 55 4d 4e 20 69 6e 74 6f 20 74 68 65  .COLUMN into the
1e770 20 53 70 61 74 69 61 6c 20 4d 65 74 61 44 61 74   Spatial MetaDat
1e780 61 20 5b 56 69 72 74 75 61 6c 20 54 61 62 6c 65  a [Virtual Table
1e790 5d 0a 2f 20 72 65 74 75 72 6e 73 20 31 20 6f 6e  ]./ returns 1 on
1e7a0 20 73 75 63 63 65 73 73 0a 2f 20 30 20 6f 6e 20   success./ 0 on 
1e7b0 66 61 69 6c 75 72 65 0a 2a 2f 0a 20 20 20 20 63  failure.*/.    c
1e7c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1e7d0 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20 63  ar *table;.    c
1e7e0 68 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b 0a 20  har sql[1024];. 
1e7f0 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69     sqlite3 *sqli
1e800 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  te = sqlite3_con
1e810 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28  text_db_handle (
1e820 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41  context);.    GA
1e830 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
1e840 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
1e850 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
1e860 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
1e870 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
1e880 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20 7b  TE_TEXT).      {
1e890 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
1e8a0 0a 09 20 20 20 20 20 20 28 22 52 65 67 69 73 74  ..      ("Regist
1e8b0 65 72 56 69 72 74 75 61 6c 47 65 6f 6d 65 74 72  erVirtualGeometr
1e8c0 79 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d  y() error: argum
1e8d0 65 6e 74 20 31 20 5b 74 61 62 6c 65 5f 6e 61 6d  ent 1 [table_nam
1e8e0 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65  e] is not of the
1e8f0 20 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29   String type\n")
1e900 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ;..  sqlite3_res
1e910 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
1e920 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  , 0);..  return;
1e930 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 74 61 62  .      }.    tab
1e940 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  le = sqlite3_val
1e950 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d  ue_text (argv[0]
1e960 29 3b 0a 20 20 20 20 69 66 20 28 21 72 65 67 69  );.    if (!regi
1e970 73 74 65 72 56 69 72 74 75 61 6c 20 28 73 71 6c  sterVirtual (sql
1e980 69 74 65 2c 20 28 63 68 61 72 20 2a 29 20 74 61  ite, (char *) ta
1e990 62 6c 65 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f  ble))..goto erro
1e9a0 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  r;.    sqlite3_r
1e9b0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
1e9c0 78 74 2c 20 31 29 3b 0a 20 20 20 20 73 74 72 63  xt, 1);.    strc
1e9d0 70 79 20 28 73 71 6c 2c 20 22 56 69 72 74 75 61  py (sql, "Virtua
1e9e0 6c 20 47 65 6f 6d 65 74 72 79 20 73 75 63 63 65  l Geometry succe
1e9f0 73 73 66 75 6c 6c 79 20 72 65 67 69 73 74 65 72  ssfully register
1ea00 65 64 22 29 3b 0a 20 20 20 20 75 70 64 61 74 65  ed");.    update
1ea10 53 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f 72  SpatiaLiteHistor
1ea20 79 20 28 73 71 6c 69 74 65 2c 20 28 63 6f 6e 73  y (sqlite, (cons
1ea30 74 20 63 68 61 72 20 2a 29 20 74 61 62 6c 65 2c  t char *) table,
1ea40 20 22 47 65 6f 6d 65 74 72 79 22 2c 20 73 71 6c   "Geometry", sql
1ea50 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
1ea60 20 65 72 72 6f 72 3a 0a 20 20 20 20 73 70 61 74   error:.    spat
1ea70 69 61 6c 69 74 65 5f 65 20 28 22 52 65 67 69 73  ialite_e ("Regis
1ea80 74 65 72 56 69 72 74 75 61 6c 47 65 6f 6d 65 74  terVirtualGeomet
1ea90 72 79 28 29 20 65 72 72 6f 72 5c 6e 22 29 3b 0a  ry() error\n");.
1eaa0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1eab0 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
1eac0 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b   0);.    return;
1ead0 0a 7d 0a 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  .}...static void
1eae0 0a 66 6e 63 74 5f 44 72 6f 70 56 69 72 74 75 61  .fnct_DropVirtua
1eaf0 6c 47 65 6f 6d 65 74 72 79 20 28 73 71 6c 69 74  lGeometry (sqlit
1eb00 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
1eb10 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a  text, int argc,.
1eb20 09 09 09 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ...  sqlite3_val
1eb30 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
1eb40 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
1eb50 20 44 72 6f 70 56 69 72 74 75 61 6c 47 65 6f 6d   DropVirtualGeom
1eb60 65 74 72 79 28 74 61 62 6c 65 29 0a 2f 0a 2f 20  etry(table)././ 
1eb70 72 65 6d 6f 76 65 73 20 54 41 42 4c 45 2e 43 4f  removes TABLE.CO
1eb80 4c 55 4d 4e 20 66 72 6f 6d 20 74 68 65 20 53 70  LUMN from the Sp
1eb90 61 74 69 61 6c 20 4d 65 74 61 44 61 74 61 20 61  atial MetaData a
1eba0 6e 64 20 44 52 4f 50 73 20 74 68 65 20 56 69 72  nd DROPs the Vir
1ebb0 74 75 61 6c 20 54 61 62 6c 65 0a 2f 20 72 65 74  tual Table./ ret
1ebc0 75 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65 73  urns 1 on succes
1ebd0 73 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72 65  s./ 0 on failure
1ebe0 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e  .*/.    const un
1ebf0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 61 62  signed char *tab
1ec00 6c 65 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c  le;.    char sql
1ec10 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72  [1024];.    char
1ec20 20 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b   *errMsg = NULL;
1ec30 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20  .    int ret;.  
1ec40 20 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b    char sqltable[
1ec50 31 30 32 34 5d 3b 0a 20 20 20 20 73 71 6c 69 74  1024];.    sqlit
1ec60 65 33 20 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c  e3 *sqlite = sql
1ec70 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
1ec80 68 61 6e 64 6c 65 20 28 63 6f 6e 74 65 78 74 29  handle (context)
1ec90 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
1eca0 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
1ecb0 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
1ecc0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
1ecd0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
1ece0 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29   != SQLITE_TEXT)
1ecf0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
1ed00 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20  ialite_e..      
1ed10 28 22 44 72 6f 70 56 69 72 74 75 61 6c 47 65 6f  ("DropVirtualGeo
1ed20 6d 65 74 72 79 28 29 20 65 72 72 6f 72 3a 20 61  metry() error: a
1ed30 72 67 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c 65  rgument 1 [table
1ed40 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66  _name] is not of
1ed50 20 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65   the String type
1ed60 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  \n");..  sqlite3
1ed70 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
1ed80 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74  text, 0);..  ret
1ed90 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
1eda0 20 74 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33   table = sqlite3
1edb0 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67  _value_text (arg
1edc0 76 5b 30 5d 29 3b 0a 20 20 20 20 73 74 72 63 70  v[0]);.    strcp
1edd0 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 28 63 68  y (sqltable, (ch
1ede0 61 72 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20  ar *) table);.  
1edf0 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69    clean_sql_stri
1ee00 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ng (sqltable);. 
1ee10 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c     sprintf (sql,
1ee20 0a 09 20 20 20 20 20 22 44 45 4c 45 54 45 20 46  ..     "DELETE F
1ee30 52 4f 4d 20 76 69 72 74 73 5f 67 65 6f 6d 65 74  ROM virts_geomet
1ee40 72 79 5f 63 6f 6c 75 6d 6e 73 20 57 48 45 52 45  ry_columns WHERE
1ee50 20 55 70 70 65 72 28 76 69 72 74 5f 6e 61 6d 65   Upper(virt_name
1ee60 29 20 3d 20 55 70 70 65 72 28 27 25 73 27 29 22  ) = Upper('%s')"
1ee70 2c 0a 09 20 20 20 20 20 73 71 6c 74 61 62 6c 65  ,..     sqltable
1ee80 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c  );.    ret = sql
1ee90 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74  ite3_exec (sqlit
1eea0 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55  e, sql, NULL, NU
1eeb0 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20  LL, &errMsg);.  
1eec0 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
1eed0 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72  ITE_OK)..goto er
1eee0 72 6f 72 3b 0a 20 20 20 20 73 74 72 63 70 79 20  ror;.    strcpy 
1eef0 28 73 71 6c 74 61 62 6c 65 2c 20 28 63 68 61 72  (sqltable, (char
1ef00 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20 20 20   *) table);.    
1ef10 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
1ef20 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  l (sqltable);.  
1ef30 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
1ef40 22 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45  "DROP TABLE IF E
1ef50 58 49 53 54 53 20 25 73 22 2c 20 73 71 6c 74 61  XISTS %s", sqlta
1ef60 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  ble);.    ret = 
1ef70 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71  sqlite3_exec (sq
1ef80 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c  lite, sql, NULL,
1ef90 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b   NULL, &errMsg);
1efa0 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
1efb0 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f  SQLITE_OK)..goto
1efc0 20 65 72 72 6f 72 3b 0a 20 20 20 20 73 71 6c 69   error;.    sqli
1efd0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
1efe0 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 20  context, 1);.   
1eff0 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 56   strcpy (sql, "V
1f000 69 72 74 75 61 6c 20 47 65 6f 6d 65 74 72 79 20  irtual Geometry 
1f010 73 75 63 63 65 73 73 66 75 6c 6c 79 20 64 72 6f  successfully dro
1f020 70 70 65 64 22 29 3b 0a 20 20 20 20 75 70 64 61  pped");.    upda
1f030 74 65 53 70 61 74 69 61 4c 69 74 65 48 69 73 74  teSpatiaLiteHist
1f040 6f 72 79 20 28 73 71 6c 69 74 65 2c 20 28 63 6f  ory (sqlite, (co
1f050 6e 73 74 20 63 68 61 72 20 2a 29 20 74 61 62 6c  nst char *) tabl
1f060 65 2c 20 22 47 65 6f 6d 65 74 72 79 22 2c 20 73  e, "Geometry", s
1f070 71 6c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ql);.    return;
1f080 0a 20 20 65 72 72 6f 72 3a 0a 20 20 20 20 73 70  .  error:.    sp
1f090 61 74 69 61 6c 69 74 65 5f 65 20 28 22 44 72 6f  atialite_e ("Dro
1f0a0 70 56 69 72 74 75 61 6c 47 65 6f 6d 65 74 72 79  pVirtualGeometry
1f0b0 28 29 20 65 72 72 6f 72 3a 20 5c 22 25 73 5c 22  () error: \"%s\"
1f0c0 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b 0a 20 20  \n", errMsg);.  
1f0d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28    sqlite3_free (
1f0e0 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c  errMsg);.    sql
1f0f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
1f100 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
1f110 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61    return;.}..sta
1f120 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49 6e  tic void.fnct_In
1f130 69 74 46 44 4f 53 70 61 74 69 61 6c 4d 65 74 61  itFDOSpatialMeta
1f140 44 61 74 61 20 28 73 71 6c 69 74 65 33 5f 63 6f  Data (sqlite3_co
1f150 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
1f160 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 20   int argc,....  
1f170 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
1f180 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
1f190 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 49  QL function:./ I
1f1a0 6e 69 74 46 44 4f 53 70 61 74 69 61 6c 4d 65 74  nitFDOSpatialMet
1f1b0 61 44 61 74 61 28 76 6f 69 64 29 0a 2f 0a 2f 20  aData(void)././ 
1f1c0 63 72 65 61 74 65 73 20 74 68 65 20 46 44 4f 2d  creates the FDO-
1f1d0 73 74 79 6c 65 64 20 53 50 41 54 49 41 4c 5f 52  styled SPATIAL_R
1f1e0 45 46 5f 53 59 53 20 61 6e 64 20 47 45 4f 4d 45  EF_SYS and GEOME
1f1f0 54 52 59 5f 43 4f 4c 55 4d 4e 53 20 74 61 62 6c  TRY_COLUMNS tabl
1f200 65 73 0a 2f 20 72 65 74 75 72 6e 73 20 31 20 6f  es./ returns 1 o
1f210 6e 20 73 75 63 63 65 73 73 0a 2f 20 30 20 6f 6e  n success./ 0 on
1f220 20 66 61 69 6c 75 72 65 0a 2a 2f 0a 20 20 20 20   failure.*/.    
1f230 63 68 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b 0a  char sql[1024];.
1f240 20 20 20 20 63 68 61 72 20 2a 65 72 72 4d 73 67      char *errMsg
1f250 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74   = NULL;.    int
1f260 20 72 65 74 3b 0a 20 20 20 20 73 71 6c 69 74 65   ret;.    sqlite
1f270 33 20 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c 69  3 *sqlite = sqli
1f280 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
1f290 61 6e 64 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b  andle (context);
1f2a0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
1f2b0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
1f2c0 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 2f 2a 20 63 72  CL_LINE */./* cr
1f2d0 65 61 74 69 6e 67 20 74 68 65 20 53 50 41 54 49  eating the SPATI
1f2e0 41 4c 5f 52 45 46 5f 53 59 53 20 74 61 62 6c 65  AL_REF_SYS table
1f2f0 73 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20  s */.    strcpy 
1f300 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54 41  (sql, "CREATE TA
1f310 42 4c 45 20 73 70 61 74 69 61 6c 5f 72 65 66 5f  BLE spatial_ref_
1f320 73 79 73 20 28 5c 6e 22 29 3b 0a 20 20 20 20 73  sys (\n");.    s
1f330 74 72 63 61 74 20 28 73 71 6c 2c 20 22 73 72 69  trcat (sql, "sri
1f340 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  d INTEGER PRIMAR
1f350 59 20 4b 45 59 2c 5c 6e 22 29 3b 0a 20 20 20 20  Y KEY,\n");.    
1f360 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 61 75  strcat (sql, "au
1f370 74 68 5f 6e 61 6d 65 20 54 45 58 54 2c 5c 6e 22  th_name TEXT,\n"
1f380 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
1f390 71 6c 2c 20 22 61 75 74 68 5f 73 72 69 64 20 49  ql, "auth_srid I
1f3a0 4e 54 45 47 45 52 2c 5c 6e 22 29 3b 0a 20 20 20  NTEGER,\n");.   
1f3b0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 73   strcat (sql, "s
1f3c0 72 74 65 78 74 20 54 45 58 54 29 22 29 3b 0a 20  rtext TEXT)");. 
1f3d0 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33     ret = sqlite3
1f3e0 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73  _exec (sqlite, s
1f3f0 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  ql, NULL, NULL, 
1f400 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  &errMsg);.    if
1f410 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
1f420 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b  OK)..goto error;
1f430 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65  ./* creating the
1f440 20 47 45 4f 4d 45 54 52 59 5f 43 4f 4c 55 4d 4e   GEOMETRY_COLUMN
1f450 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 73   tables */.    s
1f460 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43 52 45  trcpy (sql, "CRE
1f470 41 54 45 20 54 41 42 4c 45 20 67 65 6f 6d 65 74  ATE TABLE geomet
1f480 72 79 5f 63 6f 6c 75 6d 6e 73 20 28 5c 6e 22 29  ry_columns (\n")
1f490 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
1f4a0 6c 2c 20 22 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  l, "f_table_name
1f4b0 20 54 45 58 54 2c 5c 6e 22 29 3b 0a 20 20 20 20   TEXT,\n");.    
1f4c0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66 5f  strcat (sql, "f_
1f4d0 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 20  geometry_column 
1f4e0 54 45 58 54 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  TEXT,\n");.    s
1f4f0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 67 65 6f  trcat (sql, "geo
1f500 6d 65 74 72 79 5f 74 79 70 65 20 49 4e 54 45 47  metry_type INTEG
1f510 45 52 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  ER,\n");.    str
1f520 63 61 74 20 28 73 71 6c 2c 20 22 63 6f 6f 72 64  cat (sql, "coord
1f530 5f 64 69 6d 65 6e 73 69 6f 6e 20 49 4e 54 45 47  _dimension INTEG
1f540 45 52 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  ER,\n");.    str
1f550 63 61 74 20 28 73 71 6c 2c 20 22 73 72 69 64 20  cat (sql, "srid 
1f560 49 4e 54 45 47 45 52 2c 5c 6e 22 29 3b 0a 20 20  INTEGER,\n");.  
1f570 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
1f580 67 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61 74 20  geometry_format 
1f590 54 45 58 54 29 22 29 3b 0a 20 20 20 20 72 65 74  TEXT)");.    ret
1f5a0 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
1f5b0 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
1f5c0 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
1f5d0 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
1f5e0 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67  != SQLITE_OK)..g
1f5f0 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 73  oto error;.    s
1f600 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
1f610 74 20 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a  t (context, 1);.
1f620 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 65 72      return;.  er
1f630 72 6f 72 3a 0a 20 20 20 20 73 70 61 74 69 61 6c  ror:.    spatial
1f640 69 74 65 5f 65 20 28 22 49 6e 69 74 46 44 4f 53  ite_e ("InitFDOS
1f650 70 61 74 69 61 4d 65 74 61 44 61 74 61 28 29 20  patiaMetaData() 
1f660 65 72 72 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e 22  error: \"%s\"\n"
1f670 2c 20 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73  , errMsg);.    s
1f680 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72  qlite3_free (err
1f690 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Msg);.    sqlite
1f6a0 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
1f6b0 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 72  ntext, 0);.    r
1f6c0 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63  eturn;.}..static
1f6d0 20 69 6e 74 0a 72 65 63 6f 76 65 72 46 44 4f 47   int.recoverFDOG
1f6e0 65 6f 6d 43 6f 6c 75 6d 6e 20 28 73 71 6c 69 74  eomColumn (sqlit
1f6f0 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e  e3 * sqlite, con
1f700 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1f710 20 2a 74 61 62 6c 65 2c 0a 09 09 20 20 20 20 20   *table,...     
1f720 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1f730 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 2c 20 69 6e  char *column, in
1f740 74 20 78 74 79 70 65 2c 20 69 6e 74 20 73 72 69  t xtype, int sri
1f750 64 29 0a 7b 0a 2f 2a 20 63 68 65 63 6b 73 20 69  d).{./* checks i
1f760 66 20 54 41 42 4c 45 2e 43 4f 4c 55 4d 4e 20 65  f TABLE.COLUMN e
1f770 78 69 73 74 73 20 61 6e 64 20 68 61 73 20 74 68  xists and has th
1f780 65 20 72 65 71 75 69 72 65 64 20 66 65 61 74 75  e required featu
1f790 72 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f  res */.    int o
1f7a0 6b 20 3d 20 31 3b 0a 20 20 20 20 63 68 61 72 20  k = 1;.    char 
1f7b0 73 71 6c 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69  sql[1024];.    i
1f7c0 6e 74 20 74 79 70 65 3b 0a 20 20 20 20 73 71 6c  nt type;.    sql
1f7d0 69 74 65 33 5f 73 74 6d 74 20 2a 73 74 6d 74 3b  ite3_stmt *stmt;
1f7e0 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
1f7f0 6c 50 74 72 20 67 65 6f 6d 3b 0a 20 20 20 20 63  lPtr geom;.    c
1f800 6f 6e 73 74 20 76 6f 69 64 20 2a 62 6c 6f 62 5f  onst void *blob_
1f810 76 61 6c 75 65 3b 0a 20 20 20 20 69 6e 74 20 6c  value;.    int l
1f820 65 6e 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b  en;.    int ret;
1f830 0a 20 20 20 20 69 6e 74 20 69 5f 63 6f 6c 3b 0a  .    int i_col;.
1f840 20 20 20 20 63 68 61 72 20 78 63 6f 6c 75 6d 6e      char xcolumn
1f850 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72  [1024];.    char
1f860 20 78 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20   xtable[1024];. 
1f870 20 20 20 73 74 72 63 70 79 20 28 78 63 6f 6c 75     strcpy (xcolu
1f880 6d 6e 2c 20 28 63 68 61 72 20 2a 29 20 63 6f 6c  mn, (char *) col
1f890 75 6d 6e 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65  umn);.    double
1f8a0 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 78 63 6f  _quoted_sql (xco
1f8b0 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 74 72 63 70  lumn);.    strcp
1f8c0 79 20 28 78 74 61 62 6c 65 2c 20 28 63 68 61 72  y (xtable, (char
1f8d0 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20 20 20   *) table);.    
1f8e0 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
1f8f0 6c 20 28 78 74 61 62 6c 65 29 3b 0a 20 20 20 20  l (xtable);.    
1f900 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 53  sprintf (sql, "S
1f910 45 4c 45 43 54 20 25 73 20 46 52 4f 4d 20 25 73  ELECT %s FROM %s
1f920 22 2c 20 78 63 6f 6c 75 6d 6e 2c 20 78 74 61 62  ", xcolumn, xtab
1f930 6c 65 29 3b 0a 2f 2a 20 63 6f 6d 70 69 6c 69 6e  le);./* compilin
1f940 67 20 53 51 4c 20 70 72 65 70 61 72 65 64 20 73  g SQL prepared s
1f950 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
1f960 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 70 72  ret = sqlite3_pr
1f970 65 70 61 72 65 5f 76 32 20 28 73 71 6c 69 74 65  epare_v2 (sqlite
1f980 2c 20 73 71 6c 2c 20 73 74 72 6c 65 6e 20 28 73  , sql, strlen (s
1f990 71 6c 29 2c 20 26 73 74 6d 74 2c 20 4e 55 4c 4c  ql), &stmt, NULL
1f9a0 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
1f9b0 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
1f9c0 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
1f9d0 74 65 5f 65 20 28 22 72 65 63 6f 76 65 72 46 44  te_e ("recoverFD
1f9e0 4f 47 65 6f 6d 43 6f 6c 75 6d 6e 3a 20 65 72 72  OGeomColumn: err
1f9f0 6f 72 20 25 64 20 5c 22 25 73 5c 22 5c 6e 22 2c  or %d \"%s\"\n",
1fa00 0a 09 09 09 73 71 6c 69 74 65 33 5f 65 72 72 63  ....sqlite3_errc
1fa10 6f 64 65 20 28 73 71 6c 69 74 65 29 2c 20 73 71  ode (sqlite), sq
1fa20 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71  lite3_errmsg (sq
1fa30 6c 69 74 65 29 29 3b 0a 09 20 20 72 65 74 75 72  lite));..  retur
1fa40 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
1fa50 20 77 68 69 6c 65 20 28 31 29 0a 20 20 20 20 20   while (1).     
1fa60 20 7b 0a 09 20 20 2f 2a 20 73 63 72 6f 6c 6c 69   {..  /* scrolli
1fa70 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ng the result se
1fa80 74 20 72 6f 77 73 20 2a 2f 0a 09 20 20 72 65 74  t rows */..  ret
1fa90 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 20   = sqlite3_step 
1faa0 28 73 74 6d 74 29 3b 0a 09 20 20 69 66 20 28 72  (stmt);..  if (r
1fab0 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 44 4f 4e  et == SQLITE_DON
1fac0 45 29 0a 09 20 20 20 20 20 20 62 72 65 61 6b 3b  E)..      break;
1fad0 09 09 2f 2a 20 65 6e 64 20 6f 66 20 72 65 73 75  ../* end of resu
1fae0 6c 74 20 73 65 74 20 2a 2f 0a 09 20 20 69 66 20  lt set */..  if 
1faf0 28 72 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 52  (ret == SQLITE_R
1fb00 4f 57 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20  OW)..    {.../* 
1fb10 63 68 65 63 6b 69 6e 67 20 47 65 6f 6d 65 74 72  checking Geometr
1fb20 79 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 09 09  y features */...
1fb30 67 65 6f 6d 20 3d 20 4e 55 4c 4c 3b 0a 09 09 66  geom = NULL;...f
1fb40 6f 72 20 28 69 5f 63 6f 6c 20 3d 20 30 3b 20 69  or (i_col = 0; i
1fb50 5f 63 6f 6c 20 3c 20 73 71 6c 69 74 65 33 5f 63  _col < sqlite3_c
1fb60 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 20 28 73 74 6d  olumn_count (stm
1fb70 74 29 3b 20 69 5f 63 6f 6c 2b 2b 29 0a 09 09 20  t); i_col++)... 
1fb80 20 7b 0a 09 09 20 20 20 20 20 20 69 66 20 28 73   {...      if (s
1fb90 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
1fba0 70 65 20 28 73 74 6d 74 2c 20 69 5f 63 6f 6c 29  pe (stmt, i_col)
1fbb0 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
1fbc0 0a 09 09 09 20 20 6f 6b 20 3d 20 30 3b 0a 09 09  ....  ok = 0;...
1fbd0 20 20 20 20 20 20 65 6c 73 65 0a 09 09 09 7b 0a        else....{.
1fbe0 09 09 09 20 20 20 20 62 6c 6f 62 5f 76 61 6c 75  ...    blob_valu
1fbf0 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
1fc00 6d 6e 5f 62 6c 6f 62 20 28 73 74 6d 74 2c 20 69  mn_blob (stmt, i
1fc10 5f 63 6f 6c 29 3b 0a 09 09 09 20 20 20 20 6c 65  _col);....    le
1fc20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  n = sqlite3_colu
1fc30 6d 6e 5f 62 79 74 65 73 20 28 73 74 6d 74 2c 20  mn_bytes (stmt, 
1fc40 69 5f 63 6f 6c 29 3b 0a 09 09 09 20 20 20 20 67  i_col);....    g
1fc50 65 6f 6d 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  eom = gaiaFromSp
1fc60 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
1fc70 28 62 6c 6f 62 5f 76 61 6c 75 65 2c 20 6c 65 6e  (blob_value, len
1fc80 29 3b 0a 09 09 09 20 20 20 20 69 66 20 28 21 67  );....    if (!g
1fc90 65 6f 6d 29 0a 09 09 09 09 6f 6b 20 3d 20 30 3b  eom).....ok = 0;
1fca0 0a 09 09 09 20 20 20 20 65 6c 73 65 0a 09 09 09  ....    else....
1fcb0 20 20 20 20 20 20 7b 0a 09 09 09 09 20 20 69 66        {.....  if
1fcc0 20 28 67 65 6f 6d 2d 3e 53 72 69 64 20 21 3d 20   (geom->Srid != 
1fcd0 73 72 69 64 29 0a 09 09 09 09 20 20 20 20 20 20  srid).....      
1fce0 6f 6b 20 3d 20 30 3b 0a 09 09 09 09 20 20 74 79  ok = 0;.....  ty
1fcf0 70 65 20 3d 20 67 61 69 61 47 65 6f 6d 65 74 72  pe = gaiaGeometr
1fd00 79 54 79 70 65 20 28 67 65 6f 6d 29 3b 0a 09 09  yType (geom);...
1fd10 09 09 20 20 69 66 20 28 78 74 79 70 65 20 3d 3d  ..  if (xtype ==
1fd20 20 74 79 70 65 29 0a 09 09 09 09 20 20 20 20 20   type).....     
1fd30 20 3b 0a 09 09 09 09 20 20 65 6c 73 65 0a 09 09   ;.....  else...
1fd40 09 09 20 20 20 20 20 20 6f 6b 20 3d 20 30 3b 0a  ..      ok = 0;.
1fd50 09 09 09 09 20 20 67 61 69 61 46 72 65 65 47 65  ....  gaiaFreeGe
1fd60 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a 09  omColl (geom);..
1fd70 09 09 20 20 20 20 20 20 7d 0a 09 09 09 7d 0a 09  ..      }....}..
1fd80 09 20 20 7d 0a 09 20 20 20 20 7d 0a 09 20 20 69  .  }..    }..  i
1fd90 66 20 28 21 6f 6b 29 0a 09 20 20 20 20 20 20 62  f (!ok)..      b
1fda0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1fdb0 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
1fdc0 66 69 6e 61 6c 69 7a 65 20 28 73 74 6d 74 29 3b  finalize (stmt);
1fdd0 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
1fde0 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
1fdf0 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
1fe00 5f 65 20 28 22 72 65 63 6f 76 65 72 46 44 4f 47  _e ("recoverFDOG
1fe10 65 6f 6d 43 6f 6c 75 6d 6e 3a 20 65 72 72 6f 72  eomColumn: error
1fe20 20 25 64 20 5c 22 25 73 5c 22 5c 6e 22 2c 0a 09   %d \"%s\"\n",..
1fe30 09 09 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  ..sqlite3_errcod
1fe40 65 20 28 73 71 6c 69 74 65 29 2c 20 73 71 6c 69  e (sqlite), sqli
1fe50 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71 6c 69  te3_errmsg (sqli
1fe60 74 65 29 29 3b 0a 09 20 20 72 65 74 75 72 6e 20  te));..  return 
1fe70 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72  0;.      }.    r
1fe80 65 74 75 72 6e 20 6f 6b 3b 0a 7d 0a 0a 73 74 61  eturn ok;.}..sta
1fe90 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41 64  tic void.fnct_Ad
1fea0 64 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75  dFDOGeometryColu
1feb0 6d 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  mn (sqlite3_cont
1fec0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
1fed0 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 20 20 73  nt argc,....   s
1fee0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
1fef0 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
1ff00 75 6e 63 74 69 6f 6e 3a 0a 2f 20 41 64 64 46 44  unction:./ AddFD
1ff10 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28  OGeometryColumn(
1ff20 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e 2c 20 73  table, column, s
1ff30 72 69 64 2c 20 67 65 6f 6d 65 74 72 79 5f 74 79  rid, geometry_ty
1ff40 70 65 20 2c 20 64 69 6d 65 6e 73 69 6f 6e 2c 20  pe , dimension, 
1ff50 67 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61 74 20  geometry_format 
1ff60 29 0a 2f 0a 2f 20 63 72 65 61 74 65 73 20 61 20  )././ creates a 
1ff70 6e 65 77 20 43 4f 4c 55 4d 4e 20 6f 66 20 67 69  new COLUMN of gi
1ff80 76 65 6e 20 54 59 50 45 20 69 6e 74 6f 20 54 41  ven TYPE into TA
1ff90 42 4c 45 0a 2f 20 72 65 74 75 72 6e 73 20 31 20  BLE./ returns 1 
1ffa0 6f 6e 20 73 75 63 63 65 73 73 0a 2f 20 30 20 6f  on success./ 0 o
1ffb0 6e 20 66 61 69 6c 75 72 65 0a 2a 2f 0a 20 20 20  n failure.*/.   
1ffc0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 61 62   const char *tab
1ffd0 6c 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  le;.    const ch
1ffe0 61 72 20 2a 63 6f 6c 75 6d 6e 3b 0a 20 20 20 20  ar *column;.    
1fff0 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6f 72 6d  const char *form
20000 61 74 3b 0a 20 20 20 20 63 68 61 72 20 78 66 6f  at;.    char xfo
20010 72 6d 61 74 5b 36 34 5d 3b 0a 20 20 20 20 69 6e  rmat[64];.    in
20020 74 20 74 79 70 65 3b 0a 20 20 20 20 69 6e 74 20  t type;.    int 
20030 73 72 69 64 20 3d 20 2d 31 3b 0a 20 20 20 20 69  srid = -1;.    i
20040 6e 74 20 64 69 6d 65 6e 73 69 6f 6e 20 3d 20 32  nt dimension = 2
20050 3b 0a 20 20 20 20 63 68 61 72 20 64 75 6d 6d 79  ;.    char dummy
20060 5b 33 32 5d 3b 0a 20 20 20 20 63 68 61 72 20 73  [32];.    char s
20070 71 6c 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68  ql[1024];.    ch
20080 61 72 20 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c  ar *errMsg = NUL
20090 4c 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a  L;.    int ret;.
200a0 20 20 20 20 63 68 61 72 20 2a 2a 72 65 73 75 6c      char **resul
200b0 74 73 3b 0a 20 20 20 20 69 6e 74 20 72 6f 77 73  ts;.    int rows
200c0 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6c 75 6d 6e  ;.    int column
200d0 73 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  s;.    int i;.  
200e0 20 20 63 68 61 72 20 74 62 6c 6e 61 6d 65 5b 32    char tblname[2
200f0 35 36 5d 3b 0a 20 20 20 20 63 68 61 72 20 78 74  56];.    char xt
20100 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20  able[1024];.    
20110 63 68 61 72 20 78 63 6f 6c 75 6d 6e 5b 31 30 32  char xcolumn[102
20120 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c  4];.    char sql
20130 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20  table[1024];.   
20140 20 63 68 61 72 20 73 71 6c 63 6f 6c 75 6d 6e 5b   char sqlcolumn[
20150 31 30 32 34 5d 3b 0a 20 20 20 20 73 71 6c 69 74  1024];.    sqlit
20160 65 33 20 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c  e3 *sqlite = sql
20170 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
20180 68 61 6e 64 6c 65 20 28 63 6f 6e 74 65 78 74 29  handle (context)
20190 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
201a0 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
201b0 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
201c0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
201d0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
201e0 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29   != SQLITE_TEXT)
201f0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
20200 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20  ialite_e..      
20210 28 22 41 64 64 46 44 4f 47 65 6f 6d 65 74 72 79  ("AddFDOGeometry
20220 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20  Column() error: 
20230 61 72 67 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c  argument 1 [tabl
20240 65 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f  e_name] is not o
20250 66 20 74 68 65 20 53 74 72 69 6e 67 20 74 79 70  f the String typ
20260 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65  e\n");..  sqlite
20270 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
20280 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
20290 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
202a0 20 20 74 61 62 6c 65 20 3d 20 28 63 6f 6e 73 74    table = (const
202b0 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
202c0 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67  _value_text (arg
202d0 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 73  v[0]);.    if (s
202e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
202f0 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53  e (argv[1]) != S
20300 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20  QLITE_TEXT).    
20310 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
20320 65 5f 65 0a 09 20 20 20 20 20 20 28 22 41 64 64  e_e..      ("Add
20330 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d  FDOGeometryColum
20340 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d  n() error: argum
20350 65 6e 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61  ent 2 [column_na
20360 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68  me] is not of th
20370 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22  e String type\n"
20380 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
20390 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
203a0 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e  t, 0);..  return
203b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 63 6f  ;.      }.    co
203c0 6c 75 6d 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68  lumn = (const ch
203d0 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
203e0 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 31  lue_text (argv[1
203f0 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c 69  ]);.    if (sqli
20400 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
20410 61 72 67 76 5b 32 5d 29 20 21 3d 20 53 51 4c 49  argv[2]) != SQLI
20420 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20  TE_INTEGER).    
20430 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
20440 65 5f 65 0a 09 20 20 20 20 20 20 28 22 41 64 64  e_e..      ("Add
20450 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d  FDOGeometryColum
20460 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d  n() error: argum
20470 65 6e 74 20 33 20 5b 53 52 49 44 5d 20 69 73 20  ent 3 [SRID] is 
20480 6e 6f 74 20 6f 66 20 74 68 65 20 49 6e 74 65 67  not of the Integ
20490 65 72 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20  er type\n");..  
204a0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
204b0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  nt (context, 0);
204c0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
204d0 20 20 7d 0a 20 20 20 20 73 72 69 64 20 3d 20 73    }.    srid = s
204e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
204f0 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20   (argv[2]);.    
20500 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
20510 65 5f 74 79 70 65 20 28 61 72 67 76 5b 33 5d 29  e_type (argv[3])
20520 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   != SQLITE_INTEG
20530 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  ER).      {..  s
20540 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20  patialite_e..   
20550 20 20 20 28 22 41 64 64 46 44 4f 47 65 6f 6d 65     ("AddFDOGeome
20560 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f  tryColumn() erro
20570 72 3a 20 61 72 67 75 6d 65 6e 74 20 34 20 5b 67  r: argument 4 [g
20580 65 6f 6d 65 74 72 79 5f 74 79 70 65 5d 20 69 73  eometry_type] is
20590 20 6e 6f 74 20 6f 66 20 74 68 65 20 49 6e 74 65   not of the Inte
205a0 67 65 72 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20  ger type\n");.. 
205b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
205c0 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29  int (context, 0)
205d0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
205e0 20 20 20 7d 0a 20 20 20 20 74 79 70 65 20 3d 20     }.    type = 
205f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
20600 74 20 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20  t (argv[3]);.   
20610 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
20620 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 34 5d  ue_type (argv[4]
20630 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  ) != SQLITE_INTE
20640 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  GER).      {..  
20650 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20  spatialite_e..  
20660 20 20 20 20 28 22 41 64 64 46 44 4f 47 65 6f 6d      ("AddFDOGeom
20670 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72  etryColumn() err
20680 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 35 20 5b  or: argument 5 [
20690 64 69 6d 65 6e 73 69 6f 6e 5d 20 69 73 20 6e 6f  dimension] is no
206a0 74 20 6f 66 20 74 68 65 20 49 6e 74 65 67 65 72  t of the Integer
206b0 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20 73 71   type\n");..  sq
206c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
206d0 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
206e0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
206f0 7d 0a 20 20 20 20 64 69 6d 65 6e 73 69 6f 6e 20  }.    dimension 
20700 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
20710 69 6e 74 20 28 61 72 67 76 5b 34 5d 29 3b 0a 20  int (argv[4]);. 
20720 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
20730 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
20740 35 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 54 45  5]) != SQLITE_TE
20750 58 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  XT).      {..  s
20760 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20  patialite_e..   
20770 20 20 20 28 22 41 64 64 46 44 4f 47 65 6f 6d 65     ("AddFDOGeome
20780 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f  tryColumn() erro
20790 72 3a 20 61 72 67 75 6d 65 6e 74 20 36 20 5b 67  r: argument 6 [g
207a0 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61 74 5d 20  eometry_format] 
207b0 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 53 74  is not of the St
207c0 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a 09  ring type\n");..
207d0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
207e0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
207f0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
20800 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 6d 61 74      }.    format
20810 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
20820 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
20830 74 65 78 74 20 28 61 72 67 76 5b 35 5d 29 3b 0a  text (argv[5]);.
20840 20 20 20 20 69 66 20 28 74 79 70 65 20 3d 3d 0a      if (type ==.
20850 09 47 41 49 41 5f 50 4f 49 4e 54 0a 09 7c 7c 20  .GAIA_POINT..|| 
20860 74 79 70 65 20 3d 3d 0a 09 47 41 49 41 5f 4c 49  type ==..GAIA_LI
20870 4e 45 53 54 52 49 4e 47 0a 09 7c 7c 20 74 79 70  NESTRING..|| typ
20880 65 20 3d 3d 0a 09 47 41 49 41 5f 50 4f 4c 59 47  e ==..GAIA_POLYG
20890 4f 4e 0a 09 7c 7c 20 74 79 70 65 20 3d 3d 0a 09  ON..|| type ==..
208a0 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 0a  GAIA_MULTIPOINT.
208b0 09 7c 7c 20 74 79 70 65 20 3d 3d 0a 09 47 41 49  .|| type ==..GAI
208c0 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e  A_MULTILINESTRIN
208d0 47 0a 09 7c 7c 20 74 79 70 65 20 3d 3d 20 47 41  G..|| type == GA
208e0 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 20  IA_MULTIPOLYGON 
208f0 7c 7c 20 74 79 70 65 20 3d 3d 20 47 41 49 41 5f  || type == GAIA_
20900 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
20910 4f 4e 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a  ON)..;.    else.
20920 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
20930 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28  alite_e..      (
20940 22 41 64 64 46 44 4f 47 65 6f 6d 65 74 72 79 43  "AddFDOGeometryC
20950 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61  olumn() error: a
20960 72 67 75 6d 65 6e 74 20 34 20 5b 67 65 6f 6d 65  rgument 4 [geome
20970 74 72 79 5f 74 79 70 65 5d 20 68 61 73 20 61 6e  try_type] has an
20980 20 69 6c 6c 65 67 61 6c 20 76 61 6c 75 65 5c 6e   illegal value\n
20990 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72  ");..  sqlite3_r
209a0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
209b0 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72  xt, 0);..  retur
209c0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
209d0 66 20 28 64 69 6d 65 6e 73 69 6f 6e 20 3c 20 32  f (dimension < 2
209e0 20 7c 7c 20 64 69 6d 65 6e 73 69 6f 6e 20 3e 20   || dimension > 
209f0 34 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70  4).      {..  sp
20a00 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20  atialite_e..    
20a10 20 20 28 22 41 64 64 46 44 4f 47 65 6f 6d 65 74    ("AddFDOGeomet
20a20 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72  ryColumn() error
20a30 3a 20 61 72 67 75 6d 65 6e 74 20 35 20 5b 64 69  : argument 5 [di
20a40 6d 65 6e 73 69 6f 6e 5d 20 63 75 72 72 65 6e 74  mension] current
20a50 20 76 65 72 73 69 6f 6e 20 6f 6e 6c 79 20 61 63   version only ac
20a60 63 65 70 74 73 20 64 69 6d 65 6e 73 69 6f 6e 3d  cepts dimension=
20a70 32 2c 33 2c 34 5c 6e 22 29 3b 0a 09 20 20 73 71  2,3,4\n");..  sq
20a80 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
20a90 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
20aa0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
20ab0 7d 0a 20 20 20 20 69 66 20 28 73 74 72 63 61 73  }.    if (strcas
20ac0 65 63 6d 70 20 28 66 6f 72 6d 61 74 2c 20 22 57  ecmp (format, "W
20ad0 4b 54 22 29 20 3d 3d 20 30 29 0a 09 73 74 72 63  KT") == 0)..strc
20ae0 70 79 20 28 78 66 6f 72 6d 61 74 2c 20 22 57 4b  py (xformat, "WK
20af0 54 22 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66  T");.    else if
20b00 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 66 6f   (strcasecmp (fo
20b10 72 6d 61 74 2c 20 22 57 4b 42 22 29 20 3d 3d 20  rmat, "WKB") == 
20b20 30 29 0a 09 73 74 72 63 70 79 20 28 78 66 6f 72  0)..strcpy (xfor
20b30 6d 61 74 2c 20 22 57 4b 42 22 29 3b 0a 20 20 20  mat, "WKB");.   
20b40 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 61 73   else if (strcas
20b50 65 63 6d 70 20 28 66 6f 72 6d 61 74 2c 20 22 46  ecmp (format, "F
20b60 47 46 22 29 20 3d 3d 20 30 29 0a 09 73 74 72 63  GF") == 0)..strc
20b70 70 79 20 28 78 66 6f 72 6d 61 74 2c 20 22 46 47  py (xformat, "FG
20b80 46 22 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66  F");.    else if
20b90 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 66 6f   (strcasecmp (fo
20ba0 72 6d 61 74 2c 20 22 53 50 41 54 49 41 4c 49 54  rmat, "SPATIALIT
20bb0 45 22 29 20 3d 3d 20 30 29 0a 09 73 74 72 63 70  E") == 0)..strcp
20bc0 79 20 28 78 66 6f 72 6d 61 74 2c 20 22 53 50 41  y (xformat, "SPA
20bd0 54 49 41 4c 49 54 45 22 29 3b 0a 20 20 20 20 65  TIALITE");.    e
20be0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  lse.      {..  s
20bf0 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20  patialite_e..   
20c00 20 20 20 28 22 41 64 64 46 44 4f 47 65 6f 6d 65     ("AddFDOGeome
20c10 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f  tryColumn() erro
20c20 72 3a 20 61 72 67 75 6d 65 6e 74 20 36 20 5b 67  r: argument 6 [g
20c30 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61 74 5d 20  eometry_format] 
20c40 68 61 73 20 74 6f 20 62 65 20 6f 6e 65 20 6f 66  has to be one of
20c50 3a 20 57 4b 54 2c 57 4b 42 2c 46 47 46 2c 53 50  : WKT,WKB,FGF,SP
20c60 41 54 49 41 4c 49 54 45 5c 6e 22 29 3b 0a 09 20  ATIALITE\n");.. 
20c70 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
20c80 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29  int (context, 0)
20c90 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
20ca0 20 20 20 7d 0a 2f 2a 20 63 68 65 63 6b 69 6e 67     }./* checking
20cb0 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 65 78   if the table ex
20cc0 69 73 74 73 20 2a 2f 0a 20 20 20 20 73 74 72 63  ists */.    strc
20cd0 70 79 20 28 78 74 61 62 6c 65 2c 20 28 63 68 61  py (xtable, (cha
20ce0 72 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20 20  r *) table);.   
20cf0 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
20d00 71 6c 20 28 78 74 61 62 6c 65 29 3b 0a 20 20 20  ql (xtable);.   
20d10 20 73 74 72 63 70 79 20 28 78 63 6f 6c 75 6d 6e   strcpy (xcolumn
20d20 2c 20 28 63 68 61 72 20 2a 29 20 63 6f 6c 75 6d  , (char *) colum
20d30 6e 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  n);.    double_q
20d40 75 6f 74 65 64 5f 73 71 6c 20 28 78 63 6f 6c 75  uoted_sql (xcolu
20d50 6d 6e 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20  mn);.    strcpy 
20d60 28 73 71 6c 74 61 62 6c 65 2c 20 28 63 68 61 72  (sqltable, (char
20d70 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20 20 20   *) table);.    
20d80 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67  clean_sql_string
20d90 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20   (sqltable);.   
20da0 20 73 74 72 63 70 79 20 28 73 71 6c 63 6f 6c 75   strcpy (sqlcolu
20db0 6d 6e 2c 20 28 63 68 61 72 20 2a 29 20 63 6f 6c  mn, (char *) col
20dc0 75 6d 6e 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f  umn);.    clean_
20dd0 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 63  sql_string (sqlc
20de0 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 70 72 69  olumn);.    spri
20df0 6e 74 66 20 28 73 71 6c 2c 0a 09 20 20 20 20 20  ntf (sql,..     
20e00 22 53 45 4c 45 43 54 20 6e 61 6d 65 20 46 52 4f  "SELECT name FRO
20e10 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  M sqlite_master 
20e20 57 48 45 52 45 20 74 79 70 65 20 3d 20 27 74 61  WHERE type = 'ta
20e30 62 6c 65 27 20 41 4e 44 20 55 70 70 65 72 28 6e  ble' AND Upper(n
20e40 61 6d 65 29 20 3d 20 55 70 70 65 72 28 27 25 73  ame) = Upper('%s
20e50 27 29 22 2c 0a 09 20 20 20 20 20 73 71 6c 74 61  ')",..     sqlta
20e60 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  ble);.    ret = 
20e70 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
20e80 65 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  e (sqlite, sql, 
20e90 26 72 65 73 75 6c 74 73 2c 20 26 72 6f 77 73 2c  &results, &rows,
20ea0 20 26 63 6f 6c 75 6d 6e 73 2c 20 26 65 72 72 4d   &columns, &errM
20eb0 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74  sg);.    if (ret
20ec0 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20   != SQLITE_OK). 
20ed0 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61       {..  spatia
20ee0 6c 69 74 65 5f 65 20 28 22 41 64 64 46 44 4f 47  lite_e ("AddFDOG
20ef0 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 3a 20 5c  eometryColumn: \
20f00 22 25 73 5c 22 5c 6e 22 2c 20 65 72 72 4d 73 67  "%s\"\n", errMsg
20f10 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
20f20 65 65 20 28 65 72 72 4d 73 67 29 3b 0a 09 20 20  ee (errMsg);..  
20f30 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
20f40 20 20 20 20 2a 74 62 6c 6e 61 6d 65 20 3d 20 27      *tblname = '
20f50 5c 30 27 3b 0a 20 20 20 20 66 6f 72 20 28 69 20  \0';.    for (i 
20f60 3d 20 31 3b 20 69 20 3c 3d 20 72 6f 77 73 3b 20  = 1; i <= rows; 
20f70 69 2b 2b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  i++).      {..  
20f80 73 74 72 63 70 79 20 28 74 62 6c 6e 61 6d 65 2c  strcpy (tblname,
20f90 20 72 65 73 75 6c 74 73 5b 28 69 20 2a 20 63 6f   results[(i * co
20fa0 6c 75 6d 6e 73 29 5d 29 3b 0a 20 20 20 20 20 20  lumns)]);.      
20fb0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
20fc0 65 65 5f 74 61 62 6c 65 20 28 72 65 73 75 6c 74  ee_table (result
20fd0 73 29 3b 0a 20 20 20 20 69 66 20 28 2a 74 62 6c  s);.    if (*tbl
20fe0 6e 61 6d 65 20 3d 3d 20 27 5c 30 27 29 0a 20 20  name == '\0').  
20ff0 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c      {..  spatial
21000 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 41  ite_e..      ("A
21010 64 64 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c  ddFDOGeometryCol
21020 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 74 61 62  umn() error: tab
21030 6c 65 20 27 25 73 27 20 64 6f 65 73 20 6e 6f 74  le '%s' does not
21040 20 65 78 69 73 74 5c 6e 22 2c 0a 09 20 20 20 20   exist\n",..    
21050 20 20 20 74 61 62 6c 65 29 3b 0a 09 20 20 73 71     table);..  sq
21060 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
21070 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
21080 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
21090 7d 0a 2f 2a 20 74 72 79 69 6e 67 20 74 6f 20 61  }./* trying to a
210a0 64 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f  dd the column */
210b0 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
210c0 2c 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 22  , "ALTER TABLE "
210d0 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
210e0 71 6c 2c 20 78 74 61 62 6c 65 29 3b 0a 20 20 20  ql, xtable);.   
210f0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 20   strcat (sql, " 
21100 41 44 44 20 43 4f 4c 55 4d 4e 20 22 29 3b 0a 20  ADD COLUMN ");. 
21110 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
21120 78 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 74  xcolumn);.    st
21130 72 63 61 74 20 28 73 71 6c 2c 20 22 20 42 4c 4f  rcat (sql, " BLO
21140 42 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73  B");.    ret = s
21150 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
21160 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
21170 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a  NULL, &errMsg);.
21180 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53      if (ret != S
21190 51 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20  QLITE_OK)..goto 
211a0 65 72 72 6f 72 3b 0a 2f 2a 6f 6b 2c 20 69 6e 73  error;./*ok, ins
211b0 65 72 74 69 6e 67 20 69 6e 74 6f 20 67 65 6f 6d  erting into geom
211c0 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20 5b 46 44  etry_columns [FD
211d0 4f 20 53 70 61 74 69 61 6c 20 4d 65 74 61 64 61  O Spatial Metada
211e0 74 61 5d 20 2a 2f 0a 20 20 20 20 73 74 72 63 70  ta] */.    strcp
211f0 79 20 28 73 71 6c 2c 0a 09 20 20 20 20 22 49 4e  y (sql,..    "IN
21200 53 45 52 54 20 49 4e 54 4f 20 67 65 6f 6d 65 74  SERT INTO geomet
21210 72 79 5f 63 6f 6c 75 6d 6e 73 20 28 66 5f 74 61  ry_columns (f_ta
21220 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f 6d  ble_name, f_geom
21230 65 74 72 79 5f 63 6f 6c 75 6d 6e 2c 20 67 65 6f  etry_column, geo
21240 6d 65 74 72 79 5f 74 79 70 65 2c 20 22 29 3b 0a  metry_type, ");.
21250 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
21260 20 22 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f   "coord_dimensio
21270 6e 2c 20 73 72 69 64 2c 20 67 65 6f 6d 65 74 72  n, srid, geometr
21280 79 5f 66 6f 72 6d 61 74 29 20 56 41 4c 55 45 53  y_format) VALUES
21290 20 28 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74   (");.    strcat
212a0 20 28 73 71 6c 2c 20 22 27 22 29 3b 0a 20 20 20   (sql, "'");.   
212b0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71   strcat (sql, sq
212c0 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
212d0 63 61 74 20 28 73 71 6c 2c 20 22 27 2c 20 27 22  cat (sql, "', '"
212e0 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
212f0 71 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a  ql, sqlcolumn);.
21300 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
21310 20 22 27 2c 20 22 29 3b 0a 20 20 20 20 73 70 72   "', ");.    spr
21320 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22 25 64  intf (dummy, "%d
21330 2c 20 25 64 2c 20 22 2c 20 74 79 70 65 2c 20 64  , %d, ", type, d
21340 69 6d 65 6e 73 69 6f 6e 29 3b 0a 20 20 20 20 73  imension);.    s
21350 74 72 63 61 74 20 28 73 71 6c 2c 20 64 75 6d 6d  trcat (sql, dumm
21360 79 29 3b 0a 20 20 20 20 69 66 20 28 73 72 69 64  y);.    if (srid
21370 20 3c 3d 20 30 29 0a 09 73 74 72 63 61 74 20 28   <= 0)..strcat (
21380 73 71 6c 2c 20 22 2d 31 22 29 3b 0a 20 20 20 20  sql, "-1");.    
21390 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
213a0 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20  sprintf (dummy, 
213b0 22 25 64 22 2c 20 73 72 69 64 29 3b 0a 09 20 20  "%d", srid);..  
213c0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 64 75 6d  strcat (sql, dum
213d0 6d 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  my);.      }.   
213e0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 2c   strcat (sql, ",
213f0 20 27 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74   '");.    strcat
21400 20 28 73 71 6c 2c 20 78 66 6f 72 6d 61 74 29 3b   (sql, xformat);
21410 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
21420 2c 20 22 27 29 22 29 3b 0a 20 20 20 20 72 65 74  , "')");.    ret
21430 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
21440 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
21450 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73  LL, NULL, &errMs
21460 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20  g);.    if (ret 
21470 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67  != SQLITE_OK)..g
21480 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 73  oto error;.    s
21490 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
214a0 74 20 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a  t (context, 1);.
214b0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 65 72      return;.  er
214c0 72 6f 72 3a 0a 20 20 20 20 73 70 61 74 69 61 6c  ror:.    spatial
214d0 69 74 65 5f 65 20 28 22 41 64 64 46 44 4f 47 65  ite_e ("AddFDOGe
214e0 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65  ometryColumn() e
214f0 72 72 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c  rror: \"%s\"\n",
21500 20 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71   errMsg);.    sq
21510 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d  lite3_free (errM
21520 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sg);.    sqlite3
21530 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
21540 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 72 65  text, 0);.    re
21550 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  turn;.}..static 
21560 76 6f 69 64 0a 66 6e 63 74 5f 52 65 63 6f 76 65  void.fnct_Recove
21570 72 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75  rFDOGeometryColu
21580 6d 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  mn (sqlite3_cont
21590 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
215a0 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 20 20 20  nt argc,....    
215b0 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
215c0 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
215d0 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 52  QL function:./ R
215e0 65 63 6f 76 65 72 46 44 4f 47 65 6f 6d 65 74 72  ecoverFDOGeometr
215f0 79 43 6f 6c 75 6d 6e 28 74 61 62 6c 65 2c 20 63  yColumn(table, c
21600 6f 6c 75 6d 6e 2c 20 73 72 69 64 2c 20 67 65 6f  olumn, srid, geo
21610 6d 65 74 72 79 5f 74 79 70 65 20 2c 20 64 69 6d  metry_type , dim
21620 65 6e 73 69 6f 6e 2c 20 67 65 6f 6d 65 74 72 79  ension, geometry
21630 5f 66 6f 72 6d 61 74 20 29 0a 2f 0a 2f 20 63 68  _format )././ ch
21640 65 63 6b 73 20 69 66 20 61 6e 20 65 78 69 73 74  ecks if an exist
21650 69 6e 67 20 54 41 42 4c 45 2e 43 4f 4c 55 4d 4e  ing TABLE.COLUMN
21660 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 72   satisfies the r
21670 65 71 75 69 72 65 64 20 67 65 6f 6d 65 74 72 69  equired geometri
21680 63 20 66 65 61 74 75 72 65 73 0a 2f 20 69 66 20  c features./ if 
21690 79 65 73 20 61 64 64 73 20 69 74 20 74 6f 20 46  yes adds it to F
216a0 44 4f 2d 73 74 79 6c 65 64 20 53 70 61 74 69 61  DO-styled Spatia
216b0 6c 4d 65 74 61 44 61 74 61 20 0a 2f 20 72 65 74  lMetaData ./ ret
216c0 75 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65 73  urns 1 on succes
216d0 73 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72 65  s./ 0 on failure
216e0 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  .*/.    const ch
216f0 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20 63  ar *table;.    c
21700 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
21710 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  n;.    const cha
21720 72 20 2a 66 6f 72 6d 61 74 3b 0a 20 20 20 20 63  r *format;.    c
21730 68 61 72 20 78 66 6f 72 6d 61 74 5b 36 34 5d 3b  har xformat[64];
21740 0a 20 20 20 20 69 6e 74 20 74 79 70 65 3b 0a 20  .    int type;. 
21750 20 20 20 69 6e 74 20 73 72 69 64 20 3d 20 2d 31     int srid = -1
21760 3b 0a 20 20 20 20 69 6e 74 20 64 69 6d 65 6e 73  ;.    int dimens
21770 69 6f 6e 20 3d 20 32 3b 0a 20 20 20 20 63 68 61  ion = 2;.    cha
21780 72 20 64 75 6d 6d 79 5b 33 32 5d 3b 0a 20 20 20  r dummy[32];.   
21790 20 63 68 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b   char sql[1024];
217a0 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72 4d 73  .    char *errMs
217b0 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  g = NULL;.    in
217c0 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61 72 20  t ret;.    char 
217d0 2a 2a 72 65 73 75 6c 74 73 3b 0a 20 20 20 20 69  **results;.    i
217e0 6e 74 20 72 6f 77 73 3b 0a 20 20 20 20 69 6e 74  nt rows;.    int
217f0 20 63 6f 6c 75 6d 6e 73 3b 0a 20 20 20 20 69 6e   columns;.    in
21800 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 74 62  t i;.    char tb
21810 6c 6e 61 6d 65 5b 32 35 36 5d 3b 0a 20 20 20 20  lname[256];.    
21820 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31 30  char sqltable[10
21830 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71  24];.    char sq
21840 6c 63 6f 6c 75 6d 6e 5b 31 30 32 34 5d 3b 0a 20  lcolumn[1024];. 
21850 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69     sqlite3 *sqli
21860 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  te = sqlite3_con
21870 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28  text_db_handle (
21880 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41  context);.    GA
21890 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
218a0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
218b0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
218c0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
218d0 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
218e0 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20 7b  TE_TEXT).      {
218f0 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
21900 0a 09 20 20 20 20 20 20 28 22 52 65 63 6f 76 65  ..      ("Recove
21910 72 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75  rFDOGeometryColu
21920 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75  mn() error: argu
21930 6d 65 6e 74 20 31 20 5b 74 61 62 6c 65 5f 6e 61  ment 1 [table_na
21940 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68  me] is not of th
21950 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22  e String type\n"
21960 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
21970 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
21980 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e  t, 0);..  return
21990 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 74 61  ;.      }.    ta
219a0 62 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ble = (const cha
219b0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
219c0 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d  ue_text (argv[0]
219d0 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  );.    if (sqlit
219e0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
219f0 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[1]) != SQLIT
21a00 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20 7b 0a  E_TEXT).      {.
21a10 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a  .  spatialite_e.
21a20 09 20 20 20 20 20 20 28 22 52 65 63 6f 76 65 72  .      ("Recover
21a30 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d  FDOGeometryColum
21a40 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d  n() error: argum
21a50 65 6e 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61  ent 2 [column_na
21a60 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68  me] is not of th
21a70 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22  e String type\n"
21a80 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
21a90 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
21aa0 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e  t, 0);..  return
21ab0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 63 6f  ;.      }.    co
21ac0 6c 75 6d 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68  lumn = (const ch
21ad0 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
21ae0 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 31  lue_text (argv[1
21af0 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c 69  ]);.    if (sqli
21b00 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
21b10 61 72 67 76 5b 32 5d 29 20 21 3d 20 53 51 4c 49  argv[2]) != SQLI
21b20 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20  TE_INTEGER).    
21b30 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
21b40 65 5f 65 0a 09 20 20 20 20 20 20 28 22 52 65 63  e_e..      ("Rec
21b50 6f 76 65 72 46 44 4f 47 65 6f 6d 65 74 72 79 43  overFDOGeometryC
21b60 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61  olumn() error: a
21b70 72 67 75 6d 65 6e 74 20 33 20 5b 53 52 49 44 5d  rgument 3 [SRID]
21b80 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 49   is not of the I
21b90 6e 74 65 67 65 72 20 74 79 70 65 5c 6e 22 29 3b  nteger type\n");
21ba0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
21bb0 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
21bc0 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a   0);..  return;.
21bd0 20 20 20 20 20 20 7d 0a 20 20 20 20 73 72 69 64        }.    srid
21be0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
21bf0 5f 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a  _int (argv[2]);.
21c00 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
21c10 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
21c20 5b 33 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49  [3]) != SQLITE_I
21c30 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
21c40 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a  .  spatialite_e.
21c50 09 20 20 20 20 20 20 28 22 52 65 63 6f 76 65 72  .      ("Recover
21c60 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d  FDOGeometryColum
21c70 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d  n() error: argum
21c80 65 6e 74 20 34 20 5b 67 65 6f 6d 65 74 72 79 5f  ent 4 [geometry_
21c90 74 79 70 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20  type] is not of 
21ca0 74 68 65 20 49 6e 74 65 67 65 72 20 74 79 70 65  the Integer type
21cb0 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  \n");..  sqlite3
21cc0 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
21cd0 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74  text, 0);..  ret
21ce0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
21cf0 20 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f   type = sqlite3_
21d00 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
21d10 33 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c  3]);.    if (sql
21d20 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
21d30 28 61 72 67 76 5b 34 5d 29 20 21 3d 20 53 51 4c  (argv[4]) != SQL
21d40 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20  ITE_INTEGER).   
21d50 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
21d60 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 52 65  te_e..      ("Re
21d70 63 6f 76 65 72 46 44 4f 47 65 6f 6d 65 74 72 79  coverFDOGeometry
21d80 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20  Column() error: 
21d90 61 72 67 75 6d 65 6e 74 20 35 20 5b 64 69 6d 65  argument 5 [dime
21da0 6e 73 69 6f 6e 5d 20 69 73 20 6e 6f 74 20 6f 66  nsion] is not of
21db0 20 74 68 65 20 49 6e 74 65 67 65 72 20 74 79 70   the Integer typ
21dc0 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65  e\n");..  sqlite
21dd0 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
21de0 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
21df0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
21e00 20 20 64 69 6d 65 6e 73 69 6f 6e 20 3d 20 73 71    dimension = sq
21e10 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
21e20 28 61 72 67 76 5b 34 5d 29 3b 0a 20 20 20 20 69  (argv[4]);.    i
21e30 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
21e40 5f 74 79 70 65 20 28 61 72 67 76 5b 35 5d 29 20  _type (argv[5]) 
21e50 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a  != SQLITE_TEXT).
21e60 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
21e70 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28  alite_e..      (
21e80 22 52 65 63 6f 76 65 72 46 44 4f 47 65 6f 6d 65  "RecoverFDOGeome
21e90 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f  tryColumn() erro
21ea0 72 3a 20 61 72 67 75 6d 65 6e 74 20 36 20 5b 67  r: argument 6 [g
21eb0 65 6f 6d 65 74 72 79 5f 66 6f 72 6d 61 74 5d 20  eometry_format] 
21ec0 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 53 74  is not of the St
21ed0 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a 09  ring type\n");..
21ee0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
21ef0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
21f00 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
21f10 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 6d 61 74      }.    format
21f20 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
21f30 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
21f40 74 65 78 74 20 28 61 72 67 76 5b 35 5d 29 3b 0a  text (argv[5]);.
21f50 20 20 20 20 69 66 20 28 74 79 70 65 20 3d 3d 0a      if (type ==.
21f60 09 47 41 49 41 5f 50 4f 49 4e 54 0a 09 7c 7c 20  .GAIA_POINT..|| 
21f70 74 79 70 65 20 3d 3d 0a 09 47 41 49 41 5f 4c 49  type ==..GAIA_LI
21f80 4e 45 53 54 52 49 4e 47 0a 09 7c 7c 20 74 79 70  NESTRING..|| typ
21f90 65 20 3d 3d 0a 09 47 41 49 41 5f 50 4f 4c 59 47  e ==..GAIA_POLYG
21fa0 4f 4e 0a 09 7c 7c 20 74 79 70 65 20 3d 3d 0a 09  ON..|| type ==..
21fb0 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 0a  GAIA_MULTIPOINT.
21fc0 09 7c 7c 20 74 79 70 65 20 3d 3d 0a 09 47 41 49  .|| type ==..GAI
21fd0 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e  A_MULTILINESTRIN
21fe0 47 0a 09 7c 7c 20 74 79 70 65 20 3d 3d 20 47 41  G..|| type == GA
21ff0 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 20  IA_MULTIPOLYGON 
22000 7c 7c 20 74 79 70 65 20 3d 3d 20 47 41 49 41 5f  || type == GAIA_
22010 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
22020 4f 4e 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a  ON)..;.    else.
22030 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
22040 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28  alite_e..      (
22050 22 52 65 63 6f 76 65 72 46 44 4f 47 65 6f 6d 65  "RecoverFDOGeome
22060 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f  tryColumn() erro
22070 72 3a 20 61 72 67 75 6d 65 6e 74 20 34 20 5b 67  r: argument 4 [g
22080 65 6f 6d 65 74 72 79 5f 74 79 70 65 5d 20 68 61  eometry_type] ha
22090 73 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c  s an illegal val
220a0 75 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74  ue\n");..  sqlit
220b0 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
220c0 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72  ontext, 0);..  r
220d0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
220e0 20 20 20 69 66 20 28 64 69 6d 65 6e 73 69 6f 6e     if (dimension
220f0 20 3c 20 32 20 7c 7c 20 64 69 6d 65 6e 73 69 6f   < 2 || dimensio
22100 6e 20 3e 20 34 29 0a 20 20 20 20 20 20 7b 0a 09  n > 4).      {..
22110 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09    spatialite_e..
22120 20 20 20 20 20 20 28 22 52 65 63 6f 76 65 72 46        ("RecoverF
22130 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e  DOGeometryColumn
22140 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65  () error: argume
22150 6e 74 20 35 20 5b 64 69 6d 65 6e 73 69 6f 6e 5d  nt 5 [dimension]
22160 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e   current version
22170 20 6f 6e 6c 79 20 61 63 63 65 70 74 73 20 64 69   only accepts di
22180 6d 65 6e 73 69 6f 6e 3d 32 2c 33 2c 34 5c 6e 22  mension=2,3,4\n"
22190 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
221a0 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
221b0 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e  t, 0);..  return
221c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
221d0 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 66 6f   (strcasecmp (fo
221e0 72 6d 61 74 2c 20 22 57 4b 54 22 29 20 3d 3d 20  rmat, "WKT") == 
221f0 30 29 0a 09 73 74 72 63 70 79 20 28 78 66 6f 72  0)..strcpy (xfor
22200 6d 61 74 2c 20 22 57 4b 54 22 29 3b 0a 20 20 20  mat, "WKT");.   
22210 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 61 73   else if (strcas
22220 65 63 6d 70 20 28 66 6f 72 6d 61 74 2c 20 22 57  ecmp (format, "W
22230 4b 42 22 29 20 3d 3d 20 30 29 0a 09 73 74 72 63  KB") == 0)..strc
22240 70 79 20 28 78 66 6f 72 6d 61 74 2c 20 22 57 4b  py (xformat, "WK
22250 42 22 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66  B");.    else if
22260 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 66 6f   (strcasecmp (fo
22270 72 6d 61 74 2c 20 22 46 47 46 22 29 20 3d 3d 20  rmat, "FGF") == 
22280 30 29 0a 09 73 74 72 63 70 79 20 28 78 66 6f 72  0)..strcpy (xfor
22290 6d 61 74 2c 20 22 46 47 46 22 29 3b 0a 20 20 20  mat, "FGF");.   
222a0 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 61 73   else if (strcas
222b0 65 63 6d 70 20 28 66 6f 72 6d 61 74 2c 20 22 53  ecmp (format, "S
222c0 50 41 54 49 41 4c 49 54 45 22 29 20 3d 3d 20 30  PATIALITE") == 0
222d0 29 0a 09 73 74 72 63 70 79 20 28 78 66 6f 72 6d  )..strcpy (xform
222e0 61 74 2c 20 22 53 50 41 54 49 41 4c 49 54 45 22  at, "SPATIALITE"
222f0 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20  );.    else.    
22300 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
22310 65 5f 65 0a 09 20 20 20 20 20 20 28 22 52 65 63  e_e..      ("Rec
22320 6f 76 65 72 46 44 4f 47 65 6f 6d 65 74 72 79 43  overFDOGeometryC
22330 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61  olumn() error: a
22340 72 67 75 6d 65 6e 74 20 36 20 5b 67 65 6f 6d 65  rgument 6 [geome
22350 74 72 79 5f 66 6f 72 6d 61 74 5d 20 68 61 73 20  try_format] has 
22360 74 6f 20 62 65 20 6f 6e 65 20 6f 66 3a 20 57 4b  to be one of: WK
22370 54 2c 57 4b 42 2c 46 47 46 5c 6e 22 29 3b 0a 09  T,WKB,FGF\n");..
22380 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
22390 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
223a0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
223b0 20 20 20 20 7d 0a 2f 2a 20 63 68 65 63 6b 69 6e      }./* checkin
223c0 67 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 65  g if the table e
223d0 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 73 74 72  xists */.    str
223e0 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 28  cpy (sqltable, (
223f0 63 68 61 72 20 2a 29 20 74 61 62 6c 65 29 3b 0a  char *) table);.
22400 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74      clean_sql_st
22410 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b  ring (sqltable);
22420 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
22430 63 6f 6c 75 6d 6e 2c 20 28 63 68 61 72 20 2a 29  column, (char *)
22440 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 63 6c   column);.    cl
22450 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28  ean_sql_string (
22460 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  sqlcolumn);.    
22470 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 0a 09 20  sprintf (sql,.. 
22480 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65      "SELECT name
22490 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73   FROM sqlite_mas
224a0 74 65 72 20 57 48 45 52 45 20 74 79 70 65 20 3d  ter WHERE type =
224b0 20 27 74 61 62 6c 65 27 20 41 4e 44 20 55 70 70   'table' AND Upp
224c0 65 72 28 6e 61 6d 65 29 20 3d 20 55 70 70 65 72  er(name) = Upper
224d0 28 27 25 73 27 29 22 2c 0a 09 20 20 20 20 20 73  ('%s')",..     s
224e0 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 72 65  qltable);.    re
224f0 74 20 3d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  t = sqlite3_get_
22500 74 61 62 6c 65 20 28 73 71 6c 69 74 65 2c 20 73  table (sqlite, s
22510 71 6c 2c 20 26 72 65 73 75 6c 74 73 2c 20 26 72  ql, &results, &r
22520 6f 77 73 2c 20 26 63 6f 6c 75 6d 6e 73 2c 20 26  ows, &columns, &
22530 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 20  errMsg);.    if 
22540 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
22550 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70  K).      {..  sp
22560 61 74 69 61 6c 69 74 65 5f 65 20 28 22 52 65 63  atialite_e ("Rec
22570 6f 76 65 72 46 44 4f 47 65 6f 6d 65 74 72 79 43  overFDOGeometryC
22580 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 5c 6e 22  olumn: \"%s\"\n"
22590 2c 20 65 72 72 4d 73 67 29 3b 0a 09 20 20 73 71  , errMsg);..  sq
225a0 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d  lite3_free (errM
225b0 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  sg);..  return;.
225c0 20 20 20 20 20 20 7d 0a 20 20 20 20 2a 74 62 6c        }.    *tbl
225d0 6e 61 6d 65 20 3d 20 27 5c 30 27 3b 0a 20 20 20  name = '\0';.   
225e0 20 66 6f 72 20 28 69 20 3d 20 31 3b 20 69 20 3c   for (i = 1; i <
225f0 3d 20 72 6f 77 73 3b 20 69 2b 2b 29 0a 20 20 20  = rows; i++).   
22600 20 20 20 7b 0a 09 20 20 73 74 72 63 70 79 20 28     {..  strcpy (
22610 74 62 6c 6e 61 6d 65 2c 20 72 65 73 75 6c 74 73  tblname, results
22620 5b 28 69 20 2a 20 63 6f 6c 75 6d 6e 73 29 5d 29  [(i * columns)])
22630 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 73 71  ;.      }.    sq
22640 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
22650 20 28 72 65 73 75 6c 74 73 29 3b 0a 20 20 20 20   (results);.    
22660 69 66 20 28 2a 74 62 6c 6e 61 6d 65 20 3d 3d 20  if (*tblname == 
22670 27 5c 30 27 29 0a 20 20 20 20 20 20 7b 0a 09 20  '\0').      {.. 
22680 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20   spatialite_e.. 
22690 20 20 20 20 20 28 22 52 65 63 6f 76 65 72 46 44       ("RecoverFD
226a0 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28  OGeometryColumn(
226b0 29 20 65 72 72 6f 72 3a 20 74 61 62 6c 65 20 27  ) error: table '
226c0 25 73 27 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  %s' does not exi
226d0 73 74 5c 6e 22 2c 0a 09 20 20 20 20 20 20 20 74  st\n",..       t
226e0 61 62 6c 65 29 3b 0a 09 20 20 73 71 6c 69 74 65  able);..  sqlite
226f0 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
22700 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
22710 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
22720 20 20 69 66 20 28 21 72 65 63 6f 76 65 72 46 44    if (!recoverFD
22730 4f 47 65 6f 6d 43 6f 6c 75 6d 6e 0a 09 28 73 71  OGeomColumn..(sq
22740 6c 69 74 65 2c 20 28 63 6f 6e 73 74 20 75 6e 73  lite, (const uns
22750 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 74 61  igned char *) ta
22760 62 6c 65 2c 0a 09 20 28 63 6f 6e 73 74 20 75 6e  ble,.. (const un
22770 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 63  signed char *) c
22780 6f 6c 75 6d 6e 2c 20 74 79 70 65 2c 20 73 72 69  olumn, type, sri
22790 64 29 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  d)).      {..  s
227a0 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 52 65  patialite_e ("Re
227b0 63 6f 76 65 72 46 44 4f 47 65 6f 6d 65 74 72 79  coverFDOGeometry
227c0 43 6f 6c 75 6d 6e 28 29 3a 20 76 61 6c 69 64 61  Column(): valida
227d0 74 69 6f 6e 20 66 61 69 6c 65 64 5c 6e 22 29 3b  tion failed\n");
227e0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
227f0 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
22800 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a   0);..  return;.
22810 20 20 20 20 20 20 7d 0a 20 20 20 20 73 74 72 63        }.    strc
22820 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 28 63  py (sqltable, (c
22830 68 61 72 20 2a 29 20 74 62 6c 6e 61 6d 65 29 3b  har *) tblname);
22840 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73  .    clean_sql_s
22850 74 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29  tring (sqltable)
22860 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
22870 6c 2c 0a 09 20 20 20 20 22 49 4e 53 45 52 54 20  l,..    "INSERT 
22880 49 4e 54 4f 20 67 65 6f 6d 65 74 72 79 5f 63 6f  INTO geometry_co
22890 6c 75 6d 6e 73 20 28 66 5f 74 61 62 6c 65 5f 6e  lumns (f_table_n
228a0 61 6d 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79 5f  ame, f_geometry_
228b0 63 6f 6c 75 6d 6e 2c 20 67 65 6f 6d 65 74 72 79  column, geometry
228c0 5f 74 79 70 65 2c 20 22 29 3b 0a 20 20 20 20 73  _type, ");.    s
228d0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 63 6f 6f  trcat (sql, "coo
228e0 72 64 5f 64 69 6d 65 6e 73 69 6f 6e 2c 20 73 72  rd_dimension, sr
228f0 69 64 2c 20 67 65 6f 6d 65 74 72 79 5f 66 6f 72  id, geometry_for
22900 6d 61 74 29 20 56 41 4c 55 45 53 20 28 22 29 3b  mat) VALUES (");
22910 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
22920 2c 20 22 27 22 29 3b 0a 20 20 20 20 73 74 72 63  , "'");.    strc
22930 61 74 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c  at (sql, sqltabl
22940 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  e);.    strcat (
22950 73 71 6c 2c 20 22 27 2c 20 27 22 29 3b 0a 20 20  sql, "', '");.  
22960 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
22970 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73  qlcolumn);.    s
22980 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 2c 20  trcat (sql, "', 
22990 22 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  ");.    sprintf 
229a0 28 64 75 6d 6d 79 2c 20 22 25 64 2c 20 25 64 2c  (dummy, "%d, %d,
229b0 20 22 2c 20 74 79 70 65 2c 20 64 69 6d 65 6e 73   ", type, dimens
229c0 69 6f 6e 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ion);.    strcat
229d0 20 28 73 71 6c 2c 20 64 75 6d 6d 79 29 3b 0a 20   (sql, dummy);. 
229e0 20 20 20 69 66 20 28 73 72 69 64 20 3c 3d 20 30     if (srid <= 0
229f0 29 0a 09 73 74 72 63 61 74 20 28 73 71 6c 2c 20  )..strcat (sql, 
22a00 22 2d 31 22 29 3b 0a 20 20 20 20 65 6c 73 65 0a  "-1");.    else.
22a10 20 20 20 20 20 20 7b 0a 09 20 20 73 70 72 69 6e        {..  sprin
22a20 74 66 20 28 64 75 6d 6d 79 2c 20 22 25 64 22 2c  tf (dummy, "%d",
22a30 20 73 72 69 64 29 3b 0a 09 20 20 73 74 72 63 61   srid);..  strca
22a40 74 20 28 73 71 6c 2c 20 64 75 6d 6d 79 29 3b 0a  t (sql, dummy);.
22a50 20 20 20 20 20 20 7d 0a 20 20 20 20 73 74 72 63        }.    strc
22a60 61 74 20 28 73 71 6c 2c 20 22 2c 20 27 22 29 3b  at (sql, ", '");
22a70 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
22a80 2c 20 78 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20  , xformat);.    
22a90 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 29  strcat (sql, "')
22aa0 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71  ");.    ret = sq
22ab0 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
22ac0 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e  te, sql, NULL, N
22ad0 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20  ULL, &errMsg);. 
22ae0 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
22af0 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65  LITE_OK)..goto e
22b00 72 72 6f 72 3b 0a 20 20 20 20 73 71 6c 69 74 65  rror;.    sqlite
22b10 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
22b20 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 20 20 72  ntext, 1);.    r
22b30 65 74 75 72 6e 3b 0a 20 20 65 72 72 6f 72 3a 0a  eturn;.  error:.
22b40 20 20 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65      spatialite_e
22b50 20 28 22 52 65 63 6f 76 65 72 46 44 4f 47 65 6f   ("RecoverFDOGeo
22b60 6d 65 74 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72  metryColumn() er
22b70 72 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 20  ror: \"%s\"\n", 
22b80 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c  errMsg);.    sql
22b90 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73  ite3_free (errMs
22ba0 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  g);.    sqlite3_
22bb0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
22bc0 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 72 65 74  ext, 0);.    ret
22bd0 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  urn;.}..static v
22be0 6f 69 64 0a 66 6e 63 74 5f 44 69 73 63 61 72 64  oid.fnct_Discard
22bf0 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d  FDOGeometryColum
22c00 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  n (sqlite3_conte
22c10 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
22c20 74 20 61 72 67 63 2c 0a 09 09 09 20 20 20 20 20  t argc,....     
22c30 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
22c40 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
22c50 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 44 69  L function:./ Di
22c60 73 63 61 72 64 46 44 4f 47 65 6f 6d 65 74 72 79  scardFDOGeometry
22c70 43 6f 6c 75 6d 6e 28 74 61 62 6c 65 2c 20 63 6f  Column(table, co
22c80 6c 75 6d 6e 29 0a 2f 0a 2f 20 72 65 6d 6f 76 65  lumn)././ remove
22c90 73 20 54 41 42 4c 45 2e 43 4f 4c 55 4d 4e 20 66  s TABLE.COLUMN f
22ca0 72 6f 6d 20 74 68 65 20 53 70 61 74 69 61 6c 20  rom the Spatial 
22cb0 4d 65 74 61 44 61 74 61 0a 2f 20 72 65 74 75 72  MetaData./ retur
22cc0 6e 73 20 31 20 6f 6e 20 73 75 63 63 65 73 73 0a  ns 1 on success.
22cd0 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 0a 2a  / 0 on failure.*
22ce0 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  /.    const unsi
22cf0 67 6e 65 64 20 63 68 61 72 20 2a 74 61 62 6c 65  gned char *table
22d00 3b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  ;.    const unsi
22d10 67 6e 65 64 20 63 68 61 72 20 2a 63 6f 6c 75 6d  gned char *colum
22d20 6e 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 5b  n;.    char sql[
22d30 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
22d40 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a  *errMsg = NULL;.
22d50 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
22d60 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31   char sqltable[1
22d70 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 73  024];.    char s
22d80 71 6c 63 6f 6c 75 6d 6e 5b 31 30 32 34 5d 3b 0a  qlcolumn[1024];.
22d90 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c      sqlite3 *sql
22da0 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ite = sqlite3_co
22db0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20  ntext_db_handle 
22dc0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47  (context);.    G
22dd0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
22de0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
22df0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
22e00 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
22e10 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
22e20 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20  ITE_TEXT).      
22e30 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
22e40 65 0a 09 20 20 20 20 20 20 28 22 44 69 73 63 61  e..      ("Disca
22e50 72 64 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c  rdFDOGeometryCol
22e60 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67  umn() error: arg
22e70 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c 65 5f 6e  ument 1 [table_n
22e80 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74  ame] is not of t
22e90 68 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c 6e  he String type\n
22ea0 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72  ");..  sqlite3_r
22eb0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
22ec0 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72  xt, 0);..  retur
22ed0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 74  n;.      }.    t
22ee0 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  able = sqlite3_v
22ef0 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b  alue_text (argv[
22f00 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c  0]);.    if (sql
22f10 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
22f20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c  (argv[1]) != SQL
22f30 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20  ITE_TEXT).      
22f40 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
22f50 65 0a 09 20 20 20 20 20 20 28 22 44 69 73 63 61  e..      ("Disca
22f60 72 64 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c  rdFDOGeometryCol
22f70 75 6d 6e 28 29 20 65 72 72 6f 72 3a 20 61 72 67  umn() error: arg
22f80 75 6d 65 6e 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f  ument 2 [column_
22f90 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20  name] is not of 
22fa0 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c  the String type\
22fb0 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  n");..  sqlite3_
22fc0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
22fd0 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75  ext, 0);..  retu
22fe0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
22ff0 63 6f 6c 75 6d 6e 20 3d 20 73 71 6c 69 74 65 33  column = sqlite3
23000 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67  _value_text (arg
23010 76 5b 31 5d 29 3b 0a 20 20 20 20 73 74 72 63 70  v[1]);.    strcp
23020 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 28 63 68  y (sqltable, (ch
23030 61 72 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20  ar *) table);.  
23040 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69    clean_sql_stri
23050 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ng (sqltable);. 
23060 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 63 6f     strcpy (sqlco
23070 6c 75 6d 6e 2c 20 28 63 68 61 72 20 2a 29 20 63  lumn, (char *) c
23080 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 63 6c 65 61  olumn);.    clea
23090 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71  n_sql_string (sq
230a0 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 70  lcolumn);.    sp
230b0 72 69 6e 74 66 20 28 73 71 6c 2c 0a 09 20 20 20  rintf (sql,..   
230c0 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 67    "DELETE FROM g
230d0 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20  eometry_columns 
230e0 57 48 45 52 45 20 55 70 70 65 72 28 66 5f 74 61  WHERE Upper(f_ta
230f0 62 6c 65 5f 6e 61 6d 65 29 20 3d 20 55 70 70 65  ble_name) = Uppe
23100 72 28 27 25 73 27 29 20 41 4e 44 20 55 70 70 65  r('%s') AND Uppe
23110 72 28 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  r(f_geometry_col
23120 75 6d 6e 29 20 3d 20 55 70 70 65 72 28 27 25 73  umn) = Upper('%s
23130 27 29 22 2c 0a 09 20 20 20 20 20 73 71 6c 74 61  ')",..     sqlta
23140 62 6c 65 2c 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b  ble, sqlcolumn);
23150 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74  .    ret = sqlit
23160 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c  e3_exec (sqlite,
23170 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c   sql, NULL, NULL
23180 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20  , &errMsg);.    
23190 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
231a0 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f  E_OK)..goto erro
231b0 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  r;.    sqlite3_r
231c0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
231d0 78 74 2c 20 31 29 3b 0a 20 20 20 20 72 65 74 75  xt, 1);.    retu
231e0 72 6e 3b 0a 20 20 65 72 72 6f 72 3a 0a 20 20 20  rn;.  error:.   
231f0 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22   spatialite_e ("
23200 44 69 73 63 61 72 64 46 44 4f 47 65 6f 6d 65 74  DiscardFDOGeomet
23210 72 79 43 6f 6c 75 6d 6e 28 29 20 65 72 72 6f 72  ryColumn() error
23220 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 65 72 72  : \"%s\"\n", err
23230 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Msg);.    sqlite
23240 33 5f 66 72 65 65 20 28 65 72 72 4d 73 67 29 3b  3_free (errMsg);
23250 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
23260 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
23270 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
23280 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a  ;.}..static int.
23290 65 76 61 6c 5f 72 74 72 65 65 5f 65 6e 74 72 79  eval_rtree_entry
232a0 20 28 69 6e 74 20 6f 6b 5f 67 65 6f 6d 2c 20 64   (int ok_geom, d
232b0 6f 75 62 6c 65 20 67 65 6f 6d 5f 76 61 6c 75 65  ouble geom_value
232c0 2c 20 69 6e 74 20 6f 6b 5f 72 74 72 65 65 2c 0a  , int ok_rtree,.
232d0 09 09 20 20 64 6f 75 62 6c 65 20 72 74 72 65 65  ..  double rtree
232e0 5f 76 61 6c 75 65 29 0a 7b 0a 2f 2a 20 65 76 61  _value).{./* eva
232f0 6c 75 61 74 69 6e 67 20 67 65 6f 6d 2d 63 6f 6f  luating geom-coo
23300 72 64 20 61 6e 64 20 72 74 72 65 65 2d 63 6f 6f  rd and rtree-coo
23310 72 64 20 2a 2f 0a 20 20 20 20 69 66 20 28 21 6f  rd */.    if (!o
23320 6b 5f 67 65 6f 6d 20 26 26 20 21 6f 6b 5f 72 74  k_geom && !ok_rt
23330 72 65 65 29 0a 09 72 65 74 75 72 6e 20 31 3b 0a  ree)..return 1;.
23340 20 20 20 20 69 66 20 28 6f 6b 5f 67 65 6f 6d 20      if (ok_geom 
23350 26 26 20 6f 6b 5f 72 74 72 65 65 29 0a 20 20 20  && ok_rtree).   
23360 20 20 20 7b 0a 09 20 20 66 6c 6f 61 74 20 67 20     {..  float g 
23370 3d 20 28 66 6c 6f 61 74 29 20 67 65 6f 6d 5f 76  = (float) geom_v
23380 61 6c 75 65 3b 0a 09 20 20 66 6c 6f 61 74 20 72  alue;..  float r
23390 20 3d 20 28 66 6c 6f 61 74 29 20 72 74 72 65 65   = (float) rtree
233a0 5f 76 61 6c 75 65 3b 0a 09 20 20 69 66 20 28 67  _value;..  if (g
233b0 20 21 3d 20 72 29 0a 09 20 20 20 20 20 20 72 65   != r)..      re
233c0 74 75 72 6e 20 30 3b 0a 09 20 20 72 65 74 75 72  turn 0;..  retur
233d0 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
233e0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74   return 0;.}..st
233f0 61 74 69 63 20 69 6e 74 0a 63 68 65 63 6b 5f 73  atic int.check_s
23400 70 61 74 69 61 6c 5f 69 6e 64 65 78 20 28 73 71  patial_index (sq
23410 6c 69 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20  lite3 * sqlite, 
23420 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
23430 68 61 72 20 2a 74 61 62 6c 65 2c 0a 09 09 20 20  har *table,...  
23440 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
23450 64 20 63 68 61 72 20 2a 67 65 6f 6d 29 0a 7b 0a  d char *geom).{.
23460 2f 2a 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  /* attempting to
23470 20 63 68 65 63 6b 20 61 6e 20 52 2a 54 72 65 65   check an R*Tree
23480 20 66 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79   for consistency
23490 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 78 74 61   */.    char xta
234a0 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63  ble[1024];.    c
234b0 68 61 72 20 78 67 65 6f 6d 5b 31 30 32 34 5d 3b  har xgeom[1024];
234c0 0a 20 20 20 20 63 68 61 72 20 69 64 78 5f 6e 61  .    char idx_na
234d0 6d 65 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68  me[2048];.    ch
234e0 61 72 20 73 71 6c 5b 38 31 39 32 5d 3b 0a 20 20  ar sql[8192];.  
234f0 20 20 63 68 61 72 20 73 71 6c 32 5b 32 30 34 38    char sql2[2048
23500 5d 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a  ];.    int ret;.
23510 20 20 20 20 69 6e 74 20 69 73 5f 64 65 66 69 6e      int is_defin
23520 65 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  ed = 0;.    sqli
23530 74 65 33 5f 73 74 6d 74 20 2a 73 74 6d 74 3b 0a  te3_stmt *stmt;.
23540 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
23550 34 20 63 6f 75 6e 74 5f 67 65 6f 6d 3b 0a 20 20  4 count_geom;.  
23560 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
23570 63 6f 75 6e 74 5f 72 74 72 65 65 3b 0a 20 20 20  count_rtree;.   
23580 20 64 6f 75 62 6c 65 20 67 5f 78 6d 69 6e 3b 0a   double g_xmin;.
23590 20 20 20 20 64 6f 75 62 6c 65 20 67 5f 79 6d 69      double g_ymi
235a0 6e 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 67 5f  n;.    double g_
235b0 78 6d 61 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65  xmax;.    double
235c0 20 67 5f 79 6d 61 78 3b 0a 20 20 20 20 69 6e 74   g_ymax;.    int
235d0 20 6f 6b 5f 67 5f 78 6d 69 6e 3b 0a 20 20 20 20   ok_g_xmin;.    
235e0 69 6e 74 20 6f 6b 5f 67 5f 79 6d 69 6e 3b 0a 20  int ok_g_ymin;. 
235f0 20 20 20 69 6e 74 20 6f 6b 5f 67 5f 78 6d 61 78     int ok_g_xmax
23600 3b 0a 20 20 20 20 69 6e 74 20 6f 6b 5f 67 5f 79  ;.    int ok_g_y
23610 6d 61 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  max;.    double 
23620 69 5f 78 6d 69 6e 3b 0a 20 20 20 20 64 6f 75 62  i_xmin;.    doub
23630 6c 65 20 69 5f 79 6d 69 6e 3b 0a 20 20 20 20 64  le i_ymin;.    d
23640 6f 75 62 6c 65 20 69 5f 78 6d 61 78 3b 0a 20 20  ouble i_xmax;.  
23650 20 20 64 6f 75 62 6c 65 20 69 5f 79 6d 61 78 3b    double i_ymax;
23660 0a 20 20 20 20 69 6e 74 20 6f 6b 5f 69 5f 78 6d  .    int ok_i_xm
23670 69 6e 3b 0a 20 20 20 20 69 6e 74 20 6f 6b 5f 69  in;.    int ok_i
23680 5f 79 6d 69 6e 3b 0a 20 20 20 20 69 6e 74 20 6f  _ymin;.    int o
23690 6b 5f 69 5f 78 6d 61 78 3b 0a 20 20 20 20 69 6e  k_i_xmax;.    in
236a0 74 20 6f 6b 5f 69 5f 79 6d 61 78 3b 0a 0a 2f 2a  t ok_i_ymax;../*
236b0 20 63 68 65 63 6b 69 6e 67 20 69 66 20 74 68 65   checking if the
236c0 20 52 2a 54 72 65 65 20 53 70 61 74 69 61 6c 20   R*Tree Spatial 
236d0 49 6e 64 65 78 20 69 73 20 64 65 66 69 6e 65 64  Index is defined
236e0 20 2a 2f 0a 20 20 20 20 73 74 72 63 70 79 20 28   */.    strcpy (
236f0 78 74 61 62 6c 65 2c 20 28 63 6f 6e 73 74 20 63  xtable, (const c
23700 68 61 72 20 2a 29 20 74 61 62 6c 65 29 3b 0a 20  har *) table);. 
23710 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72     clean_sql_str
23720 69 6e 67 20 28 78 74 61 62 6c 65 29 3b 0a 20 20  ing (xtable);.  
23730 20 20 73 74 72 63 70 79 20 28 78 67 65 6f 6d 2c    strcpy (xgeom,
23740 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 20   (const char *) 
23750 67 65 6f 6d 29 3b 0a 20 20 20 20 63 6c 65 61 6e  geom);.    clean
23760 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28 78 67 65  _sql_string (xge
23770 6f 6d 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20  om);.    strcpy 
23780 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 43 6f  (sql, "SELECT Co
23790 75 6e 74 28 2a 29 20 46 52 4f 4d 20 67 65 6f 6d  unt(*) FROM geom
237a0 65 74 72 79 5f 63 6f 6c 75 6d 6e 73 20 22 29 3b  etry_columns ");
237b0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
237c0 6c 32 2c 20 22 57 48 45 52 45 20 55 70 70 65 72  l2, "WHERE Upper
237d0 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 20 3d  (f_table_name) =
237e0 20 55 70 70 65 72 28 27 25 73 27 29 20 22 2c 20   Upper('%s') ", 
237f0 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  xtable);.    str
23800 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
23810 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
23820 6c 32 2c 20 22 41 4e 44 20 55 70 70 65 72 28 66  l2, "AND Upper(f
23830 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e  _geometry_column
23840 29 20 3d 20 55 70 70 65 72 28 27 25 73 27 29 20  ) = Upper('%s') 
23850 22 2c 20 78 67 65 6f 6d 29 3b 0a 20 20 20 20 73  ", xgeom);.    s
23860 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32  trcat (sql, sql2
23870 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
23880 71 6c 2c 20 22 41 4e 44 20 73 70 61 74 69 61 6c  ql, "AND spatial
23890 5f 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 20 3d  _index_enabled =
238a0 20 31 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20   1");.    ret = 
238b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
238c0 76 32 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c  v2 (sqlite, sql,
238d0 20 73 74 72 6c 65 6e 20 28 73 71 6c 29 2c 20 26   strlen (sql), &
238e0 73 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20  stmt, NULL);.   
238f0 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
23900 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09  TE_OK).      {..
23910 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28    spatialite_e (
23920 22 43 68 65 63 6b 53 70 61 74 69 61 6c 49 6e 64  "CheckSpatialInd
23930 65 78 20 53 51 4c 20 65 72 72 6f 72 3a 20 25 73  ex SQL error: %s
23940 5c 6e 22 2c 0a 09 09 09 73 71 6c 69 74 65 33 5f  \n",....sqlite3_
23950 65 72 72 6d 73 67 20 28 73 71 6c 69 74 65 29 29  errmsg (sqlite))
23960 3b 0a 09 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a  ;..  return -1;.
23970 20 20 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c        }.    whil
23980 65 20 28 31 29 0a 20 20 20 20 20 20 7b 0a 09 20  e (1).      {.. 
23990 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 73   ret = sqlite3_s
239a0 74 65 70 20 28 73 74 6d 74 29 3b 0a 09 20 20 69  tep (stmt);..  i
239b0 66 20 28 72 65 74 20 3d 3d 20 53 51 4c 49 54 45  f (ret == SQLITE
239c0 5f 44 4f 4e 45 29 0a 09 20 20 20 20 20 20 62 72  _DONE)..      br
239d0 65 61 6b 3b 0a 09 20 20 69 66 20 28 72 65 74 20  eak;..  if (ret 
239e0 3d 3d 20 53 51 4c 49 54 45 5f 52 4f 57 29 0a 09  == SQLITE_ROW)..
239f0 20 20 20 20 20 20 69 73 5f 64 65 66 69 6e 65 64        is_defined
23a00 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
23a10 6e 5f 69 6e 74 20 28 73 74 6d 74 2c 20 30 29 3b  n_int (stmt, 0);
23a20 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
23a30 09 09 70 72 69 6e 74 66 20 28 22 73 71 6c 69 74  ..printf ("sqlit
23a40 65 33 5f 73 74 65 70 28 29 20 65 72 72 6f 72 3a  e3_step() error:
23a50 20 25 73 5c 6e 22 2c 20 73 71 6c 69 74 65 33 5f   %s\n", sqlite3_
23a60 65 72 72 6d 73 67 20 28 73 71 6c 69 74 65 29 29  errmsg (sqlite))
23a70 3b 0a 09 09 73 71 6c 69 74 65 33 5f 66 69 6e 61  ;...sqlite3_fina
23a80 6c 69 7a 65 20 28 73 74 6d 74 29 3b 0a 09 09 72  lize (stmt);...r
23a90 65 74 75 72 6e 20 2d 31 3b 0a 09 20 20 20 20 7d  eturn -1;..    }
23aa0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  .      }.    sql
23ab0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73  ite3_finalize (s
23ac0 74 6d 74 29 3b 0a 20 20 20 20 69 66 20 28 21 69  tmt);.    if (!i
23ad0 73 5f 64 65 66 69 6e 65 64 29 0a 09 72 65 74 75  s_defined)..retu
23ae0 72 6e 20 2d 31 3b 0a 0a 20 20 20 20 73 70 72 69  rn -1;..    spri
23af0 6e 74 66 20 28 78 67 65 6f 6d 2c 20 22 25 73 22  ntf (xgeom, "%s"
23b00 2c 20 67 65 6f 6d 29 3b 0a 20 20 20 20 64 6f 75  , geom);.    dou
23b10 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
23b20 78 67 65 6f 6d 29 3b 0a 20 20 20 20 73 74 72 63  xgeom);.    strc
23b30 70 79 20 28 78 74 61 62 6c 65 2c 20 28 63 6f 6e  py (xtable, (con
23b40 73 74 20 63 68 61 72 20 2a 29 20 74 61 62 6c 65  st char *) table
23b50 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75  );.    double_qu
23b60 6f 74 65 64 5f 73 71 6c 20 28 78 74 61 62 6c 65  oted_sql (xtable
23b70 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  );.    sprintf (
23b80 69 64 78 5f 6e 61 6d 65 2c 20 22 69 64 78 5f 25  idx_name, "idx_%
23b90 73 5f 25 73 22 2c 20 74 61 62 6c 65 2c 20 67 65  s_%s", table, ge
23ba0 6f 6d 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  om);.    double_
23bb0 71 75 6f 74 65 64 5f 73 71 6c 20 28 69 64 78 5f  quoted_sql (idx_
23bc0 6e 61 6d 65 29 3b 0a 0a 2f 2a 20 63 6f 75 6e 74  name);../* count
23bd0 69 6e 67 20 68 6f 77 20 6d 61 6e 79 20 47 65 6f  ing how many Geo
23be0 6d 65 74 72 69 65 73 20 61 72 65 20 73 65 74 20  metries are set 
23bf0 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 2d 74 61  into the main-ta
23c00 62 6c 65 20 2a 2f 0a 20 20 20 20 73 70 72 69 6e  ble */.    sprin
23c10 74 66 20 28 73 71 6c 2c 20 22 53 45 4c 45 43 54  tf (sql, "SELECT
23c20 20 43 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 25   Count(*) FROM %
23c30 73 20 22 2c 20 78 74 61 62 6c 65 29 3b 0a 20 20  s ", xtable);.  
23c40 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c    sprintf (sql2,
23c50 20 22 57 48 45 52 45 20 53 54 5f 47 65 6f 6d 65   "WHERE ST_Geome
23c60 74 72 79 54 79 70 65 28 25 73 29 20 49 53 20 4e  tryType(%s) IS N
23c70 4f 54 20 4e 55 4c 4c 22 2c 20 78 67 65 6f 6d 29  OT NULL", xgeom)
23c80 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
23c90 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 72 65  l, sql2);.    re
23ca0 74 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  t = sqlite3_prep
23cb0 61 72 65 5f 76 32 20 28 73 71 6c 69 74 65 2c 20  are_v2 (sqlite, 
23cc0 73 71 6c 2c 20 73 74 72 6c 65 6e 20 28 73 71 6c  sql, strlen (sql
23cd0 29 2c 20 26 73 74 6d 74 2c 20 4e 55 4c 4c 29 3b  ), &stmt, NULL);
23ce0 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
23cf0 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
23d00 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
23d10 5f 65 20 28 22 43 68 65 63 6b 53 70 61 74 69 61  _e ("CheckSpatia
23d20 6c 49 6e 64 65 78 20 53 51 4c 20 65 72 72 6f 72  lIndex SQL error
23d30 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 73 71 6c 69  : %s\n",....sqli
23d40 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71 6c 69  te3_errmsg (sqli
23d50 74 65 29 29 3b 0a 09 20 20 72 65 74 75 72 6e 20  te));..  return 
23d60 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
23d70 77 68 69 6c 65 20 28 31 29 0a 20 20 20 20 20 20  while (1).      
23d80 7b 0a 09 20 20 72 65 74 20 3d 20 73 71 6c 69 74  {..  ret = sqlit
23d90 65 33 5f 73 74 65 70 20 28 73 74 6d 74 29 3b 0a  e3_step (stmt);.
23da0 09 20 20 69 66 20 28 72 65 74 20 3d 3d 20 53 51  .  if (ret == SQ
23db0 4c 49 54 45 5f 44 4f 4e 45 29 0a 09 20 20 20 20  LITE_DONE)..    
23dc0 20 20 62 72 65 61 6b 3b 0a 09 20 20 69 66 20 28    break;..  if (
23dd0 72 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 52 4f  ret == SQLITE_RO
23de0 57 29 0a 09 20 20 20 20 20 20 63 6f 75 6e 74 5f  W)..      count_
23df0 67 65 6f 6d 20 3d 20 73 71 6c 69 74 65 33 5f 63  geom = sqlite3_c
23e00 6f 6c 75 6d 6e 5f 69 6e 74 20 28 73 74 6d 74 2c  olumn_int (stmt,
23e10 20 30 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20   0);..  else..  
23e20 20 20 7b 0a 09 09 70 72 69 6e 74 66 20 28 22 73    {...printf ("s
23e30 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 72  qlite3_step() er
23e40 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 69  ror: %s\n", sqli
23e50 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71 6c 69  te3_errmsg (sqli
23e60 74 65 29 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f  te));...sqlite3_
23e70 66 69 6e 61 6c 69 7a 65 20 28 73 74 6d 74 29 3b  finalize (stmt);
23e80 0a 09 09 72 65 74 75 72 6e 20 2d 31 3b 0a 09 20  ...return -1;.. 
23e90 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
23ea0 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
23eb0 65 20 28 73 74 6d 74 29 3b 0a 0a 2f 2a 20 63 6f  e (stmt);../* co
23ec0 75 6e 74 69 6e 67 20 68 6f 77 20 6d 61 6e 79 20  unting how many 
23ed0 52 2a 54 72 65 65 20 65 6e 74 72 69 65 73 20 61  R*Tree entries a
23ee0 72 65 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20  re defined */.  
23ef0 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
23f00 22 53 45 4c 45 43 54 20 43 6f 75 6e 74 28 2a 29  "SELECT Count(*)
23f10 20 46 52 4f 4d 20 25 73 22 2c 20 69 64 78 5f 6e   FROM %s", idx_n
23f20 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  ame);.    ret = 
23f30 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
23f40 76 32 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c  v2 (sqlite, sql,
23f50 20 73 74 72 6c 65 6e 20 28 73 71 6c 29 2c 20 26   strlen (sql), &
23f60 73 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20  stmt, NULL);.   
23f70 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
23f80 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09  TE_OK).      {..
23f90 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28    spatialite_e (
23fa0 22 43 68 65 63 6b 53 70 61 74 69 61 6c 49 6e 64  "CheckSpatialInd
23fb0 65 78 20 53 51 4c 20 65 72 72 6f 72 3a 20 25 73  ex SQL error: %s
23fc0 5c 6e 22 2c 0a 09 09 09 73 71 6c 69 74 65 33 5f  \n",....sqlite3_
23fd0 65 72 72 6d 73 67 20 28 73 71 6c 69 74 65 29 29  errmsg (sqlite))
23fe0 3b 0a 09 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a  ;..  return -1;.
23ff0 20 20 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c        }.    whil
24000 65 20 28 31 29 0a 20 20 20 20 20 20 7b 0a 09 20  e (1).      {.. 
24010 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 73   ret = sqlite3_s
24020 74 65 70 20 28 73 74 6d 74 29 3b 0a 09 20 20 69  tep (stmt);..  i
24030 66 20 28 72 65 74 20 3d 3d 20 53 51 4c 49 54 45  f (ret == SQLITE
24040 5f 44 4f 4e 45 29 0a 09 20 20 20 20 20 20 62 72  _DONE)..      br
24050 65 61 6b 3b 0a 09 20 20 69 66 20 28 72 65 74 20  eak;..  if (ret 
24060 3d 3d 20 53 51 4c 49 54 45 5f 52 4f 57 29 0a 09  == SQLITE_ROW)..
24070 20 20 20 20 20 20 63 6f 75 6e 74 5f 72 74 72 65        count_rtre
24080 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
24090 6d 6e 5f 69 6e 74 20 28 73 74 6d 74 2c 20 30 29  mn_int (stmt, 0)
240a0 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b  ;..  else..    {
240b0 0a 09 09 70 72 69 6e 74 66 20 28 22 73 71 6c 69  ...printf ("sqli
240c0 74 65 33 5f 73 74 65 70 28 29 20 65 72 72 6f 72  te3_step() error
240d0 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 69 74 65 33  : %s\n", sqlite3
240e0 5f 65 72 72 6d 73 67 20 28 73 71 6c 69 74 65 29  _errmsg (sqlite)
240f0 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 66 69 6e  );...sqlite3_fin
24100 61 6c 69 7a 65 20 28 73 74 6d 74 29 3b 0a 09 09  alize (stmt);...
24110 72 65 74 75 72 6e 20 2d 31 3b 0a 09 20 20 20 20  return -1;..    
24120 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 73 71  }.      }.    sq
24130 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28  lite3_finalize (
24140 73 74 6d 74 29 3b 0a 20 20 20 20 69 66 20 28 63  stmt);.    if (c
24150 6f 75 6e 74 5f 67 65 6f 6d 20 21 3d 20 63 6f 75  ount_geom != cou
24160 6e 74 5f 72 74 72 65 65 29 0a 20 20 20 20 20 20  nt_rtree).      
24170 7b 0a 09 20 20 2f 2a 20 75 6e 65 78 70 65 63 74  {..  /* unexpect
24180 65 64 20 63 6f 75 6e 74 20 64 69 66 66 65 72 65  ed count differe
24190 6e 63 65 20 2a 2f 0a 09 20 20 72 65 74 75 72 6e  nce */..  return
241a0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 2f 2a 20   0;.      }../* 
241b0 63 68 65 63 6b 69 6e 67 20 74 68 65 20 67 65 6f  checking the geo
241c0 6d 65 74 72 79 2d 74 61 62 6c 65 20 61 67 61 69  metry-table agai
241d0 6e 73 74 20 74 68 65 20 63 6f 72 72 65 73 70 6f  nst the correspo
241e0 6e 64 69 6e 67 20 52 2a 54 72 65 65 20 2a 2f 0a  nding R*Tree */.
241f0 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
24200 2c 20 22 53 45 4c 45 43 54 20 22 29 3b 0a 20 20  , "SELECT ");.  
24210 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c    sprintf (sql2,
24220 20 22 4d 62 72 4d 69 6e 58 28 67 2e 25 73 29 2c   "MbrMinX(g.%s),
24230 20 22 2c 20 78 67 65 6f 6d 29 3b 0a 20 20 20 20   ", xgeom);.    
24240 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c  strcat (sql, sql
24250 32 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  2);.    sprintf 
24260 28 73 71 6c 32 2c 20 22 4d 62 72 4d 69 6e 59 28  (sql2, "MbrMinY(
24270 67 2e 25 73 29 2c 20 22 2c 20 78 67 65 6f 6d 29  g.%s), ", xgeom)
24280 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
24290 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73 70  l, sql2);.    sp
242a0 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 4d 62  rintf (sql2, "Mb
242b0 72 4d 61 78 58 28 67 2e 25 73 29 2c 20 22 2c 20  rMaxX(g.%s), ", 
242c0 78 67 65 6f 6d 29 3b 0a 20 20 20 20 73 74 72 63  xgeom);.    strc
242d0 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a  at (sql, sql2);.
242e0 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
242f0 32 2c 20 22 4d 62 72 4d 61 78 59 28 67 2e 25 73  2, "MbrMaxY(g.%s
24300 29 2c 20 22 2c 20 78 67 65 6f 6d 29 3b 0a 20 20  ), ", xgeom);.  
24310 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
24320 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ql2);.    strcat
24330 20 28 73 71 6c 2c 20 22 69 2e 78 6d 69 6e 2c 20   (sql, "i.xmin, 
24340 69 2e 79 6d 69 6e 2c 20 69 2e 78 6d 61 78 2c 20  i.ymin, i.xmax, 
24350 69 2e 79 6d 61 78 5c 6e 22 29 3b 0a 20 20 20 20  i.ymax\n");.    
24360 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22  sprintf (sql2, "
24370 46 52 4f 4d 20 25 73 20 41 53 20 67 5c 6e 22 2c  FROM %s AS g\n",
24380 20 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74   xtable);.    st
24390 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29  rcat (sql, sql2)
243a0 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73  ;.    sprintf (s
243b0 71 6c 32 2c 20 22 4c 45 46 54 20 4a 4f 49 4e 20  ql2, "LEFT JOIN 
243c0 25 73 20 41 53 20 69 20 22 2c 20 69 64 78 5f 6e  %s AS i ", idx_n
243d0 61 6d 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ame);.    strcat
243e0 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20   (sql, sql2);.  
243f0 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
24400 4f 4e 20 28 67 2e 52 4f 57 49 44 20 3d 20 69 2e  ON (g.ROWID = i.
24410 70 6b 69 64 29 22 29 3b 0a 20 20 20 20 72 65 74  pkid)");.    ret
24420 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61   = sqlite3_prepa
24430 72 65 5f 76 32 20 28 73 71 6c 69 74 65 2c 20 73  re_v2 (sqlite, s
24440 71 6c 2c 20 73 74 72 6c 65 6e 20 28 73 71 6c 29  ql, strlen (sql)
24450 2c 20 26 73 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a  , &stmt, NULL);.
24460 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53      if (ret != S
24470 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20  QLITE_OK).      
24480 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
24490 65 20 28 22 43 68 65 63 6b 53 70 61 74 69 61 6c  e ("CheckSpatial
244a0 49 6e 64 65 78 20 53 51 4c 20 65 72 72 6f 72 3a  Index SQL error:
244b0 20 25 73 5c 6e 22 2c 0a 09 09 09 73 71 6c 69 74   %s\n",....sqlit
244c0 65 33 5f 65 72 72 6d 73 67 20 28 73 71 6c 69 74  e3_errmsg (sqlit
244d0 65 29 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 2d  e));..  return -
244e0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 77  1;.      }.    w
244f0 68 69 6c 65 20 28 31 29 0a 20 20 20 20 20 20 7b  hile (1).      {
24500 0a 09 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65  ..  ret = sqlite
24510 33 5f 73 74 65 70 20 28 73 74 6d 74 29 3b 0a 09  3_step (stmt);..
24520 20 20 69 66 20 28 72 65 74 20 3d 3d 20 53 51 4c    if (ret == SQL
24530 49 54 45 5f 44 4f 4e 45 29 0a 09 20 20 20 20 20  ITE_DONE)..     
24540 20 62 72 65 61 6b 3b 0a 09 20 20 69 66 20 28 72   break;..  if (r
24550 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 52 4f 57  et == SQLITE_ROW
24560 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 63 68  )..    {.../* ch
24570 65 63 6b 69 6e 67 20 61 20 72 6f 77 20 2a 2f 0a  ecking a row */.
24580 09 09 6f 6b 5f 67 5f 78 6d 69 6e 20 3d 20 31 3b  ..ok_g_xmin = 1;
24590 0a 09 09 6f 6b 5f 67 5f 79 6d 69 6e 20 3d 20 31  ...ok_g_ymin = 1
245a0 3b 0a 09 09 6f 6b 5f 67 5f 78 6d 61 78 20 3d 20  ;...ok_g_xmax = 
245b0 31 3b 0a 09 09 6f 6b 5f 67 5f 79 6d 61 78 20 3d  1;...ok_g_ymax =
245c0 20 31 3b 0a 09 09 6f 6b 5f 69 5f 78 6d 69 6e 20   1;...ok_i_xmin 
245d0 3d 20 31 3b 0a 09 09 6f 6b 5f 69 5f 79 6d 69 6e  = 1;...ok_i_ymin
245e0 20 3d 20 31 3b 0a 09 09 6f 6b 5f 69 5f 78 6d 61   = 1;...ok_i_xma
245f0 78 20 3d 20 31 3b 0a 09 09 6f 6b 5f 69 5f 79 6d  x = 1;...ok_i_ym
24600 61 78 20 3d 20 31 3b 0a 09 09 69 66 20 28 73 71  ax = 1;...if (sq
24610 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
24620 65 20 28 73 74 6d 74 2c 20 30 29 20 3d 3d 20 53  e (stmt, 0) == S
24630 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
24640 20 20 6f 6b 5f 67 5f 78 6d 69 6e 20 3d 20 30 3b    ok_g_xmin = 0;
24650 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67 5f  ...else...    g_
24660 78 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63  xmin = sqlite3_c
24670 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24680 6d 74 2c 20 30 29 3b 0a 09 09 69 66 20 28 73 71  mt, 0);...if (sq
24690 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
246a0 65 20 28 73 74 6d 74 2c 20 31 29 20 3d 3d 20 53  e (stmt, 1) == S
246b0 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
246c0 20 20 6f 6b 5f 67 5f 79 6d 69 6e 20 3d 20 30 3b    ok_g_ymin = 0;
246d0 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67 5f  ...else...    g_
246e0 79 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63  ymin = sqlite3_c
246f0 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24700 6d 74 2c 20 31 29 3b 0a 09 09 69 66 20 28 73 71  mt, 1);...if (sq
24710 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
24720 65 20 28 73 74 6d 74 2c 20 32 29 20 3d 3d 20 53  e (stmt, 2) == S
24730 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
24740 20 20 6f 6b 5f 67 5f 78 6d 61 78 20 3d 20 30 3b    ok_g_xmax = 0;
24750 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67 5f  ...else...    g_
24760 78 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 63  xmax = sqlite3_c
24770 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24780 6d 74 2c 20 32 29 3b 0a 09 09 69 66 20 28 73 71  mt, 2);...if (sq
24790 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
247a0 65 20 28 73 74 6d 74 2c 20 33 29 20 3d 3d 20 53  e (stmt, 3) == S
247b0 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
247c0 20 20 6f 6b 5f 67 5f 79 6d 61 78 20 3d 20 30 3b    ok_g_ymax = 0;
247d0 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67 5f  ...else...    g_
247e0 79 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 63  ymax = sqlite3_c
247f0 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24800 6d 74 2c 20 33 29 3b 0a 09 09 69 66 20 28 73 71  mt, 3);...if (sq
24810 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
24820 65 20 28 73 74 6d 74 2c 20 34 29 20 3d 3d 20 53  e (stmt, 4) == S
24830 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
24840 20 20 6f 6b 5f 69 5f 78 6d 69 6e 20 3d 20 30 3b    ok_i_xmin = 0;
24850 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69 5f  ...else...    i_
24860 78 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63  xmin = sqlite3_c
24870 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24880 6d 74 2c 20 34 29 3b 0a 09 09 69 66 20 28 73 71  mt, 4);...if (sq
24890 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
248a0 65 20 28 73 74 6d 74 2c 20 35 29 20 3d 3d 20 53  e (stmt, 5) == S
248b0 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
248c0 20 20 6f 6b 5f 69 5f 79 6d 69 6e 20 3d 20 30 3b    ok_i_ymin = 0;
248d0 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69 5f  ...else...    i_
248e0 79 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63  ymin = sqlite3_c
248f0 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24900 6d 74 2c 20 35 29 3b 0a 09 09 69 66 20 28 73 71  mt, 5);...if (sq
24910 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
24920 65 20 28 73 74 6d 74 2c 20 36 29 20 3d 3d 20 53  e (stmt, 6) == S
24930 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
24940 20 20 6f 6b 5f 69 5f 78 6d 61 78 20 3d 20 30 3b    ok_i_xmax = 0;
24950 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69 5f  ...else...    i_
24960 78 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 63  xmax = sqlite3_c
24970 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24980 6d 74 2c 20 36 29 3b 0a 09 09 69 66 20 28 73 71  mt, 6);...if (sq
24990 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
249a0 65 20 28 73 74 6d 74 2c 20 37 29 20 3d 3d 20 53  e (stmt, 7) == S
249b0 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20 20  QLITE_NULL)...  
249c0 20 20 6f 6b 5f 69 5f 79 6d 61 78 20 3d 20 30 3b    ok_i_ymax = 0;
249d0 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69 5f  ...else...    i_
249e0 79 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 63  ymax = sqlite3_c
249f0 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73 74  olumn_double (st
24a00 6d 74 2c 20 37 29 3b 0a 09 09 69 66 20 28 65 76  mt, 7);...if (ev
24a10 61 6c 5f 72 74 72 65 65 5f 65 6e 74 72 79 20 28  al_rtree_entry (
24a20 6f 6b 5f 67 5f 78 6d 69 6e 2c 20 67 5f 78 6d 69  ok_g_xmin, g_xmi
24a30 6e 2c 20 6f 6b 5f 69 5f 78 6d 69 6e 2c 20 69 5f  n, ok_i_xmin, i_
24a40 78 6d 69 6e 29 20 3d 3d 0a 09 09 20 20 20 20 30  xmin) ==...    0
24a50 29 0a 09 09 20 20 20 20 67 6f 74 6f 20 6d 69 73  )...    goto mis
24a60 6d 61 74 63 68 69 6e 67 3b 0a 09 09 69 66 20 28  matching;...if (
24a70 65 76 61 6c 5f 72 74 72 65 65 5f 65 6e 74 72 79  eval_rtree_entry
24a80 20 28 6f 6b 5f 67 5f 79 6d 69 6e 2c 20 67 5f 79   (ok_g_ymin, g_y
24a90 6d 69 6e 2c 20 6f 6b 5f 69 5f 79 6d 69 6e 2c 20  min, ok_i_ymin, 
24aa0 69 5f 79 6d 69 6e 29 20 3d 3d 0a 09 09 20 20 20  i_ymin) ==...   
24ab0 20 30 29 0a 09 09 20 20 20 20 67 6f 74 6f 20 6d   0)...    goto m
24ac0 69 73 6d 61 74 63 68 69 6e 67 3b 0a 09 09 69 66  ismatching;...if
24ad0 20 28 65 76 61 6c 5f 72 74 72 65 65 5f 65 6e 74   (eval_rtree_ent
24ae0 72 79 20 28 6f 6b 5f 67 5f 78 6d 61 78 2c 20 67  ry (ok_g_xmax, g
24af0 5f 78 6d 61 78 2c 20 6f 6b 5f 69 5f 78 6d 61 78  _xmax, ok_i_xmax
24b00 2c 20 69 5f 78 6d 61 78 29 20 3d 3d 0a 09 09 20  , i_xmax) ==... 
24b10 20 20 20 30 29 0a 09 09 20 20 20 20 67 6f 74 6f     0)...    goto
24b20 20 6d 69 73 6d 61 74 63 68 69 6e 67 3b 0a 09 09   mismatching;...
24b30 69 66 20 28 65 76 61 6c 5f 72 74 72 65 65 5f 65  if (eval_rtree_e
24b40 6e 74 72 79 20 28 6f 6b 5f 67 5f 79 6d 61 78 2c  ntry (ok_g_ymax,
24b50 20 67 5f 79 6d 61 78 2c 20 6f 6b 5f 69 5f 79 6d   g_ymax, ok_i_ym
24b60 61 78 2c 20 69 5f 79 6d 61 78 29 20 3d 3d 0a 09  ax, i_ymax) ==..
24b70 09 20 20 20 20 30 29 0a 09 09 20 20 20 20 67 6f  .    0)...    go
24b80 74 6f 20 6d 69 73 6d 61 74 63 68 69 6e 67 3b 0a  to mismatching;.
24b90 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09  .    }..  else..
24ba0 20 20 20 20 7b 0a 09 09 70 72 69 6e 74 66 20 28      {...printf (
24bb0 22 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20  "sqlite3_step() 
24bc0 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 73 71  error: %s\n", sq
24bd0 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71  lite3_errmsg (sq
24be0 6c 69 74 65 29 29 3b 0a 09 09 73 71 6c 69 74 65  lite));...sqlite
24bf0 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73 74 6d 74  3_finalize (stmt
24c00 29 3b 0a 09 09 72 65 74 75 72 6e 20 2d 31 3b 0a  );...return -1;.
24c10 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 2f  .    }.      }./
24c20 2a 20 77 65 20 68 61 76 65 20 6e 6f 77 20 74 6f  * we have now to
24c30 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 71 75   finalize the qu
24c40 65 72 79 20 5b 6d 65 6d 6f 72 79 20 63 6c 65 61  ery [memory clea
24c50 6e 75 70 5d 20 2a 2f 0a 20 20 20 20 73 71 6c 69  nup] */.    sqli
24c60 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73 74  te3_finalize (st
24c70 6d 74 29 3b 0a 0a 0a 2f 2a 20 6e 6f 77 20 77 65  mt);.../* now we
24c80 27 6c 6c 20 63 68 65 63 6b 20 74 68 65 20 52 2a  'll check the R*
24c90 54 72 65 65 20 61 67 61 69 6e 73 74 20 74 68 65  Tree against the
24ca0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 67   corresponding g
24cb0 65 6f 6d 65 74 72 79 2d 74 61 62 6c 65 20 2a 2f  eometry-table */
24cc0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
24cd0 6c 2c 20 22 53 45 4c 45 43 54 20 22 29 3b 0a 20  l, "SELECT ");. 
24ce0 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32     sprintf (sql2
24cf0 2c 20 22 4d 62 72 4d 69 6e 58 28 67 2e 25 73 29  , "MbrMinX(g.%s)
24d00 2c 20 22 2c 20 78 67 65 6f 6d 29 3b 0a 20 20 20  , ", xgeom);.   
24d10 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71   strcat (sql, sq
24d20 6c 32 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66  l2);.    sprintf
24d30 20 28 73 71 6c 32 2c 20 22 4d 62 72 4d 69 6e 59   (sql2, "MbrMinY
24d40 28 67 2e 25 73 29 2c 20 22 2c 20 78 67 65 6f 6d  (g.%s), ", xgeom
24d50 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
24d60 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73  ql, sql2);.    s
24d70 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 4d  printf (sql2, "M
24d80 62 72 4d 61 78 58 28 67 2e 25 73 29 2c 20 22 2c  brMaxX(g.%s), ",
24d90 20 78 67 65 6f 6d 29 3b 0a 20 20 20 20 73 74 72   xgeom);.    str
24da0 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
24db0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
24dc0 6c 32 2c 20 22 4d 62 72 4d 61 78 59 28 67 2e 25  l2, "MbrMaxY(g.%
24dd0 73 29 2c 20 22 2c 20 78 67 65 6f 6d 29 3b 0a 20  s), ", xgeom);. 
24de0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
24df0 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61  sql2);.    strca
24e00 74 20 28 73 71 6c 2c 20 22 69 2e 78 6d 69 6e 2c  t (sql, "i.xmin,
24e10 20 69 2e 79 6d 69 6e 2c 20 69 2e 78 6d 61 78 2c   i.ymin, i.xmax,
24e20 20 69 2e 79 6d 61 78 5c 6e 22 29 3b 0a 20 20 20   i.ymax\n");.   
24e30 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20   sprintf (sql2, 
24e40 22 46 52 4f 4d 20 25 73 20 41 53 20 69 5c 6e 22  "FROM %s AS i\n"
24e50 2c 20 69 64 78 5f 6e 61 6d 65 29 3b 0a 20 20 20  , idx_name);.   
24e60 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71   strcat (sql, sq
24e70 6c 32 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66  l2);.    sprintf
24e80 20 28 73 71 6c 32 2c 20 22 4c 45 46 54 20 4a 4f   (sql2, "LEFT JO
24e90 49 4e 20 25 73 20 41 53 20 67 20 22 2c 20 78 74  IN %s AS g ", xt
24ea0 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
24eb0 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20  t (sql, sql2);. 
24ec0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
24ed0 22 4f 4e 20 28 67 2e 52 4f 57 49 44 20 3d 20 69  "ON (g.ROWID = i
24ee0 2e 70 6b 69 64 29 22 29 3b 0a 20 20 20 20 72 65  .pkid)");.    re
24ef0 74 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  t = sqlite3_prep
24f00 61 72 65 5f 76 32 20 28 73 71 6c 69 74 65 2c 20  are_v2 (sqlite, 
24f10 73 71 6c 2c 20 73 74 72 6c 65 6e 20 28 73 71 6c  sql, strlen (sql
24f20 29 2c 20 26 73 74 6d 74 2c 20 4e 55 4c 4c 29 3b  ), &stmt, NULL);
24f30 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
24f40 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
24f50 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
24f60 5f 65 20 28 22 43 68 65 63 6b 53 70 61 74 69 61  _e ("CheckSpatia
24f70 6c 49 6e 64 65 78 20 53 51 4c 20 65 72 72 6f 72  lIndex SQL error
24f80 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 73 71 6c 69  : %s\n",....sqli
24f90 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71 6c 69  te3_errmsg (sqli
24fa0 74 65 29 29 3b 0a 09 20 20 72 65 74 75 72 6e 20  te));..  return 
24fb0 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
24fc0 77 68 69 6c 65 20 28 31 29 0a 20 20 20 20 20 20  while (1).      
24fd0 7b 0a 09 20 20 72 65 74 20 3d 20 73 71 6c 69 74  {..  ret = sqlit
24fe0 65 33 5f 73 74 65 70 20 28 73 74 6d 74 29 3b 0a  e3_step (stmt);.
24ff0 09 20 20 69 66 20 28 72 65 74 20 3d 3d 20 53 51  .  if (ret == SQ
25000 4c 49 54 45 5f 44 4f 4e 45 29 0a 09 20 20 20 20  LITE_DONE)..    
25010 20 20 62 72 65 61 6b 3b 0a 09 20 20 69 66 20 28    break;..  if (
25020 72 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 52 4f  ret == SQLITE_RO
25030 57 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 63  W)..    {.../* c
25040 68 65 63 6b 69 6e 67 20 61 20 72 6f 77 20 2a 2f  hecking a row */
25050 0a 09 09 6f 6b 5f 67 5f 78 6d 69 6e 20 3d 20 31  ...ok_g_xmin = 1
25060 3b 0a 09 09 6f 6b 5f 67 5f 79 6d 69 6e 20 3d 20  ;...ok_g_ymin = 
25070 31 3b 0a 09 09 6f 6b 5f 67 5f 78 6d 61 78 20 3d  1;...ok_g_xmax =
25080 20 31 3b 0a 09 09 6f 6b 5f 67 5f 79 6d 61 78 20   1;...ok_g_ymax 
25090 3d 20 31 3b 0a 09 09 6f 6b 5f 69 5f 78 6d 69 6e  = 1;...ok_i_xmin
250a0 20 3d 20 31 3b 0a 09 09 6f 6b 5f 69 5f 79 6d 69   = 1;...ok_i_ymi
250b0 6e 20 3d 20 31 3b 0a 09 09 6f 6b 5f 69 5f 78 6d  n = 1;...ok_i_xm
250c0 61 78 20 3d 20 31 3b 0a 09 09 6f 6b 5f 69 5f 79  ax = 1;...ok_i_y
250d0 6d 61 78 20 3d 20 31 3b 0a 09 09 69 66 20 28 73  max = 1;...if (s
250e0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
250f0 70 65 20 28 73 74 6d 74 2c 20 30 29 20 3d 3d 20  pe (stmt, 0) == 
25100 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25110 20 20 20 6f 6b 5f 67 5f 78 6d 69 6e 20 3d 20 30     ok_g_xmin = 0
25120 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67  ;...else...    g
25130 5f 78 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f  _xmin = sqlite3_
25140 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
25150 74 6d 74 2c 20 30 29 3b 0a 09 09 69 66 20 28 73  tmt, 0);...if (s
25160 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
25170 70 65 20 28 73 74 6d 74 2c 20 31 29 20 3d 3d 20  pe (stmt, 1) == 
25180 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25190 20 20 20 6f 6b 5f 67 5f 79 6d 69 6e 20 3d 20 30     ok_g_ymin = 0
251a0 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67  ;...else...    g
251b0 5f 79 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f  _ymin = sqlite3_
251c0 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
251d0 74 6d 74 2c 20 31 29 3b 0a 09 09 69 66 20 28 73  tmt, 1);...if (s
251e0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
251f0 70 65 20 28 73 74 6d 74 2c 20 32 29 20 3d 3d 20  pe (stmt, 2) == 
25200 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25210 20 20 20 6f 6b 5f 67 5f 78 6d 61 78 20 3d 20 30     ok_g_xmax = 0
25220 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67  ;...else...    g
25230 5f 78 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f  _xmax = sqlite3_
25240 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
25250 74 6d 74 2c 20 32 29 3b 0a 09 09 69 66 20 28 73  tmt, 2);...if (s
25260 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
25270 70 65 20 28 73 74 6d 74 2c 20 33 29 20 3d 3d 20  pe (stmt, 3) == 
25280 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25290 20 20 20 6f 6b 5f 67 5f 79 6d 61 78 20 3d 20 30     ok_g_ymax = 0
252a0 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 67  ;...else...    g
252b0 5f 79 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f  _ymax = sqlite3_
252c0 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
252d0 74 6d 74 2c 20 33 29 3b 0a 09 09 69 66 20 28 73  tmt, 3);...if (s
252e0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
252f0 70 65 20 28 73 74 6d 74 2c 20 34 29 20 3d 3d 20  pe (stmt, 4) == 
25300 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25310 20 20 20 6f 6b 5f 69 5f 78 6d 69 6e 20 3d 20 30     ok_i_xmin = 0
25320 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69  ;...else...    i
25330 5f 78 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f  _xmin = sqlite3_
25340 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
25350 74 6d 74 2c 20 34 29 3b 0a 09 09 69 66 20 28 73  tmt, 4);...if (s
25360 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
25370 70 65 20 28 73 74 6d 74 2c 20 35 29 20 3d 3d 20  pe (stmt, 5) == 
25380 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25390 20 20 20 6f 6b 5f 69 5f 79 6d 69 6e 20 3d 20 30     ok_i_ymin = 0
253a0 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69  ;...else...    i
253b0 5f 79 6d 69 6e 20 3d 20 73 71 6c 69 74 65 33 5f  _ymin = sqlite3_
253c0 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
253d0 74 6d 74 2c 20 35 29 3b 0a 09 09 69 66 20 28 73  tmt, 5);...if (s
253e0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
253f0 70 65 20 28 73 74 6d 74 2c 20 36 29 20 3d 3d 20  pe (stmt, 6) == 
25400 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25410 20 20 20 6f 6b 5f 69 5f 78 6d 61 78 20 3d 20 30     ok_i_xmax = 0
25420 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69  ;...else...    i
25430 5f 78 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f  _xmax = sqlite3_
25440 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
25450 74 6d 74 2c 20 36 29 3b 0a 09 09 69 66 20 28 73  tmt, 6);...if (s
25460 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
25470 70 65 20 28 73 74 6d 74 2c 20 37 29 20 3d 3d 20  pe (stmt, 7) == 
25480 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 09 09 20  SQLITE_NULL)... 
25490 20 20 20 6f 6b 5f 69 5f 79 6d 61 78 20 3d 20 30     ok_i_ymax = 0
254a0 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20 69  ;...else...    i
254b0 5f 79 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f  _ymax = sqlite3_
254c0 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20 28 73  column_double (s
254d0 74 6d 74 2c 20 37 29 3b 0a 09 09 69 66 20 28 65  tmt, 7);...if (e
254e0 76 61 6c 5f 72 74 72 65 65 5f 65 6e 74 72 79 20  val_rtree_entry 
254f0 28 6f 6b 5f 67 5f 78 6d 69 6e 2c 20 67 5f 78 6d  (ok_g_xmin, g_xm
25500 69 6e 2c 20 6f 6b 5f 69 5f 78 6d 69 6e 2c 20 69  in, ok_i_xmin, i
25510 5f 78 6d 69 6e 29 20 3d 3d 0a 09 09 20 20 20 20  _xmin) ==...    
25520 30 29 0a 09 09 20 20 20 20 67 6f 74 6f 20 6d 69  0)...    goto mi
25530 73 6d 61 74 63 68 69 6e 67 3b 0a 09 09 69 66 20  smatching;...if 
25540 28 65 76 61 6c 5f 72 74 72 65 65 5f 65 6e 74 72  (eval_rtree_entr
25550 79 20 28 6f 6b 5f 67 5f 79 6d 69 6e 2c 20 67 5f  y (ok_g_ymin, g_
25560 79 6d 69 6e 2c 20 6f 6b 5f 69 5f 79 6d 69 6e 2c  ymin, ok_i_ymin,
25570 20 69 5f 79 6d 69 6e 29 20 3d 3d 0a 09 09 20 20   i_ymin) ==...  
25580 20 20 30 29 0a 09 09 20 20 20 20 67 6f 74 6f 20    0)...    goto 
25590 6d 69 73 6d 61 74 63 68 69 6e 67 3b 0a 09 09 69  mismatching;...i
255a0 66 20 28 65 76 61 6c 5f 72 74 72 65 65 5f 65 6e  f (eval_rtree_en
255b0 74 72 79 20 28 6f 6b 5f 67 5f 78 6d 61 78 2c 20  try (ok_g_xmax, 
255c0 67 5f 78 6d 61 78 2c 20 6f 6b 5f 69 5f 78 6d 61  g_xmax, ok_i_xma
255d0 78 2c 20 69 5f 78 6d 61 78 29 20 3d 3d 0a 09 09  x, i_xmax) ==...
255e0 20 20 20 20 30 29 0a 09 09 20 20 20 20 67 6f 74      0)...    got
255f0 6f 20 6d 69 73 6d 61 74 63 68 69 6e 67 3b 0a 09  o mismatching;..
25600 09 69 66 20 28 65 76 61 6c 5f 72 74 72 65 65 5f  .if (eval_rtree_
25610 65 6e 74 72 79 20 28 6f 6b 5f 67 5f 79 6d 61 78  entry (ok_g_ymax
25620 2c 20 67 5f 79 6d 61 78 2c 20 6f 6b 5f 69 5f 79  , g_ymax, ok_i_y
25630 6d 61 78 2c 20 69 5f 79 6d 61 78 29 20 3d 3d 0a  max, i_ymax) ==.
25640 09 09 20 20 20 20 30 29 0a 09 09 20 20 20 20 67  ..    0)...    g
25650 6f 74 6f 20 6d 69 73 6d 61 74 63 68 69 6e 67 3b  oto mismatching;
25660 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a  ..    }..  else.
25670 09 20 20 20 20 7b 0a 09 09 70 72 69 6e 74 66 20  .    {...printf 
25680 28 22 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  ("sqlite3_step()
25690 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 73   error: %s\n", s
256a0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 28 73  qlite3_errmsg (s
256b0 71 6c 69 74 65 29 29 3b 0a 09 09 73 71 6c 69 74  qlite));...sqlit
256c0 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73 74 6d  e3_finalize (stm
256d0 74 29 3b 0a 09 09 72 65 74 75 72 6e 20 2d 31 3b  t);...return -1;
256e0 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a  ..    }.      }.
256f0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
25700 6c 69 7a 65 20 28 73 74 6d 74 29 3b 0a 20 20 20  lize (stmt);.   
25710 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 43   strcpy (sql, "C
25720 68 65 63 6b 20 53 70 61 74 69 61 6c 49 6e 64 65  heck SpatialInde
25730 78 3a 20 69 73 20 76 61 6c 69 64 22 29 3b 0a 20  x: is valid");. 
25740 20 20 20 75 70 64 61 74 65 53 70 61 74 69 61 4c     updateSpatiaL
25750 69 74 65 48 69 73 74 6f 72 79 20 28 73 71 6c 69  iteHistory (sqli
25760 74 65 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 20  te, (const char 
25770 2a 29 20 74 61 62 6c 65 2c 0a 09 09 09 20 20 20  *) table,....   
25780 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29    (const char *)
25790 20 67 65 6f 6d 2c 20 73 71 6c 29 3b 0a 20 20 20   geom, sql);.   
257a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 6d 69 73   return 1;.  mis
257b0 6d 61 74 63 68 69 6e 67 3a 0a 20 20 20 20 73 71  matching:.    sq
257c0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28  lite3_finalize (
257d0 73 74 6d 74 29 3b 0a 20 20 20 20 73 74 72 63 70  stmt);.    strcp
257e0 79 20 28 73 71 6c 2c 20 22 43 68 65 63 6b 20 53  y (sql, "Check S
257f0 70 61 74 69 61 6c 49 6e 64 65 78 3a 20 49 4e 43  patialIndex: INC
25800 4f 4e 53 49 53 54 45 4e 43 49 45 53 20 64 65 74  ONSISTENCIES det
25810 65 63 74 65 64 22 29 3b 0a 20 20 20 20 75 70 64  ected");.    upd
25820 61 74 65 53 70 61 74 69 61 4c 69 74 65 48 69 73  ateSpatiaLiteHis
25830 74 6f 72 79 20 28 73 71 6c 69 74 65 2c 20 28 63  tory (sqlite, (c
25840 6f 6e 73 74 20 63 68 61 72 20 2a 29 20 74 61 62  onst char *) tab
25850 6c 65 2c 0a 09 09 09 20 20 20 20 20 28 63 6f 6e  le,....     (con
25860 73 74 20 63 68 61 72 20 2a 29 20 67 65 6f 6d 2c  st char *) geom,
25870 20 73 71 6c 29 3b 0a 20 20 20 20 72 65 74 75 72   sql);.    retur
25880 6e 20 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  n 0;.}..static i
25890 6e 74 0a 63 68 65 63 6b 5f 61 6e 79 5f 73 70 61  nt.check_any_spa
258a0 74 69 61 6c 5f 69 6e 64 65 78 20 28 73 71 6c 69  tial_index (sqli
258b0 74 65 33 20 2a 20 73 71 6c 69 74 65 29 0a 7b 0a  te3 * sqlite).{.
258c0 2f 2a 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  /* attempting to
258d0 20 63 68 65 63 6b 20 61 6e 79 20 64 65 66 69 6e   check any defin
258e0 65 64 20 52 2a 54 72 65 65 20 66 6f 72 20 63 6f  ed R*Tree for co
258f0 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 20 20  nsistency */.   
25900 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
25910 63 68 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20  char *table;.   
25920 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
25930 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 3b 0a 20 20  char *column;.  
25940 20 20 69 6e 74 20 73 74 61 74 75 73 3b 0a 20 20    int status;.  
25950 20 20 63 68 61 72 20 73 71 6c 5b 31 30 32 34 5d    char sql[1024]
25960 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20  ;.    int ret;. 
25970 20 20 20 69 6e 74 20 69 6e 76 61 6c 69 64 5f 72     int invalid_r
25980 74 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 73 71  tree = 0;.    sq
25990 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 74 6d 74  lite3_stmt *stmt
259a0 3b 0a 0a 2f 2a 20 72 65 74 72 69 65 76 69 6e 67  ;../* retrieving
259b0 20 61 6e 79 20 64 65 66 69 6e 65 64 20 52 2a 54   any defined R*T
259c0 72 65 65 20 2a 2f 0a 20 20 20 20 73 74 72 63 70  ree */.    strcp
259d0 79 20 28 73 71 6c 2c 0a 09 20 20 20 20 22 53 45  y (sql,..    "SE
259e0 4c 45 43 54 20 66 5f 74 61 62 6c 65 5f 6e 61 6d  LECT f_table_nam
259f0 65 2c 20 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f  e, f_geometry_co
25a00 6c 75 6d 6e 20 46 52 4f 4d 20 67 65 6f 6d 65 74  lumn FROM geomet
25a10 72 79 5f 63 6f 6c 75 6d 6e 73 20 22 29 3b 0a 20  ry_columns ");. 
25a20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
25a30 22 57 48 45 52 45 20 73 70 61 74 69 61 6c 5f 69  "WHERE spatial_i
25a40 6e 64 65 78 5f 65 6e 61 62 6c 65 64 20 3d 20 31  ndex_enabled = 1
25a50 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71  ");.    ret = sq
25a60 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
25a70 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 73   (sqlite, sql, s
25a80 74 72 6c 65 6e 20 28 73 71 6c 29 2c 20 26 73 74  trlen (sql), &st
25a90 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 69  mt, NULL);.    i
25aa0 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
25ab0 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  _OK).      {..  
25ac0 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 43  spatialite_e ("C
25ad0 68 65 63 6b 53 70 61 74 69 61 6c 49 6e 64 65 78  heckSpatialIndex
25ae0 20 53 51 4c 20 65 72 72 6f 72 3a 20 25 73 5c 6e   SQL error: %s\n
25af0 22 2c 0a 09 09 09 73 71 6c 69 74 65 33 5f 65 72  ",....sqlite3_er
25b00 72 6d 73 67 20 28 73 71 6c 69 74 65 29 29 3b 0a  rmsg (sqlite));.
25b10 09 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20  .  return -1;.  
25b20 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 20      }.    while 
25b30 28 31 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 72  (1).      {..  r
25b40 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  et = sqlite3_ste
25b50 70 20 28 73 74 6d 74 29 3b 0a 09 20 20 69 66 20  p (stmt);..  if 
25b60 28 72 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 44  (ret == SQLITE_D
25b70 4f 4e 45 29 0a 09 20 20 20 20 20 20 62 72 65 61  ONE)..      brea
25b80 6b 3b 0a 09 20 20 69 66 20 28 72 65 74 20 3d 3d  k;..  if (ret ==
25b90 20 53 51 4c 49 54 45 5f 52 4f 57 29 0a 09 20 20   SQLITE_ROW)..  
25ba0 20 20 7b 0a 09 09 2f 2a 20 63 68 65 63 6b 69 6e    {.../* checkin
25bb0 67 20 61 20 73 69 6e 67 6c 65 20 52 2a 54 72 65  g a single R*Tre
25bc0 65 20 2a 2f 0a 09 09 74 61 62 6c 65 20 3d 20 73  e */...table = s
25bd0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
25be0 78 74 20 28 73 74 6d 74 2c 20 30 29 3b 0a 09 09  xt (stmt, 0);...
25bf0 63 6f 6c 75 6d 6e 20 3d 20 73 71 6c 69 74 65 33  column = sqlite3
25c00 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20 28 73 74  _column_text (st
25c10 6d 74 2c 20 31 29 3b 0a 09 09 73 74 61 74 75 73  mt, 1);...status
25c20 20 3d 20 63 68 65 63 6b 5f 73 70 61 74 69 61 6c   = check_spatial
25c30 5f 69 6e 64 65 78 20 28 73 71 6c 69 74 65 2c 20  _index (sqlite, 
25c40 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e 29 3b 0a  table, column);.
25c50 09 09 69 66 20 28 73 74 61 74 75 73 20 3c 20 30  ..if (status < 0
25c60 29 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20  )...  {...      
25c70 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
25c80 20 28 73 74 6d 74 29 3b 0a 09 09 20 20 20 20 20   (stmt);...     
25c90 20 72 65 74 75 72 6e 20 2d 31 3b 0a 09 09 20 20   return -1;...  
25ca0 7d 0a 09 09 69 66 20 28 73 74 61 74 75 73 20 3d  }...if (status =
25cb0 3d 20 30 29 0a 09 09 20 20 20 20 69 6e 76 61 6c  = 0)...    inval
25cc0 69 64 5f 72 74 72 65 65 20 3d 20 31 3b 0a 09 20  id_rtree = 1;.. 
25cd0 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20     }..  else..  
25ce0 20 20 7b 0a 09 09 70 72 69 6e 74 66 20 28 22 73    {...printf ("s
25cf0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 72  qlite3_step() er
25d00 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 73 71 6c 69  ror: %s\n", sqli
25d10 74 65 33 5f 65 72 72 6d 73 67 20 28 73 71 6c 69  te3_errmsg (sqli
25d20 74 65 29 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f  te));...sqlite3_
25d30 66 69 6e 61 6c 69 7a 65 20 28 73 74 6d 74 29 3b  finalize (stmt);
25d40 0a 09 09 72 65 74 75 72 6e 20 2d 31 3b 0a 09 20  ...return -1;.. 
25d50 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
25d60 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
25d70 65 20 28 73 74 6d 74 29 3b 0a 20 20 20 20 69 66  e (stmt);.    if
25d80 20 28 69 6e 76 61 6c 69 64 5f 72 74 72 65 65 29   (invalid_rtree)
25d90 0a 09 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ..return 0;.    
25da0 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61  return 1;.}..sta
25db0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 43 68  tic void.fnct_Ch
25dc0 65 63 6b 53 70 61 74 69 61 6c 49 6e 64 65 78 20  eckSpatialIndex 
25dd0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
25de0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
25df0 61 72 67 63 2c 0a 09 09 09 73 71 6c 69 74 65 33  argc,....sqlite3
25e00 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
25e10 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
25e20 6e 3a 0a 2f 20 43 68 65 63 6b 53 70 61 74 69 61  n:./ CheckSpatia
25e30 6c 49 6e 64 65 78 28 29 0a 2f 20 43 68 65 63 6b  lIndex()./ Check
25e40 53 70 61 74 69 61 6c 49 6e 64 65 78 28 74 61 62  SpatialIndex(tab
25e50 6c 65 2c 20 63 6f 6c 75 6d 6e 29 0a 2f 0a 2f 20  le, column)././ 
25e60 63 68 65 63 6b 73 20 61 20 53 70 61 74 69 61 6c  checks a Spatial
25e70 49 6e 64 65 78 20 66 6f 72 20 63 6f 6e 73 69 73  Index for consis
25e80 74 65 6e 63 79 2c 20 72 65 74 75 72 6e 69 6e 67  tency, returning
25e90 3a 0a 2f 20 31 20 2d 20 74 68 65 20 52 2a 54 72  :./ 1 - the R*Tr
25ea0 65 65 20 69 73 20 66 75 6c 6c 79 20 63 6f 6e 73  ee is fully cons
25eb0 69 73 74 65 6e 74 0a 2f 20 30 20 2d 20 74 68 65  istent./ 0 - the
25ec0 20 52 2a 54 72 65 65 20 69 73 20 69 6e 63 6f 6e   R*Tree is incon
25ed0 73 69 73 74 65 6e 74 0a 2f 20 4e 55 4c 4c 20 6f  sistent./ NULL o
25ee0 6e 20 66 61 69 6c 75 72 65 0a 2a 2f 0a 20 20 20  n failure.*/.   
25ef0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
25f00 63 68 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20  char *table;.   
25f10 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
25f20 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 3b 0a 20 20  char *column;.  
25f30 20 20 69 6e 74 20 73 74 61 74 75 73 3b 0a 20 20    int status;.  
25f40 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74    sqlite3 *sqlit
25f50 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  e = sqlite3_cont
25f60 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28 63  ext_db_handle (c
25f70 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41 49  ontext);.    GAI
25f80 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
25f90 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
25fa0 2a 2f 0a 20 20 20 20 69 66 20 28 61 72 67 63 20  */.    if (argc 
25fb0 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 09 20  == 0).      {.. 
25fc0 20 2f 2a 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73   /* no arguments
25fd0 3a 20 77 65 20 6d 75 73 74 20 63 68 65 63 6b 20  : we must check 
25fe0 61 6e 79 20 64 65 66 69 6e 65 64 20 52 2a 54 72  any defined R*Tr
25ff0 65 65 20 2a 2f 0a 09 20 20 73 74 61 74 75 73 20  ee */..  status 
26000 3d 20 63 68 65 63 6b 5f 61 6e 79 5f 73 70 61 74  = check_any_spat
26010 69 61 6c 5f 69 6e 64 65 78 20 28 73 71 6c 69 74  ial_index (sqlit
26020 65 29 3b 0a 09 20 20 69 66 20 28 73 74 61 74 75  e);..  if (statu
26030 73 20 3c 20 30 29 0a 09 20 20 20 20 20 20 73 71  s < 0)..      sq
26040 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
26050 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
26060 65 6c 73 65 20 69 66 20 28 73 74 61 74 75 73 20  else if (status 
26070 3e 20 30 29 0a 09 20 20 20 20 20 20 73 71 6c 69  > 0)..      sqli
26080 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
26090 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 09 20 20  context, 1);..  
260a0 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69  else..      sqli
260b0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
260c0 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20  context, 0);..  
260d0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
260e0 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
260f0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
26100 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
26110 54 45 58 54 29 0a 20 20 20 20 20 20 7b 0a 09 20  TEXT).      {.. 
26120 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20   spatialite_e.. 
26130 20 20 20 20 20 28 22 43 68 65 63 6b 53 70 61 74       ("CheckSpat
26140 69 61 6c 49 6e 64 65 78 28 29 20 65 72 72 6f 72  ialIndex() error
26150 3a 20 61 72 67 75 6d 65 6e 74 20 31 20 5b 74 61  : argument 1 [ta
26160 62 6c 65 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f 74  ble_name] is not
26170 20 6f 66 20 74 68 65 20 53 74 72 69 6e 67 20 74   of the String t
26180 79 70 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69  ype\n");..  sqli
26190 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
261a0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
261b0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
261c0 20 20 74 61 62 6c 65 20 3d 20 73 71 6c 69 74 65    table = sqlite
261d0 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72  3_value_text (ar
261e0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 20 28  gv[0]);.    if (
261f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
26200 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20  pe (argv[1]) != 
26210 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20  SQLITE_TEXT).   
26220 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
26230 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 43 68  te_e..      ("Ch
26240 65 63 6b 53 70 61 74 69 61 6c 49 6e 64 65 78 28  eckSpatialIndex(
26250 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e  ) error: argumen
26260 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  t 2 [column_name
26270 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20  ] is not of the 
26280 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b  String type\n");
26290 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
262a0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
262b0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
262c0 20 20 20 20 7d 0a 20 20 20 20 63 6f 6c 75 6d 6e      }.    column
262d0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
262e0 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d 29 3b  _text (argv[1]);
262f0 0a 20 20 20 20 73 74 61 74 75 73 20 3d 20 63 68  .    status = ch
26300 65 63 6b 5f 73 70 61 74 69 61 6c 5f 69 6e 64 65  eck_spatial_inde
26310 78 20 28 73 71 6c 69 74 65 2c 20 74 61 62 6c 65  x (sqlite, table
26320 2c 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 69  , column);.    i
26330 66 20 28 73 74 61 74 75 73 20 3c 20 30 29 0a 09  f (status < 0)..
26340 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
26350 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
26360 20 20 20 65 6c 73 65 20 69 66 20 28 73 74 61 74     else if (stat
26370 75 73 20 3e 20 30 29 0a 09 73 71 6c 69 74 65 33  us > 0)..sqlite3
26380 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
26390 74 65 78 74 2c 20 31 29 3b 0a 20 20 20 20 65 6c  text, 1);.    el
263a0 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  se..sqlite3_resu
263b0 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
263c0 20 30 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   0);.}..static i
263d0 6e 74 0a 72 65 63 6f 76 65 72 5f 73 70 61 74 69  nt.recover_spati
263e0 61 6c 5f 69 6e 64 65 78 20 28 73 71 6c 69 74 65  al_index (sqlite
263f0 33 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73  3 * sqlite, cons
26400 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
26410 2a 74 61 62 6c 65 2c 0a 09 09 20 20 20 20 20 20  *table,...      
26420 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
26430 63 68 61 72 20 2a 67 65 6f 6d 29 0a 7b 0a 2f 2a  char *geom).{./*
26440 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 72   attempting to r
26450 65 62 75 69 6c 64 20 61 6e 20 52 2a 54 72 65 65  ebuild an R*Tree
26460 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c   */.    char sql
26470 5b 38 31 39 32 5d 3b 0a 20 20 20 20 63 68 61 72  [8192];.    char
26480 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20 20   sql2[2048];.   
26490 20 63 68 61 72 20 2a 65 72 72 4d 73 67 20 3d 20   char *errMsg = 
264a0 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 65  NULL;.    int re
264b0 74 3b 0a 20 20 20 20 63 68 61 72 20 78 74 61 62  t;.    char xtab
264c0 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68  le[1024];.    ch
264d0 61 72 20 78 67 65 6f 6d 5b 31 30 32 34 5d 3b 0a  ar xgeom[1024];.
264e0 20 20 20 20 63 68 61 72 20 69 64 78 5f 6e 61 6d      char idx_nam
264f0 65 5b 32 30 34 38 5d 3b 0a 20 20 20 20 69 6e 74  e[2048];.    int
26500 20 69 73 5f 64 65 66 69 6e 65 64 20 3d 20 30 3b   is_defined = 0;
26510 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  .    sqlite3_stm
26520 74 20 2a 73 74 6d 74 3b 0a 0a 2f 2a 20 63 68 65  t *stmt;../* che
26530 63 6b 69 6e 67 20 69 66 20 74 68 65 20 52 2a 54  cking if the R*T
26540 72 65 65 20 53 70 61 74 69 61 6c 20 49 6e 64 65  ree Spatial Inde
26550 78 20 69 73 20 64 65 66 69 6e 65 64 20 2a 2f 0a  x is defined */.
26560 20 20 20 20 73 74 72 63 70 79 20 28 78 74 61 62      strcpy (xtab
26570 6c 65 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 20  le, (const char 
26580 2a 29 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 63  *) table);.    c
26590 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20  lean_sql_string 
265a0 28 78 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  (xtable);.    st
265b0 72 63 70 79 20 28 78 67 65 6f 6d 2c 20 28 63 6f  rcpy (xgeom, (co
265c0 6e 73 74 20 63 68 61 72 20 2a 29 20 67 65 6f 6d  nst char *) geom
265d0 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c  );.    clean_sql
265e0 5f 73 74 72 69 6e 67 20 28 78 67 65 6f 6d 29 3b  _string (xgeom);
265f0 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
26600 2c 20 22 53 45 4c 45 43 54 20 43 6f 75 6e 74 28  , "SELECT Count(
26610 2a 29 20 46 52 4f 4d 20 67 65 6f 6d 65 74 72 79  *) FROM geometry
26620 5f 63 6f 6c 75 6d 6e 73 20 22 29 3b 0a 20 20 20  _columns ");.   
26630 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20   sprintf (sql2, 
26640 22 57 48 45 52 45 20 55 70 70 65 72 28 66 5f 74  "WHERE Upper(f_t
26650 61 62 6c 65 5f 6e 61 6d 65 29 20 3d 20 55 70 70  able_name) = Upp
26660 65 72 28 27 25 73 27 29 20 22 2c 20 78 74 61 62  er('%s') ", xtab
26670 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
26680 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20  (sql, sql2);.   
26690 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20   sprintf (sql2, 
266a0 22 41 4e 44 20 55 70 70 65 72 28 66 5f 67 65 6f  "AND Upper(f_geo
266b0 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 3d 20  metry_column) = 
266c0 55 70 70 65 72 28 27 25 73 27 29 20 22 2c 20 78  Upper('%s') ", x
266d0 67 65 6f 6d 29 3b 0a 20 20 20 20 73 74 72 63 61  geom);.    strca
266e0 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20  t (sql, sql2);. 
266f0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
26700 22 41 4e 44 20 73 70 61 74 69 61 6c 5f 69 6e 64  "AND spatial_ind
26710 65 78 5f 65 6e 61 62 6c 65 64 20 3d 20 31 22 29  ex_enabled = 1")
26720 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
26730 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20 28  te3_prepare_v2 (
26740 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 73 74 72  sqlite, sql, str
26750 6c 65 6e 20 28 73 71 6c 29 2c 20 26 73 74 6d 74  len (sql), &stmt
26760 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 69 66 20  , NULL);.    if 
26770 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f  (ret != SQLITE_O
26780 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70  K).      {..  sp
26790 61 74 69 61 6c 69 74 65 5f 65 20 28 22 52 65 63  atialite_e ("Rec
267a0 6f 76 65 72 53 70 61 74 69 61 6c 49 6e 64 65 78  overSpatialIndex
267b0 20 53 51 4c 20 65 72 72 6f 72 3a 20 25 73 5c 6e   SQL error: %s\n
267c0 22 2c 0a 09 09 09 73 71 6c 69 74 65 33 5f 65 72  ",....sqlite3_er
267d0 72 6d 73 67 20 28 73 71 6c 69 74 65 29 29 3b 0a  rmsg (sqlite));.
267e0 09 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20  .  return -1;.  
267f0 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 20      }.    while 
26800 28 31 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 72  (1).      {..  r
26810 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  et = sqlite3_ste
26820 70 20 28 73 74 6d 74 29 3b 0a 09 20 20 69 66 20  p (stmt);..  if 
26830 28 72 65 74 20 3d 3d 20 53 51 4c 49 54 45 5f 44  (ret == SQLITE_D
26840 4f 4e 45 29 0a 09 20 20 20 20 20 20 62 72 65 61  ONE)..      brea
26850 6b 3b 0a 09 20 20 69 66 20 28 72 65 74 20 3d 3d  k;..  if (ret ==
26860 20 53 51 4c 49 54 45 5f 52 4f 57 29 0a 09 20 20   SQLITE_ROW)..  
26870 20 20 20 20 69 73 5f 64 65 66 69 6e 65 64 20 3d      is_defined =
26880 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
26890 69 6e 74 20 28 73 74 6d 74 2c 20 30 29 3b 0a 09  int (stmt, 0);..
268a0 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09    else..    {...
268b0 70 72 69 6e 74 66 20 28 22 73 71 6c 69 74 65 33  printf ("sqlite3
268c0 5f 73 74 65 70 28 29 20 65 72 72 6f 72 3a 20 25  _step() error: %
268d0 73 5c 6e 22 2c 20 73 71 6c 69 74 65 33 5f 65 72  s\n", sqlite3_er
268e0 72 6d 73 67 20 28 73 71 6c 69 74 65 29 29 3b 0a  rmsg (sqlite));.
268f0 09 09 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  ..sqlite3_finali
26900 7a 65 20 28 73 74 6d 74 29 3b 0a 09 09 72 65 74  ze (stmt);...ret
26910 75 72 6e 20 2d 31 3b 0a 09 20 20 20 20 7d 0a 20  urn -1;..    }. 
26920 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74       }.    sqlit
26930 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73 74 6d  e3_finalize (stm
26940 74 29 3b 0a 20 20 20 20 69 66 20 28 21 69 73 5f  t);.    if (!is_
26950 64 65 66 69 6e 65 64 29 0a 09 72 65 74 75 72 6e  defined)..return
26960 20 2d 31 3b 0a 0a 2f 2a 20 65 72 61 73 69 6e 67   -1;../* erasing
26970 20 74 68 65 20 52 2a 54 72 65 65 20 74 61 62 6c   the R*Tree tabl
26980 65 20 2a 2f 0a 20 20 20 20 73 70 72 69 6e 74 66  e */.    sprintf
26990 20 28 69 64 78 5f 6e 61 6d 65 2c 20 22 69 64 78   (idx_name, "idx
269a0 5f 25 73 5f 25 73 22 2c 20 74 61 62 6c 65 2c 20  _%s_%s", table, 
269b0 67 65 6f 6d 29 3b 0a 20 20 20 20 64 6f 75 62 6c  geom);.    doubl
269c0 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 69 64  e_quoted_sql (id
269d0 78 5f 6e 61 6d 65 29 3b 0a 20 20 20 20 73 70 72  x_name);.    spr
269e0 69 6e 74 66 20 28 73 71 6c 2c 20 22 44 45 4c 45  intf (sql, "DELE
269f0 54 45 20 46 52 4f 4d 20 25 73 22 2c 20 69 64 78  TE FROM %s", idx
26a00 5f 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 20  _name);.    ret 
26a10 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28  = sqlite3_exec (
26a20 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c  sqlite, sql, NUL
26a30 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67  L, NULL, &errMsg
26a40 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
26a50 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f  = SQLITE_OK)..go
26a60 74 6f 20 65 72 72 6f 72 3b 0a 2f 2a 20 70 6f 70  to error;./* pop
26a70 75 6c 61 74 69 6e 67 20 74 68 65 20 52 2a 54 72  ulating the R*Tr
26a80 65 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 73 63  ee table from sc
26a90 72 61 74 63 68 20 2a 2f 0a 20 20 20 20 62 75 69  ratch */.    bui
26aa0 6c 64 53 70 61 74 69 61 6c 49 6e 64 65 78 20 28  ldSpatialIndex (
26ab0 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 2c 20 28  sqlite, table, (
26ac0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 20 67 65  const char *) ge
26ad0 6f 6d 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20  om);.    strcpy 
26ae0 28 73 71 6c 2c 20 22 53 70 61 74 69 61 6c 49 6e  (sql, "SpatialIn
26af0 64 65 78 3a 20 73 75 63 63 65 73 73 66 75 6c 6c  dex: successfull
26b00 79 20 72 65 63 6f 76 65 72 65 64 22 29 3b 0a 20  y recovered");. 
26b10 20 20 20 75 70 64 61 74 65 53 70 61 74 69 61 4c     updateSpatiaL
26b20 69 74 65 48 69 73 74 6f 72 79 20 28 73 71 6c 69  iteHistory (sqli
26b30 74 65 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 20  te, (const char 
26b40 2a 29 20 74 61 62 6c 65 2c 0a 09 09 09 20 20 20  *) table,....   
26b50 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29    (const char *)
26b60 20 67 65 6f 6d 2c 20 73 71 6c 29 3b 0a 20 20 20   geom, sql);.   
26b70 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 65 72 72   return 1;.  err
26b80 6f 72 3a 0a 20 20 20 20 73 70 61 74 69 61 6c 69  or:.    spatiali
26b90 74 65 5f 65 20 28 22 52 65 63 6f 76 65 72 53 70  te_e ("RecoverSp
26ba0 61 74 69 61 6c 49 6e 64 65 78 28 29 20 65 72 72  atialIndex() err
26bb0 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 65  or: \"%s\"\n", e
26bc0 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69  rrMsg);.    sqli
26bd0 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73 67  te3_free (errMsg
26be0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
26bf0 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 72  .}..static int.r
26c00 65 63 6f 76 65 72 5f 61 6e 79 5f 73 70 61 74 69  ecover_any_spati
26c10 61 6c 5f 69 6e 64 65 78 20 28 73 71 6c 69 74 65  al_index (sqlite
26c20 33 20 2a 20 73 71 6c 69 74 65 2c 20 69 6e 74 20  3 * sqlite, int 
26c30 6e 6f 5f 63 68 65 63 6b 29 0a 7b 0a 2f 2a 20 61  no_check).{./* a
26c40 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 65 62  ttempting to reb
26c50 75 69 6c 64 20 61 6e 79 20 64 65 66 69 6e 65 64  uild any defined
26c60 20 52 2a 54 72 65 65 20 2a 2f 0a 20 20 20 20 63   R*Tree */.    c
26c70 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
26c80 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20 63  ar *table;.    c
26c90 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
26ca0 61 72 20 2a 63 6f 6c 75 6d 6e 3b 0a 20 20 20 20  ar *column;.    
26cb0 69 6e 74 20 73 74 61 74 75 73 3b 0a 20 20 20 20  int status;.    
26cc0 63 68 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b 0a  char sql[1024];.
26cd0 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
26ce0 20 69 6e 74 20 74 6f 5f 62 65 5f 66 69 78 65 64   int to_be_fixed
26cf0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74  ;.    sqlite3_st
26d00 6d 74 20 2a 73 74 6d 74 3b 0a 0a 2f 2a 20 72 65  mt *stmt;../* re
26d10 74 72 69 65 76 69 6e 67 20 61 6e 79 20 64 65 66  trieving any def
26d20 69 6e 65 64 20 52 2a 54 72 65 65 20 2a 2f 0a 20  ined R*Tree */. 
26d30 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 2c 0a     strcpy (sql,.
26d40 09 20 20 20 20 22 53 45 4c 45 43 54 20 66 5f 74  .    "SELECT f_t
26d50 61 62 6c 65 5f 6e 61 6d 65 2c 20 66 5f 67 65 6f  able_name, f_geo
26d60 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 20 46 52 4f  metry_column FRO
26d70 4d 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d  M geometry_colum
26d80 6e 73 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ns ");.    strca
26d90 74 20 28 73 71 6c 2c 20 22 57 48 45 52 45 20 73  t (sql, "WHERE s
26da0 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 65 6e 61  patial_index_ena
26db0 62 6c 65 64 20 3d 20 31 22 29 3b 0a 20 20 20 20  bled = 1");.    
26dc0 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 70 72  ret = sqlite3_pr
26dd0 65 70 61 72 65 5f 76 32 20 28 73 71 6c 69 74 65  epare_v2 (sqlite
26de0 2c 20 73 71 6c 2c 20 73 74 72 6c 65 6e 20 28 73  , sql, strlen (s
26df0 71 6c 29 2c 20 26 73 74 6d 74 2c 20 4e 55 4c 4c  ql), &stmt, NULL
26e00 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21  );.    if (ret !
26e10 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20  = SQLITE_OK).   
26e20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
26e30 74 65 5f 65 20 28 22 52 65 63 6f 76 65 72 53 70  te_e ("RecoverSp
26e40 61 74 69 61 6c 49 6e 64 65 78 20 53 51 4c 20 65  atialIndex SQL e
26e50 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 0a 09 09 09  rror: %s\n",....
26e60 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 28  sqlite3_errmsg (
26e70 73 71 6c 69 74 65 29 29 3b 0a 09 20 20 72 65 74  sqlite));..  ret
26e80 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  urn -1;.      }.
26e90 20 20 20 20 77 68 69 6c 65 20 28 31 29 0a 20 20      while (1).  
26ea0 20 20 20 20 7b 0a 09 20 20 72 65 74 20 3d 20 73      {..  ret = s
26eb0 71 6c 69 74 65 33 5f 73 74 65 70 20 28 73 74 6d  qlite3_step (stm
26ec0 74 29 3b 0a 09 20 20 69 66 20 28 72 65 74 20 3d  t);..  if (ret =
26ed0 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 29 0a 09  = SQLITE_DONE)..
26ee0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 09 20 20        break;..  
26ef0 69 66 20 28 72 65 74 20 3d 3d 20 53 51 4c 49 54  if (ret == SQLIT
26f00 45 5f 52 4f 57 29 0a 09 20 20 20 20 7b 0a 09 09  E_ROW)..    {...
26f10 2f 2a 20 63 68 65 63 6b 69 6e 67 20 61 20 73 69  /* checking a si
26f20 6e 67 6c 65 20 52 2a 54 72 65 65 20 2a 2f 0a 09  ngle R*Tree */..
26f30 09 74 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  .table = sqlite3
26f40 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20 28 73 74  _column_text (st
26f50 6d 74 2c 20 30 29 3b 0a 09 09 63 6f 6c 75 6d 6e  mt, 0);...column
26f60 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
26f70 6e 5f 74 65 78 74 20 28 73 74 6d 74 2c 20 31 29  n_text (stmt, 1)
26f80 3b 0a 09 09 74 6f 5f 62 65 5f 66 69 78 65 64 20  ;...to_be_fixed 
26f90 3d 20 31 3b 0a 09 09 69 66 20 28 21 6e 6f 5f 63  = 1;...if (!no_c
26fa0 68 65 63 6b 29 0a 09 09 20 20 7b 0a 09 09 20 20  heck)...  {...  
26fb0 20 20 20 20 73 74 61 74 75 73 20 3d 20 63 68 65      status = che
26fc0 63 6b 5f 73 70 61 74 69 61 6c 5f 69 6e 64 65 78  ck_spatial_index
26fd0 20 28 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 2c   (sqlite, table,
26fe0 20 63 6f 6c 75 6d 6e 29 3b 0a 09 09 20 20 20 20   column);...    
26ff0 20 20 69 66 20 28 73 74 61 74 75 73 20 3c 20 30    if (status < 0
27000 29 0a 09 09 09 7b 0a 09 09 09 20 20 20 20 2f 2a  )....{....    /*
27010 20 73 6f 6d 65 20 75 6e 65 78 70 65 63 74 65 64   some unexpected
27020 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20   error occurred 
27030 2a 2f 0a 09 09 09 20 20 20 20 67 6f 74 6f 20 66  */....    goto f
27040 61 74 61 6c 5f 65 72 72 6f 72 3b 0a 09 09 09 7d  atal_error;....}
27050 0a 09 09 20 20 20 20 20 20 65 6c 73 65 20 69 66  ...      else if
27060 20 28 73 74 61 74 75 73 20 3e 20 30 29 0a 09 09   (status > 0)...
27070 09 7b 0a 09 09 09 20 20 20 20 2f 2a 20 74 68 65  .{....    /* the
27080 20 53 70 61 74 69 61 6c 20 49 6e 64 65 78 20 69   Spatial Index i
27090 73 20 61 6c 72 65 61 64 79 20 76 61 6c 69 64 20  s already valid 
270a0 2a 2f 0a 09 09 09 20 20 20 20 74 6f 5f 62 65 5f  */....    to_be_
270b0 66 69 78 65 64 20 3d 20 30 3b 0a 09 09 09 7d 0a  fixed = 0;....}.
270c0 09 09 20 20 7d 0a 09 09 69 66 20 28 74 6f 5f 62  ..  }...if (to_b
270d0 65 5f 66 69 78 65 64 29 0a 09 09 20 20 7b 0a 09  e_fixed)...  {..
270e0 09 20 20 20 20 20 20 2f 2a 20 72 65 62 75 69 6c  .      /* rebuil
270f0 64 69 6e 67 20 74 68 65 20 53 70 61 74 69 61 6c  ding the Spatial
27100 20 49 6e 64 65 78 20 2a 2f 0a 09 09 20 20 20 20   Index */...    
27110 20 20 73 74 61 74 75 73 20 3d 20 72 65 63 6f 76    status = recov
27120 65 72 5f 73 70 61 74 69 61 6c 5f 69 6e 64 65 78  er_spatial_index
27130 20 28 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 2c   (sqlite, table,
27140 20 63 6f 6c 75 6d 6e 29 3b 0a 09 09 20 20 20 20   column);...    
27150 20 20 69 66 20 28 73 74 61 74 75 73 20 3c 20 30    if (status < 0
27160 29 0a 09 09 09 7b 0a 09 09 09 20 20 20 20 2f 2a  )....{....    /*
27170 20 73 6f 6d 65 20 75 6e 65 78 70 65 63 74 65 64   some unexpected
27180 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20   error occurred 
27190 2a 2f 0a 09 09 09 20 20 20 20 67 6f 74 6f 20 66  */....    goto f
271a0 61 74 61 6c 5f 65 72 72 6f 72 3b 0a 09 09 09 7d  atal_error;....}
271b0 0a 09 09 20 20 20 20 20 20 65 6c 73 65 20 69 66  ...      else if
271c0 20 28 73 74 61 74 75 73 20 3d 3d 20 30 29 0a 09   (status == 0)..
271d0 09 09 20 20 67 6f 74 6f 20 65 72 72 6f 72 3b 0a  ..  goto error;.
271e0 09 09 20 20 7d 0a 09 20 20 20 20 7d 0a 09 20 20  ..  }..    }..  
271f0 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 70 72  else..    {...pr
27200 69 6e 74 66 20 28 22 73 71 6c 69 74 65 33 5f 73  intf ("sqlite3_s
27210 74 65 70 28 29 20 65 72 72 6f 72 3a 20 25 73 5c  tep() error: %s\
27220 6e 22 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  n", sqlite3_errm
27230 73 67 20 28 73 71 6c 69 74 65 29 29 3b 0a 09 09  sg (sqlite));...
27240 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
27250 20 28 73 74 6d 74 29 3b 0a 09 09 72 65 74 75 72   (stmt);...retur
27260 6e 20 2d 31 3b 0a 09 20 20 20 20 7d 0a 20 20 20  n -1;..    }.   
27270 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
27280 5f 66 69 6e 61 6c 69 7a 65 20 28 73 74 6d 74 29  _finalize (stmt)
27290 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
272a0 20 20 65 72 72 6f 72 3a 0a 20 20 20 20 73 71 6c    error:.    sql
272b0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 28 73  ite3_finalize (s
272c0 74 6d 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  tmt);.    return
272d0 20 30 3b 0a 20 20 66 61 74 61 6c 5f 65 72 72 6f   0;.  fatal_erro
272e0 72 3a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  r:.    sqlite3_f
272f0 69 6e 61 6c 69 7a 65 20 28 73 74 6d 74 29 3b 0a  inalize (stmt);.
27300 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d      return -1;.}
27310 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
27320 63 74 5f 52 65 63 6f 76 65 72 53 70 61 74 69 61  ct_RecoverSpatia
27330 6c 49 6e 64 65 78 20 28 73 71 6c 69 74 65 33 5f  lIndex (sqlite3_
27340 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
27350 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 09  t, int argc,....
27360 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
27370 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
27380 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 52 65  L function:./ Re
27390 63 6f 76 65 72 53 70 61 74 69 61 6c 49 6e 64 65  coverSpatialInde
273a0 78 28 29 0a 2f 20 52 65 63 6f 76 65 72 53 70 61  x()./ RecoverSpa
273b0 74 69 61 6c 49 6e 64 65 78 28 6e 6f 5f 63 68 65  tialIndex(no_che
273c0 63 6b 29 0a 2f 20 52 65 63 6f 76 65 72 53 70 61  ck)./ RecoverSpa
273d0 74 69 61 6c 49 6e 64 65 78 28 74 61 62 6c 65 2c  tialIndex(table,
273e0 20 63 6f 6c 75 6d 6e 29 0a 2f 20 52 65 63 6f 76   column)./ Recov
273f0 65 72 53 70 61 74 69 61 6c 49 6e 64 65 78 28 74  erSpatialIndex(t
27400 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e 2c 20 6e 6f  able, column, no
27410 5f 63 68 65 63 6b 29 0a 2f 0a 2f 20 61 74 74 65  _check)././ atte
27420 6d 70 74 73 20 74 6f 20 72 65 62 75 69 6c 64 20  mpts to rebuild 
27430 61 20 53 70 61 74 69 61 6c 49 6e 64 65 78 2c 20  a SpatialIndex, 
27440 72 65 74 75 72 6e 69 6e 67 3a 0a 2f 20 31 20 2d  returning:./ 1 -
27450 20 6f 6e 20 73 75 63 63 65 73 73 0a 2f 20 30 20   on success./ 0 
27460 2d 20 6f 6e 20 66 61 69 6c 75 72 65 0a 2f 20 4e  - on failure./ N
27470 55 4c 4c 20 69 66 20 61 6e 79 20 73 79 6e 74 61  ULL if any synta
27480 78 20 65 72 72 6f 72 20 69 73 20 64 65 74 65 63  x error is detec
27490 74 65 64 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  ted.*/.    const
274a0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
274b0 74 61 62 6c 65 3b 0a 20 20 20 20 63 6f 6e 73 74  table;.    const
274c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
274d0 63 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 6e 74 20  column;.    int 
274e0 6e 6f 5f 63 68 65 63 6b 20 3d 20 30 3b 0a 20 20  no_check = 0;.  
274f0 20 20 69 6e 74 20 73 74 61 74 75 73 3b 0a 20 20    int status;.  
27500 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74    sqlite3 *sqlit
27510 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  e = sqlite3_cont
27520 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28 63  ext_db_handle (c
27530 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41 49  ontext);.    GAI
27540 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
27550 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
27560 2a 2f 0a 20 20 20 20 69 66 20 28 61 72 67 63 20  */.    if (argc 
27570 3c 3d 20 31 29 0a 20 20 20 20 20 20 7b 0a 09 20  <= 1).      {.. 
27580 20 2f 2a 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73   /* no arguments
27590 3a 20 77 65 20 6d 75 73 74 20 72 65 62 75 69 6c  : we must rebuil
275a0 64 20 61 6e 79 20 64 65 66 69 6e 65 64 20 52 2a  d any defined R*
275b0 54 72 65 65 20 2a 2f 0a 09 20 20 69 66 20 28 61  Tree */..  if (a
275c0 72 67 63 20 3d 3d 20 31 29 0a 09 20 20 20 20 7b  rgc == 1)..    {
275d0 0a 09 09 69 66 20 28 73 71 6c 69 74 65 33 5f 76  ...if (sqlite3_v
275e0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
275f0 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  0]) == SQLITE_IN
27600 54 45 47 45 52 29 0a 09 09 20 20 20 20 6e 6f 5f  TEGER)...    no_
27610 63 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f  check = sqlite3_
27620 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
27630 30 5d 29 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20  0]);...else...  
27640 7b 0a 09 09 20 20 20 20 20 20 73 70 61 74 69 61  {...      spatia
27650 6c 69 74 65 5f 65 0a 09 09 09 20 20 28 22 52 65  lite_e....  ("Re
27660 63 6f 76 65 72 53 70 61 74 69 61 6c 49 6e 64 65  coverSpatialInde
27670 78 28 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d  x() error: argum
27680 65 6e 74 20 31 20 5b 6e 6f 5f 63 68 65 63 6b 5d  ent 1 [no_check]
27690 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 49   is not of the I
276a0 6e 74 65 67 65 72 20 74 79 70 65 5c 6e 22 29 3b  nteger type\n");
276b0 0a 09 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  ...      sqlite3
276c0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
276d0 6e 74 65 78 74 29 3b 0a 09 09 20 20 20 20 20 20  ntext);...      
276e0 72 65 74 75 72 6e 3b 0a 09 09 20 20 7d 0a 09 20  return;...  }.. 
276f0 20 20 20 7d 0a 09 20 20 73 74 61 74 75 73 20 3d     }..  status =
27700 20 72 65 63 6f 76 65 72 5f 61 6e 79 5f 73 70 61   recover_any_spa
27710 74 69 61 6c 5f 69 6e 64 65 78 20 28 73 71 6c 69  tial_index (sqli
27720 74 65 2c 20 6e 6f 5f 63 68 65 63 6b 29 3b 0a 09  te, no_check);..
27730 20 20 69 66 20 28 73 74 61 74 75 73 20 3c 20 30    if (status < 0
27740 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  )..      sqlite3
27750 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
27760 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 20  ntext);..  else 
27770 69 66 20 28 73 74 61 74 75 73 20 3e 20 30 29 0a  if (status > 0).
27780 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
27790 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
277a0 78 74 2c 20 31 29 3b 0a 09 20 20 65 6c 73 65 0a  xt, 1);..  else.
277b0 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
277c0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
277d0 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72  xt, 0);..  retur
277e0 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  n;.      }..    
277f0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
27800 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
27810 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29   != SQLITE_TEXT)
27820 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
27830 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20  ialite_e..      
27840 28 22 52 65 63 6f 76 65 72 53 70 61 74 69 61 6c  ("RecoverSpatial
27850 49 6e 64 65 78 28 29 20 65 72 72 6f 72 3a 20 61  Index() error: a
27860 72 67 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c 65  rgument 1 [table
27870 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66  _name] is not of
27880 20 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65   the String type
27890 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  \n");..  sqlite3
278a0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
278b0 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
278c0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 74  n;.      }.    t
278d0 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  able = sqlite3_v
278e0 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b  alue_text (argv[
278f0 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71 6c  0]);.    if (sql
27900 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
27910 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c  (argv[1]) != SQL
27920 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20  ITE_TEXT).      
27930 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
27940 65 0a 09 20 20 20 20 20 20 28 22 52 65 63 6f 76  e..      ("Recov
27950 65 72 53 70 61 74 69 61 6c 49 6e 64 65 78 28 29  erSpatialIndex()
27960 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74   error: argument
27970 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 5d   2 [column_name]
27980 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 53   is not of the S
27990 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a  tring type\n");.
279a0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
279b0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
279c0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
279d0 20 20 20 7d 0a 20 20 20 20 63 6f 6c 75 6d 6e 20     }.    column 
279e0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
279f0 74 65 78 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a  text (argv[1]);.
27a00 20 20 20 20 69 66 20 28 61 72 67 63 20 3d 3d 20      if (argc == 
27a10 33 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66  3).      {..  if
27a20 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
27a30 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d  type (argv[2]) =
27a40 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
27a50 29 0a 09 20 20 20 20 20 20 6e 6f 5f 63 68 65 63  )..      no_chec
27a60 6b 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  k = sqlite3_valu
27a70 65 5f 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b  e_int (argv[2]);
27a80 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
27a90 09 09 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09  ..spatialite_e..
27aa0 09 20 20 20 20 28 22 52 65 63 6f 76 65 72 53 70  .    ("RecoverSp
27ab0 61 74 69 61 6c 49 6e 64 65 78 28 29 20 65 72 72  atialIndex() err
27ac0 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 32 20 5b  or: argument 2 [
27ad0 6e 6f 5f 63 68 65 63 6b 5d 20 69 73 20 6e 6f 74  no_check] is not
27ae0 20 6f 66 20 74 68 65 20 49 6e 74 65 67 65 72 20   of the Integer 
27af0 74 79 70 65 5c 6e 22 29 3b 0a 09 09 73 71 6c 69  type\n");...sqli
27b00 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
27b10 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09 72 65 74  (context);...ret
27b20 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20  urn;..    }.    
27b30 20 20 7d 0a 20 20 20 20 69 66 20 28 21 6e 6f 5f    }.    if (!no_
27b40 63 68 65 63 6b 29 0a 20 20 20 20 20 20 7b 0a 09  check).      {..
27b50 20 20 2f 2a 20 63 68 65 63 6b 69 6e 67 20 74 68    /* checking th
27b60 65 20 63 75 72 72 65 6e 74 20 53 70 61 74 69 61  e current Spatia
27b70 6c 49 6e 64 65 78 20 76 61 6c 69 64 69 74 79 20  lIndex validity 
27b80 2a 2f 0a 09 20 20 73 74 61 74 75 73 20 3d 20 63  */..  status = c
27b90 68 65 63 6b 5f 73 70 61 74 69 61 6c 5f 69 6e 64  heck_spatial_ind
27ba0 65 78 20 28 73 71 6c 69 74 65 2c 20 74 61 62 6c  ex (sqlite, tabl
27bb0 65 2c 20 63 6f 6c 75 6d 6e 29 3b 0a 09 20 20 69  e, column);..  i
27bc0 66 20 28 73 74 61 74 75 73 20 3c 20 30 29 0a 09  f (status < 0)..
27bd0 20 20 20 20 7b 0a 09 09 2f 2a 20 73 6f 6d 65 20      {.../* some 
27be0 75 6e 65 78 70 65 63 74 65 64 20 65 72 72 6f 72  unexpected error
27bf0 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 09 09 73   occurred */...s
27c00 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
27c10 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09  ll (context);...
27c20 72 65 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 09  return;..    }..
27c30 20 20 65 6c 73 65 20 69 66 20 28 73 74 61 74 75    else if (statu
27c40 73 20 3e 20 30 29 0a 09 20 20 20 20 7b 0a 09 09  s > 0)..    {...
27c50 2f 2a 20 74 68 65 20 53 70 61 74 69 61 6c 20 49  /* the Spatial I
27c60 6e 64 65 78 20 69 73 20 61 6c 72 65 61 64 79 20  ndex is already 
27c70 76 61 6c 69 64 20 2a 2f 0a 09 09 73 71 6c 69 74  valid */...sqlit
27c80 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
27c90 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 09 09 72 65  ontext, 1);...re
27ca0 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 20 20 20  turn;..    }.   
27cb0 20 20 20 7d 0a 2f 2a 20 72 65 62 75 69 6c 64 69     }./* rebuildi
27cc0 6e 67 20 74 68 65 20 53 70 61 74 69 61 6c 20 49  ng the Spatial I
27cd0 6e 64 65 78 20 2a 2f 0a 20 20 20 20 73 74 61 74  ndex */.    stat
27ce0 75 73 20 3d 20 72 65 63 6f 76 65 72 5f 73 70 61  us = recover_spa
27cf0 74 69 61 6c 5f 69 6e 64 65 78 20 28 73 71 6c 69  tial_index (sqli
27d00 74 65 2c 20 74 61 62 6c 65 2c 20 63 6f 6c 75 6d  te, table, colum
27d10 6e 29 3b 0a 20 20 20 20 69 66 20 28 73 74 61 74  n);.    if (stat
27d20 75 73 20 3c 20 30 29 0a 09 73 71 6c 69 74 65 33  us < 0)..sqlite3
27d30 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
27d40 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
27d50 20 69 66 20 28 73 74 61 74 75 73 20 3e 20 30 29   if (status > 0)
27d60 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
27d70 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31  _int (context, 1
27d80 29 3b 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c  );.    else..sql
27d90 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
27da0 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 7d 0a  (context, 0);.}.
27db0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
27dc0 74 5f 43 72 65 61 74 65 53 70 61 74 69 61 6c 49  t_CreateSpatialI
27dd0 6e 64 65 78 20 28 73 71 6c 69 74 65 33 5f 63 6f  ndex (sqlite3_co
27de0 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
27df0 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 73   int argc,.... s
27e00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
27e10 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
27e20 75 6e 63 74 69 6f 6e 3a 0a 2f 20 43 72 65 61 74  unction:./ Creat
27e30 65 53 70 61 74 69 61 6c 49 6e 64 65 78 28 74 61  eSpatialIndex(ta
27e40 62 6c 65 2c 20 63 6f 6c 75 6d 6e 20 29 0a 2f 0a  ble, column )./.
27e50 2f 20 63 72 65 61 74 65 73 20 61 20 53 70 61 74  / creates a Spat
27e60 69 61 6c 49 6e 64 65 78 20 62 61 73 65 64 20 6f  ialIndex based o
27e70 6e 20 43 6f 6c 75 6d 6e 20 61 6e 64 20 54 61 62  n Column and Tab
27e80 6c 65 0a 2f 20 72 65 74 75 72 6e 73 20 31 20 6f  le./ returns 1 o
27e90 6e 20 73 75 63 63 65 73 73 0a 2f 20 30 20 6f 6e  n success./ 0 on
27ea0 20 66 61 69 6c 75 72 65 0a 2a 2f 0a 20 20 20 20   failure.*/.    
27eb0 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 61 62 6c  const char *tabl
27ec0 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
27ed0 72 20 2a 63 6f 6c 75 6d 6e 3b 0a 20 20 20 20 63  r *column;.    c
27ee0 68 61 72 20 73 71 6c 5b 31 30 32 34 5d 3b 0a 20  har sql[1024];. 
27ef0 20 20 20 63 68 61 72 20 2a 65 72 72 4d 73 67 20     char *errMsg 
27f00 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20  = NULL;.    int 
27f10 72 65 74 3b 0a 20 20 20 20 63 68 61 72 20 73 71  ret;.    char sq
27f20 6c 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20  ltable[1024];.  
27f30 20 20 63 68 61 72 20 73 71 6c 63 6f 6c 75 6d 6e    char sqlcolumn
27f40 5b 31 30 32 34 5d 3b 0a 20 20 20 20 73 71 6c 69  [1024];.    sqli
27f50 74 65 33 20 2a 73 71 6c 69 74 65 20 3d 20 73 71  te3 *sqlite = sq
27f60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
27f70 5f 68 61 6e 64 6c 65 20 28 63 6f 6e 74 65 78 74  _handle (context
27f80 29 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  );.    GAIA_UNUS
27f90 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
27fa0 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
27fb0 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
27fc0 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d  ue_type (argv[0]
27fd0 29 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54  ) != SQLITE_TEXT
27fe0 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61  ).      {..  spa
27ff0 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20  tialite_e..     
28000 20 28 22 43 72 65 61 74 65 53 70 61 74 69 61 6c   ("CreateSpatial
28010 49 6e 64 65 78 28 29 20 65 72 72 6f 72 3a 20 61  Index() error: a
28020 72 67 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c 65  rgument 1 [table
28030 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66  _name] is not of
28040 20 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65   the String type
28050 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  \n");..  sqlite3
28060 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
28070 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74  text, 0);..  ret
28080 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
28090 20 74 61 62 6c 65 20 3d 20 28 63 6f 6e 73 74 20   table = (const 
280a0 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
280b0 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76  value_text (argv
280c0 5b 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71  [0]);.    if (sq
280d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
280e0 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51   (argv[1]) != SQ
280f0 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20  LITE_TEXT).     
28100 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
28110 5f 65 0a 09 20 20 20 20 20 20 28 22 43 72 65 61  _e..      ("Crea
28120 74 65 53 70 61 74 69 61 6c 49 6e 64 65 78 28 29  teSpatialIndex()
28130 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74   error: argument
28140 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 5d   2 [column_name]
28150 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 53   is not of the S
28160 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a  tring type\n");.
28170 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
28180 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
28190 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  0);..  return;. 
281a0 20 20 20 20 20 7d 0a 20 20 20 20 63 6f 6c 75 6d       }.    colum
281b0 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  n = (const char 
281c0 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
281d0 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d 29 3b  _text (argv[1]);
281e0 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
281f0 74 61 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20  table, table);. 
28200 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72     clean_sql_str
28210 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a  ing (sqltable);.
28220 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 63      strcpy (sqlc
28230 6f 6c 75 6d 6e 2c 20 63 6f 6c 75 6d 6e 29 3b 0a  olumn, column);.
28240 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74      clean_sql_st
28250 72 69 6e 67 20 28 73 71 6c 63 6f 6c 75 6d 6e 29  ring (sqlcolumn)
28260 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
28270 6c 2c 0a 09 20 20 20 20 22 55 50 44 41 54 45 20  l,..    "UPDATE 
28280 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 73  geometry_columns
28290 20 53 45 54 20 73 70 61 74 69 61 6c 5f 69 6e 64   SET spatial_ind
282a0 65 78 5f 65 6e 61 62 6c 65 64 20 3d 20 31 20 57  ex_enabled = 1 W
282b0 48 45 52 45 20 55 70 70 65 72 28 66 5f 74 61 62  HERE Upper(f_tab
282c0 6c 65 5f 6e 61 6d 65 29 20 3d 20 55 70 70 65 72  le_name) = Upper
282d0 28 27 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ('");.    strcat
282e0 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c 65 29   (sql, sqltable)
282f0 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
28300 6c 2c 20 22 27 29 20 41 4e 44 20 55 70 70 65 72  l, "') AND Upper
28310 28 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75  (f_geometry_colu
28320 6d 6e 29 20 3d 20 55 70 70 65 72 28 27 22 29 3b  mn) = Upper('");
28330 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
28340 2c 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20  , sqlcolumn);.  
28350 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
28360 27 29 20 41 4e 44 20 73 70 61 74 69 61 6c 5f 69  ') AND spatial_i
28370 6e 64 65 78 5f 65 6e 61 62 6c 65 64 20 3d 20 30  ndex_enabled = 0
28380 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71  ");.    ret = sq
28390 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
283a0 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e  te, sql, NULL, N
283b0 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20  ULL, &errMsg);. 
283c0 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
283d0 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65  LITE_OK)..goto e
283e0 72 72 6f 72 3b 0a 20 20 20 20 69 66 20 28 73 71  rror;.    if (sq
283f0 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 20 28 73  lite3_changes (s
28400 71 6c 69 74 65 29 20 3d 3d 20 30 29 0a 20 20 20  qlite) == 0).   
28410 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
28420 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 43 72  te_e..      ("Cr
28430 65 61 74 65 53 70 61 74 69 61 6c 49 6e 64 65 78  eateSpatialIndex
28440 28 29 20 65 72 72 6f 72 3a 20 65 69 74 68 65 72  () error: either
28450 20 5c 22 25 73 5c 22 2e 5c 22 25 73 5c 22 20 69   \"%s\".\"%s\" i
28460 73 6e 27 74 20 61 20 47 65 6f 6d 65 74 72 79 20  sn't a Geometry 
28470 63 6f 6c 75 6d 6e 20 6f 72 20 61 20 53 70 61 74  column or a Spat
28480 69 61 6c 49 6e 64 65 78 20 69 73 20 61 6c 72 65  ialIndex is alre
28490 61 64 79 20 64 65 66 69 6e 65 64 5c 6e 22 2c 0a  ady defined\n",.
284a0 09 20 20 20 20 20 20 20 74 61 62 6c 65 2c 20 63  .       table, c
284b0 6f 6c 75 6d 6e 29 3b 0a 09 20 20 73 71 6c 69 74  olumn);..  sqlit
284c0 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
284d0 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72  ontext, 0);..  r
284e0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
284f0 20 20 20 75 70 64 61 74 65 47 65 6f 6d 65 74 72     updateGeometr
28500 79 54 72 69 67 67 65 72 73 20 28 73 71 6c 69 74  yTriggers (sqlit
28510 65 2c 20 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e  e, table, column
28520 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
28530 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
28540 78 74 2c 20 31 29 3b 0a 20 20 20 20 73 74 72 63  xt, 1);.    strc
28550 70 79 20 28 73 71 6c 2c 20 22 52 2a 54 72 65 65  py (sql, "R*Tree
28560 20 53 70 61 74 69 61 6c 20 49 6e 64 65 78 20 73   Spatial Index s
28570 75 63 63 65 73 73 66 75 6c 6c 79 20 63 72 65 61  uccessfully crea
28580 74 65 64 22 29 3b 0a 20 20 20 20 75 70 64 61 74  ted");.    updat
28590 65 53 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f  eSpatiaLiteHisto
285a0 72 79 20 28 73 71 6c 69 74 65 2c 20 74 61 62 6c  ry (sqlite, tabl
285b0 65 2c 20 63 6f 6c 75 6d 6e 2c 20 73 71 6c 29 3b  e, column, sql);
285c0 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 65  .    return;.  e
285d0 72 72 6f 72 3a 0a 20 20 20 20 73 70 61 74 69 61  rror:.    spatia
285e0 6c 69 74 65 5f 65 20 28 22 43 72 65 61 74 65 53  lite_e ("CreateS
285f0 70 61 74 69 61 6c 49 6e 64 65 78 28 29 20 65 72  patialIndex() er
28600 72 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 20  ror: \"%s\"\n", 
28610 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c  errMsg);.    sql
28620 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73  ite3_free (errMs
28630 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  g);.    sqlite3_
28640 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
28650 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 72 65 74  ext, 0);.    ret
28660 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  urn;.}..static v
28670 6f 69 64 0a 66 6e 63 74 5f 43 72 65 61 74 65 4d  oid.fnct_CreateM
28680 62 72 43 61 63 68 65 20 28 73 71 6c 69 74 65 33  brCache (sqlite3
28690 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
286a0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
286b0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
286c0 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
286d0 6e 63 74 69 6f 6e 3a 0a 2f 20 43 72 65 61 74 65  nction:./ Create
286e0 4d 62 72 43 61 63 68 65 28 74 61 62 6c 65 2c 20  MbrCache(table, 
286f0 63 6f 6c 75 6d 6e 20 29 0a 2f 0a 2f 20 63 72 65  column )././ cre
28700 61 74 65 73 20 61 6e 20 4d 42 52 20 43 61 63 68  ates an MBR Cach
28710 65 20 62 61 73 65 64 20 6f 6e 20 43 6f 6c 75 6d  e based on Colum
28720 6e 20 61 6e 64 20 54 61 62 6c 65 0a 2f 20 72 65  n and Table./ re
28730 74 75 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65  turns 1 on succe
28740 73 73 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72  ss./ 0 on failur
28750 65 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63  e.*/.    const c
28760 68 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20  har *table;.    
28770 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75  const char *colu
28780 6d 6e 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c  mn;.    char sql
28790 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72  [1024];.    char
287a0 20 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b   *errMsg = NULL;
287b0 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20  .    int ret;.  
287c0 20 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b    char sqltable[
287d0 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
287e0 73 71 6c 63 6f 6c 75 6d 6e 5b 31 30 32 34 5d 3b  sqlcolumn[1024];
287f0 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71  .    sqlite3 *sq
28800 6c 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63  lite = sqlite3_c
28810 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
28820 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
28830 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
28840 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
28850 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
28860 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
28870 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
28880 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20  LITE_TEXT).     
28890 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
288a0 5f 65 0a 09 20 20 20 20 20 20 28 22 43 72 65 61  _e..      ("Crea
288b0 74 65 4d 62 72 43 61 63 68 65 28 29 20 65 72 72  teMbrCache() err
288c0 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 31 20 5b  or: argument 1 [
288d0 74 61 62 6c 65 5f 6e 61 6d 65 5d 20 69 73 20 6e  table_name] is n
288e0 6f 74 20 6f 66 20 74 68 65 20 53 74 72 69 6e 67  ot of the String
288f0 20 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20 73 71   type\n");..  sq
28900 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
28910 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
28920 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
28930 7d 0a 20 20 20 20 74 61 62 6c 65 20 3d 20 28 63  }.    table = (c
28940 6f 6e 73 74 20 63 68 61 72 20 2a 29 20 73 71 6c  onst char *) sql
28950 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
28960 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69  (argv[0]);.    i
28970 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
28980 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20  _type (argv[1]) 
28990 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a  != SQLITE_TEXT).
289a0 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
289b0 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28  alite_e..      (
289c0 22 43 72 65 61 74 65 4d 62 72 43 61 63 68 65 28  "CreateMbrCache(
289d0 29 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e  ) error: argumen
289e0 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  t 2 [column_name
289f0 5d 20 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20  ] is not of the 
28a00 53 74 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b  String type\n");
28a10 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
28a20 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
28a30 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a   0);..  return;.
28a40 20 20 20 20 20 20 7d 0a 20 20 20 20 63 6f 6c 75        }.    colu
28a50 6d 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  mn = (const char
28a60 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
28a70 65 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d 29  e_text (argv[1])
28a80 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
28a90 6c 74 61 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a  ltable, table);.
28aa0 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74      clean_sql_st
28ab0 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b  ring (sqltable);
28ac0 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
28ad0 63 6f 6c 75 6d 6e 2c 20 63 6f 6c 75 6d 6e 29 3b  column, column);
28ae0 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73  .    clean_sql_s
28af0 74 72 69 6e 67 20 28 73 71 6c 63 6f 6c 75 6d 6e  tring (sqlcolumn
28b00 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73  );.    strcpy (s
28b10 71 6c 2c 0a 09 20 20 20 20 22 55 50 44 41 54 45  ql,..    "UPDATE
28b20 20 67 65 6f 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e   geometry_column
28b30 73 20 53 45 54 20 73 70 61 74 69 61 6c 5f 69 6e  s SET spatial_in
28b40 64 65 78 5f 65 6e 61 62 6c 65 64 20 3d 20 32 20  dex_enabled = 2 
28b50 57 48 45 52 45 20 55 70 70 65 72 28 66 5f 74 61  WHERE Upper(f_ta
28b60 62 6c 65 5f 6e 61 6d 65 29 20 3d 20 55 70 70 65  ble_name) = Uppe
28b70 72 28 27 22 29 3b 0a 20 20 20 20 73 74 72 63 61  r('");.    strca
28b80 74 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c 65  t (sql, sqltable
28b90 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
28ba0 71 6c 2c 20 22 27 29 20 41 4e 44 20 55 70 70 65  ql, "') AND Uppe
28bb0 72 28 66 5f 67 65 6f 6d 65 74 72 79 5f 63 6f 6c  r(f_geometry_col
28bc0 75 6d 6e 29 20 3d 20 55 70 70 65 72 28 27 22 29  umn) = Upper('")
28bd0 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
28be0 6c 2c 20 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20  l, sqlcolumn);. 
28bf0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
28c00 22 27 29 20 41 4e 44 20 73 70 61 74 69 61 6c 5f  "') AND spatial_
28c10 69 6e 64 65 78 5f 65 6e 61 62 6c 65 64 20 3d 20  index_enabled = 
28c20 30 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73  0");.    ret = s
28c30 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
28c40 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
28c50 4e 55 4c 4c 2c 20 26 65 72 72 4d 73 67 29 3b 0a  NULL, &errMsg);.
28c60 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53      if (ret != S
28c70 51 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20  QLITE_OK)..goto 
28c80 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 20 28 73  error;.    if (s
28c90 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 20 28  qlite3_changes (
28ca0 73 71 6c 69 74 65 29 20 3d 3d 20 30 29 0a 20 20  sqlite) == 0).  
28cb0 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c      {..  spatial
28cc0 69 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 43  ite_e..      ("C
28cd0 72 65 61 74 65 4d 62 72 43 61 63 68 65 28 29 20  reateMbrCache() 
28ce0 65 72 72 6f 72 3a 20 65 69 74 68 65 72 20 5c 22  error: either \"
28cf0 25 73 5c 22 2e 5c 22 25 73 5c 22 20 69 73 6e 27  %s\".\"%s\" isn'
28d00 74 20 61 20 47 65 6f 6d 65 74 72 79 20 63 6f 6c  t a Geometry col
28d10 75 6d 6e 20 6f 72 20 61 20 53 70 61 74 69 61 6c  umn or a Spatial
28d20 49 6e 64 65 78 20 69 73 20 61 6c 72 65 61 64 79  Index is already
28d30 20 64 65 66 69 6e 65 64 5c 6e 22 2c 0a 09 20 20   defined\n",..  
28d40 20 20 20 20 20 74 61 62 6c 65 2c 20 63 6f 6c 75       table, colu
28d50 6d 6e 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  mn);..  sqlite3_
28d60 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
28d70 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75  ext, 0);..  retu
28d80 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
28d90 75 70 64 61 74 65 47 65 6f 6d 65 74 72 79 54 72  updateGeometryTr
28da0 69 67 67 65 72 73 20 28 73 71 6c 69 74 65 2c 20  iggers (sqlite, 
28db0 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e 29 3b 0a  table, column);.
28dc0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
28dd0 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
28de0 20 31 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20   1);.    strcpy 
28df0 28 73 71 6c 2c 20 22 4d 62 72 43 61 63 68 65 20  (sql, "MbrCache 
28e00 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 72 65  successfully cre
28e10 61 74 65 64 22 29 3b 0a 20 20 20 20 75 70 64 61  ated");.    upda
28e20 74 65 53 70 61 74 69 61 4c 69 74 65 48 69 73 74  teSpatiaLiteHist
28e30 6f 72 79 20 28 73 71 6c 69 74 65 2c 20 74 61 62  ory (sqlite, tab
28e40 6c 65 2c 20 63 6f 6c 75 6d 6e 2c 20 73 71 6c 29  le, column, sql)
28e50 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
28e60 65 72 72 6f 72 3a 0a 20 20 20 20 73 70 61 74 69  error:.    spati
28e70 61 6c 69 74 65 5f 65 20 28 22 43 72 65 61 74 65  alite_e ("Create
28e80 4d 62 72 43 61 63 68 65 28 29 20 65 72 72 6f 72  MbrCache() error
28e90 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 65 72 72  : \"%s\"\n", err
28ea0 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Msg);.    sqlite
28eb0 33 5f 66 72 65 65 20 28 65 72 72 4d 73 67 29 3b  3_free (errMsg);
28ec0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
28ed0 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
28ee0 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
28ef0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
28f00 0a 66 6e 63 74 5f 44 69 73 61 62 6c 65 53 70 61  .fnct_DisableSpa
28f10 74 69 61 6c 49 6e 64 65 78 20 28 73 71 6c 69 74  tialIndex (sqlit
28f20 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
28f30 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a  text, int argc,.
28f40 09 09 09 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ...  sqlite3_val
28f50 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
28f60 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
28f70 20 44 69 73 61 62 6c 65 53 70 61 74 69 61 6c 49   DisableSpatialI
28f80 6e 64 65 78 28 74 61 62 6c 65 2c 20 63 6f 6c 75  ndex(table, colu
28f90 6d 6e 20 29 0a 2f 0a 2f 20 64 69 73 61 62 6c 65  mn )././ disable
28fa0 73 20 61 20 53 70 61 74 69 61 6c 49 6e 64 65 78  s a SpatialIndex
28fb0 20 62 61 73 65 64 20 6f 6e 20 43 6f 6c 75 6d 6e   based on Column
28fc0 20 61 6e 64 20 54 61 62 6c 65 0a 2f 20 72 65 74   and Table./ ret
28fd0 75 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65 73  urns 1 on succes
28fe0 73 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72 65  s./ 0 on failure
28ff0 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  .*/.    const ch
29000 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20 63  ar *table;.    c
29010 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
29020 6e 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 5b  n;.    char sql[
29030 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
29040 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a  *errMsg = NULL;.
29050 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
29060 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31   char sqltable[1
29070 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 73  024];.    char s
29080 71 6c 63 6f 6c 75 6d 6e 5b 31 30 32 34 5d 3b 0a  qlcolumn[1024];.
29090 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c      sqlite3 *sql
290a0 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ite = sqlite3_co
290b0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20  ntext_db_handle 
290c0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47  (context);.    G
290d0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
290e0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
290f0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
29100 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
29110 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
29120 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20  ITE_TEXT).      
29130 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
29140 65 0a 09 20 20 20 20 20 20 28 22 44 69 73 61 62  e..      ("Disab
29150 6c 65 53 70 61 74 69 61 6c 49 6e 64 65 78 28 29  leSpatialIndex()
29160 20 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74   error: argument
29170 20 31 20 5b 74 61 62 6c 65 5f 6e 61 6d 65 5d 20   1 [table_name] 
29180 69 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 53 74  is not of the St
29190 72 69 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a 09  ring type\n");..
291a0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
291b0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
291c0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
291d0 20 20 20 20 7d 0a 20 20 20 20 74 61 62 6c 65 20      }.    table 
291e0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
291f0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
29200 65 78 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  ext (argv[0]);. 
29210 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
29220 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
29230 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 54 45  1]) != SQLITE_TE
29240 58 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  XT).      {..  s
29250 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20 20 20  patialite_e..   
29260 20 20 20 28 22 44 69 73 61 62 6c 65 53 70 61 74     ("DisableSpat
29270 69 61 6c 49 6e 64 65 78 28 29 20 65 72 72 6f 72  ialIndex() error
29280 3a 20 61 72 67 75 6d 65 6e 74 20 32 20 5b 63 6f  : argument 2 [co
29290 6c 75 6d 6e 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f  lumn_name] is no
292a0 74 20 6f 66 20 74 68 65 20 53 74 72 69 6e 67 20  t of the String 
292b0 74 79 70 65 5c 6e 22 29 3b 0a 09 20 20 73 71 6c  type\n");..  sql
292c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
292d0 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20  (context, 0);.. 
292e0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
292f0 0a 20 20 20 20 63 6f 6c 75 6d 6e 20 3d 20 28 63  .    column = (c
29300 6f 6e 73 74 20 63 68 61 72 20 2a 29 20 73 71 6c  onst char *) sql
29310 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
29320 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 73  (argv[1]);.    s
29330 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c  trcpy (sqltable,
29340 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 63 6c 65   table);.    cle
29350 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28 73  an_sql_string (s
29360 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  qltable);.    st
29370 72 63 70 79 20 28 73 71 6c 63 6f 6c 75 6d 6e 2c  rcpy (sqlcolumn,
29380 20 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 63 6c   column);.    cl
29390 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28  ean_sql_string (
293a0 73 71 6c 63 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  sqlcolumn);.    
293b0 73 74 72 63 70 79 20 28 73 71 6c 2c 0a 09 20 20  strcpy (sql,..  
293c0 20 20 22 55 50 44 41 54 45 20 67 65 6f 6d 65 74    "UPDATE geomet
293d0 72 79 5f 63 6f 6c 75 6d 6e 73 20 53 45 54 20 73  ry_columns SET s
293e0 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 65 6e 61  patial_index_ena
293f0 62 6c 65 64 20 3d 20 30 20 57 48 45 52 45 20 55  bled = 0 WHERE U
29400 70 70 65 72 28 66 5f 74 61 62 6c 65 5f 6e 61 6d  pper(f_table_nam
29410 65 29 20 3d 20 55 70 70 65 72 28 27 22 29 3b 0a  e) = Upper('");.
29420 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
29430 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20   sqltable);.    
29440 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 29  strcat (sql, "')
29450 20 41 4e 44 20 55 70 70 65 72 28 66 5f 67 65 6f   AND Upper(f_geo
29460 6d 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 3d 20  metry_column) = 
29470 55 70 70 65 72 28 27 22 29 3b 0a 20 20 20 20 73  Upper('");.    s
29480 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 63  trcat (sql, sqlc
29490 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 74 72 63  olumn);.    strc
294a0 61 74 20 28 73 71 6c 2c 20 22 27 29 20 41 4e 44  at (sql, "') AND
294b0 20 73 70 61 74 69 61 6c 5f 69 6e 64 65 78 5f 65   spatial_index_e
294c0 6e 61 62 6c 65 64 20 3c 3e 20 30 22 29 3b 0a 20  nabled <> 0");. 
294d0 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33     ret = sqlite3
294e0 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73  _exec (sqlite, s
294f0 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  ql, NULL, NULL, 
29500 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  &errMsg);.    if
29510 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
29520 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b  OK)..goto error;
29530 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
29540 5f 63 68 61 6e 67 65 73 20 28 73 71 6c 69 74 65  _changes (sqlite
29550 29 20 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a  ) == 0).      {.
29560 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a  .  spatialite_e.
29570 09 20 20 20 20 20 20 28 22 44 69 73 61 62 6c 65  .      ("Disable
29580 53 70 61 74 69 61 6c 49 6e 64 65 78 28 29 20 65  SpatialIndex() e
29590 72 72 6f 72 3a 20 65 69 74 68 65 72 20 5c 22 25  rror: either \"%
295a0 73 5c 22 2e 5c 22 25 73 5c 22 20 69 73 6e 27 74  s\".\"%s\" isn't
295b0 20 61 20 47 65 6f 6d 65 74 72 79 20 63 6f 6c 75   a Geometry colu
295c0 6d 6e 20 6f 72 20 6e 6f 20 53 70 61 74 69 61 6c  mn or no Spatial
295d0 49 6e 64 65 78 20 69 73 20 64 65 66 69 6e 65 64  Index is defined
295e0 5c 6e 22 2c 0a 09 20 20 20 20 20 20 20 74 61 62  \n",..       tab
295f0 6c 65 2c 20 63 6f 6c 75 6d 6e 29 3b 0a 09 20 20  le, column);..  
29600 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
29610 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  nt (context, 0);
29620 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
29630 20 20 7d 0a 20 20 20 20 75 70 64 61 74 65 47 65    }.    updateGe
29640 6f 6d 65 74 72 79 54 72 69 67 67 65 72 73 20 28  ometryTriggers (
29650 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 2c 20 63  sqlite, table, c
29660 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 71 6c 69  olumn);.    sqli
29670 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
29680 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 20  context, 1);.   
29690 20 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 53   strcpy (sql, "S
296a0 70 61 74 69 61 6c 49 6e 64 65 78 20 73 75 63 63  patialIndex succ
296b0 65 73 73 66 75 6c 6c 79 20 64 69 73 61 62 6c 65  essfully disable
296c0 64 22 29 3b 0a 20 20 20 20 75 70 64 61 74 65 53  d");.    updateS
296d0 70 61 74 69 61 4c 69 74 65 48 69 73 74 6f 72 79  patiaLiteHistory
296e0 20 28 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 2c   (sqlite, table,
296f0 20 63 6f 6c 75 6d 6e 2c 20 73 71 6c 29 3b 0a 20   column, sql);. 
29700 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 65 72 72     return;.  err
29710 6f 72 3a 0a 20 20 20 20 73 70 61 74 69 61 6c 69  or:.    spatiali
29720 74 65 5f 65 20 28 22 44 69 73 61 62 6c 65 53 70  te_e ("DisableSp
29730 61 74 69 61 6c 49 6e 64 65 78 28 29 20 65 72 72  atialIndex() err
29740 6f 72 3a 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 65  or: \"%s\"\n", e
29750 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69  rrMsg);.    sqli
29760 74 65 33 5f 66 72 65 65 20 28 65 72 72 4d 73 67  te3_free (errMsg
29770 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
29780 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
29790 78 74 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75  xt, 0);.    retu
297a0 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  rn;.}..static vo
297b0 69 64 0a 66 6e 63 74 5f 52 65 62 75 69 6c 64 47  id.fnct_RebuildG
297c0 65 6f 6d 65 74 72 79 54 72 69 67 67 65 72 73 20  eometryTriggers 
297d0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
297e0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
297f0 61 72 67 63 2c 0a 09 09 09 20 20 20 20 20 20 73  argc,....      s
29800 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
29810 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
29820 75 6e 63 74 69 6f 6e 3a 0a 2f 20 52 65 62 75 69  unction:./ Rebui
29830 6c 64 47 65 6f 6d 65 74 72 79 54 72 69 67 67 65  ldGeometryTrigge
29840 72 73 28 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e  rs(table, column
29850 20 29 0a 2f 0a 2f 20 72 65 62 75 69 6c 64 73 20   )././ rebuilds 
29860 47 65 6f 6d 65 74 72 79 20 54 72 69 67 67 65 72  Geometry Trigger
29870 73 20 28 63 6f 6e 73 74 72 61 69 6e 74 73 29 20  s (constraints) 
29880 20 62 61 73 65 64 20 6f 6e 20 43 6f 6c 75 6d 6e   based on Column
29890 20 61 6e 64 20 54 61 62 6c 65 0a 2f 20 72 65 74   and Table./ ret
298a0 75 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65 73  urns 1 on succes
298b0 73 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72 65  s./ 0 on failure
298c0 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  .*/.    const ch
298d0 61 72 20 2a 74 61 62 6c 65 3b 0a 20 20 20 20 63  ar *table;.    c
298e0 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
298f0 6e 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 5b  n;.    char sql[
29900 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
29910 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a  *errMsg = NULL;.
29920 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
29930 20 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 73 3b   char **results;
29940 0a 20 20 20 20 69 6e 74 20 72 6f 77 73 3b 0a 20  .    int rows;. 
29950 20 20 20 69 6e 74 20 63 6f 6c 75 6d 6e 73 3b 0a     int columns;.
29960 20 20 20 20 63 68 61 72 20 73 71 6c 74 61 62 6c      char sqltabl
29970 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61  e[1024];.    cha
29980 72 20 73 71 6c 63 6f 6c 75 6d 6e 5b 31 30 32 34  r sqlcolumn[1024
29990 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ];.    sqlite3 *
299a0 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65 33  sqlite = sqlite3
299b0 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
299c0 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  le (context);.  
299d0 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
299e0 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
299f0 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
29a00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
29a10 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
29a20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20  SQLITE_TEXT).   
29a30 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69     {..  spatiali
29a40 74 65 5f 65 0a 09 20 20 20 20 20 20 28 22 52 65  te_e..      ("Re
29a50 62 75 69 6c 64 47 65 6f 6d 65 74 72 79 54 72 69  buildGeometryTri
29a60 67 67 65 72 73 28 29 20 65 72 72 6f 72 3a 20 61  ggers() error: a
29a70 72 67 75 6d 65 6e 74 20 31 20 5b 74 61 62 6c 65  rgument 1 [table
29a80 5f 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66  _name] is not of
29a90 20 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65   the String type
29aa0 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  \n");..  sqlite3
29ab0 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
29ac0 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74  text, 0);..  ret
29ad0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
29ae0 20 74 61 62 6c 65 20 3d 20 28 63 6f 6e 73 74 20   table = (const 
29af0 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
29b00 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76  value_text (argv
29b10 5b 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 73 71  [0]);.    if (sq
29b20 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
29b30 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51   (argv[1]) != SQ
29b40 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20  LITE_TEXT).     
29b50 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
29b60 5f 65 0a 09 20 20 20 20 20 20 28 22 52 65 62 75  _e..      ("Rebu
29b70 69 6c 64 47 65 6f 6d 65 74 72 79 54 72 69 67 67  ildGeometryTrigg
29b80 65 72 73 28 29 20 65 72 72 6f 72 3a 20 61 72 67  ers() error: arg
29b90 75 6d 65 6e 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f  ument 2 [column_
29ba0 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20  name] is not of 
29bb0 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c  the String type\
29bc0 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  n");..  sqlite3_
29bd0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
29be0 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75  ext, 0);..  retu
29bf0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
29c00 63 6f 6c 75 6d 6e 20 3d 20 28 63 6f 6e 73 74 20  column = (const 
29c10 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
29c20 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76  value_text (argv
29c30 5b 31 5d 29 3b 0a 20 20 20 20 73 74 72 63 70 79  [1]);.    strcpy
29c40 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c   (sqltable, tabl
29c50 65 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71  e);.    clean_sq
29c60 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74 61 62  l_string (sqltab
29c70 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20  le);.    strcpy 
29c80 28 73 71 6c 63 6f 6c 75 6d 6e 2c 20 63 6f 6c 75  (sqlcolumn, colu
29c90 6d 6e 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73  mn);.    clean_s
29ca0 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 63 6f  ql_string (sqlco
29cb0 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 74 72 63 70  lumn);.    strcp
29cc0 79 20 28 73 71 6c 2c 0a 09 20 20 20 20 22 53 45  y (sql,..    "SE
29cd0 4c 45 43 54 20 66 5f 74 61 62 6c 65 5f 6e 61 6d  LECT f_table_nam
29ce0 65 20 46 52 4f 4d 20 67 65 6f 6d 65 74 72 79 5f  e FROM geometry_
29cf0 63 6f 6c 75 6d 6e 73 20 57 48 45 52 45 20 55 70  columns WHERE Up
29d00 70 65 72 28 66 5f 74 61 62 6c 65 5f 6e 61 6d 65  per(f_table_name
29d10 29 20 3d 20 55 70 70 65 72 28 27 22 29 3b 0a 20  ) = Upper('");. 
29d20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
29d30 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
29d40 74 72 63 61 74 20 28 73 71 6c 2c 20 22 27 29 20  trcat (sql, "') 
29d50 41 4e 44 20 55 70 70 65 72 28 66 5f 67 65 6f 6d  AND Upper(f_geom
29d60 65 74 72 79 5f 63 6f 6c 75 6d 6e 29 20 3d 20 55  etry_column) = U
29d70 70 70 65 72 20 28 27 22 29 3b 0a 20 20 20 20 73  pper ('");.    s
29d80 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 63  trcat (sql, sqlc
29d90 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 74 72 63  olumn);.    strc
29da0 61 74 20 28 73 71 6c 2c 20 22 27 29 22 29 3b 0a  at (sql, "')");.
29db0 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65      ret = sqlite
29dc0 33 5f 67 65 74 5f 74 61 62 6c 65 20 28 73 71 6c  3_get_table (sql
29dd0 69 74 65 2c 20 73 71 6c 2c 20 26 72 65 73 75 6c  ite, sql, &resul
29de0 74 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f 6c 75  ts, &rows, &colu
29df0 6d 6e 73 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  mns, NULL);.    
29e00 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
29e10 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 65 72 72 6f  E_OK)..goto erro
29e20 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  r;.    sqlite3_f
29e30 72 65 65 5f 74 61 62 6c 65 20 28 72 65 73 75 6c  ree_table (resul
29e40 74 73 29 3b 0a 20 20 20 20 69 66 20 28 72 6f 77  ts);.    if (row
29e50 73 20 3c 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a  s <= 0).      {.
29e60 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a  .  spatialite_e.
29e70 09 20 20 20 20 20 20 28 22 52 65 62 75 69 6c 64  .      ("Rebuild
29e80 47 65 6f 6d 65 74 72 79 54 72 69 67 67 65 72 73  GeometryTriggers
29e90 28 29 20 65 72 72 6f 72 3a 20 5c 22 25 73 5c 22  () error: \"%s\"
29ea0 2e 5c 22 25 73 5c 22 20 69 73 6e 27 74 20 61 20  .\"%s\" isn't a 
29eb0 47 65 6f 6d 65 74 72 79 20 63 6f 6c 75 6d 6e 5c  Geometry column\
29ec0 6e 22 2c 0a 09 20 20 20 20 20 20 20 74 61 62 6c  n",..       tabl
29ed0 65 2c 20 63 6f 6c 75 6d 6e 29 3b 0a 09 20 20 73  e, column);..  s
29ee0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
29ef0 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  t (context, 0);.
29f00 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
29f10 20 7d 0a 20 20 20 20 75 70 64 61 74 65 47 65 6f   }.    updateGeo
29f20 6d 65 74 72 79 54 72 69 67 67 65 72 73 20 28 73  metryTriggers (s
29f30 71 6c 69 74 65 2c 20 74 61 62 6c 65 2c 20 63 6f  qlite, table, co
29f40 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  lumn);.    sqlit
29f50 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
29f60 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 20 20 20 20  ontext, 1);.    
29f70 73 74 72 63 70 79 20 28 73 71 6c 2c 20 22 47 65  strcpy (sql, "Ge
29f80 6f 6d 65 74 72 79 20 54 72 69 67 67 65 72 73 20  ometry Triggers 
29f90 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 62  successfully reb
29fa0 75 69 6c 74 22 29 3b 0a 20 20 20 20 75 70 64 61  uilt");.    upda
29fb0 74 65 53 70 61 74 69 61 4c 69 74 65 48 69 73 74  teSpatiaLiteHist
29fc0 6f 72 79 20 28 73 71 6c 69 74 65 2c 20 74 61 62  ory (sqlite, tab
29fd0 6c 65 2c 20 63 6f 6c 75 6d 6e 2c 20 73 71 6c 29  le, column, sql)
29fe0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
29ff0 65 72 72 6f 72 3a 0a 20 20 20 20 73 70 61 74 69  error:.    spati
2a000 61 6c 69 74 65 5f 65 20 28 22 52 65 62 75 69 6c  alite_e ("Rebuil
2a010 64 47 65 6f 6d 65 74 72 79 54 72 69 67 67 65 72  dGeometryTrigger
2a020 73 28 29 20 65 72 72 6f 72 3a 20 5c 22 25 73 5c  s() error: \"%s\
2a030 22 5c 6e 22 2c 20 65 72 72 4d 73 67 29 3b 0a 20  "\n", errMsg);. 
2a040 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20     sqlite3_free 
2a050 28 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71  (errMsg);.    sq
2a060 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
2a070 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20   (context, 0);. 
2a080 20 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 0a 73     return;.}...s
2a090 74 61 74 69 63 20 69 6e 74 0a 63 68 65 63 6b 5f  tatic int.check_
2a0a0 74 6f 70 6f 5f 74 61 62 6c 65 20 28 73 71 6c 69  topo_table (sqli
2a0b0 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f  te3 * sqlite, co
2a0c0 6e 73 74 20 63 68 61 72 20 2a 74 61 62 6c 65 2c  nst char *table,
2a0d0 20 69 6e 74 20 69 73 5f 76 69 65 77 29 0a 7b 0a   int is_view).{.
2a0e0 2f 2a 20 63 68 65 63 6b 69 6e 67 20 69 66 20 73  /* checking if s
2a0f0 6f 6d 65 20 54 6f 70 6f 6c 6f 67 79 2d 72 65 6c  ome Topology-rel
2a100 61 74 65 64 20 74 61 62 6c 65 2f 76 69 65 77 20  ated table/view 
2a110 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a  already exists *
2a120 2f 0a 20 20 20 20 69 6e 74 20 65 78 69 73 74 73  /.    int exists
2a130 20 3d 20 30 3b 0a 20 20 20 20 63 68 61 72 20 73   = 0;.    char s
2a140 71 6c 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68  ql[2048];.    ch
2a150 61 72 20 73 71 6c 74 61 62 6c 65 5b 31 30 32 34  ar sqltable[1024
2a160 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72  ];.    char *err
2a170 4d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  Msg = NULL;.    
2a180 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61  int ret;.    cha
2a190 72 20 2a 2a 72 65 73 75 6c 74 73 3b 0a 20 20 20  r **results;.   
2a1a0 20 69 6e 74 20 72 6f 77 73 3b 0a 20 20 20 20 69   int rows;.    i
2a1b0 6e 74 20 63 6f 6c 75 6d 6e 73 3b 0a 20 20 20 20  nt columns;.    
2a1c0 69 6e 74 20 69 3b 0a 20 20 20 20 73 74 72 63 70  int i;.    strcp
2a1d0 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61 62  y (sqltable, tab
2a1e0 6c 65 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73  le);.    clean_s
2a1f0 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74 61  ql_string (sqlta
2a200 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
2a210 66 20 28 73 71 6c 2c 0a 09 20 20 20 20 20 22 53  f (sql,..     "S
2a220 45 4c 45 43 54 20 6e 61 6d 65 20 46 52 4f 4d 20  ELECT name FROM 
2a230 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
2a240 45 52 45 20 74 79 70 65 20 3d 20 27 25 73 27 20  ERE type = '%s' 
2a250 41 4e 44 20 55 70 70 65 72 28 6e 61 6d 65 29 20  AND Upper(name) 
2a260 3d 20 55 70 70 65 72 28 27 25 73 27 29 22 2c 0a  = Upper('%s')",.
2a270 09 20 20 20 20 20 28 21 69 73 5f 76 69 65 77 29  .     (!is_view)
2a280 20 3f 20 22 74 61 62 6c 65 22 20 3a 20 22 76 69   ? "table" : "vi
2a290 65 77 22 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20  ew", table);.   
2a2a0 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 67   ret = sqlite3_g
2a2b0 65 74 5f 74 61 62 6c 65 20 28 73 71 6c 69 74 65  et_table (sqlite
2a2c0 2c 20 73 71 6c 2c 20 26 72 65 73 75 6c 74 73 2c  , sql, &results,
2a2d0 20 26 72 6f 77 73 2c 20 26 63 6f 6c 75 6d 6e 73   &rows, &columns
2a2e0 2c 20 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20  , &errMsg);.    
2a2f0 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
2a300 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20  E_OK).      {.. 
2a310 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65   sqlite3_free (e
2a320 72 72 4d 73 67 29 3b 0a 09 20 20 72 65 74 75 72  rrMsg);..  retur
2a330 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2a340 20 66 6f 72 20 28 69 20 3d 20 31 3b 20 69 20 3c   for (i = 1; i <
2a350 3d 20 72 6f 77 73 3b 20 69 2b 2b 29 0a 09 65 78  = rows; i++)..ex
2a360 69 73 74 73 20 3d 20 31 3b 0a 20 20 20 20 73 71  ists = 1;.    sq
2a370 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
2a380 20 28 72 65 73 75 6c 74 73 29 3b 0a 20 20 20 20   (results);.    
2a390 72 65 74 75 72 6e 20 65 78 69 73 74 73 3b 0a 7d  return exists;.}
2a3a0 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 63 72 65  ..static int.cre
2a3b0 61 74 65 5f 74 6f 70 6f 5f 6e 6f 64 65 73 20 28  ate_topo_nodes (
2a3c0 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69 74 65  sqlite3 * sqlite
2a3d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 61  , const char *ta
2a3e0 62 6c 65 2c 20 69 6e 74 20 73 72 69 64 2c 20 69  ble, int srid, i
2a3f0 6e 74 20 64 69 6d 73 29 0a 7b 0a 2f 2a 20 63 72  nt dims).{./* cr
2a400 65 61 74 69 6e 67 20 74 68 65 20 74 6f 70 6f 5f  eating the topo_
2a410 6e 6f 64 65 73 20 74 61 62 6c 65 20 2a 2f 0a 20  nodes table */. 
2a420 20 20 20 63 68 61 72 20 73 71 6c 5b 32 30 34 38     char sql[2048
2a430 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 32  ];.    char sql2
2a440 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61 72  [2048];.    char
2a450 20 73 71 6c 74 61 62 6c 65 5b 31 30 32 34 5d 3b   sqltable[1024];
2a460 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20  .    int ret;.  
2a470 20 20 63 68 61 72 20 2a 65 72 72 5f 6d 73 67 20    char *err_msg 
2a480 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 73 74 72 63  = NULL;.    strc
2a490 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61  py (sqltable, ta
2a4a0 62 6c 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65  ble);.    double
2a4b0 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c  _quoted_sql (sql
2a4c0 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69  table);.    spri
2a4d0 6e 74 66 20 28 73 71 6c 2c 20 22 43 52 45 41 54  ntf (sql, "CREAT
2a4e0 45 20 54 41 42 4c 45 20 25 73 20 28 5c 6e 22 2c  E TABLE %s (\n",
2a4f0 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20   sqltable);.    
2a500 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6e 6f  strcat (sql, "no
2a510 64 65 5f 69 64 20 49 4e 54 45 47 45 52 20 50 52  de_id INTEGER PR
2a520 49 4d 41 52 59 20 4b 45 59 20 41 55 54 4f 49 4e  IMARY KEY AUTOIN
2a530 43 52 45 4d 45 4e 54 2c 5c 6e 22 29 3b 0a 20 20  CREMENT,\n");.  
2a540 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2a550 6e 6f 64 65 5f 63 6f 64 65 20 54 45 58 54 29 22  node_code TEXT)"
2a560 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c  );.    ret = sql
2a570 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74  ite3_exec (sqlit
2a580 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55  e, sql, NULL, NU
2a590 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20  LL, &err_msg);. 
2a5a0 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
2a5b0 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b  LITE_OK).      {
2a5c0 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
2a5d0 20 28 22 43 52 45 41 54 45 20 54 41 42 4c 45 20   ("CREATE TABLE 
2a5e0 27 25 73 27 20 65 72 72 6f 72 3a 20 25 73 5c 6e  '%s' error: %s\n
2a5f0 22 2c 20 74 61 62 6c 65 2c 20 65 72 72 5f 6d 73  ", table, err_ms
2a600 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66  g);..  sqlite3_f
2a610 72 65 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09  ree (err_msg);..
2a620 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
2a630 20 20 7d 0a 20 20 20 20 73 74 72 63 70 79 20 28    }.    strcpy (
2a640 73 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c 65 29  sqltable, table)
2a650 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f  ;.    clean_sql_
2a660 73 74 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65  string (sqltable
2a670 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  );.    sprintf (
2a680 73 71 6c 2c 0a 09 20 20 20 20 20 22 53 45 4c 45  sql,..     "SELE
2a690 43 54 20 41 64 64 47 65 6f 6d 65 74 72 79 43 6f  CT AddGeometryCo
2a6a0 6c 75 6d 6e 28 27 25 73 27 2c 20 27 47 65 6f 6d  lumn('%s', 'Geom
2a6b0 65 74 72 79 27 2c 20 25 64 2c 20 27 50 4f 49 4e  etry', %d, 'POIN
2a6c0 54 27 2c 20 27 25 73 27 2c 20 31 29 22 2c 0a 09  T', '%s', 1)",..
2a6d0 20 20 20 20 20 73 71 6c 74 61 62 6c 65 2c 20 73       sqltable, s
2a6e0 72 69 64 2c 20 28 64 69 6d 73 20 3d 3d 20 47 41  rid, (dims == GA
2a6f0 49 41 5f 58 59 5f 5a 29 20 3f 20 22 58 59 5a 22  IA_XY_Z) ? "XYZ"
2a700 20 3a 20 22 58 59 22 29 3b 0a 20 20 20 20 72 65   : "XY");.    re
2a710 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
2a720 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
2a730 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f  ULL, NULL, &err_
2a740 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  msg);.    if (re
2a750 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
2a760 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
2a770 61 6c 69 74 65 5f 65 20 28 22 41 64 64 47 65 6f  alite_e ("AddGeo
2a780 6d 65 74 72 79 43 6f 6c 75 6d 6e 20 27 25 73 27  metryColumn '%s'
2a790 2e 27 47 65 6f 6d 65 74 72 79 27 20 65 72 72 6f  .'Geometry' erro
2a7a0 72 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 74 61 62  r: %s\n",....tab
2a7b0 6c 65 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09 20  le, err_msg);.. 
2a7c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65   sqlite3_free (e
2a7d0 72 72 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74 75  rr_msg);..  retu
2a7e0 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
2a7f0 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
2a800 22 53 45 4c 45 43 54 20 43 72 65 61 74 65 53 70  "SELECT CreateSp
2a810 61 74 69 61 6c 49 6e 64 65 78 28 27 25 73 27 2c  atialIndex('%s',
2a820 20 27 47 65 6f 6d 65 74 72 79 27 29 22 2c 20 73   'Geometry')", s
2a830 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 72 65  qltable);.    re
2a840 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
2a850 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
2a860 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f  ULL, NULL, &err_
2a870 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  msg);.    if (re
2a880 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
2a890 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
2a8a0 61 6c 69 74 65 5f 65 20 28 22 43 72 65 61 74 65  alite_e ("Create
2a8b0 53 70 61 74 69 61 6c 49 6e 64 65 78 20 27 25 73  SpatialIndex '%s
2a8c0 27 2e 27 47 65 6f 6d 65 74 72 79 27 20 65 72 72  '.'Geometry' err
2a8d0 6f 72 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 74 61  or: %s\n",....ta
2a8e0 62 6c 65 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09  ble, err_msg);..
2a8f0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28    sqlite3_free (
2a900 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74  err_msg);..  ret
2a910 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
2a920 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61     strcpy (sqlta
2a930 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20  ble, table);.   
2a940 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
2a950 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql (sqltable);. 
2a960 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32     sprintf (sql2
2a970 2c 20 22 69 64 78 5f 25 73 5f 63 6f 64 65 22 2c  , "idx_%s_code",
2a980 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20   sqltable);.    
2a990 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
2a9a0 6c 20 28 73 71 6c 32 29 3b 0a 20 20 20 20 73 70  l (sql2);.    sp
2a9b0 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 43 52 45  rintf (sql, "CRE
2a9c0 41 54 45 20 49 4e 44 45 58 20 25 73 20 4f 4e 20  ATE INDEX %s ON 
2a9d0 25 73 20 28 6e 6f 64 65 5f 63 6f 64 65 29 22 2c  %s (node_code)",
2a9e0 20 73 71 6c 32 2c 20 73 71 6c 74 61 62 6c 65 29   sql2, sqltable)
2a9f0 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
2aa00 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
2aa10 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c  , sql, NULL, NUL
2aa20 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20  L, &err_msg);.  
2aa30 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
2aa40 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a  ITE_OK).      {.
2aa50 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20  .  spatialite_e 
2aa60 28 22 43 72 65 61 74 65 20 49 6e 64 65 78 20 27  ("Create Index '
2aa70 25 73 27 28 27 6e 6f 64 65 5f 63 6f 64 65 27 29  %s'('node_code')
2aa80 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 0a 09   error: %s\n",..
2aa90 09 09 73 71 6c 74 61 62 6c 65 2c 20 65 72 72 5f  ..sqltable, err_
2aaa0 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  msg);..  sqlite3
2aab0 5f 66 72 65 65 20 28 65 72 72 5f 6d 73 67 29 3b  _free (err_msg);
2aac0 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ..  return 0;.  
2aad0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
2aae0 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e   1;.}..static in
2aaf0 74 0a 63 72 65 61 74 65 5f 74 6f 70 6f 5f 65 64  t.create_topo_ed
2ab00 67 65 73 20 28 73 71 6c 69 74 65 33 20 2a 20 73  ges (sqlite3 * s
2ab10 71 6c 69 74 65 2c 20 63 6f 6e 73 74 20 63 68 61  qlite, const cha
2ab20 72 20 2a 74 61 62 6c 65 2c 20 69 6e 74 20 73 72  r *table, int sr
2ab30 69 64 2c 20 69 6e 74 20 64 69 6d 73 29 0a 7b 0a  id, int dims).{.
2ab40 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  /* creating the 
2ab50 74 6f 70 6f 5f 65 64 67 65 73 20 74 61 62 6c 65  topo_edges table
2ab60 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c   */.    char sql
2ab70 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61 72  [2048];.    char
2ab80 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20 20   sql2[2048];.   
2ab90 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31   char sqltable[1
2aba0 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72 65  024];.    int re
2abb0 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72  t;.    char *err
2abc0 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  _msg = NULL;.   
2abd0 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c   strcpy (sqltabl
2abe0 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 64  e, table);.    d
2abf0 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c  ouble_quoted_sql
2ac00 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20   (sqltable);.   
2ac10 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22   sprintf (sql, "
2ac20 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 73 20  CREATE TABLE %s 
2ac30 28 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b  (\n", sqltable);
2ac40 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2ac50 2c 20 22 65 64 67 65 5f 69 64 20 49 4e 54 45 47  , "edge_id INTEG
2ac60 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 41  ER PRIMARY KEY A
2ac70 55 54 4f 49 4e 43 52 45 4d 45 4e 54 2c 5c 6e 22  UTOINCREMENT,\n"
2ac80 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
2ac90 71 6c 2c 20 22 6e 6f 64 65 5f 66 72 6f 6d 5f 63  ql, "node_from_c
2aca0 6f 64 65 20 54 45 58 54 2c 5c 6e 22 29 3b 0a 20  ode TEXT,\n");. 
2acb0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2acc0 22 6e 6f 64 65 5f 74 6f 5f 63 6f 64 65 20 54 45  "node_to_code TE
2acd0 58 54 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  XT,\n");.    str
2ace0 63 61 74 20 28 73 71 6c 2c 20 22 65 64 67 65 5f  cat (sql, "edge_
2acf0 63 6f 64 65 20 54 45 58 54 29 22 29 3b 0a 20 20  code TEXT)");.  
2ad00 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
2ad10 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
2ad20 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
2ad30 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66  err_msg);.    if
2ad40 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
2ad50 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OK).      {..  s
2ad60 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 43 52  patialite_e ("CR
2ad70 45 41 54 45 20 54 41 42 4c 45 20 27 25 73 27 20  EATE TABLE '%s' 
2ad80 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 74 61  error: %s\n", ta
2ad90 62 6c 65 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09  ble, err_msg);..
2ada0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28    sqlite3_free (
2adb0 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74  err_msg);..  ret
2adc0 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
2add0 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61     strcpy (sqlta
2ade0 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20  ble, table);.   
2adf0 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e   clean_sql_strin
2ae00 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  g (sqltable);.  
2ae10 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 0a    sprintf (sql,.
2ae20 09 20 20 20 20 20 22 53 45 4c 45 43 54 20 41 64  .     "SELECT Ad
2ae30 64 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 28  dGeometryColumn(
2ae40 27 25 73 27 2c 20 27 47 65 6f 6d 65 74 72 79 27  '%s', 'Geometry'
2ae50 2c 20 25 64 2c 20 27 4c 49 4e 45 53 54 52 49 4e  , %d, 'LINESTRIN
2ae60 47 27 2c 20 27 25 73 27 2c 20 31 29 22 2c 0a 09  G', '%s', 1)",..
2ae70 20 20 20 20 20 73 71 6c 74 61 62 6c 65 2c 20 73       sqltable, s
2ae80 72 69 64 2c 20 28 64 69 6d 73 20 3d 3d 20 47 41  rid, (dims == GA
2ae90 49 41 5f 58 59 5f 5a 29 20 3f 20 22 58 59 5a 22  IA_XY_Z) ? "XYZ"
2aea0 20 3a 20 22 58 59 22 29 3b 0a 20 20 20 20 72 65   : "XY");.    re
2aeb0 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
2aec0 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
2aed0 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f  ULL, NULL, &err_
2aee0 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  msg);.    if (re
2aef0 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
2af00 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
2af10 61 6c 69 74 65 5f 65 20 28 22 41 64 64 47 65 6f  alite_e ("AddGeo
2af20 6d 65 74 72 79 43 6f 6c 75 6d 6e 20 27 25 73 27  metryColumn '%s'
2af30 2e 27 47 65 6f 6d 65 74 72 79 27 20 65 72 72 6f  .'Geometry' erro
2af40 72 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 74 61 62  r: %s\n",....tab
2af50 6c 65 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09 20  le, err_msg);.. 
2af60 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65   sqlite3_free (e
2af70 72 72 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74 75  rr_msg);..  retu
2af80 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
2af90 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
2afa0 22 53 45 4c 45 43 54 20 43 72 65 61 74 65 53 70  "SELECT CreateSp
2afb0 61 74 69 61 6c 49 6e 64 65 78 28 27 25 73 27 2c  atialIndex('%s',
2afc0 20 27 47 65 6f 6d 65 74 72 79 27 29 22 2c 20 73   'Geometry')", s
2afd0 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 72 65  qltable);.    re
2afe0 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
2aff0 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
2b000 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f  ULL, NULL, &err_
2b010 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  msg);.    if (re
2b020 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
2b030 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
2b040 61 6c 69 74 65 5f 65 20 28 22 43 72 65 61 74 65  alite_e ("Create
2b050 53 70 61 74 69 61 6c 49 6e 64 65 78 20 27 25 73  SpatialIndex '%s
2b060 27 2e 27 47 65 6f 6d 65 74 72 79 27 20 65 72 72  '.'Geometry' err
2b070 6f 72 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 74 61  or: %s\n",....ta
2b080 62 6c 65 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09  ble, err_msg);..
2b090 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28    sqlite3_free (
2b0a0 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74  err_msg);..  ret
2b0b0 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
2b0c0 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61     strcpy (sqlta
2b0d0 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20  ble, table);.   
2b0e0 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
2b0f0 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql (sqltable);. 
2b100 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32     sprintf (sql2
2b110 2c 20 22 69 64 78 5f 25 73 5f 63 6f 64 65 22 2c  , "idx_%s_code",
2b120 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20   sqltable);.    
2b130 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
2b140 6c 20 28 73 71 6c 32 29 3b 0a 20 20 20 20 73 70  l (sql2);.    sp
2b150 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 43 52 45  rintf (sql, "CRE
2b160 41 54 45 20 49 4e 44 45 58 20 25 73 20 4f 4e 20  ATE INDEX %s ON 
2b170 25 73 20 28 65 64 67 65 5f 63 6f 64 65 29 22 2c  %s (edge_code)",
2b180 20 73 71 6c 32 2c 20 73 71 6c 74 61 62 6c 65 29   sql2, sqltable)
2b190 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
2b1a0 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
2b1b0 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c  , sql, NULL, NUL
2b1c0 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20  L, &err_msg);.  
2b1d0 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
2b1e0 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a  ITE_OK).      {.
2b1f0 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20  .  spatialite_e 
2b200 28 22 43 72 65 61 74 65 20 49 6e 64 65 78 20 27  ("Create Index '
2b210 25 73 27 28 27 65 64 67 65 5f 63 6f 64 65 27 29  %s'('edge_code')
2b220 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 0a 09   error: %s\n",..
2b230 09 09 73 71 6c 74 61 62 6c 65 2c 20 65 72 72 5f  ..sqltable, err_
2b240 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  msg);..  sqlite3
2b250 5f 66 72 65 65 20 28 65 72 72 5f 6d 73 67 29 3b  _free (err_msg);
2b260 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ..  return 0;.  
2b270 20 20 20 20 7d 0a 20 20 20 20 73 74 72 63 70 79      }.    strcpy
2b280 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c   (sqltable, tabl
2b290 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  e);.    double_q
2b2a0 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61  uoted_sql (sqlta
2b2b0 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
2b2c0 66 20 28 73 71 6c 32 2c 20 22 69 64 78 5f 25 73  f (sql2, "idx_%s
2b2d0 5f 66 72 6f 6d 22 2c 20 73 71 6c 74 61 62 6c 65  _from", sqltable
2b2e0 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75  );.    double_qu
2b2f0 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 32 29 3b  oted_sql (sql2);
2b300 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
2b310 6c 2c 20 22 43 52 45 41 54 45 20 49 4e 44 45 58  l, "CREATE INDEX
2b320 20 25 73 20 4f 4e 20 25 73 20 28 6e 6f 64 65 5f   %s ON %s (node_
2b330 66 72 6f 6d 5f 63 6f 64 65 29 22 2c 20 73 71 6c  from_code)", sql
2b340 32 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  2, sqltable);.  
2b350 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
2b360 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
2b370 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
2b380 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66  err_msg);.    if
2b390 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
2b3a0 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OK).      {..  s
2b3b0 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 43 72  patialite_e ("Cr
2b3c0 65 61 74 65 20 49 6e 64 65 78 20 27 25 73 27 28  eate Index '%s'(
2b3d0 27 6e 6f 64 65 5f 66 72 6f 6d 5f 63 6f 64 65 27  'node_from_code'
2b3e0 29 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 0a  ) error: %s\n",.
2b3f0 09 09 09 73 71 6c 74 61 62 6c 65 2c 20 65 72 72  ...sqltable, err
2b400 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65  _msg);..  sqlite
2b410 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73 67 29  3_free (err_msg)
2b420 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  ;..  return 0;. 
2b430 20 20 20 20 20 7d 0a 20 20 20 20 73 74 72 63 70       }.    strcp
2b440 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61 62  y (sqltable, tab
2b450 6c 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  le);.    double_
2b460 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74  quoted_sql (sqlt
2b470 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e  able);.    sprin
2b480 74 66 20 28 73 71 6c 32 2c 20 22 69 64 78 5f 25  tf (sql2, "idx_%
2b490 73 5f 74 6f 22 2c 20 73 71 6c 74 61 62 6c 65 29  s_to", sqltable)
2b4a0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f  ;.    double_quo
2b4b0 74 65 64 5f 73 71 6c 20 28 73 71 6c 32 29 3b 0a  ted_sql (sql2);.
2b4c0 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
2b4d0 2c 20 22 43 52 45 41 54 45 20 49 4e 44 45 58 20  , "CREATE INDEX 
2b4e0 25 73 20 4f 4e 20 25 73 20 28 6e 6f 64 65 5f 74  %s ON %s (node_t
2b4f0 6f 5f 63 6f 64 65 29 22 2c 20 73 71 6c 32 2c 20  o_code)", sql2, 
2b500 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 72  sqltable);.    r
2b510 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
2b520 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  c (sqlite, sql, 
2b530 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
2b540 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72  _msg);.    if (r
2b550 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29  et != SQLITE_OK)
2b560 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
2b570 69 61 6c 69 74 65 5f 65 20 28 22 43 72 65 61 74  ialite_e ("Creat
2b580 65 20 49 6e 64 65 78 20 27 25 73 27 28 27 6e 6f  e Index '%s'('no
2b590 64 65 5f 74 6f 5f 63 6f 64 65 27 29 20 65 72 72  de_to_code') err
2b5a0 6f 72 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 73 71  or: %s\n",....sq
2b5b0 6c 74 61 62 6c 65 2c 20 65 72 72 5f 6d 73 67 29  ltable, err_msg)
2b5c0 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
2b5d0 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20  e (err_msg);..  
2b5e0 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2b5f0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  }.    return 1;.
2b600 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 63 72  }..static int.cr
2b610 65 61 74 65 5f 74 6f 70 6f 5f 66 61 63 65 73 20  eate_topo_faces 
2b620 28 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69 74  (sqlite3 * sqlit
2b630 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74  e, const char *t
2b640 61 62 6c 65 29 0a 7b 0a 2f 2a 20 63 72 65 61 74  able).{./* creat
2b650 69 6e 67 20 74 68 65 20 74 6f 70 6f 5f 66 61 63  ing the topo_fac
2b660 65 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  es table */.    
2b670 63 68 61 72 20 73 71 6c 5b 32 30 34 38 5d 3b 0a  char sql[2048];.
2b680 20 20 20 20 63 68 61 72 20 73 71 6c 32 5b 32 30      char sql2[20
2b690 34 38 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71  48];.    char sq
2b6a0 6c 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20  ltable[1024];.  
2b6b0 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 63    int ret;.    c
2b6c0 68 61 72 20 2a 65 72 72 5f 6d 73 67 20 3d 20 4e  har *err_msg = N
2b6d0 55 4c 4c 3b 0a 20 20 20 20 73 74 72 63 70 79 20  ULL;.    strcpy 
2b6e0 28 73 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c 65  (sqltable, table
2b6f0 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75  );.    double_qu
2b700 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62  oted_sql (sqltab
2b710 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66  le);.    sprintf
2b720 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54   (sql, "CREATE T
2b730 41 42 4c 45 20 25 73 20 28 5c 6e 22 2c 20 73 71  ABLE %s (\n", sq
2b740 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
2b750 63 61 74 20 28 73 71 6c 2c 20 22 66 61 63 65 5f  cat (sql, "face_
2b760 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
2b770 52 59 20 4b 45 59 20 41 55 54 4f 49 4e 43 52 45  RY KEY AUTOINCRE
2b780 4d 45 4e 54 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  MENT,\n");.    s
2b790 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66 61 63  trcat (sql, "fac
2b7a0 65 5f 63 6f 64 65 20 54 45 58 54 29 22 29 3b 0a  e_code TEXT)");.
2b7b0 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65      ret = sqlite
2b7c0 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20  3_exec (sqlite, 
2b7d0 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c  sql, NULL, NULL,
2b7e0 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20 20   &err_msg);.    
2b7f0 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54  if (ret != SQLIT
2b800 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20  E_OK).      {.. 
2b810 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22   spatialite_e ("
2b820 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 25 73  CREATE TABLE '%s
2b830 27 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20  ' error: %s\n", 
2b840 74 61 62 6c 65 2c 20 65 72 72 5f 6d 73 67 29 3b  table, err_msg);
2b850 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
2b860 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 72   (err_msg);..  r
2b870 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
2b880 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
2b890 74 61 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20  table, table);. 
2b8a0 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2b8b0 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b  _sql (sqltable);
2b8c0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
2b8d0 6c 32 2c 20 22 69 64 78 5f 25 73 5f 63 6f 64 65  l2, "idx_%s_code
2b8e0 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  ", sqltable);.  
2b8f0 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f    double_quoted_
2b900 73 71 6c 20 28 73 71 6c 32 29 3b 0a 20 20 20 20  sql (sql2);.    
2b910 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 43  sprintf (sql, "C
2b920 52 45 41 54 45 20 49 4e 44 45 58 20 25 73 20 4f  REATE INDEX %s O
2b930 4e 20 25 73 20 28 66 61 63 65 5f 63 6f 64 65 29  N %s (face_code)
2b940 22 2c 20 73 71 6c 32 2c 20 73 71 6c 74 61 62 6c  ", sql2, sqltabl
2b950 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71  e);.    ret = sq
2b960 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69  lite3_exec (sqli
2b970 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e  te, sql, NULL, N
2b980 55 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a  ULL, &err_msg);.
2b990 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53      if (ret != S
2b9a0 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20  QLITE_OK).      
2b9b0 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f  {..  spatialite_
2b9c0 65 20 28 22 43 72 65 61 74 65 20 49 6e 64 65 78  e ("Create Index
2b9d0 20 27 25 73 27 28 27 66 61 63 65 5f 63 6f 64 65   '%s'('face_code
2b9e0 27 29 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c  ') error: %s\n",
2b9f0 0a 09 09 09 73 71 6c 74 61 62 6c 65 2c 20 65 72  ....sqltable, er
2ba00 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74  r_msg);..  sqlit
2ba10 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73 67  e3_free (err_msg
2ba20 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a  );..  return 0;.
2ba30 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75        }.    retu
2ba40 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  rn 1;.}..static 
2ba50 69 6e 74 0a 63 72 65 61 74 65 5f 74 6f 70 6f 5f  int.create_topo_
2ba60 66 61 63 65 73 5f 65 64 67 65 73 20 28 73 71 6c  faces_edges (sql
2ba70 69 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63  ite3 * sqlite, c
2ba80 6f 6e 73 74 20 63 68 61 72 20 2a 74 61 62 6c 65  onst char *table
2ba90 2c 0a 09 09 09 20 63 6f 6e 73 74 20 63 68 61 72  ,.... const char
2baa0 20 2a 74 61 62 6c 65 32 29 0a 7b 0a 2f 2a 20 63   *table2).{./* c
2bab0 72 65 61 74 69 6e 67 20 74 68 65 20 74 6f 70 6f  reating the topo
2bac0 5f 66 61 63 65 73 5f 65 64 67 65 73 20 74 61 62  _faces_edges tab
2bad0 6c 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73  le */.    char s
2bae0 71 6c 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68  ql[2048];.    ch
2baf0 61 72 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20  ar sql2[2048];. 
2bb00 20 20 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65     char sqltable
2bb10 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20  [1024];.    int 
2bb20 72 65 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65  ret;.    char *e
2bb30 72 72 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20  rr_msg = NULL;. 
2bb40 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61     strcpy (sqlta
2bb50 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20  ble, table);.   
2bb60 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
2bb70 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql (sqltable);. 
2bb80 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c     sprintf (sql,
2bb90 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
2bba0 73 20 28 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65  s (\n", sqltable
2bbb0 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
2bbc0 71 6c 2c 20 22 66 61 63 65 5f 69 64 20 49 4e 54  ql, "face_id INT
2bbd0 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  EGER NOT NULL,\n
2bbe0 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
2bbf0 73 71 6c 2c 20 22 65 64 67 65 5f 63 6f 64 65 20  sql, "edge_code 
2bc00 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  TEXT NOT NULL,\n
2bc10 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
2bc20 73 71 6c 2c 20 22 6f 72 69 65 6e 74 61 74 69 6f  sql, "orientatio
2bc30 6e 20 54 45 58 54 2c 5c 6e 22 29 3b 0a 20 20 20  n TEXT,\n");.   
2bc40 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43   strcat (sql, "C
2bc50 4f 4e 53 54 52 41 49 4e 54 20 70 6b 5f 66 61 63  ONSTRAINT pk_fac
2bc60 65 73 5f 65 64 67 65 73 20 50 52 49 4d 41 52 59  es_edges PRIMARY
2bc70 20 4b 45 59 20 22 29 3b 0a 20 20 20 20 73 74 72   KEY ");.    str
2bc80 63 61 74 20 28 73 71 6c 2c 20 22 28 66 61 63 65  cat (sql, "(face
2bc90 5f 69 64 2c 20 65 64 67 65 5f 63 6f 64 65 29 2c  _id, edge_code),
2bca0 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
2bcb0 20 28 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49   (sql, "CONSTRAI
2bcc0 4e 54 20 66 6b 5f 66 61 63 65 73 5f 65 64 67 65  NT fk_faces_edge
2bcd0 73 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 22 29  s FOREIGN KEY ")
2bce0 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
2bcf0 6c 2c 20 22 28 66 61 63 65 5f 69 64 29 20 52 45  l, "(face_id) RE
2bd00 46 45 52 45 4e 43 45 53 20 22 29 3b 0a 20 20 20  FERENCES ");.   
2bd10 20 73 74 72 63 70 79 20 28 73 71 6c 32 2c 20 74   strcpy (sql2, t
2bd20 61 62 6c 65 32 29 3b 0a 20 20 20 20 64 6f 75 62  able2);.    doub
2bd30 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73  le_quoted_sql (s
2bd40 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ql2);.    strcat
2bd50 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20   (sql, sql2);.  
2bd60 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2bd70 20 28 66 61 63 65 5f 69 64 29 29 5c 6e 22 29 3b   (face_id))\n");
2bd80 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74  .    ret = sqlit
2bd90 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c  e3_exec (sqlite,
2bda0 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c   sql, NULL, NULL
2bdb0 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20  , &err_msg);.   
2bdc0 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
2bdd0 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09  TE_OK).      {..
2bde0 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28    spatialite_e (
2bdf0 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 25  "CREATE TABLE '%
2be00 73 27 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c  s' error: %s\n",
2be10 20 74 61 62 6c 65 2c 20 65 72 72 5f 6d 73 67 29   table, err_msg)
2be20 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
2be30 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20  e (err_msg);..  
2be40 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2be50 7d 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  }.    strcpy (sq
2be60 6c 74 61 62 6c 65 2c 20 74 61 62 6c 65 29 3b 0a  ltable, table);.
2be70 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65      double_quote
2be80 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29  d_sql (sqltable)
2be90 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73  ;.    sprintf (s
2bea0 71 6c 32 2c 20 22 69 64 78 5f 25 73 5f 65 64 67  ql2, "idx_%s_edg
2beb0 65 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  e", sqltable);. 
2bec0 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2bed0 5f 73 71 6c 20 28 73 71 6c 32 29 3b 0a 20 20 20  _sql (sql2);.   
2bee0 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22   sprintf (sql, "
2bef0 43 52 45 41 54 45 20 49 4e 44 45 58 20 25 73 20  CREATE INDEX %s 
2bf00 4f 4e 20 25 73 20 28 65 64 67 65 5f 63 6f 64 65  ON %s (edge_code
2bf10 29 22 2c 20 73 71 6c 32 2c 20 73 71 6c 74 61 62  )", sql2, sqltab
2bf20 6c 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73  le);.    ret = s
2bf30 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
2bf40 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
2bf50 4e 55 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b  NULL, &err_msg);
2bf60 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
2bf70 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
2bf80 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
2bf90 5f 65 20 28 22 43 72 65 61 74 65 20 49 6e 64 65  _e ("Create Inde
2bfa0 78 20 27 25 73 27 28 27 65 64 67 65 5f 63 6f 64  x '%s'('edge_cod
2bfb0 65 27 29 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22  e') error: %s\n"
2bfc0 2c 0a 09 09 09 73 71 6c 74 61 62 6c 65 2c 20 65  ,....sqltable, e
2bfd0 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69  rr_msg);..  sqli
2bfe0 74 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73  te3_free (err_ms
2bff0 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b  g);..  return 0;
2c000 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74  .      }.    ret
2c010 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63  urn 1;.}..static
2c020 20 69 6e 74 0a 63 72 65 61 74 65 5f 74 6f 70 6f   int.create_topo
2c030 5f 63 75 72 76 65 73 20 28 73 71 6c 69 74 65 33  _curves (sqlite3
2c040 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73 74   * sqlite, const
2c050 20 63 68 61 72 20 2a 74 61 62 6c 65 29 0a 7b 0a   char *table).{.
2c060 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  /* creating the 
2c070 74 6f 70 6f 5f 63 75 72 76 65 73 20 74 61 62 6c  topo_curves tabl
2c080 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71  e */.    char sq
2c090 6c 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61  l[2048];.    cha
2c0a0 72 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20  r sql2[2048];.  
2c0b0 20 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b    char sqltable[
2c0c0 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72  1024];.    int r
2c0d0 65 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72  et;.    char *er
2c0e0 72 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r_msg = NULL;.  
2c0f0 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62    strcpy (sqltab
2c100 6c 65 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20 20  le, table);.    
2c110 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
2c120 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  l (sqltable);.  
2c130 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
2c140 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 73  "CREATE TABLE %s
2c150 20 28 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29   (\n", sqltable)
2c160 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
2c170 6c 2c 20 22 63 75 72 76 65 5f 69 64 20 49 4e 54  l, "curve_id INT
2c180 45 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  EGER NOT NULL,\n
2c190 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
2c1a0 73 71 6c 2c 20 22 65 64 67 65 5f 63 6f 64 65 20  sql, "edge_code 
2c1b0 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  TEXT NOT NULL,\n
2c1c0 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
2c1d0 73 71 6c 2c 20 22 6f 72 69 65 6e 74 61 74 69 6f  sql, "orientatio
2c1e0 6e 20 54 45 58 54 2c 5c 6e 22 29 3b 0a 20 20 20  n TEXT,\n");.   
2c1f0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43   strcat (sql, "C
2c200 4f 4e 53 54 52 41 49 4e 54 20 70 6b 5f 63 75 72  ONSTRAINT pk_cur
2c210 76 65 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ves PRIMARY KEY 
2c220 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
2c230 73 71 6c 2c 20 22 28 63 75 72 76 65 5f 69 64 2c  sql, "(curve_id,
2c240 20 65 64 67 65 5f 63 6f 64 65 29 29 5c 6e 22 29   edge_code))\n")
2c250 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
2c260 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
2c270 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c  , sql, NULL, NUL
2c280 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20  L, &err_msg);.  
2c290 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
2c2a0 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a  ITE_OK).      {.
2c2b0 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20  .  spatialite_e 
2c2c0 28 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 27  ("CREATE TABLE '
2c2d0 25 73 27 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22  %s' error: %s\n"
2c2e0 2c 20 74 61 62 6c 65 2c 20 65 72 72 5f 6d 73 67  , table, err_msg
2c2f0 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
2c300 65 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09 20  ee (err_msg);.. 
2c310 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
2c320 20 7d 0a 20 20 20 20 73 74 72 63 70 79 20 28 73   }.    strcpy (s
2c330 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c 65 29 3b  qltable, table);
2c340 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74  .    double_quot
2c350 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65  ed_sql (sqltable
2c360 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  );.    sprintf (
2c370 73 71 6c 32 2c 20 22 69 64 78 5f 25 73 5f 65 64  sql2, "idx_%s_ed
2c380 67 65 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a  ge", sqltable);.
2c390 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65      double_quote
2c3a0 64 5f 73 71 6c 20 28 73 71 6c 32 29 3b 0a 20 20  d_sql (sql2);.  
2c3b0 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
2c3c0 22 43 52 45 41 54 45 20 49 4e 44 45 58 20 25 73  "CREATE INDEX %s
2c3d0 20 4f 4e 20 25 73 20 28 65 64 67 65 5f 63 6f 64   ON %s (edge_cod
2c3e0 65 29 22 2c 20 73 71 6c 32 2c 20 73 71 6c 74 61  e)", sql2, sqlta
2c3f0 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  ble);.    ret = 
2c400 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71  sqlite3_exec (sq
2c410 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c  lite, sql, NULL,
2c420 20 4e 55 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29   NULL, &err_msg)
2c430 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d  ;.    if (ret !=
2c440 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20   SQLITE_OK).    
2c450 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
2c460 65 5f 65 20 28 22 43 72 65 61 74 65 20 49 6e 64  e_e ("Create Ind
2c470 65 78 20 27 25 73 27 28 27 65 64 67 65 5f 63 6f  ex '%s'('edge_co
2c480 64 65 27 29 20 65 72 72 6f 72 3a 20 25 73 5c 6e  de') error: %s\n
2c490 22 2c 0a 09 09 09 73 71 6c 74 61 62 6c 65 2c 20  ",....sqltable, 
2c4a0 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c  err_msg);..  sql
2c4b0 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d  ite3_free (err_m
2c4c0 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30  sg);..  return 0
2c4d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65  ;.      }.    re
2c4e0 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69  turn 1;.}..stati
2c4f0 63 20 69 6e 74 0a 63 72 65 61 74 65 5f 74 6f 70  c int.create_top
2c500 6f 5f 73 75 72 66 61 63 65 73 20 28 73 71 6c 69  o_surfaces (sqli
2c510 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f  te3 * sqlite, co
2c520 6e 73 74 20 63 68 61 72 20 2a 74 61 62 6c 65 29  nst char *table)
2c530 0a 7b 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74  .{./* creating t
2c540 68 65 20 74 6f 70 6f 5f 73 75 72 66 61 63 65 73  he topo_surfaces
2c550 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68   table */.    ch
2c560 61 72 20 73 71 6c 5b 32 30 34 38 5d 3b 0a 20 20  ar sql[2048];.  
2c570 20 20 63 68 61 72 20 73 71 6c 32 5b 32 30 34 38    char sql2[2048
2c580 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 74  ];.    char sqlt
2c590 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20  able[1024];.    
2c5a0 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61  int ret;.    cha
2c5b0 72 20 2a 65 72 72 5f 6d 73 67 20 3d 20 4e 55 4c  r *err_msg = NUL
2c5c0 4c 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73  L;.    strcpy (s
2c5d0 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c 65 29 3b  qltable, table);
2c5e0 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74  .    double_quot
2c5f0 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65  ed_sql (sqltable
2c600 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  );.    sprintf (
2c610 73 71 6c 2c 20 22 43 52 45 41 54 45 20 54 41 42  sql, "CREATE TAB
2c620 4c 45 20 25 73 20 28 5c 6e 22 2c 20 73 71 6c 74  LE %s (\n", sqlt
2c630 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
2c640 74 20 28 73 71 6c 2c 20 22 73 75 72 66 61 63 65  t (sql, "surface
2c650 5f 69 64 20 49 4e 54 45 47 45 52 20 4e 4f 54 20  _id INTEGER NOT 
2c660 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  NULL,\n");.    s
2c670 74 72 63 61 74 20 28 73 71 6c 2c 20 22 66 61 63  trcat (sql, "fac
2c680 65 5f 63 6f 64 65 20 54 45 58 54 20 4e 4f 54 20  e_code TEXT NOT 
2c690 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  NULL,\n");.    s
2c6a0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6f 72 69  trcat (sql, "ori
2c6b0 65 6e 74 61 74 69 6f 6e 20 54 45 58 54 2c 5c 6e  entation TEXT,\n
2c6c0 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
2c6d0 73 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54  sql, "CONSTRAINT
2c6e0 20 70 6b 5f 73 75 72 66 61 63 65 73 20 50 52 49   pk_surfaces PRI
2c6f0 4d 41 52 59 20 4b 45 59 20 22 29 3b 0a 20 20 20  MARY KEY ");.   
2c700 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 28   strcat (sql, "(
2c710 73 75 72 66 61 63 65 5f 69 64 2c 20 66 61 63 65  surface_id, face
2c720 5f 63 6f 64 65 29 29 22 29 3b 0a 20 20 20 20 72  _code))");.    r
2c730 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
2c740 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  c (sqlite, sql, 
2c750 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
2c760 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72  _msg);.    if (r
2c770 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29  et != SQLITE_OK)
2c780 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
2c790 69 61 6c 69 74 65 5f 65 20 28 22 43 52 45 41 54  ialite_e ("CREAT
2c7a0 45 20 54 41 42 4c 45 20 27 25 73 27 20 65 72 72  E TABLE '%s' err
2c7b0 6f 72 3a 20 25 73 5c 6e 22 2c 20 74 61 62 6c 65  or: %s\n", table
2c7c0 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73  , err_msg);..  s
2c7d0 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72  qlite3_free (err
2c7e0 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e  _msg);..  return
2c7f0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2c800 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65  strcpy (sqltable
2c810 2c 20 74 61 62 6c 65 29 3b 0a 20 20 20 20 64 6f  , table);.    do
2c820 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
2c830 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20  (sqltable);.    
2c840 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22  sprintf (sql2, "
2c850 69 64 78 5f 25 73 5f 66 61 63 65 22 2c 20 73 71  idx_%s_face", sq
2c860 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 64 6f 75  ltable);.    dou
2c870 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
2c880 73 71 6c 32 29 3b 0a 20 20 20 20 73 70 72 69 6e  sql2);.    sprin
2c890 74 66 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45  tf (sql, "CREATE
2c8a0 20 49 4e 44 45 58 20 25 73 20 4f 4e 20 25 73 20   INDEX %s ON %s 
2c8b0 28 66 61 63 65 5f 63 6f 64 65 29 22 2c 20 73 71  (face_code)", sq
2c8c0 6c 32 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  l2, sqltable);. 
2c8d0 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33     ret = sqlite3
2c8e0 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73  _exec (sqlite, s
2c8f0 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20  ql, NULL, NULL, 
2c900 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20 20 69  &err_msg);.    i
2c910 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45  f (ret != SQLITE
2c920 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  _OK).      {..  
2c930 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 43  spatialite_e ("C
2c940 72 65 61 74 65 20 49 6e 64 65 78 20 27 25 73 27  reate Index '%s'
2c950 28 27 66 61 63 65 5f 63 6f 64 65 27 29 20 65 72  ('face_code') er
2c960 72 6f 72 3a 20 25 73 5c 6e 22 2c 0a 09 09 09 73  ror: %s\n",....s
2c970 71 6c 74 61 62 6c 65 2c 20 65 72 72 5f 6d 73 67  qltable, err_msg
2c980 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
2c990 65 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09 20  ee (err_msg);.. 
2c9a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
2c9b0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b   }.    return 1;
2c9c0 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 63  .}..static int.c
2c9d0 72 65 61 74 65 5f 63 68 65 63 6b 5f 6e 6f 64 65  reate_check_node
2c9e0 5f 63 6f 64 65 73 20 28 73 71 6c 69 74 65 33 20  _codes (sqlite3 
2c9f0 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73 74 20  * sqlite, const 
2ca00 63 68 61 72 20 2a 76 69 65 77 2c 0a 09 09 09 20  char *view,.... 
2ca10 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 61 62 6c  const char *tabl
2ca20 65 5f 6e 6f 64 65 73 29 0a 7b 0a 2f 2a 20 63 72  e_nodes).{./* cr
2ca30 65 61 74 69 6e 67 20 74 68 65 20 63 68 65 63 6b  eating the check
2ca40 20 6e 6f 64 65 20 63 6f 64 65 73 20 56 49 45 57   node codes VIEW
2ca50 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c   */.    char sql
2ca60 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61 72  [2048];.    char
2ca70 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20 20   sql2[2048];.   
2ca80 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31   char sqltable[1
2ca90 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72 65  024];.    int re
2caa0 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72  t;.    char *err
2cab0 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  _msg = NULL;.   
2cac0 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c   strcpy (sqltabl
2cad0 65 2c 20 76 69 65 77 29 3b 0a 20 20 20 20 64 6f  e, view);.    do
2cae0 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
2caf0 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20  (sqltable);.    
2cb00 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 43  sprintf (sql, "C
2cb10 52 45 41 54 45 20 56 49 45 57 20 25 73 20 41 53  REATE VIEW %s AS
2cb20 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a  \n", sqltable);.
2cb30 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
2cb40 20 22 53 45 4c 45 43 54 20 6e 6f 64 65 5f 63 6f   "SELECT node_co
2cb50 64 65 20 41 53 20 6e 6f 64 65 5f 63 6f 64 65 2c  de AS node_code,
2cb60 20 43 6f 75 6e 74 28 6e 6f 64 65 5f 69 64 29 20   Count(node_id) 
2cb70 41 53 20 63 6f 75 6e 74 5c 6e 22 29 3b 0a 20 20  AS count\n");.  
2cb80 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62    strcpy (sqltab
2cb90 6c 65 2c 20 74 61 62 6c 65 5f 6e 6f 64 65 73 29  le, table_nodes)
2cba0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f  ;.    double_quo
2cbb0 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c  ted_sql (sqltabl
2cbc0 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  e);.    sprintf 
2cbd0 28 73 71 6c 32 2c 20 22 46 52 4f 4d 20 25 73 5c  (sql2, "FROM %s\
2cbe0 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  n", sqltable);. 
2cbf0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2cc00 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61  sql2);.    strca
2cc10 74 20 28 73 71 6c 2c 20 22 47 52 4f 55 50 20 42  t (sql, "GROUP B
2cc20 59 20 6e 6f 64 65 5f 63 6f 64 65 5c 6e 22 29 3b  Y node_code\n");
2cc30 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2cc40 2c 20 22 48 41 56 49 4e 47 20 63 6f 75 6e 74 20  , "HAVING count 
2cc50 3e 20 31 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74  > 1\n");.    ret
2cc60 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
2cc70 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
2cc80 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f 6d  LL, NULL, &err_m
2cc90 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74  sg);.    if (ret
2cca0 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20   != SQLITE_OK). 
2ccb0 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61       {..  spatia
2ccc0 6c 69 74 65 5f 65 20 28 22 43 52 45 41 54 45 20  lite_e ("CREATE 
2ccd0 56 49 45 57 20 27 25 73 27 20 65 72 72 6f 72 3a  VIEW '%s' error:
2cce0 20 25 73 5c 6e 22 2c 20 76 69 65 77 2c 20 65 72   %s\n", view, er
2ccf0 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74  r_msg);..  sqlit
2cd00 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73 67  e3_free (err_msg
2cd10 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a  );..  return 0;.
2cd20 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75        }.    retu
2cd30 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  rn 1;.}..static 
2cd40 69 6e 74 0a 63 72 65 61 74 65 5f 63 68 65 63 6b  int.create_check
2cd50 5f 6e 6f 64 65 5f 67 65 6f 6d 73 20 28 73 71 6c  _node_geoms (sql
2cd60 69 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63  ite3 * sqlite, c
2cd70 6f 6e 73 74 20 63 68 61 72 20 2a 76 69 65 77 2c  onst char *view,
2cd80 0a 09 09 09 20 63 6f 6e 73 74 20 63 68 61 72 20  .... const char 
2cd90 2a 74 61 62 6c 65 5f 6e 6f 64 65 73 29 0a 7b 0a  *table_nodes).{.
2cda0 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  /* creating the 
2cdb0 63 68 65 63 6b 20 6e 6f 64 65 20 67 65 6f 6d 73  check node geoms
2cdc0 20 56 49 45 57 20 2a 2f 0a 20 20 20 20 63 68 61   VIEW */.    cha
2cdd0 72 20 73 71 6c 5b 32 30 34 38 5d 3b 0a 20 20 20  r sql[2048];.   
2cde0 20 63 68 61 72 20 73 71 6c 32 5b 32 30 34 38 5d   char sql2[2048]
2cdf0 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 74 61  ;.    char sqlta
2ce00 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69  ble[1024];.    i
2ce10 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61 72  nt ret;.    char
2ce20 20 2a 65 72 72 5f 6d 73 67 20 3d 20 4e 55 4c 4c   *err_msg = NULL
2ce30 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
2ce40 6c 74 61 62 6c 65 2c 20 76 69 65 77 29 3b 0a 20  ltable, view);. 
2ce50 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2ce60 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b  _sql (sqltable);
2ce70 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
2ce80 6c 2c 20 22 43 52 45 41 54 45 20 56 49 45 57 20  l, "CREATE VIEW 
2ce90 25 73 20 41 53 5c 6e 22 2c 20 73 71 6c 74 61 62  %s AS\n", sqltab
2cea0 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
2ceb0 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 6e 31  (sql, "SELECT n1
2cec0 2e 6e 6f 64 65 5f 69 64 20 41 53 20 6e 6f 64 65  .node_id AS node
2ced0 31 5f 69 64 2c 20 6e 31 2e 6e 6f 64 65 5f 63 6f  1_id, n1.node_co
2cee0 64 65 20 41 53 20 6e 6f 64 65 31 5f 63 6f 64 65  de AS node1_code
2cef0 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  , ");.    strcat
2cf00 20 28 73 71 6c 2c 20 22 6e 32 2e 6e 6f 64 65 5f   (sql, "n2.node_
2cf10 69 64 20 41 53 20 6e 6f 64 65 32 5f 69 64 2c 20  id AS node2_id, 
2cf20 6e 32 2e 6e 6f 64 65 5f 63 6f 64 65 20 41 53 20  n2.node_code AS 
2cf30 6e 6f 64 65 32 5f 63 6f 64 65 5c 6e 22 29 3b 0a  node2_code\n");.
2cf40 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74      strcpy (sqlt
2cf50 61 62 6c 65 2c 20 74 61 62 6c 65 5f 6e 6f 64 65  able, table_node
2cf60 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  s);.    double_q
2cf70 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61  uoted_sql (sqlta
2cf80 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
2cf90 66 20 28 73 71 6c 32 2c 20 22 46 52 4f 4d 20 25  f (sql2, "FROM %
2cfa0 73 20 41 53 20 6e 31 5c 6e 22 2c 20 73 71 6c 74  s AS n1\n", sqlt
2cfb0 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
2cfc0 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20  t (sql, sql2);. 
2cfd0 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32     sprintf (sql2
2cfe0 2c 20 22 4a 4f 49 4e 20 25 73 20 41 53 20 6e 32  , "JOIN %s AS n2
2cff0 20 4f 4e 20 28 5c 6e 22 2c 20 73 71 6c 74 61 62   ON (\n", sqltab
2d000 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
2d010 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20  (sql, sql2);.   
2d020 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 20   strcat (sql, " 
2d030 20 6e 31 2e 6e 6f 64 65 5f 69 64 20 3c 3e 20 6e   n1.node_id <> n
2d040 32 2e 6e 6f 64 65 5f 69 64 20 41 4e 44 5c 6e 22  2.node_id AND\n"
2d050 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
2d060 71 6c 2c 20 22 20 20 53 54 5f 45 71 75 61 6c 73  ql, "  ST_Equals
2d070 28 6e 31 2e 47 65 6f 6d 65 74 72 79 2c 20 6e 32  (n1.Geometry, n2
2d080 2e 47 65 6f 6d 65 74 72 79 29 20 3d 20 31 20 41  .Geometry) = 1 A
2d090 4e 44 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  ND\n");.    strc
2d0a0 61 74 20 28 73 71 6c 2c 20 22 20 20 6e 32 2e 6e  at (sql, "  n2.n
2d0b0 6f 64 65 5f 69 64 20 49 4e 20 28 5c 6e 22 29 3b  ode_id IN (\n");
2d0c0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2d0d0 2c 20 22 09 53 45 4c 45 43 54 20 52 4f 57 49 44  , ".SELECT ROWID
2d0e0 20 46 52 4f 4d 20 53 70 61 74 69 61 6c 49 6e 64   FROM SpatialInd
2d0f0 65 78 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  ex\n");.    strc
2d100 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61  py (sqltable, ta
2d110 62 6c 65 5f 6e 6f 64 65 73 29 3b 0a 20 20 20 20  ble_nodes);.    
2d120 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67  clean_sql_string
2d130 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20   (sqltable);.   
2d140 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20   sprintf (sql2, 
2d150 22 09 57 48 45 52 45 20 66 5f 74 61 62 6c 65 5f  ".WHERE f_table_
2d160 6e 61 6d 65 20 3d 20 27 25 73 27 20 41 4e 44 5c  name = '%s' AND\
2d170 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  n", sqltable);. 
2d180 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2d190 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61  sql2);.    strca
2d1a0 74 20 28 73 71 6c 2c 20 22 09 20 20 73 65 61 72  t (sql, ".  sear
2d1b0 63 68 5f 66 72 61 6d 65 20 3d 20 6e 31 2e 47 65  ch_frame = n1.Ge
2d1c0 6f 6d 65 74 72 79 29 29 5c 6e 22 29 3b 0a 20 20  ometry))\n");.  
2d1d0 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
2d1e0 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
2d1f0 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
2d200 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66  err_msg);.    if
2d210 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
2d220 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OK).      {..  s
2d230 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 43 52  patialite_e ("CR
2d240 45 41 54 45 20 56 49 45 57 20 27 25 73 27 20 65  EATE VIEW '%s' e
2d250 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 76 69 65  rror: %s\n", vie
2d260 77 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20  w, err_msg);..  
2d270 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72  sqlite3_free (er
2d280 72 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74 75 72  r_msg);..  retur
2d290 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2d2a0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74   return 1;.}..st
2d2b0 61 74 69 63 20 69 6e 74 0a 63 72 65 61 74 65 5f  atic int.create_
2d2c0 63 68 65 63 6b 5f 65 64 67 65 5f 63 6f 64 65 73  check_edge_codes
2d2d0 20 28 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69   (sqlite3 * sqli
2d2e0 74 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  te, const char *
2d2f0 76 69 65 77 2c 0a 09 09 09 20 63 6f 6e 73 74 20  view,.... const 
2d300 63 68 61 72 20 2a 74 61 62 6c 65 5f 65 64 67 65  char *table_edge
2d310 73 29 0a 7b 0a 2f 2a 20 63 72 65 61 74 69 6e 67  s).{./* creating
2d320 20 74 68 65 20 63 68 65 63 6b 20 65 64 67 65 20   the check edge 
2d330 63 6f 64 65 73 20 56 49 45 57 20 2a 2f 0a 20 20  codes VIEW */.  
2d340 20 20 63 68 61 72 20 73 71 6c 5b 32 30 34 38 5d    char sql[2048]
2d350 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 32 5b  ;.    char sql2[
2d360 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61 72 20  2048];.    char 
2d370 73 71 6c 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a  sqltable[1024];.
2d380 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
2d390 20 63 68 61 72 20 2a 65 72 72 5f 6d 73 67 20 3d   char *err_msg =
2d3a0 20 4e 55 4c 4c 3b 0a 20 20 20 20 73 74 72 63 70   NULL;.    strcp
2d3b0 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 76 69 65  y (sqltable, vie
2d3c0 77 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  w);.    double_q
2d3d0 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61  uoted_sql (sqlta
2d3e0 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
2d3f0 66 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20  f (sql, "CREATE 
2d400 56 49 45 57 20 25 73 20 41 53 5c 6e 22 2c 20 73  VIEW %s AS\n", s
2d410 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  qltable);.    st
2d420 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45 4c 45  rcat (sql, "SELE
2d430 43 54 20 65 64 67 65 5f 63 6f 64 65 20 41 53 20  CT edge_code AS 
2d440 65 64 67 65 5f 63 6f 64 65 2c 20 43 6f 75 6e 74  edge_code, Count
2d450 28 65 64 67 65 5f 69 64 29 20 41 53 20 63 6f 75  (edge_id) AS cou
2d460 6e 74 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  nt\n");.    strc
2d470 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61  py (sqltable, ta
2d480 62 6c 65 5f 65 64 67 65 73 29 3b 0a 20 20 20 20  ble_edges);.    
2d490 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
2d4a0 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  l (sqltable);.  
2d4b0 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c    sprintf (sql2,
2d4c0 20 22 46 52 4f 4d 20 25 73 5c 6e 22 2c 20 73 71   "FROM %s\n", sq
2d4d0 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
2d4e0 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
2d4f0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2d500 2c 20 22 47 52 4f 55 50 20 42 59 20 65 64 67 65  , "GROUP BY edge
2d510 5f 63 6f 64 65 5c 6e 22 29 3b 0a 20 20 20 20 73  _code\n");.    s
2d520 74 72 63 61 74 20 28 73 71 6c 2c 20 22 48 41 56  trcat (sql, "HAV
2d530 49 4e 47 20 63 6f 75 6e 74 20 3e 20 31 5c 6e 22  ING count > 1\n"
2d540 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c  );.    ret = sql
2d550 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74  ite3_exec (sqlit
2d560 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55  e, sql, NULL, NU
2d570 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20  LL, &err_msg);. 
2d580 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
2d590 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b  LITE_OK).      {
2d5a0 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65  ..  spatialite_e
2d5b0 20 28 22 43 52 45 41 54 45 20 56 49 45 57 20 27   ("CREATE VIEW '
2d5c0 25 73 27 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22  %s' error: %s\n"
2d5d0 2c 20 76 69 65 77 2c 20 65 72 72 5f 6d 73 67 29  , view, err_msg)
2d5e0 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
2d5f0 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20  e (err_msg);..  
2d600 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2d610 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  }.    return 1;.
2d620 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 63 72  }..static int.cr
2d630 65 61 74 65 5f 63 68 65 63 6b 5f 65 64 67 65 5f  eate_check_edge_
2d640 67 65 6f 6d 73 20 28 73 71 6c 69 74 65 33 20 2a  geoms (sqlite3 *
2d650 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73 74 20 63   sqlite, const c
2d660 68 61 72 20 2a 76 69 65 77 2c 0a 09 09 09 20 63  har *view,.... c
2d670 6f 6e 73 74 20 63 68 61 72 20 2a 74 61 62 6c 65  onst char *table
2d680 5f 65 64 67 65 73 29 0a 7b 0a 2f 2a 20 63 72 65  _edges).{./* cre
2d690 61 74 69 6e 67 20 74 68 65 20 63 68 65 63 6b 20  ating the check 
2d6a0 65 64 67 65 20 67 65 6f 6d 73 20 56 49 45 57 20  edge geoms VIEW 
2d6b0 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c 5b  */.    char sql[
2d6c0 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61 72 20  2048];.    char 
2d6d0 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20 20 20  sql2[2048];.    
2d6e0 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31 30  char sqltable[10
2d6f0 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72 65 74  24];.    int ret
2d700 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72 5f  ;.    char *err_
2d710 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  msg = NULL;.    
2d720 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65  strcpy (sqltable
2d730 2c 20 76 69 65 77 29 3b 0a 20 20 20 20 64 6f 75  , view);.    dou
2d740 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
2d750 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
2d760 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 43 52  printf (sql, "CR
2d770 45 41 54 45 20 56 49 45 57 20 25 73 20 41 53 5c  EATE VIEW %s AS\
2d780 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  n", sqltable);. 
2d790 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2d7a0 22 53 45 4c 45 43 54 20 65 31 2e 65 64 67 65 5f  "SELECT e1.edge_
2d7b0 69 64 20 41 53 20 65 64 67 65 31 5f 69 64 2c 20  id AS edge1_id, 
2d7c0 65 31 2e 65 64 67 65 5f 63 6f 64 65 20 41 53 20  e1.edge_code AS 
2d7d0 65 64 67 65 31 5f 63 6f 64 65 2c 20 22 29 3b 0a  edge1_code, ");.
2d7e0 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
2d7f0 20 22 65 32 2e 65 64 67 65 5f 69 64 20 41 53 20   "e2.edge_id AS 
2d800 65 64 67 65 32 5f 69 64 2c 20 65 32 2e 65 64 67  edge2_id, e2.edg
2d810 65 5f 63 6f 64 65 20 41 53 20 65 64 67 65 32 5f  e_code AS edge2_
2d820 63 6f 64 65 5c 6e 22 29 3b 0a 20 20 20 20 73 74  code\n");.    st
2d830 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20  rcpy (sqltable, 
2d840 74 61 62 6c 65 5f 65 64 67 65 73 29 3b 0a 20 20  table_edges);.  
2d850 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f    double_quoted_
2d860 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a  sql (sqltable);.
2d870 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
2d880 32 2c 20 22 46 52 4f 4d 20 25 73 20 41 53 20 65  2, "FROM %s AS e
2d890 31 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b  1\n", sqltable);
2d8a0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2d8b0 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73 70 72  , sql2);.    spr
2d8c0 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 4a 4f 49  intf (sql2, "JOI
2d8d0 4e 20 25 73 20 41 53 20 65 32 20 4f 4e 20 28 5c  N %s AS e2 ON (\
2d8e0 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  n", sqltable);. 
2d8f0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2d900 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61  sql2);.    strca
2d910 74 20 28 73 71 6c 2c 20 22 20 20 65 31 2e 65 64  t (sql, "  e1.ed
2d920 67 65 5f 69 64 20 3c 3e 20 65 32 2e 65 64 67 65  ge_id <> e2.edge
2d930 5f 69 64 20 41 4e 44 5c 6e 22 29 3b 0a 20 20 20  _id AND\n");.   
2d940 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4e   strcat (sql, "N
2d950 4f 54 20 28 65 31 2e 6e 6f 64 65 5f 66 72 6f 6d  OT (e1.node_from
2d960 5f 63 6f 64 65 20 3d 20 65 32 2e 6e 6f 64 65 5f  _code = e2.node_
2d970 66 72 6f 6d 5f 63 6f 64 65 20 22 29 3b 0a 20 20  from_code ");.  
2d980 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2d990 41 4e 44 20 65 31 2e 6e 6f 64 65 5f 74 6f 5f 63  AND e1.node_to_c
2d9a0 6f 64 65 20 3d 20 65 32 2e 6e 6f 64 65 5f 74 6f  ode = e2.node_to
2d9b0 5f 63 6f 64 65 29 20 41 4e 44 5c 6e 22 29 3b 0a  _code) AND\n");.
2d9c0 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
2d9d0 20 22 20 20 53 54 5f 43 72 6f 73 73 65 73 28 65   "  ST_Crosses(e
2d9e0 31 2e 47 65 6f 6d 65 74 72 79 2c 20 65 32 2e 47  1.Geometry, e2.G
2d9f0 65 6f 6d 65 74 72 79 29 20 3d 20 31 20 41 4e 44  eometry) = 1 AND
2da00 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
2da10 20 28 73 71 6c 2c 20 22 20 20 65 32 2e 65 64 67   (sql, "  e2.edg
2da20 65 5f 69 64 20 49 4e 20 28 5c 6e 22 29 3b 0a 20  e_id IN (\n");. 
2da30 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2da40 22 09 53 45 4c 45 43 54 20 52 4f 57 49 44 20 46  ".SELECT ROWID F
2da50 52 4f 4d 20 53 70 61 74 69 61 6c 49 6e 64 65 78  ROM SpatialIndex
2da60 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 70 79  \n");.    strcpy
2da70 20 28 73 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c   (sqltable, tabl
2da80 65 5f 65 64 67 65 73 29 3b 0a 20 20 20 20 63 6c  e_edges);.    cl
2da90 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28  ean_sql_string (
2daa0 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
2dab0 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 09  printf (sql2, ".
2dac0 57 48 45 52 45 20 66 5f 74 61 62 6c 65 5f 6e 61  WHERE f_table_na
2dad0 6d 65 20 3d 20 27 25 73 27 20 41 4e 44 5c 6e 22  me = '%s' AND\n"
2dae0 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20  , sqltable);.   
2daf0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71   strcat (sql, sq
2db00 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  l2);.    strcat 
2db10 28 73 71 6c 2c 20 22 09 20 20 73 65 61 72 63 68  (sql, ".  search
2db20 5f 66 72 61 6d 65 20 3d 20 65 31 2e 47 65 6f 6d  _frame = e1.Geom
2db30 65 74 72 79 29 29 5c 6e 22 29 3b 0a 20 20 20 20  etry))\n");.    
2db40 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78  ret = sqlite3_ex
2db50 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c  ec (sqlite, sql,
2db60 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72   NULL, NULL, &er
2db70 72 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28  r_msg);.    if (
2db80 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b  ret != SQLITE_OK
2db90 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61  ).      {..  spa
2dba0 74 69 61 6c 69 74 65 5f 65 20 28 22 43 52 45 41  tialite_e ("CREA
2dbb0 54 45 20 56 49 45 57 20 27 25 73 27 20 65 72 72  TE VIEW '%s' err
2dbc0 6f 72 3a 20 25 73 5c 6e 22 2c 20 76 69 65 77 2c  or: %s\n", view,
2dbd0 20 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71   err_msg);..  sq
2dbe0 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 5f  lite3_free (err_
2dbf0 6d 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20  msg);..  return 
2dc00 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72  0;.      }.    r
2dc10 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74  eturn 1;.}..stat
2dc20 69 63 20 69 6e 74 0a 63 72 65 61 74 65 5f 63 68  ic int.create_ch
2dc30 65 63 6b 5f 65 64 67 65 5f 6e 6f 64 65 5f 67 65  eck_edge_node_ge
2dc40 6f 6d 73 20 28 73 71 6c 69 74 65 33 20 2a 20 73  oms (sqlite3 * s
2dc50 71 6c 69 74 65 2c 20 63 6f 6e 73 74 20 63 68 61  qlite, const cha
2dc60 72 20 2a 76 69 65 77 2c 0a 09 09 09 20 20 20 20  r *view,....    
2dc70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 61    const char *ta
2dc80 62 6c 65 5f 65 64 67 65 73 2c 20 63 6f 6e 73 74  ble_edges, const
2dc90 20 63 68 61 72 20 2a 74 61 62 6c 65 5f 6e 6f 64   char *table_nod
2dca0 65 73 29 0a 7b 0a 2f 2a 20 63 72 65 61 74 69 6e  es).{./* creatin
2dcb0 67 20 74 68 65 20 63 68 65 63 6b 20 65 64 67 65  g the check edge
2dcc0 2f 6e 6f 64 65 20 67 65 6f 6d 73 20 56 49 45 57  /node geoms VIEW
2dcd0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c   */.    char sql
2dce0 5b 34 31 39 36 5d 3b 0a 20 20 20 20 63 68 61 72  [4196];.    char
2dcf0 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20 20   sql2[2048];.   
2dd00 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31   char sqltable[1
2dd10 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72 65  024];.    int re
2dd20 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72  t;.    char *err
2dd30 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  _msg = NULL;.   
2dd40 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c   strcpy (sqltabl
2dd50 65 2c 20 76 69 65 77 29 3b 0a 20 20 20 20 64 6f  e, view);.    do
2dd60 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
2dd70 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20  (sqltable);.    
2dd80 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22 43  sprintf (sql, "C
2dd90 52 45 41 54 45 20 56 49 45 57 20 25 73 20 41 53  REATE VIEW %s AS
2dda0 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a  \n", sqltable);.
2ddb0 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
2ddc0 20 22 53 45 4c 45 43 54 20 65 2e 65 64 67 65 5f   "SELECT e.edge_
2ddd0 69 64 20 41 53 20 65 64 67 65 5f 69 64 2c 20 6e  id AS edge_id, n
2dde0 2e 6e 6f 64 65 5f 69 64 20 41 53 20 6e 6f 64 65  .node_id AS node
2ddf0 5f 69 64 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  _id\n");.    str
2de00 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74  cpy (sqltable, t
2de10 61 62 6c 65 5f 65 64 67 65 73 29 3b 0a 20 20 20  able_edges);.   
2de20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
2de30 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql (sqltable);. 
2de40 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32     sprintf (sql2
2de50 2c 20 22 46 52 4f 4d 20 25 73 20 41 53 20 65 2c  , "FROM %s AS e,
2de60 20 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b   \n", sqltable);
2de70 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2de80 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72  , sql2);.    str
2de90 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 74  cpy (sqltable, t
2dea0 61 62 6c 65 5f 6e 6f 64 65 73 29 3b 0a 20 20 20  able_nodes);.   
2deb0 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
2dec0 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql (sqltable);. 
2ded0 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32     sprintf (sql2
2dee0 2c 20 22 25 73 20 41 53 20 6e 5c 6e 22 2c 20 73  , "%s AS n\n", s
2def0 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  qltable);.    st
2df00 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29  rcat (sql, sql2)
2df10 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
2df20 6c 2c 20 22 57 48 45 52 45 20 53 54 5f 49 6e 74  l, "WHERE ST_Int
2df30 65 72 73 65 63 74 73 28 65 2e 47 65 6f 6d 65 74  ersects(e.Geomet
2df40 72 79 2c 20 6e 2e 47 65 6f 6d 65 74 72 79 29 5c  ry, n.Geometry)\
2df50 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
2df60 28 73 71 6c 2c 0a 09 20 20 20 20 22 20 20 41 4e  (sql,..    "  AN
2df70 44 20 53 54 5f 45 71 75 61 6c 73 28 53 54 5f 53  D ST_Equals(ST_S
2df80 74 61 72 74 50 6f 69 6e 74 28 65 2e 47 65 6f 6d  tartPoint(e.Geom
2df90 65 74 72 79 29 2c 20 6e 2e 47 65 6f 6d 65 74 72  etry), n.Geometr
2dfa0 79 29 20 3d 20 30 5c 6e 22 29 3b 0a 20 20 20 20  y) = 0\n");.    
2dfb0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 20 20  strcat (sql, "  
2dfc0 41 4e 44 20 53 54 5f 45 71 75 61 6c 73 28 53 54  AND ST_Equals(ST
2dfd0 5f 45 6e 64 50 6f 69 6e 74 28 65 2e 47 65 6f 6d  _EndPoint(e.Geom
2dfe0 65 74 72 79 29 2c 20 6e 2e 47 65 6f 6d 65 74 72  etry), n.Geometr
2dff0 79 29 20 3d 20 30 5c 6e 22 29 3b 0a 20 20 20 20  y) = 0\n");.    
2e000 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 20 20  strcat (sql, "  
2e010 41 4e 44 20 6e 2e 52 4f 57 49 44 20 49 4e 20 28  AND n.ROWID IN (
2e020 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
2e030 20 28 73 71 6c 2c 20 22 20 20 20 20 53 45 4c 45   (sql, "    SELE
2e040 43 54 20 52 4f 57 49 44 20 46 52 4f 4d 20 53 70  CT ROWID FROM Sp
2e050 61 74 69 61 6c 49 6e 64 65 78 5c 6e 22 29 3b 0a  atialIndex\n");.
2e060 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74      strcpy (sqlt
2e070 61 62 6c 65 2c 20 74 61 62 6c 65 5f 6e 6f 64 65  able, table_node
2e080 73 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71  s);.    clean_sq
2e090 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74 61 62  l_string (sqltab
2e0a0 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66  le);.    sprintf
2e0b0 20 28 73 71 6c 32 2c 20 22 20 20 20 20 57 48 45   (sql2, "    WHE
2e0c0 52 45 20 66 5f 74 61 62 6c 65 5f 6e 61 6d 65 20  RE f_table_name 
2e0d0 3d 20 27 25 73 27 5c 6e 22 2c 20 73 71 6c 74 61  = '%s'\n", sqlta
2e0e0 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ble);.    strcat
2e0f0 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20   (sql, sql2);.  
2e100 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2e110 20 20 20 20 20 20 41 4e 44 20 73 65 61 72 63 68        AND search
2e120 5f 66 72 61 6d 65 20 3d 20 65 2e 47 65 6f 6d 65  _frame = e.Geome
2e130 74 72 79 29 3b 22 29 3b 0a 20 20 20 20 72 65 74  try);");.    ret
2e140 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20   = sqlite3_exec 
2e150 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55  (sqlite, sql, NU
2e160 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f 6d  LL, NULL, &err_m
2e170 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65 74  sg);.    if (ret
2e180 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20   != SQLITE_OK). 
2e190 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61       {..  spatia
2e1a0 6c 69 74 65 5f 65 20 28 22 43 52 45 41 54 45 20  lite_e ("CREATE 
2e1b0 56 49 45 57 20 27 25 73 27 20 65 72 72 6f 72 3a  VIEW '%s' error:
2e1c0 20 25 73 5c 6e 22 2c 20 76 69 65 77 2c 20 65 72   %s\n", view, er
2e1d0 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74  r_msg);..  sqlit
2e1e0 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73 67  e3_free (err_msg
2e1f0 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a  );..  return 0;.
2e200 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75        }.    retu
2e210 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  rn 1;.}..static 
2e220 69 6e 74 0a 63 72 65 61 74 65 5f 63 68 65 63 6b  int.create_check
2e230 5f 66 61 63 65 5f 63 6f 64 65 73 20 28 73 71 6c  _face_codes (sql
2e240 69 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63  ite3 * sqlite, c
2e250 6f 6e 73 74 20 63 68 61 72 20 2a 76 69 65 77 2c  onst char *view,
2e260 0a 09 09 09 20 63 6f 6e 73 74 20 63 68 61 72 20  .... const char 
2e270 2a 74 61 62 6c 65 5f 66 61 63 65 73 29 0a 7b 0a  *table_faces).{.
2e280 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  /* creating the 
2e290 63 68 65 63 6b 20 66 61 63 65 20 63 6f 64 65 73  check face codes
2e2a0 20 56 49 45 57 20 2a 2f 0a 20 20 20 20 63 68 61   VIEW */.    cha
2e2b0 72 20 73 71 6c 5b 32 30 34 38 5d 3b 0a 20 20 20  r sql[2048];.   
2e2c0 20 63 68 61 72 20 73 71 6c 32 5b 32 30 34 38 5d   char sql2[2048]
2e2d0 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 74 61  ;.    char sqlta
2e2e0 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69  ble[1024];.    i
2e2f0 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61 72  nt ret;.    char
2e300 20 2a 65 72 72 5f 6d 73 67 20 3d 20 4e 55 4c 4c   *err_msg = NULL
2e310 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
2e320 6c 74 61 62 6c 65 2c 20 76 69 65 77 29 3b 0a 20  ltable, view);. 
2e330 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2e340 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b  _sql (sqltable);
2e350 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
2e360 6c 2c 20 22 43 52 45 41 54 45 20 56 49 45 57 20  l, "CREATE VIEW 
2e370 25 73 20 41 53 5c 6e 22 2c 20 73 71 6c 74 61 62  %s AS\n", sqltab
2e380 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
2e390 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 66 61  (sql, "SELECT fa
2e3a0 63 65 5f 63 6f 64 65 20 41 53 20 66 61 63 65 5f  ce_code AS face_
2e3b0 63 6f 64 65 2c 20 43 6f 75 6e 74 28 66 61 63 65  code, Count(face
2e3c0 5f 69 64 29 20 41 53 20 63 6f 75 6e 74 5c 6e 22  _id) AS count\n"
2e3d0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73  );.    strcpy (s
2e3e0 71 6c 74 61 62 6c 65 2c 20 74 61 62 6c 65 5f 66  qltable, table_f
2e3f0 61 63 65 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c  aces);.    doubl
2e400 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71  e_quoted_sql (sq
2e410 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72  ltable);.    spr
2e420 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 46 52 4f  intf (sql2, "FRO
2e430 4d 20 25 73 5c 6e 22 2c 20 73 71 6c 74 61 62 6c  M %s\n", sqltabl
2e440 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  e);.    strcat (
2e450 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20  sql, sql2);.    
2e460 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 47 52  strcat (sql, "GR
2e470 4f 55 50 20 42 59 20 66 61 63 65 5f 63 6f 64 65  OUP BY face_code
2e480 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
2e490 20 28 73 71 6c 2c 20 22 48 41 56 49 4e 47 20 63   (sql, "HAVING c
2e4a0 6f 75 6e 74 20 3e 20 31 5c 6e 22 29 3b 0a 20 20  ount > 1\n");.  
2e4b0 20 20 72 65 74 20 3d 20 73 71 6c 69 74 65 33 5f    ret = sqlite3_
2e4c0 65 78 65 63 20 28 73 71 6c 69 74 65 2c 20 73 71  exec (sqlite, sq
2e4d0 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26  l, NULL, NULL, &
2e4e0 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66  err_msg);.    if
2e4f0 20 28 72 65 74 20 21 3d 20 53 51 4c 49 54 45 5f   (ret != SQLITE_
2e500 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OK).      {..  s
2e510 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 43 52  patialite_e ("CR
2e520 45 41 54 45 20 56 49 45 57 20 27 25 73 27 20 65  EATE VIEW '%s' e
2e530 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 76 69 65  rror: %s\n", vie
2e540 77 2c 20 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20  w, err_msg);..  
2e550 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28 65 72  sqlite3_free (er
2e560 72 5f 6d 73 67 29 3b 0a 09 20 20 72 65 74 75 72  r_msg);..  retur
2e570 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2e580 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74   return 1;.}..st
2e590 61 74 69 63 20 69 6e 74 0a 63 72 65 61 74 65 5f  atic int.create_
2e5a0 66 61 63 65 73 5f 72 65 73 6f 6c 76 65 64 20 28  faces_resolved (
2e5b0 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69 74 65  sqlite3 * sqlite
2e5c0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 76 69  , const char *vi
2e5d0 65 77 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ew, const char *
2e5e0 66 61 63 65 73 2c 0a 09 09 20 20 20 20 20 20 20  faces,...       
2e5f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 61 63 65  const char *face
2e600 73 5f 65 64 67 65 73 2c 20 63 6f 6e 73 74 20 63  s_edges, const c
2e610 68 61 72 20 2a 65 64 67 65 73 29 0a 7b 0a 2f 2a  har *edges).{./*
2e620 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 46 61   creating the Fa
2e630 63 65 73 20 52 65 73 6f 6c 76 65 64 20 56 49 45  ces Resolved VIE
2e640 57 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71  W */.    char sq
2e650 6c 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61  l[2048];.    cha
2e660 72 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20  r sql2[2048];.  
2e670 20 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b    char sqltable[
2e680 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72  1024];.    int r
2e690 65 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72  et;.    char *er
2e6a0 72 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r_msg = NULL;.  
2e6b0 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62    strcpy (sqltab
2e6c0 6c 65 2c 20 76 69 65 77 29 3b 0a 20 20 20 20 64  le, view);.    d
2e6d0 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c  ouble_quoted_sql
2e6e0 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20   (sqltable);.   
2e6f0 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20 22   sprintf (sql, "
2e700 43 52 45 41 54 45 20 56 49 45 57 20 25 73 20 41  CREATE VIEW %s A
2e710 53 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b  S\n", sqltable);
2e720 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2e730 2c 20 22 53 45 4c 45 43 54 20 66 2e 66 61 63 65  , "SELECT f.face
2e740 5f 69 64 20 41 53 20 66 61 63 65 5f 69 64 2c 20  _id AS face_id, 
2e750 66 2e 66 61 63 65 5f 63 6f 64 65 20 41 53 20 66  f.face_code AS f
2e760 61 63 65 5f 63 6f 64 65 2c 20 22 29 3b 0a 20 20  ace_code, ");.  
2e770 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2e780 53 54 5f 50 6f 6c 79 67 6f 6e 69 7a 65 28 65 2e  ST_Polygonize(e.
2e790 47 65 6f 6d 65 74 72 79 29 20 41 53 20 47 65 6f  Geometry) AS Geo
2e7a0 6d 65 74 72 79 5c 6e 22 29 3b 0a 20 20 20 20 73  metry\n");.    s
2e7b0 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c  trcpy (sqltable,
2e7c0 20 66 61 63 65 73 29 3b 0a 20 20 20 20 64 6f 75   faces);.    dou
2e7d0 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
2e7e0 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
2e7f0 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 46  printf (sql2, "F
2e800 52 4f 4d 20 25 73 20 41 53 20 66 5c 6e 22 2c 20  ROM %s AS f\n", 
2e810 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
2e820 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32  trcat (sql, sql2
2e830 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
2e840 71 6c 2c 20 22 4c 45 46 54 20 4a 4f 49 4e 20 22  ql, "LEFT JOIN "
2e850 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73  );.    strcpy (s
2e860 71 6c 74 61 62 6c 65 2c 20 66 61 63 65 73 5f 65  qltable, faces_e
2e870 64 67 65 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c  dges);.    doubl
2e880 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71  e_quoted_sql (sq
2e890 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
2e8a0 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 74 61 62  cat (sql, sqltab
2e8b0 6c 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  le);.    double_
2e8c0 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74  quoted_sql (sqlt
2e8d0 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
2e8e0 74 20 28 73 71 6c 2c 20 22 20 41 53 20 66 65 20  t (sql, " AS fe 
2e8f0 4f 4e 20 28 66 65 2e 66 61 63 65 5f 69 64 20 3d  ON (fe.face_id =
2e900 20 66 2e 66 61 63 65 5f 69 64 29 5c 6e 22 29 3b   f.face_id)\n");
2e910 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2e920 2c 20 22 4c 45 46 54 20 4a 4f 49 4e 20 22 29 3b  , "LEFT JOIN ");
2e930 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
2e940 74 61 62 6c 65 2c 20 65 64 67 65 73 29 3b 0a 20  table, edges);. 
2e950 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2e960 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b  _sql (sqltable);
2e970 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2e980 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20  , sqltable);.   
2e990 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
2e9a0 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql (sqltable);. 
2e9b0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2e9c0 22 20 41 53 20 65 20 4f 4e 20 28 65 2e 65 64 67  " AS e ON (e.edg
2e9d0 65 5f 63 6f 64 65 20 3d 20 66 65 2e 65 64 67 65  e_code = fe.edge
2e9e0 5f 63 6f 64 65 29 5c 6e 22 29 3b 0a 20 20 20 20  _code)\n");.    
2e9f0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 47 52  strcat (sql, "GR
2ea00 4f 55 50 20 42 59 20 66 2e 66 61 63 65 5f 69 64  OUP BY f.face_id
2ea10 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  \n");.    ret = 
2ea20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71  sqlite3_exec (sq
2ea30 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c  lite, sql, NULL,
2ea40 20 4e 55 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29   NULL, &err_msg)
2ea50 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d  ;.    if (ret !=
2ea60 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20   SQLITE_OK).    
2ea70 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
2ea80 65 5f 65 20 28 22 43 52 45 41 54 45 20 56 49 45  e_e ("CREATE VIE
2ea90 57 20 27 25 73 27 20 65 72 72 6f 72 3a 20 25 73  W '%s' error: %s
2eaa0 5c 6e 22 2c 20 76 69 65 77 2c 20 65 72 72 5f 6d  \n", view, err_m
2eab0 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  sg);..  sqlite3_
2eac0 66 72 65 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a  free (err_msg);.
2ead0 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20  .  return 0;.   
2eae0 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
2eaf0 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  1;.}..static int
2eb00 0a 63 72 65 61 74 65 5f 63 75 72 76 65 73 5f 72  .create_curves_r
2eb10 65 73 6f 6c 76 65 64 20 28 73 71 6c 69 74 65 33  esolved (sqlite3
2eb20 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73 74   * sqlite, const
2eb30 20 63 68 61 72 20 2a 76 69 65 77 2c 0a 09 09 09   char *view,....
2eb40 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 75 72 76  const char *curv
2eb50 65 73 2c 20 63 68 61 72 20 2a 65 64 67 65 73 29  es, char *edges)
2eb60 0a 7b 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74  .{./* creating t
2eb70 68 65 20 43 75 72 76 65 73 20 52 65 73 6f 6c 76  he Curves Resolv
2eb80 65 64 20 56 49 45 57 20 2a 2f 0a 20 20 20 20 63  ed VIEW */.    c
2eb90 68 61 72 20 73 71 6c 5b 32 30 34 38 5d 3b 0a 20  har sql[2048];. 
2eba0 20 20 20 63 68 61 72 20 73 71 6c 32 5b 32 30 34     char sql2[204
2ebb0 38 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c  8];.    char sql
2ebc0 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20  table[1024];.   
2ebd0 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 68   int ret;.    ch
2ebe0 61 72 20 2a 65 72 72 5f 6d 73 67 20 3d 20 4e 55  ar *err_msg = NU
2ebf0 4c 4c 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28  LL;.    strcpy (
2ec00 73 71 6c 74 61 62 6c 65 2c 20 76 69 65 77 29 3b  sqltable, view);
2ec10 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74  .    double_quot
2ec20 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65  ed_sql (sqltable
2ec30 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  );.    sprintf (
2ec40 73 71 6c 2c 20 22 43 52 45 41 54 45 20 56 49 45  sql, "CREATE VIE
2ec50 57 20 25 73 20 41 53 5c 6e 22 2c 20 73 71 6c 74  W %s AS\n", sqlt
2ec60 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
2ec70 74 20 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20  t (sql, "SELECT 
2ec80 63 2e 63 75 72 76 65 5f 69 64 20 41 53 20 63 75  c.curve_id AS cu
2ec90 72 76 65 5f 69 64 2c 20 22 29 3b 0a 20 20 20 20  rve_id, ");.    
2eca0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 43 61  strcat (sql, "Ca
2ecb0 73 74 54 6f 4d 75 6c 74 69 4c 69 6e 65 73 74 72  stToMultiLinestr
2ecc0 69 6e 67 28 53 54 5f 43 6f 6c 6c 65 63 74 28 65  ing(ST_Collect(e
2ecd0 2e 47 65 6f 6d 65 74 72 79 29 29 20 41 53 20 47  .Geometry)) AS G
2ece0 65 6f 6d 65 74 72 79 5c 6e 22 29 3b 0a 20 20 20  eometry\n");.   
2ecf0 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c   strcpy (sqltabl
2ed00 65 2c 20 63 75 72 76 65 73 29 3b 0a 20 20 20 20  e, curves);.    
2ed10 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
2ed20 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  l (sqltable);.  
2ed30 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c    sprintf (sql2,
2ed40 20 22 46 52 4f 4d 20 25 73 20 41 53 20 63 5c 6e   "FROM %s AS c\n
2ed50 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  ", sqltable);.  
2ed60 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
2ed70 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ql2);.    strcat
2ed80 20 28 73 71 6c 2c 20 22 4c 45 46 54 20 4a 4f 49   (sql, "LEFT JOI
2ed90 4e 20 22 29 3b 0a 20 20 20 20 73 74 72 63 70 79  N ");.    strcpy
2eda0 20 28 73 71 6c 74 61 62 6c 65 2c 20 65 64 67 65   (sqltable, edge
2edb0 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  s);.    double_q
2edc0 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61  uoted_sql (sqlta
2edd0 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ble);.    strcat
2ede0 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c 65 29   (sql, sqltable)
2edf0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f  ;.    double_quo
2ee00 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c  ted_sql (sqltabl
2ee10 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  e);.    strcat (
2ee20 73 71 6c 2c 20 22 20 41 53 20 65 20 4f 4e 20 28  sql, " AS e ON (
2ee30 65 2e 65 64 67 65 5f 63 6f 64 65 20 3d 20 63 2e  e.edge_code = c.
2ee40 65 64 67 65 5f 63 6f 64 65 29 5c 6e 22 29 3b 0a  edge_code)\n");.
2ee50 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
2ee60 20 22 47 52 4f 55 50 20 42 59 20 63 2e 63 75 72   "GROUP BY c.cur
2ee70 76 65 5f 69 64 5c 6e 22 29 3b 0a 20 20 20 20 72  ve_id\n");.    r
2ee80 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  et = sqlite3_exe
2ee90 63 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20  c (sqlite, sql, 
2eea0 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72  NULL, NULL, &err
2eeb0 5f 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72  _msg);.    if (r
2eec0 65 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29  et != SQLITE_OK)
2eed0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
2eee0 69 61 6c 69 74 65 5f 65 20 28 22 43 52 45 41 54  ialite_e ("CREAT
2eef0 45 20 56 49 45 57 20 27 25 73 27 20 65 72 72 6f  E VIEW '%s' erro
2ef00 72 3a 20 25 73 5c 6e 22 2c 20 76 69 65 77 2c 20  r: %s\n", view, 
2ef10 65 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c  err_msg);..  sql
2ef20 69 74 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d  ite3_free (err_m
2ef30 73 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30  sg);..  return 0
2ef40 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65  ;.      }.    re
2ef50 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69  turn 1;.}..stati
2ef60 63 20 69 6e 74 0a 63 72 65 61 74 65 5f 73 75 72  c int.create_sur
2ef70 66 61 63 65 73 5f 72 65 73 6f 6c 76 65 64 20 28  faces_resolved (
2ef80 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69 74 65  sqlite3 * sqlite
2ef90 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 76 69  , const char *vi
2efa0 65 77 2c 0a 09 09 09 20 20 63 6f 6e 73 74 20 63  ew,....  const c
2efb0 68 61 72 20 2a 73 75 72 66 61 63 65 73 2c 20 63  har *surfaces, c
2efc0 6f 6e 73 74 20 63 68 61 72 20 2a 66 61 63 65 73  onst char *faces
2efd0 29 0a 7b 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20  ).{./* creating 
2efe0 74 68 65 20 53 75 72 66 61 63 65 73 20 52 65 73  the Surfaces Res
2eff0 6f 6c 76 65 64 20 56 49 45 57 20 2a 2f 0a 20 20  olved VIEW */.  
2f000 20 20 63 68 61 72 20 73 71 6c 5b 32 30 34 38 5d    char sql[2048]
2f010 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 32 5b  ;.    char sql2[
2f020 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61 72 20  2048];.    char 
2f030 73 71 6c 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a  sqltable[1024];.
2f040 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
2f050 20 63 68 61 72 20 2a 65 72 72 5f 6d 73 67 20 3d   char *err_msg =
2f060 20 4e 55 4c 4c 3b 0a 20 20 20 20 73 74 72 63 70   NULL;.    strcp
2f070 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 76 69 65  y (sqltable, vie
2f080 77 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  w);.    double_q
2f090 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61  uoted_sql (sqlta
2f0a0 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
2f0b0 66 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20  f (sql, "CREATE 
2f0c0 56 49 45 57 20 25 73 20 41 53 5c 6e 22 2c 20 73  VIEW %s AS\n", s
2f0d0 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  qltable);.    st
2f0e0 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45 4c 45  rcat (sql, "SELE
2f0f0 43 54 20 73 2e 73 75 72 66 61 63 65 5f 69 64 20  CT s.surface_id 
2f100 41 53 20 73 75 72 66 61 63 65 5f 69 64 2c 5c 6e  AS surface_id,\n
2f110 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
2f120 73 71 6c 2c 0a 09 20 20 20 20 22 20 20 43 61 73  sql,..    "  Cas
2f130 74 54 6f 4d 75 6c 74 69 70 6f 6c 79 67 6f 6e 28  tToMultipolygon(
2f140 53 54 5f 55 6e 61 72 79 55 6e 69 6f 6e 28 53 54  ST_UnaryUnion(ST
2f150 5f 43 6f 6c 6c 65 63 74 28 66 2e 47 65 6f 6d 65  _Collect(f.Geome
2f160 74 72 79 29 29 29 20 41 53 20 47 65 6f 6d 65 74  try))) AS Geomet
2f170 72 79 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  ry\n");.    strc
2f180 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 73 75  py (sqltable, su
2f190 72 66 61 63 65 73 29 3b 0a 20 20 20 20 64 6f 75  rfaces);.    dou
2f1a0 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
2f1b0 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
2f1c0 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 46  printf (sql2, "F
2f1d0 52 4f 4d 20 25 73 20 41 53 20 73 5c 6e 22 2c 20  ROM %s AS s\n", 
2f1e0 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
2f1f0 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32  trcat (sql, sql2
2f200 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
2f210 71 6c 2c 20 22 4c 45 46 54 20 4a 4f 49 4e 20 22  ql, "LEFT JOIN "
2f220 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73  );.    strcpy (s
2f230 71 6c 74 61 62 6c 65 2c 20 66 61 63 65 73 29 3b  qltable, faces);
2f240 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74  .    double_quot
2f250 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65  ed_sql (sqltable
2f260 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
2f270 71 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql, sqltable);. 
2f280 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2f290 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b  _sql (sqltable);
2f2a0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2f2b0 2c 20 22 20 41 53 20 66 20 4f 4e 20 28 66 2e 66  , " AS f ON (f.f
2f2c0 61 63 65 5f 63 6f 64 65 20 3d 20 73 2e 66 61 63  ace_code = s.fac
2f2d0 65 5f 63 6f 64 65 29 5c 6e 22 29 3b 0a 20 20 20  e_code)\n");.   
2f2e0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 47   strcat (sql, "G
2f2f0 52 4f 55 50 20 42 59 20 73 2e 73 75 72 66 61 63  ROUP BY s.surfac
2f300 65 5f 69 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65  e_id\n");.    re
2f310 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
2f320 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
2f330 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f  ULL, NULL, &err_
2f340 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  msg);.    if (re
2f350 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
2f360 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
2f370 61 6c 69 74 65 5f 65 20 28 22 43 52 45 41 54 45  alite_e ("CREATE
2f380 20 56 49 45 57 20 27 25 73 27 20 65 72 72 6f 72   VIEW '%s' error
2f390 3a 20 25 73 5c 6e 22 2c 20 76 69 65 77 2c 20 65  : %s\n", view, e
2f3a0 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69  rr_msg);..  sqli
2f3b0 74 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73  te3_free (err_ms
2f3c0 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b  g);..  return 0;
2f3d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74  .      }.    ret
2f3e0 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63  urn 1;.}..static
2f3f0 20 69 6e 74 0a 63 72 65 61 74 65 5f 64 61 6e 67   int.create_dang
2f400 6c 69 6e 67 5f 6e 6f 64 65 73 20 28 73 71 6c 69  ling_nodes (sqli
2f410 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f  te3 * sqlite, co
2f420 6e 73 74 20 63 68 61 72 20 2a 76 69 65 77 2c 0a  nst char *view,.
2f430 09 09 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ..       const c
2f440 68 61 72 20 2a 6e 6f 64 65 73 2c 20 63 6f 6e 73  har *nodes, cons
2f450 74 20 63 68 61 72 20 2a 65 64 67 65 73 29 0a 7b  t char *edges).{
2f460 0a 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65  ./* creating the
2f470 20 44 61 6e 67 6c 69 6e 67 20 4e 6f 64 65 73 20   Dangling Nodes 
2f480 56 49 45 57 20 2a 2f 0a 20 20 20 20 63 68 61 72  VIEW */.    char
2f490 20 73 71 6c 5b 38 31 39 32 5d 3b 0a 20 20 20 20   sql[8192];.    
2f4a0 63 68 61 72 20 73 71 6c 32 5b 32 30 34 38 5d 3b  char sql2[2048];
2f4b0 0a 20 20 20 20 63 68 61 72 20 73 71 6c 74 61 62  .    char sqltab
2f4c0 6c 65 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e  le[1024];.    in
2f4d0 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61 72 20  t ret;.    char 
2f4e0 2a 65 72 72 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b  *err_msg = NULL;
2f4f0 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
2f500 74 61 62 6c 65 2c 20 76 69 65 77 29 3b 0a 20 20  table, view);.  
2f510 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f    double_quoted_
2f520 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a  sql (sqltable);.
2f530 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
2f540 2c 20 22 43 52 45 41 54 45 20 56 49 45 57 20 25  , "CREATE VIEW %
2f550 73 20 41 53 5c 6e 22 2c 20 73 71 6c 74 61 62 6c  s AS\n", sqltabl
2f560 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  e);.    strcat (
2f570 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 6e 2e 6e  sql, "SELECT n.n
2f580 6f 64 65 5f 69 64 20 41 53 20 6e 6f 64 65 5f 69  ode_id AS node_i
2f590 64 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 70  d\n");.    strcp
2f5a0 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 6e 6f 64  y (sqltable, nod
2f5b0 65 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  es);.    double_
2f5c0 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74  quoted_sql (sqlt
2f5d0 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e  able);.    sprin
2f5e0 74 66 20 28 73 71 6c 32 2c 20 22 46 52 4f 4d 20  tf (sql2, "FROM 
2f5f0 25 73 20 41 53 20 6e 5c 6e 22 2c 20 73 71 6c 74  %s AS n\n", sqlt
2f600 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
2f610 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20  t (sql, sql2);. 
2f620 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2f630 22 4c 45 46 54 20 4a 4f 49 4e 20 22 29 3b 0a 20  "LEFT JOIN ");. 
2f640 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61     strcpy (sqlta
2f650 62 6c 65 2c 20 65 64 67 65 73 29 3b 0a 20 20 20  ble, edges);.   
2f660 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73   double_quoted_s
2f670 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql (sqltable);. 
2f680 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2f690 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 64  sqltable);.    d
2f6a0 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c  ouble_quoted_sql
2f6b0 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20   (sqltable);.   
2f6c0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 20   strcat (sql, " 
2f6d0 41 53 20 65 20 4f 4e 20 28 6e 2e 6e 6f 64 65 5f  AS e ON (n.node_
2f6e0 63 6f 64 65 20 3d 20 65 2e 6e 6f 64 65 5f 66 72  code = e.node_fr
2f6f0 6f 6d 5f 63 6f 64 65 29 5c 6e 22 29 3b 0a 20 20  om_code)\n");.  
2f700 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2f710 57 48 45 52 45 20 65 2e 65 64 67 65 5f 69 64 20  WHERE e.edge_id 
2f720 49 53 20 4e 55 4c 4c 5c 6e 22 29 3b 0a 20 20 20  IS NULL\n");.   
2f730 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 49   strcat (sql, "I
2f740 4e 54 45 52 53 45 43 54 5c 6e 22 29 3b 0a 20 20  NTERSECT\n");.  
2f750 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2f760 53 45 4c 45 43 54 20 6e 2e 6e 6f 64 65 5f 69 64  SELECT n.node_id
2f770 20 41 53 20 6e 6f 64 65 5f 69 64 5c 6e 22 29 3b   AS node_id\n");
2f780 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
2f790 74 61 62 6c 65 2c 20 6e 6f 64 65 73 29 3b 0a 20  table, nodes);. 
2f7a0 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2f7b0 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b  _sql (sqltable);
2f7c0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
2f7d0 6c 32 2c 20 22 46 52 4f 4d 20 25 73 20 41 53 20  l2, "FROM %s AS 
2f7e0 6e 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b  n\n", sqltable);
2f7f0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2f800 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72  , sql2);.    str
2f810 63 61 74 20 28 73 71 6c 2c 20 22 4c 45 46 54 20  cat (sql, "LEFT 
2f820 4a 4f 49 4e 20 22 29 3b 0a 20 20 20 20 73 74 72  JOIN ");.    str
2f830 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 65  cpy (sqltable, e
2f840 64 67 65 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c  dges);.    doubl
2f850 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71  e_quoted_sql (sq
2f860 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
2f870 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 74 61 62  cat (sql, sqltab
2f880 6c 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  le);.    double_
2f890 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74  quoted_sql (sqlt
2f8a0 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
2f8b0 74 20 28 73 71 6c 2c 20 22 20 41 53 20 65 20 4f  t (sql, " AS e O
2f8c0 4e 20 28 6e 2e 6e 6f 64 65 5f 63 6f 64 65 20 3d  N (n.node_code =
2f8d0 20 65 2e 6e 6f 64 65 5f 74 6f 5f 63 6f 64 65 29   e.node_to_code)
2f8e0 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
2f8f0 20 28 73 71 6c 2c 20 22 57 48 45 52 45 20 65 2e   (sql, "WHERE e.
2f900 65 64 67 65 5f 69 64 20 49 53 20 4e 55 4c 4c 5c  edge_id IS NULL\
2f910 6e 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 73  n");.    ret = s
2f920 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71 6c  qlite3_exec (sql
2f930 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20  ite, sql, NULL, 
2f940 4e 55 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b  NULL, &err_msg);
2f950 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
2f960 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
2f970 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
2f980 5f 65 20 28 22 43 52 45 41 54 45 20 56 49 45 57  _e ("CREATE VIEW
2f990 20 27 25 73 27 20 65 72 72 6f 72 3a 20 25 73 5c   '%s' error: %s\
2f9a0 6e 22 2c 20 76 69 65 77 2c 20 65 72 72 5f 6d 73  n", view, err_ms
2f9b0 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 66  g);..  sqlite3_f
2f9c0 72 65 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a 09  ree (err_msg);..
2f9d0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
2f9e0 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31    }.    return 1
2f9f0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a  ;.}..static int.
2fa00 63 72 65 61 74 65 5f 64 61 6e 67 6c 69 6e 67 5f  create_dangling_
2fa10 65 64 67 65 73 20 28 73 71 6c 69 74 65 33 20 2a  edges (sqlite3 *
2fa20 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73 74 20 63   sqlite, const c
2fa30 68 61 72 20 2a 76 69 65 77 2c 0a 09 09 20 20 20  har *view,...   
2fa40 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2fa50 65 64 67 65 73 2c 20 63 6f 6e 73 74 20 63 68 61  edges, const cha
2fa60 72 20 2a 66 61 63 65 73 5f 65 64 67 65 73 2c 0a  r *faces_edges,.
2fa70 09 09 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ..       const c
2fa80 68 61 72 20 2a 63 75 72 76 65 73 29 0a 7b 0a 2f  har *curves).{./
2fa90 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 44  * creating the D
2faa0 61 6e 67 6c 69 6e 67 20 45 64 67 65 73 20 56 49  angling Edges VI
2fab0 45 57 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73  EW */.    char s
2fac0 71 6c 5b 38 31 39 32 5d 3b 0a 20 20 20 20 63 68  ql[8192];.    ch
2fad0 61 72 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20  ar sql2[2048];. 
2fae0 20 20 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65     char sqltable
2faf0 5b 31 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20  [1024];.    int 
2fb00 72 65 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65  ret;.    char *e
2fb10 72 72 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 20  rr_msg = NULL;. 
2fb20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61     strcpy (sqlta
2fb30 62 6c 65 2c 20 76 69 65 77 29 3b 0a 20 20 20 20  ble, view);.    
2fb40 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
2fb50 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  l (sqltable);.  
2fb60 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 2c 20    sprintf (sql, 
2fb70 22 43 52 45 41 54 45 20 56 49 45 57 20 25 73 20  "CREATE VIEW %s 
2fb80 41 53 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29  AS\n", sqltable)
2fb90 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
2fba0 6c 2c 20 22 53 45 4c 45 43 54 20 65 2e 65 64 67  l, "SELECT e.edg
2fbb0 65 5f 69 64 20 41 53 20 65 64 67 65 5f 69 64 5c  e_id AS edge_id\
2fbc0 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20  n");.    strcpy 
2fbd0 28 73 71 6c 74 61 62 6c 65 2c 20 65 64 67 65 73  (sqltable, edges
2fbe0 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75  );.    double_qu
2fbf0 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62  oted_sql (sqltab
2fc00 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66  le);.    sprintf
2fc10 20 28 73 71 6c 32 2c 20 22 46 52 4f 4d 20 25 73   (sql2, "FROM %s
2fc20 20 41 53 20 65 5c 6e 22 2c 20 73 71 6c 74 61 62   AS e\n", sqltab
2fc30 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
2fc40 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20  (sql, sql2);.   
2fc50 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4c   strcat (sql, "L
2fc60 45 46 54 20 4a 4f 49 4e 20 22 29 3b 0a 20 20 20  EFT JOIN ");.   
2fc70 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c   strcpy (sqltabl
2fc80 65 2c 20 66 61 63 65 73 5f 65 64 67 65 73 29 3b  e, faces_edges);
2fc90 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74  .    double_quot
2fca0 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65  ed_sql (sqltable
2fcb0 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
2fcc0 71 6c 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ql, sqltable);. 
2fcd0 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64     double_quoted
2fce0 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b  _sql (sqltable);
2fcf0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
2fd00 2c 20 22 20 41 53 20 66 20 4f 4e 20 28 65 2e 65  , " AS f ON (e.e
2fd10 64 67 65 5f 63 6f 64 65 20 3d 20 66 2e 65 64 67  dge_code = f.edg
2fd20 65 5f 63 6f 64 65 29 5c 6e 22 29 3b 0a 20 20 20  e_code)\n");.   
2fd30 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 57   strcat (sql, "W
2fd40 48 45 52 45 20 66 2e 65 64 67 65 5f 63 6f 64 65  HERE f.edge_code
2fd50 20 49 53 20 4e 55 4c 4c 5c 6e 22 29 3b 0a 20 20   IS NULL\n");.  
2fd60 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
2fd70 49 4e 54 45 52 53 45 43 54 5c 6e 22 29 3b 0a 20  INTERSECT\n");. 
2fd80 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
2fd90 22 53 45 4c 45 43 54 20 65 2e 65 64 67 65 5f 69  "SELECT e.edge_i
2fda0 64 20 41 53 20 65 64 67 65 5f 69 64 5c 6e 22 29  d AS edge_id\n")
2fdb0 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
2fdc0 6c 74 61 62 6c 65 2c 20 65 64 67 65 73 29 3b 0a  ltable, edges);.
2fdd0 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65      double_quote
2fde0 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29  d_sql (sqltable)
2fdf0 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73  ;.    sprintf (s
2fe00 71 6c 32 2c 20 22 46 52 4f 4d 20 25 73 20 41 53  ql2, "FROM %s AS
2fe10 20 65 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29   e\n", sqltable)
2fe20 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
2fe30 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73 74  l, sql2);.    st
2fe40 72 63 61 74 20 28 73 71 6c 2c 20 22 4c 45 46 54  rcat (sql, "LEFT
2fe50 20 4a 4f 49 4e 20 22 29 3b 0a 20 20 20 20 73 74   JOIN ");.    st
2fe60 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20  rcpy (sqltable, 
2fe70 63 75 72 76 65 73 29 3b 0a 20 20 20 20 64 6f 75  curves);.    dou
2fe80 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28  ble_quoted_sql (
2fe90 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
2fea0 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 74  trcat (sql, sqlt
2feb0 61 62 6c 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c  able);.    doubl
2fec0 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71  e_quoted_sql (sq
2fed0 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
2fee0 63 61 74 20 28 73 71 6c 2c 20 22 20 41 53 20 63  cat (sql, " AS c
2fef0 20 4f 4e 20 28 65 2e 65 64 67 65 5f 63 6f 64 65   ON (e.edge_code
2ff00 20 3d 20 63 2e 65 64 67 65 5f 63 6f 64 65 29 5c   = c.edge_code)\
2ff10 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
2ff20 28 73 71 6c 2c 20 22 57 48 45 52 45 20 63 2e 65  (sql, "WHERE c.e
2ff30 64 67 65 5f 63 6f 64 65 20 49 53 20 4e 55 4c 4c  dge_code IS NULL
2ff40 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  \n");.    ret = 
2ff50 73 71 6c 69 74 65 33 5f 65 78 65 63 20 28 73 71  sqlite3_exec (sq
2ff60 6c 69 74 65 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c  lite, sql, NULL,
2ff70 20 4e 55 4c 4c 2c 20 26 65 72 72 5f 6d 73 67 29   NULL, &err_msg)
2ff80 3b 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d  ;.    if (ret !=
2ff90 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20   SQLITE_OK).    
2ffa0 20 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74    {..  spatialit
2ffb0 65 5f 65 20 28 22 43 52 45 41 54 45 20 56 49 45  e_e ("CREATE VIE
2ffc0 57 20 27 25 73 27 20 65 72 72 6f 72 3a 20 25 73  W '%s' error: %s
2ffd0 5c 6e 22 2c 20 76 69 65 77 2c 20 65 72 72 5f 6d  \n", view, err_m
2ffe0 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  sg);..  sqlite3_
2fff0 66 72 65 65 20 28 65 72 72 5f 6d 73 67 29 3b 0a  free (err_msg);.
30000 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20  .  return 0;.   
30010 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
30020 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  1;.}..static int
30030 0a 63 72 65 61 74 65 5f 63 68 65 63 6b 5f 65 64  .create_check_ed
30040 67 65 73 5f 66 72 6f 6d 5f 74 6f 20 28 73 71 6c  ges_from_to (sql
30050 69 74 65 33 20 2a 20 73 71 6c 69 74 65 2c 20 63  ite3 * sqlite, c
30060 6f 6e 73 74 20 63 68 61 72 20 2a 76 69 65 77 2c  onst char *view,
30070 0a 09 09 09 20 20 20 20 63 6f 6e 73 74 20 63 68  ....    const ch
30080 61 72 20 2a 65 64 67 65 73 2c 20 63 6f 6e 73 74  ar *edges, const
30090 20 63 68 61 72 20 2a 6e 6f 64 65 73 29 0a 7b 0a   char *nodes).{.
300a0 2f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  /* creating the 
300b0 45 64 67 65 73 2f 4e 6f 64 65 73 20 5b 66 72 6f  Edges/Nodes [fro
300c0 6d 2f 74 6f 5d 20 56 49 45 57 20 2a 2f 0a 20 20  m/to] VIEW */.  
300d0 20 20 63 68 61 72 20 73 71 6c 5b 38 31 39 32 5d    char sql[8192]
300e0 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 32 5b  ;.    char sql2[
300f0 32 30 34 38 5d 3b 0a 20 20 20 20 63 68 61 72 20  2048];.    char 
30100 73 71 6c 74 61 62 6c 65 5b 31 30 32 34 5d 3b 0a  sqltable[1024];.
30110 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
30120 20 63 68 61 72 20 2a 65 72 72 5f 6d 73 67 20 3d   char *err_msg =
30130 20 4e 55 4c 4c 3b 0a 20 20 20 20 73 74 72 63 70   NULL;.    strcp
30140 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 76 69 65  y (sqltable, vie
30150 77 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  w);.    double_q
30160 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61  uoted_sql (sqlta
30170 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
30180 66 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20  f (sql, "CREATE 
30190 56 49 45 57 20 25 73 20 41 53 5c 6e 22 2c 20 73  VIEW %s AS\n", s
301a0 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  qltable);.    st
301b0 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45 4c 45  rcat (sql, "SELE
301c0 43 54 20 65 2e 65 64 67 65 5f 69 64 20 41 53 20  CT e.edge_id AS 
301d0 65 64 67 65 5f 69 64 2c 20 6e 2e 6e 6f 64 65 5f  edge_id, n.node_
301e0 69 64 20 41 53 20 6e 6f 64 65 5f 69 64 2c 5c 6e  id AS node_id,\n
301f0 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
30200 73 71 6c 2c 20 22 20 20 6e 2e 6e 6f 64 65 5f 63  sql, "  n.node_c
30210 6f 64 65 20 41 53 20 6e 6f 64 65 5f 63 6f 64 65  ode AS node_code
30220 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
30230 74 20 28 73 71 6c 2c 20 22 20 20 27 4d 69 73 6d  t (sql, "  'Mism
30240 61 74 63 68 69 6e 67 20 63 6f 6f 72 64 73 27 20  atching coords' 
30250 41 53 20 65 72 72 6f 72 5f 63 61 75 73 65 5c 6e  AS error_cause\n
30260 22 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28  ");.    strcpy (
30270 73 71 6c 74 61 62 6c 65 2c 20 65 64 67 65 73 29  sqltable, edges)
30280 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f  ;.    double_quo
30290 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c  ted_sql (sqltabl
302a0 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  e);.    sprintf 
302b0 28 73 71 6c 32 2c 20 22 46 52 4f 4d 20 25 73 20  (sql2, "FROM %s 
302c0 41 53 20 65 5c 6e 22 2c 20 73 71 6c 74 61 62 6c  AS e\n", sqltabl
302d0 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  e);.    strcat (
302e0 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20  sql, sql2);.    
302f0 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4a 4f  strcat (sql, "JO
30300 49 4e 20 22 29 3b 0a 20 20 20 20 73 74 72 63 70  IN ");.    strcp
30310 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 6e 6f 64  y (sqltable, nod
30320 65 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  es);.    double_
30330 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74  quoted_sql (sqlt
30340 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
30350 74 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c 65  t (sql, sqltable
30360 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75  );.    double_qu
30370 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62  oted_sql (sqltab
30380 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
30390 28 73 71 6c 2c 20 22 20 41 53 20 6e 20 4f 4e 20  (sql, " AS n ON 
303a0 28 65 2e 6e 6f 64 65 5f 66 72 6f 6d 5f 63 6f 64  (e.node_from_cod
303b0 65 20 3d 20 6e 2e 6e 6f 64 65 5f 63 6f 64 65 29  e = n.node_code)
303c0 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74  \n");.    strcat
303d0 20 28 73 71 6c 2c 0a 09 20 20 20 20 22 57 48 45   (sql,..    "WHE
303e0 52 45 20 53 54 5f 45 71 75 61 6c 73 28 53 54 5f  RE ST_Equals(ST_
303f0 53 74 61 72 74 50 6f 69 6e 74 28 65 2e 47 65 6f  StartPoint(e.Geo
30400 6d 65 74 72 79 29 2c 20 6e 2e 47 65 6f 6d 65 74  metry), n.Geomet
30410 72 79 29 20 3d 20 30 5c 6e 22 29 3b 0a 20 20 20  ry) = 0\n");.   
30420 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 55   strcat (sql, "U
30430 4e 49 4f 4e 5c 6e 22 29 3b 0a 20 20 20 20 73 74  NION\n");.    st
30440 72 63 61 74 20 28 73 71 6c 2c 20 22 53 45 4c 45  rcat (sql, "SELE
30450 43 54 20 65 2e 65 64 67 65 5f 69 64 20 41 53 20  CT e.edge_id AS 
30460 65 64 67 65 5f 69 64 2c 20 6e 2e 6e 6f 64 65 5f  edge_id, n.node_
30470 69 64 20 41 53 20 6e 6f 64 65 5f 69 64 2c 5c 6e  id AS node_id,\n
30480 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
30490 73 71 6c 2c 20 22 20 20 6e 2e 6e 6f 64 65 5f 63  sql, "  n.node_c
304a0 6f 64 65 20 41 53 20 6e 6f 64 65 5f 63 6f 64 65  ode AS node_code
304b0 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
304c0 74 20 28 73 71 6c 2c 20 22 20 20 27 4d 69 73 6d  t (sql, "  'Mism
304d0 61 74 63 68 69 6e 67 20 63 6f 6f 72 64 73 27 20  atching coords' 
304e0 41 53 20 65 72 72 6f 72 5f 63 61 75 73 65 5c 6e  AS error_cause\n
304f0 22 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28  ");.    strcpy (
30500 73 71 6c 74 61 62 6c 65 2c 20 65 64 67 65 73 29  sqltable, edges)
30510 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f  ;.    double_quo
30520 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c  ted_sql (sqltabl
30530 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  e);.    sprintf 
30540 28 73 71 6c 32 2c 20 22 46 52 4f 4d 20 25 73 20  (sql2, "FROM %s 
30550 41 53 20 65 5c 6e 22 2c 20 73 71 6c 74 61 62 6c  AS e\n", sqltabl
30560 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  e);.    strcat (
30570 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20  sql, sql2);.    
30580 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 4a 4f  strcat (sql, "JO
30590 49 4e 20 22 29 3b 0a 20 20 20 20 73 74 72 63 70  IN ");.    strcp
305a0 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 6e 6f 64  y (sqltable, nod
305b0 65 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f  es);.    double_
305c0 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74  quoted_sql (sqlt
305d0 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
305e0 74 20 28 73 71 6c 2c 20 73 71 6c 74 61 62 6c 65  t (sql, sqltable
305f0 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71 75  );.    double_qu
30600 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61 62  oted_sql (sqltab
30610 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
30620 28 73 71 6c 2c 20 22 20 41 53 20 6e 20 4f 4e 20  (sql, " AS n ON 
30630 28 65 2e 6e 6f 64 65 5f 74 6f 5f 63 6f 64 65 20  (e.node_to_code 
30640 3d 20 6e 2e 6e 6f 64 65 5f 63 6f 64 65 29 5c 6e  = n.node_code)\n
30650 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
30660 73 71 6c 2c 20 22 57 48 45 52 45 20 53 54 5f 45  sql, "WHERE ST_E
30670 71 75 61 6c 73 28 53 54 5f 45 6e 64 50 6f 69 6e  quals(ST_EndPoin
30680 74 28 65 2e 47 65 6f 6d 65 74 72 79 29 2c 20 6e  t(e.Geometry), n
30690 2e 47 65 6f 6d 65 74 72 79 29 20 3d 20 30 5c 6e  .Geometry) = 0\n
306a0 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
306b0 73 71 6c 2c 20 22 55 4e 49 4f 4e 5c 6e 22 29 3b  sql, "UNION\n");
306c0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
306d0 2c 20 22 53 45 4c 45 43 54 20 65 2e 65 64 67 65  , "SELECT e.edge
306e0 5f 69 64 20 41 53 20 65 64 67 65 5f 69 64 2c 20  _id AS edge_id, 
306f0 6e 2e 6e 6f 64 65 5f 69 64 20 41 53 20 6e 6f 64  n.node_id AS nod
30700 65 5f 69 64 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  e_id,\n");.    s
30710 74 72 63 61 74 20 28 73 71 6c 2c 20 22 20 20 6e  trcat (sql, "  n
30720 2e 6e 6f 64 65 5f 63 6f 64 65 20 41 53 20 6e 6f  .node_code AS no
30730 64 65 5f 63 6f 64 65 2c 5c 6e 22 29 3b 0a 20 20  de_code,\n");.  
30740 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
30750 20 20 27 55 6e 72 65 73 6f 6c 76 65 64 20 4e 6f    'Unresolved No
30760 64 65 20 72 65 66 65 72 65 6e 63 65 27 20 41 53  de reference' AS
30770 20 65 72 72 6f 72 5f 63 61 75 73 65 5c 6e 22 29   error_cause\n")
30780 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
30790 6c 74 61 62 6c 65 2c 20 65 64 67 65 73 29 3b 0a  ltable, edges);.
307a0 20 20 20 20 64 6f 75 62 6c 65 5f 71 75 6f 74 65      double_quote
307b0 64 5f 73 71 6c 20 28 73 71 6c 74 61 62 6c 65 29  d_sql (sqltable)
307c0 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73  ;.    sprintf (s
307d0 71 6c 32 2c 20 22 46 52 4f 4d 20 25 73 20 41 53  ql2, "FROM %s AS
307e0 20 65 5c 6e 22 2c 20 73 71 6c 74 61 62 6c 65 29   e\n", sqltable)
307f0 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
30800 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73 74  l, sql2);.    st
30810 72 63 61 74 20 28 73 71 6c 2c 20 22 4c 45 46 54  rcat (sql, "LEFT
30820 20 4a 4f 49 4e 20 22 29 3b 0a 20 20 20 20 73 74   JOIN ");.    st
30830 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20  rcpy (sqltable, 
30840 6e 6f 64 65 73 29 3b 0a 20 20 20 20 64 6f 75 62  nodes);.    doub
30850 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73  le_quoted_sql (s
30860 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  qltable);.    st
30870 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 74 61  rcat (sql, sqlta
30880 62 6c 65 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65  ble);.    double
30890 5f 71 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c  _quoted_sql (sql
308a0 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63  table);.    strc
308b0 61 74 20 28 73 71 6c 2c 20 22 20 41 53 20 6e 20  at (sql, " AS n 
308c0 4f 4e 20 28 65 2e 6e 6f 64 65 5f 66 72 6f 6d 5f  ON (e.node_from_
308d0 63 6f 64 65 20 3d 20 6e 2e 6e 6f 64 65 5f 63 6f  code = n.node_co
308e0 64 65 29 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  de)\n");.    str
308f0 63 61 74 20 28 73 71 6c 2c 20 22 57 48 45 52 45  cat (sql, "WHERE
30900 20 6e 2e 6e 6f 64 65 5f 69 64 20 49 53 20 4e 55   n.node_id IS NU
30910 4c 4c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  LL\n");.    strc
30920 61 74 20 28 73 71 6c 2c 20 22 55 4e 49 4f 4e 5c  at (sql, "UNION\
30930 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
30940 28 73 71 6c 2c 20 22 53 45 4c 45 43 54 20 65 2e  (sql, "SELECT e.
30950 65 64 67 65 5f 69 64 20 41 53 20 65 64 67 65 5f  edge_id AS edge_
30960 69 64 2c 20 6e 2e 6e 6f 64 65 5f 69 64 20 41 53  id, n.node_id AS
30970 20 6e 6f 64 65 5f 69 64 2c 5c 6e 22 29 3b 0a 20   node_id,\n");. 
30980 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
30990 22 20 20 6e 2e 6e 6f 64 65 5f 63 6f 64 65 20 41  "  n.node_code A
309a0 53 20 6e 6f 64 65 5f 63 6f 64 65 2c 5c 6e 22 29  S node_code,\n")
309b0 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
309c0 6c 2c 20 22 20 20 27 55 6e 72 65 73 6f 6c 76 65  l, "  'Unresolve
309d0 64 20 4e 6f 64 65 20 72 65 66 65 72 65 6e 63 65  d Node reference
309e0 27 20 41 53 20 65 72 72 6f 72 5f 63 61 75 73 65  ' AS error_cause
309f0 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 70 79  \n");.    strcpy
30a00 20 28 73 71 6c 74 61 62 6c 65 2c 20 65 64 67 65   (sqltable, edge
30a10 73 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65 5f 71  s);.    double_q
30a20 75 6f 74 65 64 5f 73 71 6c 20 28 73 71 6c 74 61  uoted_sql (sqlta
30a30 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
30a40 66 20 28 73 71 6c 32 2c 20 22 46 52 4f 4d 20 25  f (sql2, "FROM %
30a50 73 20 41 53 20 65 5c 6e 22 2c 20 73 71 6c 74 61  s AS e\n", sqlta
30a60 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ble);.    strcat
30a70 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20   (sql, sql2);.  
30a80 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22    strcat (sql, "
30a90 4c 45 46 54 20 4a 4f 49 4e 20 22 29 3b 0a 20 20  LEFT JOIN ");.  
30aa0 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62    strcpy (sqltab
30ab0 6c 65 2c 20 6e 6f 64 65 73 29 3b 0a 20 20 20 20  le, nodes);.    
30ac0 64 6f 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71  double_quoted_sq
30ad0 6c 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  l (sqltable);.  
30ae0 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
30af0 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 64 6f  qltable);.    do
30b00 75 62 6c 65 5f 71 75 6f 74 65 64 5f 73 71 6c 20  uble_quoted_sql 
30b10 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20  (sqltable);.    
30b20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 20 41  strcat (sql, " A
30b30 53 20 6e 20 4f 4e 20 28 65 2e 6e 6f 64 65 5f 74  S n ON (e.node_t
30b40 6f 5f 63 6f 64 65 20 3d 20 6e 2e 6e 6f 64 65 5f  o_code = n.node_
30b50 63 6f 64 65 29 5c 6e 22 29 3b 0a 20 20 20 20 73  code)\n");.    s
30b60 74 72 63 61 74 20 28 73 71 6c 2c 20 22 57 48 45  trcat (sql, "WHE
30b70 52 45 20 6e 2e 6e 6f 64 65 5f 69 64 20 49 53 20  RE n.node_id IS 
30b80 4e 55 4c 4c 5c 6e 22 29 3b 0a 20 20 20 20 72 65  NULL\n");.    re
30b90 74 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  t = sqlite3_exec
30ba0 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 4e   (sqlite, sql, N
30bb0 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 26 65 72 72 5f  ULL, NULL, &err_
30bc0 6d 73 67 29 3b 0a 20 20 20 20 69 66 20 28 72 65  msg);.    if (re
30bd0 74 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 0a  t != SQLITE_OK).
30be0 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69        {..  spati
30bf0 61 6c 69 74 65 5f 65 20 28 22 43 52 45 41 54 45  alite_e ("CREATE
30c00 20 56 49 45 57 20 27 25 73 27 20 65 72 72 6f 72   VIEW '%s' error
30c10 3a 20 25 73 5c 6e 22 2c 20 76 69 65 77 2c 20 65  : %s\n", view, e
30c20 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69  rr_msg);..  sqli
30c30 74 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73  te3_free (err_ms
30c40 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b  g);..  return 0;
30c50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74  .      }.    ret
30c60 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63  urn 1;.}..static
30c70 20 69 6e 74 0a 63 72 65 61 74 65 5f 74 6f 70 6f   int.create_topo
30c80 5f 6d 61 73 74 65 72 20 28 73 71 6c 69 74 65 33  _master (sqlite3
30c90 20 2a 20 73 71 6c 69 74 65 29 0a 7b 0a 2f 2a 20   * sqlite).{./* 
30ca0 63 72 65 61 74 69 6e 67 20 74 68 65 20 74 6f 70  creating the top
30cb0 6f 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a  o_master table *
30cc0 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c 5b 34  /.    char sql[4
30cd0 31 39 36 5d 3b 0a 20 20 20 20 69 6e 74 20 72 65  196];.    int re
30ce0 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72  t;.    char *err
30cf0 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 0a 2f 2a  _msg = NULL;../*
30d00 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 74 61   creating the ta
30d10 62 6c 65 20 2a 2f 0a 20 20 20 20 73 74 72 63 70  ble */.    strcp
30d20 79 20 28 73 71 6c 2c 20 22 43 52 45 41 54 45 20  y (sql, "CREATE 
30d30 54 41 42 4c 45 20 74 6f 70 6f 6c 6f 67 79 5f 6d  TABLE topology_m
30d40 61 73 74 65 72 20 28 5c 6e 22 29 3b 0a 20 20 20  aster (\n");.   
30d50 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 6e   strcat (sql, "n
30d60 6f 64 65 73 20 54 45 58 54 20 4e 4f 54 20 4e 55  odes TEXT NOT NU
30d70 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72  LL,\n");.    str
30d80 63 61 74 20 28 73 71 6c 2c 20 22 65 64 67 65 73  cat (sql, "edges
30d90 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c   TEXT NOT NULL,\
30da0 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
30db0 28 73 71 6c 2c 20 22 66 61 63 65 73 20 54 45 58  (sql, "faces TEX
30dc0 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b  T NOT NULL,\n");
30dd0 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
30de0 2c 20 22 66 61 63 65 73 5f 65 64 67 65 73 20 54  , "faces_edges T
30df0 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22  EXT NOT NULL,\n"
30e00 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
30e10 71 6c 2c 20 22 63 75 72 76 65 73 20 54 45 58 54  ql, "curves TEXT
30e20 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a   NOT NULL,\n");.
30e30 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
30e40 20 22 73 75 72 66 61 63 65 73 20 54 45 58 54 20   "surfaces TEXT 
30e50 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20  NOT NULL,\n");. 
30e60 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
30e70 22 63 68 65 63 6b 5f 6e 6f 64 65 5f 69 64 73 20  "check_node_ids 
30e80 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e  TEXT NOT NULL,\n
30e90 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  ");.    strcat (
30ea0 73 71 6c 2c 20 22 63 68 65 63 6b 5f 6e 6f 64 65  sql, "check_node
30eb0 5f 67 65 6f 6d 73 20 54 45 58 54 20 4e 4f 54 20  _geoms TEXT NOT 
30ec0 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  NULL,\n");.    s
30ed0 74 72 63 61 74 20 28 73 71 6c 2c 20 22 63 68 65  trcat (sql, "che
30ee0 63 6b 5f 65 64 67 65 5f 69 64 73 20 54 45 58 54  ck_edge_ids TEXT
30ef0 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a   NOT NULL,\n");.
30f00 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
30f10 20 22 63 68 65 63 6b 5f 65 64 67 65 5f 67 65 6f   "check_edge_geo
30f20 6d 73 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c  ms TEXT NOT NULL
30f30 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61  ,\n");.    strca
30f40 74 20 28 73 71 6c 2c 20 22 63 68 65 63 6b 5f 65  t (sql, "check_e
30f50 64 67 65 5f 6e 6f 64 65 5f 67 65 6f 6d 73 20 54  dge_node_geoms T
30f60 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22  EXT NOT NULL,\n"
30f70 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
30f80 71 6c 2c 20 22 63 68 65 63 6b 5f 66 61 63 65 5f  ql, "check_face_
30f90 69 64 73 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c  ids TEXT NOT NUL
30fa0 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73 74 72 63  L,\n");.    strc
30fb0 61 74 20 28 73 71 6c 2c 20 22 66 61 63 65 73 5f  at (sql, "faces_
30fc0 72 65 73 6f 6c 76 65 64 20 54 45 58 54 20 4e 4f  resolved TEXT NO
30fd0 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20  T NULL,\n");.   
30fe0 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 22 63   strcat (sql, "c
30ff0 75 72 76 65 73 5f 72 65 73 6f 6c 76 65 64 20 54  urves_resolved T
31000 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22  EXT NOT NULL,\n"
31010 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
31020 71 6c 2c 20 22 73 75 72 66 61 63 65 73 5f 72 65  ql, "surfaces_re
31030 73 6f 6c 76 65 64 20 54 45 58 54 20 4e 4f 54 20  solved TEXT NOT 
31040 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20 20 20 20 73  NULL,\n");.    s
31050 74 72 63 61 74 20 28 73 71 6c 2c 20 22 64 61 6e  trcat (sql, "dan
31060 67 6c 69 6e 67 5f 6e 6f 64 65 73 20 54 45 58 54  gling_nodes TEXT
31070 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a   NOT NULL,\n");.
31080 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
31090 20 22 64 61 6e 67 6c 69 6e 67 5f 65 64 67 65 73   "dangling_edges
310a0 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c   TEXT NOT NULL,\
310b0 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
310c0 28 73 71 6c 2c 20 22 63 68 65 63 6b 5f 65 64 67  (sql, "check_edg
310d0 65 73 5f 66 72 6f 6d 5f 74 6f 20 54 45 58 54 20  es_from_to TEXT 
310e0 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22 29 3b 0a 20  NOT NULL,\n");. 
310f0 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
31100 22 63 6f 6f 72 64 5f 64 69 6d 65 6e 73 69 6f 6e  "coord_dimension
31110 20 54 45 58 54 20 4e 4f 54 20 4e 55 4c 4c 2c 5c   TEXT NOT NULL,\
31120 6e 22 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  n");.    strcat 
31130 28 73 71 6c 2c 20 22 73 72 69 64 20 49 4e 54 45  (sql, "srid INTE
31140 47 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 5c 6e 22  GER NOT NULL,\n"
31150 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
31160 71 6c 2c 20 22 43 4f 4e 53 54 52 41 49 4e 54 20  ql, "CONSTRAINT 
31170 66 6b 5f 74 6f 70 6f 5f 6d 61 73 74 65 72 20 46  fk_topo_master F
31180 4f 52 45 49 47 4e 20 4b 45 59 20 5c 6e 22 29 3b  OREIGN KEY \n");
31190 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
311a0 2c 20 22 28 73 72 69 64 29 20 52 45 46 45 52 45  , "(srid) REFERE
311b0 4e 43 45 53 20 73 70 61 74 69 61 6c 5f 72 65 66  NCES spatial_ref
311c0 5f 73 79 73 20 28 73 72 69 64 29 29 5c 6e 22 29  _sys (srid))\n")
311d0 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
311e0 74 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65  te3_exec (sqlite
311f0 2c 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c  , sql, NULL, NUL
31200 4c 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20  L, &err_msg);.  
31210 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c    if (ret != SQL
31220 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a  ITE_OK).      {.
31230 09 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20  .  spatialite_e 
31240 28 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 27  ("CREATE TABLE '
31250 74 6f 70 6f 6c 6f 67 79 5f 6d 61 73 74 65 72 27  topology_master'
31260 20 65 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 65   error: %s\n", e
31270 72 72 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69  rr_msg);..  sqli
31280 74 65 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73  te3_free (err_ms
31290 67 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b  g);..  return 0;
312a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74  .      }.    ret
312b0 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63  urn 1;.}..static
312c0 20 69 6e 74 0a 75 70 64 61 74 65 5f 74 6f 70 6f   int.update_topo
312d0 5f 6d 61 73 74 65 72 20 28 73 71 6c 69 74 65 33  _master (sqlite3
312e0 20 2a 20 73 71 6c 69 74 65 2c 20 63 6f 6e 73 74   * sqlite, const
312f0 20 63 68 61 72 20 2a 6e 6f 64 65 73 2c 20 63 6f   char *nodes, co
31300 6e 73 74 20 63 68 61 72 20 2a 65 64 67 65 73 2c  nst char *edges,
31310 0a 09 09 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ...    const cha
31320 72 20 2a 66 61 63 65 73 2c 20 63 6f 6e 73 74 20  r *faces, const 
31330 63 68 61 72 20 2a 66 61 63 65 73 5f 65 64 67 65  char *faces_edge
31340 73 2c 0a 09 09 20 20 20 20 63 6f 6e 73 74 20 63  s,...    const c
31350 68 61 72 20 2a 63 75 72 76 65 73 2c 20 63 6f 6e  har *curves, con
31360 73 74 20 63 68 61 72 20 2a 73 75 72 66 61 63 65  st char *surface
31370 73 2c 0a 09 09 20 20 20 20 63 6f 6e 73 74 20 63  s,...    const c
31380 68 61 72 20 2a 63 68 65 63 6b 5f 6e 6f 64 65 73  har *check_nodes
31390 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 68  , const char *ch
313a0 65 63 6b 5f 6e 6f 64 65 5f 67 65 6f 6d 73 2c 0a  eck_node_geoms,.
313b0 09 09 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ..    const char
313c0 20 2a 63 68 65 63 6b 5f 65 64 67 65 73 2c 20 63   *check_edges, c
313d0 6f 6e 73 74 20 63 68 61 72 20 2a 63 68 65 63 6b  onst char *check
313e0 5f 65 64 67 65 5f 67 65 6f 6d 73 2c 0a 09 09 20  _edge_geoms,... 
313f0 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63     const char *c
31400 68 65 63 6b 5f 65 64 67 65 5f 6e 6f 64 65 5f 67  heck_edge_node_g
31410 65 6f 6d 73 2c 0a 09 09 20 20 20 20 63 6f 6e 73  eoms,...    cons
31420 74 20 63 68 61 72 20 2a 63 68 65 63 6b 5f 66 61  t char *check_fa
31430 63 65 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ces, const char 
31440 2a 66 61 63 65 73 5f 72 65 73 2c 0a 09 09 20 20  *faces_res,...  
31450 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 75    const char *cu
31460 72 76 65 73 5f 72 65 73 2c 20 63 6f 6e 73 74 20  rves_res, const 
31470 63 68 61 72 20 2a 73 75 72 66 61 63 65 73 5f 72  char *surfaces_r
31480 65 73 2c 0a 09 09 20 20 20 20 63 6f 6e 73 74 20  es,...    const 
31490 63 68 61 72 20 2a 64 61 6e 67 6c 69 6e 67 5f 6e  char *dangling_n
314a0 6f 64 65 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  odes, const char
314b0 20 2a 64 61 6e 67 6c 69 6e 67 5f 65 64 67 65 73   *dangling_edges
314c0 2c 0a 09 09 20 20 20 20 63 6f 6e 73 74 20 63 68  ,...    const ch
314d0 61 72 20 2a 63 68 65 63 6b 5f 65 64 67 65 73 5f  ar *check_edges_
314e0 66 72 6f 6d 5f 74 6f 2c 20 69 6e 74 20 73 72 69  from_to, int sri
314f0 64 2c 20 69 6e 74 20 64 69 6d 73 29 0a 7b 0a 2f  d, int dims).{./
31500 2a 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 74  * updating the t
31510 6f 70 6f 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  opo_master table
31520 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 71 6c   */.    char sql
31530 5b 34 31 39 36 5d 3b 0a 20 20 20 20 63 68 61 72  [4196];.    char
31540 20 73 71 6c 32 5b 32 30 34 38 5d 3b 0a 20 20 20   sql2[2048];.   
31550 20 63 68 61 72 20 73 71 6c 74 61 62 6c 65 5b 31   char sqltable[1
31560 30 32 34 5d 3b 0a 20 20 20 20 69 6e 74 20 72 65  024];.    int re
31570 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 65 72 72  t;.    char *err
31580 5f 6d 73 67 20 3d 20 4e 55 4c 4c 3b 0a 0a 2f 2a  _msg = NULL;../*
31590 20 69 6e 73 65 72 74 69 6e 67 20 54 6f 70 6f 6c   inserting Topol
315a0 6f 67 79 20 64 61 74 61 20 69 6e 74 6f 20 4d 41  ogy data into MA
315b0 53 54 45 52 20 2a 2f 0a 20 20 20 20 73 74 72 63  STER */.    strc
315c0 70 79 20 28 73 71 6c 2c 20 22 49 4e 53 45 52 54  py (sql, "INSERT
315d0 20 49 4e 54 4f 20 74 6f 70 6f 6c 6f 67 79 5f 6d   INTO topology_m
315e0 61 73 74 65 72 20 22 29 3b 0a 20 20 20 20 73 74  aster ");.    st
315f0 72 63 61 74 20 28 73 71 6c 2c 20 22 28 6e 6f 64  rcat (sql, "(nod
31600 65 73 2c 20 65 64 67 65 73 2c 20 66 61 63 65 73  es, edges, faces
31610 2c 20 66 61 63 65 73 5f 65 64 67 65 73 2c 20 22  , faces_edges, "
31620 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
31630 71 6c 2c 20 22 63 75 72 76 65 73 2c 20 73 75 72  ql, "curves, sur
31640 66 61 63 65 73 2c 20 63 68 65 63 6b 5f 6e 6f 64  faces, check_nod
31650 65 5f 69 64 73 2c 20 22 29 3b 0a 20 20 20 20 73  e_ids, ");.    s
31660 74 72 63 61 74 20 28 73 71 6c 2c 20 22 63 68 65  trcat (sql, "che
31670 63 6b 5f 6e 6f 64 65 5f 67 65 6f 6d 73 2c 20 63  ck_node_geoms, c
31680 68 65 63 6b 5f 65 64 67 65 5f 69 64 73 2c 20 22  heck_edge_ids, "
31690 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
316a0 71 6c 2c 20 22 63 68 65 63 6b 5f 65 64 67 65 5f  ql, "check_edge_
316b0 67 65 6f 6d 73 2c 20 63 68 65 63 6b 5f 65 64 67  geoms, check_edg
316c0 65 5f 6e 6f 64 65 5f 67 65 6f 6d 73 2c 20 22 29  e_node_geoms, ")
316d0 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
316e0 6c 2c 20 22 63 68 65 63 6b 5f 66 61 63 65 5f 69  l, "check_face_i
316f0 64 73 2c 20 66 61 63 65 73 5f 72 65 73 6f 6c 76  ds, faces_resolv
31700 65 64 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63  ed, ");.    strc
31710 61 74 20 28 73 71 6c 2c 20 22 63 75 72 76 65 73  at (sql, "curves
31720 5f 72 65 73 6f 6c 76 65 64 2c 20 73 75 72 66 61  _resolved, surfa
31730 63 65 73 5f 72 65 73 6f 6c 76 65 64 2c 20 22 29  ces_resolved, ")
31740 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71  ;.    strcat (sq
31750 6c 2c 20 22 64 61 6e 67 6c 69 6e 67 5f 6e 6f 64  l, "dangling_nod
31760 65 73 2c 20 64 61 6e 67 6c 69 6e 67 5f 65 64 67  es, dangling_edg
31770 65 73 2c 20 22 29 3b 0a 20 20 20 20 73 74 72 63  es, ");.    strc
31780 61 74 20 28 73 71 6c 2c 20 22 63 68 65 63 6b 5f  at (sql, "check_
31790 65 64 67 65 73 5f 66 72 6f 6d 5f 74 6f 2c 20 22  edges_from_to, "
317a0 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
317b0 71 6c 2c 20 22 63 6f 6f 72 64 5f 64 69 6d 65 6e  ql, "coord_dimen
317c0 73 69 6f 6e 2c 20 73 72 69 64 29 20 22 29 3b 0a  sion, srid) ");.
317d0 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c      strcat (sql,
317e0 20 22 56 41 4c 55 45 53 20 28 22 29 3b 0a 20 20   "VALUES (");.  
317f0 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62    strcpy (sqltab
31800 6c 65 2c 20 6e 6f 64 65 73 29 3b 0a 20 20 20 20  le, nodes);.    
31810 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67  clean_sql_string
31820 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20   (sqltable);.   
31830 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20   sprintf (sql2, 
31840 22 27 25 73 27 2c 20 22 2c 20 73 71 6c 74 61 62  "'%s', ", sqltab
31850 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20  le);.    strcat 
31860 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20  (sql, sql2);.   
31870 20 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c   strcpy (sqltabl
31880 65 2c 20 65 64 67 65 73 29 3b 0a 20 20 20 20 63  e, edges);.    c
31890 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20  lean_sql_string 
318a0 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20  (sqltable);.    
318b0 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22  sprintf (sql2, "
318c0 27 25 73 27 2c 20 22 2c 20 73 71 6c 74 61 62 6c  '%s', ", sqltabl
318d0 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28  e);.    strcat (
318e0 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20  sql, sql2);.    
318f0 73 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65  strcpy (sqltable
31900 2c 20 66 61 63 65 73 29 3b 0a 20 20 20 20 63 6c  , faces);.    cl
31910 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28  ean_sql_string (
31920 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
31930 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 27  printf (sql2, "'
31940 25 73 27 2c 20 22 2c 20 73 71 6c 74 61 62 6c 65  %s', ", sqltable
31950 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
31960 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73  ql, sql2);.    s
31970 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c  trcpy (sqltable,
31980 20 66 61 63 65 73 5f 65 64 67 65 73 29 3b 0a 20   faces_edges);. 
31990 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72     clean_sql_str
319a0 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a  ing (sqltable);.
319b0 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
319c0 32 2c 20 22 27 25 73 27 2c 20 22 2c 20 73 71 6c  2, "'%s', ", sql
319d0 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63  table);.    strc
319e0 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a  at (sql, sql2);.
319f0 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74      strcpy (sqlt
31a00 61 62 6c 65 2c 20 63 75 72 76 65 73 29 3b 0a 20  able, curves);. 
31a10 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72     clean_sql_str
31a20 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a  ing (sqltable);.
31a30 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
31a40 32 2c 20 22 27 25 73 27 2c 20 22 2c 20 73 71 6c  2, "'%s', ", sql
31a50 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63  table);.    strc
31a60 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a  at (sql, sql2);.
31a70 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74      strcpy (sqlt
31a80 61 62 6c 65 2c 20 73 75 72 66 61 63 65 73 29 3b  able, surfaces);
31a90 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73  .    clean_sql_s
31aa0 74 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29  tring (sqltable)
31ab0 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73  ;.    sprintf (s
31ac0 71 6c 32 2c 20 22 27 25 73 27 2c 20 22 2c 20 73  ql2, "'%s', ", s
31ad0 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74  qltable);.    st
31ae0 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29  rcat (sql, sql2)
31af0 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71  ;.    strcpy (sq
31b00 6c 74 61 62 6c 65 2c 20 63 68 65 63 6b 5f 6e 6f  ltable, check_no
31b10 64 65 73 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f  des);.    clean_
31b20 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74  sql_string (sqlt
31b30 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e  able);.    sprin
31b40 74 66 20 28 73 71 6c 32 2c 20 22 27 25 73 27 2c  tf (sql2, "'%s',
31b50 20 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20   ", sqltable);. 
31b60 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
31b70 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 70  sql2);.    strcp
31b80 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 63 68 65  y (sqltable, che
31b90 63 6b 5f 6e 6f 64 65 5f 67 65 6f 6d 73 29 3b 0a  ck_node_geoms);.
31ba0 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74      clean_sql_st
31bb0 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b  ring (sqltable);
31bc0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
31bd0 6c 32 2c 20 22 27 25 73 27 2c 20 22 2c 20 73 71  l2, "'%s', ", sq
31be0 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
31bf0 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
31c00 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
31c10 74 61 62 6c 65 2c 20 63 68 65 63 6b 5f 65 64 67  table, check_edg
31c20 65 73 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73  es);.    clean_s
31c30 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74 61  ql_string (sqlta
31c40 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74  ble);.    sprint
31c50 66 20 28 73 71 6c 32 2c 20 22 27 25 73 27 2c 20  f (sql2, "'%s', 
31c60 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  ", sqltable);.  
31c70 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73    strcat (sql, s
31c80 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 70 79  ql2);.    strcpy
31c90 20 28 73 71 6c 74 61 62 6c 65 2c 20 63 68 65 63   (sqltable, chec
31ca0 6b 5f 65 64 67 65 5f 67 65 6f 6d 73 29 3b 0a 20  k_edge_geoms);. 
31cb0 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72     clean_sql_str
31cc0 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a  ing (sqltable);.
31cd0 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c      sprintf (sql
31ce0 32 2c 20 22 27 25 73 27 2c 20 22 2c 20 73 71 6c  2, "'%s', ", sql
31cf0 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63  table);.    strc
31d00 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a  at (sql, sql2);.
31d10 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74      strcpy (sqlt
31d20 61 62 6c 65 2c 20 63 68 65 63 6b 5f 65 64 67 65  able, check_edge
31d30 5f 6e 6f 64 65 5f 67 65 6f 6d 73 29 3b 0a 20 20  _node_geoms);.  
31d40 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69    clean_sql_stri
31d50 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20  ng (sqltable);. 
31d60 20 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32     sprintf (sql2
31d70 2c 20 22 27 25 73 27 2c 20 22 2c 20 73 71 6c 74  , "'%s', ", sqlt
31d80 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61  able);.    strca
31d90 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20  t (sql, sql2);. 
31da0 20 20 20 73 74 72 63 70 79 20 28 73 71 6c 74 61     strcpy (sqlta
31db0 62 6c 65 2c 20 63 68 65 63 6b 5f 66 61 63 65 73  ble, check_faces
31dc0 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c  );.    clean_sql
31dd0 5f 73 74 72 69 6e 67 20 28 73 71 6c 74 61 62 6c  _string (sqltabl
31de0 65 29 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20  e);.    sprintf 
31df0 28 73 71 6c 32 2c 20 22 27 25 73 27 2c 20 22 2c  (sql2, "'%s', ",
31e00 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20   sqltable);.    
31e10 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c  strcat (sql, sql
31e20 32 29 3b 0a 20 20 20 20 73 74 72 63 70 79 20 28  2);.    strcpy (
31e30 73 71 6c 74 61 62 6c 65 2c 20 66 61 63 65 73 5f  sqltable, faces_
31e40 72 65 73 29 3b 0a 20 20 20 20 63 6c 65 61 6e 5f  res);.    clean_
31e50 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71 6c 74  sql_string (sqlt
31e60 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72 69 6e  able);.    sprin
31e70 74 66 20 28 73 71 6c 32 2c 20 22 27 25 73 27 2c  tf (sql2, "'%s',
31e80 20 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b 0a 20   ", sqltable);. 
31e90 20 20 20 73 74 72 63 61 74 20 28 73 71 6c 2c 20     strcat (sql, 
31ea0 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72 63 70  sql2);.    strcp
31eb0 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 63 75 72  y (sqltable, cur
31ec0 76 65 73 5f 72 65 73 29 3b 0a 20 20 20 20 63 6c  ves_res);.    cl
31ed0 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28  ean_sql_string (
31ee0 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73  sqltable);.    s
31ef0 70 72 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 27  printf (sql2, "'
31f00 25 73 27 2c 20 22 2c 20 73 71 6c 74 61 62 6c 65  %s', ", sqltable
31f10 29 3b 0a 20 20 20 20 73 74 72 63 61 74 20 28 73  );.    strcat (s
31f20 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73  ql, sql2);.    s
31f30 74 72 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c  trcpy (sqltable,
31f40 20 73 75 72 66 61 63 65 73 5f 72 65 73 29 3b 0a   surfaces_res);.
31f50 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74      clean_sql_st
31f60 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b  ring (sqltable);
31f70 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
31f80 6c 32 2c 20 22 27 25 73 27 2c 20 22 2c 20 73 71  l2, "'%s', ", sq
31f90 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
31fa0 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
31fb0 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
31fc0 74 61 62 6c 65 2c 20 64 61 6e 67 6c 69 6e 67 5f  table, dangling_
31fd0 6e 6f 64 65 73 29 3b 0a 20 20 20 20 63 6c 65 61  nodes);.    clea
31fe0 6e 5f 73 71 6c 5f 73 74 72 69 6e 67 20 28 73 71  n_sql_string (sq
31ff0 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 70 72  ltable);.    spr
32000 69 6e 74 66 20 28 73 71 6c 32 2c 20 22 27 25 73  intf (sql2, "'%s
32010 27 2c 20 22 2c 20 73 71 6c 74 61 62 6c 65 29 3b  ', ", sqltable);
32020 0a 20 20 20 20 73 74 72 63 61 74 20 28 73 71 6c  .    strcat (sql
32030 2c 20 73 71 6c 32 29 3b 0a 20 20 20 20 73 74 72  , sql2);.    str
32040 63 70 79 20 28 73 71 6c 74 61 62 6c 65 2c 20 64  cpy (sqltable, d
32050 61 6e 67 6c 69 6e 67 5f 65 64 67 65 73 29 3b 0a  angling_edges);.
32060 20 20 20 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74      clean_sql_st
32070 72 69 6e 67 20 28 73 71 6c 74 61 62 6c 65 29 3b  ring (sqltable);
32080 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
32090 6c 32 2c 20 22 27 25 73 27 2c 20 22 2c 20 73 71  l2, "'%s', ", sq
320a0 6c 74 61 62 6c 65 29 3b 0a 20 20 20 20 73 74 72  ltable);.    str
320b0 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
320c0 0a 20 20 20 20 73 74 72 63 70 79 20 28 73 71 6c  .    strcpy (sql
320d0 74 61 62 6c 65 2c 20 63 68 65 63 6b 5f 65 64 67  table, check_edg
320e0 65 73 5f 66 72 6f 6d 5f 74 6f 29 3b 0a 20 20 20  es_from_to);.   
320f0 20 63 6c 65 61 6e 5f 73 71 6c 5f 73 74 72 69 6e   clean_sql_strin
32100 67 20 28 73 71 6c 74 61 62 6c 65 29 3b 0a 20 20  g (sqltable);.  
32110 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c    sprintf (sql2,
32120 20 22 27 25 73 27 2c 20 22 2c 20 73 71 6c 74 61   "'%s', ", sqlta
32130 62 6c 65 29 3b 0a 20 20 20 20 73 74 72 63 61 74  ble);.    strcat
32140 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b 0a 20 20   (sql, sql2);.  
32150 20 20 73 70 72 69 6e 74 66 20 28 73 71 6c 32 2c    sprintf (sql2,
32160 20 22 27 25 73 27 2c 20 25 64 29 22 2c 20 28 64   "'%s', %d)", (d
32170 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a  ims == GAIA_XY_Z
32180 29 20 3f 20 22 58 59 5a 22 20 3a 20 22 58 59 22  ) ? "XYZ" : "XY"
32190 2c 20 73 72 69 64 29 3b 0a 20 20 20 20 73 74 72  , srid);.    str
321a0 63 61 74 20 28 73 71 6c 2c 20 73 71 6c 32 29 3b  cat (sql, sql2);
321b0 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69 74  .    ret = sqlit
321c0 65 33 5f 65 78 65 63 20 28 73 71 6c 69 74 65 2c  e3_exec (sqlite,
321d0 20 73 71 6c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c   sql, NULL, NULL
321e0 2c 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20 20 20  , &err_msg);.   
321f0 20 69 66 20 28 72 65 74 20 21 3d 20 53 51 4c 49   if (ret != SQLI
32200 54 45 5f 4f 4b 29 0a 20 20 20 20 20 20 7b 0a 09  TE_OK).      {..
32210 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28    spatialite_e (
32220 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 27 74 6f  "INSERT INTO 'to
32230 70 6f 6c 6f 67 79 5f 6d 61 73 74 65 72 27 20 65  pology_master' e
32240 72 72 6f 72 3a 20 25 73 5c 6e 22 2c 20 65 72 72  rror: %s\n", err
32250 5f 6d 73 67 29 3b 0a 09 20 20 73 71 6c 69 74 65  _msg);..  sqlite
32260 33 5f 66 72 65 65 20 28 65 72 72 5f 6d 73 67 29  3_free (err_msg)
32270 3b 0a 09 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  ;..  return 0;. 
32280 20 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72       }.    retur
32290 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  n 1;.}..static v
322a0 6f 69 64 0a 66 6e 63 74 5f 43 72 65 61 74 65 54  oid.fnct_CreateT
322b0 6f 70 6f 6c 6f 67 79 54 61 62 6c 65 73 20 28 73  opologyTables (s
322c0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
322d0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
322e0 67 63 2c 0a 09 09 09 20 20 20 73 71 6c 69 74 65  gc,....   sqlite
322f0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
32300 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
32310 6f 6e 3a 0a 2f 20 43 72 65 61 74 65 54 6f 70 6f  on:./ CreateTopo
32320 6c 6f 67 79 54 61 62 6c 65 73 28 73 72 69 64 2c  logyTables(srid,
32330 20 63 6f 6f 72 64 5f 64 69 6d 73 29 0a 2f 20 20   coord_dims)./  
32340 6f 72 0a 2f 20 43 72 65 61 74 65 54 6f 70 6f 6c  or./ CreateTopol
32350 6f 67 79 54 61 62 6c 65 73 28 70 72 65 66 69 78  ogyTables(prefix
32360 2c 20 73 72 69 64 2c 20 63 6f 6f 72 64 5f 64 69  , srid, coord_di
32370 6d 73 29 0a 2f 0a 2f 20 63 72 65 61 74 65 73 20  ms)././ creates 
32380 61 6e 79 20 54 6f 70 6f 6c 6f 67 79 20 72 65 6c  any Topology rel
32390 61 74 65 64 20 74 61 62 6c 65 20 0a 2f 20 72 65  ated table ./ re
323a0 74 75 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65  turns 1 on succe
323b0 73 73 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72  ss./ 0 on failur
323c0 65 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63  e.*/.    const c
323d0 68 61 72 20 2a 70 72 65 66 69 78 20 3d 20 22 74  har *prefix = "t
323e0 6f 70 6f 5f 22 3b 0a 20 20 20 20 63 6f 6e 73 74  opo_";.    const
323f0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
32400 74 78 74 5f 64 69 6d 73 3b 0a 20 20 20 20 69 6e  txt_dims;.    in
32410 74 20 73 72 69 64 20 3d 20 2d 31 3b 0a 20 20 20  t srid = -1;.   
32420 20 69 6e 74 20 64 69 6d 65 6e 73 69 6f 6e 3b 0a   int dimension;.
32430 20 20 20 20 69 6e 74 20 64 69 6d 73 20 3d 20 2d      int dims = -
32440 31 3b 0a 20 20 20 20 63 68 61 72 20 74 61 62 6c  1;.    char tabl
32450 65 5f 63 75 72 76 65 73 5b 31 30 32 34 5d 3b 0a  e_curves[1024];.
32460 20 20 20 20 63 68 61 72 20 74 61 62 6c 65 5f 73      char table_s
32470 75 72 66 61 63 65 73 5b 31 30 32 34 5d 3b 0a 20  urfaces[1024];. 
32480 20 20 20 63 68 61 72 20 74 61 62 6c 65 5f 6e 6f     char table_no
32490 64 65 73 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63  des[1024];.    c
324a0 68 61 72 20 74 61 62 6c 65 5f 65 64 67 65 73 5b  har table_edges[
324b0 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
324c0 74 61 62 6c 65 5f 66 61 63 65 73 5b 31 30 32 34  table_faces[1024
324d0 5d 3b 0a 20 20 20 20 63 68 61 72 20 74 61 62 6c  ];.    char tabl
324e0 65 5f 66 61 63 65 73 5f 65 64 67 65 73 5b 31 30  e_faces_edges[10
324f0 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 76 69  24];.    char vi
32500 65 77 5f 63 68 65 63 6b 5f 6e 6f 64 65 5f 63 6f  ew_check_node_co
32510 64 65 73 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63  des[1024];.    c
32520 68 61 72 20 76 69 65 77 5f 63 68 65 63 6b 5f 6e  har view_check_n
32530 6f 64 65 5f 67 65 6f 6d 73 5b 31 30 32 34 5d 3b  ode_geoms[1024];
32540 0a 20 20 20 20 63 68 61 72 20 76 69 65 77 5f 63  .    char view_c
32550 68 65 63 6b 5f 65 64 67 65 5f 63 6f 64 65 73 5b  heck_edge_codes[
32560 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
32570 76 69 65 77 5f 63 68 65 63 6b 5f 65 64 67 65 5f  view_check_edge_
32580 67 65 6f 6d 73 5b 31 30 32 34 5d 3b 0a 20 20 20  geoms[1024];.   
32590 20 63 68 61 72 20 76 69 65 77 5f 63 68 65 63 6b   char view_check
325a0 5f 65 64 67 65 5f 6e 6f 64 65 5f 67 65 6f 6d 73  _edge_node_geoms
325b0 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72  [1024];.    char
325c0 20 76 69 65 77 5f 63 68 65 63 6b 5f 66 61 63 65   view_check_face
325d0 5f 63 6f 64 65 73 5b 31 30 32 34 5d 3b 0a 20 20  _codes[1024];.  
325e0 20 20 63 68 61 72 20 76 69 65 77 5f 66 61 63 65    char view_face
325f0 73 5f 72 65 73 6f 6c 76 65 64 5b 31 30 32 34 5d  s_resolved[1024]
32600 3b 0a 20 20 20 20 63 68 61 72 20 76 69 65 77 5f  ;.    char view_
32610 63 75 72 76 65 73 5f 72 65 73 6f 6c 76 65 64 5b  curves_resolved[
32620 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20  1024];.    char 
32630 76 69 65 77 5f 73 75 72 66 61 63 65 73 5f 72 65  view_surfaces_re
32640 73 6f 6c 76 65 64 5b 31 30 32 34 5d 3b 0a 20 20  solved[1024];.  
32650 20 20 63 68 61 72 20 76 69 65 77 5f 64 61 6e 67    char view_dang
32660 6c 69 6e 67 5f 6e 6f 64 65 73 5b 31 30 32 34 5d  ling_nodes[1024]
32670 3b 0a 20 20 20 20 63 68 61 72 20 76 69 65 77 5f  ;.    char view_
32680 64 61 6e 67 6c 69 6e 67 5f 65 64 67 65 73 5b 31  dangling_edges[1
32690 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72 20 76  024];.    char v
326a0 69 65 77 5f 65 64 67 65 73 5f 63 68 65 63 6b 5f  iew_edges_check_
326b0 66 72 6f 6d 5f 74 6f 5b 31 30 32 34 5d 3b 0a 20  from_to[1024];. 
326c0 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74     const char *t
326d0 61 62 6c 65 73 5b 32 30 5d 3b 0a 20 20 20 20 69  ables[20];.    i
326e0 6e 74 20 76 69 65 77 73 5b 32 30 5d 3b 0a 20 20  nt views[20];.  
326f0 20 20 69 6e 74 20 2a 70 5f 76 69 65 77 3b 0a 20    int *p_view;. 
32700 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a     const char **
32710 70 5f 74 62 6c 3b 0a 20 20 20 20 69 6e 74 20 6f  p_tbl;.    int o
32720 6b 5f 74 61 62 6c 65 3b 0a 20 20 20 20 69 6e 74  k_table;.    int
32730 20 63 72 65 61 74 65 5f 6d 61 73 74 65 72 20 3d   create_master =
32740 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   1;.    sqlite3 
32750 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65  *sqlite = sqlite
32760 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
32770 64 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  dle (context);. 
32780 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
32790 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
327a0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
327b0 28 61 72 67 63 20 3d 3d 20 33 29 0a 20 20 20 20  (argc == 3).    
327c0 20 20 7b 0a 09 20 20 69 66 20 28 73 71 6c 69 74    {..  if (sqlit
327d0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
327e0 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[0]) != SQLIT
327f0 45 5f 54 45 58 54 29 0a 09 20 20 20 20 7b 0a 09  E_TEXT)..    {..
32800 09 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 09  .spatialite_e...
32810 20 20 20 20 28 22 43 72 65 61 74 65 54 6f 70 6f      ("CreateTopo
32820 6c 6f 67 79 54 61 62 6c 65 73 28 29 20 65 72 72  logyTables() err
32830 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 31 20 5b  or: argument 1 [
32840 74 61 62 6c 65 5f 70 72 65 66 69 78 5d 20 69 73  table_prefix] is
32850 20 6e 6f 74 20 6f 66 20 74 68 65 20 53 74 72 69   not of the Stri
32860 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a 09 09 73  ng type\n");...s
32870 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
32880 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  t (context, 0);.
32890 09 09 72 65 74 75 72 6e 3b 0a 09 20 20 20 20 7d  ..return;..    }
328a0 0a 09 20 20 70 72 65 66 69 78 20 3d 20 28 63 68  ..  prefix = (ch
328b0 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
328c0 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 30  lue_text (argv[0
328d0 5d 29 3b 0a 09 20 20 69 66 20 28 73 71 6c 69 74  ]);..  if (sqlit
328e0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
328f0 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[1]) != SQLIT
32900 45 5f 49 4e 54 45 47 45 52 29 0a 09 20 20 20 20  E_INTEGER)..    
32910 7b 0a 09 09 73 70 61 74 69 61 6c 69 74 65 5f 65  {...spatialite_e
32920 0a 09 09 20 20 20 20 28 22 43 72 65 61 74 65 54  ...    ("CreateT
32930 6f 70 6f 6c 6f 67 79 54 61 62 6c 65 73 28 29 20  opologyTables() 
32940 65 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20  error: argument 
32950 32 20 5b 53 52 49 44 5d 20 69 73 20 6e 6f 74 20  2 [SRID] is not 
32960 6f 66 20 74 68 65 20 49 6e 74 65 67 65 72 20 74  of the Integer t
32970 79 70 65 5c 6e 22 29 3b 0a 09 09 73 71 6c 69 74  ype\n");...sqlit
32980 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
32990 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 09 72 65  ontext, 0);...re
329a0 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20  turn;..    }..  
329b0 73 72 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76  srid = sqlite3_v
329c0 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31  alue_int (argv[1
329d0 5d 29 3b 0a 09 20 20 69 66 20 28 73 71 6c 69 74  ]);..  if (sqlit
329e0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
329f0 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[2]) == SQLIT
32a00 45 5f 49 4e 54 45 47 45 52 29 0a 09 20 20 20 20  E_INTEGER)..    
32a10 7b 0a 09 09 64 69 6d 65 6e 73 69 6f 6e 20 3d 20  {...dimension = 
32a20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
32a30 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 09 69  t (argv[2]);...i
32a40 66 20 28 64 69 6d 65 6e 73 69 6f 6e 20 3d 3d 20  f (dimension == 
32a50 32 29 0a 09 09 20 20 20 20 64 69 6d 73 20 3d 20  2)...    dims = 
32a60 47 41 49 41 5f 58 59 3b 0a 09 09 69 66 20 28 64  GAIA_XY;...if (d
32a70 69 6d 65 6e 73 69 6f 6e 20 3d 3d 20 33 29 0a 09  imension == 3)..
32a80 09 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41  .    dims = GAIA
32a90 5f 58 59 5f 5a 3b 0a 09 20 20 20 20 7d 0a 09 20  _XY_Z;..    }.. 
32aa0 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
32ab0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
32ac0 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[2]) == SQLITE
32ad0 5f 54 45 58 54 29 0a 09 20 20 20 20 7b 0a 09 09  _TEXT)..    {...
32ae0 74 78 74 5f 64 69 6d 73 20 3d 20 73 71 6c 69 74  txt_dims = sqlit
32af0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61  e3_value_text (a
32b00 72 67 76 5b 32 5d 29 3b 0a 09 09 69 66 20 28 73  rgv[2]);...if (s
32b10 74 72 63 61 73 65 63 6d 70 20 28 28 63 68 61 72  trcasecmp ((char
32b20 20 2a 29 20 74 78 74 5f 64 69 6d 73 2c 20 22 58   *) txt_dims, "X
32b30 59 22 29 20 3d 3d 20 30 29 0a 09 09 20 20 20 20  Y") == 0)...    
32b40 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59 3b 0a  dims = GAIA_XY;.
32b50 09 09 69 66 20 28 73 74 72 63 61 73 65 63 6d 70  ..if (strcasecmp
32b60 20 28 28 63 68 61 72 20 2a 29 20 74 78 74 5f 64   ((char *) txt_d
32b70 69 6d 73 2c 20 22 58 59 5a 22 29 20 3d 3d 20 30  ims, "XYZ") == 0
32b80 29 0a 09 09 20 20 20 20 64 69 6d 73 20 3d 20 47  )...    dims = G
32b90 41 49 41 5f 58 59 5f 5a 3b 0a 09 20 20 20 20 7d  AIA_XY_Z;..    }
32ba0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
32bb0 09 09 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09  ..spatialite_e..
32bc0 09 20 20 20 20 28 22 43 72 65 61 74 65 54 6f 70  .    ("CreateTop
32bd0 6f 6c 6f 67 79 54 61 62 6c 65 73 28 29 20 65 72  ologyTables() er
32be0 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 33 20  ror: argument 3 
32bf0 5b 64 69 6d 65 6e 73 69 6f 6e 5d 20 69 73 20 6e  [dimension] is n
32c00 6f 74 20 6f 66 20 74 68 65 20 49 6e 74 65 67 65  ot of the Intege
32c10 72 20 6f 72 20 54 65 78 74 20 74 79 70 65 5c 6e  r or Text type\n
32c20 22 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  ");...sqlite3_re
32c30 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
32c40 74 2c 20 30 29 3b 0a 09 09 72 65 74 75 72 6e 3b  t, 0);...return;
32c50 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a  ..    }.      }.
32c60 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
32c70 0a 09 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f  ..  if (sqlite3_
32c80 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
32c90 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49  [0]) != SQLITE_I
32ca0 4e 54 45 47 45 52 29 0a 09 20 20 20 20 7b 0a 09  NTEGER)..    {..
32cb0 09 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 09  .spatialite_e...
32cc0 20 20 20 20 28 22 43 72 65 61 74 65 54 6f 70 6f      ("CreateTopo
32cd0 6c 6f 67 79 54 61 62 6c 65 73 28 29 20 65 72 72  logyTables() err
32ce0 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 31 20 5b  or: argument 1 [
32cf0 53 52 49 44 5d 20 69 73 20 6e 6f 74 20 6f 66 20  SRID] is not of 
32d00 74 68 65 20 49 6e 74 65 67 65 72 20 74 79 70 65  the Integer type
32d10 5c 6e 22 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f  \n");...sqlite3_
32d20 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
32d30 65 78 74 2c 20 30 29 3b 0a 09 09 72 65 74 75 72  ext, 0);...retur
32d40 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20 73 72 69  n;..    }..  sri
32d50 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
32d60 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b  e_int (argv[0]);
32d70 0a 09 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f  ..  if (sqlite3_
32d80 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
32d90 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [1]) == SQLITE_I
32da0 4e 54 45 47 45 52 29 0a 09 20 20 20 20 7b 0a 09  NTEGER)..    {..
32db0 09 64 69 6d 65 6e 73 69 6f 6e 20 3d 20 73 71 6c  .dimension = sql
32dc0 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
32dd0 61 72 67 76 5b 31 5d 29 3b 0a 09 09 69 66 20 28  argv[1]);...if (
32de0 64 69 6d 65 6e 73 69 6f 6e 20 3d 3d 20 32 29 0a  dimension == 2).
32df0 09 09 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49  ..    dims = GAI
32e00 41 5f 58 59 3b 0a 09 09 69 66 20 28 64 69 6d 65  A_XY;...if (dime
32e10 6e 73 69 6f 6e 20 3d 3d 20 33 29 0a 09 09 20 20  nsion == 3)...  
32e20 20 20 64 69 6d 73 20 3d 20 47 41 49 41 5f 58 59    dims = GAIA_XY
32e30 5f 5a 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c  _Z;..    }..  el
32e40 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  se if (sqlite3_v
32e50 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
32e60 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 54 45  1]) == SQLITE_TE
32e70 58 54 29 0a 09 20 20 20 20 7b 0a 09 09 74 78 74  XT)..    {...txt
32e80 5f 64 69 6d 73 20 3d 20 73 71 6c 69 74 65 33 5f  _dims = sqlite3_
32e90 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76  value_text (argv
32ea0 5b 31 5d 29 3b 0a 09 09 69 66 20 28 73 74 72 63  [1]);...if (strc
32eb0 61 73 65 63 6d 70 20 28 28 63 68 61 72 20 2a 29  asecmp ((char *)
32ec0 20 74 78 74 5f 64 69 6d 73 2c 20 22 58 59 22 29   txt_dims, "XY")
32ed0 20 3d 3d 20 30 29 0a 09 09 20 20 20 20 64 69 6d   == 0)...    dim
32ee0 73 20 3d 20 47 41 49 41 5f 58 59 3b 0a 09 09 69  s = GAIA_XY;...i
32ef0 66 20 28 73 74 72 63 61 73 65 63 6d 70 20 28 28  f (strcasecmp ((
32f00 63 68 61 72 20 2a 29 20 74 78 74 5f 64 69 6d 73  char *) txt_dims
32f10 2c 20 22 58 59 5a 22 29 20 3d 3d 20 30 29 0a 09  , "XYZ") == 0)..
32f20 09 20 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41  .    dims = GAIA
32f30 5f 58 59 5f 5a 3b 0a 09 20 20 20 20 7d 0a 09 20  _XY_Z;..    }.. 
32f40 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 73   else..    {...s
32f50 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 09 20 20  patialite_e...  
32f60 20 20 28 22 43 72 65 61 74 65 54 6f 70 6f 6c 6f    ("CreateTopolo
32f70 67 79 54 61 62 6c 65 73 28 29 20 65 72 72 6f 72  gyTables() error
32f80 3a 20 61 72 67 75 6d 65 6e 74 20 32 20 5b 64 69  : argument 2 [di
32f90 6d 65 6e 73 69 6f 6e 5d 20 69 73 20 6e 6f 74 20  mension] is not 
32fa0 6f 66 20 74 68 65 20 49 6e 74 65 67 65 72 20 6f  of the Integer o
32fb0 72 20 54 65 78 74 20 74 79 70 65 5c 6e 22 29 3b  r Text type\n");
32fc0 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ...sqlite3_resul
32fd0 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
32fe0 30 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20  0);...return;.. 
32ff0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
33000 20 69 66 20 28 64 69 6d 73 20 3d 3d 20 47 41 49   if (dims == GAI
33010 41 5f 58 59 20 7c 7c 20 64 69 6d 73 20 3d 3d 20  A_XY || dims == 
33020 47 41 49 41 5f 58 59 5f 5a 29 0a 09 3b 0a 20 20  GAIA_XY_Z)..;.  
33030 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
33040 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09    spatialite_e..
33050 20 20 20 20 20 20 28 22 43 72 65 61 74 65 54 6f        ("CreateTo
33060 70 6f 6c 6f 67 79 54 61 62 6c 65 73 28 29 20 65  pologyTables() e
33070 72 72 6f 72 3a 20 5b 64 69 6d 65 6e 73 69 6f 6e  rror: [dimension
33080 5d 20 49 4c 4c 45 47 41 4c 20 56 41 4c 55 45 5c  ] ILLEGAL VALUE\
33090 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  n");..  sqlite3_
330a0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
330b0 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75  ext, 0);..  retu
330c0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
330d0 69 66 20 28 73 72 69 64 20 3c 3d 20 30 29 0a 20  if (srid <= 0). 
330e0 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74 69 61       {..  spatia
330f0 6c 69 74 65 5f 65 20 28 22 43 72 65 61 74 65 54  lite_e ("CreateT
33100 6f 70 6f 6c 6f 67 79 54 61 62 6c 65 73 28 29 20  opologyTables() 
33110 65 72 72 6f 72 3a 20 5b 53 52 49 44 5d 20 49 4c  error: [SRID] IL
33120 4c 45 47 41 4c 20 56 41 4c 55 45 5c 6e 22 29 3b  LEGAL VALUE\n");
33130 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
33140 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
33150 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a   0);..  return;.
33160 20 20 20 20 20 20 7d 0a 0a 2f 2a 20 63 68 65 63        }../* chec
33170 6b 69 6e 67 20 54 6f 70 6f 6c 6f 67 79 20 74 61  king Topology ta
33180 62 6c 65 73 20 2a 2f 0a 20 20 20 20 74 61 62 6c  bles */.    tabl
33190 65 73 5b 30 5d 20 3d 20 22 74 6f 70 6f 6c 6f 67  es[0] = "topolog
331a0 79 5f 6d 61 73 74 65 72 22 3b 0a 20 20 20 20 76  y_master";.    v
331b0 69 65 77 73 5b 30 5d 20 3d 20 30 3b 0a 20 20 20  iews[0] = 0;.   
331c0 20 73 70 72 69 6e 74 66 20 28 74 61 62 6c 65 5f   sprintf (table_
331d0 63 75 72 76 65 73 2c 20 22 25 73 63 75 72 76 65  curves, "%scurve
331e0 73 22 2c 20 70 72 65 66 69 78 29 3b 0a 20 20 20  s", prefix);.   
331f0 20 74 61 62 6c 65 73 5b 31 5d 20 3d 20 74 61 62   tables[1] = tab
33200 6c 65 5f 63 75 72 76 65 73 3b 0a 20 20 20 20 76  le_curves;.    v
33210 69 65 77 73 5b 31 5d 20 3d 20 30 3b 0a 20 20 20  iews[1] = 0;.   
33220 20 73 70 72 69 6e 74 66 20 28 74 61 62 6c 65 5f   sprintf (table_
33230 73 75 72 66 61 63 65 73 2c 20 22 25 73 73 75 72  surfaces, "%ssur
33240 66 61 63 65 73 22 2c 20 70 72 65 66 69 78 29 3b  faces", prefix);
33250 0a 20 20 20 20 74 61 62 6c 65 73 5b 32 5d 20 3d  .    tables[2] =
33260 20 74 61 62 6c 65 5f 73 75 72 66 61 63 65 73 3b   table_surfaces;
33270 0a 20 20 20 20 76 69 65 77 73 5b 32 5d 20 3d 20  .    views[2] = 
33280 30 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  0;.    sprintf (
33290 74 61 62 6c 65 5f 6e 6f 64 65 73 2c 20 22 25 73  table_nodes, "%s
332a0 6e 6f 64 65 73 22 2c 20 70 72 65 66 69 78 29 3b  nodes", prefix);
332b0 0a 20 20 20 20 74 61 62 6c 65 73 5b 33 5d 20 3d  .    tables[3] =
332c0 20 74 61 62 6c 65 5f 6e 6f 64 65 73 3b 0a 20 20   table_nodes;.  
332d0 20 20 76 69 65 77 73 5b 33 5d 20 3d 20 30 3b 0a    views[3] = 0;.
332e0 20 20 20 20 73 70 72 69 6e 74 66 20 28 74 61 62      sprintf (tab
332f0 6c 65 5f 65 64 67 65 73 2c 20 22 25 73 65 64 67  le_edges, "%sedg
33300 65 73 22 2c 20 70 72 65 66 69 78 29 3b 0a 20 20  es", prefix);.  
33310 20 20 74 61 62 6c 65 73 5b 34 5d 20 3d 20 74 61    tables[4] = ta
33320 62 6c 65 5f 65 64 67 65 73 3b 0a 20 20 20 20 76  ble_edges;.    v
33330 69 65 77 73 5b 34 5d 20 3d 20 30 3b 0a 20 20 20  iews[4] = 0;.   
33340 20 73 70 72 69 6e 74 66 20 28 74 61 62 6c 65 5f   sprintf (table_
33350 66 61 63 65 73 2c 20 22 25 73 66 61 63 65 73 22  faces, "%sfaces"
33360 2c 20 70 72 65 66 69 78 29 3b 0a 20 20 20 20 74  , prefix);.    t
33370 61 62 6c 65 73 5b 35 5d 20 3d 20 74 61 62 6c 65  ables[5] = table
33380 5f 66 61 63 65 73 3b 0a 20 20 20 20 76 69 65 77  _faces;.    view
33390 73 5b 35 5d 20 3d 20 30 3b 0a 20 20 20 20 73 70  s[5] = 0;.    sp
333a0 72 69 6e 74 66 20 28 74 61 62 6c 65 5f 66 61 63  rintf (table_fac
333b0 65 73 5f 65 64 67 65 73 2c 20 22 25 73 66 61 63  es_edges, "%sfac
333c0 65 73 5f 65 64 67 65 73 22 2c 20 70 72 65 66 69  es_edges", prefi
333d0 78 29 3b 0a 20 20 20 20 74 61 62 6c 65 73 5b 36  x);.    tables[6
333e0 5d 20 3d 20 74 61 62 6c 65 5f 66 61 63 65 73 5f  ] = table_faces_
333f0 65 64 67 65 73 3b 0a 20 20 20 20 76 69 65 77 73  edges;.    views
33400 5b 36 5d 20 3d 20 30 3b 0a 20 20 20 20 73 70 72  [6] = 0;.    spr
33410 69 6e 74 66 20 28 76 69 65 77 5f 63 68 65 63 6b  intf (view_check
33420 5f 6e 6f 64 65 5f 63 6f 64 65 73 2c 20 22 25 73  _node_codes, "%s
33430 6e 6f 64 65 73 5f 63 68 65 63 6b 5f 64 75 70 6c  nodes_check_dupl
33440 5f 63 6f 64 65 73 22 2c 20 70 72 65 66 69 78 29  _codes", prefix)
33450 3b 0a 20 20 20 20 74 61 62 6c 65 73 5b 37 5d 20  ;.    tables[7] 
33460 3d 20 76 69 65 77 5f 63 68 65 63 6b 5f 6e 6f 64  = view_check_nod
33470 65 5f 63 6f 64 65 73 3b 0a 20 20 20 20 76 69 65  e_codes;.    vie
33480 77 73 5b 37 5d 20 3d 20 31 3b 0a 20 20 20 20 73  ws[7] = 1;.    s
33490 70 72 69 6e 74 66 20 28 76 69 65 77 5f 63 68 65  printf (view_che
334a0 63 6b 5f 6e 6f 64 65 5f 67 65 6f 6d 73 2c 20 22  ck_node_geoms, "
334b0 25 73 6e 6f 64 65 73 5f 63 68 65 63 6b 5f 64 75  %snodes_check_du
334c0 70 6c 5f 67 65 6f 6d 73 22 2c 20 70 72 65 66 69  pl_geoms", prefi
334d0 78 29 3b 0a 20 20 20 20 74 61 62 6c 65 73 5b 38  x);.    tables[8
334e0 5d 20 3d 20 76 69 65 77 5f 63 68 65 63 6b 5f 6e  ] = view_check_n
334f0 6f 64 65 5f 67 65 6f 6d 73 3b 0a 20 20 20 20 76  ode_geoms;.    v
33500 69 65 77 73 5b 38 5d 20 3d 20 31 3b 0a 20 20 20  iews[8] = 1;.   
33510 20 73 70 72 69 6e 74 66 20 28 76 69 65 77 5f 63   sprintf (view_c
33520 68 65 63 6b 5f 65 64 67 65 5f 63 6f 64 65 73 2c  heck_edge_codes,
33530 20 22 25 73 65 64 67 65 73 5f 63 68 65 63 6b 5f   "%sedges_check_
33540 64 75 70 6c 5f 63 6f 64 65 73 22 2c 20 70 72 65  dupl_codes", pre
33550 66 69 78 29 3b 0a 20 20 20 20 74 61 62 6c 65 73  fix);.    tables
33560 5b 39 5d 20 3d 20 76 69 65 77 5f 63 68 65 63 6b  [9] = view_check
33570 5f 65 64 67 65 5f 63 6f 64 65 73 3b 0a 20 20 20  _edge_codes;.   
33580 20 76 69 65 77 73 5b 39 5d 20 3d 20 31 3b 0a 20   views[9] = 1;. 
33590 20 20 20 73 70 72 69 6e 74 66 20 28 76 69 65 77     sprintf (view
335a0 5f 63 68 65 63 6b 5f 65 64 67 65 5f 67 65 6f 6d  _check_edge_geom
335b0 73 2c 20 22 25 73 65 64 67 65 73 5f 63 68 65 63  s, "%sedges_chec
335c0 6b 5f 69 6e 74 65 72 73 65 63 74 69 6f 6e 73 22  k_intersections"
335d0 2c 20 70 72 65 66 69 78 29 3b 0a 20 20 20 20 74  , prefix);.    t
335e0 61 62 6c 65 73 5b 31 30 5d 20 3d 20 76 69 65 77  ables[10] = view
335f0 5f 63 68 65 63 6b 5f 65 64 67 65 5f 67 65 6f 6d  _check_edge_geom
33600 73 3b 0a 20 20 20 20 76 69 65 77 73 5b 31 30 5d  s;.    views[10]
33610 20 3d 20 31 3b 0a 20 20 20 20 73 70 72 69 6e 74   = 1;.    sprint
33620 66 20 28 76 69 65 77 5f 63 68 65 63 6b 5f 65 64  f (view_check_ed
33630 67 65 5f 6e 6f 64 65 5f 67 65 6f 6d 73 2c 20 22  ge_node_geoms, "
33640 25 73 65 64 67 65 73 5f 63 68 65 63 6b 5f 6e 6f  %sedges_check_no
33650 64 65 73 22 2c 20 70 72 65 66 69 78 29 3b 0a 20  des", prefix);. 
33660 20 20 20 74 61 62 6c 65 73 5b 31 31 5d 20 3d 20     tables[11] = 
33670 76 69 65 77 5f 63 68 65 63 6b 5f 65 64 67 65 5f  view_check_edge_
33680 6e 6f 64 65 5f 67 65 6f 6d 73 3b 0a 20 20 20 20  node_geoms;.    
33690 76 69 65 77 73 5b 31 31 5d 20 3d 20 31 3b 0a 20  views[11] = 1;. 
336a0 20 20 20 73 70 72 69 6e 74 66 20 28 76 69 65 77     sprintf (view
336b0 5f 63 68 65 63 6b 5f 66 61 63 65 5f 63 6f 64 65  _check_face_code
336c0 73 2c 20 22 25 73 66 61 63 65 73 5f 63 68 65 63  s, "%sfaces_chec
336d0 6b 5f 64 75 70 6c 5f 63 6f 64 65 73 22 2c 20 70  k_dupl_codes", p
336e0 72 65 66 69 78 29 3b 0a 20 20 20 20 74 61 62 6c  refix);.    tabl
336f0 65 73 5b 31 32 5d 20 3d 20 76 69 65 77 5f 63 68  es[12] = view_ch
33700 65 63 6b 5f 66 61 63 65 5f 63 6f 64 65 73 3b 0a  eck_face_codes;.
33710 20 20 20 20 76 69 65 77 73 5b 31 32 5d 20 3d 20      views[12] = 
33720 31 3b 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28  1;.    sprintf (
33730 76 69 65 77 5f 66 61 63 65 73 5f 72 65 73 6f 6c  view_faces_resol
33740 76 65 64 2c 20 22 25 73 66 61 63 65 73 5f 72 65  ved, "%sfaces_re
33750 73 6f 6c 76 65 64 22 2c 20 70 72 65 66 69 78 29  solved", prefix)
33760 3b 0a 20 20 20 20 74 61 62 6c 65 73 5b 31 33 5d  ;.    tables[13]
33770 20 3d 20 76 69 65 77 5f 66 61 63 65 73 5f 72 65   = view_faces_re
33780 73 6f 6c 76 65 64 3b 0a 20 20 20 20 76 69 65 77  solved;.    view
33790 73 5b 31 33 5d 20 3d 20 31 3b 0a 20 20 20 20 73  s[13] = 1;.    s
337a0 70 72 69 6e 74 66 20 28 76 69 65 77 5f 63 75 72  printf (view_cur
337b0 76 65 73 5f 72 65 73 6f 6c 76 65 64 2c 20 22 25  ves_resolved, "%
337c0 73 63 75 72 76 65 73 5f 72 65 73 6f 6c 76 65 64  scurves_resolved
337d0 22 2c 20 70 72 65 66 69 78 29 3b 0a 20 20 20 20  ", prefix);.    
337e0 74 61 62 6c 65 73 5b 31 34 5d 20 3d 20 76 69 65  tables[14] = vie
337f0 77 5f 63 75 72 76 65 73 5f 72 65 73 6f 6c 76 65  w_curves_resolve
33800 64 3b 0a 20 20 20 20 76 69 65 77 73 5b 31 34 5d  d;.    views[14]
33810 20 3d 20 31 3b 0a 20 20 20 20 73 70 72 69 6e 74   = 1;.    sprint
33820 66 20 28 76 69 65 77 5f 73 75 72 66 61 63 65 73  f (view_surfaces
33830 5f 72 65 73 6f 6c 76 65 64 2c 20 22 25 73 73 75  _resolved, "%ssu
33840 72 66 61 63 65 73 5f 72 65 73 6f 6c 76 65 64 22  rfaces_resolved"
33850 2c 20 70 72 65 66 69 78 29 3b 0a 20 20 20 20 74  , prefix);.    t
33860 61 62 6c 65 73 5b 31 35 5d 20 3d 20 76 69 65 77  ables[15] = view
33870 5f 73 75 72 66 61 63 65 73 5f 72 65 73 6f 6c 76  _surfaces_resolv
33880 65 64 3b 0a 20 20 20 20 76 69 65 77 73 5b 31 35  ed;.    views[15
33890 5d 20 3d 20 31 3b 0a 20 20 20 20 73 70 72 69 6e  ] = 1;.    sprin
338a0 74 66 20 28 76 69 65 77 5f 64 61 6e 67 6c 69 6e  tf (view_danglin
338b0 67 5f 6e 6f 64 65 73 2c 20 22 25 73 64 61 6e 67  g_nodes, "%sdang
338c0 6c 69 6e 67 5f 6e 6f 64 65 73 22 2c 20 70 72 65  ling_nodes", pre
338d0 66 69 78 29 3b 0a 20 20 20 20 74 61 62 6c 65 73  fix);.    tables
338e0 5b 31 36 5d 20 3d 20 76 69 65 77 5f 64 61 6e 67  [16] = view_dang
338f0 6c 69 6e 67 5f 6e 6f 64 65 73 3b 0a 20 20 20 20  ling_nodes;.    
33900 76 69 65 77 73 5b 31 36 5d 20 3d 20 31 3b 0a 20  views[16] = 1;. 
33910 20 20 20 73 70 72 69 6e 74 66 20 28 76 69 65 77     sprintf (view
33920 5f 64 61 6e 67 6c 69 6e 67 5f 65 64 67 65 73 2c  _dangling_edges,
33930 20 22 25 73 64 61 6e 67 6c 69 6e 67 5f 65 64 67   "%sdangling_edg
33940 65 73 22 2c 20 70 72 65 66 69 78 29 3b 0a 20 20  es", prefix);.  
33950 20 20 74 61 62 6c 65 73 5b 31 37 5d 20 3d 20 76    tables[17] = v
33960 69 65 77 5f 64 61 6e 67 6c 69 6e 67 5f 65 64 67  iew_dangling_edg
33970 65 73 3b 0a 20 20 20 20 76 69 65 77 73 5b 31 37  es;.    views[17
33980 5d 20 3d 20 31 3b 0a 20 20 20 20 73 70 72 69 6e  ] = 1;.    sprin
33990 74 66 20 28 76 69 65 77 5f 65 64 67 65 73 5f 63  tf (view_edges_c
339a0 68 65 63 6b 5f 66 72 6f 6d 5f 74 6f 2c 20 22 25  heck_from_to, "%
339b0 73 65 64 67 65 73 5f 63 68 65 63 6b 5f 66 72 6f  sedges_check_fro
339c0 6d 5f 74 6f 22 2c 20 70 72 65 66 69 78 29 3b 0a  m_to", prefix);.
339d0 20 20 20 20 74 61 62 6c 65 73 5b 31 38 5d 20 3d      tables[18] =
339e0 20 76 69 65 77 5f 65 64 67 65 73 5f 63 68 65 63   view_edges_chec
339f0 6b 5f 66 72 6f 6d 5f 74 6f 3b 0a 20 20 20 20 76  k_from_to;.    v
33a00 69 65 77 73 5b 31 38 5d 20 3d 20 31 3b 0a 20 20  iews[18] = 1;.  
33a10 20 20 74 61 62 6c 65 73 5b 31 39 5d 20 3d 20 4e    tables[19] = N
33a20 55 4c 4c 3b 0a 20 20 20 20 70 5f 76 69 65 77 20  ULL;.    p_view 
33a30 3d 20 76 69 65 77 73 3b 0a 20 20 20 20 70 5f 74  = views;.    p_t
33a40 62 6c 20 3d 20 74 61 62 6c 65 73 3b 0a 20 20 20  bl = tables;.   
33a50 20 77 68 69 6c 65 20 28 2a 70 5f 74 62 6c 20 21   while (*p_tbl !
33a60 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a  = NULL).      {.
33a70 09 20 20 6f 6b 5f 74 61 62 6c 65 20 3d 20 63 68  .  ok_table = ch
33a80 65 63 6b 5f 74 6f 70 6f 5f 74 61 62 6c 65 20 28  eck_topo_table (
33a90 73 71 6c 69 74 65 2c 20 2a 70 5f 74 62 6c 2c 20  sqlite, *p_tbl, 
33aa0 2a 70 5f 76 69 65 77 29 3b 0a 09 20 20 69 66 20  *p_view);..  if 
33ab0 28 6f 6b 5f 74 61 62 6c 65 29 0a 09 20 20 20 20  (ok_table)..    
33ac0 7b 0a 09 09 69 66 20 28 73 74 72 63 6d 70 20 28  {...if (strcmp (
33ad0 2a 70 5f 74 62 6c 2c 20 22 74 6f 70 6f 6c 6f 67  *p_tbl, "topolog
33ae0 79 5f 6d 61 73 74 65 72 22 29 20 3d 3d 20 30 29  y_master") == 0)
33af0 0a 09 09 20 20 20 20 63 72 65 61 74 65 5f 6d 61  ...    create_ma
33b00 73 74 65 72 20 3d 20 30 3b 0a 09 09 65 6c 73 65  ster = 0;...else
33b10 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 73  ...  {...      s
33b20 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 09 09 20  patialite_e.... 
33b30 20 28 22 43 72 65 61 74 65 54 6f 70 6f 6c 6f 67   ("CreateTopolog
33b40 79 54 61 62 6c 65 73 28 29 20 65 72 72 6f 72 3a  yTables() error:
33b50 20 74 61 62 6c 65 20 27 25 73 27 20 61 6c 72 65   table '%s' alre
33b60 61 64 79 20 65 78 69 73 74 73 5c 6e 22 2c 0a 09  ady exists\n",..
33b70 09 09 20 20 20 2a 70 5f 74 62 6c 29 3b 0a 09 09  ..   *p_tbl);...
33b80 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
33b90 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
33ba0 74 2c 20 30 29 3b 0a 09 09 20 20 20 20 20 20 72  t, 0);...      r
33bb0 65 74 75 72 6e 3b 0a 09 09 20 20 7d 0a 09 20 20  eturn;...  }..  
33bc0 20 20 7d 0a 09 20 20 70 5f 74 62 6c 2b 2b 3b 0a    }..  p_tbl++;.
33bd0 09 20 20 70 5f 76 69 65 77 2b 2b 3b 0a 20 20 20  .  p_view++;.   
33be0 20 20 20 7d 0a 0a 2f 2a 20 63 72 65 61 74 69 6e     }../* creatin
33bf0 67 20 54 6f 70 6f 6c 6f 67 79 20 74 61 62 6c 65  g Topology table
33c00 73 20 2a 2f 0a 20 20 20 20 69 66 20 28 63 72 65  s */.    if (cre
33c10 61 74 65 5f 6d 61 73 74 65 72 29 0a 20 20 20 20  ate_master).    
33c20 20 20 7b 0a 09 20 20 69 66 20 28 21 63 72 65 61    {..  if (!crea
33c30 74 65 5f 74 6f 70 6f 5f 6d 61 73 74 65 72 20 28  te_topo_master (
33c40 73 71 6c 69 74 65 29 29 0a 09 20 20 20 20 20 20  sqlite))..      
33c50 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20  goto error;.    
33c60 20 20 7d 0a 20 20 20 20 69 66 20 28 21 63 72 65    }.    if (!cre
33c70 61 74 65 5f 74 6f 70 6f 5f 6e 6f 64 65 73 20 28  ate_topo_nodes (
33c80 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 5f 6e 6f  sqlite, table_no
33c90 64 65 73 2c 20 73 72 69 64 2c 20 64 69 6d 73 29  des, srid, dims)
33ca0 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20  )..goto error;. 
33cb0 20 20 20 69 66 20 28 21 63 72 65 61 74 65 5f 74     if (!create_t
33cc0 6f 70 6f 5f 65 64 67 65 73 20 28 73 71 6c 69 74  opo_edges (sqlit
33cd0 65 2c 20 74 61 62 6c 65 5f 65 64 67 65 73 2c 20  e, table_edges, 
33ce0 73 72 69 64 2c 20 64 69 6d 73 29 29 0a 09 67 6f  srid, dims))..go
33cf0 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 69 66  to error;.    if
33d00 20 28 21 63 72 65 61 74 65 5f 74 6f 70 6f 5f 66   (!create_topo_f
33d10 61 63 65 73 20 28 73 71 6c 69 74 65 2c 20 74 61  aces (sqlite, ta
33d20 62 6c 65 5f 66 61 63 65 73 29 29 0a 09 67 6f 74  ble_faces))..got
33d30 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 20  o error;.    if 
33d40 28 21 63 72 65 61 74 65 5f 74 6f 70 6f 5f 66 61  (!create_topo_fa
33d50 63 65 73 5f 65 64 67 65 73 20 28 73 71 6c 69 74  ces_edges (sqlit
33d60 65 2c 20 74 61 62 6c 65 5f 66 61 63 65 73 5f 65  e, table_faces_e
33d70 64 67 65 73 2c 20 74 61 62 6c 65 5f 66 61 63 65  dges, table_face
33d80 73 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b  s))..goto error;
33d90 0a 20 20 20 20 69 66 20 28 21 63 72 65 61 74 65  .    if (!create
33da0 5f 74 6f 70 6f 5f 63 75 72 76 65 73 20 28 73 71  _topo_curves (sq
33db0 6c 69 74 65 2c 20 74 61 62 6c 65 5f 63 75 72 76  lite, table_curv
33dc0 65 73 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72  es))..goto error
33dd0 3b 0a 20 20 20 20 69 66 20 28 21 63 72 65 61 74  ;.    if (!creat
33de0 65 5f 74 6f 70 6f 5f 73 75 72 66 61 63 65 73 20  e_topo_surfaces 
33df0 28 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 5f 73  (sqlite, table_s
33e00 75 72 66 61 63 65 73 29 29 0a 09 67 6f 74 6f 20  urfaces))..goto 
33e10 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 20 28 21  error;.    if (!
33e20 63 72 65 61 74 65 5f 63 68 65 63 6b 5f 6e 6f 64  create_check_nod
33e30 65 5f 63 6f 64 65 73 20 28 73 71 6c 69 74 65 2c  e_codes (sqlite,
33e40 20 76 69 65 77 5f 63 68 65 63 6b 5f 6e 6f 64 65   view_check_node
33e50 5f 63 6f 64 65 73 2c 20 74 61 62 6c 65 5f 6e 6f  _codes, table_no
33e60 64 65 73 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f  des))..goto erro
33e70 72 3b 0a 20 20 20 20 69 66 20 28 21 63 72 65 61  r;.    if (!crea
33e80 74 65 5f 63 68 65 63 6b 5f 6e 6f 64 65 5f 67 65  te_check_node_ge
33e90 6f 6d 73 20 28 73 71 6c 69 74 65 2c 20 76 69 65  oms (sqlite, vie
33ea0 77 5f 63 68 65 63 6b 5f 6e 6f 64 65 5f 67 65 6f  w_check_node_geo
33eb0 6d 73 2c 20 74 61 62 6c 65 5f 6e 6f 64 65 73 29  ms, table_nodes)
33ec0 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20  )..goto error;. 
33ed0 20 20 20 69 66 20 28 21 63 72 65 61 74 65 5f 63     if (!create_c
33ee0 68 65 63 6b 5f 65 64 67 65 5f 63 6f 64 65 73 20  heck_edge_codes 
33ef0 28 73 71 6c 69 74 65 2c 20 76 69 65 77 5f 63 68  (sqlite, view_ch
33f00 65 63 6b 5f 65 64 67 65 5f 63 6f 64 65 73 2c 20  eck_edge_codes, 
33f10 74 61 62 6c 65 5f 65 64 67 65 73 29 29 0a 09 67  table_edges))..g
33f20 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 69  oto error;.    i
33f30 66 20 28 21 63 72 65 61 74 65 5f 63 68 65 63 6b  f (!create_check
33f40 5f 65 64 67 65 5f 67 65 6f 6d 73 20 28 73 71 6c  _edge_geoms (sql
33f50 69 74 65 2c 20 76 69 65 77 5f 63 68 65 63 6b 5f  ite, view_check_
33f60 65 64 67 65 5f 67 65 6f 6d 73 2c 20 74 61 62 6c  edge_geoms, tabl
33f70 65 5f 65 64 67 65 73 29 29 0a 09 67 6f 74 6f 20  e_edges))..goto 
33f80 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 20 28 21  error;.    if (!
33f90 63 72 65 61 74 65 5f 63 68 65 63 6b 5f 65 64 67  create_check_edg
33fa0 65 5f 6e 6f 64 65 5f 67 65 6f 6d 73 0a 09 28 73  e_node_geoms..(s
33fb0 71 6c 69 74 65 2c 20 76 69 65 77 5f 63 68 65 63  qlite, view_chec
33fc0 6b 5f 65 64 67 65 5f 6e 6f 64 65 5f 67 65 6f 6d  k_edge_node_geom
33fd0 73 2c 20 74 61 62 6c 65 5f 65 64 67 65 73 2c 20  s, table_edges, 
33fe0 74 61 62 6c 65 5f 6e 6f 64 65 73 29 29 0a 09 67  table_nodes))..g
33ff0 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 69  oto error;.    i
34000 66 20 28 21 63 72 65 61 74 65 5f 63 68 65 63 6b  f (!create_check
34010 5f 66 61 63 65 5f 63 6f 64 65 73 20 28 73 71 6c  _face_codes (sql
34020 69 74 65 2c 20 76 69 65 77 5f 63 68 65 63 6b 5f  ite, view_check_
34030 66 61 63 65 5f 63 6f 64 65 73 2c 20 74 61 62 6c  face_codes, tabl
34040 65 5f 66 61 63 65 73 29 29 0a 09 67 6f 74 6f 20  e_faces))..goto 
34050 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 20 28 21  error;.    if (!
34060 63 72 65 61 74 65 5f 66 61 63 65 73 5f 72 65 73  create_faces_res
34070 6f 6c 76 65 64 0a 09 28 73 71 6c 69 74 65 2c 20  olved..(sqlite, 
34080 76 69 65 77 5f 66 61 63 65 73 5f 72 65 73 6f 6c  view_faces_resol
34090 76 65 64 2c 20 74 61 62 6c 65 5f 66 61 63 65 73  ved, table_faces
340a0 2c 20 74 61 62 6c 65 5f 66 61 63 65 73 5f 65 64  , table_faces_ed
340b0 67 65 73 2c 0a 09 20 74 61 62 6c 65 5f 65 64 67  ges,.. table_edg
340c0 65 73 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72  es))..goto error
340d0 3b 0a 20 20 20 20 69 66 20 28 21 63 72 65 61 74  ;.    if (!creat
340e0 65 5f 63 75 72 76 65 73 5f 72 65 73 6f 6c 76 65  e_curves_resolve
340f0 64 0a 09 28 73 71 6c 69 74 65 2c 20 76 69 65 77  d..(sqlite, view
34100 5f 63 75 72 76 65 73 5f 72 65 73 6f 6c 76 65 64  _curves_resolved
34110 2c 20 74 61 62 6c 65 5f 63 75 72 76 65 73 2c 20  , table_curves, 
34120 74 61 62 6c 65 5f 65 64 67 65 73 29 29 0a 09 67  table_edges))..g
34130 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20 69  oto error;.    i
34140 66 20 28 21 63 72 65 61 74 65 5f 73 75 72 66 61  f (!create_surfa
34150 63 65 73 5f 72 65 73 6f 6c 76 65 64 0a 09 28 73  ces_resolved..(s
34160 71 6c 69 74 65 2c 20 76 69 65 77 5f 73 75 72 66  qlite, view_surf
34170 61 63 65 73 5f 72 65 73 6f 6c 76 65 64 2c 20 74  aces_resolved, t
34180 61 62 6c 65 5f 73 75 72 66 61 63 65 73 2c 20 76  able_surfaces, v
34190 69 65 77 5f 66 61 63 65 73 5f 72 65 73 6f 6c 76  iew_faces_resolv
341a0 65 64 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72  ed))..goto error
341b0 3b 0a 20 20 20 20 69 66 20 28 21 63 72 65 61 74  ;.    if (!creat
341c0 65 5f 64 61 6e 67 6c 69 6e 67 5f 6e 6f 64 65 73  e_dangling_nodes
341d0 0a 09 28 73 71 6c 69 74 65 2c 20 76 69 65 77 5f  ..(sqlite, view_
341e0 64 61 6e 67 6c 69 6e 67 5f 6e 6f 64 65 73 2c 20  dangling_nodes, 
341f0 74 61 62 6c 65 5f 6e 6f 64 65 73 2c 20 74 61 62  table_nodes, tab
34200 6c 65 5f 65 64 67 65 73 29 29 0a 09 67 6f 74 6f  le_edges))..goto
34210 20 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 20 28   error;.    if (
34220 21 63 72 65 61 74 65 5f 64 61 6e 67 6c 69 6e 67  !create_dangling
34230 5f 65 64 67 65 73 0a 09 28 73 71 6c 69 74 65 2c  _edges..(sqlite,
34240 20 76 69 65 77 5f 64 61 6e 67 6c 69 6e 67 5f 65   view_dangling_e
34250 64 67 65 73 2c 20 74 61 62 6c 65 5f 65 64 67 65  dges, table_edge
34260 73 2c 20 74 61 62 6c 65 5f 66 61 63 65 73 5f 65  s, table_faces_e
34270 64 67 65 73 2c 0a 09 20 74 61 62 6c 65 5f 63 75  dges,.. table_cu
34280 72 76 65 73 29 29 0a 09 67 6f 74 6f 20 65 72 72  rves))..goto err
34290 6f 72 3b 0a 20 20 20 20 69 66 20 28 21 63 72 65  or;.    if (!cre
342a0 61 74 65 5f 63 68 65 63 6b 5f 65 64 67 65 73 5f  ate_check_edges_
342b0 66 72 6f 6d 5f 74 6f 0a 09 28 73 71 6c 69 74 65  from_to..(sqlite
342c0 2c 20 76 69 65 77 5f 65 64 67 65 73 5f 63 68 65  , view_edges_che
342d0 63 6b 5f 66 72 6f 6d 5f 74 6f 2c 20 74 61 62 6c  ck_from_to, tabl
342e0 65 5f 65 64 67 65 73 2c 20 74 61 62 6c 65 5f 6e  e_edges, table_n
342f0 6f 64 65 73 29 29 0a 09 67 6f 74 6f 20 65 72 72  odes))..goto err
34300 6f 72 3b 0a 20 20 20 20 69 66 20 28 21 75 70 64  or;.    if (!upd
34310 61 74 65 5f 74 6f 70 6f 5f 6d 61 73 74 65 72 0a  ate_topo_master.
34320 09 28 73 71 6c 69 74 65 2c 20 74 61 62 6c 65 5f  .(sqlite, table_
34330 6e 6f 64 65 73 2c 20 74 61 62 6c 65 5f 65 64 67  nodes, table_edg
34340 65 73 2c 20 74 61 62 6c 65 5f 66 61 63 65 73 2c  es, table_faces,
34350 20 74 61 62 6c 65 5f 66 61 63 65 73 5f 65 64 67   table_faces_edg
34360 65 73 2c 0a 09 20 74 61 62 6c 65 5f 63 75 72 76  es,.. table_curv
34370 65 73 2c 20 74 61 62 6c 65 5f 73 75 72 66 61 63  es, table_surfac
34380 65 73 2c 20 76 69 65 77 5f 63 68 65 63 6b 5f 6e  es, view_check_n
34390 6f 64 65 5f 63 6f 64 65 73 2c 0a 09 20 76 69 65  ode_codes,.. vie
343a0 77 5f 63 68 65 63 6b 5f 6e 6f 64 65 5f 67 65 6f  w_check_node_geo
343b0 6d 73 2c 20 76 69 65 77 5f 63 68 65 63 6b 5f 65  ms, view_check_e
343c0 64 67 65 5f 63 6f 64 65 73 2c 20 76 69 65 77 5f  dge_codes, view_
343d0 63 68 65 63 6b 5f 65 64 67 65 5f 67 65 6f 6d 73  check_edge_geoms
343e0 2c 0a 09 20 76 69 65 77 5f 63 68 65 63 6b 5f 65  ,.. view_check_e
343f0 64 67 65 5f 6e 6f 64 65 5f 67 65 6f 6d 73 2c 20  dge_node_geoms, 
34400 76 69 65 77 5f 63 68 65 63 6b 5f 66 61 63 65 5f  view_check_face_
34410 63 6f 64 65 73 2c 20 76 69 65 77 5f 66 61 63 65  codes, view_face
34420 73 5f 72 65 73 6f 6c 76 65 64 2c 0a 09 20 76 69  s_resolved,.. vi
34430 65 77 5f 63 75 72 76 65 73 5f 72 65 73 6f 6c 76  ew_curves_resolv
34440 65 64 2c 20 76 69 65 77 5f 73 75 72 66 61 63 65  ed, view_surface
34450 73 5f 72 65 73 6f 6c 76 65 64 2c 20 76 69 65 77  s_resolved, view
34460 5f 64 61 6e 67 6c 69 6e 67 5f 6e 6f 64 65 73 2c  _dangling_nodes,
34470 0a 09 20 76 69 65 77 5f 64 61 6e 67 6c 69 6e 67  .. view_dangling
34480 5f 65 64 67 65 73 2c 20 76 69 65 77 5f 65 64 67  _edges, view_edg
34490 65 73 5f 63 68 65 63 6b 5f 66 72 6f 6d 5f 74 6f  es_check_from_to
344a0 2c 20 73 72 69 64 2c 20 64 69 6d 73 29 29 0a 09  , srid, dims))..
344b0 67 6f 74 6f 20 65 72 72 6f 72 3b 0a 20 20 20 20  goto error;.    
344c0 75 70 64 61 74 65 53 70 61 74 69 61 4c 69 74 65  updateSpatiaLite
344d0 48 69 73 74 6f 72 79 20 28 73 71 6c 69 74 65 2c  History (sqlite,
344e0 20 22 2a 2a 2a 20 54 4f 50 4f 4c 4f 47 59 20 2a   "*** TOPOLOGY *
344f0 2a 2a 22 2c 20 4e 55 4c 4c 2c 0a 09 09 09 20 20  **", NULL,....  
34500 20 20 20 22 54 6f 70 6f 6c 6f 67 79 20 74 61 62     "Topology tab
34510 6c 65 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  les successfully
34520 20 63 72 65 61 74 65 64 22 29 3b 0a 20 20 20 20   created");.    
34530 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
34540 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b  nt (context, 1);
34550 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20  .    return;..  
34560 65 72 72 6f 72 3a 0a 20 20 20 20 73 71 6c 69 74  error:.    sqlit
34570 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
34580 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20  ontext, 0);.    
34590 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69  return;.}..stati
345a0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 55 70 64 61  c void.fnct_Upda
345b0 74 65 4c 61 79 65 72 53 74 61 74 69 73 74 69 63  teLayerStatistic
345c0 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  s (sqlite3_conte
345d0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
345e0 74 20 61 72 67 63 2c 0a 09 09 09 20 20 20 20 73  t argc,....    s
345f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
34600 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
34610 75 6e 63 74 69 6f 6e 3a 0a 2f 20 55 70 64 61 74  unction:./ Updat
34620 65 4c 61 79 65 72 53 74 61 74 69 73 74 69 63 73  eLayerStatistics
34630 28 74 61 62 6c 65 2c 20 63 6f 6c 75 6d 6e 20 29  (table, column )
34640 0a 2f 0a 2f 20 55 70 64 61 74 65 73 20 4c 41 59  ././ Updates LAY
34650 45 52 5f 53 54 41 54 49 53 54 49 43 53 20 5b 62  ER_STATISTICS [b
34660 61 73 65 64 20 6f 6e 20 43 6f 6c 75 6d 6e 20 61  ased on Column a
34670 6e 64 20 54 61 62 6c 65 5d 0a 2f 20 72 65 74 75  nd Table]./ retu
34680 72 6e 73 20 31 20 6f 6e 20 73 75 63 63 65 73 73  rns 1 on success
34690 0a 2f 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 0a  ./ 0 on failure.
346a0 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  */.    const cha
346b0 72 20 2a 73 71 6c 3b 0a 20 20 20 20 63 6f 6e 73  r *sql;.    cons
346c0 74 20 63 68 61 72 20 2a 74 61 62 6c 65 20 3d 20  t char *table = 
346d0 4e 55 4c 4c 3b 0a 20 20 20 20 63 6f 6e 73 74 20  NULL;.    const 
346e0 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 20 3d 20 4e  char *column = N
346f0 55 4c 4c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ULL;.    sqlite3
34700 20 2a 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74   *sqlite = sqlit
34710 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
34720 6e 64 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a  ndle (context);.
34730 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
34740 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
34750 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
34760 20 28 61 72 67 63 20 3e 3d 20 31 29 0a 20 20 20   (argc >= 1).   
34770 20 20 20 7b 0a 09 20 20 69 66 20 28 73 71 6c 69     {..  if (sqli
34780 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
34790 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
347a0 54 45 5f 54 45 58 54 29 0a 09 20 20 20 20 7b 0a  TE_TEXT)..    {.
347b0 09 09 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09  ..spatialite_e..
347c0 09 20 20 20 20 28 22 55 70 64 61 74 65 4c 61 79  .    ("UpdateLay
347d0 65 72 53 74 61 74 69 73 74 69 63 73 28 29 20 65  erStatistics() e
347e0 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 31  rror: argument 1
347f0 20 5b 74 61 62 6c 65 5f 6e 61 6d 65 5d 20 69 73   [table_name] is
34800 20 6e 6f 74 20 6f 66 20 74 68 65 20 53 74 72 69   not of the Stri
34810 6e 67 20 74 79 70 65 5c 6e 22 29 3b 0a 09 09 73  ng type\n");...s
34820 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
34830 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  t (context, 0);.
34840 09 09 72 65 74 75 72 6e 3b 0a 09 20 20 20 20 7d  ..return;..    }
34850 0a 09 20 20 74 61 62 6c 65 20 3d 20 28 63 6f 6e  ..  table = (con
34860 73 74 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  st char *) sqlit
34870 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61  e3_value_text (a
34880 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d  rgv[0]);.      }
34890 0a 20 20 20 20 69 66 20 28 61 72 67 63 20 3e 3d  .    if (argc >=
348a0 20 32 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69   2).      {..  i
348b0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
348c0 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20  _type (argv[1]) 
348d0 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a  != SQLITE_TEXT).
348e0 09 20 20 20 20 7b 0a 09 09 73 70 61 74 69 61 6c  .    {...spatial
348f0 69 74 65 5f 65 0a 09 09 20 20 20 20 28 22 55 70  ite_e...    ("Up
34900 64 61 74 65 4c 61 79 65 72 53 74 61 74 69 73 74  dateLayerStatist
34910 69 63 73 28 29 20 65 72 72 6f 72 3a 20 61 72 67  ics() error: arg
34920 75 6d 65 6e 74 20 32 20 5b 63 6f 6c 75 6d 6e 5f  ument 2 [column_
34930 6e 61 6d 65 5d 20 69 73 20 6e 6f 74 20 6f 66 20  name] is not of 
34940 74 68 65 20 53 74 72 69 6e 67 20 74 79 70 65 5c  the String type\
34950 6e 22 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72  n");...sqlite3_r
34960 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
34970 78 74 2c 20 30 29 3b 0a 09 09 72 65 74 75 72 6e  xt, 0);...return
34980 3b 0a 09 20 20 20 20 7d 0a 09 20 20 63 6f 6c 75  ;..    }..  colu
34990 6d 6e 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  mn = (const char
349a0 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
349b0 65 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d 29  e_text (argv[1])
349c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
349d0 20 28 21 75 70 64 61 74 65 5f 6c 61 79 65 72 5f   (!update_layer_
349e0 73 74 61 74 69 73 74 69 63 73 20 28 73 71 6c 69  statistics (sqli
349f0 74 65 2c 20 74 61 62 6c 65 2c 20 63 6f 6c 75 6d  te, table, colum
34a00 6e 29 29 0a 09 67 6f 74 6f 20 65 72 72 6f 72 3b  n))..goto error;
34a10 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
34a20 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
34a30 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 20 3d 20  , 1);.    sql = 
34a40 22 55 70 64 61 74 65 4c 61 79 65 72 53 74 61 74  "UpdateLayerStat
34a50 69 73 74 69 63 73 22 3b 0a 20 20 20 20 69 66 20  istics";.    if 
34a60 28 74 61 62 6c 65 20 3d 3d 20 4e 55 4c 4c 29 0a  (table == NULL).
34a70 09 74 61 62 6c 65 20 3d 20 22 41 4c 4c 2d 54 41  .table = "ALL-TA
34a80 42 4c 45 53 22 3b 0a 20 20 20 20 69 66 20 28 63  BLES";.    if (c
34a90 6f 6c 75 6d 6e 20 3d 3d 20 4e 55 4c 4c 29 0a 09  olumn == NULL)..
34aa0 63 6f 6c 75 6d 6e 20 3d 20 22 41 4c 4c 2d 47 45  column = "ALL-GE
34ab0 4f 4d 45 54 52 59 2d 43 4f 4c 55 4d 4e 53 22 3b  OMETRY-COLUMNS";
34ac0 0a 20 20 20 20 75 70 64 61 74 65 53 70 61 74 69  .    updateSpati
34ad0 61 4c 69 74 65 48 69 73 74 6f 72 79 20 28 73 71  aLiteHistory (sq
34ae0 6c 69 74 65 2c 20 28 63 6f 6e 73 74 20 63 68 61  lite, (const cha
34af0 72 20 2a 29 20 74 61 62 6c 65 2c 0a 09 09 09 20  r *) table,.... 
34b00 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20      (const char 
34b10 2a 29 20 63 6f 6c 75 6d 6e 2c 20 73 71 6c 29 3b  *) column, sql);
34b20 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 65  .    return;.  e
34b30 72 72 6f 72 3a 0a 20 20 20 20 73 71 6c 69 74 65  rror:.    sqlite
34b40 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
34b50 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 72  ntext, 0);.    r
34b60 65 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63  eturn;.}..static
34b70 20 67 61 69 61 50 6f 69 6e 74 50 74 72 0a 73 69   gaiaPointPtr.si
34b80 6d 70 6c 65 50 6f 69 6e 74 20 28 67 61 69 61 47  mplePoint (gaiaG
34b90 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 29 0a  eomCollPtr geo).
34ba0 7b 0a 2f 2a 20 68 65 6c 70 65 72 20 66 75 6e 63  {./* helper func
34bb0 74 69 6f 6e 0a 2f 20 69 66 20 74 68 69 73 20 47  tion./ if this G
34bc0 45 4f 4d 45 54 52 59 20 63 6f 6e 74 61 69 6e 73  EOMETRY contains
34bd0 20 6f 6e 6c 79 20 6f 6e 65 20 50 4f 49 4e 54 2c   only one POINT,
34be0 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 65 6c   and no other el
34bf0 65 6d 65 6e 74 61 72 79 20 67 65 6f 6d 65 74 72  ementary geometr
34c00 79 0a 2f 20 74 68 65 20 50 4f 49 4e 54 20 61 64  y./ the POINT ad
34c10 64 72 65 73 73 20 77 69 6c 6c 20 62 65 20 72 65  dress will be re
34c20 74 75 72 6e 65 64 0a 2f 20 6f 74 68 65 72 77 69  turned./ otherwi
34c30 73 65 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20  se NULL will be 
34c40 72 65 74 75 72 6e 65 64 0a 2a 2f 0a 20 20 20 20  returned.*/.    
34c50 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20  int cnt = 0;.   
34c60 20 67 61 69 61 50 6f 69 6e 74 50 74 72 20 70 6f   gaiaPointPtr po
34c70 69 6e 74 3b 0a 20 20 20 20 67 61 69 61 50 6f 69  int;.    gaiaPoi
34c80 6e 74 50 74 72 20 74 68 69 73 5f 70 6f 69 6e 74  ntPtr this_point
34c90 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 20   = NULL;.    if 
34ca0 28 21 67 65 6f 29 0a 09 72 65 74 75 72 6e 20 4e  (!geo)..return N
34cb0 55 4c 4c 3b 0a 20 20 20 20 69 66 20 28 67 65 6f  ULL;.    if (geo
34cc0 2d 3e 46 69 72 73 74 4c 69 6e 65 73 74 72 69 6e  ->FirstLinestrin
34cd0 67 20 7c 7c 20 67 65 6f 2d 3e 46 69 72 73 74 50  g || geo->FirstP
34ce0 6f 6c 79 67 6f 6e 29 0a 09 72 65 74 75 72 6e 20  olygon)..return 
34cf0 4e 55 4c 4c 3b 0a 20 20 20 20 70 6f 69 6e 74 20  NULL;.    point 
34d00 3d 20 67 65 6f 2d 3e 46 69 72 73 74 50 6f 69 6e  = geo->FirstPoin
34d10 74 3b 0a 20 20 20 20 77 68 69 6c 65 20 28 70 6f  t;.    while (po
34d20 69 6e 74 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  int).      {..  
34d30 2f 2a 20 63 6f 75 6e 74 69 6e 67 20 68 6f 77 20  /* counting how 
34d40 6d 61 6e 79 20 50 4f 49 4e 54 73 20 61 72 65 20  many POINTs are 
34d50 74 68 65 72 65 20 2a 2f 0a 09 20 20 63 6e 74 2b  there */..  cnt+
34d60 2b 3b 0a 09 20 20 74 68 69 73 5f 70 6f 69 6e 74  +;..  this_point
34d70 20 3d 20 70 6f 69 6e 74 3b 0a 09 20 20 70 6f 69   = point;..  poi
34d80 6e 74 20 3d 20 70 6f 69 6e 74 2d 3e 4e 65 78 74  nt = point->Next
34d90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
34da0 20 28 63 6e 74 20 3d 3d 20 31 20 26 26 20 74 68   (cnt == 1 && th
34db0 69 73 5f 70 6f 69 6e 74 29 0a 09 72 65 74 75 72  is_point)..retur
34dc0 6e 20 74 68 69 73 5f 70 6f 69 6e 74 3b 0a 20 20  n this_point;.  
34dd0 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 7d    return NULL;.}
34de0 0a 0a 73 74 61 74 69 63 20 67 61 69 61 4c 69 6e  ..static gaiaLin
34df0 65 73 74 72 69 6e 67 50 74 72 0a 73 69 6d 70 6c  estringPtr.simpl
34e00 65 4c 69 6e 65 73 74 72 69 6e 67 20 28 67 61 69  eLinestring (gai
34e10 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
34e20 29 0a 7b 0a 2f 2a 20 68 65 6c 70 65 72 20 66 75  ).{./* helper fu
34e30 6e 63 74 69 6f 6e 0a 2f 20 69 66 20 74 68 69 73  nction./ if this
34e40 20 47 45 4f 4d 45 54 52 59 20 63 6f 6e 74 61 69   GEOMETRY contai
34e50 6e 73 20 6f 6e 6c 79 20 6f 6e 65 20 4c 49 4e 45  ns only one LINE
34e60 53 54 52 49 4e 47 2c 20 61 6e 64 20 6e 6f 20 6f  STRING, and no o
34e70 74 68 65 72 20 65 6c 65 6d 65 6e 74 61 72 79 20  ther elementary 
34e80 67 65 6f 6d 65 74 72 79 0a 2f 20 74 68 65 20 4c  geometry./ the L
34e90 49 4e 45 53 54 52 49 4e 47 20 61 64 64 72 65 73  INESTRING addres
34ea0 73 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  s will be return
34eb0 65 64 0a 2f 20 6f 74 68 65 72 77 69 73 65 20 4e  ed./ otherwise N
34ec0 55 4c 4c 20 77 69 6c 6c 20 62 65 20 72 65 74 75  ULL will be retu
34ed0 72 6e 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20  rned.*/.    int 
34ee0 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 67 61 69  cnt = 0;.    gai
34ef0 61 4c 69 6e 65 73 74 72 69 6e 67 50 74 72 20 6c  aLinestringPtr l
34f00 69 6e 65 3b 0a 20 20 20 20 67 61 69 61 4c 69 6e  ine;.    gaiaLin
34f10 65 73 74 72 69 6e 67 50 74 72 20 74 68 69 73 5f  estringPtr this_
34f20 6c 69 6e 65 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  line = NULL;.   
34f30 20 69 66 20 28 21 67 65 6f 29 0a 09 72 65 74 75   if (!geo)..retu
34f40 72 6e 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 20  rn NULL;.    if 
34f50 28 67 65 6f 2d 3e 46 69 72 73 74 50 6f 69 6e 74  (geo->FirstPoint
34f60 20 7c 7c 20 67 65 6f 2d 3e 46 69 72 73 74 50 6f   || geo->FirstPo
34f70 6c 79 67 6f 6e 29 0a 09 72 65 74 75 72 6e 20 4e  lygon)..return N
34f80 55 4c 4c 3b 0a 20 20 20 20 6c 69 6e 65 20 3d 20  ULL;.    line = 
34f90 67 65 6f 2d 3e 46 69 72 73 74 4c 69 6e 65 73 74  geo->FirstLinest
34fa0 72 69 6e 67 3b 0a 20 20 20 20 77 68 69 6c 65 20  ring;.    while 
34fb0 28 6c 69 6e 65 29 0a 20 20 20 20 20 20 7b 0a 09  (line).      {..
34fc0 20 20 2f 2a 20 63 6f 75 6e 74 69 6e 67 20 68 6f    /* counting ho
34fd0 77 20 6d 61 6e 79 20 4c 49 4e 45 53 54 52 49 4e  w many LINESTRIN
34fe0 47 73 20 61 72 65 20 74 68 65 72 65 20 2a 2f 0a  Gs are there */.
34ff0 09 20 20 63 6e 74 2b 2b 3b 0a 09 20 20 74 68 69  .  cnt++;..  thi
35000 73 5f 6c 69 6e 65 20 3d 20 6c 69 6e 65 3b 0a 09  s_line = line;..
35010 20 20 6c 69 6e 65 20 3d 20 6c 69 6e 65 2d 3e 4e    line = line->N
35020 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ext;.      }.   
35030 20 69 66 20 28 63 6e 74 20 3d 3d 20 31 20 26 26   if (cnt == 1 &&
35040 20 74 68 69 73 5f 6c 69 6e 65 29 0a 09 72 65 74   this_line)..ret
35050 75 72 6e 20 74 68 69 73 5f 6c 69 6e 65 3b 0a 20  urn this_line;. 
35060 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a     return NULL;.
35070 7d 0a 0a 73 74 61 74 69 63 20 67 61 69 61 50 6f  }..static gaiaPo
35080 6c 79 67 6f 6e 50 74 72 0a 73 69 6d 70 6c 65 50  lygonPtr.simpleP
35090 6f 6c 79 67 6f 6e 20 28 67 61 69 61 47 65 6f 6d  olygon (gaiaGeom
350a0 43 6f 6c 6c 50 74 72 20 67 65 6f 29 0a 7b 0a 2f  CollPtr geo).{./
350b0 2a 20 68 65 6c 70 65 72 20 66 75 6e 63 74 69 6f  * helper functio
350c0 6e 0a 2f 20 69 66 20 74 68 69 73 20 47 45 4f 4d  n./ if this GEOM
350d0 45 54 52 59 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  ETRY contains on
350e0 6c 79 20 6f 6e 65 20 50 4f 4c 59 47 4f 4e 2c 20  ly one POLYGON, 
350f0 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 65 6c 65  and no other ele
35100 6d 65 6e 74 61 72 79 20 67 65 6f 6d 65 74 72 79  mentary geometry
35110 0a 2f 20 74 68 65 20 50 4f 4c 59 47 4f 4e 20 61  ./ the POLYGON a
35120 64 64 72 65 73 73 20 77 69 6c 6c 20 62 65 20 72  ddress will be r
35130 65 74 75 72 6e 65 64 0a 2f 20 6f 74 68 65 72 77  eturned./ otherw
35140 69 73 65 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65  ise NULL will be
35150 20 72 65 74 75 72 6e 65 64 0a 2a 2f 0a 20 20 20   returned.*/.   
35160 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
35170 20 20 67 61 69 61 50 6f 6c 79 67 6f 6e 50 74 72    gaiaPolygonPtr
35180 20 70 6f 6c 79 67 3b 0a 20 20 20 20 67 61 69 61   polyg;.    gaia
35190 50 6f 6c 79 67 6f 6e 50 74 72 20 74 68 69 73 5f  PolygonPtr this_
351a0 70 6f 6c 79 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20  polyg = NULL;.  
351b0 20 20 69 66 20 28 21 67 65 6f 29 0a 09 72 65 74    if (!geo)..ret
351c0 75 72 6e 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66  urn NULL;.    if
351d0 20 28 67 65 6f 2d 3e 46 69 72 73 74 50 6f 69 6e   (geo->FirstPoin
351e0 74 20 7c 7c 20 67 65 6f 2d 3e 46 69 72 73 74 4c  t || geo->FirstL
351f0 69 6e 65 73 74 72 69 6e 67 29 0a 09 72 65 74 75  inestring)..retu
35200 72 6e 20 4e 55 4c 4c 3b 0a 20 20 20 20 70 6f 6c  rn NULL;.    pol
35210 79 67 20 3d 20 67 65 6f 2d 3e 46 69 72 73 74 50  yg = geo->FirstP
35220 6f 6c 79 67 6f 6e 3b 0a 20 20 20 20 77 68 69 6c  olygon;.    whil
35230 65 20 28 70 6f 6c 79 67 29 0a 20 20 20 20 20 20  e (polyg).      
35240 7b 0a 09 20 20 2f 2a 20 63 6f 75 6e 74 69 6e 67  {..  /* counting
35250 20 68 6f 77 20 6d 61 6e 79 20 50 4f 4c 59 47 4f   how many POLYGO
35260 4e 73 20 61 72 65 20 74 68 65 72 65 20 2a 2f 0a  Ns are there */.
35270 09 20 20 63 6e 74 2b 2b 3b 0a 09 20 20 74 68 69  .  cnt++;..  thi
35280 73 5f 70 6f 6c 79 67 20 3d 20 70 6f 6c 79 67 3b  s_polyg = polyg;
35290 0a 09 20 20 70 6f 6c 79 67 20 3d 20 70 6f 6c 79  ..  polyg = poly
352a0 67 2d 3e 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  g->Next;.      }
352b0 0a 20 20 20 20 69 66 20 28 63 6e 74 20 3d 3d 20  .    if (cnt == 
352c0 31 20 26 26 20 74 68 69 73 5f 70 6f 6c 79 67 29  1 && this_polyg)
352d0 0a 09 72 65 74 75 72 6e 20 74 68 69 73 5f 70 6f  ..return this_po
352e0 6c 79 67 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  lyg;.    return 
352f0 4e 55 4c 4c 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  NULL;.}..static 
35300 76 6f 69 64 0a 66 6e 63 74 5f 41 73 54 65 78 74  void.fnct_AsText
35310 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
35320 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
35330 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
35340 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
35350 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
35360 0a 2f 20 41 73 54 65 78 74 28 42 4c 4f 42 20 65  ./ AsText(BLOB e
35370 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29  ncoded geometry)
35380 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65  ././ returns the
35390 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 57   corresponding W
353a0 4b 54 20 65 6e 63 6f 64 65 64 20 76 61 6c 75 65  KT encoded value
353b0 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
353c0 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
353d0 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
353e0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
353f0 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
35400 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65  ytes;.    int le
35410 6e 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75  n;.    gaiaOutBu
35420 66 66 65 72 20 6f 75 74 5f 62 75 66 3b 0a 20 20  ffer out_buf;.  
35430 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
35440 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
35450 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
35460 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
35470 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
35480 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
35490 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
354a0 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
354b0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
354c0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
354d0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
354e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
354f0 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
35500 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
35510 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
35520 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
35530 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
35540 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
35550 5d 29 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42  ]);.    gaiaOutB
35560 75 66 66 65 72 49 6e 69 74 69 61 6c 69 7a 65 20  ufferInitialize 
35570 28 26 6f 75 74 5f 62 75 66 29 3b 0a 20 20 20 20  (&out_buf);.    
35580 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  geo = gaiaFromSp
35590 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
355a0 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
355b0 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29  );.    if (!geo)
355c0 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
355d0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
355e0 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
355f0 7b 0a 09 20 20 67 61 69 61 4f 75 74 57 6b 74 20  {..  gaiaOutWkt 
35600 28 26 6f 75 74 5f 62 75 66 2c 20 67 65 6f 29 3b  (&out_buf, geo);
35610 0a 09 20 20 69 66 20 28 6f 75 74 5f 62 75 66 2e  ..  if (out_buf.
35620 45 72 72 6f 72 20 7c 7c 20 6f 75 74 5f 62 75 66  Error || out_buf
35630 2e 42 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c 29  .Buffer == NULL)
35640 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
35650 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
35660 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09  text);..  else..
35670 20 20 20 20 7b 0a 09 09 6c 65 6e 20 3d 20 6f 75      {...len = ou
35680 74 5f 62 75 66 2e 57 72 69 74 65 4f 66 66 73 65  t_buf.WriteOffse
35690 74 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73  t;...sqlite3_res
356a0 75 6c 74 5f 74 65 78 74 20 28 63 6f 6e 74 65 78  ult_text (contex
356b0 74 2c 20 6f 75 74 5f 62 75 66 2e 42 75 66 66 65  t, out_buf.Buffe
356c0 72 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09  r, len, free);..
356d0 09 6f 75 74 5f 62 75 66 2e 42 75 66 66 65 72 20  .out_buf.Buffer 
356e0 3d 20 4e 55 4c 4c 3b 0a 09 20 20 20 20 7d 0a 20  = NULL;..    }. 
356f0 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
35700 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
35710 29 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75  );.    gaiaOutBu
35720 66 66 65 72 52 65 73 65 74 20 28 26 6f 75 74 5f  fferReset (&out_
35730 62 75 66 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  buf);.}..static 
35740 76 6f 69 64 0a 66 6e 63 74 5f 41 73 57 6b 74 20  void.fnct_AsWkt 
35750 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
35760 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
35770 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
35780 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
35790 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
357a0 2f 20 41 73 57 6b 74 28 42 4c 4f 42 20 65 6e 63  / AsWkt(BLOB enc
357b0 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 20 5b 2c  oded geometry [,
357c0 20 49 6e 74 65 67 65 72 20 70 72 65 63 69 73 69   Integer precisi
357d0 6f 6e 5d 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73  on])././ returns
357e0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
357f0 6e 67 20 57 4b 54 20 65 6e 63 6f 64 65 64 20 76  ng WKT encoded v
35800 61 6c 75 65 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  alue./ or NULL i
35810 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65  f any error is e
35820 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20  ncountered.*/.  
35830 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
35840 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
35850 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e   n_bytes;.    in
35860 74 20 6c 65 6e 3b 0a 20 20 20 20 69 6e 74 20 70  t len;.    int p
35870 72 65 63 69 73 69 6f 6e 20 3d 20 31 35 3b 0a 20  recision = 15;. 
35880 20 20 20 67 61 69 61 4f 75 74 42 75 66 66 65 72     gaiaOutBuffer
35890 20 6f 75 74 5f 62 75 66 3b 0a 20 20 20 20 67 61   out_buf;.    ga
358a0 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
358b0 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41  o = NULL;.    GA
358c0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
358d0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
358e0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
358f0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
35900 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
35910 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
35920 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
35930 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
35940 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
35950 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 61 72      }.    if (ar
35960 67 63 20 3d 3d 20 32 29 0a 20 20 20 20 20 20 7b  gc == 2).      {
35970 0a 09 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f  ..  if (sqlite3_
35980 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
35990 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [1]) == SQLITE_I
359a0 4e 54 45 47 45 52 29 0a 09 20 20 20 20 20 20 70  NTEGER)..      p
359b0 72 65 63 69 73 69 6f 6e 20 3d 20 73 71 6c 69 74  recision = sqlit
359c0 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
359d0 67 76 5b 31 5d 29 3b 0a 09 20 20 65 6c 73 65 0a  gv[1]);..  else.
359e0 09 20 20 20 20 7b 0a 09 09 73 71 6c 69 74 65 33  .    {...sqlite3
359f0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
35a00 6e 74 65 78 74 29 3b 0a 09 09 72 65 74 75 72 6e  ntext);...return
35a10 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d  ;..    }.      }
35a20 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
35a30 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
35a40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
35a50 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
35a60 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
35a70 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
35a80 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
35a90 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
35aa0 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
35ab0 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
35ac0 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75 66  ;.    gaiaOutBuf
35ad0 66 65 72 49 6e 69 74 69 61 6c 69 7a 65 20 28 26  ferInitialize (&
35ae0 6f 75 74 5f 62 75 66 29 3b 0a 20 20 20 20 69 66  out_buf);.    if
35af0 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33   (!geo)..sqlite3
35b00 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
35b10 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
35b20 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 61 69 61  .      {..  gaia
35b30 4f 75 74 57 6b 74 53 74 72 69 63 74 20 28 26 6f  OutWktStrict (&o
35b40 75 74 5f 62 75 66 2c 20 67 65 6f 2c 20 70 72 65  ut_buf, geo, pre
35b50 63 69 73 69 6f 6e 29 3b 0a 09 20 20 69 66 20 28  cision);..  if (
35b60 6f 75 74 5f 62 75 66 2e 45 72 72 6f 72 20 7c 7c  out_buf.Error ||
35b70 20 6f 75 74 5f 62 75 66 2e 42 75 66 66 65 72 20   out_buf.Buffer 
35b80 3d 3d 20 4e 55 4c 4c 29 0a 09 20 20 20 20 20 20  == NULL)..      
35b90 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
35ba0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
35bb0 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09    else..    {...
35bc0 6c 65 6e 20 3d 20 6f 75 74 5f 62 75 66 2e 57 72  len = out_buf.Wr
35bd0 69 74 65 4f 66 66 73 65 74 3b 0a 09 09 73 71 6c  iteOffset;...sql
35be0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
35bf0 20 28 63 6f 6e 74 65 78 74 2c 20 6f 75 74 5f 62   (context, out_b
35c00 75 66 2e 42 75 66 66 65 72 2c 20 6c 65 6e 2c 20  uf.Buffer, len, 
35c10 66 72 65 65 29 3b 0a 09 09 6f 75 74 5f 62 75 66  free);...out_buf
35c20 2e 42 75 66 66 65 72 20 3d 20 4e 55 4c 4c 3b 0a  .Buffer = NULL;.
35c30 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20  .    }.      }. 
35c40 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
35c50 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20 20 67  oll (geo);.    g
35c60 61 69 61 4f 75 74 42 75 66 66 65 72 52 65 73 65  aiaOutBufferRese
35c70 74 20 28 26 6f 75 74 5f 62 75 66 29 3b 0a 7d 0a  t (&out_buf);.}.
35c80 0a 2f 2a 0a 2f 0a 2f 20 41 73 53 76 67 28 67 65  ./*././ AsSvg(ge
35c90 6f 6d 65 74 72 79 2c 5b 72 65 6c 61 74 69 76 65  ometry,[relative
35ca0 5d 2c 20 5b 70 72 65 63 69 73 69 6f 6e 5d 29 20  ], [precision]) 
35cb0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2f  implementation./
35cc0 0a 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f  .///////////////
35cd0 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f  ////////////////
35ce0 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f  ////////////////
35cf0 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 2f 0a 2f 0a  /////////////./.
35d00 2f 20 41 75 74 68 6f 72 3a 20 4b 6c 61 75 73 20  / Author: Klaus 
35d10 46 6f 65 72 73 74 65 72 20 6b 6c 61 75 73 2e 66  Foerster klaus.f
35d20 6f 65 72 73 74 65 72 40 73 76 67 2e 63 63 0a 2f  oerster@svg.cc./
35d30 20 76 65 72 73 69 6f 6e 20 30 2e 39 2e 20 32 30   version 0.9. 20
35d40 30 38 20 53 65 70 74 65 6d 62 65 72 20 32 31 0a  08 September 21.
35d50 20 2f 0a 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76   /. */..static v
35d60 6f 69 64 0a 66 6e 63 74 5f 41 73 53 76 67 20 28  oid.fnct_AsSvg (
35d70 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
35d80 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
35d90 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
35da0 75 65 20 2a 2a 20 61 72 67 76 2c 0a 09 20 20 20  ue ** argv,..   
35db0 20 69 6e 74 20 72 65 6c 61 74 69 76 65 2c 20 69   int relative, i
35dc0 6e 74 20 70 72 65 63 69 73 69 6f 6e 29 0a 7b 0a  nt precision).{.
35dd0 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
35de0 0a 20 20 20 41 73 53 76 67 28 42 4c 4f 42 20 65  .   AsSvg(BLOB e
35df0 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c  ncoded geometry,
35e00 20 5b 69 6e 74 20 72 65 6c 61 74 69 76 65 5d 2c   [int relative],
35e10 20 5b 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 5d   [int precision]
35e20 29 0a 20 20 20 72 65 74 75 72 6e 73 20 74 68 65  ).   returns the
35e30 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 53   corresponding S
35e40 56 47 20 65 6e 63 6f 64 65 64 20 76 61 6c 75 65  VG encoded value
35e50 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
35e60 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
35e70 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
35e80 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
35e90 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
35ea0 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  es;.    int len;
35eb0 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75 66 66  .    gaiaOutBuff
35ec0 65 72 20 6f 75 74 5f 62 75 66 3b 0a 20 20 20 20  er out_buf;.    
35ed0 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
35ee0 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
35ef0 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
35f00 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
35f10 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
35f20 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
35f30 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
35f40 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
35f50 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
35f60 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
35f70 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
35f80 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
35f90 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
35fa0 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
35fb0 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
35fc0 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
35fd0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
35fe0 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
35ff0 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61  ;.    geo = gaia
36000 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
36010 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
36020 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
36030 28 21 67 65 6f 29 0a 20 20 20 20 20 20 7b 0a 09  (!geo).      {..
36040 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
36050 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
36060 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
36070 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20    }.    else.   
36080 20 20 20 7b 0a 09 20 20 2f 2a 20 6d 61 6b 65 20     {..  /* make 
36090 73 75 72 65 20 72 65 6c 61 74 69 76 65 20 69 73  sure relative is
360a0 20 30 20 6f 72 20 31 20 2a 2f 0a 09 20 20 69 66   0 or 1 */..  if
360b0 20 28 72 65 6c 61 74 69 76 65 20 3e 20 30 29 0a   (relative > 0).
360c0 09 20 20 20 20 20 20 72 65 6c 61 74 69 76 65 20  .      relative 
360d0 3d 20 31 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20  = 1;..  else..  
360e0 20 20 20 20 72 65 6c 61 74 69 76 65 20 3d 20 30      relative = 0
360f0 3b 0a 09 20 20 2f 2a 20 6d 61 6b 65 20 73 75 72  ;..  /* make sur
36100 65 20 70 72 65 63 69 73 69 6f 6e 20 69 73 20 62  e precision is b
36110 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 35 20  etween 0 and 15 
36120 2d 20 64 65 66 61 75 6c 74 20 74 6f 20 36 20 69  - default to 6 i
36130 66 20 61 62 73 65 6e 74 20 2a 2f 0a 09 20 20 69  f absent */..  i
36140 66 20 28 70 72 65 63 69 73 69 6f 6e 20 3e 20 47  f (precision > G
36150 41 49 41 5f 53 56 47 5f 44 45 46 41 55 4c 54 5f  AIA_SVG_DEFAULT_
36160 4d 41 58 5f 50 52 45 43 49 53 49 4f 4e 29 0a 09  MAX_PRECISION)..
36170 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
36180 3d 20 47 41 49 41 5f 53 56 47 5f 44 45 46 41 55  = GAIA_SVG_DEFAU
36190 4c 54 5f 4d 41 58 5f 50 52 45 43 49 53 49 4f 4e  LT_MAX_PRECISION
361a0 3b 0a 09 20 20 69 66 20 28 70 72 65 63 69 73 69  ;..  if (precisi
361b0 6f 6e 20 3c 20 30 29 0a 09 20 20 20 20 20 20 70  on < 0)..      p
361c0 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 09 20  recision = 0;.. 
361d0 20 2f 2a 20 70 72 6f 64 75 63 65 20 53 56 47 2d   /* produce SVG-
361e0 6e 6f 74 61 74 69 6f 6e 20 2d 20 61 63 74 75 61  notation - actua
361f0 6c 20 77 6f 72 6b 20 69 73 20 64 6f 6e 65 20 69  l work is done i
36200 6e 20 67 61 69 61 67 65 6f 2f 67 67 5f 77 6b 74  n gaiageo/gg_wkt
36210 2e 63 20 2a 2f 0a 09 20 20 67 61 69 61 4f 75 74  .c */..  gaiaOut
36220 42 75 66 66 65 72 49 6e 69 74 69 61 6c 69 7a 65  BufferInitialize
36230 20 28 26 6f 75 74 5f 62 75 66 29 3b 0a 09 20 20   (&out_buf);..  
36240 67 61 69 61 4f 75 74 53 76 67 20 28 26 6f 75 74  gaiaOutSvg (&out
36250 5f 62 75 66 2c 20 67 65 6f 2c 20 72 65 6c 61 74  _buf, geo, relat
36260 69 76 65 2c 20 70 72 65 63 69 73 69 6f 6e 29 3b  ive, precision);
36270 0a 09 20 20 69 66 20 28 6f 75 74 5f 62 75 66 2e  ..  if (out_buf.
36280 45 72 72 6f 72 20 7c 7c 20 6f 75 74 5f 62 75 66  Error || out_buf
36290 2e 42 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c 29  .Buffer == NULL)
362a0 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
362b0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
362c0 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09  text);..  else..
362d0 20 20 20 20 7b 0a 09 09 6c 65 6e 20 3d 20 6f 75      {...len = ou
362e0 74 5f 62 75 66 2e 57 72 69 74 65 4f 66 66 73 65  t_buf.WriteOffse
362f0 74 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73  t;...sqlite3_res
36300 75 6c 74 5f 74 65 78 74 20 28 63 6f 6e 74 65 78  ult_text (contex
36310 74 2c 20 6f 75 74 5f 62 75 66 2e 42 75 66 66 65  t, out_buf.Buffe
36320 72 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09  r, len, free);..
36330 09 6f 75 74 5f 62 75 66 2e 42 75 66 66 65 72 20  .out_buf.Buffer 
36340 3d 20 4e 55 4c 4c 3b 0a 09 20 20 20 20 7d 0a 20  = NULL;..    }. 
36350 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
36360 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
36370 29 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75  );.    gaiaOutBu
36380 66 66 65 72 52 65 73 65 74 20 28 26 6f 75 74 5f  fferReset (&out_
36390 62 75 66 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  buf);.}..static 
363a0 76 6f 69 64 0a 66 6e 63 74 5f 41 73 53 76 67 31  void.fnct_AsSvg1
363b0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
363c0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
363d0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
363e0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
363f0 2f 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 6f 75  /* called withou
36400 74 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 72 67  t additional arg
36410 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 66 6e  uments */.    fn
36420 63 74 5f 41 73 53 76 67 20 28 63 6f 6e 74 65 78  ct_AsSvg (contex
36430 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47  t, argc, argv, G
36440 41 49 41 5f 53 56 47 5f 44 45 46 41 55 4c 54 5f  AIA_SVG_DEFAULT_
36450 52 45 4c 41 54 49 56 45 2c 0a 09 09 47 41 49 41  RELATIVE,...GAIA
36460 5f 53 56 47 5f 44 45 46 41 55 4c 54 5f 50 52 45  _SVG_DEFAULT_PRE
36470 43 49 53 49 4f 4e 29 3b 0a 7d 0a 0a 73 74 61 74  CISION);.}..stat
36480 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41 73 53  ic void.fnct_AsS
36490 76 67 32 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  vg2 (sqlite3_con
364a0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
364b0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
364c0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
364d0 0a 7b 0a 2f 2a 20 63 61 6c 6c 65 64 20 77 69 74  .{./* called wit
364e0 68 20 72 65 6c 61 74 69 76 65 2d 73 77 69 74 63  h relative-switc
364f0 68 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  h */.    if (sql
36500 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
36510 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
36520 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 09 66 6e  ITE_INTEGER)..fn
36530 63 74 5f 41 73 53 76 67 20 28 63 6f 6e 74 65 78  ct_AsSvg (contex
36540 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 73  t, argc, argv, s
36550 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
36560 20 28 61 72 67 76 5b 31 5d 29 2c 0a 09 09 20 20   (argv[1]),...  
36570 20 20 47 41 49 41 5f 53 56 47 5f 44 45 46 41 55    GAIA_SVG_DEFAU
36580 4c 54 5f 50 52 45 43 49 53 49 4f 4e 29 3b 0a 20  LT_PRECISION);. 
36590 20 20 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33     else..sqlite3
365a0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
365b0 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  ntext);.}..stati
365c0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41 73 53 76  c void.fnct_AsSv
365d0 67 33 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  g3 (sqlite3_cont
365e0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
365f0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
36600 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
36610 7b 0a 2f 2a 20 63 61 6c 6c 65 64 20 77 69 74 68  {./* called with
36620 20 72 65 6c 61 74 69 76 65 2d 73 77 69 74 63 68   relative-switch
36630 20 61 6e 64 20 70 72 65 63 69 73 69 6f 6e 2d 61   and precision-a
36640 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 69  rgument */.    i
36650 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
36660 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20  _type (argv[1]) 
36670 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
36680 52 0a 09 26 26 20 73 71 6c 69 74 65 33 5f 76 61  R..&& sqlite3_va
36690 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32  lue_type (argv[2
366a0 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
366b0 45 47 45 52 29 0a 09 66 6e 63 74 5f 41 73 53 76  EGER)..fnct_AsSv
366c0 67 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  g (context, argc
366d0 2c 20 61 72 67 76 2c 20 73 71 6c 69 74 65 33 5f  , argv, sqlite3_
366e0 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
366f0 31 5d 29 2c 0a 09 09 20 20 20 20 73 71 6c 69 74  1]),...    sqlit
36700 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
36710 67 76 5b 32 5d 29 29 3b 0a 20 20 20 20 65 6c 73  gv[2]));.    els
36720 65 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e..sqlite3_resul
36730 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
36740 3b 0a 7d 0a 0a 2f 2a 20 45 4e 44 20 6f 66 20 4b  ;.}../* END of K
36750 6c 61 75 73 20 46 6f 65 72 73 74 65 72 20 41 73  laus Foerster As
36760 53 76 67 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61  Svg() implementa
36770 74 69 6f 6e 20 2a 2f 0a 0a 73 74 61 74 69 63 20  tion */..static 
36780 76 6f 69 64 0a 70 72 6f 6a 5f 70 61 72 61 6d 73  void.proj_params
36790 20 28 73 71 6c 69 74 65 33 20 2a 20 73 71 6c 69   (sqlite3 * sqli
367a0 74 65 2c 20 69 6e 74 20 73 72 69 64 2c 20 63 68  te, int srid, ch
367b0 61 72 20 2a 70 72 6f 6a 5f 70 61 72 61 6d 73 29  ar *proj_params)
367c0 0a 7b 0a 2f 2a 20 72 65 74 72 69 76 65 73 20 74  .{./* retrives t
367d0 68 65 20 50 52 4f 4a 20 70 61 72 61 6d 73 20 66  he PROJ params f
367e0 72 6f 6d 20 53 50 41 54 49 41 4c 5f 53 59 53 5f  rom SPATIAL_SYS_
367f0 52 45 46 20 74 61 62 6c 65 2c 20 69 66 20 70 6f  REF table, if po
36800 73 73 69 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68  ssible */.    ch
36810 61 72 20 73 71 6c 5b 32 35 36 5d 3b 0a 20 20 20  ar sql[256];.   
36820 20 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 73 3b   char **results;
36830 0a 20 20 20 20 69 6e 74 20 72 6f 77 73 3b 0a 20  .    int rows;. 
36840 20 20 20 69 6e 74 20 63 6f 6c 75 6d 6e 73 3b 0a     int columns;.
36850 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69      int i;.    i
36860 6e 74 20 72 65 74 3b 0a 20 20 20 20 63 68 61 72  nt ret;.    char
36870 20 2a 65 72 72 4d 73 67 20 3d 20 4e 55 4c 4c 3b   *errMsg = NULL;
36880 0a 20 20 20 20 2a 70 72 6f 6a 5f 70 61 72 61 6d  .    *proj_param
36890 73 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 73 70  s = '\0';.    sp
368a0 72 69 6e 74 66 20 28 73 71 6c 2c 0a 09 20 20 20  rintf (sql,..   
368b0 20 20 22 53 45 4c 45 43 54 20 70 72 6f 6a 34 74    "SELECT proj4t
368c0 65 78 74 20 46 52 4f 4d 20 73 70 61 74 69 61 6c  ext FROM spatial
368d0 5f 72 65 66 5f 73 79 73 20 57 48 45 52 45 20 73  _ref_sys WHERE s
368e0 72 69 64 20 3d 20 25 64 22 2c 20 73 72 69 64 29  rid = %d", srid)
368f0 3b 0a 20 20 20 20 72 65 74 20 3d 20 73 71 6c 69  ;.    ret = sqli
36900 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 28 73  te3_get_table (s
36910 71 6c 69 74 65 2c 20 73 71 6c 2c 20 26 72 65 73  qlite, sql, &res
36920 75 6c 74 73 2c 20 26 72 6f 77 73 2c 20 26 63 6f  ults, &rows, &co
36930 6c 75 6d 6e 73 2c 20 26 65 72 72 4d 73 67 29 3b  lumns, &errMsg);
36940 0a 20 20 20 20 69 66 20 28 72 65 74 20 21 3d 20  .    if (ret != 
36950 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 20 20  SQLITE_OK).     
36960 20 7b 0a 09 20 20 73 70 61 74 69 61 6c 69 74 65   {..  spatialite
36970 5f 65 20 28 22 75 6e 6b 6e 6f 77 6e 20 53 52 49  _e ("unknown SRI
36980 44 3a 20 25 64 5c 74 3c 25 73 3e 5c 6e 22 2c 20  D: %d\t<%s>\n", 
36990 73 72 69 64 2c 20 65 72 72 4d 73 67 29 3b 0a 09  srid, errMsg);..
369a0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 28    sqlite3_free (
369b0 65 72 72 4d 73 67 29 3b 0a 09 20 20 72 65 74 75  errMsg);..  retu
369c0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
369d0 66 6f 72 20 28 69 20 3d 20 31 3b 20 69 20 3c 3d  for (i = 1; i <=
369e0 20 72 6f 77 73 3b 20 69 2b 2b 29 0a 09 73 74 72   rows; i++)..str
369f0 63 70 79 20 28 70 72 6f 6a 5f 70 61 72 61 6d 73  cpy (proj_params
36a00 2c 20 72 65 73 75 6c 74 73 5b 28 69 20 2a 20 63  , results[(i * c
36a10 6f 6c 75 6d 6e 73 29 5d 29 3b 0a 20 20 20 20 69  olumns)]);.    i
36a20 66 20 28 2a 70 72 6f 6a 5f 70 61 72 61 6d 73 20  f (*proj_params 
36a30 3d 3d 20 27 5c 30 27 29 0a 09 73 70 61 74 69 61  == '\0')..spatia
36a40 6c 69 74 65 5f 65 20 28 22 75 6e 6b 6e 6f 77 6e  lite_e ("unknown
36a50 20 53 52 49 44 3a 20 25 64 5c 6e 22 2c 20 73 72   SRID: %d\n", sr
36a60 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
36a70 5f 66 72 65 65 5f 74 61 62 6c 65 20 28 72 65 73  _free_table (res
36a80 75 6c 74 73 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  ults);.}..#ifnde
36a90 66 20 4f 4d 49 54 5f 50 52 4f 4a 09 09 2f 2a 20  f OMIT_PROJ../* 
36aa0 50 52 4f 4a 2e 34 20 69 73 20 73 74 72 69 63 74  PROJ.4 is strict
36ab0 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  ly required to s
36ac0 75 70 70 6f 72 74 20 4b 4d 4c 20 2a 2f 0a 73 74  upport KML */.st
36ad0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41  atic void.fnct_A
36ae0 73 4b 6d 6c 31 20 28 73 71 6c 69 74 65 33 5f 63  sKml1 (sqlite3_c
36af0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
36b00 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
36b10 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
36b20 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
36b30 74 69 6f 6e 3a 0a 2f 20 41 73 4b 6d 6c 28 42 4c  tion:./ AsKml(BL
36b40 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65  OB encoded geome
36b50 74 72 79 20 5b 2c 20 49 6e 74 65 67 65 72 20 70  try [, Integer p
36b60 72 65 63 69 73 69 6f 6e 5d 29 0a 2f 0a 2f 20 72  recision])././ r
36b70 65 74 75 72 6e 73 20 74 68 65 20 63 6f 72 72 65  eturns the corre
36b80 73 70 6f 6e 64 69 6e 67 20 27 62 61 72 65 20 67  sponding 'bare g
36b90 65 6f 6d 27 20 4b 4d 4c 20 72 65 70 72 65 73 65  eom' KML represe
36ba0 6e 74 61 74 69 6f 6e 20 0a 2f 20 6f 72 20 4e 55  ntation ./ or NU
36bb0 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
36bc0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
36bd0 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
36be0 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20  har *p_blob;.   
36bf0 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20   int n_bytes;.  
36c00 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 67    int len;.    g
36c10 61 69 61 4f 75 74 42 75 66 66 65 72 20 6f 75 74  aiaOutBuffer out
36c20 5f 62 75 66 3b 0a 20 20 20 20 67 61 69 61 47 65  _buf;.    gaiaGe
36c30 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
36c40 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65  NULL;.    gaiaGe
36c50 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 5f 77 67  omCollPtr geo_wg
36c60 73 38 34 3b 0a 20 20 20 20 63 68 61 72 20 70 72  s84;.    char pr
36c70 6f 6a 5f 66 72 6f 6d 5b 32 30 34 38 5d 3b 0a 20  oj_from[2048];. 
36c80 20 20 20 63 68 61 72 20 70 72 6f 6a 5f 74 6f 5b     char proj_to[
36c90 32 30 34 38 5d 3b 0a 20 20 20 20 69 6e 74 20 70  2048];.    int p
36ca0 72 65 63 69 73 69 6f 6e 20 3d 20 31 35 3b 0a 20  recision = 15;. 
36cb0 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69     sqlite3 *sqli
36cc0 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  te = sqlite3_con
36cd0 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28  text_db_handle (
36ce0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41  context);.    GA
36cf0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
36d00 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
36d10 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
36d20 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
36d30 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
36d40 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
36d50 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
36d60 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
36d70 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
36d80 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
36d90 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
36da0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
36db0 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
36dc0 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
36dd0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
36de0 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
36df0 20 20 20 20 69 66 20 28 61 72 67 63 20 3d 3d 20      if (argc == 
36e00 32 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66  2).      {..  if
36e10 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
36e20 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d  type (argv[1]) =
36e30 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
36e40 29 0a 09 20 20 20 20 20 20 70 72 65 63 69 73 69  )..      precisi
36e50 6f 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  on = sqlite3_val
36e60 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
36e70 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b  ;..  else..    {
36e80 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ...sqlite3_resul
36e90 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
36ea0 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20 20 20  ;...return;..   
36eb0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67   }.      }.    g
36ec0 61 69 61 4f 75 74 42 75 66 66 65 72 49 6e 69 74  aiaOutBufferInit
36ed0 69 61 6c 69 7a 65 20 28 26 6f 75 74 5f 62 75 66  ialize (&out_buf
36ee0 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69  );.    geo = gai
36ef0 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42  aFromSpatiaLiteB
36f00 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20  lobWkb (p_blob, 
36f10 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66  n_bytes);.    if
36f20 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33   (!geo)..sqlite3
36f30 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
36f40 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
36f50 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20 28  .      {..  if (
36f60 67 65 6f 2d 3e 53 72 69 64 20 3d 3d 20 34 33 32  geo->Srid == 432
36f70 36 29 0a 09 20 20 20 20 20 20 3b 09 09 09 2f 2a  6)..      ;.../*
36f80 20 61 6c 72 65 61 64 79 20 57 47 53 38 34 20 2a   already WGS84 *
36f90 2f 0a 09 20 20 65 6c 73 65 20 69 66 20 28 67 65  /..  else if (ge
36fa0 6f 2d 3e 53 72 69 64 20 3c 3d 20 30 29 0a 09 20  o->Srid <= 0).. 
36fb0 20 20 20 7b 0a 09 09 2f 2a 20 75 6e 6b 6e 6f 77     {.../* unknow
36fc0 6e 20 53 52 49 44 3a 20 67 69 76 69 6e 67 20 75  n SRID: giving u
36fd0 70 20 2a 2f 0a 09 09 73 71 6c 69 74 65 33 5f 72  p */...sqlite3_r
36fe0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
36ff0 65 78 74 29 3b 0a 09 09 67 6f 74 6f 20 73 74 6f  ext);...goto sto
37000 70 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c 73  p;..    }..  els
37010 65 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 61 74  e..    {.../* at
37020 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 65 70 72  tempting to repr
37030 6f 6a 65 63 74 20 69 6e 74 6f 20 57 47 53 38 34  oject into WGS84
37040 20 2a 2f 0a 09 09 70 72 6f 6a 5f 70 61 72 61 6d   */...proj_param
37050 73 20 28 73 71 6c 69 74 65 2c 20 67 65 6f 2d 3e  s (sqlite, geo->
37060 53 72 69 64 2c 20 70 72 6f 6a 5f 66 72 6f 6d 29  Srid, proj_from)
37070 3b 0a 09 09 70 72 6f 6a 5f 70 61 72 61 6d 73 20  ;...proj_params 
37080 28 73 71 6c 69 74 65 2c 20 34 33 32 36 2c 20 70  (sqlite, 4326, p
37090 72 6f 6a 5f 74 6f 29 3b 0a 09 09 69 66 20 28 2a  roj_to);...if (*
370a0 70 72 6f 6a 5f 74 6f 20 3d 3d 20 27 5c 30 27 20  proj_to == '\0' 
370b0 7c 7c 20 2a 70 72 6f 6a 5f 66 72 6f 6d 20 3d 3d  || *proj_from ==
370c0 20 27 5c 30 27 29 0a 09 09 20 20 7b 0a 09 09 20   '\0')...  {... 
370d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
370e0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
370f0 74 29 3b 0a 09 09 20 20 20 20 20 20 67 6f 74 6f  t);...      goto
37100 20 73 74 6f 70 3b 0a 09 09 20 20 7d 0a 09 09 67   stop;...  }...g
37110 65 6f 5f 77 67 73 38 34 20 3d 20 67 61 69 61 54  eo_wgs84 = gaiaT
37120 72 61 6e 73 66 6f 72 6d 20 28 67 65 6f 2c 20 70  ransform (geo, p
37130 72 6f 6a 5f 66 72 6f 6d 2c 20 70 72 6f 6a 5f 74  roj_from, proj_t
37140 6f 29 3b 0a 09 09 69 66 20 28 21 67 65 6f 5f 77  o);...if (!geo_w
37150 67 73 38 34 29 0a 09 09 20 20 7b 0a 09 09 20 20  gs84)...  {...  
37160 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
37170 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
37180 29 3b 0a 09 09 20 20 20 20 20 20 67 6f 74 6f 20  );...      goto 
37190 73 74 6f 70 3b 0a 09 09 20 20 7d 0a 09 09 2f 2a  stop;...  }.../*
371a0 20 6f 6b 2c 20 72 65 70 72 6f 6a 65 63 74 69 6f   ok, reprojectio
371b0 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  n was successful
371c0 20 2a 2f 0a 09 09 67 61 69 61 46 72 65 65 47 65   */...gaiaFreeGe
371d0 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09 09  omColl (geo);...
371e0 67 65 6f 20 3d 20 67 65 6f 5f 77 67 73 38 34 3b  geo = geo_wgs84;
371f0 0a 09 20 20 20 20 7d 0a 09 20 20 2f 2a 20 70 72  ..    }..  /* pr
37200 6f 64 75 63 65 20 4b 4d 4c 2d 6e 6f 74 61 74 69  oduce KML-notati
37210 6f 6e 20 2d 20 61 63 74 75 61 6c 20 77 6f 72 6b  on - actual work
37220 20 69 73 20 64 6f 6e 65 20 69 6e 20 67 61 69 61   is done in gaia
37230 67 65 6f 2f 67 67 5f 77 6b 74 2e 63 20 2a 2f 0a  geo/gg_wkt.c */.
37240 09 20 20 67 61 69 61 4f 75 74 42 61 72 65 4b 6d  .  gaiaOutBareKm
37250 6c 20 28 26 6f 75 74 5f 62 75 66 2c 20 67 65 6f  l (&out_buf, geo
37260 2c 20 70 72 65 63 69 73 69 6f 6e 29 3b 0a 09 20  , precision);.. 
37270 20 69 66 20 28 6f 75 74 5f 62 75 66 2e 45 72 72   if (out_buf.Err
37280 6f 72 20 7c 7c 20 6f 75 74 5f 62 75 66 2e 42 75  or || out_buf.Bu
37290 66 66 65 72 20 3d 3d 20 4e 55 4c 4c 29 0a 09 20  ffer == NULL).. 
372a0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
372b0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
372c0 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20  t);..  else..   
372d0 20 7b 0a 09 09 6c 65 6e 20 3d 20 6f 75 74 5f 62   {...len = out_b
372e0 75 66 2e 57 72 69 74 65 4f 66 66 73 65 74 3b 0a  uf.WriteOffset;.
372f0 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
37300 5f 74 65 78 74 20 28 63 6f 6e 74 65 78 74 2c 20  _text (context, 
37310 6f 75 74 5f 62 75 66 2e 42 75 66 66 65 72 2c 20  out_buf.Buffer, 
37320 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09 6f 75  len, free);...ou
37330 74 5f 62 75 66 2e 42 75 66 66 65 72 20 3d 20 4e  t_buf.Buffer = N
37340 55 4c 4c 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20  ULL;..    }.    
37350 20 20 7d 0a 20 20 73 74 6f 70 3a 0a 20 20 20 20    }.  stop:.    
37360 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
37370 20 28 67 65 6f 29 3b 0a 20 20 20 20 67 61 69 61   (geo);.    gaia
37380 4f 75 74 42 75 66 66 65 72 52 65 73 65 74 20 28  OutBufferReset (
37390 26 6f 75 74 5f 62 75 66 29 3b 0a 7d 0a 0a 73 74  &out_buf);.}..st
373a0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41  atic void.fnct_A
373b0 73 4b 6d 6c 33 20 28 73 71 6c 69 74 65 33 5f 63  sKml3 (sqlite3_c
373c0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
373d0 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
373e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
373f0 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
37400 74 69 6f 6e 3a 0a 2f 20 41 73 4b 6d 6c 28 41 6e  tion:./ AsKml(An
37410 79 74 68 69 6e 67 20 6e 61 6d 65 2c 20 41 6e 79  ything name, Any
37420 74 68 69 6e 67 20 64 65 73 63 72 69 70 74 69 6f  thing descriptio
37430 6e 2c 20 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20  n, BLOB encoded 
37440 67 65 6f 6d 65 74 72 79 20 5b 2c 20 49 6e 74 65  geometry [, Inte
37450 67 65 72 20 70 72 65 63 69 73 69 6f 6e 5d 29 0a  ger precision]).
37460 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20  /./ returns the 
37470 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 27 66  corresponding 'f
37480 75 6c 6c 27 20 4b 4d 4c 20 72 65 70 72 65 73 65  ull' KML represe
37490 6e 74 61 74 69 6f 6e 20 0a 2f 20 6f 72 20 4e 55  ntation ./ or NU
374a0 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
374b0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
374c0 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
374d0 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20  har *p_blob;.   
374e0 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20   int n_bytes;.  
374f0 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 67    int len;.    g
37500 61 69 61 4f 75 74 42 75 66 66 65 72 20 6f 75 74  aiaOutBuffer out
37510 5f 62 75 66 3b 0a 20 20 20 20 67 61 69 61 47 65  _buf;.    gaiaGe
37520 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
37530 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65  NULL;.    gaiaGe
37540 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 5f 77 67  omCollPtr geo_wg
37550 73 38 34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  s84;.    sqlite3
37560 5f 69 6e 74 36 34 20 69 6e 74 5f 76 61 6c 75 65  _int64 int_value
37570 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 64 62 6c  ;.    double dbl
37580 5f 76 61 6c 75 65 3b 0a 20 20 20 20 63 6f 6e 73  _value;.    cons
37590 74 20 63 68 61 72 20 2a 6e 61 6d 65 3b 0a 20 20  t char *name;.  
375a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 65    const char *de
375b0 73 63 3b 0a 20 20 20 20 63 68 61 72 20 2a 6e 61  sc;.    char *na
375c0 6d 65 5f 6d 61 6c 6c 6f 63 20 3d 20 4e 55 4c 4c  me_malloc = NULL
375d0 3b 0a 20 20 20 20 63 68 61 72 20 2a 64 65 73 63  ;.    char *desc
375e0 5f 6d 61 6c 6c 6f 63 20 3d 20 4e 55 4c 4c 3b 0a  _malloc = NULL;.
375f0 20 20 20 20 63 68 61 72 20 64 75 6d 6d 79 5b 31      char dummy[1
37600 32 38 5d 3b 0a 20 20 20 20 63 68 61 72 20 70 72  28];.    char pr
37610 6f 6a 5f 66 72 6f 6d 5b 32 30 34 38 5d 3b 0a 20  oj_from[2048];. 
37620 20 20 20 63 68 61 72 20 70 72 6f 6a 5f 74 6f 5b     char proj_to[
37630 32 30 34 38 5d 3b 0a 20 20 20 20 69 6e 74 20 70  2048];.    int p
37640 72 65 63 69 73 69 6f 6e 20 3d 20 31 35 3b 0a 20  recision = 15;. 
37650 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69     sqlite3 *sqli
37660 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  te = sqlite3_con
37670 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28  text_db_handle (
37680 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41  context);.    GA
37690 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
376a0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
376b0 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 20 28   */.    switch (
376c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
376d0 70 65 20 28 61 72 67 76 5b 30 5d 29 29 0a 20 20  pe (argv[0])).  
376e0 20 20 20 20 7b 0a 20 20 20 20 20 20 63 61 73 65      {.      case
376f0 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 0a 09 20   SQLITE_TEXT:.. 
37700 20 6e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63   name = (const c
37710 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
37720 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b  alue_text (argv[
37730 30 5d 29 3b 0a 09 20 20 6c 65 6e 20 3d 20 73 74  0]);..  len = st
37740 72 6c 65 6e 20 28 6e 61 6d 65 29 3b 0a 09 20 20  rlen (name);..  
37750 6e 61 6d 65 5f 6d 61 6c 6c 6f 63 20 3d 20 6d 61  name_malloc = ma
37760 6c 6c 6f 63 20 28 6c 65 6e 20 2b 20 31 29 3b 0a  lloc (len + 1);.
37770 09 20 20 73 74 72 63 70 79 20 28 6e 61 6d 65 5f  .  strcpy (name_
37780 6d 61 6c 6c 6f 63 2c 20 6e 61 6d 65 29 3b 0a 09  malloc, name);..
37790 20 20 6e 61 6d 65 20 3d 20 6e 61 6d 65 5f 6d 61    name = name_ma
377a0 6c 6c 6f 63 3b 0a 09 20 20 62 72 65 61 6b 3b 0a  lloc;..  break;.
377b0 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
377c0 45 5f 49 4e 54 45 47 45 52 3a 0a 09 20 20 69 6e  E_INTEGER:..  in
377d0 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  t_value = sqlite
377e0 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 20 28 61  3_value_int64 (a
377f0 72 67 76 5b 30 5d 29 3b 0a 23 69 66 20 64 65 66  rgv[0]);.#if def
37800 69 6e 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20  ined(_WIN32) || 
37810 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33  defined(__MINGW3
37820 32 5f 5f 29 0a 2f 2a 20 43 41 56 45 41 54 3a 20  2__)./* CAVEAT: 
37830 4d 24 20 72 75 6e 74 69 6d 65 20 64 6f 65 73 6e  M$ runtime doesn
37840 27 74 20 73 75 70 70 6f 72 74 73 20 25 6c 6c 64  't supports %lld
37850 20 66 6f 72 20 36 34 20 62 69 74 73 20 2a 2f 0a   for 64 bits */.
37860 09 20 20 73 70 72 69 6e 74 66 20 28 64 75 6d 6d  .  sprintf (dumm
37870 79 2c 20 22 25 49 36 34 64 22 2c 20 69 6e 74 5f  y, "%I64d", int_
37880 76 61 6c 75 65 29 3b 0a 23 65 6c 73 65 0a 09 20  value);.#else.. 
37890 20 73 70 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c   sprintf (dummy,
378a0 20 22 25 6c 6c 64 22 2c 20 69 6e 74 5f 76 61 6c   "%lld", int_val
378b0 75 65 29 3b 0a 23 65 6e 64 69 66 0a 09 20 20 6c  ue);.#endif..  l
378c0 65 6e 20 3d 20 73 74 72 6c 65 6e 20 28 64 75 6d  en = strlen (dum
378d0 6d 79 29 3b 0a 09 20 20 6e 61 6d 65 5f 6d 61 6c  my);..  name_mal
378e0 6c 6f 63 20 3d 20 6d 61 6c 6c 6f 63 20 28 6c 65  loc = malloc (le
378f0 6e 20 2b 20 31 29 3b 0a 09 20 20 73 74 72 63 70  n + 1);..  strcp
37900 79 20 28 6e 61 6d 65 5f 6d 61 6c 6c 6f 63 2c 20  y (name_malloc, 
37910 64 75 6d 6d 79 29 3b 0a 09 20 20 6e 61 6d 65 20  dummy);..  name 
37920 3d 20 6e 61 6d 65 5f 6d 61 6c 6c 6f 63 3b 0a 09  = name_malloc;..
37930 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
37940 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ase SQLITE_FLOAT
37950 3a 0a 09 20 20 64 62 6c 5f 76 61 6c 75 65 20 3d  :..  dbl_value =
37960 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
37970 6f 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 3b  ouble (argv[0]);
37980 0a 09 20 20 73 70 72 69 6e 74 66 20 28 64 75 6d  ..  sprintf (dum
37990 6d 79 2c 20 22 25 31 2e 36 66 22 2c 20 64 62 6c  my, "%1.6f", dbl
379a0 5f 76 61 6c 75 65 29 3b 0a 09 20 20 6c 65 6e 20  _value);..  len 
379b0 3d 20 73 74 72 6c 65 6e 20 28 64 75 6d 6d 79 29  = strlen (dummy)
379c0 3b 0a 09 20 20 6e 61 6d 65 5f 6d 61 6c 6c 6f 63  ;..  name_malloc
379d0 20 3d 20 6d 61 6c 6c 6f 63 20 28 6c 65 6e 20 2b   = malloc (len +
379e0 20 31 29 3b 0a 09 20 20 73 74 72 63 70 79 20 28   1);..  strcpy (
379f0 6e 61 6d 65 5f 6d 61 6c 6c 6f 63 2c 20 64 75 6d  name_malloc, dum
37a00 6d 79 29 3b 0a 09 20 20 6e 61 6d 65 20 3d 20 6e  my);..  name = n
37a10 61 6d 65 5f 6d 61 6c 6c 6f 63 3b 0a 09 20 20 62  ame_malloc;..  b
37a20 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
37a30 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 09 20   SQLITE_BLOB:.. 
37a40 20 6e 61 6d 65 20 3d 20 22 42 4c 4f 42 22 3b 0a   name = "BLOB";.
37a50 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  .  break;.      
37a60 64 65 66 61 75 6c 74 3a 0a 09 20 20 6e 61 6d 65  default:..  name
37a70 20 3d 20 22 4e 55 4c 4c 22 3b 0a 09 20 20 62 72   = "NULL";..  br
37a80 65 61 6b 3b 0a 20 20 20 20 20 20 7d 3b 0a 20 20  eak;.      };.  
37a90 20 20 73 77 69 74 63 68 20 28 73 71 6c 69 74 65    switch (sqlite
37aa0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
37ab0 67 76 5b 31 5d 29 29 0a 20 20 20 20 20 20 7b 0a  gv[1])).      {.
37ac0 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
37ad0 45 5f 54 45 58 54 3a 0a 09 20 20 64 65 73 63 20  E_TEXT:..  desc 
37ae0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
37af0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
37b00 65 78 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09  ext (argv[1]);..
37b10 20 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 20 28    len = strlen (
37b20 64 65 73 63 29 3b 0a 09 20 20 64 65 73 63 5f 6d  desc);..  desc_m
37b30 61 6c 6c 6f 63 20 3d 20 6d 61 6c 6c 6f 63 20 28  alloc = malloc (
37b40 6c 65 6e 20 2b 20 31 29 3b 0a 09 20 20 73 74 72  len + 1);..  str
37b50 63 70 79 20 28 64 65 73 63 5f 6d 61 6c 6c 6f 63  cpy (desc_malloc
37b60 2c 20 64 65 73 63 29 3b 0a 09 20 20 64 65 73 63  , desc);..  desc
37b70 20 3d 20 64 65 73 63 5f 6d 61 6c 6c 6f 63 3b 0a   = desc_malloc;.
37b80 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  .  break;.      
37b90 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
37ba0 47 45 52 3a 0a 09 20 20 69 6e 74 5f 76 61 6c 75  GER:..  int_valu
37bb0 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
37bc0 65 5f 69 6e 74 36 34 20 28 61 72 67 76 5b 31 5d  e_int64 (argv[1]
37bd0 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  );.#if defined(_
37be0 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65  WIN32) || define
37bf0 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f 29 0a 2f  d(__MINGW32__)./
37c00 2a 20 43 41 56 45 41 54 3a 20 4d 24 20 72 75 6e  * CAVEAT: M$ run
37c10 74 69 6d 65 20 64 6f 65 73 6e 27 74 20 73 75 70  time doesn't sup
37c20 70 6f 72 74 73 20 25 6c 6c 64 20 66 6f 72 20 36  ports %lld for 6
37c30 34 20 62 69 74 73 20 2a 2f 0a 09 20 20 73 70 72  4 bits */..  spr
37c40 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22 25 49  intf (dummy, "%I
37c50 36 34 64 22 2c 20 69 6e 74 5f 76 61 6c 75 65 29  64d", int_value)
37c60 3b 0a 23 65 6c 73 65 0a 09 20 20 73 70 72 69 6e  ;.#else..  sprin
37c70 74 66 20 28 64 75 6d 6d 79 2c 20 22 25 6c 6c 64  tf (dummy, "%lld
37c80 22 2c 20 69 6e 74 5f 76 61 6c 75 65 29 3b 0a 23  ", int_value);.#
37c90 65 6e 64 69 66 0a 09 20 20 6c 65 6e 20 3d 20 73  endif..  len = s
37ca0 74 72 6c 65 6e 20 28 64 75 6d 6d 79 29 3b 0a 09  trlen (dummy);..
37cb0 20 20 64 65 73 63 5f 6d 61 6c 6c 6f 63 20 3d 20    desc_malloc = 
37cc0 6d 61 6c 6c 6f 63 20 28 6c 65 6e 20 2b 20 31 29  malloc (len + 1)
37cd0 3b 0a 09 20 20 73 74 72 63 70 79 20 28 64 65 73  ;..  strcpy (des
37ce0 63 5f 6d 61 6c 6c 6f 63 2c 20 64 75 6d 6d 79 29  c_malloc, dummy)
37cf0 3b 0a 09 20 20 64 65 73 63 20 3d 20 64 65 73 63  ;..  desc = desc
37d00 5f 6d 61 6c 6c 6f 63 3b 0a 09 20 20 62 72 65 61  _malloc;..  brea
37d10 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  k;.      case SQ
37d20 4c 49 54 45 5f 46 4c 4f 41 54 3a 0a 09 20 20 64  LITE_FLOAT:..  d
37d30 62 6c 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  bl_value = sqlit
37d40 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
37d50 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 73 70  (argv[1]);..  sp
37d60 72 69 6e 74 66 20 28 64 75 6d 6d 79 2c 20 22 25  rintf (dummy, "%
37d70 31 2e 36 66 22 2c 20 64 62 6c 5f 76 61 6c 75 65  1.6f", dbl_value
37d80 29 3b 0a 09 20 20 6c 65 6e 20 3d 20 73 74 72 6c  );..  len = strl
37d90 65 6e 20 28 64 75 6d 6d 79 29 3b 0a 09 20 20 64  en (dummy);..  d
37da0 65 73 63 5f 6d 61 6c 6c 6f 63 20 3d 20 6d 61 6c  esc_malloc = mal
37db0 6c 6f 63 20 28 6c 65 6e 20 2b 20 31 29 3b 0a 09  loc (len + 1);..
37dc0 20 20 73 74 72 63 70 79 20 28 64 65 73 63 5f 6d    strcpy (desc_m
37dd0 61 6c 6c 6f 63 2c 20 64 75 6d 6d 79 29 3b 0a 09  alloc, dummy);..
37de0 20 20 64 65 73 63 20 3d 20 64 65 73 63 5f 6d 61    desc = desc_ma
37df0 6c 6c 6f 63 3b 0a 09 20 20 62 72 65 61 6b 3b 0a  lloc;..  break;.
37e00 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
37e10 45 5f 42 4c 4f 42 3a 0a 09 20 20 64 65 73 63 20  E_BLOB:..  desc 
37e20 3d 20 22 42 4c 4f 42 22 3b 0a 09 20 20 62 72 65  = "BLOB";..  bre
37e30 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  ak;.      defaul
37e40 74 3a 0a 09 20 20 64 65 73 63 20 3d 20 22 4e 55  t:..  desc = "NU
37e50 4c 4c 22 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20  LL";..  break;. 
37e60 20 20 20 20 20 7d 3b 0a 20 20 20 20 67 61 69 61       };.    gaia
37e70 4f 75 74 42 75 66 66 65 72 49 6e 69 74 69 61 6c  OutBufferInitial
37e80 69 7a 65 20 28 26 6f 75 74 5f 62 75 66 29 3b 0a  ize (&out_buf);.
37e90 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
37ea0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
37eb0 5b 32 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [2]) != SQLITE_B
37ec0 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
37ed0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
37ee0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
37ef0 20 20 67 6f 74 6f 20 73 74 6f 70 3b 0a 20 20 20    goto stop;.   
37f00 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20     }.    p_blob 
37f10 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
37f20 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
37f30 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 32 5d 29  e_blob (argv[2])
37f40 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
37f50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
37f60 74 65 73 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20  tes (argv[2]);. 
37f70 20 20 20 69 66 20 28 61 72 67 63 20 3d 3d 20 34     if (argc == 4
37f80 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20  ).      {..  if 
37f90 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
37fa0 79 70 65 20 28 61 72 67 76 5b 33 5d 29 20 3d 3d  ype (argv[3]) ==
37fb0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
37fc0 0a 09 20 20 20 20 20 20 70 72 65 63 69 73 69 6f  ..      precisio
37fd0 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
37fe0 65 5f 69 6e 74 20 28 61 72 67 76 5b 33 5d 29 3b  e_int (argv[3]);
37ff0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
38000 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
38010 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
38020 0a 09 09 67 6f 74 6f 20 73 74 6f 70 3b 0a 09 20  ...goto stop;.. 
38030 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
38040 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
38050 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
38060 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
38070 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
38080 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
38090 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
380a0 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
380b0 20 7b 0a 09 20 20 69 66 20 28 67 65 6f 2d 3e 53   {..  if (geo->S
380c0 72 69 64 20 3d 3d 20 34 33 32 36 29 0a 09 20 20  rid == 4326)..  
380d0 20 20 20 20 3b 09 09 09 2f 2a 20 61 6c 72 65 61      ;.../* alrea
380e0 64 79 20 57 47 53 38 34 20 2a 2f 0a 09 20 20 65  dy WGS84 */..  e
380f0 6c 73 65 20 69 66 20 28 67 65 6f 2d 3e 53 72 69  lse if (geo->Sri
38100 64 20 3d 3d 20 30 29 0a 09 20 20 20 20 7b 0a 09  d == 0)..    {..
38110 09 2f 2a 20 75 6e 6b 6e 6f 77 6e 20 53 52 49 44  ./* unknown SRID
38120 3a 20 67 69 76 69 6e 67 20 75 70 20 2a 2f 0a 09  : giving up */..
38130 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
38140 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
38150 09 09 67 6f 74 6f 20 73 74 6f 70 3b 0a 09 20 20  ..goto stop;..  
38160 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20    }..  else..   
38170 20 7b 0a 09 09 2f 2a 20 61 74 74 65 6d 70 74 69   {.../* attempti
38180 6e 67 20 74 6f 20 72 65 70 72 6f 6a 65 63 74 20  ng to reproject 
38190 69 6e 74 6f 20 57 47 53 38 34 20 2a 2f 0a 09 09  into WGS84 */...
381a0 70 72 6f 6a 5f 70 61 72 61 6d 73 20 28 73 71 6c  proj_params (sql
381b0 69 74 65 2c 20 67 65 6f 2d 3e 53 72 69 64 2c 20  ite, geo->Srid, 
381c0 70 72 6f 6a 5f 66 72 6f 6d 29 3b 0a 09 09 70 72  proj_from);...pr
381d0 6f 6a 5f 70 61 72 61 6d 73 20 28 73 71 6c 69 74  oj_params (sqlit
381e0 65 2c 20 34 33 32 36 2c 20 70 72 6f 6a 5f 74 6f  e, 4326, proj_to
381f0 29 3b 0a 09 09 69 66 20 28 2a 70 72 6f 6a 5f 74  );...if (*proj_t
38200 6f 20 3d 3d 20 27 5c 30 27 20 7c 7c 20 2a 70 72  o == '\0' || *pr
38210 6f 6a 5f 66 72 6f 6d 20 3d 3d 20 27 5c 30 27 29  oj_from == '\0')
38220 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 73  ...  {...      s
38230 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
38240 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09  ll (context);...
38250 20 20 20 20 20 20 67 6f 74 6f 20 73 74 6f 70 3b        goto stop;
38260 0a 09 09 20 20 7d 0a 09 09 67 65 6f 5f 77 67 73  ...  }...geo_wgs
38270 38 34 20 3d 20 67 61 69 61 54 72 61 6e 73 66 6f  84 = gaiaTransfo
38280 72 6d 20 28 67 65 6f 2c 20 70 72 6f 6a 5f 66 72  rm (geo, proj_fr
38290 6f 6d 2c 20 70 72 6f 6a 5f 74 6f 29 3b 0a 09 09  om, proj_to);...
382a0 69 66 20 28 21 67 65 6f 5f 77 67 73 38 34 29 0a  if (!geo_wgs84).
382b0 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 73 71  ..  {...      sq
382c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
382d0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09 20  l (context);... 
382e0 20 20 20 20 20 67 6f 74 6f 20 73 74 6f 70 3b 0a       goto stop;.
382f0 09 09 20 20 7d 0a 09 09 2f 2a 20 6f 6b 2c 20 72  ..  }.../* ok, r
38300 65 70 72 6f 6a 65 63 74 69 6f 6e 20 77 61 73 20  eprojection was 
38310 73 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 09 09  successful */...
38320 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
38330 20 28 67 65 6f 29 3b 0a 09 09 67 65 6f 20 3d 20   (geo);...geo = 
38340 67 65 6f 5f 77 67 73 38 34 3b 0a 09 20 20 20 20  geo_wgs84;..    
38350 7d 0a 09 20 20 2f 2a 20 70 72 6f 64 75 63 65 20  }..  /* produce 
38360 4b 4d 4c 2d 6e 6f 74 61 74 69 6f 6e 20 2d 20 61  KML-notation - a
38370 63 74 75 61 6c 20 77 6f 72 6b 20 69 73 20 64 6f  ctual work is do
38380 6e 65 20 69 6e 20 67 61 69 61 67 65 6f 2f 67 67  ne in gaiageo/gg
38390 5f 77 6b 74 2e 63 20 2a 2f 0a 09 20 20 67 61 69  _wkt.c */..  gai
383a0 61 4f 75 74 46 75 6c 6c 4b 6d 6c 20 28 26 6f 75  aOutFullKml (&ou
383b0 74 5f 62 75 66 2c 20 6e 61 6d 65 2c 20 64 65 73  t_buf, name, des
383c0 63 2c 20 67 65 6f 2c 20 70 72 65 63 69 73 69 6f  c, geo, precisio
383d0 6e 29 3b 0a 09 20 20 69 66 20 28 6f 75 74 5f 62  n);..  if (out_b
383e0 75 66 2e 45 72 72 6f 72 20 7c 7c 20 6f 75 74 5f  uf.Error || out_
383f0 62 75 66 2e 42 75 66 66 65 72 20 3d 3d 20 4e 55  buf.Buffer == NU
38400 4c 4c 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74  LL)..      sqlit
38410 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
38420 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73  context);..  els
38430 65 0a 09 20 20 20 20 7b 0a 09 09 6c 65 6e 20 3d  e..    {...len =
38440 20 6f 75 74 5f 62 75 66 2e 57 72 69 74 65 4f 66   out_buf.WriteOf
38450 66 73 65 74 3b 0a 09 09 73 71 6c 69 74 65 33 5f  fset;...sqlite3_
38460 72 65 73 75 6c 74 5f 74 65 78 74 20 28 63 6f 6e  result_text (con
38470 74 65 78 74 2c 20 6f 75 74 5f 62 75 66 2e 42 75  text, out_buf.Bu
38480 66 66 65 72 2c 20 6c 65 6e 2c 20 66 72 65 65 29  ffer, len, free)
38490 3b 0a 09 09 6f 75 74 5f 62 75 66 2e 42 75 66 66  ;...out_buf.Buff
384a0 65 72 20 3d 20 4e 55 4c 4c 3b 0a 09 20 20 20 20  er = NULL;..    
384b0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 73 74 6f 70  }.      }.  stop
384c0 3a 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  :.    gaiaFreeGe
384d0 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20  omColl (geo);.  
384e0 20 20 69 66 20 28 6e 61 6d 65 5f 6d 61 6c 6c 6f    if (name_mallo
384f0 63 29 0a 09 66 72 65 65 20 28 6e 61 6d 65 5f 6d  c)..free (name_m
38500 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 20 28  alloc);.    if (
38510 64 65 73 63 5f 6d 61 6c 6c 6f 63 29 0a 09 66 72  desc_malloc)..fr
38520 65 65 20 28 64 65 73 63 5f 6d 61 6c 6c 6f 63 29  ee (desc_malloc)
38530 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75 66  ;.    gaiaOutBuf
38540 66 65 72 52 65 73 65 74 20 28 26 6f 75 74 5f 62  ferReset (&out_b
38550 75 66 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  uf);.}..static v
38560 6f 69 64 0a 66 6e 63 74 5f 41 73 4b 6d 6c 20 28  oid.fnct_AsKml (
38570 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
38580 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
38590 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
385a0 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
385b0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
385c0 20 41 73 4b 6d 6c 28 41 6e 79 74 68 69 6e 67 20   AsKml(Anything 
385d0 6e 61 6d 65 2c 20 41 6e 79 74 68 69 6e 67 20 64  name, Anything d
385e0 65 73 63 72 69 70 74 69 6f 6e 2c 20 42 4c 4f 42  escription, BLOB
385f0 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72   encoded geometr
38600 79 29 0a 2f 20 20 20 20 20 6f 72 0a 2f 20 41 73  y)./     or./ As
38610 4b 6d 6c 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64  Kml(BLOB encoded
38620 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
38630 65 74 75 72 6e 73 20 74 68 65 20 63 6f 72 72 65  eturns the corre
38640 73 70 6f 6e 64 69 6e 67 20 4b 4d 4c 20 72 65 70  sponding KML rep
38650 72 65 73 65 6e 74 61 74 69 6f 6e 20 0a 2f 20 6f  resentation ./ o
38660 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72  r NULL if any er
38670 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
38680 65 64 0a 2a 2f 0a 20 20 20 20 69 66 20 28 61 72  ed.*/.    if (ar
38690 67 63 20 3d 3d 20 33 20 7c 7c 20 61 72 67 63 20  gc == 3 || argc 
386a0 3d 3d 20 34 29 0a 09 66 6e 63 74 5f 41 73 4b 6d  == 4)..fnct_AsKm
386b0 6c 33 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  l3 (context, arg
386c0 63 2c 20 61 72 67 76 29 3b 0a 20 20 20 20 65 6c  c, argv);.    el
386d0 73 65 0a 09 66 6e 63 74 5f 41 73 4b 6d 6c 31 20  se..fnct_AsKml1 
386e0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
386f0 61 72 67 76 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  argv);.}.#endif 
38700 2f 2a 20 65 6e 64 20 69 6e 63 6c 75 64 69 6e 67  /* end including
38710 20 50 52 4f 4a 2e 34 20 2a 2f 0a 0a 73 74 61 74   PROJ.4 */..stat
38720 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41 73 47  ic void.fnct_AsG
38730 6d 6c 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ml (sqlite3_cont
38740 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
38750 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
38760 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
38770 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
38780 6e 3a 0a 2f 20 41 73 47 6d 6c 28 42 4c 4f 42 20  n:./ AsGml(BLOB 
38790 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79  encoded geometry
387a0 29 0a 2f 20 20 20 20 6f 72 0a 2f 20 41 73 47 6d  )./    or./ AsGm
387b0 6c 28 69 6e 74 65 67 65 72 20 76 65 72 73 69 6f  l(integer versio
387c0 6e 2c 20 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20  n, BLOB encoded 
387d0 67 65 6f 6d 65 74 72 79 29 0a 2f 20 20 20 20 6f  geometry)./    o
387e0 72 0a 2f 20 41 73 47 6d 6c 28 69 6e 74 65 67 65  r./ AsGml(intege
387f0 72 20 76 65 72 73 69 6f 6e 2c 20 42 4c 4f 42 20  r version, BLOB 
38800 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79  encoded geometry
38810 2c 20 69 6e 74 65 67 65 72 20 70 72 65 63 69 73  , integer precis
38820 69 6f 6e 29 0a 2f 0a 2f 20 2a 76 65 72 73 69 6f  ion)././ *versio
38830 6e 2a 20 6d 61 79 20 62 65 20 32 20 28 47 4d 4c  n* may be 2 (GML
38840 20 32 2e 31 2e 32 29 20 6f 72 20 33 20 28 47 4d   2.1.2) or 3 (GM
38850 4c 20 33 2e 31 2e 31 29 0a 2f 20 64 65 66 61 75  L 3.1.1)./ defau
38860 6c 74 20 2a 76 65 72 73 69 6f 6e 2a 3a 20 32 0a  lt *version*: 2.
38870 2f 0a 2f 20 2a 70 72 65 63 69 73 69 6f 6e 2a 20  /./ *precision* 
38880 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
38890 20 6f 75 74 70 75 74 20 64 65 63 69 6d 61 6c 20   output decimal 
388a0 64 69 67 69 74 73 0a 2f 20 64 65 66 61 75 6c 74  digits./ default
388b0 20 2a 70 72 65 63 69 73 69 6f 6e 2a 3a 20 31 35   *precision*: 15
388c0 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65  ././ returns the
388d0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 47   corresponding G
388e0 4d 4c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ML representatio
388f0 6e 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  n ./ or NULL if 
38900 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
38910 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
38920 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
38930 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
38940 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
38950 6c 65 6e 3b 0a 20 20 20 20 69 6e 74 20 76 65 72  len;.    int ver
38960 73 69 6f 6e 20 3d 20 32 3b 0a 20 20 20 20 69 6e  sion = 2;.    in
38970 74 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 31 35  t precision = 15
38980 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75 66  ;.    gaiaOutBuf
38990 66 65 72 20 6f 75 74 5f 62 75 66 3b 0a 20 20 20  fer out_buf;.   
389a0 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
389b0 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
389c0 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
389d0 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
389e0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 61  INE */.    if (a
389f0 72 67 63 20 3d 3d 20 33 29 0a 20 20 20 20 20 20  rgc == 3).      
38a00 7b 0a 09 20 20 69 66 20 28 73 71 6c 69 74 65 33  {..  if (sqlite3
38a10 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
38a20 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[0]) == SQLITE_
38a30 49 4e 54 45 47 45 52 29 0a 09 20 20 20 20 20 20  INTEGER)..      
38a40 76 65 72 73 69 6f 6e 20 3d 20 73 71 6c 69 74 65  version = sqlite
38a50 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
38a60 76 5b 30 5d 29 3b 0a 09 20 20 65 6c 73 65 0a 09  v[0]);..  else..
38a70 20 20 20 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f      {...sqlite3_
38a80 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
38a90 74 65 78 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b  text);...return;
38aa0 0a 09 20 20 20 20 7d 0a 09 20 20 69 66 20 28 73  ..    }..  if (s
38ab0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
38ac0 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53  e (argv[1]) != S
38ad0 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 09 20 20 20  QLITE_BLOB)..   
38ae0 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73   {...sqlite3_res
38af0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
38b00 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20  t);...return;.. 
38b10 20 20 20 7d 0a 09 20 20 70 5f 62 6c 6f 62 20 3d     }..  p_blob =
38b20 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
38b30 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
38b40 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b  _blob (argv[1]);
38b50 0a 09 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71  ..  n_bytes = sq
38b60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
38b70 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20  s (argv[1]);..  
38b80 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
38b90 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29  e_type (argv[2])
38ba0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
38bb0 45 52 29 0a 09 20 20 20 20 20 20 70 72 65 63 69  ER)..      preci
38bc0 73 69 6f 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  sion = sqlite3_v
38bd0 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 32  alue_int (argv[2
38be0 5d 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20  ]);..  else..   
38bf0 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73   {...sqlite3_res
38c00 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
38c10 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20  t);...return;.. 
38c20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
38c30 20 65 6c 73 65 20 69 66 20 28 61 72 67 63 20 3d   else if (argc =
38c40 3d 20 32 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  = 2).      {..  
38c50 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
38c60 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
38c70 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
38c80 45 52 0a 09 20 20 20 20 20 20 26 26 20 73 71 6c  ER..      && sql
38c90 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
38ca0 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
38cb0 49 54 45 5f 42 4c 4f 42 29 0a 09 20 20 20 20 7b  ITE_BLOB)..    {
38cc0 0a 09 09 76 65 72 73 69 6f 6e 20 3d 20 73 71 6c  ...version = sql
38cd0 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
38ce0 61 72 67 76 5b 30 5d 29 3b 0a 09 09 70 5f 62 6c  argv[0]);...p_bl
38cf0 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
38d00 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
38d10 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
38d20 31 5d 29 3b 0a 09 09 6e 5f 62 79 74 65 73 20 3d  1]);...n_bytes =
38d30 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
38d40 79 74 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a  ytes (argv[1]);.
38d50 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 20 69  .    }..  else i
38d60 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
38d70 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
38d80 3d 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 0a 09  == SQLITE_BLOB..
38d90 09 20 20 20 26 26 20 73 71 6c 69 74 65 33 5f 76  .   && sqlite3_v
38da0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
38db0 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  1]) == SQLITE_IN
38dc0 54 45 47 45 52 29 0a 09 20 20 20 20 7b 0a 09 09  TEGER)..    {...
38dd0 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
38de0 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
38df0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
38e00 72 67 76 5b 30 5d 29 3b 0a 09 09 6e 5f 62 79 74  rgv[0]);...n_byt
38e10 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
38e20 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
38e30 5d 29 3b 0a 09 09 70 72 65 63 69 73 69 6f 6e 20  ]);...precision 
38e40 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
38e50 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09  int (argv[1]);..
38e60 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20      }..  else.. 
38e70 20 20 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72     {...sqlite3_r
38e80 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
38e90 65 78 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a  ext);...return;.
38ea0 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20  .    }.      }. 
38eb0 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
38ec0 09 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  .  if (sqlite3_v
38ed0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
38ee0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
38ef0 4f 42 29 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c  OB)..    {...sql
38f00 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
38f10 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09 72 65   (context);...re
38f20 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20  turn;..    }..  
38f30 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
38f40 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
38f50 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
38f60 72 67 76 5b 30 5d 29 3b 0a 09 20 20 6e 5f 62 79  rgv[0]);..  n_by
38f70 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
38f80 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
38f90 30 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  0]);.      }.   
38fa0 20 67 61 69 61 4f 75 74 42 75 66 66 65 72 49 6e   gaiaOutBufferIn
38fb0 69 74 69 61 6c 69 7a 65 20 28 26 6f 75 74 5f 62  itialize (&out_b
38fc0 75 66 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  uf);.    geo = g
38fd0 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
38fe0 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
38ff0 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
39000 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
39010 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
39020 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
39030 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a  se.      {..  /*
39040 20 70 72 6f 64 75 63 65 20 47 4d 4c 2d 6e 6f 74   produce GML-not
39050 61 74 69 6f 6e 20 2d 20 61 63 74 75 61 6c 20 77  ation - actual w
39060 6f 72 6b 20 69 73 20 64 6f 6e 65 20 69 6e 20 67  ork is done in g
39070 61 69 61 67 65 6f 2f 67 67 5f 77 6b 74 2e 63 20  aiageo/gg_wkt.c 
39080 2a 2f 0a 09 20 20 67 61 69 61 4f 75 74 47 6d 6c  */..  gaiaOutGml
39090 20 28 26 6f 75 74 5f 62 75 66 2c 20 76 65 72 73   (&out_buf, vers
390a0 69 6f 6e 2c 20 70 72 65 63 69 73 69 6f 6e 2c 20  ion, precision, 
390b0 67 65 6f 29 3b 0a 09 20 20 69 66 20 28 6f 75 74  geo);..  if (out
390c0 5f 62 75 66 2e 45 72 72 6f 72 20 7c 7c 20 6f 75  _buf.Error || ou
390d0 74 5f 62 75 66 2e 42 75 66 66 65 72 20 3d 3d 20  t_buf.Buffer == 
390e0 4e 55 4c 4c 29 0a 09 20 20 20 20 20 20 73 71 6c  NULL)..      sql
390f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
39100 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65   (context);..  e
39110 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 6c 65 6e  lse..    {...len
39120 20 3d 20 6f 75 74 5f 62 75 66 2e 57 72 69 74 65   = out_buf.Write
39130 4f 66 66 73 65 74 3b 0a 09 09 73 71 6c 69 74 65  Offset;...sqlite
39140 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 20 28 63  3_result_text (c
39150 6f 6e 74 65 78 74 2c 20 6f 75 74 5f 62 75 66 2e  ontext, out_buf.
39160 42 75 66 66 65 72 2c 20 6c 65 6e 2c 20 66 72 65  Buffer, len, fre
39170 65 29 3b 0a 09 09 6f 75 74 5f 62 75 66 2e 42 75  e);...out_buf.Bu
39180 66 66 65 72 20 3d 20 4e 55 4c 4c 3b 0a 09 20 20  ffer = NULL;..  
39190 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
391a0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
391b0 20 28 67 65 6f 29 3b 0a 20 20 20 20 67 61 69 61   (geo);.    gaia
391c0 4f 75 74 42 75 66 66 65 72 52 65 73 65 74 20 28  OutBufferReset (
391d0 26 6f 75 74 5f 62 75 66 29 3b 0a 7d 0a 0a 73 74  &out_buf);.}..st
391e0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 41  atic void.fnct_A
391f0 73 47 65 6f 4a 53 4f 4e 20 28 73 71 6c 69 74 65  sGeoJSON (sqlite
39200 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
39210 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
39220 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
39230 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
39240 75 6e 63 74 69 6f 6e 3a 0a 2f 20 41 73 47 65 6f  unction:./ AsGeo
39250 4a 53 4f 4e 28 42 4c 4f 42 20 65 6e 63 6f 64 65  JSON(BLOB encode
39260 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 20 20 20  d geometry)./   
39270 20 6f 72 0a 2f 20 41 73 47 65 6f 4a 53 4f 4e 28   or./ AsGeoJSON(
39280 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f  BLOB encoded geo
39290 6d 65 74 72 79 2c 20 69 6e 74 65 67 65 72 20 70  metry, integer p
392a0 72 65 63 69 73 69 6f 6e 29 0a 2f 20 20 20 20 6f  recision)./    o
392b0 72 0a 2f 20 41 73 47 65 6f 4a 53 4f 4e 28 42 4c  r./ AsGeoJSON(BL
392c0 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65  OB encoded geome
392d0 74 72 79 2c 20 69 6e 74 65 67 65 72 20 70 72 65  try, integer pre
392e0 63 69 73 69 6f 6e 2c 20 69 6e 74 65 67 65 72 20  cision, integer 
392f0 6f 70 74 69 6f 6e 73 29 0a 2f 0a 2f 20 2a 70 72  options)././ *pr
39300 65 63 69 73 69 6f 6e 2a 20 69 73 20 74 68 65 20  ecision* is the 
39310 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74  number of output
39320 20 64 65 63 69 6d 61 6c 20 64 69 67 69 74 73 0a   decimal digits.
39330 2f 20 64 65 66 61 75 6c 74 20 2a 70 72 65 63 69  / default *preci
39340 73 69 6f 6e 2a 3a 20 31 35 0a 2f 0a 2f 20 2a 6f  sion*: 15././ *o
39350 70 74 69 6f 6e 73 2a 20 6d 61 79 20 62 65 20 6f  ptions* may be o
39360 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
39370 69 6e 67 73 3a 0a 2f 20 20 20 30 20 3d 20 6e 6f  ings:./   0 = no
39380 20 6f 70 74 69 6f 6e 73 20 5b 64 65 66 61 75 6c   options [defaul
39390 74 5d 0a 2f 20 20 20 31 20 3d 20 47 65 6f 4a 53  t]./   1 = GeoJS
393a0 4f 4e 20 4d 42 52 0a 2f 20 20 20 32 20 3d 20 47  ON MBR./   2 = G
393b0 65 6f 4a 53 4f 4e 20 53 68 6f 72 74 20 43 52 53  eoJSON Short CRS
393c0 20 28 65 2e 67 20 45 50 53 47 3a 34 33 32 36 29   (e.g EPSG:4326)
393d0 20 0a 2f 20 20 20 33 20 3d 20 31 20 2b 20 32 20   ./   3 = 1 + 2 
393e0 28 4d 62 72 20 2b 20 73 68 6f 72 74 43 72 73 29  (Mbr + shortCrs)
393f0 0a 2f 20 20 20 34 20 3d 20 47 65 6f 4a 53 4f 4e  ./   4 = GeoJSON
39400 20 4c 6f 6e 67 20 43 52 53 20 28 65 2e 67 20 75   Long CRS (e.g u
39410 72 6e 3a 6f 67 63 3a 64 65 66 3a 63 72 73 3a 45  rn:ogc:def:crs:E
39420 50 53 47 3a 3a 34 33 32 36 29 0a 2f 20 20 20 35  PSG::4326)./   5
39430 20 3d 20 31 20 2b 20 34 20 28 4d 62 72 20 2b 20   = 1 + 4 (Mbr + 
39440 6c 6f 6e 67 43 72 73 29 0a 2f 0a 2f 20 72 65 74  longCrs)././ ret
39450 75 72 6e 73 20 74 68 65 20 63 6f 72 72 65 73 70  urns the corresp
39460 6f 6e 64 69 6e 67 20 47 4d 4c 20 72 65 70 72 65  onding GML repre
39470 73 65 6e 74 61 74 69 6f 6e 20 0a 2f 20 6f 72 20  sentation ./ or 
39480 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f  NULL if any erro
39490 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
394a0 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  .*/.    unsigned
394b0 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20   char *p_blob;. 
394c0 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a     int n_bytes;.
394d0 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
394e0 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 20 3d   int precision =
394f0 20 31 35 3b 0a 20 20 20 20 69 6e 74 20 6f 70 74   15;.    int opt
39500 69 6f 6e 73 20 3d 20 30 3b 0a 20 20 20 20 67 61  ions = 0;.    ga
39510 69 61 4f 75 74 42 75 66 66 65 72 20 6f 75 74 5f  iaOutBuffer out_
39520 62 75 66 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  buf;.    gaiaGeo
39530 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e  mCollPtr geo = N
39540 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ULL;.    GAIA_UN
39550 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
39560 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
39570 20 20 20 69 66 20 28 61 72 67 63 20 3d 3d 20 33     if (argc == 3
39580 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20  ).      {..  if 
39590 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
395a0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
395b0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 0a 09 20 20   SQLITE_BLOB..  
395c0 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 5f 76      && sqlite3_v
395d0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
395e0 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  1]) == SQLITE_IN
395f0 54 45 47 45 52 0a 09 20 20 20 20 20 20 26 26 20  TEGER..      && 
39600 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
39610 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d 3d 20  pe (argv[2]) == 
39620 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a  SQLITE_INTEGER).
39630 09 20 20 20 20 7b 0a 09 09 70 5f 62 6c 6f 62 20  .    {...p_blob 
39640 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
39650 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
39660 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
39670 3b 0a 09 09 6e 5f 62 79 74 65 73 20 3d 20 73 71  ;...n_bytes = sq
39680 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
39690 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09 09 70  s (argv[0]);...p
396a0 72 65 63 69 73 69 6f 6e 20 3d 20 73 71 6c 69 74  recision = sqlit
396b0 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
396c0 67 76 5b 31 5d 29 3b 0a 09 09 6f 70 74 69 6f 6e  gv[1]);...option
396d0 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s = sqlite3_valu
396e0 65 5f 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b  e_int (argv[2]);
396f0 0a 09 09 69 66 20 28 6f 70 74 69 6f 6e 73 20 3e  ...if (options >
39700 3d 20 31 20 26 26 20 6f 70 74 69 6f 6e 73 20 3c  = 1 && options <
39710 3d 20 35 29 0a 09 09 20 20 20 20 3b 0a 09 09 65  = 5)...    ;...e
39720 6c 73 65 0a 09 09 20 20 20 20 6f 70 74 69 6f 6e  lse...    option
39730 73 20 3d 20 30 3b 0a 09 20 20 20 20 7d 0a 09 20  s = 0;..    }.. 
39740 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 73   else..    {...s
39750 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
39760 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09  ll (context);...
39770 72 65 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 20  return;..    }. 
39780 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 20       }.    else 
39790 69 66 20 28 61 72 67 63 20 3d 3d 20 32 29 0a 20  if (argc == 2). 
397a0 20 20 20 20 20 7b 0a 09 20 20 69 66 20 28 73 71       {..  if (sq
397b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
397c0 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
397d0 4c 49 54 45 5f 42 4c 4f 42 0a 09 20 20 20 20 20  LITE_BLOB..     
397e0 20 26 26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   && sqlite3_valu
397f0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29  e_type (argv[1])
39800 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
39810 45 52 29 0a 09 20 20 20 20 7b 0a 09 09 70 5f 62  ER)..    {...p_b
39820 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  lob = (unsigned 
39830 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
39840 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76  value_blob (argv
39850 5b 30 5d 29 3b 0a 09 09 6e 5f 62 79 74 65 73 20  [0]);...n_bytes 
39860 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
39870 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b  bytes (argv[0]);
39880 0a 09 09 70 72 65 63 69 73 69 6f 6e 20 3d 20 73  ...precision = s
39890 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
398a0 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 20   (argv[1]);..   
398b0 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20   }..  else..    
398c0 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75  {...sqlite3_resu
398d0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
398e0 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20 20  );...return;..  
398f0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
39900 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
39910 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
39920 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
39930 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
39940 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c 69 74 65  ..    {...sqlite
39950 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
39960 6f 6e 74 65 78 74 29 3b 0a 09 09 72 65 74 75 72  ontext);...retur
39970 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20 70 5f 62  n;..    }..  p_b
39980 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  lob = (unsigned 
39990 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
399a0 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76  value_blob (argv
399b0 5b 30 5d 29 3b 0a 09 20 20 6e 5f 62 79 74 65 73  [0]);..  n_bytes
399c0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
399d0 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
399e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  ;.      }.    ga
399f0 69 61 4f 75 74 42 75 66 66 65 72 49 6e 69 74 69  iaOutBufferIniti
39a00 61 6c 69 7a 65 20 28 26 6f 75 74 5f 62 75 66 29  alize (&out_buf)
39a10 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61  ;.    geo = gaia
39a20 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
39a30 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
39a40 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
39a50 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f  (!geo)..sqlite3_
39a60 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
39a70 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a  text);.    else.
39a80 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 70 72        {..  /* pr
39a90 6f 64 75 63 65 20 47 65 6f 4a 53 4f 4e 2d 6e 6f  oduce GeoJSON-no
39aa0 74 61 74 69 6f 6e 20 2d 20 61 63 74 75 61 6c 20  tation - actual 
39ab0 77 6f 72 6b 20 69 73 20 64 6f 6e 65 20 69 6e 20  work is done in 
39ac0 67 61 69 61 67 65 6f 2f 67 67 5f 77 6b 74 2e 63  gaiageo/gg_wkt.c
39ad0 20 2a 2f 0a 09 20 20 67 61 69 61 4f 75 74 47 65   */..  gaiaOutGe
39ae0 6f 4a 53 4f 4e 20 28 26 6f 75 74 5f 62 75 66 2c  oJSON (&out_buf,
39af0 20 67 65 6f 2c 20 70 72 65 63 69 73 69 6f 6e 2c   geo, precision,
39b00 20 6f 70 74 69 6f 6e 73 29 3b 0a 09 20 20 69 66   options);..  if
39b10 20 28 6f 75 74 5f 62 75 66 2e 45 72 72 6f 72 20   (out_buf.Error 
39b20 7c 7c 20 6f 75 74 5f 62 75 66 2e 42 75 66 66 65  || out_buf.Buffe
39b30 72 20 3d 3d 20 4e 55 4c 4c 29 0a 09 20 20 20 20  r == NULL)..    
39b40 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
39b50 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
39b60 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
39b70 09 09 6c 65 6e 20 3d 20 6f 75 74 5f 62 75 66 2e  ..len = out_buf.
39b80 57 72 69 74 65 4f 66 66 73 65 74 3b 0a 09 09 73  WriteOffset;...s
39b90 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
39ba0 78 74 20 28 63 6f 6e 74 65 78 74 2c 20 6f 75 74  xt (context, out
39bb0 5f 62 75 66 2e 42 75 66 66 65 72 2c 20 6c 65 6e  _buf.Buffer, len
39bc0 2c 20 66 72 65 65 29 3b 0a 09 09 6f 75 74 5f 62  , free);...out_b
39bd0 75 66 2e 42 75 66 66 65 72 20 3d 20 4e 55 4c 4c  uf.Buffer = NULL
39be0 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d  ;..    }.      }
39bf0 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f  .    gaiaFreeGeo
39c00 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20  mColl (geo);.   
39c10 20 67 61 69 61 4f 75 74 42 75 66 66 65 72 52 65   gaiaOutBufferRe
39c20 73 65 74 20 28 26 6f 75 74 5f 62 75 66 29 3b 0a  set (&out_buf);.
39c30 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
39c40 6e 63 74 5f 41 73 42 69 6e 61 72 79 20 28 73 71  nct_AsBinary (sq
39c50 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
39c60 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
39c70 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
39c80 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
39c90 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 41  QL function:./ A
39ca0 73 42 69 6e 61 72 79 28 42 4c 4f 42 20 65 6e 63  sBinary(BLOB enc
39cb0 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f  oded geometry)./
39cc0 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20 63  ./ returns the c
39cd0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 57 4b 42  orresponding WKB
39ce0 20 65 6e 63 6f 64 65 64 20 76 61 6c 75 65 0a 2f   encoded value./
39cf0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
39d00 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
39d10 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
39d20 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
39d30 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
39d40 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  es;.    int len;
39d50 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
39d60 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e  ar *p_result = N
39d70 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ULL;.    gaiaGeo
39d80 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e  mCollPtr geo = N
39d90 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ULL;.    GAIA_UN
39da0 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
39db0 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
39dc0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
39dd0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
39de0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
39df0 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
39e00 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
39e10 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
39e20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
39e30 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
39e40 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
39e50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
39e60 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
39e70 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
39e80 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
39e90 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
39ea0 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
39eb0 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
39ec0 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
39ed0 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a  ;.    if (!geo).
39ee0 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
39ef0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
39f00 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
39f10 0a 09 20 20 67 61 69 61 54 6f 57 6b 62 20 28 67  ..  gaiaToWkb (g
39f20 65 6f 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26  eo, &p_result, &
39f30 6c 65 6e 29 3b 0a 09 20 20 69 66 20 28 21 70 5f  len);..  if (!p_
39f40 72 65 73 75 6c 74 29 0a 09 20 20 20 20 20 20 73  result)..      s
39f50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
39f60 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
39f70 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
39f80 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
39f90 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73   (context, p_res
39fa0 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b  ult, len, free);
39fb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
39fc0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
39fd0 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  eo);.}..static v
39fe0 6f 69 64 0a 66 6e 63 74 5f 41 73 46 47 46 20 28  oid.fnct_AsFGF (
39ff0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3a000 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
3a010 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
3a020 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
3a030 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
3a040 20 41 73 46 47 46 28 42 4c 4f 42 20 65 6e 63 6f   AsFGF(BLOB enco
3a050 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a  ded geometry)./.
3a060 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 6f  / returns the co
3a070 72 72 65 73 70 6f 6e 64 69 6e 67 20 46 47 46 20  rresponding FGF 
3a080 65 6e 63 6f 64 65 64 20 76 61 6c 75 65 0a 2f 20  encoded value./ 
3a090 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65  or NULL if any e
3a0a0 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
3a0b0 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67  red.*/.    unsig
3a0c0 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62  ned char *p_blob
3a0d0 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65  ;.    int n_byte
3a0e0 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  s;.    int len;.
3a0f0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
3a100 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55  r *p_result = NU
3a110 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
3a120 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
3a130 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6f 72  LL;.    int coor
3a140 64 5f 64 69 6d 73 3b 0a 20 20 20 20 47 41 49 41  d_dims;.    GAIA
3a150 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
3a160 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
3a170 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
3a180 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
3a190 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
3a1a0 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
3a1b0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a1c0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
3a1d0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
3a1e0 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
3a1f0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
3a200 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
3a210 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
3a220 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
3a230 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a240 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
3a250 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
3a260 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
3a270 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) != SQLITE_INT
3a280 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
3a290 20 73 70 61 74 69 61 6c 69 74 65 5f 65 0a 09 20   spatialite_e.. 
3a2a0 20 20 20 20 20 28 22 41 73 46 47 46 28 29 20 65       ("AsFGF() e
3a2b0 72 72 6f 72 3a 20 61 72 67 75 6d 65 6e 74 20 32  rror: argument 2
3a2c0 20 5b 67 65 6f 6d 5f 63 6f 6f 72 64 73 5d 20 69   [geom_coords] i
3a2d0 73 20 6e 6f 74 20 6f 66 20 74 68 65 20 49 6e 74  s not of the Int
3a2e0 65 67 65 72 20 74 79 70 65 5c 6e 22 29 3b 0a 09  eger type\n");..
3a2f0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a300 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
3a310 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
3a320 20 20 7d 0a 20 20 20 20 63 6f 6f 72 64 5f 64 69    }.    coord_di
3a330 6d 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ms = sqlite3_val
3a340 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
3a350 3b 0a 20 20 20 20 69 66 20 28 63 6f 6f 72 64 5f  ;.    if (coord_
3a360 64 69 6d 73 0a 09 3d 3d 20 30 20 7c 7c 20 63 6f  dims..== 0 || co
3a370 6f 72 64 5f 64 69 6d 73 20 3d 3d 20 31 20 7c 7c  ord_dims == 1 ||
3a380 20 63 6f 6f 72 64 5f 64 69 6d 73 20 3d 3d 20 32   coord_dims == 2
3a390 20 7c 7c 20 63 6f 6f 72 64 5f 64 69 6d 73 20 3d   || coord_dims =
3a3a0 3d 20 33 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65  = 3)..;.    else
3a3b0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 70 61 74  .      {..  spat
3a3c0 69 61 6c 69 74 65 5f 65 0a 09 20 20 20 20 20 20  ialite_e..      
3a3d0 28 22 41 73 46 47 46 28 29 20 65 72 72 6f 72 3a  ("AsFGF() error:
3a3e0 20 61 72 67 75 6d 65 6e 74 20 32 20 5b 67 65 6f   argument 2 [geo
3a3f0 6d 5f 63 6f 6f 72 64 73 5d 20 6f 75 74 20 6f 66  m_coords] out of
3a400 20 72 61 6e 67 65 20 5b 30 2c 31 2c 32 2c 33 5d   range [0,1,2,3]
3a410 5c 6e 22 29 3b 0a 09 20 20 73 71 6c 69 74 65 33  \n");..  sqlite3
3a420 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
3a430 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
3a440 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67  n;.      }.    g
3a450 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
3a460 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
3a470 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
3a480 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a  ;.    if (!geo).
3a490 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
3a4a0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
3a4b0 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
3a4c0 0a 09 20 20 67 61 69 61 54 6f 46 67 66 20 28 67  ..  gaiaToFgf (g
3a4d0 65 6f 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26  eo, &p_result, &
3a4e0 6c 65 6e 2c 20 63 6f 6f 72 64 5f 64 69 6d 73 29  len, coord_dims)
3a4f0 3b 0a 09 20 20 69 66 20 28 21 70 5f 72 65 73 75  ;..  if (!p_resu
3a500 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lt)..      sqlit
3a510 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
3a520 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73  context);..  els
3a530 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  e..      sqlite3
3a540 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
3a550 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
3a560 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 20 20 20   len, free);.   
3a570 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65     }.    gaiaFre
3a580 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b  eGeomColl (geo);
3a590 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
3a5a0 66 6e 63 74 5f 4d 61 6b 65 50 6f 69 6e 74 31 20  fnct_MakePoint1 
3a5b0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3a5c0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
3a5d0 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
3a5e0 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
3a5f0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
3a600 2f 20 4d 61 6b 65 50 6f 69 6e 74 28 64 6f 75 62  / MakePoint(doub
3a610 6c 65 20 58 2c 20 64 6f 75 62 6c 65 20 59 29 0a  le X, double Y).
3a620 2f 0a 2f 20 62 75 69 6c 64 73 20 61 20 50 4f 49  /./ builds a POI
3a630 4e 54 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  NT ./ or NULL if
3a640 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
3a650 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
3a660 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 69 6e   int len;.    in
3a670 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  t int_value;.   
3a680 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
3a690 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
3a6a0 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20  .    double x;. 
3a6b0 20 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20     double y;.   
3a6c0 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
3a6d0 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
3a6e0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
3a6f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3a700 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53  e (argv[0]) == S
3a710 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 78 20  QLITE_FLOAT)..x 
3a720 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3a730 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29  double (argv[0])
3a740 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73  ;.    else if (s
3a750 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3a760 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53  e (argv[0]) == S
3a770 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
3a780 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61       {..  int_va
3a790 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lue = sqlite3_va
3a7a0 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d  lue_int (argv[0]
3a7b0 29 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61  );..  x = int_va
3a7c0 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lue;.      }.   
3a7d0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
3a7e0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3a7f0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
3a800 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
3a810 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74   }.    if (sqlit
3a820 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3a830 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[1]) == SQLIT
3a840 45 5f 46 4c 4f 41 54 29 0a 09 79 20 3d 20 73 71  E_FLOAT)..y = sq
3a850 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
3a860 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  le (argv[1]);.  
3a870 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74    else if (sqlit
3a880 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3a890 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[1]) == SQLIT
3a8a0 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
3a8b0 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20   {..  int_value 
3a8c0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3a8d0 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09  int (argv[1]);..
3a8e0 20 20 79 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b    y = int_value;
3a8f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
3a900 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
3a910 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
3a920 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
3a930 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
3a940 20 20 20 67 61 69 61 4d 61 6b 65 50 6f 69 6e 74     gaiaMakePoint
3a950 20 28 78 2c 20 79 2c 20 30 2c 20 26 70 5f 72 65   (x, y, 0, &p_re
3a960 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20  sult, &len);.   
3a970 20 69 66 20 28 21 70 5f 72 65 73 75 6c 74 29 0a   if (!p_result).
3a980 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
3a990 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
3a9a0 20 20 20 20 65 6c 73 65 0a 09 73 71 6c 69 74 65      else..sqlite
3a9b0 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63  3_result_blob (c
3a9c0 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74  ontext, p_result
3a9d0 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 7d 0a  , len, free);.}.
3a9e0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
3a9f0 74 5f 4d 61 6b 65 50 6f 69 6e 74 32 20 28 73 71  t_MakePoint2 (sq
3aa00 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
3aa10 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
3aa20 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
3aa30 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
3aa40 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4d  QL function:./ M
3aa50 61 6b 65 50 6f 69 6e 74 28 64 6f 75 62 6c 65 20  akePoint(double 
3aa60 58 2c 20 64 6f 75 62 6c 65 20 59 2c 20 69 6e 74  X, double Y, int
3aa70 20 53 52 49 44 29 0a 2f 0a 2f 20 62 75 69 6c 64   SRID)././ build
3aa80 73 20 61 20 50 4f 49 4e 54 20 0a 2f 20 6f 72 20  s a POINT ./ or 
3aa90 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f  NULL if any erro
3aaa0 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
3aab0 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  .*/.    int len;
3aac0 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c  .    int int_val
3aad0 75 65 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  ue;.    unsigned
3aae0 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
3aaf0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62  = NULL;.    doub
3ab00 6c 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65  le x;.    double
3ab10 20 79 3b 0a 20 20 20 20 69 6e 74 20 73 72 69 64   y;.    int srid
3ab20 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
3ab30 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
3ab40 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
3ab50 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
3ab60 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
3ab70 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54   == SQLITE_FLOAT
3ab80 29 0a 09 78 20 3d 20 73 71 6c 69 74 65 33 5f 76  )..x = sqlite3_v
3ab90 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67  alue_double (arg
3aba0 76 5b 30 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20  v[0]);.    else 
3abb0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
3abc0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
3abd0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
3abe0 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  ER).      {..  i
3abf0 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  nt_value = sqlit
3ac00 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
3ac10 67 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69  gv[0]);..  x = i
3ac20 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  nt_value;.      
3ac30 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  }.    else.     
3ac40 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
3ac50 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
3ac60 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
3ac70 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
3ac80 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
3ac90 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20  pe (argv[1]) == 
3aca0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 79  SQLITE_FLOAT)..y
3acb0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3acc0 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d  _double (argv[1]
3acd0 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28  );.    else if (
3ace0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
3acf0 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20  pe (argv[1]) == 
3ad00 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a  SQLITE_INTEGER).
3ad10 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76        {..  int_v
3ad20 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  alue = sqlite3_v
3ad30 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31  alue_int (argv[1
3ad40 5d 29 3b 0a 09 20 20 79 20 3d 20 69 6e 74 5f 76  ]);..  y = int_v
3ad50 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  alue;.      }.  
3ad60 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
3ad70 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3ad80 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
3ad90 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
3ada0 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69    }.    if (sqli
3adb0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
3adc0 61 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49  argv[2]) == SQLI
3add0 54 45 5f 49 4e 54 45 47 45 52 29 0a 09 73 72 69  TE_INTEGER)..sri
3ade0 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
3adf0 65 5f 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b  e_int (argv[2]);
3ae00 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
3ae10 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
3ae20 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
3ae30 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
3ae40 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 4d       }.    gaiaM
3ae50 61 6b 65 50 6f 69 6e 74 20 28 78 2c 20 79 2c 20  akePoint (x, y, 
3ae60 73 72 69 64 2c 20 26 70 5f 72 65 73 75 6c 74 2c  srid, &p_result,
3ae70 20 26 6c 65 6e 29 3b 0a 20 20 20 20 69 66 20 28   &len);.    if (
3ae80 21 70 5f 72 65 73 75 6c 74 29 0a 09 73 71 6c 69  !p_result)..sqli
3ae90 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
3aea0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
3aeb0 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  lse..sqlite3_res
3aec0 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78  ult_blob (contex
3aed0 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e  t, p_result, len
3aee0 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74  , free);.}..stat
3aef0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 61 6b  ic void.fnct_Mak
3af00 65 50 6f 69 6e 74 5a 31 20 28 73 71 6c 69 74 65  ePointZ1 (sqlite
3af10 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
3af20 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
3af30 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
3af40 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
3af50 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4d 61 6b 65 50  unction:./ MakeP
3af60 6f 69 6e 74 5a 28 64 6f 75 62 6c 65 20 58 2c 20  ointZ(double X, 
3af70 64 6f 75 62 6c 65 20 59 2c 20 64 6f 75 62 6c 65  double Y, double
3af80 20 5a 29 0a 2f 0a 2f 20 62 75 69 6c 64 73 20 61   Z)././ builds a
3af90 20 50 4f 49 4e 54 20 5a 20 0a 2f 20 6f 72 20 4e   POINT Z ./ or N
3afa0 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
3afb0 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
3afc0 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  */.    int len;.
3afd0 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75      int int_valu
3afe0 65 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  e;.    unsigned 
3aff0 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
3b000 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62 6c   NULL;.    doubl
3b010 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  e x;.    double 
3b020 79 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 7a 3b  y;.    double z;
3b030 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
3b040 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
3b050 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
3b060 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
3b070 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
3b080 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29  == SQLITE_FLOAT)
3b090 0a 09 78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ..x = sqlite3_va
3b0a0 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76  lue_double (argv
3b0b0 5b 30 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69  [0]);.    else i
3b0c0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
3b0d0 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
3b0e0 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
3b0f0 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e  R).      {..  in
3b100 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  t_value = sqlite
3b110 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
3b120 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e  v[0]);..  x = in
3b130 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d  t_value;.      }
3b140 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
3b150 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
3b160 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
3b170 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
3b180 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73       }.    if (s
3b190 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3b1a0 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53  e (argv[1]) == S
3b1b0 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 79 20  QLITE_FLOAT)..y 
3b1c0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3b1d0 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29  double (argv[1])
3b1e0 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73  ;.    else if (s
3b1f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3b200 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53  e (argv[1]) == S
3b210 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
3b220 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61       {..  int_va
3b230 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lue = sqlite3_va
3b240 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d  lue_int (argv[1]
3b250 29 3b 0a 09 20 20 79 20 3d 20 69 6e 74 5f 76 61  );..  y = int_va
3b260 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lue;.      }.   
3b270 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
3b280 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3b290 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
3b2a0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
3b2b0 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74   }.    if (sqlit
3b2c0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3b2d0 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[2]) == SQLIT
3b2e0 45 5f 46 4c 4f 41 54 29 0a 09 7a 20 3d 20 73 71  E_FLOAT)..z = sq
3b2f0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
3b300 6c 65 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  le (argv[2]);.  
3b310 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74    else if (sqlit
3b320 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3b330 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[2]) == SQLIT
3b340 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
3b350 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20   {..  int_value 
3b360 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3b370 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09  int (argv[2]);..
3b380 20 20 7a 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b    z = int_value;
3b390 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
3b3a0 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
3b3b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
3b3c0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
3b3d0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
3b3e0 20 20 20 67 61 69 61 4d 61 6b 65 50 6f 69 6e 74     gaiaMakePoint
3b3f0 5a 20 28 78 2c 20 79 2c 20 7a 2c 20 30 2c 20 26  Z (x, y, z, 0, &
3b400 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b  p_result, &len);
3b410 0a 20 20 20 20 69 66 20 28 21 70 5f 72 65 73 75  .    if (!p_resu
3b420 6c 74 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  lt)..sqlite3_res
3b430 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
3b440 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 09 73 71  t);.    else..sq
3b450 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
3b460 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
3b470 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29  sult, len, free)
3b480 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
3b490 0a 66 6e 63 74 5f 4d 61 6b 65 50 6f 69 6e 74 5a  .fnct_MakePointZ
3b4a0 32 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  2 (sqlite3_conte
3b4b0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
3b4c0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
3b4d0 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
3b4e0 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
3b4f0 3a 0a 2f 20 4d 61 6b 65 50 6f 69 6e 74 5a 28 64  :./ MakePointZ(d
3b500 6f 75 62 6c 65 20 58 2c 20 64 6f 75 62 6c 65 20  ouble X, double 
3b510 59 2c 20 64 6f 75 62 6c 65 20 5a 2c 20 69 6e 74  Y, double Z, int
3b520 20 53 52 49 44 29 0a 2f 0a 2f 20 62 75 69 6c 64   SRID)././ build
3b530 73 20 61 20 50 4f 49 4e 54 20 5a 0a 2f 20 6f 72  s a POINT Z./ or
3b540 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
3b550 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
3b560 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e  d.*/.    int len
3b570 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61  ;.    int int_va
3b580 6c 75 65 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  lue;.    unsigne
3b590 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
3b5a0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75   = NULL;.    dou
3b5b0 62 6c 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c  ble x;.    doubl
3b5c0 65 20 79 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  e y;.    double 
3b5d0 7a 3b 0a 20 20 20 20 69 6e 74 20 73 72 69 64 3b  z;.    int srid;
3b5e0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
3b5f0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
3b600 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
3b610 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
3b620 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
3b630 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29  == SQLITE_FLOAT)
3b640 0a 09 78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ..x = sqlite3_va
3b650 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76  lue_double (argv
3b660 5b 30 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69  [0]);.    else i
3b670 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
3b680 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
3b690 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
3b6a0 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e  R).      {..  in
3b6b0 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  t_value = sqlite
3b6c0 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
3b6d0 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e  v[0]);..  x = in
3b6e0 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d  t_value;.      }
3b6f0 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
3b700 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
3b710 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
3b720 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
3b730 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73       }.    if (s
3b740 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3b750 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53  e (argv[1]) == S
3b760 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 79 20  QLITE_FLOAT)..y 
3b770 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3b780 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29  double (argv[1])
3b790 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73  ;.    else if (s
3b7a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3b7b0 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53  e (argv[1]) == S
3b7c0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
3b7d0 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61       {..  int_va
3b7e0 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lue = sqlite3_va
3b7f0 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d  lue_int (argv[1]
3b800 29 3b 0a 09 20 20 79 20 3d 20 69 6e 74 5f 76 61  );..  y = int_va
3b810 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lue;.      }.   
3b820 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
3b830 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3b840 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
3b850 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
3b860 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74   }.    if (sqlit
3b870 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3b880 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[2]) == SQLIT
3b890 45 5f 46 4c 4f 41 54 29 0a 09 7a 20 3d 20 73 71  E_FLOAT)..z = sq
3b8a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
3b8b0 6c 65 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  le (argv[2]);.  
3b8c0 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74    else if (sqlit
3b8d0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3b8e0 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[2]) == SQLIT
3b8f0 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
3b900 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20   {..  int_value 
3b910 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3b920 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09  int (argv[2]);..
3b930 20 20 7a 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b    z = int_value;
3b940 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
3b950 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
3b960 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
3b970 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
3b980 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
3b990 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
3b9a0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
3b9b0 33 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  3]) == SQLITE_IN
3b9c0 54 45 47 45 52 29 0a 09 73 72 69 64 20 3d 20 73  TEGER)..srid = s
3b9d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
3b9e0 20 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20   (argv[3]);.    
3b9f0 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
3ba00 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
3ba10 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
3ba20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
3ba30 7d 0a 20 20 20 20 67 61 69 61 4d 61 6b 65 50 6f  }.    gaiaMakePo
3ba40 69 6e 74 5a 20 28 78 2c 20 79 2c 20 7a 2c 20 73  intZ (x, y, z, s
3ba50 72 69 64 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  rid, &p_result, 
3ba60 26 6c 65 6e 29 3b 0a 20 20 20 20 69 66 20 28 21  &len);.    if (!
3ba70 70 5f 72 65 73 75 6c 74 29 0a 09 73 71 6c 69 74  p_result)..sqlit
3ba80 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
3ba90 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
3baa0 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  se..sqlite3_resu
3bab0 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74  lt_blob (context
3bac0 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c  , p_result, len,
3bad0 20 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69   free);.}..stati
3bae0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 61 6b 65  c void.fnct_Make
3baf0 50 6f 69 6e 74 4d 31 20 28 73 71 6c 69 74 65 33  PointM1 (sqlite3
3bb00 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
3bb10 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
3bb20 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
3bb30 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
3bb40 6e 63 74 69 6f 6e 3a 0a 2f 20 4d 61 6b 65 50 6f  nction:./ MakePo
3bb50 69 6e 74 4d 28 64 6f 75 62 6c 65 20 58 2c 20 64  intM(double X, d
3bb60 6f 75 62 6c 65 20 59 2c 20 64 6f 75 62 6c 65 20  ouble Y, double 
3bb70 4d 29 0a 2f 0a 2f 20 62 75 69 6c 64 73 20 61 20  M)././ builds a 
3bb80 50 4f 49 4e 54 20 4d 0a 2f 20 6f 72 20 4e 55 4c  POINT M./ or NUL
3bb90 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69  L if any error i
3bba0 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f  s encountered.*/
3bbb0 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  .    int len;.  
3bbc0 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b    int int_value;
3bbd0 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
3bbe0 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e  ar *p_result = N
3bbf0 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  ULL;.    double 
3bc00 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79 3b  x;.    double y;
3bc10 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d 3b 0a 20  .    double m;. 
3bc20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
3bc30 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
3bc40 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
3bc50 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
3bc60 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
3bc70 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09   SQLITE_FLOAT)..
3bc80 78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  x = sqlite3_valu
3bc90 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 30  e_double (argv[0
3bca0 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20  ]);.    else if 
3bcb0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
3bcc0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
3bcd0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
3bce0 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f  .      {..  int_
3bcf0 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  value = sqlite3_
3bd00 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
3bd10 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f  0]);..  x = int_
3bd20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  value;.      }. 
3bd30 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
3bd40 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
3bd50 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
3bd60 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
3bd70 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c     }.    if (sql
3bd80 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
3bd90 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
3bda0 49 54 45 5f 46 4c 4f 41 54 29 0a 09 79 20 3d 20  ITE_FLOAT)..y = 
3bdb0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
3bdc0 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a  uble (argv[1]);.
3bdd0 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c      else if (sql
3bde0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
3bdf0 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
3be00 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20  ITE_INTEGER).   
3be10 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75     {..  int_valu
3be20 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
3be30 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b  e_int (argv[1]);
3be40 0a 09 20 20 79 20 3d 20 69 6e 74 5f 76 61 6c 75  ..  y = int_valu
3be50 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65  e;.      }.    e
3be60 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  lse.      {..  s
3be70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3be80 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
3be90 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
3bea0 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
3beb0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
3bec0 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[2]) == SQLITE_
3bed0 46 4c 4f 41 54 29 0a 09 6d 20 3d 20 73 71 6c 69  FLOAT)..m = sqli
3bee0 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
3bef0 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20   (argv[2]);.    
3bf00 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33  else if (sqlite3
3bf10 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
3bf20 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[2]) == SQLITE_
3bf30 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b  INTEGER).      {
3bf40 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20  ..  int_value = 
3bf50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
3bf60 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20  t (argv[2]);..  
3bf70 6d 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20  m = int_value;. 
3bf80 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
3bf90 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
3bfa0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
3bfb0 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
3bfc0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
3bfd0 20 67 61 69 61 4d 61 6b 65 50 6f 69 6e 74 4d 20   gaiaMakePointM 
3bfe0 28 78 2c 20 79 2c 20 6d 2c 20 30 2c 20 26 70 5f  (x, y, m, 0, &p_
3bff0 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20  result, &len);. 
3c000 20 20 20 69 66 20 28 21 70 5f 72 65 73 75 6c 74     if (!p_result
3c010 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
3c020 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
3c030 3b 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c 69  ;.    else..sqli
3c040 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
3c050 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
3c060 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
3c070 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
3c080 6e 63 74 5f 4d 61 6b 65 50 6f 69 6e 74 4d 32 20  nct_MakePointM2 
3c090 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3c0a0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
3c0b0 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
3c0c0 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
3c0d0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
3c0e0 2f 20 4d 61 6b 65 50 6f 69 6e 74 4d 28 64 6f 75  / MakePointM(dou
3c0f0 62 6c 65 20 58 2c 20 64 6f 75 62 6c 65 20 59 2c  ble X, double Y,
3c100 20 64 6f 75 62 6c 65 20 4d 2c 20 69 6e 74 20 53   double M, int S
3c110 52 49 44 29 0a 2f 0a 2f 20 62 75 69 6c 64 73 20  RID)././ builds 
3c120 61 20 50 4f 49 4e 54 20 4d 0a 2f 20 6f 72 20 4e  a POINT M./ or N
3c130 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
3c140 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
3c150 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  */.    int len;.
3c160 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75      int int_valu
3c170 65 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  e;.    unsigned 
3c180 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
3c190 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62 6c   NULL;.    doubl
3c1a0 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  e x;.    double 
3c1b0 79 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d 3b  y;.    double m;
3c1c0 0a 20 20 20 20 69 6e 74 20 73 72 69 64 3b 0a 20  .    int srid;. 
3c1d0 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
3c1e0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
3c1f0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
3c200 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
3c210 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
3c220 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09   SQLITE_FLOAT)..
3c230 78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  x = sqlite3_valu
3c240 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 30  e_double (argv[0
3c250 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20  ]);.    else if 
3c260 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
3c270 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
3c280 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
3c290 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f  .      {..  int_
3c2a0 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  value = sqlite3_
3c2b0 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
3c2c0 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f  0]);..  x = int_
3c2d0 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  value;.      }. 
3c2e0 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
3c2f0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
3c300 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
3c310 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
3c320 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c     }.    if (sql
3c330 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
3c340 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
3c350 49 54 45 5f 46 4c 4f 41 54 29 0a 09 79 20 3d 20  ITE_FLOAT)..y = 
3c360 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
3c370 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a  uble (argv[1]);.
3c380 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c      else if (sql
3c390 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
3c3a0 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
3c3b0 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20  ITE_INTEGER).   
3c3c0 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75     {..  int_valu
3c3d0 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
3c3e0 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b  e_int (argv[1]);
3c3f0 0a 09 20 20 79 20 3d 20 69 6e 74 5f 76 61 6c 75  ..  y = int_valu
3c400 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65  e;.      }.    e
3c410 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  lse.      {..  s
3c420 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3c430 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
3c440 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
3c450 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
3c460 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
3c470 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[2]) == SQLITE_
3c480 46 4c 4f 41 54 29 0a 09 6d 20 3d 20 73 71 6c 69  FLOAT)..m = sqli
3c490 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
3c4a0 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20   (argv[2]);.    
3c4b0 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33  else if (sqlite3
3c4c0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
3c4d0 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[2]) == SQLITE_
3c4e0 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b  INTEGER).      {
3c4f0 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20  ..  int_value = 
3c500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
3c510 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20  t (argv[2]);..  
3c520 6d 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20  m = int_value;. 
3c530 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
3c540 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
3c550 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
3c560 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
3c570 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
3c580 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
3c590 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 33 5d  ue_type (argv[3]
3c5a0 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  ) == SQLITE_INTE
3c5b0 47 45 52 29 0a 09 73 72 69 64 20 3d 20 73 71 6c  GER)..srid = sql
3c5c0 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
3c5d0 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 65 6c  argv[3]);.    el
3c5e0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  se.      {..  sq
3c5f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
3c600 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
3c610 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
3c620 20 20 20 20 67 61 69 61 4d 61 6b 65 50 6f 69 6e      gaiaMakePoin
3c630 74 4d 20 28 78 2c 20 79 2c 20 6d 2c 20 73 72 69  tM (x, y, m, sri
3c640 64 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c  d, &p_result, &l
3c650 65 6e 29 3b 0a 20 20 20 20 69 66 20 28 21 70 5f  en);.    if (!p_
3c660 72 65 73 75 6c 74 29 0a 09 73 71 6c 69 74 65 33  result)..sqlite3
3c670 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
3c680 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
3c690 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
3c6a0 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20  _blob (context, 
3c6b0 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66  p_result, len, f
3c6c0 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ree);.}..static 
3c6d0 76 6f 69 64 0a 66 6e 63 74 5f 4d 61 6b 65 50 6f  void.fnct_MakePo
3c6e0 69 6e 74 5a 4d 31 20 28 73 71 6c 69 74 65 33 5f  intZM1 (sqlite3_
3c6f0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
3c700 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
3c710 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
3c720 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
3c730 63 74 69 6f 6e 3a 0a 2f 20 4d 61 6b 65 50 6f 69  ction:./ MakePoi
3c740 6e 74 5a 4d 28 64 6f 75 62 6c 65 20 58 2c 20 64  ntZM(double X, d
3c750 6f 75 62 6c 65 20 59 2c 20 64 6f 75 62 6c 65 20  ouble Y, double 
3c760 5a 2c 20 64 6f 75 62 6c 65 20 4d 29 0a 2f 0a 2f  Z, double M)././
3c770 20 62 75 69 6c 64 73 20 61 20 50 4f 49 4e 54 20   builds a POINT 
3c780 5a 4d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  ZM ./ or NULL if
3c790 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
3c7a0 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
3c7b0 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 69 6e   int len;.    in
3c7c0 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  t int_value;.   
3c7d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
3c7e0 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
3c7f0 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20  .    double x;. 
3c800 20 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20     double y;.   
3c810 20 64 6f 75 62 6c 65 20 7a 3b 0a 20 20 20 20 64   double z;.    d
3c820 6f 75 62 6c 65 20 6d 3b 0a 20 20 20 20 47 41 49  ouble m;.    GAI
3c830 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
3c840 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
3c850 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
3c860 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3c870 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[0]) == SQLIT
3c880 45 5f 46 4c 4f 41 54 29 0a 09 78 20 3d 20 73 71  E_FLOAT)..x = sq
3c890 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
3c8a0 6c 65 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  le (argv[0]);.  
3c8b0 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74    else if (sqlit
3c8c0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
3c8d0 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[0]) == SQLIT
3c8e0 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
3c8f0 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20   {..  int_value 
3c900 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3c910 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09  int (argv[0]);..
3c920 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b    x = int_value;
3c930 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
3c940 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
3c950 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
3c960 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
3c970 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
3c980 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
3c990 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
3c9a0 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c  1]) == SQLITE_FL
3c9b0 4f 41 54 29 0a 09 79 20 3d 20 73 71 6c 69 74 65  OAT)..y = sqlite
3c9c0 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28  3_value_double (
3c9d0 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c  argv[1]);.    el
3c9e0 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  se if (sqlite3_v
3c9f0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
3ca00 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  1]) == SQLITE_IN
3ca10 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09  TEGER).      {..
3ca20 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71    int_value = sq
3ca30 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
3ca40 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 79 20  (argv[1]);..  y 
3ca50 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  = int_value;.   
3ca60 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20     }.    else.  
3ca70 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
3ca80 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
3ca90 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
3caa0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
3cab0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
3cac0 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20  _type (argv[2]) 
3cad0 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29  == SQLITE_FLOAT)
3cae0 0a 09 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ..z = sqlite3_va
3caf0 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76  lue_double (argv
3cb00 5b 32 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69  [2]);.    else i
3cb10 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
3cb20 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20  _type (argv[2]) 
3cb30 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
3cb40 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e  R).      {..  in
3cb50 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  t_value = sqlite
3cb60 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
3cb70 76 5b 32 5d 29 3b 0a 09 20 20 7a 20 3d 20 69 6e  v[2]);..  z = in
3cb80 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d  t_value;.      }
3cb90 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
3cba0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
3cbb0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
3cbc0 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
3cbd0 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73       }.    if (s
3cbe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3cbf0 65 20 28 61 72 67 76 5b 33 5d 29 20 3d 3d 20 53  e (argv[3]) == S
3cc00 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 6d 20  QLITE_FLOAT)..m 
3cc10 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3cc20 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 33 5d 29  double (argv[3])
3cc30 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73  ;.    else if (s
3cc40 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3cc50 65 20 28 61 72 67 76 5b 33 5d 29 20 3d 3d 20 53  e (argv[3]) == S
3cc60 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
3cc70 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61       {..  int_va
3cc80 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lue = sqlite3_va
3cc90 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 33 5d  lue_int (argv[3]
3cca0 29 3b 0a 09 20 20 6d 20 3d 20 69 6e 74 5f 76 61  );..  m = int_va
3ccb0 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lue;.      }.   
3ccc0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
3ccd0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3cce0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
3ccf0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
3cd00 20 7d 0a 20 20 20 20 67 61 69 61 4d 61 6b 65 50   }.    gaiaMakeP
3cd10 6f 69 6e 74 5a 4d 20 28 78 2c 20 79 2c 20 7a 2c  ointZM (x, y, z,
3cd20 20 6d 2c 20 30 2c 20 26 70 5f 72 65 73 75 6c 74   m, 0, &p_result
3cd30 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20 69 66 20  , &len);.    if 
3cd40 28 21 70 5f 72 65 73 75 6c 74 29 0a 09 73 71 6c  (!p_result)..sql
3cd50 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
3cd60 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
3cd70 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65  else..sqlite3_re
3cd80 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
3cd90 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
3cda0 6e 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61  n, free);.}..sta
3cdb0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 61  tic void.fnct_Ma
3cdc0 6b 65 50 6f 69 6e 74 5a 4d 32 20 28 73 71 6c 69  kePointZM2 (sqli
3cdd0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
3cde0 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
3cdf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
3ce00 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
3ce10 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4d 61 6b   function:./ Mak
3ce20 65 50 6f 69 6e 74 5a 4d 28 64 6f 75 62 6c 65 20  ePointZM(double 
3ce30 58 2c 20 64 6f 75 62 6c 65 20 59 2c 20 64 6f 75  X, double Y, dou
3ce40 62 6c 65 20 5a 2c 20 64 6f 75 62 6c 65 20 4d 2c  ble Z, double M,
3ce50 20 69 6e 74 20 53 52 49 44 29 0a 2f 0a 2f 20 62   int SRID)././ b
3ce60 75 69 6c 64 73 20 61 20 50 4f 49 4e 54 20 0a 2f  uilds a POINT ./
3ce70 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
3ce80 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
3ce90 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20  ered.*/.    int 
3cea0 6c 65 6e 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74  len;.    int int
3ceb0 5f 76 61 6c 75 65 3b 0a 20 20 20 20 75 6e 73 69  _value;.    unsi
3cec0 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
3ced0 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ult = NULL;.    
3cee0 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 64 6f  double x;.    do
3cef0 75 62 6c 65 20 79 3b 0a 20 20 20 20 64 6f 75 62  uble y;.    doub
3cf00 6c 65 20 7a 3b 0a 20 20 20 20 64 6f 75 62 6c 65  le z;.    double
3cf10 20 6d 3b 0a 20 20 20 20 69 6e 74 20 73 72 69 64   m;.    int srid
3cf20 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
3cf30 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
3cf40 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
3cf50 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
3cf60 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
3cf70 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54   == SQLITE_FLOAT
3cf80 29 0a 09 78 20 3d 20 73 71 6c 69 74 65 33 5f 76  )..x = sqlite3_v
3cf90 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67  alue_double (arg
3cfa0 76 5b 30 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20  v[0]);.    else 
3cfb0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
3cfc0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
3cfd0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
3cfe0 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  ER).      {..  i
3cff0 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  nt_value = sqlit
3d000 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
3d010 67 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69  gv[0]);..  x = i
3d020 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  nt_value;.      
3d030 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  }.    else.     
3d040 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
3d050 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
3d060 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
3d070 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
3d080 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
3d090 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20  pe (argv[1]) == 
3d0a0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 79  SQLITE_FLOAT)..y
3d0b0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3d0c0 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d  _double (argv[1]
3d0d0 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28  );.    else if (
3d0e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
3d0f0 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20  pe (argv[1]) == 
3d100 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a  SQLITE_INTEGER).
3d110 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76        {..  int_v
3d120 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  alue = sqlite3_v
3d130 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31  alue_int (argv[1
3d140 5d 29 3b 0a 09 20 20 79 20 3d 20 69 6e 74 5f 76  ]);..  y = int_v
3d150 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  alue;.      }.  
3d160 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
3d170 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3d180 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
3d190 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
3d1a0 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69    }.    if (sqli
3d1b0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
3d1c0 61 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49  argv[2]) == SQLI
3d1d0 54 45 5f 46 4c 4f 41 54 29 0a 09 7a 20 3d 20 73  TE_FLOAT)..z = s
3d1e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
3d1f0 62 6c 65 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20  ble (argv[2]);. 
3d200 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69     else if (sqli
3d210 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
3d220 61 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49  argv[2]) == SQLI
3d230 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20  TE_INTEGER).    
3d240 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65    {..  int_value
3d250 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3d260 5f 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a  _int (argv[2]);.
3d270 09 20 20 7a 20 3d 20 69 6e 74 5f 76 61 6c 75 65  .  z = int_value
3d280 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c  ;.      }.    el
3d290 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  se.      {..  sq
3d2a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
3d2b0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
3d2c0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
3d2d0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
3d2e0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
3d2f0 5b 33 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46  [3]) == SQLITE_F
3d300 4c 4f 41 54 29 0a 09 6d 20 3d 20 73 71 6c 69 74  LOAT)..m = sqlit
3d310 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
3d320 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 65  (argv[3]);.    e
3d330 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f  lse if (sqlite3_
3d340 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
3d350 5b 33 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [3]) == SQLITE_I
3d360 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
3d370 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73  .  int_value = s
3d380 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
3d390 20 28 61 72 67 76 5b 33 5d 29 3b 0a 09 20 20 6d   (argv[3]);..  m
3d3a0 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20   = int_value;.  
3d3b0 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20      }.    else. 
3d3c0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
3d3d0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
3d3e0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
3d3f0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
3d400 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
3d410 65 5f 74 79 70 65 20 28 61 72 67 76 5b 34 5d 29  e_type (argv[4])
3d420 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
3d430 45 52 29 0a 09 73 72 69 64 20 3d 20 73 71 6c 69  ER)..srid = sqli
3d440 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61  te3_value_int (a
3d450 72 67 76 5b 34 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[4]);.    els
3d460 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
3d470 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
3d480 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
3d490 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
3d4a0 20 20 20 67 61 69 61 4d 61 6b 65 50 6f 69 6e 74     gaiaMakePoint
3d4b0 5a 4d 20 28 78 2c 20 79 2c 20 7a 2c 20 6d 2c 20  ZM (x, y, z, m, 
3d4c0 73 72 69 64 2c 20 26 70 5f 72 65 73 75 6c 74 2c  srid, &p_result,
3d4d0 20 26 6c 65 6e 29 3b 0a 20 20 20 20 69 66 20 28   &len);.    if (
3d4e0 21 70 5f 72 65 73 75 6c 74 29 0a 09 73 71 6c 69  !p_result)..sqli
3d4f0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
3d500 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
3d510 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  lse..sqlite3_res
3d520 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78  ult_blob (contex
3d530 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e  t, p_result, len
3d540 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74  , free);.}..stat
3d550 69 63 20 76 6f 69 64 0a 61 64 64 47 65 6f 6d 50  ic void.addGeomP
3d560 6f 69 6e 74 54 6f 44 79 6e 61 6d 69 63 4c 69 6e  ointToDynamicLin
3d570 65 20 28 67 61 69 61 44 79 6e 61 6d 69 63 4c 69  e (gaiaDynamicLi
3d580 6e 65 50 74 72 20 64 79 6e 2c 20 67 61 69 61 47  nePtr dyn, gaiaG
3d590 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d 29  eomCollPtr geom)
3d5a0 0a 7b 0a 2f 2a 20 61 70 70 65 6e 64 69 6e 67 20  .{./* appending 
3d5b0 61 20 73 69 6d 70 6c 65 2d 50 6f 69 6e 74 20 47  a simple-Point G
3d5c0 65 6f 6d 65 74 72 79 20 74 6f 20 61 20 44 79 6e  eometry to a Dyn
3d5d0 61 6d 69 63 20 4c 69 6e 65 20 2a 2f 0a 20 20 20  amic Line */.   
3d5e0 20 69 6e 74 20 70 74 73 3b 0a 20 20 20 20 69 6e   int pts;.    in
3d5f0 74 20 6c 6e 73 3b 0a 20 20 20 20 69 6e 74 20 70  t lns;.    int p
3d600 67 73 3b 0a 20 20 20 20 67 61 69 61 50 6f 69 6e  gs;.    gaiaPoin
3d610 74 50 74 72 20 70 74 3b 0a 20 20 20 20 67 61 69  tPtr pt;.    gai
3d620 61 4c 69 6e 65 73 74 72 69 6e 67 50 74 72 20 6c  aLinestringPtr l
3d630 6e 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79 67  n;.    gaiaPolyg
3d640 6f 6e 50 74 72 20 70 67 3b 0a 0a 20 20 20 20 69  onPtr pg;..    i
3d650 66 20 28 64 79 6e 20 3d 3d 20 4e 55 4c 4c 29 0a  f (dyn == NULL).
3d660 09 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 20  .return;.    if 
3d670 28 64 79 6e 2d 3e 45 72 72 6f 72 29 0a 09 72 65  (dyn->Error)..re
3d680 74 75 72 6e 3b 0a 2f 2a 20 63 68 65 63 6b 69 6e  turn;./* checkin
3d690 67 20 69 66 20 47 45 4f 4d 20 73 69 6d 70 6c 79  g if GEOM simply
3d6a0 20 69 73 20 61 20 50 4f 49 4e 54 20 2a 2f 0a 20   is a POINT */. 
3d6b0 20 20 20 69 66 20 28 67 65 6f 6d 20 3d 3d 20 4e     if (geom == N
3d6c0 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  ULL).      {..  
3d6d0 64 79 6e 2d 3e 45 72 72 6f 72 20 3d 20 31 3b 0a  dyn->Error = 1;.
3d6e0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
3d6f0 20 7d 0a 20 20 20 20 70 74 73 20 3d 20 30 3b 0a   }.    pts = 0;.
3d700 20 20 20 20 6c 6e 73 20 3d 20 30 3b 0a 20 20 20      lns = 0;.   
3d710 20 70 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 74   pgs = 0;.    pt
3d720 20 3d 20 67 65 6f 6d 2d 3e 46 69 72 73 74 50 6f   = geom->FirstPo
3d730 69 6e 74 3b 0a 20 20 20 20 77 68 69 6c 65 20 28  int;.    while (
3d740 70 74 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 70  pt).      {..  p
3d750 74 73 2b 2b 3b 0a 09 20 20 70 74 20 3d 20 70 74  ts++;..  pt = pt
3d760 2d 3e 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  ->Next;.      }.
3d770 20 20 20 20 6c 6e 20 3d 20 67 65 6f 6d 2d 3e 46      ln = geom->F
3d780 69 72 73 74 4c 69 6e 65 73 74 72 69 6e 67 3b 0a  irstLinestring;.
3d790 20 20 20 20 77 68 69 6c 65 20 28 6c 6e 29 0a 20      while (ln). 
3d7a0 20 20 20 20 20 7b 0a 09 20 20 6c 6e 73 2b 2b 3b       {..  lns++;
3d7b0 0a 09 20 20 6c 6e 20 3d 20 6c 6e 2d 3e 4e 65 78  ..  ln = ln->Nex
3d7c0 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  t;.      }.    p
3d7d0 67 20 3d 20 67 65 6f 6d 2d 3e 46 69 72 73 74 50  g = geom->FirstP
3d7e0 6f 6c 79 67 6f 6e 3b 0a 20 20 20 20 77 68 69 6c  olygon;.    whil
3d7f0 65 20 28 70 67 29 0a 20 20 20 20 20 20 7b 0a 09  e (pg).      {..
3d800 20 20 70 67 73 2b 2b 3b 0a 09 20 20 70 67 20 3d    pgs++;..  pg =
3d810 20 70 67 2d 3e 4e 65 78 74 3b 0a 20 20 20 20 20   pg->Next;.     
3d820 20 7d 0a 20 20 20 20 69 66 20 28 70 74 73 20 3d   }.    if (pts =
3d830 3d 20 31 20 26 26 20 6c 6e 73 20 3d 3d 20 30 20  = 1 && lns == 0 
3d840 26 26 20 70 67 73 20 3d 3d 20 30 29 0a 09 3b 0a  && pgs == 0)..;.
3d850 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
3d860 0a 09 20 20 2f 2a 20 66 61 69 6c 75 72 65 3a 20  ..  /* failure: 
3d870 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 50 4f 49  not a simple POI
3d880 4e 54 20 2a 2f 0a 09 20 20 64 79 6e 2d 3e 45 72  NT */..  dyn->Er
3d890 72 6f 72 20 3d 20 31 3b 0a 09 20 20 72 65 74 75  ror = 1;..  retu
3d8a0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rn;.      }..   
3d8b0 20 69 66 20 28 64 79 6e 2d 3e 53 72 69 64 20 21   if (dyn->Srid !
3d8c0 3d 20 67 65 6f 6d 2d 3e 53 72 69 64 29 0a 20 20  = geom->Srid).  
3d8d0 20 20 20 20 7b 0a 09 20 20 2f 2a 20 66 61 69 6c      {..  /* fail
3d8e0 75 72 65 3a 20 53 52 49 44 20 6d 69 73 6d 61 74  ure: SRID mismat
3d8f0 63 68 20 2a 2f 0a 09 20 20 64 79 6e 2d 3e 45 72  ch */..  dyn->Er
3d900 72 6f 72 20 3d 20 31 3b 0a 09 20 20 72 65 74 75  ror = 1;..  retu
3d910 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rn;.      }..   
3d920 20 73 77 69 74 63 68 20 28 67 65 6f 6d 2d 3e 46   switch (geom->F
3d930 69 72 73 74 50 6f 69 6e 74 2d 3e 44 69 6d 65 6e  irstPoint->Dimen
3d940 73 69 6f 6e 4d 6f 64 65 6c 29 0a 20 20 20 20 20  sionModel).     
3d950 20 7b 0a 20 20 20 20 20 20 63 61 73 65 20 47 41   {.      case GA
3d960 49 41 5f 58 59 5f 5a 5f 4d 3a 0a 09 20 20 67 61  IA_XY_Z_M:..  ga
3d970 69 61 41 70 70 65 6e 64 50 6f 69 6e 74 5a 4d 54  iaAppendPointZMT
3d980 6f 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 64 79  oDynamicLine (dy
3d990 6e 2c 20 67 65 6f 6d 2d 3e 46 69 72 73 74 50 6f  n, geom->FirstPo
3d9a0 69 6e 74 2d 3e 58 2c 0a 09 09 09 09 09 20 20 67  int->X,......  g
3d9b0 65 6f 6d 2d 3e 46 69 72 73 74 50 6f 69 6e 74 2d  eom->FirstPoint-
3d9c0 3e 59 2c 0a 09 09 09 09 09 20 20 67 65 6f 6d 2d  >Y,......  geom-
3d9d0 3e 46 69 72 73 74 50 6f 69 6e 74 2d 3e 5a 2c 0a  >FirstPoint->Z,.
3d9e0 09 09 09 09 09 20 20 67 65 6f 6d 2d 3e 46 69 72  .....  geom->Fir
3d9f0 73 74 50 6f 69 6e 74 2d 3e 4d 29 3b 0a 09 20 20  stPoint->M);..  
3da00 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3da10 65 20 47 41 49 41 5f 58 59 5f 5a 3a 0a 09 20 20  e GAIA_XY_Z:..  
3da20 67 61 69 61 41 70 70 65 6e 64 50 6f 69 6e 74 5a  gaiaAppendPointZ
3da30 54 6f 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 64  ToDynamicLine (d
3da40 79 6e 2c 20 67 65 6f 6d 2d 3e 46 69 72 73 74 50  yn, geom->FirstP
3da50 6f 69 6e 74 2d 3e 58 2c 0a 09 09 09 09 09 20 67  oint->X,...... g
3da60 65 6f 6d 2d 3e 46 69 72 73 74 50 6f 69 6e 74 2d  eom->FirstPoint-
3da70 3e 59 2c 0a 09 09 09 09 09 20 67 65 6f 6d 2d 3e  >Y,...... geom->
3da80 46 69 72 73 74 50 6f 69 6e 74 2d 3e 5a 29 3b 0a  FirstPoint->Z);.
3da90 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  .  break;.      
3daa0 63 61 73 65 20 47 41 49 41 5f 58 59 5f 4d 3a 0a  case GAIA_XY_M:.
3dab0 09 20 20 67 61 69 61 41 70 70 65 6e 64 50 6f 69  .  gaiaAppendPoi
3dac0 6e 74 4d 54 6f 44 79 6e 61 6d 69 63 4c 69 6e 65  ntMToDynamicLine
3dad0 20 28 64 79 6e 2c 20 67 65 6f 6d 2d 3e 46 69 72   (dyn, geom->Fir
3dae0 73 74 50 6f 69 6e 74 2d 3e 58 2c 0a 09 09 09 09  stPoint->X,.....
3daf0 09 20 67 65 6f 6d 2d 3e 46 69 72 73 74 50 6f 69  . geom->FirstPoi
3db00 6e 74 2d 3e 59 2c 0a 09 09 09 09 09 20 67 65 6f  nt->Y,...... geo
3db10 6d 2d 3e 46 69 72 73 74 50 6f 69 6e 74 2d 3e 4d  m->FirstPoint->M
3db20 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20  );..  break;.   
3db30 20 20 20 64 65 66 61 75 6c 74 3a 0a 09 20 20 67     default:..  g
3db40 61 69 61 41 70 70 65 6e 64 50 6f 69 6e 74 54 6f  aiaAppendPointTo
3db50 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 64 79 6e  DynamicLine (dyn
3db60 2c 20 67 65 6f 6d 2d 3e 46 69 72 73 74 50 6f 69  , geom->FirstPoi
3db70 6e 74 2d 3e 58 2c 0a 09 09 09 09 09 67 65 6f 6d  nt->X,......geom
3db80 2d 3e 46 69 72 73 74 50 6f 69 6e 74 2d 3e 59 29  ->FirstPoint->Y)
3db90 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;..  break;.    
3dba0 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f    }.}..static vo
3dbb0 69 64 0a 66 6e 63 74 5f 4d 61 6b 65 4c 69 6e 65  id.fnct_MakeLine
3dbc0 5f 73 74 65 70 20 28 73 71 6c 69 74 65 33 5f 63  _step (sqlite3_c
3dbd0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
3dbe0 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
3dbf0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
3dc00 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
3dc10 74 69 6f 6e 3a 0a 2f 20 4d 61 6b 65 4c 69 6e 65  tion:./ MakeLine
3dc20 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f  (BLOBencoded geo
3dc30 6d 29 0a 2f 0a 2f 20 61 67 67 72 65 67 61 74 65  m)././ aggregate
3dc40 20 66 75 6e 63 74 69 6f 6e 20 2d 20 53 54 45 50   function - STEP
3dc50 0a 2f 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ./.*/.    unsign
3dc60 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
3dc70 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
3dc80 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
3dc90 6c 6c 50 74 72 20 67 65 6f 6d 3b 0a 20 20 20 20  llPtr geom;.    
3dca0 67 61 69 61 44 79 6e 61 6d 69 63 4c 69 6e 65 50  gaiaDynamicLineP
3dcb0 74 72 20 2a 70 3b 0a 20 20 20 20 47 41 49 41 5f  tr *p;.    GAIA_
3dcc0 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
3dcd0 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
3dce0 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
3dcf0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
3dd00 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
3dd10 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
3dd20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3dd30 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
3dd40 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
3dd50 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
3dd60 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
3dd70 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
3dd80 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
3dd90 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
3dda0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
3ddb0 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
3ddc0 20 67 65 6f 6d 20 3d 20 67 61 69 61 46 72 6f 6d   geom = gaiaFrom
3ddd0 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
3dde0 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
3ddf0 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65  es);.    if (!ge
3de00 6f 6d 29 0a 09 72 65 74 75 72 6e 3b 0a 20 20 20  om)..return;.   
3de10 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
3de20 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 28  regate_context (
3de30 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 20  context, sizeof 
3de40 28 67 61 69 61 44 79 6e 61 6d 69 63 4c 69 6e 65  (gaiaDynamicLine
3de50 50 74 72 29 29 3b 0a 20 20 20 20 69 66 20 28 21  Ptr));.    if (!
3de60 28 2a 70 29 29 0a 20 20 20 20 20 20 7b 0a 09 20  (*p)).      {.. 
3de70 20 2f 2a 20 74 68 69 73 20 69 73 20 74 68 65 20   /* this is the 
3de80 66 69 72 73 74 20 72 6f 77 20 2a 2f 0a 09 20 20  first row */..  
3de90 2a 70 20 3d 20 67 61 69 61 41 6c 6c 6f 63 44 79  *p = gaiaAllocDy
3dea0 6e 61 6d 69 63 4c 69 6e 65 20 28 29 3b 0a 09 20  namicLine ();.. 
3deb0 20 28 2a 70 29 2d 3e 53 72 69 64 20 3d 20 67 65   (*p)->Srid = ge
3dec0 6f 6d 2d 3e 53 72 69 64 3b 0a 09 20 20 61 64 64  om->Srid;..  add
3ded0 47 65 6f 6d 50 6f 69 6e 74 54 6f 44 79 6e 61 6d  GeomPointToDynam
3dee0 69 63 4c 69 6e 65 20 28 2a 70 2c 20 67 65 6f 6d  icLine (*p, geom
3def0 29 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47 65  );..  gaiaFreeGe
3df00 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a 20  omColl (geom);. 
3df10 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
3df20 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 73 75        {..  /* su
3df30 62 73 65 71 75 65 6e 74 20 72 6f 77 73 20 2a 2f  bsequent rows */
3df40 0a 09 20 20 61 64 64 47 65 6f 6d 50 6f 69 6e 74  ..  addGeomPoint
3df50 54 6f 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 2a  ToDynamicLine (*
3df60 70 2c 20 67 65 6f 6d 29 3b 0a 09 20 20 67 61 69  p, geom);..  gai
3df70 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
3df80 65 6f 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a  eom);.      }.}.
3df90 0a 73 74 61 74 69 63 20 67 61 69 61 47 65 6f 6d  .static gaiaGeom
3dfa0 43 6f 6c 6c 50 74 72 0a 67 65 6f 6d 46 72 6f 6d  CollPtr.geomFrom
3dfb0 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 67 61 69  DynamicLine (gai
3dfc0 61 44 79 6e 61 6d 69 63 4c 69 6e 65 50 74 72 20  aDynamicLinePtr 
3dfd0 64 79 6e 29 0a 7b 0a 2f 2a 20 61 74 74 65 6d 70  dyn).{./* attemp
3dfe0 74 69 6e 67 20 74 6f 20 62 75 69 6c 64 20 61 20  ting to build a 
3dff0 47 65 6f 6d 65 74 72 79 20 66 72 6f 6d 20 61 20  Geometry from a 
3e000 44 79 6e 61 6d 69 63 20 4c 69 6e 65 20 2a 2f 0a  Dynamic Line */.
3e010 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
3e020 50 74 72 20 67 65 6f 6d 20 3d 20 4e 55 4c 4c 3b  Ptr geom = NULL;
3e030 0a 20 20 20 20 67 61 69 61 4c 69 6e 65 73 74 72  .    gaiaLinestr
3e040 69 6e 67 50 74 72 20 6c 6e 20 3d 20 4e 55 4c 4c  ingPtr ln = NULL
3e050 3b 0a 20 20 20 20 67 61 69 61 50 6f 69 6e 74 50  ;.    gaiaPointP
3e060 74 72 20 70 74 3b 0a 20 20 20 20 69 6e 74 20 69  tr pt;.    int i
3e070 76 3b 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74  v;.    int count
3e080 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 64 69   = 0;.    int di
3e090 6d 73 20 3d 20 47 41 49 41 5f 58 59 3b 0a 0a 20  ms = GAIA_XY;.. 
3e0a0 20 20 20 69 66 20 28 64 79 6e 20 3d 3d 20 4e 55     if (dyn == NU
3e0b0 4c 4c 29 0a 09 72 65 74 75 72 6e 20 4e 55 4c 4c  LL)..return NULL
3e0c0 3b 0a 20 20 20 20 69 66 20 28 64 79 6e 2d 3e 45  ;.    if (dyn->E
3e0d0 72 72 6f 72 29 0a 09 72 65 74 75 72 6e 20 4e 55  rror)..return NU
3e0e0 4c 4c 3b 0a 0a 20 20 20 20 70 74 20 3d 20 64 79  LL;..    pt = dy
3e0f0 6e 2d 3e 46 69 72 73 74 3b 0a 20 20 20 20 77 68  n->First;.    wh
3e100 69 6c 65 20 28 70 74 29 0a 20 20 20 20 20 20 7b  ile (pt).      {
3e110 0a 09 20 20 2f 2a 20 63 6f 75 6e 74 69 6e 67 20  ..  /* counting 
3e120 70 6f 69 6e 74 73 20 61 6e 64 20 63 68 65 63 6b  points and check
3e130 69 6e 67 20 64 69 6d 73 20 2a 2f 0a 09 20 20 63  ing dims */..  c
3e140 6f 75 6e 74 2b 2b 3b 0a 09 20 20 69 66 20 28 64  ount++;..  if (d
3e150 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59 20 26  ims == GAIA_XY &
3e160 26 20 70 74 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d  & pt->DimensionM
3e170 6f 64 65 6c 20 21 3d 20 47 41 49 41 5f 58 59 29  odel != GAIA_XY)
3e180 0a 09 20 20 20 20 20 20 64 69 6d 73 20 3d 20 70  ..      dims = p
3e190 74 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65  t->DimensionMode
3e1a0 6c 3b 0a 09 20 20 69 66 20 28 64 69 6d 73 20 3d  l;..  if (dims =
3e1b0 3d 20 47 41 49 41 5f 58 59 5f 5a 0a 09 20 20 20  = GAIA_XY_Z..   
3e1c0 20 20 20 26 26 20 28 70 74 2d 3e 44 69 6d 65 6e     && (pt->Dimen
3e1d0 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49  sionModel == GAI
3e1e0 41 5f 58 59 5f 4d 0a 09 09 20 20 7c 7c 20 70 74  A_XY_M...  || pt
3e1f0 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c  ->DimensionModel
3e200 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29   == GAIA_XY_Z_M)
3e210 29 0a 09 20 20 20 20 20 20 64 69 6d 73 20 3d 20  )..      dims = 
3e220 47 41 49 41 5f 58 59 5f 5a 5f 4d 3b 0a 09 20 20  GAIA_XY_Z_M;..  
3e230 69 66 20 28 64 69 6d 73 20 3d 3d 20 47 41 49 41  if (dims == GAIA
3e240 5f 58 59 5f 4d 0a 09 20 20 20 20 20 20 26 26 20  _XY_M..      && 
3e250 28 70 74 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f  (pt->DimensionMo
3e260 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a  del == GAIA_XY_Z
3e270 0a 09 09 20 20 7c 7c 20 70 74 2d 3e 44 69 6d 65  ...  || pt->Dime
3e280 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41  nsionModel == GA
3e290 49 41 5f 58 59 5f 5a 5f 4d 29 29 0a 09 20 20 20  IA_XY_Z_M))..   
3e2a0 20 20 20 64 69 6d 73 20 3d 20 47 41 49 41 5f 58     dims = GAIA_X
3e2b0 59 5f 5a 5f 4d 3b 0a 09 20 20 70 74 20 3d 20 70  Y_Z_M;..  pt = p
3e2c0 74 2d 3e 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  t->Next;.      }
3e2d0 0a 20 20 20 20 69 66 20 28 63 6f 75 6e 74 20 3c  .    if (count <
3e2e0 20 32 29 0a 09 72 65 74 75 72 6e 20 4e 55 4c 4c   2)..return NULL
3e2f0 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 20 28 64  ;..    switch (d
3e300 69 6d 73 29 0a 20 20 20 20 20 20 7b 0a 20 20 20  ims).      {.   
3e310 20 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f     case GAIA_XY_
3e320 5a 5f 4d 3a 0a 09 20 20 67 65 6f 6d 20 3d 20 67  Z_M:..  geom = g
3e330 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c  aiaAllocGeomColl
3e340 58 59 5a 4d 20 28 29 3b 0a 09 20 20 6c 6e 20 3d  XYZM ();..  ln =
3e350 20 67 61 69 61 41 6c 6c 6f 63 4c 69 6e 65 73 74   gaiaAllocLinest
3e360 72 69 6e 67 58 59 5a 4d 20 28 63 6f 75 6e 74 29  ringXYZM (count)
3e370 3b 0a 09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;..  break;.    
3e380 20 20 63 61 73 65 20 47 41 49 41 5f 58 59 5f 5a    case GAIA_XY_Z
3e390 3a 0a 09 20 20 67 65 6f 6d 20 3d 20 67 61 69 61  :..  geom = gaia
3e3a0 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58 59 5a  AllocGeomCollXYZ
3e3b0 20 28 29 3b 0a 09 20 20 6c 6e 20 3d 20 67 61 69   ();..  ln = gai
3e3c0 61 41 6c 6c 6f 63 4c 69 6e 65 73 74 72 69 6e 67  aAllocLinestring
3e3d0 58 59 5a 20 28 63 6f 75 6e 74 29 3b 0a 09 20 20  XYZ (count);..  
3e3e0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3e3f0 65 20 47 41 49 41 5f 58 59 5f 4d 3a 0a 09 20 20  e GAIA_XY_M:..  
3e400 67 65 6f 6d 20 3d 20 67 61 69 61 41 6c 6c 6f 63  geom = gaiaAlloc
3e410 47 65 6f 6d 43 6f 6c 6c 58 59 4d 20 28 29 3b 0a  GeomCollXYM ();.
3e420 09 20 20 6c 6e 20 3d 20 67 61 69 61 41 6c 6c 6f  .  ln = gaiaAllo
3e430 63 4c 69 6e 65 73 74 72 69 6e 67 58 59 4d 20 28  cLinestringXYM (
3e440 63 6f 75 6e 74 29 3b 0a 09 20 20 62 72 65 61 6b  count);..  break
3e450 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ;.      default:
3e460 0a 09 20 20 67 65 6f 6d 20 3d 20 67 61 69 61 41  ..  geom = gaiaA
3e470 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 20 28 29 3b  llocGeomColl ();
3e480 0a 09 20 20 6c 6e 20 3d 20 67 61 69 61 41 6c 6c  ..  ln = gaiaAll
3e490 6f 63 4c 69 6e 65 73 74 72 69 6e 67 20 28 63 6f  ocLinestring (co
3e4a0 75 6e 74 29 3b 0a 09 20 20 62 72 65 61 6b 3b 0a  unt);..  break;.
3e4b0 20 20 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69 66        };..    if
3e4c0 20 28 67 65 6f 6d 20 21 3d 20 4e 55 4c 4c 20 26   (geom != NULL &
3e4d0 26 20 6c 6e 20 21 3d 20 4e 55 4c 4c 29 0a 20 20  & ln != NULL).  
3e4e0 20 20 20 20 7b 0a 09 20 20 67 61 69 61 49 6e 73      {..  gaiaIns
3e4f0 65 72 74 4c 69 6e 65 73 74 72 69 6e 67 49 6e 47  ertLinestringInG
3e500 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d 2c 20 6c  eomColl (geom, l
3e510 6e 29 3b 0a 09 20 20 67 65 6f 6d 2d 3e 53 72 69  n);..  geom->Sri
3e520 64 20 3d 20 64 79 6e 2d 3e 53 72 69 64 3b 0a 20  d = dyn->Srid;. 
3e530 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
3e540 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20 28 67        {..  if (g
3e550 65 6f 6d 29 0a 09 20 20 20 20 20 20 67 61 69 61  eom)..      gaia
3e560 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
3e570 6f 6d 29 3b 0a 09 20 20 69 66 20 28 6c 6e 29 0a  om);..  if (ln).
3e580 09 20 20 20 20 20 20 67 61 69 61 46 72 65 65 4c  .      gaiaFreeL
3e590 69 6e 65 73 74 72 69 6e 67 20 28 6c 6e 29 3b 0a  inestring (ln);.
3e5a0 09 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a  .  return NULL;.
3e5b0 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 69 76 20        }..    iv 
3e5c0 3d 20 30 3b 0a 20 20 20 20 70 74 20 3d 20 64 79  = 0;.    pt = dy
3e5d0 6e 2d 3e 46 69 72 73 74 3b 0a 20 20 20 20 77 68  n->First;.    wh
3e5e0 69 6c 65 20 28 70 74 29 0a 20 20 20 20 20 20 7b  ile (pt).      {
3e5f0 0a 09 20 20 2f 2a 20 73 65 74 74 69 6e 67 20 6c  ..  /* setting l
3e600 69 6e 65 73 74 72 69 6e 67 20 70 6f 69 6e 74 73  inestring points
3e610 20 2a 2f 0a 09 20 20 69 66 20 28 64 69 6d 73 20   */..  if (dims 
3e620 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a  == GAIA_XY_Z_M).
3e630 09 20 20 20 20 7b 0a 09 09 67 61 69 61 53 65 74  .    {...gaiaSet
3e640 50 6f 69 6e 74 58 59 5a 4d 20 28 6c 6e 2d 3e 43  PointXYZM (ln->C
3e650 6f 6f 72 64 73 2c 20 69 76 2c 20 70 74 2d 3e 58  oords, iv, pt->X
3e660 2c 20 70 74 2d 3e 59 2c 20 70 74 2d 3e 5a 2c 20  , pt->Y, pt->Z, 
3e670 70 74 2d 3e 4d 29 3b 0a 09 20 20 20 20 7d 0a 09  pt->M);..    }..
3e680 20 20 65 6c 73 65 20 69 66 20 28 64 69 6d 73 20    else if (dims 
3e690 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09 20  == GAIA_XY_Z).. 
3e6a0 20 20 20 7b 0a 09 09 67 61 69 61 53 65 74 50 6f     {...gaiaSetPo
3e6b0 69 6e 74 58 59 5a 20 28 6c 6e 2d 3e 43 6f 6f 72  intXYZ (ln->Coor
3e6c0 64 73 2c 20 69 76 2c 20 70 74 2d 3e 58 2c 20 70  ds, iv, pt->X, p
3e6d0 74 2d 3e 59 2c 20 70 74 2d 3e 5a 29 3b 0a 09 20  t->Y, pt->Z);.. 
3e6e0 20 20 20 7d 0a 09 20 20 65 6c 73 65 20 69 66 20     }..  else if 
3e6f0 28 64 69 6d 73 20 3d 3d 20 47 41 49 41 5f 58 59  (dims == GAIA_XY
3e700 5f 4d 29 0a 09 20 20 20 20 7b 0a 09 09 67 61 69  _M)..    {...gai
3e710 61 53 65 74 50 6f 69 6e 74 58 59 4d 20 28 6c 6e  aSetPointXYM (ln
3e720 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 70 74  ->Coords, iv, pt
3e730 2d 3e 58 2c 20 70 74 2d 3e 59 2c 20 70 74 2d 3e  ->X, pt->Y, pt->
3e740 4d 29 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c  M);..    }..  el
3e750 73 65 0a 09 20 20 20 20 7b 0a 09 09 67 61 69 61  se..    {...gaia
3e760 53 65 74 50 6f 69 6e 74 20 28 6c 6e 2d 3e 43 6f  SetPoint (ln->Co
3e770 6f 72 64 73 2c 20 69 76 2c 20 70 74 2d 3e 58 2c  ords, iv, pt->X,
3e780 20 70 74 2d 3e 59 29 3b 0a 09 20 20 20 20 7d 0a   pt->Y);..    }.
3e790 09 20 20 69 76 2b 2b 3b 0a 09 20 20 70 74 20 3d  .  iv++;..  pt =
3e7a0 20 70 74 2d 3e 4e 65 78 74 3b 0a 20 20 20 20 20   pt->Next;.     
3e7b0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 67 65   }.    return ge
3e7c0 6f 6d 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  om;.}..static vo
3e7d0 69 64 0a 66 6e 63 74 5f 4d 61 6b 65 4c 69 6e 65  id.fnct_MakeLine
3e7e0 5f 66 69 6e 61 6c 20 28 73 71 6c 69 74 65 33 5f  _final (sqlite3_
3e7f0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
3e800 74 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  t).{./* SQL func
3e810 74 69 6f 6e 3a 0a 2f 20 4d 61 6b 65 4c 69 6e 65  tion:./ MakeLine
3e820 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f  (BLOBencoded geo
3e830 6d 29 0a 2f 0a 2f 20 61 67 67 72 65 67 61 74 65  m)././ aggregate
3e840 20 66 75 6e 63 74 69 6f 6e 20 2d 20 46 49 4e 41   function - FINA
3e850 4c 0a 2f 0a 2a 2f 0a 20 20 20 20 67 61 69 61 47  L./.*/.    gaiaG
3e860 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c  eomCollPtr resul
3e870 74 3b 0a 20 20 20 20 67 61 69 61 44 79 6e 61 6d  t;.    gaiaDynam
3e880 69 63 4c 69 6e 65 50 74 72 20 2a 70 20 3d 20 73  icLinePtr *p = s
3e890 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
3e8a0 5f 63 6f 6e 74 65 78 74 20 28 63 6f 6e 74 65 78  _context (contex
3e8b0 74 2c 20 30 29 3b 0a 20 20 20 20 69 66 20 28 21  t, 0);.    if (!
3e8c0 70 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  p).      {..  sq
3e8d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
3e8e0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
3e8f0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
3e900 20 20 20 20 72 65 73 75 6c 74 20 3d 20 67 65 6f      result = geo
3e910 6d 46 72 6f 6d 44 79 6e 61 6d 69 63 4c 69 6e 65  mFromDynamicLine
3e920 20 28 2a 70 29 3b 0a 20 20 20 20 67 61 69 61 46   (*p);.    gaiaF
3e930 72 65 65 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28  reeDynamicLine (
3e940 2a 70 29 3b 0a 20 20 20 20 69 66 20 28 21 72 65  *p);.    if (!re
3e950 73 75 6c 74 29 0a 09 73 71 6c 69 74 65 33 5f 72  sult)..sqlite3_r
3e960 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
3e970 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20  ext);.    else. 
3e980 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 62 75 69       {..  /* bui
3e990 6c 64 73 20 74 68 65 20 42 4c 4f 42 20 67 65 6f  lds the BLOB geo
3e9a0 6d 65 74 72 79 20 74 6f 20 62 65 20 72 65 74 75  metry to be retu
3e9b0 72 6e 65 64 20 2a 2f 0a 09 20 20 69 6e 74 20 6c  rned */..  int l
3e9c0 65 6e 3b 0a 09 20 20 75 6e 73 69 67 6e 65 64 20  en;..  unsigned 
3e9d0 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
3e9e0 20 4e 55 4c 4c 3b 0a 09 20 20 67 61 69 61 54 6f   NULL;..  gaiaTo
3e9f0 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
3ea00 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65  b (result, &p_re
3ea10 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 20 20  sult, &len);..  
3ea20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
3ea30 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
3ea40 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
3ea50 65 29 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47  e);..  gaiaFreeG
3ea60 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29  eomColl (result)
3ea70 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a 0a 73 74 61  ;.      }.}..sta
3ea80 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 61  tic void.fnct_Ma
3ea90 6b 65 4c 69 6e 65 20 28 73 71 6c 69 74 65 33 5f  keLine (sqlite3_
3eaa0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
3eab0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
3eac0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
3ead0 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
3eae0 63 74 69 6f 6e 3a 0a 2f 20 4d 61 6b 65 4c 69 6e  ction:./ MakeLin
3eaf0 65 28 70 6f 69 6e 74 2d 67 65 6f 6d 65 74 72 79  e(point-geometry
3eb00 20 67 65 6f 6d 31 2c 20 70 6f 69 6e 74 2d 67 65   geom1, point-ge
3eb10 6f 6d 65 74 72 79 20 67 65 6f 6d 32 29 0a 2f 0a  ometry geom2)./.
3eb20 2f 20 62 75 69 6c 64 73 20 61 20 53 45 47 4d 45  / builds a SEGME
3eb30 4e 54 20 6a 6f 69 6e 69 6e 67 20 74 77 6f 20 50  NT joining two P
3eb40 4f 49 4e 54 73 20 0a 2f 20 6f 72 20 4e 55 4c 4c  OINTs ./ or NULL
3eb50 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
3eb60 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
3eb70 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
3eb80 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
3eb90 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20  p_blob;.    int 
3eba0 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 75 6e 73  n_bytes;.    uns
3ebb0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
3ebc0 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  sult = NULL;.   
3ebd0 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
3ebe0 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20   geo1 = NULL;.  
3ebf0 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
3ec00 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20  r geo2 = NULL;. 
3ec10 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
3ec20 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
3ec30 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
3ec40 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
3ec50 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
3ec60 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
3ec70 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
3ec80 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
3ec90 6e 74 65 78 74 29 3b 0a 09 20 20 67 6f 74 6f 20  ntext);..  goto 
3eca0 73 74 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20  stop;.      }.  
3ecb0 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
3ecc0 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
3ecd0 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
3ece0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
3ecf0 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
3ed00 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
3ed10 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31  gv[0]);.    geo1
3ed20 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69   = gaiaFromSpati
3ed30 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f  aLiteBlobWkb (p_
3ed40 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a  blob, n_bytes);.
3ed50 20 20 20 20 69 66 20 28 21 67 65 6f 31 29 0a 20      if (!geo1). 
3ed60 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
3ed70 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
3ed80 6f 6e 74 65 78 74 29 3b 0a 09 20 20 67 6f 74 6f  ontext);..  goto
3ed90 20 73 74 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20   stop;.      }. 
3eda0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
3edb0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
3edc0 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  1]) != SQLITE_BL
3edd0 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
3ede0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3edf0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
3ee00 20 67 6f 74 6f 20 73 74 6f 70 3b 0a 20 20 20 20   goto stop;.    
3ee10 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
3ee20 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
3ee30 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
3ee40 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b  _blob (argv[1]);
3ee50 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
3ee60 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3ee70 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  es (argv[1]);.  
3ee80 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f    geo2 = gaiaFro
3ee90 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
3eea0 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
3eeb0 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
3eec0 65 6f 32 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  eo2).      {..  
3eed0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
3eee0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
3eef0 20 20 67 6f 74 6f 20 73 74 6f 70 3b 0a 20 20 20    goto stop;.   
3ef00 20 20 20 7d 0a 20 20 20 20 67 61 69 61 4d 61 6b     }.    gaiaMak
3ef10 65 4c 69 6e 65 20 28 67 65 6f 31 2c 20 67 65 6f  eLine (geo1, geo
3ef20 32 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c  2, &p_result, &l
3ef30 65 6e 29 3b 0a 20 20 20 20 69 66 20 28 21 70 5f  en);.    if (!p_
3ef40 72 65 73 75 6c 74 29 0a 09 73 71 6c 69 74 65 33  result)..sqlite3
3ef50 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
3ef60 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
3ef70 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
3ef80 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20  _blob (context, 
3ef90 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66  p_result, len, f
3efa0 72 65 65 29 3b 0a 20 20 73 74 6f 70 3a 0a 20 20  ree);.  stop:.  
3efb0 20 20 69 66 20 28 67 65 6f 31 29 0a 09 67 61 69    if (geo1)..gai
3efc0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
3efd0 65 6f 31 29 3b 0a 20 20 20 20 69 66 20 28 67 65  eo1);.    if (ge
3efe0 6f 32 29 0a 09 67 61 69 61 46 72 65 65 47 65 6f  o2)..gaiaFreeGeo
3eff0 6d 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a  mColl (geo2);.}.
3f000 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
3f010 74 5f 43 6f 6c 6c 65 63 74 5f 73 74 65 70 20 28  t_Collect_step (
3f020 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3f030 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
3f040 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
3f050 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
3f060 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
3f070 20 43 6f 6c 6c 65 63 74 28 42 4c 4f 42 65 6e 63   Collect(BLOBenc
3f080 6f 64 65 64 20 67 65 6f 6d 29 0a 2f 0a 2f 20 61  oded geom)././ a
3f090 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
3f0a0 6e 20 2d 20 53 54 45 50 0a 2f 0a 2a 2f 0a 20 20  n - STEP./.*/.  
3f0b0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
3f0c0 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
3f0d0 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61   n_bytes;.    ga
3f0e0 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
3f0f0 6f 6d 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  om;.    gaiaGeom
3f100 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a  CollPtr result;.
3f110 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
3f120 50 74 72 20 2a 70 3b 0a 20 20 20 20 47 41 49 41  Ptr *p;.    GAIA
3f130 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
3f140 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
3f150 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
3f160 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
3f170 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
3f180 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
3f190 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3f1a0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
3f1b0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
3f1c0 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
3f1d0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
3f1e0 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
3f1f0 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
3f200 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
3f210 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3f220 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
3f230 20 20 67 65 6f 6d 20 3d 20 67 61 69 61 46 72 6f    geom = gaiaFro
3f240 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
3f250 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
3f260 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
3f270 65 6f 6d 29 0a 09 72 65 74 75 72 6e 3b 0a 20 20  eom)..return;.  
3f280 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67    p = sqlite3_ag
3f290 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20  gregate_context 
3f2a0 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66  (context, sizeof
3f2b0 20 28 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74   (gaiaGeomCollPt
3f2c0 72 29 29 3b 0a 20 20 20 20 69 66 20 28 21 28 2a  r));.    if (!(*
3f2d0 70 29 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f  p)).      {..  /
3f2e0 2a 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  * this is the fi
3f2f0 72 73 74 20 72 6f 77 20 2a 2f 0a 09 20 20 2a 70  rst row */..  *p
3f300 20 3d 20 67 65 6f 6d 3b 0a 20 20 20 20 20 20 7d   = geom;.      }
3f310 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
3f320 7b 0a 09 20 20 2f 2a 20 73 75 62 73 65 71 75 65  {..  /* subseque
3f330 6e 74 20 72 6f 77 73 20 2a 2f 0a 09 20 20 72 65  nt rows */..  re
3f340 73 75 6c 74 20 3d 20 67 61 69 61 4d 65 72 67 65  sult = gaiaMerge
3f350 47 65 6f 6d 65 74 72 69 65 73 20 28 2a 70 2c 20  Geometries (*p, 
3f360 67 65 6f 6d 29 3b 0a 09 20 20 67 61 69 61 46 72  geom);..  gaiaFr
3f370 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 2a 70 29 3b  eeGeomColl (*p);
3f380 0a 09 20 20 2a 70 20 3d 20 72 65 73 75 6c 74 3b  ..  *p = result;
3f390 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d  ..  gaiaFreeGeom
3f3a0 43 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a 20 20 20  Coll (geom);.   
3f3b0 20 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76     }.}..static v
3f3c0 6f 69 64 0a 66 6e 63 74 5f 43 6f 6c 6c 65 63 74  oid.fnct_Collect
3f3d0 5f 66 69 6e 61 6c 20 28 73 71 6c 69 74 65 33 5f  _final (sqlite3_
3f3e0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
3f3f0 74 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  t).{./* SQL func
3f400 74 69 6f 6e 3a 0a 2f 20 43 6f 6c 6c 65 63 74 28  tion:./ Collect(
3f410 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d  BLOBencoded geom
3f420 29 0a 2f 0a 2f 20 61 67 67 72 65 67 61 74 65 20  )././ aggregate 
3f430 66 75 6e 63 74 69 6f 6e 20 2d 20 46 49 4e 41 4c  function - FINAL
3f440 0a 2f 0a 2a 2f 0a 20 20 20 20 67 61 69 61 47 65  ./.*/.    gaiaGe
3f450 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74  omCollPtr result
3f460 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
3f470 6c 6c 50 74 72 20 2a 70 20 3d 20 73 71 6c 69 74  llPtr *p = sqlit
3f480 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
3f490 74 65 78 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  text (context, 0
3f4a0 29 3b 0a 20 20 20 20 69 66 20 28 21 70 29 0a 20  );.    if (!p). 
3f4b0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
3f4c0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
3f4d0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
3f4e0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
3f4f0 72 65 73 75 6c 74 20 3d 20 2a 70 3b 0a 20 20 20  result = *p;.   
3f500 20 69 66 20 28 21 72 65 73 75 6c 74 29 0a 09 73   if (!result)..s
3f510 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3f520 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ll (context);.  
3f530 20 20 65 6c 73 65 20 69 66 20 28 67 61 69 61 49    else if (gaiaI
3f540 73 45 6d 70 74 79 20 28 72 65 73 75 6c 74 29 29  sEmpty (result))
3f550 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 61 69 61  .      {..  gaia
3f560 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65  FreeGeomColl (re
3f570 73 75 6c 74 29 3b 0a 09 20 20 73 71 6c 69 74 65  sult);..  sqlite
3f580 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
3f590 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  ontext);.      }
3f5a0 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
3f5b0 7b 0a 09 20 20 2f 2a 20 62 75 69 6c 64 73 20 74  {..  /* builds t
3f5c0 68 65 20 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79  he BLOB geometry
3f5d0 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
3f5e0 2a 2f 0a 09 20 20 69 6e 74 20 6c 65 6e 3b 0a 09  */..  int len;..
3f5f0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
3f600 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  *p_result = NULL
3f610 3b 0a 09 20 20 67 61 69 61 54 6f 53 70 61 74 69  ;..  gaiaToSpati
3f620 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72 65  aLiteBlobWkb (re
3f630 73 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c  sult, &p_result,
3f640 20 26 6c 65 6e 29 3b 0a 09 20 20 73 71 6c 69 74   &len);..  sqlit
3f650 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28  e3_result_blob (
3f660 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
3f670 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09  t, len, free);..
3f680 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
3f690 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 20 20 20  ll (result);.   
3f6a0 20 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76     }.}..static v
3f6b0 6f 69 64 0a 66 6e 63 74 5f 43 6f 6c 6c 65 63 74  oid.fnct_Collect
3f6c0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
3f6d0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
3f6e0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
3f6f0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
3f700 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
3f710 0a 2f 20 43 6f 6c 6c 65 63 74 28 67 65 6f 6d 65  ./ Collect(geome
3f720 74 72 79 20 67 65 6f 6d 31 2c 20 67 65 6f 6d 65  try geom1, geome
3f730 74 72 79 20 67 65 6f 6d 32 29 0a 2f 0a 2f 20 6d  try geom2)././ m
3f740 65 72 67 65 73 20 74 77 6f 20 67 65 6e 65 72 69  erges two generi
3f750 63 20 47 45 4f 4d 45 54 52 49 45 53 20 69 6e 74  c GEOMETRIES int
3f760 6f 20 61 20 73 69 6e 67 6c 65 20 6f 6e 65 20 0a  o a single one .
3f770 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79  / or NULL if any
3f780 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
3f790 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73  tered.*/.    uns
3f7a0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
3f7b0 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  ob;.    int n_by
3f7c0 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  tes;.    gaiaGeo
3f7d0 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 31 20 3d 20  mCollPtr geo1 = 
3f7e0 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65  NULL;.    gaiaGe
3f7f0 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 32 20 3d  omCollPtr geo2 =
3f800 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47   NULL;.    gaiaG
3f810 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c  eomCollPtr resul
3f820 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  t;.    GAIA_UNUS
3f830 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
3f840 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
3f850 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
3f860 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d  ue_type (argv[0]
3f870 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  ) != SQLITE_BLOB
3f880 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
3f890 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
3f8a0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
3f8b0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
3f8c0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
3f8d0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
3f8e0 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  1]) != SQLITE_BL
3f8f0 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
3f900 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3f910 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
3f920 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
3f930 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
3f940 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
3f950 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
3f960 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
3f970 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
3f980 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
3f990 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
3f9a0 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  eo1 = gaiaFromSp
3f9b0 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
3f9c0 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
3f9d0 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20  );.    p_blob = 
3f9e0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
3f9f0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
3fa00 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a  blob (argv[1]);.
3fa10 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
3fa20 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
3fa30 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  s (argv[1]);.   
3fa40 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d   geo2 = gaiaFrom
3fa50 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
3fa60 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
3fa70 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65  es);.    if (!ge
3fa80 6f 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09 73 71  o1 || !geo2)..sq
3fa90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
3faa0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
3fab0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
3fac0 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 4d 65   result = gaiaMe
3fad0 72 67 65 47 65 6f 6d 65 74 72 69 65 73 20 28 67  rgeGeometries (g
3fae0 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20 20 69  eo1, geo2);..  i
3faf0 66 20 28 21 72 65 73 75 6c 74 29 0a 09 20 20 20  f (!result)..   
3fb00 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3fb10 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
3fb20 3b 0a 09 20 20 65 6c 73 65 20 69 66 20 28 67 61  ;..  else if (ga
3fb30 69 61 49 73 45 6d 70 74 79 20 28 72 65 73 75 6c  iaIsEmpty (resul
3fb40 74 29 29 0a 09 20 20 20 20 7b 0a 09 09 67 61 69  t))..    {...gai
3fb50 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72  aFreeGeomColl (r
3fb60 65 73 75 6c 74 29 3b 0a 09 09 73 71 6c 69 74 65  esult);...sqlite
3fb70 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
3fb80 6f 6e 74 65 78 74 29 3b 0a 09 20 20 20 20 7d 0a  ontext);..    }.
3fb90 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09  .  else..    {..
3fba0 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20 42  ./* builds the B
3fbb0 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f 20  LOB geometry to 
3fbc0 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 09  be returned */..
3fbd0 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73 69  .int len;...unsi
3fbe0 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
3fbf0 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 67 61  ult = NULL;...ga
3fc00 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c  iaToSpatiaLiteBl
3fc10 6f 62 57 6b 62 20 28 72 65 73 75 6c 74 2c 20 26  obWkb (result, &
3fc20 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b  p_result, &len);
3fc30 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ...sqlite3_resul
3fc40 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
3fc50 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
3fc60 66 72 65 65 29 3b 0a 09 09 67 61 69 61 46 72 65  free);...gaiaFre
3fc70 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c  eGeomColl (resul
3fc80 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20  t);..    }.     
3fc90 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47   }.    gaiaFreeG
3fca0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a  eomColl (geo1);.
3fcb0 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
3fcc0 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a  Coll (geo2);.}..
3fcd0 73 74 61 74 69 63 20 76 6f 69 64 0a 67 65 6f 6d  static void.geom
3fce0 5f 66 72 6f 6d 5f 74 65 78 74 31 20 28 73 71 6c  _from_text1 (sql
3fcf0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
3fd00 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
3fd10 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
3fd20 2a 2a 20 61 72 67 76 2c 0a 09 09 20 73 68 6f 72  ** argv,... shor
3fd30 74 20 74 79 70 65 29 0a 7b 0a 2f 2a 20 53 51 4c  t type).{./* SQL
3fd40 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 47 65 6f   function:./ Geo
3fd50 6d 46 72 6f 6d 54 65 78 74 28 57 4b 54 20 65 6e  mFromText(WKT en
3fd60 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a  coded geometry).
3fd70 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20  /./ returns the 
3fd80 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79  current geometry
3fd90 20 62 79 20 70 61 72 73 69 6e 67 20 57 4b 54 20   by parsing WKT 
3fda0 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20 0a  encoded string .
3fdb0 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79  / or NULL if any
3fdc0 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
3fdd0 74 65 72 65 64 0a 2f 0a 2f 20 69 66 20 2a 74 79  tered././ if *ty
3fde0 70 65 2a 20 69 73 20 61 20 6e 65 67 61 74 69 76  pe* is a negativ
3fdf0 65 20 76 61 6c 75 65 20 63 61 6e 20 61 63 63 65  e value can acce
3fe00 70 74 20 61 6e 79 20 47 45 4f 4d 45 54 52 59 20  pt any GEOMETRY 
3fe10 43 4c 41 53 53 0a 2f 20 6f 74 68 65 72 77 69 73  CLASS./ otherwis
3fe20 65 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 73 20  e only requests 
3fe30 63 6f 6e 66 6f 72 6d 69 6e 67 20 77 69 74 68 20  conforming with 
3fe40 72 65 71 75 69 72 65 64 20 43 4c 41 53 53 20 61  required CLASS a
3fe50 72 65 20 76 61 6c 69 64 0a 2a 2f 0a 20 20 20 20  re valid.*/.    
3fe60 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73  int len;.    uns
3fe70 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
3fe80 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  sult = NULL;.   
3fe90 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
3fea0 63 68 61 72 20 2a 74 65 78 74 3b 0a 20 20 20 20  char *text;.    
3feb0 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
3fec0 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
3fed0 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
3fee0 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
3fef0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
3ff00 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
3ff10 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
3ff20 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20  LITE_TEXT).     
3ff30 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
3ff40 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
3ff50 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
3ff60 20 20 20 20 20 20 7d 0a 20 20 20 20 74 65 78 74        }.    text
3ff70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3ff80 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d 29 3b  _text (argv[0]);
3ff90 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 50  .    geo = gaiaP
3ffa0 61 72 73 65 57 6b 74 20 28 74 65 78 74 2c 20 74  arseWkt (text, t
3ffb0 79 70 65 29 3b 0a 20 20 20 20 69 66 20 28 67 65  ype);.    if (ge
3ffc0 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20  o == NULL).     
3ffd0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
3ffe0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
3fff0 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
40000 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61        }.    gaia
40010 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
40020 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65 73  Wkb (geo, &p_res
40030 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20  ult, &len);.    
40040 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
40050 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71 6c 69   (geo);.    sqli
40060 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
40070 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
40080 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
40090 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 67  }..static void.g
400a0 65 6f 6d 5f 66 72 6f 6d 5f 74 65 78 74 32 20 28  eom_from_text2 (
400b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
400c0 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
400d0 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
400e0 75 65 20 2a 2a 20 61 72 67 76 2c 0a 09 09 20 73  ue ** argv,... s
400f0 68 6f 72 74 20 74 79 70 65 29 0a 7b 0a 2f 2a 20  hort type).{./* 
40100 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
40110 47 65 6f 6d 46 72 6f 6d 54 65 78 74 28 57 4b 54  GeomFromText(WKT
40120 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72   encoded geometr
40130 79 2c 20 53 52 49 44 29 0a 2f 0a 2f 20 72 65 74  y, SRID)././ ret
40140 75 72 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74  urns the current
40150 20 67 65 6f 6d 65 74 72 79 20 62 79 20 70 61 72   geometry by par
40160 73 69 6e 67 20 57 4b 54 20 65 6e 63 6f 64 65 64  sing WKT encoded
40170 20 73 74 72 69 6e 67 20 0a 2f 20 6f 72 20 4e 55   string ./ or NU
40180 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
40190 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2f  is encountered./
401a0 0a 2f 20 69 66 20 2a 74 79 70 65 2a 20 69 73 20  ./ if *type* is 
401b0 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65  a negative value
401c0 20 63 61 6e 20 61 63 63 65 70 74 20 61 6e 79 20   can accept any 
401d0 47 45 4f 4d 45 54 52 59 20 43 4c 41 53 53 0a 2f  GEOMETRY CLASS./
401e0 20 6f 74 68 65 72 77 69 73 65 20 6f 6e 6c 79 20   otherwise only 
401f0 72 65 71 75 65 73 74 73 20 63 6f 6e 66 6f 72 6d  requests conform
40200 69 6e 67 20 77 69 74 68 20 72 65 71 75 69 72 65  ing with require
40210 64 20 43 4c 41 53 53 20 61 72 65 20 76 61 6c 69  d CLASS are vali
40220 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e  d.*/.    int len
40230 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
40240 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20  har *p_result = 
40250 4e 55 4c 4c 3b 0a 20 20 20 20 63 6f 6e 73 74 20  NULL;.    const 
40260 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74  unsigned char *t
40270 65 78 74 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ext;.    gaiaGeo
40280 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e  mCollPtr geo = N
40290 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ULL;.    GAIA_UN
402a0 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
402b0 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
402c0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
402d0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
402e0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 54 45  0]) != SQLITE_TE
402f0 58 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  XT).      {..  s
40300 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
40310 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
40320 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
40330 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
40340 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
40350 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[1]) != SQLITE_
40360 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b  INTEGER).      {
40370 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
40380 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
40390 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
403a0 20 20 20 20 7d 0a 20 20 20 20 74 65 78 74 20 3d      }.    text =
403b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
403c0 65 78 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  ext (argv[0]);. 
403d0 20 20 20 67 65 6f 20 3d 20 67 61 69 61 50 61 72     geo = gaiaPar
403e0 73 65 57 6b 74 20 28 74 65 78 74 2c 20 74 79 70  seWkt (text, typ
403f0 65 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 20  e);.    if (geo 
40400 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b  == NULL).      {
40410 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
40420 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
40430 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
40440 20 20 20 20 7d 0a 20 20 20 20 67 65 6f 2d 3e 53      }.    geo->S
40450 72 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  rid = sqlite3_va
40460 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d  lue_int (argv[1]
40470 29 3b 0a 20 20 20 20 67 61 69 61 54 6f 53 70 61  );.    gaiaToSpa
40480 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
40490 67 65 6f 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  geo, &p_result, 
404a0 26 6c 65 6e 29 3b 0a 20 20 20 20 67 61 69 61 46  &len);.    gaiaF
404b0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
404c0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
404d0 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74  esult_blob (cont
404e0 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c  ext, p_result, l
404f0 65 6e 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 73 74  en, free);.}..st
40500 61 74 69 63 20 69 6e 74 0a 63 68 65 63 6b 5f 77  atic int.check_w
40510 6b 62 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  kb (const unsign
40520 65 64 20 63 68 61 72 20 2a 77 6b 62 2c 20 69 6e  ed char *wkb, in
40530 74 20 73 69 7a 65 2c 20 73 68 6f 72 74 20 74 79  t size, short ty
40540 70 65 29 0a 7b 0a 2f 2a 20 63 68 65 63 6b 69 6e  pe).{./* checkin
40550 67 20 74 79 70 65 20 63 6f 68 65 72 65 6e 63 79  g type coherency
40560 20 66 6f 72 20 57 4b 42 20 65 6e 63 6f 64 65 64   for WKB encoded
40570 20 47 45 4f 4d 45 54 52 59 20 2a 2f 0a 20 20 20   GEOMETRY */.   
40580 20 69 6e 74 20 6c 69 74 74 6c 65 5f 65 6e 64 69   int little_endi
40590 61 6e 3b 0a 20 20 20 20 69 6e 74 20 77 6b 62 5f  an;.    int wkb_
405a0 74 79 70 65 3b 0a 20 20 20 20 69 6e 74 20 65 6e  type;.    int en
405b0 64 69 61 6e 5f 61 72 63 68 20 3d 20 67 61 69 61  dian_arch = gaia
405c0 45 6e 64 69 61 6e 41 72 63 68 20 28 29 3b 0a 20  EndianArch ();. 
405d0 20 20 20 69 66 20 28 73 69 7a 65 20 3c 20 35 29     if (size < 5)
405e0 0a 09 72 65 74 75 72 6e 20 30 3b 09 09 2f 2a 20  ..return 0;../* 
405f0 74 6f 6f 20 73 68 6f 72 74 20 74 6f 20 62 65 20  too short to be 
40600 61 20 57 4b 42 20 2a 2f 0a 20 20 20 20 69 66 20  a WKB */.    if 
40610 28 2a 28 77 6b 62 20 2b 20 30 29 20 3d 3d 20 30  (*(wkb + 0) == 0
40620 78 30 31 29 0a 09 6c 69 74 74 6c 65 5f 65 6e 64  x01)..little_end
40630 69 61 6e 20 3d 20 47 41 49 41 5f 4c 49 54 54 4c  ian = GAIA_LITTL
40640 45 5f 45 4e 44 49 41 4e 3b 0a 20 20 20 20 65 6c  E_ENDIAN;.    el
40650 73 65 20 69 66 20 28 2a 28 77 6b 62 20 2b 20 30  se if (*(wkb + 0
40660 29 20 3d 3d 20 30 78 30 30 29 0a 09 6c 69 74 74  ) == 0x00)..litt
40670 6c 65 5f 65 6e 64 69 61 6e 20 3d 20 47 41 49 41  le_endian = GAIA
40680 5f 42 49 47 5f 45 4e 44 49 41 4e 3b 0a 20 20 20  _BIG_ENDIAN;.   
40690 20 65 6c 73 65 0a 09 72 65 74 75 72 6e 20 30 3b   else..return 0;
406a0 09 09 2f 2a 20 69 6c 6c 65 67 61 6c 20 62 79 74  ../* illegal byt
406b0 65 20 6f 72 64 65 72 69 6e 67 3b 20 6e 65 69 74  e ordering; neit
406c0 68 65 72 20 42 49 47 2d 45 4e 44 49 41 4e 20 6e  her BIG-ENDIAN n
406d0 6f 72 20 4c 49 54 54 4c 45 2d 45 4e 44 49 41 4e  or LITTLE-ENDIAN
406e0 20 2a 2f 0a 20 20 20 20 77 6b 62 5f 74 79 70 65   */.    wkb_type
406f0 20 3d 20 67 61 69 61 49 6d 70 6f 72 74 33 32 20   = gaiaImport32 
40700 28 77 6b 62 20 2b 20 31 2c 20 6c 69 74 74 6c 65  (wkb + 1, little
40710 5f 65 6e 64 69 61 6e 2c 20 65 6e 64 69 61 6e 5f  _endian, endian_
40720 61 72 63 68 29 3b 0a 20 20 20 20 69 66 20 28 77  arch);.    if (w
40730 6b 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f  kb_type == GAIA_
40740 50 4f 49 4e 54 20 7c 7c 20 77 6b 62 5f 74 79 70  POINT || wkb_typ
40750 65 20 3d 3d 20 47 41 49 41 5f 4c 49 4e 45 53 54  e == GAIA_LINEST
40760 52 49 4e 47 0a 09 7c 7c 20 77 6b 62 5f 74 79 70  RING..|| wkb_typ
40770 65 20 3d 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f  e == GAIA_POLYGO
40780 4e 20 7c 7c 20 77 6b 62 5f 74 79 70 65 20 3d 3d  N || wkb_type ==
40790 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54   GAIA_MULTIPOINT
407a0 0a 09 7c 7c 20 77 6b 62 5f 74 79 70 65 20 3d 3d  ..|| wkb_type ==
407b0 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45 53   GAIA_MULTILINES
407c0 54 52 49 4e 47 20 7c 7c 20 77 6b 62 5f 74 79 70  TRING || wkb_typ
407d0 65 20 3d 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50  e == GAIA_MULTIP
407e0 4f 4c 59 47 4f 4e 0a 09 7c 7c 20 77 6b 62 5f 74  OLYGON..|| wkb_t
407f0 79 70 65 20 3d 3d 20 47 41 49 41 5f 47 45 4f 4d  ype == GAIA_GEOM
40800 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 20 7c  ETRYCOLLECTION |
40810 7c 20 77 6b 62 5f 74 79 70 65 20 3d 3d 20 47 41  | wkb_type == GA
40820 49 41 5f 50 4f 49 4e 54 5a 0a 09 7c 7c 20 77 6b  IA_POINTZ..|| wk
40830 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 4c  b_type == GAIA_L
40840 49 4e 45 53 54 52 49 4e 47 5a 20 7c 7c 20 77 6b  INESTRINGZ || wk
40850 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 50  b_type == GAIA_P
40860 4f 4c 59 47 4f 4e 5a 0a 09 7c 7c 20 77 6b 62 5f  OLYGONZ..|| wkb_
40870 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 4d 55 4c  type == GAIA_MUL
40880 54 49 50 4f 49 4e 54 5a 20 7c 7c 20 77 6b 62 5f  TIPOINTZ || wkb_
40890 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 4d 55 4c  type == GAIA_MUL
408a0 54 49 4c 49 4e 45 53 54 52 49 4e 47 5a 0a 09 7c  TILINESTRINGZ..|
408b0 7c 20 77 6b 62 5f 74 79 70 65 20 3d 3d 20 47 41  | wkb_type == GA
408c0 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 5a  IA_MULTIPOLYGONZ
408d0 0a 09 7c 7c 20 77 6b 62 5f 74 79 70 65 20 3d 3d  ..|| wkb_type ==
408e0 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f   GAIA_GEOMETRYCO
408f0 4c 4c 45 43 54 49 4f 4e 5a 20 7c 7c 20 77 6b 62  LLECTIONZ || wkb
40900 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 50 4f  _type == GAIA_PO
40910 49 4e 54 4d 0a 09 7c 7c 20 77 6b 62 5f 74 79 70  INTM..|| wkb_typ
40920 65 20 3d 3d 20 47 41 49 41 5f 4c 49 4e 45 53 54  e == GAIA_LINEST
40930 52 49 4e 47 4d 20 7c 7c 20 77 6b 62 5f 74 79 70  RINGM || wkb_typ
40940 65 20 3d 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f  e == GAIA_POLYGO
40950 4e 4d 0a 09 7c 7c 20 77 6b 62 5f 74 79 70 65 20  NM..|| wkb_type 
40960 3d 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49  == GAIA_MULTIPOI
40970 4e 54 4d 20 7c 7c 20 77 6b 62 5f 74 79 70 65 20  NTM || wkb_type 
40980 3d 3d 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e  == GAIA_MULTILIN
40990 45 53 54 52 49 4e 47 4d 0a 09 7c 7c 20 77 6b 62  ESTRINGM..|| wkb
409a0 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 4d 55  _type == GAIA_MU
409b0 4c 54 49 50 4f 4c 59 47 4f 4e 4d 0a 09 7c 7c 20  LTIPOLYGONM..|| 
409c0 77 6b 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41  wkb_type == GAIA
409d0 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54  _GEOMETRYCOLLECT
409e0 49 4f 4e 4d 20 7c 7c 20 77 6b 62 5f 74 79 70 65  IONM || wkb_type
409f0 20 3d 3d 20 47 41 49 41 5f 50 4f 49 4e 54 5a 4d   == GAIA_POINTZM
40a00 0a 09 7c 7c 20 77 6b 62 5f 74 79 70 65 20 3d 3d  ..|| wkb_type ==
40a10 20 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47   GAIA_LINESTRING
40a20 5a 4d 20 7c 7c 20 77 6b 62 5f 74 79 70 65 20 3d  ZM || wkb_type =
40a30 3d 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 5a 4d  = GAIA_POLYGONZM
40a40 0a 09 7c 7c 20 77 6b 62 5f 74 79 70 65 20 3d 3d  ..|| wkb_type ==
40a50 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54   GAIA_MULTIPOINT
40a60 5a 4d 20 7c 7c 20 77 6b 62 5f 74 79 70 65 20 3d  ZM || wkb_type =
40a70 3d 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45  = GAIA_MULTILINE
40a80 53 54 52 49 4e 47 5a 4d 0a 09 7c 7c 20 77 6b 62  STRINGZM..|| wkb
40a90 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 4d 55  _type == GAIA_MU
40aa0 4c 54 49 50 4f 4c 59 47 4f 4e 5a 4d 0a 09 7c 7c  LTIPOLYGONZM..||
40ab0 20 77 6b 62 5f 74 79 70 65 20 3d 3d 20 47 41 49   wkb_type == GAI
40ac0 41 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43  A_GEOMETRYCOLLEC
40ad0 54 49 4f 4e 5a 4d 29 0a 09 3b 0a 20 20 20 20 65  TIONZM)..;.    e
40ae0 6c 73 65 0a 09 72 65 74 75 72 6e 20 30 3b 09 09  lse..return 0;..
40af0 2f 2a 20 69 6c 6c 65 67 61 6c 20 47 45 4f 4d 45  /* illegal GEOME
40b00 54 52 59 20 43 4c 41 53 53 20 2a 2f 0a 20 20 20  TRY CLASS */.   
40b10 20 69 66 20 28 74 79 70 65 20 3c 20 30 29 0a 09   if (type < 0)..
40b20 3b 09 09 09 2f 2a 20 6e 6f 20 72 65 73 74 72 69  ;.../* no restri
40b30 6e 63 74 69 6f 6e 20 61 62 6f 75 74 20 47 45 4f  nction about GEO
40b40 4d 45 54 52 59 20 43 4c 41 53 53 20 54 59 50 45  METRY CLASS TYPE
40b50 20 2a 2f 0a 20 20 20 20 65 6c 73 65 0a 20 20 20   */.    else.   
40b60 20 20 20 7b 0a 09 20 20 69 66 20 28 77 6b 62 5f     {..  if (wkb_
40b70 74 79 70 65 20 21 3d 20 74 79 70 65 29 0a 09 20  type != type).. 
40b80 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 09 09       return 0;..
40b90 2f 2a 20 69 6e 76 61 6c 69 64 20 43 4c 41 53 53  /* invalid CLASS
40ba0 20 54 59 50 45 20 66 6f 72 20 72 65 71 75 65 73   TYPE for reques
40bb0 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  t */.      }.   
40bc0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74   return 1;.}..st
40bd0 61 74 69 63 20 76 6f 69 64 0a 67 65 6f 6d 5f 66  atic void.geom_f
40be0 72 6f 6d 5f 77 6b 62 31 20 28 73 71 6c 69 74 65  rom_wkb1 (sqlite
40bf0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
40c00 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
40c10 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
40c20 61 72 67 76 2c 0a 09 09 73 68 6f 72 74 20 74 79  argv,...short ty
40c30 70 65 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  pe).{./* SQL fun
40c40 63 74 69 6f 6e 3a 0a 2f 20 47 65 6f 6d 46 72 6f  ction:./ GeomFro
40c50 6d 57 4b 42 28 57 4b 42 20 65 6e 63 6f 64 65 64  mWKB(WKB encoded
40c60 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
40c70 65 74 75 72 6e 73 20 74 68 65 20 63 75 72 72 65  eturns the curre
40c80 6e 74 20 67 65 6f 6d 65 74 72 79 20 62 79 20 70  nt geometry by p
40c90 61 72 73 69 6e 67 20 61 20 57 4b 42 20 65 6e 63  arsing a WKB enc
40ca0 6f 64 65 64 20 62 6c 6f 62 20 0a 2f 20 6f 72 20  oded blob ./ or 
40cb0 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f  NULL if any erro
40cc0 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
40cd0 0a 2f 0a 2f 20 69 66 20 2a 74 79 70 65 2a 20 69  ././ if *type* i
40ce0 73 20 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c  s a negative val
40cf0 75 65 20 63 61 6e 20 61 63 63 65 70 74 20 61 6e  ue can accept an
40d00 79 20 47 45 4f 4d 45 54 52 59 20 43 4c 41 53 53  y GEOMETRY CLASS
40d10 0a 2f 20 6f 74 68 65 72 77 69 73 65 20 6f 6e 6c  ./ otherwise onl
40d20 79 20 72 65 71 75 65 73 74 73 20 63 6f 6e 66 6f  y requests confo
40d30 72 6d 69 6e 67 20 77 69 74 68 20 72 65 71 75 69  rming with requi
40d40 72 65 64 20 43 4c 41 53 53 20 61 72 65 20 76 61  red CLASS are va
40d50 6c 69 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  lid.*/.    int l
40d60 65 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  en;.    int n_by
40d70 74 65 73 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  tes;.    unsigne
40d80 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
40d90 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 63 6f 6e   = NULL;.    con
40da0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
40db0 20 2a 77 6b 62 3b 0a 20 20 20 20 67 61 69 61 47   *wkb;.    gaiaG
40dc0 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
40dd0 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f   NULL;.    GAIA_
40de0 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
40df0 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
40e00 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
40e10 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
40e20 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
40e30 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
40e40 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
40e50 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
40e60 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
40e70 20 7d 0a 20 20 20 20 77 6b 62 20 3d 20 73 71 6c   }.    wkb = sql
40e80 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
40e90 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
40ea0 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
40eb0 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
40ec0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 20 28  gv[0]);.    if (
40ed0 21 63 68 65 63 6b 5f 77 6b 62 20 28 77 6b 62 2c  !check_wkb (wkb,
40ee0 20 6e 5f 62 79 74 65 73 2c 20 74 79 70 65 29 29   n_bytes, type))
40ef0 0a 09 72 65 74 75 72 6e 3b 0a 20 20 20 20 67 65  ..return;.    ge
40f00 6f 20 3d 20 67 61 69 61 46 72 6f 6d 57 6b 62 20  o = gaiaFromWkb 
40f10 28 77 6b 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a  (wkb, n_bytes);.
40f20 20 20 20 20 69 66 20 28 67 65 6f 20 3d 3d 20 4e      if (geo == N
40f30 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  ULL).      {..  
40f40 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
40f50 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
40f60 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
40f70 7d 0a 20 20 20 20 67 61 69 61 54 6f 53 70 61 74  }.    gaiaToSpat
40f80 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67  iaLiteBlobWkb (g
40f90 65 6f 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26  eo, &p_result, &
40fa0 6c 65 6e 29 3b 0a 20 20 20 20 67 61 69 61 46 72  len);.    gaiaFr
40fb0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
40fc0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
40fd0 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
40fe0 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
40ff0 6e 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61  n, free);.}..sta
41000 74 69 63 20 76 6f 69 64 0a 67 65 6f 6d 5f 66 72  tic void.geom_fr
41010 6f 6d 5f 77 6b 62 32 20 28 73 71 6c 69 74 65 33  om_wkb2 (sqlite3
41020 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
41030 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
41040 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
41050 72 67 76 2c 0a 09 09 73 68 6f 72 74 20 74 79 70  rgv,...short typ
41060 65 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  e).{./* SQL func
41070 74 69 6f 6e 3a 0a 2f 20 47 65 6f 6d 46 72 6f 6d  tion:./ GeomFrom
41080 57 4b 42 28 57 4b 42 20 65 6e 63 6f 64 65 64 20  WKB(WKB encoded 
41090 67 65 6f 6d 65 74 72 79 2c 20 53 52 49 44 29 0a  geometry, SRID).
410a0 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20  /./ returns the 
410b0 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79  current geometry
410c0 20 62 79 20 70 61 72 73 69 6e 67 20 61 20 57 4b   by parsing a WK
410d0 42 20 65 6e 63 6f 64 65 64 20 62 6c 6f 62 0a 2f  B encoded blob./
410e0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
410f0 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
41100 65 72 65 64 0a 2f 0a 2f 20 69 66 20 2a 74 79 70  ered././ if *typ
41110 65 2a 20 69 73 20 61 20 6e 65 67 61 74 69 76 65  e* is a negative
41120 20 76 61 6c 75 65 20 63 61 6e 20 61 63 63 65 70   value can accep
41130 74 20 61 6e 79 20 47 45 4f 4d 45 54 52 59 20 43  t any GEOMETRY C
41140 4c 41 53 53 0a 2f 20 6f 74 68 65 72 77 69 73 65  LASS./ otherwise
41150 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 73 20 63   only requests c
41160 6f 6e 66 6f 72 6d 69 6e 67 20 77 69 74 68 20 72  onforming with r
41170 65 71 75 69 72 65 64 20 43 4c 41 53 53 20 61 72  equired CLASS ar
41180 65 20 76 61 6c 69 64 0a 2a 2f 0a 20 20 20 20 69  e valid.*/.    i
41190 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 69 6e 74 20  nt len;.    int 
411a0 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 75 6e 73  n_bytes;.    uns
411b0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
411c0 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  sult = NULL;.   
411d0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
411e0 63 68 61 72 20 2a 77 6b 62 3b 0a 20 20 20 20 67  char *wkb;.    g
411f0 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
41200 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47  eo = NULL;.    G
41210 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
41220 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
41230 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
41240 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
41250 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
41260 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
41270 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
41280 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
41290 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
412a0 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73       }.    if (s
412b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
412c0 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53  e (argv[1]) != S
412d0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
412e0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
412f0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
41300 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
41310 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
41320 77 6b 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  wkb = sqlite3_va
41330 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30  lue_blob (argv[0
41340 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20  ]);.    n_bytes 
41350 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
41360 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b  bytes (argv[0]);
41370 0a 20 20 20 20 69 66 20 28 21 63 68 65 63 6b 5f  .    if (!check_
41380 77 6b 62 20 28 77 6b 62 2c 20 6e 5f 62 79 74 65  wkb (wkb, n_byte
41390 73 2c 20 74 79 70 65 29 29 0a 09 72 65 74 75 72  s, type))..retur
413a0 6e 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69  n;.    geo = gai
413b0 61 46 72 6f 6d 57 6b 62 20 28 77 6b 62 2c 20 6e  aFromWkb (wkb, n
413c0 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
413d0 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20  (geo == NULL).  
413e0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
413f0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
41400 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
41410 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67  n;.      }.    g
41420 65 6f 2d 3e 53 72 69 64 20 3d 20 73 71 6c 69 74  eo->Srid = sqlit
41430 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
41440 67 76 5b 31 5d 29 3b 0a 20 20 20 20 67 61 69 61  gv[1]);.    gaia
41450 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
41460 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65 73  Wkb (geo, &p_res
41470 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20  ult, &len);.    
41480 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
41490 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71 6c 69   (geo);.    sqli
414a0 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
414b0 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
414c0 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
414d0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
414e0 6e 63 74 5f 47 65 6f 6d 65 74 72 79 46 72 6f 6d  nct_GeometryFrom
414f0 46 47 46 31 20 28 73 71 6c 69 74 65 33 5f 63 6f  FGF1 (sqlite3_co
41500 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
41510 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 20 20 20   int argc,...   
41520 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
41530 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
41540 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
41550 47 65 6f 6d 46 72 6f 6d 46 47 46 28 46 47 46 20  GeomFromFGF(FGF 
41560 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79  encoded geometry
41570 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68  )././ returns th
41580 65 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74  e current geomet
41590 72 79 20 62 79 20 70 61 72 73 69 6e 67 20 61 6e  ry by parsing an
415a0 20 46 47 46 20 65 6e 63 6f 64 65 64 20 62 6c 6f   FGF encoded blo
415b0 62 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  b ./ or NULL if 
415c0 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
415d0 6f 75 6e 74 65 72 65 64 0a 2f 0a 2f 20 69 66 20  ountered././ if 
415e0 2a 74 79 70 65 2a 20 69 73 20 61 20 6e 65 67 61  *type* is a nega
415f0 74 69 76 65 20 76 61 6c 75 65 20 63 61 6e 20 61  tive value can a
41600 63 63 65 70 74 20 61 6e 79 20 47 45 4f 4d 45 54  ccept any GEOMET
41610 52 59 20 43 4c 41 53 53 0a 2f 20 6f 74 68 65 72  RY CLASS./ other
41620 77 69 73 65 20 6f 6e 6c 79 20 72 65 71 75 65 73  wise only reques
41630 74 73 20 63 6f 6e 66 6f 72 6d 69 6e 67 20 77 69  ts conforming wi
41640 74 68 20 72 65 71 75 69 72 65 64 20 43 4c 41 53  th required CLAS
41650 53 20 61 72 65 20 76 61 6c 69 64 0a 2a 2f 0a 20  S are valid.*/. 
41660 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20     int len;.    
41670 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
41680 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
41690 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
416a0 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  .    const unsig
416b0 6e 65 64 20 63 68 61 72 20 2a 66 67 66 3b 0a 20  ned char *fgf;. 
416c0 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
416d0 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20  tr geo = NULL;. 
416e0 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
416f0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
41700 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
41710 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
41720 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
41730 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
41740 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
41750 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
41760 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
41770 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 66  n;.      }.    f
41780 67 66 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  gf = sqlite3_val
41790 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
417a0 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
417b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
417c0 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
417d0 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
417e0 6f 6d 46 67 66 20 28 66 67 66 2c 20 6e 5f 62 79  omFgf (fgf, n_by
417f0 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 67 65  tes);.    if (ge
41800 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20  o == NULL).     
41810 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
41820 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
41830 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
41840 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61        }.    gaia
41850 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
41860 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65 73  Wkb (geo, &p_res
41870 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20  ult, &len);.    
41880 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
41890 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71 6c 69   (geo);.    sqli
418a0 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
418b0 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
418c0 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
418d0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
418e0 6e 63 74 5f 47 65 6f 6d 65 74 72 79 46 72 6f 6d  nct_GeometryFrom
418f0 46 47 46 32 20 28 73 71 6c 69 74 65 33 5f 63 6f  FGF2 (sqlite3_co
41900 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
41910 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 20 20 20   int argc,...   
41920 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
41930 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
41940 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
41950 47 65 6f 6d 46 72 6f 6d 46 47 46 28 46 47 46 20  GeomFromFGF(FGF 
41960 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79  encoded geometry
41970 2c 20 53 52 49 44 29 0a 2f 0a 2f 20 72 65 74 75  , SRID)././ retu
41980 72 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  rns the current 
41990 67 65 6f 6d 65 74 72 79 20 62 79 20 70 61 72 73  geometry by pars
419a0 69 6e 67 20 61 6e 20 46 47 46 20 65 6e 63 6f 64  ing an FGF encod
419b0 65 64 20 73 74 72 69 6e 67 20 0a 2f 20 6f 72 20  ed string ./ or 
419c0 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f  NULL if any erro
419d0 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
419e0 0a 2f 0a 2f 20 69 66 20 2a 74 79 70 65 2a 20 69  ././ if *type* i
419f0 73 20 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c  s a negative val
41a00 75 65 20 63 61 6e 20 61 63 63 65 70 74 20 61 6e  ue can accept an
41a10 79 20 47 45 4f 4d 45 54 52 59 20 43 4c 41 53 53  y GEOMETRY CLASS
41a20 0a 2f 20 6f 74 68 65 72 77 69 73 65 20 6f 6e 6c  ./ otherwise onl
41a30 79 20 72 65 71 75 65 73 74 73 20 63 6f 6e 66 6f  y requests confo
41a40 72 6d 69 6e 67 20 77 69 74 68 20 72 65 71 75 69  rming with requi
41a50 72 65 64 20 43 4c 41 53 53 20 61 72 65 20 76 61  red CLASS are va
41a60 6c 69 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  lid.*/.    int l
41a70 65 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  en;.    int n_by
41a80 74 65 73 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  tes;.    unsigne
41a90 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
41aa0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 63 6f 6e   = NULL;.    con
41ab0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
41ac0 20 2a 66 67 66 3b 0a 20 20 20 20 67 61 69 61 47   *fgf;.    gaiaG
41ad0 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
41ae0 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f   NULL;.    GAIA_
41af0 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
41b00 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
41b10 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
41b20 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
41b30 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
41b40 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
41b50 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
41b60 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
41b70 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
41b80 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74   }.    if (sqlit
41b90 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
41ba0 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[1]) != SQLIT
41bb0 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
41bc0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
41bd0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
41be0 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
41bf0 20 20 20 20 20 20 7d 0a 20 20 20 20 66 67 66 20        }.    fgf 
41c00 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
41c10 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
41c20 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
41c30 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
41c40 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
41c50 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 46   geo = gaiaFromF
41c60 67 66 20 28 66 67 66 2c 20 6e 5f 62 79 74 65 73  gf (fgf, n_bytes
41c70 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 20 3d  );.    if (geo =
41c80 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a  = NULL).      {.
41c90 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
41ca0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
41cb0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
41cc0 20 20 20 7d 0a 20 20 20 20 67 65 6f 2d 3e 53 72     }.    geo->Sr
41cd0 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  id = sqlite3_val
41ce0 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
41cf0 3b 0a 20 20 20 20 67 61 69 61 54 6f 53 70 61 74  ;.    gaiaToSpat
41d00 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67  iaLiteBlobWkb (g
41d10 65 6f 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26  eo, &p_result, &
41d20 6c 65 6e 29 3b 0a 20 20 20 20 67 61 69 61 46 72  len);.    gaiaFr
41d30 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
41d40 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
41d50 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
41d60 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
41d70 6e 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a  n, free);.}../*.
41d80 2f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  / the following 
41d90 66 75 6e 63 74 69 6f 6e 73 20 73 69 6d 70 6c 79  functions simply
41da0 20 72 65 61 64 64 72 65 73 73 20 74 68 65 20 72   readdress the r
41db0 65 71 75 65 73 74 20 74 6f 20 67 65 6f 6d 5f 66  equest to geom_f
41dc0 72 6f 6d 5f 74 65 78 74 3f 28 29 0a 2f 20 73 65  rom_text?()./ se
41dd0 74 74 69 6e 67 20 74 68 65 20 61 70 70 72 6f 70  tting the approp
41de0 72 69 61 74 65 20 47 45 4f 4d 45 54 52 59 20 43  riate GEOMETRY C
41df0 4c 41 53 53 20 54 59 50 45 0a 2a 2f 0a 0a 73 74  LASS TYPE.*/..st
41e00 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 47  atic void.fnct_G
41e10 65 6f 6d 46 72 6f 6d 54 65 78 74 31 20 28 73 71  eomFromText1 (sq
41e20 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
41e30 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
41e40 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
41e50 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20   ** argv).{.    
41e60 67 65 6f 6d 5f 66 72 6f 6d 5f 74 65 78 74 31 20  geom_from_text1 
41e70 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
41e80 61 72 67 76 2c 20 28 73 68 6f 72 74 29 20 2d 31  argv, (short) -1
41e90 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
41ea0 64 0a 66 6e 63 74 5f 47 65 6f 6d 46 72 6f 6d 54  d.fnct_GeomFromT
41eb0 65 78 74 32 20 28 73 71 6c 69 74 65 33 5f 63 6f  ext2 (sqlite3_co
41ec0 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
41ed0 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
41ee0 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
41ef0 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f  ).{.    geom_fro
41f00 6d 5f 74 65 78 74 32 20 28 63 6f 6e 74 65 78 74  m_text2 (context
41f10 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73  , argc, argv, (s
41f20 68 6f 72 74 29 20 2d 31 29 3b 0a 7d 0a 0a 73 74  hort) -1);.}..st
41f30 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 47  atic void.fnct_G
41f40 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54 65 78 74 31  eomCollFromText1
41f50 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
41f60 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
41f70 20 61 72 67 63 2c 0a 09 09 09 73 71 6c 69 74 65   argc,....sqlite
41f80 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
41f90 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d  .{.    geom_from
41fa0 5f 74 65 78 74 31 20 28 63 6f 6e 74 65 78 74 2c  _text1 (context,
41fb0 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68   argc, argv, (sh
41fc0 6f 72 74 29 20 47 41 49 41 5f 47 45 4f 4d 45 54  ort) GAIA_GEOMET
41fd0 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 29 3b 0a 7d  RYCOLLECTION);.}
41fe0 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
41ff0 63 74 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54  ct_GeomCollFromT
42000 65 78 74 32 20 28 73 71 6c 69 74 65 33 5f 63 6f  ext2 (sqlite3_co
42010 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
42020 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 09 73 71   int argc,....sq
42030 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
42040 72 67 76 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f  rgv).{.    geom_
42050 66 72 6f 6d 5f 74 65 78 74 32 20 28 63 6f 6e 74  from_text2 (cont
42060 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  ext, argc, argv,
42070 20 28 73 68 6f 72 74 29 20 47 41 49 41 5f 47 45   (short) GAIA_GE
42080 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e  OMETRYCOLLECTION
42090 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
420a0 64 0a 66 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d 54  d.fnct_LineFromT
420b0 65 78 74 31 20 28 73 71 6c 69 74 65 33 5f 63 6f  ext1 (sqlite3_co
420c0 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
420d0 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
420e0 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
420f0 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f  ).{.    geom_fro
42100 6d 5f 74 65 78 74 31 20 28 63 6f 6e 74 65 78 74  m_text1 (context
42110 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73  , argc, argv, (s
42120 68 6f 72 74 29 20 47 41 49 41 5f 4c 49 4e 45 53  hort) GAIA_LINES
42130 54 52 49 4e 47 29 3b 0a 7d 0a 0a 73 74 61 74 69  TRING);.}..stati
42140 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4c 69 6e 65  c void.fnct_Line
42150 46 72 6f 6d 54 65 78 74 32 20 28 73 71 6c 69 74  FromText2 (sqlit
42160 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
42170 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
42180 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
42190 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 67 65 6f   argv).{.    geo
421a0 6d 5f 66 72 6f 6d 5f 74 65 78 74 32 20 28 63 6f  m_from_text2 (co
421b0 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
421c0 76 2c 20 28 73 68 6f 72 74 29 20 47 41 49 41 5f  v, (short) GAIA_
421d0 4c 49 4e 45 53 54 52 49 4e 47 29 3b 0a 7d 0a 0a  LINESTRING);.}..
421e0 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
421f0 5f 50 6f 69 6e 74 46 72 6f 6d 54 65 78 74 31 20  _PointFromText1 
42200 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
42210 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
42220 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
42230 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20  lue ** argv).{. 
42240 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 74 65 78     geom_from_tex
42250 74 31 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  t1 (context, arg
42260 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74 29  c, argv, (short)
42270 20 47 41 49 41 5f 50 4f 49 4e 54 29 3b 0a 7d 0a   GAIA_POINT);.}.
42280 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
42290 74 5f 50 6f 69 6e 74 46 72 6f 6d 54 65 78 74 32  t_PointFromText2
422a0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
422b0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
422c0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
422d0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
422e0 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 74 65      geom_from_te
422f0 78 74 32 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  xt2 (context, ar
42300 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74  gc, argv, (short
42310 29 20 47 41 49 41 5f 50 4f 49 4e 54 29 3b 0a 7d  ) GAIA_POINT);.}
42320 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
42330 63 74 5f 50 6f 6c 79 46 72 6f 6d 54 65 78 74 31  ct_PolyFromText1
42340 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
42350 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
42360 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
42370 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
42380 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 74 65      geom_from_te
42390 78 74 31 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  xt1 (context, ar
423a0 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74  gc, argv, (short
423b0 29 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 29 3b  ) GAIA_POLYGON);
423c0 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
423d0 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 54 65 78  fnct_PolyFromTex
423e0 74 32 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t2 (sqlite3_cont
423f0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
42400 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
42410 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
42420 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f  {.    geom_from_
42430 74 65 78 74 32 20 28 63 6f 6e 74 65 78 74 2c 20  text2 (context, 
42440 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f  argc, argv, (sho
42450 72 74 29 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e  rt) GAIA_POLYGON
42460 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
42470 64 0a 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d  d.fnct_MLineFrom
42480 54 65 78 74 31 20 28 73 71 6c 69 74 65 33 5f 63  Text1 (sqlite3_c
42490 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
424a0 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
424b0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
424c0 76 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72  v).{.    geom_fr
424d0 6f 6d 5f 74 65 78 74 31 20 28 63 6f 6e 74 65 78  om_text1 (contex
424e0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28  t, argc, argv, (
424f0 73 68 6f 72 74 29 20 47 41 49 41 5f 4d 55 4c 54  short) GAIA_MULT
42500 49 4c 49 4e 45 53 54 52 49 4e 47 29 3b 0a 7d 0a  ILINESTRING);.}.
42510 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
42520 74 5f 4d 4c 69 6e 65 46 72 6f 6d 54 65 78 74 32  t_MLineFromText2
42530 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
42540 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
42550 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
42560 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
42570 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 74 65      geom_from_te
42580 78 74 32 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  xt2 (context, ar
42590 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74  gc, argv, (short
425a0 29 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45  ) GAIA_MULTILINE
425b0 53 54 52 49 4e 47 29 3b 0a 7d 0a 0a 73 74 61 74  STRING);.}..stat
425c0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 50 6f  ic void.fnct_MPo
425d0 69 6e 74 46 72 6f 6d 54 65 78 74 31 20 28 73 71  intFromText1 (sq
425e0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
425f0 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
42600 63 2c 0a 09 09 20 20 20 20 20 20 73 71 6c 69 74  c,...      sqlit
42610 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
42620 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f  ).{.    geom_fro
42630 6d 5f 74 65 78 74 31 20 28 63 6f 6e 74 65 78 74  m_text1 (context
42640 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73  , argc, argv, (s
42650 68 6f 72 74 29 20 47 41 49 41 5f 4d 55 4c 54 49  hort) GAIA_MULTI
42660 50 4f 49 4e 54 29 3b 0a 7d 0a 0a 73 74 61 74 69  POINT);.}..stati
42670 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 50 6f 69  c void.fnct_MPoi
42680 6e 74 46 72 6f 6d 54 65 78 74 32 20 28 73 71 6c  ntFromText2 (sql
42690 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
426a0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
426b0 2c 0a 09 09 20 20 20 20 20 20 73 71 6c 69 74 65  ,...      sqlite
426c0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
426d0 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d  .{.    geom_from
426e0 5f 74 65 78 74 32 20 28 63 6f 6e 74 65 78 74 2c  _text2 (context,
426f0 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68   argc, argv, (sh
42700 6f 72 74 29 20 47 41 49 41 5f 4d 55 4c 54 49 50  ort) GAIA_MULTIP
42710 4f 49 4e 54 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  OINT);.}..static
42720 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 50 6f 6c 79   void.fnct_MPoly
42730 46 72 6f 6d 54 65 78 74 31 20 28 73 71 6c 69 74  FromText1 (sqlit
42740 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
42750 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
42760 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
42770 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 67 65 6f   argv).{.    geo
42780 6d 5f 66 72 6f 6d 5f 74 65 78 74 31 20 28 63 6f  m_from_text1 (co
42790 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
427a0 76 2c 20 28 73 68 6f 72 74 29 20 47 41 49 41 5f  v, (short) GAIA_
427b0 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 29 3b 0a 7d  MULTIPOLYGON);.}
427c0 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
427d0 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74  ct_MPolyFromText
427e0 32 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  2 (sqlite3_conte
427f0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
42800 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
42810 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
42820 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 74  .    geom_from_t
42830 65 78 74 32 20 28 63 6f 6e 74 65 78 74 2c 20 61  ext2 (context, a
42840 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72  rgc, argv, (shor
42850 74 29 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c  t) GAIA_MULTIPOL
42860 59 47 4f 4e 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  YGON);.}..static
42870 20 76 6f 69 64 0a 66 6e 63 74 5f 57 6b 74 54 6f   void.fnct_WktTo
42880 53 71 6c 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Sql (sqlite3_con
42890 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
428a0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
428b0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
428c0 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
428d0 6f 6e 3a 0a 2f 20 53 54 5f 57 4b 54 54 6f 53 51  on:./ ST_WKTToSQ
428e0 4c 28 57 4b 54 20 65 6e 63 6f 64 65 64 20 67 65  L(WKT encoded ge
428f0 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75  ometry)././ retu
42900 72 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  rns the current 
42910 67 65 6f 6d 65 74 72 79 20 62 79 20 70 61 72 73  geometry by pars
42920 69 6e 67 20 57 4b 54 20 65 6e 63 6f 64 65 64 20  ing WKT encoded 
42930 73 74 72 69 6e 67 20 0a 2f 20 6f 72 20 4e 55 4c  string ./ or NUL
42940 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69  L if any error i
42950 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2f 0a  s encountered./.
42960 2f 20 74 68 65 20 53 52 49 44 20 69 73 20 61 6c  / the SRID is al
42970 77 61 79 73 20 30 20 5b 53 51 4c 2f 4d 4d 20 66  ways 0 [SQL/MM f
42980 75 6e 63 74 69 6f 6e 5d 0a 2a 2f 0a 20 20 20 20  unction].*/.    
42990 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73  int len;.    uns
429a0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
429b0 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  sult = NULL;.   
429c0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
429d0 63 68 61 72 20 2a 74 65 78 74 3b 0a 20 20 20 20  char *text;.    
429e0 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
429f0 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
42a00 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
42a10 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
42a20 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
42a30 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
42a40 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
42a50 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20  LITE_TEXT).     
42a60 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
42a70 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
42a80 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
42a90 20 20 20 20 20 20 7d 0a 20 20 20 20 74 65 78 74        }.    text
42aa0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
42ab0 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d 29 3b  _text (argv[0]);
42ac0 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 50  .    geo = gaiaP
42ad0 61 72 73 65 57 6b 74 20 28 74 65 78 74 2c 20 2d  arseWkt (text, -
42ae0 31 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 20  1);.    if (geo 
42af0 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b  == NULL).      {
42b00 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
42b10 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
42b20 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
42b30 20 20 20 20 7d 0a 20 20 20 20 67 65 6f 2d 3e 53      }.    geo->S
42b40 72 69 64 20 3d 20 30 3b 0a 20 20 20 20 67 61 69  rid = 0;.    gai
42b50 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  aToSpatiaLiteBlo
42b60 62 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65  bWkb (geo, &p_re
42b70 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20  sult, &len);.   
42b80 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
42b90 6c 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71 6c  l (geo);.    sql
42ba0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
42bb0 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73   (context, p_res
42bc0 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b  ult, len, free);
42bd0 0a 7d 0a 0a 2f 2a 0a 2f 20 74 68 65 20 66 6f 6c  .}../*./ the fol
42be0 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  lowing functions
42bf0 20 73 69 6d 70 6c 79 20 72 65 61 64 64 72 65 73   simply readdres
42c00 73 20 74 68 65 20 72 65 71 75 65 73 74 20 74 6f  s the request to
42c10 20 67 65 6f 6d 5f 66 72 6f 6d 5f 77 6b 62 3f 28   geom_from_wkb?(
42c20 29 0a 2f 20 73 65 74 74 69 6e 67 20 74 68 65 20  )./ setting the 
42c30 61 70 70 72 6f 70 72 69 61 74 65 20 47 45 4f 4d  appropriate GEOM
42c40 45 54 52 59 20 43 4c 41 53 53 20 54 59 50 45 0a  ETRY CLASS TYPE.
42c50 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  */..static void.
42c60 66 6e 63 74 5f 47 65 6f 6d 46 72 6f 6d 57 6b 62  fnct_GeomFromWkb
42c70 31 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  1 (sqlite3_conte
42c80 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
42c90 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
42ca0 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
42cb0 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 77  .    geom_from_w
42cc0 6b 62 31 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  kb1 (context, ar
42cd0 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74  gc, argv, (short
42ce0 29 20 2d 31 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  ) -1);.}..static
42cf0 20 76 6f 69 64 0a 66 6e 63 74 5f 47 65 6f 6d 46   void.fnct_GeomF
42d00 72 6f 6d 57 6b 62 32 20 28 73 71 6c 69 74 65 33  romWkb2 (sqlite3
42d10 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
42d20 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
42d30 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
42d40 72 67 76 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f  rgv).{.    geom_
42d50 66 72 6f 6d 5f 77 6b 62 32 20 28 63 6f 6e 74 65  from_wkb2 (conte
42d60 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20  xt, argc, argv, 
42d70 28 73 68 6f 72 74 29 20 2d 31 29 3b 0a 7d 0a 0a  (short) -1);.}..
42d80 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
42d90 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 57 6b 62  _GeomCollFromWkb
42da0 31 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  1 (sqlite3_conte
42db0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
42dc0 74 20 61 72 67 63 2c 0a 09 09 20 20 20 20 20 20  t argc,...      
42dd0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
42de0 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 67 65  * argv).{.    ge
42df0 6f 6d 5f 66 72 6f 6d 5f 77 6b 62 31 20 28 63 6f  om_from_wkb1 (co
42e00 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
42e10 76 2c 20 28 73 68 6f 72 74 29 20 47 41 49 41 5f  v, (short) GAIA_
42e20 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49  GEOMETRYCOLLECTI
42e30 4f 4e 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  ON);.}..static v
42e40 6f 69 64 0a 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c  oid.fnct_GeomCol
42e50 6c 46 72 6f 6d 57 6b 62 32 20 28 73 71 6c 69 74  lFromWkb2 (sqlit
42e60 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
42e70 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a  text, int argc,.
42e80 09 09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  ..       sqlite3
42e90 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
42ea0 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f  {.    geom_from_
42eb0 77 6b 62 32 20 28 63 6f 6e 74 65 78 74 2c 20 61  wkb2 (context, a
42ec0 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72  rgc, argv, (shor
42ed0 74 29 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59  t) GAIA_GEOMETRY
42ee0 43 4f 4c 4c 45 43 54 49 4f 4e 29 3b 0a 7d 0a 0a  COLLECTION);.}..
42ef0 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
42f00 5f 4c 69 6e 65 46 72 6f 6d 57 6b 62 31 20 28 73  _LineFromWkb1 (s
42f10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
42f20 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
42f30 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
42f40 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20  e ** argv).{.   
42f50 20 67 65 6f 6d 5f 66 72 6f 6d 5f 77 6b 62 31 20   geom_from_wkb1 
42f60 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
42f70 61 72 67 76 2c 20 28 73 68 6f 72 74 29 20 47 41  argv, (short) GA
42f80 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 29 3b 0a  IA_LINESTRING);.
42f90 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
42fa0 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d 57 6b 62 32  nct_LineFromWkb2
42fb0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
42fc0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
42fd0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
42fe0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
42ff0 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 77 6b      geom_from_wk
43000 62 32 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  b2 (context, arg
43010 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74 29  c, argv, (short)
43020 20 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47   GAIA_LINESTRING
43030 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
43040 64 0a 66 6e 63 74 5f 50 6f 69 6e 74 46 72 6f 6d  d.fnct_PointFrom
43050 57 6b 62 31 20 28 73 71 6c 69 74 65 33 5f 63 6f  Wkb1 (sqlite3_co
43060 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
43070 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
43080 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
43090 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f  ).{.    geom_fro
430a0 6d 5f 77 6b 62 31 20 28 63 6f 6e 74 65 78 74 2c  m_wkb1 (context,
430b0 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68   argc, argv, (sh
430c0 6f 72 74 29 20 47 41 49 41 5f 50 4f 49 4e 54 29  ort) GAIA_POINT)
430d0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
430e0 0a 66 6e 63 74 5f 50 6f 69 6e 74 46 72 6f 6d 57  .fnct_PointFromW
430f0 6b 62 32 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  kb2 (sqlite3_con
43100 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
43110 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
43120 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
43130 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d  .{.    geom_from
43140 5f 77 6b 62 32 20 28 63 6f 6e 74 65 78 74 2c 20  _wkb2 (context, 
43150 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f  argc, argv, (sho
43160 72 74 29 20 47 41 49 41 5f 50 4f 49 4e 54 29 3b  rt) GAIA_POINT);
43170 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
43180 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 57 6b 62  fnct_PolyFromWkb
43190 31 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  1 (sqlite3_conte
431a0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
431b0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
431c0 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
431d0 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 77  .    geom_from_w
431e0 6b 62 31 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  kb1 (context, ar
431f0 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74  gc, argv, (short
43200 29 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 29 3b  ) GAIA_POLYGON);
43210 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
43220 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 57 6b 62  fnct_PolyFromWkb
43230 32 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  2 (sqlite3_conte
43240 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
43250 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
43260 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
43270 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 77  .    geom_from_w
43280 6b 62 32 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  kb2 (context, ar
43290 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72 74  gc, argv, (short
432a0 29 20 47 41 49 41 5f 50 4f 4c 59 47 4f 4e 29 3b  ) GAIA_POLYGON);
432b0 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
432c0 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d 57 6b  fnct_MLineFromWk
432d0 62 31 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b1 (sqlite3_cont
432e0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
432f0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
43300 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
43310 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f  {.    geom_from_
43320 77 6b 62 31 20 28 63 6f 6e 74 65 78 74 2c 20 61  wkb1 (context, a
43330 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68 6f 72  rgc, argv, (shor
43340 74 29 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e  t) GAIA_MULTILIN
43350 45 53 54 52 49 4e 47 29 3b 0a 7d 0a 0a 73 74 61  ESTRING);.}..sta
43360 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 4c  tic void.fnct_ML
43370 69 6e 65 46 72 6f 6d 57 6b 62 32 20 28 73 71 6c  ineFromWkb2 (sql
43380 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
43390 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
433a0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
433b0 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 67  ** argv).{.    g
433c0 65 6f 6d 5f 66 72 6f 6d 5f 77 6b 62 32 20 28 63  eom_from_wkb2 (c
433d0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
433e0 67 76 2c 20 28 73 68 6f 72 74 29 20 47 41 49 41  gv, (short) GAIA
433f0 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47  _MULTILINESTRING
43400 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
43410 64 0a 66 6e 63 74 5f 4d 50 6f 69 6e 74 46 72 6f  d.fnct_MPointFro
43420 6d 57 6b 62 31 20 28 73 71 6c 69 74 65 33 5f 63  mWkb1 (sqlite3_c
43430 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
43440 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
43450 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
43460 76 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72  v).{.    geom_fr
43470 6f 6d 5f 77 6b 62 31 20 28 63 6f 6e 74 65 78 74  om_wkb1 (context
43480 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73  , argc, argv, (s
43490 68 6f 72 74 29 20 47 41 49 41 5f 4d 55 4c 54 49  hort) GAIA_MULTI
434a0 50 4f 49 4e 54 29 3b 0a 7d 0a 0a 73 74 61 74 69  POINT);.}..stati
434b0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 50 6f 69  c void.fnct_MPoi
434c0 6e 74 46 72 6f 6d 57 6b 62 32 20 28 73 71 6c 69  ntFromWkb2 (sqli
434d0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
434e0 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
434f0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
43500 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 67 65  * argv).{.    ge
43510 6f 6d 5f 66 72 6f 6d 5f 77 6b 62 32 20 28 63 6f  om_from_wkb2 (co
43520 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
43530 76 2c 20 28 73 68 6f 72 74 29 20 47 41 49 41 5f  v, (short) GAIA_
43540 4d 55 4c 54 49 50 4f 49 4e 54 29 3b 0a 7d 0a 0a  MULTIPOINT);.}..
43550 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
43560 5f 4d 50 6f 6c 79 46 72 6f 6d 57 6b 62 31 20 28  _MPolyFromWkb1 (
43570 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
43580 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
43590 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
435a0 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
435b0 20 20 67 65 6f 6d 5f 66 72 6f 6d 5f 77 6b 62 31    geom_from_wkb1
435c0 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c   (context, argc,
435d0 20 61 72 67 76 2c 20 28 73 68 6f 72 74 29 20 47   argv, (short) G
435e0 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e  AIA_MULTIPOLYGON
435f0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
43600 64 0a 66 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d  d.fnct_MPolyFrom
43610 57 6b 62 32 20 28 73 71 6c 69 74 65 33 5f 63 6f  Wkb2 (sqlite3_co
43620 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
43630 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
43640 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
43650 29 0a 7b 0a 20 20 20 20 67 65 6f 6d 5f 66 72 6f  ).{.    geom_fro
43660 6d 5f 77 6b 62 32 20 28 63 6f 6e 74 65 78 74 2c  m_wkb2 (context,
43670 20 61 72 67 63 2c 20 61 72 67 76 2c 20 28 73 68   argc, argv, (sh
43680 6f 72 74 29 20 47 41 49 41 5f 4d 55 4c 54 49 50  ort) GAIA_MULTIP
43690 4f 4c 59 47 4f 4e 29 3b 0a 7d 0a 0a 73 74 61 74  OLYGON);.}..stat
436a0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 57 6b 62  ic void.fnct_Wkb
436b0 54 6f 53 71 6c 20 28 73 71 6c 69 74 65 33 5f 63  ToSql (sqlite3_c
436c0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
436d0 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
436e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
436f0 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
43700 74 69 6f 6e 3a 0a 2f 20 53 54 5f 57 4b 42 54 6f  tion:./ ST_WKBTo
43710 53 51 4c 28 57 4b 42 20 65 6e 63 6f 64 65 64 20  SQL(WKB encoded 
43720 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65  geometry)././ re
43730 74 75 72 6e 73 20 74 68 65 20 63 75 72 72 65 6e  turns the curren
43740 74 20 67 65 6f 6d 65 74 72 79 20 62 79 20 70 61  t geometry by pa
43750 72 73 69 6e 67 20 61 20 57 4b 42 20 65 6e 63 6f  rsing a WKB enco
43760 64 65 64 20 62 6c 6f 62 20 0a 2f 20 6f 72 20 4e  ded blob ./ or N
43770 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
43780 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
43790 2f 0a 2f 20 74 68 65 20 53 52 49 44 20 69 73 20  /./ the SRID is 
437a0 61 6c 77 61 79 73 20 30 20 5b 53 51 4c 2f 4d 4d  always 0 [SQL/MM
437b0 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2f 0a 20 20   function].*/.  
437c0 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 69    int len;.    i
437d0 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
437e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
437f0 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a  _result = NULL;.
43800 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e      const unsign
43810 65 64 20 63 68 61 72 20 2a 77 6b 62 3b 0a 20 20  ed char *wkb;.  
43820 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
43830 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
43840 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
43850 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
43860 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
43870 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
43880 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
43890 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
438a0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
438b0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
438c0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
438d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 77 6b  ;.      }.    wk
438e0 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  b = sqlite3_valu
438f0 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
43900 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
43910 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
43920 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  tes (argv[0]);. 
43930 20 20 20 69 66 20 28 21 63 68 65 63 6b 5f 77 6b     if (!check_wk
43940 62 20 28 77 6b 62 2c 20 6e 5f 62 79 74 65 73 2c  b (wkb, n_bytes,
43950 20 2d 31 29 29 0a 09 72 65 74 75 72 6e 3b 0a 20   -1))..return;. 
43960 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f     geo = gaiaFro
43970 6d 57 6b 62 20 28 77 6b 62 2c 20 6e 5f 62 79 74  mWkb (wkb, n_byt
43980 65 73 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f  es);.    if (geo
43990 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20   == NULL).      
439a0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
439b0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
439c0 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
439d0 20 20 20 20 20 7d 0a 20 20 20 20 67 65 6f 2d 3e       }.    geo->
439e0 53 72 69 64 20 3d 20 30 3b 0a 20 20 20 20 67 61  Srid = 0;.    ga
439f0 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c  iaToSpatiaLiteBl
43a00 6f 62 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72  obWkb (geo, &p_r
43a10 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20  esult, &len);.  
43a20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
43a30 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71  ll (geo);.    sq
43a40 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
43a50 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
43a60 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29  sult, len, free)
43a70 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
43a80 0a 66 6e 63 74 5f 43 6f 6d 70 72 65 73 73 47 65  .fnct_CompressGe
43a90 6f 6d 65 74 72 79 20 28 73 71 6c 69 74 65 33 5f  ometry (sqlite3_
43aa0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
43ab0 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 20  t, int argc,... 
43ac0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
43ad0 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
43ae0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
43af0 2f 20 43 6f 6d 70 72 65 73 73 47 65 6f 6d 65 74  / CompressGeomet
43b00 72 79 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20  ry(BLOB encoded 
43b10 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65  geometry)././ re
43b20 74 75 72 6e 73 20 61 20 43 4f 4d 50 52 45 53 53  turns a COMPRESS
43b30 45 44 20 67 65 6f 6d 65 74 72 79 20 5b 69 66 20  ED geometry [if 
43b40 61 20 76 61 6c 69 64 20 47 65 6f 6d 65 74 72 79  a valid Geometry
43b50 20 77 61 73 20 73 75 70 70 6c 69 65 64 5d 0a 2f   was supplied]./
43b60 20 6f 72 20 4e 55 4c 4c 20 69 6e 20 61 6e 79 20   or NULL in any 
43b70 6f 74 68 65 72 20 63 61 73 65 0a 2a 2f 0a 20 20  other case.*/.  
43b80 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
43b90 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
43ba0 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e   n_bytes;.    in
43bb0 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67  t len;.    unsig
43bc0 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75  ned char *p_resu
43bd0 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  lt = NULL;.    g
43be0 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
43bf0 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47  eo = NULL;.    G
43c00 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
43c10 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
43c20 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
43c30 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
43c40 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
43c50 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
43c60 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
43c70 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
43c80 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
43c90 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f       }.    p_blo
43ca0 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
43cb0 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
43cc0 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30  lue_blob (argv[0
43cd0 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20  ]);.    n_bytes 
43ce0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
43cf0 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b  bytes (argv[0]);
43d00 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46  .    geo = gaiaF
43d10 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  romSpatiaLiteBlo
43d20 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f  bWkb (p_blob, n_
43d30 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28  bytes);.    if (
43d40 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72  !geo)..sqlite3_r
43d50 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
43d60 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20  ext);.    else. 
43d70 20 20 20 20 20 7b 0a 09 20 20 67 61 69 61 54 6f       {..  gaiaTo
43d80 43 6f 6d 70 72 65 73 73 65 64 42 6c 6f 62 57 6b  CompressedBlobWk
43d90 62 20 28 67 65 6f 2c 20 26 70 5f 72 65 73 75 6c  b (geo, &p_resul
43da0 74 2c 20 26 6c 65 6e 29 3b 0a 09 20 20 73 71 6c  t, &len);..  sql
43db0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
43dc0 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73   (context, p_res
43dd0 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b  ult, len, free);
43de0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
43df0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
43e00 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  eo);.}..static v
43e10 6f 69 64 0a 66 6e 63 74 5f 55 6e 63 6f 6d 70 72  oid.fnct_Uncompr
43e20 65 73 73 47 65 6f 6d 65 74 72 79 20 28 73 71 6c  essGeometry (sql
43e30 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
43e40 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
43e50 2c 0a 09 09 09 20 73 71 6c 69 74 65 33 5f 76 61  ,.... sqlite3_va
43e60 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
43e70 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
43e80 2f 20 55 6e 63 6f 6d 70 72 65 73 73 47 65 6f 6d  / UncompressGeom
43e90 65 74 72 79 28 42 4c 4f 42 20 65 6e 63 6f 64 65  etry(BLOB encode
43ea0 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20  d geometry)././ 
43eb0 72 65 74 75 72 6e 73 20 61 6e 20 55 4e 43 4f 4d  returns an UNCOM
43ec0 50 52 45 53 53 45 44 20 67 65 6f 6d 65 74 72 79  PRESSED geometry
43ed0 20 5b 69 66 20 61 20 76 61 6c 69 64 20 47 65 6f   [if a valid Geo
43ee0 6d 65 74 72 79 20 77 61 73 20 73 75 70 70 6c 69  metry was suppli
43ef0 65 64 5d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  ed] ./ or NULL i
43f00 6e 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73 65  n any other case
43f10 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  .*/.    unsigned
43f20 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20   char *p_blob;. 
43f30 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a     int n_bytes;.
43f40 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
43f50 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
43f60 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
43f70 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
43f80 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b  lPtr geo = NULL;
43f90 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
43fa0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
43fb0 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
43fc0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
43fd0 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
43fe0 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
43ff0 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
44000 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
44010 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
44020 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
44030 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
44040 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
44050 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
44060 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
44070 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
44080 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
44090 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
440a0 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
440b0 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
440c0 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
440d0 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
440e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
440f0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
44100 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
44110 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65  gaiaToSpatiaLite
44120 42 6c 6f 62 57 6b 62 20 28 67 65 6f 2c 20 26 70  BlobWkb (geo, &p
44130 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a  _result, &len);.
44140 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
44150 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
44160 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
44170 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  free);.      }. 
44180 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
44190 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
441a0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 53  atic void.fnct_S
441b0 61 6e 69 74 69 7a 65 47 65 6f 6d 65 74 72 79 20  anitizeGeometry 
441c0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
441d0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
441e0 61 72 67 63 2c 0a 09 09 20 20 20 20 20 20 20 73  argc,...       s
441f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
44200 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
44210 75 6e 63 74 69 6f 6e 3a 0a 2f 20 53 61 6e 69 74  unction:./ Sanit
44220 69 7a 65 47 65 6f 6d 65 74 72 79 28 42 4c 4f 42  izeGeometry(BLOB
44230 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72   encoded geometr
44240 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61  y)././ returns a
44250 20 53 41 4e 49 54 49 5a 45 44 20 67 65 6f 6d 65   SANITIZED geome
44260 74 72 79 20 5b 69 66 20 61 20 76 61 6c 69 64 20  try [if a valid 
44270 47 65 6f 6d 65 74 72 79 20 77 61 73 20 73 75 70  Geometry was sup
44280 70 6c 69 65 64 5d 0a 2f 20 6f 72 20 4e 55 4c 4c  plied]./ or NULL
44290 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 63 61   in any other ca
442a0 73 65 0a 2f 0a 2f 20 53 61 6e 69 74 69 7a 69 6e  se././ Sanitizin
442b0 67 20 69 6e 63 6c 75 64 65 73 3a 0a 2f 20 2d 20  g includes:./ - 
442c0 72 65 70 65 61 74 65 64 20 76 65 72 74 69 63 65  repeated vertice
442d0 73 20 73 75 70 70 72 65 73 73 69 6f 6e 0a 2f 20  s suppression./ 
442e0 2d 20 65 6e 66 6f 72 63 69 6e 67 20 72 69 6e 67  - enforcing ring
442f0 20 63 6c 6f 73 75 72 65 0a 2f 0a 2a 2f 0a 20 20   closure./.*/.  
44300 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
44310 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
44320 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e   n_bytes;.    in
44330 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67  t len;.    unsig
44340 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75  ned char *p_resu
44350 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  lt = NULL;.    g
44360 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
44370 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  eo = NULL;.    g
44380 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 73  aiaGeomCollPtr s
44390 61 6e 69 74 69 7a 65 64 20 3d 20 4e 55 4c 4c 3b  anitized = NULL;
443a0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
443b0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
443c0 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
443d0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
443e0 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
443f0 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
44400 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
44410 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
44420 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
44430 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
44440 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
44450 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
44460 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
44470 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
44480 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
44490 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
444a0 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
444b0 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
444c0 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
444d0 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
444e0 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
444f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
44500 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
44510 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
44520 73 61 6e 69 74 69 7a 65 64 20 3d 20 67 61 69 61  sanitized = gaia
44530 53 61 6e 69 74 69 7a 65 20 28 67 65 6f 29 3b 0a  Sanitize (geo);.
44540 09 20 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c  .  gaiaToSpatiaL
44550 69 74 65 42 6c 6f 62 57 6b 62 20 28 73 61 6e 69  iteBlobWkb (sani
44560 74 69 7a 65 64 2c 20 26 70 5f 72 65 73 75 6c 74  tized, &p_result
44570 2c 20 26 6c 65 6e 29 3b 0a 09 20 20 73 71 6c 69  , &len);..  sqli
44580 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
44590 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
445a0 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
445b0 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61        }.    gaia
445c0 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
445d0 6f 29 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65  o);.    gaiaFree
445e0 47 65 6f 6d 43 6f 6c 6c 20 28 73 61 6e 69 74 69  GeomColl (saniti
445f0 7a 65 64 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  zed);.}..static 
44600 76 6f 69 64 0a 63 61 73 74 5f 63 6f 75 6e 74 20  void.cast_count 
44610 28 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72  (gaiaGeomCollPtr
44620 20 67 65 6f 6d 2c 20 69 6e 74 20 2a 70 74 73 2c   geom, int *pts,
44630 20 69 6e 74 20 2a 6c 6e 73 2c 20 69 6e 74 20 2a   int *lns, int *
44640 70 67 73 29 0a 7b 0a 2f 2a 20 63 6f 75 6e 74 69  pgs).{./* counti
44650 6e 67 20 65 6c 65 6d 65 6e 74 61 72 79 20 67 65  ng elementary ge
44660 6f 6d 65 74 72 69 65 73 20 2a 2f 0a 20 20 20 20  ometries */.    
44670 69 6e 74 20 6e 5f 70 74 73 20 3d 20 30 3b 0a 20  int n_pts = 0;. 
44680 20 20 20 69 6e 74 20 6e 5f 6c 6e 73 20 3d 20 30     int n_lns = 0
44690 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 70 67 73 20  ;.    int n_pgs 
446a0 3d 20 30 3b 0a 20 20 20 20 67 61 69 61 50 6f 69  = 0;.    gaiaPoi
446b0 6e 74 50 74 72 20 70 74 3b 0a 20 20 20 20 67 61  ntPtr pt;.    ga
446c0 69 61 4c 69 6e 65 73 74 72 69 6e 67 50 74 72 20  iaLinestringPtr 
446d0 6c 6e 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79  ln;.    gaiaPoly
446e0 67 6f 6e 50 74 72 20 70 67 3b 0a 20 20 20 20 69  gonPtr pg;.    i
446f0 66 20 28 67 65 6f 6d 29 0a 20 20 20 20 20 20 7b  f (geom).      {
44700 0a 09 20 20 70 74 20 3d 20 67 65 6f 6d 2d 3e 46  ..  pt = geom->F
44710 69 72 73 74 50 6f 69 6e 74 3b 0a 09 20 20 77 68  irstPoint;..  wh
44720 69 6c 65 20 28 70 74 29 0a 09 20 20 20 20 7b 0a  ile (pt)..    {.
44730 09 09 6e 5f 70 74 73 2b 2b 3b 0a 09 09 70 74 20  ..n_pts++;...pt 
44740 3d 20 70 74 2d 3e 4e 65 78 74 3b 0a 09 20 20 20  = pt->Next;..   
44750 20 7d 0a 09 20 20 6c 6e 20 3d 20 67 65 6f 6d 2d   }..  ln = geom-
44760 3e 46 69 72 73 74 4c 69 6e 65 73 74 72 69 6e 67  >FirstLinestring
44770 3b 0a 09 20 20 77 68 69 6c 65 20 28 6c 6e 29 0a  ;..  while (ln).
44780 09 20 20 20 20 7b 0a 09 09 6e 5f 6c 6e 73 2b 2b  .    {...n_lns++
44790 3b 0a 09 09 6c 6e 20 3d 20 6c 6e 2d 3e 4e 65 78  ;...ln = ln->Nex
447a0 74 3b 0a 09 20 20 20 20 7d 0a 09 20 20 70 67 20  t;..    }..  pg 
447b0 3d 20 67 65 6f 6d 2d 3e 46 69 72 73 74 50 6f 6c  = geom->FirstPol
447c0 79 67 6f 6e 3b 0a 09 20 20 77 68 69 6c 65 20 28  ygon;..  while (
447d0 70 67 29 0a 09 20 20 20 20 7b 0a 09 09 6e 5f 70  pg)..    {...n_p
447e0 67 73 2b 2b 3b 0a 09 09 70 67 20 3d 20 70 67 2d  gs++;...pg = pg-
447f0 3e 4e 65 78 74 3b 0a 09 20 20 20 20 7d 0a 20 20  >Next;..    }.  
44800 20 20 20 20 7d 0a 20 20 20 20 2a 70 74 73 20 3d      }.    *pts =
44810 20 6e 5f 70 74 73 3b 0a 20 20 20 20 2a 6c 6e 73   n_pts;.    *lns
44820 20 3d 20 6e 5f 6c 6e 73 3b 0a 20 20 20 20 2a 70   = n_lns;.    *p
44830 67 73 20 3d 20 6e 5f 70 67 73 3b 0a 7d 0a 0a 73  gs = n_pgs;.}..s
44840 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
44850 43 61 73 74 54 6f 50 6f 69 6e 74 20 28 73 71 6c  CastToPoint (sql
44860 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
44870 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
44880 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
44890 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
448a0 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 43 61  L function:./ Ca
448b0 73 74 54 6f 50 6f 69 6e 74 28 42 4c 4f 42 20 65  stToPoint(BLOB e
448c0 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29  ncoded geometry)
448d0 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 50  ././ returns a P
448e0 4f 49 4e 54 2d 74 79 70 65 20 67 65 6f 6d 65 74  OINT-type geomet
448f0 72 79 20 5b 69 66 20 63 6f 6e 76 65 72 73 69 6f  ry [if conversio
44900 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 5d 20 0a  n is possible] .
44910 2f 20 6f 72 20 4e 55 4c 4c 20 69 6e 20 61 6e 79  / or NULL in any
44920 20 6f 74 68 65 72 20 63 61 73 65 0a 2a 2f 0a 20   other case.*/. 
44930 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
44940 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e   *p_blob;.    in
44950 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69  t n_bytes;.    i
44960 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69  nt len;.    unsi
44970 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
44980 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ult = NULL;.    
44990 69 6e 74 20 70 74 73 3b 0a 20 20 20 20 69 6e 74  int pts;.    int
449a0 20 6c 6e 73 3b 0a 20 20 20 20 69 6e 74 20 70 67   lns;.    int pg
449b0 73 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  s;.    gaiaGeomC
449c0 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c  ollPtr geo = NUL
449d0 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
449e0 6f 6c 6c 50 74 72 20 67 65 6f 6d 32 20 3d 20 4e  ollPtr geom2 = N
449f0 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ULL;.    GAIA_UN
44a00 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
44a10 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
44a20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
44a30 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
44a40 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
44a50 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
44a60 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
44a70 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
44a80 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
44a90 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
44aa0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
44ab0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
44ac0 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
44ad0 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
44ae0 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
44af0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
44b00 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
44b10 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
44b20 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
44b30 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a  ;.    if (!geo).
44b40 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
44b50 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
44b60 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
44b70 0a 09 20 20 63 61 73 74 5f 63 6f 75 6e 74 20 28  ..  cast_count (
44b80 67 65 6f 2c 20 26 70 74 73 2c 20 26 6c 6e 73 2c  geo, &pts, &lns,
44b90 20 26 70 67 73 29 3b 0a 09 20 20 69 66 20 28 70   &pgs);..  if (p
44ba0 74 73 20 3d 3d 20 31 20 26 26 20 6c 6e 73 20 3d  ts == 1 && lns =
44bb0 3d 20 30 20 26 26 20 70 67 73 20 3d 3d 20 30 29  = 0 && pgs == 0)
44bc0 0a 09 20 20 20 20 7b 0a 09 09 67 65 6f 6d 32 20  ..    {...geom2 
44bd0 3d 20 67 61 69 61 43 6c 6f 6e 65 47 65 6f 6d 43  = gaiaCloneGeomC
44be0 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09 09 67 65 6f  oll (geo);...geo
44bf0 6d 32 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e  m2->Srid = geo->
44c00 53 72 69 64 3b 0a 09 09 67 65 6f 6d 32 2d 3e 44  Srid;...geom2->D
44c10 65 63 6c 61 72 65 64 54 79 70 65 20 3d 20 47 41  eclaredType = GA
44c20 49 41 5f 50 4f 49 4e 54 3b 0a 09 09 67 61 69 61  IA_POINT;...gaia
44c30 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
44c40 57 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70 5f 72  Wkb (geom2, &p_r
44c50 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09  esult, &len);...
44c60 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
44c70 20 28 67 65 6f 6d 32 29 3b 0a 09 09 73 71 6c 69   (geom2);...sqli
44c80 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
44c90 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
44ca0 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
44cb0 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09  .    }..  else..
44cc0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
44cd0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
44ce0 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  xt);.      }.   
44cf0 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
44d00 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74  l (geo);.}..stat
44d10 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 43 61 73  ic void.fnct_Cas
44d20 74 54 6f 4c 69 6e 65 73 74 72 69 6e 67 20 28 73  tToLinestring (s
44d30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
44d40 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
44d50 67 63 2c 0a 09 09 20 20 20 20 20 20 20 73 71 6c  gc,...       sql
44d60 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
44d70 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
44d80 63 74 69 6f 6e 3a 0a 2f 20 43 61 73 74 54 6f 4c  ction:./ CastToL
44d90 69 6e 65 73 74 72 69 6e 67 28 42 4c 4f 42 20 65  inestring(BLOB e
44da0 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29  ncoded geometry)
44db0 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 4c  ././ returns a L
44dc0 49 4e 45 53 54 52 49 4e 47 2d 74 79 70 65 20 67  INESTRING-type g
44dd0 65 6f 6d 65 74 72 79 20 5b 69 66 20 63 6f 6e 76  eometry [if conv
44de0 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  ersion is possib
44df0 6c 65 5d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  le] ./ or NULL i
44e00 6e 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73 65  n any other case
44e10 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  .*/.    unsigned
44e20 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20   char *p_blob;. 
44e30 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a     int n_bytes;.
44e40 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
44e50 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
44e60 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
44e70 0a 20 20 20 20 69 6e 74 20 70 74 73 3b 0a 20 20  .    int pts;.  
44e80 20 20 69 6e 74 20 6c 6e 73 3b 0a 20 20 20 20 69    int lns;.    i
44e90 6e 74 20 70 67 73 3b 0a 20 20 20 20 67 61 69 61  nt pgs;.    gaia
44ea0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
44eb0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
44ec0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d  GeomCollPtr geom
44ed0 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41  2 = NULL;.    GA
44ee0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
44ef0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
44f00 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
44f10 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
44f20 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
44f30 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
44f40 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
44f50 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
44f60 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
44f70 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
44f80 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
44f90 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
44fa0 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
44fb0 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
44fc0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
44fd0 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
44fe0 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
44ff0 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
45000 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
45010 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
45020 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  geo)..sqlite3_re
45030 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
45040 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  xt);.    else.  
45050 20 20 20 20 7b 0a 09 20 20 63 61 73 74 5f 63 6f      {..  cast_co
45060 75 6e 74 20 28 67 65 6f 2c 20 26 70 74 73 2c 20  unt (geo, &pts, 
45070 26 6c 6e 73 2c 20 26 70 67 73 29 3b 0a 09 20 20  &lns, &pgs);..  
45080 69 66 20 28 70 74 73 20 3d 3d 20 30 20 26 26 20  if (pts == 0 && 
45090 6c 6e 73 20 3d 3d 20 31 20 26 26 20 70 67 73 20  lns == 1 && pgs 
450a0 3d 3d 20 30 29 0a 09 20 20 20 20 7b 0a 09 09 67  == 0)..    {...g
450b0 65 6f 6d 32 20 3d 20 67 61 69 61 43 6c 6f 6e 65  eom2 = gaiaClone
450c0 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a  GeomColl (geo);.
450d0 09 09 67 65 6f 6d 32 2d 3e 53 72 69 64 20 3d 20  ..geom2->Srid = 
450e0 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 67 65 6f  geo->Srid;...geo
450f0 6d 32 2d 3e 44 65 63 6c 61 72 65 64 54 79 70 65  m2->DeclaredType
45100 20 3d 20 47 41 49 41 5f 4c 49 4e 45 53 54 52 49   = GAIA_LINESTRI
45110 4e 47 3b 0a 09 09 67 61 69 61 54 6f 53 70 61 74  NG;...gaiaToSpat
45120 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67  iaLiteBlobWkb (g
45130 65 6f 6d 32 2c 20 26 70 5f 72 65 73 75 6c 74 2c  eom2, &p_result,
45140 20 26 6c 65 6e 29 3b 0a 09 09 67 61 69 61 46 72   &len);...gaiaFr
45150 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d  eeGeomColl (geom
45160 32 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  2);...sqlite3_re
45170 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
45180 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
45190 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 20 20 7d  n, free);..    }
451a0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20  ..  else..      
451b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
451c0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
451d0 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
451e0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
451f0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
45200 64 0a 66 6e 63 74 5f 43 61 73 74 54 6f 50 6f 6c  d.fnct_CastToPol
45210 79 67 6f 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f  ygon (sqlite3_co
45220 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
45230 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
45240 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
45250 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
45260 69 6f 6e 3a 0a 2f 20 43 61 73 74 54 6f 50 6f 6c  ion:./ CastToPol
45270 79 67 6f 6e 28 42 4c 4f 42 20 65 6e 63 6f 64 65  ygon(BLOB encode
45280 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20  d geometry)././ 
45290 72 65 74 75 72 6e 73 20 61 20 50 4f 4c 59 47 4f  returns a POLYGO
452a0 4e 2d 74 79 70 65 20 67 65 6f 6d 65 74 72 79 20  N-type geometry 
452b0 5b 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  [if conversion i
452c0 73 20 70 6f 73 73 69 62 6c 65 5d 20 0a 2f 20 6f  s possible] ./ o
452d0 72 20 4e 55 4c 4c 20 69 6e 20 61 6e 79 20 6f 74  r NULL in any ot
452e0 68 65 72 20 63 61 73 65 0a 2a 2f 0a 20 20 20 20  her case.*/.    
452f0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
45300 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
45310 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
45320 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  len;.    unsigne
45330 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
45340 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74   = NULL;.    int
45350 20 70 74 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6e   pts;.    int ln
45360 73 3b 0a 20 20 20 20 69 6e 74 20 70 67 73 3b 0a  s;.    int pgs;.
45370 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
45380 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a  Ptr geo = NULL;.
45390 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
453a0 50 74 72 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c  Ptr geom2 = NULL
453b0 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
453c0 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
453d0 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
453e0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
453f0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
45400 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
45410 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
45420 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
45430 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
45440 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
45450 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
45460 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
45470 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
45480 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
45490 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
454a0 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
454b0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20  gv[0]);.    geo 
454c0 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
454d0 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
454e0 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
454f0 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71     if (!geo)..sq
45500 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
45510 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
45520 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
45530 20 63 61 73 74 5f 63 6f 75 6e 74 20 28 67 65 6f   cast_count (geo
45540 2c 20 26 70 74 73 2c 20 26 6c 6e 73 2c 20 26 70  , &pts, &lns, &p
45550 67 73 29 3b 0a 09 20 20 69 66 20 28 70 74 73 20  gs);..  if (pts 
45560 3d 3d 20 30 20 26 26 20 6c 6e 73 20 3d 3d 20 30  == 0 && lns == 0
45570 20 26 26 20 70 67 73 20 3d 3d 20 31 29 0a 09 20   && pgs == 1).. 
45580 20 20 20 7b 0a 09 09 67 65 6f 6d 32 20 3d 20 67     {...geom2 = g
45590 61 69 61 43 6c 6f 6e 65 47 65 6f 6d 43 6f 6c 6c  aiaCloneGeomColl
455a0 20 28 67 65 6f 29 3b 0a 09 09 67 65 6f 6d 32 2d   (geo);...geom2-
455b0 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69  >Srid = geo->Sri
455c0 64 3b 0a 09 09 67 65 6f 6d 32 2d 3e 44 65 63 6c  d;...geom2->Decl
455d0 61 72 65 64 54 79 70 65 20 3d 20 47 41 49 41 5f  aredType = GAIA_
455e0 50 4f 4c 59 47 4f 4e 3b 0a 09 09 67 61 69 61 54  POLYGON;...gaiaT
455f0 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  oSpatiaLiteBlobW
45600 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70 5f 72 65  kb (geom2, &p_re
45610 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 67  sult, &len);...g
45620 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
45630 28 67 65 6f 6d 32 29 3b 0a 09 09 73 71 6c 69 74  (geom2);...sqlit
45640 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28  e3_result_blob (
45650 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
45660 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09  t, len, free);..
45670 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20      }..  else.. 
45680 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
45690 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
456a0 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
456b0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
456c0 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69   (geo);.}..stati
456d0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 43 61 73 74  c void.fnct_Cast
456e0 54 6f 4d 75 6c 74 69 50 6f 69 6e 74 20 28 73 71  ToMultiPoint (sq
456f0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
45700 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
45710 63 2c 0a 09 09 20 20 20 20 20 20 20 73 71 6c 69  c,...       sqli
45720 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
45730 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
45740 74 69 6f 6e 3a 0a 2f 20 43 61 73 74 54 6f 4d 75  tion:./ CastToMu
45750 6c 74 69 50 6f 69 6e 74 28 42 4c 4f 42 20 65 6e  ltiPoint(BLOB en
45760 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a  coded geometry).
45770 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 4d 55  /./ returns a MU
45780 4c 54 49 50 4f 49 4e 54 2d 74 79 70 65 20 67 65  LTIPOINT-type ge
45790 6f 6d 65 74 72 79 20 5b 69 66 20 63 6f 6e 76 65  ometry [if conve
457a0 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c  rsion is possibl
457b0 65 5d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 6e  e] ./ or NULL in
457c0 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73 65 0a   any other case.
457d0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
457e0 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
457f0 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
45800 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20     int len;.    
45810 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
45820 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a  _result = NULL;.
45830 20 20 20 20 69 6e 74 20 70 74 73 3b 0a 20 20 20      int pts;.   
45840 20 69 6e 74 20 6c 6e 73 3b 0a 20 20 20 20 69 6e   int lns;.    in
45850 74 20 70 67 73 3b 0a 20 20 20 20 67 61 69 61 47  t pgs;.    gaiaG
45860 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
45870 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47   NULL;.    gaiaG
45880 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d 32  eomCollPtr geom2
45890 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49   = NULL;.    GAI
458a0 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
458b0 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
458c0 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
458d0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
458e0 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[0]) != SQLIT
458f0 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a  E_BLOB).      {.
45900 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
45910 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
45920 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
45930 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20     }.    p_blob 
45940 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
45950 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
45960 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
45970 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
45980 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
45990 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  tes (argv[0]);. 
459a0 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f     geo = gaiaFro
459b0 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
459c0 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
459d0 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
459e0 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  eo)..sqlite3_res
459f0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
45a00 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  t);.    else.   
45a10 20 20 20 7b 0a 09 20 20 63 61 73 74 5f 63 6f 75     {..  cast_cou
45a20 6e 74 20 28 67 65 6f 2c 20 26 70 74 73 2c 20 26  nt (geo, &pts, &
45a30 6c 6e 73 2c 20 26 70 67 73 29 3b 0a 09 20 20 69  lns, &pgs);..  i
45a40 66 20 28 70 74 73 20 3e 3d 20 31 20 26 26 20 6c  f (pts >= 1 && l
45a50 6e 73 20 3d 3d 20 30 20 26 26 20 70 67 73 20 3d  ns == 0 && pgs =
45a60 3d 20 30 29 0a 09 20 20 20 20 7b 0a 09 09 67 65  = 0)..    {...ge
45a70 6f 6d 32 20 3d 20 67 61 69 61 43 6c 6f 6e 65 47  om2 = gaiaCloneG
45a80 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09  eomColl (geo);..
45a90 09 67 65 6f 6d 32 2d 3e 53 72 69 64 20 3d 20 67  .geom2->Srid = g
45aa0 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 67 65 6f 6d  eo->Srid;...geom
45ab0 32 2d 3e 44 65 63 6c 61 72 65 64 54 79 70 65 20  2->DeclaredType 
45ac0 3d 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e  = GAIA_MULTIPOIN
45ad0 54 3b 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69  T;...gaiaToSpati
45ae0 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65  aLiteBlobWkb (ge
45af0 6f 6d 32 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  om2, &p_result, 
45b00 26 6c 65 6e 29 3b 0a 09 09 67 61 69 61 46 72 65  &len);...gaiaFre
45b10 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d 32  eGeomColl (geom2
45b20 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73  );...sqlite3_res
45b30 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78  ult_blob (contex
45b40 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e  t, p_result, len
45b50 2c 20 66 72 65 65 29 3b 0a 09 20 20 20 20 7d 0a  , free);..    }.
45b60 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73  .  else..      s
45b70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
45b80 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ll (context);.  
45b90 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72      }.    gaiaFr
45ba0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
45bb0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
45bc0 0a 66 6e 63 74 5f 43 61 73 74 54 6f 4d 75 6c 74  .fnct_CastToMult
45bd0 69 4c 69 6e 65 73 74 72 69 6e 67 20 28 73 71 6c  iLinestring (sql
45be0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
45bf0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
45c00 2c 0a 09 09 09 20 20 20 20 73 71 6c 69 74 65 33  ,....    sqlite3
45c10 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
45c20 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
45c30 6e 3a 0a 2f 20 43 61 73 74 54 6f 4d 75 6c 74 69  n:./ CastToMulti
45c40 4c 69 6e 65 73 74 72 69 6e 67 28 42 4c 4f 42 20  Linestring(BLOB 
45c50 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79  encoded geometry
45c60 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20  )././ returns a 
45c70 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 2d  MULTILINESTRING-
45c80 74 79 70 65 20 67 65 6f 6d 65 74 72 79 20 5b 69  type geometry [i
45c90 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  f conversion is 
45ca0 70 6f 73 73 69 62 6c 65 5d 20 0a 2f 20 6f 72 20  possible] ./ or 
45cb0 4e 55 4c 4c 20 69 6e 20 61 6e 79 20 6f 74 68 65  NULL in any othe
45cc0 72 20 63 61 73 65 0a 2a 2f 0a 20 20 20 20 75 6e  r case.*/.    un
45cd0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
45ce0 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
45cf0 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65  ytes;.    int le
45d00 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  n;.    unsigned 
45d10 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
45d20 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 70   NULL;.    int p
45d30 74 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6e 73 3b  ts;.    int lns;
45d40 0a 20 20 20 20 69 6e 74 20 70 67 73 3b 0a 20 20  .    int pgs;.  
45d50 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
45d60 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
45d70 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
45d80 72 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c 3b 0a  r geom2 = NULL;.
45d90 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
45da0 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
45db0 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
45dc0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
45dd0 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
45de0 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
45df0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
45e00 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
45e10 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
45e20 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
45e30 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
45e40 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
45e50 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
45e60 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[0]);.    n_b
45e70 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
45e80 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
45e90 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20  [0]);.    geo = 
45ea0 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
45eb0 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
45ec0 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
45ed0 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69   if (!geo)..sqli
45ee0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
45ef0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
45f00 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 63  lse.      {..  c
45f10 61 73 74 5f 63 6f 75 6e 74 20 28 67 65 6f 2c 20  ast_count (geo, 
45f20 26 70 74 73 2c 20 26 6c 6e 73 2c 20 26 70 67 73  &pts, &lns, &pgs
45f30 29 3b 0a 09 20 20 69 66 20 28 70 74 73 20 3d 3d  );..  if (pts ==
45f40 20 30 20 26 26 20 6c 6e 73 20 3e 3d 20 31 20 26   0 && lns >= 1 &
45f50 26 20 70 67 73 20 3d 3d 20 30 29 0a 09 20 20 20  & pgs == 0)..   
45f60 20 7b 0a 09 09 67 65 6f 6d 32 20 3d 20 67 61 69   {...geom2 = gai
45f70 61 43 6c 6f 6e 65 47 65 6f 6d 43 6f 6c 6c 20 28  aCloneGeomColl (
45f80 67 65 6f 29 3b 0a 09 09 67 65 6f 6d 32 2d 3e 53  geo);...geom2->S
45f90 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b  rid = geo->Srid;
45fa0 0a 09 09 67 65 6f 6d 32 2d 3e 44 65 63 6c 61 72  ...geom2->Declar
45fb0 65 64 54 79 70 65 20 3d 20 47 41 49 41 5f 4d 55  edType = GAIA_MU
45fc0 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 3b 0a 09  LTILINESTRING;..
45fd0 09 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74  .gaiaToSpatiaLit
45fe0 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 6d 32 2c  eBlobWkb (geom2,
45ff0 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e   &p_result, &len
46000 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f  );...gaiaFreeGeo
46010 6d 43 6f 6c 6c 20 28 67 65 6f 6d 32 29 3b 0a 09  mColl (geom2);..
46020 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
46030 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70  blob (context, p
46040 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72  _result, len, fr
46050 65 65 29 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65  ee);..    }..  e
46060 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lse..      sqlit
46070 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
46080 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
46090 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
460a0 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a  omColl (geo);.}.
460b0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
460c0 74 5f 43 61 73 74 54 6f 4d 75 6c 74 69 50 6f 6c  t_CastToMultiPol
460d0 79 67 6f 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f  ygon (sqlite3_co
460e0 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
460f0 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 73   int argc,.... s
46100 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
46110 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
46120 75 6e 63 74 69 6f 6e 3a 0a 2f 20 43 61 73 74 54  unction:./ CastT
46130 6f 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 28 42 4c  oMultiPolygon(BL
46140 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65  OB encoded geome
46150 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73  try)././ returns
46160 20 61 20 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 2d   a MULTIPOLYGON-
46170 74 79 70 65 20 67 65 6f 6d 65 74 72 79 20 5b 69  type geometry [i
46180 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  f conversion is 
46190 70 6f 73 73 69 62 6c 65 5d 20 0a 2f 20 6f 72 20  possible] ./ or 
461a0 4e 55 4c 4c 20 69 6e 20 61 6e 79 20 6f 74 68 65  NULL in any othe
461b0 72 20 63 61 73 65 0a 2a 2f 0a 20 20 20 20 75 6e  r case.*/.    un
461c0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
461d0 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
461e0 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65  ytes;.    int le
461f0 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  n;.    unsigned 
46200 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
46210 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 70   NULL;.    int p
46220 74 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6e 73 3b  ts;.    int lns;
46230 0a 20 20 20 20 69 6e 74 20 70 67 73 3b 0a 20 20  .    int pgs;.  
46240 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
46250 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
46260 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
46270 72 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c 3b 0a  r geom2 = NULL;.
46280 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
46290 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
462a0 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
462b0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
462c0 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
462d0 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
462e0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
462f0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
46300 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
46310 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
46320 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
46330 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
46340 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
46350 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[0]);.    n_b
46360 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
46370 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
46380 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20  [0]);.    geo = 
46390 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
463a0 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
463b0 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
463c0 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69   if (!geo)..sqli
463d0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
463e0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
463f0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 63  lse.      {..  c
46400 61 73 74 5f 63 6f 75 6e 74 20 28 67 65 6f 2c 20  ast_count (geo, 
46410 26 70 74 73 2c 20 26 6c 6e 73 2c 20 26 70 67 73  &pts, &lns, &pgs
46420 29 3b 0a 09 20 20 69 66 20 28 70 74 73 20 3d 3d  );..  if (pts ==
46430 20 30 20 26 26 20 6c 6e 73 20 3d 3d 20 30 20 26   0 && lns == 0 &
46440 26 20 70 67 73 20 3e 3d 20 31 29 0a 09 20 20 20  & pgs >= 1)..   
46450 20 7b 0a 09 09 67 65 6f 6d 32 20 3d 20 67 61 69   {...geom2 = gai
46460 61 43 6c 6f 6e 65 47 65 6f 6d 43 6f 6c 6c 20 28  aCloneGeomColl (
46470 67 65 6f 29 3b 0a 09 09 67 65 6f 6d 32 2d 3e 53  geo);...geom2->S
46480 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b  rid = geo->Srid;
46490 0a 09 09 67 65 6f 6d 32 2d 3e 44 65 63 6c 61 72  ...geom2->Declar
464a0 65 64 54 79 70 65 20 3d 20 47 41 49 41 5f 4d 55  edType = GAIA_MU
464b0 4c 54 49 50 4f 4c 59 47 4f 4e 3b 0a 09 09 67 61  LTIPOLYGON;...ga
464c0 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c  iaToSpatiaLiteBl
464d0 6f 62 57 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70  obWkb (geom2, &p
464e0 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a  _result, &len);.
464f0 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f  ..gaiaFreeGeomCo
46500 6c 6c 20 28 67 65 6f 6d 32 29 3b 0a 09 09 73 71  ll (geom2);...sq
46510 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
46520 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
46530 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29  sult, len, free)
46540 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65  ;..    }..  else
46550 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
46560 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
46570 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  text);.      }. 
46580 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
46590 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
465a0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 43  atic void.fnct_C
465b0 61 73 74 54 6f 47 65 6f 6d 65 74 72 79 43 6f 6c  astToGeometryCol
465c0 6c 65 63 74 69 6f 6e 20 28 73 71 6c 69 74 65 33  lection (sqlite3
465d0 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
465e0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09  xt, int argc,...
465f0 09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
46600 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
46610 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
46620 3a 0a 2f 20 43 61 73 74 54 6f 47 65 6f 6d 65 74  :./ CastToGeomet
46630 72 79 43 6f 6c 6c 65 63 74 69 6f 6e 28 42 4c 4f  ryCollection(BLO
46640 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74  B encoded geomet
46650 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20  ry)././ returns 
46660 61 20 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43  a GEOMETRYCOLLEC
46670 54 49 4f 4e 2d 74 79 70 65 20 67 65 6f 6d 65 74  TION-type geomet
46680 72 79 20 5b 69 66 20 63 6f 6e 76 65 72 73 69 6f  ry [if conversio
46690 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 5d 20 0a  n is possible] .
466a0 2f 20 6f 72 20 4e 55 4c 4c 20 69 6e 20 61 6e 79  / or NULL in any
466b0 20 6f 74 68 65 72 20 63 61 73 65 0a 2a 2f 0a 20   other case.*/. 
466c0 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
466d0 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e   *p_blob;.    in
466e0 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69  t n_bytes;.    i
466f0 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69  nt len;.    unsi
46700 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
46710 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ult = NULL;.    
46720 69 6e 74 20 70 74 73 3b 0a 20 20 20 20 69 6e 74  int pts;.    int
46730 20 6c 6e 73 3b 0a 20 20 20 20 69 6e 74 20 70 67   lns;.    int pg
46740 73 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  s;.    gaiaGeomC
46750 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c  ollPtr geo = NUL
46760 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
46770 6f 6c 6c 50 74 72 20 67 65 6f 6d 32 20 3d 20 4e  ollPtr geom2 = N
46780 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ULL;.    GAIA_UN
46790 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
467a0 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
467b0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
467c0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
467d0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
467e0 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
467f0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
46800 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
46810 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
46820 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
46830 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
46840 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
46850 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
46860 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
46870 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
46880 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
46890 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
468a0 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
468b0 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
468c0 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a  ;.    if (!geo).
468d0 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
468e0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
468f0 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
46900 0a 09 20 20 63 61 73 74 5f 63 6f 75 6e 74 20 28  ..  cast_count (
46910 67 65 6f 2c 20 26 70 74 73 2c 20 26 6c 6e 73 2c  geo, &pts, &lns,
46920 20 26 70 67 73 29 3b 0a 09 20 20 69 66 20 28 70   &pgs);..  if (p
46930 74 73 20 3e 3d 20 31 20 7c 7c 20 6c 6e 73 20 3e  ts >= 1 || lns >
46940 3d 20 31 20 7c 7c 20 70 67 73 20 3e 3d 20 31 29  = 1 || pgs >= 1)
46950 0a 09 20 20 20 20 7b 0a 09 09 67 65 6f 6d 32 20  ..    {...geom2 
46960 3d 20 67 61 69 61 43 6c 6f 6e 65 47 65 6f 6d 43  = gaiaCloneGeomC
46970 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09 09 67 65 6f  oll (geo);...geo
46980 6d 32 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e  m2->Srid = geo->
46990 53 72 69 64 3b 0a 09 09 67 65 6f 6d 32 2d 3e 44  Srid;...geom2->D
469a0 65 63 6c 61 72 65 64 54 79 70 65 20 3d 20 47 41  eclaredType = GA
469b0 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45  IA_GEOMETRYCOLLE
469c0 43 54 49 4f 4e 3b 0a 09 09 67 61 69 61 54 6f 53  CTION;...gaiaToS
469d0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
469e0 20 28 67 65 6f 6d 32 2c 20 26 70 5f 72 65 73 75   (geom2, &p_resu
469f0 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 67 61 69  lt, &len);...gai
46a00 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
46a10 65 6f 6d 32 29 3b 0a 09 09 73 71 6c 69 74 65 33  eom2);...sqlite3
46a20 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
46a30 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
46a40 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20   len, free);..  
46a50 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20    }..  else..   
46a60 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
46a70 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
46a80 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  ;.      }.    ga
46a90 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
46aa0 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  geo);.}..static 
46ab0 76 6f 69 64 0a 66 6e 63 74 5f 43 61 73 74 54 6f  void.fnct_CastTo
46ac0 4d 75 6c 74 69 20 28 73 71 6c 69 74 65 33 5f 63  Multi (sqlite3_c
46ad0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
46ae0 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
46af0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
46b00 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
46b10 74 69 6f 6e 3a 0a 2f 20 43 61 73 74 54 6f 4d 75  tion:./ CastToMu
46b20 6c 74 69 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64  lti(BLOB encoded
46b30 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
46b40 65 74 75 72 6e 73 20 61 20 4d 55 4c 54 49 50 4f  eturns a MULTIPO
46b50 49 4e 54 2c 20 4d 55 4c 54 49 4c 49 4e 45 53 54  INT, MULTILINEST
46b60 52 49 4e 47 2c 20 4d 55 4c 54 49 50 4f 4c 59 47  RING, MULTIPOLYG
46b70 4f 4e 20 6f 72 0a 2f 20 47 45 4f 4d 45 54 52 59  ON or./ GEOMETRY
46b80 43 4f 4c 4c 45 43 54 49 4f 4e 2d 74 79 70 65 20  COLLECTION-type 
46b90 67 65 6f 6d 65 74 72 79 20 5b 69 66 20 63 6f 6e  geometry [if con
46ba0 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69  version is possi
46bb0 62 6c 65 5d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20  ble] ./ or NULL 
46bc0 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73  in any other cas
46bd0 65 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  e.*/.    unsigne
46be0 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
46bf0 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
46c00 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  .    int len;.  
46c10 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
46c20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  *p_result = NULL
46c30 3b 0a 20 20 20 20 69 6e 74 20 70 74 73 3b 0a 20  ;.    int pts;. 
46c40 20 20 20 69 6e 74 20 6c 6e 73 3b 0a 20 20 20 20     int lns;.    
46c50 69 6e 74 20 70 67 73 3b 0a 20 20 20 20 67 61 69  int pgs;.    gai
46c60 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
46c70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
46c80 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
46c90 6d 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47  m2 = NULL;.    G
46ca0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
46cb0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
46cc0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
46cd0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
46ce0 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
46cf0 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
46d00 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
46d10 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
46d20 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
46d30 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f       }.    p_blo
46d40 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
46d50 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
46d60 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30  lue_blob (argv[0
46d70 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20  ]);.    n_bytes 
46d80 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
46d90 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b  bytes (argv[0]);
46da0 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46  .    geo = gaiaF
46db0 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  romSpatiaLiteBlo
46dc0 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f  bWkb (p_blob, n_
46dd0 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28  bytes);.    if (
46de0 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72  !geo)..sqlite3_r
46df0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
46e00 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20  ext);.    else. 
46e10 20 20 20 20 20 7b 0a 09 20 20 63 61 73 74 5f 63       {..  cast_c
46e20 6f 75 6e 74 20 28 67 65 6f 2c 20 26 70 74 73 2c  ount (geo, &pts,
46e30 20 26 6c 6e 73 2c 20 26 70 67 73 29 3b 0a 09 20   &lns, &pgs);.. 
46e40 20 69 66 20 28 70 74 73 20 3e 3d 20 31 20 7c 7c   if (pts >= 1 ||
46e50 20 6c 6e 73 20 3e 3d 20 31 20 7c 7c 20 70 67 73   lns >= 1 || pgs
46e60 20 3e 3d 20 31 29 0a 09 20 20 20 20 7b 0a 09 09   >= 1)..    {...
46e70 67 65 6f 6d 32 20 3d 20 67 61 69 61 43 6c 6f 6e  geom2 = gaiaClon
46e80 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b  eGeomColl (geo);
46e90 0a 09 09 67 65 6f 6d 32 2d 3e 53 72 69 64 20 3d  ...geom2->Srid =
46ea0 20 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 69 66   geo->Srid;...if
46eb0 20 28 70 74 73 20 3e 3d 20 31 20 26 26 20 6c 6e   (pts >= 1 && ln
46ec0 73 20 3d 3d 20 30 20 26 26 20 70 67 73 20 3d 3d  s == 0 && pgs ==
46ed0 20 30 29 0a 09 09 20 20 20 20 67 65 6f 6d 32 2d   0)...    geom2-
46ee0 3e 44 65 63 6c 61 72 65 64 54 79 70 65 20 3d 20  >DeclaredType = 
46ef0 47 41 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 3b  GAIA_MULTIPOINT;
46f00 0a 09 09 65 6c 73 65 20 69 66 20 28 70 74 73 20  ...else if (pts 
46f10 3d 3d 20 30 20 26 26 20 6c 6e 73 20 3e 3d 20 31  == 0 && lns >= 1
46f20 20 26 26 20 70 67 73 20 3d 3d 20 30 29 0a 09 09   && pgs == 0)...
46f30 20 20 20 20 67 65 6f 6d 32 2d 3e 44 65 63 6c 61      geom2->Decla
46f40 72 65 64 54 79 70 65 20 3d 20 47 41 49 41 5f 4d  redType = GAIA_M
46f50 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 3b 0a  ULTILINESTRING;.
46f60 09 09 65 6c 73 65 20 69 66 20 28 70 74 73 20 3d  ..else if (pts =
46f70 3d 20 30 20 26 26 20 6c 6e 73 20 3d 3d 20 30 20  = 0 && lns == 0 
46f80 26 26 20 70 67 73 20 3e 3d 20 31 29 0a 09 09 20  && pgs >= 1)... 
46f90 20 20 20 67 65 6f 6d 32 2d 3e 44 65 63 6c 61 72     geom2->Declar
46fa0 65 64 54 79 70 65 20 3d 20 47 41 49 41 5f 4d 55  edType = GAIA_MU
46fb0 4c 54 49 50 4f 4c 59 47 4f 4e 3b 0a 09 09 65 6c  LTIPOLYGON;...el
46fc0 73 65 0a 09 09 20 20 20 20 67 65 6f 6d 32 2d 3e  se...    geom2->
46fd0 44 65 63 6c 61 72 65 64 54 79 70 65 20 3d 20 47  DeclaredType = G
46fe0 41 49 41 5f 47 45 4f 4d 45 54 52 59 43 4f 4c 4c  AIA_GEOMETRYCOLL
46ff0 45 43 54 49 4f 4e 3b 0a 09 09 67 61 69 61 54 6f  ECTION;...gaiaTo
47000 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
47010 62 20 28 67 65 6f 6d 32 2c 20 26 70 5f 72 65 73  b (geom2, &p_res
47020 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 67 61  ult, &len);...ga
47030 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
47040 67 65 6f 6d 32 29 3b 0a 09 09 73 71 6c 69 74 65  geom2);...sqlite
47050 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63  3_result_blob (c
47060 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74  ontext, p_result
47070 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20  , len, free);.. 
47080 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20     }..  else..  
47090 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
470a0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
470b0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67  );.      }.    g
470c0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
470d0 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  (geo);.}..static
470e0 20 76 6f 69 64 0a 66 6e 63 74 5f 43 61 73 74 54   void.fnct_CastT
470f0 6f 53 69 6e 67 6c 65 20 28 73 71 6c 69 74 65 33  oSingle (sqlite3
47100 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
47110 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
47120 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
47130 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
47140 6e 63 74 69 6f 6e 3a 0a 2f 20 43 61 73 74 54 6f  nction:./ CastTo
47150 53 69 6e 67 6c 65 28 42 4c 4f 42 20 65 6e 63 6f  Single(BLOB enco
47160 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a  ded geometry)./.
47170 2f 20 72 65 74 75 72 6e 73 20 61 20 50 4f 49 4e  / returns a POIN
47180 54 2c 20 4c 49 4e 45 53 54 52 49 4e 47 20 6f 72  T, LINESTRING or
47190 20 50 4f 4c 59 47 4f 4e 2d 74 79 70 65 20 67 65   POLYGON-type ge
471a0 6f 6d 65 74 72 79 20 5b 69 66 20 63 6f 6e 76 65  ometry [if conve
471b0 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c  rsion is possibl
471c0 65 5d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 6e  e] ./ or NULL in
471d0 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73 65 0a   any other case.
471e0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
471f0 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
47200 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
47210 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20     int len;.    
47220 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
47230 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a  _result = NULL;.
47240 20 20 20 20 69 6e 74 20 70 74 73 3b 0a 20 20 20      int pts;.   
47250 20 69 6e 74 20 6c 6e 73 3b 0a 20 20 20 20 69 6e   int lns;.    in
47260 74 20 70 67 73 3b 0a 20 20 20 20 69 6e 74 20 6f  t pgs;.    int o
47270 6b 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  k;.    gaiaGeomC
47280 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c  ollPtr geo = NUL
47290 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
472a0 6f 6c 6c 50 74 72 20 67 65 6f 6d 32 20 3d 20 4e  ollPtr geom2 = N
472b0 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ULL;.    GAIA_UN
472c0 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
472d0 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
472e0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
472f0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
47300 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
47310 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
47320 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
47330 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
47340 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
47350 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
47360 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
47370 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
47380 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
47390 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
473a0 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
473b0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
473c0 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
473d0 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
473e0 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
473f0 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a  ;.    if (!geo).
47400 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
47410 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
47420 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
47430 0a 09 20 20 63 61 73 74 5f 63 6f 75 6e 74 20 28  ..  cast_count (
47440 67 65 6f 2c 20 26 70 74 73 2c 20 26 6c 6e 73 2c  geo, &pts, &lns,
47450 20 26 70 67 73 29 3b 0a 09 20 20 6f 6b 20 3d 20   &pgs);..  ok = 
47460 30 3b 0a 09 20 20 69 66 20 28 70 74 73 20 3d 3d  0;..  if (pts ==
47470 20 31 20 26 26 20 6c 6e 73 20 3d 3d 20 30 20 26   1 && lns == 0 &
47480 26 20 70 67 73 20 3d 3d 20 30 29 0a 09 20 20 20  & pgs == 0)..   
47490 20 20 20 6f 6b 20 3d 20 31 3b 0a 09 20 20 69 66     ok = 1;..  if
474a0 20 28 70 74 73 20 3d 3d 20 30 20 26 26 20 6c 6e   (pts == 0 && ln
474b0 73 20 3d 3d 20 31 20 26 26 20 70 67 73 20 3d 3d  s == 1 && pgs ==
474c0 20 30 29 0a 09 20 20 20 20 20 20 6f 6b 20 3d 20   0)..      ok = 
474d0 31 3b 0a 09 20 20 69 66 20 28 70 74 73 20 3d 3d  1;..  if (pts ==
474e0 20 30 20 26 26 20 6c 6e 73 20 3d 3d 20 30 20 26   0 && lns == 0 &
474f0 26 20 70 67 73 20 3d 3d 20 31 29 0a 09 20 20 20  & pgs == 1)..   
47500 20 20 20 6f 6b 20 3d 20 31 3b 0a 09 20 20 69 66     ok = 1;..  if
47510 20 28 6f 6b 29 0a 09 20 20 20 20 7b 0a 09 09 67   (ok)..    {...g
47520 65 6f 6d 32 20 3d 20 67 61 69 61 43 6c 6f 6e 65  eom2 = gaiaClone
47530 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a  GeomColl (geo);.
47540 09 09 67 65 6f 6d 32 2d 3e 53 72 69 64 20 3d 20  ..geom2->Srid = 
47550 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 69 66 20  geo->Srid;...if 
47560 28 70 74 73 20 3d 3d 20 31 29 0a 09 09 20 20 20  (pts == 1)...   
47570 20 67 65 6f 6d 32 2d 3e 44 65 63 6c 61 72 65 64   geom2->Declared
47580 54 79 70 65 20 3d 20 47 41 49 41 5f 50 4f 49 4e  Type = GAIA_POIN
47590 54 3b 0a 09 09 65 6c 73 65 20 69 66 20 28 6c 6e  T;...else if (ln
475a0 73 20 3d 3d 20 31 29 0a 09 09 20 20 20 20 67 65  s == 1)...    ge
475b0 6f 6d 32 2d 3e 44 65 63 6c 61 72 65 64 54 79 70  om2->DeclaredTyp
475c0 65 20 3d 20 47 41 49 41 5f 4c 49 4e 45 53 54 52  e = GAIA_LINESTR
475d0 49 4e 47 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20  ING;...else...  
475e0 20 20 67 65 6f 6d 32 2d 3e 44 65 63 6c 61 72 65    geom2->Declare
475f0 64 54 79 70 65 20 3d 20 47 41 49 41 5f 50 4f 4c  dType = GAIA_POL
47600 59 47 4f 4e 3b 0a 09 09 67 61 69 61 54 6f 53 70  YGON;...gaiaToSp
47610 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
47620 28 67 65 6f 6d 32 2c 20 26 70 5f 72 65 73 75 6c  (geom2, &p_resul
47630 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 67 61 69 61  t, &len);...gaia
47640 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
47650 6f 6d 32 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f  om2);...sqlite3_
47660 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e  result_blob (con
47670 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20  text, p_result, 
47680 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 20  len, free);..   
47690 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20   }..  else..    
476a0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
476b0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
476c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
476d0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
476e0 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  eo);.}..static v
476f0 6f 69 64 0a 66 6e 63 74 5f 43 61 73 74 54 6f 58  oid.fnct_CastToX
47700 59 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  Y (sqlite3_conte
47710 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
47720 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
47730 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
47740 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
47750 3a 0a 2f 20 43 61 73 74 54 6f 58 59 28 42 4c 4f  :./ CastToXY(BLO
47760 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74  B encoded geomet
47770 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20  ry)././ returns 
47780 61 6e 20 58 59 2d 64 69 6d 65 6e 73 69 6f 6e 20  an XY-dimension 
47790 47 65 6f 6d 65 74 72 79 20 5b 69 66 20 63 6f 6e  Geometry [if con
477a0 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69  version is possi
477b0 62 6c 65 5d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20  ble] ./ or NULL 
477c0 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73  in any other cas
477d0 65 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  e.*/.    unsigne
477e0 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
477f0 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
47800 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  .    int len;.  
47810 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
47820 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  *p_result = NULL
47830 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
47840 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
47850 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
47860 6c 6c 50 74 72 20 67 65 6f 6d 32 20 3d 20 4e 55  llPtr geom2 = NU
47870 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  LL;.    GAIA_UNU
47880 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
47890 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
478a0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
478b0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
478c0 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
478d0 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
478e0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
478f0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
47900 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
47910 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
47920 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
47930 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
47940 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
47950 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
47960 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
47970 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
47980 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  o = gaiaFromSpat
47990 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
479a0 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
479b0 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09  .    if (!geo)..
479c0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
479d0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
479e0 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
479f0 09 20 20 67 65 6f 6d 32 20 3d 20 67 61 69 61 43  .  geom2 = gaiaC
47a00 61 73 74 47 65 6f 6d 43 6f 6c 6c 54 6f 58 59 20  astGeomCollToXY 
47a10 28 67 65 6f 29 3b 0a 09 20 20 69 66 20 28 67 65  (geo);..  if (ge
47a20 6f 6d 32 29 0a 09 20 20 20 20 7b 0a 09 09 67 65  om2)..    {...ge
47a30 6f 6d 32 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d  om2->Srid = geo-
47a40 3e 53 72 69 64 3b 0a 09 09 67 61 69 61 54 6f 53  >Srid;...gaiaToS
47a50 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
47a60 20 28 67 65 6f 6d 32 2c 20 26 70 5f 72 65 73 75   (geom2, &p_resu
47a70 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 67 61 69  lt, &len);...gai
47a80 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
47a90 65 6f 6d 32 29 3b 0a 09 09 73 71 6c 69 74 65 33  eom2);...sqlite3
47aa0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
47ab0 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
47ac0 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20   len, free);..  
47ad0 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20    }..  else..   
47ae0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
47af0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
47b00 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  ;.      }.    ga
47b10 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
47b20 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  geo);.}..static 
47b30 76 6f 69 64 0a 66 6e 63 74 5f 43 61 73 74 54 6f  void.fnct_CastTo
47b40 58 59 5a 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  XYZ (sqlite3_con
47b50 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
47b60 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
47b70 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
47b80 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
47b90 6f 6e 3a 0a 2f 20 43 61 73 74 54 6f 58 59 28 42  on:./ CastToXY(B
47ba0 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d  LOB encoded geom
47bb0 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  etry)././ return
47bc0 73 20 61 6e 20 58 59 2d 64 69 6d 65 6e 73 69 6f  s an XY-dimensio
47bd0 6e 20 47 65 6f 6d 65 74 72 79 20 5b 69 66 20 63  n Geometry [if c
47be0 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73  onversion is pos
47bf0 73 69 62 6c 65 5d 20 0a 2f 20 6f 72 20 4e 55 4c  sible] ./ or NUL
47c00 4c 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 63  L in any other c
47c10 61 73 65 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67  ase.*/.    unsig
47c20 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62  ned char *p_blob
47c30 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65  ;.    int n_byte
47c40 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  s;.    int len;.
47c50 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
47c60 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55  r *p_result = NU
47c70 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
47c80 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
47c90 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
47ca0 43 6f 6c 6c 50 74 72 20 67 65 6f 6d 32 20 3d 20  CollPtr geom2 = 
47cb0 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55  NULL;.    GAIA_U
47cc0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
47cd0 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
47ce0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
47cf0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
47d00 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
47d10 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
47d20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
47d30 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
47d40 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
47d50 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
47d60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
47d70 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
47d80 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
47d90 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
47da0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
47db0 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
47dc0 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  geo = gaiaFromSp
47dd0 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
47de0 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
47df0 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29  );.    if (!geo)
47e00 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
47e10 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
47e20 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
47e30 7b 0a 09 20 20 67 65 6f 6d 32 20 3d 20 67 61 69  {..  geom2 = gai
47e40 61 43 61 73 74 47 65 6f 6d 43 6f 6c 6c 54 6f 58  aCastGeomCollToX
47e50 59 5a 20 28 67 65 6f 29 3b 0a 09 20 20 69 66 20  YZ (geo);..  if 
47e60 28 67 65 6f 6d 32 29 0a 09 20 20 20 20 7b 0a 09  (geom2)..    {..
47e70 09 67 65 6f 6d 32 2d 3e 53 72 69 64 20 3d 20 67  .geom2->Srid = g
47e80 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 67 61 69 61  eo->Srid;...gaia
47e90 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
47ea0 57 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70 5f 72  Wkb (geom2, &p_r
47eb0 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09  esult, &len);...
47ec0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
47ed0 20 28 67 65 6f 6d 32 29 3b 0a 09 09 73 71 6c 69   (geom2);...sqli
47ee0 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
47ef0 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
47f00 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
47f10 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09  .    }..  else..
47f20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
47f30 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
47f40 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  xt);.      }.   
47f50 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
47f60 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74  l (geo);.}..stat
47f70 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 43 61 73  ic void.fnct_Cas
47f80 74 54 6f 58 59 4d 20 28 73 71 6c 69 74 65 33 5f  tToXYM (sqlite3_
47f90 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
47fa0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
47fb0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
47fc0 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
47fd0 63 74 69 6f 6e 3a 0a 2f 20 43 61 73 74 54 6f 58  ction:./ CastToX
47fe0 59 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67  Y(BLOB encoded g
47ff0 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74  eometry)././ ret
48000 75 72 6e 73 20 61 6e 20 58 59 4d 2d 64 69 6d 65  urns an XYM-dime
48010 6e 73 69 6f 6e 20 47 65 6f 6d 65 74 72 79 20 5b  nsion Geometry [
48020 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  if conversion is
48030 20 70 6f 73 73 69 62 6c 65 5d 20 0a 2f 20 6f 72   possible] ./ or
48040 20 4e 55 4c 4c 20 69 6e 20 61 6e 79 20 6f 74 68   NULL in any oth
48050 65 72 20 63 61 73 65 0a 2a 2f 0a 20 20 20 20 75  er case.*/.    u
48060 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
48070 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
48080 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c  bytes;.    int l
48090 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  en;.    unsigned
480a0 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
480b0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
480c0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
480d0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
480e0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d  GeomCollPtr geom
480f0 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41  2 = NULL;.    GA
48100 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
48110 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
48120 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
48130 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
48140 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
48150 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
48160 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
48170 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
48180 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
48190 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
481a0 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
481b0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
481c0 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
481d0 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
481e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
481f0 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
48200 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
48210 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
48220 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
48230 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
48240 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  geo)..sqlite3_re
48250 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
48260 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  xt);.    else.  
48270 20 20 20 20 7b 0a 09 20 20 67 65 6f 6d 32 20 3d      {..  geom2 =
48280 20 67 61 69 61 43 61 73 74 47 65 6f 6d 43 6f 6c   gaiaCastGeomCol
48290 6c 54 6f 58 59 4d 20 28 67 65 6f 29 3b 0a 09 20  lToXYM (geo);.. 
482a0 20 69 66 20 28 67 65 6f 6d 32 29 0a 09 20 20 20   if (geom2)..   
482b0 20 7b 0a 09 09 67 65 6f 6d 32 2d 3e 53 72 69 64   {...geom2->Srid
482c0 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09   = geo->Srid;...
482d0 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65  gaiaToSpatiaLite
482e0 42 6c 6f 62 57 6b 62 20 28 67 65 6f 6d 32 2c 20  BlobWkb (geom2, 
482f0 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29  &p_result, &len)
48300 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d  ;...gaiaFreeGeom
48310 43 6f 6c 6c 20 28 67 65 6f 6d 32 29 3b 0a 09 09  Coll (geom2);...
48320 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
48330 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
48340 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
48350 65 29 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c  e);..    }..  el
48360 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  se..      sqlite
48370 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
48380 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  ontext);.      }
48390 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f  .    gaiaFreeGeo
483a0 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a  mColl (geo);.}..
483b0 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
483c0 5f 43 61 73 74 54 6f 58 59 5a 4d 20 28 73 71 6c  _CastToXYZM (sql
483d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
483e0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
483f0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
48400 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
48410 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 43 61  L function:./ Ca
48420 73 74 54 6f 58 59 28 42 4c 4f 42 20 65 6e 63 6f  stToXY(BLOB enco
48430 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a  ded geometry)./.
48440 2f 20 72 65 74 75 72 6e 73 20 61 6e 20 58 59 5a  / returns an XYZ
48450 4d 2d 64 69 6d 65 6e 73 69 6f 6e 20 47 65 6f 6d  M-dimension Geom
48460 65 74 72 79 20 5b 69 66 20 63 6f 6e 76 65 72 73  etry [if convers
48470 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 5d  ion is possible]
48480 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 6e 20 61   ./ or NULL in a
48490 6e 79 20 6f 74 68 65 72 20 63 61 73 65 0a 2a 2f  ny other case.*/
484a0 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
484b0 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20  ar *p_blob;.    
484c0 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
484d0 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e   int len;.    un
484e0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72  signed char *p_r
484f0 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20  esult = NULL;.  
48500 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
48510 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
48520 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
48530 72 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c 3b 0a  r geom2 = NULL;.
48540 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
48550 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
48560 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
48570 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
48580 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
48590 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
485a0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
485b0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
485c0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
485d0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
485e0 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
485f0 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
48600 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
48610 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[0]);.    n_b
48620 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
48630 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
48640 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20  [0]);.    geo = 
48650 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
48660 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
48670 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
48680 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69   if (!geo)..sqli
48690 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
486a0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
486b0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 67  lse.      {..  g
486c0 65 6f 6d 32 20 3d 20 67 61 69 61 43 61 73 74 47  eom2 = gaiaCastG
486d0 65 6f 6d 43 6f 6c 6c 54 6f 58 59 5a 4d 20 28 67  eomCollToXYZM (g
486e0 65 6f 29 3b 0a 09 20 20 69 66 20 28 67 65 6f 6d  eo);..  if (geom
486f0 32 29 0a 09 20 20 20 20 7b 0a 09 09 67 65 6f 6d  2)..    {...geom
48700 32 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53  2->Srid = geo->S
48710 72 69 64 3b 0a 09 09 67 61 69 61 54 6f 53 70 61  rid;...gaiaToSpa
48720 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
48730 67 65 6f 6d 32 2c 20 26 70 5f 72 65 73 75 6c 74  geom2, &p_result
48740 2c 20 26 6c 65 6e 29 3b 0a 09 09 67 61 69 61 46  , &len);...gaiaF
48750 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
48760 6d 32 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72  m2);...sqlite3_r
48770 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74  esult_blob (cont
48780 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c  ext, p_result, l
48790 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 20 20  en, free);..    
487a0 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20  }..  else..     
487b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
487c0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
487d0 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61        }.    gaia
487e0 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
487f0 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  o);.}..static vo
48800 69 64 0a 66 6e 63 74 5f 45 78 74 72 61 63 74 4d  id.fnct_ExtractM
48810 75 6c 74 69 50 6f 69 6e 74 20 28 73 71 6c 69 74  ultiPoint (sqlit
48820 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
48830 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a  text, int argc,.
48840 09 09 09 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ...sqlite3_value
48850 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
48860 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 45  QL function:./ E
48870 78 74 72 61 63 74 4d 75 6c 74 69 50 6f 69 6e 74  xtractMultiPoint
48880 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65  (BLOB encoded ge
48890 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75  ometry)././ retu
488a0 72 6e 73 20 61 20 4d 55 4c 54 49 50 4f 49 4e 54  rns a MULTIPOINT
488b0 2d 74 79 70 65 20 67 65 6f 6d 65 74 72 79 20 5b  -type geometry [
488c0 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  if conversion is
488d0 20 70 6f 73 73 69 62 6c 65 5d 20 0a 2f 20 6f 72   possible] ./ or
488e0 20 4e 55 4c 4c 20 69 6e 20 61 6e 79 20 6f 74 68   NULL in any oth
488f0 65 72 20 63 61 73 65 0a 2a 2f 0a 20 20 20 20 75  er case.*/.    u
48900 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
48910 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
48920 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c  bytes;.    int l
48930 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  en;.    unsigned
48940 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
48950 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20  = NULL;.    int 
48960 70 74 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6e 73  pts;.    int lns
48970 3b 0a 20 20 20 20 69 6e 74 20 70 67 73 3b 0a 20  ;.    int pgs;. 
48980 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
48990 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20  tr geo = NULL;. 
489a0 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
489b0 74 72 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c 3b  tr geom2 = NULL;
489c0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
489d0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
489e0 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
489f0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
48a00 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
48a10 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
48a20 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
48a30 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
48a40 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
48a50 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
48a60 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
48a70 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
48a80 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
48a90 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
48aa0 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
48ab0 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
48ac0 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
48ad0 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
48ae0 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
48af0 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
48b00 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
48b10 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
48b20 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
48b30 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
48b40 63 61 73 74 5f 63 6f 75 6e 74 20 28 67 65 6f 2c  cast_count (geo,
48b50 20 26 70 74 73 2c 20 26 6c 6e 73 2c 20 26 70 67   &pts, &lns, &pg
48b60 73 29 3b 0a 09 20 20 69 66 20 28 70 74 73 20 3e  s);..  if (pts >
48b70 3d 20 31 29 0a 09 20 20 20 20 7b 0a 09 09 67 65  = 1)..    {...ge
48b80 6f 6d 32 20 3d 20 67 61 69 61 43 6c 6f 6e 65 47  om2 = gaiaCloneG
48b90 65 6f 6d 43 6f 6c 6c 50 6f 69 6e 74 73 20 28 67  eomCollPoints (g
48ba0 65 6f 29 3b 0a 09 09 67 65 6f 6d 32 2d 3e 53 72  eo);...geom2->Sr
48bb0 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b 0a  id = geo->Srid;.
48bc0 09 09 67 65 6f 6d 32 2d 3e 44 65 63 6c 61 72 65  ..geom2->Declare
48bd0 64 54 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c  dType = GAIA_MUL
48be0 54 49 50 4f 49 4e 54 3b 0a 09 09 67 61 69 61 54  TIPOINT;...gaiaT
48bf0 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  oSpatiaLiteBlobW
48c00 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70 5f 72 65  kb (geom2, &p_re
48c10 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 67  sult, &len);...g
48c20 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
48c30 28 67 65 6f 6d 32 29 3b 0a 09 09 73 71 6c 69 74  (geom2);...sqlit
48c40 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28  e3_result_blob (
48c50 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
48c60 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09  t, len, free);..
48c70 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20      }..  else.. 
48c80 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
48c90 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
48ca0 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
48cb0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
48cc0 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69   (geo);.}..stati
48cd0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 45 78 74 72  c void.fnct_Extr
48ce0 61 63 74 4d 75 6c 74 69 4c 69 6e 65 73 74 72 69  actMultiLinestri
48cf0 6e 67 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ng (sqlite3_cont
48d00 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
48d10 6e 74 20 61 72 67 63 2c 0a 09 09 09 20 20 20 20  nt argc,....    
48d20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
48d30 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
48d40 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 45 78 74   function:./ Ext
48d50 72 61 63 74 4d 75 6c 74 69 4c 69 6e 65 73 74 72  ractMultiLinestr
48d60 69 6e 67 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64  ing(BLOB encoded
48d70 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
48d80 65 74 75 72 6e 73 20 61 20 4d 55 4c 54 49 4c 49  eturns a MULTILI
48d90 4e 45 53 54 52 49 4e 47 2d 74 79 70 65 20 67 65  NESTRING-type ge
48da0 6f 6d 65 74 72 79 20 5b 69 66 20 63 6f 6e 76 65  ometry [if conve
48db0 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c  rsion is possibl
48dc0 65 5d 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 6e  e] ./ or NULL in
48dd0 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73 65 0a   any other case.
48de0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
48df0 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
48e00 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
48e10 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20     int len;.    
48e20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
48e30 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a  _result = NULL;.
48e40 20 20 20 20 69 6e 74 20 70 74 73 3b 0a 20 20 20      int pts;.   
48e50 20 69 6e 74 20 6c 6e 73 3b 0a 20 20 20 20 69 6e   int lns;.    in
48e60 74 20 70 67 73 3b 0a 20 20 20 20 67 61 69 61 47  t pgs;.    gaiaG
48e70 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
48e80 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47   NULL;.    gaiaG
48e90 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d 32  eomCollPtr geom2
48ea0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49   = NULL;.    GAI
48eb0 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
48ec0 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
48ed0 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
48ee0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
48ef0 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[0]) != SQLIT
48f00 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a  E_BLOB).      {.
48f10 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
48f20 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
48f30 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
48f40 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20     }.    p_blob 
48f50 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
48f60 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
48f70 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
48f80 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
48f90 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
48fa0 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  tes (argv[0]);. 
48fb0 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f     geo = gaiaFro
48fc0 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
48fd0 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
48fe0 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
48ff0 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  eo)..sqlite3_res
49000 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
49010 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  t);.    else.   
49020 20 20 20 7b 0a 09 20 20 63 61 73 74 5f 63 6f 75     {..  cast_cou
49030 6e 74 20 28 67 65 6f 2c 20 26 70 74 73 2c 20 26  nt (geo, &pts, &
49040 6c 6e 73 2c 20 26 70 67 73 29 3b 0a 09 20 20 69  lns, &pgs);..  i
49050 66 20 28 6c 6e 73 20 3e 3d 20 31 29 0a 09 20 20  f (lns >= 1)..  
49060 20 20 7b 0a 09 09 67 65 6f 6d 32 20 3d 20 67 61    {...geom2 = ga
49070 69 61 43 6c 6f 6e 65 47 65 6f 6d 43 6f 6c 6c 4c  iaCloneGeomCollL
49080 69 6e 65 73 74 72 69 6e 67 73 20 28 67 65 6f 29  inestrings (geo)
49090 3b 0a 09 09 67 65 6f 6d 32 2d 3e 53 72 69 64 20  ;...geom2->Srid 
490a0 3d 20 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 67  = geo->Srid;...g
490b0 65 6f 6d 32 2d 3e 44 65 63 6c 61 72 65 64 54 79  eom2->DeclaredTy
490c0 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c 54 49 4c  pe = GAIA_MULTIL
490d0 49 4e 45 53 54 52 49 4e 47 3b 0a 09 09 67 61 69  INESTRING;...gai
490e0 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  aToSpatiaLiteBlo
490f0 62 57 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70 5f  bWkb (geom2, &p_
49100 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09  result, &len);..
49110 09 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c  .gaiaFreeGeomCol
49120 6c 20 28 67 65 6f 6d 32 29 3b 0a 09 09 73 71 6c  l (geom2);...sql
49130 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
49140 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73   (context, p_res
49150 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b  ult, len, free);
49160 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a  ..    }..  else.
49170 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
49180 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
49190 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ext);.      }.  
491a0 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
491b0 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61  ll (geo);.}..sta
491c0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 45 78  tic void.fnct_Ex
491d0 74 72 61 63 74 4d 75 6c 74 69 50 6f 6c 79 67 6f  tractMultiPolygo
491e0 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  n (sqlite3_conte
491f0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
49200 74 20 61 72 67 63 2c 0a 09 09 09 20 20 73 71 6c  t argc,....  sql
49210 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
49220 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
49230 63 74 69 6f 6e 3a 0a 2f 20 45 78 74 72 61 63 74  ction:./ Extract
49240 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 28 42 4c 4f  MultiPolygon(BLO
49250 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74  B encoded geomet
49260 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20  ry)././ returns 
49270 61 20 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 2d 74  a MULTIPOLYGON-t
49280 79 70 65 20 67 65 6f 6d 65 74 72 79 20 5b 69 66  ype geometry [if
49290 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
492a0 6f 73 73 69 62 6c 65 5d 20 0a 2f 20 6f 72 20 4e  ossible] ./ or N
492b0 55 4c 4c 20 69 6e 20 61 6e 79 20 6f 74 68 65 72  ULL in any other
492c0 20 63 61 73 65 0a 2a 2f 0a 20 20 20 20 75 6e 73   case.*/.    uns
492d0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
492e0 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  ob;.    int n_by
492f0 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e  tes;.    int len
49300 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
49310 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20  har *p_result = 
49320 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 70 74  NULL;.    int pt
49330 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6e 73 3b 0a  s;.    int lns;.
49340 20 20 20 20 69 6e 74 20 70 67 73 3b 0a 20 20 20      int pgs;.   
49350 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
49360 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
49370 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
49380 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c 3b 0a 20   geom2 = NULL;. 
49390 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
493a0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
493b0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
493c0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
493d0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
493e0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
493f0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
49400 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
49410 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
49420 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
49430 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
49440 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
49450 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
49460 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
49470 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
49480 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
49490 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
494a0 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
494b0 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
494c0 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
494d0 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
494e0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
494f0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
49500 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 63 61  se.      {..  ca
49510 73 74 5f 63 6f 75 6e 74 20 28 67 65 6f 2c 20 26  st_count (geo, &
49520 70 74 73 2c 20 26 6c 6e 73 2c 20 26 70 67 73 29  pts, &lns, &pgs)
49530 3b 0a 09 20 20 69 66 20 28 70 67 73 20 3e 3d 20  ;..  if (pgs >= 
49540 31 29 0a 09 20 20 20 20 7b 0a 09 09 67 65 6f 6d  1)..    {...geom
49550 32 20 3d 20 67 61 69 61 43 6c 6f 6e 65 47 65 6f  2 = gaiaCloneGeo
49560 6d 43 6f 6c 6c 50 6f 6c 79 67 6f 6e 73 20 28 67  mCollPolygons (g
49570 65 6f 29 3b 0a 09 09 67 65 6f 6d 32 2d 3e 53 72  eo);...geom2->Sr
49580 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b 0a  id = geo->Srid;.
49590 09 09 67 65 6f 6d 32 2d 3e 44 65 63 6c 61 72 65  ..geom2->Declare
495a0 64 54 79 70 65 20 3d 20 47 41 49 41 5f 4d 55 4c  dType = GAIA_MUL
495b0 54 49 50 4f 4c 59 47 4f 4e 3b 0a 09 09 67 61 69  TIPOLYGON;...gai
495c0 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  aToSpatiaLiteBlo
495d0 62 57 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70 5f  bWkb (geom2, &p_
495e0 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09  result, &len);..
495f0 09 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c  .gaiaFreeGeomCol
49600 6c 20 28 67 65 6f 6d 32 29 3b 0a 09 09 73 71 6c  l (geom2);...sql
49610 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
49620 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73   (context, p_res
49630 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b  ult, len, free);
49640 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a  ..    }..  else.
49650 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
49660 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
49670 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ext);.      }.  
49680 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
49690 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61  ll (geo);.}..sta
496a0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 52 65  tic void.fnct_Re
496b0 76 65 72 73 65 20 28 73 71 6c 69 74 65 33 5f 63  verse (sqlite3_c
496c0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
496d0 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
496e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
496f0 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
49700 74 69 6f 6e 3a 0a 2f 20 53 54 5f 52 65 76 65 72  tion:./ ST_Rever
49710 73 65 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20  se(BLOB encoded 
49720 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65  geometry)././ re
49730 74 75 72 6e 73 20 61 20 6e 65 77 20 47 65 6f 6d  turns a new Geom
49740 65 74 72 79 3a 20 61 6e 79 20 4c 69 6e 65 73 74  etry: any Linest
49750 72 69 6e 67 20 6f 72 20 52 69 6e 67 20 77 69 6c  ring or Ring wil
49760 6c 20 62 65 20 69 6e 20 72 65 76 65 72 73 65 20  l be in reverse 
49770 6f 72 64 65 72 0a 2f 20 6f 72 20 4e 55 4c 4c 20  order./ or NULL 
49780 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 63 61 73  in any other cas
49790 65 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  e.*/.    unsigne
497a0 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
497b0 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
497c0 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  .    int len;.  
497d0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
497e0 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  *p_result = NULL
497f0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
49800 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
49810 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
49820 6c 6c 50 74 72 20 67 65 6f 6d 32 20 3d 20 4e 55  llPtr geom2 = NU
49830 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  LL;.    GAIA_UNU
49840 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
49850 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
49860 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
49870 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
49880 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
49890 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
498a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
498b0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
498c0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
498d0 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
498e0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
498f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
49900 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
49910 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
49920 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
49930 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
49940 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  o = gaiaFromSpat
49950 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
49960 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
49970 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09  .    if (!geo)..
49980 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
49990 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
499a0 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
499b0 09 20 20 67 65 6f 6d 32 20 3d 20 67 61 69 61 43  .  geom2 = gaiaC
499c0 6c 6f 6e 65 47 65 6f 6d 43 6f 6c 6c 53 70 65 63  loneGeomCollSpec
499d0 69 61 6c 20 28 67 65 6f 2c 20 47 41 49 41 5f 52  ial (geo, GAIA_R
499e0 45 56 45 52 53 45 5f 4f 52 44 45 52 29 3b 0a 09  EVERSE_ORDER);..
499f0 20 20 67 65 6f 6d 32 2d 3e 53 72 69 64 20 3d 20    geom2->Srid = 
49a00 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 20 20 67 61  geo->Srid;..  ga
49a10 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c  iaToSpatiaLiteBl
49a20 6f 62 57 6b 62 20 28 67 65 6f 6d 32 2c 20 26 70  obWkb (geom2, &p
49a30 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a  _result, &len);.
49a40 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43  .  gaiaFreeGeomC
49a50 6f 6c 6c 20 28 67 65 6f 6d 32 29 3b 0a 09 20 20  oll (geom2);..  
49a60 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
49a70 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
49a80 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
49a90 65 29 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47  e);..  gaiaFreeG
49aa0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20  eomColl (geo);. 
49ab0 20 20 20 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63       }.}..static
49ac0 20 76 6f 69 64 0a 66 6e 63 74 5f 46 6f 72 63 65   void.fnct_Force
49ad0 4c 48 52 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  LHR (sqlite3_con
49ae0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
49af0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
49b00 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
49b10 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
49b20 6f 6e 3a 0a 2f 20 53 54 5f 46 6f 72 63 65 4c 48  on:./ ST_ForceLH
49b30 52 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67  R(BLOB encoded g
49b40 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74  eometry)././ ret
49b50 75 72 6e 73 20 61 20 6e 65 77 20 47 65 6f 6d 65  urns a new Geome
49b60 74 72 79 3a 20 61 6e 79 20 45 78 74 65 72 69 6f  try: any Exterio
49b70 72 20 52 69 6e 67 20 77 69 6c 6c 20 62 65 20 69  r Ring will be i
49b80 6e 20 63 6c 6f 63 6b 77 69 73 65 20 6f 72 69 65  n clockwise orie
49b90 6e 74 61 74 69 6f 6e 0a 2f 20 20 20 20 20 20 20  ntation./       
49ba0 20 20 61 6e 64 20 61 6e 79 20 49 6e 74 65 72 69    and any Interi
49bb0 6f 72 20 52 69 6e 67 20 77 69 6c 6c 20 62 65 20  or Ring will be 
49bc0 69 6e 20 63 6f 75 6e 74 65 72 2d 63 6c 6f 63 6b  in counter-clock
49bd0 77 69 73 65 20 6f 72 69 65 6e 74 61 74 69 6f 6e  wise orientation
49be0 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 6e 20 61 6e  ./ or NULL in an
49bf0 79 20 6f 74 68 65 72 20 63 61 73 65 0a 2a 2f 0a  y other case.*/.
49c00 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
49c10 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69  r *p_blob;.    i
49c20 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
49c30 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73  int len;.    uns
49c40 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
49c50 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  sult = NULL;.   
49c60 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
49c70 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
49c80 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
49c90 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c 3b 0a 20   geom2 = NULL;. 
49ca0 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
49cb0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
49cc0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
49cd0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
49ce0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
49cf0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
49d00 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
49d10 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
49d20 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
49d30 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
49d40 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
49d50 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
49d60 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
49d70 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
49d80 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
49d90 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
49da0 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
49db0 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
49dc0 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
49dd0 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
49de0 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
49df0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
49e00 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
49e10 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 65  se.      {..  ge
49e20 6f 6d 32 20 3d 20 67 61 69 61 43 6c 6f 6e 65 47  om2 = gaiaCloneG
49e30 65 6f 6d 43 6f 6c 6c 53 70 65 63 69 61 6c 20 28  eomCollSpecial (
49e40 67 65 6f 2c 20 47 41 49 41 5f 4c 48 52 5f 4f 52  geo, GAIA_LHR_OR
49e50 44 45 52 29 3b 0a 09 20 20 67 65 6f 6d 32 2d 3e  DER);..  geom2->
49e60 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64  Srid = geo->Srid
49e70 3b 0a 09 20 20 67 61 69 61 54 6f 53 70 61 74 69  ;..  gaiaToSpati
49e80 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65  aLiteBlobWkb (ge
49e90 6f 6d 32 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  om2, &p_result, 
49ea0 26 6c 65 6e 29 3b 0a 09 20 20 67 61 69 61 46 72  &len);..  gaiaFr
49eb0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d  eeGeomColl (geom
49ec0 32 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72  2);..  sqlite3_r
49ed0 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74  esult_blob (cont
49ee0 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c  ext, p_result, l
49ef0 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 67 61  en, free);..  ga
49f00 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
49f10 67 65 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a  geo);.      }.}.
49f20 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
49f30 74 5f 44 69 6d 65 6e 73 69 6f 6e 20 28 73 71 6c  t_Dimension (sql
49f40 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
49f50 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
49f60 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
49f70 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
49f80 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 44 69  L function:./ Di
49f90 6d 65 6e 73 69 6f 6e 28 42 4c 4f 42 20 65 6e 63  mension(BLOB enc
49fa0 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f  oded geometry)./
49fb0 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f 20 30 20  ./ returns:./ 0 
49fc0 69 66 20 67 65 6f 6d 65 74 72 79 20 69 73 20 61  if geometry is a
49fd0 20 50 4f 49 4e 54 20 6f 72 20 4d 55 4c 54 49 50   POINT or MULTIP
49fe0 4f 49 4e 54 0a 2f 20 31 20 69 66 20 67 65 6f 6d  OINT./ 1 if geom
49ff0 65 74 72 79 20 69 73 20 61 20 4c 49 4e 45 53 54  etry is a LINEST
4a000 52 49 4e 47 20 6f 72 20 4d 55 4c 54 49 4c 49 4e  RING or MULTILIN
4a010 45 53 54 52 49 4e 47 0a 2f 20 32 20 69 66 20 67  ESTRING./ 2 if g
4a020 65 6f 6d 65 74 72 79 20 69 73 20 61 20 50 4f 4c  eometry is a POL
4a030 59 47 4f 4e 20 6f 72 20 4d 55 4c 54 49 50 4f 4c  YGON or MULTIPOL
4a040 59 47 4f 4e 0a 2f 20 30 2c 20 31 2c 20 32 2c 20  YGON./ 0, 1, 2, 
4a050 66 6f 72 20 47 45 4f 4d 45 54 52 59 43 4f 4c 4c  for GEOMETRYCOLL
4a060 45 43 54 49 4f 4e 53 20 61 63 63 6f 72 64 69 6e  ECTIONS accordin
4a070 67 20 74 6f 20 67 65 6f 6d 65 74 72 69 65 73 20  g to geometries 
4a080 63 6f 6e 74 61 69 6e 65 64 20 69 6e 73 69 64 65  contained inside
4a090 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
4a0a0 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
4a0b0 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
4a0c0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
4a0d0 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
4a0e0 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 64 69  ytes;.    int di
4a0f0 6d 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  m;.    gaiaGeomC
4a100 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c  ollPtr geo = NUL
4a110 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  L;.    GAIA_UNUS
4a120 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
4a130 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
4a140 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
4a150 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d  ue_type (argv[0]
4a160 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  ) != SQLITE_BLOB
4a170 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
4a180 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4a190 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
4a1a0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
4a1b0 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73     p_blob = (uns
4a1c0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71  igned char *) sq
4a1d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
4a1e0 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
4a1f0 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65  n_bytes = sqlite
4a200 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61  3_value_bytes (a
4a210 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[0]);.    geo
4a220 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69   = gaiaFromSpati
4a230 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f  aLiteBlobWkb (p_
4a240 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a  blob, n_bytes);.
4a250 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73      if (!geo)..s
4a260 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
4a270 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ll (context);.  
4a280 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
4a290 20 20 64 69 6d 20 3d 20 67 61 69 61 44 69 6d 65    dim = gaiaDime
4a2a0 6e 73 69 6f 6e 20 28 67 65 6f 29 3b 0a 09 20 20  nsion (geo);..  
4a2b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
4a2c0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 64 69 6d  nt (context, dim
4a2d0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67  );.      }.    g
4a2e0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
4a2f0 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  (geo);.}..static
4a300 20 76 6f 69 64 0a 66 6e 63 74 5f 43 6f 6f 72 64   void.fnct_Coord
4a310 44 69 6d 65 6e 73 69 6f 6e 20 28 73 71 6c 69 74  Dimension (sqlit
4a320 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
4a330 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
4a340 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
4a350 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20   argv).{./* SQL 
4a360 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 43 6f 6f 72  function:./ Coor
4a370 64 44 69 6d 65 6e 73 69 6f 6e 28 42 4c 4f 42 20  dDimension(BLOB 
4a380 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79  encoded geometry
4a390 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f  )././ returns:./
4a3a0 20 27 58 59 27 2c 20 27 58 59 4d 27 2c 20 27 58   'XY', 'XYM', 'X
4a3b0 59 5a 27 2c 20 27 58 59 5a 4d 27 0a 2f 20 6f 72  YZ', 'XYZM'./ or
4a3c0 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
4a3d0 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
4a3e0 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  d.*/.    unsigne
4a3f0 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
4a400 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
4a410 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  .    int len;.  
4a420 20 20 63 68 61 72 20 2a 70 5f 64 69 6d 20 3d 20    char *p_dim = 
4a430 4e 55 4c 4c 3b 0a 20 20 20 20 63 68 61 72 20 2a  NULL;.    char *
4a440 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
4a450 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
4a460 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b  lPtr geo = NULL;
4a470 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
4a480 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
4a490 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
4a4a0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
4a4b0 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
4a4c0 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
4a4d0 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
4a4e0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
4a4f0 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
4a500 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
4a510 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
4a520 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
4a530 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
4a540 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
4a550 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
4a560 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
4a570 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
4a580 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
4a590 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
4a5a0 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
4a5b0 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
4a5c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4a5d0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
4a5e0 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
4a5f0 69 66 20 28 67 65 6f 2d 3e 44 69 6d 65 6e 73 69  if (geo->Dimensi
4a600 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f  onModel == GAIA_
4a610 58 59 29 0a 09 20 20 20 20 20 20 70 5f 64 69 6d  XY)..      p_dim
4a620 20 3d 20 22 58 59 22 3b 0a 09 20 20 65 6c 73 65   = "XY";..  else
4a630 20 69 66 20 28 67 65 6f 2d 3e 44 69 6d 65 6e 73   if (geo->Dimens
4a640 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41  ionModel == GAIA
4a650 5f 58 59 5f 5a 29 0a 09 20 20 20 20 20 20 70 5f  _XY_Z)..      p_
4a660 64 69 6d 20 3d 20 22 58 59 5a 22 3b 0a 09 20 20  dim = "XYZ";..  
4a670 65 6c 73 65 20 69 66 20 28 67 65 6f 2d 3e 44 69  else if (geo->Di
4a680 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20  mensionModel == 
4a690 47 41 49 41 5f 58 59 5f 4d 29 0a 09 20 20 20 20  GAIA_XY_M)..    
4a6a0 20 20 70 5f 64 69 6d 20 3d 20 22 58 59 4d 22 3b    p_dim = "XYM";
4a6b0 0a 09 20 20 65 6c 73 65 20 69 66 20 28 67 65 6f  ..  else if (geo
4a6c0 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c  ->DimensionModel
4a6d0 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29   == GAIA_XY_Z_M)
4a6e0 0a 09 20 20 20 20 20 20 70 5f 64 69 6d 20 3d 20  ..      p_dim = 
4a6f0 22 58 59 5a 4d 22 3b 0a 09 20 20 69 66 20 28 70  "XYZM";..  if (p
4a700 5f 64 69 6d 29 0a 09 20 20 20 20 7b 0a 09 09 6c  _dim)..    {...l
4a710 65 6e 20 3d 20 73 74 72 6c 65 6e 20 28 70 5f 64  en = strlen (p_d
4a720 69 6d 29 3b 0a 09 09 70 5f 72 65 73 75 6c 74 20  im);...p_result 
4a730 3d 20 6d 61 6c 6c 6f 63 20 28 6c 65 6e 20 2b 20  = malloc (len + 
4a740 31 29 3b 0a 09 09 73 74 72 63 70 79 20 28 70 5f  1);...strcpy (p_
4a750 72 65 73 75 6c 74 2c 20 70 5f 64 69 6d 29 3b 0a  result, p_dim);.
4a760 09 20 20 20 20 7d 0a 09 20 20 69 66 20 28 21 70  .    }..  if (!p
4a770 5f 72 65 73 75 6c 74 29 0a 09 20 20 20 20 20 20  _result)..      
4a780 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
4a790 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
4a7a0 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09    else..    {...
4a7b0 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 20 28 70 5f  len = strlen (p_
4a7c0 72 65 73 75 6c 74 29 3b 0a 09 09 73 71 6c 69 74  result);...sqlit
4a7d0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 20 28  e3_result_text (
4a7e0 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
4a7f0 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09  t, len, free);..
4a800 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
4a810 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
4a820 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61  ll (geo);.}..sta
4a830 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4e 44  tic void.fnct_ND
4a840 69 6d 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ims (sqlite3_con
4a850 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
4a860 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
4a870 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
4a880 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
4a890 6f 6e 3a 0a 2f 20 53 54 5f 4e 44 69 6d 73 28 42  on:./ ST_NDims(B
4a8a0 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d  LOB encoded geom
4a8b0 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  etry)././ return
4a8c0 73 3a 0a 2f 20 32 2c 20 33 20 6f 72 20 34 0a 2f  s:./ 2, 3 or 4./
4a8d0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
4a8e0 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
4a8f0 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
4a900 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
4a910 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
4a920 65 73 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 75  es;.    int resu
4a930 6c 74 20 3d 20 30 3b 0a 20 20 20 20 67 61 69 61  lt = 0;.    gaia
4a940 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
4a950 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41  = NULL;.    GAIA
4a960 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
4a970 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
4a980 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
4a990 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
4a9a0 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
4a9b0 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
4a9c0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
4a9d0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
4a9e0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
4a9f0 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
4aa00 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
4aa10 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
4aa20 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
4aa30 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
4aa40 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
4aa50 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
4aa60 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d    geo = gaiaFrom
4aa70 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
4aa80 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
4aa90 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65  es);.    if (!ge
4aaa0 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  o)..sqlite3_resu
4aab0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
4aac0 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20  );.    else.    
4aad0 20 20 7b 0a 09 20 20 69 66 20 28 67 65 6f 2d 3e    {..  if (geo->
4aae0 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
4aaf0 3d 20 47 41 49 41 5f 58 59 29 0a 09 20 20 20 20  = GAIA_XY)..    
4ab00 20 20 72 65 73 75 6c 74 20 3d 20 32 3b 0a 09 20    result = 2;.. 
4ab10 20 65 6c 73 65 20 69 66 20 28 67 65 6f 2d 3e 44   else if (geo->D
4ab20 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d  imensionModel ==
4ab30 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09 20 20 20   GAIA_XY_Z)..   
4ab40 20 20 20 72 65 73 75 6c 74 20 3d 20 33 3b 0a 09     result = 3;..
4ab50 20 20 65 6c 73 65 20 69 66 20 28 67 65 6f 2d 3e    else if (geo->
4ab60 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
4ab70 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 20 20  = GAIA_XY_M)..  
4ab80 20 20 20 20 72 65 73 75 6c 74 20 3d 20 33 3b 0a      result = 3;.
4ab90 09 20 20 65 6c 73 65 20 69 66 20 28 67 65 6f 2d  .  else if (geo-
4aba0 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20  >DimensionModel 
4abb0 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a  == GAIA_XY_Z_M).
4abc0 09 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20  .      result = 
4abd0 34 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  4;..  sqlite3_re
4abe0 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
4abf0 74 2c 20 72 65 73 75 6c 74 29 3b 0a 20 20 20 20  t, result);.    
4ac00 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65    }.    gaiaFree
4ac10 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a  GeomColl (geo);.
4ac20 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
4ac30 6e 63 74 5f 47 65 6f 6d 65 74 72 79 54 79 70 65  nct_GeometryType
4ac40 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
4ac50 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
4ac60 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
4ac70 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
4ac80 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
4ac90 0a 2f 20 47 65 6f 6d 65 74 72 79 54 79 70 65 28  ./ GeometryType(
4aca0 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f  BLOB encoded geo
4acb0 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72  metry)././ retur
4acc0 6e 73 20 74 68 65 20 63 6c 61 73 73 20 66 6f 72  ns the class for
4acd0 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
4ace0 79 3a 0a 2f 20 27 50 4f 49 4e 54 27 20 6f 72 20  y:./ 'POINT' or 
4acf0 27 4d 55 4c 54 49 50 4f 49 4e 54 27 20 5b 5a 2c  'MULTIPOINT' [Z,
4ad00 20 4d 2c 20 5a 4d 5d 0a 2f 20 27 4c 49 4e 45 53   M, ZM]./ 'LINES
4ad10 54 52 49 4e 47 27 20 6f 72 20 27 4d 55 4c 54 49  TRING' or 'MULTI
4ad20 4c 49 4e 45 53 54 52 49 4e 47 27 20 5b 5a 2c 20  LINESTRING' [Z, 
4ad30 4d 2c 20 5a 4d 5d 0a 2f 20 27 50 4f 4c 59 47 4f  M, ZM]./ 'POLYGO
4ad40 4e 27 20 6f 72 20 27 4d 55 4c 54 49 50 4f 4c 59  N' or 'MULTIPOLY
4ad50 47 4f 4e 27 20 5b 5a 2c 20 4d 2c 20 5a 4d 5d 0a  GON' [Z, M, ZM].
4ad60 2f 20 27 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45  / 'GEOMETRYCOLLE
4ad70 43 54 49 4f 4e 27 20 20 5b 5a 2c 20 4d 2c 20 5a  CTION'  [Z, M, Z
4ad80 4d 5d 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  M]./ or NULL if 
4ad90 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
4ada0 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
4adb0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
4adc0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
4add0 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
4ade0 6c 65 6e 3b 0a 20 20 20 20 69 6e 74 20 74 79 70  len;.    int typ
4adf0 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 5f 74  e;.    char *p_t
4ae00 79 70 65 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ype = NULL;.    
4ae10 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
4ae20 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47   NULL;.    gaiaG
4ae30 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
4ae40 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f   NULL;.    GAIA_
4ae50 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
4ae60 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
4ae70 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
4ae80 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
4ae90 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
4aea0 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
4aeb0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
4aec0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
4aed0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
4aee0 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
4aef0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
4af00 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
4af10 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
4af20 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
4af30 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
4af40 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
4af50 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
4af60 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
4af70 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
4af80 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
4af90 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
4afa0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
4afb0 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
4afc0 20 7b 0a 09 20 20 74 79 70 65 20 3d 20 67 61 69   {..  type = gai
4afd0 61 47 65 6f 6d 65 74 72 79 54 79 70 65 20 28 67  aGeometryType (g
4afe0 65 6f 29 3b 0a 09 20 20 73 77 69 74 63 68 20 28  eo);..  switch (
4aff0 74 79 70 65 29 0a 09 20 20 20 20 7b 0a 09 20 20  type)..    {..  
4b000 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f 49 4e    case GAIA_POIN
4b010 54 3a 0a 09 09 70 5f 74 79 70 65 20 3d 20 22 50  T:...p_type = "P
4b020 4f 49 4e 54 22 3b 0a 09 09 62 72 65 61 6b 3b 0a  OINT";...break;.
4b030 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 50  .    case GAIA_P
4b040 4f 49 4e 54 5a 3a 0a 09 09 70 5f 74 79 70 65 20  OINTZ:...p_type 
4b050 3d 20 22 50 4f 49 4e 54 20 5a 22 3b 0a 09 09 62  = "POINT Z";...b
4b060 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
4b070 47 41 49 41 5f 50 4f 49 4e 54 4d 3a 0a 09 09 70  GAIA_POINTM:...p
4b080 5f 74 79 70 65 20 3d 20 22 50 4f 49 4e 54 20 4d  _type = "POINT M
4b090 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  ";...break;..   
4b0a0 20 63 61 73 65 20 47 41 49 41 5f 50 4f 49 4e 54   case GAIA_POINT
4b0b0 5a 4d 3a 0a 09 09 70 5f 74 79 70 65 20 3d 20 22  ZM:...p_type = "
4b0c0 50 4f 49 4e 54 20 5a 4d 22 3b 0a 09 09 62 72 65  POINT ZM";...bre
4b0d0 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
4b0e0 49 41 5f 4d 55 4c 54 49 50 4f 49 4e 54 3a 0a 09  IA_MULTIPOINT:..
4b0f0 09 70 5f 74 79 70 65 20 3d 20 22 4d 55 4c 54 49  .p_type = "MULTI
4b100 50 4f 49 4e 54 22 3b 0a 09 09 62 72 65 61 6b 3b  POINT";...break;
4b110 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
4b120 4d 55 4c 54 49 50 4f 49 4e 54 5a 3a 0a 09 09 70  MULTIPOINTZ:...p
4b130 5f 74 79 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f  _type = "MULTIPO
4b140 49 4e 54 20 5a 22 3b 0a 09 09 62 72 65 61 6b 3b  INT Z";...break;
4b150 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
4b160 4d 55 4c 54 49 50 4f 49 4e 54 4d 3a 0a 09 09 70  MULTIPOINTM:...p
4b170 5f 74 79 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f  _type = "MULTIPO
4b180 49 4e 54 20 4d 22 3b 0a 09 09 62 72 65 61 6b 3b  INT M";...break;
4b190 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
4b1a0 4d 55 4c 54 49 50 4f 49 4e 54 5a 4d 3a 0a 09 09  MULTIPOINTZM:...
4b1b0 70 5f 74 79 70 65 20 3d 20 22 4d 55 4c 54 49 50  p_type = "MULTIP
4b1c0 4f 49 4e 54 20 5a 4d 22 3b 0a 09 09 62 72 65 61  OINT ZM";...brea
4b1d0 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49  k;..    case GAI
4b1e0 41 5f 4c 49 4e 45 53 54 52 49 4e 47 3a 0a 09 20  A_LINESTRING:.. 
4b1f0 20 20 20 63 61 73 65 20 47 41 49 41 5f 43 4f 4d     case GAIA_COM
4b200 50 52 45 53 53 45 44 5f 4c 49 4e 45 53 54 52 49  PRESSED_LINESTRI
4b210 4e 47 3a 0a 09 09 70 5f 74 79 70 65 20 3d 20 22  NG:...p_type = "
4b220 4c 49 4e 45 53 54 52 49 4e 47 22 3b 0a 09 09 62  LINESTRING";...b
4b230 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20  reak;..    case 
4b240 47 41 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 5a  GAIA_LINESTRINGZ
4b250 3a 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  :..    case GAIA
4b260 5f 43 4f 4d 50 52 45 53 53 45 44 5f 4c 49 4e 45  _COMPRESSED_LINE
4b270 53 54 52 49 4e 47 5a 3a 0a 09 09 70 5f 74 79 70  STRINGZ:...p_typ
4b280 65 20 3d 20 22 4c 49 4e 45 53 54 52 49 4e 47 20  e = "LINESTRING 
4b290 5a 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  Z";...break;..  
4b2a0 20 20 63 61 73 65 20 47 41 49 41 5f 4c 49 4e 45    case GAIA_LINE
4b2b0 53 54 52 49 4e 47 4d 3a 0a 09 20 20 20 20 63 61  STRINGM:..    ca
4b2c0 73 65 20 47 41 49 41 5f 43 4f 4d 50 52 45 53 53  se GAIA_COMPRESS
4b2d0 45 44 5f 4c 49 4e 45 53 54 52 49 4e 47 4d 3a 0a  ED_LINESTRINGM:.
4b2e0 09 09 70 5f 74 79 70 65 20 3d 20 22 4c 49 4e 45  ..p_type = "LINE
4b2f0 53 54 52 49 4e 47 20 4d 22 3b 0a 09 09 62 72 65  STRING M";...bre
4b300 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41  ak;..    case GA
4b310 49 41 5f 4c 49 4e 45 53 54 52 49 4e 47 5a 4d 3a  IA_LINESTRINGZM:
4b320 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
4b330 43 4f 4d 50 52 45 53 53 45 44 5f 4c 49 4e 45 53  COMPRESSED_LINES
4b340 54 52 49 4e 47 5a 4d 3a 0a 09 09 70 5f 74 79 70  TRINGZM:...p_typ
4b350 65 20 3d 20 22 4c 49 4e 45 53 54 52 49 4e 47 20  e = "LINESTRING 
4b360 5a 4d 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20  ZM";...break;.. 
4b370 20 20 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c     case GAIA_MUL
4b380 54 49 4c 49 4e 45 53 54 52 49 4e 47 3a 0a 09 09  TILINESTRING:...
4b390 70 5f 74 79 70 65 20 3d 20 22 4d 55 4c 54 49 4c  p_type = "MULTIL
4b3a0 49 4e 45 53 54 52 49 4e 47 22 3b 0a 09 09 62 72  INESTRING";...br
4b3b0 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47  eak;..    case G
4b3c0 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52  AIA_MULTILINESTR
4b3d0 49 4e 47 5a 3a 0a 09 09 70 5f 74 79 70 65 20 3d  INGZ:...p_type =
4b3e0 20 22 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e   "MULTILINESTRIN
4b3f0 47 20 5a 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  G Z";...break;..
4b400 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 4d 55      case GAIA_MU
4b410 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 4d 3a 0a  LTILINESTRINGM:.
4b420 09 09 70 5f 74 79 70 65 20 3d 20 22 4d 55 4c 54  ..p_type = "MULT
4b430 49 4c 49 4e 45 53 54 52 49 4e 47 20 4d 22 3b 0a  ILINESTRING M";.
4b440 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
4b450 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e  se GAIA_MULTILIN
4b460 45 53 54 52 49 4e 47 5a 4d 3a 0a 09 09 70 5f 74  ESTRINGZM:...p_t
4b470 79 70 65 20 3d 20 22 4d 55 4c 54 49 4c 49 4e 45  ype = "MULTILINE
4b480 53 54 52 49 4e 47 20 5a 4d 22 3b 0a 09 09 62 72  STRING ZM";...br
4b490 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47  eak;..    case G
4b4a0 41 49 41 5f 50 4f 4c 59 47 4f 4e 3a 0a 09 20 20  AIA_POLYGON:..  
4b4b0 20 20 63 61 73 65 20 47 41 49 41 5f 43 4f 4d 50    case GAIA_COMP
4b4c0 52 45 53 53 45 44 5f 50 4f 4c 59 47 4f 4e 3a 0a  RESSED_POLYGON:.
4b4d0 09 09 70 5f 74 79 70 65 20 3d 20 22 50 4f 4c 59  ..p_type = "POLY
4b4e0 47 4f 4e 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09  GON";...break;..
4b4f0 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f      case GAIA_PO
4b500 4c 59 47 4f 4e 5a 3a 0a 09 20 20 20 20 63 61 73  LYGONZ:..    cas
4b510 65 20 47 41 49 41 5f 43 4f 4d 50 52 45 53 53 45  e GAIA_COMPRESSE
4b520 44 5f 50 4f 4c 59 47 4f 4e 5a 3a 0a 09 09 70 5f  D_POLYGONZ:...p_
4b530 74 79 70 65 20 3d 20 22 50 4f 4c 59 47 4f 4e 20  type = "POLYGON 
4b540 5a 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  Z";...break;..  
4b550 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f 4c 59    case GAIA_POLY
4b560 47 4f 4e 4d 3a 0a 09 20 20 20 20 63 61 73 65 20  GONM:..    case 
4b570 47 41 49 41 5f 43 4f 4d 50 52 45 53 53 45 44 5f  GAIA_COMPRESSED_
4b580 50 4f 4c 59 47 4f 4e 4d 3a 0a 09 09 70 5f 74 79  POLYGONM:...p_ty
4b590 70 65 20 3d 20 22 50 4f 4c 59 47 4f 4e 20 4d 22  pe = "POLYGON M"
4b5a0 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
4b5b0 63 61 73 65 20 47 41 49 41 5f 50 4f 4c 59 47 4f  case GAIA_POLYGO
4b5c0 4e 5a 4d 3a 0a 09 20 20 20 20 63 61 73 65 20 47  NZM:..    case G
4b5d0 41 49 41 5f 43 4f 4d 50 52 45 53 53 45 44 5f 50  AIA_COMPRESSED_P
4b5e0 4f 4c 59 47 4f 4e 5a 4d 3a 0a 09 09 70 5f 74 79  OLYGONZM:...p_ty
4b5f0 70 65 20 3d 20 22 50 4f 4c 59 47 4f 4e 20 5a 4d  pe = "POLYGON ZM
4b600 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20  ";...break;..   
4b610 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49   case GAIA_MULTI
4b620 50 4f 4c 59 47 4f 4e 3a 0a 09 09 70 5f 74 79 70  POLYGON:...p_typ
4b630 65 20 3d 20 22 4d 55 4c 54 49 50 4f 4c 59 47 4f  e = "MULTIPOLYGO
4b640 4e 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  N";...break;..  
4b650 20 20 63 61 73 65 20 47 41 49 41 5f 4d 55 4c 54    case GAIA_MULT
4b660 49 50 4f 4c 59 47 4f 4e 5a 3a 0a 09 09 70 5f 74  IPOLYGONZ:...p_t
4b670 79 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f 4c 59  ype = "MULTIPOLY
4b680 47 4f 4e 20 5a 22 3b 0a 09 09 62 72 65 61 6b 3b  GON Z";...break;
4b690 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
4b6a0 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 4d 3a 0a 09  MULTIPOLYGONM:..
4b6b0 09 70 5f 74 79 70 65 20 3d 20 22 4d 55 4c 54 49  .p_type = "MULTI
4b6c0 50 4f 4c 59 47 4f 4e 20 4d 22 3b 0a 09 09 62 72  POLYGON M";...br
4b6d0 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47  eak;..    case G
4b6e0 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e  AIA_MULTIPOLYGON
4b6f0 5a 4d 3a 0a 09 09 70 5f 74 79 70 65 20 3d 20 22  ZM:...p_type = "
4b700 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 20 5a 4d 22  MULTIPOLYGON ZM"
4b710 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
4b720 63 61 73 65 20 47 41 49 41 5f 47 45 4f 4d 45 54  case GAIA_GEOMET
4b730 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 3a 0a 09 09  RYCOLLECTION:...
4b740 70 5f 74 79 70 65 20 3d 20 22 47 45 4f 4d 45 54  p_type = "GEOMET
4b750 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 22 3b 0a 09  RYCOLLECTION";..
4b760 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
4b770 65 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59 43  e GAIA_GEOMETRYC
4b780 4f 4c 4c 45 43 54 49 4f 4e 5a 3a 0a 09 09 70 5f  OLLECTIONZ:...p_
4b790 74 79 70 65 20 3d 20 22 47 45 4f 4d 45 54 52 59  type = "GEOMETRY
4b7a0 43 4f 4c 4c 45 43 54 49 4f 4e 20 5a 22 3b 0a 09  COLLECTION Z";..
4b7b0 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
4b7c0 65 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59 43  e GAIA_GEOMETRYC
4b7d0 4f 4c 4c 45 43 54 49 4f 4e 4d 3a 0a 09 09 70 5f  OLLECTIONM:...p_
4b7e0 74 79 70 65 20 3d 20 22 47 45 4f 4d 45 54 52 59  type = "GEOMETRY
4b7f0 43 4f 4c 4c 45 43 54 49 4f 4e 20 4d 22 3b 0a 09  COLLECTION M";..
4b800 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
4b810 65 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59 43  e GAIA_GEOMETRYC
4b820 4f 4c 4c 45 43 54 49 4f 4e 5a 4d 3a 0a 09 09 70  OLLECTIONZM:...p
4b830 5f 74 79 70 65 20 3d 20 22 47 45 4f 4d 45 54 52  _type = "GEOMETR
4b840 59 43 4f 4c 4c 45 43 54 49 4f 4e 20 5a 4d 22 3b  YCOLLECTION ZM";
4b850 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 7d  ...break;..    }
4b860 3b 0a 09 20 20 69 66 20 28 70 5f 74 79 70 65 29  ;..  if (p_type)
4b870 0a 09 20 20 20 20 7b 0a 09 09 6c 65 6e 20 3d 20  ..    {...len = 
4b880 73 74 72 6c 65 6e 20 28 70 5f 74 79 70 65 29 3b  strlen (p_type);
4b890 0a 09 09 70 5f 72 65 73 75 6c 74 20 3d 20 6d 61  ...p_result = ma
4b8a0 6c 6c 6f 63 20 28 6c 65 6e 20 2b 20 31 29 3b 0a  lloc (len + 1);.
4b8b0 09 09 73 74 72 63 70 79 20 28 70 5f 72 65 73 75  ..strcpy (p_resu
4b8c0 6c 74 2c 20 70 5f 74 79 70 65 29 3b 0a 09 20 20  lt, p_type);..  
4b8d0 20 20 7d 0a 09 20 20 69 66 20 28 21 70 5f 72 65    }..  if (!p_re
4b8e0 73 75 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c  sult)..      sql
4b8f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4b900 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65   (context);..  e
4b910 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 6c 65 6e  lse..    {...len
4b920 20 3d 20 73 74 72 6c 65 6e 20 28 70 5f 72 65 73   = strlen (p_res
4b930 75 6c 74 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f  ult);...sqlite3_
4b940 72 65 73 75 6c 74 5f 74 65 78 74 20 28 63 6f 6e  result_text (con
4b950 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20  text, p_result, 
4b960 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 20  len, free);..   
4b970 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67   }.      }.    g
4b980 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
4b990 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  (geo);.}..static
4b9a0 20 76 6f 69 64 0a 66 6e 63 74 5f 47 65 6f 6d 65   void.fnct_Geome
4b9b0 74 72 79 41 6c 69 61 73 54 79 70 65 20 28 73 71  tryAliasType (sq
4b9c0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
4b9d0 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
4b9e0 63 2c 0a 09 09 09 73 71 6c 69 74 65 33 5f 76 61  c,....sqlite3_va
4b9f0 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
4ba00 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
4ba10 2f 20 47 65 6f 6d 65 74 72 79 41 6c 69 61 73 54  / GeometryAliasT
4ba20 79 70 65 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64  ype(BLOB encoded
4ba30 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
4ba40 65 74 75 72 6e 73 20 74 68 65 20 61 6c 69 61 73  eturns the alias
4ba50 2d 63 6c 61 73 73 20 66 6f 72 20 63 75 72 72 65  -class for curre
4ba60 6e 74 20 67 65 6f 6d 65 74 72 79 3a 0a 2f 20 27  nt geometry:./ '
4ba70 50 4f 49 4e 54 27 0a 2f 20 27 4c 49 4e 45 53 54  POINT'./ 'LINEST
4ba80 52 49 4e 47 27 0a 2f 20 27 50 4f 4c 59 47 4f 4e  RING'./ 'POLYGON
4ba90 27 0a 2f 20 27 4d 55 4c 54 49 50 4f 49 4e 54 27  './ 'MULTIPOINT'
4baa0 0a 2f 20 27 4d 55 4c 54 49 4c 49 4e 45 53 54 52  ./ 'MULTILINESTR
4bab0 49 4e 47 27 0a 2f 20 27 4d 55 4c 54 49 50 4f 4c  ING'./ 'MULTIPOL
4bac0 59 47 4f 4e 27 0a 2f 20 27 47 45 4f 4d 45 54 52  YGON'./ 'GEOMETR
4bad0 59 43 4f 4c 4c 45 43 54 49 4f 4e 27 20 0a 2f 20  YCOLLECTION' ./ 
4bae0 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65  or NULL if any e
4baf0 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
4bb00 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67  red.*/.    unsig
4bb10 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62  ned char *p_blob
4bb20 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65  ;.    int n_byte
4bb30 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  s;.    int len;.
4bb40 20 20 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20      int type;.  
4bb50 20 20 63 68 61 72 20 2a 70 5f 74 79 70 65 20 3d    char *p_type =
4bb60 20 4e 55 4c 4c 3b 0a 20 20 20 20 63 68 61 72 20   NULL;.    char 
4bb70 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  *p_result = NULL
4bb80 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
4bb90 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
4bba0 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
4bbb0 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
4bbc0 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
4bbd0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
4bbe0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
4bbf0 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
4bc00 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
4bc10 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
4bc20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
4bc30 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
4bc40 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
4bc50 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
4bc60 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
4bc70 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
4bc80 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
4bc90 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
4bca0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20  gv[0]);.    geo 
4bcb0 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
4bcc0 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
4bcd0 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
4bce0 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71     if (!geo)..sq
4bcf0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
4bd00 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
4bd10 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
4bd20 20 74 79 70 65 20 3d 20 67 61 69 61 47 65 6f 6d   type = gaiaGeom
4bd30 65 74 72 79 41 6c 69 61 73 54 79 70 65 20 28 67  etryAliasType (g
4bd40 65 6f 29 3b 0a 09 20 20 73 77 69 74 63 68 20 28  eo);..  switch (
4bd50 74 79 70 65 29 0a 09 20 20 20 20 7b 0a 09 20 20  type)..    {..  
4bd60 20 20 63 61 73 65 20 47 41 49 41 5f 50 4f 49 4e    case GAIA_POIN
4bd70 54 3a 0a 09 09 70 5f 74 79 70 65 20 3d 20 22 50  T:...p_type = "P
4bd80 4f 49 4e 54 22 3b 0a 09 09 62 72 65 61 6b 3b 0a  OINT";...break;.
4bd90 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f 4d  .    case GAIA_M
4bda0 55 4c 54 49 50 4f 49 4e 54 3a 0a 09 09 70 5f 74  ULTIPOINT:...p_t
4bdb0 79 70 65 20 3d 20 22 4d 55 4c 54 49 50 4f 49 4e  ype = "MULTIPOIN
4bdc0 54 22 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20  T";...break;..  
4bdd0 20 20 63 61 73 65 20 47 41 49 41 5f 4c 49 4e 45    case GAIA_LINE
4bde0 53 54 52 49 4e 47 3a 0a 09 09 70 5f 74 79 70 65  STRING:...p_type
4bdf0 20 3d 20 22 4c 49 4e 45 53 54 52 49 4e 47 22 3b   = "LINESTRING";
4be00 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
4be10 61 73 65 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49  ase GAIA_MULTILI
4be20 4e 45 53 54 52 49 4e 47 3a 0a 09 09 70 5f 74 79  NESTRING:...p_ty
4be30 70 65 20 3d 20 22 4d 55 4c 54 49 4c 49 4e 45 53  pe = "MULTILINES
4be40 54 52 49 4e 47 22 3b 0a 09 09 62 72 65 61 6b 3b  TRING";...break;
4be50 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41 5f  ..    case GAIA_
4be60 50 4f 4c 59 47 4f 4e 3a 0a 09 09 70 5f 74 79 70  POLYGON:...p_typ
4be70 65 20 3d 20 22 50 4f 4c 59 47 4f 4e 22 3b 0a 09  e = "POLYGON";..
4be80 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73  .break;..    cas
4be90 65 20 47 41 49 41 5f 4d 55 4c 54 49 50 4f 4c 59  e GAIA_MULTIPOLY
4bea0 47 4f 4e 3a 0a 09 09 70 5f 74 79 70 65 20 3d 20  GON:...p_type = 
4beb0 22 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 22 3b 0a  "MULTIPOLYGON";.
4bec0 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
4bed0 73 65 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59  se GAIA_GEOMETRY
4bee0 43 4f 4c 4c 45 43 54 49 4f 4e 3a 0a 09 09 70 5f  COLLECTION:...p_
4bef0 74 79 70 65 20 3d 20 22 47 45 4f 4d 45 54 52 59  type = "GEOMETRY
4bf00 43 4f 4c 4c 45 43 54 49 4f 4e 22 3b 0a 09 09 62  COLLECTION";...b
4bf10 72 65 61 6b 3b 0a 09 20 20 20 20 7d 3b 0a 09 20  reak;..    };.. 
4bf20 20 69 66 20 28 70 5f 74 79 70 65 29 0a 09 20 20   if (p_type)..  
4bf30 20 20 7b 0a 09 09 6c 65 6e 20 3d 20 73 74 72 6c    {...len = strl
4bf40 65 6e 20 28 70 5f 74 79 70 65 29 3b 0a 09 09 70  en (p_type);...p
4bf50 5f 72 65 73 75 6c 74 20 3d 20 6d 61 6c 6c 6f 63  _result = malloc
4bf60 20 28 6c 65 6e 20 2b 20 31 29 3b 0a 09 09 73 74   (len + 1);...st
4bf70 72 63 70 79 20 28 70 5f 72 65 73 75 6c 74 2c 20  rcpy (p_result, 
4bf80 70 5f 74 79 70 65 29 3b 0a 09 20 20 20 20 7d 0a  p_type);..    }.
4bf90 09 20 20 69 66 20 28 21 70 5f 72 65 73 75 6c 74  .  if (!p_result
4bfa0 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  )..      sqlite3
4bfb0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
4bfc0 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a  ntext);..  else.
4bfd0 09 20 20 20 20 7b 0a 09 09 6c 65 6e 20 3d 20 73  .    {...len = s
4bfe0 74 72 6c 65 6e 20 28 70 5f 72 65 73 75 6c 74 29  trlen (p_result)
4bff0 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75  ;...sqlite3_resu
4c000 6c 74 5f 74 65 78 74 20 28 63 6f 6e 74 65 78 74  lt_text (context
4c010 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c  , p_result, len,
4c020 20 66 72 65 65 29 3b 0a 09 20 20 20 20 7d 0a 20   free);..    }. 
4c030 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
4c040 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
4c050 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
4c060 64 0a 66 6e 63 74 5f 53 72 69 64 46 72 6f 6d 41  d.fnct_SridFromA
4c070 75 74 68 43 52 53 20 28 73 71 6c 69 74 65 33 5f  uthCRS (sqlite3_
4c080 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
4c090 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 20  t, int argc,... 
4c0a0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
4c0b0 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
4c0c0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
4c0d0 20 53 72 69 64 46 72 6f 6d 41 75 74 68 43 52 53   SridFromAuthCRS
4c0e0 28 61 75 74 68 5f 6e 61 6d 65 2c 20 61 75 74 68  (auth_name, auth
4c0f0 5f 73 72 69 64 29 0a 2f 0a 2f 20 72 65 74 75 72  _srid)././ retur
4c100 6e 73 20 74 68 65 20 53 52 49 44 0a 2f 20 6f 72  ns the SRID./ or
4c110 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
4c120 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
4c130 64 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75  d.*/.    const u
4c140 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 75  nsigned char *au
4c150 74 68 5f 6e 61 6d 65 3b 0a 20 20 20 20 69 6e 74  th_name;.    int
4c160 20 61 75 74 68 5f 73 72 69 64 3b 0a 20 20 20 20   auth_srid;.    
4c170 69 6e 74 20 73 72 69 64 20 3d 20 2d 31 3b 0a 20  int srid = -1;. 
4c180 20 20 20 63 68 61 72 20 73 71 6c 5b 31 30 32 34     char sql[1024
4c190 5d 3b 0a 20 20 20 20 63 68 61 72 20 73 71 6c 32  ];.    char sql2
4c1a0 5b 31 30 32 34 5d 3b 0a 20 20 20 20 63 68 61 72  [1024];.    char
4c1b0 20 2a 2a 72 65 73 75 6c 74 73 3b 0a 20 20 20 20   **results;.    
4c1c0 69 6e 74 20 6e 5f 72 6f 77 73 3b 0a 20 20 20 20  int n_rows;.    
4c1d0 69 6e 74 20 6e 5f 63 6f 6c 75 6d 6e 73 3b 0a 20  int n_columns;. 
4c1e0 20 20 20 63 68 61 72 20 2a 65 72 72 5f 6d 73 67     char *err_msg
4c1f0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74   = NULL;.    int
4c200 20 72 65 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b   ret;.    int i;
4c210 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71  .    sqlite3 *sq
4c220 6c 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63  lite = sqlite3_c
4c230 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
4c240 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
4c250 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
4c260 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
4c270 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
4c280 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
4c290 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
4c2a0 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20  LITE_TEXT).     
4c2b0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
4c2c0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
4c2d0 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
4c2e0 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
4c2f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
4c300 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20  pe (argv[1]) != 
4c310 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a  SQLITE_INTEGER).
4c320 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
4c330 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
4c340 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
4c350 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
4c360 20 61 75 74 68 5f 6e 61 6d 65 20 3d 20 73 71 6c   auth_name = sql
4c370 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
4c380 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 61  (argv[0]);.    a
4c390 75 74 68 5f 73 72 69 64 20 3d 20 73 71 6c 69 74  uth_srid = sqlit
4c3a0 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
4c3b0 67 76 5b 31 5d 29 3b 0a 0a 20 20 20 20 73 70 72  gv[1]);..    spr
4c3c0 69 6e 74 66 20 28 73 71 6c 2c 20 22 53 45 4c 45  intf (sql, "SELE
4c3d0 43 54 20 73 72 69 64 20 46 52 4f 4d 20 73 70 61  CT srid FROM spa
4c3e0 74 69 61 6c 5f 72 65 66 5f 73 79 73 20 22 29 3b  tial_ref_sys ");
4c3f0 0a 20 20 20 20 73 70 72 69 6e 74 66 20 28 73 71  .    sprintf (sq
4c400 6c 32 2c 20 22 57 48 45 52 45 20 55 70 70 65 72  l2, "WHERE Upper
4c410 28 61 75 74 68 5f 6e 61 6d 65 29 20 3d 20 55 70  (auth_name) = Up
4c420 70 65 72 28 27 25 73 27 29 20 41 4e 44 20 61 75  per('%s') AND au
4c430 74 68 5f 73 72 69 64 20 3d 20 25 64 22 2c 0a 09  th_srid = %d",..
4c440 20 20 20 20 20 61 75 74 68 5f 6e 61 6d 65 2c 20       auth_name, 
4c450 61 75 74 68 5f 73 72 69 64 29 3b 0a 20 20 20 20  auth_srid);.    
4c460 73 74 72 63 61 74 20 28 73 71 6c 2c 20 73 71 6c  strcat (sql, sql
4c470 32 29 3b 0a 20 20 20 20 72 65 74 20 3d 0a 09 73  2);.    ret =..s
4c480 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
4c490 20 28 73 71 6c 69 74 65 2c 20 73 71 6c 2c 20 26   (sqlite, sql, &
4c4a0 72 65 73 75 6c 74 73 2c 20 26 6e 5f 72 6f 77 73  results, &n_rows
4c4b0 2c 20 26 6e 5f 63 6f 6c 75 6d 6e 73 2c 0a 09 09  , &n_columns,...
4c4c0 09 20 20 20 26 65 72 72 5f 6d 73 67 29 3b 0a 20  .   &err_msg);. 
4c4d0 20 20 20 69 66 20 28 72 65 74 20 21 3d 20 53 51     if (ret != SQ
4c4e0 4c 49 54 45 5f 4f 4b 29 0a 09 67 6f 74 6f 20 64  LITE_OK)..goto d
4c4f0 6f 6e 65 3b 0a 20 20 20 20 69 66 20 28 6e 5f 72  one;.    if (n_r
4c500 6f 77 73 20 3e 3d 20 31 29 0a 20 20 20 20 20 20  ows >= 1).      
4c510 7b 0a 09 20 20 66 6f 72 20 28 69 20 3d 20 31 3b  {..  for (i = 1;
4c520 20 69 20 3c 3d 20 6e 5f 72 6f 77 73 3b 20 69 2b   i <= n_rows; i+
4c530 2b 29 0a 09 20 20 20 20 20 20 73 72 69 64 20 3d  +)..      srid =
4c540 20 61 74 6f 69 20 28 72 65 73 75 6c 74 73 5b 28   atoi (results[(
4c550 69 20 2a 20 6e 5f 63 6f 6c 75 6d 6e 73 29 20 2b  i * n_columns) +
4c560 20 30 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   0]);.      }.  
4c570 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74    sqlite3_free_t
4c580 61 62 6c 65 20 28 72 65 73 75 6c 74 73 29 3b 0a  able (results);.
4c590 20 20 64 6f 6e 65 3a 0a 20 20 20 20 73 71 6c 69    done:.    sqli
4c5a0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
4c5b0 63 6f 6e 74 65 78 74 2c 20 73 72 69 64 29 3b 0a  context, srid);.
4c5c0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
4c5d0 6e 63 74 5f 53 52 49 44 20 28 73 71 6c 69 74 65  nct_SRID (sqlite
4c5e0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
4c5f0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
4c600 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
4c610 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
4c620 75 6e 63 74 69 6f 6e 3a 0a 2f 20 53 72 69 64 28  unction:./ Srid(
4c630 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f  BLOB encoded geo
4c640 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72  metry)././ retur
4c650 6e 73 20 74 68 65 20 53 52 49 44 0a 2f 20 6f 72  ns the SRID./ or
4c660 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
4c670 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
4c680 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  d.*/.    unsigne
4c690 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
4c6a0 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
4c6b0 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
4c6c0 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b  lPtr geo = NULL;
4c6d0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
4c6e0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
4c6f0 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
4c700 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
4c710 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
4c720 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
4c730 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
4c740 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
4c750 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
4c760 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
4c770 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
4c780 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
4c790 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
4c7a0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
4c7b0 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
4c7c0 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
4c7d0 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
4c7e0 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
4c7f0 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
4c800 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
4c810 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
4c820 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4c830 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
4c840 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65  else..sqlite3_re
4c850 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
4c860 74 2c 20 67 65 6f 2d 3e 53 72 69 64 29 3b 0a 20  t, geo->Srid);. 
4c870 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
4c880 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
4c890 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 53  atic void.fnct_S
4c8a0 65 74 53 52 49 44 20 28 73 71 6c 69 74 65 33 5f  etSRID (sqlite3_
4c8b0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
4c8c0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
4c8d0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
4c8e0 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
4c8f0 63 74 69 6f 6e 3a 0a 2f 20 53 65 74 53 72 69 64  ction:./ SetSrid
4c900 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f  (BLOBencoded geo
4c910 6d 65 74 72 79 2c 20 73 72 69 64 29 0a 2f 0a 2f  metry, srid)././
4c920 20 72 65 74 75 72 6e 73 20 61 20 6e 65 77 20 67   returns a new g
4c930 65 6f 6d 65 74 72 79 20 74 68 61 74 20 69 73 20  eometry that is 
4c940 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 6e 65  the original one
4c950 20 72 65 63 65 69 76 65 64 2c 20 62 75 74 20 77   received, but w
4c960 69 74 68 20 74 68 65 20 6e 65 77 20 53 52 49 44  ith the new SRID
4c970 20 5b 6e 6f 20 63 6f 6f 72 64 69 6e 61 74 65 73   [no coordinates
4c980 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20   translation is 
4c990 61 70 70 6c 69 65 64 5d 0a 2f 20 6f 72 20 4e 55  applied]./ or NU
4c9a0 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
4c9b0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
4c9c0 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
4c9d0 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20  har *p_blob;.   
4c9e0 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20   int n_bytes;.  
4c9f0 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
4ca00 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
4ca10 20 20 69 6e 74 20 73 72 69 64 3b 0a 20 20 20 20    int srid;.    
4ca20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
4ca30 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a  _result = NULL;.
4ca40 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
4ca50 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
4ca60 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
4ca70 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
4ca80 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
4ca90 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
4caa0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
4cab0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
4cac0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
4cad0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
4cae0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
4caf0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29  e_type (argv[1])
4cb00 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
4cb10 45 52 29 0a 09 73 72 69 64 20 3d 20 73 71 6c 69  ER)..srid = sqli
4cb20 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61  te3_value_int (a
4cb30 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[1]);.    els
4cb40 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
4cb50 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4cb60 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
4cb70 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
4cb80 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73     p_blob = (uns
4cb90 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71  igned char *) sq
4cba0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
4cbb0 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
4cbc0 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65  n_bytes = sqlite
4cbd0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61  3_value_bytes (a
4cbe0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[0]);.    geo
4cbf0 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69   = gaiaFromSpati
4cc00 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f  aLiteBlobWkb (p_
4cc10 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a  blob, n_bytes);.
4cc20 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73      if (!geo)..s
4cc30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
4cc40 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ll (context);.  
4cc50 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
4cc60 20 20 67 65 6f 2d 3e 53 72 69 64 20 3d 20 73 72    geo->Srid = sr
4cc70 69 64 3b 0a 09 20 20 67 61 69 61 54 6f 53 70 61  id;..  gaiaToSpa
4cc80 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
4cc90 67 65 6f 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  geo, &p_result, 
4cca0 26 6e 5f 62 79 74 65 73 29 3b 0a 09 20 20 73 71  &n_bytes);..  sq
4ccb0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
4ccc0 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
4ccd0 73 75 6c 74 2c 20 6e 5f 62 79 74 65 73 2c 20 66  sult, n_bytes, f
4cce0 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ree);.      }.  
4ccf0 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
4cd00 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61  ll (geo);.}..sta
4cd10 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49 73  tic void.fnct_Is
4cd20 45 6d 70 74 79 20 28 73 71 6c 69 74 65 33 5f 63  Empty (sqlite3_c
4cd30 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
4cd40 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
4cd50 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
4cd60 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
4cd70 74 69 6f 6e 3a 0a 2f 20 49 73 45 6d 70 74 79 28  tion:./ IsEmpty(
4cd80 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f  BLOB encoded geo
4cd90 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72  metry)././ retur
4cda0 6e 73 3a 0a 2f 20 31 20 69 66 20 74 68 69 73 20  ns:./ 1 if this 
4cdb0 67 65 6f 6d 65 74 72 79 20 63 6f 6e 74 61 69 6e  geometry contain
4cdc0 73 20 6e 6f 20 65 6c 65 6d 65 6e 74 61 72 79 20  s no elementary 
4cdd0 67 65 6f 6d 65 74 72 69 65 73 0a 2f 20 30 20 6f  geometries./ 0 o
4cde0 74 68 65 72 77 69 73 65 0a 2f 20 6f 72 20 2d 31  therwise./ or -1
4cdf0 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
4ce00 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
4ce10 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
4ce20 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69  r *p_blob;.    i
4ce30 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
4ce40 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
4ce50 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
4ce60 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
4ce70 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
4ce80 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
4ce90 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
4cea0 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
4ceb0 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
4cec0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
4ced0 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
4cee0 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72  t, -1);..  retur
4cef0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
4cf00 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
4cf10 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
4cf20 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
4cf30 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
4cf40 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
4cf50 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
4cf60 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
4cf70 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
4cf80 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
4cf90 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
4cfa0 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
4cfb0 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
4cfc0 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20  ontext, -1);.   
4cfd0 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72   else..sqlite3_r
4cfe0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
4cff0 78 74 2c 20 67 61 69 61 49 73 45 6d 70 74 79 20  xt, gaiaIsEmpty 
4d000 28 67 65 6f 29 29 3b 0a 20 20 20 20 67 61 69 61  (geo));.    gaia
4d010 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
4d020 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  o);.}..static vo
4d030 69 64 0a 66 6e 63 74 5f 49 73 33 44 20 28 73 71  id.fnct_Is3D (sq
4d040 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
4d050 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
4d060 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
4d070 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
4d080 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 49  QL function:./ I
4d090 73 33 44 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64  s3D(BLOB encoded
4d0a0 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
4d0b0 65 74 75 72 6e 73 3a 0a 2f 20 31 20 69 66 20 74  eturns:./ 1 if t
4d0c0 68 69 73 20 67 65 6f 6d 65 74 72 79 20 68 61 73  his geometry has
4d0d0 20 5a 20 63 6f 6f 72 64 73 0a 2f 20 30 20 6f 74   Z coords./ 0 ot
4d0e0 68 65 72 77 69 73 65 0a 2f 20 6f 72 20 2d 31 20  herwise./ or -1 
4d0f0 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20  if any error is 
4d100 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20  encountered.*/. 
4d110 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
4d120 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e   *p_blob;.    in
4d130 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 67  t n_bytes;.    g
4d140 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
4d150 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47  eo = NULL;.    G
4d160 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
4d170 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
4d180 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
4d190 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
4d1a0 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
4d1b0 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
4d1c0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
4d1d0 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
4d1e0 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e  , -1);..  return
4d1f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
4d200 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
4d210 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
4d220 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
4d230 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
4d240 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
4d250 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
4d260 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61  ]);.    geo = ga
4d270 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65  iaFromSpatiaLite
4d280 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c  BlobWkb (p_blob,
4d290 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69   n_bytes);.    i
4d2a0 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65  f (!geo)..sqlite
4d2b0 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
4d2c0 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20  ntext, -1);.    
4d2d0 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
4d2e0 69 66 20 28 67 65 6f 2d 3e 44 69 6d 65 6e 73 69  if (geo->Dimensi
4d2f0 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f  onModel == GAIA_
4d300 58 59 5f 5a 0a 09 20 20 20 20 20 20 7c 7c 20 67  XY_Z..      || g
4d310 65 6f 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64  eo->DimensionMod
4d320 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f  el == GAIA_XY_Z_
4d330 4d 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  M)..      sqlite
4d340 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
4d350 6e 74 65 78 74 2c 20 31 29 3b 0a 09 20 20 65 6c  ntext, 1);..  el
4d360 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  se..      sqlite
4d370 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
4d380 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 20  ntext, 0);.     
4d390 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47   }.    gaiaFreeG
4d3a0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d  eomColl (geo);.}
4d3b0 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
4d3c0 63 74 5f 49 73 4d 65 61 73 75 72 65 64 20 28 73  ct_IsMeasured (s
4d3d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
4d3e0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
4d3f0 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
4d400 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
4d410 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
4d420 49 73 4d 65 61 73 75 72 65 64 28 42 4c 4f 42 20  IsMeasured(BLOB 
4d430 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79  encoded geometry
4d440 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f  )././ returns:./
4d450 20 31 20 69 66 20 74 68 69 73 20 67 65 6f 6d 65   1 if this geome
4d460 74 72 79 20 68 61 73 20 4d 20 63 6f 6f 72 64 73  try has M coords
4d470 0a 2f 20 30 20 6f 74 68 65 72 77 69 73 65 0a 2f  ./ 0 otherwise./
4d480 20 6f 72 20 2d 31 20 69 66 20 61 6e 79 20 65 72   or -1 if any er
4d490 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
4d4a0 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ed.*/.    unsign
4d4b0 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
4d4c0 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
4d4d0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
4d4e0 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
4d4f0 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
4d500 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
4d510 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
4d520 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
4d530 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
4d540 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
4d550 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
4d560 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
4d570 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20  context, -1);.. 
4d580 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
4d590 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
4d5a0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
4d5b0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
4d5c0 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
4d5d0 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
4d5e0 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
4d5f0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
4d600 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
4d610 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
4d620 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
4d630 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a  ;.    if (!geo).
4d640 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
4d650 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31  int (context, -1
4d660 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20  );.    else.    
4d670 20 20 7b 0a 09 20 20 69 66 20 28 67 65 6f 2d 3e    {..  if (geo->
4d680 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
4d690 3d 20 47 41 49 41 5f 58 59 5f 4d 0a 09 20 20 20  = GAIA_XY_M..   
4d6a0 20 20 20 7c 7c 20 67 65 6f 2d 3e 44 69 6d 65 6e     || geo->Dimen
4d6b0 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49  sionModel == GAI
4d6c0 41 5f 58 59 5f 5a 5f 4d 29 0a 09 20 20 20 20 20  A_XY_Z_M)..     
4d6d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
4d6e0 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31 29  int (context, 1)
4d6f0 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20  ;..  else..     
4d700 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
4d710 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29  int (context, 0)
4d720 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  ;.      }.    ga
4d730 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
4d740 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  geo);.}..static 
4d750 76 6f 69 64 0a 66 6e 63 74 5f 4d 69 6e 5a 20 28  void.fnct_MinZ (
4d760 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
4d770 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
4d780 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
4d790 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
4d7a0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
4d7b0 20 53 54 5f 4d 69 6e 5a 28 42 4c 4f 42 20 65 6e   ST_MinZ(BLOB en
4d7c0 63 6f 64 65 64 20 47 45 4d 45 54 52 59 29 0a 2f  coded GEMETRY)./
4d7d0 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20 4d  ./ returns the M
4d7e0 69 6e 5a 20 63 6f 6f 72 64 69 6e 61 74 65 20 66  inZ coordinate f
4d7f0 6f 72 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65  or current geome
4d800 74 72 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  try ./ or NULL i
4d810 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65  f any error is e
4d820 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20  ncountered.*/.  
4d830 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
4d840 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
4d850 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 64 6f   n_bytes;.    do
4d860 75 62 6c 65 20 6d 69 6e 3b 0a 20 20 20 20 64 6f  uble min;.    do
4d870 75 62 6c 65 20 6d 61 78 3b 0a 20 20 20 20 67 61  uble max;.    ga
4d880 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
4d890 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41  o = NULL;.    GA
4d8a0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
4d8b0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
4d8c0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
4d8d0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
4d8e0 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
4d8f0 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
4d900 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
4d910 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
4d920 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
4d930 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
4d940 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
4d950 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
4d960 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
4d970 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
4d980 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
4d990 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
4d9a0 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
4d9b0 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
4d9c0 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
4d9d0 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
4d9e0 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  geo)..sqlite3_re
4d9f0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
4da00 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  xt);.    else.  
4da10 20 20 20 20 7b 0a 09 20 20 69 66 20 28 67 65 6f      {..  if (geo
4da20 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c  ->DimensionModel
4da30 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 0a 09 20   == GAIA_XY_Z.. 
4da40 20 20 20 20 20 7c 7c 20 67 65 6f 2d 3e 44 69 6d       || geo->Dim
4da50 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47  ensionModel == G
4da60 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 20 20 20  AIA_XY_Z_M)..   
4da70 20 7b 0a 09 09 67 61 69 61 5a 52 61 6e 67 65 47   {...gaiaZRangeG
4da80 65 6f 6d 65 74 72 79 20 28 67 65 6f 2c 20 26 6d  eometry (geo, &m
4da90 69 6e 2c 20 26 6d 61 78 29 3b 0a 09 09 73 71 6c  in, &max);...sql
4daa0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
4dab0 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 6d 69 6e  le (context, min
4dac0 29 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c 73  );..    }..  els
4dad0 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  e..      sqlite3
4dae0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
4daf0 6e 74 65 78 74 29 3b 0a 09 20 20 67 61 69 61 46  ntext);..  gaiaF
4db00 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
4db10 29 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a 0a 73 74  );.      }.}..st
4db20 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d  atic void.fnct_M
4db30 61 78 5a 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  axZ (sqlite3_con
4db40 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
4db50 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
4db60 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
4db70 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
4db80 6f 6e 3a 0a 2f 20 53 54 5f 4d 61 78 5a 28 42 4c  on:./ ST_MaxZ(BL
4db90 4f 42 20 65 6e 63 6f 64 65 64 20 47 45 4d 45 54  OB encoded GEMET
4dba0 52 59 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20  RY)././ returns 
4dbb0 74 68 65 20 4d 61 78 5a 20 63 6f 6f 72 64 69 6e  the MaxZ coordin
4dbc0 61 74 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20  ate for current 
4dbd0 67 65 6f 6d 65 74 72 79 20 0a 2f 20 6f 72 20 4e  geometry ./ or N
4dbe0 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
4dbf0 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
4dc00 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
4dc10 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
4dc20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
4dc30 20 20 20 64 6f 75 62 6c 65 20 6d 69 6e 3b 0a 20     double min;. 
4dc40 20 20 20 64 6f 75 62 6c 65 20 6d 61 78 3b 0a 20     double max;. 
4dc50 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
4dc60 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20  tr geo = NULL;. 
4dc70 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
4dc80 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
4dc90 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
4dca0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
4dcb0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
4dcc0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
4dcd0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
4dce0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
4dcf0 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
4dd00 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
4dd10 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
4dd20 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
4dd30 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
4dd40 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
4dd50 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
4dd60 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
4dd70 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
4dd80 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
4dd90 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
4dda0 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
4ddb0 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
4ddc0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
4ddd0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
4dde0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66  se.      {..  if
4ddf0 20 28 67 65 6f 2d 3e 44 69 6d 65 6e 73 69 6f 6e   (geo->Dimension
4de00 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59  Model == GAIA_XY
4de10 5f 5a 0a 09 20 20 20 20 20 20 7c 7c 20 67 65 6f  _Z..      || geo
4de20 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c  ->DimensionModel
4de30 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29   == GAIA_XY_Z_M)
4de40 0a 09 20 20 20 20 7b 0a 09 09 67 61 69 61 5a 52  ..    {...gaiaZR
4de50 61 6e 67 65 47 65 6f 6d 65 74 72 79 20 28 67 65  angeGeometry (ge
4de60 6f 2c 20 26 6d 69 6e 2c 20 26 6d 61 78 29 3b 0a  o, &min, &max);.
4de70 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
4de80 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74  _double (context
4de90 2c 20 6d 61 78 29 3b 0a 09 20 20 20 20 7d 0a 09  , max);..    }..
4dea0 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71    else..      sq
4deb0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
4dec0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
4ded0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
4dee0 20 28 67 65 6f 29 3b 0a 20 20 20 20 20 20 7d 0a   (geo);.      }.
4def0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
4df00 6e 63 74 5f 4d 69 6e 4d 20 28 73 71 6c 69 74 65  nct_MinM (sqlite
4df10 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
4df20 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
4df30 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
4df40 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
4df50 75 6e 63 74 69 6f 6e 3a 0a 2f 20 53 54 5f 4d 69  unction:./ ST_Mi
4df60 6e 4d 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20  nM(BLOB encoded 
4df70 47 45 4d 45 54 52 59 29 0a 2f 0a 2f 20 72 65 74  GEMETRY)././ ret
4df80 75 72 6e 73 20 74 68 65 20 4d 69 6e 4d 20 63 6f  urns the MinM co
4df90 6f 72 64 69 6e 61 74 65 20 66 6f 72 20 63 75 72  ordinate for cur
4dfa0 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 20 0a 2f  rent geometry ./
4dfb0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
4dfc0 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
4dfd0 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
4dfe0 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
4dff0 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
4e000 65 73 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d  es;.    double m
4e010 69 6e 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d  in;.    double m
4e020 61 78 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  ax;.    gaiaGeom
4e030 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
4e040 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  LL;.    GAIA_UNU
4e050 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
4e060 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
4e070 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
4e080 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
4e090 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
4e0a0 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
4e0b0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
4e0c0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
4e0d0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
4e0e0 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
4e0f0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
4e100 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
4e110 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
4e120 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
4e130 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
4e140 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
4e150 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  o = gaiaFromSpat
4e160 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
4e170 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
4e180 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09  .    if (!geo)..
4e190 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
4e1a0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
4e1b0 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
4e1c0 09 20 20 69 66 20 28 67 65 6f 2d 3e 44 69 6d 65  .  if (geo->Dime
4e1d0 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41  nsionModel == GA
4e1e0 49 41 5f 58 59 5f 4d 0a 09 20 20 20 20 20 20 7c  IA_XY_M..      |
4e1f0 7c 20 67 65 6f 2d 3e 44 69 6d 65 6e 73 69 6f 6e  | geo->Dimension
4e200 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59  Model == GAIA_XY
4e210 5f 5a 5f 4d 29 0a 09 20 20 20 20 7b 0a 09 09 67  _Z_M)..    {...g
4e220 61 69 61 4d 52 61 6e 67 65 47 65 6f 6d 65 74 72  aiaMRangeGeometr
4e230 79 20 28 67 65 6f 2c 20 26 6d 69 6e 2c 20 26 6d  y (geo, &min, &m
4e240 61 78 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72  ax);...sqlite3_r
4e250 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f  esult_double (co
4e260 6e 74 65 78 74 2c 20 6d 69 6e 29 3b 0a 09 20 20  ntext, min);..  
4e270 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20    }..  else..   
4e280 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
4e290 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
4e2a0 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f  ;..  gaiaFreeGeo
4e2b0 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20  mColl (geo);.   
4e2c0 20 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76     }.}..static v
4e2d0 6f 69 64 0a 66 6e 63 74 5f 4d 61 78 4d 20 28 73  oid.fnct_MaxM (s
4e2e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
4e2f0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
4e300 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
4e310 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
4e320 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
4e330 53 54 5f 4d 61 78 4d 28 42 4c 4f 42 20 65 6e 63  ST_MaxM(BLOB enc
4e340 6f 64 65 64 20 47 45 4d 45 54 52 59 29 0a 2f 0a  oded GEMETRY)./.
4e350 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20 4d 61  / returns the Ma
4e360 78 4d 20 63 6f 6f 72 64 69 6e 61 74 65 20 66 6f  xM coordinate fo
4e370 72 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74  r current geomet
4e380 72 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  ry ./ or NULL if
4e390 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
4e3a0 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
4e3b0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
4e3c0 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20  p_blob;.    int 
4e3d0 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 64 6f 75  n_bytes;.    dou
4e3e0 62 6c 65 20 6d 69 6e 3b 0a 20 20 20 20 64 6f 75  ble min;.    dou
4e3f0 62 6c 65 20 6d 61 78 3b 0a 20 20 20 20 67 61 69  ble max;.    gai
4e400 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
4e410 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49   = NULL;.    GAI
4e420 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
4e430 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
4e440 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
4e450 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
4e460 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[0]) != SQLIT
4e470 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a  E_BLOB).      {.
4e480 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
4e490 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
4e4a0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
4e4b0 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20     }.    p_blob 
4e4c0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
4e4d0 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
4e4e0 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
4e4f0 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
4e500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
4e510 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  tes (argv[0]);. 
4e520 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f     geo = gaiaFro
4e530 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
4e540 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
4e550 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
4e560 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  eo)..sqlite3_res
4e570 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
4e580 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  t);.    else.   
4e590 20 20 20 7b 0a 09 20 20 69 66 20 28 67 65 6f 2d     {..  if (geo-
4e5a0 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20  >DimensionModel 
4e5b0 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 0a 09 20 20  == GAIA_XY_M..  
4e5c0 20 20 20 20 7c 7c 20 67 65 6f 2d 3e 44 69 6d 65      || geo->Dime
4e5d0 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41  nsionModel == GA
4e5e0 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 20 20 20 20  IA_XY_Z_M)..    
4e5f0 7b 0a 09 09 67 61 69 61 4d 52 61 6e 67 65 47 65  {...gaiaMRangeGe
4e600 6f 6d 65 74 72 79 20 28 67 65 6f 2c 20 26 6d 69  ometry (geo, &mi
4e610 6e 2c 20 26 6d 61 78 29 3b 0a 09 09 73 71 6c 69  n, &max);...sqli
4e620 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
4e630 65 20 28 63 6f 6e 74 65 78 74 2c 20 6d 61 78 29  e (context, max)
4e640 3b 0a 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65  ;..    }..  else
4e650 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
4e660 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
4e670 74 65 78 74 29 3b 0a 09 20 20 67 61 69 61 46 72  text);..  gaiaFr
4e680 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
4e690 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a 0a 73 74 61  ;.      }.}..sta
4e6a0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 45 6e  tic void.fnct_En
4e6b0 76 65 6c 6f 70 65 20 28 73 71 6c 69 74 65 33 5f  velope (sqlite3_
4e6c0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
4e6d0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
4e6e0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
4e6f0 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
4e700 63 74 69 6f 6e 3a 0a 2f 20 45 6e 76 65 6c 6f 70  ction:./ Envelop
4e710 65 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67  e(BLOB encoded g
4e720 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74  eometry)././ ret
4e730 75 72 6e 73 20 74 68 65 20 4d 42 52 20 66 6f 72  urns the MBR for
4e740 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
4e750 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  y ./ or NULL if 
4e760 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
4e770 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
4e780 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
4e790 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
4e7a0 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
4e7b0 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  len;.    unsigne
4e7c0 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
4e7d0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
4e7e0 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
4e7f0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
4e800 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 62 62 6f  aGeomCollPtr bbo
4e810 78 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79 67  x;.    gaiaPolyg
4e820 6f 6e 50 74 72 20 70 6f 6c 79 67 3b 0a 20 20 20  onPtr polyg;.   
4e830 20 67 61 69 61 52 69 6e 67 50 74 72 20 72 65 63   gaiaRingPtr rec
4e840 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  t;.    GAIA_UNUS
4e850 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
4e860 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
4e870 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
4e880 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d  ue_type (argv[0]
4e890 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  ) != SQLITE_BLOB
4e8a0 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
4e8b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4e8c0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
4e8d0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
4e8e0 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73     p_blob = (uns
4e8f0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71  igned char *) sq
4e900 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
4e910 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
4e920 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65  n_bytes = sqlite
4e930 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61  3_value_bytes (a
4e940 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[0]);.    geo
4e950 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69   = gaiaFromSpati
4e960 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f  aLiteBlobWkb (p_
4e970 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a  blob, n_bytes);.
4e980 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73      if (!geo)..s
4e990 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
4e9a0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ll (context);.  
4e9b0 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
4e9c0 20 20 67 61 69 61 4d 62 72 47 65 6f 6d 65 74 72    gaiaMbrGeometr
4e9d0 79 20 28 67 65 6f 29 3b 0a 09 20 20 62 62 6f 78  y (geo);..  bbox
4e9e0 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d   = gaiaAllocGeom
4e9f0 43 6f 6c 6c 20 28 29 3b 0a 09 20 20 62 62 6f 78  Coll ();..  bbox
4ea00 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72  ->Srid = geo->Sr
4ea10 69 64 3b 0a 09 20 20 70 6f 6c 79 67 20 3d 20 67  id;..  polyg = g
4ea20 61 69 61 41 64 64 50 6f 6c 79 67 6f 6e 54 6f 47  aiaAddPolygonToG
4ea30 65 6f 6d 43 6f 6c 6c 20 28 62 62 6f 78 2c 20 35  eomColl (bbox, 5
4ea40 2c 20 30 29 3b 0a 09 20 20 72 65 63 74 20 3d 20  , 0);..  rect = 
4ea50 70 6f 6c 79 67 2d 3e 45 78 74 65 72 69 6f 72 3b  polyg->Exterior;
4ea60 0a 09 20 20 67 61 69 61 53 65 74 50 6f 69 6e 74  ..  gaiaSetPoint
4ea70 20 28 72 65 63 74 2d 3e 43 6f 6f 72 64 73 2c 20   (rect->Coords, 
4ea80 30 2c 20 67 65 6f 2d 3e 4d 69 6e 58 2c 20 67 65  0, geo->MinX, ge
4ea90 6f 2d 3e 4d 69 6e 59 29 3b 09 2f 2a 20 76 65 72  o->MinY);./* ver
4eaa0 74 65 78 20 23 20 31 20 2a 2f 0a 09 20 20 67 61  tex # 1 */..  ga
4eab0 69 61 53 65 74 50 6f 69 6e 74 20 28 72 65 63 74  iaSetPoint (rect
4eac0 2d 3e 43 6f 6f 72 64 73 2c 20 31 2c 20 67 65 6f  ->Coords, 1, geo
4ead0 2d 3e 4d 61 78 58 2c 20 67 65 6f 2d 3e 4d 69 6e  ->MaxX, geo->Min
4eae0 59 29 3b 09 2f 2a 20 76 65 72 74 65 78 20 23 20  Y);./* vertex # 
4eaf0 32 20 2a 2f 0a 09 20 20 67 61 69 61 53 65 74 50  2 */..  gaiaSetP
4eb00 6f 69 6e 74 20 28 72 65 63 74 2d 3e 43 6f 6f 72  oint (rect->Coor
4eb10 64 73 2c 20 32 2c 20 67 65 6f 2d 3e 4d 61 78 58  ds, 2, geo->MaxX
4eb20 2c 20 67 65 6f 2d 3e 4d 61 78 59 29 3b 09 2f 2a  , geo->MaxY);./*
4eb30 20 76 65 72 74 65 78 20 23 20 33 20 2a 2f 0a 09   vertex # 3 */..
4eb40 20 20 67 61 69 61 53 65 74 50 6f 69 6e 74 20 28    gaiaSetPoint (
4eb50 72 65 63 74 2d 3e 43 6f 6f 72 64 73 2c 20 33 2c  rect->Coords, 3,
4eb60 20 67 65 6f 2d 3e 4d 69 6e 58 2c 20 67 65 6f 2d   geo->MinX, geo-
4eb70 3e 4d 61 78 59 29 3b 09 2f 2a 20 76 65 72 74 65  >MaxY);./* verte
4eb80 78 20 23 20 34 20 2a 2f 0a 09 20 20 67 61 69 61  x # 4 */..  gaia
4eb90 53 65 74 50 6f 69 6e 74 20 28 72 65 63 74 2d 3e  SetPoint (rect->
4eba0 43 6f 6f 72 64 73 2c 20 34 2c 20 67 65 6f 2d 3e  Coords, 4, geo->
4ebb0 4d 69 6e 58 2c 20 67 65 6f 2d 3e 4d 69 6e 59 29  MinX, geo->MinY)
4ebc0 3b 09 2f 2a 20 76 65 72 74 65 78 20 23 20 35 20  ;./* vertex # 5 
4ebd0 5b 73 61 6d 65 20 61 73 20 76 65 72 74 65 78 20  [same as vertex 
4ebe0 23 20 31 20 74 6f 20 63 6c 6f 73 65 20 74 68 65  # 1 to close the
4ebf0 20 70 6f 6c 79 67 6f 6e 5d 20 2a 2f 0a 09 20 20   polygon] */..  
4ec00 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65  gaiaToSpatiaLite
4ec10 42 6c 6f 62 57 6b 62 20 28 62 62 6f 78 2c 20 26  BlobWkb (bbox, &
4ec20 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b  p_result, &len);
4ec30 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d  ..  gaiaFreeGeom
4ec40 43 6f 6c 6c 20 28 62 62 6f 78 29 3b 0a 09 20 20  Coll (bbox);..  
4ec50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
4ec60 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
4ec70 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
4ec80 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
4ec90 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
4eca0 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69   (geo);.}..stati
4ecb0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 45 78 70 61  c void.fnct_Expa
4ecc0 6e 64 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nd (sqlite3_cont
4ecd0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
4ece0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
4ecf0 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
4ed00 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
4ed10 6e 3a 0a 2f 20 53 54 5f 45 78 70 61 6e 64 28 42  n:./ ST_Expand(B
4ed20 4c 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d  LOB encoded geom
4ed30 65 74 72 79 2c 20 64 6f 75 62 6c 65 20 61 6d 6f  etry, double amo
4ed40 75 6e 74 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73  unt)././ returns
4ed50 20 74 68 65 20 4d 42 52 20 66 6f 72 20 63 75 72   the MBR for cur
4ed60 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 20 65 78  rent geometry ex
4ed70 70 61 6e 64 65 64 20 62 79 20 22 61 6d 6f 75 6e  panded by "amoun
4ed80 74 22 20 69 6e 20 65 61 63 68 20 64 69 72 65 63  t" in each direc
4ed90 74 69 6f 6e 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  tion./ or NULL i
4eda0 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65  f any error is e
4edb0 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20  ncountered.*/.  
4edc0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
4edd0 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
4ede0 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e   n_bytes;.    in
4edf0 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67  t len;.    unsig
4ee00 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75  ned char *p_resu
4ee10 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  lt = NULL;.    g
4ee20 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
4ee30 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  eo = NULL;.    g
4ee40 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 62  aiaGeomCollPtr b
4ee50 62 6f 78 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c  box;.    gaiaPol
4ee60 79 67 6f 6e 50 74 72 20 70 6f 6c 79 67 3b 0a 20  ygonPtr polyg;. 
4ee70 20 20 20 67 61 69 61 52 69 6e 67 50 74 72 20 72     gaiaRingPtr r
4ee80 65 63 74 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  ect;.    double 
4ee90 74 69 63 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74  tic;.    int int
4eea0 5f 76 61 6c 75 65 3b 0a 20 20 20 20 47 41 49 41  _value;.    GAIA
4eeb0 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
4eec0 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
4eed0 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
4eee0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
4eef0 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
4ef00 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
4ef10 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
4ef20 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
4ef30 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
4ef40 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69    }.    if (sqli
4ef50 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
4ef60 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49  argv[1]) == SQLI
4ef70 54 45 5f 46 4c 4f 41 54 29 0a 09 74 69 63 20 3d  TE_FLOAT)..tic =
4ef80 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
4ef90 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b  ouble (argv[1]);
4efa0 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71  .    else if (sq
4efb0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
4efc0 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51   (argv[1]) == SQ
4efd0 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
4efe0 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c      {..  int_val
4eff0 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
4f000 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
4f010 3b 0a 09 20 20 74 69 63 20 3d 20 69 6e 74 5f 76  ;..  tic = int_v
4f020 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  alue;.      }.  
4f030 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
4f040 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
4f050 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
4f060 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
4f070 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
4f080 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
4f090 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
4f0a0 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
4f0b0 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
4f0c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
4f0d0 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
4f0e0 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d    geo = gaiaFrom
4f0f0 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
4f100 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
4f110 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65  es);.    if (!ge
4f120 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  o)..sqlite3_resu
4f130 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
4f140 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20  );.    else.    
4f150 20 20 7b 0a 09 20 20 67 61 69 61 4d 62 72 47 65    {..  gaiaMbrGe
4f160 6f 6d 65 74 72 79 20 28 67 65 6f 29 3b 0a 09 20  ometry (geo);.. 
4f170 20 62 62 6f 78 20 3d 20 67 61 69 61 41 6c 6c 6f   bbox = gaiaAllo
4f180 63 47 65 6f 6d 43 6f 6c 6c 20 28 29 3b 0a 09 20  cGeomColl ();.. 
4f190 20 62 62 6f 78 2d 3e 53 72 69 64 20 3d 20 67 65   bbox->Srid = ge
4f1a0 6f 2d 3e 53 72 69 64 3b 0a 09 20 20 70 6f 6c 79  o->Srid;..  poly
4f1b0 67 20 3d 20 67 61 69 61 41 64 64 50 6f 6c 79 67  g = gaiaAddPolyg
4f1c0 6f 6e 54 6f 47 65 6f 6d 43 6f 6c 6c 20 28 62 62  onToGeomColl (bb
4f1d0 6f 78 2c 20 35 2c 20 30 29 3b 0a 09 20 20 72 65  ox, 5, 0);..  re
4f1e0 63 74 20 3d 20 70 6f 6c 79 67 2d 3e 45 78 74 65  ct = polyg->Exte
4f1f0 72 69 6f 72 3b 0a 09 20 20 67 61 69 61 53 65 74  rior;..  gaiaSet
4f200 50 6f 69 6e 74 20 28 72 65 63 74 2d 3e 43 6f 6f  Point (rect->Coo
4f210 72 64 73 2c 20 30 2c 20 67 65 6f 2d 3e 4d 69 6e  rds, 0, geo->Min
4f220 58 20 2d 20 74 69 63 2c 20 67 65 6f 2d 3e 4d 69  X - tic, geo->Mi
4f230 6e 59 20 2d 20 74 69 63 29 3b 09 2f 2a 20 76 65  nY - tic);./* ve
4f240 72 74 65 78 20 23 20 31 20 2a 2f 0a 09 20 20 67  rtex # 1 */..  g
4f250 61 69 61 53 65 74 50 6f 69 6e 74 20 28 72 65 63  aiaSetPoint (rec
4f260 74 2d 3e 43 6f 6f 72 64 73 2c 20 31 2c 20 67 65  t->Coords, 1, ge
4f270 6f 2d 3e 4d 61 78 58 20 2b 20 74 69 63 2c 20 67  o->MaxX + tic, g
4f280 65 6f 2d 3e 4d 69 6e 59 20 2d 20 74 69 63 29 3b  eo->MinY - tic);
4f290 09 2f 2a 20 76 65 72 74 65 78 20 23 20 32 20 2a  ./* vertex # 2 *
4f2a0 2f 0a 09 20 20 67 61 69 61 53 65 74 50 6f 69 6e  /..  gaiaSetPoin
4f2b0 74 20 28 72 65 63 74 2d 3e 43 6f 6f 72 64 73 2c  t (rect->Coords,
4f2c0 20 32 2c 20 67 65 6f 2d 3e 4d 61 78 58 20 2b 20   2, geo->MaxX + 
4f2d0 74 69 63 2c 20 67 65 6f 2d 3e 4d 61 78 59 20 2b  tic, geo->MaxY +
4f2e0 20 74 69 63 29 3b 09 2f 2a 20 76 65 72 74 65 78   tic);./* vertex
4f2f0 20 23 20 33 20 2a 2f 0a 09 20 20 67 61 69 61 53   # 3 */..  gaiaS
4f300 65 74 50 6f 69 6e 74 20 28 72 65 63 74 2d 3e 43  etPoint (rect->C
4f310 6f 6f 72 64 73 2c 20 33 2c 20 67 65 6f 2d 3e 4d  oords, 3, geo->M
4f320 69 6e 58 20 2d 20 74 69 63 2c 20 67 65 6f 2d 3e  inX - tic, geo->
4f330 4d 61 78 59 20 2b 20 74 69 63 29 3b 09 2f 2a 20  MaxY + tic);./* 
4f340 76 65 72 74 65 78 20 23 20 34 20 2a 2f 0a 09 20  vertex # 4 */.. 
4f350 20 67 61 69 61 53 65 74 50 6f 69 6e 74 20 28 72   gaiaSetPoint (r
4f360 65 63 74 2d 3e 43 6f 6f 72 64 73 2c 20 34 2c 20  ect->Coords, 4, 
4f370 67 65 6f 2d 3e 4d 69 6e 58 20 2d 20 74 69 63 2c  geo->MinX - tic,
4f380 20 67 65 6f 2d 3e 4d 69 6e 59 20 2d 20 74 69 63   geo->MinY - tic
4f390 29 3b 09 2f 2a 20 76 65 72 74 65 78 20 23 20 35  );./* vertex # 5
4f3a0 20 5b 73 61 6d 65 20 61 73 20 76 65 72 74 65 78   [same as vertex
4f3b0 20 23 20 31 20 74 6f 20 63 6c 6f 73 65 20 74 68   # 1 to close th
4f3c0 65 20 70 6f 6c 79 67 6f 6e 5d 20 2a 2f 0a 09 20  e polygon] */.. 
4f3d0 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74   gaiaToSpatiaLit
4f3e0 65 42 6c 6f 62 57 6b 62 20 28 62 62 6f 78 2c 20  eBlobWkb (bbox, 
4f3f0 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29  &p_result, &len)
4f400 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f  ;..  gaiaFreeGeo
4f410 6d 43 6f 6c 6c 20 28 62 62 6f 78 29 3b 0a 09 20  mColl (bbox);.. 
4f420 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
4f430 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70  blob (context, p
4f440 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72  _result, len, fr
4f450 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ee);.      }.   
4f460 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
4f470 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74  l (geo);.}..stat
4f480 69 63 20 76 6f 69 64 0a 62 75 69 6c 64 5f 66 69  ic void.build_fi
4f490 6c 74 65 72 5f 6d 62 72 20 28 73 71 6c 69 74 65  lter_mbr (sqlite
4f4a0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
4f4b0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09  ext, int argc,..
4f4c0 09 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
4f4d0 20 2a 2a 20 61 72 67 76 2c 20 69 6e 74 20 6d 6f   ** argv, int mo
4f4e0 64 65 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  de).{./* SQL fun
4f4f0 63 74 69 6f 6e 73 3a 0a 2f 20 42 75 69 6c 64 4d  ctions:./ BuildM
4f500 62 72 46 69 6c 74 65 72 28 64 6f 75 62 6c 65 20  brFilter(double 
4f510 58 31 2c 20 64 6f 75 62 6c 65 20 59 31 2c 20 64  X1, double Y1, d
4f520 6f 75 62 6c 65 20 58 32 2c 20 64 6f 75 62 6c 65  ouble X2, double
4f530 20 59 32 29 0a 2f 20 46 69 6c 74 65 72 4d 42 52   Y2)./ FilterMBR
4f540 57 69 74 68 69 6e 28 64 6f 75 62 6c 65 20 58 31  Within(double X1
4f550 2c 20 64 6f 75 62 6c 65 20 59 31 2c 20 64 6f 75  , double Y1, dou
4f560 62 6c 65 20 58 32 2c 20 64 6f 75 62 6c 65 20 59  ble X2, double Y
4f570 32 29 0a 2f 20 46 69 6c 74 65 72 4d 42 52 43 6f  2)./ FilterMBRCo
4f580 6e 74 61 69 6e 28 64 6f 75 62 6c 65 20 58 31 2c  ntain(double X1,
4f590 20 64 6f 75 62 6c 65 20 59 31 2c 20 64 6f 75 62   double Y1, doub
4f5a0 6c 65 20 58 32 2c 20 64 6f 75 62 6c 65 20 59 32  le X2, double Y2
4f5b0 29 0a 2f 20 46 69 6c 74 65 72 4d 42 52 49 6e 74  )./ FilterMBRInt
4f5c0 65 72 73 65 63 74 73 28 64 6f 75 62 6c 65 20 58  ersects(double X
4f5d0 31 2c 20 64 6f 75 62 6c 65 20 59 31 2c 20 64 6f  1, double Y1, do
4f5e0 75 62 6c 65 20 58 32 2c 20 64 6f 75 62 6c 65 20  uble X2, double 
4f5f0 59 32 29 0a 2f 0a 2f 20 62 75 69 6c 64 73 20 61  Y2)././ builds a
4f600 20 67 65 6e 65 72 69 63 20 66 69 6c 74 65 72 20   generic filter 
4f610 66 6f 72 20 4d 42 52 20 66 72 6f 6d 20 74 77 6f  for MBR from two
4f620 20 70 6f 69 6e 74 73 20 28 69 64 65 6e 74 69 66   points (identif
4f630 79 69 6e 67 20 61 20 72 65 63 74 61 6e 67 6c 65  ying a rectangle
4f640 27 73 20 64 69 61 67 6f 6e 61 6c 29 20 0a 2f 20  's diagonal) ./ 
4f650 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65  or NULL if any e
4f660 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
4f670 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  red.*/.    int l
4f680 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  en;.    unsigned
4f690 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
4f6a0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62  = NULL;.    doub
4f6b0 6c 65 20 78 31 3b 0a 20 20 20 20 64 6f 75 62 6c  le x1;.    doubl
4f6c0 65 20 79 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65  e y1;.    double
4f6d0 20 78 32 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20   x2;.    double 
4f6e0 79 32 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f  y2;.    int int_
4f6f0 76 61 6c 75 65 3b 0a 20 20 20 20 47 41 49 41 5f  value;.    GAIA_
4f700 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
4f710 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
4f720 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
4f730 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
4f740 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[0]) == SQLITE_
4f750 46 4c 4f 41 54 29 0a 09 78 31 20 3d 20 73 71 6c  FLOAT)..x1 = sql
4f760 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
4f770 65 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  e (argv[0]);.   
4f780 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
4f790 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
4f7a0 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[0]) == SQLITE
4f7b0 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
4f7c0 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
4f7d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
4f7e0 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20  nt (argv[0]);.. 
4f7f0 20 78 31 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b   x1 = int_value;
4f800 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
4f810 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
4f820 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4f830 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
4f840 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
4f850 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
4f860 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
4f870 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c  1]) == SQLITE_FL
4f880 4f 41 54 29 0a 09 79 31 20 3d 20 73 71 6c 69 74  OAT)..y1 = sqlit
4f890 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
4f8a0 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65  (argv[1]);.    e
4f8b0 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f  lse if (sqlite3_
4f8c0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
4f8d0 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [1]) == SQLITE_I
4f8e0 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
4f8f0 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73  .  int_value = s
4f900 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
4f910 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 79   (argv[1]);..  y
4f920 31 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20  1 = int_value;. 
4f930 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
4f940 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
4f950 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
4f960 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
4f970 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
4f980 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
4f990 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d  ue_type (argv[2]
4f9a0 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41  ) == SQLITE_FLOA
4f9b0 54 29 0a 09 78 32 20 3d 20 73 71 6c 69 74 65 33  T)..x2 = sqlite3
4f9c0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61  _value_double (a
4f9d0 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[2]);.    els
4f9e0 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
4f9f0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32  lue_type (argv[2
4fa00 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
4fa10 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
4fa20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
4fa30 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
4fa40 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20 78 32 20  argv[2]);..  x2 
4fa50 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  = int_value;.   
4fa60 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20     }.    else.  
4fa70 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
4fa80 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
4fa90 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
4faa0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
4fab0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
4fac0 5f 74 79 70 65 20 28 61 72 67 76 5b 33 5d 29 20  _type (argv[3]) 
4fad0 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29  == SQLITE_FLOAT)
4fae0 0a 09 79 32 20 3d 20 73 71 6c 69 74 65 33 5f 76  ..y2 = sqlite3_v
4faf0 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67  alue_double (arg
4fb00 76 5b 33 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20  v[3]);.    else 
4fb10 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
4fb20 65 5f 74 79 70 65 20 28 61 72 67 76 5b 33 5d 29  e_type (argv[3])
4fb30 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
4fb40 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  ER).      {..  i
4fb50 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  nt_value = sqlit
4fb60 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
4fb70 67 76 5b 33 5d 29 3b 0a 09 20 20 79 32 20 3d 20  gv[3]);..  y2 = 
4fb80 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20  int_value;.     
4fb90 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20   }.    else.    
4fba0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
4fbb0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
4fbc0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
4fbd0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
4fbe0 61 42 75 69 6c 64 46 69 6c 74 65 72 4d 62 72 20  aBuildFilterMbr 
4fbf0 28 78 31 2c 20 79 31 2c 20 78 32 2c 20 79 32 2c  (x1, y1, x2, y2,
4fc00 20 6d 6f 64 65 2c 20 26 70 5f 72 65 73 75 6c 74   mode, &p_result
4fc10 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20 69 66 20  , &len);.    if 
4fc20 28 21 70 5f 72 65 73 75 6c 74 29 0a 09 73 71 6c  (!p_result)..sql
4fc30 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
4fc40 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
4fc50 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65  else..sqlite3_re
4fc60 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
4fc70 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
4fc80 6e 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a  n, free);.}../*.
4fc90 2f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  / the following 
4fca0 66 75 6e 63 74 69 6f 6e 73 20 73 69 6d 70 6c 79  functions simply
4fcb0 20 72 65 61 64 64 72 65 73 73 20 74 68 65 20 72   readdress the r
4fcc0 65 71 75 65 73 74 20 74 6f 20 62 75 69 6c 64 5f  equest to build_
4fcd0 66 69 6c 74 65 72 5f 6d 62 72 28 29 0a 2f 20 73  filter_mbr()./ s
4fce0 65 74 74 69 6e 67 20 74 68 65 20 61 70 70 72 6f  etting the appro
4fcf0 70 72 69 61 74 65 20 4d 4f 44 65 0a 2a 2f 0a 0a  priate MODe.*/..
4fd00 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
4fd10 5f 42 75 69 6c 64 4d 62 72 46 69 6c 74 65 72 20  _BuildMbrFilter 
4fd20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
4fd30 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
4fd40 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
4fd50 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20  lue ** argv).{. 
4fd60 20 20 20 62 75 69 6c 64 5f 66 69 6c 74 65 72 5f     build_filter_
4fd70 6d 62 72 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  mbr (context, ar
4fd80 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 46  gc, argv, GAIA_F
4fd90 49 4c 54 45 52 5f 4d 42 52 5f 44 45 43 4c 41 52  ILTER_MBR_DECLAR
4fda0 45 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  E);.}..static vo
4fdb0 69 64 0a 66 6e 63 74 5f 46 69 6c 74 65 72 4d 62  id.fnct_FilterMb
4fdc0 72 57 69 74 68 69 6e 20 28 73 71 6c 69 74 65 33  rWithin (sqlite3
4fdd0 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
4fde0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09  xt, int argc,...
4fdf0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
4fe00 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20  lue ** argv).{. 
4fe10 20 20 20 62 75 69 6c 64 5f 66 69 6c 74 65 72 5f     build_filter_
4fe20 6d 62 72 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  mbr (context, ar
4fe30 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 46  gc, argv, GAIA_F
4fe40 49 4c 54 45 52 5f 4d 42 52 5f 57 49 54 48 49 4e  ILTER_MBR_WITHIN
4fe50 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
4fe60 64 0a 66 6e 63 74 5f 46 69 6c 74 65 72 4d 62 72  d.fnct_FilterMbr
4fe70 43 6f 6e 74 61 69 6e 73 20 28 73 71 6c 69 74 65  Contains (sqlite
4fe80 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
4fe90 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09  ext, int argc,..
4fea0 09 09 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  ..sqlite3_value 
4feb0 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 62  ** argv).{.    b
4fec0 75 69 6c 64 5f 66 69 6c 74 65 72 5f 6d 62 72 20  uild_filter_mbr 
4fed0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
4fee0 61 72 67 76 2c 20 47 41 49 41 5f 46 49 4c 54 45  argv, GAIA_FILTE
4fef0 52 5f 4d 42 52 5f 43 4f 4e 54 41 49 4e 53 29 3b  R_MBR_CONTAINS);
4ff00 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
4ff10 66 6e 63 74 5f 46 69 6c 74 65 72 4d 62 72 49 6e  fnct_FilterMbrIn
4ff20 74 65 72 73 65 63 74 73 20 28 73 71 6c 69 74 65  tersects (sqlite
4ff30 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
4ff40 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09  ext, int argc,..
4ff50 09 09 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ..  sqlite3_valu
4ff60 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20  e ** argv).{.   
4ff70 20 62 75 69 6c 64 5f 66 69 6c 74 65 72 5f 6d 62   build_filter_mb
4ff80 72 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  r (context, argc
4ff90 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 46 49 4c  , argv, GAIA_FIL
4ffa0 54 45 52 5f 4d 42 52 5f 49 4e 54 45 52 53 45 43  TER_MBR_INTERSEC
4ffb0 54 53 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  TS);.}..static v
4ffc0 6f 69 64 0a 66 6e 63 74 5f 42 75 69 6c 64 4d 62  oid.fnct_BuildMb
4ffd0 72 31 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  r1 (sqlite3_cont
4ffe0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
4fff0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
50000 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
50010 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
50020 6e 3a 0a 2f 20 42 75 69 6c 64 4d 42 52 28 64 6f  n:./ BuildMBR(do
50030 75 62 6c 65 20 58 31 2c 20 64 6f 75 62 6c 65 20  uble X1, double 
50040 59 31 2c 20 64 6f 75 62 6c 65 20 58 32 2c 20 64  Y1, double X2, d
50050 6f 75 62 6c 65 20 59 32 29 0a 2f 0a 2f 20 62 75  ouble Y2)././ bu
50060 69 6c 64 73 20 61 6e 20 4d 42 52 20 66 72 6f 6d  ilds an MBR from
50070 20 74 77 6f 20 70 6f 69 6e 74 73 20 28 69 64 65   two points (ide
50080 6e 74 69 66 79 69 6e 67 20 61 20 72 65 63 74 61  ntifying a recta
50090 6e 67 6c 65 27 73 20 64 69 61 67 6f 6e 61 6c 29  ngle's diagonal)
500a0 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61   ./ or NULL if a
500b0 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
500c0 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69  untered.*/.    i
500d0 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69  nt len;.    unsi
500e0 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
500f0 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ult = NULL;.    
50100 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20 20 20 64  double x1;.    d
50110 6f 75 62 6c 65 20 79 31 3b 0a 20 20 20 20 64 6f  ouble y1;.    do
50120 75 62 6c 65 20 78 32 3b 0a 20 20 20 20 64 6f 75  uble x2;.    dou
50130 62 6c 65 20 79 32 3b 0a 20 20 20 20 69 6e 74 20  ble y2;.    int 
50140 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 47  int_value;.    G
50150 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
50160 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
50170 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
50180 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
50190 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c  (argv[0]) == SQL
501a0 49 54 45 5f 46 4c 4f 41 54 29 0a 09 78 31 20 3d  ITE_FLOAT)..x1 =
501b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
501c0 6f 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 3b  ouble (argv[0]);
501d0 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71  .    else if (sq
501e0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
501f0 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
50200 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
50210 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c      {..  int_val
50220 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
50230 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29  ue_int (argv[0])
50240 3b 0a 09 20 20 78 31 20 3d 20 69 6e 74 5f 76 61  ;..  x1 = int_va
50250 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lue;.      }.   
50260 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
50270 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
50280 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
50290 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
502a0 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74   }.    if (sqlit
502b0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
502c0 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[1]) == SQLIT
502d0 45 5f 46 4c 4f 41 54 29 0a 09 79 31 20 3d 20 73  E_FLOAT)..y1 = s
502e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
502f0 62 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20  ble (argv[1]);. 
50300 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69     else if (sqli
50310 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
50320 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49  argv[1]) == SQLI
50330 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20  TE_INTEGER).    
50340 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65    {..  int_value
50350 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
50360 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a  _int (argv[1]);.
50370 09 20 20 79 31 20 3d 20 69 6e 74 5f 76 61 6c 75  .  y1 = int_valu
50380 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65  e;.      }.    e
50390 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  lse.      {..  s
503a0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
503b0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
503c0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
503d0 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
503e0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
503f0 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[2]) == SQLITE_
50400 46 4c 4f 41 54 29 0a 09 78 32 20 3d 20 73 71 6c  FLOAT)..x2 = sql
50410 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
50420 65 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  e (argv[2]);.   
50430 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
50440 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
50450 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[2]) == SQLITE
50460 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
50470 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
50480 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
50490 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20  nt (argv[2]);.. 
504a0 20 78 32 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b   x2 = int_value;
504b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
504c0 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
504d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
504e0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
504f0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
50500 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
50510 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
50520 33 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c  3]) == SQLITE_FL
50530 4f 41 54 29 0a 09 79 32 20 3d 20 73 71 6c 69 74  OAT)..y2 = sqlit
50540 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
50550 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 65  (argv[3]);.    e
50560 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f  lse if (sqlite3_
50570 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
50580 5b 33 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [3]) == SQLITE_I
50590 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
505a0 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73  .  int_value = s
505b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
505c0 20 28 61 72 67 76 5b 33 5d 29 3b 0a 09 20 20 79   (argv[3]);..  y
505d0 32 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20  2 = int_value;. 
505e0 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
505f0 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
50600 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
50610 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
50620 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
50630 20 67 61 69 61 42 75 69 6c 64 4d 62 72 20 28 78   gaiaBuildMbr (x
50640 31 2c 20 79 31 2c 20 78 32 2c 20 79 32 2c 20 2d  1, y1, x2, y2, -
50650 31 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c  1, &p_result, &l
50660 65 6e 29 3b 0a 20 20 20 20 69 66 20 28 21 70 5f  en);.    if (!p_
50670 72 65 73 75 6c 74 29 0a 09 73 71 6c 69 74 65 33  result)..sqlite3
50680 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
50690 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
506a0 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
506b0 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20  _blob (context, 
506c0 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66  p_result, len, f
506d0 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ree);.}..static 
506e0 76 6f 69 64 0a 66 6e 63 74 5f 42 75 69 6c 64 4d  void.fnct_BuildM
506f0 62 72 32 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  br2 (sqlite3_con
50700 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
50710 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
50720 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
50730 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
50740 6f 6e 3a 0a 2f 20 42 75 69 6c 64 4d 42 52 28 64  on:./ BuildMBR(d
50750 6f 75 62 6c 65 20 58 31 2c 20 64 6f 75 62 6c 65  ouble X1, double
50760 20 59 31 2c 20 64 6f 75 62 6c 65 20 58 32 2c 20   Y1, double X2, 
50770 64 6f 75 62 6c 65 20 59 32 2c 20 69 6e 74 20 53  double Y2, int S
50780 52 49 44 29 0a 2f 0a 2f 20 62 75 69 6c 64 73 20  RID)././ builds 
50790 61 6e 20 4d 42 52 20 66 72 6f 6d 20 74 77 6f 20  an MBR from two 
507a0 70 6f 69 6e 74 73 20 28 69 64 65 6e 74 69 66 79  points (identify
507b0 69 6e 67 20 61 20 72 65 63 74 61 6e 67 6c 65 27  ing a rectangle'
507c0 73 20 64 69 61 67 6f 6e 61 6c 29 20 0a 2f 20 6f  s diagonal) ./ o
507d0 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72  r NULL if any er
507e0 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
507f0 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65  ed.*/.    int le
50800 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  n;.    unsigned 
50810 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
50820 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62 6c   NULL;.    doubl
50830 65 20 78 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65  e x1;.    double
50840 20 79 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20   y1;.    double 
50850 78 32 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79  x2;.    double y
50860 32 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76  2;.    int int_v
50870 61 6c 75 65 3b 0a 20 20 20 20 69 6e 74 20 73 72  alue;.    int sr
50880 69 64 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  id;.    GAIA_UNU
50890 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
508a0 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
508b0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
508c0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
508d0 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f  ]) == SQLITE_FLO
508e0 41 54 29 0a 09 78 31 20 3d 20 73 71 6c 69 74 65  AT)..x1 = sqlite
508f0 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28  3_value_double (
50900 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 65 6c  argv[0]);.    el
50910 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  se if (sqlite3_v
50920 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
50930 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  0]) == SQLITE_IN
50940 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09  TEGER).      {..
50950 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71    int_value = sq
50960 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
50970 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20 78 31  (argv[0]);..  x1
50980 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20   = int_value;.  
50990 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20      }.    else. 
509a0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
509b0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
509c0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
509d0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
509e0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
509f0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29  e_type (argv[1])
50a00 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54   == SQLITE_FLOAT
50a10 29 0a 09 79 31 20 3d 20 73 71 6c 69 74 65 33 5f  )..y1 = sqlite3_
50a20 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72  value_double (ar
50a30 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73 65  gv[1]);.    else
50a40 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
50a50 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d  ue_type (argv[1]
50a60 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  ) == SQLITE_INTE
50a70 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  GER).      {..  
50a80 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69  int_value = sqli
50a90 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61  te3_value_int (a
50aa0 72 67 76 5b 31 5d 29 3b 0a 09 20 20 79 31 20 3d  rgv[1]);..  y1 =
50ab0 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20   int_value;.    
50ac0 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20    }.    else.   
50ad0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
50ae0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
50af0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
50b00 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
50b10 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
50b20 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d  type (argv[2]) =
50b30 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a  = SQLITE_FLOAT).
50b40 09 78 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  .x2 = sqlite3_va
50b50 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76  lue_double (argv
50b60 5b 32 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69  [2]);.    else i
50b70 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
50b80 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20  _type (argv[2]) 
50b90 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
50ba0 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e  R).      {..  in
50bb0 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  t_value = sqlite
50bc0 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
50bd0 76 5b 32 5d 29 3b 0a 09 20 20 78 32 20 3d 20 69  v[2]);..  x2 = i
50be0 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  nt_value;.      
50bf0 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  }.    else.     
50c00 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
50c10 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
50c20 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
50c30 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
50c40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
50c50 70 65 20 28 61 72 67 76 5b 33 5d 29 20 3d 3d 20  pe (argv[3]) == 
50c60 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 79  SQLITE_FLOAT)..y
50c70 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  2 = sqlite3_valu
50c80 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 33  e_double (argv[3
50c90 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20  ]);.    else if 
50ca0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
50cb0 79 70 65 20 28 61 72 67 76 5b 33 5d 29 20 3d 3d  ype (argv[3]) ==
50cc0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
50cd0 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f  .      {..  int_
50ce0 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  value = sqlite3_
50cf0 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
50d00 33 5d 29 3b 0a 09 20 20 79 32 20 3d 20 69 6e 74  3]);..  y2 = int
50d10 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a  _value;.      }.
50d20 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
50d30 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
50d40 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
50d50 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
50d60 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71      }.    if (sq
50d70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
50d80 20 28 61 72 67 76 5b 34 5d 29 20 3d 3d 20 53 51   (argv[4]) == SQ
50d90 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 09 73  LITE_INTEGER)..s
50da0 72 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  rid = sqlite3_va
50db0 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 34 5d  lue_int (argv[4]
50dc0 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20  );.    else.    
50dd0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
50de0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
50df0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
50e00 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
50e10 61 42 75 69 6c 64 4d 62 72 20 28 78 31 2c 20 79  aBuildMbr (x1, y
50e20 31 2c 20 78 32 2c 20 79 32 2c 20 73 72 69 64 2c  1, x2, y2, srid,
50e30 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e   &p_result, &len
50e40 29 3b 0a 20 20 20 20 69 66 20 28 21 70 5f 72 65  );.    if (!p_re
50e50 73 75 6c 74 29 0a 09 73 71 6c 69 74 65 33 5f 72  sult)..sqlite3_r
50e60 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
50e70 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 09  ext);.    else..
50e80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
50e90 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
50ea0 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
50eb0 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  e);.}..static vo
50ec0 69 64 0a 66 6e 63 74 5f 42 75 69 6c 64 43 69 72  id.fnct_BuildCir
50ed0 63 6c 65 4d 62 72 31 20 28 73 71 6c 69 74 65 33  cleMbr1 (sqlite3
50ee0 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
50ef0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09  xt, int argc,...
50f00 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
50f10 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
50f20 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
50f30 2f 20 42 75 69 6c 64 43 69 72 63 6c 65 4d 42 52  / BuildCircleMBR
50f40 28 64 6f 75 62 6c 65 20 58 2c 20 64 6f 75 62 6c  (double X, doubl
50f50 65 20 59 2c 20 64 6f 75 62 6c 65 20 72 61 64 69  e Y, double radi
50f60 75 73 29 0a 2f 0a 2f 20 62 75 69 6c 64 73 20 61  us)././ builds a
50f70 6e 20 4d 42 52 20 66 72 6f 6d 20 74 77 6f 20 70  n MBR from two p
50f80 6f 69 6e 74 73 20 28 69 64 65 6e 74 69 66 79 69  oints (identifyi
50f90 6e 67 20 61 20 72 65 63 74 61 6e 67 6c 65 27 73  ng a rectangle's
50fa0 20 64 69 61 67 6f 6e 61 6c 29 20 0a 2f 20 6f 72   diagonal) ./ or
50fb0 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
50fc0 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
50fd0 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e  d.*/.    int len
50fe0 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
50ff0 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20  har *p_result = 
51000 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62 6c 65  NULL;.    double
51010 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79   x;.    double y
51020 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 61 64  ;.    double rad
51030 69 75 73 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74  ius;.    int int
51040 5f 76 61 6c 75 65 3b 0a 20 20 20 20 47 41 49 41  _value;.    GAIA
51050 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
51060 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
51070 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
51080 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
51090 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[0]) == SQLITE
510a0 5f 46 4c 4f 41 54 29 0a 09 78 20 3d 20 73 71 6c  _FLOAT)..x = sql
510b0 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
510c0 65 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  e (argv[0]);.   
510d0 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
510e0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
510f0 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[0]) == SQLITE
51100 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
51110 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
51120 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
51130 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20  nt (argv[0]);.. 
51140 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a   x = int_value;.
51150 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
51160 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
51170 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
51180 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
51190 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
511a0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
511b0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
511c0 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f  ]) == SQLITE_FLO
511d0 41 54 29 0a 09 79 20 3d 20 73 71 6c 69 74 65 33  AT)..y = sqlite3
511e0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61  _value_double (a
511f0 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[1]);.    els
51200 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
51210 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
51220 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
51230 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
51240 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
51250 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
51260 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 79 20 3d  argv[1]);..  y =
51270 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20   int_value;.    
51280 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20    }.    else.   
51290 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
512a0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
512b0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
512c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
512d0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
512e0 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d  type (argv[2]) =
512f0 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a  = SQLITE_FLOAT).
51300 09 72 61 64 69 75 73 20 3d 20 73 71 6c 69 74 65  .radius = sqlite
51310 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28  3_value_double (
51320 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 65 6c  argv[2]);.    el
51330 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  se if (sqlite3_v
51340 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
51350 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  2]) == SQLITE_IN
51360 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09  TEGER).      {..
51370 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71    int_value = sq
51380 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
51390 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20 72 61  (argv[2]);..  ra
513a0 64 69 75 73 20 3d 20 69 6e 74 5f 76 61 6c 75 65  dius = int_value
513b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c  ;.      }.    el
513c0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  se.      {..  sq
513d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
513e0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
513f0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
51400 20 20 20 20 67 61 69 61 42 75 69 6c 64 43 69 72      gaiaBuildCir
51410 63 6c 65 4d 62 72 20 28 78 2c 20 79 2c 20 72 61  cleMbr (x, y, ra
51420 64 69 75 73 2c 20 2d 31 2c 20 26 70 5f 72 65 73  dius, -1, &p_res
51430 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20  ult, &len);.    
51440 69 66 20 28 21 70 5f 72 65 73 75 6c 74 29 0a 09  if (!p_result)..
51450 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
51460 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
51470 20 20 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33     else..sqlite3
51480 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
51490 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
514a0 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 7d 0a 0a   len, free);.}..
514b0 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
514c0 5f 42 75 69 6c 64 43 69 72 63 6c 65 4d 62 72 32  _BuildCircleMbr2
514d0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
514e0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
514f0 20 61 72 67 63 2c 0a 09 09 20 20 20 20 20 20 73   argc,...      s
51500 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
51510 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
51520 75 6e 63 74 69 6f 6e 3a 0a 2f 20 42 75 69 6c 64  unction:./ Build
51530 43 69 72 63 6c 65 4d 42 52 28 64 6f 75 62 6c 65  CircleMBR(double
51540 20 58 2c 20 64 6f 75 62 6c 65 20 59 2c 20 64 6f   X, double Y, do
51550 75 62 6c 65 20 72 61 64 69 75 73 2c 20 69 6e 74  uble radius, int
51560 20 53 52 49 44 29 0a 2f 0a 2f 20 62 75 69 6c 64   SRID)././ build
51570 73 20 61 6e 20 4d 42 52 20 66 72 6f 6d 20 74 77  s an MBR from tw
51580 6f 20 70 6f 69 6e 74 73 20 28 69 64 65 6e 74 69  o points (identi
51590 66 79 69 6e 67 20 61 20 72 65 63 74 61 6e 67 6c  fying a rectangl
515a0 65 27 73 20 64 69 61 67 6f 6e 61 6c 29 20 0a 2f  e's diagonal) ./
515b0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
515c0 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
515d0 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20  ered.*/.    int 
515e0 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  len;.    unsigne
515f0 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
51600 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75   = NULL;.    dou
51610 62 6c 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c  ble x;.    doubl
51620 65 20 79 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  e y;.    double 
51630 72 61 64 69 75 73 3b 0a 20 20 20 20 69 6e 74 20  radius;.    int 
51640 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 69  int_value;.    i
51650 6e 74 20 73 72 69 64 3b 0a 20 20 20 20 47 41 49  nt srid;.    GAI
51660 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
51670 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
51680 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
51690 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
516a0 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[0]) == SQLIT
516b0 45 5f 46 4c 4f 41 54 29 0a 09 78 20 3d 20 73 71  E_FLOAT)..x = sq
516c0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
516d0 6c 65 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  le (argv[0]);.  
516e0 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74    else if (sqlit
516f0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
51700 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[0]) == SQLIT
51710 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
51720 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20   {..  int_value 
51730 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
51740 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09  int (argv[0]);..
51750 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b    x = int_value;
51760 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
51770 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
51780 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
51790 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
517a0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
517b0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
517c0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
517d0 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c  1]) == SQLITE_FL
517e0 4f 41 54 29 0a 09 79 20 3d 20 73 71 6c 69 74 65  OAT)..y = sqlite
517f0 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28  3_value_double (
51800 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c  argv[1]);.    el
51810 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  se if (sqlite3_v
51820 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
51830 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  1]) == SQLITE_IN
51840 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09  TEGER).      {..
51850 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71    int_value = sq
51860 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
51870 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 79 20  (argv[1]);..  y 
51880 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  = int_value;.   
51890 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20     }.    else.  
518a0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
518b0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
518c0 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
518d0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
518e0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
518f0 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20  _type (argv[2]) 
51900 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29  == SQLITE_FLOAT)
51910 0a 09 72 61 64 69 75 73 20 3d 20 73 71 6c 69 74  ..radius = sqlit
51920 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
51930 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 65  (argv[2]);.    e
51940 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f  lse if (sqlite3_
51950 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
51960 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [2]) == SQLITE_I
51970 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
51980 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73  .  int_value = s
51990 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
519a0 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20 72   (argv[2]);..  r
519b0 61 64 69 75 73 20 3d 20 69 6e 74 5f 76 61 6c 75  adius = int_valu
519c0 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65  e;.      }.    e
519d0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  lse.      {..  s
519e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
519f0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
51a00 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
51a10 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
51a20 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
51a30 76 5b 33 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[3]) == SQLITE_
51a40 49 4e 54 45 47 45 52 29 0a 09 73 72 69 64 20 3d  INTEGER)..srid =
51a50 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
51a60 6e 74 20 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20  nt (argv[3]);.  
51a70 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
51a80 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
51a90 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
51aa0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
51ab0 20 20 7d 0a 20 20 20 20 67 61 69 61 42 75 69 6c    }.    gaiaBuil
51ac0 64 43 69 72 63 6c 65 4d 62 72 20 28 78 2c 20 79  dCircleMbr (x, y
51ad0 2c 20 72 61 64 69 75 73 2c 20 73 72 69 64 2c 20  , radius, srid, 
51ae0 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29  &p_result, &len)
51af0 3b 0a 20 20 20 20 69 66 20 28 21 70 5f 72 65 73  ;.    if (!p_res
51b00 75 6c 74 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  ult)..sqlite3_re
51b10 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
51b20 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 09 73  xt);.    else..s
51b30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
51b40 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72  ob (context, p_r
51b50 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65  esult, len, free
51b60 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
51b70 64 0a 66 6e 63 74 5f 45 78 74 65 6e 74 5f 73 74  d.fnct_Extent_st
51b80 65 70 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep (sqlite3_cont
51b90 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
51ba0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
51bb0 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
51bc0 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
51bd0 6e 3a 0a 2f 20 45 78 74 65 6e 74 28 42 4c 4f 42  n:./ Extent(BLOB
51be0 65 6e 63 6f 64 65 64 20 67 65 6f 6d 29 0a 2f 0a  encoded geom)./.
51bf0 2f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  / aggregate func
51c00 74 69 6f 6e 20 2d 20 53 54 45 50 0a 2f 0a 2a 2f  tion - STEP./.*/
51c10 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
51c20 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20  ar *p_blob;.    
51c30 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
51c40 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
51c50 20 67 65 6f 6d 3b 0a 20 20 20 20 64 6f 75 62 6c   geom;.    doubl
51c60 65 20 2a 2a 70 3b 0a 20 20 20 20 64 6f 75 62 6c  e **p;.    doubl
51c70 65 20 2a 6d 61 78 5f 6d 69 6e 3b 0a 20 20 20 20  e *max_min;.    
51c80 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
51c90 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
51ca0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
51cb0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
51cc0 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
51cd0 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
51ce0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
51cf0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
51d00 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
51d10 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
51d20 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
51d30 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
51d40 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
51d50 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
51d60 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
51d70 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
51d80 3b 0a 20 20 20 20 67 65 6f 6d 20 3d 20 67 61 69  ;.    geom = gai
51d90 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42  aFromSpatiaLiteB
51da0 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20  lobWkb (p_blob, 
51db0 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66  n_bytes);.    if
51dc0 20 28 21 67 65 6f 6d 29 0a 09 72 65 74 75 72 6e   (!geom)..return
51dd0 3b 0a 20 20 20 20 67 61 69 61 4d 62 72 47 65 6f  ;.    gaiaMbrGeo
51de0 6d 65 74 72 79 20 28 67 65 6f 6d 29 3b 0a 20 20  metry (geom);.  
51df0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67    p = sqlite3_ag
51e00 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20  gregate_context 
51e10 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66  (context, sizeof
51e20 20 28 64 6f 75 62 6c 65 20 2a 2a 29 29 3b 0a 20   (double **));. 
51e30 20 20 20 69 66 20 28 21 28 2a 70 29 29 0a 20 20     if (!(*p)).  
51e40 20 20 20 20 7b 0a 09 20 20 2f 2a 20 74 68 69 73      {..  /* this
51e50 20 69 73 20 74 68 65 20 66 69 72 73 74 20 72 6f   is the first ro
51e60 77 20 2a 2f 0a 09 20 20 6d 61 78 5f 6d 69 6e 20  w */..  max_min 
51e70 3d 20 6d 61 6c 6c 6f 63 20 28 73 69 7a 65 6f 66  = malloc (sizeof
51e80 20 28 64 6f 75 62 6c 65 29 20 2a 20 34 29 3b 0a   (double) * 4);.
51e90 09 20 20 2a 28 6d 61 78 5f 6d 69 6e 20 2b 20 30  .  *(max_min + 0
51ea0 29 20 3d 20 67 65 6f 6d 2d 3e 4d 69 6e 58 3b 0a  ) = geom->MinX;.
51eb0 09 20 20 2a 28 6d 61 78 5f 6d 69 6e 20 2b 20 31  .  *(max_min + 1
51ec0 29 20 3d 20 67 65 6f 6d 2d 3e 4d 69 6e 59 3b 0a  ) = geom->MinY;.
51ed0 09 20 20 2a 28 6d 61 78 5f 6d 69 6e 20 2b 20 32  .  *(max_min + 2
51ee0 29 20 3d 20 67 65 6f 6d 2d 3e 4d 61 78 58 3b 0a  ) = geom->MaxX;.
51ef0 09 20 20 2a 28 6d 61 78 5f 6d 69 6e 20 2b 20 33  .  *(max_min + 3
51f00 29 20 3d 20 67 65 6f 6d 2d 3e 4d 61 78 59 3b 0a  ) = geom->MaxY;.
51f10 09 20 20 2a 70 20 3d 20 6d 61 78 5f 6d 69 6e 3b  .  *p = max_min;
51f20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
51f30 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20  e.      {..  /* 
51f40 73 75 62 73 65 71 75 65 6e 74 20 72 6f 77 73 20  subsequent rows 
51f50 2a 2f 0a 09 20 20 6d 61 78 5f 6d 69 6e 20 3d 20  */..  max_min = 
51f60 2a 70 3b 0a 09 20 20 69 66 20 28 67 65 6f 6d 2d  *p;..  if (geom-
51f70 3e 4d 69 6e 58 20 3c 20 2a 28 6d 61 78 5f 6d 69  >MinX < *(max_mi
51f80 6e 20 2b 20 30 29 29 0a 09 20 20 20 20 20 20 2a  n + 0))..      *
51f90 28 6d 61 78 5f 6d 69 6e 20 2b 20 30 29 20 3d 20  (max_min + 0) = 
51fa0 67 65 6f 6d 2d 3e 4d 69 6e 58 3b 0a 09 20 20 69  geom->MinX;..  i
51fb0 66 20 28 67 65 6f 6d 2d 3e 4d 69 6e 59 20 3c 20  f (geom->MinY < 
51fc0 2a 28 6d 61 78 5f 6d 69 6e 20 2b 20 31 29 29 0a  *(max_min + 1)).
51fd0 09 20 20 20 20 20 20 2a 28 6d 61 78 5f 6d 69 6e  .      *(max_min
51fe0 20 2b 20 31 29 20 3d 20 67 65 6f 6d 2d 3e 4d 69   + 1) = geom->Mi
51ff0 6e 59 3b 0a 09 20 20 69 66 20 28 67 65 6f 6d 2d  nY;..  if (geom-
52000 3e 4d 61 78 58 20 3e 20 2a 28 6d 61 78 5f 6d 69  >MaxX > *(max_mi
52010 6e 20 2b 20 32 29 29 0a 09 20 20 20 20 20 20 2a  n + 2))..      *
52020 28 6d 61 78 5f 6d 69 6e 20 2b 20 32 29 20 3d 20  (max_min + 2) = 
52030 67 65 6f 6d 2d 3e 4d 61 78 58 3b 0a 09 20 20 69  geom->MaxX;..  i
52040 66 20 28 67 65 6f 6d 2d 3e 4d 61 78 59 20 3e 20  f (geom->MaxY > 
52050 2a 28 6d 61 78 5f 6d 69 6e 20 2b 20 33 29 29 0a  *(max_min + 3)).
52060 09 20 20 20 20 20 20 2a 28 6d 61 78 5f 6d 69 6e  .      *(max_min
52070 20 2b 20 33 29 20 3d 20 67 65 6f 6d 2d 3e 4d 61   + 3) = geom->Ma
52080 78 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xY;.      }.    
52090 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
520a0 20 28 67 65 6f 6d 29 3b 0a 7d 0a 0a 73 74 61 74   (geom);.}..stat
520b0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 45 78 74  ic void.fnct_Ext
520c0 65 6e 74 5f 66 69 6e 61 6c 20 28 73 71 6c 69 74  ent_final (sqlit
520d0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
520e0 74 65 78 74 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  text).{./* SQL f
520f0 75 6e 63 74 69 6f 6e 3a 0a 2f 20 45 78 74 65 6e  unction:./ Exten
52100 74 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65  t(BLOBencoded ge
52110 6f 6d 29 0a 2f 0a 2f 20 61 67 67 72 65 67 61 74  om)././ aggregat
52120 65 20 66 75 6e 63 74 69 6f 6e 20 2d 20 46 49 4e  e function - FIN
52130 41 4c 0a 2f 0a 2a 2f 0a 20 20 20 20 67 61 69 61  AL./.*/.    gaia
52140 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75  GeomCollPtr resu
52150 6c 74 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79  lt;.    gaiaPoly
52160 67 6f 6e 50 74 72 20 70 6f 6c 79 67 3b 0a 20 20  gonPtr polyg;.  
52170 20 20 67 61 69 61 52 69 6e 67 50 74 72 20 72 65    gaiaRingPtr re
52180 63 74 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a  ct;.    double *
52190 6d 61 78 5f 6d 69 6e 3b 0a 20 20 20 20 64 6f 75  max_min;.    dou
521a0 62 6c 65 20 6d 69 6e 78 3b 0a 20 20 20 20 64 6f  ble minx;.    do
521b0 75 62 6c 65 20 6d 69 6e 79 3b 0a 20 20 20 20 64  uble miny;.    d
521c0 6f 75 62 6c 65 20 6d 61 78 78 3b 0a 20 20 20 20  ouble maxx;.    
521d0 64 6f 75 62 6c 65 20 6d 61 78 79 3b 0a 20 20 20  double maxy;.   
521e0 20 64 6f 75 62 6c 65 20 2a 2a 70 20 3d 20 73 71   double **p = sq
521f0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
52200 63 6f 6e 74 65 78 74 20 28 63 6f 6e 74 65 78 74  context (context
52210 2c 20 30 29 3b 0a 20 20 20 20 69 66 20 28 21 70  , 0);.    if (!p
52220 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
52230 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
52240 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
52250 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
52260 20 20 20 6d 61 78 5f 6d 69 6e 20 3d 20 2a 70 3b     max_min = *p;
52270 0a 20 20 20 20 69 66 20 28 21 6d 61 78 5f 6d 69  .    if (!max_mi
52280 6e 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  n).      {..  sq
52290 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
522a0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
522b0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
522c0 20 20 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69      result = gai
522d0 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 20 28  aAllocGeomColl (
522e0 29 3b 0a 20 20 20 20 69 66 20 28 21 72 65 73 75  );.    if (!resu
522f0 6c 74 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  lt)..sqlite3_res
52300 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
52310 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  t);.    else.   
52320 20 20 20 7b 0a 09 20 20 2f 2a 20 62 75 69 6c 64     {..  /* build
52330 73 20 74 68 65 20 42 4c 4f 42 20 67 65 6f 6d 65  s the BLOB geome
52340 74 72 79 20 74 6f 20 62 65 20 72 65 74 75 72 6e  try to be return
52350 65 64 20 2a 2f 0a 09 20 20 69 6e 74 20 6c 65 6e  ed */..  int len
52360 3b 0a 09 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ;..  unsigned ch
52370 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e  ar *p_result = N
52380 55 4c 4c 3b 0a 09 20 20 70 6f 6c 79 67 20 3d 20  ULL;..  polyg = 
52390 67 61 69 61 41 64 64 50 6f 6c 79 67 6f 6e 54 6f  gaiaAddPolygonTo
523a0 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74  GeomColl (result
523b0 2c 20 35 2c 20 30 29 3b 0a 09 20 20 72 65 63 74  , 5, 0);..  rect
523c0 20 3d 20 70 6f 6c 79 67 2d 3e 45 78 74 65 72 69   = polyg->Exteri
523d0 6f 72 3b 0a 09 20 20 6d 69 6e 78 20 3d 20 2a 28  or;..  minx = *(
523e0 6d 61 78 5f 6d 69 6e 20 2b 20 30 29 3b 0a 09 20  max_min + 0);.. 
523f0 20 6d 69 6e 79 20 3d 20 2a 28 6d 61 78 5f 6d 69   miny = *(max_mi
52400 6e 20 2b 20 31 29 3b 0a 09 20 20 6d 61 78 78 20  n + 1);..  maxx 
52410 3d 20 2a 28 6d 61 78 5f 6d 69 6e 20 2b 20 32 29  = *(max_min + 2)
52420 3b 0a 09 20 20 6d 61 78 79 20 3d 20 2a 28 6d 61  ;..  maxy = *(ma
52430 78 5f 6d 69 6e 20 2b 20 33 29 3b 0a 09 20 20 67  x_min + 3);..  g
52440 61 69 61 53 65 74 50 6f 69 6e 74 20 28 72 65 63  aiaSetPoint (rec
52450 74 2d 3e 43 6f 6f 72 64 73 2c 20 30 2c 20 6d 69  t->Coords, 0, mi
52460 6e 78 2c 20 6d 69 6e 79 29 3b 09 2f 2a 20 76 65  nx, miny);./* ve
52470 72 74 65 78 20 23 20 31 20 2a 2f 0a 09 20 20 67  rtex # 1 */..  g
52480 61 69 61 53 65 74 50 6f 69 6e 74 20 28 72 65 63  aiaSetPoint (rec
52490 74 2d 3e 43 6f 6f 72 64 73 2c 20 31 2c 20 6d 61  t->Coords, 1, ma
524a0 78 78 2c 20 6d 69 6e 79 29 3b 09 2f 2a 20 76 65  xx, miny);./* ve
524b0 72 74 65 78 20 23 20 32 20 2a 2f 0a 09 20 20 67  rtex # 2 */..  g
524c0 61 69 61 53 65 74 50 6f 69 6e 74 20 28 72 65 63  aiaSetPoint (rec
524d0 74 2d 3e 43 6f 6f 72 64 73 2c 20 32 2c 20 6d 61  t->Coords, 2, ma
524e0 78 78 2c 20 6d 61 78 79 29 3b 09 2f 2a 20 76 65  xx, maxy);./* ve
524f0 72 74 65 78 20 23 20 33 20 2a 2f 0a 09 20 20 67  rtex # 3 */..  g
52500 61 69 61 53 65 74 50 6f 69 6e 74 20 28 72 65 63  aiaSetPoint (rec
52510 74 2d 3e 43 6f 6f 72 64 73 2c 20 33 2c 20 6d 69  t->Coords, 3, mi
52520 6e 78 2c 20 6d 61 78 79 29 3b 09 2f 2a 20 76 65  nx, maxy);./* ve
52530 72 74 65 78 20 23 20 34 20 2a 2f 0a 09 20 20 67  rtex # 4 */..  g
52540 61 69 61 53 65 74 50 6f 69 6e 74 20 28 72 65 63  aiaSetPoint (rec
52550 74 2d 3e 43 6f 6f 72 64 73 2c 20 34 2c 20 6d 69  t->Coords, 4, mi
52560 6e 78 2c 20 6d 69 6e 79 29 3b 09 2f 2a 20 76 65  nx, miny);./* ve
52570 72 74 65 78 20 23 20 35 20 5b 73 61 6d 65 20 61  rtex # 5 [same a
52580 73 20 76 65 72 74 65 78 20 23 20 31 20 74 6f 20  s vertex # 1 to 
52590 63 6c 6f 73 65 20 74 68 65 20 70 6f 6c 79 67 6f  close the polygo
525a0 6e 5d 20 2a 2f 0a 09 20 20 67 61 69 61 54 6f 53  n] */..  gaiaToS
525b0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
525c0 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65 73   (result, &p_res
525d0 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 20 20 73  ult, &len);..  s
525e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
525f0 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72  ob (context, p_r
52600 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65  esult, len, free
52610 29 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47 65  );..  gaiaFreeGe
52620 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b  omColl (result);
52630 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 66 72 65  .      }.    fre
52640 65 20 28 6d 61 78 5f 6d 69 6e 29 3b 0a 7d 0a 0a  e (max_min);.}..
52650 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
52660 5f 4d 62 72 4d 69 6e 58 20 28 73 71 6c 69 74 65  _MbrMinX (sqlite
52670 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
52680 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
52690 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
526a0 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
526b0 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4d 62 72 4d 69  unction:./ MbrMi
526c0 6e 58 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20  nX(BLOB encoded 
526d0 47 45 4d 45 54 52 59 29 0a 2f 0a 2f 20 72 65 74  GEMETRY)././ ret
526e0 75 72 6e 73 20 74 68 65 20 4d 69 6e 58 20 63 6f  urns the MinX co
526f0 6f 72 64 69 6e 61 74 65 20 66 6f 72 20 63 75 72  ordinate for cur
52700 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 27 73 20  rent geometry's 
52710 4d 42 52 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  MBR ./ or NULL i
52720 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65  f any error is e
52730 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20  ncountered.*/.  
52740 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
52750 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
52760 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 64 6f   n_bytes;.    do
52770 75 62 6c 65 20 63 6f 6f 72 64 3b 0a 20 20 20 20  uble coord;.    
52780 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
52790 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
527a0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
527b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
527c0 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
527d0 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
527e0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
527f0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
52800 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
52810 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
52820 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
52830 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
52840 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
52850 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
52860 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
52870 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
52880 3b 0a 20 20 20 20 69 66 20 28 21 67 61 69 61 47  ;.    if (!gaiaG
52890 65 74 4d 62 72 4d 69 6e 58 20 28 70 5f 62 6c 6f  etMbrMinX (p_blo
528a0 62 2c 20 6e 5f 62 79 74 65 73 2c 20 26 63 6f 6f  b, n_bytes, &coo
528b0 72 64 29 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  rd))..sqlite3_re
528c0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
528d0 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 09 73  xt);.    else..s
528e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f  qlite3_result_do
528f0 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 63  uble (context, c
52900 6f 6f 72 64 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  oord);.}..static
52910 20 76 6f 69 64 0a 66 6e 63 74 5f 4d 62 72 4d 61   void.fnct_MbrMa
52920 78 58 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  xX (sqlite3_cont
52930 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
52940 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
52950 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
52960 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
52970 6e 3a 0a 2f 20 4d 62 72 4d 61 78 58 28 42 4c 4f  n:./ MbrMaxX(BLO
52980 42 20 65 6e 63 6f 64 65 64 20 47 45 4d 45 54 52  B encoded GEMETR
52990 59 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74  Y)././ returns t
529a0 68 65 20 4d 61 78 58 20 63 6f 6f 72 64 69 6e 61  he MaxX coordina
529b0 74 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20 67  te for current g
529c0 65 6f 6d 65 74 72 79 27 73 20 4d 42 52 20 0a 2f  eometry's MBR ./
529d0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
529e0 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
529f0 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
52a00 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
52a10 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
52a20 65 73 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63  es;.    double c
52a30 6f 6f 72 64 3b 0a 20 20 20 20 47 41 49 41 5f 55  oord;.    GAIA_U
52a40 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
52a50 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
52a60 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
52a70 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
52a80 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
52a90 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
52aa0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
52ab0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
52ac0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
52ad0 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
52ae0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
52af0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
52b00 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
52b10 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
52b20 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
52b30 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
52b40 69 66 20 28 21 67 61 69 61 47 65 74 4d 62 72 4d  if (!gaiaGetMbrM
52b50 61 78 58 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  axX (p_blob, n_b
52b60 79 74 65 73 2c 20 26 63 6f 6f 72 64 29 29 0a 09  ytes, &coord))..
52b70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
52b80 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
52b90 20 20 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33     else..sqlite3
52ba0 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28  _result_double (
52bb0 63 6f 6e 74 65 78 74 2c 20 63 6f 6f 72 64 29 3b  context, coord);
52bc0 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
52bd0 66 6e 63 74 5f 4d 62 72 4d 69 6e 59 20 28 73 71  fnct_MbrMinY (sq
52be0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
52bf0 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
52c00 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
52c10 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
52c20 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4d  QL function:./ M
52c30 62 72 4d 69 6e 59 28 42 4c 4f 42 20 65 6e 63 6f  brMinY(BLOB enco
52c40 64 65 64 20 47 45 4d 45 54 52 59 29 0a 2f 0a 2f  ded GEMETRY)././
52c50 20 72 65 74 75 72 6e 73 20 74 68 65 20 4d 69 6e   returns the Min
52c60 59 20 63 6f 6f 72 64 69 6e 61 74 65 20 66 6f 72  Y coordinate for
52c70 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
52c80 79 27 73 20 4d 42 52 20 0a 2f 20 6f 72 20 4e 55  y's MBR ./ or NU
52c90 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
52ca0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
52cb0 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
52cc0 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20  har *p_blob;.   
52cd0 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20   int n_bytes;.  
52ce0 20 20 64 6f 75 62 6c 65 20 63 6f 6f 72 64 3b 0a    double coord;.
52cf0 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
52d00 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
52d10 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
52d20 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
52d30 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
52d40 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
52d50 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
52d60 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
52d70 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
52d80 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
52d90 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
52da0 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
52db0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
52dc0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[0]);.    n_b
52dd0 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
52de0 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
52df0 5b 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 21 67  [0]);.    if (!g
52e00 61 69 61 47 65 74 4d 62 72 4d 69 6e 59 20 28 70  aiaGetMbrMinY (p
52e10 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 2c 20  _blob, n_bytes, 
52e20 26 63 6f 6f 72 64 29 29 0a 09 73 71 6c 69 74 65  &coord))..sqlite
52e30 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
52e40 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73  ontext);.    els
52e50 65 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e..sqlite3_resul
52e60 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78  t_double (contex
52e70 74 2c 20 63 6f 6f 72 64 29 3b 0a 7d 0a 0a 73 74  t, coord);.}..st
52e80 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d  atic void.fnct_M
52e90 62 72 4d 61 78 59 20 28 73 71 6c 69 74 65 33 5f  brMaxY (sqlite3_
52ea0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
52eb0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
52ec0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
52ed0 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
52ee0 63 74 69 6f 6e 3a 0a 2f 20 4d 62 72 4d 61 78 59  ction:./ MbrMaxY
52ef0 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 47 45  (BLOB encoded GE
52f00 4d 45 54 52 59 29 0a 2f 0a 2f 20 72 65 74 75 72  METRY)././ retur
52f10 6e 73 20 74 68 65 20 4d 61 78 59 20 63 6f 6f 72  ns the MaxY coor
52f20 64 69 6e 61 74 65 20 66 6f 72 20 63 75 72 72 65  dinate for curre
52f30 6e 74 20 67 65 6f 6d 65 74 72 79 27 73 20 4d 42  nt geometry's MB
52f40 52 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  R ./ or NULL if 
52f50 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
52f60 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
52f70 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
52f80 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
52f90 5f 62 79 74 65 73 3b 0a 20 20 20 20 64 6f 75 62  _bytes;.    doub
52fa0 6c 65 20 63 6f 6f 72 64 3b 0a 20 20 20 20 47 41  le coord;.    GA
52fb0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
52fc0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
52fd0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
52fe0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
52ff0 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
53000 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
53010 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
53020 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
53030 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
53040 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
53050 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
53060 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
53070 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
53080 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
53090 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
530a0 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
530b0 20 20 20 20 69 66 20 28 21 67 61 69 61 47 65 74      if (!gaiaGet
530c0 4d 62 72 4d 61 78 59 20 28 70 5f 62 6c 6f 62 2c  MbrMaxY (p_blob,
530d0 20 6e 5f 62 79 74 65 73 2c 20 26 63 6f 6f 72 64   n_bytes, &coord
530e0 29 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  ))..sqlite3_resu
530f0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
53100 29 3b 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c  );.    else..sql
53110 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
53120 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 63 6f 6f  le (context, coo
53130 72 64 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  rd);.}..#ifndef 
53140 4f 4d 49 54 5f 47 45 4f 43 41 4c 4c 42 41 43 4b  OMIT_GEOCALLBACK
53150 53 09 2f 2a 20 73 75 70 70 6f 72 74 69 6e 67 20  S./* supporting 
53160 52 54 72 65 65 20 67 65 6f 6d 65 74 72 79 20 63  RTree geometry c
53170 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 73 74 61 74  allbacks */.stat
53180 69 63 20 76 6f 69 64 0a 67 61 69 61 5f 6d 62 72  ic void.gaia_mbr
53190 5f 64 65 6c 20 28 76 6f 69 64 20 2a 70 29 0a 7b  _del (void *p).{
531a0 0a 2f 2a 20 66 72 65 65 69 6e 67 20 64 61 74 61  ./* freeing data
531b0 20 75 73 65 64 20 62 79 20 52 2a 54 72 65 65 20   used by R*Tree 
531c0 47 65 6f 6d 65 74 72 79 20 43 61 6c 6c 62 61 63  Geometry Callbac
531d0 6b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  k */.    sqlite3
531e0 5f 66 72 65 65 20 28 70 29 3b 0a 7d 0a 0a 73 74  _free (p);.}..st
531f0 61 74 69 63 20 69 6e 74 0a 66 6e 63 74 5f 52 54  atic int.fnct_RT
53200 72 65 65 49 6e 74 65 72 73 65 63 74 73 20 28 73  reeIntersects (s
53210 71 6c 69 74 65 33 5f 72 74 72 65 65 5f 67 65 6f  qlite3_rtree_geo
53220 6d 65 74 72 79 20 2a 20 70 2c 20 69 6e 74 20 6e  metry * p, int n
53230 43 6f 6f 72 64 2c 20 64 6f 75 62 6c 65 20 2a 61  Coord, double *a
53240 43 6f 6f 72 64 2c 0a 09 09 20 20 20 20 20 20 69  Coord,...      i
53250 6e 74 20 2a 70 52 65 73 29 0a 7b 0a 2f 2a 20 52  nt *pRes).{./* R
53260 2a 54 72 65 65 20 47 65 6f 6d 65 74 72 79 20 63  *Tree Geometry c
53270 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
53280 3a 0a 2f 20 2e 2e 2e 20 4d 41 54 43 48 20 52 54  :./ ... MATCH RT
53290 72 65 65 49 6e 74 65 72 73 65 63 74 73 28 64 6f  reeIntersects(do
532a0 75 62 6c 65 20 78 31 2c 20 64 6f 75 62 6c 65 20  uble x1, double 
532b0 79 31 2c 20 64 6f 75 62 6c 65 20 78 32 2c 20 64  y1, double x2, d
532c0 6f 75 62 6c 65 20 79 32 29 0a 2a 2f 0a 20 20 20  ouble y2).*/.   
532d0 20 73 74 72 75 63 74 20 67 61 69 61 5f 72 74 72   struct gaia_rtr
532e0 65 65 5f 6d 62 72 20 2a 6d 62 72 3b 0a 20 20 20  ee_mbr *mbr;.   
532f0 20 64 6f 75 62 6c 65 20 78 6d 69 6e 3b 0a 20 20   double xmin;.  
53300 20 20 64 6f 75 62 6c 65 20 78 6d 61 78 3b 0a 20    double xmax;. 
53310 20 20 20 64 6f 75 62 6c 65 20 79 6d 69 6e 3b 0a     double ymin;.
53320 20 20 20 20 64 6f 75 62 6c 65 20 79 6d 61 78 3b      double ymax;
53330 0a 20 20 20 20 66 6c 6f 61 74 20 66 6d 69 6e 78  .    float fminx
53340 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 6d 69 6e  ;.    float fmin
53350 79 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 6d 61  y;.    float fma
53360 78 78 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 6d  xx;.    float fm
53370 61 78 79 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  axy;.    double 
53380 74 69 63 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  tic;.    double 
53390 74 69 63 32 3b 0a 0a 20 20 20 20 69 66 20 28 70  tic2;..    if (p
533a0 2d 3e 70 55 73 65 72 20 3d 3d 20 30 29 0a 20 20  ->pUser == 0).  
533b0 20 20 20 20 7b 0a 09 20 20 2f 2a 20 66 69 72 73      {..  /* firs
533c0 74 20 63 61 6c 6c 3a 20 77 65 20 6d 75 73 74 20  t call: we must 
533d0 63 68 65 63 6b 20 61 72 67 73 20 61 6e 64 20 74  check args and t
533e0 68 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 20 74  hen initialize t
533f0 68 65 20 4d 42 52 20 73 74 72 75 63 74 20 2a 2f  he MBR struct */
53400 0a 09 20 20 69 66 20 28 6e 43 6f 6f 72 64 20 21  ..  if (nCoord !
53410 3d 20 34 29 0a 09 20 20 20 20 20 20 72 65 74 75  = 4)..      retu
53420 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
53430 0a 09 20 20 69 66 20 28 70 2d 3e 6e 50 61 72 61  ..  if (p->nPara
53440 6d 20 21 3d 20 34 29 0a 09 20 20 20 20 20 20 72  m != 4)..      r
53450 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
53460 4f 52 3b 0a 09 20 20 6d 62 72 20 3d 20 28 73 74  OR;..  mbr = (st
53470 72 75 63 74 20 67 61 69 61 5f 72 74 72 65 65 5f  ruct gaia_rtree_
53480 6d 62 72 20 2a 29 20 28 70 2d 3e 70 55 73 65 72  mbr *) (p->pUser
53490 20 3d 0a 09 09 09 09 09 20 20 20 73 71 6c 69 74   =......   sqlit
534a0 65 33 5f 6d 61 6c 6c 6f 63 20 28 73 69 7a 65 6f  e3_malloc (sizeo
534b0 66 0a 09 09 09 09 09 09 09 20 20 20 28 73 74 72  f........   (str
534c0 75 63 74 0a 09 09 09 09 09 09 09 20 20 20 20 67  uct........    g
534d0 61 69 61 5f 72 74 72 65 65 5f 6d 62 72 29 29 29  aia_rtree_mbr)))
534e0 3b 0a 09 20 20 69 66 20 28 21 6d 62 72 29 0a 09  ;..  if (!mbr)..
534f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
53500 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 09 20 20 70 2d  ITE_NOMEM;..  p-
53510 3e 78 44 65 6c 55 73 65 72 20 3d 20 67 61 69 61  >xDelUser = gaia
53520 5f 6d 62 72 5f 64 65 6c 3b 0a 09 20 20 78 6d 69  _mbr_del;..  xmi
53530 6e 20 3d 20 70 2d 3e 61 50 61 72 61 6d 5b 30 5d  n = p->aParam[0]
53540 3b 0a 09 20 20 79 6d 69 6e 20 3d 20 70 2d 3e 61  ;..  ymin = p->a
53550 50 61 72 61 6d 5b 31 5d 3b 0a 09 20 20 78 6d 61  Param[1];..  xma
53560 78 20 3d 20 70 2d 3e 61 50 61 72 61 6d 5b 32 5d  x = p->aParam[2]
53570 3b 0a 09 20 20 79 6d 61 78 20 3d 20 70 2d 3e 61  ;..  ymax = p->a
53580 50 61 72 61 6d 5b 33 5d 3b 0a 09 20 20 69 66 20  Param[3];..  if 
53590 28 78 6d 69 6e 20 3e 20 78 6d 61 78 29 0a 09 20  (xmin > xmax).. 
535a0 20 20 20 7b 0a 09 09 78 6d 69 6e 20 3d 20 70 2d     {...xmin = p-
535b0 3e 61 50 61 72 61 6d 5b 32 5d 3b 0a 09 09 78 6d  >aParam[2];...xm
535c0 61 78 20 3d 20 70 2d 3e 61 50 61 72 61 6d 5b 30  ax = p->aParam[0
535d0 5d 3b 0a 09 20 20 20 20 7d 0a 09 20 20 69 66 20  ];..    }..  if 
535e0 28 79 6d 69 6e 20 3e 20 79 6d 61 78 29 0a 09 20  (ymin > ymax).. 
535f0 20 20 20 7b 0a 09 09 79 6d 69 6e 20 3d 20 70 2d     {...ymin = p-
53600 3e 61 50 61 72 61 6d 5b 33 5d 3b 0a 09 09 79 6d  >aParam[3];...ym
53610 61 78 20 3d 20 70 2d 3e 61 50 61 72 61 6d 5b 31  ax = p->aParam[1
53620 5d 3b 0a 09 20 20 20 20 7d 0a 0a 09 20 20 2f 2a  ];..    }...  /*
53630 20 61 64 6a 75 73 74 69 6e 67 20 74 68 65 20 4d   adjusting the M
53640 42 52 20 73 6f 20 74 6f 20 63 6f 6d 70 65 6e 73  BR so to compens
53650 61 74 65 20 66 6f 72 20 44 4f 55 42 4c 45 2f 46  ate for DOUBLE/F
53660 4c 4f 41 54 20 74 72 75 6e 63 61 74 69 6f 6e 73  LOAT truncations
53670 20 2a 2f 0a 09 20 20 66 6d 69 6e 78 20 3d 20 28   */..  fminx = (
53680 66 6c 6f 61 74 29 20 78 6d 69 6e 3b 0a 09 20 20  float) xmin;..  
53690 66 6d 69 6e 79 20 3d 20 28 66 6c 6f 61 74 29 20  fminy = (float) 
536a0 79 6d 69 6e 3b 0a 09 20 20 66 6d 61 78 78 20 3d  ymin;..  fmaxx =
536b0 20 28 66 6c 6f 61 74 29 20 78 6d 61 78 3b 0a 09   (float) xmax;..
536c0 20 20 66 6d 61 78 79 20 3d 20 28 66 6c 6f 61 74    fmaxy = (float
536d0 29 20 79 6d 61 78 3b 0a 09 20 20 74 69 63 20 3d  ) ymax;..  tic =
536e0 20 66 61 62 73 20 28 78 6d 69 6e 20 2d 20 66 6d   fabs (xmin - fm
536f0 69 6e 78 29 3b 0a 09 20 20 74 69 63 32 20 3d 20  inx);..  tic2 = 
53700 66 61 62 73 20 28 79 6d 69 6e 20 2d 20 66 6d 69  fabs (ymin - fmi
53710 6e 79 29 3b 0a 09 20 20 69 66 20 28 74 69 63 32  ny);..  if (tic2
53720 20 3e 20 74 69 63 29 0a 09 20 20 20 20 20 20 74   > tic)..      t
53730 69 63 20 3d 20 74 69 63 32 3b 0a 09 20 20 74 69  ic = tic2;..  ti
53740 63 32 20 3d 20 66 61 62 73 20 28 78 6d 61 78 20  c2 = fabs (xmax 
53750 2d 20 66 6d 61 78 78 29 3b 0a 09 20 20 69 66 20  - fmaxx);..  if 
53760 28 74 69 63 32 20 3e 20 74 69 63 29 0a 09 20 20  (tic2 > tic)..  
53770 20 20 20 20 74 69 63 20 3d 20 74 69 63 32 3b 0a      tic = tic2;.
53780 09 20 20 74 69 63 32 20 3d 20 66 61 62 73 20 28  .  tic2 = fabs (
53790 79 6d 61 78 20 2d 20 66 6d 61 78 79 29 3b 0a 09  ymax - fmaxy);..
537a0 20 20 69 66 20 28 74 69 63 32 20 3e 20 74 69 63    if (tic2 > tic
537b0 29 0a 09 20 20 20 20 20 20 74 69 63 20 3d 20 74  )..      tic = t
537c0 69 63 32 3b 0a 09 20 20 74 69 63 20 2a 3d 20 32  ic2;..  tic *= 2
537d0 2e 30 3b 0a 0a 09 20 20 6d 62 72 2d 3e 6d 69 6e  .0;...  mbr->min
537e0 78 20 3d 20 78 6d 69 6e 20 2d 20 74 69 63 3b 0a  x = xmin - tic;.
537f0 09 20 20 6d 62 72 2d 3e 6d 69 6e 79 20 3d 20 79  .  mbr->miny = y
53800 6d 69 6e 20 2d 20 74 69 63 3b 0a 09 20 20 6d 62  min - tic;..  mb
53810 72 2d 3e 6d 61 78 78 20 3d 20 78 6d 61 78 20 2b  r->maxx = xmax +
53820 20 74 69 63 3b 0a 09 20 20 6d 62 72 2d 3e 6d 61   tic;..  mbr->ma
53830 78 79 20 3d 20 79 6d 61 78 20 2b 20 74 69 63 3b  xy = ymax + tic;
53840 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 6d 62  .      }..    mb
53850 72 20 3d 20 28 73 74 72 75 63 74 20 67 61 69 61  r = (struct gaia
53860 5f 72 74 72 65 65 5f 6d 62 72 20 2a 29 20 28 70  _rtree_mbr *) (p
53870 2d 3e 70 55 73 65 72 29 3b 0a 20 20 20 20 78 6d  ->pUser);.    xm
53880 69 6e 20 3d 20 61 43 6f 6f 72 64 5b 30 5d 3b 0a  in = aCoord[0];.
53890 20 20 20 20 78 6d 61 78 20 3d 20 61 43 6f 6f 72      xmax = aCoor
538a0 64 5b 31 5d 3b 0a 20 20 20 20 79 6d 69 6e 20 3d  d[1];.    ymin =
538b0 20 61 43 6f 6f 72 64 5b 32 5d 3b 0a 20 20 20 20   aCoord[2];.    
538c0 79 6d 61 78 20 3d 20 61 43 6f 6f 72 64 5b 33 5d  ymax = aCoord[3]
538d0 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b  ;.    *pRes = 1;
538e0 0a 2f 2a 20 65 76 61 6c 75 61 74 69 6e 67 20 49  ./* evaluating I
538f0 6e 74 65 72 73 65 63 74 73 20 72 65 6c 61 74 69  ntersects relati
53900 6f 6e 73 68 69 70 20 2a 2f 0a 20 20 20 20 69 66  onship */.    if
53910 20 28 78 6d 69 6e 20 3e 20 6d 62 72 2d 3e 6d 61   (xmin > mbr->ma
53920 78 78 29 0a 09 2a 70 52 65 73 20 3d 20 30 3b 0a  xx)..*pRes = 0;.
53930 20 20 20 20 69 66 20 28 78 6d 61 78 20 3c 20 6d      if (xmax < m
53940 62 72 2d 3e 6d 69 6e 78 29 0a 09 2a 70 52 65 73  br->minx)..*pRes
53950 20 3d 20 30 3b 0a 20 20 20 20 69 66 20 28 79 6d   = 0;.    if (ym
53960 69 6e 20 3e 20 6d 62 72 2d 3e 6d 61 78 79 29 0a  in > mbr->maxy).
53970 09 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20  .*pRes = 0;.    
53980 69 66 20 28 79 6d 61 78 20 3c 20 6d 62 72 2d 3e  if (ymax < mbr->
53990 6d 69 6e 79 29 0a 09 2a 70 52 65 73 20 3d 20 30  miny)..*pRes = 0
539a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
539b0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69  ITE_OK;.}..stati
539c0 63 20 69 6e 74 0a 66 6e 63 74 5f 52 54 72 65 65  c int.fnct_RTree
539d0 44 69 73 74 57 69 74 68 69 6e 20 28 73 71 6c 69  DistWithin (sqli
539e0 74 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74  te3_rtree_geomet
539f0 72 79 20 2a 20 70 2c 20 69 6e 74 20 6e 43 6f 6f  ry * p, int nCoo
53a00 72 64 2c 20 64 6f 75 62 6c 65 20 2a 61 43 6f 6f  rd, double *aCoo
53a10 72 64 2c 0a 09 09 20 20 20 20 20 20 69 6e 74 20  rd,...      int 
53a20 2a 70 52 65 73 29 0a 7b 0a 2f 2a 20 52 2a 54 72  *pRes).{./* R*Tr
53a30 65 65 20 47 65 6f 6d 65 74 72 79 20 63 61 6c 6c  ee Geometry call
53a40 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f  back function:./
53a50 20 2e 2e 2e 20 4d 41 54 43 48 20 52 54 72 65 65   ... MATCH RTree
53a60 44 69 73 74 57 69 74 68 69 6e 28 64 6f 75 62 6c  DistWithin(doubl
53a70 65 20 78 2c 20 64 6f 75 62 6c 65 20 79 2c 20 64  e x, double y, d
53a80 6f 75 62 6c 65 20 72 61 64 69 75 73 29 0a 2a 2f  ouble radius).*/
53a90 0a 20 20 20 20 73 74 72 75 63 74 20 67 61 69 61  .    struct gaia
53aa0 5f 72 74 72 65 65 5f 6d 62 72 20 2a 6d 62 72 3b  _rtree_mbr *mbr;
53ab0 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 6d 69 6e  .    double xmin
53ac0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 6d 61  ;.    double xma
53ad0 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79 6d  x;.    double ym
53ae0 69 6e 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79  in;.    double y
53af0 6d 61 78 3b 0a 0a 20 20 20 20 69 66 20 28 70 2d  max;..    if (p-
53b00 3e 70 55 73 65 72 20 3d 3d 20 30 29 0a 20 20 20  >pUser == 0).   
53b10 20 20 20 7b 0a 09 20 20 2f 2a 20 66 69 72 73 74     {..  /* first
53b20 20 63 61 6c 6c 3a 20 77 65 20 6d 75 73 74 20 63   call: we must c
53b30 68 65 63 6b 20 61 72 67 73 20 61 6e 64 20 74 68  heck args and th
53b40 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  en initialize th
53b50 65 20 4d 42 52 20 73 74 72 75 63 74 20 2a 2f 0a  e MBR struct */.
53b60 09 20 20 69 66 20 28 6e 43 6f 6f 72 64 20 21 3d  .  if (nCoord !=
53b70 20 34 29 0a 09 20 20 20 20 20 20 72 65 74 75 72   4)..      retur
53b80 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
53b90 09 20 20 69 66 20 28 70 2d 3e 6e 50 61 72 61 6d  .  if (p->nParam
53ba0 20 21 3d 20 33 29 0a 09 20 20 20 20 20 20 72 65   != 3)..      re
53bb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
53bc0 52 3b 0a 09 20 20 6d 62 72 20 3d 20 28 73 74 72  R;..  mbr = (str
53bd0 75 63 74 20 67 61 69 61 5f 72 74 72 65 65 5f 6d  uct gaia_rtree_m
53be0 62 72 20 2a 29 20 28 70 2d 3e 70 55 73 65 72 20  br *) (p->pUser 
53bf0 3d 0a 09 09 09 09 09 20 20 20 73 71 6c 69 74 65  =......   sqlite
53c00 33 5f 6d 61 6c 6c 6f 63 20 28 73 69 7a 65 6f 66  3_malloc (sizeof
53c10 0a 09 09 09 09 09 09 09 20 20 20 28 73 74 72 75  ........   (stru
53c20 63 74 0a 09 09 09 09 09 09 09 20 20 20 20 67 61  ct........    ga
53c30 69 61 5f 72 74 72 65 65 5f 6d 62 72 29 29 29 3b  ia_rtree_mbr)));
53c40 0a 09 20 20 69 66 20 28 21 6d 62 72 29 0a 09 20  ..  if (!mbr).. 
53c50 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
53c60 54 45 5f 4e 4f 4d 45 4d 3b 0a 09 20 20 70 2d 3e  TE_NOMEM;..  p->
53c70 78 44 65 6c 55 73 65 72 20 3d 20 67 61 69 61 5f  xDelUser = gaia_
53c80 6d 62 72 5f 64 65 6c 3b 0a 09 20 20 6d 62 72 2d  mbr_del;..  mbr-
53c90 3e 6d 69 6e 78 20 3d 20 70 2d 3e 61 50 61 72 61  >minx = p->aPara
53ca0 6d 5b 30 5d 20 2d 20 70 2d 3e 61 50 61 72 61 6d  m[0] - p->aParam
53cb0 5b 32 5d 3b 0a 09 20 20 6d 62 72 2d 3e 6d 69 6e  [2];..  mbr->min
53cc0 79 20 3d 20 70 2d 3e 61 50 61 72 61 6d 5b 31 5d  y = p->aParam[1]
53cd0 20 2d 20 70 2d 3e 61 50 61 72 61 6d 5b 32 5d 3b   - p->aParam[2];
53ce0 0a 09 20 20 6d 62 72 2d 3e 6d 61 78 78 20 3d 20  ..  mbr->maxx = 
53cf0 70 2d 3e 61 50 61 72 61 6d 5b 30 5d 20 2b 20 70  p->aParam[0] + p
53d00 2d 3e 61 50 61 72 61 6d 5b 32 5d 3b 0a 09 20 20  ->aParam[2];..  
53d10 6d 62 72 2d 3e 6d 61 78 79 20 3d 20 70 2d 3e 61  mbr->maxy = p->a
53d20 50 61 72 61 6d 5b 31 5d 20 2b 20 70 2d 3e 61 50  Param[1] + p->aP
53d30 61 72 61 6d 5b 32 5d 3b 0a 20 20 20 20 20 20 7d  aram[2];.      }
53d40 0a 0a 20 20 20 20 6d 62 72 20 3d 20 28 73 74 72  ..    mbr = (str
53d50 75 63 74 20 67 61 69 61 5f 72 74 72 65 65 5f 6d  uct gaia_rtree_m
53d60 62 72 20 2a 29 20 28 70 2d 3e 70 55 73 65 72 29  br *) (p->pUser)
53d70 3b 0a 20 20 20 20 78 6d 69 6e 20 3d 20 61 43 6f  ;.    xmin = aCo
53d80 6f 72 64 5b 30 5d 3b 0a 20 20 20 20 78 6d 61 78  ord[0];.    xmax
53d90 20 3d 20 61 43 6f 6f 72 64 5b 31 5d 3b 0a 20 20   = aCoord[1];.  
53da0 20 20 79 6d 69 6e 20 3d 20 61 43 6f 6f 72 64 5b    ymin = aCoord[
53db0 32 5d 3b 0a 20 20 20 20 79 6d 61 78 20 3d 20 61  2];.    ymax = a
53dc0 43 6f 6f 72 64 5b 33 5d 3b 0a 20 20 20 20 2a 70  Coord[3];.    *p
53dd0 52 65 73 20 3d 20 31 3b 0a 2f 2a 20 65 76 61 6c  Res = 1;./* eval
53de0 75 61 74 69 6e 67 20 49 6e 74 65 72 73 65 63 74  uating Intersect
53df0 73 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 2a  s relationship *
53e00 2f 0a 20 20 20 20 69 66 20 28 78 6d 69 6e 20 3e  /.    if (xmin >
53e10 20 6d 62 72 2d 3e 6d 61 78 78 29 0a 09 2a 70 52   mbr->maxx)..*pR
53e20 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 66 20 28  es = 0;.    if (
53e30 78 6d 61 78 20 3c 20 6d 62 72 2d 3e 6d 69 6e 78  xmax < mbr->minx
53e40 29 0a 09 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20  )..*pRes = 0;.  
53e50 20 20 69 66 20 28 79 6d 69 6e 20 3e 20 6d 62 72    if (ymin > mbr
53e60 2d 3e 6d 61 78 79 29 0a 09 2a 70 52 65 73 20 3d  ->maxy)..*pRes =
53e70 20 30 3b 0a 20 20 20 20 69 66 20 28 79 6d 61 78   0;.    if (ymax
53e80 20 3c 20 6d 62 72 2d 3e 6d 69 6e 79 29 0a 09 2a   < mbr->miny)..*
53e90 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  pRes = 0;.    re
53ea0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
53eb0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 65 6e 64 20  }.#endif /* end 
53ec0 52 54 72 65 65 20 67 65 6f 6d 65 74 72 79 20 63  RTree geometry c
53ed0 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 0a 73 74 61  allbacks */..sta
53ee0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 58 20  tic void.fnct_X 
53ef0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
53f00 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
53f10 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
53f20 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
53f30 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
53f40 2f 20 58 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64  / X(BLOB encoded
53f50 20 50 4f 49 4e 54 29 0a 2f 0a 2f 20 72 65 74 75   POINT)././ retu
53f60 72 6e 73 20 74 68 65 20 58 20 63 6f 6f 72 64 69  rns the X coordi
53f70 6e 61 74 65 20 66 6f 72 20 63 75 72 72 65 6e 74  nate for current
53f80 20 50 4f 49 4e 54 20 67 65 6f 6d 65 74 72 79 20   POINT geometry 
53f90 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
53fa0 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
53fb0 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
53fc0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
53fd0 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
53fe0 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47 65  ytes;.    gaiaGe
53ff0 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
54000 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 50 6f  NULL;.    gaiaPo
54010 69 6e 74 50 74 72 20 70 6f 69 6e 74 3b 0a 20 20  intPtr point;.  
54020 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
54030 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
54040 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
54050 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
54060 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
54070 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
54080 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
54090 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
540a0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
540b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
540c0 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
540d0 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
540e0 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
540f0 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
54100 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
54110 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
54120 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61  ]);.    geo = ga
54130 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65  iaFromSpatiaLite
54140 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c  BlobWkb (p_blob,
54150 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69   n_bytes);.    i
54160 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65  f (!geo)..sqlite
54170 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
54180 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73  ontext);.    els
54190 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 70 6f 69  e.      {..  poi
541a0 6e 74 20 3d 20 73 69 6d 70 6c 65 50 6f 69 6e 74  nt = simplePoint
541b0 20 28 67 65 6f 29 3b 0a 09 20 20 69 66 20 28 21   (geo);..  if (!
541c0 70 6f 69 6e 74 29 0a 09 20 20 20 20 20 20 73 71  point)..      sq
541d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
541e0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
541f0 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69  else..      sqli
54200 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
54210 65 20 28 63 6f 6e 74 65 78 74 2c 20 70 6f 69 6e  e (context, poin
54220 74 2d 3e 58 29 3b 0a 20 20 20 20 20 20 7d 0a 20  t->X);.      }. 
54230 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
54240 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
54250 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 59  atic void.fnct_Y
54260 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
54270 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
54280 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
54290 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
542a0 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
542b0 0a 2f 20 59 28 42 4c 4f 42 20 65 6e 63 6f 64 65  ./ Y(BLOB encode
542c0 64 20 50 4f 49 4e 54 29 0a 2f 0a 2f 20 72 65 74  d POINT)././ ret
542d0 75 72 6e 73 20 74 68 65 20 59 20 63 6f 6f 72 64  urns the Y coord
542e0 69 6e 61 74 65 20 66 6f 72 20 63 75 72 72 65 6e  inate for curren
542f0 74 20 50 4f 49 4e 54 20 67 65 6f 6d 65 74 72 79  t POINT geometry
54300 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61   ./ or NULL if a
54310 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
54320 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75  untered.*/.    u
54330 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
54340 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
54350 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47  bytes;.    gaiaG
54360 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
54370 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 50   NULL;.    gaiaP
54380 6f 69 6e 74 50 74 72 20 70 6f 69 6e 74 3b 0a 20  ointPtr point;. 
54390 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
543a0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
543b0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
543c0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
543d0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
543e0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
543f0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
54400 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
54410 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
54420 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
54430 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
54440 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
54450 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
54460 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
54470 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
54480 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
54490 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
544a0 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
544b0 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
544c0 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
544d0 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
544e0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
544f0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
54500 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 70 6f  se.      {..  po
54510 69 6e 74 20 3d 20 73 69 6d 70 6c 65 50 6f 69 6e  int = simplePoin
54520 74 20 28 67 65 6f 29 3b 0a 09 20 20 69 66 20 28  t (geo);..  if (
54530 21 70 6f 69 6e 74 29 0a 09 20 20 20 20 20 20 73  !point)..      s
54540 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
54550 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
54560 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
54570 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
54580 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 70 6f 69  le (context, poi
54590 6e 74 2d 3e 59 29 3b 0a 20 20 20 20 20 20 7d 0a  nt->Y);.      }.
545a0 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
545b0 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73  Coll (geo);.}..s
545c0 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
545d0 5a 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  Z (sqlite3_conte
545e0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
545f0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
54600 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
54610 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
54620 3a 0a 2f 20 5a 28 42 4c 4f 42 20 65 6e 63 6f 64  :./ Z(BLOB encod
54630 65 64 20 50 4f 49 4e 54 29 0a 2f 0a 2f 20 72 65  ed POINT)././ re
54640 74 75 72 6e 73 20 74 68 65 20 5a 20 63 6f 6f 72  turns the Z coor
54650 64 69 6e 61 74 65 20 66 6f 72 20 63 75 72 72 65  dinate for curre
54660 6e 74 20 50 4f 49 4e 54 20 67 65 6f 6d 65 74 72  nt POINT geometr
54670 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  y ./ or NULL if 
54680 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
54690 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
546a0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
546b0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
546c0 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61  _bytes;.    gaia
546d0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
546e0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
546f0 50 6f 69 6e 74 50 74 72 20 70 6f 69 6e 74 3b 0a  PointPtr point;.
54700 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
54710 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
54720 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
54730 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
54740 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
54750 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
54760 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
54770 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
54780 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
54790 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
547a0 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
547b0 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
547c0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
547d0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[0]);.    n_b
547e0 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
547f0 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
54800 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20  [0]);.    geo = 
54810 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
54820 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
54830 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
54840 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69   if (!geo)..sqli
54850 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
54860 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
54870 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 70  lse.      {..  p
54880 6f 69 6e 74 20 3d 20 73 69 6d 70 6c 65 50 6f 69  oint = simplePoi
54890 6e 74 20 28 67 65 6f 29 3b 0a 09 20 20 69 66 20  nt (geo);..  if 
548a0 28 21 70 6f 69 6e 74 29 0a 09 20 20 20 20 20 20  (!point)..      
548b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
548c0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
548d0 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09    else..    {...
548e0 69 66 20 28 70 6f 69 6e 74 2d 3e 44 69 6d 65 6e  if (point->Dimen
548f0 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49  sionModel == GAI
54900 41 5f 58 59 5f 5a 0a 09 09 20 20 20 20 7c 7c 20  A_XY_Z...    || 
54910 70 6f 69 6e 74 2d 3e 44 69 6d 65 6e 73 69 6f 6e  point->Dimension
54920 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59  Model == GAIA_XY
54930 5f 5a 5f 4d 29 0a 09 09 20 20 20 20 73 71 6c 69  _Z_M)...    sqli
54940 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
54950 65 20 28 63 6f 6e 74 65 78 74 2c 20 70 6f 69 6e  e (context, poin
54960 74 2d 3e 5a 29 3b 0a 09 09 65 6c 73 65 0a 09 09  t->Z);...else...
54970 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
54980 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
54990 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20  );..    }.      
549a0 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
549b0 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a  omColl (geo);.}.
549c0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
549d0 74 5f 4d 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  t_M (sqlite3_con
549e0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
549f0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
54a00 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
54a10 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
54a20 6f 6e 3a 0a 2f 20 4d 28 42 4c 4f 42 20 65 6e 63  on:./ M(BLOB enc
54a30 6f 64 65 64 20 50 4f 49 4e 54 29 0a 2f 0a 2f 20  oded POINT)././ 
54a40 72 65 74 75 72 6e 73 20 74 68 65 20 4d 20 63 6f  returns the M co
54a50 6f 72 64 69 6e 61 74 65 20 66 6f 72 20 63 75 72  ordinate for cur
54a60 72 65 6e 74 20 50 4f 49 4e 54 20 67 65 6f 6d 65  rent POINT geome
54a70 74 72 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  try ./ or NULL i
54a80 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65  f any error is e
54a90 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20  ncountered.*/.  
54aa0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
54ab0 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
54ac0 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61   n_bytes;.    ga
54ad0 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
54ae0 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61  o = NULL;.    ga
54af0 69 61 50 6f 69 6e 74 50 74 72 20 70 6f 69 6e 74  iaPointPtr point
54b00 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
54b10 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
54b20 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
54b30 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
54b40 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
54b50 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
54b60 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
54b70 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
54b80 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
54b90 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
54ba0 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
54bb0 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
54bc0 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
54bd0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
54be0 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
54bf0 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
54c00 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20  gv[0]);.    geo 
54c10 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
54c20 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
54c30 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
54c40 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71     if (!geo)..sq
54c50 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
54c60 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
54c70 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
54c80 20 70 6f 69 6e 74 20 3d 20 73 69 6d 70 6c 65 50   point = simpleP
54c90 6f 69 6e 74 20 28 67 65 6f 29 3b 0a 09 20 20 69  oint (geo);..  i
54ca0 66 20 28 21 70 6f 69 6e 74 29 0a 09 20 20 20 20  f (!point)..    
54cb0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
54cc0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
54cd0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
54ce0 09 09 69 66 20 28 70 6f 69 6e 74 2d 3e 44 69 6d  ..if (point->Dim
54cf0 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47  ensionModel == G
54d00 41 49 41 5f 58 59 5f 4d 0a 09 09 20 20 20 20 7c  AIA_XY_M...    |
54d10 7c 20 70 6f 69 6e 74 2d 3e 44 69 6d 65 6e 73 69  | point->Dimensi
54d20 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f  onModel == GAIA_
54d30 58 59 5f 5a 5f 4d 29 0a 09 09 20 20 20 20 73 71  XY_Z_M)...    sq
54d40 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
54d50 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 70 6f  ble (context, po
54d60 69 6e 74 2d 3e 4d 29 3b 0a 09 09 65 6c 73 65 0a  int->M);...else.
54d70 09 09 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ..    sqlite3_re
54d80 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
54d90 78 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20  xt);..    }.    
54da0 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65    }.    gaiaFree
54db0 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a  GeomColl (geo);.
54dc0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
54dd0 6e 63 74 5f 4e 75 6d 50 6f 69 6e 74 73 20 28 73  nct_NumPoints (s
54de0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
54df0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
54e00 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
54e10 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
54e20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
54e30 4e 75 6d 50 6f 69 6e 74 73 28 42 4c 4f 42 20 65  NumPoints(BLOB e
54e40 6e 63 6f 64 65 64 20 4c 49 4e 45 53 54 52 49 4e  ncoded LINESTRIN
54e50 47 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74  G)././ returns t
54e60 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 65 72  he number of ver
54e70 74 69 63 65 73 20 66 6f 72 20 63 75 72 72 65 6e  tices for curren
54e80 74 20 4c 49 4e 45 53 54 52 49 4e 47 20 67 65 6f  t LINESTRING geo
54e90 6d 65 74 72 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c  metry ./ or NULL
54ea0 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
54eb0 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
54ec0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
54ed0 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69  r *p_blob;.    i
54ee0 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
54ef0 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
54f00 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
54f10 67 61 69 61 4c 69 6e 65 73 74 72 69 6e 67 50 74  gaiaLinestringPt
54f20 72 20 6c 69 6e 65 3b 0a 20 20 20 20 47 41 49 41  r line;.    GAIA
54f30 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
54f40 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
54f50 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
54f60 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
54f70 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
54f80 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
54f90 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
54fa0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
54fb0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
54fc0 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
54fd0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
54fe0 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
54ff0 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
55000 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
55010 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
55020 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
55030 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d    geo = gaiaFrom
55040 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
55050 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
55060 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65  es);.    if (!ge
55070 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  o)..sqlite3_resu
55080 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
55090 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20  );.    else.    
550a0 20 20 7b 0a 09 20 20 6c 69 6e 65 20 3d 20 73 69    {..  line = si
550b0 6d 70 6c 65 4c 69 6e 65 73 74 72 69 6e 67 20 28  mpleLinestring (
550c0 67 65 6f 29 3b 0a 09 20 20 69 66 20 28 21 6c 69  geo);..  if (!li
550d0 6e 65 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74  ne)..      sqlit
550e0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
550f0 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73  context);..  els
55100 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  e..      sqlite3
55110 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
55120 74 65 78 74 2c 20 6c 69 6e 65 2d 3e 50 6f 69 6e  text, line->Poin
55130 74 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ts);.      }.   
55140 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
55150 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74  l (geo);.}..stat
55160 69 63 20 76 6f 69 64 0a 70 6f 69 6e 74 5f 6e 20  ic void.point_n 
55170 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
55180 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
55190 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
551a0 6c 75 65 20 2a 2a 20 61 72 67 76 2c 0a 09 20 69  lue ** argv,.. i
551b0 6e 74 20 72 65 71 75 65 73 74 29 0a 7b 0a 2f 2a  nt request).{./*
551c0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 3a 0a   SQL functions:.
551d0 2f 20 53 74 61 72 74 50 6f 69 6e 74 28 42 4c 4f  / StartPoint(BLO
551e0 42 20 65 6e 63 6f 64 65 64 20 4c 49 4e 45 53 54  B encoded LINEST
551f0 52 49 4e 47 20 67 65 6f 6d 65 74 72 79 29 0a 2f  RING geometry)./
55200 20 45 6e 64 50 6f 69 6e 74 28 42 4c 4f 42 20 65   EndPoint(BLOB e
55210 6e 63 6f 64 65 64 20 4c 49 4e 45 53 54 52 49 4e  ncoded LINESTRIN
55220 47 20 67 65 6f 6d 65 74 72 79 29 0a 2f 20 50 6f  G geometry)./ Po
55230 69 6e 74 4e 28 42 4c 4f 42 20 65 6e 63 6f 64 65  intN(BLOB encode
55240 64 20 4c 49 4e 45 53 54 52 49 4e 47 20 67 65 6f  d LINESTRING geo
55250 6d 65 74 72 79 2c 20 69 6e 74 65 67 65 72 20 70  metry, integer p
55260 6f 69 6e 74 5f 6e 6f 29 0a 2f 0a 2f 20 72 65 74  oint_no)././ ret
55270 75 72 6e 73 20 74 68 65 20 4e 74 68 20 50 4f 49  urns the Nth POI
55280 4e 54 20 66 6f 72 20 63 75 72 72 65 6e 74 20 4c  NT for current L
55290 49 4e 45 53 54 52 49 4e 47 20 67 65 6f 6d 65 74  INESTRING geomet
552a0 72 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  ry ./ or NULL if
552b0 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
552c0 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
552d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
552e0 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20  p_blob;.    int 
552f0 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74  n_bytes;.    int
55300 20 76 65 72 74 65 78 3b 0a 20 20 20 20 69 6e 74   vertex;.    int
55310 20 6c 65 6e 3b 0a 20 20 20 20 64 6f 75 62 6c 65   len;.    double
55320 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79   x;.    double y
55330 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 7a 3b 0a  ;.    double z;.
55340 20 20 20 20 64 6f 75 62 6c 65 20 6d 3b 0a 20 20      double m;.  
55350 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
55360 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  *p_result = NULL
55370 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
55380 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
55390 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
553a0 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20  llPtr result;.  
553b0 20 20 67 61 69 61 4c 69 6e 65 73 74 72 69 6e 67    gaiaLinestring
553c0 50 74 72 20 6c 69 6e 65 3b 0a 20 20 20 20 47 41  Ptr line;.    GA
553d0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
553e0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
553f0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
55400 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
55410 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
55420 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
55430 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
55440 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
55450 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
55460 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 72 65      }.    if (re
55470 71 75 65 73 74 20 3d 3d 20 47 41 49 41 5f 50 4f  quest == GAIA_PO
55480 49 4e 54 4e 29 0a 20 20 20 20 20 20 7b 0a 09 20  INTN).      {.. 
55490 20 2f 2a 20 50 6f 69 6e 74 4e 28 29 20 72 65 71   /* PointN() req
554a0 75 69 72 65 73 20 70 6f 69 6e 74 20 69 6e 64 65  uires point inde
554b0 78 20 74 6f 20 62 65 20 64 65 66 69 6e 65 64 20  x to be defined 
554c0 61 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  as an SQL functi
554d0 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 09  on argument */..
554e0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
554f0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
55500 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) != SQLITE_INT
55510 45 47 45 52 29 0a 09 20 20 20 20 7b 0a 09 09 73  EGER)..    {...s
55520 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
55530 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09  ll (context);...
55540 72 65 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 09  return;..    }..
55550 20 20 76 65 72 74 65 78 20 3d 20 73 71 6c 69 74    vertex = sqlit
55560 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
55570 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  gv[1]);.      }.
55580 20 20 20 20 65 6c 73 65 20 69 66 20 28 72 65 71      else if (req
55590 75 65 73 74 20 3d 3d 20 47 41 49 41 5f 45 4e 44  uest == GAIA_END
555a0 5f 50 4f 49 4e 54 29 0a 09 76 65 72 74 65 78 20  _POINT)..vertex 
555b0 3d 20 2d 31 3b 09 09 2f 2a 20 45 6e 64 50 6f 69  = -1;../* EndPoi
555c0 6e 74 28 29 20 73 70 65 63 69 66 69 65 73 20 61  nt() specifies a
555d0 20 6e 65 67 61 74 69 76 65 20 70 6f 69 6e 74 20   negative point 
555e0 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 65 6c 73  index */.    els
555f0 65 0a 09 76 65 72 74 65 78 20 3d 20 31 3b 09 09  e..vertex = 1;..
55600 2f 2a 20 53 74 61 72 74 50 6f 69 6e 74 28 29 20  /* StartPoint() 
55610 2a 2f 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20  */.    p_blob = 
55620 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
55630 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
55640 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
55650 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
55660 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
55670 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
55680 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
55690 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
556a0 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
556b0 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
556c0 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
556d0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
556e0 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
556f0 20 7b 0a 09 20 20 6c 69 6e 65 20 3d 20 73 69 6d   {..  line = sim
55700 70 6c 65 4c 69 6e 65 73 74 72 69 6e 67 20 28 67  pleLinestring (g
55710 65 6f 29 3b 0a 09 20 20 69 66 20 28 21 6c 69 6e  eo);..  if (!lin
55720 65 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  e)..      sqlite
55730 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
55740 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65  ontext);..  else
55750 0a 09 20 20 20 20 7b 0a 09 09 69 66 20 28 76 65  ..    {...if (ve
55760 72 74 65 78 20 3c 20 30 29 0a 09 09 20 20 20 20  rtex < 0)...    
55770 76 65 72 74 65 78 20 3d 20 6c 69 6e 65 2d 3e 50  vertex = line->P
55780 6f 69 6e 74 73 20 2d 20 31 3b 0a 09 09 65 6c 73  oints - 1;...els
55790 65 0a 09 09 20 20 20 20 76 65 72 74 65 78 20 2d  e...    vertex -
557a0 3d 20 31 3b 09 2f 2a 20 64 65 63 72 65 61 73 69  = 1;./* decreasi
557b0 6e 67 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 64  ng the point ind
557c0 65 78 20 62 79 20 31 2c 20 62 65 63 61 75 73 65  ex by 1, because
557d0 20 50 6f 69 6e 74 4e 20 63 6f 75 6e 74 73 20 73   PointN counts s
557e0 74 61 72 74 69 6e 67 20 61 74 20 69 6e 64 65 78  tarting at index
557f0 20 31 20 2a 2f 0a 09 09 69 66 20 28 76 65 72 74   1 */...if (vert
55800 65 78 20 3e 3d 20 30 20 26 26 20 76 65 72 74 65  ex >= 0 && verte
55810 78 20 3c 20 6c 69 6e 65 2d 3e 50 6f 69 6e 74 73  x < line->Points
55820 29 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20  )...  {...      
55830 69 66 20 28 6c 69 6e 65 2d 3e 44 69 6d 65 6e 73  if (line->Dimens
55840 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41  ionModel == GAIA
55850 5f 58 59 5f 5a 29 0a 09 09 09 7b 0a 09 09 09 20  _XY_Z)....{.... 
55860 20 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74 58     gaiaGetPointX
55870 59 5a 20 28 6c 69 6e 65 2d 3e 43 6f 6f 72 64 73  YZ (line->Coords
55880 2c 20 76 65 72 74 65 78 2c 20 26 78 2c 20 26 79  , vertex, &x, &y
55890 2c 20 26 7a 29 3b 0a 09 09 09 20 20 20 20 72 65  , &z);....    re
558a0 73 75 6c 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63  sult = gaiaAlloc
558b0 47 65 6f 6d 43 6f 6c 6c 58 59 5a 20 28 29 3b 0a  GeomCollXYZ ();.
558c0 09 09 09 20 20 20 20 72 65 73 75 6c 74 2d 3e 53  ...    result->S
558d0 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b  rid = geo->Srid;
558e0 0a 09 09 09 20 20 20 20 67 61 69 61 41 64 64 50  ....    gaiaAddP
558f0 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c 58 59  ointToGeomCollXY
55900 5a 20 28 72 65 73 75 6c 74 2c 20 78 2c 20 79 2c  Z (result, x, y,
55910 20 7a 29 3b 0a 09 09 09 7d 0a 09 09 20 20 20 20   z);....}...    
55920 20 20 65 6c 73 65 20 69 66 20 28 6c 69 6e 65 2d    else if (line-
55930 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20  >DimensionModel 
55940 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 09  == GAIA_XY_M)...
55950 09 7b 0a 09 09 09 20 20 20 20 67 61 69 61 47 65  .{....    gaiaGe
55960 74 50 6f 69 6e 74 58 59 4d 20 28 6c 69 6e 65 2d  tPointXYM (line-
55970 3e 43 6f 6f 72 64 73 2c 20 76 65 72 74 65 78 2c  >Coords, vertex,
55980 20 26 78 2c 20 26 79 2c 20 26 6d 29 3b 0a 09 09   &x, &y, &m);...
55990 09 20 20 20 20 72 65 73 75 6c 74 20 3d 20 67 61  .    result = ga
559a0 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58  iaAllocGeomCollX
559b0 59 4d 20 28 29 3b 0a 09 09 09 20 20 20 20 72 65  YM ();....    re
559c0 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f  sult->Srid = geo
559d0 2d 3e 53 72 69 64 3b 0a 09 09 09 20 20 20 20 67  ->Srid;....    g
559e0 61 69 61 41 64 64 50 6f 69 6e 74 54 6f 47 65 6f  aiaAddPointToGeo
559f0 6d 43 6f 6c 6c 58 59 4d 20 28 72 65 73 75 6c 74  mCollXYM (result
55a00 2c 20 78 2c 20 79 2c 20 6d 29 3b 0a 09 09 09 7d  , x, y, m);....}
55a10 0a 09 09 20 20 20 20 20 20 65 6c 73 65 20 69 66  ...      else if
55a20 20 28 6c 69 6e 65 2d 3e 44 69 6d 65 6e 73 69 6f   (line->Dimensio
55a30 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
55a40 59 5f 5a 5f 4d 29 0a 09 09 09 7b 0a 09 09 09 20  Y_Z_M)....{.... 
55a50 20 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74 58     gaiaGetPointX
55a60 59 5a 4d 20 28 6c 69 6e 65 2d 3e 43 6f 6f 72 64  YZM (line->Coord
55a70 73 2c 20 76 65 72 74 65 78 2c 20 26 78 2c 20 26  s, vertex, &x, &
55a80 79 2c 20 26 7a 2c 0a 09 09 09 09 09 20 20 20 20  y, &z,......    
55a90 20 20 26 6d 29 3b 0a 09 09 09 20 20 20 20 72 65    &m);....    re
55aa0 73 75 6c 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63  sult = gaiaAlloc
55ab0 47 65 6f 6d 43 6f 6c 6c 58 59 5a 4d 20 28 29 3b  GeomCollXYZM ();
55ac0 0a 09 09 09 20 20 20 20 72 65 73 75 6c 74 2d 3e  ....    result->
55ad0 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64  Srid = geo->Srid
55ae0 3b 0a 09 09 09 20 20 20 20 67 61 69 61 41 64 64  ;....    gaiaAdd
55af0 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c 58  PointToGeomCollX
55b00 59 5a 4d 20 28 72 65 73 75 6c 74 2c 20 78 2c 20  YZM (result, x, 
55b10 79 2c 20 7a 2c 20 6d 29 3b 0a 09 09 09 7d 0a 09  y, z, m);....}..
55b20 09 20 20 20 20 20 20 65 6c 73 65 0a 09 09 09 7b  .      else....{
55b30 0a 09 09 09 20 20 20 20 67 61 69 61 47 65 74 50  ....    gaiaGetP
55b40 6f 69 6e 74 20 28 6c 69 6e 65 2d 3e 43 6f 6f 72  oint (line->Coor
55b50 64 73 2c 20 76 65 72 74 65 78 2c 20 26 78 2c 20  ds, vertex, &x, 
55b60 26 79 29 3b 0a 09 09 09 20 20 20 20 72 65 73 75  &y);....    resu
55b70 6c 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65  lt = gaiaAllocGe
55b80 6f 6d 43 6f 6c 6c 20 28 29 3b 0a 09 09 09 20 20  omColl ();....  
55b90 20 20 72 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d    result->Srid =
55ba0 20 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 09 20   geo->Srid;.... 
55bb0 20 20 20 67 61 69 61 41 64 64 50 6f 69 6e 74 54     gaiaAddPointT
55bc0 6f 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c  oGeomColl (resul
55bd0 74 2c 20 78 2c 20 79 29 3b 0a 09 09 09 7d 0a 09  t, x, y);....}..
55be0 09 20 20 7d 0a 09 09 65 6c 73 65 0a 09 09 20 20  .  }...else...  
55bf0 20 20 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b    result = NULL;
55c00 0a 09 09 69 66 20 28 21 72 65 73 75 6c 74 29 0a  ...if (!result).
55c10 09 09 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ..    sqlite3_re
55c20 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
55c30 78 74 29 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20  xt);...else...  
55c40 7b 0a 09 09 20 20 20 20 20 20 67 61 69 61 54 6f  {...      gaiaTo
55c50 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
55c60 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65  b (result, &p_re
55c70 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 20  sult, &len);... 
55c80 20 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f       gaiaFreeGeo
55c90 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a  mColl (result);.
55ca0 09 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
55cb0 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e  result_blob (con
55cc0 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20  text, p_result, 
55cd0 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09 20 20  len, free);...  
55ce0 7d 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d  }..    }.      }
55cf0 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f  .    gaiaFreeGeo
55d00 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a  mColl (geo);.}..
55d10 2f 2a 0a 2f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  /*./ the followi
55d20 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 73 69 6d  ng functions sim
55d30 70 6c 79 20 72 65 61 64 64 72 65 73 73 20 74 68  ply readdress th
55d40 65 20 72 65 71 75 65 73 74 20 74 6f 20 70 6f 69  e request to poi
55d50 6e 74 5f 6e 28 29 0a 2f 20 73 65 74 74 69 6e 67  nt_n()./ setting
55d60 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
55d70 20 72 65 71 75 65 73 74 20 6d 6f 64 65 0a 2a 2f   request mode.*/
55d80 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
55d90 63 74 5f 53 74 61 72 74 50 6f 69 6e 74 20 28 73  ct_StartPoint (s
55da0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
55db0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
55dc0 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
55dd0 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20  e ** argv).{.   
55de0 20 70 6f 69 6e 74 5f 6e 20 28 63 6f 6e 74 65 78   point_n (contex
55df0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47  t, argc, argv, G
55e00 41 49 41 5f 53 54 41 52 54 5f 50 4f 49 4e 54 29  AIA_START_POINT)
55e10 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
55e20 0a 66 6e 63 74 5f 45 6e 64 50 6f 69 6e 74 20 28  .fnct_EndPoint (
55e30 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
55e40 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
55e50 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
55e60 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
55e70 20 20 70 6f 69 6e 74 5f 6e 20 28 63 6f 6e 74 65    point_n (conte
55e80 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20  xt, argc, argv, 
55e90 47 41 49 41 5f 45 4e 44 5f 50 4f 49 4e 54 29 3b  GAIA_END_POINT);
55ea0 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
55eb0 66 6e 63 74 5f 50 6f 69 6e 74 4e 20 28 73 71 6c  fnct_PointN (sql
55ec0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
55ed0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
55ee0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
55ef0 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 70  ** argv).{.    p
55f00 6f 69 6e 74 5f 6e 20 28 63 6f 6e 74 65 78 74 2c  oint_n (context,
55f10 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49   argc, argv, GAI
55f20 41 5f 50 4f 49 4e 54 4e 29 3b 0a 7d 0a 0a 73 74  A_POINTN);.}..st
55f30 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 45  atic void.fnct_E
55f40 78 74 65 72 69 6f 72 52 69 6e 67 20 28 73 71 6c  xteriorRing (sql
55f50 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
55f60 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
55f70 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
55f80 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
55f90 4c 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2f 20 45  L functions:./ E
55fa0 78 74 65 72 69 6f 72 52 69 6e 67 28 42 4c 4f 42  xteriorRing(BLOB
55fb0 20 65 6e 63 6f 64 65 64 20 50 4f 4c 59 47 4f 4e   encoded POLYGON
55fc0 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
55fd0 65 74 75 72 6e 73 20 74 68 65 20 45 58 54 45 52  eturns the EXTER
55fe0 49 4f 52 20 52 49 4e 47 20 66 6f 72 20 63 75 72  IOR RING for cur
55ff0 72 65 6e 74 20 50 4f 4c 59 47 4f 4e 20 67 65 6f  rent POLYGON geo
56000 6d 65 74 72 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c  metry ./ or NULL
56010 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
56020 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
56030 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
56040 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69  r *p_blob;.    i
56050 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
56060 69 6e 74 20 69 76 3b 0a 20 20 20 20 64 6f 75 62  int iv;.    doub
56070 6c 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65  le x;.    double
56080 20 79 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 7a   y;.    double z
56090 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d 3b 0a  ;.    double m;.
560a0 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
560b0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
560c0 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
560d0 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
560e0 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b  lPtr geo = NULL;
560f0 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
56100 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20  lPtr result;.   
56110 20 67 61 69 61 50 6f 6c 79 67 6f 6e 50 74 72 20   gaiaPolygonPtr 
56120 70 6f 6c 79 67 3b 0a 20 20 20 20 67 61 69 61 52  polyg;.    gaiaR
56130 69 6e 67 50 74 72 20 72 69 6e 67 3b 0a 20 20 20  ingPtr ring;.   
56140 20 67 61 69 61 4c 69 6e 65 73 74 72 69 6e 67 50   gaiaLinestringP
56150 74 72 20 6c 69 6e 65 3b 0a 20 20 20 20 47 41 49  tr line;.    GAI
56160 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
56170 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
56180 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
56190 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
561a0 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[0]) != SQLIT
561b0 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a  E_BLOB).      {.
561c0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
561d0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
561e0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
561f0 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20     }.    p_blob 
56200 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
56210 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
56220 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
56230 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
56240 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
56250 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  tes (argv[0]);. 
56260 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f     geo = gaiaFro
56270 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
56280 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
56290 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
562a0 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  eo)..sqlite3_res
562b0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
562c0 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  t);.    else.   
562d0 20 20 20 7b 0a 09 20 20 70 6f 6c 79 67 20 3d 20     {..  polyg = 
562e0 73 69 6d 70 6c 65 50 6f 6c 79 67 6f 6e 20 28 67  simplePolygon (g
562f0 65 6f 29 3b 0a 09 20 20 69 66 20 28 21 70 6f 6c  eo);..  if (!pol
56300 79 67 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74  yg)..      sqlit
56310 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
56320 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73  context);..  els
56330 65 0a 09 20 20 20 20 7b 0a 09 09 72 69 6e 67 20  e..    {...ring 
56340 3d 20 70 6f 6c 79 67 2d 3e 45 78 74 65 72 69 6f  = polyg->Exterio
56350 72 3b 0a 09 09 69 66 20 28 72 69 6e 67 2d 3e 44  r;...if (ring->D
56360 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d  imensionModel ==
56370 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09 09 20 20   GAIA_XY_Z)...  
56380 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41    result = gaiaA
56390 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58 59 5a 20  llocGeomCollXYZ 
563a0 28 29 3b 0a 09 09 65 6c 73 65 20 69 66 20 28 72  ();...else if (r
563b0 69 6e 67 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f  ing->DimensionMo
563c0 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d  del == GAIA_XY_M
563d0 29 0a 09 09 20 20 20 20 72 65 73 75 6c 74 20 3d  )...    result =
563e0 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f   gaiaAllocGeomCo
563f0 6c 6c 58 59 4d 20 28 29 3b 0a 09 09 65 6c 73 65  llXYM ();...else
56400 20 69 66 20 28 72 69 6e 67 2d 3e 44 69 6d 65 6e   if (ring->Dimen
56410 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49  sionModel == GAI
56420 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 20 20 20 20  A_XY_Z_M)...    
56430 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41 6c 6c  result = gaiaAll
56440 6f 63 47 65 6f 6d 43 6f 6c 6c 58 59 5a 4d 20 28  ocGeomCollXYZM (
56450 29 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20  );...else...    
56460 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41 6c 6c  result = gaiaAll
56470 6f 63 47 65 6f 6d 43 6f 6c 6c 20 28 29 3b 0a 09  ocGeomColl ();..
56480 09 72 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20  .result->Srid = 
56490 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 6c 69 6e  geo->Srid;...lin
564a0 65 20 3d 20 67 61 69 61 41 64 64 4c 69 6e 65 73  e = gaiaAddLines
564b0 74 72 69 6e 67 54 6f 47 65 6f 6d 43 6f 6c 6c 20  tringToGeomColl 
564c0 28 72 65 73 75 6c 74 2c 20 72 69 6e 67 2d 3e 50  (result, ring->P
564d0 6f 69 6e 74 73 29 3b 0a 09 09 66 6f 72 20 28 69  oints);...for (i
564e0 76 20 3d 20 30 3b 20 69 76 20 3c 20 6c 69 6e 65  v = 0; iv < line
564f0 2d 3e 50 6f 69 6e 74 73 3b 20 69 76 2b 2b 29 0a  ->Points; iv++).
56500 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 69 66  ..  {...      if
56510 20 28 72 69 6e 67 2d 3e 44 69 6d 65 6e 73 69 6f   (ring->Dimensio
56520 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
56530 59 5f 5a 29 0a 09 09 09 7b 0a 09 09 09 20 20 20  Y_Z)....{....   
56540 20 67 61 69 61 47 65 74 50 6f 69 6e 74 58 59 5a   gaiaGetPointXYZ
56550 20 28 72 69 6e 67 2d 3e 43 6f 6f 72 64 73 2c 20   (ring->Coords, 
56560 69 76 2c 20 26 78 2c 20 26 79 2c 20 26 7a 29 3b  iv, &x, &y, &z);
56570 0a 09 09 09 20 20 20 20 67 61 69 61 53 65 74 50  ....    gaiaSetP
56580 6f 69 6e 74 58 59 5a 20 28 6c 69 6e 65 2d 3e 43  ointXYZ (line->C
56590 6f 6f 72 64 73 2c 20 69 76 2c 20 78 2c 20 79 2c  oords, iv, x, y,
565a0 20 7a 29 3b 0a 09 09 09 7d 0a 09 09 20 20 20 20   z);....}...    
565b0 20 20 65 6c 73 65 20 69 66 20 28 72 69 6e 67 2d    else if (ring-
565c0 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20  >DimensionModel 
565d0 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 09  == GAIA_XY_M)...
565e0 09 7b 0a 09 09 09 20 20 20 20 67 61 69 61 47 65  .{....    gaiaGe
565f0 74 50 6f 69 6e 74 58 59 4d 20 28 72 69 6e 67 2d  tPointXYM (ring-
56600 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 26 78 2c  >Coords, iv, &x,
56610 20 26 79 2c 20 26 6d 29 3b 0a 09 09 09 20 20 20   &y, &m);....   
56620 20 67 61 69 61 53 65 74 50 6f 69 6e 74 58 59 4d   gaiaSetPointXYM
56630 20 28 6c 69 6e 65 2d 3e 43 6f 6f 72 64 73 2c 20   (line->Coords, 
56640 69 76 2c 20 78 2c 20 79 2c 20 6d 29 3b 0a 09 09  iv, x, y, m);...
56650 09 7d 0a 09 09 20 20 20 20 20 20 65 6c 73 65 20  .}...      else 
56660 69 66 20 28 72 69 6e 67 2d 3e 44 69 6d 65 6e 73  if (ring->Dimens
56670 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41  ionModel == GAIA
56680 5f 58 59 5f 5a 5f 4d 29 0a 09 09 09 7b 0a 09 09  _XY_Z_M)....{...
56690 09 20 20 20 20 67 61 69 61 47 65 74 50 6f 69 6e  .    gaiaGetPoin
566a0 74 58 59 5a 4d 20 28 72 69 6e 67 2d 3e 43 6f 6f  tXYZM (ring->Coo
566b0 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26 79 2c  rds, iv, &x, &y,
566c0 20 26 7a 2c 20 26 6d 29 3b 0a 09 09 09 20 20 20   &z, &m);....   
566d0 20 67 61 69 61 53 65 74 50 6f 69 6e 74 58 59 5a   gaiaSetPointXYZ
566e0 4d 20 28 6c 69 6e 65 2d 3e 43 6f 6f 72 64 73 2c  M (line->Coords,
566f0 20 69 76 2c 20 78 2c 20 79 2c 20 7a 2c 20 6d 29   iv, x, y, z, m)
56700 3b 0a 09 09 09 7d 0a 09 09 20 20 20 20 20 20 65  ;....}...      e
56710 6c 73 65 0a 09 09 09 7b 0a 09 09 09 20 20 20 20  lse....{....    
56720 67 61 69 61 47 65 74 50 6f 69 6e 74 20 28 72 69  gaiaGetPoint (ri
56730 6e 67 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20  ng->Coords, iv, 
56740 26 78 2c 20 26 79 29 3b 0a 09 09 09 20 20 20 20  &x, &y);....    
56750 67 61 69 61 53 65 74 50 6f 69 6e 74 20 28 6c 69  gaiaSetPoint (li
56760 6e 65 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20  ne->Coords, iv, 
56770 78 2c 20 79 29 3b 0a 09 09 09 7d 0a 09 09 20 20  x, y);....}...  
56780 7d 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69 61  }...gaiaToSpatia
56790 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72 65 73  LiteBlobWkb (res
567a0 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  ult, &p_result, 
567b0 26 6c 65 6e 29 3b 0a 09 09 67 61 69 61 46 72 65  &len);...gaiaFre
567c0 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c  eGeomColl (resul
567d0 74 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  t);...sqlite3_re
567e0 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
567f0 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
56800 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 20 20 7d  n, free);..    }
56810 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
56820 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
56830 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  eo);.}..static v
56840 6f 69 64 0a 66 6e 63 74 5f 4e 75 6d 49 6e 74 65  oid.fnct_NumInte
56850 72 69 6f 72 52 69 6e 67 73 20 28 73 71 6c 69 74  riorRings (sqlit
56860 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
56870 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a  text, int argc,.
56880 09 09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  ..       sqlite3
56890 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
568a0 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
568b0 6e 3a 0a 2f 20 4e 75 6d 49 6e 74 65 72 69 6f 72  n:./ NumInterior
568c0 52 69 6e 67 73 28 42 4c 4f 42 20 65 6e 63 6f 64  Rings(BLOB encod
568d0 65 64 20 50 4f 4c 59 47 4f 4e 29 0a 2f 0a 2f 20  ed POLYGON)././ 
568e0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
568f0 65 72 20 6f 66 20 49 4e 54 45 52 49 4f 52 20 52  er of INTERIOR R
56900 49 4e 47 53 20 66 6f 72 20 63 75 72 72 65 6e 74  INGS for current
56910 20 50 4f 4c 59 47 4f 4e 20 67 65 6f 6d 65 74 72   POLYGON geometr
56920 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  y ./ or NULL if 
56930 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
56940 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
56950 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
56960 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
56970 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61  _bytes;.    gaia
56980 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
56990 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
569a0 50 6f 6c 79 67 6f 6e 50 74 72 20 70 6f 6c 79 67  PolygonPtr polyg
569b0 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
569c0 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
569d0 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
569e0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
569f0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
56a00 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
56a10 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
56a20 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
56a30 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
56a40 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
56a50 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
56a60 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
56a70 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
56a80 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
56a90 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
56aa0 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
56ab0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20  gv[0]);.    geo 
56ac0 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
56ad0 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
56ae0 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
56af0 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71     if (!geo)..sq
56b00 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
56b10 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
56b20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
56b30 20 70 6f 6c 79 67 20 3d 20 73 69 6d 70 6c 65 50   polyg = simpleP
56b40 6f 6c 79 67 6f 6e 20 28 67 65 6f 29 3b 0a 09 20  olygon (geo);.. 
56b50 20 69 66 20 28 21 70 6f 6c 79 67 29 0a 09 20 20   if (!polyg)..  
56b60 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
56b70 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
56b80 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20  );..  else..    
56b90 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
56ba0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 70  _int (context, p
56bb0 6f 6c 79 67 2d 3e 4e 75 6d 49 6e 74 65 72 69 6f  olyg->NumInterio
56bc0 72 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rs);.      }.   
56bd0 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
56be0 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74  l (geo);.}..stat
56bf0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49 6e 74  ic void.fnct_Int
56c00 65 72 69 6f 72 52 69 6e 67 4e 20 28 73 71 6c 69  eriorRingN (sqli
56c10 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
56c20 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
56c30 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
56c40 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
56c50 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2f 20 49 6e   functions:./ In
56c60 74 65 72 69 6f 72 52 69 6e 67 4e 28 42 4c 4f 42  teriorRingN(BLOB
56c70 20 65 6e 63 6f 64 65 64 20 50 4f 4c 59 47 4f 4e   encoded POLYGON
56c80 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 72   geometry)././ r
56c90 65 74 75 72 6e 73 20 74 68 65 20 4e 74 68 20 49  eturns the Nth I
56ca0 4e 54 45 52 49 4f 52 20 52 49 4e 47 20 66 6f 72  NTERIOR RING for
56cb0 20 63 75 72 72 65 6e 74 20 50 4f 4c 59 47 4f 4e   current POLYGON
56cc0 20 67 65 6f 6d 65 74 72 79 20 0a 2f 20 6f 72 20   geometry ./ or 
56cd0 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f  NULL if any erro
56ce0 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
56cf0 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  .*/.    unsigned
56d00 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20   char *p_blob;. 
56d10 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a     int n_bytes;.
56d20 20 20 20 20 69 6e 74 20 62 6f 72 64 65 72 3b 0a      int border;.
56d30 20 20 20 20 69 6e 74 20 69 76 3b 0a 20 20 20 20      int iv;.    
56d40 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 64 6f  double x;.    do
56d50 75 62 6c 65 20 79 3b 0a 20 20 20 20 64 6f 75 62  uble y;.    doub
56d60 6c 65 20 7a 3b 0a 20 20 20 20 64 6f 75 62 6c 65  le z;.    double
56d70 20 6d 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b   m;.    int len;
56d80 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
56d90 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e  ar *p_result = N
56da0 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ULL;.    gaiaGeo
56db0 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e  mCollPtr geo = N
56dc0 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ULL;.    gaiaGeo
56dd0 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b  mCollPtr result;
56de0 0a 20 20 20 20 67 61 69 61 50 6f 6c 79 67 6f 6e  .    gaiaPolygon
56df0 50 74 72 20 70 6f 6c 79 67 3b 0a 20 20 20 20 67  Ptr polyg;.    g
56e00 61 69 61 52 69 6e 67 50 74 72 20 72 69 6e 67 3b  aiaRingPtr ring;
56e10 0a 20 20 20 20 67 61 69 61 4c 69 6e 65 73 74 72  .    gaiaLinestr
56e20 69 6e 67 50 74 72 20 6c 69 6e 65 3b 0a 20 20 20  ingPtr line;.   
56e30 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
56e40 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
56e50 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
56e60 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
56e70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
56e80 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20  QLITE_BLOB).    
56e90 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
56ea0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
56eb0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
56ec0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
56ed0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
56ee0 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d  ype (argv[1]) !=
56ef0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
56f00 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
56f10 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
56f20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
56f30 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
56f40 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
56f50 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
56f60 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
56f70 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
56f80 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
56f90 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
56fa0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 62 6f 72 64  gv[0]);.    bord
56fb0 65 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  er = sqlite3_val
56fc0 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
56fd0 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61  ;.    geo = gaia
56fe0 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
56ff0 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
57000 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
57010 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f  (!geo)..sqlite3_
57020 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
57030 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a  text);.    else.
57040 20 20 20 20 20 20 7b 0a 09 20 20 70 6f 6c 79 67        {..  polyg
57050 20 3d 20 73 69 6d 70 6c 65 50 6f 6c 79 67 6f 6e   = simplePolygon
57060 20 28 67 65 6f 29 3b 0a 09 20 20 69 66 20 28 21   (geo);..  if (!
57070 70 6f 6c 79 67 29 0a 09 20 20 20 20 20 20 73 71  polyg)..      sq
57080 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
57090 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
570a0 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 69 66  else..    {...if
570b0 20 28 62 6f 72 64 65 72 20 3e 3d 20 31 20 26 26   (border >= 1 &&
570c0 20 62 6f 72 64 65 72 20 3c 3d 20 70 6f 6c 79 67   border <= polyg
570d0 2d 3e 4e 75 6d 49 6e 74 65 72 69 6f 72 73 29 0a  ->NumInteriors).
570e0 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 72 69  ..  {...      ri
570f0 6e 67 20 3d 20 70 6f 6c 79 67 2d 3e 49 6e 74 65  ng = polyg->Inte
57100 72 69 6f 72 73 20 2b 20 28 62 6f 72 64 65 72 20  riors + (border 
57110 2d 20 31 29 3b 0a 09 09 20 20 20 20 20 20 69 66  - 1);...      if
57120 20 28 72 69 6e 67 2d 3e 44 69 6d 65 6e 73 69 6f   (ring->Dimensio
57130 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
57140 59 5f 5a 29 0a 09 09 09 20 20 72 65 73 75 6c 74  Y_Z)....  result
57150 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d   = gaiaAllocGeom
57160 43 6f 6c 6c 58 59 5a 20 28 29 3b 0a 09 09 20 20  CollXYZ ();...  
57170 20 20 20 20 65 6c 73 65 20 69 66 20 28 72 69 6e      else if (rin
57180 67 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65  g->DimensionMode
57190 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a  l == GAIA_XY_M).
571a0 09 09 09 20 20 72 65 73 75 6c 74 20 3d 20 67 61  ...  result = ga
571b0 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58  iaAllocGeomCollX
571c0 59 4d 20 28 29 3b 0a 09 09 20 20 20 20 20 20 65  YM ();...      e
571d0 6c 73 65 20 69 66 20 28 72 69 6e 67 2d 3e 44 69  lse if (ring->Di
571e0 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20  mensionModel == 
571f0 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 09  GAIA_XY_Z_M)....
57200 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41    result = gaiaA
57210 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58 59 5a 4d  llocGeomCollXYZM
57220 20 28 29 3b 0a 09 09 20 20 20 20 20 20 65 6c 73   ();...      els
57230 65 0a 09 09 09 20 20 72 65 73 75 6c 74 20 3d 20  e....  result = 
57240 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c  gaiaAllocGeomCol
57250 6c 20 28 29 3b 0a 09 09 20 20 20 20 20 20 72 65  l ();...      re
57260 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f  sult->Srid = geo
57270 2d 3e 53 72 69 64 3b 0a 09 09 20 20 20 20 20 20  ->Srid;...      
57280 6c 69 6e 65 20 3d 20 67 61 69 61 41 64 64 4c 69  line = gaiaAddLi
57290 6e 65 73 74 72 69 6e 67 54 6f 47 65 6f 6d 43 6f  nestringToGeomCo
572a0 6c 6c 20 28 72 65 73 75 6c 74 2c 20 72 69 6e 67  ll (result, ring
572b0 2d 3e 50 6f 69 6e 74 73 29 3b 0a 09 09 20 20 20  ->Points);...   
572c0 20 20 20 66 6f 72 20 28 69 76 20 3d 20 30 3b 20     for (iv = 0; 
572d0 69 76 20 3c 20 6c 69 6e 65 2d 3e 50 6f 69 6e 74  iv < line->Point
572e0 73 3b 20 69 76 2b 2b 29 0a 09 09 09 7b 0a 09 09  s; iv++)....{...
572f0 09 20 20 20 20 69 66 20 28 72 69 6e 67 2d 3e 44  .    if (ring->D
57300 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d  imensionModel ==
57310 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09 09 09 20   GAIA_XY_Z).... 
57320 20 20 20 20 20 7b 0a 09 09 09 09 20 20 67 61 69       {.....  gai
57330 61 47 65 74 50 6f 69 6e 74 58 59 5a 20 28 72 69  aGetPointXYZ (ri
57340 6e 67 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20  ng->Coords, iv, 
57350 26 78 2c 20 26 79 2c 0a 09 09 09 09 09 09 20 20  &x, &y,.......  
57360 20 26 7a 29 3b 0a 09 09 09 09 20 20 67 61 69 61   &z);.....  gaia
57370 53 65 74 50 6f 69 6e 74 58 59 5a 20 28 6c 69 6e  SetPointXYZ (lin
57380 65 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 78  e->Coords, iv, x
57390 2c 20 79 2c 20 7a 29 3b 0a 09 09 09 20 20 20 20  , y, z);....    
573a0 20 20 7d 0a 09 09 09 20 20 20 20 65 6c 73 65 20    }....    else 
573b0 69 66 20 28 72 69 6e 67 2d 3e 44 69 6d 65 6e 73  if (ring->Dimens
573c0 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41  ionModel == GAIA
573d0 5f 58 59 5f 4d 29 0a 09 09 09 20 20 20 20 20 20  _XY_M)....      
573e0 7b 0a 09 09 09 09 20 20 67 61 69 61 47 65 74 50  {.....  gaiaGetP
573f0 6f 69 6e 74 58 59 4d 20 28 72 69 6e 67 2d 3e 43  ointXYM (ring->C
57400 6f 6f 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26  oords, iv, &x, &
57410 79 2c 0a 09 09 09 09 09 09 20 20 20 26 6d 29 3b  y,.......   &m);
57420 0a 09 09 09 09 20 20 67 61 69 61 53 65 74 50 6f  .....  gaiaSetPo
57430 69 6e 74 58 59 4d 20 28 6c 69 6e 65 2d 3e 43 6f  intXYM (line->Co
57440 6f 72 64 73 2c 20 69 76 2c 20 78 2c 20 79 2c 20  ords, iv, x, y, 
57450 6d 29 3b 0a 09 09 09 20 20 20 20 20 20 7d 0a 09  m);....      }..
57460 09 09 20 20 20 20 65 6c 73 65 20 69 66 20 28 72  ..    else if (r
57470 69 6e 67 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f  ing->DimensionMo
57480 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a  del == GAIA_XY_Z
57490 5f 4d 29 0a 09 09 09 20 20 20 20 20 20 7b 0a 09  _M)....      {..
574a0 09 09 09 20 20 67 61 69 61 47 65 74 50 6f 69 6e  ...  gaiaGetPoin
574b0 74 58 59 5a 4d 20 28 72 69 6e 67 2d 3e 43 6f 6f  tXYZM (ring->Coo
574c0 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26 79 2c  rds, iv, &x, &y,
574d0 0a 09 09 09 09 09 09 20 20 20 20 26 7a 2c 20 26  .......    &z, &
574e0 6d 29 3b 0a 09 09 09 09 20 20 67 61 69 61 53 65  m);.....  gaiaSe
574f0 74 50 6f 69 6e 74 58 59 5a 4d 20 28 6c 69 6e 65  tPointXYZM (line
57500 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 78 2c  ->Coords, iv, x,
57510 20 79 2c 20 7a 2c 0a 09 09 09 09 09 09 20 20 20   y, z,.......   
57520 20 6d 29 3b 0a 09 09 09 20 20 20 20 20 20 7d 0a   m);....      }.
57530 09 09 09 20 20 20 20 65 6c 73 65 0a 09 09 09 20  ...    else.... 
57540 20 20 20 20 20 7b 0a 09 09 09 09 20 20 67 61 69       {.....  gai
57550 61 47 65 74 50 6f 69 6e 74 20 28 72 69 6e 67 2d  aGetPoint (ring-
57560 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 26 78 2c  >Coords, iv, &x,
57570 20 26 79 29 3b 0a 09 09 09 09 20 20 67 61 69 61   &y);.....  gaia
57580 53 65 74 50 6f 69 6e 74 20 28 6c 69 6e 65 2d 3e  SetPoint (line->
57590 43 6f 6f 72 64 73 2c 20 69 76 2c 20 78 2c 20 79  Coords, iv, x, y
575a0 29 3b 0a 09 09 09 20 20 20 20 20 20 7d 0a 09 09  );....      }...
575b0 09 7d 0a 09 09 20 20 20 20 20 20 67 61 69 61 54  .}...      gaiaT
575c0 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  oSpatiaLiteBlobW
575d0 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72  kb (result, &p_r
575e0 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09  esult, &len);...
575f0 20 20 20 20 20 20 67 61 69 61 46 72 65 65 47 65        gaiaFreeGe
57600 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b  omColl (result);
57610 0a 09 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  ...      sqlite3
57620 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
57630 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
57640 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09 20   len, free);... 
57650 20 7d 0a 09 09 65 6c 73 65 0a 09 09 20 20 20 20   }...else...    
57660 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
57670 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
57680 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
57690 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
576a0 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61  ll (geo);.}..sta
576b0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4e 75  tic void.fnct_Nu
576c0 6d 47 65 6f 6d 65 74 72 69 65 73 20 28 73 71 6c  mGeometries (sql
576d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
576e0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
576f0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
57700 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
57710 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4e 75  L function:./ Nu
57720 6d 47 65 6f 6d 65 74 72 69 65 73 28 42 4c 4f 42  mGeometries(BLOB
57730 20 65 6e 63 6f 64 65 64 20 47 45 4f 4d 45 54 52   encoded GEOMETR
57740 59 43 4f 4c 4c 45 43 54 49 4f 4e 29 0a 2f 0a 2f  YCOLLECTION)././
57750 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
57760 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 61 72  ber of elementar
57770 79 20 67 65 6f 6d 65 74 72 69 65 73 20 66 6f 72  y geometries for
57780 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
57790 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  y ./ or NULL if 
577a0 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
577b0 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
577c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
577d0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
577e0 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
577f0 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 67 61 69  cnt = 0;.    gai
57800 61 50 6f 69 6e 74 50 74 72 20 70 6f 69 6e 74 3b  aPointPtr point;
57810 0a 20 20 20 20 67 61 69 61 4c 69 6e 65 73 74 72  .    gaiaLinestr
57820 69 6e 67 50 74 72 20 6c 69 6e 65 3b 0a 20 20 20  ingPtr line;.   
57830 20 67 61 69 61 50 6f 6c 79 67 6f 6e 50 74 72 20   gaiaPolygonPtr 
57840 70 6f 6c 79 67 3b 0a 20 20 20 20 67 61 69 61 47  polyg;.    gaiaG
57850 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
57860 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f   NULL;.    GAIA_
57870 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
57880 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
57890 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
578a0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
578b0 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
578c0 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
578d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
578e0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
578f0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
57900 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
57910 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
57920 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
57930 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
57940 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
57950 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
57960 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
57970 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
57980 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
57990 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
579a0 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
579b0 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
579c0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
579d0 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
579e0 20 7b 0a 09 20 20 70 6f 69 6e 74 20 3d 20 67 65   {..  point = ge
579f0 6f 2d 3e 46 69 72 73 74 50 6f 69 6e 74 3b 0a 09  o->FirstPoint;..
57a00 20 20 77 68 69 6c 65 20 28 70 6f 69 6e 74 29 0a    while (point).
57a10 09 20 20 20 20 7b 0a 09 09 2f 2a 20 63 6f 75 6e  .    {.../* coun
57a20 74 73 20 68 6f 77 20 6d 61 6e 79 20 70 6f 69 6e  ts how many poin
57a30 74 73 20 61 72 65 20 74 68 65 72 65 20 2a 2f 0a  ts are there */.
57a40 09 09 63 6e 74 2b 2b 3b 0a 09 09 70 6f 69 6e 74  ..cnt++;...point
57a50 20 3d 20 70 6f 69 6e 74 2d 3e 4e 65 78 74 3b 0a   = point->Next;.
57a60 09 20 20 20 20 7d 0a 09 20 20 6c 69 6e 65 20 3d  .    }..  line =
57a70 20 67 65 6f 2d 3e 46 69 72 73 74 4c 69 6e 65 73   geo->FirstLines
57a80 74 72 69 6e 67 3b 0a 09 20 20 77 68 69 6c 65 20  tring;..  while 
57a90 28 6c 69 6e 65 29 0a 09 20 20 20 20 7b 0a 09 09  (line)..    {...
57aa0 2f 2a 20 63 6f 75 6e 74 73 20 68 6f 77 20 6d 61  /* counts how ma
57ab0 6e 79 20 6c 69 6e 65 73 74 72 69 6e 67 73 20 61  ny linestrings a
57ac0 72 65 20 74 68 65 72 65 20 2a 2f 0a 09 09 63 6e  re there */...cn
57ad0 74 2b 2b 3b 0a 09 09 6c 69 6e 65 20 3d 20 6c 69  t++;...line = li
57ae0 6e 65 2d 3e 4e 65 78 74 3b 0a 09 20 20 20 20 7d  ne->Next;..    }
57af0 0a 09 20 20 70 6f 6c 79 67 20 3d 20 67 65 6f 2d  ..  polyg = geo-
57b00 3e 46 69 72 73 74 50 6f 6c 79 67 6f 6e 3b 0a 09  >FirstPolygon;..
57b10 20 20 77 68 69 6c 65 20 28 70 6f 6c 79 67 29 0a    while (polyg).
57b20 09 20 20 20 20 7b 0a 09 09 2f 2a 20 63 6f 75 6e  .    {.../* coun
57b30 74 73 20 68 6f 77 20 6d 61 6e 79 20 70 6f 6c 79  ts how many poly
57b40 67 6f 6e 73 20 61 72 65 20 74 68 65 72 65 20 2a  gons are there *
57b50 2f 0a 09 09 63 6e 74 2b 2b 3b 0a 09 09 70 6f 6c  /...cnt++;...pol
57b60 79 67 20 3d 20 70 6f 6c 79 67 2d 3e 4e 65 78 74  yg = polyg->Next
57b70 3b 0a 09 20 20 20 20 7d 0a 09 20 20 73 71 6c 69  ;..    }..  sqli
57b80 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
57b90 63 6f 6e 74 65 78 74 2c 20 63 6e 74 29 3b 0a 20  context, cnt);. 
57ba0 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
57bb0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
57bc0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
57bd0 64 0a 66 6e 63 74 5f 4e 50 6f 69 6e 74 73 20 28  d.fnct_NPoints (
57be0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
57bf0 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
57c00 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
57c10 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
57c20 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
57c30 20 53 54 5f 4e 50 6f 69 6e 74 73 28 42 4c 4f 42   ST_NPoints(BLOB
57c40 20 65 6e 63 6f 64 65 64 20 47 45 4f 4d 45 54 52   encoded GEOMETR
57c50 59 43 4f 4c 4c 45 43 54 49 4f 4e 29 0a 2f 0a 2f  YCOLLECTION)././
57c60 20 72 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74   returns the tot
57c70 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69  al number of poi
57c80 6e 74 73 2f 76 65 72 74 69 63 65 73 20 66 6f 72  nts/vertices for
57c90 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
57ca0 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  y ./ or NULL if 
57cb0 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
57cc0 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
57cd0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
57ce0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
57cf0 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
57d00 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  cnt = 0;.    int
57d10 20 69 62 3b 0a 20 20 20 20 67 61 69 61 50 6f 69   ib;.    gaiaPoi
57d20 6e 74 50 74 72 20 70 6f 69 6e 74 3b 0a 20 20 20  ntPtr point;.   
57d30 20 67 61 69 61 4c 69 6e 65 73 74 72 69 6e 67 50   gaiaLinestringP
57d40 74 72 20 6c 69 6e 65 3b 0a 20 20 20 20 67 61 69  tr line;.    gai
57d50 61 50 6f 6c 79 67 6f 6e 50 74 72 20 70 6f 6c 79  aPolygonPtr poly
57d60 67 3b 0a 20 20 20 20 67 61 69 61 52 69 6e 67 50  g;.    gaiaRingP
57d70 74 72 20 72 6e 67 3b 0a 20 20 20 20 67 61 69 61  tr rng;.    gaia
57d80 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
57d90 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41  = NULL;.    GAIA
57da0 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
57db0 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
57dc0 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
57dd0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
57de0 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
57df0 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
57e00 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
57e10 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
57e20 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
57e30 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
57e40 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
57e50 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
57e60 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
57e70 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
57e80 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
57e90 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
57ea0 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d    geo = gaiaFrom
57eb0 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
57ec0 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
57ed0 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65  es);.    if (!ge
57ee0 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  o)..sqlite3_resu
57ef0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
57f00 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20  );.    else.    
57f10 20 20 7b 0a 09 20 20 70 6f 69 6e 74 20 3d 20 67    {..  point = g
57f20 65 6f 2d 3e 46 69 72 73 74 50 6f 69 6e 74 3b 0a  eo->FirstPoint;.
57f30 09 20 20 77 68 69 6c 65 20 28 70 6f 69 6e 74 29  .  while (point)
57f40 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 63 6f 75  ..    {.../* cou
57f50 6e 74 73 20 68 6f 77 20 6d 61 6e 79 20 70 6f 69  nts how many poi
57f60 6e 74 73 20 61 72 65 20 74 68 65 72 65 20 2a 2f  nts are there */
57f70 0a 09 09 63 6e 74 2b 2b 3b 0a 09 09 70 6f 69 6e  ...cnt++;...poin
57f80 74 20 3d 20 70 6f 69 6e 74 2d 3e 4e 65 78 74 3b  t = point->Next;
57f90 0a 09 20 20 20 20 7d 0a 09 20 20 6c 69 6e 65 20  ..    }..  line 
57fa0 3d 20 67 65 6f 2d 3e 46 69 72 73 74 4c 69 6e 65  = geo->FirstLine
57fb0 73 74 72 69 6e 67 3b 0a 09 20 20 77 68 69 6c 65  string;..  while
57fc0 20 28 6c 69 6e 65 29 0a 09 20 20 20 20 7b 0a 09   (line)..    {..
57fd0 09 2f 2a 20 63 6f 75 6e 74 73 20 68 6f 77 20 6d  ./* counts how m
57fe0 61 6e 79 20 70 6f 69 6e 74 73 20 61 72 65 20 74  any points are t
57ff0 68 65 72 65 20 2a 2f 0a 09 09 63 6e 74 20 2b 3d  here */...cnt +=
58000 20 6c 69 6e 65 2d 3e 50 6f 69 6e 74 73 3b 0a 09   line->Points;..
58010 09 6c 69 6e 65 20 3d 20 6c 69 6e 65 2d 3e 4e 65  .line = line->Ne
58020 78 74 3b 0a 09 20 20 20 20 7d 0a 09 20 20 70 6f  xt;..    }..  po
58030 6c 79 67 20 3d 20 67 65 6f 2d 3e 46 69 72 73 74  lyg = geo->First
58040 50 6f 6c 79 67 6f 6e 3b 0a 09 20 20 77 68 69 6c  Polygon;..  whil
58050 65 20 28 70 6f 6c 79 67 29 0a 09 20 20 20 20 7b  e (polyg)..    {
58060 0a 09 09 2f 2a 20 63 6f 75 6e 74 73 20 68 6f 77  .../* counts how
58070 20 6d 61 6e 79 20 70 6f 69 6e 74 73 20 61 72 65   many points are
58080 20 69 6e 20 74 68 65 20 65 78 74 65 72 69 6f 72   in the exterior
58090 20 72 69 6e 67 20 2a 2f 0a 09 09 72 6e 67 20 3d   ring */...rng =
580a0 20 70 6f 6c 79 67 2d 3e 45 78 74 65 72 69 6f 72   polyg->Exterior
580b0 3b 0a 09 09 63 6e 74 20 2b 3d 20 72 6e 67 2d 3e  ;...cnt += rng->
580c0 50 6f 69 6e 74 73 3b 0a 09 09 66 6f 72 20 28 69  Points;...for (i
580d0 62 20 3d 20 30 3b 20 69 62 20 3c 20 70 6f 6c 79  b = 0; ib < poly
580e0 67 2d 3e 4e 75 6d 49 6e 74 65 72 69 6f 72 73 3b  g->NumInteriors;
580f0 20 69 62 2b 2b 29 0a 09 09 20 20 7b 0a 09 09 20   ib++)...  {... 
58100 20 20 20 20 20 2f 2a 20 70 72 6f 63 65 73 73 69       /* processi
58110 6e 67 20 61 6e 79 20 69 6e 74 65 72 69 6f 72 20  ng any interior 
58120 72 69 6e 67 20 2a 2f 0a 09 09 20 20 20 20 20 20  ring */...      
58130 72 6e 67 20 3d 20 70 6f 6c 79 67 2d 3e 49 6e 74  rng = polyg->Int
58140 65 72 69 6f 72 73 20 2b 20 69 62 3b 0a 09 09 20  eriors + ib;... 
58150 20 20 20 20 20 63 6e 74 20 2b 3d 20 72 6e 67 2d       cnt += rng-
58160 3e 50 6f 69 6e 74 73 3b 0a 09 09 20 20 7d 0a 09  >Points;...  }..
58170 09 70 6f 6c 79 67 20 3d 20 70 6f 6c 79 67 2d 3e  .polyg = polyg->
58180 4e 65 78 74 3b 0a 09 20 20 20 20 7d 0a 09 20 20  Next;..    }..  
58190 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
581a0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 63 6e 74  nt (context, cnt
581b0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67  );.      }.    g
581c0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
581d0 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  (geo);.}..static
581e0 20 76 6f 69 64 0a 66 6e 63 74 5f 4e 52 69 6e 67   void.fnct_NRing
581f0 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  s (sqlite3_conte
58200 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
58210 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
58220 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
58230 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
58240 3a 0a 2f 20 53 54 5f 4e 52 69 6e 67 73 28 42 4c  :./ ST_NRings(BL
58250 4f 42 20 65 6e 63 6f 64 65 64 20 47 45 4f 4d 45  OB encoded GEOME
58260 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 29 0a 2f  TRYCOLLECTION)./
58270 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20 74  ./ returns the t
58280 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72  otal number of r
58290 69 6e 67 73 20 66 6f 72 20 63 75 72 72 65 6e 74  ings for current
582a0 20 67 65 6f 6d 65 74 72 79 20 0a 2f 20 28 74 68   geometry ./ (th
582b0 69 73 20 69 6e 63 6c 75 64 69 6e 67 20 62 6f 74  is including bot
582c0 68 20 69 6e 74 65 72 69 6f 72 20 61 6e 64 20 65  h interior and e
582d0 78 74 65 72 69 6f 72 20 72 69 6e 67 73 29 0a 2f  xterior rings)./
582e0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
582f0 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
58300 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
58310 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
58320 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
58330 65 73 3b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20  es;.    int cnt 
58340 3d 20 30 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c  = 0;.    gaiaPol
58350 79 67 6f 6e 50 74 72 20 70 6f 6c 79 67 3b 0a 20  ygonPtr polyg;. 
58360 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
58370 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20  tr geo = NULL;. 
58380 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
58390 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
583a0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
583b0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
583c0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
583d0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
583e0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
583f0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
58400 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
58410 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
58420 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
58430 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
58440 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
58450 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
58460 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
58470 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
58480 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
58490 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
584a0 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
584b0 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
584c0 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
584d0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
584e0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
584f0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 70 6f  se.      {..  po
58500 6c 79 67 20 3d 20 67 65 6f 2d 3e 46 69 72 73 74  lyg = geo->First
58510 50 6f 6c 79 67 6f 6e 3b 0a 09 20 20 77 68 69 6c  Polygon;..  whil
58520 65 20 28 70 6f 6c 79 67 29 0a 09 20 20 20 20 7b  e (polyg)..    {
58530 0a 09 09 2f 2a 20 63 6f 75 6e 74 73 20 68 6f 77  .../* counts how
58540 20 6d 61 6e 79 20 72 69 6e 67 73 20 61 72 65 20   many rings are 
58550 74 68 65 72 65 20 2a 2f 0a 09 09 63 6e 74 20 2b  there */...cnt +
58560 3d 20 70 6f 6c 79 67 2d 3e 4e 75 6d 49 6e 74 65  = polyg->NumInte
58570 72 69 6f 72 73 20 2b 20 31 3b 0a 09 09 70 6f 6c  riors + 1;...pol
58580 79 67 20 3d 20 70 6f 6c 79 67 2d 3e 4e 65 78 74  yg = polyg->Next
58590 3b 0a 09 20 20 20 20 7d 0a 09 20 20 73 71 6c 69  ;..    }..  sqli
585a0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
585b0 63 6f 6e 74 65 78 74 2c 20 63 6e 74 29 3b 0a 20  context, cnt);. 
585c0 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
585d0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
585e0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 68 61  );.}..static cha
585f0 72 20 67 61 72 73 4d 61 70 70 69 6e 67 5b 32 34  r garsMapping[24
58600 5d 20 3d 20 7b 20 27 41 27 2c 20 27 42 27 2c 20  ] = { 'A', 'B', 
58610 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c 20 27  'C', 'D', 'E', '
58620 46 27 2c 20 27 47 27 2c 20 27 48 27 2c 20 27 4a  F', 'G', 'H', 'J
58630 27 2c 0a 20 20 20 20 27 4b 27 2c 20 27 4c 27 2c  ',.    'K', 'L',
58640 20 27 4d 27 2c 20 27 4e 27 2c 20 27 50 27 2c 20   'M', 'N', 'P', 
58650 27 51 27 2c 20 27 52 27 2c 20 27 53 27 2c 20 27  'Q', 'R', 'S', '
58660 54 27 2c 20 27 55 27 2c 20 27 56 27 2c 20 27 57  T', 'U', 'V', 'W
58670 27 2c 20 27 58 27 2c 20 27 59 27 2c 20 27 5a 27  ', 'X', 'Y', 'Z'
58680 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 63 68 61 72  .};..static char
58690 0a 67 61 72 73 4c 65 74 74 65 72 43 6f 64 65 20  .garsLetterCode 
586a0 28 69 6e 74 20 76 61 6c 75 65 29 0a 7b 0a 20 20  (int value).{.  
586b0 20 20 72 65 74 75 72 6e 20 67 61 72 73 4d 61 70    return garsMap
586c0 70 69 6e 67 5b 76 61 6c 75 65 5d 3b 0a 7d 0a 0a  ping[value];.}..
586d0 73 74 61 74 69 63 20 69 6e 74 0a 67 61 72 73 4d  static int.garsM
586e0 61 70 70 69 6e 67 49 6e 64 65 78 20 28 63 6f 6e  appingIndex (con
586f0 73 74 20 63 68 61 72 20 6c 65 74 74 65 72 29 0a  st char letter).
58700 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b  {.    int i = 0;
58710 0a 20 20 20 20 66 6f 72 20 28 69 20 3d 20 30 3b  .    for (i = 0;
58720 20 69 20 3c 20 32 34 3b 20 2b 2b 69 29 0a 20 20   i < 24; ++i).  
58730 20 20 20 20 7b 0a 09 20 20 69 66 20 28 6c 65 74      {..  if (let
58740 74 65 72 20 3d 3d 20 67 61 72 73 4d 61 70 70 69  ter == garsMappi
58750 6e 67 5b 69 5d 29 0a 09 20 20 20 20 20 20 72 65  ng[i])..      re
58760 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a  turn i;.      }.
58770 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d      return -1;.}
58780 0a 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 0a  ..static double.
58790 67 61 72 73 4c 65 74 74 65 72 54 6f 44 65 67 72  garsLetterToDegr
587a0 65 65 73 4c 61 74 20 28 63 68 61 72 20 6d 73 64  eesLat (char msd
587b0 2c 20 63 68 61 72 20 6c 73 64 29 0a 7b 0a 20 20  , char lsd).{.  
587c0 20 20 64 6f 75 62 6c 65 20 68 69 67 68 20 3d 20    double high = 
587d0 67 61 72 73 4d 61 70 70 69 6e 67 49 6e 64 65 78  garsMappingIndex
587e0 20 28 6d 73 64 29 20 2a 20 32 34 2e 30 3b 0a 20   (msd) * 24.0;. 
587f0 20 20 20 64 6f 75 62 6c 65 20 6c 6f 77 20 3d 20     double low = 
58800 67 61 72 73 4d 61 70 70 69 6e 67 49 6e 64 65 78  garsMappingIndex
58810 20 28 6c 73 64 29 3b 0a 20 20 20 20 69 66 20 28   (lsd);.    if (
58820 28 68 69 67 68 20 3c 20 30 29 20 7c 7c 20 28 6c  (high < 0) || (l
58830 6f 77 20 3c 20 30 29 29 0a 20 20 20 20 20 20 7b  ow < 0)).      {
58840 0a 09 20 20 72 65 74 75 72 6e 20 2d 31 30 30 2e  ..  return -100.
58850 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 72  0;.      }.    r
58860 65 74 75 72 6e 20 28 28 28 68 69 67 68 20 2b 20  eturn (((high + 
58870 6c 6f 77 29 20 2a 20 30 2e 35 29 20 2d 20 39 30  low) * 0.5) - 90
58880 2e 30 29 3b 0a 7d 0a 0a 0a 73 74 61 74 69 63 20  .0);.}...static 
58890 76 6f 69 64 0a 66 6e 63 74 5f 47 41 52 53 4d 62  void.fnct_GARSMb
588a0 72 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  r (sqlite3_conte
588b0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
588c0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
588d0 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
588e0 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
588f0 3a 0a 2f 20 47 41 52 53 4d 62 72 28 54 65 78 74  :./ GARSMbr(Text
58900 29 0a 2f 0a 2f 20 63 6f 6e 76 65 72 74 73 20 74  )././ converts t
58910 68 65 20 54 65 78 74 20 28 77 68 69 63 68 20 73  he Text (which s
58920 68 6f 75 6c 64 20 62 65 20 61 20 76 61 6c 69 64  hould be a valid
58930 20 47 41 52 53 20 61 72 65 61 29 20 74 6f 20 74   GARS area) to t
58940 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
58950 0a 2f 20 4d 42 52 20 67 65 6f 6d 65 74 72 79 2e  ./ MBR geometry.
58960 0a 2f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ./ This function
58970 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c   will return NUL
58980 4c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  L if an error oc
58990 63 75 72 73 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73  curs.*/.    cons
589a0 74 20 63 68 61 72 20 2a 74 65 78 74 20 3d 20 4e  t char *text = N
589b0 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e  ULL;.    int len
589c0 20 3d 20 30 3b 0a 20 20 20 20 75 6e 73 69 67 6e   = 0;.    unsign
589d0 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c  ed char *p_resul
589e0 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f  t = NULL;.    do
589f0 75 62 6c 65 20 78 31 20 3d 20 30 2e 30 3b 0a 20  uble x1 = 0.0;. 
58a00 20 20 20 64 6f 75 62 6c 65 20 79 31 20 3d 20 30     double y1 = 0
58a10 2e 30 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78  .0;.    double x
58a20 32 20 3d 20 30 2e 30 3b 0a 20 20 20 20 64 6f 75  2 = 0.0;.    dou
58a30 62 6c 65 20 79 32 20 3d 20 30 2e 30 3b 0a 0a 20  ble y2 = 0.0;.. 
58a40 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
58a50 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
58a60 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
58a70 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
58a80 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
58a90 20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20   SQLITE_TEXT).  
58aa0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
58ab0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
58ac0 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
58ad0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 74  n;.      }.    t
58ae0 65 78 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ext = (const cha
58af0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
58b00 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d  ue_text (argv[0]
58b10 29 3b 0a 20 20 20 20 69 66 20 28 28 73 74 72 6c  );.    if ((strl
58b20 65 6e 20 28 74 65 78 74 29 20 3c 20 35 29 20 7c  en (text) < 5) |
58b30 7c 20 28 73 74 72 6c 65 6e 20 28 74 65 78 74 29  | (strlen (text)
58b40 20 3e 20 37 29 29 0a 20 20 20 20 20 20 7b 0a 09   > 7)).      {..
58b50 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
58b60 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
58b70 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
58b80 20 20 7d 0a 20 20 20 20 69 66 20 28 73 74 72 6c    }.    if (strl
58b90 65 6e 20 28 74 65 78 74 29 20 3d 3d 20 35 29 0a  en (text) == 5).
58ba0 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 20 6e        {..  int n
58bb0 75 6d 4d 61 74 63 68 20 3d 20 30 3b 0a 09 20 20  umMatch = 0;..  
58bc0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 64 69 67  unsigned int dig
58bd0 69 74 31 30 30 20 3d 20 30 3b 0a 09 20 20 63 68  it100 = 0;..  ch
58be0 61 72 20 6c 65 74 74 65 72 4d 53 44 20 3d 20 27  ar letterMSD = '
58bf0 5c 30 27 3b 0a 09 20 20 63 68 61 72 20 6c 65 74  \0';..  char let
58c00 74 65 72 4c 53 44 20 3d 20 27 5c 30 27 3b 0a 09  terLSD = '\0';..
58c10 20 20 6e 75 6d 4d 61 74 63 68 20 3d 20 73 73 63    numMatch = ssc
58c20 61 6e 66 20 28 74 65 78 74 2c 20 22 25 75 25 63  anf (text, "%u%c
58c30 25 63 22 2c 20 26 64 69 67 69 74 31 30 30 2c 20  %c", &digit100, 
58c40 26 6c 65 74 74 65 72 4d 53 44 2c 20 26 6c 65 74  &letterMSD, &let
58c50 74 65 72 4c 53 44 29 3b 0a 09 20 20 69 66 20 28  terLSD);..  if (
58c60 6e 75 6d 4d 61 74 63 68 20 21 3d 20 33 29 0a 09  numMatch != 3)..
58c70 20 20 20 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f      {...sqlite3_
58c80 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
58c90 74 65 78 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b  text);...return;
58ca0 0a 09 20 20 20 20 7d 0a 09 20 20 78 31 20 3d 20  ..    }..  x1 = 
58cb0 28 28 64 69 67 69 74 31 30 30 20 2d 20 31 29 20  ((digit100 - 1) 
58cc0 2a 20 30 2e 35 29 20 2d 20 31 38 30 2e 30 3b 0a  * 0.5) - 180.0;.
58cd0 09 20 20 79 31 20 3d 20 67 61 72 73 4c 65 74 74  .  y1 = garsLett
58ce0 65 72 54 6f 44 65 67 72 65 65 73 4c 61 74 20 28  erToDegreesLat (
58cf0 6c 65 74 74 65 72 4d 53 44 2c 20 6c 65 74 74 65  letterMSD, lette
58d00 72 4c 53 44 29 3b 0a 09 20 20 69 66 20 28 28 78  rLSD);..  if ((x
58d10 31 20 3c 20 2d 31 38 30 2e 30 29 20 7c 7c 20 28  1 < -180.0) || (
58d20 78 31 20 3e 20 31 37 39 2e 35 29 20 7c 7c 20 28  x1 > 179.5) || (
58d30 79 31 20 3c 20 2d 39 30 2e 30 29 20 7c 7c 20 28  y1 < -90.0) || (
58d40 79 31 20 3e 20 38 39 2e 35 29 29 0a 09 20 20 20  y1 > 89.5))..   
58d50 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73   {...sqlite3_res
58d60 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
58d70 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20  t);...return;.. 
58d80 20 20 20 7d 0a 09 20 20 78 32 20 3d 20 78 31 20     }..  x2 = x1 
58d90 2b 20 30 2e 35 3b 0a 09 20 20 79 32 20 3d 20 79  + 0.5;..  y2 = y
58da0 31 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20 20 7d  1 + 0.5;.      }
58db0 0a 20 20 20 20 69 66 20 28 73 74 72 6c 65 6e 20  .    if (strlen 
58dc0 28 74 65 78 74 29 20 3d 3d 20 36 29 0a 20 20 20  (text) == 6).   
58dd0 20 20 20 7b 0a 09 20 20 75 6e 73 69 67 6e 65 64     {..  unsigned
58de0 20 69 6e 74 20 6e 75 6d 4d 61 74 63 68 20 3d 20   int numMatch = 
58df0 30 3b 0a 09 20 20 75 6e 73 69 67 6e 65 64 20 69  0;..  unsigned i
58e00 6e 74 20 64 69 67 69 74 31 30 30 20 3d 20 30 3b  nt digit100 = 0;
58e10 0a 09 20 20 63 68 61 72 20 6c 65 74 74 65 72 4d  ..  char letterM
58e20 53 44 20 3d 20 27 5c 30 27 3b 0a 09 20 20 63 68  SD = '\0';..  ch
58e30 61 72 20 6c 65 74 74 65 72 4c 53 44 20 3d 20 27  ar letterLSD = '
58e40 5c 30 27 3b 0a 09 20 20 75 6e 73 69 67 6e 65 64  \0';..  unsigned
58e50 20 69 6e 74 20 64 69 67 69 74 53 65 67 6d 65 6e   int digitSegmen
58e60 74 20 3d 20 30 3b 0a 09 20 20 6e 75 6d 4d 61 74  t = 0;..  numMat
58e70 63 68 20 3d 0a 09 20 20 20 20 20 20 73 73 63 61  ch =..      ssca
58e80 6e 66 20 28 74 65 78 74 2c 20 22 25 75 25 63 25  nf (text, "%u%c%
58e90 63 25 75 22 2c 20 26 64 69 67 69 74 31 30 30 2c  c%u", &digit100,
58ea0 20 26 6c 65 74 74 65 72 4d 53 44 2c 20 26 6c 65   &letterMSD, &le
58eb0 74 74 65 72 4c 53 44 2c 0a 09 09 20 20 20 20 20  tterLSD,...     
58ec0 20 26 64 69 67 69 74 53 65 67 6d 65 6e 74 29 3b   &digitSegment);
58ed0 0a 09 20 20 69 66 20 28 6e 75 6d 4d 61 74 63 68  ..  if (numMatch
58ee0 20 21 3d 20 34 29 0a 09 20 20 20 20 7b 0a 09 09   != 4)..    {...
58ef0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
58f00 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
58f10 09 72 65 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a  .return;..    }.
58f20 09 20 20 69 66 20 28 28 64 69 67 69 74 53 65 67  .  if ((digitSeg
58f30 6d 65 6e 74 20 3c 20 31 29 20 7c 7c 20 28 64 69  ment < 1) || (di
58f40 67 69 74 53 65 67 6d 65 6e 74 20 3e 20 34 29 29  gitSegment > 4))
58f50 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c 69 74 65  ..    {...sqlite
58f60 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
58f70 6f 6e 74 65 78 74 29 3b 0a 09 09 72 65 74 75 72  ontext);...retur
58f80 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20 78 31 20  n;..    }..  x1 
58f90 3d 20 28 28 64 69 67 69 74 31 30 30 20 2d 20 31  = ((digit100 - 1
58fa0 29 20 2a 20 30 2e 35 29 20 2d 20 31 38 30 2e 30  ) * 0.5) - 180.0
58fb0 3b 0a 09 20 20 69 66 20 28 28 64 69 67 69 74 53  ;..  if ((digitS
58fc0 65 67 6d 65 6e 74 20 3d 3d 20 32 29 20 7c 7c 20  egment == 2) || 
58fd0 28 64 69 67 69 74 53 65 67 6d 65 6e 74 20 3d 3d  (digitSegment ==
58fe0 20 34 29 29 0a 09 20 20 20 20 7b 0a 09 09 78 31   4))..    {...x1
58ff0 20 2b 3d 20 30 2e 32 35 3b 0a 09 20 20 20 20 7d   += 0.25;..    }
59000 0a 09 20 20 79 31 20 3d 20 67 61 72 73 4c 65 74  ..  y1 = garsLet
59010 74 65 72 54 6f 44 65 67 72 65 65 73 4c 61 74 20  terToDegreesLat 
59020 28 6c 65 74 74 65 72 4d 53 44 2c 20 6c 65 74 74  (letterMSD, lett
59030 65 72 4c 53 44 29 3b 0a 09 20 20 69 66 20 28 28  erLSD);..  if ((
59040 64 69 67 69 74 53 65 67 6d 65 6e 74 20 3d 3d 20  digitSegment == 
59050 31 29 20 7c 7c 20 28 64 69 67 69 74 53 65 67 6d  1) || (digitSegm
59060 65 6e 74 20 3d 3d 20 32 29 29 0a 09 20 20 20 20  ent == 2))..    
59070 7b 0a 09 09 79 31 20 2b 3d 20 30 2e 32 35 3b 0a  {...y1 += 0.25;.
59080 09 20 20 20 20 7d 0a 09 20 20 69 66 20 28 28 78  .    }..  if ((x
59090 31 20 3c 20 2d 31 38 30 2e 30 29 20 7c 7c 20 28  1 < -180.0) || (
590a0 78 31 20 3e 20 31 37 39 2e 37 35 29 20 7c 7c 20  x1 > 179.75) || 
590b0 28 79 31 20 3c 20 2d 39 30 2e 30 29 20 7c 7c 20  (y1 < -90.0) || 
590c0 28 79 31 20 3e 20 38 39 2e 37 35 29 29 0a 09 20  (y1 > 89.75)).. 
590d0 20 20 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72     {...sqlite3_r
590e0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
590f0 65 78 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a  ext);...return;.
59100 09 20 20 20 20 7d 0a 09 20 20 78 32 20 3d 20 78  .    }..  x2 = x
59110 31 20 2b 20 30 2e 32 35 3b 0a 09 20 20 79 32 20  1 + 0.25;..  y2 
59120 3d 20 79 31 20 2b 20 30 2e 32 35 3b 0a 20 20 20  = y1 + 0.25;.   
59130 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 74 72     }.    if (str
59140 6c 65 6e 20 28 74 65 78 74 29 20 3d 3d 20 37 29  len (text) == 7)
59150 0a 20 20 20 20 20 20 7b 0a 09 20 20 75 6e 73 69  .      {..  unsi
59160 67 6e 65 64 20 69 6e 74 20 6e 75 6d 4d 61 74 63  gned int numMatc
59170 68 20 3d 20 30 3b 0a 09 20 20 75 6e 73 69 67 6e  h = 0;..  unsign
59180 65 64 20 69 6e 74 20 64 69 67 69 74 31 30 30 20  ed int digit100 
59190 3d 20 30 3b 0a 09 20 20 63 68 61 72 20 6c 65 74  = 0;..  char let
591a0 74 65 72 4d 53 44 20 3d 20 27 5c 30 27 3b 0a 09  terMSD = '\0';..
591b0 20 20 63 68 61 72 20 6c 65 74 74 65 72 4c 53 44    char letterLSD
591c0 20 3d 20 27 5c 30 27 3b 0a 09 20 20 75 6e 73 69   = '\0';..  unsi
591d0 67 6e 65 64 20 69 6e 74 20 64 69 67 69 74 41 6e  gned int digitAn
591e0 64 4b 65 79 70 61 64 20 3d 20 30 3b 0a 09 20 20  dKeypad = 0;..  
591f0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 64 69 67  unsigned int dig
59200 69 74 53 65 67 6d 65 6e 74 20 3d 20 30 3b 0a 09  itSegment = 0;..
59210 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6b    unsigned int k
59220 65 79 70 61 64 4e 75 6d 62 65 72 20 3d 20 30 3b  eypadNumber = 0;
59230 0a 09 20 20 6e 75 6d 4d 61 74 63 68 20 3d 0a 09  ..  numMatch =..
59240 20 20 20 20 20 20 73 73 63 61 6e 66 20 28 74 65        sscanf (te
59250 78 74 2c 20 22 25 75 25 63 25 63 25 75 22 2c 20  xt, "%u%c%c%u", 
59260 26 64 69 67 69 74 31 30 30 2c 20 26 6c 65 74 74  &digit100, &lett
59270 65 72 4d 53 44 2c 20 26 6c 65 74 74 65 72 4c 53  erMSD, &letterLS
59280 44 2c 0a 09 09 20 20 20 20 20 20 26 64 69 67 69  D,...      &digi
59290 74 41 6e 64 4b 65 79 70 61 64 29 3b 0a 09 20 20  tAndKeypad);..  
592a0 69 66 20 28 6e 75 6d 4d 61 74 63 68 20 21 3d 20  if (numMatch != 
592b0 34 29 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c 69  4)..    {...sqli
592c0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
592d0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09 72 65 74  (context);...ret
592e0 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20 64  urn;..    }..  d
592f0 69 67 69 74 53 65 67 6d 65 6e 74 20 3d 20 64 69  igitSegment = di
59300 67 69 74 41 6e 64 4b 65 79 70 61 64 20 2f 20 31  gitAndKeypad / 1
59310 30 3b 0a 09 20 20 6b 65 79 70 61 64 4e 75 6d 62  0;..  keypadNumb
59320 65 72 20 3d 20 64 69 67 69 74 41 6e 64 4b 65 79  er = digitAndKey
59330 70 61 64 20 25 20 31 30 3b 0a 09 20 20 69 66 20  pad % 10;..  if 
59340 28 28 64 69 67 69 74 53 65 67 6d 65 6e 74 20 3c  ((digitSegment <
59350 20 31 29 20 7c 7c 20 28 64 69 67 69 74 53 65 67   1) || (digitSeg
59360 6d 65 6e 74 20 3e 20 34 29 29 0a 09 20 20 20 20  ment > 4))..    
59370 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75  {...sqlite3_resu
59380 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
59390 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20 20  );...return;..  
593a0 20 20 7d 0a 09 20 20 69 66 20 28 6b 65 79 70 61    }..  if (keypa
593b0 64 4e 75 6d 62 65 72 20 3c 20 31 29 0a 09 20 20  dNumber < 1)..  
593c0 20 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65    {...sqlite3_re
593d0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
593e0 78 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09  xt);...return;..
593f0 20 20 20 20 7d 0a 09 20 20 78 31 20 3d 20 28 28      }..  x1 = ((
59400 64 69 67 69 74 31 30 30 20 2d 20 31 29 20 2a 20  digit100 - 1) * 
59410 30 2e 35 29 20 2d 20 31 38 30 2e 30 3b 0a 09 20  0.5) - 180.0;.. 
59420 20 69 66 20 28 28 64 69 67 69 74 53 65 67 6d 65   if ((digitSegme
59430 6e 74 20 3d 3d 20 32 29 20 7c 7c 20 28 64 69 67  nt == 2) || (dig
59440 69 74 53 65 67 6d 65 6e 74 20 3d 3d 20 34 29 29  itSegment == 4))
59450 0a 09 20 20 20 20 7b 0a 09 09 78 31 20 2b 3d 20  ..    {...x1 += 
59460 30 2e 32 35 3b 0a 09 20 20 20 20 7d 0a 09 20 20  0.25;..    }..  
59470 79 31 20 3d 20 67 61 72 73 4c 65 74 74 65 72 54  y1 = garsLetterT
59480 6f 44 65 67 72 65 65 73 4c 61 74 20 28 6c 65 74  oDegreesLat (let
59490 74 65 72 4d 53 44 2c 20 6c 65 74 74 65 72 4c 53  terMSD, letterLS
594a0 44 29 3b 0a 09 20 20 69 66 20 28 28 64 69 67 69  D);..  if ((digi
594b0 74 53 65 67 6d 65 6e 74 20 3d 3d 20 31 29 20 7c  tSegment == 1) |
594c0 7c 20 28 64 69 67 69 74 53 65 67 6d 65 6e 74 20  | (digitSegment 
594d0 3d 3d 20 32 29 29 0a 09 20 20 20 20 7b 0a 09 09  == 2))..    {...
594e0 79 31 20 2b 3d 20 30 2e 32 35 3b 0a 09 20 20 20  y1 += 0.25;..   
594f0 20 7d 0a 09 20 20 73 77 69 74 63 68 20 28 6b 65   }..  switch (ke
59500 79 70 61 64 4e 75 6d 62 65 72 29 0a 09 20 20 20  ypadNumber)..   
59510 20 7b 0a 09 20 20 20 20 63 61 73 65 20 31 3a 0a   {..    case 1:.
59520 09 09 78 31 20 2b 3d 20 30 20 2a 20 30 2e 32 35  ..x1 += 0 * 0.25
59530 20 2f 20 33 3b 0a 09 09 79 31 20 2b 3d 20 32 20   / 3;...y1 += 2 
59540 2a 20 30 2e 32 35 20 2f 20 33 3b 0a 09 09 62 72  * 0.25 / 3;...br
59550 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 32  eak;..    case 2
59560 3a 0a 09 09 78 31 20 2b 3d 20 31 20 2a 20 30 2e  :...x1 += 1 * 0.
59570 32 35 20 2f 20 33 3b 0a 09 09 79 31 20 2b 3d 20  25 / 3;...y1 += 
59580 32 20 2a 20 30 2e 32 35 20 2f 20 33 3b 0a 09 09  2 * 0.25 / 3;...
59590 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
595a0 20 33 3a 0a 09 09 78 31 20 2b 3d 20 32 20 2a 20   3:...x1 += 2 * 
595b0 30 2e 32 35 20 2f 20 33 3b 0a 09 09 79 31 20 2b  0.25 / 3;...y1 +
595c0 3d 20 32 20 2a 20 30 2e 32 35 20 2f 20 33 3b 0a  = 2 * 0.25 / 3;.
595d0 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
595e0 73 65 20 34 3a 0a 09 09 78 31 20 2b 3d 20 30 20  se 4:...x1 += 0 
595f0 2a 20 30 2e 32 35 20 2f 20 33 3b 0a 09 09 79 31  * 0.25 / 3;...y1
59600 20 2b 3d 20 31 20 2a 20 30 2e 32 35 20 2f 20 33   += 1 * 0.25 / 3
59610 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
59620 63 61 73 65 20 35 3a 0a 09 09 78 31 20 2b 3d 20  case 5:...x1 += 
59630 31 20 2a 20 30 2e 32 35 20 2f 20 33 3b 0a 09 09  1 * 0.25 / 3;...
59640 79 31 20 2b 3d 20 31 20 2a 20 30 2e 32 35 20 2f  y1 += 1 * 0.25 /
59650 20 33 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20   3;...break;..  
59660 20 20 63 61 73 65 20 36 3a 0a 09 09 78 31 20 2b    case 6:...x1 +
59670 3d 20 32 20 2a 20 30 2e 32 35 20 2f 20 33 3b 0a  = 2 * 0.25 / 3;.
59680 09 09 79 31 20 2b 3d 20 31 20 2a 20 30 2e 32 35  ..y1 += 1 * 0.25
59690 20 2f 20 33 3b 0a 09 09 62 72 65 61 6b 3b 0a 09   / 3;...break;..
596a0 20 20 20 20 63 61 73 65 20 37 3a 0a 09 09 78 31      case 7:...x1
596b0 20 2b 3d 20 30 20 2a 20 30 2e 32 35 20 2f 20 33   += 0 * 0.25 / 3
596c0 3b 0a 09 09 79 31 20 2b 3d 20 30 20 2a 20 30 2e  ;...y1 += 0 * 0.
596d0 32 35 20 2f 20 33 3b 0a 09 09 62 72 65 61 6b 3b  25 / 3;...break;
596e0 0a 09 20 20 20 20 63 61 73 65 20 38 3a 0a 09 09  ..    case 8:...
596f0 78 31 20 2b 3d 20 31 20 2a 20 30 2e 32 35 20 2f  x1 += 1 * 0.25 /
59700 20 33 3b 0a 09 09 79 31 20 2b 3d 20 30 20 2a 20   3;...y1 += 0 * 
59710 30 2e 32 35 20 2f 20 33 3b 0a 09 09 62 72 65 61  0.25 / 3;...brea
59720 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 39 3a 0a  k;..    case 9:.
59730 09 09 78 31 20 2b 3d 20 32 20 2a 20 30 2e 32 35  ..x1 += 2 * 0.25
59740 20 2f 20 33 3b 0a 09 09 79 31 20 2b 3d 20 30 20   / 3;...y1 += 0 
59750 2a 20 30 2e 32 35 20 2f 20 33 3b 0a 09 09 62 72  * 0.25 / 3;...br
59760 65 61 6b 3b 0a 09 20 20 20 20 7d 0a 09 20 20 69  eak;..    }..  i
59770 66 20 28 28 78 31 20 3c 20 2d 31 38 30 2e 30 29  f ((x1 < -180.0)
59780 20 7c 7c 20 28 78 31 20 3e 3d 20 31 38 30 2e 30   || (x1 >= 180.0
59790 29 20 7c 7c 20 28 79 31 20 3c 20 2d 39 30 2e 30  ) || (y1 < -90.0
597a0 29 20 7c 7c 20 28 79 31 20 3e 3d 20 39 30 2e 30  ) || (y1 >= 90.0
597b0 29 29 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c 69  ))..    {...sqli
597c0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
597d0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09 72 65 74  (context);...ret
597e0 75 72 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20 78  urn;..    }..  x
597f0 32 20 3d 20 78 31 20 2b 20 28 30 2e 32 35 20 2f  2 = x1 + (0.25 /
59800 20 33 29 3b 0a 09 20 20 79 32 20 3d 20 79 31 20   3);..  y2 = y1 
59810 2b 20 28 30 2e 32 35 20 2f 20 33 29 3b 0a 20 20  + (0.25 / 3);.  
59820 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 42 75      }.    gaiaBu
59830 69 6c 64 4d 62 72 20 28 78 31 2c 20 79 31 2c 20  ildMbr (x1, y1, 
59840 78 32 2c 20 79 32 2c 20 34 33 32 36 2c 20 26 70  x2, y2, 4326, &p
59850 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a  _result, &len);.
59860 20 20 20 20 69 66 20 28 21 70 5f 72 65 73 75 6c      if (!p_resul
59870 74 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  t).      {..  sq
59880 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
59890 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
598a0 70 72 69 6e 74 66 20 28 22 62 61 64 20 70 5f 72  printf ("bad p_r
598b0 65 73 75 6c 74 5c 6e 22 29 3b 0a 20 20 20 20 20  esult\n");.     
598c0 20 7d 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c   }.    else..sql
598d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
598e0 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73   (context, p_res
598f0 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b  ult, len, free);
59900 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
59910 66 6e 63 74 5f 54 6f 47 41 52 53 20 28 73 71 6c  fnct_ToGARS (sql
59920 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
59930 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
59940 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
59950 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
59960 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 54 6f  L function:./ To
59970 47 41 52 53 28 42 4c 4f 42 20 65 6e 63 6f 64 65  GARS(BLOB encode
59980 64 20 50 4f 49 4e 54 29 0a 2f 0a 2f 20 72 65 74  d POINT)././ ret
59990 75 72 6e 73 20 74 68 65 20 47 6c 6f 62 61 6c 20  urns the Global 
599a0 41 72 65 61 20 52 65 66 65 72 65 6e 63 65 20 53  Area Reference S
599b0 79 73 74 65 6d 20 63 6f 6f 72 64 69 6e 61 74 65  ystem coordinate
599c0 20 61 72 65 61 20 66 6f 72 20 61 20 67 69 76 65   area for a give
599d0 6e 20 70 6f 69 6e 74 2c 0a 2f 20 6f 72 20 4e 55  n point,./ or NU
599e0 4c 4c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  LL if an error o
599f0 63 63 75 72 73 0a 2a 2f 0a 20 20 20 20 75 6e 73  ccurs.*/.    uns
59a00 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
59a10 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  ob;.    int n_by
59a20 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 70 74 73  tes;.    int pts
59a30 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6c 6e   = 0;.    int ln
59a40 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 70  s = 0;.    int p
59a50 67 73 20 3d 20 30 3b 0a 20 20 20 20 67 61 69 61  gs = 0;.    gaia
59a60 50 6f 69 6e 74 50 74 72 20 70 6f 69 6e 74 3b 0a  PointPtr point;.
59a70 20 20 20 20 67 61 69 61 4c 69 6e 65 73 74 72 69      gaiaLinestri
59a80 6e 67 50 74 72 20 6c 69 6e 65 3b 0a 20 20 20 20  ngPtr line;.    
59a90 67 61 69 61 50 6f 6c 79 67 6f 6e 50 74 72 20 70  gaiaPolygonPtr p
59aa0 6f 6c 79 67 3b 0a 20 20 20 20 67 61 69 61 47 65  olyg;.    gaiaGe
59ab0 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
59ac0 4e 55 4c 4c 3b 0a 20 20 20 20 63 68 61 72 20 70  NULL;.    char p
59ad0 5f 72 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 20 20  _result[8];.    
59ae0 69 6e 74 20 6c 6f 6e 5f 62 61 6e 64 20 3d 20 30  int lon_band = 0
59af0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6c 6f 6e  ;.    double lon
59b00 5f 6d 69 6e 75 74 65 73 20 3d 20 30 3b 0a 20 20  _minutes = 0;.  
59b10 20 20 69 6e 74 20 73 65 67 6d 65 6e 74 4e 75 6d    int segmentNum
59b20 62 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  ber = 0;.    int
59b30 20 6c 61 74 5f 62 61 6e 64 20 3d 20 30 3b 0a 20   lat_band = 0;. 
59b40 20 20 20 64 6f 75 62 6c 65 20 6c 61 74 5f 6d 69     double lat_mi
59b50 6e 75 74 65 73 20 3d 20 30 3b 0a 20 20 20 20 47  nutes = 0;.    G
59b60 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
59b70 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
59b80 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
59b90 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
59ba0 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
59bb0 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
59bc0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
59bd0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
59be0 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
59bf0 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f       }.    p_blo
59c00 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
59c10 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
59c20 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30  lue_blob (argv[0
59c30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20  ]);.    n_bytes 
59c40 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
59c50 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b  bytes (argv[0]);
59c60 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46  .    geo = gaiaF
59c70 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  romSpatiaLiteBlo
59c80 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f  bWkb (p_blob, n_
59c90 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28  bytes);.    if (
59ca0 21 67 65 6f 29 0a 20 20 20 20 20 20 7b 0a 09 20  !geo).      {.. 
59cb0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
59cc0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
59cd0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
59ce0 20 7d 0a 20 20 20 20 67 61 69 61 4e 6f 72 6d 61   }.    gaiaNorma
59cf0 6c 69 7a 65 4c 6f 6e 4c 61 74 20 28 67 65 6f 29  lizeLonLat (geo)
59d00 3b 0a 20 20 20 20 70 6f 69 6e 74 20 3d 20 67 65  ;.    point = ge
59d10 6f 2d 3e 46 69 72 73 74 50 6f 69 6e 74 3b 0a 20  o->FirstPoint;. 
59d20 20 20 20 77 68 69 6c 65 20 28 70 6f 69 6e 74 20     while (point 
59d30 21 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b  != NULL).      {
59d40 0a 09 20 20 70 74 73 2b 2b 3b 0a 09 20 20 70 6f  ..  pts++;..  po
59d50 69 6e 74 20 3d 20 70 6f 69 6e 74 2d 3e 4e 65 78  int = point->Nex
59d60 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 6c  t;.      }.    l
59d70 69 6e 65 20 3d 20 67 65 6f 2d 3e 46 69 72 73 74  ine = geo->First
59d80 4c 69 6e 65 73 74 72 69 6e 67 3b 0a 20 20 20 20  Linestring;.    
59d90 77 68 69 6c 65 20 28 6c 69 6e 65 20 21 3d 20 4e  while (line != N
59da0 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  ULL).      {..  
59db0 6c 6e 73 2b 2b 3b 0a 09 20 20 6c 69 6e 65 20 3d  lns++;..  line =
59dc0 20 6c 69 6e 65 2d 3e 4e 65 78 74 3b 0a 20 20 20   line->Next;.   
59dd0 20 20 20 7d 0a 20 20 20 20 70 6f 6c 79 67 20 3d     }.    polyg =
59de0 20 67 65 6f 2d 3e 46 69 72 73 74 50 6f 6c 79 67   geo->FirstPolyg
59df0 6f 6e 3b 0a 20 20 20 20 77 68 69 6c 65 20 28 70  on;.    while (p
59e00 6f 6c 79 67 20 21 3d 20 4e 55 4c 4c 29 0a 20 20  olyg != NULL).  
59e10 20 20 20 20 7b 0a 09 20 20 70 67 73 2b 2b 3b 0a      {..  pgs++;.
59e20 09 20 20 70 6f 6c 79 67 20 3d 20 70 6f 6c 79 67  .  polyg = polyg
59e30 2d 3e 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  ->Next;.      }.
59e40 20 20 20 20 69 66 20 28 70 74 73 20 3d 3d 20 31      if (pts == 1
59e50 20 26 26 20 6c 6e 73 20 3d 3d 20 30 20 26 26 20   && lns == 0 && 
59e60 70 67 73 20 3d 3d 20 30 29 0a 09 70 6f 69 6e 74  pgs == 0)..point
59e70 20 3d 20 67 65 6f 2d 3e 46 69 72 73 74 50 6f 69   = geo->FirstPoi
59e80 6e 74 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  nt;.    else.   
59e90 20 20 20 7b 0a 09 20 20 2f 2a 20 6e 6f 74 20 61     {..  /* not a
59ea0 20 73 69 6e 67 6c 65 20 50 6f 69 6e 74 20 2a 2f   single Point */
59eb0 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d  ..  gaiaFreeGeom
59ec0 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09 20 20 73  Coll (geo);..  s
59ed0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
59ee0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
59ef0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
59f00 0a 20 20 20 20 2f 2a 20 6c 6f 6e 67 69 74 75 64  .    /* longitud
59f10 65 20 62 61 6e 64 20 2a 2f 0a 20 20 20 20 6c 6f  e band */.    lo
59f20 6e 5f 62 61 6e 64 20 3d 20 31 20 2b 20 28 69 6e  n_band = 1 + (in
59f30 74 29 20 28 28 70 6f 69 6e 74 2d 3e 58 20 2b 20  t) ((point->X + 
59f40 31 38 30 2e 30 29 20 2a 20 32 29 3b 0a 20 20 20  180.0) * 2);.   
59f50 20 73 70 72 69 6e 74 66 20 28 70 5f 72 65 73 75   sprintf (p_resu
59f60 6c 74 2c 20 22 25 30 33 69 22 2c 20 6c 6f 6e 5f  lt, "%03i", lon_
59f70 62 61 6e 64 29 3b 0a 20 20 20 20 2f 2a 20 6c 61  band);.    /* la
59f80 74 69 74 75 64 65 20 62 61 6e 64 20 2a 2f 0a 20  titude band */. 
59f90 20 20 20 6c 61 74 5f 62 61 6e 64 20 3d 20 28 69     lat_band = (i
59fa0 6e 74 29 20 28 28 70 6f 69 6e 74 2d 3e 59 20 2b  nt) ((point->Y +
59fb0 20 39 30 2e 30 29 20 2a 20 32 29 3b 0a 20 20 20   90.0) * 2);.   
59fc0 20 70 5f 72 65 73 75 6c 74 5b 33 5d 20 3d 20 67   p_result[3] = g
59fd0 61 72 73 4c 65 74 74 65 72 43 6f 64 65 20 28 6c  arsLetterCode (l
59fe0 61 74 5f 62 61 6e 64 20 2f 20 32 34 29 3b 0a 20  at_band / 24);. 
59ff0 20 20 20 70 5f 72 65 73 75 6c 74 5b 34 5d 20 3d     p_result[4] =
5a000 20 67 61 72 73 4c 65 74 74 65 72 43 6f 64 65 20   garsLetterCode 
5a010 28 6c 61 74 5f 62 61 6e 64 20 25 20 32 34 29 3b  (lat_band % 24);
5a020 0a 20 20 20 20 2f 2a 20 71 75 61 64 72 61 6e 74  .    /* quadrant
5a030 20 2a 2f 0a 20 20 20 20 6c 6f 6e 5f 6d 69 6e 75   */.    lon_minu
5a040 74 65 73 20 3d 20 66 6d 6f 64 20 28 28 70 6f 69  tes = fmod ((poi
5a050 6e 74 2d 3e 58 20 2b 20 31 38 30 2e 30 29 2c 20  nt->X + 180.0), 
5a060 30 2e 35 29 20 2a 20 36 30 2e 30 3b 0a 20 20 20  0.5) * 60.0;.   
5a070 20 69 66 20 28 6c 6f 6e 5f 6d 69 6e 75 74 65 73   if (lon_minutes
5a080 20 3c 20 31 35 2e 30 29 0a 20 20 20 20 20 20 7b   < 15.0).      {
5a090 0a 09 20 20 73 65 67 6d 65 6e 74 4e 75 6d 62 65  ..  segmentNumbe
5a0a0 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  r = 1;.      }. 
5a0b0 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
5a0c0 09 20 20 73 65 67 6d 65 6e 74 4e 75 6d 62 65 72  .  segmentNumber
5a0d0 20 3d 20 32 3b 0a 09 20 20 6c 6f 6e 5f 6d 69 6e   = 2;..  lon_min
5a0e0 75 74 65 73 20 2d 3d 20 31 35 2e 30 3b 0a 20 20  utes -= 15.0;.  
5a0f0 20 20 20 20 7d 0a 20 20 20 20 6c 61 74 5f 6d 69      }.    lat_mi
5a100 6e 75 74 65 73 20 3d 20 66 6d 6f 64 20 28 28 70  nutes = fmod ((p
5a110 6f 69 6e 74 2d 3e 59 20 2b 20 39 30 2e 30 29 2c  oint->Y + 90.0),
5a120 20 30 2e 35 29 20 2a 20 36 30 2e 30 3b 0a 20 20   0.5) * 60.0;.  
5a130 20 20 69 66 20 28 6c 61 74 5f 6d 69 6e 75 74 65    if (lat_minute
5a140 73 20 3c 20 31 35 2e 30 29 0a 20 20 20 20 20 20  s < 15.0).      
5a150 7b 0a 09 20 20 73 65 67 6d 65 6e 74 4e 75 6d 62  {..  segmentNumb
5a160 65 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d  er += 2;.      }
5a170 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
5a180 7b 0a 09 20 20 2f 2a 20 77 65 20 61 6c 72 65 61  {..  /* we alrea
5a190 64 79 20 68 61 76 65 20 74 68 65 20 72 69 67 68  dy have the righ
5a1a0 74 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 09 20 20  t segment */..  
5a1b0 6c 61 74 5f 6d 69 6e 75 74 65 73 20 2d 3d 20 31  lat_minutes -= 1
5a1c0 35 2e 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  5.0;.      }.   
5a1d0 20 73 70 72 69 6e 74 66 20 28 26 28 70 5f 72 65   sprintf (&(p_re
5a1e0 73 75 6c 74 5b 35 5d 29 2c 20 22 25 69 22 2c 20  sult[5]), "%i", 
5a1f0 73 65 67 6d 65 6e 74 4e 75 6d 62 65 72 29 3b 0a  segmentNumber);.
5a200 20 20 20 20 2f 2a 20 61 72 65 61 20 2a 2f 0a 20      /* area */. 
5a210 20 20 20 73 65 67 6d 65 6e 74 4e 75 6d 62 65 72     segmentNumber
5a220 20 3d 20 30 3b 0a 20 20 20 20 69 66 20 28 6c 6f   = 0;.    if (lo
5a230 6e 5f 6d 69 6e 75 74 65 73 20 3e 3d 20 31 30 2e  n_minutes >= 10.
5a240 30 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 65  0).      {..  se
5a250 67 6d 65 6e 74 4e 75 6d 62 65 72 20 3d 20 33 3b  gmentNumber = 3;
5a260 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
5a270 65 20 69 66 20 28 6c 6f 6e 5f 6d 69 6e 75 74 65  e if (lon_minute
5a280 73 20 3e 3d 20 35 2e 30 29 0a 20 20 20 20 20 20  s >= 5.0).      
5a290 7b 0a 09 20 20 73 65 67 6d 65 6e 74 4e 75 6d 62  {..  segmentNumb
5a2a0 65 72 20 3d 20 32 3b 0a 20 20 20 20 20 20 7d 0a  er = 2;.      }.
5a2b0 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
5a2c0 0a 09 20 20 73 65 67 6d 65 6e 74 4e 75 6d 62 65  ..  segmentNumbe
5a2d0 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  r = 1;.      }. 
5a2e0 20 20 20 69 66 20 28 6c 61 74 5f 6d 69 6e 75 74     if (lat_minut
5a2f0 65 73 20 3e 3d 20 31 30 2e 30 29 0a 20 20 20 20  es >= 10.0).    
5a300 20 20 7b 0a 09 20 20 2f 2a 20 6e 6f 74 68 69 6e    {..  /* nothin
5a310 67 20 74 6f 20 61 64 64 20 2a 2f 0a 20 20 20 20  g to add */.    
5a320 20 20 7d 0a 20 20 20 20 65 6c 73 65 20 69 66 20    }.    else if 
5a330 28 6c 61 74 5f 6d 69 6e 75 74 65 73 20 3e 3d 20  (lat_minutes >= 
5a340 35 2e 30 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  5.0).      {..  
5a350 73 65 67 6d 65 6e 74 4e 75 6d 62 65 72 20 2b 3d  segmentNumber +=
5a360 20 33 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   3;.      }.    
5a370 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
5a380 73 65 67 6d 65 6e 74 4e 75 6d 62 65 72 20 2b 3d  segmentNumber +=
5a390 20 36 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   6;.      }.    
5a3a0 73 70 72 69 6e 74 66 20 28 26 28 70 5f 72 65 73  sprintf (&(p_res
5a3b0 75 6c 74 5b 36 5d 29 2c 20 22 25 69 22 2c 20 73  ult[6]), "%i", s
5a3c0 65 67 6d 65 6e 74 4e 75 6d 62 65 72 29 3b 0a 20  egmentNumber);. 
5a3d0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
5a3e0 74 5f 74 65 78 74 20 28 63 6f 6e 74 65 78 74 2c  t_text (context,
5a3f0 20 70 5f 72 65 73 75 6c 74 2c 20 37 2c 20 53 51   p_result, 7, SQ
5a400 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
5a410 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f  .    gaiaFreeGeo
5a420 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a  mColl (geo);.}..
5a430 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
5a440 5f 47 65 6f 6d 65 74 72 79 4e 20 28 73 71 6c 69  _GeometryN (sqli
5a450 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
5a460 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
5a470 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
5a480 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
5a490 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 47 65 6f   function:./ Geo
5a4a0 6d 65 74 72 79 4e 28 42 4c 4f 42 20 65 6e 63 6f  metryN(BLOB enco
5a4b0 64 65 64 20 47 45 4f 4d 45 54 52 59 43 4f 4c 4c  ded GEOMETRYCOLL
5a4c0 45 43 54 49 4f 4e 20 67 65 6f 6d 65 74 72 79 29  ECTION geometry)
5a4d0 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65  ././ returns the
5a4e0 20 4e 74 68 20 67 65 6f 6d 65 74 72 79 20 66 6f   Nth geometry fo
5a4f0 72 20 63 75 72 72 65 6e 74 20 47 45 4f 4d 45 54  r current GEOMET
5a500 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 20 6f 72 20  RYCOLLECTION or 
5a510 4d 55 4c 54 49 78 78 78 78 20 67 65 6f 6d 65 74  MULTIxxxx geomet
5a520 72 79 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  ry ./ or NULL if
5a530 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
5a540 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
5a550 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
5a560 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20  p_blob;.    int 
5a570 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74  n_bytes;.    int
5a580 20 65 6e 74 69 74 79 3b 0a 20 20 20 20 69 6e 74   entity;.    int
5a590 20 6c 65 6e 3b 0a 20 20 20 20 69 6e 74 20 63 6e   len;.    int cn
5a5a0 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 69  t = 0;.    int i
5a5b0 76 3b 0a 20 20 20 20 69 6e 74 20 69 62 3b 0a 20  v;.    int ib;. 
5a5c0 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20     double x;.   
5a5d0 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20 20 64   double y;.    d
5a5e0 6f 75 62 6c 65 20 7a 3b 0a 20 20 20 20 64 6f 75  ouble z;.    dou
5a5f0 62 6c 65 20 6d 3b 0a 20 20 20 20 67 61 69 61 50  ble m;.    gaiaP
5a600 6f 69 6e 74 50 74 72 20 70 6f 69 6e 74 3b 0a 20  ointPtr point;. 
5a610 20 20 20 67 61 69 61 4c 69 6e 65 73 74 72 69 6e     gaiaLinestrin
5a620 67 50 74 72 20 6c 69 6e 65 3b 0a 20 20 20 20 67  gPtr line;.    g
5a630 61 69 61 4c 69 6e 65 73 74 72 69 6e 67 50 74 72  aiaLinestringPtr
5a640 20 6c 69 6e 65 32 3b 0a 20 20 20 20 67 61 69 61   line2;.    gaia
5a650 50 6f 6c 79 67 6f 6e 50 74 72 20 70 6f 6c 79 67  PolygonPtr polyg
5a660 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79 67 6f  ;.    gaiaPolygo
5a670 6e 50 74 72 20 70 6f 6c 79 67 32 3b 0a 20 20 20  nPtr polyg2;.   
5a680 20 67 61 69 61 52 69 6e 67 50 74 72 20 72 69 6e   gaiaRingPtr rin
5a690 67 5f 69 6e 3b 0a 20 20 20 20 67 61 69 61 52 69  g_in;.    gaiaRi
5a6a0 6e 67 50 74 72 20 72 69 6e 67 5f 6f 75 74 3b 0a  ngPtr ring_out;.
5a6b0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
5a6c0 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55  r *p_result = NU
5a6d0 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
5a6e0 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
5a6f0 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
5a700 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 20 3d  CollPtr result =
5a710 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f   NULL;.    GAIA_
5a720 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
5a730 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
5a740 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
5a750 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
5a760 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
5a770 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
5a780 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
5a790 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
5a7a0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
5a7b0 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74   }.    if (sqlit
5a7c0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
5a7d0 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[1]) != SQLIT
5a7e0 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20  E_INTEGER).     
5a7f0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
5a800 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
5a810 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
5a820 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
5a830 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
5a840 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
5a850 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
5a860 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
5a870 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
5a880 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
5a890 3b 0a 20 20 20 20 65 6e 74 69 74 79 20 3d 20 73  ;.    entity = s
5a8a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
5a8b0 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20   (argv[1]);.    
5a8c0 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  geo = gaiaFromSp
5a8d0 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
5a8e0 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
5a8f0 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29  );.    if (!geo)
5a900 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
5a910 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
5a920 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
5a930 7b 0a 09 20 20 70 6f 69 6e 74 20 3d 20 67 65 6f  {..  point = geo
5a940 2d 3e 46 69 72 73 74 50 6f 69 6e 74 3b 0a 09 20  ->FirstPoint;.. 
5a950 20 77 68 69 6c 65 20 28 70 6f 69 6e 74 29 0a 09   while (point)..
5a960 20 20 20 20 7b 0a 09 09 2f 2a 20 63 6f 75 6e 74      {.../* count
5a970 73 20 68 6f 77 20 6d 61 6e 79 20 70 6f 69 6e 74  s how many point
5a980 73 20 61 72 65 20 74 68 65 72 65 20 2a 2f 0a 09  s are there */..
5a990 09 63 6e 74 2b 2b 3b 0a 09 09 69 66 20 28 63 6e  .cnt++;...if (cn
5a9a0 74 20 3d 3d 20 65 6e 74 69 74 79 29 0a 09 09 20  t == entity)... 
5a9b0 20 7b 0a 09 09 20 20 20 20 20 20 2f 2a 20 6f 6b   {...      /* ok
5a9c0 2c 20 72 65 71 75 69 72 65 64 20 65 6c 65 6d 65  , required eleme
5a9d0 6e 74 61 72 79 20 67 65 6f 6d 65 74 72 79 20 69  ntary geometry i
5a9e0 73 20 74 68 69 73 20 50 4f 49 4e 54 20 2a 2f 0a  s this POINT */.
5a9f0 09 09 20 20 20 20 20 20 69 66 20 28 70 6f 69 6e  ..      if (poin
5aa00 74 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65  t->DimensionMode
5aa10 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29 0a  l == GAIA_XY_Z).
5aa20 09 09 09 20 20 72 65 73 75 6c 74 20 3d 20 67 61  ...  result = ga
5aa30 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58  iaAllocGeomCollX
5aa40 59 5a 20 28 29 3b 0a 09 09 20 20 20 20 20 20 65  YZ ();...      e
5aa50 6c 73 65 20 69 66 20 28 70 6f 69 6e 74 2d 3e 44  lse if (point->D
5aa60 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d  imensionModel ==
5aa70 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 09 09 20   GAIA_XY_M).... 
5aa80 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41 6c   result = gaiaAl
5aa90 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58 59 4d 20 28  locGeomCollXYM (
5aaa0 29 3b 0a 09 09 20 20 20 20 20 20 65 6c 73 65 20  );...      else 
5aab0 69 66 20 28 70 6f 69 6e 74 2d 3e 44 69 6d 65 6e  if (point->Dimen
5aac0 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49  sionModel == GAI
5aad0 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 09 20 20 72  A_XY_Z_M)....  r
5aae0 65 73 75 6c 74 20 3d 20 67 61 69 61 41 6c 6c 6f  esult = gaiaAllo
5aaf0 63 47 65 6f 6d 43 6f 6c 6c 58 59 5a 4d 20 28 29  cGeomCollXYZM ()
5ab00 3b 0a 09 09 20 20 20 20 20 20 65 6c 73 65 0a 09  ;...      else..
5ab10 09 09 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69  ..  result = gai
5ab20 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 20 28  aAllocGeomColl (
5ab30 29 3b 0a 09 09 20 20 20 20 20 20 72 65 73 75 6c  );...      resul
5ab40 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53  t->Srid = geo->S
5ab50 72 69 64 3b 0a 09 09 20 20 20 20 20 20 69 66 20  rid;...      if 
5ab60 28 70 6f 69 6e 74 2d 3e 44 69 6d 65 6e 73 69 6f  (point->Dimensio
5ab70 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
5ab80 59 5f 5a 29 0a 09 09 09 20 20 67 61 69 61 41 64  Y_Z)....  gaiaAd
5ab90 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c  dPointToGeomColl
5aba0 58 59 5a 20 28 72 65 73 75 6c 74 2c 20 70 6f 69  XYZ (result, poi
5abb0 6e 74 2d 3e 58 2c 0a 09 09 09 09 09 09 20 20 20  nt->X,.......   
5abc0 20 20 70 6f 69 6e 74 2d 3e 59 2c 20 70 6f 69 6e    point->Y, poin
5abd0 74 2d 3e 5a 29 3b 0a 09 09 20 20 20 20 20 20 65  t->Z);...      e
5abe0 6c 73 65 20 69 66 20 28 70 6f 69 6e 74 2d 3e 44  lse if (point->D
5abf0 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d  imensionModel ==
5ac00 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 09 09 20   GAIA_XY_M).... 
5ac10 20 67 61 69 61 41 64 64 50 6f 69 6e 74 54 6f 47   gaiaAddPointToG
5ac20 65 6f 6d 43 6f 6c 6c 58 59 4d 20 28 72 65 73 75  eomCollXYM (resu
5ac30 6c 74 2c 20 70 6f 69 6e 74 2d 3e 58 2c 0a 09 09  lt, point->X,...
5ac40 09 09 09 09 20 20 20 20 20 70 6f 69 6e 74 2d 3e  ....     point->
5ac50 59 2c 20 70 6f 69 6e 74 2d 3e 4d 29 3b 0a 09 09  Y, point->M);...
5ac60 20 20 20 20 20 20 65 6c 73 65 20 69 66 20 28 70        else if (p
5ac70 6f 69 6e 74 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d  oint->DimensionM
5ac80 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f  odel == GAIA_XY_
5ac90 5a 5f 4d 29 0a 09 09 09 20 20 67 61 69 61 41 64  Z_M)....  gaiaAd
5aca0 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c  dPointToGeomColl
5acb0 58 59 5a 4d 20 28 72 65 73 75 6c 74 2c 20 70 6f  XYZM (result, po
5acc0 69 6e 74 2d 3e 58 2c 0a 09 09 09 09 09 09 20 20  int->X,.......  
5acd0 20 20 20 20 70 6f 69 6e 74 2d 3e 59 2c 20 70 6f      point->Y, po
5ace0 69 6e 74 2d 3e 5a 2c 0a 09 09 09 09 09 09 20 20  int->Z,.......  
5acf0 20 20 20 20 70 6f 69 6e 74 2d 3e 4d 29 3b 0a 09      point->M);..
5ad00 09 20 20 20 20 20 20 65 6c 73 65 0a 09 09 09 20  .      else.... 
5ad10 20 67 61 69 61 41 64 64 50 6f 69 6e 74 54 6f 47   gaiaAddPointToG
5ad20 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 2c  eomColl (result,
5ad30 20 70 6f 69 6e 74 2d 3e 58 2c 20 70 6f 69 6e 74   point->X, point
5ad40 2d 3e 59 29 3b 0a 09 09 20 20 20 20 20 20 67 6f  ->Y);...      go
5ad50 74 6f 20 73 6b 69 70 3b 0a 09 09 20 20 7d 0a 09  to skip;...  }..
5ad60 09 70 6f 69 6e 74 20 3d 20 70 6f 69 6e 74 2d 3e  .point = point->
5ad70 4e 65 78 74 3b 0a 09 20 20 20 20 7d 0a 09 20 20  Next;..    }..  
5ad80 6c 69 6e 65 20 3d 20 67 65 6f 2d 3e 46 69 72 73  line = geo->Firs
5ad90 74 4c 69 6e 65 73 74 72 69 6e 67 3b 0a 09 20 20  tLinestring;..  
5ada0 77 68 69 6c 65 20 28 6c 69 6e 65 29 0a 09 20 20  while (line)..  
5adb0 20 20 7b 0a 09 09 2f 2a 20 63 6f 75 6e 74 73 20    {.../* counts 
5adc0 68 6f 77 20 6d 61 6e 79 20 6c 69 6e 65 73 74 72  how many linestr
5add0 69 6e 67 73 20 61 72 65 20 74 68 65 72 65 20 2a  ings are there *
5ade0 2f 0a 09 09 63 6e 74 2b 2b 3b 0a 09 09 69 66 20  /...cnt++;...if 
5adf0 28 63 6e 74 20 3d 3d 20 65 6e 74 69 74 79 29 0a  (cnt == entity).
5ae00 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 2f 2a  ..  {...      /*
5ae10 20 6f 6b 2c 20 72 65 71 75 69 72 65 64 20 65 6c   ok, required el
5ae20 65 6d 65 6e 74 61 72 79 20 67 65 6f 6d 65 74 72  ementary geometr
5ae30 79 20 69 73 20 74 68 69 73 20 4c 49 4e 45 53 54  y is this LINEST
5ae40 52 49 4e 47 20 2a 2f 0a 09 09 20 20 20 20 20 20  RING */...      
5ae50 69 66 20 28 6c 69 6e 65 2d 3e 44 69 6d 65 6e 73  if (line->Dimens
5ae60 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41  ionModel == GAIA
5ae70 5f 58 59 5f 5a 29 0a 09 09 09 20 20 72 65 73 75  _XY_Z)....  resu
5ae80 6c 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65  lt = gaiaAllocGe
5ae90 6f 6d 43 6f 6c 6c 58 59 5a 20 28 29 3b 0a 09 09  omCollXYZ ();...
5aea0 20 20 20 20 20 20 65 6c 73 65 20 69 66 20 28 6c        else if (l
5aeb0 69 6e 65 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f  ine->DimensionMo
5aec0 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d  del == GAIA_XY_M
5aed0 29 0a 09 09 09 20 20 72 65 73 75 6c 74 20 3d 20  )....  result = 
5aee0 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c  gaiaAllocGeomCol
5aef0 6c 58 59 4d 20 28 29 3b 0a 09 09 20 20 20 20 20  lXYM ();...     
5af00 20 65 6c 73 65 20 69 66 20 28 6c 69 6e 65 2d 3e   else if (line->
5af10 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
5af20 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09  = GAIA_XY_Z_M)..
5af30 09 09 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69  ..  result = gai
5af40 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58 59  aAllocGeomCollXY
5af50 5a 4d 20 28 29 3b 0a 09 09 20 20 20 20 20 20 65  ZM ();...      e
5af60 6c 73 65 0a 09 09 09 20 20 72 65 73 75 6c 74 20  lse....  result 
5af70 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43  = gaiaAllocGeomC
5af80 6f 6c 6c 20 28 29 3b 0a 09 09 20 20 20 20 20 20  oll ();...      
5af90 72 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67  result->Srid = g
5afa0 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 20 20 20 20  eo->Srid;...    
5afb0 20 20 6c 69 6e 65 32 20 3d 0a 09 09 09 20 20 67    line2 =....  g
5afc0 61 69 61 41 64 64 4c 69 6e 65 73 74 72 69 6e 67  aiaAddLinestring
5afd0 54 6f 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75  ToGeomColl (resu
5afe0 6c 74 2c 20 6c 69 6e 65 2d 3e 50 6f 69 6e 74 73  lt, line->Points
5aff0 29 3b 0a 09 09 20 20 20 20 20 20 66 6f 72 20 28  );...      for (
5b000 69 76 20 3d 20 30 3b 20 69 76 20 3c 20 6c 69 6e  iv = 0; iv < lin
5b010 65 32 2d 3e 50 6f 69 6e 74 73 3b 20 69 76 2b 2b  e2->Points; iv++
5b020 29 0a 09 09 09 7b 0a 09 09 09 20 20 20 20 69 66  )....{....    if
5b030 20 28 6c 69 6e 65 2d 3e 44 69 6d 65 6e 73 69 6f   (line->Dimensio
5b040 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
5b050 59 5f 5a 29 0a 09 09 09 20 20 20 20 20 20 7b 0a  Y_Z)....      {.
5b060 09 09 09 09 20 20 67 61 69 61 47 65 74 50 6f 69  ....  gaiaGetPoi
5b070 6e 74 58 59 5a 20 28 6c 69 6e 65 2d 3e 43 6f 6f  ntXYZ (line->Coo
5b080 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26 79 2c  rds, iv, &x, &y,
5b090 0a 09 09 09 09 09 09 20 20 20 26 7a 29 3b 0a 09  .......   &z);..
5b0a0 09 09 09 20 20 67 61 69 61 53 65 74 50 6f 69 6e  ...  gaiaSetPoin
5b0b0 74 58 59 5a 20 28 6c 69 6e 65 32 2d 3e 43 6f 6f  tXYZ (line2->Coo
5b0c0 72 64 73 2c 20 69 76 2c 20 78 2c 20 79 2c 20 7a  rds, iv, x, y, z
5b0d0 29 3b 0a 09 09 09 20 20 20 20 20 20 7d 0a 09 09  );....      }...
5b0e0 09 20 20 20 20 65 6c 73 65 20 69 66 20 28 6c 69  .    else if (li
5b0f0 6e 65 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64  ne->DimensionMod
5b100 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 4d 29  el == GAIA_XY_M)
5b110 0a 09 09 09 20 20 20 20 20 20 7b 0a 09 09 09 09  ....      {.....
5b120 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74 58 59    gaiaGetPointXY
5b130 4d 20 28 6c 69 6e 65 2d 3e 43 6f 6f 72 64 73 2c  M (line->Coords,
5b140 20 69 76 2c 20 26 78 2c 20 26 79 2c 0a 09 09 09   iv, &x, &y,....
5b150 09 09 09 20 20 20 26 6d 29 3b 0a 09 09 09 09 20  ...   &m);..... 
5b160 20 67 61 69 61 53 65 74 50 6f 69 6e 74 58 59 4d   gaiaSetPointXYM
5b170 20 28 6c 69 6e 65 32 2d 3e 43 6f 6f 72 64 73 2c   (line2->Coords,
5b180 20 69 76 2c 20 78 2c 20 79 2c 20 6d 29 3b 0a 09   iv, x, y, m);..
5b190 09 09 20 20 20 20 20 20 7d 0a 09 09 09 20 20 20  ..      }....   
5b1a0 20 65 6c 73 65 20 69 66 20 28 6c 69 6e 65 2d 3e   else if (line->
5b1b0 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
5b1c0 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09  = GAIA_XY_Z_M)..
5b1d0 09 09 20 20 20 20 20 20 7b 0a 09 09 09 09 20 20  ..      {.....  
5b1e0 67 61 69 61 47 65 74 50 6f 69 6e 74 58 59 5a 4d  gaiaGetPointXYZM
5b1f0 20 28 6c 69 6e 65 2d 3e 43 6f 6f 72 64 73 2c 20   (line->Coords, 
5b200 69 76 2c 20 26 78 2c 20 26 79 2c 0a 09 09 09 09  iv, &x, &y,.....
5b210 09 09 20 20 20 20 26 7a 2c 20 26 6d 29 3b 0a 09  ..    &z, &m);..
5b220 09 09 09 20 20 67 61 69 61 53 65 74 50 6f 69 6e  ...  gaiaSetPoin
5b230 74 58 59 5a 4d 20 28 6c 69 6e 65 32 2d 3e 43 6f  tXYZM (line2->Co
5b240 6f 72 64 73 2c 20 69 76 2c 20 78 2c 20 79 2c 20  ords, iv, x, y, 
5b250 7a 2c 0a 09 09 09 09 09 09 20 20 20 20 6d 29 3b  z,.......    m);
5b260 0a 09 09 09 20 20 20 20 20 20 7d 0a 09 09 09 20  ....      }.... 
5b270 20 20 20 65 6c 73 65 0a 09 09 09 20 20 20 20 20     else....     
5b280 20 7b 0a 09 09 09 09 20 20 67 61 69 61 47 65 74   {.....  gaiaGet
5b290 50 6f 69 6e 74 20 28 6c 69 6e 65 2d 3e 43 6f 6f  Point (line->Coo
5b2a0 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26 79 29  rds, iv, &x, &y)
5b2b0 3b 0a 09 09 09 09 20 20 67 61 69 61 53 65 74 50  ;.....  gaiaSetP
5b2c0 6f 69 6e 74 20 28 6c 69 6e 65 32 2d 3e 43 6f 6f  oint (line2->Coo
5b2d0 72 64 73 2c 20 69 76 2c 20 78 2c 20 79 29 3b 0a  rds, iv, x, y);.
5b2e0 09 09 09 20 20 20 20 20 20 7d 0a 09 09 09 7d 0a  ...      }....}.
5b2f0 09 09 20 20 20 20 20 20 67 6f 74 6f 20 73 6b 69  ..      goto ski
5b300 70 3b 0a 09 09 20 20 7d 0a 09 09 6c 69 6e 65 20  p;...  }...line 
5b310 3d 20 6c 69 6e 65 2d 3e 4e 65 78 74 3b 0a 09 20  = line->Next;.. 
5b320 20 20 20 7d 0a 09 20 20 70 6f 6c 79 67 20 3d 20     }..  polyg = 
5b330 67 65 6f 2d 3e 46 69 72 73 74 50 6f 6c 79 67 6f  geo->FirstPolygo
5b340 6e 3b 0a 09 20 20 77 68 69 6c 65 20 28 70 6f 6c  n;..  while (pol
5b350 79 67 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20  yg)..    {.../* 
5b360 63 6f 75 6e 74 73 20 68 6f 77 20 6d 61 6e 79 20  counts how many 
5b370 70 6f 6c 79 67 6f 6e 73 20 61 72 65 20 74 68 65  polygons are the
5b380 72 65 20 2a 2f 0a 09 09 63 6e 74 2b 2b 3b 0a 09  re */...cnt++;..
5b390 09 69 66 20 28 63 6e 74 20 3d 3d 20 65 6e 74 69  .if (cnt == enti
5b3a0 74 79 29 0a 09 09 20 20 7b 0a 09 09 20 20 20 20  ty)...  {...    
5b3b0 20 20 2f 2a 20 6f 6b 2c 20 72 65 71 75 69 72 65    /* ok, require
5b3c0 64 20 65 6c 65 6d 65 6e 74 61 72 79 20 67 65 6f  d elementary geo
5b3d0 6d 65 74 72 79 20 69 73 20 74 68 69 73 20 50 4f  metry is this PO
5b3e0 4c 59 47 4f 4e 20 2a 2f 0a 09 09 20 20 20 20 20  LYGON */...     
5b3f0 20 69 66 20 28 70 6f 6c 79 67 2d 3e 44 69 6d 65   if (polyg->Dime
5b400 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41  nsionModel == GA
5b410 49 41 5f 58 59 5f 5a 29 0a 09 09 09 20 20 72 65  IA_XY_Z)....  re
5b420 73 75 6c 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63  sult = gaiaAlloc
5b430 47 65 6f 6d 43 6f 6c 6c 58 59 5a 20 28 29 3b 0a  GeomCollXYZ ();.
5b440 09 09 20 20 20 20 20 20 65 6c 73 65 20 69 66 20  ..      else if 
5b450 28 70 6f 6c 79 67 2d 3e 44 69 6d 65 6e 73 69 6f  (polyg->Dimensio
5b460 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
5b470 59 5f 4d 29 0a 09 09 09 20 20 72 65 73 75 6c 74  Y_M)....  result
5b480 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d   = gaiaAllocGeom
5b490 43 6f 6c 6c 58 59 4d 20 28 29 3b 0a 09 09 20 20  CollXYM ();...  
5b4a0 20 20 20 20 65 6c 73 65 20 69 66 20 28 70 6f 6c      else if (pol
5b4b0 79 67 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64  yg->DimensionMod
5b4c0 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f  el == GAIA_XY_Z_
5b4d0 4d 29 0a 09 09 09 20 20 72 65 73 75 6c 74 20 3d  M)....  result =
5b4e0 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f   gaiaAllocGeomCo
5b4f0 6c 6c 58 59 5a 4d 20 28 29 3b 0a 09 09 20 20 20  llXYZM ();...   
5b500 20 20 20 65 6c 73 65 0a 09 09 09 20 20 72 65 73     else....  res
5b510 75 6c 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47  ult = gaiaAllocG
5b520 65 6f 6d 43 6f 6c 6c 20 28 29 3b 0a 09 09 20 20  eomColl ();...  
5b530 20 20 20 20 72 65 73 75 6c 74 2d 3e 53 72 69 64      result->Srid
5b540 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09   = geo->Srid;...
5b550 20 20 20 20 20 20 72 69 6e 67 5f 69 6e 20 3d 20        ring_in = 
5b560 70 6f 6c 79 67 2d 3e 45 78 74 65 72 69 6f 72 3b  polyg->Exterior;
5b570 0a 09 09 20 20 20 20 20 20 70 6f 6c 79 67 32 20  ...      polyg2 
5b580 3d 0a 09 09 09 20 20 67 61 69 61 41 64 64 50 6f  =....  gaiaAddPo
5b590 6c 79 67 6f 6e 54 6f 47 65 6f 6d 43 6f 6c 6c 20  lygonToGeomColl 
5b5a0 28 72 65 73 75 6c 74 2c 20 72 69 6e 67 5f 69 6e  (result, ring_in
5b5b0 2d 3e 50 6f 69 6e 74 73 2c 0a 09 09 09 09 09 09  ->Points,.......
5b5c0 20 20 20 20 70 6f 6c 79 67 2d 3e 4e 75 6d 49 6e      polyg->NumIn
5b5d0 74 65 72 69 6f 72 73 29 3b 0a 09 09 20 20 20 20  teriors);...    
5b5e0 20 20 72 69 6e 67 5f 6f 75 74 20 3d 20 70 6f 6c    ring_out = pol
5b5f0 79 67 32 2d 3e 45 78 74 65 72 69 6f 72 3b 0a 09  yg2->Exterior;..
5b600 09 20 20 20 20 20 20 66 6f 72 20 28 69 76 20 3d  .      for (iv =
5b610 20 30 3b 20 69 76 20 3c 20 72 69 6e 67 5f 6f 75   0; iv < ring_ou
5b620 74 2d 3e 50 6f 69 6e 74 73 3b 20 69 76 2b 2b 29  t->Points; iv++)
5b630 0a 09 09 09 7b 0a 09 09 09 20 20 20 20 2f 2a 20  ....{....    /* 
5b640 63 6f 70 79 69 6e 67 20 74 68 65 20 65 78 74 65  copying the exte
5b650 72 69 6f 72 20 72 69 6e 67 20 50 4f 49 4e 54 73  rior ring POINTs
5b660 20 2a 2f 0a 09 09 09 20 20 20 20 69 66 20 28 72   */....    if (r
5b670 69 6e 67 5f 69 6e 2d 3e 44 69 6d 65 6e 73 69 6f  ing_in->Dimensio
5b680 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
5b690 59 5f 5a 29 0a 09 09 09 20 20 20 20 20 20 7b 0a  Y_Z)....      {.
5b6a0 09 09 09 09 20 20 67 61 69 61 47 65 74 50 6f 69  ....  gaiaGetPoi
5b6b0 6e 74 58 59 5a 20 28 72 69 6e 67 5f 69 6e 2d 3e  ntXYZ (ring_in->
5b6c0 43 6f 6f 72 64 73 2c 20 69 76 2c 20 26 78 2c 20  Coords, iv, &x, 
5b6d0 26 79 2c 0a 09 09 09 09 09 09 20 20 20 26 7a 29  &y,.......   &z)
5b6e0 3b 0a 09 09 09 09 20 20 67 61 69 61 53 65 74 50  ;.....  gaiaSetP
5b6f0 6f 69 6e 74 58 59 5a 20 28 72 69 6e 67 5f 6f 75  ointXYZ (ring_ou
5b700 74 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 78  t->Coords, iv, x
5b710 2c 20 79 2c 0a 09 09 09 09 09 09 20 20 20 7a 29  , y,.......   z)
5b720 3b 0a 09 09 09 20 20 20 20 20 20 7d 0a 09 09 09  ;....      }....
5b730 20 20 20 20 65 6c 73 65 20 69 66 20 28 72 69 6e      else if (rin
5b740 67 5f 69 6e 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d  g_in->DimensionM
5b750 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f  odel == GAIA_XY_
5b760 4d 29 0a 09 09 09 20 20 20 20 20 20 7b 0a 09 09  M)....      {...
5b770 09 09 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74  ..  gaiaGetPoint
5b780 58 59 4d 20 28 72 69 6e 67 5f 69 6e 2d 3e 43 6f  XYM (ring_in->Co
5b790 6f 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26 79  ords, iv, &x, &y
5b7a0 2c 0a 09 09 09 09 09 09 20 20 20 26 6d 29 3b 0a  ,.......   &m);.
5b7b0 09 09 09 09 20 20 67 61 69 61 53 65 74 50 6f 69  ....  gaiaSetPoi
5b7c0 6e 74 58 59 4d 20 28 72 69 6e 67 5f 6f 75 74 2d  ntXYM (ring_out-
5b7d0 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 78 2c 20  >Coords, iv, x, 
5b7e0 79 2c 0a 09 09 09 09 09 09 20 20 20 6d 29 3b 0a  y,.......   m);.
5b7f0 09 09 09 20 20 20 20 20 20 7d 0a 09 09 09 20 20  ...      }....  
5b800 20 20 65 6c 73 65 20 69 66 20 28 72 69 6e 67 5f    else if (ring_
5b810 69 6e 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64  in->DimensionMod
5b820 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 5f  el == GAIA_XY_Z_
5b830 4d 29 0a 09 09 09 20 20 20 20 20 20 7b 0a 09 09  M)....      {...
5b840 09 09 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74  ..  gaiaGetPoint
5b850 58 59 5a 4d 20 28 72 69 6e 67 5f 69 6e 2d 3e 43  XYZM (ring_in->C
5b860 6f 6f 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26  oords, iv, &x, &
5b870 79 2c 0a 09 09 09 09 09 09 20 20 20 20 26 7a 2c  y,.......    &z,
5b880 20 26 6d 29 3b 0a 09 09 09 09 20 20 67 61 69 61   &m);.....  gaia
5b890 53 65 74 50 6f 69 6e 74 58 59 5a 4d 20 28 72 69  SetPointXYZM (ri
5b8a0 6e 67 5f 6f 75 74 2d 3e 43 6f 6f 72 64 73 2c 20  ng_out->Coords, 
5b8b0 69 76 2c 20 78 2c 20 79 2c 0a 09 09 09 09 09 09  iv, x, y,.......
5b8c0 20 20 20 20 7a 2c 20 6d 29 3b 0a 09 09 09 20 20      z, m);....  
5b8d0 20 20 20 20 7d 0a 09 09 09 20 20 20 20 65 6c 73      }....    els
5b8e0 65 0a 09 09 09 20 20 20 20 20 20 7b 0a 09 09 09  e....      {....
5b8f0 09 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74 20  .  gaiaGetPoint 
5b900 28 72 69 6e 67 5f 69 6e 2d 3e 43 6f 6f 72 64 73  (ring_in->Coords
5b910 2c 20 69 76 2c 20 26 78 2c 20 26 79 29 3b 0a 09  , iv, &x, &y);..
5b920 09 09 09 20 20 67 61 69 61 53 65 74 50 6f 69 6e  ...  gaiaSetPoin
5b930 74 20 28 72 69 6e 67 5f 6f 75 74 2d 3e 43 6f 6f  t (ring_out->Coo
5b940 72 64 73 2c 20 69 76 2c 20 78 2c 20 79 29 3b 0a  rds, iv, x, y);.
5b950 09 09 09 20 20 20 20 20 20 7d 0a 09 09 09 7d 0a  ...      }....}.
5b960 09 09 20 20 20 20 20 20 66 6f 72 20 28 69 62 20  ..      for (ib 
5b970 3d 20 30 3b 20 69 62 20 3c 20 70 6f 6c 79 67 32  = 0; ib < polyg2
5b980 2d 3e 4e 75 6d 49 6e 74 65 72 69 6f 72 73 3b 20  ->NumInteriors; 
5b990 69 62 2b 2b 29 0a 09 09 09 7b 0a 09 09 09 20 20  ib++)....{....  
5b9a0 20 20 2f 2a 20 70 72 6f 63 65 73 73 69 6e 67 20    /* processing 
5b9b0 74 68 65 20 69 6e 74 65 72 69 6f 72 20 72 69 6e  the interior rin
5b9c0 67 73 20 2a 2f 0a 09 09 09 20 20 20 20 72 69 6e  gs */....    rin
5b9d0 67 5f 69 6e 20 3d 20 70 6f 6c 79 67 2d 3e 49 6e  g_in = polyg->In
5b9e0 74 65 72 69 6f 72 73 20 2b 20 69 62 3b 0a 09 09  teriors + ib;...
5b9f0 09 20 20 20 20 72 69 6e 67 5f 6f 75 74 20 3d 0a  .    ring_out =.
5ba00 09 09 09 09 67 61 69 61 41 64 64 49 6e 74 65 72  ....gaiaAddInter
5ba10 69 6f 72 52 69 6e 67 20 28 70 6f 6c 79 67 32 2c  iorRing (polyg2,
5ba20 20 69 62 2c 0a 09 09 09 09 09 09 20 20 20 20 20   ib,.......     
5ba30 72 69 6e 67 5f 69 6e 2d 3e 50 6f 69 6e 74 73 29  ring_in->Points)
5ba40 3b 0a 09 09 09 20 20 20 20 66 6f 72 20 28 69 76  ;....    for (iv
5ba50 20 3d 20 30 3b 20 69 76 20 3c 20 72 69 6e 67 5f   = 0; iv < ring_
5ba60 6f 75 74 2d 3e 50 6f 69 6e 74 73 3b 20 69 76 2b  out->Points; iv+
5ba70 2b 29 0a 09 09 09 20 20 20 20 20 20 7b 0a 09 09  +)....      {...
5ba80 09 09 20 20 69 66 20 28 72 69 6e 67 5f 69 6e 2d  ..  if (ring_in-
5ba90 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20  >DimensionModel 
5baa0 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09 09  == GAIA_XY_Z)...
5bab0 09 09 20 20 20 20 7b 0a 09 09 09 09 09 67 61 69  ..    {......gai
5bac0 61 47 65 74 50 6f 69 6e 74 58 59 5a 20 28 72 69  aGetPointXYZ (ri
5bad0 6e 67 5f 69 6e 2d 3e 43 6f 6f 72 64 73 2c 20 69  ng_in->Coords, i
5bae0 76 2c 0a 09 09 09 09 09 09 09 20 26 78 2c 20 26  v,........ &x, &
5baf0 79 2c 20 26 7a 29 3b 0a 09 09 09 09 09 67 61 69  y, &z);......gai
5bb00 61 53 65 74 50 6f 69 6e 74 58 59 5a 20 28 72 69  aSetPointXYZ (ri
5bb10 6e 67 5f 6f 75 74 2d 3e 43 6f 6f 72 64 73 2c 20  ng_out->Coords, 
5bb20 69 76 2c 0a 09 09 09 09 09 09 09 20 78 2c 20 79  iv,........ x, y
5bb30 2c 20 7a 29 3b 0a 09 09 09 09 20 20 20 20 7d 0a  , z);.....    }.
5bb40 09 09 09 09 20 20 65 6c 73 65 20 69 66 20 28 72  ....  else if (r
5bb50 69 6e 67 5f 69 6e 2d 3e 44 69 6d 65 6e 73 69 6f  ing_in->Dimensio
5bb60 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
5bb70 59 5f 4d 29 0a 09 09 09 09 20 20 20 20 7b 0a 09  Y_M).....    {..
5bb80 09 09 09 09 67 61 69 61 47 65 74 50 6f 69 6e 74  ....gaiaGetPoint
5bb90 58 59 4d 20 28 72 69 6e 67 5f 69 6e 2d 3e 43 6f  XYM (ring_in->Co
5bba0 6f 72 64 73 2c 20 69 76 2c 0a 09 09 09 09 09 09  ords, iv,.......
5bbb0 09 20 26 78 2c 20 26 79 2c 20 26 6d 29 3b 0a 09  . &x, &y, &m);..
5bbc0 09 09 09 09 67 61 69 61 53 65 74 50 6f 69 6e 74  ....gaiaSetPoint
5bbd0 58 59 4d 20 28 72 69 6e 67 5f 6f 75 74 2d 3e 43  XYM (ring_out->C
5bbe0 6f 6f 72 64 73 2c 20 69 76 2c 0a 09 09 09 09 09  oords, iv,......
5bbf0 09 09 20 78 2c 20 79 2c 20 6d 29 3b 0a 09 09 09  .. x, y, m);....
5bc00 09 20 20 20 20 7d 0a 09 09 09 09 20 20 65 6c 73  .    }.....  els
5bc10 65 20 69 66 20 28 72 69 6e 67 5f 69 6e 2d 3e 44  e if (ring_in->D
5bc20 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d  imensionModel ==
5bc30 0a 09 09 09 09 09 20 20 20 47 41 49 41 5f 58 59  ......   GAIA_XY
5bc40 5f 5a 5f 4d 29 0a 09 09 09 09 20 20 20 20 7b 0a  _Z_M).....    {.
5bc50 09 09 09 09 09 67 61 69 61 47 65 74 50 6f 69 6e  .....gaiaGetPoin
5bc60 74 58 59 5a 4d 20 28 72 69 6e 67 5f 69 6e 2d 3e  tXYZM (ring_in->
5bc70 43 6f 6f 72 64 73 2c 20 69 76 2c 0a 09 09 09 09  Coords, iv,.....
5bc80 09 09 09 20 20 26 78 2c 20 26 79 2c 20 26 7a 2c  ...  &x, &y, &z,
5bc90 20 26 6d 29 3b 0a 09 09 09 09 09 67 61 69 61 53   &m);......gaiaS
5bca0 65 74 50 6f 69 6e 74 58 59 5a 4d 20 28 72 69 6e  etPointXYZM (rin
5bcb0 67 5f 6f 75 74 2d 3e 43 6f 6f 72 64 73 2c 20 69  g_out->Coords, i
5bcc0 76 2c 0a 09 09 09 09 09 09 09 20 20 78 2c 20 79  v,........  x, y
5bcd0 2c 20 7a 2c 20 6d 29 3b 0a 09 09 09 09 20 20 20  , z, m);.....   
5bce0 20 7d 0a 09 09 09 09 20 20 65 6c 73 65 0a 09 09   }.....  else...
5bcf0 09 09 20 20 20 20 7b 0a 09 09 09 09 09 67 61 69  ..    {......gai
5bd00 61 47 65 74 50 6f 69 6e 74 20 28 72 69 6e 67 5f  aGetPoint (ring_
5bd10 69 6e 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20  in->Coords, iv, 
5bd20 26 78 2c 0a 09 09 09 09 09 09 20 20 20 20 20 20  &x,.......      
5bd30 26 79 29 3b 0a 09 09 09 09 09 67 61 69 61 53 65  &y);......gaiaSe
5bd40 74 50 6f 69 6e 74 20 28 72 69 6e 67 5f 6f 75 74  tPoint (ring_out
5bd50 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 78 2c  ->Coords, iv, x,
5bd60 0a 09 09 09 09 09 09 20 20 20 20 20 20 79 29 3b  .......      y);
5bd70 0a 09 09 09 09 20 20 20 20 7d 0a 09 09 09 20 20  .....    }....  
5bd80 20 20 20 20 7d 0a 09 09 09 7d 0a 09 09 20 20 20      }....}...   
5bd90 20 20 20 67 6f 74 6f 20 73 6b 69 70 3b 0a 09 09     goto skip;...
5bda0 20 20 7d 0a 09 09 70 6f 6c 79 67 20 3d 20 70 6f    }...polyg = po
5bdb0 6c 79 67 2d 3e 4e 65 78 74 3b 0a 09 20 20 20 20  lyg->Next;..    
5bdc0 7d 0a 09 73 6b 69 70 3a 0a 09 20 20 69 66 20 28  }..skip:..  if (
5bdd0 72 65 73 75 6c 74 29 0a 09 20 20 20 20 7b 0a 09  result)..    {..
5bde0 09 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74  .gaiaToSpatiaLit
5bdf0 65 42 6c 6f 62 57 6b 62 20 28 72 65 73 75 6c 74  eBlobWkb (result
5be00 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65  , &p_result, &le
5be10 6e 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65  n);...gaiaFreeGe
5be20 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b  omColl (result);
5be30 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ...sqlite3_resul
5be40 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
5be50 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
5be60 66 72 65 65 29 3b 0a 09 20 20 20 20 7d 0a 09 20  free);..    }.. 
5be70 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
5be80 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
5be90 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
5bea0 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65    }.    gaiaFree
5beb0 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a  GeomColl (geo);.
5bec0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 6d  }..static void.m
5bed0 62 72 73 5f 65 76 61 6c 20 28 73 71 6c 69 74 65  brs_eval (sqlite
5bee0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
5bef0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
5bf00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
5bf10 61 72 67 76 2c 0a 09 20 20 20 69 6e 74 20 72 65  argv,..   int re
5bf20 71 75 65 73 74 29 0a 7b 0a 2f 2a 20 53 51 4c 20  quest).{./* SQL 
5bf30 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4d 42 52 73  function:./ MBRs
5bf40 6f 6d 65 74 68 69 6e 67 28 42 4c 4f 42 20 65 6e  omething(BLOB en
5bf50 63 6f 64 65 64 20 47 45 4f 4d 45 54 52 59 2d 31  coded GEOMETRY-1
5bf60 2c 20 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 47  , BLOB encoded G
5bf70 45 4f 4d 45 54 52 59 2d 32 29 0a 2f 0a 2f 20 72  EOMETRY-2)././ r
5bf80 65 74 75 72 6e 73 3a 0a 2f 20 31 20 69 66 20 74  eturns:./ 1 if t
5bf90 68 65 20 72 65 71 75 69 72 65 64 20 73 70 61 74  he required spat
5bfa0 69 61 6c 20 72 65 6c 61 74 69 6f 6e 73 68 69 70  ial relationship
5bfb0 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f   between the two
5bfc0 20 4d 42 52 73 20 69 73 20 54 52 55 45 0a 2f 20   MBRs is TRUE./ 
5bfd0 30 20 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f 72  0 otherwise./ or
5bfe0 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
5bff0 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
5c000 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  d.*/.    unsigne
5c010 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
5c020 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
5c030 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20  .    int ret;.  
5c040 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
5c050 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20  r geo1 = NULL;. 
5c060 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
5c070 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a  tr geo2 = NULL;.
5c080 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
5c090 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
5c0a0 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
5c0b0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
5c0c0 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
5c0d0 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
5c0e0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
5c0f0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
5c100 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
5c110 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
5c120 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
5c130 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29  e_type (argv[1])
5c140 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
5c150 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
5c160 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
5c170 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
5c180 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
5c190 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
5c1a0 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
5c1b0 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
5c1c0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
5c1d0 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
5c1e0 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
5c1f0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31  gv[0]);.    geo1
5c200 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69   = gaiaFromSpati
5c210 61 4c 69 74 65 42 6c 6f 62 4d 62 72 20 28 70 5f  aLiteBlobMbr (p_
5c220 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a  blob, n_bytes);.
5c230 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
5c240 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
5c250 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
5c260 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  b (argv[1]);.   
5c270 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
5c280 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
5c290 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65  argv[1]);.    ge
5c2a0 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  o2 = gaiaFromSpa
5c2b0 74 69 61 4c 69 74 65 42 6c 6f 62 4d 62 72 20 28  tiaLiteBlobMbr (
5c2c0 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
5c2d0 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 31 20  ;.    if (!geo1 
5c2e0 7c 7c 20 21 67 65 6f 32 29 0a 09 73 71 6c 69 74  || !geo2)..sqlit
5c2f0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
5c300 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
5c310 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65  se.      {..  re
5c320 74 20 3d 20 30 3b 0a 09 20 20 67 61 69 61 4d 62  t = 0;..  gaiaMb
5c330 72 47 65 6f 6d 65 74 72 79 20 28 67 65 6f 31 29  rGeometry (geo1)
5c340 3b 0a 09 20 20 67 61 69 61 4d 62 72 47 65 6f 6d  ;..  gaiaMbrGeom
5c350 65 74 72 79 20 28 67 65 6f 32 29 3b 0a 09 20 20  etry (geo2);..  
5c360 73 77 69 74 63 68 20 28 72 65 71 75 65 73 74 29  switch (request)
5c370 0a 09 20 20 20 20 7b 0a 09 20 20 20 20 63 61 73  ..    {..    cas
5c380 65 20 47 41 49 41 5f 4d 42 52 5f 43 4f 4e 54 41  e GAIA_MBR_CONTA
5c390 49 4e 53 3a 0a 09 09 72 65 74 20 3d 20 67 61 69  INS:...ret = gai
5c3a0 61 4d 62 72 73 43 6f 6e 74 61 69 6e 73 20 28 67  aMbrsContains (g
5c3b0 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 09 62 72  eo1, geo2);...br
5c3c0 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 47  eak;..    case G
5c3d0 41 49 41 5f 4d 42 52 5f 44 49 53 4a 4f 49 4e 54  AIA_MBR_DISJOINT
5c3e0 3a 0a 09 09 72 65 74 20 3d 20 67 61 69 61 4d 62  :...ret = gaiaMb
5c3f0 72 73 44 69 73 6a 6f 69 6e 74 20 28 67 65 6f 31  rsDisjoint (geo1
5c400 2c 20 67 65 6f 32 29 3b 0a 09 09 62 72 65 61 6b  , geo2);...break
5c410 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  ;..    case GAIA
5c420 5f 4d 42 52 5f 45 51 55 41 4c 3a 0a 09 09 72 65  _MBR_EQUAL:...re
5c430 74 20 3d 20 67 61 69 61 4d 62 72 73 45 71 75 61  t = gaiaMbrsEqua
5c440 6c 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a  l (geo1, geo2);.
5c450 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61  ..break;..    ca
5c460 73 65 20 47 41 49 41 5f 4d 42 52 5f 49 4e 54 45  se GAIA_MBR_INTE
5c470 52 53 45 43 54 53 3a 0a 09 09 72 65 74 20 3d 20  RSECTS:...ret = 
5c480 67 61 69 61 4d 62 72 73 49 6e 74 65 72 73 65 63  gaiaMbrsIntersec
5c490 74 73 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b  ts (geo1, geo2);
5c4a0 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20 63  ...break;..    c
5c4b0 61 73 65 20 47 41 49 41 5f 4d 42 52 5f 4f 56 45  ase GAIA_MBR_OVE
5c4c0 52 4c 41 50 53 3a 0a 09 09 72 65 74 20 3d 20 67  RLAPS:...ret = g
5c4d0 61 69 61 4d 62 72 73 4f 76 65 72 6c 61 70 73 20  aiaMbrsOverlaps 
5c4e0 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 09  (geo1, geo2);...
5c4f0 62 72 65 61 6b 3b 0a 09 20 20 20 20 63 61 73 65  break;..    case
5c500 20 47 41 49 41 5f 4d 42 52 5f 54 4f 55 43 48 45   GAIA_MBR_TOUCHE
5c510 53 3a 0a 09 09 72 65 74 20 3d 20 67 61 69 61 4d  S:...ret = gaiaM
5c520 62 72 73 54 6f 75 63 68 65 73 20 28 67 65 6f 31  brsTouches (geo1
5c530 2c 20 67 65 6f 32 29 3b 0a 09 09 62 72 65 61 6b  , geo2);...break
5c540 3b 0a 09 20 20 20 20 63 61 73 65 20 47 41 49 41  ;..    case GAIA
5c550 5f 4d 42 52 5f 57 49 54 48 49 4e 3a 0a 09 09 72  _MBR_WITHIN:...r
5c560 65 74 20 3d 20 67 61 69 61 4d 62 72 73 57 69 74  et = gaiaMbrsWit
5c570 68 69 6e 20 28 67 65 6f 31 2c 20 67 65 6f 32 29  hin (geo1, geo2)
5c580 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20 20 20 20  ;...break;..    
5c590 7d 0a 09 20 20 69 66 20 28 72 65 74 20 3c 20 30  }..  if (ret < 0
5c5a0 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  )..      sqlite3
5c5b0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
5c5c0 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a  ntext);..  else.
5c5d0 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
5c5e0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
5c5f0 78 74 2c 20 72 65 74 29 3b 0a 20 20 20 20 20 20  xt, ret);.      
5c600 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
5c610 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a 20  omColl (geo1);. 
5c620 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
5c630 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a 2f  oll (geo2);.}../
5c640 2a 0a 2f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  *./ the followin
5c650 67 20 66 75 6e 63 74 69 6f 6e 73 20 73 69 6d 70  g functions simp
5c660 6c 79 20 72 65 61 64 64 72 65 73 73 20 74 68 65  ly readdress the
5c670 20 6d 62 72 5f 65 76 61 6c 28 29 0a 2f 20 73 65   mbr_eval()./ se
5c680 74 74 69 6e 67 20 74 68 65 20 61 70 70 72 6f 70  tting the approp
5c690 72 69 61 74 65 20 72 65 71 75 65 73 74 20 6d 6f  riate request mo
5c6a0 64 65 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f  de.*/..static vo
5c6b0 69 64 0a 66 6e 63 74 5f 4d 62 72 43 6f 6e 74 61  id.fnct_MbrConta
5c6c0 69 6e 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ins (sqlite3_con
5c6d0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
5c6e0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
5c6f0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
5c700 0a 7b 0a 20 20 20 20 6d 62 72 73 5f 65 76 61 6c  .{.    mbrs_eval
5c710 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c   (context, argc,
5c720 20 61 72 67 76 2c 20 47 41 49 41 5f 4d 42 52 5f   argv, GAIA_MBR_
5c730 43 4f 4e 54 41 49 4e 53 29 3b 0a 7d 0a 0a 73 74  CONTAINS);.}..st
5c740 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4d  atic void.fnct_M
5c750 62 72 44 69 73 6a 6f 69 6e 74 20 28 73 71 6c 69  brDisjoint (sqli
5c760 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
5c770 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
5c780 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
5c790 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 6d 62  * argv).{.    mb
5c7a0 72 73 5f 65 76 61 6c 20 28 63 6f 6e 74 65 78 74  rs_eval (context
5c7b0 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
5c7c0 49 41 5f 4d 42 52 5f 44 49 53 4a 4f 49 4e 54 29  IA_MBR_DISJOINT)
5c7d0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
5c7e0 0a 66 6e 63 74 5f 4d 62 72 45 71 75 61 6c 20 28  .fnct_MbrEqual (
5c7f0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
5c800 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
5c810 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
5c820 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
5c830 20 20 6d 62 72 73 5f 65 76 61 6c 20 28 63 6f 6e    mbrs_eval (con
5c840 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
5c850 2c 20 47 41 49 41 5f 4d 42 52 5f 45 51 55 41 4c  , GAIA_MBR_EQUAL
5c860 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
5c870 64 0a 66 6e 63 74 5f 4d 62 72 49 6e 74 65 72 73  d.fnct_MbrInters
5c880 65 63 74 73 20 28 73 71 6c 69 74 65 33 5f 63 6f  ects (sqlite3_co
5c890 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
5c8a0 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
5c8b0 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
5c8c0 29 0a 7b 0a 20 20 20 20 6d 62 72 73 5f 65 76 61  ).{.    mbrs_eva
5c8d0 6c 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  l (context, argc
5c8e0 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 4d 42 52  , argv, GAIA_MBR
5c8f0 5f 49 4e 54 45 52 53 45 43 54 53 29 3b 0a 7d 0a  _INTERSECTS);.}.
5c900 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
5c910 74 5f 45 6e 76 49 6e 74 65 72 73 65 63 74 73 20  t_EnvIntersects 
5c920 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
5c930 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
5c940 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
5c950 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
5c960 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
5c970 2f 20 53 54 5f 45 6e 76 49 6e 74 65 72 73 65 63  / ST_EnvIntersec
5c980 74 73 28 47 65 6f 6d 65 74 72 79 20 67 65 6f 6d  ts(Geometry geom
5c990 2c 20 64 6f 75 62 6c 65 20 58 31 2c 20 64 6f 75  , double X1, dou
5c9a0 62 6c 65 20 59 31 2c 20 64 6f 75 62 6c 65 20 58  ble Y1, double X
5c9b0 32 2c 20 64 6f 75 62 6c 65 20 59 32 29 0a 2f 20  2, double Y2)./ 
5c9c0 53 54 5f 45 6e 76 65 6c 6f 70 65 73 49 6e 74 65  ST_EnvelopesInte
5c9d0 72 73 65 63 74 73 28 47 65 6f 6d 65 74 72 79 20  rsects(Geometry 
5c9e0 67 65 6f 6d 2c 20 64 6f 75 62 6c 65 20 58 31 2c  geom, double X1,
5c9f0 20 64 6f 75 62 6c 65 20 59 31 2c 20 64 6f 75 62   double Y1, doub
5ca00 6c 65 20 58 32 2c 20 64 6f 75 62 6c 65 20 59 32  le X2, double Y2
5ca10 29 0a 2f 0a 2f 20 74 68 65 20 73 65 63 6f 6e 64  )././ the second
5ca20 20 4d 42 52 20 69 73 20 64 65 66 69 6e 65 64 20   MBR is defined 
5ca30 62 79 20 74 77 6f 20 70 6f 69 6e 74 73 20 28 69  by two points (i
5ca40 64 65 6e 74 69 66 79 69 6e 67 20 61 20 72 65 63  dentifying a rec
5ca50 74 61 6e 67 6c 65 27 73 20 64 69 61 67 6f 6e 61  tangle's diagona
5ca60 6c 29 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  l) ./ or NULL if
5ca70 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
5ca80 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
5ca90 20 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20 20 20   double x1;.    
5caa0 64 6f 75 62 6c 65 20 79 31 3b 0a 20 20 20 20 64  double y1;.    d
5cab0 6f 75 62 6c 65 20 78 32 3b 0a 20 20 20 20 64 6f  ouble x2;.    do
5cac0 75 62 6c 65 20 79 32 3b 0a 20 20 20 20 69 6e 74  uble y2;.    int
5cad0 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20   int_value;.    
5cae0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
5caf0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
5cb00 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
5cb10 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 67 61 69  ret = 0;.    gai
5cb20 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
5cb30 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61  1 = NULL;.    ga
5cb40 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
5cb50 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  o2 = NULL;.    g
5cb60 61 69 61 4c 69 6e 65 73 74 72 69 6e 67 50 74 72  aiaLinestringPtr
5cb70 20 6c 6e 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e   ln;.    GAIA_UN
5cb80 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
5cb90 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
5cba0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
5cbb0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
5cbc0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
5cbd0 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
5cbe0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
5cbf0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
5cc00 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
5cc10 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
5cc20 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
5cc30 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[1]) == SQLITE_
5cc40 46 4c 4f 41 54 29 0a 09 78 31 20 3d 20 73 71 6c  FLOAT)..x1 = sql
5cc50 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
5cc60 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  e (argv[1]);.   
5cc70 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
5cc80 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
5cc90 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
5cca0 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
5ccb0 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
5ccc0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
5ccd0 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20  nt (argv[1]);.. 
5cce0 20 78 31 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b   x1 = int_value;
5ccf0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
5cd00 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
5cd10 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
5cd20 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
5cd30 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
5cd40 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
5cd50 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
5cd60 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c  2]) == SQLITE_FL
5cd70 4f 41 54 29 0a 09 79 31 20 3d 20 73 71 6c 69 74  OAT)..y1 = sqlit
5cd80 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
5cd90 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 65  (argv[2]);.    e
5cda0 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f  lse if (sqlite3_
5cdb0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
5cdc0 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [2]) == SQLITE_I
5cdd0 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
5cde0 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73  .  int_value = s
5cdf0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
5ce00 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20 79   (argv[2]);..  y
5ce10 31 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20  1 = int_value;. 
5ce20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
5ce30 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
5ce40 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
5ce50 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
5ce60 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
5ce70 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
5ce80 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 33 5d  ue_type (argv[3]
5ce90 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41  ) == SQLITE_FLOA
5cea0 54 29 0a 09 78 32 20 3d 20 73 71 6c 69 74 65 33  T)..x2 = sqlite3
5ceb0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61  _value_double (a
5cec0 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[3]);.    els
5ced0 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
5cee0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 33  lue_type (argv[3
5cef0 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
5cf00 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
5cf10 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
5cf20 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
5cf30 61 72 67 76 5b 33 5d 29 3b 0a 09 20 20 78 32 20  argv[3]);..  x2 
5cf40 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  = int_value;.   
5cf50 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20     }.    else.  
5cf60 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
5cf70 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
5cf80 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
5cf90 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
5cfa0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
5cfb0 5f 74 79 70 65 20 28 61 72 67 76 5b 34 5d 29 20  _type (argv[4]) 
5cfc0 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29  == SQLITE_FLOAT)
5cfd0 0a 09 79 32 20 3d 20 73 71 6c 69 74 65 33 5f 76  ..y2 = sqlite3_v
5cfe0 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67  alue_double (arg
5cff0 76 5b 34 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20  v[4]);.    else 
5d000 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
5d010 65 5f 74 79 70 65 20 28 61 72 67 76 5b 34 5d 29  e_type (argv[4])
5d020 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
5d030 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  ER).      {..  i
5d040 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  nt_value = sqlit
5d050 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
5d060 67 76 5b 34 5d 29 3b 0a 09 20 20 79 32 20 3d 20  gv[4]);..  y2 = 
5d070 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20  int_value;.     
5d080 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20   }.    else.    
5d090 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
5d0a0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
5d0b0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
5d0c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62  .      }.    p_b
5d0d0 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  lob = (unsigned 
5d0e0 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
5d0f0 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76  value_blob (argv
5d100 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65  [0]);.    n_byte
5d110 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s = sqlite3_valu
5d120 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d  e_bytes (argv[0]
5d130 29 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20 67 61  );.    geo1 = ga
5d140 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65  iaFromSpatiaLite
5d150 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c  BlobWkb (p_blob,
5d160 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69   n_bytes);.    i
5d170 66 20 28 21 67 65 6f 31 29 0a 09 73 71 6c 69 74  f (!geo1)..sqlit
5d180 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
5d190 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
5d1a0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 61  se.      {..  ga
5d1b0 69 61 4d 62 72 47 65 6f 6d 65 74 72 79 20 28 67  iaMbrGeometry (g
5d1c0 65 6f 31 29 3b 0a 09 20 20 67 65 6f 32 20 3d 20  eo1);..  geo2 = 
5d1d0 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c  gaiaAllocGeomCol
5d1e0 6c 20 28 29 3b 0a 09 20 20 6c 6e 20 3d 20 67 61  l ();..  ln = ga
5d1f0 69 61 41 64 64 4c 69 6e 65 73 74 72 69 6e 67 54  iaAddLinestringT
5d200 6f 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 2c  oGeomColl (geo2,
5d210 20 32 29 3b 0a 09 20 20 67 61 69 61 53 65 74 50   2);..  gaiaSetP
5d220 6f 69 6e 74 20 28 6c 6e 2d 3e 43 6f 6f 72 64 73  oint (ln->Coords
5d230 2c 20 30 2c 20 78 31 2c 20 79 31 29 3b 0a 09 20  , 0, x1, y1);.. 
5d240 20 67 61 69 61 53 65 74 50 6f 69 6e 74 20 28 6c   gaiaSetPoint (l
5d250 6e 2d 3e 43 6f 6f 72 64 73 2c 20 31 2c 20 78 32  n->Coords, 1, x2
5d260 2c 20 79 32 29 3b 0a 09 20 20 67 61 69 61 4d 62  , y2);..  gaiaMb
5d270 72 47 65 6f 6d 65 74 72 79 20 28 67 65 6f 32 29  rGeometry (geo2)
5d280 3b 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61 4d  ;..  ret = gaiaM
5d290 62 72 73 49 6e 74 65 72 73 65 63 74 73 20 28 67  brsIntersects (g
5d2a0 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20 20 73  eo1, geo2);..  s
5d2b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
5d2c0 74 20 28 63 6f 6e 74 65 78 74 2c 20 72 65 74 29  t (context, ret)
5d2d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  ;.      }.    ga
5d2e0 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
5d2f0 67 65 6f 31 29 3b 0a 20 20 20 20 67 61 69 61 46  geo1);.    gaiaF
5d300 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
5d310 32 29 3b 0a 7d 0a 0a 0a 73 74 61 74 69 63 20 76  2);.}...static v
5d320 6f 69 64 0a 66 6e 63 74 5f 4d 62 72 4f 76 65 72  oid.fnct_MbrOver
5d330 6c 61 70 73 20 28 73 71 6c 69 74 65 33 5f 63 6f  laps (sqlite3_co
5d340 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
5d350 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
5d360 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
5d370 29 0a 7b 0a 20 20 20 20 6d 62 72 73 5f 65 76 61  ).{.    mbrs_eva
5d380 6c 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  l (context, argc
5d390 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 4d 42 52  , argv, GAIA_MBR
5d3a0 5f 4f 56 45 52 4c 41 50 53 29 3b 0a 7d 0a 0a 73  _OVERLAPS);.}..s
5d3b0 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
5d3c0 4d 62 72 54 6f 75 63 68 65 73 20 28 73 71 6c 69  MbrTouches (sqli
5d3d0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
5d3e0 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
5d3f0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
5d400 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 6d 62  * argv).{.    mb
5d410 72 73 5f 65 76 61 6c 20 28 63 6f 6e 74 65 78 74  rs_eval (context
5d420 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
5d430 49 41 5f 4d 42 52 5f 54 4f 55 43 48 45 53 29 3b  IA_MBR_TOUCHES);
5d440 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
5d450 66 6e 63 74 5f 4d 62 72 57 69 74 68 69 6e 20 28  fnct_MbrWithin (
5d460 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
5d470 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
5d480 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
5d490 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
5d4a0 20 20 6d 62 72 73 5f 65 76 61 6c 20 28 63 6f 6e    mbrs_eval (con
5d4b0 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
5d4c0 2c 20 47 41 49 41 5f 4d 42 52 5f 57 49 54 48 49  , GAIA_MBR_WITHI
5d4d0 4e 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  N);.}..static vo
5d4e0 69 64 0a 66 6e 63 74 5f 53 68 69 66 74 43 6f 6f  id.fnct_ShiftCoo
5d4f0 72 64 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  rds (sqlite3_con
5d500 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
5d510 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
5d520 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
5d530 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
5d540 6f 6e 3a 0a 2f 20 53 68 69 66 74 43 6f 6f 72 64  on:./ ShiftCoord
5d550 73 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65  s(BLOBencoded ge
5d560 6f 6d 65 74 72 79 2c 20 73 68 69 66 74 58 2c 20  ometry, shiftX, 
5d570 73 68 69 66 74 59 29 0a 2f 0a 2f 20 72 65 74 75  shiftY)././ retu
5d580 72 6e 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74  rns a new geomet
5d590 72 79 20 74 68 61 74 20 69 73 20 74 68 65 20 6f  ry that is the o
5d5a0 72 69 67 69 6e 61 6c 20 6f 6e 65 20 72 65 63 65  riginal one rece
5d5b0 69 76 65 64 2c 20 62 75 74 20 77 69 74 68 20 73  ived, but with s
5d5c0 68 69 66 74 65 64 20 63 6f 6f 72 64 69 6e 61 74  hifted coordinat
5d5d0 65 73 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  es./ or NULL if 
5d5e0 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
5d5f0 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
5d600 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
5d610 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
5d620 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
5d630 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  len;.    unsigne
5d640 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
5d650 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
5d660 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
5d670 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75   = NULL;.    dou
5d680 62 6c 65 20 73 68 69 66 74 5f 78 3b 0a 20 20 20  ble shift_x;.   
5d690 20 64 6f 75 62 6c 65 20 73 68 69 66 74 5f 79 3b   double shift_y;
5d6a0 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c  .    int int_val
5d6b0 75 65 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  ue;.    GAIA_UNU
5d6c0 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
5d6d0 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
5d6e0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
5d6f0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
5d700 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
5d710 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
5d720 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
5d730 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
5d740 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
5d750 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
5d760 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
5d770 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46  [1]) == SQLITE_F
5d780 4c 4f 41 54 29 0a 09 73 68 69 66 74 5f 78 20 3d  LOAT)..shift_x =
5d790 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
5d7a0 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b  ouble (argv[1]);
5d7b0 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71  .    else if (sq
5d7c0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
5d7d0 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51   (argv[1]) == SQ
5d7e0 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
5d7f0 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c      {..  int_val
5d800 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
5d810 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
5d820 3b 0a 09 20 20 73 68 69 66 74 5f 78 20 3d 20 69  ;..  shift_x = i
5d830 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  nt_value;.      
5d840 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  }.    else.     
5d850 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
5d860 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
5d870 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
5d880 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
5d890 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
5d8a0 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d 3d 20  pe (argv[2]) == 
5d8b0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 73  SQLITE_FLOAT)..s
5d8c0 68 69 66 74 5f 79 20 3d 20 73 71 6c 69 74 65 33  hift_y = sqlite3
5d8d0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61  _value_double (a
5d8e0 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[2]);.    els
5d8f0 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
5d900 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32  lue_type (argv[2
5d910 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
5d920 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
5d930 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
5d940 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
5d950 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20 73 68 69  argv[2]);..  shi
5d960 66 74 5f 79 20 3d 20 69 6e 74 5f 76 61 6c 75 65  ft_y = int_value
5d970 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c  ;.      }.    el
5d980 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  se.      {..  sq
5d990 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
5d9a0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
5d9b0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
5d9c0 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
5d9d0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
5d9e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
5d9f0 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
5da00 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
5da10 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
5da20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
5da30 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  o = gaiaFromSpat
5da40 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
5da50 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
5da60 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09  .    if (!geo)..
5da70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
5da80 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
5da90 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
5daa0 09 20 20 67 61 69 61 53 68 69 66 74 43 6f 6f 72  .  gaiaShiftCoor
5dab0 64 73 20 28 67 65 6f 2c 20 73 68 69 66 74 5f 78  ds (geo, shift_x
5dac0 2c 20 73 68 69 66 74 5f 79 29 3b 0a 09 20 20 67  , shift_y);..  g
5dad0 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42  aiaToSpatiaLiteB
5dae0 6c 6f 62 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f  lobWkb (geo, &p_
5daf0 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09  result, &len);..
5db00 20 20 69 66 20 28 21 70 5f 72 65 73 75 6c 74 29    if (!p_result)
5db10 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
5db20 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
5db30 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09  text);..  else..
5db40 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
5db50 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
5db60 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
5db70 6e 2c 20 66 72 65 65 29 3b 0a 20 20 20 20 20 20  n, free);.      
5db80 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
5db90 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a  omColl (geo);.}.
5dba0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
5dbb0 74 5f 54 72 61 6e 73 6c 61 74 65 20 28 73 71 6c  t_Translate (sql
5dbc0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
5dbd0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
5dbe0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
5dbf0 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
5dc00 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 54 72  L function:./ Tr
5dc10 61 6e 73 6c 61 74 65 28 42 4c 4f 42 65 6e 63 6f  anslate(BLOBenco
5dc20 64 65 64 20 67 65 6f 6d 65 74 72 79 2c 20 73 68  ded geometry, sh
5dc30 69 66 74 58 2c 20 73 68 69 66 74 59 2c 20 73 68  iftX, shiftY, sh
5dc40 69 66 74 5a 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  iftZ)././ return
5dc50 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74 72 79  s a new geometry
5dc60 20 74 68 61 74 20 69 73 20 74 68 65 20 6f 72 69   that is the ori
5dc70 67 69 6e 61 6c 20 6f 6e 65 20 72 65 63 65 69 76  ginal one receiv
5dc80 65 64 2c 20 62 75 74 20 77 69 74 68 20 73 68 69  ed, but with shi
5dc90 66 74 65 64 20 63 6f 6f 72 64 69 6e 61 74 65 73  fted coordinates
5dca0 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
5dcb0 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
5dcc0 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
5dcd0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
5dce0 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
5dcf0 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65  ytes;.    int le
5dd00 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  n;.    unsigned 
5dd10 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
5dd20 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47   NULL;.    gaiaG
5dd30 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
5dd40 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75 62 6c   NULL;.    doubl
5dd50 65 20 73 68 69 66 74 5f 78 3b 0a 20 20 20 20 64  e shift_x;.    d
5dd60 6f 75 62 6c 65 20 73 68 69 66 74 5f 79 3b 0a 20  ouble shift_y;. 
5dd70 20 20 20 64 6f 75 62 6c 65 20 73 68 69 66 74 5f     double shift_
5dd80 7a 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76  z;.    int int_v
5dd90 61 6c 75 65 3b 0a 20 20 20 20 47 41 49 41 5f 55  alue;.    GAIA_U
5dda0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
5ddb0 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
5ddc0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
5ddd0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
5dde0 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
5ddf0 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
5de00 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
5de10 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
5de20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
5de30 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
5de40 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
5de50 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
5de60 5f 46 4c 4f 41 54 29 0a 09 73 68 69 66 74 5f 78  _FLOAT)..shift_x
5de70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
5de80 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d  _double (argv[1]
5de90 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28  );.    else if (
5dea0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
5deb0 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20  pe (argv[1]) == 
5dec0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a  SQLITE_INTEGER).
5ded0 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76        {..  int_v
5dee0 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  alue = sqlite3_v
5def0 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31  alue_int (argv[1
5df00 5d 29 3b 0a 09 20 20 73 68 69 66 74 5f 78 20 3d  ]);..  shift_x =
5df10 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20   int_value;.    
5df20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20    }.    else.   
5df30 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
5df40 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
5df50 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
5df60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
5df70 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
5df80 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d  type (argv[2]) =
5df90 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a  = SQLITE_FLOAT).
5dfa0 09 73 68 69 66 74 5f 79 20 3d 20 73 71 6c 69 74  .shift_y = sqlit
5dfb0 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
5dfc0 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 65  (argv[2]);.    e
5dfd0 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f  lse if (sqlite3_
5dfe0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
5dff0 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [2]) == SQLITE_I
5e000 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
5e010 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73  .  int_value = s
5e020 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
5e030 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20 73   (argv[2]);..  s
5e040 68 69 66 74 5f 79 20 3d 20 69 6e 74 5f 76 61 6c  hift_y = int_val
5e050 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
5e060 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
5e070 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
5e080 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
5e090 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
5e0a0 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
5e0b0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
5e0c0 67 76 5b 33 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[3]) == SQLITE
5e0d0 5f 46 4c 4f 41 54 29 0a 09 73 68 69 66 74 5f 7a  _FLOAT)..shift_z
5e0e0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
5e0f0 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 33 5d  _double (argv[3]
5e100 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28  );.    else if (
5e110 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
5e120 70 65 20 28 61 72 67 76 5b 33 5d 29 20 3d 3d 20  pe (argv[3]) == 
5e130 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a  SQLITE_INTEGER).
5e140 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76        {..  int_v
5e150 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  alue = sqlite3_v
5e160 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 33  alue_int (argv[3
5e170 5d 29 3b 0a 09 20 20 73 68 69 66 74 5f 7a 20 3d  ]);..  shift_z =
5e180 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20   int_value;.    
5e190 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20    }.    else.   
5e1a0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
5e1b0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
5e1c0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
5e1d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
5e1e0 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
5e1f0 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
5e200 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
5e210 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
5e220 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
5e230 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
5e240 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61  ]);.    geo = ga
5e250 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65  iaFromSpatiaLite
5e260 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c  BlobWkb (p_blob,
5e270 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69   n_bytes);.    i
5e280 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65  f (!geo)..sqlite
5e290 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
5e2a0 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73  ontext);.    els
5e2b0 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 61 69  e.      {..  gai
5e2c0 61 53 68 69 66 74 43 6f 6f 72 64 73 33 44 20 28  aShiftCoords3D (
5e2d0 67 65 6f 2c 20 73 68 69 66 74 5f 78 2c 20 73 68  geo, shift_x, sh
5e2e0 69 66 74 5f 79 2c 20 73 68 69 66 74 5f 7a 29 3b  ift_y, shift_z);
5e2f0 0a 09 20 20 67 61 69 61 54 6f 53 70 61 74 69 61  ..  gaiaToSpatia
5e300 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f  LiteBlobWkb (geo
5e310 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65  , &p_result, &le
5e320 6e 29 3b 0a 09 20 20 69 66 20 28 21 70 5f 72 65  n);..  if (!p_re
5e330 73 75 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c  sult)..      sql
5e340 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
5e350 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65   (context);..  e
5e360 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lse..      sqlit
5e370 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28  e3_result_blob (
5e380 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
5e390 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 20  t, len, free);. 
5e3a0 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
5e3b0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
5e3c0 29 3b 0a 7d 0a 0a 0a 73 74 61 74 69 63 20 76 6f  );.}...static vo
5e3d0 69 64 0a 66 6e 63 74 5f 53 68 69 66 74 4c 6f 6e  id.fnct_ShiftLon
5e3e0 67 69 74 75 64 65 20 28 73 71 6c 69 74 65 33 5f  gitude (sqlite3_
5e3f0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
5e400 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
5e410 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
5e420 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
5e430 63 74 69 6f 6e 3a 0a 2f 20 53 68 69 66 74 4c 6f  ction:./ ShiftLo
5e440 6e 67 69 74 75 64 65 28 42 4c 4f 42 65 6e 63 6f  ngitude(BLOBenco
5e450 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a  ded geometry)./.
5e460 2f 20 72 65 74 75 72 6e 73 20 61 20 6e 65 77 20  / returns a new 
5e470 67 65 6f 6d 65 74 72 79 20 74 68 61 74 20 69 73  geometry that is
5e480 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 6e   the original on
5e490 65 20 72 65 63 65 69 76 65 64 2c 20 62 75 74 20  e received, but 
5e4a0 77 69 74 68 20 6e 65 67 61 74 69 76 65 0a 2f 20  with negative./ 
5e4b0 6c 6f 6e 67 69 74 75 64 65 73 20 73 68 69 66 74  longitudes shift
5e4c0 65 64 20 62 79 20 33 36 30 0a 2f 20 6f 72 20 4e  ed by 360./ or N
5e4d0 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
5e4e0 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
5e4f0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
5e500 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
5e510 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
5e520 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20     int len;.    
5e530 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
5e540 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a  _result = NULL;.
5e550 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
5e560 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a  Ptr geo = NULL;.
5e570 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
5e580 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
5e590 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
5e5a0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
5e5b0 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
5e5c0 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
5e5d0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
5e5e0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
5e5f0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
5e600 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
5e610 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
5e620 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
5e630 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
5e640 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[0]);.    n_b
5e650 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
5e660 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
5e670 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20  [0]);.    geo = 
5e680 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
5e690 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
5e6a0 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
5e6b0 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69   if (!geo)..sqli
5e6c0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
5e6d0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
5e6e0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 67  lse.      {..  g
5e6f0 61 69 61 53 68 69 66 74 4c 6f 6e 67 69 74 75 64  aiaShiftLongitud
5e700 65 20 28 67 65 6f 29 3b 0a 09 20 20 67 61 69 61  e (geo);..  gaia
5e710 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
5e720 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65 73  Wkb (geo, &p_res
5e730 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 20 20 69  ult, &len);..  i
5e740 66 20 28 21 70 5f 72 65 73 75 6c 74 29 0a 09 20  f (!p_result).. 
5e750 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
5e760 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
5e770 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20  t);..  else..   
5e780 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
5e790 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
5e7a0 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
5e7b0 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  free);.      }. 
5e7c0 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
5e7d0 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
5e7e0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4e  atic void.fnct_N
5e7f0 6f 72 6d 61 6c 69 7a 65 4c 6f 6e 4c 61 74 20 28  ormalizeLonLat (
5e800 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
5e810 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
5e820 72 67 63 2c 0a 09 09 20 20 20 20 20 20 73 71 6c  rgc,...      sql
5e830 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
5e840 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
5e850 63 74 69 6f 6e 3a 0a 2f 20 4e 6f 72 6d 61 6c 69  ction:./ Normali
5e860 7a 65 4c 6f 6e 4c 61 74 20 28 42 4c 4f 42 65 6e  zeLonLat (BLOBen
5e870 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a  coded geometry).
5e880 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 6e 65  /./ returns a ne
5e890 77 20 67 65 6f 6d 65 74 72 79 20 74 68 61 74 20  w geometry that 
5e8a0 69 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  is the original 
5e8b0 6f 6e 65 20 72 65 63 65 69 76 65 64 2c 20 62 75  one received, bu
5e8c0 74 20 77 69 74 68 20 6c 6f 6e 67 69 74 75 64 65  t with longitude
5e8d0 0a 2f 20 61 6e 64 20 6c 61 74 69 74 75 64 65 20  ./ and latitude 
5e8e0 76 61 6c 75 65 73 20 73 68 69 66 74 65 64 20 69  values shifted i
5e8f0 6e 74 6f 20 74 68 65 20 72 61 6e 67 65 20 5b 2d  nto the range [-
5e900 31 38 30 20 2d 20 31 38 30 2c 20 2d 39 30 20 2d  180 - 180, -90 -
5e910 20 39 30 5d 2e 20 0a 2f 20 4e 55 4c 4c 20 69 73   90]. ./ NULL is
5e920 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 6e 79   returned if any
5e930 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
5e940 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73  tered.*/.    uns
5e950 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
5e960 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  ob;.    int n_by
5e970 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e  tes;.    int len
5e980 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
5e990 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20  har *p_result = 
5e9a0 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65  NULL;.    gaiaGe
5e9b0 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
5e9c0 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55  NULL;.    GAIA_U
5e9d0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
5e9e0 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
5e9f0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
5ea00 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
5ea10 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
5ea20 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
5ea30 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
5ea40 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
5ea50 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
5ea60 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
5ea70 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
5ea80 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
5ea90 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
5eaa0 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
5eab0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
5eac0 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
5ead0 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  geo = gaiaFromSp
5eae0 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
5eaf0 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
5eb00 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29  );.    if (!geo)
5eb10 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
5eb20 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
5eb30 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
5eb40 7b 0a 09 20 20 67 61 69 61 4e 6f 72 6d 61 6c 69  {..  gaiaNormali
5eb50 7a 65 4c 6f 6e 4c 61 74 20 28 67 65 6f 29 3b 0a  zeLonLat (geo);.
5eb60 09 20 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c  .  gaiaToSpatiaL
5eb70 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 2c  iteBlobWkb (geo,
5eb80 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e   &p_result, &len
5eb90 29 3b 0a 09 20 20 69 66 20 28 21 70 5f 72 65 73  );..  if (!p_res
5eba0 75 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c 69  ult)..      sqli
5ebb0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
5ebc0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c  (context);..  el
5ebd0 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  se..      sqlite
5ebe0 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63  3_result_blob (c
5ebf0 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74  ontext, p_result
5ec00 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 20 20  , len, free);.  
5ec10 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72      }.    gaiaFr
5ec20 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
5ec30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
5ec40 0a 66 6e 63 74 5f 53 63 61 6c 65 43 6f 6f 72 64  .fnct_ScaleCoord
5ec50 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  s (sqlite3_conte
5ec60 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
5ec70 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
5ec80 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
5ec90 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
5eca0 3a 0a 2f 20 53 63 61 6c 65 43 6f 6f 72 64 73 28  :./ ScaleCoords(
5ecb0 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d  BLOBencoded geom
5ecc0 65 74 72 79 2c 20 73 63 61 6c 65 5f 66 61 63 74  etry, scale_fact
5ecd0 6f 72 5f 78 20 5b 2c 20 73 63 61 6c 65 5f 66 61  or_x [, scale_fa
5ece0 63 74 6f 72 5f 79 5d 29 0a 2f 0a 2f 20 72 65 74  ctor_y])././ ret
5ecf0 75 72 6e 73 20 61 20 6e 65 77 20 67 65 6f 6d 65  urns a new geome
5ed00 74 72 79 20 74 68 61 74 20 69 73 20 74 68 65 20  try that is the 
5ed10 6f 72 69 67 69 6e 61 6c 20 6f 6e 65 20 72 65 63  original one rec
5ed20 65 69 76 65 64 2c 20 62 75 74 20 77 69 74 68 20  eived, but with 
5ed30 73 63 61 6c 65 64 20 63 6f 6f 72 64 69 6e 61 74  scaled coordinat
5ed40 65 73 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  es./ or NULL if 
5ed50 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
5ed60 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
5ed70 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
5ed80 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
5ed90 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
5eda0 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  len;.    unsigne
5edb0 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
5edc0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
5edd0 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
5ede0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75   = NULL;.    dou
5edf0 62 6c 65 20 73 63 61 6c 65 5f 78 3b 0a 20 20 20  ble scale_x;.   
5ee00 20 64 6f 75 62 6c 65 20 73 63 61 6c 65 5f 79 3b   double scale_y;
5ee10 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c  .    int int_val
5ee20 75 65 3b 0a 20 20 20 20 69 66 20 28 73 71 6c 69  ue;.    if (sqli
5ee30 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
5ee40 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
5ee50 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
5ee60 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
5ee70 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
5ee80 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
5ee90 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71      }.    if (sq
5eea0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
5eeb0 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51   (argv[1]) == SQ
5eec0 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 73 63 61  LITE_FLOAT)..sca
5eed0 6c 65 5f 78 20 3d 20 73 71 6c 69 74 65 33 5f 76  le_x = sqlite3_v
5eee0 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67  alue_double (arg
5eef0 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20  v[1]);.    else 
5ef00 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
5ef10 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29  e_type (argv[1])
5ef20 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
5ef30 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  ER).      {..  i
5ef40 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  nt_value = sqlit
5ef50 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
5ef60 67 76 5b 31 5d 29 3b 0a 09 20 20 73 63 61 6c 65  gv[1]);..  scale
5ef70 5f 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a  _x = int_value;.
5ef80 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
5ef90 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
5efa0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
5efb0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
5efc0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
5efd0 20 20 69 66 20 28 61 72 67 63 20 3d 3d 20 32 29    if (argc == 2)
5efe0 0a 09 73 63 61 6c 65 5f 79 20 3d 20 73 63 61 6c  ..scale_y = scal
5eff0 65 5f 78 3b 09 2f 2a 20 74 68 69 73 20 6f 6e 65  e_x;./* this one
5f000 20 69 73 20 61 6e 20 69 73 6f 74 72 6f 70 69 63   is an isotropic
5f010 20 73 63 61 6c 69 6e 67 20 72 65 71 75 65 73 74   scaling request
5f020 20 2a 2f 0a 20 20 20 20 65 6c 73 65 0a 20 20 20   */.    else.   
5f030 20 20 20 7b 0a 09 20 20 2f 2a 20 61 6e 20 61 6e     {..  /* an an
5f040 69 73 6f 74 72 6f 70 69 63 20 73 63 61 6c 69 6e  isotropic scalin
5f050 67 20 69 73 20 72 65 71 75 65 73 74 65 64 20 2a  g is requested *
5f060 2f 0a 09 20 20 69 66 20 28 73 71 6c 69 74 65 33  /..  if (sqlite3
5f070 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
5f080 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[2]) == SQLITE_
5f090 46 4c 4f 41 54 29 0a 09 20 20 20 20 20 20 73 63  FLOAT)..      sc
5f0a0 61 6c 65 5f 79 20 3d 20 73 71 6c 69 74 65 33 5f  ale_y = sqlite3_
5f0b0 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72  value_double (ar
5f0c0 67 76 5b 32 5d 29 3b 0a 09 20 20 65 6c 73 65 20  gv[2]);..  else 
5f0d0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
5f0e0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29  e_type (argv[2])
5f0f0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
5f100 45 52 29 0a 09 20 20 20 20 7b 0a 09 09 69 6e 74  ER)..    {...int
5f110 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33  _value = sqlite3
5f120 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76  _value_int (argv
5f130 5b 32 5d 29 3b 0a 09 09 73 63 61 6c 65 5f 79 20  [2]);...scale_y 
5f140 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 09 20 20  = int_value;..  
5f150 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20    }..  else..   
5f160 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73   {...sqlite3_res
5f170 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
5f180 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20  t);...return;.. 
5f190 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
5f1a0 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
5f1b0 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
5f1c0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
5f1d0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
5f1e0 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
5f1f0 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
5f200 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
5f210 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
5f220 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
5f230 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
5f240 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
5f250 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
5f260 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
5f270 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
5f280 67 61 69 61 53 63 61 6c 65 43 6f 6f 72 64 73 20  gaiaScaleCoords 
5f290 28 67 65 6f 2c 20 73 63 61 6c 65 5f 78 2c 20 73  (geo, scale_x, s
5f2a0 63 61 6c 65 5f 79 29 3b 0a 09 20 20 67 61 69 61  cale_y);..  gaia
5f2b0 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
5f2c0 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65 73  Wkb (geo, &p_res
5f2d0 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 20 20 69  ult, &len);..  i
5f2e0 66 20 28 21 70 5f 72 65 73 75 6c 74 29 0a 09 20  f (!p_result).. 
5f2f0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
5f300 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
5f310 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20  t);..  else..   
5f320 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
5f330 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
5f340 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
5f350 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  free);.      }. 
5f360 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
5f370 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
5f380 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 52  atic void.fnct_R
5f390 6f 74 61 74 65 43 6f 6f 72 64 73 20 28 73 71 6c  otateCoords (sql
5f3a0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
5f3b0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
5f3c0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
5f3d0 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
5f3e0 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 52 6f  L function:./ Ro
5f3f0 74 61 74 65 43 6f 6f 72 64 73 28 42 4c 4f 42 65  tateCoords(BLOBe
5f400 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c  ncoded geometry,
5f410 20 61 6e 67 6c 65 29 0a 2f 0a 2f 20 72 65 74 75   angle)././ retu
5f420 72 6e 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74  rns a new geomet
5f430 72 79 20 74 68 61 74 20 69 73 20 74 68 65 20 6f  ry that is the o
5f440 72 69 67 69 6e 61 6c 20 6f 6e 65 20 72 65 63 65  riginal one rece
5f450 69 76 65 64 2c 20 62 75 74 20 77 69 74 68 20 72  ived, but with r
5f460 6f 74 61 74 65 64 20 63 6f 6f 72 64 69 6e 61 74  otated coordinat
5f470 65 73 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  es./ or NULL if 
5f480 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
5f490 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
5f4a0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
5f4b0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
5f4c0 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
5f4d0 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  len;.    unsigne
5f4e0 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
5f4f0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
5f500 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
5f510 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 6f 75   = NULL;.    dou
5f520 62 6c 65 20 61 6e 67 6c 65 3b 0a 20 20 20 20 69  ble angle;.    i
5f530 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20  nt int_value;.  
5f540 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
5f550 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
5f560 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
5f570 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
5f580 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
5f590 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
5f5a0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
5f5b0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
5f5c0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
5f5d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
5f5e0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
5f5f0 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d  type (argv[1]) =
5f600 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a  = SQLITE_FLOAT).
5f610 09 61 6e 67 6c 65 20 3d 20 73 71 6c 69 74 65 33  .angle = sqlite3
5f620 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61  _value_double (a
5f630 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73  rgv[1]);.    els
5f640 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
5f650 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
5f660 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
5f670 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
5f680 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
5f690 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
5f6a0 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 61 6e 67  argv[1]);..  ang
5f6b0 6c 65 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a  le = int_value;.
5f6c0 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
5f6d0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
5f6e0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
5f6f0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
5f700 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
5f710 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
5f720 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
5f730 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
5f740 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
5f750 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
5f760 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
5f770 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20  gv[0]);.    geo 
5f780 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
5f790 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
5f7a0 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
5f7b0 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71     if (!geo)..sq
5f7c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
5f7d0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
5f7e0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
5f7f0 20 67 61 69 61 52 6f 74 61 74 65 43 6f 6f 72 64   gaiaRotateCoord
5f800 73 20 28 67 65 6f 2c 20 61 6e 67 6c 65 29 3b 0a  s (geo, angle);.
5f810 09 20 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c  .  gaiaToSpatiaL
5f820 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 2c  iteBlobWkb (geo,
5f830 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e   &p_result, &len
5f840 29 3b 0a 09 20 20 69 66 20 28 21 70 5f 72 65 73  );..  if (!p_res
5f850 75 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c 69  ult)..      sqli
5f860 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
5f870 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c  (context);..  el
5f880 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  se..      sqlite
5f890 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63  3_result_blob (c
5f8a0 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74  ontext, p_result
5f8b0 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 20 20  , len, free);.  
5f8c0 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72      }.    gaiaFr
5f8d0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
5f8e0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
5f8f0 0a 66 6e 63 74 5f 52 65 66 6c 65 63 74 43 6f 6f  .fnct_ReflectCoo
5f900 72 64 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  rds (sqlite3_con
5f910 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
5f920 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
5f930 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
5f940 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
5f950 6f 6e 3a 0a 2f 20 52 65 66 6c 65 63 74 43 6f 6f  on:./ ReflectCoo
5f960 72 64 73 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20  rds(BLOBencoded 
5f970 67 65 6f 6d 65 74 72 79 2c 20 78 5f 61 78 69 73  geometry, x_axis
5f980 2c 20 20 79 5f 61 78 69 73 29 0a 2f 0a 2f 20 72  ,  y_axis)././ r
5f990 65 74 75 72 6e 73 20 61 20 6e 65 77 20 67 65 6f  eturns a new geo
5f9a0 6d 65 74 72 79 20 74 68 61 74 20 69 73 20 74 68  metry that is th
5f9b0 65 20 6f 72 69 67 69 6e 61 6c 20 6f 6e 65 20 72  e original one r
5f9c0 65 63 65 69 76 65 64 2c 20 62 75 74 20 77 69 74  eceived, but wit
5f9d0 68 20 6d 69 72 72 6f 72 65 64 20 63 6f 6f 72 64  h mirrored coord
5f9e0 69 6e 61 74 65 73 0a 2f 20 6f 72 20 4e 55 4c 4c  inates./ or NULL
5f9f0 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
5fa00 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
5fa10 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
5fa20 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69  r *p_blob;.    i
5fa30 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
5fa40 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73  int len;.    uns
5fa50 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
5fa60 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  sult = NULL;.   
5fa70 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
5fa80 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
5fa90 20 69 6e 74 20 78 5f 61 78 69 73 3b 0a 20 20 20   int x_axis;.   
5faa0 20 69 6e 74 20 79 5f 61 78 69 73 3b 0a 20 20 20   int y_axis;.   
5fab0 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
5fac0 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
5fad0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
5fae0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
5faf0 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
5fb00 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20  QLITE_BLOB).    
5fb10 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
5fb20 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
5fb30 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
5fb40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
5fb50 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
5fb60 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d  ype (argv[1]) ==
5fb70 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
5fb80 0a 09 78 5f 61 78 69 73 20 3d 20 73 71 6c 69 74  ..x_axis = sqlit
5fb90 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
5fba0 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73 65  gv[1]);.    else
5fbb0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
5fbc0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
5fbd0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
5fbe0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
5fbf0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
5fc00 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32  lue_type (argv[2
5fc10 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
5fc20 45 47 45 52 29 0a 09 79 5f 61 78 69 73 20 3d 20  EGER)..y_axis = 
5fc30 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
5fc40 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  t (argv[2]);.   
5fc50 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
5fc60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
5fc70 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
5fc80 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
5fc90 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
5fca0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
5fcb0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
5fcc0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
5fcd0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
5fce0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
5fcf0 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
5fd00 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
5fd10 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
5fd20 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
5fd30 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
5fd40 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
5fd50 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
5fd60 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
5fd70 20 7b 0a 09 20 20 67 61 69 61 52 65 66 6c 65 63   {..  gaiaReflec
5fd80 74 43 6f 6f 72 64 73 20 28 67 65 6f 2c 20 78 5f  tCoords (geo, x_
5fd90 61 78 69 73 2c 20 79 5f 61 78 69 73 29 3b 0a 09  axis, y_axis);..
5fda0 20 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69    gaiaToSpatiaLi
5fdb0 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 2c 20  teBlobWkb (geo, 
5fdc0 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29  &p_result, &len)
5fdd0 3b 0a 09 20 20 69 66 20 28 21 70 5f 72 65 73 75  ;..  if (!p_resu
5fde0 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lt)..      sqlit
5fdf0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
5fe00 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73  context);..  els
5fe10 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  e..      sqlite3
5fe20 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
5fe30 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
5fe40 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 20 20 20   len, free);.   
5fe50 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65     }.    gaiaFre
5fe60 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b  eGeomColl (geo);
5fe70 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
5fe80 66 6e 63 74 5f 53 77 61 70 43 6f 6f 72 64 73 20  fnct_SwapCoords 
5fe90 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
5fea0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
5feb0 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
5fec0 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
5fed0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
5fee0 2f 20 53 77 61 70 43 6f 6f 72 64 73 28 42 4c 4f  / SwapCoords(BLO
5fef0 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72  Bencoded geometr
5ff00 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61  y)././ returns a
5ff10 20 6e 65 77 20 67 65 6f 6d 65 74 72 79 20 74 68   new geometry th
5ff20 61 74 20 69 73 20 74 68 65 20 6f 72 69 67 69 6e  at is the origin
5ff30 61 6c 20 6f 6e 65 20 72 65 63 65 69 76 65 64 2c  al one received,
5ff40 20 62 75 74 20 77 69 74 68 20 73 77 61 70 70 65   but with swappe
5ff50 64 20 78 2d 20 61 6e 64 20 79 2d 63 6f 6f 72 64  d x- and y-coord
5ff60 69 6e 61 74 65 0a 2f 20 6f 72 20 4e 55 4c 4c 20  inate./ or NULL 
5ff70 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20  if any error is 
5ff80 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20  encountered.*/. 
5ff90 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
5ffa0 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e   *p_blob;.    in
5ffb0 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69  t n_bytes;.    i
5ffc0 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69  nt len;.    unsi
5ffd0 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
5ffe0 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ult = NULL;.    
5fff0 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
60000 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
60010 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
60020 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
60030 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
60040 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
60050 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
60060 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
60070 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
60080 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
60090 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
600a0 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
600b0 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
600c0 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
600d0 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
600e0 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
600f0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
60100 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
60110 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61  ;.    geo = gaia
60120 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
60130 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
60140 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
60150 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f  (!geo)..sqlite3_
60160 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
60170 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a  text);.    else.
60180 20 20 20 20 20 20 7b 0a 09 20 20 67 61 69 61 53        {..  gaiaS
60190 77 61 70 43 6f 6f 72 64 73 20 28 67 65 6f 29 3b  wapCoords (geo);
601a0 0a 09 20 20 67 61 69 61 54 6f 53 70 61 74 69 61  ..  gaiaToSpatia
601b0 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f  LiteBlobWkb (geo
601c0 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65  , &p_result, &le
601d0 6e 29 3b 0a 09 20 20 69 66 20 28 21 70 5f 72 65  n);..  if (!p_re
601e0 73 75 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c  sult)..      sql
601f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
60200 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65   (context);..  e
60210 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lse..      sqlit
60220 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28  e3_result_blob (
60230 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
60240 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 20  t, len, free);. 
60250 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
60260 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
60270 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  );.}..static int
60280 0a 67 65 74 5f 65 6c 6c 69 70 73 65 5f 70 61 72  .get_ellipse_par
60290 61 6d 73 20 28 73 71 6c 69 74 65 33 20 2a 20 73  ams (sqlite3 * s
602a0 71 6c 69 74 65 2c 20 69 6e 74 20 73 72 69 64 2c  qlite, int srid,
602b0 20 64 6f 75 62 6c 65 20 2a 61 2c 20 64 6f 75 62   double *a, doub
602c0 6c 65 20 2a 62 2c 0a 09 09 20 20 20 20 64 6f 75  le *b,...    dou
602d0 62 6c 65 20 2a 72 66 29 0a 7b 0a 2f 2a 20 0a 2f  ble *rf).{./* ./
602e0 20 72 65 74 72 69 65 76 65 73 20 74 68 65 20 50   retrieves the P
602f0 52 4f 4a 20 2b 65 6c 6c 70 73 3d 78 78 20 5b 2b  ROJ +ellps=xx [+
60300 61 3d 78 78 20 2b 62 3d 78 78 5d 20 70 61 72 61  a=xx +b=xx] para
60310 6d 73 20 0a 2f 66 72 6f 6d 20 53 50 41 54 49 41  ms ./from SPATIA
60320 4c 5f 53 59 53 5f 52 45 46 20 74 61 62 6c 65 2c  L_SYS_REF table,
60330 20 69 66 20 70 6f 73 73 69 62 6c 65 20 0a 2a 2f   if possible .*/
60340 0a 20 20 20 20 63 68 61 72 20 70 72 6f 6a 34 74  .    char proj4t
60350 65 78 74 5b 32 30 34 38 5d 3b 0a 20 20 20 20 63  ext[2048];.    c
60360 68 61 72 20 2a 70 5f 70 72 6f 6a 3b 0a 20 20 20  har *p_proj;.   
60370 20 63 68 61 72 20 2a 70 5f 65 6c 6c 70 73 3b 0a   char *p_ellps;.
60380 20 20 20 20 63 68 61 72 20 2a 70 5f 64 61 74 75      char *p_datu
60390 6d 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 5f 61  m;.    char *p_a
603a0 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 5f 62 3b  ;.    char *p_b;
603b0 0a 20 20 20 20 63 68 61 72 20 2a 70 5f 65 6e 64  .    char *p_end
603c0 3b 0a 20 20 20 20 70 72 6f 6a 5f 70 61 72 61 6d  ;.    proj_param
603d0 73 20 28 73 71 6c 69 74 65 2c 20 73 72 69 64 2c  s (sqlite, srid,
603e0 20 70 72 6f 6a 34 74 65 78 74 29 3b 0a 20 20 20   proj4text);.   
603f0 20 69 66 20 28 2a 70 72 6f 6a 34 74 65 78 74 20   if (*proj4text 
60400 3d 3d 20 27 5c 30 27 29 0a 09 72 65 74 75 72 6e  == '\0')..return
60410 20 30 3b 0a 2f 2a 20 70 61 72 73 69 6e 67 20 74   0;./* parsing t
60420 68 65 20 70 72 6f 6a 34 74 65 78 74 20 67 65 6f  he proj4text geo
60430 64 65 73 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a  desic string */.
60440 20 20 20 20 70 5f 70 72 6f 6a 20 3d 20 73 74 72      p_proj = str
60450 73 74 72 20 28 70 72 6f 6a 34 74 65 78 74 2c 20  str (proj4text, 
60460 22 2b 70 72 6f 6a 3d 22 29 3b 0a 20 20 20 20 70  "+proj=");.    p
60470 5f 64 61 74 75 6d 20 3d 20 73 74 72 73 74 72 20  _datum = strstr 
60480 28 70 72 6f 6a 34 74 65 78 74 2c 20 22 2b 64 61  (proj4text, "+da
60490 74 75 6d 3d 22 29 3b 0a 20 20 20 20 70 5f 65 6c  tum=");.    p_el
604a0 6c 70 73 20 3d 20 73 74 72 73 74 72 20 28 70 72  lps = strstr (pr
604b0 6f 6a 34 74 65 78 74 2c 20 22 2b 65 6c 6c 70 73  oj4text, "+ellps
604c0 3d 22 29 3b 0a 20 20 20 20 70 5f 61 20 3d 20 73  =");.    p_a = s
604d0 74 72 73 74 72 20 28 70 72 6f 6a 34 74 65 78 74  trstr (proj4text
604e0 2c 20 22 2b 61 3d 22 29 3b 0a 20 20 20 20 70 5f  , "+a=");.    p_
604f0 62 20 3d 20 73 74 72 73 74 72 20 28 70 72 6f 6a  b = strstr (proj
60500 34 74 65 78 74 2c 20 22 2b 62 3d 22 29 3b 0a 2f  4text, "+b=");./
60510 2a 20 63 68 65 63 6b 69 6e 67 20 69 66 20 2b 70  * checking if +p
60520 72 6f 6a 3d 6c 6f 6e 67 6c 61 74 20 69 73 20 74  roj=longlat is t
60530 72 75 65 20 2a 2f 0a 20 20 20 20 69 66 20 28 21  rue */.    if (!
60540 70 5f 70 72 6f 6a 29 0a 09 72 65 74 75 72 6e 20  p_proj)..return 
60550 30 3b 0a 20 20 20 20 70 5f 65 6e 64 20 3d 20 73  0;.    p_end = s
60560 74 72 63 68 72 20 28 70 5f 70 72 6f 6a 2c 20 27  trchr (p_proj, '
60570 20 27 29 3b 0a 20 20 20 20 69 66 20 28 70 5f 65   ');.    if (p_e
60580 6e 64 29 0a 09 2a 70 5f 65 6e 64 20 3d 20 27 5c  nd)..*p_end = '\
60590 30 27 3b 0a 20 20 20 20 69 66 20 28 73 74 72 63  0';.    if (strc
605a0 6d 70 20 28 70 5f 70 72 6f 6a 20 2b 20 36 2c 20  mp (p_proj + 6, 
605b0 22 6c 6f 6e 67 6c 61 74 22 29 20 21 3d 20 30 29  "longlat") != 0)
605c0 0a 09 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ..return 0;.    
605d0 69 66 20 28 70 5f 65 6c 6c 70 73 29 0a 20 20 20  if (p_ellps).   
605e0 20 20 20 7b 0a 09 20 20 2f 2a 20 74 72 79 69 6e     {..  /* tryin
605f0 67 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  g to retrieve th
60600 65 20 65 6c 6c 69 70 73 6f 69 64 20 70 61 72 61  e ellipsoid para
60610 6d 73 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 09 20  ms by name */.. 
60620 20 70 5f 65 6e 64 20 3d 20 73 74 72 63 68 72 20   p_end = strchr 
60630 28 70 5f 65 6c 6c 70 73 2c 20 27 20 27 29 3b 0a  (p_ellps, ' ');.
60640 09 20 20 69 66 20 28 70 5f 65 6e 64 29 0a 09 20  .  if (p_end).. 
60650 20 20 20 20 20 2a 70 5f 65 6e 64 20 3d 20 27 5c       *p_end = '\
60660 30 27 3b 0a 09 20 20 69 66 20 28 67 61 69 61 45  0';..  if (gaiaE
60670 6c 6c 69 70 73 65 50 61 72 61 6d 73 20 28 70 5f  llipseParams (p_
60680 65 6c 6c 70 73 20 2b 20 37 2c 20 61 2c 20 62 2c  ellps + 7, a, b,
60690 20 72 66 29 29 0a 09 20 20 20 20 20 20 72 65 74   rf))..      ret
606a0 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
606b0 20 20 20 65 6c 73 65 20 69 66 20 28 70 5f 64 61     else if (p_da
606c0 74 75 6d 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  tum).      {..  
606d0 2f 2a 0a 09 20 20 20 20 20 2f 20 73 74 61 72 74  /*..     / start
606e0 69 6e 67 20 73 69 6e 63 65 20 47 44 41 4c 20 31  ing since GDAL 1
606f0 2e 39 2e 30 20 74 68 65 20 57 47 53 38 34 20 5b  .9.0 the WGS84 [
60700 34 33 32 36 5d 20 50 52 4f 4a 2e 34 20 64 65 66  4326] PROJ.4 def
60710 20 64 6f 65 73 6e 27 74 20 0a 09 20 20 20 20 20   doesn't ..     
60720 2f 20 64 65 63 6c 61 72 65 73 20 61 6e 79 20 6c  / declares any l
60730 6f 6e 67 65 72 20 74 68 65 20 22 2b 65 6c 6c 70  onger the "+ellp
60740 73 3d 22 20 70 61 72 61 6d 0a 09 20 20 20 20 20  s=" param..     
60750 2f 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 77  / in this case w
60760 65 27 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20  e'll attempt to 
60770 72 65 63 6f 76 65 72 20 75 73 69 6e 67 20 22 2b  recover using "+
60780 64 61 74 75 6d 3d 22 2e 0a 09 20 20 20 2a 2f 0a  datum="...   */.
60790 09 20 20 70 5f 65 6e 64 20 3d 20 73 74 72 63 68  .  p_end = strch
607a0 72 20 28 70 5f 64 61 74 75 6d 2c 20 27 20 27 29  r (p_datum, ' ')
607b0 3b 0a 09 20 20 69 66 20 28 70 5f 65 6e 64 29 0a  ;..  if (p_end).
607c0 09 20 20 20 20 20 20 2a 70 5f 65 6e 64 20 3d 20  .      *p_end = 
607d0 27 5c 30 27 3b 0a 09 20 20 69 66 20 28 67 61 69  '\0';..  if (gai
607e0 61 45 6c 6c 69 70 73 65 50 61 72 61 6d 73 20 28  aEllipseParams (
607f0 70 5f 64 61 74 75 6d 20 2b 20 37 2c 20 61 2c 20  p_datum + 7, a, 
60800 62 2c 20 72 66 29 29 0a 09 20 20 20 20 20 20 72  b, rf))..      r
60810 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
60820 0a 20 20 20 20 69 66 20 28 70 5f 61 20 26 26 20  .    if (p_a && 
60830 70 5f 62 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  p_b).      {..  
60840 2f 2a 20 74 72 79 69 6e 67 20 74 6f 20 72 65 74  /* trying to ret
60850 72 69 65 76 65 20 74 68 65 20 2b 61 3d 78 78 20  rieve the +a=xx 
60860 61 6e 64 20 2b 62 3d 78 78 20 61 72 67 73 20 2a  and +b=xx args *
60870 2f 0a 09 20 20 70 5f 65 6e 64 20 3d 20 73 74 72  /..  p_end = str
60880 63 68 72 20 28 70 5f 61 2c 20 27 20 27 29 3b 0a  chr (p_a, ' ');.
60890 09 20 20 69 66 20 28 70 5f 65 6e 64 29 0a 09 20  .  if (p_end).. 
608a0 20 20 20 20 20 2a 70 5f 65 6e 64 20 3d 20 27 5c       *p_end = '\
608b0 30 27 3b 0a 09 20 20 70 5f 65 6e 64 20 3d 20 73  0';..  p_end = s
608c0 74 72 63 68 72 20 28 70 5f 62 2c 20 27 20 27 29  trchr (p_b, ' ')
608d0 3b 0a 09 20 20 69 66 20 28 70 5f 65 6e 64 29 0a  ;..  if (p_end).
608e0 09 20 20 20 20 20 20 2a 70 5f 65 6e 64 20 3d 20  .      *p_end = 
608f0 27 5c 30 27 3b 0a 09 20 20 2a 61 20 3d 20 61 74  '\0';..  *a = at
60900 6f 66 20 28 70 5f 61 20 2b 20 33 29 3b 0a 09 20  of (p_a + 3);.. 
60910 20 2a 62 20 3d 20 61 74 6f 66 20 28 70 5f 62 20   *b = atof (p_b 
60920 2b 20 33 29 3b 0a 09 20 20 2a 72 66 20 3d 20 31  + 3);..  *rf = 1
60930 2e 30 20 2f 20 28 28 2a 61 20 2d 20 2a 62 29 20  .0 / ((*a - *b) 
60940 2f 20 2a 61 29 3b 0a 09 20 20 72 65 74 75 72 6e  / *a);..  return
60950 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
60960 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61  return 0;.}..sta
60970 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 46 72  tic void.fnct_Fr
60980 6f 6d 45 57 4b 42 20 28 73 71 6c 69 74 65 33 5f  omEWKB (sqlite3_
60990 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
609a0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
609b0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
609c0 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
609d0 63 74 69 6f 6e 3a 0a 2f 20 47 65 6f 6d 46 72 6f  ction:./ GeomFro
609e0 6d 45 57 4b 42 28 45 57 4b 42 20 65 6e 63 6f 64  mEWKB(EWKB encod
609f0 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f  ed geometry)././
60a00 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 75 72   returns the cur
60a10 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 20 62 79  rent geometry by
60a20 20 70 61 72 73 69 6e 67 20 47 65 6f 73 2f 50 6f   parsing Geos/Po
60a30 73 74 47 69 73 20 45 57 4b 42 20 65 6e 63 6f 64  stGis EWKB encod
60a40 65 64 20 73 74 72 69 6e 67 20 0a 2f 20 6f 72 20  ed string ./ or 
60a50 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f  NULL if any erro
60a60 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
60a70 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  .*/.    int len;
60a80 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
60a90 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e  ar *p_result = N
60aa0 55 4c 4c 3b 0a 20 20 20 20 63 6f 6e 73 74 20 75  ULL;.    const u
60ab0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65  nsigned char *te
60ac0 78 74 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  xt;.    gaiaGeom
60ad0 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
60ae0 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  LL;.    GAIA_UNU
60af0 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
60b00 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
60b10 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
60b20 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
60b30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58  ]) != SQLITE_TEX
60b40 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  T).      {..  sq
60b50 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
60b60 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
60b70 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
60b80 20 20 20 20 74 65 78 74 20 3d 20 73 71 6c 69 74      text = sqlit
60b90 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61  e3_value_text (a
60ba0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[0]);.    geo
60bb0 20 3d 20 67 61 69 61 46 72 6f 6d 45 57 4b 42 20   = gaiaFromEWKB 
60bc0 28 74 65 78 74 29 3b 0a 20 20 20 20 69 66 20 28  (text);.    if (
60bd0 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20  geo == NULL).   
60be0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
60bf0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
60c00 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
60c10 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  ;.      }.    ga
60c20 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c  iaToSpatiaLiteBl
60c30 6f 62 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72  obWkb (geo, &p_r
60c40 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20  esult, &len);.  
60c50 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
60c60 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71  ll (geo);.    sq
60c70 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
60c80 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
60c90 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29  sult, len, free)
60ca0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
60cb0 0a 66 6e 63 74 5f 54 6f 45 57 4b 42 20 28 73 71  .fnct_ToEWKB (sq
60cc0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
60cd0 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
60ce0 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
60cf0 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
60d00 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 41  QL function:./ A
60d10 73 45 57 4b 42 28 42 4c 4f 42 20 65 6e 63 6f 64  sEWKB(BLOB encod
60d20 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f  ed geometry)././
60d30 20 72 65 74 75 72 6e 73 20 61 20 74 65 78 74 20   returns a text 
60d40 73 74 72 69 6e 67 20 63 6f 72 72 65 73 70 6f 6e  string correspon
60d50 64 69 6e 67 20 74 6f 20 47 65 6f 73 2f 50 6f 73  ding to Geos/Pos
60d60 74 47 49 53 20 45 57 4b 42 20 6e 6f 74 61 74 69  tGIS EWKB notati
60d70 6f 6e 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  on ./ or NULL if
60d80 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
60d90 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
60da0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
60db0 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20  p_blob;.    int 
60dc0 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74  n_bytes;.    int
60dd0 20 6c 65 6e 3b 0a 20 20 20 20 67 61 69 61 4f 75   len;.    gaiaOu
60de0 74 42 75 66 66 65 72 20 6f 75 74 5f 62 75 66 3b  tBuffer out_buf;
60df0 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
60e00 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b  lPtr geo = NULL;
60e10 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
60e20 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
60e30 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
60e40 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
60e50 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
60e60 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
60e70 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
60e80 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
60e90 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
60ea0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
60eb0 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
60ec0 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
60ed0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
60ee0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
60ef0 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
60f00 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
60f10 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
60f20 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
60f30 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
60f40 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
60f50 20 20 69 66 20 28 21 67 65 6f 29 0a 20 20 20 20    if (!geo).    
60f60 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
60f70 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
60f80 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
60f90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
60fa0 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 61 69  e.      {..  gai
60fb0 61 4f 75 74 42 75 66 66 65 72 49 6e 69 74 69 61  aOutBufferInitia
60fc0 6c 69 7a 65 20 28 26 6f 75 74 5f 62 75 66 29 3b  lize (&out_buf);
60fd0 0a 09 20 20 67 61 69 61 54 6f 45 57 4b 42 20 28  ..  gaiaToEWKB (
60fe0 26 6f 75 74 5f 62 75 66 2c 20 67 65 6f 29 3b 0a  &out_buf, geo);.
60ff0 09 20 20 69 66 20 28 6f 75 74 5f 62 75 66 2e 45  .  if (out_buf.E
61000 72 72 6f 72 20 7c 7c 20 6f 75 74 5f 62 75 66 2e  rror || out_buf.
61010 42 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c 29 0a  Buffer == NULL).
61020 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
61030 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
61040 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20  ext);..  else.. 
61050 20 20 20 7b 0a 09 09 6c 65 6e 20 3d 20 6f 75 74     {...len = out
61060 5f 62 75 66 2e 57 72 69 74 65 4f 66 66 73 65 74  _buf.WriteOffset
61070 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75  ;...sqlite3_resu
61080 6c 74 5f 74 65 78 74 20 28 63 6f 6e 74 65 78 74  lt_text (context
61090 2c 20 6f 75 74 5f 62 75 66 2e 42 75 66 66 65 72  , out_buf.Buffer
610a0 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09  , len, free);...
610b0 6f 75 74 5f 62 75 66 2e 42 75 66 66 65 72 20 3d  out_buf.Buffer =
610c0 20 4e 55 4c 4c 3b 0a 09 20 20 20 20 7d 0a 20 20   NULL;..    }.  
610d0 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72      }.    gaiaFr
610e0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
610f0 3b 0a 20 20 20 20 67 61 69 61 4f 75 74 42 75 66  ;.    gaiaOutBuf
61100 66 65 72 52 65 73 65 74 20 28 26 6f 75 74 5f 62  ferReset (&out_b
61110 75 66 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  uf);.}..static v
61120 6f 69 64 0a 66 6e 63 74 5f 54 6f 45 57 4b 54 20  oid.fnct_ToEWKT 
61130 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
61140 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
61150 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
61160 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
61170 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
61180 2f 20 41 73 45 57 4b 54 28 42 4c 4f 42 20 65 6e  / AsEWKT(BLOB en
61190 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a  coded geometry).
611a0 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20  /./ returns the 
611b0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 6f  corresponding Po
611c0 73 74 47 49 53 20 45 57 4b 54 20 65 6e 63 6f 64  stGIS EWKT encod
611d0 65 64 20 76 61 6c 75 65 0a 2f 20 6f 72 20 4e 55  ed value./ or NU
611e0 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
611f0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
61200 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
61210 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20  har *p_blob;.   
61220 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20   int n_bytes;.  
61230 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 67    int len;.    g
61240 61 69 61 4f 75 74 42 75 66 66 65 72 20 6f 75 74  aiaOutBuffer out
61250 5f 62 75 66 3b 0a 20 20 20 20 67 61 69 61 47 65  _buf;.    gaiaGe
61260 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
61270 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55  NULL;.    GAIA_U
61280 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
61290 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
612a0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
612b0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
612c0 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
612d0 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
612e0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
612f0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
61300 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
61310 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
61320 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
61330 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
61340 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
61350 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
61360 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
61370 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
61380 67 61 69 61 4f 75 74 42 75 66 66 65 72 49 6e 69  gaiaOutBufferIni
61390 74 69 61 6c 69 7a 65 20 28 26 6f 75 74 5f 62 75  tialize (&out_bu
613a0 66 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61  f);.    geo = ga
613b0 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65  iaFromSpatiaLite
613c0 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c  BlobWkb (p_blob,
613d0 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69   n_bytes);.    i
613e0 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65  f (!geo)..sqlite
613f0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
61400 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73  ontext);.    els
61410 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 61 69  e.      {..  gai
61420 61 54 6f 45 57 4b 54 20 28 26 6f 75 74 5f 62 75  aToEWKT (&out_bu
61430 66 2c 20 67 65 6f 29 3b 0a 09 20 20 69 66 20 28  f, geo);..  if (
61440 6f 75 74 5f 62 75 66 2e 45 72 72 6f 72 20 7c 7c  out_buf.Error ||
61450 20 6f 75 74 5f 62 75 66 2e 42 75 66 66 65 72 20   out_buf.Buffer 
61460 3d 3d 20 4e 55 4c 4c 29 0a 09 20 20 20 20 20 20  == NULL)..      
61470 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
61480 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
61490 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09    else..    {...
614a0 6c 65 6e 20 3d 20 6f 75 74 5f 62 75 66 2e 57 72  len = out_buf.Wr
614b0 69 74 65 4f 66 66 73 65 74 3b 0a 09 09 73 71 6c  iteOffset;...sql
614c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
614d0 20 28 63 6f 6e 74 65 78 74 2c 20 6f 75 74 5f 62   (context, out_b
614e0 75 66 2e 42 75 66 66 65 72 2c 20 6c 65 6e 2c 20  uf.Buffer, len, 
614f0 66 72 65 65 29 3b 0a 09 09 6f 75 74 5f 62 75 66  free);...out_buf
61500 2e 42 75 66 66 65 72 20 3d 20 4e 55 4c 4c 3b 0a  .Buffer = NULL;.
61510 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20  .    }.      }. 
61520 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
61530 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20 20 67  oll (geo);.    g
61540 61 69 61 4f 75 74 42 75 66 66 65 72 52 65 73 65  aiaOutBufferRese
61550 74 20 28 26 6f 75 74 5f 62 75 66 29 3b 0a 7d 0a  t (&out_buf);.}.
61560 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
61570 74 5f 46 72 6f 6d 45 57 4b 54 20 28 73 71 6c 69  t_FromEWKT (sqli
61580 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
61590 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
615a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
615b0 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
615c0 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 47 65 6f   function:./ Geo
615d0 6d 46 72 6f 6d 45 57 4b 54 28 45 57 4b 54 20 65  mFromEWKT(EWKT e
615e0 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29  ncoded geometry)
615f0 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65  ././ returns the
61600 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
61610 79 20 62 79 20 70 61 72 73 69 6e 67 20 45 57 4b  y by parsing EWK
61620 54 20 20 28 50 6f 73 74 47 49 53 29 20 65 6e 63  T  (PostGIS) enc
61630 6f 64 65 64 20 73 74 72 69 6e 67 20 0a 2f 20 6f  oded string ./ o
61640 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72  r NULL if any er
61650 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
61660 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65  ed.*/.    int le
61670 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  n;.    unsigned 
61680 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
61690 20 4e 55 4c 4c 3b 0a 20 20 20 20 63 6f 6e 73 74   NULL;.    const
616a0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
616b0 74 65 78 74 3b 0a 20 20 20 20 67 61 69 61 47 65  text;.    gaiaGe
616c0 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
616d0 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55  NULL;.    GAIA_U
616e0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
616f0 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
61700 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
61710 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
61720 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 54  [0]) != SQLITE_T
61730 45 58 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  EXT).      {..  
61740 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
61750 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
61760 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
61770 7d 0a 20 20 20 20 74 65 78 74 20 3d 20 73 71 6c  }.    text = sql
61780 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
61790 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
617a0 65 6f 20 3d 20 67 61 69 61 50 61 72 73 65 45 57  eo = gaiaParseEW
617b0 4b 54 20 28 74 65 78 74 29 3b 0a 20 20 20 20 69  KT (text);.    i
617c0 66 20 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a  f (geo == NULL).
617d0 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
617e0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
617f0 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
61800 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
61810 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74   gaiaToSpatiaLit
61820 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 2c 20 26  eBlobWkb (geo, &
61830 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b  p_result, &len);
61840 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f  .    gaiaFreeGeo
61850 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20  mColl (geo);.   
61860 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
61870 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70  blob (context, p
61880 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72  _result, len, fr
61890 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  ee);.}..static v
618a0 6f 69 64 0a 66 6e 63 74 5f 46 72 6f 6d 47 65 6f  oid.fnct_FromGeo
618b0 4a 53 4f 4e 20 28 73 71 6c 69 74 65 33 5f 63 6f  JSON (sqlite3_co
618c0 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
618d0 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
618e0 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
618f0 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
61900 69 6f 6e 3a 0a 2f 20 47 65 6f 6d 46 72 6f 6d 47  ion:./ GeomFromG
61910 65 6f 4a 53 4f 4e 28 47 65 6f 4a 53 4f 4e 20 65  eoJSON(GeoJSON e
61920 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29  ncoded geometry)
61930 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65  ././ returns the
61940 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
61950 79 20 62 79 20 70 61 72 73 69 6e 67 20 47 65 6f  y by parsing Geo
61960 4a 53 4f 4e 20 65 6e 63 6f 64 65 64 20 73 74 72  JSON encoded str
61970 69 6e 67 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  ing ./ or NULL i
61980 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65  f any error is e
61990 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20  ncountered.*/.  
619a0 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 75    int len;.    u
619b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
619c0 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20  result = NULL;. 
619d0 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
619e0 64 20 63 68 61 72 20 2a 74 65 78 74 3b 0a 20 20  d char *text;.  
619f0 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
61a00 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
61a10 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
61a20 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
61a30 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
61a40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
61a50 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
61a60 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20  SQLITE_TEXT).   
61a70 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
61a80 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
61a90 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
61aa0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 74 65  ;.      }.    te
61ab0 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  xt = sqlite3_val
61ac0 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d  ue_text (argv[0]
61ad0 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69  );.    geo = gai
61ae0 61 50 61 72 73 65 47 65 6f 4a 53 4f 4e 20 28 74  aParseGeoJSON (t
61af0 65 78 74 29 3b 0a 20 20 20 20 69 66 20 28 67 65  ext);.    if (ge
61b00 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20  o == NULL).     
61b10 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
61b20 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
61b30 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
61b40 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61        }.    gaia
61b50 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
61b60 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65 73  Wkb (geo, &p_res
61b70 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20  ult, &len);.    
61b80 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
61b90 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71 6c 69   (geo);.    sqli
61ba0 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
61bb0 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
61bc0 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
61bd0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
61be0 6e 63 74 5f 46 72 6f 6d 4b 6d 6c 20 28 73 71 6c  nct_FromKml (sql
61bf0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
61c00 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
61c10 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
61c20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
61c30 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 47 65  L function:./ Ge
61c40 6f 6d 46 72 6f 6d 4b 6d 6c 28 4b 4d 4c 20 65 6e  omFromKml(KML en
61c50 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a  coded geometry).
61c60 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20  /./ returns the 
61c70 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79  current geometry
61c80 20 62 79 20 70 61 72 73 69 6e 67 20 4b 4d 4c 20   by parsing KML 
61c90 65 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20 0a  encoded string .
61ca0 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79  / or NULL if any
61cb0 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
61cc0 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74  tered.*/.    int
61cd0 20 6c 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e   len;.    unsign
61ce0 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c  ed char *p_resul
61cf0 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 63 6f  t = NULL;.    co
61d00 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
61d10 72 20 2a 74 65 78 74 3b 0a 20 20 20 20 67 61 69  r *text;.    gai
61d20 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
61d30 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49   = NULL;.    GAI
61d40 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
61d50 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
61d60 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
61d70 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
61d80 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[0]) != SQLIT
61d90 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20 7b 0a  E_TEXT).      {.
61da0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
61db0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
61dc0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
61dd0 20 20 20 7d 0a 20 20 20 20 74 65 78 74 20 3d 20     }.    text = 
61de0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
61df0 78 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  xt (argv[0]);.  
61e00 20 20 67 65 6f 20 3d 20 67 61 69 61 50 61 72 73    geo = gaiaPars
61e10 65 4b 6d 6c 20 28 74 65 78 74 29 3b 0a 20 20 20  eKml (text);.   
61e20 20 69 66 20 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c   if (geo == NULL
61e30 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
61e40 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
61e50 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
61e60 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
61e70 20 20 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c     gaiaToSpatiaL
61e80 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 2c  iteBlobWkb (geo,
61e90 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e   &p_result, &len
61ea0 29 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65 47  );.    gaiaFreeG
61eb0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20  eomColl (geo);. 
61ec0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
61ed0 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
61ee0 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
61ef0 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  free);.}..static
61f00 20 76 6f 69 64 0a 66 6e 63 74 5f 46 72 6f 6d 47   void.fnct_FromG
61f10 6d 6c 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ml (sqlite3_cont
61f20 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
61f30 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
61f40 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
61f50 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
61f60 6e 3a 0a 2f 20 47 65 6f 6d 46 72 6f 6d 47 6d 6c  n:./ GeomFromGml
61f70 28 47 4d 4c 20 65 6e 63 6f 64 65 64 20 67 65 6f  (GML encoded geo
61f80 6d 65 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72  metry)././ retur
61f90 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 67  ns the current g
61fa0 65 6f 6d 65 74 72 79 20 62 79 20 70 61 72 73 69  eometry by parsi
61fb0 6e 67 20 47 4d 4c 20 65 6e 63 6f 64 65 64 20 73  ng GML encoded s
61fc0 74 72 69 6e 67 20 0a 2f 20 6f 72 20 4e 55 4c 4c  tring ./ or NULL
61fd0 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
61fe0 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
61ff0 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
62000 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
62010 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
62020 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  .    const unsig
62030 6e 65 64 20 63 68 61 72 20 2a 74 65 78 74 3b 0a  ned char *text;.
62040 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
62050 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a  Ptr geo = NULL;.
62060 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c      sqlite3 *sql
62070 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ite = sqlite3_co
62080 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20  ntext_db_handle 
62090 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47  (context);.    G
620a0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
620b0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
620c0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
620d0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
620e0 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
620f0 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20 20 20  ITE_TEXT).      
62100 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
62110 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
62120 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
62130 20 20 20 20 20 7d 0a 20 20 20 20 74 65 78 74 20       }.    text 
62140 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
62150 74 65 78 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a  text (argv[0]);.
62160 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 50 61      geo = gaiaPa
62170 72 73 65 47 6d 6c 20 28 74 65 78 74 2c 20 73 71  rseGml (text, sq
62180 6c 69 74 65 29 3b 0a 20 20 20 20 69 66 20 28 67  lite);.    if (g
62190 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20  eo == NULL).    
621a0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
621b0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
621c0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
621d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
621e0 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  aToSpatiaLiteBlo
621f0 62 57 6b 62 20 28 67 65 6f 2c 20 26 70 5f 72 65  bWkb (geo, &p_re
62200 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20  sult, &len);.   
62210 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
62220 6c 20 28 67 65 6f 29 3b 0a 20 20 20 20 73 71 6c  l (geo);.    sql
62230 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
62240 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73   (context, p_res
62250 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b  ult, len, free);
62260 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
62270 66 6e 63 74 5f 4c 69 6e 65 73 46 72 6f 6d 52 69  fnct_LinesFromRi
62280 6e 67 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ngs (sqlite3_con
62290 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
622a0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
622b0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
622c0 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
622d0 6f 6e 3a 0a 2f 20 4c 69 6e 65 73 46 72 6f 6d 52  on:./ LinesFromR
622e0 69 6e 67 73 28 42 4c 4f 42 65 6e 63 6f 64 65 64  ings(BLOBencoded
622f0 20 67 65 6f 6d 65 74 72 79 2c 20 42 4f 4f 4c 20   geometry, BOOL 
62300 6d 75 6c 74 69 5f 6c 69 6e 65 73 74 72 69 6e 67  multi_linestring
62310 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20  )././ returns a 
62320 6e 65 77 20 67 65 6f 6d 65 74 72 79 20 5b 4c 49  new geometry [LI
62330 4e 45 53 54 52 49 4e 47 20 6f 72 20 4d 55 4c 54  NESTRING or MULT
62340 49 4c 49 4e 45 53 54 52 49 4e 47 5d 20 72 65 70  ILINESTRING] rep
62350 72 65 73 65 6e 74 69 6e 67 20 0a 2f 20 74 68 65  resenting ./ the
62360 20 6c 69 6e 65 61 72 69 7a 61 74 69 6f 6e 20 66   linearization f
62370 6f 72 20 63 75 72 72 65 6e 74 20 28 4d 55 4c 54  or current (MULT
62380 49 29 50 4f 4c 59 47 4f 4e 20 67 65 6f 6d 65 74  I)POLYGON geomet
62390 72 79 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  ry./ or NULL if 
623a0 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
623b0 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
623c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
623d0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
623e0 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61  _bytes;.    gaia
623f0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
62400 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
62410 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d  GeomCollPtr geom
62420 5f 6e 65 77 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  _new = NULL;.   
62430 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 69 6e   int len;.    in
62440 74 20 6d 75 6c 74 69 5f 6c 69 6e 65 73 74 72 69  t multi_linestri
62450 6e 67 20 3d 20 30 3b 0a 20 20 20 20 75 6e 73 69  ng = 0;.    unsi
62460 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
62470 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ult = NULL;.    
62480 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
62490 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
624a0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
624b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
624c0 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
624d0 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
624e0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
624f0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
62500 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
62510 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
62520 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
62530 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
62540 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
62550 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
62560 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
62570 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
62580 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61  ;.    geo = gaia
62590 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
625a0 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
625b0 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
625c0 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20  (geo == NULL).  
625d0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
625e0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
625f0 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
62600 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
62610 66 20 28 61 72 67 63 20 3d 3d 20 32 29 0a 20 20  f (argc == 2).  
62620 20 20 20 20 7b 0a 09 20 20 69 66 20 28 73 71 6c      {..  if (sql
62630 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
62640 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
62650 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 09 20 20  ITE_INTEGER)..  
62660 20 20 20 20 6d 75 6c 74 69 5f 6c 69 6e 65 73 74      multi_linest
62670 72 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 5f 76  ring = sqlite3_v
62680 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31  alue_int (argv[1
62690 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
626a0 67 65 6f 6d 5f 6e 65 77 20 3d 20 67 61 69 61 4c  geom_new = gaiaL
626b0 69 6e 65 61 72 69 7a 65 20 28 67 65 6f 2c 20 6d  inearize (geo, m
626c0 75 6c 74 69 5f 6c 69 6e 65 73 74 72 69 6e 67 29  ulti_linestring)
626d0 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 6d 5f  ;.    if (!geom_
626e0 6e 65 77 29 0a 09 67 6f 74 6f 20 69 6e 76 61 6c  new)..goto inval
626f0 69 64 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65  id;.    gaiaFree
62700 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a  GeomColl (geo);.
62710 20 20 20 20 67 61 69 61 54 6f 53 70 61 74 69 61      gaiaToSpatia
62720 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f  LiteBlobWkb (geo
62730 6d 5f 6e 65 77 2c 20 26 70 5f 72 65 73 75 6c 74  m_new, &p_result
62740 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20 67 61 69  , &len);.    gai
62750 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
62760 65 6f 6d 5f 6e 65 77 29 3b 0a 20 20 20 20 73 71  eom_new);.    sq
62770 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
62780 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
62790 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29  sult, len, free)
627a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
627b0 69 6e 76 61 6c 69 64 3a 0a 20 20 20 20 69 66 20  invalid:.    if 
627c0 28 67 65 6f 29 0a 09 67 61 69 61 46 72 65 65 47  (geo)..gaiaFreeG
627d0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20  eomColl (geo);. 
627e0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
627f0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
62800 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4f 4d 49  ;.}..#ifndef OMI
62810 54 5f 47 45 4f 53 09 09 2f 2a 20 69 6e 63 6c 75  T_GEOS../* inclu
62820 64 69 6e 67 20 47 45 4f 53 20 2a 2f 0a 0a 73 74  ding GEOS */..st
62830 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 42  atic void.fnct_B
62840 75 69 6c 64 41 72 65 61 20 28 73 71 6c 69 74 65  uildArea (sqlite
62850 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
62860 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
62870 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
62880 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
62890 75 6e 63 74 69 6f 6e 3a 0a 2f 20 42 75 69 6c 64  unction:./ Build
628a0 41 72 65 61 28 42 4c 4f 42 65 6e 63 6f 64 65 64  Area(BLOBencoded
628b0 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 41   geometry)././ A
628c0 73 73 75 6d 69 6e 67 20 74 68 61 74 20 47 65 6f  ssuming that Geo
628d0 6d 65 74 72 79 20 72 65 70 72 65 73 65 6e 74 73  metry represents
628e0 20 61 20 73 65 74 20 6f 66 20 73 70 61 72 73 65   a set of sparse
628f0 20 4c 69 6e 65 73 74 72 69 6e 67 73 2c 0a 2f 20   Linestrings,./ 
62900 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69  this function wi
62910 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  ll attempt to re
62920 61 73 73 65 6d 62 6c 65 20 61 20 73 69 6e 67 6c  assemble a singl
62930 65 20 50 6f 6c 79 67 6f 6e 0a 2f 20 28 6f 72 20  e Polygon./ (or 
62940 61 20 73 65 74 20 6f 66 20 50 6f 6c 79 67 6f 6e  a set of Polygon
62950 73 29 0a 2f 20 4e 55 4c 4c 20 69 73 20 72 65 74  s)./ NULL is ret
62960 75 72 6e 65 64 20 66 6f 72 20 69 6e 76 61 6c 69  urned for invali
62970 64 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 20  d arguments.*/. 
62980 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
62990 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e   *p_blob;.    in
629a0 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 67  t n_bytes;.    g
629b0 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
629c0 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  eo = NULL;.    g
629d0 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72  aiaGeomCollPtr r
629e0 65 73 75 6c 74 3b 0a 20 20 20 20 47 41 49 41 5f  esult;.    GAIA_
629f0 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
62a00 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
62a10 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
62a20 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
62a30 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
62a40 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
62a50 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
62a60 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
62a70 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
62a80 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
62a90 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
62aa0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
62ab0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
62ac0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
62ad0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
62ae0 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
62af0 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
62b00 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
62b10 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
62b20 73 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 20  s);.    if (geo 
62b30 3d 3d 20 4e 55 4c 4c 29 0a 09 73 71 6c 69 74 65  == NULL)..sqlite
62b40 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
62b50 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73  ontext);.    els
62b60 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65 73  e.      {..  res
62b70 75 6c 74 20 3d 20 67 61 69 61 50 6f 6c 79 67 6f  ult = gaiaPolygo
62b80 6e 69 7a 65 20 28 67 65 6f 2c 20 30 29 3b 0a 09  nize (geo, 0);..
62b90 20 20 69 66 20 28 72 65 73 75 6c 74 20 3d 3d 20    if (result == 
62ba0 4e 55 4c 4c 29 0a 09 20 20 20 20 20 20 73 71 6c  NULL)..      sql
62bb0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
62bc0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65   (context);..  e
62bd0 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20  lse..    {.../* 
62be0 62 75 69 6c 64 73 20 74 68 65 20 42 4c 4f 42 20  builds the BLOB 
62bf0 67 65 6f 6d 65 74 72 79 20 74 6f 20 62 65 20 72  geometry to be r
62c00 65 74 75 72 6e 65 64 20 2a 2f 0a 09 09 69 6e 74  eturned */...int
62c10 20 6c 65 6e 3b 0a 09 09 75 6e 73 69 67 6e 65 64   len;...unsigned
62c20 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
62c30 3d 20 4e 55 4c 4c 3b 0a 09 09 72 65 73 75 6c 74  = NULL;...result
62c40 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72  ->Srid = geo->Sr
62c50 69 64 3b 0a 09 09 67 61 69 61 54 6f 53 70 61 74  id;...gaiaToSpat
62c60 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72  iaLiteBlobWkb (r
62c70 65 73 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74  esult, &p_result
62c80 2c 20 26 6c 65 6e 29 3b 0a 09 09 73 71 6c 69 74  , &len);...sqlit
62c90 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28  e3_result_blob (
62ca0 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c  context, p_resul
62cb0 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09  t, len, free);..
62cc0 09 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c  .gaiaFreeGeomCol
62cd0 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09 20 20 20  l (result);..   
62ce0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67   }.      }.    g
62cf0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
62d00 28 67 65 6f 29 3b 0a 7d 0a 0a 0a 73 74 61 74 69  (geo);.}...stati
62d10 63 20 76 6f 69 64 0a 66 6e 63 74 5f 50 6f 6c 79  c void.fnct_Poly
62d20 67 6f 6e 69 7a 65 5f 73 74 65 70 20 28 73 71 6c  gonize_step (sql
62d30 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
62d40 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
62d50 2c 0a 09 09 20 20 20 20 20 20 73 71 6c 69 74 65  ,...      sqlite
62d60 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
62d70 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
62d80 6f 6e 3a 0a 2f 20 50 6f 6c 79 67 6f 6e 69 7a 65  on:./ Polygonize
62d90 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f  (BLOBencoded geo
62da0 6d 29 0a 2f 0a 2f 20 61 67 67 72 65 67 61 74 65  m)././ aggregate
62db0 20 66 75 6e 63 74 69 6f 6e 20 2d 20 53 54 45 50   function - STEP
62dc0 0a 2f 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ./.*/.    unsign
62dd0 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
62de0 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
62df0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
62e00 6c 6c 50 74 72 20 67 65 6f 6d 3b 0a 20 20 20 20  llPtr geom;.    
62e10 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
62e20 72 65 73 75 6c 74 3b 0a 20 20 20 20 67 61 69 61  result;.    gaia
62e30 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 2a 70 3b 0a  GeomCollPtr *p;.
62e40 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
62e50 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
62e60 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
62e70 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
62e80 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
62e90 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
62ea0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
62eb0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
62ec0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
62ed0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
62ee0 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
62ef0 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
62f00 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
62f10 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[0]);.    n_b
62f20 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
62f30 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
62f40 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 6d 20 3d  [0]);.    geom =
62f50 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
62f60 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
62f70 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
62f80 20 20 69 66 20 28 21 67 65 6f 6d 29 0a 09 72 65    if (!geom)..re
62f90 74 75 72 6e 3b 0a 20 20 20 20 70 20 3d 20 73 71  turn;.    p = sq
62fa0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
62fb0 63 6f 6e 74 65 78 74 20 28 63 6f 6e 74 65 78 74  context (context
62fc0 2c 20 73 69 7a 65 6f 66 20 28 67 61 69 61 47 65  , sizeof (gaiaGe
62fd0 6f 6d 43 6f 6c 6c 50 74 72 29 29 3b 0a 20 20 20  omCollPtr));.   
62fe0 20 69 66 20 28 21 28 2a 70 29 29 0a 20 20 20 20   if (!(*p)).    
62ff0 20 20 7b 0a 09 20 20 2f 2a 20 74 68 69 73 20 69    {..  /* this i
63000 73 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 20  s the first row 
63010 2a 2f 0a 09 20 20 2a 70 20 3d 20 67 65 6f 6d 3b  */..  *p = geom;
63020 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
63030 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20  e.      {..  /* 
63040 73 75 62 73 65 71 75 65 6e 74 20 72 6f 77 73 20  subsequent rows 
63050 2a 2f 0a 09 20 20 72 65 73 75 6c 74 20 3d 20 67  */..  result = g
63060 61 69 61 4d 65 72 67 65 47 65 6f 6d 65 74 72 69  aiaMergeGeometri
63070 65 73 20 28 2a 70 2c 20 67 65 6f 6d 29 3b 0a 09  es (*p, geom);..
63080 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
63090 6c 6c 20 28 2a 70 29 3b 0a 09 20 20 2a 70 20 3d  ll (*p);..  *p =
630a0 20 72 65 73 75 6c 74 3b 0a 09 20 20 67 61 69 61   result;..  gaia
630b0 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
630c0 6f 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a 0a  om);.      }.}..
630d0 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
630e0 5f 50 6f 6c 79 67 6f 6e 69 7a 65 5f 66 69 6e 61  _Polygonize_fina
630f0 6c 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l (sqlite3_conte
63100 78 74 20 2a 20 63 6f 6e 74 65 78 74 29 0a 7b 0a  xt * context).{.
63110 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
63120 0a 2f 20 50 6f 6c 79 67 6f 6e 69 7a 65 28 42 4c  ./ Polygonize(BL
63130 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 29 0a  OBencoded geom).
63140 2f 0a 2f 20 61 67 67 72 65 67 61 74 65 20 66 75  /./ aggregate fu
63150 6e 63 74 69 6f 6e 20 2d 20 46 49 4e 41 4c 0a 2f  nction - FINAL./
63160 0a 2a 2f 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  .*/.    gaiaGeom
63170 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a  CollPtr result;.
63180 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
63190 50 74 72 20 67 65 6f 6d 3b 0a 20 20 20 20 67 61  Ptr geom;.    ga
631a0 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 2a 70  iaGeomCollPtr *p
631b0 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
631c0 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 28 63 6f  gate_context (co
631d0 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 69  ntext, 0);.    i
631e0 66 20 28 21 70 29 0a 20 20 20 20 20 20 7b 0a 09  f (!p).      {..
631f0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
63200 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
63210 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
63220 20 20 7d 0a 20 20 20 20 72 65 73 75 6c 74 20 3d    }.    result =
63230 20 2a 70 3b 0a 20 20 20 20 69 66 20 28 21 72 65   *p;.    if (!re
63240 73 75 6c 74 29 0a 09 73 71 6c 69 74 65 33 5f 72  sult)..sqlite3_r
63250 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
63260 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20  ext);.    else. 
63270 20 20 20 20 20 7b 0a 09 20 20 67 65 6f 6d 20 3d       {..  geom =
63280 20 67 61 69 61 50 6f 6c 79 67 6f 6e 69 7a 65 20   gaiaPolygonize 
63290 28 72 65 73 75 6c 74 2c 20 30 29 3b 0a 09 20 20  (result, 0);..  
632a0 69 66 20 28 67 65 6f 6d 20 3d 3d 20 4e 55 4c 4c  if (geom == NULL
632b0 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  )..      sqlite3
632c0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
632d0 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a  ntext);..  else.
632e0 09 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75 69 6c  .    {.../* buil
632f0 64 73 20 74 68 65 20 42 4c 4f 42 20 67 65 6f 6d  ds the BLOB geom
63300 65 74 72 79 20 74 6f 20 62 65 20 72 65 74 75 72  etry to be retur
63310 6e 65 64 20 2a 2f 0a 09 09 69 6e 74 20 6c 65 6e  ned */...int len
63320 3b 0a 09 09 75 6e 73 69 67 6e 65 64 20 63 68 61  ;...unsigned cha
63330 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55  r *p_result = NU
63340 4c 4c 3b 0a 09 09 67 65 6f 6d 2d 3e 53 72 69 64  LL;...geom->Srid
63350 20 3d 20 72 65 73 75 6c 74 2d 3e 53 72 69 64 3b   = result->Srid;
63360 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69 61 4c  ...gaiaToSpatiaL
63370 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 6d  iteBlobWkb (geom
63380 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65  , &p_result, &le
63390 6e 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  n);...sqlite3_re
633a0 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
633b0 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
633c0 6e 2c 20 66 72 65 65 29 3b 0a 09 09 67 61 69 61  n, free);...gaia
633d0 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
633e0 6f 6d 29 3b 0a 09 20 20 20 20 7d 0a 09 20 20 67  om);..    }..  g
633f0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
63400 28 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20  (result);.      
63410 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 65  }.}..#endif /* e
63420 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 47 45 4f  nd including GEO
63430 53 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69  S */..static voi
63440 64 0a 66 6e 63 74 5f 44 69 73 73 6f 6c 76 65 53  d.fnct_DissolveS
63450 65 67 6d 65 6e 74 73 20 28 73 71 6c 69 74 65 33  egments (sqlite3
63460 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
63470 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09  xt, int argc,...
63480 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
63490 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
634a0 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
634b0 0a 2f 20 44 69 73 73 6f 6c 76 65 53 65 67 6d 65  ./ DissolveSegme
634c0 6e 74 73 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20  nts(BLOBencoded 
634d0 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 44 69  geometry)././ Di
634e0 73 73 6f 6c 76 65 73 20 61 6e 79 20 4c 49 4e 45  ssolves any LINE
634f0 53 54 52 49 4e 47 20 6f 72 20 52 49 4e 47 20 69  STRING or RING i
63500 6e 74 6f 20 65 6c 65 6d 65 6e 74 61 72 79 20 73  nto elementary s
63510 65 67 6d 65 6e 74 73 0a 2f 20 4e 55 4c 4c 20 69  egments./ NULL i
63520 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 69  s returned for i
63530 6e 76 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 73  nvalid arguments
63540 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  .*/.    unsigned
63550 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20   char *p_blob;. 
63560 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a     int n_bytes;.
63570 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
63580 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a  Ptr geo = NULL;.
63590 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
635a0 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20 20  Ptr result;.    
635b0 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
635c0 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
635d0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
635e0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
635f0 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
63600 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
63610 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
63620 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
63630 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
63640 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
63650 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
63660 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
63670 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
63680 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
63690 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
636a0 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
636b0 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61  ;.    geo = gaia
636c0 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
636d0 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
636e0 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
636f0 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 09 73  (geo == NULL)..s
63700 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
63710 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ll (context);.  
63720 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
63730 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 44    result = gaiaD
63740 69 73 73 6f 6c 76 65 53 65 67 6d 65 6e 74 73 20  issolveSegments 
63750 28 67 65 6f 29 3b 0a 09 20 20 69 66 20 28 72 65  (geo);..  if (re
63760 73 75 6c 74 20 3d 3d 20 4e 55 4c 4c 29 0a 09 20  sult == NULL).. 
63770 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
63780 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
63790 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20  t);..  else..   
637a0 20 7b 0a 09 09 2f 2a 20 62 75 69 6c 64 73 20 74   {.../* builds t
637b0 68 65 20 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79  he BLOB geometry
637c0 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
637d0 2a 2f 0a 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09  */...int len;...
637e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
637f0 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a  _result = NULL;.
63800 09 09 72 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d  ..result->Srid =
63810 20 67 65 6f 2d 3e 53 72 69 64 3b 0a 09 09 67 61   geo->Srid;...ga
63820 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c  iaToSpatiaLiteBl
63830 6f 62 57 6b 62 20 28 72 65 73 75 6c 74 2c 20 26  obWkb (result, &
63840 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b  p_result, &len);
63850 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ...sqlite3_resul
63860 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
63870 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
63880 66 72 65 65 29 3b 0a 09 09 67 61 69 61 46 72 65  free);...gaiaFre
63890 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c  eGeomColl (resul
638a0 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20  t);..    }.     
638b0 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47   }.    gaiaFreeG
638c0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d  eomColl (geo);.}
638d0 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
638e0 63 74 5f 44 69 73 73 6f 6c 76 65 50 6f 69 6e 74  ct_DissolvePoint
638f0 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  s (sqlite3_conte
63900 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
63910 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
63920 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
63930 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
63940 3a 0a 2f 20 44 69 73 73 6f 6c 76 65 50 6f 69 6e  :./ DissolvePoin
63950 74 73 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67  ts(BLOBencoded g
63960 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f 20 44 69 73  eometry)././ Dis
63970 73 6f 6c 76 65 73 20 61 6e 79 20 4c 49 4e 45 53  solves any LINES
63980 54 52 49 4e 47 20 6f 72 20 52 49 4e 47 20 69 6e  TRING or RING in
63990 74 6f 20 65 6c 65 6d 65 6e 74 61 72 79 20 56 65  to elementary Ve
639a0 72 74 69 63 65 73 0a 2f 20 4e 55 4c 4c 20 69 73  rtices./ NULL is
639b0 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 69 6e   returned for in
639c0 76 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 73 0a  valid arguments.
639d0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
639e0 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
639f0 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
63a00 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
63a10 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20  tr geo = NULL;. 
63a20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
63a30 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 47  tr result;.    G
63a40 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
63a50 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
63a60 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
63a70 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
63a80 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
63a90 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
63aa0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
63ab0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
63ac0 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
63ad0 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f       }.    p_blo
63ae0 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
63af0 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
63b00 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30  lue_blob (argv[0
63b10 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20  ]);.    n_bytes 
63b20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
63b30 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b  bytes (argv[0]);
63b40 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46  .    geo = gaiaF
63b50 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  romSpatiaLiteBlo
63b60 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f  bWkb (p_blob, n_
63b70 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28  bytes);.    if (
63b80 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 09 73 71  geo == NULL)..sq
63b90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
63ba0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
63bb0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
63bc0 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 44 69   result = gaiaDi
63bd0 73 73 6f 6c 76 65 50 6f 69 6e 74 73 20 28 67 65  ssolvePoints (ge
63be0 6f 29 3b 0a 09 20 20 69 66 20 28 72 65 73 75 6c  o);..  if (resul
63bf0 74 20 3d 3d 20 4e 55 4c 4c 29 0a 09 20 20 20 20  t == NULL)..    
63c00 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
63c10 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
63c20 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
63c30 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20  ../* builds the 
63c40 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f  BLOB geometry to
63c50 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a   be returned */.
63c60 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73  ..int len;...uns
63c70 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
63c80 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72  sult = NULL;...r
63c90 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65  esult->Srid = ge
63ca0 6f 2d 3e 53 72 69 64 3b 0a 09 09 67 61 69 61 54  o->Srid;...gaiaT
63cb0 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  oSpatiaLiteBlobW
63cc0 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72  kb (result, &p_r
63cd0 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09  esult, &len);...
63ce0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
63cf0 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
63d00 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
63d10 65 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65  e);...gaiaFreeGe
63d20 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b  omColl (result);
63d30 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a  ..    }.      }.
63d40 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
63d50 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73  Coll (geo);.}..s
63d60 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
63d70 43 6f 6c 6c 65 63 74 69 6f 6e 45 78 74 72 61 63  CollectionExtrac
63d80 74 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t (sqlite3_conte
63d90 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
63da0 74 20 61 72 67 63 2c 0a 09 09 09 73 71 6c 69 74  t argc,....sqlit
63db0 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
63dc0 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
63dd0 69 6f 6e 3a 0a 2f 20 43 6f 6c 6c 65 63 74 69 6f  ion:./ Collectio
63de0 6e 45 78 74 72 61 63 74 28 42 4c 4f 42 65 6e 63  nExtract(BLOBenc
63df0 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c 20 49  oded geometry, I
63e00 6e 74 65 67 65 72 20 74 79 70 65 29 0a 2f 0a 2f  nteger type)././
63e10 20 45 78 74 72 61 63 74 73 20 66 72 6f 6d 20 61   Extracts from a
63e20 20 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54   GEOMETRYCOLLECT
63e30 49 4f 4e 20 61 6e 79 20 69 74 65 6d 20 6f 66 20  ION any item of 
63e40 74 68 65 20 72 65 71 75 69 72 65 64 20 54 59 50  the required TYP
63e50 45 0a 2f 20 31 3d 50 6f 69 6e 74 20 2d 20 32 3d  E./ 1=Point - 2=
63e60 4c 69 6e 65 73 74 72 69 6e 67 20 2d 20 33 3d 50  Linestring - 3=P
63e70 6f 6c 79 67 6f 6e 0a 2f 20 4e 55 4c 4c 20 69 73  olygon./ NULL is
63e80 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 69 6e   returned for in
63e90 76 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 73 0a  valid arguments.
63ea0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
63eb0 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
63ec0 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
63ed0 20 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 20     int type;.   
63ee0 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
63ef0 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
63f00 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
63f10 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 47 41 49   result;.    GAI
63f20 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
63f30 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
63f40 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
63f50 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
63f60 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[0]) != SQLIT
63f70 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a  E_BLOB).      {.
63f80 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
63f90 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
63fa0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
63fb0 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c     }.    if (sql
63fc0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
63fd0 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
63fe0 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 09 74 79  ITE_INTEGER)..ty
63ff0 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pe = sqlite3_val
64000 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
64010 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
64020 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
64030 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
64040 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
64050 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
64060 74 79 70 65 20 3d 3d 20 31 20 7c 7c 20 74 79 70  type == 1 || typ
64070 65 20 3d 3d 20 32 20 7c 7c 20 74 79 70 65 20 3d  e == 2 || type =
64080 3d 20 33 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65  = 3)..;.    else
64090 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
640a0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
640b0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
640c0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
640d0 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
640e0 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
640f0 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
64100 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
64110 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
64120 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
64130 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20  gv[0]);.    geo 
64140 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
64150 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
64160 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
64170 20 20 20 69 66 20 28 67 65 6f 20 3d 3d 20 4e 55     if (geo == NU
64180 4c 4c 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  LL)..sqlite3_res
64190 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
641a0 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  t);.    else.   
641b0 20 20 20 7b 0a 09 20 20 73 77 69 74 63 68 20 28     {..  switch (
641c0 74 79 70 65 29 0a 09 20 20 20 20 7b 0a 09 20 20  type)..    {..  
641d0 20 20 63 61 73 65 20 31 3a 0a 09 09 72 65 73 75    case 1:...resu
641e0 6c 74 20 3d 20 67 61 69 61 45 78 74 72 61 63 74  lt = gaiaExtract
641f0 50 6f 69 6e 74 73 46 72 6f 6d 47 65 6f 6d 43 6f  PointsFromGeomCo
64200 6c 6c 20 28 67 65 6f 29 3b 0a 09 09 62 72 65 61  ll (geo);...brea
64210 6b 3b 0a 09 20 20 20 20 63 61 73 65 20 32 3a 0a  k;..    case 2:.
64220 09 09 72 65 73 75 6c 74 20 3d 20 67 61 69 61 45  ..result = gaiaE
64230 78 74 72 61 63 74 4c 69 6e 65 73 74 72 69 6e 67  xtractLinestring
64240 73 46 72 6f 6d 47 65 6f 6d 43 6f 6c 6c 20 28 67  sFromGeomColl (g
64250 65 6f 29 3b 0a 09 09 62 72 65 61 6b 3b 0a 09 20  eo);...break;.. 
64260 20 20 20 63 61 73 65 20 33 3a 0a 09 09 72 65 73     case 3:...res
64270 75 6c 74 20 3d 20 67 61 69 61 45 78 74 72 61 63  ult = gaiaExtrac
64280 74 50 6f 6c 79 67 6f 6e 73 46 72 6f 6d 47 65 6f  tPolygonsFromGeo
64290 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09 09 62  mColl (geo);...b
642a0 72 65 61 6b 3b 0a 09 20 20 20 20 7d 3b 0a 09 20  reak;..    };.. 
642b0 20 69 66 20 28 72 65 73 75 6c 74 20 3d 3d 20 4e   if (result == N
642c0 55 4c 4c 29 0a 09 20 20 20 20 20 20 73 71 6c 69  ULL)..      sqli
642d0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
642e0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c  (context);..  el
642f0 73 65 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 62  se..    {.../* b
64300 75 69 6c 64 73 20 74 68 65 20 42 4c 4f 42 20 67  uilds the BLOB g
64310 65 6f 6d 65 74 72 79 20 74 6f 20 62 65 20 72 65  eometry to be re
64320 74 75 72 6e 65 64 20 2a 2f 0a 09 09 69 6e 74 20  turned */...int 
64330 6c 65 6e 3b 0a 09 09 75 6e 73 69 67 6e 65 64 20  len;...unsigned 
64340 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d  char *p_result =
64350 20 4e 55 4c 4c 3b 0a 09 09 72 65 73 75 6c 74 2d   NULL;...result-
64360 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69  >Srid = geo->Sri
64370 64 3b 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69  d;...gaiaToSpati
64380 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72 65  aLiteBlobWkb (re
64390 73 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c  sult, &p_result,
643a0 20 26 6c 65 6e 29 3b 0a 09 09 73 71 6c 69 74 65   &len);...sqlite
643b0 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63  3_result_blob (c
643c0 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74  ontext, p_result
643d0 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09  , len, free);...
643e0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
643f0 20 28 72 65 73 75 6c 74 29 3b 0a 09 20 20 20 20   (result);..    
64400 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  }.      }.    ga
64410 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
64420 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  geo);.}..static 
64430 76 6f 69 64 0a 66 6e 63 74 5f 4c 6f 63 61 74 65  void.fnct_Locate
64440 42 65 74 77 65 65 6e 4d 65 61 73 75 72 65 73 20  BetweenMeasures 
64450 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
64460 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
64470 61 72 67 63 2c 0a 09 09 09 20 20 20 20 73 71 6c  argc,....    sql
64480 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
64490 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
644a0 63 74 69 6f 6e 73 3a 0a 2f 20 53 54 5f 4c 6f 63  ctions:./ ST_Loc
644b0 61 74 65 5f 41 6c 6f 6e 67 5f 4d 65 61 73 75 72  ate_Along_Measur
644c0 65 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65  e(BLOBencoded ge
644d0 6f 6d 65 74 72 79 2c 20 44 6f 75 62 6c 65 20 6d  ometry, Double m
644e0 5f 76 61 6c 75 65 29 0a 2f 20 53 54 5f 4c 6f 63  _value)./ ST_Loc
644f0 61 74 65 5f 42 65 74 77 65 65 6e 5f 4d 65 61 73  ate_Between_Meas
64500 75 72 65 73 28 42 4c 4f 42 65 6e 63 6f 64 65 64  ures(BLOBencoded
64510 20 67 65 6f 6d 65 74 72 79 2c 20 44 6f 75 62 6c   geometry, Doubl
64520 65 20 6d 5f 73 74 61 72 74 2c 20 44 6f 75 62 6c  e m_start, Doubl
64530 65 20 6d 5f 65 6e 64 29 0a 2f 0a 2f 20 45 78 74  e m_end)././ Ext
64540 72 61 63 74 73 20 66 72 6f 6d 20 61 20 47 45 4f  racts from a GEO
64550 4d 45 54 52 59 20 28 73 75 70 70 6f 72 74 69 6e  METRY (supportin
64560 67 20 4d 29 20 61 6e 79 20 50 6f 69 6e 74 2f 4c  g M) any Point/L
64570 69 6e 65 73 74 72 69 6e 67 0a 2f 20 6d 61 74 63  inestring./ matc
64580 68 69 6e 67 20 74 68 65 20 72 61 6e 67 65 20 6f  hing the range o
64590 66 20 6d 65 61 73 75 72 65 73 0a 2f 20 4e 55 4c  f measures./ NUL
645a0 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 66 6f  L is returned fo
645b0 72 20 69 6e 76 61 6c 69 64 20 61 72 67 75 6d 65  r invalid argume
645c0 6e 74 73 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67  nts.*/.    unsig
645d0 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62  ned char *p_blob
645e0 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65  ;.    int n_byte
645f0 73 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d 5f  s;.    double m_
64600 73 74 61 72 74 3b 0a 20 20 20 20 64 6f 75 62 6c  start;.    doubl
64610 65 20 6d 5f 65 6e 64 3b 0a 20 20 20 20 69 6e 74  e m_end;.    int
64620 20 69 6e 74 76 61 6c 3b 0a 20 20 20 20 67 61 69   intval;.    gai
64630 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
64640 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
64650 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73  aGeomCollPtr res
64660 75 6c 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ult;.    GAIA_UN
64670 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
64680 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
64690 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
646a0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
646b0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
646c0 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
646d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
646e0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
646f0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
64700 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
64710 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
64720 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[1]) == SQLITE_
64730 46 4c 4f 41 54 29 0a 09 6d 5f 73 74 61 72 74 20  FLOAT)..m_start 
64740 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
64750 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29  double (argv[1])
64760 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73  ;.    else if (s
64770 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
64780 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53  e (argv[1]) == S
64790 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
647a0 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 76 61 6c       {..  intval
647b0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
647c0 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a  _int (argv[1]);.
647d0 09 20 20 6d 5f 73 74 61 72 74 20 3d 20 69 6e 74  .  m_start = int
647e0 76 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  val;.      }.   
647f0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
64800 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
64810 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
64820 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
64830 20 7d 0a 20 20 20 20 69 66 20 28 61 72 67 63 20   }.    if (argc 
64840 3e 20 32 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  > 2).      {..  
64850 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
64860 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29  e_type (argv[2])
64870 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54   == SQLITE_FLOAT
64880 29 0a 09 20 20 20 20 20 20 6d 5f 65 6e 64 20 3d  )..      m_end =
64890 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
648a0 6f 75 62 6c 65 20 28 61 72 67 76 5b 32 5d 29 3b  ouble (argv[2]);
648b0 0a 09 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c  ..  else if (sql
648c0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
648d0 28 61 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c  (argv[2]) == SQL
648e0 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 09 20 20  ITE_INTEGER)..  
648f0 20 20 7b 0a 09 09 69 6e 74 76 61 6c 20 3d 20 73    {...intval = s
64900 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
64910 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 09 6d 5f   (argv[2]);...m_
64920 65 6e 64 20 3d 20 69 6e 74 76 61 6c 3b 0a 09 20  end = intval;.. 
64930 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20     }..  else..  
64940 20 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65    {...sqlite3_re
64950 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
64960 78 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09  xt);...return;..
64970 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
64980 20 20 65 6c 73 65 0a 09 6d 5f 65 6e 64 20 3d 20    else..m_end = 
64990 6d 5f 73 74 61 72 74 3b 0a 20 20 20 20 70 5f 62  m_start;.    p_b
649a0 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  lob = (unsigned 
649b0 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
649c0 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76  value_blob (argv
649d0 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65  [0]);.    n_byte
649e0 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s = sqlite3_valu
649f0 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d  e_bytes (argv[0]
64a00 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69  );.    geo = gai
64a10 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42  aFromSpatiaLiteB
64a20 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20  lobWkb (p_blob, 
64a30 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66  n_bytes);.    if
64a40 20 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 09   (geo == NULL)..
64a50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
64a60 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
64a70 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
64a80 09 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61  .  result = gaia
64a90 4c 6f 63 61 74 65 42 65 74 77 65 65 6e 4d 65 61  LocateBetweenMea
64aa0 73 75 72 65 73 20 28 67 65 6f 2c 20 6d 5f 73 74  sures (geo, m_st
64ab0 61 72 74 2c 20 6d 5f 65 6e 64 29 3b 0a 09 20 20  art, m_end);..  
64ac0 69 66 20 28 72 65 73 75 6c 74 20 3d 3d 20 4e 55  if (result == NU
64ad0 4c 4c 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74  LL)..      sqlit
64ae0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
64af0 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73  context);..  els
64b00 65 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75  e..    {.../* bu
64b10 69 6c 64 73 20 74 68 65 20 42 4c 4f 42 20 67 65  ilds the BLOB ge
64b20 6f 6d 65 74 72 79 20 74 6f 20 62 65 20 72 65 74  ometry to be ret
64b30 75 72 6e 65 64 20 2a 2f 0a 09 09 69 6e 74 20 6c  urned */...int l
64b40 65 6e 3b 0a 09 09 75 6e 73 69 67 6e 65 64 20 63  en;...unsigned c
64b50 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20  har *p_result = 
64b60 4e 55 4c 4c 3b 0a 09 09 72 65 73 75 6c 74 2d 3e  NULL;...result->
64b70 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64  Srid = geo->Srid
64b80 3b 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69 61  ;...gaiaToSpatia
64b90 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72 65 73  LiteBlobWkb (res
64ba0 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  ult, &p_result, 
64bb0 26 6c 65 6e 29 3b 0a 09 09 73 71 6c 69 74 65 33  &len);...sqlite3
64bc0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
64bd0 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
64be0 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09 67   len, free);...g
64bf0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
64c00 28 72 65 73 75 6c 74 29 3b 0a 09 20 20 20 20 7d  (result);..    }
64c10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
64c20 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
64c30 65 6f 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  eo);.}..#ifndef 
64c40 4f 4d 49 54 5f 50 52 4f 4a 09 09 2f 2a 20 69 6e  OMIT_PROJ../* in
64c50 63 6c 75 64 69 6e 67 20 50 52 4f 4a 2e 34 20 2a  cluding PROJ.4 *
64c60 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  /..static void.f
64c70 6e 63 74 5f 54 72 61 6e 73 66 6f 72 6d 20 28 73  nct_Transform (s
64c80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
64c90 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
64ca0 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
64cb0 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
64cc0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
64cd0 54 72 61 6e 73 66 6f 72 6d 28 42 4c 4f 42 65 6e  Transform(BLOBen
64ce0 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c 20  coded geometry, 
64cf0 73 72 69 64 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  srid)././ return
64d00 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74 72 79  s a new geometry
64d10 20 74 68 61 74 20 69 73 20 74 68 65 20 6f 72 69   that is the ori
64d20 67 69 6e 61 6c 20 6f 6e 65 20 72 65 63 65 69 76  ginal one receiv
64d30 65 64 2c 20 62 75 74 20 77 69 74 68 20 74 68 65  ed, but with the
64d40 20 6e 65 77 20 53 52 49 44 20 5b 6e 6f 20 63 6f   new SRID [no co
64d50 6f 72 64 69 6e 61 74 65 73 20 74 72 61 6e 73 6c  ordinates transl
64d60 61 74 69 6f 6e 20 69 73 20 61 70 70 6c 69 65 64  ation is applied
64d70 5d 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61  ]./ or NULL if a
64d80 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
64d90 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75  untered.*/.    u
64da0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
64db0 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
64dc0 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47  bytes;.    gaiaG
64dd0 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
64de0 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47   NULL;.    gaiaG
64df0 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c  eomCollPtr resul
64e00 74 3b 0a 20 20 20 20 69 6e 74 20 73 72 69 64 5f  t;.    int srid_
64e10 66 72 6f 6d 3b 0a 20 20 20 20 69 6e 74 20 73 72  from;.    int sr
64e20 69 64 5f 74 6f 3b 0a 20 20 20 20 63 68 61 72 20  id_to;.    char 
64e30 70 72 6f 6a 5f 66 72 6f 6d 5b 32 30 34 38 5d 3b  proj_from[2048];
64e40 0a 20 20 20 20 63 68 61 72 20 70 72 6f 6a 5f 74  .    char proj_t
64e50 6f 5b 32 30 34 38 5d 3b 0a 20 20 20 20 73 71 6c  o[2048];.    sql
64e60 69 74 65 33 20 2a 73 71 6c 69 74 65 20 3d 20 73  ite3 *sqlite = s
64e70 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
64e80 62 5f 68 61 6e 64 6c 65 20 28 63 6f 6e 74 65 78  b_handle (contex
64e90 74 29 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  t);.    GAIA_UNU
64ea0 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
64eb0 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
64ec0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
64ed0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
64ee0 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
64ef0 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
64f00 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
64f10 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
64f20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
64f30 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
64f40 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
64f50 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [1]) == SQLITE_I
64f60 4e 54 45 47 45 52 29 0a 09 73 72 69 64 5f 74 6f  NTEGER)..srid_to
64f70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
64f80 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a  _int (argv[1]);.
64f90 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
64fa0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
64fb0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
64fc0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
64fd0 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
64fe0 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
64ff0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
65000 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
65010 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
65020 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
65030 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
65040 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
65050 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
65060 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
65070 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
65080 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  geo)..sqlite3_re
65090 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
650a0 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  xt);.    else.  
650b0 20 20 20 20 7b 0a 09 20 20 2a 70 72 6f 6a 5f 66      {..  *proj_f
650c0 72 6f 6d 20 3d 20 27 5c 30 27 3b 0a 09 20 20 2a  rom = '\0';..  *
650d0 70 72 6f 6a 5f 74 6f 20 3d 20 27 5c 30 27 3b 0a  proj_to = '\0';.
650e0 09 20 20 73 72 69 64 5f 66 72 6f 6d 20 3d 20 67  .  srid_from = g
650f0 65 6f 2d 3e 53 72 69 64 3b 0a 09 20 20 70 72 6f  eo->Srid;..  pro
65100 6a 5f 70 61 72 61 6d 73 20 28 73 71 6c 69 74 65  j_params (sqlite
65110 2c 20 73 72 69 64 5f 66 72 6f 6d 2c 20 70 72 6f  , srid_from, pro
65120 6a 5f 66 72 6f 6d 29 3b 0a 09 20 20 70 72 6f 6a  j_from);..  proj
65130 5f 70 61 72 61 6d 73 20 28 73 71 6c 69 74 65 2c  _params (sqlite,
65140 20 73 72 69 64 5f 74 6f 2c 20 70 72 6f 6a 5f 74   srid_to, proj_t
65150 6f 29 3b 0a 09 20 20 69 66 20 28 2a 70 72 6f 6a  o);..  if (*proj
65160 5f 74 6f 20 3d 3d 20 27 5c 30 27 20 7c 7c 20 2a  _to == '\0' || *
65170 70 72 6f 6a 5f 66 72 6f 6d 20 3d 3d 20 27 5c 30  proj_from == '\0
65180 27 29 0a 09 20 20 20 20 7b 0a 09 09 67 61 69 61  ')..    {...gaia
65190 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
651a0 6f 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  o);...sqlite3_re
651b0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
651c0 78 74 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09  xt);...return;..
651d0 20 20 20 20 7d 0a 09 20 20 72 65 73 75 6c 74 20      }..  result 
651e0 3d 20 67 61 69 61 54 72 61 6e 73 66 6f 72 6d 20  = gaiaTransform 
651f0 28 67 65 6f 2c 20 70 72 6f 6a 5f 66 72 6f 6d 2c  (geo, proj_from,
65200 20 70 72 6f 6a 5f 74 6f 29 3b 0a 09 20 20 69 66   proj_to);..  if
65210 20 28 21 72 65 73 75 6c 74 29 0a 09 20 20 20 20   (!result)..    
65220 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
65230 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
65240 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
65250 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20  ../* builds the 
65260 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f  BLOB geometry to
65270 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a   be returned */.
65280 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73  ..int len;...uns
65290 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
652a0 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72  sult = NULL;...r
652b0 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 73 72  esult->Srid = sr
652c0 69 64 5f 74 6f 3b 0a 09 09 67 61 69 61 54 6f 53  id_to;...gaiaToS
652d0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
652e0 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65 73   (result, &p_res
652f0 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 73 71  ult, &len);...sq
65300 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
65310 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
65320 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29  sult, len, free)
65330 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d  ;...gaiaFreeGeom
65340 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09  Coll (result);..
65350 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
65360 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
65370 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 23 65 6e  ll (geo);.}..#en
65380 64 69 66 20 2f 2a 20 65 6e 64 20 69 6e 63 6c 75  dif /* end inclu
65390 64 69 6e 67 20 50 52 4f 4a 2e 34 20 2a 2f 0a 0a  ding PROJ.4 */..
653a0 23 69 66 6e 64 65 66 20 4f 4d 49 54 5f 47 45 4f  #ifndef OMIT_GEO
653b0 53 09 09 2f 2a 20 69 6e 63 6c 75 64 69 6e 67 20  S../* including 
653c0 47 45 4f 53 20 2a 2f 0a 0a 73 74 61 74 69 63 20  GEOS */..static 
653d0 76 6f 69 64 0a 66 6e 63 74 5f 42 6f 75 6e 64 61  void.fnct_Bounda
653e0 72 79 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ry (sqlite3_cont
653f0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
65400 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
65410 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
65420 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
65430 6e 3a 0a 2f 20 42 6f 75 6e 64 61 72 79 28 42 4c  n:./ Boundary(BL
65440 4f 42 20 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65  OB encoded geome
65450 74 72 79 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73  try)././ returns
65460 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 6f 72 69   the combinatori
65470 61 6c 20 62 6f 75 6e 64 61 72 79 20 66 6f 72 20  al boundary for 
65480 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79  current geometry
65490 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61   ./ or NULL if a
654a0 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
654b0 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75  untered.*/.    u
654c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
654d0 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
654e0 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 6c  bytes;.    int l
654f0 65 6e 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  en;.    unsigned
65500 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
65510 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
65520 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
65530 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
65540 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 62 6f 75 6e  GeomCollPtr boun
65550 64 61 72 79 3b 0a 20 20 20 20 47 41 49 41 5f 55  dary;.    GAIA_U
65560 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
65570 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
65580 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
65590 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
655a0 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
655b0 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
655c0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
655d0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
655e0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
655f0 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
65600 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
65610 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
65620 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
65630 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
65640 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
65650 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
65660 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  geo = gaiaFromSp
65670 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
65680 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
65690 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29  );.    if (!geo)
656a0 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
656b0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
656c0 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
656d0 7b 0a 09 20 20 69 66 20 28 67 61 69 61 49 73 45  {..  if (gaiaIsE
656e0 6d 70 74 79 20 28 67 65 6f 29 29 0a 09 20 20 20  mpty (geo))..   
656f0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
65700 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
65710 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b  ;..  else..    {
65720 0a 09 09 62 6f 75 6e 64 61 72 79 20 3d 20 67 61  ...boundary = ga
65730 69 61 42 6f 75 6e 64 61 72 79 20 28 67 65 6f 29  iaBoundary (geo)
65740 3b 0a 09 09 69 66 20 28 21 62 6f 75 6e 64 61 72  ;...if (!boundar
65750 79 29 0a 09 09 20 20 20 20 73 71 6c 69 74 65 33  y)...    sqlite3
65760 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
65770 6e 74 65 78 74 29 3b 0a 09 09 65 6c 73 65 0a 09  ntext);...else..
65780 09 20 20 7b 0a 09 09 20 20 20 20 20 20 67 61 69  .  {...      gai
65790 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  aToSpatiaLiteBlo
657a0 62 57 6b 62 20 28 62 6f 75 6e 64 61 72 79 2c 20  bWkb (boundary, 
657b0 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29  &p_result, &len)
657c0 3b 0a 09 09 20 20 20 20 20 20 67 61 69 61 46 72  ;...      gaiaFr
657d0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 62 6f 75 6e  eeGeomColl (boun
657e0 64 61 72 79 29 3b 0a 09 09 20 20 20 20 20 20 73  dary);...      s
657f0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
65800 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72  ob (context, p_r
65810 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65  esult, len, free
65820 29 3b 0a 09 09 20 20 7d 0a 09 20 20 20 20 7d 0a  );...  }..    }.
65830 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61        }.    gaia
65840 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
65850 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  o);.}..static vo
65860 69 64 0a 66 6e 63 74 5f 49 73 43 6c 6f 73 65 64  id.fnct_IsClosed
65870 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
65880 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
65890 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
658a0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
658b0 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
658c0 0a 2f 20 49 73 43 6c 6f 73 65 64 28 42 4c 4f 42  ./ IsClosed(BLOB
658d0 20 65 6e 63 6f 64 65 64 20 4c 49 4e 45 53 54 52   encoded LINESTR
658e0 49 4e 47 20 6f 72 20 4d 55 4c 54 49 4c 49 4e 45  ING or MULTILINE
658f0 53 54 52 49 4e 47 20 67 65 6f 6d 65 74 72 79 29  STRING geometry)
65900 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f 20  ././ returns:./ 
65910 31 20 69 66 20 74 68 69 73 20 4c 49 4e 45 53 54  1 if this LINEST
65920 52 49 4e 47 20 69 73 20 63 6c 6f 73 65 64 20 5b  RING is closed [
65930 6f 72 20 69 66 20 74 68 69 73 20 69 73 20 61 20  or if this is a 
65940 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 20  MULTILINESTRING 
65950 61 6e 64 20 65 76 65 72 79 20 4c 49 4e 45 53 54  and every LINEST
65960 52 49 4e 47 73 20 61 72 65 20 63 6c 6f 73 65 64  RINGs are closed
65970 5d 20 0a 2f 20 30 20 6f 74 68 65 72 77 69 73 65  ] ./ 0 otherwise
65980 0a 2f 20 6f 72 20 2d 31 20 69 66 20 61 6e 79 20  ./ or -1 if any 
65990 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
659a0 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
659b0 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
659c0 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
659d0 65 73 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  es;.    gaiaGeom
659e0 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
659f0 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  LL;.    GAIA_UNU
65a00 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
65a10 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
65a20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
65a30 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
65a40 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
65a50 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
65a60 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
65a70 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a   (context, -1);.
65a80 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
65a90 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
65aa0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
65ab0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
65ac0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
65ad0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
65ae0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
65af0 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
65b00 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
65b10 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
65b20 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
65b30 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
65b40 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
65b50 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
65b60 2d 31 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  -1);.    else.  
65b70 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
65b80 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
65b90 74 65 78 74 2c 20 67 61 69 61 49 73 43 6c 6f 73  text, gaiaIsClos
65ba0 65 64 47 65 6f 6d 20 28 67 65 6f 29 29 3b 0a 20  edGeom (geo));. 
65bb0 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46       }.    gaiaF
65bc0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
65bd0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
65be0 64 0a 66 6e 63 74 5f 49 73 53 69 6d 70 6c 65 20  d.fnct_IsSimple 
65bf0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
65c00 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
65c10 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
65c20 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
65c30 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
65c40 2f 20 49 73 53 69 6d 70 6c 65 28 42 4c 4f 42 20  / IsSimple(BLOB 
65c50 65 6e 63 6f 64 65 64 20 47 45 4f 4d 45 54 52 59  encoded GEOMETRY
65c60 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f  )././ returns:./
65c70 20 31 20 69 66 20 74 68 69 73 20 47 45 4f 4d 45   1 if this GEOME
65c80 54 52 59 20 69 73 20 73 69 6d 70 6c 65 0a 2f 20  TRY is simple./ 
65c90 30 20 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f 72  0 otherwise./ or
65ca0 20 2d 31 20 69 66 20 61 6e 79 20 65 72 72 6f 72   -1 if any error
65cb0 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
65cc0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
65cd0 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
65ce0 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
65cf0 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20     int ret;.    
65d00 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
65d10 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
65d20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
65d30 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
65d40 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
65d50 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
65d60 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
65d70 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
65d80 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
65d90 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
65da0 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72  t, -1);..  retur
65db0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
65dc0 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
65dd0 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
65de0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
65df0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
65e00 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
65e10 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
65e20 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
65e30 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
65e40 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
65e50 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
65e60 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
65e70 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
65e80 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20  ontext, -1);.   
65e90 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
65ea0 20 72 65 74 20 3d 20 67 61 69 61 49 73 53 69 6d   ret = gaiaIsSim
65eb0 70 6c 65 20 28 67 65 6f 29 3b 0a 09 20 20 69 66  ple (geo);..  if
65ec0 20 28 72 65 74 20 3c 20 30 29 0a 09 20 20 20 20   (ret < 0)..    
65ed0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
65ee0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d  _int (context, -
65ef0 31 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20  1);..  else..   
65f00 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
65f10 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
65f20 72 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ret);.      }.  
65f30 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
65f40 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61  ll (geo);.}..sta
65f50 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49 73  tic void.fnct_Is
65f60 52 69 6e 67 20 28 73 71 6c 69 74 65 33 5f 63 6f  Ring (sqlite3_co
65f70 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
65f80 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
65f90 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
65fa0 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
65fb0 69 6f 6e 3a 0a 2f 20 49 73 52 69 6e 67 28 42 4c  ion:./ IsRing(BL
65fc0 4f 42 20 65 6e 63 6f 64 65 64 20 4c 49 4e 45 53  OB encoded LINES
65fd0 54 52 49 4e 47 20 67 65 6f 6d 65 74 72 79 29 0a  TRING geometry).
65fe0 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f 20 31  /./ returns:./ 1
65ff0 20 69 66 20 74 68 69 73 20 4c 49 4e 45 53 54 52   if this LINESTR
66000 49 4e 47 20 69 73 20 61 20 76 61 6c 69 64 20 52  ING is a valid R
66010 49 4e 47 0a 2f 20 30 20 6f 74 68 65 72 77 69 73  ING./ 0 otherwis
66020 65 0a 2f 20 6f 72 20 2d 31 20 69 66 20 61 6e 79  e./ or -1 if any
66030 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
66040 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73  tered.*/.    uns
66050 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
66060 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  ob;.    int n_by
66070 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 72 65 74  tes;.    int ret
66080 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
66090 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
660a0 3b 0a 20 20 20 20 67 61 69 61 4c 69 6e 65 73 74  ;.    gaiaLinest
660b0 72 69 6e 67 50 74 72 20 6c 69 6e 65 3b 0a 20 20  ringPtr line;.  
660c0 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
660d0 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
660e0 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
660f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
66100 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
66110 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
66120 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
66130 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
66140 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74  ext, -1);..  ret
66150 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
66160 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
66170 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
66180 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
66190 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
661a0 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
661b0 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
661c0 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
661d0 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
661e0 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
661f0 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
66200 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
66210 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
66220 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 20  (context, -1);. 
66230 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
66240 09 20 20 6c 69 6e 65 20 3d 20 73 69 6d 70 6c 65  .  line = simple
66250 4c 69 6e 65 73 74 72 69 6e 67 20 28 67 65 6f 29  Linestring (geo)
66260 3b 0a 09 20 20 69 66 20 28 21 6c 69 6e 65 20 3c  ;..  if (!line <
66270 20 30 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74   0)..      sqlit
66280 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
66290 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20  ontext, -1);..  
662a0 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 72 65  else..    {...re
662b0 74 20 3d 20 67 61 69 61 49 73 52 69 6e 67 20 28  t = gaiaIsRing (
662c0 6c 69 6e 65 29 3b 0a 09 09 73 71 6c 69 74 65 33  line);...sqlite3
662d0 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
662e0 74 65 78 74 2c 20 72 65 74 29 3b 0a 09 20 20 20  text, ret);..   
662f0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67   }.      }.    g
66300 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
66310 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  (geo);.}..static
66320 20 76 6f 69 64 0a 66 6e 63 74 5f 49 73 56 61 6c   void.fnct_IsVal
66330 69 64 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  id (sqlite3_cont
66340 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
66350 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
66360 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
66370 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
66380 6e 3a 0a 2f 20 49 73 56 61 6c 69 64 28 42 4c 4f  n:./ IsValid(BLO
66390 42 20 65 6e 63 6f 64 65 64 20 47 45 4f 4d 45 54  B encoded GEOMET
663a0 52 59 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 3a  RY)././ returns:
663b0 0a 2f 20 31 20 69 66 20 74 68 69 73 20 47 45 4f  ./ 1 if this GEO
663c0 4d 45 54 52 59 20 69 73 20 61 20 76 61 6c 69 64  METRY is a valid
663d0 20 6f 6e 65 0a 2f 20 30 20 6f 74 68 65 72 77 69   one./ 0 otherwi
663e0 73 65 0a 2f 20 6f 72 20 2d 31 20 69 66 20 61 6e  se./ or -1 if an
663f0 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
66400 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
66410 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
66420 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
66430 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 72 65  ytes;.    int re
66440 74 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  t;.    gaiaGeomC
66450 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c  ollPtr geo = NUL
66460 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  L;.    GAIA_UNUS
66470 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
66480 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
66490 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
664a0 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d  ue_type (argv[0]
664b0 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  ) != SQLITE_BLOB
664c0 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
664d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
664e0 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09  (context, -1);..
664f0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
66500 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
66510 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
66520 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
66530 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
66540 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
66550 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
66560 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
66570 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  geo = gaiaFromSp
66580 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
66590 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
665a0 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29  );.    if (!geo)
665b0 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
665c0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d  _int (context, -
665d0 31 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  1);.    else.   
665e0 20 20 20 7b 0a 09 20 20 72 65 74 20 3d 20 67 61     {..  ret = ga
665f0 69 61 49 73 56 61 6c 69 64 20 28 67 65 6f 29 3b  iaIsValid (geo);
66600 0a 09 20 20 69 66 20 28 72 65 74 20 3c 20 30 29  ..  if (ret < 0)
66610 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
66620 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
66630 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 65 6c 73  ext, -1);..  els
66640 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33  e..      sqlite3
66650 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
66660 74 65 78 74 2c 20 72 65 74 29 3b 0a 20 20 20 20  text, ret);.    
66670 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65    }.    gaiaFree
66680 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a  GeomColl (geo);.
66690 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
666a0 6e 63 74 5f 4c 65 6e 67 74 68 20 28 73 71 6c 69  nct_Length (sqli
666b0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
666c0 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
666d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
666e0 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
666f0 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 47 4c 65   function:./ GLe
66700 6e 67 74 68 28 42 4c 4f 42 20 65 6e 63 6f 64 65  ngth(BLOB encode
66710 64 20 47 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43  d GEOMETRYCOLLEC
66720 54 49 4f 4e 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  TION)././ return
66730 73 20 20 74 68 65 20 74 6f 74 61 6c 20 6c 65 6e  s  the total len
66740 67 74 68 20 66 6f 72 20 63 75 72 72 65 6e 74 20  gth for current 
66750 67 65 6f 6d 65 74 72 79 20 0a 2f 20 6f 72 20 4e  geometry ./ or N
66760 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
66770 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
66780 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
66790 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
667a0 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
667b0 20 20 20 64 6f 75 62 6c 65 20 6c 65 6e 67 74 68     double length
667c0 20 3d 20 30 2e 30 3b 0a 20 20 20 20 69 6e 74 20   = 0.0;.    int 
667d0 72 65 74 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ret;.    gaiaGeo
667e0 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e  mCollPtr geo = N
667f0 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ULL;.    GAIA_UN
66800 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
66810 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
66820 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
66830 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
66840 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
66850 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
66860 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
66870 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
66880 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
66890 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
668a0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
668b0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
668c0 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
668d0 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
668e0 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
668f0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
66900 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  eo = gaiaFromSpa
66910 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
66920 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
66930 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a  ;.    if (!geo).
66940 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
66950 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
66960 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
66970 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61 47 65  ..  ret = gaiaGe
66980 6f 6d 43 6f 6c 6c 4c 65 6e 67 74 68 20 28 67 65  omCollLength (ge
66990 6f 2c 20 26 6c 65 6e 67 74 68 29 3b 0a 09 20 20  o, &length);..  
669a0 69 66 20 28 21 72 65 74 29 0a 09 20 20 20 20 20  if (!ret)..     
669b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
669c0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
669d0 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73  .  else..      s
669e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f  qlite3_result_do
669f0 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 6c  uble (context, l
66a00 65 6e 67 74 68 29 3b 0a 20 20 20 20 20 20 7d 0a  ength);.      }.
66a10 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
66a20 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73  Coll (geo);.}..s
66a30 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
66a40 41 72 65 61 20 28 73 71 6c 69 74 65 33 5f 63 6f  Area (sqlite3_co
66a50 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
66a60 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
66a70 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
66a80 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
66a90 69 6f 6e 3a 0a 2f 20 41 72 65 61 28 42 4c 4f 42  ion:./ Area(BLOB
66aa0 20 65 6e 63 6f 64 65 64 20 47 45 4f 4d 45 54 52   encoded GEOMETR
66ab0 59 43 4f 4c 4c 45 43 54 49 4f 4e 29 0a 2f 0a 2f  YCOLLECTION)././
66ac0 20 72 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74   returns the tot
66ad0 61 6c 20 61 72 65 61 20 66 6f 72 20 63 75 72 72  al area for curr
66ae0 65 6e 74 20 67 65 6f 6d 65 74 72 79 20 0a 2f 20  ent geometry ./ 
66af0 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65  or NULL if any e
66b00 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
66b10 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67  red.*/.    unsig
66b20 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62  ned char *p_blob
66b30 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65  ;.    int n_byte
66b40 73 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 61 72  s;.    double ar
66b50 65 61 20 3d 20 30 2e 30 3b 0a 20 20 20 20 69 6e  ea = 0.0;.    in
66b60 74 20 72 65 74 3b 0a 20 20 20 20 67 61 69 61 47  t ret;.    gaiaG
66b70 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
66b80 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f   NULL;.    GAIA_
66b90 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
66ba0 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
66bb0 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
66bc0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
66bd0 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
66be0 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
66bf0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
66c00 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
66c10 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
66c20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
66c30 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
66c40 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
66c50 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
66c60 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
66c70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
66c80 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
66c90 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
66ca0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
66cb0 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
66cc0 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
66cd0 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
66ce0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
66cf0 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
66d00 20 7b 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61   {..  ret = gaia
66d10 47 65 6f 6d 43 6f 6c 6c 41 72 65 61 20 28 67 65  GeomCollArea (ge
66d20 6f 2c 20 26 61 72 65 61 29 3b 0a 09 20 20 69 66  o, &area);..  if
66d30 20 28 21 72 65 74 29 0a 09 20 20 20 20 20 20 73   (!ret)..      s
66d40 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
66d50 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
66d60 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
66d70 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
66d80 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 65  le (context, are
66d90 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  a);.      }.    
66da0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
66db0 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69   (geo);.}..stati
66dc0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 43 65 6e 74  c void.fnct_Cent
66dd0 72 6f 69 64 20 28 73 71 6c 69 74 65 33 5f 63 6f  roid (sqlite3_co
66de0 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
66df0 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
66e00 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
66e10 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
66e20 69 6f 6e 3a 0a 2f 20 43 65 6e 74 72 6f 69 64 28  ion:./ Centroid(
66e30 42 4c 4f 42 65 6e 63 6f 64 65 64 20 50 4f 4c 59  BLOBencoded POLY
66e40 47 4f 4e 20 6f 72 20 4d 55 4c 54 49 50 4f 4c 59  GON or MULTIPOLY
66e50 47 4f 4e 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a  GON geometry)./.
66e60 2f 20 72 65 74 75 72 6e 73 20 61 20 50 4f 49 4e  / returns a POIN
66e70 54 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  T representing t
66e80 68 65 20 63 65 6e 74 72 6f 69 64 20 66 6f 72 20  he centroid for 
66e90 63 75 72 72 65 6e 74 20 50 4f 4c 59 47 4f 4e 20  current POLYGON 
66ea0 2f 20 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 20 67  / MULTIPOLYGON g
66eb0 65 6f 6d 65 74 72 79 20 0a 2f 20 6f 72 20 4e 55  eometry ./ or NU
66ec0 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
66ed0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
66ee0 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
66ef0 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20  har *p_blob;.   
66f00 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20   int n_bytes;.  
66f10 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 69    int len;.    i
66f20 6e 74 20 72 65 74 3b 0a 20 20 20 20 64 6f 75 62  nt ret;.    doub
66f30 6c 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65  le x;.    double
66f40 20 79 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   y;.    unsigned
66f50 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
66f60 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
66f70 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20  GeomCollPtr geo 
66f80 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
66f90 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75  GeomCollPtr resu
66fa0 6c 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  lt;.    GAIA_UNU
66fb0 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
66fc0 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
66fd0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
66fe0 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
66ff0 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
67000 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
67010 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
67020 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
67030 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
67040 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
67050 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
67060 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
67070 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
67080 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
67090 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
670a0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
670b0 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  o = gaiaFromSpat
670c0 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
670d0 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
670e0 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09  .    if (!geo)..
670f0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
67100 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
67110 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
67120 09 20 20 69 66 20 28 67 61 69 61 49 73 45 6d 70  .  if (gaiaIsEmp
67130 74 79 20 28 67 65 6f 29 29 0a 09 20 20 20 20 20  ty (geo))..     
67140 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
67150 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
67160 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09  .  else..    {..
67170 09 72 65 74 20 3d 20 67 61 69 61 47 65 6f 6d 43  .ret = gaiaGeomC
67180 6f 6c 6c 43 65 6e 74 72 6f 69 64 20 28 67 65 6f  ollCentroid (geo
67190 2c 20 26 78 2c 20 26 79 29 3b 0a 09 09 69 66 20  , &x, &y);...if 
671a0 28 21 72 65 74 29 0a 09 09 20 20 20 20 73 71 6c  (!ret)...    sql
671b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
671c0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09 65 6c   (context);...el
671d0 73 65 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20  se...  {...     
671e0 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41 6c   result = gaiaAl
671f0 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 20 28 29 3b 0a  locGeomColl ();.
67200 09 09 20 20 20 20 20 20 72 65 73 75 6c 74 2d 3e  ..      result->
67210 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64  Srid = geo->Srid
67220 3b 0a 09 09 20 20 20 20 20 20 67 61 69 61 41 64  ;...      gaiaAd
67230 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c  dPointToGeomColl
67240 20 28 72 65 73 75 6c 74 2c 20 78 2c 20 79 29 3b   (result, x, y);
67250 0a 09 09 20 20 20 20 20 20 67 61 69 61 54 6f 53  ...      gaiaToS
67260 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
67270 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65 73   (result, &p_res
67280 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 20 20  ult, &len);...  
67290 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
672a0 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09  Coll (result);..
672b0 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
672c0 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74  esult_blob (cont
672d0 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c  ext, p_result, l
672e0 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09 20 20 7d  en, free);...  }
672f0 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a  ..    }.      }.
67300 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
67310 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73  Coll (geo);.}..s
67320 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
67330 50 6f 69 6e 74 4f 6e 53 75 72 66 61 63 65 20 28  PointOnSurface (
67340 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
67350 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
67360 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
67370 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
67380 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
67390 20 50 6f 69 6e 74 4f 6e 53 75 72 66 61 63 65 28   PointOnSurface(
673a0 42 4c 4f 42 65 6e 63 6f 64 65 64 20 50 4f 4c 59  BLOBencoded POLY
673b0 47 4f 4e 20 6f 72 20 4d 55 4c 54 49 50 4f 4c 59  GON or MULTIPOLY
673c0 47 4f 4e 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a  GON geometry)./.
673d0 2f 20 72 65 74 75 72 6e 73 20 61 20 50 4f 49 4e  / returns a POIN
673e0 54 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  T guaranteed to 
673f0 6c 69 65 20 6f 6e 20 74 68 65 20 53 75 72 66 61  lie on the Surfa
67400 63 65 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  ce./ or NULL if 
67410 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
67420 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
67430 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
67440 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
67450 5f 62 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20  _bytes;.    int 
67460 6c 65 6e 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  len;.    double 
67470 78 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79 3b  x;.    double y;
67480 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
67490 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e  ar *p_result = N
674a0 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ULL;.    gaiaGeo
674b0 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e  mCollPtr geo = N
674c0 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ULL;.    gaiaGeo
674d0 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b  mCollPtr result;
674e0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
674f0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
67500 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
67510 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
67520 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
67530 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
67540 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
67550 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
67560 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
67570 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
67580 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
67590 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
675a0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
675b0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
675c0 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
675d0 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
675e0 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
675f0 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
67600 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
67610 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
67620 20 20 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c    if (!geo)..sql
67630 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
67640 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   (context);.    
67650 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
67660 69 66 20 28 21 67 61 69 61 47 65 74 50 6f 69 6e  if (!gaiaGetPoin
67670 74 4f 6e 53 75 72 66 61 63 65 20 28 67 65 6f 2c  tOnSurface (geo,
67680 20 26 78 2c 20 26 79 29 29 0a 09 20 20 20 20 20   &x, &y))..     
67690 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
676a0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
676b0 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09  .  else..    {..
676c0 09 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41 6c  .result = gaiaAl
676d0 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 20 28 29 3b 0a  locGeomColl ();.
676e0 09 09 67 61 69 61 41 64 64 50 6f 69 6e 74 54 6f  ..gaiaAddPointTo
676f0 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74  GeomColl (result
67700 2c 20 78 2c 20 79 29 3b 0a 09 09 72 65 73 75 6c  , x, y);...resul
67710 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53  t->Srid = geo->S
67720 72 69 64 3b 0a 09 09 67 61 69 61 54 6f 53 70 61  rid;...gaiaToSpa
67730 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
67740 72 65 73 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c  result, &p_resul
67750 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 67 61 69 61  t, &len);...gaia
67760 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65  FreeGeomColl (re
67770 73 75 6c 74 29 3b 0a 09 09 73 71 6c 69 74 65 33  sult);...sqlite3
67780 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
67790 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
677a0 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20   len, free);..  
677b0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
677c0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
677d0 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69   (geo);.}..stati
677e0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 53 69 6d 70  c void.fnct_Simp
677f0 6c 69 66 79 20 28 73 71 6c 69 74 65 33 5f 63 6f  lify (sqlite3_co
67800 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
67810 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
67820 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
67830 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
67840 69 6f 6e 3a 0a 2f 20 53 69 6d 70 6c 69 66 79 28  ion:./ Simplify(
67850 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d  BLOBencoded geom
67860 65 74 72 79 2c 20 74 6f 6c 65 72 61 6e 63 65 29  etry, tolerance)
67870 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 6e  ././ returns a n
67880 65 77 20 67 65 6f 6d 65 74 72 79 20 74 68 61 74  ew geometry that
67890 20 69 73 20 61 20 63 61 72 69 63 61 74 75 72 65   is a caricature
678a0 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
678b0 20 6f 6e 65 20 72 65 63 65 69 76 65 64 2c 20 62   one received, b
678c0 75 74 20 73 69 6d 70 6c 69 66 69 65 64 20 75 73  ut simplified us
678d0 69 6e 67 20 74 68 65 20 44 6f 75 67 6c 61 73 2d  ing the Douglas-
678e0 50 65 75 6b 65 72 20 61 6c 67 6f 72 69 68 74 6d  Peuker algorihtm
678f0 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
67900 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
67910 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
67920 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
67930 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
67940 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47 65  ytes;.    gaiaGe
67950 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20  omCollPtr geo = 
67960 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65  NULL;.    gaiaGe
67970 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74  omCollPtr result
67980 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61  ;.    int int_va
67990 6c 75 65 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  lue;.    double 
679a0 74 6f 6c 65 72 61 6e 63 65 3b 0a 20 20 20 20 47  tolerance;.    G
679b0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
679c0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
679d0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
679e0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
679f0 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
67a00 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
67a10 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
67a20 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
67a30 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
67a40 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73       }.    if (s
67a50 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
67a60 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53  e (argv[1]) == S
67a70 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 74 6f  QLITE_FLOAT)..to
67a80 6c 65 72 61 6e 63 65 20 3d 20 73 71 6c 69 74 65  lerance = sqlite
67a90 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28  3_value_double (
67aa0 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c  argv[1]);.    el
67ab0 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  se if (sqlite3_v
67ac0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
67ad0 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  1]) == SQLITE_IN
67ae0 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09  TEGER).      {..
67af0 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71    int_value = sq
67b00 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
67b10 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20 20 74 6f  (argv[1]);..  to
67b20 6c 65 72 61 6e 63 65 20 3d 20 69 6e 74 5f 76 61  lerance = int_va
67b30 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lue;.      }.   
67b40 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
67b50 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
67b60 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
67b70 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
67b80 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
67b90 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
67ba0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
67bb0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
67bc0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
67bd0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
67be0 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
67bf0 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
67c00 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
67c10 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
67c20 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
67c30 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
67c40 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
67c50 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
67c60 20 7b 0a 09 20 20 72 65 73 75 6c 74 20 3d 20 67   {..  result = g
67c70 61 69 61 47 65 6f 6d 43 6f 6c 6c 53 69 6d 70 6c  aiaGeomCollSimpl
67c80 69 66 79 20 28 67 65 6f 2c 20 74 6f 6c 65 72 61  ify (geo, tolera
67c90 6e 63 65 29 3b 0a 09 20 20 69 66 20 28 21 72 65  nce);..  if (!re
67ca0 73 75 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c  sult)..      sql
67cb0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
67cc0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65   (context);..  e
67cd0 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20  lse..    {.../* 
67ce0 62 75 69 6c 64 73 20 74 68 65 20 42 4c 4f 42 20  builds the BLOB 
67cf0 67 65 6f 6d 65 74 72 79 20 74 6f 20 62 65 20 72  geometry to be r
67d00 65 74 75 72 6e 65 64 20 2a 2f 0a 09 09 69 6e 74  eturned */...int
67d10 20 6c 65 6e 3b 0a 09 09 75 6e 73 69 67 6e 65 64   len;...unsigned
67d20 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20   char *p_result 
67d30 3d 20 4e 55 4c 4c 3b 0a 09 09 67 61 69 61 54 6f  = NULL;...gaiaTo
67d40 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
67d50 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65  b (result, &p_re
67d60 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 73  sult, &len);...s
67d70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
67d80 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72  ob (context, p_r
67d90 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65  esult, len, free
67da0 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f  );...gaiaFreeGeo
67db0 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a  mColl (result);.
67dc0 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20  .    }.      }. 
67dd0 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
67de0 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
67df0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 53  atic void.fnct_S
67e00 69 6d 70 6c 69 66 79 50 72 65 73 65 72 76 65 54  implifyPreserveT
67e10 6f 70 6f 6c 6f 67 79 20 28 73 71 6c 69 74 65 33  opology (sqlite3
67e20 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
67e30 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09  xt, int argc,...
67e40 09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
67e50 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
67e60 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
67e70 3a 0a 2f 20 53 69 6d 70 6c 69 66 79 50 72 65 73  :./ SimplifyPres
67e80 65 72 76 65 54 6f 70 6f 6c 6f 67 79 28 42 4c 4f  erveTopology(BLO
67e90 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72  Bencoded geometr
67ea0 79 2c 20 74 6f 6c 65 72 61 6e 63 65 29 0a 2f 0a  y, tolerance)./.
67eb0 2f 20 72 65 74 75 72 6e 73 20 61 20 6e 65 77 20  / returns a new 
67ec0 67 65 6f 6d 65 74 72 79 20 74 68 61 74 20 69 73  geometry that is
67ed0 20 61 20 63 61 72 69 63 61 74 75 72 65 20 6f 66   a caricature of
67ee0 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 6e   the original on
67ef0 65 20 72 65 63 65 69 76 65 64 2c 20 62 75 74 20  e received, but 
67f00 73 69 6d 70 6c 69 66 69 65 64 20 75 73 69 6e 67  simplified using
67f10 20 74 68 65 20 44 6f 75 67 6c 61 73 2d 50 65 75   the Douglas-Peu
67f20 6b 65 72 20 61 6c 67 6f 72 69 68 74 6d 20 5b 70  ker algorihtm [p
67f30 72 65 73 65 72 76 69 6e 67 20 74 6f 70 6f 6c 6f  reserving topolo
67f40 67 79 5d 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  gy]./ or NULL if
67f50 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
67f60 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
67f70 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
67f80 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20  p_blob;.    int 
67f90 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69  n_bytes;.    gai
67fa0 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
67fb0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
67fc0 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73  aGeomCollPtr res
67fd0 75 6c 74 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74  ult;.    int int
67fe0 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64 6f 75 62  _value;.    doub
67ff0 6c 65 20 74 6f 6c 65 72 61 6e 63 65 3b 0a 20 20  le tolerance;.  
68000 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
68010 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
68020 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
68030 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
68040 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
68050 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
68060 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
68070 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
68080 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
68090 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
680a0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
680b0 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d  type (argv[1]) =
680c0 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a  = SQLITE_FLOAT).
680d0 09 74 6f 6c 65 72 61 6e 63 65 20 3d 20 73 71 6c  .tolerance = sql
680e0 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
680f0 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  e (argv[1]);.   
68100 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
68110 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
68120 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
68130 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
68140 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
68150 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
68160 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20  nt (argv[1]);.. 
68170 20 74 6f 6c 65 72 61 6e 63 65 20 3d 20 69 6e 74   tolerance = int
68180 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a  _value;.      }.
68190 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
681a0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
681b0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
681c0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
681d0 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
681e0 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
681f0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
68200 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
68210 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
68220 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
68230 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
68240 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
68250 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
68260 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
68270 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
68280 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  geo)..sqlite3_re
68290 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
682a0 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  xt);.    else.  
682b0 20 20 20 20 7b 0a 09 20 20 72 65 73 75 6c 74 20      {..  result 
682c0 3d 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 53 69  = gaiaGeomCollSi
682d0 6d 70 6c 69 66 79 50 72 65 73 65 72 76 65 54 6f  mplifyPreserveTo
682e0 70 6f 6c 6f 67 79 20 28 67 65 6f 2c 20 74 6f 6c  pology (geo, tol
682f0 65 72 61 6e 63 65 29 3b 0a 09 20 20 69 66 20 28  erance);..  if (
68300 21 72 65 73 75 6c 74 29 0a 09 20 20 20 20 20 20  !result)..      
68310 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
68320 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
68330 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09    else..    {...
68340 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20 42 4c  /* builds the BL
68350 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f 20 62  OB geometry to b
68360 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 09 09  e returned */...
68370 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73 69 67  int len;...unsig
68380 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75  ned char *p_resu
68390 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 67 61 69  lt = NULL;...gai
683a0 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  aToSpatiaLiteBlo
683b0 62 57 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70  bWkb (result, &p
683c0 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a  _result, &len);.
683d0 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
683e0 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20  _blob (context, 
683f0 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66  p_result, len, f
68400 72 65 65 29 3b 0a 09 09 67 61 69 61 46 72 65 65  ree);...gaiaFree
68410 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74  GeomColl (result
68420 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20  );..    }.      
68430 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
68440 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a  omColl (geo);.}.
68450 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
68460 74 5f 43 6f 6e 76 65 78 48 75 6c 6c 20 28 73 71  t_ConvexHull (sq
68470 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
68480 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
68490 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
684a0 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53   ** argv).{./* S
684b0 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 43  QL function:./ C
684c0 6f 6e 76 65 78 48 75 6c 6c 28 42 4c 4f 42 65 6e  onvexHull(BLOBen
684d0 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a  coded geometry).
684e0 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 6e 65  /./ returns a ne
684f0 77 20 67 65 6f 6d 65 74 72 79 20 72 65 70 72 65  w geometry repre
68500 73 65 6e 74 69 6e 67 20 74 68 65 20 43 4f 4e 56  senting the CONV
68510 45 58 20 48 55 4c 4c 20 66 6f 72 20 63 75 72 72  EX HULL for curr
68520 65 6e 74 20 67 65 6f 6d 65 74 72 79 0a 2f 20 6f  ent geometry./ o
68530 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72  r NULL if any er
68540 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
68550 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ed.*/.    unsign
68560 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
68570 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
68580 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20  ;.    int len;. 
68590 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
685a0 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c   *p_result = NUL
685b0 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
685c0 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c  ollPtr geo = NUL
685d0 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
685e0 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20  ollPtr result;. 
685f0 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
68600 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
68610 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
68620 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
68630 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
68640 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
68650 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
68660 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
68670 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
68680 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
68690 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
686a0 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
686b0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
686c0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
686d0 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
686e0 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
686f0 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67  0]);.    geo = g
68700 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
68710 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
68720 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
68730 69 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74  if (!geo)..sqlit
68740 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
68750 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
68760 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65  se.      {..  re
68770 73 75 6c 74 20 3d 20 67 61 69 61 43 6f 6e 76 65  sult = gaiaConve
68780 78 48 75 6c 6c 20 28 67 65 6f 29 3b 0a 09 20 20  xHull (geo);..  
68790 69 66 20 28 21 72 65 73 75 6c 74 29 0a 09 20 20  if (!result)..  
687a0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
687b0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
687c0 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20  );..  else..    
687d0 7b 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69 61  {...gaiaToSpatia
687e0 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72 65 73  LiteBlobWkb (res
687f0 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  ult, &p_result, 
68800 26 6c 65 6e 29 3b 0a 09 09 73 71 6c 69 74 65 33  &len);...sqlite3
68810 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
68820 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
68830 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09 67   len, free);...g
68840 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
68850 28 72 65 73 75 6c 74 29 3b 0a 09 20 20 20 20 7d  (result);..    }
68860 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
68870 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
68880 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  eo);.}..static v
68890 6f 69 64 0a 66 6e 63 74 5f 42 75 66 66 65 72 20  oid.fnct_Buffer 
688a0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
688b0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
688c0 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
688d0 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
688e0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
688f0 2f 20 42 75 66 66 65 72 28 42 4c 4f 42 65 6e 63  / Buffer(BLOBenc
68900 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c 20 72  oded geometry, r
68910 61 64 69 75 73 29 0a 2f 0a 2f 20 72 65 74 75 72  adius)././ retur
68920 6e 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74 72  ns a new geometr
68930 79 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  y representing t
68940 68 65 20 42 55 46 46 45 52 20 66 6f 72 20 63 75  he BUFFER for cu
68950 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 0a 2f  rrent geometry./
68960 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
68970 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
68980 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
68990 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
689a0 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
689b0 65 73 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  es;.    gaiaGeom
689c0 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
689d0 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
689e0 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a  CollPtr result;.
689f0 20 20 20 20 64 6f 75 62 6c 65 20 72 61 64 69 75      double radiu
68a00 73 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76  s;.    int int_v
68a10 61 6c 75 65 3b 0a 20 20 20 20 47 41 49 41 5f 55  alue;.    GAIA_U
68a20 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
68a30 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
68a40 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
68a50 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
68a60 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
68a70 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
68a80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
68a90 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
68aa0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
68ab0 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
68ac0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
68ad0 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
68ae0 5f 46 4c 4f 41 54 29 0a 09 72 61 64 69 75 73 20  _FLOAT)..radius 
68af0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
68b00 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29  double (argv[1])
68b10 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73  ;.    else if (s
68b20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
68b30 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53  e (argv[1]) == S
68b40 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
68b50 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61       {..  int_va
68b60 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lue = sqlite3_va
68b70 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d  lue_int (argv[1]
68b80 29 3b 0a 09 20 20 72 61 64 69 75 73 20 3d 20 69  );..  radius = i
68b90 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  nt_value;.      
68ba0 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  }.    else.     
68bb0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
68bc0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
68bd0 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
68be0 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
68bf0 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
68c00 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
68c10 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
68c20 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
68c30 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
68c40 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
68c50 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61  ;.    geo = gaia
68c60 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
68c70 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
68c80 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
68c90 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f  (!geo)..sqlite3_
68ca0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
68cb0 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a  text);.    else.
68cc0 20 20 20 20 20 20 7b 0a 09 20 20 72 65 73 75 6c        {..  resul
68cd0 74 20 3d 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c  t = gaiaGeomColl
68ce0 42 75 66 66 65 72 20 28 67 65 6f 2c 20 72 61 64  Buffer (geo, rad
68cf0 69 75 73 2c 20 33 30 29 3b 0a 09 20 20 69 66 20  ius, 30);..  if 
68d00 28 21 72 65 73 75 6c 74 29 0a 09 20 20 20 20 20  (!result)..     
68d10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
68d20 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
68d30 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09  .  else..    {..
68d40 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20 42  ./* builds the B
68d50 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f 20  LOB geometry to 
68d60 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 09  be returned */..
68d70 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73 69  .int len;...unsi
68d80 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
68d90 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72 65  ult = NULL;...re
68da0 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f  sult->Srid = geo
68db0 2d 3e 53 72 69 64 3b 0a 09 09 67 61 69 61 54 6f  ->Srid;...gaiaTo
68dc0 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
68dd0 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65  b (result, &p_re
68de0 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 73  sult, &len);...s
68df0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
68e00 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72  ob (context, p_r
68e10 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65  esult, len, free
68e20 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f  );...gaiaFreeGeo
68e30 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a  mColl (result);.
68e40 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20  .    }.      }. 
68e50 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
68e60 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
68e70 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49  atic void.fnct_I
68e80 6e 74 65 72 73 65 63 74 69 6f 6e 20 28 73 71 6c  ntersection (sql
68e90 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
68ea0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
68eb0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
68ec0 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
68ed0 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 49 6e  L function:./ In
68ee0 74 65 72 73 65 63 74 69 6f 6e 28 42 4c 4f 42 65  tersection(BLOBe
68ef0 6e 63 6f 64 65 64 20 67 65 6f 6d 31 2c 20 42 4c  ncoded geom1, BL
68f00 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 32 29  OBencoded geom2)
68f10 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 6e  ././ returns a n
68f20 65 77 20 67 65 6f 6d 65 74 72 79 20 72 65 70 72  ew geometry repr
68f30 65 73 65 6e 74 69 6e 67 20 74 68 65 20 49 4e 54  esenting the INT
68f40 45 52 53 45 43 54 49 4f 4e 20 6f 66 20 62 6f 74  ERSECTION of bot
68f50 68 20 67 65 6f 6d 65 74 72 69 65 73 0a 2f 20 6f  h geometries./ o
68f60 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72  r NULL if any er
68f70 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
68f80 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ed.*/.    unsign
68f90 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
68fa0 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
68fb0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
68fc0 6c 6c 50 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c  llPtr geo1 = NUL
68fd0 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
68fe0 6f 6c 6c 50 74 72 20 67 65 6f 32 20 3d 20 4e 55  ollPtr geo2 = NU
68ff0 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
69000 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a  CollPtr result;.
69010 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
69020 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
69030 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
69040 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
69050 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21  type (argv[0]) !
69060 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
69070 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
69080 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
69090 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
690a0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
690b0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
690c0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29  e_type (argv[1])
690d0 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29   != SQLITE_BLOB)
690e0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
690f0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
69100 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
69110 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
69120 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
69130 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
69140 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
69150 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
69160 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
69170 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
69180 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31  gv[0]);.    geo1
69190 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69   = gaiaFromSpati
691a0 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f  aLiteBlobWkb (p_
691b0 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a  blob, n_bytes);.
691c0 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
691d0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
691e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
691f0 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  b (argv[1]);.   
69200 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
69210 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
69220 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65  argv[1]);.    ge
69230 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  o2 = gaiaFromSpa
69240 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
69250 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
69260 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 31 20  ;.    if (!geo1 
69270 7c 7c 20 21 67 65 6f 32 29 0a 09 73 71 6c 69 74  || !geo2)..sqlit
69280 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
69290 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
692a0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65  se.      {..  re
692b0 73 75 6c 74 20 3d 20 67 61 69 61 47 65 6f 6d 65  sult = gaiaGeome
692c0 74 72 79 49 6e 74 65 72 73 65 63 74 69 6f 6e 20  tryIntersection 
692d0 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20  (geo1, geo2);.. 
692e0 20 69 66 20 28 21 72 65 73 75 6c 74 29 0a 09 20   if (!result).. 
692f0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
69300 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
69310 74 29 3b 0a 09 20 20 65 6c 73 65 20 69 66 20 28  t);..  else if (
69320 67 61 69 61 49 73 45 6d 70 74 79 20 28 72 65 73  gaiaIsEmpty (res
69330 75 6c 74 29 29 0a 09 20 20 20 20 7b 0a 09 09 67  ult))..    {...g
69340 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
69350 28 72 65 73 75 6c 74 29 3b 0a 09 09 73 71 6c 69  (result);...sqli
69360 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
69370 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 20 20  (context);..    
69380 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b  }..  else..    {
69390 0a 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65  .../* builds the
693a0 20 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74   BLOB geometry t
693b0 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f  o be returned */
693c0 0a 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e  ...int len;...un
693d0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72  signed char *p_r
693e0 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09  esult = NULL;...
693f0 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65  gaiaToSpatiaLite
69400 42 6c 6f 62 57 6b 62 20 28 72 65 73 75 6c 74 2c  BlobWkb (result,
69410 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e   &p_result, &len
69420 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73  );...sqlite3_res
69430 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78  ult_blob (contex
69440 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e  t, p_result, len
69450 2c 20 66 72 65 65 29 3b 0a 09 09 67 61 69 61 46  , free);...gaiaF
69460 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73  reeGeomColl (res
69470 75 6c 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20  ult);..    }.   
69480 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65     }.    gaiaFre
69490 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29  eGeomColl (geo1)
694a0 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  ;.    gaiaFreeGe
694b0 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d  omColl (geo2);.}
694c0 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
694d0 63 74 5f 55 6e 69 6f 6e 5f 73 74 65 70 20 28 73  ct_Union_step (s
694e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
694f0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
69500 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
69510 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
69520 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
69530 55 6e 69 6f 6e 28 42 4c 4f 42 65 6e 63 6f 64 65  Union(BLOBencode
69540 64 20 67 65 6f 6d 29 0a 2f 0a 2f 20 61 67 67 72  d geom)././ aggr
69550 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2d  egate function -
69560 20 53 54 45 50 0a 2f 0a 2a 2f 0a 20 20 20 20 75   STEP./.*/.    u
69570 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
69580 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
69590 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47  bytes;.    gaiaG
695a0 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d 3b  eomCollPtr geom;
695b0 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
695c0 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20  lPtr result;.   
695d0 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
695e0 20 2a 70 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e   *p;.    GAIA_UN
695f0 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
69600 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
69610 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
69620 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
69630 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
69640 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
69650 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
69660 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
69670 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
69680 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
69690 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
696a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
696b0 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
696c0 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
696d0 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
696e0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
696f0 65 6f 6d 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  eom = gaiaFromSp
69700 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
69710 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
69720 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 6d  );.    if (!geom
69730 29 0a 09 72 65 74 75 72 6e 3b 0a 20 20 20 20 70  )..return;.    p
69740 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
69750 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 28 63 6f  gate_context (co
69760 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 20 28 67  ntext, sizeof (g
69770 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 29 29  aiaGeomCollPtr))
69780 3b 0a 20 20 20 20 69 66 20 28 21 28 2a 70 29 29  ;.    if (!(*p))
69790 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 74  .      {..  /* t
697a0 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74  his is the first
697b0 20 72 6f 77 20 2a 2f 0a 09 20 20 2a 70 20 3d 20   row */..  *p = 
697c0 67 65 6f 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  geom;.      }.  
697d0 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
697e0 20 20 2f 2a 20 73 75 62 73 65 71 75 65 6e 74 20    /* subsequent 
697f0 72 6f 77 73 20 2a 2f 0a 09 20 20 72 65 73 75 6c  rows */..  resul
69800 74 20 3d 20 67 61 69 61 47 65 6f 6d 65 74 72 79  t = gaiaGeometry
69810 55 6e 69 6f 6e 20 28 2a 70 2c 20 67 65 6f 6d 29  Union (*p, geom)
69820 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f  ;..  gaiaFreeGeo
69830 6d 43 6f 6c 6c 20 28 2a 70 29 3b 0a 09 20 20 2a  mColl (*p);..  *
69840 70 20 3d 20 72 65 73 75 6c 74 3b 0a 09 20 20 67  p = result;..  g
69850 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
69860 28 67 65 6f 6d 29 3b 0a 20 20 20 20 20 20 7d 0a  (geom);.      }.
69870 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
69880 6e 63 74 5f 55 6e 69 6f 6e 5f 66 69 6e 61 6c 20  nct_Union_final 
69890 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
698a0 20 2a 20 63 6f 6e 74 65 78 74 29 0a 7b 0a 2f 2a   * context).{./*
698b0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
698c0 20 55 6e 69 6f 6e 28 42 4c 4f 42 65 6e 63 6f 64   Union(BLOBencod
698d0 65 64 20 67 65 6f 6d 29 0a 2f 0a 2f 20 61 67 67  ed geom)././ agg
698e0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
698f0 2d 20 46 49 4e 41 4c 0a 2f 0a 2a 2f 0a 20 20 20  - FINAL./.*/.   
69900 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
69910 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 67 61 69   result;.    gai
69920 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 2a 70 20  aGeomCollPtr *p 
69930 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
69940 61 74 65 5f 63 6f 6e 74 65 78 74 20 28 63 6f 6e  ate_context (con
69950 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 20 69 66  text, 0);.    if
69960 20 28 21 70 29 0a 20 20 20 20 20 20 7b 0a 09 20   (!p).      {.. 
69970 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
69980 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
69990 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
699a0 20 7d 0a 20 20 20 20 72 65 73 75 6c 74 20 3d 20   }.    result = 
699b0 2a 70 3b 0a 20 20 20 20 69 66 20 28 21 72 65 73  *p;.    if (!res
699c0 75 6c 74 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  ult)..sqlite3_re
699d0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
699e0 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66  xt);.    else if
699f0 20 28 67 61 69 61 49 73 45 6d 70 74 79 20 28 72   (gaiaIsEmpty (r
69a00 65 73 75 6c 74 29 29 0a 20 20 20 20 20 20 7b 0a  esult)).      {.
69a10 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43  .  gaiaFreeGeomC
69a20 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09 20  oll (result);.. 
69a30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
69a40 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
69a50 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
69a60 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 62  .      {..  /* b
69a70 75 69 6c 64 73 20 74 68 65 20 42 4c 4f 42 20 67  uilds the BLOB g
69a80 65 6f 6d 65 74 72 79 20 74 6f 20 62 65 20 72 65  eometry to be re
69a90 74 75 72 6e 65 64 20 2a 2f 0a 09 20 20 69 6e 74  turned */..  int
69aa0 20 6c 65 6e 3b 0a 09 20 20 75 6e 73 69 67 6e 65   len;..  unsigne
69ab0 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
69ac0 20 3d 20 4e 55 4c 4c 3b 0a 09 20 20 67 61 69 61   = NULL;..  gaia
69ad0 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
69ae0 57 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f  Wkb (result, &p_
69af0 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09  result, &len);..
69b00 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
69b10 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20  _blob (context, 
69b20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66  p_result, len, f
69b30 72 65 65 29 3b 0a 09 20 20 67 61 69 61 46 72 65  ree);..  gaiaFre
69b40 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c  eGeomColl (resul
69b50 74 29 3b 0a 20 20 20 20 20 20 7d 0a 7d 0a 0a 73  t);.      }.}..s
69b60 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
69b70 55 6e 69 6f 6e 20 28 73 71 6c 69 74 65 33 5f 63  Union (sqlite3_c
69b80 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
69b90 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
69ba0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
69bb0 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
69bc0 74 69 6f 6e 3a 0a 2f 20 55 6e 69 6f 6e 28 42 4c  tion:./ Union(BL
69bd0 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 31 2c  OBencoded geom1,
69be0 20 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f   BLOBencoded geo
69bf0 6d 32 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20  m2)././ returns 
69c00 61 20 6e 65 77 20 67 65 6f 6d 65 74 72 79 20 72  a new geometry r
69c10 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
69c20 55 4e 49 4f 4e 20 6f 66 20 62 6f 74 68 20 67 65  UNION of both ge
69c30 6f 6d 65 74 72 69 65 73 0a 2f 20 6f 72 20 4e 55  ometries./ or NU
69c40 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
69c50 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
69c60 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
69c70 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20  har *p_blob;.   
69c80 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20   int n_bytes;.  
69c90 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
69ca0 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20  r geo1 = NULL;. 
69cb0 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
69cc0 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a  tr geo2 = NULL;.
69cd0 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
69ce0 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20 20  Ptr result;.    
69cf0 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
69d00 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
69d10 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
69d20 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
69d30 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
69d40 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
69d50 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
69d60 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
69d70 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
69d80 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
69d90 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
69da0 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20  pe (argv[1]) != 
69db0 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
69dc0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
69dd0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
69de0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
69df0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
69e00 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
69e10 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
69e20 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
69e30 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
69e40 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
69e50 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
69e60 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20 67  ]);.    geo1 = g
69e70 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
69e80 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
69e90 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
69ea0 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
69eb0 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
69ec0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
69ed0 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[1]);.    n_b
69ee0 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
69ef0 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
69f00 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f 32 20 3d  [1]);.    geo2 =
69f10 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
69f20 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
69f30 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
69f40 20 20 69 66 20 28 21 67 65 6f 31 20 7c 7c 20 21    if (!geo1 || !
69f50 67 65 6f 32 29 0a 09 73 71 6c 69 74 65 33 5f 72  geo2)..sqlite3_r
69f60 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
69f70 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20  ext);.    else. 
69f80 20 20 20 20 20 7b 0a 09 20 20 72 65 73 75 6c 74       {..  result
69f90 20 3d 20 67 61 69 61 47 65 6f 6d 65 74 72 79 55   = gaiaGeometryU
69fa0 6e 69 6f 6e 20 28 67 65 6f 31 2c 20 67 65 6f 32  nion (geo1, geo2
69fb0 29 3b 0a 09 20 20 69 66 20 28 21 72 65 73 75 6c  );..  if (!resul
69fc0 74 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  t)..      sqlite
69fd0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
69fe0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65  ontext);..  else
69ff0 20 69 66 20 28 67 61 69 61 49 73 45 6d 70 74 79   if (gaiaIsEmpty
6a000 20 28 72 65 73 75 6c 74 29 29 0a 09 20 20 20 20   (result))..    
6a010 7b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d  {...gaiaFreeGeom
6a020 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09  Coll (result);..
6a030 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
6a040 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
6a050 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09  .    }..  else..
6a060 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75 69 6c 64      {.../* build
6a070 73 20 74 68 65 20 42 4c 4f 42 20 67 65 6f 6d 65  s the BLOB geome
6a080 74 72 79 20 74 6f 20 62 65 20 72 65 74 75 72 6e  try to be return
6a090 65 64 20 2a 2f 0a 09 09 69 6e 74 20 6c 65 6e 3b  ed */...int len;
6a0a0 0a 09 09 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ...unsigned char
6a0b0 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c   *p_result = NUL
6a0c0 4c 3b 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69  L;...gaiaToSpati
6a0d0 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72 65  aLiteBlobWkb (re
6a0e0 73 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c  sult, &p_result,
6a0f0 20 26 6c 65 6e 29 3b 0a 09 09 73 71 6c 69 74 65   &len);...sqlite
6a100 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63  3_result_blob (c
6a110 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74  ontext, p_result
6a120 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09  , len, free);...
6a130 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
6a140 20 28 72 65 73 75 6c 74 29 3b 0a 09 20 20 20 20   (result);..    
6a150 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  }.      }.    ga
6a160 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
6a170 67 65 6f 31 29 3b 0a 20 20 20 20 67 61 69 61 46  geo1);.    gaiaF
6a180 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
6a190 32 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  2);.}..static vo
6a1a0 69 64 0a 66 6e 63 74 5f 44 69 66 66 65 72 65 6e  id.fnct_Differen
6a1b0 63 65 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ce (sqlite3_cont
6a1c0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
6a1d0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
6a1e0 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
6a1f0 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
6a200 6e 3a 0a 2f 20 44 69 66 66 65 72 65 6e 63 65 28  n:./ Difference(
6a210 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d  BLOBencoded geom
6a220 31 2c 20 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67  1, BLOBencoded g
6a230 65 6f 6d 32 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  eom2)././ return
6a240 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74 72 79  s a new geometry
6a250 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
6a260 65 20 44 49 46 46 45 52 45 4e 43 45 20 6f 66 20  e DIFFERENCE of 
6a270 62 6f 74 68 20 67 65 6f 6d 65 74 72 69 65 73 0a  both geometries.
6a280 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79  / or NULL if any
6a290 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
6a2a0 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73  tered.*/.    uns
6a2b0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
6a2c0 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  ob;.    int n_by
6a2d0 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  tes;.    gaiaGeo
6a2e0 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 31 20 3d 20  mCollPtr geo1 = 
6a2f0 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65  NULL;.    gaiaGe
6a300 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 32 20 3d  omCollPtr geo2 =
6a310 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47   NULL;.    gaiaG
6a320 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c  eomCollPtr resul
6a330 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  t;.    GAIA_UNUS
6a340 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
6a350 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
6a360 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
6a370 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d  ue_type (argv[0]
6a380 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  ) != SQLITE_BLOB
6a390 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
6a3a0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
6a3b0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
6a3c0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
6a3d0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
6a3e0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
6a3f0 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  1]) != SQLITE_BL
6a400 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
6a410 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
6a420 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
6a430 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
6a440 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
6a450 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
6a460 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
6a470 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ob (argv[0]);.  
6a480 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
6a490 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
6a4a0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67  (argv[0]);.    g
6a4b0 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  eo1 = gaiaFromSp
6a4c0 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
6a4d0 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
6a4e0 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20  );.    p_blob = 
6a4f0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
6a500 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
6a510 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a  blob (argv[1]);.
6a520 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
6a530 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
6a540 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  s (argv[1]);.   
6a550 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d   geo2 = gaiaFrom
6a560 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
6a570 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
6a580 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65  es);.    if (!ge
6a590 6f 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09 73 71  o1 || !geo2)..sq
6a5a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
6a5b0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
6a5c0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
6a5d0 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 47 65   result = gaiaGe
6a5e0 6f 6d 65 74 72 79 44 69 66 66 65 72 65 6e 63 65  ometryDifference
6a5f0 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09   (geo1, geo2);..
6a600 20 20 69 66 20 28 21 72 65 73 75 6c 74 29 0a 09    if (!result)..
6a610 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
6a620 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
6a630 78 74 29 3b 0a 09 20 20 65 6c 73 65 20 69 66 20  xt);..  else if 
6a640 28 67 61 69 61 49 73 45 6d 70 74 79 20 28 72 65  (gaiaIsEmpty (re
6a650 73 75 6c 74 29 29 0a 09 20 20 20 20 7b 0a 09 09  sult))..    {...
6a660 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
6a670 20 28 72 65 73 75 6c 74 29 3b 0a 09 09 73 71 6c   (result);...sql
6a680 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
6a690 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 20   (context);..   
6a6a0 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20   }..  else..    
6a6b0 7b 0a 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68  {.../* builds th
6a6c0 65 20 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20  e BLOB geometry 
6a6d0 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
6a6e0 2f 0a 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75  /...int len;...u
6a6f0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
6a700 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09  result = NULL;..
6a710 09 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74  .gaiaToSpatiaLit
6a720 65 42 6c 6f 62 57 6b 62 20 28 72 65 73 75 6c 74  eBlobWkb (result
6a730 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65  , &p_result, &le
6a740 6e 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  n);...sqlite3_re
6a750 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
6a760 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
6a770 6e 2c 20 66 72 65 65 29 3b 0a 09 09 67 61 69 61  n, free);...gaia
6a780 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65  FreeGeomColl (re
6a790 73 75 6c 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20  sult);..    }.  
6a7a0 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72      }.    gaiaFr
6a7b0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31  eeGeomColl (geo1
6a7c0 29 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65 47  );.    gaiaFreeG
6a7d0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a  eomColl (geo2);.
6a7e0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
6a7f0 6e 63 74 5f 53 79 6d 44 69 66 66 65 72 65 6e 63  nct_SymDifferenc
6a800 65 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e (sqlite3_conte
6a810 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
6a820 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
6a830 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
6a840 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ./* SQL function
6a850 3a 0a 2f 20 53 79 6d 44 69 66 66 65 72 65 6e 63  :./ SymDifferenc
6a860 65 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65  e(BLOBencoded ge
6a870 6f 6d 31 2c 20 42 4c 4f 42 65 6e 63 6f 64 65 64  om1, BLOBencoded
6a880 20 67 65 6f 6d 32 29 0a 2f 0a 2f 20 72 65 74 75   geom2)././ retu
6a890 72 6e 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74  rns a new geomet
6a8a0 72 79 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  ry representing 
6a8b0 74 68 65 20 53 59 4d 4d 45 54 52 49 43 20 44 49  the SYMMETRIC DI
6a8c0 46 46 45 52 45 4e 43 45 20 6f 66 20 62 6f 74 68  FFERENCE of both
6a8d0 20 67 65 6f 6d 65 74 72 69 65 73 0a 2f 20 6f 72   geometries./ or
6a8e0 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
6a8f0 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
6a900 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  d.*/.    unsigne
6a910 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
6a920 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
6a930 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
6a940 6c 50 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c  lPtr geo1 = NULL
6a950 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
6a960 6c 6c 50 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c  llPtr geo2 = NUL
6a970 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
6a980 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20  ollPtr result;. 
6a990 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
6a9a0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
6a9b0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
6a9c0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
6a9d0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
6a9e0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
6a9f0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
6aa00 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
6aa10 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
6aa20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
6aa30 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
6aa40 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20  _type (argv[1]) 
6aa50 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
6aa60 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
6aa70 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
6aa80 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
6aa90 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
6aaa0 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
6aab0 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
6aac0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
6aad0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
6aae0 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
6aaf0 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
6ab00 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20  v[0]);.    geo1 
6ab10 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
6ab20 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
6ab30 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
6ab40 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73     p_blob = (uns
6ab50 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71  igned char *) sq
6ab60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
6ab70 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20   (argv[1]);.    
6ab80 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65  n_bytes = sqlite
6ab90 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61  3_value_bytes (a
6aba0 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[1]);.    geo
6abb0 32 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  2 = gaiaFromSpat
6abc0 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
6abd0 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
6abe0 0a 20 20 20 20 69 66 20 28 21 67 65 6f 31 20 7c  .    if (!geo1 |
6abf0 7c 20 21 67 65 6f 32 29 0a 09 73 71 6c 69 74 65  | !geo2)..sqlite
6ac00 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
6ac10 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73  ontext);.    els
6ac20 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65 73  e.      {..  res
6ac30 75 6c 74 20 3d 20 67 61 69 61 47 65 6f 6d 65 74  ult = gaiaGeomet
6ac40 72 79 53 79 6d 44 69 66 66 65 72 65 6e 63 65 20  rySymDifference 
6ac50 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20  (geo1, geo2);.. 
6ac60 20 69 66 20 28 21 72 65 73 75 6c 74 29 0a 09 20   if (!result).. 
6ac70 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
6ac80 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
6ac90 74 29 3b 0a 09 20 20 65 6c 73 65 20 69 66 20 28  t);..  else if (
6aca0 67 61 69 61 49 73 45 6d 70 74 79 20 28 72 65 73  gaiaIsEmpty (res
6acb0 75 6c 74 29 29 0a 09 20 20 20 20 7b 0a 09 09 67  ult))..    {...g
6acc0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
6acd0 28 72 65 73 75 6c 74 29 3b 0a 09 09 73 71 6c 69  (result);...sqli
6ace0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
6acf0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 20 20  (context);..    
6ad00 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b  }..  else..    {
6ad10 0a 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65  .../* builds the
6ad20 20 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74   BLOB geometry t
6ad30 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f  o be returned */
6ad40 0a 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e  ...int len;...un
6ad50 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72  signed char *p_r
6ad60 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09  esult = NULL;...
6ad70 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65  gaiaToSpatiaLite
6ad80 42 6c 6f 62 57 6b 62 20 28 72 65 73 75 6c 74 2c  BlobWkb (result,
6ad90 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e   &p_result, &len
6ada0 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73  );...sqlite3_res
6adb0 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78  ult_blob (contex
6adc0 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e  t, p_result, len
6add0 2c 20 66 72 65 65 29 3b 0a 09 09 67 61 69 61 46  , free);...gaiaF
6ade0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73  reeGeomColl (res
6adf0 75 6c 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20  ult);..    }.   
6ae00 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65     }.    gaiaFre
6ae10 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29  eGeomColl (geo1)
6ae20 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  ;.    gaiaFreeGe
6ae30 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d  omColl (geo2);.}
6ae40 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
6ae50 63 74 5f 45 71 75 61 6c 73 20 28 73 71 6c 69 74  ct_Equals (sqlit
6ae60 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
6ae70 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
6ae80 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
6ae90 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20   argv).{./* SQL 
6aea0 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 45 71 75 61  function:./ Equa
6aeb0 6c 73 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67  ls(BLOBencoded g
6aec0 65 6f 6d 31 2c 20 42 4c 4f 42 65 6e 63 6f 64 65  eom1, BLOBencode
6aed0 64 20 67 65 6f 6d 32 29 0a 2f 0a 2f 20 72 65 74  d geom2)././ ret
6aee0 75 72 6e 73 3a 0a 2f 20 31 20 69 66 20 74 68 65  urns:./ 1 if the
6aef0 20 74 77 6f 20 67 65 6f 6d 65 74 72 69 65 73 20   two geometries 
6af00 61 72 65 20 22 73 70 61 74 69 61 6c 6c 79 20 65  are "spatially e
6af10 71 75 61 6c 22 0a 2f 20 30 20 6f 74 68 65 72 77  qual"./ 0 otherw
6af20 69 73 65 0a 2f 20 6f 72 20 2d 31 20 69 66 20 61  ise./ or -1 if a
6af30 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
6af40 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75  untered.*/.    u
6af50 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
6af60 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
6af70 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47  bytes;.    gaiaG
6af80 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 31 20  eomCollPtr geo1 
6af90 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
6afa0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 32  GeomCollPtr geo2
6afb0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74   = NULL;.    int
6afc0 20 72 65 74 3b 0a 20 20 20 20 47 41 49 41 5f 55   ret;.    GAIA_U
6afd0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
6afe0 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
6aff0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
6b000 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
6b010 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
6b020 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
6b030 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
6b040 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29  nt (context, -1)
6b050 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
6b060 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c     }.    if (sql
6b070 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
6b080 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c  (argv[1]) != SQL
6b090 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
6b0a0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
6b0b0 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
6b0c0 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e  , -1);..  return
6b0d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
6b0e0 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
6b0f0 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
6b100 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
6b110 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
6b120 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
6b130 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
6b140 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20 67  ]);.    geo1 = g
6b150 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
6b160 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
6b170 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
6b180 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e  p_blob = (unsign
6b190 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74  ed char *) sqlit
6b1a0 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61  e3_value_blob (a
6b1b0 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f 62  rgv[1]);.    n_b
6b1c0 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  ytes = sqlite3_v
6b1d0 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76  alue_bytes (argv
6b1e0 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f 32 20 3d  [1]);.    geo2 =
6b1f0 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c   gaiaFromSpatiaL
6b200 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c  iteBlobWkb (p_bl
6b210 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20  ob, n_bytes);.  
6b220 20 20 69 66 20 28 21 67 65 6f 31 20 7c 7c 20 21    if (!geo1 || !
6b230 67 65 6f 32 29 0a 09 73 71 6c 69 74 65 33 5f 72  geo2)..sqlite3_r
6b240 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
6b250 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20 65 6c 73  xt, -1);.    els
6b260 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65 74  e.      {..  ret
6b270 20 3d 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 45   = gaiaGeomCollE
6b280 71 75 61 6c 73 20 28 67 65 6f 31 2c 20 67 65 6f  quals (geo1, geo
6b290 32 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72  2);..  sqlite3_r
6b2a0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
6b2b0 78 74 2c 20 72 65 74 29 3b 0a 20 20 20 20 20 20  xt, ret);.      
6b2c0 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
6b2d0 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a 20  omColl (geo1);. 
6b2e0 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
6b2f0 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a 73  oll (geo2);.}..s
6b300 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
6b310 49 6e 74 65 72 73 65 63 74 73 20 28 73 71 6c 69  Intersects (sqli
6b320 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
6b330 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
6b340 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
6b350 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
6b360 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 49 6e 74   function:./ Int
6b370 65 72 73 65 63 74 73 28 42 4c 4f 42 65 6e 63 6f  ersects(BLOBenco
6b380 64 65 64 20 67 65 6f 6d 31 2c 20 42 4c 4f 42 65  ded geom1, BLOBe
6b390 6e 63 6f 64 65 64 20 67 65 6f 6d 32 29 0a 2f 0a  ncoded geom2)./.
6b3a0 2f 20 72 65 74 75 72 6e 73 3a 0a 2f 20 31 20 69  / returns:./ 1 i
6b3b0 66 20 74 68 65 20 74 77 6f 20 67 65 6f 6d 65 74  f the two geomet
6b3c0 72 69 65 73 20 64 6f 20 22 73 70 61 74 69 61 6c  ries do "spatial
6b3d0 6c 79 20 69 6e 74 65 72 73 65 63 74 73 22 0a 2f  ly intersects"./
6b3e0 20 30 20 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f   0 otherwise./ o
6b3f0 72 20 2d 31 20 69 66 20 61 6e 79 20 65 72 72 6f  r -1 if any erro
6b400 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
6b410 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  .*/.    unsigned
6b420 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20   char *p_blob;. 
6b430 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a     int n_bytes;.
6b440 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
6b450 50 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b  Ptr geo1 = NULL;
6b460 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
6b470 6c 50 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c  lPtr geo2 = NULL
6b480 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20  ;.    int ret;. 
6b490 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
6b4a0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
6b4b0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
6b4c0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
6b4d0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
6b4e0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
6b4f0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
6b500 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
6b510 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65  text, -1);..  re
6b520 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
6b530 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
6b540 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
6b550 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
6b560 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
6b570 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
6b580 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a   (context, -1);.
6b590 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
6b5a0 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
6b5b0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
6b5c0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
6b5d0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
6b5e0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
6b5f0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
6b600 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
6b610 20 67 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d   geo1 = gaiaFrom
6b620 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
6b630 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
6b640 65 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20  es);.    p_blob 
6b650 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
6b660 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
6b670 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29  e_blob (argv[1])
6b680 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
6b690 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
6b6a0 74 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20  tes (argv[1]);. 
6b6b0 20 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72     geo2 = gaiaFr
6b6c0 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
6b6d0 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
6b6e0 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
6b6f0 67 65 6f 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09  geo1 || !geo2)..
6b700 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
6b710 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29  nt (context, -1)
6b720 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
6b730 20 7b 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61   {..  ret = gaia
6b740 47 65 6f 6d 43 6f 6c 6c 49 6e 74 65 72 73 65 63  GeomCollIntersec
6b750 74 73 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b  ts (geo1, geo2);
6b760 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
6b770 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
6b780 20 72 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20   ret);.      }. 
6b790 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
6b7a0 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a 20 20 20 20  oll (geo1);.    
6b7b0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
6b7c0 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a 73 74 61 74   (geo2);.}..stat
6b7d0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 44 69 73  ic void.fnct_Dis
6b7e0 6a 6f 69 6e 74 20 28 73 71 6c 69 74 65 33 5f 63  joint (sqlite3_c
6b7f0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
6b800 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
6b810 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
6b820 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
6b830 74 69 6f 6e 3a 0a 2f 20 44 69 73 6a 6f 69 6e 74  tion:./ Disjoint
6b840 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f  (BLOBencoded geo
6b850 6d 31 2c 20 42 4c 4f 42 65 6e 63 6f 64 65 64 20  m1, BLOBencoded 
6b860 67 65 6f 6d 32 29 0a 2f 0a 2f 20 72 65 74 75 72  geom2)././ retur
6b870 6e 73 3a 0a 2f 20 31 20 69 66 20 74 68 65 20 74  ns:./ 1 if the t
6b880 77 6f 20 67 65 6f 6d 65 74 72 69 65 73 20 61 72  wo geometries ar
6b890 65 20 22 73 70 61 74 69 61 6c 6c 79 20 64 69 73  e "spatially dis
6b8a0 6a 6f 69 6e 74 22 0a 2f 20 30 20 6f 74 68 65 72  joint"./ 0 other
6b8b0 77 69 73 65 0a 2f 20 6f 72 20 2d 31 20 69 66 20  wise./ or -1 if 
6b8c0 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
6b8d0 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
6b8e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
6b8f0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
6b900 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61  _bytes;.    gaia
6b910 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 31  GeomCollPtr geo1
6b920 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
6b930 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
6b940 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  2 = NULL;.    in
6b950 74 20 72 65 74 3b 0a 20 20 20 20 47 41 49 41 5f  t ret;.    GAIA_
6b960 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
6b970 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
6b980 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
6b990 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
6b9a0 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[0]) != SQLITE_
6b9b0 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
6b9c0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
6b9d0 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31  int (context, -1
6b9e0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
6b9f0 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71      }.    if (sq
6ba00 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
6ba10 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51   (argv[1]) != SQ
6ba20 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
6ba30 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
6ba40 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
6ba50 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72  t, -1);..  retur
6ba60 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
6ba70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
6ba80 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
6ba90 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
6baa0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
6bab0 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
6bac0 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
6bad0 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20  0]);.    geo1 = 
6bae0 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
6baf0 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
6bb00 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
6bb10 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
6bb20 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
6bb30 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
6bb40 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f  argv[1]);.    n_
6bb50 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
6bb60 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
6bb70 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f 32 20  v[1]);.    geo2 
6bb80 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
6bb90 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
6bba0 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
6bbb0 20 20 20 69 66 20 28 21 67 65 6f 31 20 7c 7c 20     if (!geo1 || 
6bbc0 21 67 65 6f 32 29 0a 09 73 71 6c 69 74 65 33 5f  !geo2)..sqlite3_
6bbd0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
6bbe0 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20 65 6c  ext, -1);.    el
6bbf0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65  se.      {..  re
6bc00 74 20 3d 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c  t = gaiaGeomColl
6bc10 44 69 73 6a 6f 69 6e 74 20 28 67 65 6f 31 2c 20  Disjoint (geo1, 
6bc20 67 65 6f 32 29 3b 0a 09 20 20 73 71 6c 69 74 65  geo2);..  sqlite
6bc30 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
6bc40 6e 74 65 78 74 2c 20 72 65 74 29 3b 0a 20 20 20  ntext, ret);.   
6bc50 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65     }.    gaiaFre
6bc60 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29  eGeomColl (geo1)
6bc70 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  ;.    gaiaFreeGe
6bc80 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d  omColl (geo2);.}
6bc90 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
6bca0 63 74 5f 4f 76 65 72 6c 61 70 73 20 28 73 71 6c  ct_Overlaps (sql
6bcb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
6bcc0 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
6bcd0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
6bce0 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
6bcf0 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4f 76  L function:./ Ov
6bd00 65 72 6c 61 70 73 28 42 4c 4f 42 65 6e 63 6f 64  erlaps(BLOBencod
6bd10 65 64 20 67 65 6f 6d 31 2c 20 42 4c 4f 42 65 6e  ed geom1, BLOBen
6bd20 63 6f 64 65 64 20 67 65 6f 6d 32 29 0a 2f 0a 2f  coded geom2)././
6bd30 20 72 65 74 75 72 6e 73 3a 0a 2f 20 31 20 69 66   returns:./ 1 if
6bd40 20 74 68 65 20 74 77 6f 20 67 65 6f 6d 65 74 72   the two geometr
6bd50 69 65 73 20 64 6f 20 22 73 70 61 74 69 61 6c 6c  ies do "spatiall
6bd60 79 20 6f 76 65 72 6c 61 70 73 22 0a 2f 20 30 20  y overlaps"./ 0 
6bd70 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f 72 20 2d  otherwise./ or -
6bd80 31 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69  1 if any error i
6bd90 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f  s encountered.*/
6bda0 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
6bdb0 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20  ar *p_blob;.    
6bdc0 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
6bdd0 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
6bde0 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20   geo1 = NULL;.  
6bdf0 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
6be00 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20  r geo2 = NULL;. 
6be10 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20     int ret;.    
6be20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
6be30 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
6be40 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
6be50 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
6be60 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
6be70 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
6be80 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
6be90 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
6bea0 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72  t, -1);..  retur
6beb0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
6bec0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
6bed0 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20  _type (argv[1]) 
6bee0 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
6bef0 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
6bf00 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
6bf10 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20  ontext, -1);..  
6bf20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
6bf30 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
6bf40 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
6bf50 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
6bf60 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
6bf70 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
6bf80 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
6bf90 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
6bfa0 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  o1 = gaiaFromSpa
6bfb0 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
6bfc0 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
6bfd0 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  ;.    p_blob = (
6bfe0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
6bff0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
6c000 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20  lob (argv[1]);. 
6c010 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
6c020 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
6c030 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20   (argv[1]);.    
6c040 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d 53  geo2 = gaiaFromS
6c050 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
6c060 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
6c070 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
6c080 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09 73 71 6c  1 || !geo2)..sql
6c090 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
6c0a0 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 20  (context, -1);. 
6c0b0 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
6c0c0 09 20 20 72 65 74 20 3d 20 67 61 69 61 47 65 6f  .  ret = gaiaGeo
6c0d0 6d 43 6f 6c 6c 4f 76 65 72 6c 61 70 73 20 28 67  mCollOverlaps (g
6c0e0 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20 20 73  eo1, geo2);..  s
6c0f0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
6c100 74 20 28 63 6f 6e 74 65 78 74 2c 20 72 65 74 29  t (context, ret)
6c110 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61  ;.      }.    ga
6c120 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
6c130 67 65 6f 31 29 3b 0a 20 20 20 20 67 61 69 61 46  geo1);.    gaiaF
6c140 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
6c150 32 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  2);.}..static vo
6c160 69 64 0a 66 6e 63 74 5f 43 72 6f 73 73 65 73 20  id.fnct_Crosses 
6c170 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
6c180 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
6c190 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
6c1a0 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
6c1b0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
6c1c0 2f 20 43 72 6f 73 73 65 73 28 42 4c 4f 42 65 6e  / Crosses(BLOBen
6c1d0 63 6f 64 65 64 20 67 65 6f 6d 31 2c 20 42 4c 4f  coded geom1, BLO
6c1e0 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 32 29 0a  Bencoded geom2).
6c1f0 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f 20 31  /./ returns:./ 1
6c200 20 69 66 20 74 68 65 20 74 77 6f 20 67 65 6f 6d   if the two geom
6c210 65 74 72 69 65 73 20 64 6f 20 22 73 70 61 74 69  etries do "spati
6c220 61 6c 6c 79 20 63 72 6f 73 73 65 73 22 0a 2f 20  ally crosses"./ 
6c230 30 20 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f 72  0 otherwise./ or
6c240 20 2d 31 20 69 66 20 61 6e 79 20 65 72 72 6f 72   -1 if any error
6c250 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
6c260 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
6c270 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
6c280 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
6c290 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
6c2a0 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a  tr geo1 = NULL;.
6c2b0 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
6c2c0 50 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b  Ptr geo2 = NULL;
6c2d0 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20  .    int ret;.  
6c2e0 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
6c2f0 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
6c300 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
6c310 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
6c320 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
6c330 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
6c340 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
6c350 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
6c360 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74  ext, -1);..  ret
6c370 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
6c380 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
6c390 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d  ue_type (argv[1]
6c3a0 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  ) != SQLITE_BLOB
6c3b0 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
6c3c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
6c3d0 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09  (context, -1);..
6c3e0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
6c3f0 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
6c400 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
6c410 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
6c420 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
6c430 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
6c440 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
6c450 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
6c460 67 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53  geo1 = gaiaFromS
6c470 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
6c480 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
6c490 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d  s);.    p_blob =
6c4a0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
6c4b0 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
6c4c0 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b  _blob (argv[1]);
6c4d0 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
6c4e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
6c4f0 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  es (argv[1]);.  
6c500 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f    geo2 = gaiaFro
6c510 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
6c520 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
6c530 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
6c540 65 6f 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09 73  eo1 || !geo2)..s
6c550 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
6c560 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b  t (context, -1);
6c570 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
6c580 7b 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61 47  {..  ret = gaiaG
6c590 65 6f 6d 43 6f 6c 6c 43 72 6f 73 73 65 73 20 28  eomCollCrosses (
6c5a0 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20 20  geo1, geo2);..  
6c5b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
6c5c0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 72 65 74  nt (context, ret
6c5d0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67  );.      }.    g
6c5e0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
6c5f0 28 67 65 6f 31 29 3b 0a 20 20 20 20 67 61 69 61  (geo1);.    gaia
6c600 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
6c610 6f 32 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  o2);.}..static v
6c620 6f 69 64 0a 66 6e 63 74 5f 54 6f 75 63 68 65 73  oid.fnct_Touches
6c630 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
6c640 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
6c650 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
6c660 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
6c670 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
6c680 0a 2f 20 54 6f 75 63 68 65 73 28 42 4c 4f 42 65  ./ Touches(BLOBe
6c690 6e 63 6f 64 65 64 20 67 65 6f 6d 31 2c 20 42 4c  ncoded geom1, BL
6c6a0 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 32 29  OBencoded geom2)
6c6b0 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f 20  ././ returns:./ 
6c6c0 31 20 69 66 20 74 68 65 20 74 77 6f 20 67 65 6f  1 if the two geo
6c6d0 6d 65 74 72 69 65 73 20 64 6f 20 22 73 70 61 74  metries do "spat
6c6e0 69 61 6c 6c 79 20 74 6f 75 63 68 65 73 22 0a 2f  ially touches"./
6c6f0 20 30 20 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f   0 otherwise./ o
6c700 72 20 2d 31 20 69 66 20 61 6e 79 20 65 72 72 6f  r -1 if any erro
6c710 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
6c720 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  .*/.    unsigned
6c730 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20   char *p_blob;. 
6c740 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a     int n_bytes;.
6c750 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
6c760 50 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b  Ptr geo1 = NULL;
6c770 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
6c780 6c 50 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c  lPtr geo2 = NULL
6c790 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20  ;.    int ret;. 
6c7a0 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
6c7b0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
6c7c0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
6c7d0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
6c7e0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
6c7f0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
6c800 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
6c810 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
6c820 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65  text, -1);..  re
6c830 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
6c840 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
6c850 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
6c860 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
6c870 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
6c880 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
6c890 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a   (context, -1);.
6c8a0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
6c8b0 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
6c8c0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
6c8d0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
6c8e0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
6c8f0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
6c900 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
6c910 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
6c920 20 67 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d   geo1 = gaiaFrom
6c930 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
6c940 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
6c950 65 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20  es);.    p_blob 
6c960 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
6c970 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
6c980 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29  e_blob (argv[1])
6c990 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
6c9a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
6c9b0 74 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20  tes (argv[1]);. 
6c9c0 20 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72     geo2 = gaiaFr
6c9d0 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
6c9e0 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
6c9f0 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
6ca00 67 65 6f 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09  geo1 || !geo2)..
6ca10 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
6ca20 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29  nt (context, -1)
6ca30 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
6ca40 20 7b 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61   {..  ret = gaia
6ca50 47 65 6f 6d 43 6f 6c 6c 54 6f 75 63 68 65 73 20  GeomCollTouches 
6ca60 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20  (geo1, geo2);.. 
6ca70 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
6ca80 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 72 65  int (context, re
6ca90 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
6caa0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
6cab0 20 28 67 65 6f 31 29 3b 0a 20 20 20 20 67 61 69   (geo1);.    gai
6cac0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
6cad0 65 6f 32 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  eo2);.}..static 
6cae0 76 6f 69 64 0a 66 6e 63 74 5f 57 69 74 68 69 6e  void.fnct_Within
6caf0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
6cb00 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
6cb10 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
6cb20 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
6cb30 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
6cb40 0a 2f 20 57 69 74 68 69 6e 28 42 4c 4f 42 65 6e  ./ Within(BLOBen
6cb50 63 6f 64 65 64 20 67 65 6f 6d 31 2c 20 42 4c 4f  coded geom1, BLO
6cb60 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 32 29 0a  Bencoded geom2).
6cb70 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f 20 31  /./ returns:./ 1
6cb80 20 69 66 20 47 45 4f 4d 2d 31 20 69 73 20 63 6f   if GEOM-1 is co
6cb90 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69 6e  mpletely contain
6cba0 65 64 20 77 69 74 68 69 6e 20 47 45 4f 4d 2d 32  ed within GEOM-2
6cbb0 0a 2f 20 30 20 6f 74 68 65 72 77 69 73 65 0a 2f  ./ 0 otherwise./
6cbc0 20 6f 72 20 2d 31 20 69 66 20 61 6e 79 20 65 72   or -1 if any er
6cbd0 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
6cbe0 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ed.*/.    unsign
6cbf0 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
6cc00 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
6cc10 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
6cc20 6c 6c 50 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c  llPtr geo1 = NUL
6cc30 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  L;.    gaiaGeomC
6cc40 6f 6c 6c 50 74 72 20 67 65 6f 32 20 3d 20 4e 55  ollPtr geo2 = NU
6cc50 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b  LL;.    int ret;
6cc60 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
6cc70 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
6cc80 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
6cc90 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
6cca0 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
6ccb0 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
6ccc0 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
6ccd0 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
6cce0 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20  ontext, -1);..  
6ccf0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
6cd00 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
6cd10 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
6cd20 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [1]) != SQLITE_B
6cd30 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
6cd40 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
6cd50 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29  nt (context, -1)
6cd60 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
6cd70 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20     }.    p_blob 
6cd80 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
6cd90 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
6cda0 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
6cdb0 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
6cdc0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
6cdd0 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  tes (argv[0]);. 
6cde0 20 20 20 67 65 6f 31 20 3d 20 67 61 69 61 46 72     geo1 = gaiaFr
6cdf0 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
6ce00 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
6ce10 79 74 65 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f  ytes);.    p_blo
6ce20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
6ce30 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
6ce40 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31  lue_blob (argv[1
6ce50 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20  ]);.    n_bytes 
6ce60 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
6ce70 62 79 74 65 73 20 28 61 72 67 76 5b 31 5d 29 3b  bytes (argv[1]);
6ce80 0a 20 20 20 20 67 65 6f 32 20 3d 20 67 61 69 61  .    geo2 = gaia
6ce90 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
6cea0 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
6ceb0 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
6cec0 28 21 67 65 6f 31 20 7c 7c 20 21 67 65 6f 32 29  (!geo1 || !geo2)
6ced0 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
6cee0 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d  _int (context, -
6cef0 31 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  1);.    else.   
6cf00 20 20 20 7b 0a 09 20 20 72 65 74 20 3d 20 67 61     {..  ret = ga
6cf10 69 61 47 65 6f 6d 43 6f 6c 6c 57 69 74 68 69 6e  iaGeomCollWithin
6cf20 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09   (geo1, geo2);..
6cf30 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
6cf40 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 72  _int (context, r
6cf50 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  et);.      }.   
6cf60 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
6cf70 6c 20 28 67 65 6f 31 29 3b 0a 20 20 20 20 67 61  l (geo1);.    ga
6cf80 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
6cf90 67 65 6f 32 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  geo2);.}..static
6cfa0 20 76 6f 69 64 0a 66 6e 63 74 5f 43 6f 6e 74 61   void.fnct_Conta
6cfb0 69 6e 73 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ins (sqlite3_con
6cfc0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
6cfd0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
6cfe0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
6cff0 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
6d000 6f 6e 3a 0a 2f 20 43 6f 6e 74 61 69 6e 73 28 42  on:./ Contains(B
6d010 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 31  LOBencoded geom1
6d020 2c 20 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65  , BLOBencoded ge
6d030 6f 6d 32 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73  om2)././ returns
6d040 3a 0a 2f 20 31 20 69 66 20 47 45 4f 4d 2d 31 20  :./ 1 if GEOM-1 
6d050 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61  completely conta
6d060 69 6e 73 20 47 45 4f 4d 2d 32 0a 2f 20 30 20 6f  ins GEOM-2./ 0 o
6d070 74 68 65 72 77 69 73 65 0a 2f 20 6f 72 20 2d 31  therwise./ or -1
6d080 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
6d090 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
6d0a0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
6d0b0 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69  r *p_blob;.    i
6d0c0 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
6d0d0 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
6d0e0 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  geo1 = NULL;.   
6d0f0 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
6d100 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20   geo2 = NULL;.  
6d110 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20 47    int ret;.    G
6d120 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
6d130 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
6d140 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
6d150 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
6d160 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
6d170 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
6d180 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
6d190 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
6d1a0 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e  , -1);..  return
6d1b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
6d1c0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
6d1d0 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21  type (argv[1]) !
6d1e0 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20  = SQLITE_BLOB). 
6d1f0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
6d200 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
6d210 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72  ntext, -1);..  r
6d220 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
6d230 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73     p_blob = (uns
6d240 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71  igned char *) sq
6d250 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
6d260 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
6d270 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65  n_bytes = sqlite
6d280 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61  3_value_bytes (a
6d290 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[0]);.    geo
6d2a0 31 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  1 = gaiaFromSpat
6d2b0 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
6d2c0 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
6d2d0 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75  .    p_blob = (u
6d2e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20  nsigned char *) 
6d2f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
6d300 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ob (argv[1]);.  
6d310 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69    n_bytes = sqli
6d320 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20  te3_value_bytes 
6d330 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 67  (argv[1]);.    g
6d340 65 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  eo2 = gaiaFromSp
6d350 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
6d360 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
6d370 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f 31  );.    if (!geo1
6d380 20 7c 7c 20 21 67 65 6f 32 29 0a 09 73 71 6c 69   || !geo2)..sqli
6d390 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
6d3a0 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 20 20  context, -1);.  
6d3b0 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
6d3c0 20 20 72 65 74 20 3d 20 67 61 69 61 47 65 6f 6d    ret = gaiaGeom
6d3d0 43 6f 6c 6c 43 6f 6e 74 61 69 6e 73 20 28 67 65  CollContains (ge
6d3e0 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20 20 73 71  o1, geo2);..  sq
6d3f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
6d400 20 28 63 6f 6e 74 65 78 74 2c 20 72 65 74 29 3b   (context, ret);
6d410 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
6d420 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
6d430 65 6f 31 29 3b 0a 20 20 20 20 67 61 69 61 46 72  eo1);.    gaiaFr
6d440 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32  eeGeomColl (geo2
6d450 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
6d460 64 0a 66 6e 63 74 5f 52 65 6c 61 74 65 20 28 73  d.fnct_Relate (s
6d470 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
6d480 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
6d490 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
6d4a0 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
6d4b0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
6d4c0 52 65 6c 61 74 65 28 42 4c 4f 42 65 6e 63 6f 64  Relate(BLOBencod
6d4d0 65 64 20 67 65 6f 6d 31 2c 20 42 4c 4f 42 65 6e  ed geom1, BLOBen
6d4e0 63 6f 64 65 64 20 67 65 6f 6d 32 2c 20 73 74 72  coded geom2, str
6d4f0 69 6e 67 20 70 61 74 74 65 72 6e 29 0a 2f 0a 2f  ing pattern)././
6d500 20 72 65 74 75 72 6e 73 3a 0a 2f 20 31 20 69 66   returns:./ 1 if
6d510 20 47 45 4f 4d 2d 31 20 61 6e 64 20 47 45 4f 4d   GEOM-1 and GEOM
6d520 2d 32 20 68 61 76 65 20 61 20 73 70 61 74 69 61  -2 have a spatia
6d530 6c 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 61  l relationship a
6d540 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  s specified by t
6d550 68 65 20 70 61 74 74 65 72 6e 4d 61 74 72 69 78  he patternMatrix
6d560 20 0a 2f 20 30 20 6f 74 68 65 72 77 69 73 65 0a   ./ 0 otherwise.
6d570 2f 20 6f 72 20 2d 31 20 69 66 20 61 6e 79 20 65  / or -1 if any e
6d580 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
6d590 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67  red.*/.    unsig
6d5a0 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62  ned char *p_blob
6d5b0 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65  ;.    int n_byte
6d5c0 73 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43  s;.    gaiaGeomC
6d5d0 6f 6c 6c 50 74 72 20 67 65 6f 31 20 3d 20 4e 55  ollPtr geo1 = NU
6d5e0 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  LL;.    gaiaGeom
6d5f0 43 6f 6c 6c 50 74 72 20 67 65 6f 32 20 3d 20 4e  CollPtr geo2 = N
6d600 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 65 74  ULL;.    int ret
6d610 3b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  ;.    const unsi
6d620 67 6e 65 64 20 63 68 61 72 20 2a 70 61 74 74 65  gned char *patte
6d630 72 6e 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  rn;.    GAIA_UNU
6d640 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
6d650 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
6d660 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
6d670 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
6d680 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
6d690 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
6d6a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
6d6b0 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a   (context, -1);.
6d6c0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
6d6d0 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74   }.    if (sqlit
6d6e0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
6d6f0 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54  rgv[1]) != SQLIT
6d700 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a  E_BLOB).      {.
6d710 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
6d720 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
6d730 2d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  -1);..  return;.
6d740 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
6d750 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
6d760 70 65 20 28 61 72 67 76 5b 32 5d 29 20 21 3d 20  pe (argv[2]) != 
6d770 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20  SQLITE_TEXT).   
6d780 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
6d790 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
6d7a0 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74  ext, -1);..  ret
6d7b0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
6d7c0 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
6d7d0 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
6d7e0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
6d7f0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f  argv[0]);.    n_
6d800 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
6d810 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
6d820 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20  v[0]);.    geo1 
6d830 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
6d840 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
6d850 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
6d860 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73     p_blob = (uns
6d870 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71  igned char *) sq
6d880 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
6d890 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20   (argv[1]);.    
6d8a0 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65  n_bytes = sqlite
6d8b0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61  3_value_bytes (a
6d8c0 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[1]);.    geo
6d8d0 32 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  2 = gaiaFromSpat
6d8e0 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
6d8f0 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
6d900 0a 20 20 20 20 70 61 74 74 65 72 6e 20 3d 20 73  .    pattern = s
6d910 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
6d920 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  t (argv[2]);.   
6d930 20 69 66 20 28 21 67 65 6f 31 20 7c 7c 20 21 67   if (!geo1 || !g
6d940 65 6f 32 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  eo2)..sqlite3_re
6d950 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
6d960 74 2c 20 2d 31 29 3b 0a 20 20 20 20 65 6c 73 65  t, -1);.    else
6d970 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65 74 20  .      {..  ret 
6d980 3d 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 52 65  = gaiaGeomCollRe
6d990 6c 61 74 65 20 28 67 65 6f 31 2c 20 67 65 6f 32  late (geo1, geo2
6d9a0 2c 20 28 63 68 61 72 20 2a 29 20 70 61 74 74 65  , (char *) patte
6d9b0 72 6e 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  rn);..  sqlite3_
6d9c0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
6d9d0 65 78 74 2c 20 72 65 74 29 3b 0a 20 20 20 20 20  ext, ret);.     
6d9e0 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47   }.    gaiaFreeG
6d9f0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a  eomColl (geo1);.
6da00 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
6da10 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a  Coll (geo2);.}..
6da20 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
6da30 5f 44 69 73 74 61 6e 63 65 20 28 73 71 6c 69 74  _Distance (sqlit
6da40 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
6da50 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
6da60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
6da70 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20   argv).{./* SQL 
6da80 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 44 69 73 74  function:./ Dist
6da90 61 6e 63 65 28 42 4c 4f 42 65 6e 63 6f 64 65 64  ance(BLOBencoded
6daa0 20 67 65 6f 6d 31 2c 20 42 4c 4f 42 65 6e 63 6f   geom1, BLOBenco
6dab0 64 65 64 20 67 65 6f 6d 32 29 0a 2f 0a 2f 20 72  ded geom2)././ r
6dac0 65 74 75 72 6e 73 20 74 68 65 20 64 69 73 74 61  eturns the dista
6dad0 6e 63 65 20 62 65 74 77 65 65 6e 20 47 45 4f 4d  nce between GEOM
6dae0 2d 31 20 61 6e 64 20 47 45 4f 4d 2d 32 0a 2a 2f  -1 and GEOM-2.*/
6daf0 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
6db00 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20  ar *p_blob;.    
6db10 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
6db20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
6db30 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20   geo1 = NULL;.  
6db40 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
6db50 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20  r geo2 = NULL;. 
6db60 20 20 20 64 6f 75 62 6c 65 20 64 69 73 74 3b 0a     double dist;.
6db70 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
6db80 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
6db90 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
6dba0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
6dbb0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
6dbc0 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
6dbd0 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20  QLITE_BLOB).    
6dbe0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
6dbf0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
6dc00 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
6dc10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
6dc20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
6dc30 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d  ype (argv[1]) !=
6dc40 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
6dc50 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
6dc60 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
6dc70 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
6dc80 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
6dc90 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
6dca0 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
6dcb0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
6dcc0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
6dcd0 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
6dce0 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
6dcf0 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20  0]);.    geo1 = 
6dd00 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
6dd10 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
6dd20 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
6dd30 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
6dd40 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
6dd50 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
6dd60 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f  argv[1]);.    n_
6dd70 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
6dd80 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
6dd90 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f 32 20  v[1]);.    geo2 
6dda0 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
6ddb0 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
6ddc0 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
6ddd0 20 20 20 69 66 20 28 21 67 65 6f 31 20 7c 7c 20     if (!geo1 || 
6dde0 21 67 65 6f 32 29 0a 09 73 71 6c 69 74 65 33 5f  !geo2)..sqlite3_
6ddf0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
6de00 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a  text);.    else.
6de10 20 20 20 20 20 20 7b 0a 09 20 20 72 65 74 20 3d        {..  ret =
6de20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 44 69 73   gaiaGeomCollDis
6de30 74 61 6e 63 65 20 28 67 65 6f 31 2c 20 67 65 6f  tance (geo1, geo
6de40 32 2c 20 26 64 69 73 74 29 3b 0a 09 20 20 69 66  2, &dist);..  if
6de50 20 28 21 72 65 74 29 0a 09 20 20 20 20 20 20 73   (!ret)..      s
6de60 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
6de70 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
6de80 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
6de90 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
6dea0 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 64 69 73  le (context, dis
6deb0 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
6dec0 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
6ded0 20 28 67 65 6f 31 29 3b 0a 20 20 20 20 67 61 69   (geo1);.    gai
6dee0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
6def0 65 6f 32 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  eo2);.}..static 
6df00 76 6f 69 64 0a 66 6e 63 74 5f 50 74 44 69 73 74  void.fnct_PtDist
6df10 57 69 74 68 69 6e 20 28 73 71 6c 69 74 65 33 5f  Within (sqlite3_
6df20 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
6df30 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
6df40 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
6df50 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
6df60 63 74 69 6f 6e 3a 0a 2f 20 50 74 44 69 73 74 57  ction:./ PtDistW
6df70 69 74 68 69 6e 28 42 4c 4f 42 65 6e 63 6f 64 65  ithin(BLOBencode
6df80 64 20 67 65 6f 6d 31 2c 20 42 4c 4f 42 65 6e 63  d geom1, BLOBenc
6df90 6f 64 65 64 20 67 65 6f 6d 32 2c 20 64 6f 75 62  oded geom2, doub
6dfa0 6c 65 20 64 69 73 74 20 0a 2f 20 5b 2c 20 62 6f  le dist ./ [, bo
6dfb0 6f 6c 65 6e 20 75 73 65 5f 73 70 68 65 72 6f 69  olen use_spheroi
6dfc0 64 5d 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20  d])././ returns 
6dfd0 54 52 55 45 20 69 66 20 74 68 65 20 64 69 73 74  TRUE if the dist
6dfe0 61 6e 63 65 20 62 65 74 77 65 65 6e 20 47 45 4f  ance between GEO
6dff0 4d 2d 31 20 61 6e 64 20 47 45 4f 4d 2d 32 0a 2f  M-1 and GEOM-2./
6e000 20 69 73 20 6c 65 73 73 20 6f 72 20 65 71 75 61   is less or equa
6e010 6c 20 74 6f 20 64 69 73 74 0a 2f 0a 2f 20 2d 20  l to dist././ - 
6e020 69 66 20 62 6f 74 68 20 67 65 6f 6d 31 20 61 6e  if both geom1 an
6e030 64 20 67 65 6f 6d 32 20 61 72 65 20 69 6e 20 74  d geom2 are in t
6e040 68 65 20 34 33 32 36 20 28 57 47 53 38 34 29 20  he 4326 (WGS84) 
6e050 53 52 49 44 2c 0a 2f 20 20 20 28 61 6e 64 20 64  SRID,./   (and d
6e060 6f 65 73 20 61 63 74 75 61 6c 6c 79 20 63 6f 6e  oes actually con
6e070 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 50  tains a single P
6e080 4f 49 4e 54 20 65 61 63 68 20 6f 6e 65 29 0a 2f  OINT each one)./
6e090 20 20 20 64 69 73 74 20 69 73 20 61 73 73 75 6d     dist is assum
6e0a0 65 64 20 74 6f 20 62 65 20 6d 65 61 73 75 72 65  ed to be measure
6e0b0 64 20 69 6e 20 4d 65 74 65 72 73 0a 2f 20 2d 20  d in Meters./ - 
6e0c0 69 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  in this case the
6e0d0 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 20 75 73   optional arg us
6e0e0 65 5f 73 70 68 65 72 6f 69 64 20 69 73 0a 2f 20  e_spheroid is./ 
6e0f0 20 20 63 68 65 63 6b 65 64 20 74 6f 20 64 65 74    checked to det
6e100 65 72 6d 69 6e 65 20 69 66 20 67 65 6f 64 65 73  ermine if geodes
6e110 69 63 20 64 69 73 74 61 6e 63 65 20 68 61 73 20  ic distance has 
6e120 74 6f 20 62 65 0a 2f 20 20 20 63 6f 6d 70 75 74  to be./   comput
6e130 65 64 20 6f 6e 20 74 68 65 20 73 70 68 65 72 65  ed on the sphere
6e140 20 28 71 75 69 63 6b 65 73 74 29 20 6f 72 20 6f   (quickest) or o
6e150 6e 20 74 68 65 20 73 70 68 65 72 6f 69 64 20 0a  n the spheroid .
6e160 2f 20 20 20 64 65 66 61 75 6c 74 3a 20 75 73 65  /   default: use
6e170 5f 73 70 68 65 72 6f 69 64 20 3d 20 46 41 4c 53  _spheroid = FALS
6e180 45 0a 2f 20 0a 2f 20 69 6e 20 61 6e 79 20 6f 74  E./ ./ in any ot
6e190 68 65 72 20 63 61 73 65 20 74 68 65 20 22 70 6c  her case the "pl
6e1a0 61 69 6e 22 20 64 69 73 74 61 6e 63 65 20 69 73  ain" distance is
6e1b0 20 65 76 61 6c 75 61 74 65 64 0a 2a 2f 0a 20 20   evaluated.*/.  
6e1c0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
6e1d0 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
6e1e0 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61   n_bytes;.    ga
6e1f0 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
6e200 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  o1 = NULL;.    g
6e210 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
6e220 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  eo2 = NULL;.    
6e230 67 61 69 61 50 6f 69 6e 74 50 74 72 20 70 74 3b  gaiaPointPtr pt;
6e240 0a 20 20 20 20 67 61 69 61 4c 69 6e 65 73 74 72  .    gaiaLinestr
6e250 69 6e 67 50 74 72 20 6c 6e 3b 0a 20 20 20 20 67  ingPtr ln;.    g
6e260 61 69 61 50 6f 6c 79 67 6f 6e 50 74 72 20 70 67  aiaPolygonPtr pg
6e270 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 65 66  ;.    double ref
6e280 5f 64 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 75  _dist;.    int u
6e290 73 65 5f 73 70 68 65 72 6f 69 64 20 3d 20 30 3b  se_spheroid = 0;
6e2a0 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 30 3b 0a  .    double x0;.
6e2b0 20 20 20 20 64 6f 75 62 6c 65 20 79 30 3b 0a 20      double y0;. 
6e2c0 20 20 20 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20     double x1;.  
6e2d0 20 20 64 6f 75 62 6c 65 20 79 31 3b 0a 20 20 20    double y1;.   
6e2e0 20 69 6e 74 20 70 74 30 20 3d 20 30 3b 0a 20 20   int pt0 = 0;.  
6e2f0 20 20 69 6e 74 20 6c 6e 30 20 3d 20 30 3b 0a 20    int ln0 = 0;. 
6e300 20 20 20 69 6e 74 20 70 67 30 20 3d 20 30 3b 0a     int pg0 = 0;.
6e310 20 20 20 20 69 6e 74 20 70 74 31 20 3d 20 30 3b      int pt1 = 0;
6e320 0a 20 20 20 20 69 6e 74 20 6c 6e 31 20 3d 20 30  .    int ln1 = 0
6e330 3b 0a 20 20 20 20 69 6e 74 20 70 67 31 20 3d 20  ;.    int pg1 = 
6e340 30 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 64 69  0;.    double di
6e350 73 74 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 61  st;.    double a
6e360 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 62 3b 0a  ;.    double b;.
6e370 20 20 20 20 64 6f 75 62 6c 65 20 72 66 3b 0a 20      double rf;. 
6e380 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20     int ret;.    
6e390 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
6e3a0 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
6e3b0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
6e3c0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
6e3d0 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
6e3e0 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
6e3f0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
6e400 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
6e410 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
6e420 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
6e430 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
6e440 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20  pe (argv[1]) != 
6e450 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
6e460 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
6e470 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
6e480 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
6e490 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
6e4a0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
6e4b0 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d  type (argv[2]) =
6e4c0 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
6e4d0 0a 09 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c  ..|| sqlite3_val
6e4e0 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d  ue_type (argv[2]
6e4f0 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41  ) == SQLITE_FLOA
6e500 54 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a 20  T)..;.    else. 
6e510 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
6e520 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
6e530 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
6e540 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
6e550 69 66 20 28 61 72 67 63 20 3d 3d 20 34 29 0a 20  if (argc == 4). 
6e560 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 6f 70 74       {..  /* opt
6e570 69 6f 6e 61 6c 20 75 73 65 5f 73 70 68 65 72 6f  ional use_sphero
6e580 69 64 20 61 72 67 20 2a 2f 0a 09 20 20 69 66 20  id arg */..  if 
6e590 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
6e5a0 79 70 65 20 28 61 72 67 76 5b 33 5d 29 20 21 3d  ype (argv[3]) !=
6e5b0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
6e5c0 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c 69 74 65  ..    {...sqlite
6e5d0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
6e5e0 6f 6e 74 65 78 74 29 3b 0a 09 09 72 65 74 75 72  ontext);...retur
6e5f0 6e 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20  n;..    }.      
6e600 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
6e610 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
6e620 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
6e630 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
6e640 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
6e650 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
6e660 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
6e670 67 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53  geo1 = gaiaFromS
6e680 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
6e690 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
6e6a0 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d  s);.    p_blob =
6e6b0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
6e6c0 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
6e6d0 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b  _blob (argv[1]);
6e6e0 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
6e6f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
6e700 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  es (argv[1]);.  
6e710 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f    geo2 = gaiaFro
6e720 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
6e730 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
6e740 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 73 71  tes);.    if (sq
6e750 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
6e760 20 28 61 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51   (argv[2]) == SQ
6e770 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
6e780 20 20 20 20 7b 0a 09 20 20 69 6e 74 20 64 73 74      {..  int dst
6e790 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
6e7a0 5f 69 6e 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a  _int (argv[2]);.
6e7b0 09 20 20 72 65 66 5f 64 69 73 74 20 3d 20 64 73  .  ref_dist = ds
6e7c0 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65  t;.      }.    e
6e7d0 6c 73 65 0a 09 72 65 66 5f 64 69 73 74 20 3d 20  lse..ref_dist = 
6e7e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
6e7f0 75 62 6c 65 20 28 61 72 67 76 5b 32 5d 29 3b 0a  uble (argv[2]);.
6e800 20 20 20 20 69 66 20 28 61 72 67 63 20 3d 3d 20      if (argc == 
6e810 34 29 0a 09 75 73 65 5f 73 70 68 65 72 6f 69 64  4)..use_spheroid
6e820 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
6e830 5f 69 6e 74 20 28 61 72 67 76 5b 33 5d 29 3b 0a  _int (argv[3]);.
6e840 20 20 20 20 69 66 20 28 21 67 65 6f 31 20 7c 7c      if (!geo1 ||
6e850 20 21 67 65 6f 32 29 0a 09 73 71 6c 69 74 65 33   !geo2)..sqlite3
6e860 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
6e870 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
6e880 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20 28  .      {..  if (
6e890 67 65 6f 31 2d 3e 53 72 69 64 20 3d 3d 20 34 33  geo1->Srid == 43
6e8a0 32 36 20 26 26 20 67 65 6f 32 2d 3e 53 72 69 64  26 && geo2->Srid
6e8b0 20 3d 3d 20 34 33 32 36 29 0a 09 20 20 20 20 7b   == 4326)..    {
6e8c0 0a 09 09 2f 2a 20 63 68 65 63 6b 69 6e 67 20 66  .../* checking f
6e8d0 6f 72 20 73 69 6e 67 6c 65 20 70 6f 69 6e 74 73  or single points
6e8e0 20 2a 2f 0a 09 09 70 74 20 3d 20 67 65 6f 31 2d   */...pt = geo1-
6e8f0 3e 46 69 72 73 74 50 6f 69 6e 74 3b 0a 09 09 77  >FirstPoint;...w
6e900 68 69 6c 65 20 28 70 74 29 0a 09 09 20 20 7b 0a  hile (pt)...  {.
6e910 09 09 20 20 20 20 20 20 78 30 20 3d 20 70 74 2d  ..      x0 = pt-
6e920 3e 58 3b 0a 09 09 20 20 20 20 20 20 79 30 20 3d  >X;...      y0 =
6e930 20 70 74 2d 3e 59 3b 0a 09 09 20 20 20 20 20 20   pt->Y;...      
6e940 70 74 30 2b 2b 3b 0a 09 09 20 20 20 20 20 20 70  pt0++;...      p
6e950 74 20 3d 20 70 74 2d 3e 4e 65 78 74 3b 0a 09 09  t = pt->Next;...
6e960 20 20 7d 0a 09 09 6c 6e 20 3d 20 67 65 6f 31 2d    }...ln = geo1-
6e970 3e 46 69 72 73 74 4c 69 6e 65 73 74 72 69 6e 67  >FirstLinestring
6e980 3b 0a 09 09 77 68 69 6c 65 20 28 6c 6e 29 0a 09  ;...while (ln)..
6e990 09 20 20 7b 0a 09 09 20 20 20 20 20 20 6c 6e 30  .  {...      ln0
6e9a0 2b 2b 3b 0a 09 09 20 20 20 20 20 20 6c 6e 20 3d  ++;...      ln =
6e9b0 20 6c 6e 2d 3e 4e 65 78 74 3b 0a 09 09 20 20 7d   ln->Next;...  }
6e9c0 0a 09 09 70 67 20 3d 20 67 65 6f 31 2d 3e 46 69  ...pg = geo1->Fi
6e9d0 72 73 74 50 6f 6c 79 67 6f 6e 3b 0a 09 09 77 68  rstPolygon;...wh
6e9e0 69 6c 65 20 28 70 67 29 0a 09 09 20 20 7b 0a 09  ile (pg)...  {..
6e9f0 09 20 20 20 20 20 20 70 67 30 2b 2b 3b 0a 09 09  .      pg0++;...
6ea00 20 20 20 20 20 20 70 67 20 3d 20 70 67 2d 3e 4e        pg = pg->N
6ea10 65 78 74 3b 0a 09 09 20 20 7d 0a 09 09 70 74 20  ext;...  }...pt 
6ea20 3d 20 67 65 6f 32 2d 3e 46 69 72 73 74 50 6f 69  = geo2->FirstPoi
6ea30 6e 74 3b 0a 09 09 77 68 69 6c 65 20 28 70 74 29  nt;...while (pt)
6ea40 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 78  ...  {...      x
6ea50 31 20 3d 20 70 74 2d 3e 58 3b 0a 09 09 20 20 20  1 = pt->X;...   
6ea60 20 20 20 79 31 20 3d 20 70 74 2d 3e 59 3b 0a 09     y1 = pt->Y;..
6ea70 09 20 20 20 20 20 20 70 74 31 2b 2b 3b 0a 09 09  .      pt1++;...
6ea80 20 20 20 20 20 20 70 74 20 3d 20 70 74 2d 3e 4e        pt = pt->N
6ea90 65 78 74 3b 0a 09 09 20 20 7d 0a 09 09 6c 6e 20  ext;...  }...ln 
6eaa0 3d 20 67 65 6f 32 2d 3e 46 69 72 73 74 4c 69 6e  = geo2->FirstLin
6eab0 65 73 74 72 69 6e 67 3b 0a 09 09 77 68 69 6c 65  estring;...while
6eac0 20 28 6c 6e 29 0a 09 09 20 20 7b 0a 09 09 20 20   (ln)...  {...  
6ead0 20 20 20 20 6c 6e 31 2b 2b 3b 0a 09 09 20 20 20      ln1++;...   
6eae0 20 20 20 6c 6e 20 3d 20 6c 6e 2d 3e 4e 65 78 74     ln = ln->Next
6eaf0 3b 0a 09 09 20 20 7d 0a 09 09 70 67 20 3d 20 67  ;...  }...pg = g
6eb00 65 6f 32 2d 3e 46 69 72 73 74 50 6f 6c 79 67 6f  eo2->FirstPolygo
6eb10 6e 3b 0a 09 09 77 68 69 6c 65 20 28 70 67 29 0a  n;...while (pg).
6eb20 09 09 20 20 7b 0a 09 09 20 20 20 20 20 20 70 67  ..  {...      pg
6eb30 31 2b 2b 3b 0a 09 09 20 20 20 20 20 20 70 67 20  1++;...      pg 
6eb40 3d 20 70 67 2d 3e 4e 65 78 74 3b 0a 09 09 20 20  = pg->Next;...  
6eb50 7d 0a 09 09 69 66 20 28 70 74 30 20 3d 3d 20 31  }...if (pt0 == 1
6eb60 20 26 26 20 70 74 31 20 3d 3d 20 31 20 26 26 20   && pt1 == 1 && 
6eb70 6c 6e 30 20 3d 3d 20 30 20 26 26 20 6c 6e 31 20  ln0 == 0 && ln1 
6eb80 3d 3d 20 30 20 26 26 20 70 67 30 20 3d 3d 20 30  == 0 && pg0 == 0
6eb90 0a 09 09 20 20 20 20 26 26 20 70 67 31 20 3d 3d  ...    && pg1 ==
6eba0 20 30 29 0a 09 09 20 20 7b 0a 09 09 20 20 20 20   0)...  {...    
6ebb0 20 20 2f 2a 20 75 73 69 6e 67 20 67 65 6f 64 65    /* using geode
6ebc0 73 69 63 20 64 69 73 74 61 6e 63 65 20 2a 2f 0a  sic distance */.
6ebd0 09 09 20 20 20 20 20 20 61 20 3d 20 36 33 37 38  ..      a = 6378
6ebe0 31 33 37 2e 30 3b 0a 09 09 20 20 20 20 20 20 72  137.0;...      r
6ebf0 66 20 3d 20 32 39 38 2e 32 35 37 32 32 33 35 36  f = 298.25722356
6ec00 33 3b 0a 09 09 20 20 20 20 20 20 62 20 3d 20 28  3;...      b = (
6ec10 61 20 2a 20 28 31 2e 30 20 2d 20 28 31 2e 30 20  a * (1.0 - (1.0 
6ec20 2f 20 72 66 29 29 29 3b 0a 09 09 20 20 20 20 20  / rf)));...     
6ec30 20 69 66 20 28 75 73 65 5f 73 70 68 65 72 6f 69   if (use_spheroi
6ec40 64 29 0a 09 09 09 7b 0a 09 09 09 20 20 20 20 64  d)....{....    d
6ec50 69 73 74 20 3d 0a 09 09 09 09 67 61 69 61 47 65  ist =.....gaiaGe
6ec60 6f 64 65 73 69 63 44 69 73 74 61 6e 63 65 20 28  odesicDistance (
6ec70 61 2c 20 62 2c 20 72 66 2c 20 79 30 2c 20 78 30  a, b, rf, y0, x0
6ec80 2c 20 79 31 2c 20 78 31 29 3b 0a 09 09 09 20 20  , y1, x1);....  
6ec90 20 20 69 66 20 28 64 69 73 74 20 3c 3d 20 72 65    if (dist <= re
6eca0 66 5f 64 69 73 74 29 0a 09 09 09 09 73 71 6c 69  f_dist).....sqli
6ecb0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
6ecc0 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 09 09 09  context, 1);....
6ecd0 20 20 20 20 65 6c 73 65 0a 09 09 09 09 73 71 6c      else.....sql
6ece0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
6ecf0 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 09  (context, 0);...
6ed00 09 7d 0a 09 09 20 20 20 20 20 20 65 6c 73 65 0a  .}...      else.
6ed10 09 09 09 7b 0a 09 09 09 20 20 20 20 64 69 73 74  ...{....    dist
6ed20 20 3d 0a 09 09 09 09 67 61 69 61 47 72 65 61 74   =.....gaiaGreat
6ed30 43 69 72 63 6c 65 44 69 73 74 61 6e 63 65 20 28  CircleDistance (
6ed40 61 2c 20 62 2c 20 79 30 2c 20 78 30 2c 20 79 31  a, b, y0, x0, y1
6ed50 2c 20 78 31 29 3b 0a 09 09 09 20 20 20 20 69 66  , x1);....    if
6ed60 20 28 64 69 73 74 20 3c 3d 20 72 65 66 5f 64 69   (dist <= ref_di
6ed70 73 74 29 0a 09 09 09 09 73 71 6c 69 74 65 33 5f  st).....sqlite3_
6ed80 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
6ed90 65 78 74 2c 20 31 29 3b 0a 09 09 09 20 20 20 20  ext, 1);....    
6eda0 65 6c 73 65 0a 09 09 09 09 73 71 6c 69 74 65 33  else.....sqlite3
6edb0 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e  _result_int (con
6edc0 74 65 78 74 2c 20 30 29 3b 0a 09 09 09 7d 0a 09  text, 0);....}..
6edd0 09 20 20 20 20 20 20 67 6f 74 6f 20 73 74 6f 70  .      goto stop
6ede0 3b 0a 09 09 20 20 7d 0a 09 20 20 20 20 7d 0a 2f  ;...  }..    }./
6edf0 2a 20 64 65 66 61 75 6c 74 69 6e 67 20 74 6f 20  * defaulting to 
6ee00 66 6c 61 74 20 64 69 73 74 61 6e 63 65 20 2a 2f  flat distance */
6ee10 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61 47 65  ..  ret = gaiaGe
6ee20 6f 6d 43 6f 6c 6c 44 69 73 74 61 6e 63 65 20 28  omCollDistance (
6ee30 67 65 6f 31 2c 20 67 65 6f 32 2c 20 26 64 69 73  geo1, geo2, &dis
6ee40 74 29 3b 0a 09 20 20 69 66 20 28 21 72 65 74 29  t);..  if (!ret)
6ee50 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
6ee60 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
6ee70 74 65 78 74 29 3b 0a 09 20 20 69 66 20 28 64 69  text);..  if (di
6ee80 73 74 20 3c 3d 20 72 65 66 5f 64 69 73 74 29 0a  st <= ref_dist).
6ee90 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
6eea0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
6eeb0 78 74 2c 20 31 29 3b 0a 09 20 20 65 6c 73 65 0a  xt, 1);..  else.
6eec0 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
6eed0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
6eee0 78 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  xt, 0);.      }.
6eef0 20 20 73 74 6f 70 3a 0a 20 20 20 20 67 61 69 61    stop:.    gaia
6ef00 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
6ef10 6f 31 29 3b 0a 20 20 20 20 67 61 69 61 46 72 65  o1);.    gaiaFre
6ef20 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 29  eGeomColl (geo2)
6ef30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
6ef40 0a 67 65 6f 73 5f 65 72 72 6f 72 20 28 63 6f 6e  .geos_error (con
6ef50 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 2e 2e  st char *fmt, ..
6ef60 2e 29 0a 7b 0a 2f 2a 20 72 65 70 6f 72 74 69 6e  .).{./* reportin
6ef70 67 20 73 6f 6d 65 20 47 45 4f 53 20 65 72 72 6f  g some GEOS erro
6ef80 72 20 2a 2f 0a 20 20 20 20 76 61 5f 6c 69 73 74  r */.    va_list
6ef90 20 61 70 3b 0a 20 20 20 20 63 68 61 72 20 6d 73   ap;.    char ms
6efa0 67 5b 32 30 34 38 5d 3b 0a 20 20 20 20 76 61 5f  g[2048];.    va_
6efb0 73 74 61 72 74 20 28 61 70 2c 20 66 6d 74 29 3b  start (ap, fmt);
6efc0 0a 20 20 20 20 76 73 70 72 69 6e 74 66 20 28 6d  .    vsprintf (m
6efd0 73 67 2c 20 66 6d 74 2c 20 61 70 29 3b 0a 20 20  sg, fmt, ap);.  
6efe0 20 20 76 61 5f 65 6e 64 20 28 61 70 29 3b 0a 20    va_end (ap);. 
6eff0 20 20 20 73 70 61 74 69 61 6c 69 74 65 5f 65 20     spatialite_e 
6f000 28 22 47 45 4f 53 20 65 72 72 6f 72 3a 20 25 73  ("GEOS error: %s
6f010 5c 6e 22 2c 20 6d 73 67 29 3b 0a 20 20 20 20 67  \n", msg);.    g
6f020 61 69 61 53 65 74 47 65 6f 73 45 72 72 6f 72 4d  aiaSetGeosErrorM
6f030 73 67 20 28 6d 73 67 29 3b 0a 7d 0a 0a 0a 73 74  sg (msg);.}...st
6f040 61 74 69 63 20 76 6f 69 64 0a 67 65 6f 73 5f 77  atic void.geos_w
6f050 61 72 6e 69 6e 67 20 28 63 6f 6e 73 74 20 63 68  arning (const ch
6f060 61 72 20 2a 66 6d 74 2c 20 2e 2e 2e 29 0a 7b 0a  ar *fmt, ...).{.
6f070 2f 2a 20 72 65 70 6f 72 74 69 6e 67 20 73 6f 6d  /* reporting som
6f080 65 20 47 45 4f 53 20 77 61 72 6e 69 6e 67 20 2a  e GEOS warning *
6f090 2f 0a 20 20 20 20 76 61 5f 6c 69 73 74 20 61 70  /.    va_list ap
6f0a0 3b 0a 20 20 20 20 63 68 61 72 20 6d 73 67 5b 32  ;.    char msg[2
6f0b0 30 34 38 5d 3b 0a 20 20 20 20 76 61 5f 73 74 61  048];.    va_sta
6f0c0 72 74 20 28 61 70 2c 20 66 6d 74 29 3b 0a 20 20  rt (ap, fmt);.  
6f0d0 20 20 76 73 70 72 69 6e 74 66 20 28 6d 73 67 2c    vsprintf (msg,
6f0e0 20 66 6d 74 2c 20 61 70 29 3b 0a 20 20 20 20 76   fmt, ap);.    v
6f0f0 61 5f 65 6e 64 20 28 61 70 29 3b 0a 20 20 20 20  a_end (ap);.    
6f100 73 70 61 74 69 61 6c 69 74 65 5f 65 20 28 22 47  spatialite_e ("G
6f110 45 4f 53 20 77 61 72 6e 69 6e 67 3a 20 25 73 5c  EOS warning: %s\
6f120 6e 22 2c 20 6d 73 67 29 3b 0a 20 20 20 20 67 61  n", msg);.    ga
6f130 69 61 53 65 74 47 65 6f 73 57 61 72 6e 69 6e 67  iaSetGeosWarning
6f140 4d 73 67 20 28 6d 73 67 29 3b 0a 7d 0a 0a 73 74  Msg (msg);.}..st
6f150 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 61  atic void.fnct_a
6f160 75 78 5f 70 6f 6c 79 67 6f 6e 69 7a 65 20 28 73  ux_polygonize (s
6f170 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
6f180 20 63 6f 6e 74 65 78 74 2c 20 67 61 69 61 47 65   context, gaiaGe
6f190 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d 5f 6f  omCollPtr geom_o
6f1a0 72 67 2c 0a 09 09 20 20 20 20 20 69 6e 74 20 66  rg,...     int f
6f1b0 6f 72 63 65 5f 6d 75 6c 74 69 70 6f 6c 79 67 6f  orce_multipolygo
6f1c0 6e 2c 20 69 6e 74 20 61 6c 6c 6f 77 5f 6d 75 6c  n, int allow_mul
6f1d0 74 69 70 6f 6c 79 67 6f 6e 29 0a 7b 0a 2f 2a 20  tipolygon).{./* 
6f1e0 61 20 20 63 6f 6d 6d 6f 6e 20 66 75 6e 63 74 69  a  common functi
6f1f0 6f 6e 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 6e  on performing an
6f200 79 20 6b 69 6e 64 20 6f 66 20 70 6f 6c 79 67 6f  y kind of polygo
6f210 6e 69 7a 61 74 69 6f 6e 20 6f 70 20 2a 2f 0a 20  nization op */. 
6f220 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
6f230 74 72 20 67 65 6f 6d 5f 6e 65 77 20 3d 20 4e 55  tr geom_new = NU
6f240 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  LL;.    int len;
6f250 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
6f260 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e  ar *p_result = N
6f270 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c  ULL;.    gaiaPol
6f280 79 67 6f 6e 50 74 72 20 70 67 3b 0a 20 20 20 20  ygonPtr pg;.    
6f290 69 6e 74 20 70 67 73 20 3d 20 30 3b 0a 20 20 20  int pgs = 0;.   
6f2a0 20 69 66 20 28 21 67 65 6f 6d 5f 6f 72 67 29 0a   if (!geom_org).
6f2b0 09 67 6f 74 6f 20 69 6e 76 61 6c 69 64 3b 0a 20  .goto invalid;. 
6f2c0 20 20 20 67 65 6f 6d 5f 6e 65 77 20 3d 20 67 61     geom_new = ga
6f2d0 69 61 50 6f 6c 79 67 6f 6e 69 7a 65 20 28 67 65  iaPolygonize (ge
6f2e0 6f 6d 5f 6f 72 67 2c 20 66 6f 72 63 65 5f 6d 75  om_org, force_mu
6f2f0 6c 74 69 70 6f 6c 79 67 6f 6e 29 3b 0a 20 20 20  ltipolygon);.   
6f300 20 69 66 20 28 21 67 65 6f 6d 5f 6e 65 77 29 0a   if (!geom_new).
6f310 09 67 6f 74 6f 20 69 6e 76 61 6c 69 64 3b 0a 20  .goto invalid;. 
6f320 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
6f330 6f 6c 6c 20 28 67 65 6f 6d 5f 6f 72 67 29 3b 0a  oll (geom_org);.
6f340 20 20 20 20 70 67 20 3d 20 67 65 6f 6d 5f 6e 65      pg = geom_ne
6f350 77 2d 3e 46 69 72 73 74 50 6f 6c 79 67 6f 6e 3b  w->FirstPolygon;
6f360 0a 20 20 20 20 77 68 69 6c 65 20 28 70 67 29 0a  .    while (pg).
6f370 20 20 20 20 20 20 7b 0a 09 20 20 70 67 73 2b 2b        {..  pgs++
6f380 3b 0a 09 20 20 70 67 20 3d 20 70 67 2d 3e 4e 65  ;..  pg = pg->Ne
6f390 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
6f3a0 69 66 20 28 70 67 73 20 3e 20 31 20 26 26 20 61  if (pgs > 1 && a
6f3b0 6c 6c 6f 77 5f 6d 75 6c 74 69 70 6f 6c 79 67 6f  llow_multipolygo
6f3c0 6e 20 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a  n == 0).      {.
6f3d0 09 20 20 2f 2a 20 69 6e 76 61 6c 69 64 3a 20 61  .  /* invalid: a
6f3e0 20 50 4f 4c 59 47 4f 4e 20 69 73 20 65 78 70 65   POLYGON is expe
6f3f0 63 74 65 64 20 21 21 21 20 2a 2f 0a 09 20 20 67  cted !!! */..  g
6f400 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
6f410 28 67 65 6f 6d 5f 6e 65 77 29 3b 0a 09 20 20 73  (geom_new);..  s
6f420 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
6f430 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
6f440 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
6f450 0a 20 20 20 20 67 61 69 61 54 6f 53 70 61 74 69  .    gaiaToSpati
6f460 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65  aLiteBlobWkb (ge
6f470 6f 6d 5f 6e 65 77 2c 20 26 70 5f 72 65 73 75 6c  om_new, &p_resul
6f480 74 2c 20 26 6c 65 6e 29 3b 0a 20 20 20 20 67 61  t, &len);.    ga
6f490 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
6f4a0 67 65 6f 6d 5f 6e 65 77 29 3b 0a 20 20 20 20 73  geom_new);.    s
6f4b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
6f4c0 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72  ob (context, p_r
6f4d0 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65  esult, len, free
6f4e0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
6f4f0 20 69 6e 76 61 6c 69 64 3a 0a 20 20 20 20 69 66   invalid:.    if
6f500 20 28 67 65 6f 6d 5f 6f 72 67 29 0a 09 67 61 69   (geom_org)..gai
6f510 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
6f520 65 6f 6d 5f 6f 72 67 29 3b 0a 20 20 20 20 73 71  eom_org);.    sq
6f530 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
6f540 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 7d 0a 0a  l (context);.}..
6f550 2f 2a 0a 2f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  /*./ the followi
6f560 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 70 65 72  ng functions per
6f570 66 6f 72 6d 73 20 69 6e 69 74 69 61 6c 20 61 72  forms initial ar
6f580 67 75 6d 65 6e 74 20 63 68 65 63 6b 69 6e 67 2c  gument checking,
6f590 20 0a 2f 20 61 6e 64 20 74 68 65 6e 20 72 65 61   ./ and then rea
6f5a0 64 64 72 65 73 73 69 6e 67 20 74 68 65 20 72 65  ddressing the re
6f5b0 71 75 65 73 74 20 74 6f 20 66 6e 63 74 5f 61 75  quest to fnct_au
6f5c0 78 5f 70 6f 6c 79 67 6f 6e 69 7a 65 28 29 0a 2f  x_polygonize()./
6f5d0 20 66 6f 72 20 61 63 74 75 61 6c 20 70 72 6f 63   for actual proc
6f5e0 65 73 73 69 6e 67 0a 2a 2f 0a 0a 73 74 61 74 69  essing.*/..stati
6f5f0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 42 64 50 6f  c void.fnct_BdPo
6f600 6c 79 46 72 6f 6d 54 65 78 74 31 20 28 73 71 6c  lyFromText1 (sql
6f610 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
6f620 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
6f630 2c 0a 09 09 20 20 20 20 20 20 73 71 6c 69 74 65  ,...      sqlite
6f640 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
6f650 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
6f660 6f 6e 3a 0a 2f 20 42 64 50 6f 6c 79 46 72 6f 6d  on:./ BdPolyFrom
6f670 54 65 78 74 28 57 4b 54 20 65 6e 63 6f 64 65 64  Text(WKT encoded
6f680 20 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47   MULTILINESTRING
6f690 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68  )././ returns th
6f6a0 65 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74  e current geomet
6f6b0 72 79 20 5b 50 4f 4c 59 47 4f 4e 5d 20 62 79 20  ry [POLYGON] by 
6f6c0 70 61 72 73 69 6e 67 20 61 20 57 4b 54 20 65 6e  parsing a WKT en
6f6d0 63 6f 64 65 64 20 4d 55 4c 54 49 4c 49 4e 45 53  coded MULTILINES
6f6e0 54 52 49 4e 47 20 0a 2f 20 6f 72 20 4e 55 4c 4c  TRING ./ or NULL
6f6f0 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
6f700 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2f 0a 2a   encountered./.*
6f710 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  /.    const unsi
6f720 67 6e 65 64 20 63 68 61 72 20 2a 74 65 78 74 3b  gned char *text;
6f730 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
6f740 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b  lPtr geo = NULL;
6f750 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
6f760 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
6f770 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
6f780 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
6f790 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
6f7a0 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a  != SQLITE_TEXT).
6f7b0 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
6f7c0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
6f7d0 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
6f7e0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
6f7f0 20 74 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f   text = sqlite3_
6f800 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67 76  value_text (argv
6f810 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20  [0]);.    geo = 
6f820 67 61 69 61 50 61 72 73 65 57 6b 74 20 28 74 65  gaiaParseWkt (te
6f830 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 20  xt, -1);.    if 
6f840 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20  (geo == NULL).  
6f850 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
6f860 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
6f870 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
6f880 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
6f890 66 20 28 67 65 6f 2d 3e 44 65 63 6c 61 72 65 64  f (geo->Declared
6f8a0 54 79 70 65 20 21 3d 20 47 41 49 41 5f 4d 55 4c  Type != GAIA_MUL
6f8b0 54 49 4c 49 4e 45 53 54 52 49 4e 47 29 0a 20 20  TILINESTRING).  
6f8c0 20 20 20 20 7b 0a 09 20 20 67 61 69 61 46 72 65      {..  gaiaFre
6f8d0 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b  eGeomColl (geo);
6f8e0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
6f8f0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
6f900 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
6f910 20 20 20 20 7d 0a 20 20 20 20 67 65 6f 2d 3e 53      }.    geo->S
6f920 72 69 64 20 3d 20 30 3b 0a 20 20 20 20 66 6e 63  rid = 0;.    fnc
6f930 74 5f 61 75 78 5f 70 6f 6c 79 67 6f 6e 69 7a 65  t_aux_polygonize
6f940 20 28 63 6f 6e 74 65 78 74 2c 20 67 65 6f 2c 20   (context, geo, 
6f950 30 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72  0, 0);.    retur
6f960 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  n;.}..static voi
6f970 64 0a 66 6e 63 74 5f 42 64 50 6f 6c 79 46 72 6f  d.fnct_BdPolyFro
6f980 6d 54 65 78 74 32 20 28 73 71 6c 69 74 65 33 5f  mText2 (sqlite3_
6f990 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
6f9a0 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09 20  t, int argc,... 
6f9b0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
6f9c0 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
6f9d0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
6f9e0 20 42 64 50 6f 6c 79 46 72 6f 6d 54 65 78 74 28   BdPolyFromText(
6f9f0 57 4b 54 20 65 6e 63 6f 64 65 64 20 4d 55 4c 54  WKT encoded MULT
6fa00 49 4c 49 4e 45 53 54 52 49 4e 47 2c 20 53 52 49  ILINESTRING, SRI
6fa10 44 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74  D)././ returns t
6fa20 68 65 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65  he current geome
6fa30 74 72 79 20 5b 50 4f 4c 59 47 4f 4e 5d 20 62 79  try [POLYGON] by
6fa40 20 70 61 72 73 69 6e 67 20 61 20 57 4b 54 20 65   parsing a WKT e
6fa50 6e 63 6f 64 65 64 20 4d 55 4c 54 49 4c 49 4e 45  ncoded MULTILINE
6fa60 53 54 52 49 4e 47 20 0a 2f 20 6f 72 20 4e 55 4c  STRING ./ or NUL
6fa70 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69  L if any error i
6fa80 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2f 0a  s encountered./.
6fa90 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  */.    const uns
6faa0 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 78 74  igned char *text
6fab0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
6fac0 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
6fad0 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
6fae0 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
6faf0 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
6fb00 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
6fb10 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
6fb20 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 29   != SQLITE_TEXT)
6fb30 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
6fb40 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
6fb50 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
6fb60 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
6fb70 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
6fb80 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31  lue_type (argv[1
6fb90 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) != SQLITE_INT
6fba0 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
6fbb0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
6fbc0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
6fbd0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
6fbe0 20 7d 0a 20 20 20 20 74 65 78 74 20 3d 20 73 71   }.    text = sq
6fbf0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
6fc00 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
6fc10 67 65 6f 20 3d 20 67 61 69 61 50 61 72 73 65 57  geo = gaiaParseW
6fc20 6b 74 20 28 74 65 78 74 2c 20 2d 31 29 3b 0a 20  kt (text, -1);. 
6fc30 20 20 20 69 66 20 28 67 65 6f 20 3d 3d 20 4e 55     if (geo == NU
6fc40 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  LL).      {..  s
6fc50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
6fc60 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
6fc70 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
6fc80 0a 20 20 20 20 69 66 20 28 67 65 6f 2d 3e 44 65  .    if (geo->De
6fc90 63 6c 61 72 65 64 54 79 70 65 20 21 3d 20 47 41  claredType != GA
6fca0 49 41 5f 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49  IA_MULTILINESTRI
6fcb0 4e 47 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 67  NG).      {..  g
6fcc0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
6fcd0 28 67 65 6f 29 3b 0a 09 20 20 73 71 6c 69 74 65  (geo);..  sqlite
6fce0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
6fcf0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
6fd00 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
6fd10 67 65 6f 2d 3e 53 72 69 64 20 3d 20 73 71 6c 69  geo->Srid = sqli
6fd20 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61  te3_value_int (a
6fd30 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 66 6e 63  rgv[1]);.    fnc
6fd40 74 5f 61 75 78 5f 70 6f 6c 79 67 6f 6e 69 7a 65  t_aux_polygonize
6fd50 20 28 63 6f 6e 74 65 78 74 2c 20 67 65 6f 2c 20   (context, geo, 
6fd60 30 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72  0, 0);.    retur
6fd70 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  n;.}..static voi
6fd80 64 0a 66 6e 63 74 5f 42 64 4d 50 6f 6c 79 46 72  d.fnct_BdMPolyFr
6fd90 6f 6d 54 65 78 74 31 20 28 73 71 6c 69 74 65 33  omText1 (sqlite3
6fda0 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
6fdb0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09 09  xt, int argc,...
6fdc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
6fdd0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
6fde0 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
6fdf0 0a 2f 20 42 64 4d 50 6f 6c 79 46 72 6f 6d 54 65  ./ BdMPolyFromTe
6fe00 78 74 28 57 4b 54 20 65 6e 63 6f 64 65 64 20 4d  xt(WKT encoded M
6fe10 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 29 0a  ULTILINESTRING).
6fe20 2f 0a 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20  /./ returns the 
6fe30 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79  current geometry
6fe40 20 5b 4d 55 4c 54 49 50 4f 4c 59 47 4f 4e 5d 20   [MULTIPOLYGON] 
6fe50 62 79 20 70 61 72 73 69 6e 67 20 61 20 57 4b 54  by parsing a WKT
6fe60 20 65 6e 63 6f 64 65 64 20 4d 55 4c 54 49 4c 49   encoded MULTILI
6fe70 4e 45 53 54 52 49 4e 47 20 0a 2f 20 6f 72 20 4e  NESTRING ./ or N
6fe80 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
6fe90 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
6fea0 2f 0a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75  /.*/.    const u
6feb0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65  nsigned char *te
6fec0 78 74 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  xt;.    gaiaGeom
6fed0 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55  CollPtr geo = NU
6fee0 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  LL;.    GAIA_UNU
6fef0 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
6ff00 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
6ff10 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
6ff20 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
6ff30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 54 45 58  ]) != SQLITE_TEX
6ff40 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  T).      {..  sq
6ff50 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
6ff60 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
6ff70 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
6ff80 20 20 20 20 74 65 78 74 20 3d 20 73 71 6c 69 74      text = sqlit
6ff90 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61  e3_value_text (a
6ffa0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f  rgv[0]);.    geo
6ffb0 20 3d 20 67 61 69 61 50 61 72 73 65 57 6b 74 20   = gaiaParseWkt 
6ffc0 28 74 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20  (text, -1);.    
6ffd0 69 66 20 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29  if (geo == NULL)
6ffe0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
6fff0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
70000 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
70010 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
70020 20 20 69 66 20 28 67 65 6f 2d 3e 44 65 63 6c 61    if (geo->Decla
70030 72 65 64 54 79 70 65 20 21 3d 20 47 41 49 41 5f  redType != GAIA_
70040 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 29  MULTILINESTRING)
70050 0a 20 20 20 20 20 20 7b 0a 09 20 20 67 61 69 61  .      {..  gaia
70060 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
70070 6f 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72  o);..  sqlite3_r
70080 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
70090 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
700a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 65 6f  .      }.    geo
700b0 2d 3e 53 72 69 64 20 3d 20 30 3b 0a 20 20 20 20  ->Srid = 0;.    
700c0 66 6e 63 74 5f 61 75 78 5f 70 6f 6c 79 67 6f 6e  fnct_aux_polygon
700d0 69 7a 65 20 28 63 6f 6e 74 65 78 74 2c 20 67 65  ize (context, ge
700e0 6f 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 72 65  o, 1, 1);.    re
700f0 74 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  turn;.}..static 
70100 76 6f 69 64 0a 66 6e 63 74 5f 42 64 4d 50 6f 6c  void.fnct_BdMPol
70110 79 46 72 6f 6d 54 65 78 74 32 20 28 73 71 6c 69  yFromText2 (sqli
70120 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
70130 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
70140 0a 09 09 20 20 20 20 20 20 20 73 71 6c 69 74 65  ...       sqlite
70150 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
70160 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
70170 6f 6e 3a 0a 2f 20 42 64 4d 50 6f 6c 79 46 72 6f  on:./ BdMPolyFro
70180 6d 54 65 78 74 28 57 4b 54 20 65 6e 63 6f 64 65  mText(WKT encode
70190 64 20 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e  d MULTILINESTRIN
701a0 47 2c 20 53 52 49 44 29 0a 2f 0a 2f 20 72 65 74  G, SRID)././ ret
701b0 75 72 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74  urns the current
701c0 20 67 65 6f 6d 65 74 72 79 20 5b 4d 55 4c 54 49   geometry [MULTI
701d0 50 4f 4c 59 47 4f 4e 5d 20 62 79 20 70 61 72 73  POLYGON] by pars
701e0 69 6e 67 20 61 20 57 4b 54 20 65 6e 63 6f 64 65  ing a WKT encode
701f0 64 20 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e  d MULTILINESTRIN
70200 47 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  G ./ or NULL if 
70210 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
70220 6f 75 6e 74 65 72 65 64 0a 2f 0a 2a 2f 0a 20 20  ountered./.*/.  
70230 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
70240 20 63 68 61 72 20 2a 74 65 78 74 3b 0a 20 20 20   char *text;.   
70250 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
70260 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
70270 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
70280 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
70290 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
702a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
702b0 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
702c0 51 4c 49 54 45 5f 54 45 58 54 29 0a 20 20 20 20  QLITE_TEXT).    
702d0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
702e0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
702f0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
70300 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
70310 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
70320 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d  ype (argv[1]) !=
70330 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
70340 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
70350 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
70360 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
70370 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
70380 20 20 74 65 78 74 20 3d 20 73 71 6c 69 74 65 33    text = sqlite3
70390 5f 76 61 6c 75 65 5f 74 65 78 74 20 28 61 72 67  _value_text (arg
703a0 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d  v[0]);.    geo =
703b0 20 67 61 69 61 50 61 72 73 65 57 6b 74 20 28 74   gaiaParseWkt (t
703c0 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66  ext, -1);.    if
703d0 20 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20   (geo == NULL). 
703e0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
703f0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
70400 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
70410 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
70420 69 66 20 28 67 65 6f 2d 3e 44 65 63 6c 61 72 65  if (geo->Declare
70430 64 54 79 70 65 20 21 3d 20 47 41 49 41 5f 4d 55  dType != GAIA_MU
70440 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 29 0a 20  LTILINESTRING). 
70450 20 20 20 20 20 7b 0a 09 20 20 67 61 69 61 46 72       {..  gaiaFr
70460 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
70470 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ;..  sqlite3_res
70480 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
70490 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
704a0 20 20 20 20 20 7d 0a 20 20 20 20 67 65 6f 2d 3e       }.    geo->
704b0 53 72 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76  Srid = sqlite3_v
704c0 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31  alue_int (argv[1
704d0 5d 29 3b 0a 20 20 20 20 66 6e 63 74 5f 61 75 78  ]);.    fnct_aux
704e0 5f 70 6f 6c 79 67 6f 6e 69 7a 65 20 28 63 6f 6e  _polygonize (con
704f0 74 65 78 74 2c 20 67 65 6f 2c 20 31 2c 20 31 29  text, geo, 1, 1)
70500 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  ;.    return;.}.
70510 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
70520 74 5f 42 64 50 6f 6c 79 46 72 6f 6d 57 4b 42 31  t_BdPolyFromWKB1
70530 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
70540 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
70550 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
70560 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
70570 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
70580 0a 2f 20 42 64 50 6f 6c 79 46 72 6f 6d 57 4b 42  ./ BdPolyFromWKB
70590 28 57 4b 42 20 65 6e 63 6f 64 65 64 20 4d 55 4c  (WKB encoded MUL
705a0 54 49 4c 49 4e 45 53 54 52 49 4e 47 29 0a 2f 0a  TILINESTRING)./.
705b0 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 75  / returns the cu
705c0 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 20 5b  rrent geometry [
705d0 50 4f 4c 59 47 4f 4e 5d 20 62 79 20 70 61 72 73  POLYGON] by pars
705e0 69 6e 67 20 61 20 57 4b 42 20 65 6e 63 6f 64 65  ing a WKB encode
705f0 64 20 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e  d MULTILINESTRIN
70600 47 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  G ./ or NULL if 
70610 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
70620 6f 75 6e 74 65 72 65 64 0a 2f 0a 2a 2f 0a 20 20  ountered./.*/.  
70630 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
70640 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
70650 64 20 63 68 61 72 20 2a 77 6b 62 3b 0a 20 20 20  d char *wkb;.   
70660 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
70670 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
70680 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
70690 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
706a0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
706b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
706c0 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
706d0 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20  QLITE_BLOB).    
706e0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
706f0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
70700 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
70710 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 77 6b 62  .      }.    wkb
70720 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
70730 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
70740 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
70750 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
70760 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
70770 20 20 69 66 20 28 21 63 68 65 63 6b 5f 77 6b 62    if (!check_wkb
70780 20 28 77 6b 62 2c 20 6e 5f 62 79 74 65 73 2c 20   (wkb, n_bytes, 
70790 2d 31 29 29 0a 09 72 65 74 75 72 6e 3b 0a 20 20  -1))..return;.  
707a0 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d    geo = gaiaFrom
707b0 57 6b 62 20 28 77 6b 62 2c 20 6e 5f 62 79 74 65  Wkb (wkb, n_byte
707c0 73 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 20  s);.    if (geo 
707d0 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b  == NULL).      {
707e0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
707f0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
70800 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
70810 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 67 65      }.    if (ge
70820 6f 2d 3e 44 65 63 6c 61 72 65 64 54 79 70 65 20  o->DeclaredType 
70830 21 3d 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e  != GAIA_MULTILIN
70840 45 53 54 52 49 4e 47 29 0a 20 20 20 20 20 20 7b  ESTRING).      {
70850 0a 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d  ..  gaiaFreeGeom
70860 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09 20 20 73  Coll (geo);..  s
70870 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
70880 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
70890 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
708a0 0a 20 20 20 20 67 65 6f 2d 3e 53 72 69 64 20 3d  .    geo->Srid =
708b0 20 30 3b 0a 20 20 20 20 66 6e 63 74 5f 61 75 78   0;.    fnct_aux
708c0 5f 70 6f 6c 79 67 6f 6e 69 7a 65 20 28 63 6f 6e  _polygonize (con
708d0 74 65 78 74 2c 20 67 65 6f 2c 20 30 2c 20 30 29  text, geo, 0, 0)
708e0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  ;.    return;.}.
708f0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
70900 74 5f 42 64 50 6f 6c 79 46 72 6f 6d 57 4b 42 32  t_BdPolyFromWKB2
70910 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
70920 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
70930 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
70940 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
70950 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
70960 0a 2f 20 42 64 50 6f 6c 79 46 72 6f 6d 57 4b 42  ./ BdPolyFromWKB
70970 28 57 4b 42 20 65 6e 63 6f 64 65 64 20 4d 55 4c  (WKB encoded MUL
70980 54 49 4c 49 4e 45 53 54 52 49 4e 47 29 0a 2f 0a  TILINESTRING)./.
70990 2f 20 72 65 74 75 72 6e 73 20 74 68 65 20 63 75  / returns the cu
709a0 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 20 5b  rrent geometry [
709b0 50 4f 4c 59 47 4f 4e 5d 20 62 79 20 70 61 72 73  POLYGON] by pars
709c0 69 6e 67 20 61 20 57 4b 42 20 65 6e 63 6f 64 65  ing a WKB encode
709d0 64 20 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e  d MULTILINESTRIN
709e0 47 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  G ./ or NULL if 
709f0 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
70a00 6f 75 6e 74 65 72 65 64 0a 2f 0a 2a 2f 0a 20 20  ountered./.*/.  
70a10 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
70a20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
70a30 64 20 63 68 61 72 20 2a 77 6b 62 3b 0a 20 20 20  d char *wkb;.   
70a40 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
70a50 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   geo = NULL;.   
70a60 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
70a70 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
70a80 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
70a90 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
70aa0 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
70ab0 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20  QLITE_BLOB).    
70ac0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
70ad0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
70ae0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
70af0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
70b00 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
70b10 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d  ype (argv[1]) !=
70b20 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
70b30 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
70b40 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
70b50 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
70b60 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
70b70 20 20 77 6b 62 20 3d 20 73 71 6c 69 74 65 33 5f    wkb = sqlite3_
70b80 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76  value_blob (argv
70b90 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65  [0]);.    n_byte
70ba0 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s = sqlite3_valu
70bb0 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d  e_bytes (argv[0]
70bc0 29 3b 0a 20 20 20 20 69 66 20 28 21 63 68 65 63  );.    if (!chec
70bd0 6b 5f 77 6b 62 20 28 77 6b 62 2c 20 6e 5f 62 79  k_wkb (wkb, n_by
70be0 74 65 73 2c 20 2d 31 29 29 0a 09 72 65 74 75 72  tes, -1))..retur
70bf0 6e 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61 69  n;.    geo = gai
70c00 61 46 72 6f 6d 57 6b 62 20 28 77 6b 62 2c 20 6e  aFromWkb (wkb, n
70c10 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
70c20 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20  (geo == NULL).  
70c30 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
70c40 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
70c50 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
70c60 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
70c70 66 20 28 67 65 6f 2d 3e 44 65 63 6c 61 72 65 64  f (geo->Declared
70c80 54 79 70 65 20 21 3d 20 47 41 49 41 5f 4d 55 4c  Type != GAIA_MUL
70c90 54 49 4c 49 4e 45 53 54 52 49 4e 47 29 0a 20 20  TILINESTRING).  
70ca0 20 20 20 20 7b 0a 09 20 20 67 61 69 61 46 72 65      {..  gaiaFre
70cb0 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b  eGeomColl (geo);
70cc0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
70cd0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
70ce0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
70cf0 20 20 20 20 7d 0a 20 20 20 20 67 65 6f 2d 3e 53      }.    geo->S
70d00 72 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  rid = sqlite3_va
70d10 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d  lue_int (argv[1]
70d20 29 3b 0a 20 20 20 20 66 6e 63 74 5f 61 75 78 5f  );.    fnct_aux_
70d30 70 6f 6c 79 67 6f 6e 69 7a 65 20 28 63 6f 6e 74  polygonize (cont
70d40 65 78 74 2c 20 67 65 6f 2c 20 30 2c 20 30 29 3b  ext, geo, 0, 0);
70d50 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  .    return;.}..
70d60 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
70d70 5f 42 64 4d 50 6f 6c 79 46 72 6f 6d 57 4b 42 31  _BdMPolyFromWKB1
70d80 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
70d90 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
70da0 20 61 72 67 63 2c 0a 09 09 20 20 20 20 20 20 73   argc,...      s
70db0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
70dc0 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
70dd0 75 6e 63 74 69 6f 6e 3a 0a 2f 20 42 64 4d 50 6f  unction:./ BdMPo
70de0 6c 79 46 72 6f 6d 57 4b 42 28 57 4b 42 20 65 6e  lyFromWKB(WKB en
70df0 63 6f 64 65 64 20 4d 55 4c 54 49 4c 49 4e 45 53  coded MULTILINES
70e00 54 52 49 4e 47 29 0a 2f 0a 2f 20 72 65 74 75 72  TRING)././ retur
70e10 6e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 67  ns the current g
70e20 65 6f 6d 65 74 72 79 20 5b 4d 55 4c 54 49 50 4f  eometry [MULTIPO
70e30 4c 59 47 4f 4e 5d 20 62 79 20 70 61 72 73 69 6e  LYGON] by parsin
70e40 67 20 61 20 57 4b 42 20 65 6e 63 6f 64 65 64 20  g a WKB encoded 
70e50 4d 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 20  MULTILINESTRING 
70e60 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
70e70 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
70e80 6e 74 65 72 65 64 0a 2f 0a 2a 2f 0a 20 20 20 20  ntered./.*/.    
70e90 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
70ea0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
70eb0 63 68 61 72 20 2a 77 6b 62 3b 0a 20 20 20 20 67  char *wkb;.    g
70ec0 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
70ed0 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47  eo = NULL;.    G
70ee0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
70ef0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
70f00 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
70f10 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
70f20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
70f30 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
70f40 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
70f50 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
70f60 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
70f70 20 20 20 20 20 7d 0a 20 20 20 20 77 6b 62 20 3d       }.    wkb =
70f80 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
70f90 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
70fa0 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
70fb0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
70fc0 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
70fd0 69 66 20 28 21 63 68 65 63 6b 5f 77 6b 62 20 28  if (!check_wkb (
70fe0 77 6b 62 2c 20 6e 5f 62 79 74 65 73 2c 20 2d 31  wkb, n_bytes, -1
70ff0 29 29 0a 09 72 65 74 75 72 6e 3b 0a 20 20 20 20  ))..return;.    
71000 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 57 6b  geo = gaiaFromWk
71010 62 20 28 77 6b 62 2c 20 6e 5f 62 79 74 65 73 29  b (wkb, n_bytes)
71020 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 20 3d 3d  ;.    if (geo ==
71030 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09   NULL).      {..
71040 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
71050 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
71060 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
71070 20 20 7d 0a 20 20 20 20 69 66 20 28 67 65 6f 2d    }.    if (geo-
71080 3e 44 65 63 6c 61 72 65 64 54 79 70 65 20 21 3d  >DeclaredType !=
71090 20 47 41 49 41 5f 4d 55 4c 54 49 4c 49 4e 45 53   GAIA_MULTILINES
710a0 54 52 49 4e 47 29 0a 20 20 20 20 20 20 7b 0a 09  TRING).      {..
710b0 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
710c0 6c 6c 20 28 67 65 6f 29 3b 0a 09 20 20 73 71 6c  ll (geo);..  sql
710d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
710e0 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
710f0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
71100 20 20 20 67 65 6f 2d 3e 53 72 69 64 20 3d 20 30     geo->Srid = 0
71110 3b 0a 20 20 20 20 66 6e 63 74 5f 61 75 78 5f 70  ;.    fnct_aux_p
71120 6f 6c 79 67 6f 6e 69 7a 65 20 28 63 6f 6e 74 65  olygonize (conte
71130 78 74 2c 20 67 65 6f 2c 20 31 2c 20 31 29 3b 0a  xt, geo, 1, 1);.
71140 20 20 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 73      return;.}..s
71150 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
71160 42 64 4d 50 6f 6c 79 46 72 6f 6d 57 4b 42 32 20  BdMPolyFromWKB2 
71170 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
71180 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
71190 61 72 67 63 2c 0a 09 09 20 20 20 20 20 20 73 71  argc,...      sq
711a0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
711b0 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
711c0 6e 63 74 69 6f 6e 3a 0a 2f 20 42 64 4d 50 6f 6c  nction:./ BdMPol
711d0 79 46 72 6f 6d 57 4b 42 28 57 4b 42 20 65 6e 63  yFromWKB(WKB enc
711e0 6f 64 65 64 20 4d 55 4c 54 49 4c 49 4e 45 53 54  oded MULTILINEST
711f0 52 49 4e 47 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  RING)././ return
71200 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 67 65  s the current ge
71210 6f 6d 65 74 72 79 20 5b 4d 55 4c 54 49 50 4f 4c  ometry [MULTIPOL
71220 59 47 4f 4e 5d 20 62 79 20 70 61 72 73 69 6e 67  YGON] by parsing
71230 20 61 20 57 4b 42 20 65 6e 63 6f 64 65 64 20 4d   a WKB encoded M
71240 55 4c 54 49 4c 49 4e 45 53 54 52 49 4e 47 20 0a  ULTILINESTRING .
71250 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79  / or NULL if any
71260 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
71270 74 65 72 65 64 0a 2f 0a 2a 2f 0a 20 20 20 20 69  tered./.*/.    i
71280 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
71290 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
712a0 68 61 72 20 2a 77 6b 62 3b 0a 20 20 20 20 67 61  har *wkb;.    ga
712b0 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
712c0 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 47 41  o = NULL;.    GA
712d0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
712e0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
712f0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
71300 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
71310 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
71320 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
71330 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
71340 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
71350 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
71360 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71      }.    if (sq
71370 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
71380 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51   (argv[1]) != SQ
71390 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
713a0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
713b0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
713c0 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
713d0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 77  n;.      }.    w
713e0 6b 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  kb = sqlite3_val
713f0 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
71400 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
71410 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
71420 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
71430 20 20 20 20 69 66 20 28 21 63 68 65 63 6b 5f 77      if (!check_w
71440 6b 62 20 28 77 6b 62 2c 20 6e 5f 62 79 74 65 73  kb (wkb, n_bytes
71450 2c 20 2d 31 29 29 0a 09 72 65 74 75 72 6e 3b 0a  , -1))..return;.
71460 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
71470 6f 6d 57 6b 62 20 28 77 6b 62 2c 20 6e 5f 62 79  omWkb (wkb, n_by
71480 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 67 65  tes);.    if (ge
71490 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20  o == NULL).     
714a0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
714b0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
714c0 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
714d0 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28        }.    if (
714e0 67 65 6f 2d 3e 44 65 63 6c 61 72 65 64 54 79 70  geo->DeclaredTyp
714f0 65 20 21 3d 20 47 41 49 41 5f 4d 55 4c 54 49 4c  e != GAIA_MULTIL
71500 49 4e 45 53 54 52 49 4e 47 29 0a 20 20 20 20 20  INESTRING).     
71510 20 7b 0a 09 20 20 67 61 69 61 46 72 65 65 47 65   {..  gaiaFreeGe
71520 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 09 20  omColl (geo);.. 
71530 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
71540 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
71550 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
71560 20 7d 0a 20 20 20 20 67 65 6f 2d 3e 53 72 69 64   }.    geo->Srid
71570 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
71580 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a  _int (argv[1]);.
71590 20 20 20 20 66 6e 63 74 5f 61 75 78 5f 70 6f 6c      fnct_aux_pol
715a0 79 67 6f 6e 69 7a 65 20 28 63 6f 6e 74 65 78 74  ygonize (context
715b0 2c 20 67 65 6f 2c 20 31 2c 20 31 29 3b 0a 20 20  , geo, 1, 1);.  
715c0 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66    return;.}..#if
715d0 64 65 66 20 47 45 4f 53 5f 41 44 56 41 4e 43 45  def GEOS_ADVANCE
715e0 44 09 09 2f 2a 20 47 45 4f 53 20 61 64 76 61 6e  D../* GEOS advan
715f0 63 65 64 20 61 6e 64 20 65 78 70 65 72 69 6d 65  ced and experime
71600 6e 74 61 6c 20 66 65 61 74 75 72 65 73 20 2a 2f  ntal features */
71610 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
71620 63 74 5f 4f 66 66 73 65 74 43 75 72 76 65 20 28  ct_OffsetCurve (
71630 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
71640 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
71650 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
71660 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
71670 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
71680 20 4f 66 66 73 65 74 43 75 72 76 65 28 42 4c 4f   OffsetCurve(BLO
71690 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72  Bencoded geometr
716a0 79 2c 20 72 61 64 69 75 73 2c 20 6c 65 66 74 2d  y, radius, left-
716b0 6f 72 2d 72 69 67 68 74 2d 73 69 64 65 29 0a 2f  or-right-side)./
716c0 0a 2f 20 72 65 74 75 72 6e 73 20 61 20 6e 65 77  ./ returns a new
716d0 20 67 65 6f 6d 65 74 72 79 20 72 65 70 72 65 73   geometry repres
716e0 65 6e 74 69 6e 67 20 74 68 65 20 4f 46 46 53 45  enting the OFFSE
716f0 54 2d 43 55 52 56 45 20 66 6f 72 20 63 75 72 72  T-CURVE for curr
71700 65 6e 74 20 67 65 6f 6d 65 74 72 79 0a 2f 20 5b  ent geometry./ [
71710 61 20 4c 49 4e 45 53 54 52 49 4e 47 20 69 73 20  a LINESTRING is 
71720 65 78 70 65 63 74 65 64 5d 0a 2f 20 6f 72 20 4e  expected]./ or N
71730 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72  ULL if any error
71740 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
71750 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
71760 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
71770 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
71780 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
71790 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20  tr geo = NULL;. 
717a0 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
717b0 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 64  tr result;.    d
717c0 6f 75 62 6c 65 20 72 61 64 69 75 73 3b 0a 20 20  ouble radius;.  
717d0 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b    int int_value;
717e0 0a 20 20 20 20 69 6e 74 20 6c 65 66 74 5f 72 69  .    int left_ri
717f0 67 68 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ght;.    GAIA_UN
71800 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
71810 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
71820 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
71830 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
71840 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
71850 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
71860 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
71870 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
71880 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
71890 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
718a0 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
718b0 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[1]) == SQLITE_
718c0 46 4c 4f 41 54 29 0a 09 72 61 64 69 75 73 20 3d  FLOAT)..radius =
718d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
718e0 6f 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b  ouble (argv[1]);
718f0 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71  .    else if (sq
71900 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
71910 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51   (argv[1]) == SQ
71920 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
71930 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c      {..  int_val
71940 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
71950 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29  ue_int (argv[1])
71960 3b 0a 09 20 20 72 61 64 69 75 73 20 3d 20 69 6e  ;..  radius = in
71970 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d  t_value;.      }
71980 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
71990 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
719a0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
719b0 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
719c0 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73       }.    if (s
719d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
719e0 65 20 28 61 72 67 76 5b 32 5d 29 20 3d 3d 20 53  e (argv[2]) == S
719f0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 09  QLITE_INTEGER)..
71a00 6c 65 66 74 5f 72 69 67 68 74 20 3d 20 73 71 6c  left_right = sql
71a10 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
71a20 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 65 6c  argv[2]);.    el
71a30 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  se.      {..  sq
71a40 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
71a50 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
71a60 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
71a70 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
71a80 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
71a90 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
71aa0 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
71ab0 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
71ac0 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
71ad0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
71ae0 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  o = gaiaFromSpat
71af0 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
71b00 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
71b10 0a 20 20 20 20 69 66 20 28 21 67 65 6f 29 0a 09  .    if (!geo)..
71b20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
71b30 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ull (context);. 
71b40 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
71b50 09 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61  .  result = gaia
71b60 4f 66 66 73 65 74 43 75 72 76 65 20 28 67 65 6f  OffsetCurve (geo
71b70 2c 20 72 61 64 69 75 73 2c 20 31 36 2c 20 6c 65  , radius, 16, le
71b80 66 74 5f 72 69 67 68 74 29 3b 0a 09 20 20 69 66  ft_right);..  if
71b90 20 28 21 72 65 73 75 6c 74 29 0a 09 20 20 20 20   (!result)..    
71ba0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
71bb0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
71bc0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
71bd0 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20  ../* builds the 
71be0 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f  BLOB geometry to
71bf0 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a   be returned */.
71c00 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73  ..int len;...uns
71c10 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
71c20 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72  sult = NULL;...r
71c30 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65  esult->Srid = ge
71c40 6f 2d 3e 53 72 69 64 3b 0a 09 09 67 61 69 61 54  o->Srid;...gaiaT
71c50 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  oSpatiaLiteBlobW
71c60 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72  kb (result, &p_r
71c70 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09  esult, &len);...
71c80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
71c90 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
71ca0 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
71cb0 65 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65  e);...gaiaFreeGe
71cc0 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b  omColl (result);
71cd0 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a  ..    }.      }.
71ce0 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
71cf0 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73  Coll (geo);.}..s
71d00 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
71d10 53 69 6e 67 6c 65 53 69 64 65 64 42 75 66 66 65  SingleSidedBuffe
71d20 72 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  r (sqlite3_conte
71d30 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
71d40 74 20 61 72 67 63 2c 0a 09 09 09 73 71 6c 69 74  t argc,....sqlit
71d50 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
71d60 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
71d70 69 6f 6e 3a 0a 2f 20 53 69 6e 67 6c 65 53 69 64  ion:./ SingleSid
71d80 65 64 42 75 66 66 65 72 28 42 4c 4f 42 65 6e 63  edBuffer(BLOBenc
71d90 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 2c 20 72  oded geometry, r
71da0 61 64 69 75 73 2c 20 6c 65 66 74 2d 6f 72 2d 72  adius, left-or-r
71db0 69 67 68 74 2d 73 69 64 65 29 0a 2f 0a 2f 20 72  ight-side)././ r
71dc0 65 74 75 72 6e 73 20 61 20 6e 65 77 20 67 65 6f  eturns a new geo
71dd0 6d 65 74 72 79 20 72 65 70 72 65 73 65 6e 74 69  metry representi
71de0 6e 67 20 74 68 65 20 53 69 6e 67 6c 65 53 69 64  ng the SingleSid
71df0 65 64 20 42 55 46 46 45 52 20 0a 2f 20 66 6f 72  ed BUFFER ./ for
71e00 20 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72   current geometr
71e10 79 20 5b 61 20 4c 49 4e 45 53 54 52 49 4e 47 20  y [a LINESTRING 
71e20 69 73 20 65 78 70 65 63 74 65 64 5d 0a 2f 20 6f  is expected]./ o
71e30 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72  r NULL if any er
71e40 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
71e50 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ed.*/.    unsign
71e60 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
71e70 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
71e80 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
71e90 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
71ea0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
71eb0 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20  llPtr result;.  
71ec0 20 20 64 6f 75 62 6c 65 20 72 61 64 69 75 73 3b    double radius;
71ed0 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c  .    int int_val
71ee0 75 65 3b 0a 20 20 20 20 69 6e 74 20 6c 65 66 74  ue;.    int left
71ef0 5f 72 69 67 68 74 3b 0a 20 20 20 20 47 41 49 41  _right;.    GAIA
71f00 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
71f10 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
71f20 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
71f30 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
71f40 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
71f50 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
71f60 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
71f70 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
71f80 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
71f90 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69    }.    if (sqli
71fa0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
71fb0 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49  argv[1]) == SQLI
71fc0 54 45 5f 46 4c 4f 41 54 29 0a 09 72 61 64 69 75  TE_FLOAT)..radiu
71fd0 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s = sqlite3_valu
71fe0 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 31  e_double (argv[1
71ff0 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20  ]);.    else if 
72000 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
72010 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d  ype (argv[1]) ==
72020 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
72030 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f  .      {..  int_
72040 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  value = sqlite3_
72050 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
72060 31 5d 29 3b 0a 09 20 20 72 61 64 69 75 73 20 3d  1]);..  radius =
72070 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20   int_value;.    
72080 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20    }.    else.   
72090 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
720a0 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
720b0 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
720c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66  ;.      }.    if
720d0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
720e0 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d  type (argv[2]) =
720f0 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
72100 29 0a 09 6c 65 66 74 5f 72 69 67 68 74 20 3d 20  )..left_right = 
72110 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
72120 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  t (argv[2]);.   
72130 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
72140 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
72150 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
72160 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
72170 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
72180 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
72190 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
721a0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
721b0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
721c0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
721d0 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
721e0 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53   geo = gaiaFromS
721f0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
72200 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
72210 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
72220 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
72230 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
72240 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
72250 20 7b 0a 09 20 20 72 65 73 75 6c 74 20 3d 20 67   {..  result = g
72260 61 69 61 53 69 6e 67 6c 65 53 69 64 65 64 42 75  aiaSingleSidedBu
72270 66 66 65 72 20 28 67 65 6f 2c 20 72 61 64 69 75  ffer (geo, radiu
72280 73 2c 20 31 36 2c 20 6c 65 66 74 5f 72 69 67 68  s, 16, left_righ
72290 74 29 3b 0a 09 20 20 69 66 20 28 21 72 65 73 75  t);..  if (!resu
722a0 6c 74 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lt)..      sqlit
722b0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
722c0 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65 6c 73  context);..  els
722d0 65 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75  e..    {.../* bu
722e0 69 6c 64 73 20 74 68 65 20 42 4c 4f 42 20 67 65  ilds the BLOB ge
722f0 6f 6d 65 74 72 79 20 74 6f 20 62 65 20 72 65 74  ometry to be ret
72300 75 72 6e 65 64 20 2a 2f 0a 09 09 69 6e 74 20 6c  urned */...int l
72310 65 6e 3b 0a 09 09 75 6e 73 69 67 6e 65 64 20 63  en;...unsigned c
72320 68 61 72 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20  har *p_result = 
72330 4e 55 4c 4c 3b 0a 09 09 72 65 73 75 6c 74 2d 3e  NULL;...result->
72340 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64  Srid = geo->Srid
72350 3b 0a 09 09 67 61 69 61 54 6f 53 70 61 74 69 61  ;...gaiaToSpatia
72360 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 72 65 73  LiteBlobWkb (res
72370 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20  ult, &p_result, 
72380 26 6c 65 6e 29 3b 0a 09 09 73 71 6c 69 74 65 33  &len);...sqlite3
72390 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f  _result_blob (co
723a0 6e 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c  ntext, p_result,
723b0 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 09 67   len, free);...g
723c0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
723d0 28 72 65 73 75 6c 74 29 3b 0a 09 20 20 20 20 7d  (result);..    }
723e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
723f0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
72400 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  eo);.}..static v
72410 6f 69 64 0a 66 6e 63 74 5f 48 61 75 73 64 6f 72  oid.fnct_Hausdor
72420 66 66 44 69 73 74 61 6e 63 65 20 28 73 71 6c 69  ffDistance (sqli
72430 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
72440 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
72450 0a 09 09 09 73 71 6c 69 74 65 33 5f 76 61 6c 75  ....sqlite3_valu
72460 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
72470 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
72480 48 61 75 73 64 6f 72 66 66 44 69 73 74 61 6e 63  HausdorffDistanc
72490 65 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65  e(BLOBencoded ge
724a0 6f 6d 31 2c 20 42 4c 4f 42 65 6e 63 6f 64 65 64  om1, BLOBencoded
724b0 20 67 65 6f 6d 32 29 0a 2f 0a 2f 20 72 65 74 75   geom2)././ retu
724c0 72 6e 73 20 74 68 65 20 64 69 73 63 72 65 74 65  rns the discrete
724d0 20 48 61 75 73 64 6f 72 66 66 20 64 69 73 74 61   Hausdorff dista
724e0 6e 63 65 20 62 65 74 77 65 65 6e 20 47 45 4f 4d  nce between GEOM
724f0 2d 31 20 61 6e 64 20 47 45 4f 4d 2d 32 0a 2a 2f  -1 and GEOM-2.*/
72500 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
72510 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20  ar *p_blob;.    
72520 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
72530 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
72540 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20   geo1 = NULL;.  
72550 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
72560 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20  r geo2 = NULL;. 
72570 20 20 20 64 6f 75 62 6c 65 20 64 69 73 74 3b 0a     double dist;.
72580 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20      int ret;.   
72590 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
725a0 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
725b0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
725c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
725d0 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
725e0 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20  QLITE_BLOB).    
725f0 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
72600 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
72610 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
72620 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
72630 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
72640 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d  ype (argv[1]) !=
72650 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
72660 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
72670 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
72680 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
72690 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
726a0 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
726b0 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
726c0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
726d0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
726e0 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
726f0 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
72700 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20  0]);.    geo1 = 
72710 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
72720 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
72730 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
72740 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
72750 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
72760 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
72770 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f  argv[1]);.    n_
72780 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
72790 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
727a0 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f 32 20  v[1]);.    geo2 
727b0 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
727c0 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
727d0 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
727e0 20 20 20 69 66 20 28 21 67 65 6f 31 20 7c 7c 20     if (!geo1 || 
727f0 21 67 65 6f 32 29 0a 09 73 71 6c 69 74 65 33 5f  !geo2)..sqlite3_
72800 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
72810 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a  text);.    else.
72820 20 20 20 20 20 20 7b 0a 09 20 20 72 65 74 20 3d        {..  ret =
72830 20 67 61 69 61 48 61 75 73 64 6f 72 66 66 44 69   gaiaHausdorffDi
72840 73 74 61 6e 63 65 20 28 67 65 6f 31 2c 20 67 65  stance (geo1, ge
72850 6f 32 2c 20 26 64 69 73 74 29 3b 0a 09 20 20 69  o2, &dist);..  i
72860 66 20 28 21 72 65 74 29 0a 09 20 20 20 20 20 20  f (!ret)..      
72870 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
72880 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
72890 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
728a0 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74  _double (context
728b0 2c 20 64 69 73 74 29 3b 0a 20 20 20 20 20 20 7d  , dist);.      }
728c0 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f  .    gaiaFreeGeo
728d0 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a 20 20  mColl (geo1);.  
728e0 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
728f0 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a 73 74  ll (geo2);.}..st
72900 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 53  atic void.fnct_S
72910 68 61 72 65 64 50 61 74 68 73 20 28 73 71 6c 69  haredPaths (sqli
72920 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
72930 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
72940 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
72950 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
72960 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 53 68 61   function:./ Sha
72970 72 65 64 50 61 74 68 73 28 42 4c 4f 42 65 6e 63  redPaths(BLOBenc
72980 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 31 2c 20  oded geometry1, 
72990 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d  BLOBencoded geom
729a0 65 74 72 79 32 29 0a 2f 0a 2f 20 72 65 74 75 72  etry2)././ retur
729b0 6e 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74 72  ns a new geometr
729c0 79 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 63  y representing c
729d0 6f 6d 6d 6f 6e 20 28 73 68 61 72 65 64 29 20 45  ommon (shared) E
729e0 64 67 65 73 0a 2f 20 5b 74 77 6f 20 4c 49 4e 45  dges./ [two LINE
729f0 53 54 52 49 4e 47 73 2f 4d 55 4c 54 49 4c 49 4e  STRINGs/MULTILIN
72a00 45 53 54 52 49 4e 47 73 20 61 72 65 20 65 78 70  ESTRINGs are exp
72a10 65 63 74 65 64 5d 0a 2f 20 6f 72 20 4e 55 4c 4c  ected]./ or NULL
72a20 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
72a30 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
72a40 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
72a50 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69  r *p_blob;.    i
72a60 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20  nt n_bytes;.    
72a70 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
72a80 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  geo1 = NULL;.   
72a90 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
72aa0 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20   geo2 = NULL;.  
72ab0 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
72ac0 72 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 47 41  r result;.    GA
72ad0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
72ae0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
72af0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
72b00 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
72b10 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
72b20 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
72b30 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
72b40 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
72b50 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
72b60 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71      }.    if (sq
72b70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
72b80 20 28 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51   (argv[1]) != SQ
72b90 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
72ba0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
72bb0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
72bc0 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
72bd0 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
72be0 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
72bf0 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
72c00 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
72c10 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
72c20 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
72c30 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
72c40 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20 67 61 69  ;.    geo1 = gai
72c50 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42  aFromSpatiaLiteB
72c60 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20  lobWkb (p_blob, 
72c70 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 70 5f  n_bytes);.    p_
72c80 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
72c90 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
72ca0 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
72cb0 76 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[1]);.    n_byt
72cc0 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
72cd0 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 31  ue_bytes (argv[1
72ce0 5d 29 3b 0a 20 20 20 20 67 65 6f 32 20 3d 20 67  ]);.    geo2 = g
72cf0 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
72d00 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
72d10 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
72d20 69 66 20 28 67 65 6f 31 20 3d 3d 20 4e 55 4c 4c  if (geo1 == NULL
72d30 20 7c 7c 20 67 65 6f 32 20 3d 3d 20 4e 55 4c 4c   || geo2 == NULL
72d40 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  )..sqlite3_resul
72d50 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
72d60 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  ;.    else.     
72d70 20 7b 0a 09 20 20 72 65 73 75 6c 74 20 3d 20 67   {..  result = g
72d80 61 69 61 53 68 61 72 65 64 50 61 74 68 73 20 28  aiaSharedPaths (
72d90 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09 20 20  geo1, geo2);..  
72da0 69 66 20 28 21 72 65 73 75 6c 74 29 0a 09 20 20  if (!result)..  
72db0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
72dc0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
72dd0 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20  );..  else..    
72de0 7b 0a 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68  {.../* builds th
72df0 65 20 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20  e BLOB geometry 
72e00 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
72e10 2f 0a 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75  /...int len;...u
72e20 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
72e30 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09  result = NULL;..
72e40 09 72 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20  .result->Srid = 
72e50 67 65 6f 31 2d 3e 53 72 69 64 3b 0a 09 09 67 61  geo1->Srid;...ga
72e60 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c  iaToSpatiaLiteBl
72e70 6f 62 57 6b 62 20 28 72 65 73 75 6c 74 2c 20 26  obWkb (result, &
72e80 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b  p_result, &len);
72e90 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ...sqlite3_resul
72ea0 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
72eb0 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
72ec0 66 72 65 65 29 3b 0a 09 09 67 61 69 61 46 72 65  free);...gaiaFre
72ed0 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c  eGeomColl (resul
72ee0 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20  t);..    }.     
72ef0 20 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47   }.    gaiaFreeG
72f00 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a  eomColl (geo1);.
72f10 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
72f20 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a  Coll (geo2);.}..
72f30 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
72f40 5f 43 6f 76 65 72 73 20 28 73 71 6c 69 74 65 33  _Covers (sqlite3
72f50 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
72f60 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
72f70 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
72f80 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
72f90 6e 63 74 69 6f 6e 3a 0a 2f 20 43 6f 76 65 72 73  nction:./ Covers
72fa0 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f  (BLOBencoded geo
72fb0 6d 31 2c 20 42 4c 4f 42 65 6e 63 6f 64 65 64 20  m1, BLOBencoded 
72fc0 67 65 6f 6d 32 29 0a 2f 0a 2f 20 72 65 74 75 72  geom2)././ retur
72fd0 6e 73 3a 0a 2f 20 31 20 69 66 20 47 45 4f 4d 2d  ns:./ 1 if GEOM-
72fe0 31 20 22 73 70 61 74 69 61 6c 6c 79 20 63 6f 76  1 "spatially cov
72ff0 65 72 73 22 20 47 45 4f 4d 2d 32 0a 2f 20 30 20  ers" GEOM-2./ 0 
73000 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f 72 20 2d  otherwise./ or -
73010 31 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69  1 if any error i
73020 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f  s encountered.*/
73030 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
73040 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20  ar *p_blob;.    
73050 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
73060 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
73070 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20   geo1 = NULL;.  
73080 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
73090 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b 0a 20  r geo2 = NULL;. 
730a0 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20 20 20     int ret;.    
730b0 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
730c0 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
730d0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
730e0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
730f0 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51   (argv[0]) != SQ
73100 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20  LITE_BLOB).     
73110 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
73120 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78  sult_int (contex
73130 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74 75 72  t, -1);..  retur
73140 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69  n;.      }.    i
73150 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
73160 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20  _type (argv[1]) 
73170 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
73180 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
73190 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
731a0 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20  ontext, -1);..  
731b0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
731c0 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
731d0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
731e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
731f0 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
73200 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
73210 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
73220 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
73230 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  o1 = gaiaFromSpa
73240 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
73250 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
73260 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  ;.    p_blob = (
73270 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
73280 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
73290 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20  lob (argv[1]);. 
732a0 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
732b0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
732c0 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20   (argv[1]);.    
732d0 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d 53  geo2 = gaiaFromS
732e0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
732f0 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
73300 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67 65 6f  s);.    if (!geo
73310 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09 73 71 6c  1 || !geo2)..sql
73320 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
73330 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 20  (context, -1);. 
73340 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
73350 09 20 20 72 65 74 20 3d 20 67 61 69 61 47 65 6f  .  ret = gaiaGeo
73360 6d 43 6f 6c 6c 43 6f 76 65 72 73 20 28 67 65 6f  mCollCovers (geo
73370 31 2c 20 67 65 6f 32 29 3b 0a 09 20 20 73 71 6c  1, geo2);..  sql
73380 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
73390 28 63 6f 6e 74 65 78 74 2c 20 72 65 74 29 3b 0a  (context, ret);.
733a0 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61        }.    gaia
733b0 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65  FreeGeomColl (ge
733c0 6f 31 29 3b 0a 20 20 20 20 67 61 69 61 46 72 65  o1);.    gaiaFre
733d0 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 29  eGeomColl (geo2)
733e0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
733f0 0a 66 6e 63 74 5f 43 6f 76 65 72 65 64 42 79 20  .fnct_CoveredBy 
73400 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
73410 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
73420 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
73430 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
73440 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
73450 2f 20 43 6f 76 65 72 65 64 42 79 28 42 4c 4f 42  / CoveredBy(BLOB
73460 65 6e 63 6f 64 65 64 20 67 65 6f 6d 31 2c 20 42  encoded geom1, B
73470 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 32  LOBencoded geom2
73480 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 3a 0a 2f  )././ returns:./
73490 20 31 20 69 66 20 47 45 4f 4d 2d 31 20 69 73 20   1 if GEOM-1 is 
734a0 22 73 70 61 74 69 61 6c 6c 79 20 63 6f 76 65 72  "spatially cover
734b0 65 64 20 62 79 22 20 47 45 4f 4d 2d 32 0a 2f 20  ed by" GEOM-2./ 
734c0 30 20 6f 74 68 65 72 77 69 73 65 0a 2f 20 6f 72  0 otherwise./ or
734d0 20 2d 31 20 69 66 20 61 6e 79 20 65 72 72 6f 72   -1 if any error
734e0 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
734f0 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
73500 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
73510 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
73520 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
73530 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a  tr geo1 = NULL;.
73540 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
73550 50 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b  Ptr geo2 = NULL;
73560 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a 20 20  .    int ret;.  
73570 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
73580 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
73590 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
735a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
735b0 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
735c0 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
735d0 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
735e0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
735f0 65 78 74 2c 20 2d 31 29 3b 0a 09 20 20 72 65 74  ext, -1);..  ret
73600 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
73610 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
73620 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d  ue_type (argv[1]
73630 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  ) != SQLITE_BLOB
73640 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  ).      {..  sql
73650 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
73660 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a 09  (context, -1);..
73670 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
73680 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
73690 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
736a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
736b0 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
736c0 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
736d0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
736e0 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
736f0 67 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53  geo1 = gaiaFromS
73700 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
73710 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
73720 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d  s);.    p_blob =
73730 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
73740 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
73750 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b  _blob (argv[1]);
73760 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
73770 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
73780 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  es (argv[1]);.  
73790 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f    geo2 = gaiaFro
737a0 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
737b0 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
737c0 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21 67  tes);.    if (!g
737d0 65 6f 31 20 7c 7c 20 21 67 65 6f 32 29 0a 09 73  eo1 || !geo2)..s
737e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
737f0 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b  t (context, -1);
73800 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20  .    else.      
73810 7b 0a 09 20 20 72 65 74 20 3d 20 67 61 69 61 47  {..  ret = gaiaG
73820 65 6f 6d 43 6f 6c 6c 43 6f 76 65 72 65 64 42 79  eomCollCoveredBy
73830 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09   (geo1, geo2);..
73840 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
73850 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 72  _int (context, r
73860 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  et);.      }.   
73870 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
73880 6c 20 28 67 65 6f 31 29 3b 0a 20 20 20 20 67 61  l (geo1);.    ga
73890 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28  iaFreeGeomColl (
738a0 67 65 6f 32 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  geo2);.}..static
738b0 20 76 6f 69 64 0a 66 6e 63 74 5f 4c 69 6e 65 49   void.fnct_LineI
738c0 6e 74 65 72 70 6f 6c 61 74 65 50 6f 69 6e 74 20  nterpolatePoint 
738d0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
738e0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
738f0 61 72 67 63 2c 0a 09 09 09 20 20 20 73 71 6c 69  argc,....   sqli
73900 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
73910 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
73920 74 69 6f 6e 3a 0a 2f 20 4c 69 6e 65 49 6e 74 65  tion:./ LineInte
73930 72 70 6f 6c 61 74 65 50 6f 69 6e 74 28 42 4c 4f  rpolatePoint(BLO
73940 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72  Bencoded geometr
73950 79 31 2c 20 64 6f 75 62 6c 65 20 66 72 61 63 74  y1, double fract
73960 69 6f 6e 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73  ion)././ returns
73970 20 61 20 6e 65 77 20 67 65 6f 6d 65 74 72 79 20   a new geometry 
73980 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61 20 70  representing a p
73990 6f 69 6e 74 20 69 6e 74 65 72 70 6f 6c 61 74 65  oint interpolate
739a0 64 20 61 6c 6f 6e 67 20 61 20 6c 69 6e 65 0a 2f  d along a line./
739b0 20 5b 61 20 4c 49 4e 45 53 54 52 49 4e 47 20 69   [a LINESTRING i
739c0 73 20 65 78 70 65 63 74 65 64 20 2f 20 66 72 61  s expected / fra
739d0 63 74 69 6f 6e 20 72 61 6e 67 69 6e 67 20 66 72  ction ranging fr
739e0 6f 6d 20 30 2e 30 20 74 6f 20 31 2e 30 5d 0a 2f  om 0.0 to 1.0]./
739f0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
73a00 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
73a10 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69  ered.*/.    unsi
73a20 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
73a30 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
73a40 65 73 3b 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f  es;.    int int_
73a50 76 61 6c 75 65 3b 0a 20 20 20 20 64 6f 75 62 6c  value;.    doubl
73a60 65 20 66 72 61 63 74 69 6f 6e 3b 0a 20 20 20 20  e fraction;.    
73a70 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
73a80 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  geo = NULL;.    
73a90 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
73aa0 72 65 73 75 6c 74 3b 0a 20 20 20 20 47 41 49 41  result;.    GAIA
73ab0 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
73ac0 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
73ad0 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
73ae0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
73af0 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
73b00 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
73b10 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
73b20 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
73b30 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
73b40 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69    }.    if (sqli
73b50 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
73b60 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49  argv[1]) == SQLI
73b70 54 45 5f 46 4c 4f 41 54 29 0a 09 66 72 61 63 74  TE_FLOAT)..fract
73b80 69 6f 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ion = sqlite3_va
73b90 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76  lue_double (argv
73ba0 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69  [1]);.    else i
73bb0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
73bc0 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20  _type (argv[1]) 
73bd0 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
73be0 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e  R).      {..  in
73bf0 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  t_value = sqlite
73c00 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
73c10 76 5b 31 5d 29 3b 0a 09 20 20 66 72 61 63 74 69  v[1]);..  fracti
73c20 6f 6e 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a  on = int_value;.
73c30 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
73c40 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
73c50 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
73c60 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
73c70 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
73c80 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69    p_blob = (unsi
73c90 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c  gned char *) sql
73ca0 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20  ite3_value_blob 
73cb0 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e  (argv[0]);.    n
73cc0 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33  _bytes = sqlite3
73cd0 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72  _value_bytes (ar
73ce0 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65 6f 20  gv[0]);.    geo 
73cf0 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
73d00 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
73d10 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
73d20 20 20 20 69 66 20 28 67 65 6f 20 3d 3d 20 4e 55     if (geo == NU
73d30 4c 4c 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  LL)..sqlite3_res
73d40 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
73d50 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20  t);.    else.   
73d60 20 20 20 7b 0a 09 20 20 72 65 73 75 6c 74 20 3d     {..  result =
73d70 20 67 61 69 61 4c 69 6e 65 49 6e 74 65 72 70 6f   gaiaLineInterpo
73d80 6c 61 74 65 50 6f 69 6e 74 20 28 67 65 6f 2c 20  latePoint (geo, 
73d90 66 72 61 63 74 69 6f 6e 29 3b 0a 09 20 20 69 66  fraction);..  if
73da0 20 28 21 72 65 73 75 6c 74 29 0a 09 20 20 20 20   (!result)..    
73db0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
73dc0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
73dd0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
73de0 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20  ../* builds the 
73df0 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f  BLOB geometry to
73e00 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a   be returned */.
73e10 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73  ..int len;...uns
73e20 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
73e30 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72  sult = NULL;...r
73e40 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65  esult->Srid = ge
73e50 6f 2d 3e 53 72 69 64 3b 0a 09 09 67 61 69 61 54  o->Srid;...gaiaT
73e60 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  oSpatiaLiteBlobW
73e70 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72  kb (result, &p_r
73e80 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09  esult, &len);...
73e90 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
73ea0 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
73eb0 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
73ec0 65 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65  e);...gaiaFreeGe
73ed0 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b  omColl (result);
73ee0 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a  ..    }.      }.
73ef0 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d      gaiaFreeGeom
73f00 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73  Coll (geo);.}..s
73f10 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
73f20 4c 69 6e 65 49 6e 74 65 72 70 6f 6c 61 74 65 45  LineInterpolateE
73f30 71 75 69 64 69 73 74 61 6e 74 50 6f 69 6e 74 73  quidistantPoints
73f40 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
73f50 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
73f60 20 61 72 67 63 2c 0a 09 09 09 09 20 20 20 20 20   argc,.....     
73f70 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
73f80 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
73f90 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 4c 69  L function:./ Li
73fa0 6e 65 49 6e 74 65 72 70 6f 6c 61 74 65 45 71 75  neInterpolateEqu
73fb0 69 64 69 73 74 61 6e 74 50 6f 69 6e 74 53 28 42  idistantPointS(B
73fc0 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65  LOBencoded geome
73fd0 74 72 79 31 2c 20 64 6f 75 62 6c 65 20 64 69 73  try1, double dis
73fe0 74 61 6e 63 65 29 0a 2f 0a 2f 20 72 65 74 75 72  tance)././ retur
73ff0 6e 73 20 61 20 6e 65 77 20 67 65 6f 6d 65 74 72  ns a new geometr
74000 79 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61  y representing a
74010 20 70 6f 69 6e 74 20 69 6e 74 65 72 70 6f 6c 61   point interpola
74020 74 65 64 20 61 6c 6f 6e 67 20 61 20 6c 69 6e 65  ted along a line
74030 0a 2f 20 5b 61 20 4c 49 4e 45 53 54 52 49 4e 47  ./ [a LINESTRING
74040 20 69 73 20 65 78 70 65 63 74 65 64 20 2f 20 66   is expected / f
74050 72 61 63 74 69 6f 6e 20 72 61 6e 67 69 6e 67 20  raction ranging 
74060 66 72 6f 6d 20 30 2e 30 20 74 6f 20 31 2e 30 5d  from 0.0 to 1.0]
74070 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
74080 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
74090 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
740a0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
740b0 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
740c0 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 69 6e  ytes;.    int in
740d0 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64 6f 75  t_value;.    dou
740e0 62 6c 65 20 64 69 73 74 61 6e 63 65 3b 0a 20 20  ble distance;.  
740f0 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
74100 72 20 67 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20  r geo = NULL;.  
74110 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74    gaiaGeomCollPt
74120 72 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 47 41  r result;.    GA
74130 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
74140 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
74150 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
74160 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
74170 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
74180 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
74190 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
741a0 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
741b0 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
741c0 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71      }.    if (sq
741d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
741e0 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51   (argv[1]) == SQ
741f0 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 64 69 73  LITE_FLOAT)..dis
74200 74 61 6e 63 65 20 3d 20 73 71 6c 69 74 65 33 5f  tance = sqlite3_
74210 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72  value_double (ar
74220 67 76 5b 31 5d 29 3b 0a 20 20 20 20 65 6c 73 65  gv[1]);.    else
74230 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
74240 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d  ue_type (argv[1]
74250 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  ) == SQLITE_INTE
74260 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  GER).      {..  
74270 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69  int_value = sqli
74280 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61  te3_value_int (a
74290 72 67 76 5b 31 5d 29 3b 0a 09 20 20 64 69 73 74  rgv[1]);..  dist
742a0 61 6e 63 65 20 3d 20 69 6e 74 5f 76 61 6c 75 65  ance = int_value
742b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c  ;.      }.    el
742c0 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  se.      {..  sq
742d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
742e0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
742f0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
74300 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
74310 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
74320 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
74330 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
74340 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
74350 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
74360 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
74370 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74  o = gaiaFromSpat
74380 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70  iaLiteBlobWkb (p
74390 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b  _blob, n_bytes);
743a0 0a 20 20 20 20 69 66 20 28 67 65 6f 20 3d 3d 20  .    if (geo == 
743b0 4e 55 4c 4c 29 0a 09 73 71 6c 69 74 65 33 5f 72  NULL)..sqlite3_r
743c0 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
743d0 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20  ext);.    else. 
743e0 20 20 20 20 20 7b 0a 09 20 20 72 65 73 75 6c 74       {..  result
743f0 20 3d 20 67 61 69 61 4c 69 6e 65 49 6e 74 65 72   = gaiaLineInter
74400 70 6f 6c 61 74 65 45 71 75 69 64 69 73 74 61 6e  polateEquidistan
74410 74 50 6f 69 6e 74 73 20 28 67 65 6f 2c 20 64 69  tPoints (geo, di
74420 73 74 61 6e 63 65 29 3b 0a 09 20 20 69 66 20 28  stance);..  if (
74430 21 72 65 73 75 6c 74 29 0a 09 20 20 20 20 20 20  !result)..      
74440 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
74450 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
74460 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09    else..    {...
74470 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20 42 4c  /* builds the BL
74480 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f 20 62  OB geometry to b
74490 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 09 09  e returned */...
744a0 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73 69 67  int len;...unsig
744b0 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75  ned char *p_resu
744c0 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72 65 73  lt = NULL;...res
744d0 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d  ult->Srid = geo-
744e0 3e 53 72 69 64 3b 0a 09 09 67 61 69 61 54 6f 53  >Srid;...gaiaToS
744f0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
74500 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65 73   (result, &p_res
74510 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 73 71  ult, &len);...sq
74520 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
74530 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65  b (context, p_re
74540 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29  sult, len, free)
74550 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d  ;...gaiaFreeGeom
74560 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09  Coll (result);..
74570 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
74580 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
74590 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61  ll (geo);.}..sta
745a0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4c 69  tic void.fnct_Li
745b0 6e 65 4c 6f 63 61 74 65 50 6f 69 6e 74 20 28 73  neLocatePoint (s
745c0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
745d0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
745e0 67 63 2c 0a 09 09 20 20 20 20 20 20 73 71 6c 69  gc,...      sqli
745f0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
74600 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
74610 74 69 6f 6e 3a 0a 2f 20 4c 69 6e 65 4c 6f 63 61  tion:./ LineLoca
74620 74 65 50 6f 69 6e 74 28 42 4c 4f 42 65 6e 63 6f  tePoint(BLOBenco
74630 64 65 64 20 67 65 6f 6d 65 74 72 79 31 2c 20 42  ded geometry1, B
74640 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65  LOBencoded geome
74650 74 72 79 32 29 0a 2f 0a 2f 20 72 65 74 75 72 6e  try2)././ return
74660 20 61 20 6e 75 6d 62 65 72 20 28 62 65 74 77 65   a number (betwe
74670 65 6e 20 30 2e 30 20 61 6e 64 20 31 2e 30 29 20  en 0.0 and 1.0) 
74680 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65  representing the
74690 20 6c 6f 63 61 74 69 6f 6e 20 0a 2f 20 6f 66 20   location ./ of 
746a0 74 68 65 20 63 6c 6f 73 65 73 74 20 70 6f 69 6e  the closest poin
746b0 74 20 6f 6e 20 4c 69 6e 65 53 74 72 69 6e 67 20  t on LineString 
746c0 74 6f 20 74 68 65 20 67 69 76 65 6e 20 50 6f 69  to the given Poi
746d0 6e 74 2c 20 61 73 20 61 20 66 72 61 63 74 69 6f  nt, as a fractio
746e0 6e 20 0a 2f 20 6f 66 20 74 6f 74 61 6c 20 32 64  n ./ of total 2d
746f0 20 6c 69 6e 65 20 6c 65 6e 67 74 68 0a 2f 0a 2f   line length././
74700 20 2d 20 67 65 6f 6d 31 20 69 73 20 65 78 70 65   - geom1 is expe
74710 63 74 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e  cted to represen
74720 74 20 73 6f 6d 65 20 4c 49 4e 45 53 54 52 49 4e  t some LINESTRIN
74730 47 0a 2f 20 2d 20 67 65 6f 6d 32 20 69 73 20 65  G./ - geom2 is e
74740 78 70 65 63 74 65 64 20 74 6f 20 72 65 70 72 65  xpected to repre
74750 73 65 6e 74 20 73 6f 6d 65 20 50 4f 49 4e 54 0a  sent some POINT.
74760 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
74770 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
74780 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
74790 20 20 20 64 6f 75 62 6c 65 20 66 72 61 63 74 69     double fracti
747a0 6f 6e 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d  on;.    gaiaGeom
747b0 43 6f 6c 6c 50 74 72 20 67 65 6f 31 20 3d 20 4e  CollPtr geo1 = N
747c0 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ULL;.    gaiaGeo
747d0 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 32 20 3d 20  mCollPtr geo2 = 
747e0 4e 55 4c 4c 3b 0a 20 20 20 20 47 41 49 41 5f 55  NULL;.    GAIA_U
747f0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
74800 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
74810 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
74820 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
74830 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
74840 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
74850 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
74860 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
74870 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
74880 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
74890 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
748a0 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[1]) != SQLITE
748b0 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
748c0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
748d0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
748e0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
748f0 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
74900 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
74910 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
74920 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
74930 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
74940 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
74950 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
74960 20 20 67 65 6f 31 20 3d 20 67 61 69 61 46 72 6f    geo1 = gaiaFro
74970 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
74980 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
74990 74 65 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62  tes);.    p_blob
749a0 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
749b0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
749c0 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d  ue_blob (argv[1]
749d0 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
749e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
749f0 79 74 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a  ytes (argv[1]);.
74a00 20 20 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46      geo2 = gaiaF
74a10 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  romSpatiaLiteBlo
74a20 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f  bWkb (p_blob, n_
74a30 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28  bytes);.    if (
74a40 67 65 6f 31 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20  geo1 == NULL || 
74a50 67 65 6f 32 20 3d 3d 20 4e 55 4c 4c 29 0a 09 73  geo2 == NULL)..s
74a60 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
74a70 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ll (context);.  
74a80 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09    else.      {..
74a90 20 20 66 72 61 63 74 69 6f 6e 20 3d 20 67 61 69    fraction = gai
74aa0 61 4c 69 6e 65 4c 6f 63 61 74 65 50 6f 69 6e 74  aLineLocatePoint
74ab0 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b 0a 09   (geo1, geo2);..
74ac0 20 20 69 66 20 28 66 72 61 63 74 69 6f 6e 20 3e    if (fraction >
74ad0 3d 20 30 2e 30 20 26 26 20 66 72 61 63 74 69 6f  = 0.0 && fractio
74ae0 6e 20 3c 3d 20 31 2e 30 29 0a 09 20 20 20 20 20  n <= 1.0)..     
74af0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
74b00 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c  double (context,
74b10 20 66 72 61 63 74 69 6f 6e 29 3b 0a 09 20 20 65   fraction);..  e
74b20 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lse..      sqlit
74b30 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
74b40 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
74b50 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
74b60 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a 20  omColl (geo1);. 
74b70 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
74b80 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a 73  oll (geo2);.}..s
74b90 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
74ba0 4c 69 6e 65 53 75 62 73 74 72 69 6e 67 20 28 73  LineSubstring (s
74bb0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
74bc0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
74bd0 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
74be0 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
74bf0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
74c00 4c 69 6e 65 53 75 62 73 74 72 69 6e 67 28 42 4c  LineSubstring(BL
74c10 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74  OBencoded geomet
74c20 72 79 31 2c 20 64 6f 75 62 6c 65 20 73 74 61 72  ry1, double star
74c30 74 5f 66 72 61 63 74 69 6f 6e 2c 20 64 6f 75 62  t_fraction, doub
74c40 6c 65 20 65 6e 64 5f 66 72 61 63 74 69 6f 6e 29  le end_fraction)
74c50 0a 2f 0a 2f 20 52 65 74 75 72 6e 20 61 20 4c 69  ././ Return a Li
74c60 6e 65 73 74 72 69 6e 67 20 62 65 69 6e 67 20 61  nestring being a
74c70 20 73 75 62 73 74 72 69 6e 67 20 6f 66 20 74 68   substring of th
74c80 65 20 69 6e 70 75 74 20 6f 6e 65 20 73 74 61 72  e input one star
74c90 74 69 6e 67 20 61 6e 64 20 65 6e 64 69 6e 67 20  ting and ending 
74ca0 61 74 20 0a 2f 20 74 68 65 20 67 69 76 65 6e 20  at ./ the given 
74cb0 66 72 61 63 74 69 6f 6e 73 20 6f 66 20 74 6f 74  fractions of tot
74cc0 61 6c 20 32 64 20 6c 65 6e 67 74 68 20 5b 66 72  al 2d length [fr
74cd0 61 63 74 69 6f 6e 73 20 72 61 6e 67 69 6e 67 20  actions ranging 
74ce0 66 72 6f 6d 20 30 2e 30 20 74 6f 20 31 2e 30 5d  from 0.0 to 1.0]
74cf0 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
74d00 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
74d10 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e  ntered.*/.    un
74d20 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62  signed char *p_b
74d30 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62  lob;.    int n_b
74d40 79 74 65 73 3b 0a 20 20 20 20 69 6e 74 20 69 6e  ytes;.    int in
74d50 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64 6f 75  t_value;.    dou
74d60 62 6c 65 20 66 72 61 63 74 69 6f 6e 31 3b 0a 20  ble fraction1;. 
74d70 20 20 20 64 6f 75 62 6c 65 20 66 72 61 63 74 69     double fracti
74d80 6f 6e 32 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  on2;.    gaiaGeo
74d90 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e  mCollPtr geo = N
74da0 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 47 65 6f  ULL;.    gaiaGeo
74db0 6d 43 6f 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b  mCollPtr result;
74dc0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
74dd0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
74de0 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
74df0 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
74e00 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
74e10 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a  != SQLITE_BLOB).
74e20 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
74e30 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
74e40 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
74e50 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
74e60 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
74e70 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 31 5d  ue_type (argv[1]
74e80 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41  ) == SQLITE_FLOA
74e90 54 29 0a 09 66 72 61 63 74 69 6f 6e 31 20 3d 20  T)..fraction1 = 
74ea0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
74eb0 75 62 6c 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a  uble (argv[1]);.
74ec0 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c      else if (sql
74ed0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
74ee0 28 61 72 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c  (argv[1]) == SQL
74ef0 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20  ITE_INTEGER).   
74f00 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75     {..  int_valu
74f10 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
74f20 65 5f 69 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b  e_int (argv[1]);
74f30 0a 09 20 20 66 72 61 63 74 69 6f 6e 31 20 3d 20  ..  fraction1 = 
74f40 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20  int_value;.     
74f50 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20   }.    else.    
74f60 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
74f70 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
74f80 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
74f90 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
74fa0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
74fb0 79 70 65 20 28 61 72 67 76 5b 32 5d 29 20 3d 3d  ype (argv[2]) ==
74fc0 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09   SQLITE_FLOAT)..
74fd0 66 72 61 63 74 69 6f 6e 32 20 3d 20 73 71 6c 69  fraction2 = sqli
74fe0 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
74ff0 20 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20   (argv[2]);.    
75000 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33  else if (sqlite3
75010 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
75020 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[2]) == SQLITE_
75030 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b  INTEGER).      {
75040 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20  ..  int_value = 
75050 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
75060 74 20 28 61 72 67 76 5b 32 5d 29 3b 0a 09 20 20  t (argv[2]);..  
75070 66 72 61 63 74 69 6f 6e 32 20 3d 20 69 6e 74 5f  fraction2 = int_
75080 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  value;.      }. 
75090 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
750a0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
750b0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
750c0 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
750d0 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20     }.    p_blob 
750e0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
750f0 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
75100 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29  e_blob (argv[0])
75110 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
75120 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
75130 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  tes (argv[0]);. 
75140 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72 6f     geo = gaiaFro
75150 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57  mSpatiaLiteBlobW
75160 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79  kb (p_blob, n_by
75170 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 67 65  tes);.    if (ge
75180 6f 20 3d 3d 20 4e 55 4c 4c 29 0a 09 73 71 6c 69  o == NULL)..sqli
75190 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
751a0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65  (context);.    e
751b0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72  lse.      {..  r
751c0 65 73 75 6c 74 20 3d 20 67 61 69 61 4c 69 6e 65  esult = gaiaLine
751d0 53 75 62 73 74 72 69 6e 67 20 28 67 65 6f 2c 20  Substring (geo, 
751e0 66 72 61 63 74 69 6f 6e 31 2c 20 66 72 61 63 74  fraction1, fract
751f0 69 6f 6e 32 29 3b 0a 09 20 20 69 66 20 28 21 72  ion2);..  if (!r
75200 65 73 75 6c 74 29 0a 09 20 20 20 20 20 20 73 71  esult)..      sq
75210 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
75220 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
75230 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09 09 2f 2a  else..    {.../*
75240 20 62 75 69 6c 64 73 20 74 68 65 20 42 4c 4f 42   builds the BLOB
75250 20 67 65 6f 6d 65 74 72 79 20 74 6f 20 62 65 20   geometry to be 
75260 72 65 74 75 72 6e 65 64 20 2a 2f 0a 09 09 69 6e  returned */...in
75270 74 20 6c 65 6e 3b 0a 09 09 75 6e 73 69 67 6e 65  t len;...unsigne
75280 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75 6c 74  d char *p_result
75290 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72 65 73 75 6c   = NULL;...resul
752a0 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f 2d 3e 53  t->Srid = geo->S
752b0 72 69 64 3b 0a 09 09 67 61 69 61 54 6f 53 70 61  rid;...gaiaToSpa
752c0 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
752d0 72 65 73 75 6c 74 2c 20 26 70 5f 72 65 73 75 6c  result, &p_resul
752e0 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 73 71 6c 69  t, &len);...sqli
752f0 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
75300 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72 65 73 75  (context, p_resu
75310 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65 29 3b 0a  lt, len, free);.
75320 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f  ..gaiaFreeGeomCo
75330 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09 20 20  ll (result);..  
75340 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
75350 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
75360 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69   (geo);.}..stati
75370 63 20 76 6f 69 64 0a 66 6e 63 74 5f 43 6c 6f 73  c void.fnct_Clos
75380 65 73 74 50 6f 69 6e 74 20 28 73 71 6c 69 74 65  estPoint (sqlite
75390 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
753a0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
753b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
753c0 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
753d0 75 6e 63 74 69 6f 6e 3a 0a 2f 20 43 6c 6f 73 65  unction:./ Close
753e0 73 74 50 6f 69 6e 74 28 42 4c 4f 42 65 6e 63 6f  stPoint(BLOBenco
753f0 64 65 64 20 67 65 6f 6d 65 74 72 79 31 2c 20 42  ded geometry1, B
75400 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65  LOBencoded geome
75410 74 72 79 32 29 0a 2f 0a 2f 20 52 65 74 75 72 6e  try2)././ Return
75420 73 20 74 68 65 20 50 6f 69 6e 74 20 6f 6e 20 67  s the Point on g
75430 65 6f 6d 31 20 74 68 61 74 20 69 73 20 63 6c 6f  eom1 that is clo
75440 73 65 73 74 20 74 6f 20 67 65 6f 6d 32 0a 2f 20  sest to geom2./ 
75450 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
75460 20 66 6f 72 20 69 6e 76 61 6c 69 64 20 61 72 67   for invalid arg
75470 75 6d 65 6e 74 73 20 28 6f 72 20 69 66 20 64 69  uments (or if di
75480 73 74 61 6e 63 65 20 69 73 20 5a 45 52 4f 29 0a  stance is ZERO).
75490 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
754a0 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
754b0 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
754c0 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
754d0 74 72 20 67 65 6f 31 20 3d 20 4e 55 4c 4c 3b 0a  tr geo1 = NULL;.
754e0 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c      gaiaGeomColl
754f0 50 74 72 20 67 65 6f 32 20 3d 20 4e 55 4c 4c 3b  Ptr geo2 = NULL;
75500 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c  .    gaiaGeomCol
75510 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20  lPtr result;.   
75520 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
75530 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
75540 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
75550 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
75560 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53  e (argv[0]) != S
75570 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20  QLITE_BLOB).    
75580 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
75590 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
755a0 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
755b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
755c0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
755d0 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 21 3d  ype (argv[1]) !=
755e0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
755f0 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
75600 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
75610 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
75620 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
75630 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
75640 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
75650 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
75660 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
75670 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
75680 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
75690 30 5d 29 3b 0a 20 20 20 20 67 65 6f 31 20 3d 20  0]);.    geo1 = 
756a0 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69  gaiaFromSpatiaLi
756b0 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f  teBlobWkb (p_blo
756c0 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20  b, n_bytes);.   
756d0 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67   p_blob = (unsig
756e0 6e 65 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69  ned char *) sqli
756f0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28  te3_value_blob (
75700 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f  argv[1]);.    n_
75710 62 79 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f  bytes = sqlite3_
75720 76 61 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67  value_bytes (arg
75730 76 5b 31 5d 29 3b 0a 20 20 20 20 67 65 6f 32 20  v[1]);.    geo2 
75740 3d 20 67 61 69 61 46 72 6f 6d 53 70 61 74 69 61  = gaiaFromSpatia
75750 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62  LiteBlobWkb (p_b
75760 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20  lob, n_bytes);. 
75770 20 20 20 69 66 20 28 67 65 6f 31 20 3d 3d 20 4e     if (geo1 == N
75780 55 4c 4c 20 7c 7c 20 67 65 6f 32 20 3d 3d 20 4e  ULL || geo2 == N
75790 55 4c 4c 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  ULL)..sqlite3_re
757a0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
757b0 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  xt);.    else.  
757c0 20 20 20 20 7b 0a 09 20 20 72 65 73 75 6c 74 20      {..  result 
757d0 3d 20 67 61 69 61 53 68 6f 72 74 65 73 74 4c 69  = gaiaShortestLi
757e0 6e 65 20 28 67 65 6f 31 2c 20 67 65 6f 32 29 3b  ne (geo1, geo2);
757f0 0a 09 20 20 69 66 20 28 72 65 73 75 6c 74 20 3d  ..  if (result =
75800 3d 20 4e 55 4c 4c 29 0a 09 20 20 20 20 20 20 73  = NULL)..      s
75810 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
75820 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
75830 20 65 6c 73 65 20 69 66 20 28 72 65 73 75 6c 74   else if (result
75840 2d 3e 46 69 72 73 74 4c 69 6e 65 73 74 72 69 6e  ->FirstLinestrin
75850 67 20 3d 3d 20 4e 55 4c 4c 29 0a 09 20 20 20 20  g == NULL)..    
75860 7b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d  {...gaiaFreeGeom
75870 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a 09  Coll (result);..
75880 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
75890 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
758a0 09 20 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09  .    }..  else..
758b0 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75 69 6c 64      {.../* build
758c0 73 20 74 68 65 20 42 4c 4f 42 20 67 65 6f 6d 65  s the BLOB geome
758d0 74 72 79 20 74 6f 20 62 65 20 72 65 74 75 72 6e  try to be return
758e0 65 64 20 2a 2f 0a 09 09 64 6f 75 62 6c 65 20 78  ed */...double x
758f0 3b 0a 09 09 64 6f 75 62 6c 65 20 79 3b 0a 09 09  ;...double y;...
75900 64 6f 75 62 6c 65 20 7a 3b 0a 09 09 64 6f 75 62  double z;...doub
75910 6c 65 20 6d 3b 0a 09 09 69 6e 74 20 6c 65 6e 3b  le m;...int len;
75920 0a 09 09 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ...unsigned char
75930 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c   *p_result = NUL
75940 4c 3b 0a 09 09 67 61 69 61 47 65 6f 6d 43 6f 6c  L;...gaiaGeomCol
75950 6c 50 74 72 20 70 74 20 3d 20 4e 55 4c 4c 3b 0a  lPtr pt = NULL;.
75960 09 09 67 61 69 61 4c 69 6e 65 73 74 72 69 6e 67  ..gaiaLinestring
75970 50 74 72 20 6c 6e 20 3d 20 72 65 73 75 6c 74 2d  Ptr ln = result-
75980 3e 46 69 72 73 74 4c 69 6e 65 73 74 72 69 6e 67  >FirstLinestring
75990 3b 0a 09 09 69 66 20 28 6c 6e 2d 3e 44 69 6d 65  ;...if (ln->Dime
759a0 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41  nsionModel == GA
759b0 49 41 5f 58 59 5f 5a 29 0a 09 09 20 20 20 20 70  IA_XY_Z)...    p
759c0 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f  t = gaiaAllocGeo
759d0 6d 43 6f 6c 6c 58 59 5a 20 28 29 3b 0a 09 09 65  mCollXYZ ();...e
759e0 6c 73 65 20 69 66 20 28 6c 6e 2d 3e 44 69 6d 65  lse if (ln->Dime
759f0 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41  nsionModel == GA
75a00 49 41 5f 58 59 5f 4d 29 0a 09 09 20 20 20 20 70  IA_XY_M)...    p
75a10 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f  t = gaiaAllocGeo
75a20 6d 43 6f 6c 6c 58 59 4d 20 28 29 3b 0a 09 09 65  mCollXYM ();...e
75a30 6c 73 65 20 69 66 20 28 6c 6e 2d 3e 44 69 6d 65  lse if (ln->Dime
75a40 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41  nsionModel == GA
75a50 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 20 20 20  IA_XY_Z_M)...   
75a60 20 70 74 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47   pt = gaiaAllocG
75a70 65 6f 6d 43 6f 6c 6c 58 59 5a 4d 20 28 29 3b 0a  eomCollXYZM ();.
75a80 09 09 65 6c 73 65 0a 09 09 20 20 20 20 70 74 20  ..else...    pt 
75a90 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43  = gaiaAllocGeomC
75aa0 6f 6c 6c 20 28 29 3b 0a 09 09 69 66 20 28 6c 6e  oll ();...if (ln
75ab0 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c  ->DimensionModel
75ac0 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09   == GAIA_XY_Z)..
75ad0 09 20 20 7b 0a 09 09 20 20 20 20 20 20 67 61 69  .  {...      gai
75ae0 61 47 65 74 50 6f 69 6e 74 58 59 5a 20 28 6c 6e  aGetPointXYZ (ln
75af0 2d 3e 43 6f 6f 72 64 73 2c 20 30 2c 20 26 78 2c  ->Coords, 0, &x,
75b00 20 26 79 2c 20 26 7a 29 3b 0a 09 09 20 20 20 20   &y, &z);...    
75b10 20 20 67 61 69 61 41 64 64 50 6f 69 6e 74 54 6f    gaiaAddPointTo
75b20 47 65 6f 6d 43 6f 6c 6c 58 59 5a 20 28 70 74 2c  GeomCollXYZ (pt,
75b30 20 78 2c 20 79 2c 20 7a 29 3b 0a 09 09 20 20 7d   x, y, z);...  }
75b40 0a 09 09 65 6c 73 65 20 69 66 20 28 6c 6e 2d 3e  ...else if (ln->
75b50 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
75b60 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 09 20  = GAIA_XY_M)... 
75b70 20 7b 0a 09 09 20 20 20 20 20 20 67 61 69 61 47   {...      gaiaG
75b80 65 74 50 6f 69 6e 74 58 59 4d 20 28 6c 6e 2d 3e  etPointXYM (ln->
75b90 43 6f 6f 72 64 73 2c 20 30 2c 20 26 78 2c 20 26  Coords, 0, &x, &
75ba0 79 2c 20 26 6d 29 3b 0a 09 09 20 20 20 20 20 20  y, &m);...      
75bb0 67 61 69 61 41 64 64 50 6f 69 6e 74 54 6f 47 65  gaiaAddPointToGe
75bc0 6f 6d 43 6f 6c 6c 58 59 4d 20 28 70 74 2c 20 78  omCollXYM (pt, x
75bd0 2c 20 79 2c 20 6d 29 3b 0a 09 09 20 20 7d 0a 09  , y, m);...  }..
75be0 09 65 6c 73 65 20 69 66 20 28 6c 6e 2d 3e 44 69  .else if (ln->Di
75bf0 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20  mensionModel == 
75c00 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09 09 20  GAIA_XY_Z_M)... 
75c10 20 7b 0a 09 09 20 20 20 20 20 20 67 61 69 61 47   {...      gaiaG
75c20 65 74 50 6f 69 6e 74 58 59 5a 4d 20 28 6c 6e 2d  etPointXYZM (ln-
75c30 3e 43 6f 6f 72 64 73 2c 20 30 2c 20 26 78 2c 20  >Coords, 0, &x, 
75c40 26 79 2c 20 26 7a 2c 20 26 6d 29 3b 0a 09 09 20  &y, &z, &m);... 
75c50 20 20 20 20 20 67 61 69 61 41 64 64 50 6f 69 6e       gaiaAddPoin
75c60 74 54 6f 47 65 6f 6d 43 6f 6c 6c 58 59 5a 4d 20  tToGeomCollXYZM 
75c70 28 70 74 2c 20 78 2c 20 79 2c 20 7a 2c 20 6d 29  (pt, x, y, z, m)
75c80 3b 0a 09 09 20 20 7d 0a 09 09 65 6c 73 65 0a 09  ;...  }...else..
75c90 09 20 20 7b 0a 09 09 20 20 20 20 20 20 67 61 69  .  {...      gai
75ca0 61 47 65 74 50 6f 69 6e 74 20 28 6c 6e 2d 3e 43  aGetPoint (ln->C
75cb0 6f 6f 72 64 73 2c 20 30 2c 20 26 78 2c 20 26 79  oords, 0, &x, &y
75cc0 29 3b 0a 09 09 20 20 20 20 20 20 67 61 69 61 41  );...      gaiaA
75cd0 64 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c  ddPointToGeomCol
75ce0 6c 20 28 70 74 2c 20 78 2c 20 79 29 3b 0a 09 09  l (pt, x, y);...
75cf0 20 20 7d 0a 09 09 70 74 2d 3e 53 72 69 64 20 3d    }...pt->Srid =
75d00 20 67 65 6f 31 2d 3e 53 72 69 64 3b 0a 09 09 67   geo1->Srid;...g
75d10 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42  aiaToSpatiaLiteB
75d20 6c 6f 62 57 6b 62 20 28 70 74 2c 20 26 70 5f 72  lobWkb (pt, &p_r
75d30 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09  esult, &len);...
75d40 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
75d50 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f  lob (context, p_
75d60 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65  result, len, fre
75d70 65 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65  e);...gaiaFreeGe
75d80 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b  omColl (result);
75d90 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f 6d 43  ...gaiaFreeGeomC
75da0 6f 6c 6c 20 28 70 74 29 3b 0a 09 20 20 20 20 7d  oll (pt);..    }
75db0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 67 61 69  .      }.    gai
75dc0 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67  aFreeGeomColl (g
75dd0 65 6f 31 29 3b 0a 20 20 20 20 67 61 69 61 46 72  eo1);.    gaiaFr
75de0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32  eeGeomColl (geo2
75df0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
75e00 64 0a 66 6e 63 74 5f 53 68 6f 72 74 65 73 74 4c  d.fnct_ShortestL
75e10 69 6e 65 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ine (sqlite3_con
75e20 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
75e30 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
75e40 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
75e50 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
75e60 6f 6e 3a 0a 2f 20 53 68 6f 72 74 65 73 74 4c 69  on:./ ShortestLi
75e70 6e 65 28 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67  ne(BLOBencoded g
75e80 65 6f 6d 65 74 72 79 31 2c 20 42 4c 4f 42 65 6e  eometry1, BLOBen
75e90 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 32 29  coded geometry2)
75ea0 0a 2f 0a 2f 20 52 65 74 75 72 6e 73 20 74 68 65  ././ Returns the
75eb0 20 73 68 6f 72 74 65 73 74 20 6c 69 6e 65 20 62   shortest line b
75ec0 65 74 77 65 65 6e 20 74 77 6f 20 67 65 6f 6d 65  etween two geome
75ed0 74 72 69 65 73 0a 2f 20 4e 55 4c 4c 20 69 73 20  tries./ NULL is 
75ee0 72 65 74 75 72 6e 65 64 20 66 6f 72 20 69 6e 76  returned for inv
75ef0 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 73 20 28  alid arguments (
75f00 6f 72 20 69 66 20 64 69 73 74 61 6e 63 65 20 69  or if distance i
75f10 73 20 5a 45 52 4f 29 0a 2a 2f 0a 20 20 20 20 75  s ZERO).*/.    u
75f20 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
75f30 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
75f40 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61 47  bytes;.    gaiaG
75f50 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 31 20  eomCollPtr geo1 
75f60 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61  = NULL;.    gaia
75f70 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 32  GeomCollPtr geo2
75f80 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
75f90 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65 73  aGeomCollPtr res
75fa0 75 6c 74 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e  ult;.    GAIA_UN
75fb0 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f  USED ();../* LCO
75fc0 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20  V_EXCL_LINE */. 
75fd0 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76     if (sqlite3_v
75fe0 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
75ff0 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c  0]) != SQLITE_BL
76000 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  OB).      {..  s
76010 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
76020 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
76030 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
76040 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
76050 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
76060 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f  v[1]) != SQLITE_
76070 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
76080 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
76090 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
760a0 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
760b0 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
760c0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
760d0 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
760e0 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
760f0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
76100 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
76110 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
76120 20 67 65 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d   geo1 = gaiaFrom
76130 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
76140 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74  b (p_blob, n_byt
76150 65 73 29 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20  es);.    p_blob 
76160 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
76170 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   *) sqlite3_valu
76180 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29  e_blob (argv[1])
76190 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20  ;.    n_bytes = 
761a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
761b0 74 65 73 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20  tes (argv[1]);. 
761c0 20 20 20 67 65 6f 32 20 3d 20 67 61 69 61 46 72     geo2 = gaiaFr
761d0 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
761e0 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
761f0 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 67  ytes);.    if (g
76200 65 6f 31 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 67  eo1 == NULL || g
76210 65 6f 32 20 3d 3d 20 4e 55 4c 4c 29 0a 09 73 71  eo2 == NULL)..sq
76220 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
76230 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
76240 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
76250 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61 53 68   result = gaiaSh
76260 6f 72 74 65 73 74 4c 69 6e 65 20 28 67 65 6f 31  ortestLine (geo1
76270 2c 20 67 65 6f 32 29 3b 0a 09 20 20 73 71 6c 69  , geo2);..  sqli
76280 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
76290 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 69 66  (context);..  if
762a0 20 28 21 72 65 73 75 6c 74 29 0a 09 20 20 20 20   (!result)..    
762b0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
762c0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
762d0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a  ..  else..    {.
762e0 09 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20  ../* builds the 
762f0 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f  BLOB geometry to
76300 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a   be returned */.
76310 09 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73  ..int len;...uns
76320 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65  igned char *p_re
76330 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72  sult = NULL;...r
76340 65 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65  esult->Srid = ge
76350 6f 31 2d 3e 53 72 69 64 3b 0a 09 09 67 61 69 61  o1->Srid;...gaia
76360 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  ToSpatiaLiteBlob
76370 57 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f  Wkb (result, &p_
76380 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09  result, &len);..
76390 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
763a0 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70  blob (context, p
763b0 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72  _result, len, fr
763c0 65 65 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47  ee);...gaiaFreeG
763d0 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29  eomColl (result)
763e0 3b 0a 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d  ;..    }.      }
763f0 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65 6f  .    gaiaFreeGeo
76400 6d 43 6f 6c 6c 20 28 67 65 6f 31 29 3b 0a 20 20  mColl (geo1);.  
76410 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f    gaiaFreeGeomCo
76420 6c 6c 20 28 67 65 6f 32 29 3b 0a 7d 0a 0a 73 74  ll (geo2);.}..st
76430 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 53  atic void.fnct_S
76440 6e 61 70 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nap (sqlite3_con
76450 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
76460 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
76470 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
76480 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
76490 6f 6e 3a 0a 2f 20 53 6e 61 70 28 42 4c 4f 42 65  on:./ Snap(BLOBe
764a0 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 31  ncoded geometry1
764b0 2c 20 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65  , BLOBencoded ge
764c0 6f 6d 65 74 72 79 32 2c 20 64 6f 75 62 6c 65 20  ometry2, double 
764d0 74 6f 6c 65 72 61 6e 63 65 29 0a 2f 0a 2f 20 52  tolerance)././ R
764e0 65 74 75 72 6e 73 20 61 20 6e 65 77 20 47 65 6f  eturns a new Geo
764f0 6d 65 74 72 79 20 63 6f 72 72 65 73 70 6f 6e 64  metry correspond
76500 69 6e 67 20 74 6f 20 67 65 6f 6d 31 20 73 6e 61  ing to geom1 sna
76510 70 70 65 64 20 74 6f 20 67 65 6f 6d 32 0a 2f 20  pped to geom2./ 
76520 61 6e 64 20 75 73 69 6e 67 20 74 68 65 20 67 69  and using the gi
76530 76 65 6e 20 74 6f 6c 65 72 61 6e 63 65 0a 2f 20  ven tolerance./ 
76540 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
76550 20 66 6f 72 20 69 6e 76 61 6c 69 64 20 61 72 67   for invalid arg
76560 75 6d 65 6e 74 73 20 28 6f 72 20 69 66 20 64 69  uments (or if di
76570 73 74 61 6e 63 65 20 69 73 20 5a 45 52 4f 29 0a  stance is ZERO).
76580 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
76590 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20  char *p_blob;.  
765a0 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20    int n_bytes;. 
765b0 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65     int int_value
765c0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 74 6f 6c  ;.    double tol
765d0 65 72 61 6e 63 65 3b 0a 20 20 20 20 67 61 69 61  erance;.    gaia
765e0 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 31  GeomCollPtr geo1
765f0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69   = NULL;.    gai
76600 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f  aGeomCollPtr geo
76610 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61  2 = NULL;.    ga
76620 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65  iaGeomCollPtr re
76630 73 75 6c 74 3b 0a 20 20 20 20 47 41 49 41 5f 55  sult;.    GAIA_U
76640 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
76650 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
76660 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
76670 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
76680 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
76690 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
766a0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
766b0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
766c0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
766d0 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
766e0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
766f0 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[1]) != SQLITE
76700 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
76710 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
76720 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
76730 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
76740 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69    }.    if (sqli
76750 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
76760 61 72 67 76 5b 32 5d 29 20 3d 3d 20 53 51 4c 49  argv[2]) == SQLI
76770 54 45 5f 46 4c 4f 41 54 29 0a 09 74 6f 6c 65 72  TE_FLOAT)..toler
76780 61 6e 63 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  ance = sqlite3_v
76790 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67  alue_double (arg
767a0 76 5b 32 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20  v[2]);.    else 
767b0 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
767c0 65 5f 74 79 70 65 20 28 61 72 67 76 5b 32 5d 29  e_type (argv[2])
767d0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
767e0 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  ER).      {..  i
767f0 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  nt_value = sqlit
76800 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
76810 67 76 5b 32 5d 29 3b 0a 09 20 20 74 6f 6c 65 72  gv[2]);..  toler
76820 61 6e 63 65 20 3d 20 69 6e 74 5f 76 61 6c 75 65  ance = int_value
76830 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c  ;.      }.    el
76840 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  se.      {..  sq
76850 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
76860 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
76870 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
76880 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28 75 6e      p_blob = (un
76890 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 73  signed char *) s
768a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
768b0 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  b (argv[0]);.   
768c0 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c 69 74   n_bytes = sqlit
768d0 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 28  e3_value_bytes (
768e0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 67 65  argv[0]);.    ge
768f0 6f 31 20 3d 20 67 61 69 61 46 72 6f 6d 53 70 61  o1 = gaiaFromSpa
76900 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20 28  tiaLiteBlobWkb (
76910 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
76920 3b 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  ;.    p_blob = (
76930 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
76940 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
76950 6c 6f 62 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20  lob (argv[1]);. 
76960 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
76970 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
76980 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20   (argv[1]);.    
76990 67 65 6f 32 20 3d 20 67 61 69 61 46 72 6f 6d 53  geo2 = gaiaFromS
769a0 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62  patiaLiteBlobWkb
769b0 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65   (p_blob, n_byte
769c0 73 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 31  s);.    if (geo1
769d0 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 67 65 6f 32   == NULL || geo2
769e0 20 3d 3d 20 4e 55 4c 4c 29 0a 09 73 71 6c 69 74   == NULL)..sqlit
769f0 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
76a00 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c  context);.    el
76a10 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65  se.      {..  re
76a20 73 75 6c 74 20 3d 20 67 61 69 61 53 6e 61 70 20  sult = gaiaSnap 
76a30 28 67 65 6f 31 2c 20 67 65 6f 32 2c 20 74 6f 6c  (geo1, geo2, tol
76a40 65 72 61 6e 63 65 29 3b 0a 09 20 20 69 66 20 28  erance);..  if (
76a50 72 65 73 75 6c 74 20 3d 3d 20 4e 55 4c 4c 29 0a  result == NULL).
76a60 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
76a70 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
76a80 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20  ext);..  else.. 
76a90 20 20 20 7b 0a 09 09 2f 2a 20 62 75 69 6c 64 73     {.../* builds
76aa0 20 74 68 65 20 42 4c 4f 42 20 67 65 6f 6d 65 74   the BLOB geomet
76ab0 72 79 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  ry to be returne
76ac0 64 20 2a 2f 0a 09 09 69 6e 74 20 6c 65 6e 3b 0a  d */...int len;.
76ad0 09 09 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  ..unsigned char 
76ae0 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  *p_result = NULL
76af0 3b 0a 09 09 72 65 73 75 6c 74 2d 3e 53 72 69 64  ;...result->Srid
76b00 20 3d 20 67 65 6f 31 2d 3e 53 72 69 64 3b 0a 09   = geo1->Srid;..
76b10 09 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74  .gaiaToSpatiaLit
76b20 65 42 6c 6f 62 57 6b 62 20 28 72 65 73 75 6c 74  eBlobWkb (result
76b30 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65  , &p_result, &le
76b40 6e 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  n);...sqlite3_re
76b50 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
76b60 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
76b70 6e 2c 20 66 72 65 65 29 3b 0a 09 09 67 61 69 61  n, free);...gaia
76b80 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65  FreeGeomColl (re
76b90 73 75 6c 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20  sult);..    }.  
76ba0 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72      }.    gaiaFr
76bb0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 31  eeGeomColl (geo1
76bc0 29 3b 0a 20 20 20 20 67 61 69 61 46 72 65 65 47  );.    gaiaFreeG
76bd0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 32 29 3b 0a  eomColl (geo2);.
76be0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
76bf0 6e 63 74 5f 4c 69 6e 65 4d 65 72 67 65 20 28 73  nct_LineMerge (s
76c00 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
76c10 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
76c20 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
76c30 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
76c40 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
76c50 4c 69 6e 65 4d 65 72 67 65 28 42 4c 4f 42 65 6e  LineMerge(BLOBen
76c60 63 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 29 0a  coded geometry).
76c70 2f 0a 2f 20 41 73 73 75 6d 69 6e 67 20 74 68 61  /./ Assuming tha
76c80 74 20 47 65 6f 6d 65 74 72 79 20 72 65 70 72 65  t Geometry repre
76c90 73 65 6e 74 73 20 61 20 73 65 74 20 6f 66 20 73  sents a set of s
76ca0 70 61 72 73 65 20 4c 69 6e 65 73 74 72 69 6e 67  parse Linestring
76cb0 73 2c 0a 2f 20 74 68 69 73 20 66 75 6e 63 74 69  s,./ this functi
76cc0 6f 6e 20 77 69 6c 6c 20 61 74 74 65 6d 70 74 20  on will attempt 
76cd0 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 20 61 20  to reassemble a 
76ce0 73 69 6e 67 6c 65 20 6c 69 6e 65 0a 2f 20 28 6f  single line./ (o
76cf0 72 20 61 20 73 65 74 20 6f 66 20 6c 69 6e 65 73  r a set of lines
76d00 29 0a 2f 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  )./ NULL is retu
76d10 72 6e 65 64 20 66 6f 72 20 69 6e 76 61 6c 69 64  rned for invalid
76d20 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 20 20   arguments.*/.  
76d30 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
76d40 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74  *p_blob;.    int
76d50 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61   n_bytes;.    ga
76d60 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
76d70 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61  o = NULL;.    ga
76d80 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 72 65  iaGeomCollPtr re
76d90 73 75 6c 74 3b 0a 20 20 20 20 47 41 49 41 5f 55  sult;.    GAIA_U
76da0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
76db0 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
76dc0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
76dd0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
76de0 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42  [0]) != SQLITE_B
76df0 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
76e00 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
76e10 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
76e20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
76e30 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20 28  }.    p_blob = (
76e40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
76e50 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
76e60 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20  lob (argv[0]);. 
76e70 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71 6c     n_bytes = sql
76e80 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
76e90 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
76ea0 67 65 6f 20 3d 20 67 61 69 61 46 72 6f 6d 53 70  geo = gaiaFromSp
76eb0 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b 62 20  atiaLiteBlobWkb 
76ec0 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73  (p_blob, n_bytes
76ed0 29 3b 0a 20 20 20 20 69 66 20 28 67 65 6f 20 3d  );.    if (geo =
76ee0 3d 20 4e 55 4c 4c 29 0a 09 73 71 6c 69 74 65 33  = NULL)..sqlite3
76ef0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
76f00 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73 65  ntext);.    else
76f10 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 65 73 75  .      {..  resu
76f20 6c 74 20 3d 20 67 61 69 61 4c 69 6e 65 4d 65 72  lt = gaiaLineMer
76f30 67 65 20 28 67 65 6f 29 3b 0a 09 20 20 69 66 20  ge (geo);..  if 
76f40 28 72 65 73 75 6c 74 20 3d 3d 20 4e 55 4c 4c 29  (result == NULL)
76f50 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
76f60 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
76f70 74 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09  text);..  else..
76f80 20 20 20 20 7b 0a 09 09 2f 2a 20 62 75 69 6c 64      {.../* build
76f90 73 20 74 68 65 20 42 4c 4f 42 20 67 65 6f 6d 65  s the BLOB geome
76fa0 74 72 79 20 74 6f 20 62 65 20 72 65 74 75 72 6e  try to be return
76fb0 65 64 20 2a 2f 0a 09 09 69 6e 74 20 6c 65 6e 3b  ed */...int len;
76fc0 0a 09 09 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ...unsigned char
76fd0 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c   *p_result = NUL
76fe0 4c 3b 0a 09 09 72 65 73 75 6c 74 2d 3e 53 72 69  L;...result->Sri
76ff0 64 20 3d 20 67 65 6f 2d 3e 53 72 69 64 3b 0a 09  d = geo->Srid;..
77000 09 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74  .gaiaToSpatiaLit
77010 65 42 6c 6f 62 57 6b 62 20 28 72 65 73 75 6c 74  eBlobWkb (result
77020 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c 65  , &p_result, &le
77030 6e 29 3b 0a 09 09 73 71 6c 69 74 65 33 5f 72 65  n);...sqlite3_re
77040 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65  sult_blob (conte
77050 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65  xt, p_result, le
77060 6e 2c 20 66 72 65 65 29 3b 0a 09 09 67 61 69 61  n, free);...gaia
77070 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65  FreeGeomColl (re
77080 73 75 6c 74 29 3b 0a 09 20 20 20 20 7d 0a 20 20  sult);..    }.  
77090 20 20 20 20 7d 0a 20 20 20 20 67 61 69 61 46 72      }.    gaiaFr
770a0 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29  eeGeomColl (geo)
770b0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
770c0 0a 66 6e 63 74 5f 55 6e 61 72 79 55 6e 69 6f 6e  .fnct_UnaryUnion
770d0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
770e0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
770f0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
77100 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
77110 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
77120 0a 2f 20 55 6e 61 72 79 55 6e 69 6f 6e 28 42 4c  ./ UnaryUnion(BL
77130 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d 65 74  OBencoded geomet
77140 72 79 29 0a 2f 0a 2f 20 65 78 61 63 74 6c 79 20  ry)././ exactly 
77150 6c 69 6b 65 20 55 6e 69 6f 6e 2c 20 62 75 74 20  like Union, but 
77160 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 43  using a single C
77170 6f 6c 6c 65 63 74 69 6f 6e 0a 2f 20 4e 55 4c 4c  ollection./ NULL
77180 20 69 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72   is returned for
77190 20 69 6e 76 61 6c 69 64 20 61 72 67 75 6d 65 6e   invalid argumen
771a0 74 73 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  ts.*/.    unsign
771b0 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
771c0 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
771d0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
771e0 6c 6c 50 74 72 20 67 65 6f 20 3d 20 4e 55 4c 4c  llPtr geo = NULL
771f0 3b 0a 20 20 20 20 67 61 69 61 47 65 6f 6d 43 6f  ;.    gaiaGeomCo
77200 6c 6c 50 74 72 20 72 65 73 75 6c 74 3b 0a 20 20  llPtr result;.  
77210 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
77220 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
77230 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
77240 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
77250 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
77260 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
77270 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
77280 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
77290 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
772a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
772b0 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
772c0 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
772d0 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
772e0 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
772f0 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
77300 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
77310 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61  ]);.    geo = ga
77320 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65  iaFromSpatiaLite
77330 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c  BlobWkb (p_blob,
77340 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69   n_bytes);.    i
77350 66 20 28 67 65 6f 20 3d 3d 20 4e 55 4c 4c 29 0a  f (geo == NULL).
77360 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
77370 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
77380 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
77390 0a 09 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69  ..  result = gai
773a0 61 55 6e 61 72 79 55 6e 69 6f 6e 20 28 67 65 6f  aUnaryUnion (geo
773b0 29 3b 0a 09 20 20 69 66 20 28 72 65 73 75 6c 74  );..  if (result
773c0 20 3d 3d 20 4e 55 4c 4c 29 0a 09 20 20 20 20 20   == NULL)..     
773d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
773e0 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
773f0 09 20 20 65 6c 73 65 0a 09 20 20 20 20 7b 0a 09  .  else..    {..
77400 09 2f 2a 20 62 75 69 6c 64 73 20 74 68 65 20 42  ./* builds the B
77410 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20 74 6f 20  LOB geometry to 
77420 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 09  be returned */..
77430 09 69 6e 74 20 6c 65 6e 3b 0a 09 09 75 6e 73 69  .int len;...unsi
77440 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73  gned char *p_res
77450 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 72 65  ult = NULL;...re
77460 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f  sult->Srid = geo
77470 2d 3e 53 72 69 64 3b 0a 09 09 67 61 69 61 54 6f  ->Srid;...gaiaTo
77480 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62 57 6b  SpatiaLiteBlobWk
77490 62 20 28 72 65 73 75 6c 74 2c 20 26 70 5f 72 65  b (result, &p_re
774a0 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a 09 09 73  sult, &len);...s
774b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
774c0 6f 62 20 28 63 6f 6e 74 65 78 74 2c 20 70 5f 72  ob (context, p_r
774d0 65 73 75 6c 74 2c 20 6c 65 6e 2c 20 66 72 65 65  esult, len, free
774e0 29 3b 0a 09 09 67 61 69 61 46 72 65 65 47 65 6f  );...gaiaFreeGeo
774f0 6d 43 6f 6c 6c 20 28 72 65 73 75 6c 74 29 3b 0a  mColl (result);.
77500 09 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20  .    }.      }. 
77510 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
77520 6f 6c 6c 20 28 67 65 6f 29 3b 0a 7d 0a 0a 73 74  oll (geo);.}..st
77530 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 4c  atic void.fnct_L
77540 69 6e 65 73 43 75 74 41 74 4e 6f 64 65 73 20 28  inesCutAtNodes (
77550 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
77560 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
77570 72 67 63 2c 0a 09 09 20 20 20 20 20 20 73 71 6c  rgc,...      sql
77580 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
77590 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
775a0 63 74 69 6f 6e 3a 0a 2f 20 4c 69 6e 65 73 43 75  ction:./ LinesCu
775b0 74 41 74 4e 6f 64 65 73 28 42 4c 4f 42 65 6e 63  tAtNodes(BLOBenc
775c0 6f 64 65 64 20 67 65 6f 6d 65 74 72 79 31 2c 20  oded geometry1, 
775d0 42 4c 4f 42 65 6e 63 6f 64 65 64 20 67 65 6f 6d  BLOBencoded geom
775e0 65 74 72 79 32 29 0a 2f 0a 2f 20 41 73 73 75 6d  etry2)././ Assum
775f0 69 6e 67 20 74 68 61 74 20 47 65 6f 6d 65 74 72  ing that Geometr
77600 79 2d 31 20 72 65 70 72 65 73 65 6e 74 73 20 61  y-1 represents a
77610 20 73 65 74 20 6f 66 20 61 72 62 69 74 72 61 79   set of arbitray
77620 20 4c 69 6e 65 73 74 72 69 6e 67 73 2c 0a 2f 20   Linestrings,./ 
77630 61 6e 64 20 74 68 61 74 20 47 65 6f 6d 65 74 72  and that Geometr
77640 79 2d 32 20 72 65 70 72 65 73 65 6e 74 73 20 6f  y-2 represents o
77650 66 20 61 72 62 69 74 72 61 72 79 20 50 6f 69 6e  f arbitrary Poin
77660 74 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ts, this functio
77670 6e 20 0a 2f 20 77 69 6c 6c 20 74 68 65 6e 20 61  n ./ will then a
77680 74 74 65 6d 70 74 20 74 6f 20 63 75 74 20 6c 69  ttempt to cut li
77690 6e 65 73 20 61 63 63 6f 72 64 69 6e 67 6c 79 20  nes accordingly 
776a0 74 6f 20 67 69 76 65 6e 20 6e 6f 64 65 73 2e 0a  to given nodes..
776b0 2f 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  / NULL is return
776c0 65 64 20 66 6f 72 20 69 6e 76 61 6c 69 64 20 61  ed for invalid a
776d0 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 20 20 20 20  rguments.*/.    
776e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
776f0 5f 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e  _blob;.    int n
77700 5f 62 79 74 65 73 3b 0a 20 20 20 20 67 61 69 61  _bytes;.    gaia
77710 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 6d  GeomCollPtr geom
77720 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61  1 = NULL;.    ga
77730 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
77740 6f 6d 32 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  om2 = NULL;.    
77750 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20  gaiaGeomCollPtr 
77760 72 65 73 75 6c 74 3b 0a 20 20 20 20 47 41 49 41  result;.    GAIA
77770 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
77780 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
77790 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
777a0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
777b0 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49 54 45  gv[0]) != SQLITE
777c0 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
777d0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
777e0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
777f0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
77800 20 20 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69    }.    if (sqli
77810 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
77820 61 72 67 76 5b 31 5d 29 20 21 3d 20 53 51 4c 49  argv[1]) != SQLI
77830 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
77840 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
77850 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
77860 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
77870 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
77880 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
77890 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
778a0 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
778b0 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
778c0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
778d0 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
778e0 20 20 20 20 67 65 6f 6d 31 20 3d 20 67 61 69 61      geom1 = gaia
778f0 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
77900 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
77910 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 70 5f 62  _bytes);.    p_b
77920 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  lob = (unsigned 
77930 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  char *) sqlite3_
77940 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76  value_blob (argv
77950 5b 31 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65  [1]);.    n_byte
77960 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s = sqlite3_valu
77970 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 31 5d  e_bytes (argv[1]
77980 29 3b 0a 20 20 20 20 67 65 6f 6d 32 20 3d 20 67  );.    geom2 = g
77990 61 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74  aiaFromSpatiaLit
779a0 65 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62  eBlobWkb (p_blob
779b0 2c 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20  , n_bytes);.    
779c0 69 66 20 28 67 65 6f 6d 31 20 3d 3d 20 4e 55 4c  if (geom1 == NUL
779d0 4c 20 7c 7c 20 67 65 6f 6d 32 20 3d 3d 20 4e 55  L || geom2 == NU
779e0 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  LL).      {..  i
779f0 66 20 28 67 65 6f 6d 31 29 0a 09 20 20 20 20 20  f (geom1)..     
77a00 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
77a10 6c 20 28 67 65 6f 6d 31 29 3b 0a 09 20 20 69 66  l (geom1);..  if
77a20 20 28 67 65 6f 6d 32 29 0a 09 20 20 20 20 20 20   (geom2)..      
77a30 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c  gaiaFreeGeomColl
77a40 20 28 67 65 6f 6d 32 29 3b 0a 09 20 20 73 71 6c   (geom2);..  sql
77a50 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
77a60 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
77a70 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
77a80 20 20 20 72 65 73 75 6c 74 20 3d 20 67 61 69 61     result = gaia
77a90 4c 69 6e 65 73 43 75 74 41 74 4e 6f 64 65 73 20  LinesCutAtNodes 
77aa0 28 67 65 6f 6d 31 2c 20 67 65 6f 6d 32 29 3b 0a  (geom1, geom2);.
77ab0 20 20 20 20 69 66 20 28 72 65 73 75 6c 74 20 3d      if (result =
77ac0 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a  = NULL).      {.
77ad0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
77ae0 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
77af0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c  ;.      }.    el
77b00 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a  se.      {..  /*
77b10 20 62 75 69 6c 64 73 20 74 68 65 20 42 4c 4f 42   builds the BLOB
77b20 20 67 65 6f 6d 65 74 72 79 20 74 6f 20 62 65 20   geometry to be 
77b30 72 65 74 75 72 6e 65 64 20 2a 2f 0a 09 20 20 69  returned */..  i
77b40 6e 74 20 6c 65 6e 3b 0a 09 20 20 75 6e 73 69 67  nt len;..  unsig
77b50 6e 65 64 20 63 68 61 72 20 2a 70 5f 72 65 73 75  ned char *p_resu
77b60 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 09 20 20 72 65  lt = NULL;..  re
77b70 73 75 6c 74 2d 3e 53 72 69 64 20 3d 20 67 65 6f  sult->Srid = geo
77b80 6d 31 2d 3e 53 72 69 64 3b 0a 09 20 20 67 61 69  m1->Srid;..  gai
77b90 61 54 6f 53 70 61 74 69 61 4c 69 74 65 42 6c 6f  aToSpatiaLiteBlo
77ba0 62 57 6b 62 20 28 72 65 73 75 6c 74 2c 20 26 70  bWkb (result, &p
77bb0 5f 72 65 73 75 6c 74 2c 20 26 6c 65 6e 29 3b 0a  _result, &len);.
77bc0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
77bd0 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
77be0 20 70 5f 72 65 73 75 6c 74 2c 20 6c 65 6e 2c 20   p_result, len, 
77bf0 66 72 65 65 29 3b 0a 09 20 20 67 61 69 61 46 72  free);..  gaiaFr
77c00 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 72 65 73 75  eeGeomColl (resu
77c10 6c 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lt);.      }.   
77c20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
77c30 6c 20 28 67 65 6f 6d 31 29 3b 0a 20 20 20 20 67  l (geom1);.    g
77c40 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
77c50 28 67 65 6f 6d 32 29 3b 0a 7d 0a 0a 73 74 61 74  (geom2);.}..stat
77c60 69 63 20 69 6e 74 0a 63 6d 70 5f 70 74 5f 63 6f  ic int.cmp_pt_co
77c70 6f 72 64 73 20 28 63 6f 6e 73 74 20 76 6f 69 64  ords (const void
77c80 20 2a 70 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *p1, const void
77c90 20 2a 70 32 29 0a 7b 0a 2f 2a 20 63 6f 6d 70 61   *p2).{./* compa
77ca0 72 65 73 20 74 77 6f 20 6e 6f 64 65 73 20 20 62  res two nodes  b
77cb0 79 20 49 44 20 5b 66 6f 72 20 51 53 4f 52 54 5d  y ID [for QSORT]
77cc0 20 2a 2f 0a 20 20 20 20 67 61 69 61 50 6f 69 6e   */.    gaiaPoin
77cd0 74 50 74 72 20 70 74 31 20 3d 20 2a 28 28 67 61  tPtr pt1 = *((ga
77ce0 69 61 50 6f 69 6e 74 50 74 72 20 2a 29 20 70 31  iaPointPtr *) p1
77cf0 29 3b 0a 20 20 20 20 67 61 69 61 50 6f 69 6e 74  );.    gaiaPoint
77d00 50 74 72 20 70 74 32 20 3d 20 2a 28 28 67 61 69  Ptr pt2 = *((gai
77d10 61 50 6f 69 6e 74 50 74 72 20 2a 29 20 70 32 29  aPointPtr *) p2)
77d20 3b 0a 20 20 20 20 69 66 20 28 70 74 31 2d 3e 58  ;.    if (pt1->X
77d30 20 3d 3d 20 70 74 32 2d 3e 58 20 26 26 20 70 74   == pt2->X && pt
77d40 31 2d 3e 59 20 3d 3d 20 70 74 32 2d 3e 59 20 26  1->Y == pt2->Y &
77d50 26 20 70 74 31 2d 3e 5a 20 3d 3d 20 70 74 32 2d  & pt1->Z == pt2-
77d60 3e 5a 29 0a 09 72 65 74 75 72 6e 20 30 3b 0a 20  >Z)..return 0;. 
77d70 20 20 20 69 66 20 28 70 74 31 2d 3e 58 20 3e 20     if (pt1->X > 
77d80 70 74 32 2d 3e 58 29 0a 09 72 65 74 75 72 6e 20  pt2->X)..return 
77d90 31 3b 0a 20 20 20 20 69 66 20 28 70 74 31 2d 3e  1;.    if (pt1->
77da0 58 20 3d 3d 20 70 74 32 2d 3e 58 20 26 26 20 70  X == pt2->X && p
77db0 74 31 2d 3e 59 20 3e 20 70 74 32 2d 3e 59 29 0a  t1->Y > pt2->Y).
77dc0 09 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 69  .return 1;.    i
77dd0 66 20 28 70 74 31 2d 3e 58 20 3d 3d 20 70 74 32  f (pt1->X == pt2
77de0 2d 3e 58 20 26 26 20 70 74 31 2d 3e 59 20 3d 3d  ->X && pt1->Y ==
77df0 20 70 74 32 2d 3e 59 20 26 26 20 70 74 31 2d 3e   pt2->Y && pt1->
77e00 5a 20 3e 20 70 74 32 2d 3e 5a 29 0a 09 72 65 74  Z > pt2->Z)..ret
77e10 75 72 6e 20 31 3b 0a 20 20 20 20 72 65 74 75 72  urn 1;.    retur
77e20 6e 20 2d 31 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n -1;.}..static 
77e30 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 0a  gaiaGeomCollPtr.
77e40 61 75 78 50 6f 6c 79 67 4e 6f 64 65 73 20 28 67  auxPolygNodes (g
77e50 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
77e60 65 6f 6d 29 0a 7b 0a 2f 2a 20 61 74 74 65 6d 70  eom).{./* attemp
77e70 74 69 6e 67 20 74 6f 20 69 64 65 6e 74 69 66 79  ting to identify
77e80 20 52 69 6e 67 2d 4e 6f 64 65 73 20 2a 2f 0a 20   Ring-Nodes */. 
77e90 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
77ea0 74 72 20 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c  tr result = NULL
77eb0 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79 67 6f  ;.    gaiaPolygo
77ec0 6e 50 74 72 20 70 67 3b 0a 20 20 20 20 67 61 69  nPtr pg;.    gai
77ed0 61 52 69 6e 67 50 74 72 20 72 6e 67 3b 0a 20 20  aRingPtr rng;.  
77ee0 20 20 67 61 69 61 50 6f 69 6e 74 50 74 72 20 70    gaiaPointPtr p
77ef0 74 3b 0a 20 20 20 20 67 61 69 61 50 6f 69 6e 74  t;.    gaiaPoint
77f00 50 74 72 20 70 72 65 76 5f 70 74 3b 0a 20 20 20  Ptr prev_pt;.   
77f10 20 67 61 69 61 50 6f 69 6e 74 50 74 72 20 2a 73   gaiaPointPtr *s
77f20 6f 72 74 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20 20  orted = NULL;.  
77f30 20 20 69 6e 74 20 63 6f 75 6e 74 20 3d 20 30 3b    int count = 0;
77f40 0a 20 20 20 20 69 6e 74 20 69 76 3b 0a 20 20 20  .    int iv;.   
77f50 20 69 6e 74 20 69 62 3b 0a 20 20 20 20 64 6f 75   int ib;.    dou
77f60 62 6c 65 20 78 3b 0a 20 20 20 20 64 6f 75 62 6c  ble x;.    doubl
77f70 65 20 79 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  e y;.    double 
77f80 7a 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d 3b  z;.    double m;
77f90 0a 0a 2f 2a 20 69 6e 73 65 72 74 69 6e 67 20 61  ../* inserting a
77fa0 6c 6c 20 50 6f 69 6e 74 73 20 69 6e 74 6f 20 61  ll Points into a
77fb0 20 44 79 6e 61 6d 69 63 20 4c 69 6e 65 20 2a 2f   Dynamic Line */
77fc0 0a 20 20 20 20 67 61 69 61 44 79 6e 61 6d 69 63  .    gaiaDynamic
77fd0 4c 69 6e 65 50 74 72 20 64 79 6e 20 3d 20 67 61  LinePtr dyn = ga
77fe0 69 61 41 6c 6c 6f 63 44 79 6e 61 6d 69 63 4c 69  iaAllocDynamicLi
77ff0 6e 65 20 28 29 3b 0a 20 20 20 20 70 67 20 3d 20  ne ();.    pg = 
78000 67 65 6f 6d 2d 3e 46 69 72 73 74 50 6f 6c 79 67  geom->FirstPolyg
78010 6f 6e 3b 0a 20 20 20 20 77 68 69 6c 65 20 28 70  on;.    while (p
78020 67 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 72 6e  g).      {..  rn
78030 67 20 3d 20 70 67 2d 3e 45 78 74 65 72 69 6f 72  g = pg->Exterior
78040 3b 0a 09 20 20 2f 2a 20 43 41 56 45 41 54 3a 20  ;..  /* CAVEAT: 
78050 66 69 72 73 74 20 70 6f 69 6e 74 20 6e 65 65 64  first point need
78060 73 20 74 6f 20 62 65 20 73 6b 69 70 70 65 64 20  s to be skipped 
78070 28 63 6c 6f 73 65 64 20 72 69 6e 67 29 20 2a 2f  (closed ring) */
78080 0a 09 20 20 66 6f 72 20 28 69 76 20 3d 20 31 3b  ..  for (iv = 1;
78090 20 69 76 20 3c 20 72 6e 67 2d 3e 50 6f 69 6e 74   iv < rng->Point
780a0 73 3b 20 69 76 2b 2b 29 0a 09 20 20 20 20 7b 0a  s; iv++)..    {.
780b0 09 09 2f 2a 20 65 78 74 65 72 69 6f 72 20 72 69  ../* exterior ri
780c0 6e 67 20 2a 2f 0a 09 09 69 66 20 28 67 65 6f 6d  ng */...if (geom
780d0 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c  ->DimensionModel
780e0 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29 0a 09   == GAIA_XY_Z)..
780f0 09 20 20 7b 0a 09 09 20 20 20 20 20 20 67 61 69  .  {...      gai
78100 61 47 65 74 50 6f 69 6e 74 58 59 5a 20 28 72 6e  aGetPointXYZ (rn
78110 67 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 26  g->Coords, iv, &
78120 78 2c 20 26 79 2c 20 26 7a 29 3b 0a 09 09 20 20  x, &y, &z);...  
78130 20 20 20 20 67 61 69 61 41 70 70 65 6e 64 50 6f      gaiaAppendPo
78140 69 6e 74 5a 54 6f 44 79 6e 61 6d 69 63 4c 69 6e  intZToDynamicLin
78150 65 20 28 64 79 6e 2c 20 78 2c 20 79 2c 20 7a 29  e (dyn, x, y, z)
78160 3b 0a 09 09 20 20 7d 0a 09 09 65 6c 73 65 20 69  ;...  }...else i
78170 66 20 28 67 65 6f 6d 2d 3e 44 69 6d 65 6e 73 69  f (geom->Dimensi
78180 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f  onModel == GAIA_
78190 58 59 5f 4d 29 0a 09 09 20 20 7b 0a 09 09 20 20  XY_M)...  {...  
781a0 20 20 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74      gaiaGetPoint
781b0 58 59 4d 20 28 72 6e 67 2d 3e 43 6f 6f 72 64 73  XYM (rng->Coords
781c0 2c 20 69 76 2c 20 26 78 2c 20 26 79 2c 20 26 6d  , iv, &x, &y, &m
781d0 29 3b 0a 09 09 20 20 20 20 20 20 67 61 69 61 41  );...      gaiaA
781e0 70 70 65 6e 64 50 6f 69 6e 74 4d 54 6f 44 79 6e  ppendPointMToDyn
781f0 61 6d 69 63 4c 69 6e 65 20 28 64 79 6e 2c 20 78  amicLine (dyn, x
78200 2c 20 79 2c 20 6d 29 3b 0a 09 09 20 20 7d 0a 09  , y, m);...  }..
78210 09 65 6c 73 65 20 69 66 20 28 67 65 6f 6d 2d 3e  .else if (geom->
78220 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
78230 3d 20 47 41 49 41 5f 58 59 5f 5a 5f 4d 29 0a 09  = GAIA_XY_Z_M)..
78240 09 20 20 7b 0a 09 09 20 20 20 20 20 20 67 61 69  .  {...      gai
78250 61 47 65 74 50 6f 69 6e 74 58 59 5a 4d 20 28 72  aGetPointXYZM (r
78260 6e 67 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20  ng->Coords, iv, 
78270 26 78 2c 20 26 79 2c 20 26 7a 2c 20 26 6d 29 3b  &x, &y, &z, &m);
78280 0a 09 09 20 20 20 20 20 20 67 61 69 61 41 70 70  ...      gaiaApp
78290 65 6e 64 50 6f 69 6e 74 5a 4d 54 6f 44 79 6e 61  endPointZMToDyna
782a0 6d 69 63 4c 69 6e 65 20 28 64 79 6e 2c 20 78 2c  micLine (dyn, x,
782b0 20 79 2c 20 7a 2c 20 6d 29 3b 0a 09 09 20 20 7d   y, z, m);...  }
782c0 0a 09 09 65 6c 73 65 0a 09 09 20 20 7b 0a 09 09  ...else...  {...
782d0 20 20 20 20 20 20 67 61 69 61 47 65 74 50 6f 69        gaiaGetPoi
782e0 6e 74 20 28 72 6e 67 2d 3e 43 6f 6f 72 64 73 2c  nt (rng->Coords,
782f0 20 69 76 2c 20 26 78 2c 20 26 79 29 3b 0a 09 09   iv, &x, &y);...
78300 20 20 20 20 20 20 67 61 69 61 41 70 70 65 6e 64        gaiaAppend
78310 50 6f 69 6e 74 54 6f 44 79 6e 61 6d 69 63 4c 69  PointToDynamicLi
78320 6e 65 20 28 64 79 6e 2c 20 78 2c 20 79 29 3b 0a  ne (dyn, x, y);.
78330 09 09 20 20 7d 0a 09 20 20 20 20 7d 0a 0a 09 20  ..  }..    }... 
78340 20 66 6f 72 20 28 69 62 20 3d 20 30 3b 20 69 62   for (ib = 0; ib
78350 20 3c 20 70 67 2d 3e 4e 75 6d 49 6e 74 65 72 69   < pg->NumInteri
78360 6f 72 73 3b 20 69 62 2b 2b 29 0a 09 20 20 20 20  ors; ib++)..    
78370 7b 0a 09 09 72 6e 67 20 3d 20 70 67 2d 3e 49 6e  {...rng = pg->In
78380 74 65 72 69 6f 72 73 20 2b 20 69 62 3b 0a 09 09  teriors + ib;...
78390 2f 2a 20 43 41 56 45 41 54 3a 20 66 69 72 73 74  /* CAVEAT: first
783a0 20 70 6f 69 6e 74 20 6e 65 65 64 73 20 74 6f 20   point needs to 
783b0 62 65 20 73 6b 69 70 70 65 64 20 28 63 6c 6f 73  be skipped (clos
783c0 65 64 20 72 69 6e 67 29 20 2a 2f 0a 09 09 66 6f  ed ring) */...fo
783d0 72 20 28 69 76 20 3d 20 31 3b 20 69 76 20 3c 20  r (iv = 1; iv < 
783e0 72 6e 67 2d 3e 50 6f 69 6e 74 73 3b 20 69 76 2b  rng->Points; iv+
783f0 2b 29 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20  +)...  {...     
78400 20 2f 2a 20 69 6e 74 65 72 69 6f 72 20 72 69 6e   /* interior rin
78410 67 20 2a 2f 0a 09 09 20 20 20 20 20 20 69 66 20  g */...      if 
78420 28 67 65 6f 6d 2d 3e 44 69 6d 65 6e 73 69 6f 6e  (geom->Dimension
78430 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59  Model == GAIA_XY
78440 5f 5a 29 0a 09 09 09 7b 0a 09 09 09 20 20 20 20  _Z)....{....    
78450 67 61 69 61 47 65 74 50 6f 69 6e 74 58 59 5a 20  gaiaGetPointXYZ 
78460 28 72 6e 67 2d 3e 43 6f 6f 72 64 73 2c 20 69 76  (rng->Coords, iv
78470 2c 20 26 78 2c 20 26 79 2c 20 26 7a 29 3b 0a 09  , &x, &y, &z);..
78480 09 09 20 20 20 20 67 61 69 61 41 70 70 65 6e 64  ..    gaiaAppend
78490 50 6f 69 6e 74 5a 54 6f 44 79 6e 61 6d 69 63 4c  PointZToDynamicL
784a0 69 6e 65 20 28 64 79 6e 2c 20 78 2c 20 79 2c 20  ine (dyn, x, y, 
784b0 7a 29 3b 0a 09 09 09 7d 0a 09 09 20 20 20 20 20  z);....}...     
784c0 20 65 6c 73 65 20 69 66 20 28 67 65 6f 6d 2d 3e   else if (geom->
784d0 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 20 3d  DimensionModel =
784e0 3d 20 47 41 49 41 5f 58 59 5f 4d 29 0a 09 09 09  = GAIA_XY_M)....
784f0 7b 0a 09 09 09 20 20 20 20 67 61 69 61 47 65 74  {....    gaiaGet
78500 50 6f 69 6e 74 58 59 4d 20 28 72 6e 67 2d 3e 43  PointXYM (rng->C
78510 6f 6f 72 64 73 2c 20 69 76 2c 20 26 78 2c 20 26  oords, iv, &x, &
78520 79 2c 20 26 6d 29 3b 0a 09 09 09 20 20 20 20 67  y, &m);....    g
78530 61 69 61 41 70 70 65 6e 64 50 6f 69 6e 74 4d 54  aiaAppendPointMT
78540 6f 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 64 79  oDynamicLine (dy
78550 6e 2c 20 78 2c 20 79 2c 20 6d 29 3b 0a 09 09 09  n, x, y, m);....
78560 7d 0a 09 09 20 20 20 20 20 20 65 6c 73 65 20 69  }...      else i
78570 66 20 28 67 65 6f 6d 2d 3e 44 69 6d 65 6e 73 69  f (geom->Dimensi
78580 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f  onModel == GAIA_
78590 58 59 5f 5a 5f 4d 29 0a 09 09 09 7b 0a 09 09 09  XY_Z_M)....{....
785a0 20 20 20 20 67 61 69 61 47 65 74 50 6f 69 6e 74      gaiaGetPoint
785b0 58 59 5a 4d 20 28 72 6e 67 2d 3e 43 6f 6f 72 64  XYZM (rng->Coord
785c0 73 2c 20 69 76 2c 20 26 78 2c 20 26 79 2c 20 26  s, iv, &x, &y, &
785d0 7a 2c 20 26 6d 29 3b 0a 09 09 09 20 20 20 20 67  z, &m);....    g
785e0 61 69 61 41 70 70 65 6e 64 50 6f 69 6e 74 5a 4d  aiaAppendPointZM
785f0 54 6f 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 64  ToDynamicLine (d
78600 79 6e 2c 20 78 2c 20 79 2c 20 7a 2c 20 6d 29 3b  yn, x, y, z, m);
78610 0a 09 09 09 7d 0a 09 09 20 20 20 20 20 20 65 6c  ....}...      el
78620 73 65 0a 09 09 09 7b 0a 09 09 09 20 20 20 20 67  se....{....    g
78630 61 69 61 47 65 74 50 6f 69 6e 74 20 28 72 6e 67  aiaGetPoint (rng
78640 2d 3e 43 6f 6f 72 64 73 2c 20 69 76 2c 20 26 78  ->Coords, iv, &x
78650 2c 20 26 79 29 3b 0a 09 09 09 20 20 20 20 67 61  , &y);....    ga
78660 69 61 41 70 70 65 6e 64 50 6f 69 6e 74 54 6f 44  iaAppendPointToD
78670 79 6e 61 6d 69 63 4c 69 6e 65 20 28 64 79 6e 2c  ynamicLine (dyn,
78680 20 78 2c 20 79 29 3b 0a 09 09 09 7d 0a 09 09 20   x, y);....}... 
78690 20 7d 0a 09 20 20 20 20 7d 0a 09 20 20 70 67 20   }..    }..  pg 
786a0 3d 20 70 67 2d 3e 4e 65 78 74 3b 0a 20 20 20 20  = pg->Next;.    
786b0 20 20 7d 0a 0a 20 20 20 20 70 74 20 3d 20 64 79    }..    pt = dy
786c0 6e 2d 3e 46 69 72 73 74 3b 0a 20 20 20 20 77 68  n->First;.    wh
786d0 69 6c 65 20 28 70 74 29 0a 20 20 20 20 20 20 7b  ile (pt).      {
786e0 0a 09 20 20 2f 2a 20 63 6f 75 6e 74 69 6e 67 20  ..  /* counting 
786f0 68 6f 77 20 6d 61 6e 79 20 70 6f 69 6e 74 73 20  how many points 
78700 2a 2f 0a 09 20 20 63 6f 75 6e 74 2b 2b 3b 0a 09  */..  count++;..
78710 20 20 70 74 20 3d 20 70 74 2d 3e 4e 65 78 74 3b    pt = pt->Next;
78720 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
78730 28 63 6f 75 6e 74 20 3d 3d 20 30 29 0a 20 20 20  (count == 0).   
78740 20 20 20 7b 0a 09 20 20 67 61 69 61 46 72 65 65     {..  gaiaFree
78750 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28 64 79 6e  DynamicLine (dyn
78760 29 3b 0a 09 20 20 72 65 74 75 72 6e 20 4e 55 4c  );..  return NUL
78770 4c 3b 0a 20 20 20 20 20 20 7d 0a 0a 2f 2a 20 61  L;.      }../* a
78780 6c 6c 6f 63 61 74 69 6e 67 20 61 6e 64 20 69 6e  llocating and in
78790 69 74 69 61 6c 69 7a 69 6e 67 20 61 6e 20 61 72  itializing an ar
787a0 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ray of pointers 
787b0 2a 2f 0a 20 20 20 20 73 6f 72 74 65 64 20 3d 20  */.    sorted = 
787c0 6d 61 6c 6c 6f 63 20 28 73 69 7a 65 6f 66 20 28  malloc (sizeof (
787d0 67 61 69 61 50 6f 69 6e 74 50 74 72 29 20 2a 20  gaiaPointPtr) * 
787e0 63 6f 75 6e 74 29 3b 0a 20 20 20 20 69 76 20 3d  count);.    iv =
787f0 20 30 3b 0a 20 20 20 20 70 74 20 3d 20 64 79 6e   0;.    pt = dyn
78800 2d 3e 46 69 72 73 74 3b 0a 20 20 20 20 77 68 69  ->First;.    whi
78810 6c 65 20 28 70 74 29 0a 20 20 20 20 20 20 7b 0a  le (pt).      {.
78820 09 20 20 2a 28 73 6f 72 74 65 64 20 2b 20 69 76  .  *(sorted + iv
78830 2b 2b 29 20 3d 20 70 74 3b 0a 09 20 20 70 74 20  ++) = pt;..  pt 
78840 3d 20 70 74 2d 3e 4e 65 78 74 3b 0a 20 20 20 20  = pt->Next;.    
78850 20 20 7d 0a 0a 2f 2a 20 73 6f 72 74 69 6e 67 20    }../* sorting 
78860 70 6f 69 6e 74 73 20 62 79 20 63 6f 6f 72 64 73  points by coords
78870 20 2a 2f 0a 20 20 20 20 71 73 6f 72 74 20 28 73   */.    qsort (s
78880 6f 72 74 65 64 2c 20 63 6f 75 6e 74 2c 20 73 69  orted, count, si
78890 7a 65 6f 66 20 28 67 61 69 61 50 6f 69 6e 74 50  zeof (gaiaPointP
788a0 74 72 29 2c 20 63 6d 70 5f 70 74 5f 63 6f 6f 72  tr), cmp_pt_coor
788b0 64 73 29 3b 0a 0a 20 20 20 20 69 66 20 28 67 65  ds);..    if (ge
788c0 6f 6d 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64  om->DimensionMod
788d0 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59 5f 5a 29  el == GAIA_XY_Z)
788e0 0a 09 72 65 73 75 6c 74 20 3d 20 67 61 69 61 41  ..result = gaiaA
788f0 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58 59 5a 20  llocGeomCollXYZ 
78900 28 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20  ();.    else if 
78910 28 67 65 6f 6d 2d 3e 44 69 6d 65 6e 73 69 6f 6e  (geom->Dimension
78920 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59  Model == GAIA_XY
78930 5f 4d 29 0a 09 72 65 73 75 6c 74 20 3d 20 67 61  _M)..result = ga
78940 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 58  iaAllocGeomCollX
78950 59 4d 20 28 29 3b 0a 20 20 20 20 65 6c 73 65 20  YM ();.    else 
78960 69 66 20 28 67 65 6f 6d 2d 3e 44 69 6d 65 6e 73  if (geom->Dimens
78970 69 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41  ionModel == GAIA
78980 5f 58 59 5f 5a 5f 4d 29 0a 09 72 65 73 75 6c 74  _XY_Z_M)..result
78990 20 3d 20 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d   = gaiaAllocGeom
789a0 43 6f 6c 6c 58 59 5a 4d 20 28 29 3b 0a 20 20 20  CollXYZM ();.   
789b0 20 65 6c 73 65 0a 09 72 65 73 75 6c 74 20 3d 20   else..result = 
789c0 67 61 69 61 41 6c 6c 6f 63 47 65 6f 6d 43 6f 6c  gaiaAllocGeomCol
789d0 6c 20 28 29 3b 0a 20 20 20 20 72 65 73 75 6c 74  l ();.    result
789e0 2d 3e 53 72 69 64 20 3d 20 67 65 6f 6d 2d 3e 53  ->Srid = geom->S
789f0 72 69 64 3b 0a 0a 2f 2a 20 69 64 65 6e 74 69 66  rid;../* identif
78a00 79 69 6e 67 20 6e 6f 64 65 73 20 2a 2f 0a 20 20  ying nodes */.  
78a10 20 20 70 72 65 76 5f 70 74 20 3d 20 4e 55 4c 4c    prev_pt = NULL
78a20 3b 0a 20 20 20 20 66 6f 72 20 28 69 76 20 3d 20  ;.    for (iv = 
78a30 30 3b 20 69 76 20 3c 20 63 6f 75 6e 74 3b 20 69  0; iv < count; i
78a40 76 2b 2b 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  v++).      {..  
78a50 70 74 20 3d 20 2a 28 73 6f 72 74 65 64 20 2b 20  pt = *(sorted + 
78a60 69 76 29 3b 0a 09 20 20 69 66 20 28 70 72 65 76  iv);..  if (prev
78a70 5f 70 74 20 21 3d 20 4e 55 4c 4c 29 0a 09 20 20  _pt != NULL)..  
78a80 20 20 7b 0a 09 09 69 66 20 28 70 72 65 76 5f 70    {...if (prev_p
78a90 74 2d 3e 58 20 3d 3d 20 70 74 2d 3e 58 20 26 26  t->X == pt->X &&
78aa0 20 70 72 65 76 5f 70 74 2d 3e 59 20 3d 3d 20 70   prev_pt->Y == p
78ab0 74 2d 3e 59 0a 09 09 20 20 20 20 26 26 20 70 72  t->Y...    && pr
78ac0 65 76 5f 70 74 2d 3e 5a 20 3d 3d 20 70 74 2d 3e  ev_pt->Z == pt->
78ad0 5a 29 0a 09 09 20 20 7b 0a 09 09 20 20 20 20 20  Z)...  {...     
78ae0 20 69 66 20 28 72 65 73 75 6c 74 2d 3e 4c 61 73   if (result->Las
78af0 74 50 6f 69 6e 74 20 21 3d 20 4e 55 4c 4c 29 0a  tPoint != NULL).
78b00 09 09 09 7b 0a 09 09 09 20 20 20 20 69 66 20 28  ...{....    if (
78b10 72 65 73 75 6c 74 2d 3e 4c 61 73 74 50 6f 69 6e  result->LastPoin
78b20 74 2d 3e 58 20 3d 3d 20 70 74 2d 3e 58 0a 09 09  t->X == pt->X...
78b30 09 09 26 26 20 72 65 73 75 6c 74 2d 3e 4c 61 73  ..&& result->Las
78b40 74 50 6f 69 6e 74 2d 3e 59 20 3d 3d 20 70 74 2d  tPoint->Y == pt-
78b50 3e 59 0a 09 09 09 09 26 26 20 72 65 73 75 6c 74  >Y.....&& result
78b60 2d 3e 4c 61 73 74 50 6f 69 6e 74 2d 3e 5a 20 3d  ->LastPoint->Z =
78b70 3d 20 70 74 2d 3e 5a 29 0a 09 09 09 09 63 6f 6e  = pt->Z).....con
78b80 74 69 6e 75 65 3b 0a 09 09 09 7d 0a 09 09 20 20  tinue;....}...  
78b90 20 20 20 20 2f 2a 20 4e 6f 64 65 20 66 6f 75 6e      /* Node foun
78ba0 64 20 2a 2f 0a 09 09 20 20 20 20 20 20 69 66 20  d */...      if 
78bb0 28 72 65 73 75 6c 74 2d 3e 44 69 6d 65 6e 73 69  (result->Dimensi
78bc0 6f 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f  onModel == GAIA_
78bd0 58 59 5f 5a 29 0a 09 09 09 20 20 67 61 69 61 41  XY_Z)....  gaiaA
78be0 64 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c  ddPointToGeomCol
78bf0 6c 58 59 5a 20 28 72 65 73 75 6c 74 2c 20 70 74  lXYZ (result, pt
78c00 2d 3e 58 2c 20 70 74 2d 3e 59 2c 0a 09 09 09 09  ->X, pt->Y,.....
78c10 09 09 20 20 20 20 20 70 74 2d 3e 5a 29 3b 0a 09  ..     pt->Z);..
78c20 09 20 20 20 20 20 20 65 6c 73 65 20 69 66 20 28  .      else if (
78c30 72 65 73 75 6c 74 2d 3e 44 69 6d 65 6e 73 69 6f  result->Dimensio
78c40 6e 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58  nModel == GAIA_X
78c50 59 5f 4d 29 0a 09 09 09 20 20 67 61 69 61 41 64  Y_M)....  gaiaAd
78c60 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c  dPointToGeomColl
78c70 58 59 4d 20 28 72 65 73 75 6c 74 2c 20 70 74 2d  XYM (result, pt-
78c80 3e 58 2c 20 70 74 2d 3e 59 2c 0a 09 09 09 09 09  >X, pt->Y,......
78c90 09 20 20 20 20 20 70 74 2d 3e 4d 29 3b 0a 09 09  .     pt->M);...
78ca0 20 20 20 20 20 20 65 6c 73 65 20 69 66 20 28 72        else if (r
78cb0 65 73 75 6c 74 2d 3e 44 69 6d 65 6e 73 69 6f 6e  esult->Dimension
78cc0 4d 6f 64 65 6c 20 3d 3d 20 47 41 49 41 5f 58 59  Model == GAIA_XY
78cd0 5f 5a 5f 4d 29 0a 09 09 09 20 20 67 61 69 61 41  _Z_M)....  gaiaA
78ce0 64 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c  ddPointToGeomCol
78cf0 6c 58 59 5a 4d 20 28 72 65 73 75 6c 74 2c 20 70  lXYZM (result, p
78d00 74 2d 3e 58 2c 0a 09 09 09 09 09 09 20 20 20 20  t->X,.......    
78d10 20 20 70 74 2d 3e 59 2c 20 70 74 2d 3e 5a 2c 20    pt->Y, pt->Z, 
78d20 70 74 2d 3e 4d 29 3b 0a 09 09 20 20 20 20 20 20  pt->M);...      
78d30 65 6c 73 65 0a 09 09 09 20 20 67 61 69 61 41 64  else....  gaiaAd
78d40 64 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c  dPointToGeomColl
78d50 20 28 72 65 73 75 6c 74 2c 20 70 74 2d 3e 58 2c   (result, pt->X,
78d60 20 70 74 2d 3e 59 29 3b 0a 09 09 20 20 7d 0a 09   pt->Y);...  }..
78d70 20 20 20 20 7d 0a 09 20 20 70 72 65 76 5f 70 74      }..  prev_pt
78d80 20 3d 20 70 74 3b 0a 20 20 20 20 20 20 7d 0a 0a   = pt;.      }..
78d90 20 20 20 20 69 66 20 28 72 65 73 75 6c 74 2d 3e      if (result->
78da0 46 69 72 73 74 50 6f 69 6e 74 20 3d 3d 20 4e 55  FirstPoint == NU
78db0 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 67  LL).      {..  g
78dc0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
78dd0 28 72 65 73 75 6c 74 29 3b 0a 09 20 20 72 65 73  (result);..  res
78de0 75 6c 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ult = NULL;.    
78df0 20 20 7d 0a 20 20 20 20 66 72 65 65 20 28 73 6f    }.    free (so
78e00 72 74 65 64 29 3b 0a 20 20 20 20 67 61 69 61 46  rted);.    gaiaF
78e10 72 65 65 44 79 6e 61 6d 69 63 4c 69 6e 65 20 28  reeDynamicLine (
78e20 64 79 6e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  dyn);.    return
78e30 20 72 65 73 75 6c 74 3b 0a 7d 0a 0a 73 74 61 74   result;.}..stat
78e40 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 52 69 6e  ic void.fnct_Rin
78e50 67 73 43 75 74 41 74 4e 6f 64 65 73 20 28 73 71  gsCutAtNodes (sq
78e60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
78e70 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
78e80 63 2c 0a 09 09 20 20 20 20 20 20 73 71 6c 69 74  c,...      sqlit
78e90 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
78ea0 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74  ).{./* SQL funct
78eb0 69 6f 6e 3a 0a 2f 20 52 69 6e 67 73 43 75 74 41  ion:./ RingsCutA
78ec0 74 4e 6f 64 65 73 28 42 4c 4f 42 65 6e 63 6f 64  tNodes(BLOBencod
78ed0 65 64 20 67 65 6f 6d 65 74 72 79 29 0a 2f 0a 2f  ed geometry)././
78ee0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   This function w
78ef0 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 72  ill attempt to r
78f00 65 74 75 72 6e 20 61 20 63 6f 6c 6c 65 63 74 69  eturn a collecti
78f10 6f 6e 20 6f 66 20 6c 69 6e 65 73 0a 2f 20 72 65  on of lines./ re
78f20 70 72 65 73 65 6e 74 69 6e 67 20 50 6f 6c 79 67  presenting Polyg
78f30 6f 6e 2f 52 69 6e 67 73 3a 20 74 68 65 20 69 6e  on/Rings: the in
78f40 70 75 74 20 67 65 6f 6d 65 74 72 79 20 69 73 20  put geometry is 
78f50 65 78 70 65 63 74 65 64 0a 2f 20 74 6f 20 62 65  expected./ to be
78f60 20 61 20 50 6f 6c 79 67 6f 6e 20 6f 72 20 4d 75   a Polygon or Mu
78f70 6c 74 69 50 6f 6c 79 67 6f 6e 2e 0a 2f 20 45 61  ltiPolygon../ Ea
78f80 63 68 20 52 69 6e 67 20 77 69 6c 6c 20 62 65 20  ch Ring will be 
78f90 63 75 74 20 61 63 63 6f 72 64 69 6e 67 6c 79 20  cut accordingly 
78fa0 74 6f 20 61 6e 79 20 69 64 65 6e 74 69 66 69 65  to any identifie
78fb0 64 20 22 6e 6f 64 65 22 0a 2f 20 69 2e 65 2e 20  d "node"./ i.e. 
78fc0 73 65 6c 66 2d 69 6e 74 65 72 73 65 63 74 69 6f  self-intersectio
78fd0 6e 73 20 6f 72 20 69 6e 74 65 72 73 65 63 74 69  ns or intersecti
78fe0 6f 6e 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20  ons between two 
78ff0 52 69 6e 67 73 2e 0a 2f 0a 2f 20 4e 55 4c 4c 20  Rings.././ NULL 
79000 69 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20  is returned for 
79010 69 6e 76 61 6c 69 64 20 61 72 67 75 6d 65 6e 74  invalid argument
79020 73 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 70 74 73  s.*/.    int pts
79030 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6c 6e   = 0;.    int ln
79040 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 70  s = 0;.    int p
79050 67 73 20 3d 20 30 3b 0a 20 20 20 20 75 6e 73 69  gs = 0;.    unsi
79060 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f  gned char *p_blo
79070 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74  b;.    int n_byt
79080 65 73 3b 0a 20 20 20 20 67 61 69 61 50 6f 69 6e  es;.    gaiaPoin
79090 74 50 74 72 20 70 74 3b 0a 20 20 20 20 67 61 69  tPtr pt;.    gai
790a0 61 4c 69 6e 65 73 74 72 69 6e 67 50 74 72 20 6c  aLinestringPtr l
790b0 6e 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79 67  n;.    gaiaPolyg
790c0 6f 6e 50 74 72 20 70 67 3b 0a 20 20 20 20 67 61  onPtr pg;.    ga
790d0 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65  iaGeomCollPtr ge
790e0 6f 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  om = NULL;.    g
790f0 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
79100 65 6f 6d 31 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  eom1 = NULL;.   
79110 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
79120 20 67 65 6f 6d 32 20 3d 20 4e 55 4c 4c 3b 0a 20   geom2 = NULL;. 
79130 20 20 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50     gaiaGeomCollP
79140 74 72 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 47  tr result;.    G
79150 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
79160 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
79170 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
79180 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
79190 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
791a0 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
791b0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
791c0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
791d0 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
791e0 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f       }.    p_blo
791f0 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  b = (unsigned ch
79200 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61  ar *) sqlite3_va
79210 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30  lue_blob (argv[0
79220 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20  ]);.    n_bytes 
79230 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
79240 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b  bytes (argv[0]);
79250 0a 20 20 20 20 67 65 6f 6d 20 3d 20 67 61 69 61  .    geom = gaia
79260 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c  FromSpatiaLiteBl
79270 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e  obWkb (p_blob, n
79280 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20  _bytes);.    if 
79290 28 67 65 6f 6d 20 3d 3d 20 4e 55 4c 4c 29 0a 20  (geom == NULL). 
792a0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
792b0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
792c0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
792d0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 2f 2a 20  rn;.      }../* 
792e0 63 68 65 63 6b 69 6e 67 20 69 66 20 47 65 6f 6d  checking if Geom
792f0 65 74 72 79 20 69 73 20 61 20 50 6f 6c 79 67 6f  etry is a Polygo
79300 6e 20 6f 72 20 4d 75 6c 74 69 50 6f 6c 79 68 6f  n or MultiPolyho
79310 6e 20 2a 2f 0a 20 20 20 20 70 74 20 3d 20 67 65  n */.    pt = ge
79320 6f 6d 2d 3e 46 69 72 73 74 50 6f 69 6e 74 3b 0a  om->FirstPoint;.
79330 20 20 20 20 77 68 69 6c 65 20 28 70 74 29 0a 20      while (pt). 
79340 20 20 20 20 20 7b 0a 09 20 20 70 74 73 2b 2b 3b       {..  pts++;
79350 0a 09 20 20 70 74 20 3d 20 70 74 2d 3e 4e 65 78  ..  pt = pt->Nex
79360 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 6c  t;.      }.    l
79370 6e 20 3d 20 67 65 6f 6d 2d 3e 46 69 72 73 74 4c  n = geom->FirstL
79380 69 6e 65 73 74 72 69 6e 67 3b 0a 20 20 20 20 77  inestring;.    w
79390 68 69 6c 65 20 28 6c 6e 29 0a 20 20 20 20 20 20  hile (ln).      
793a0 7b 0a 09 20 20 6c 6e 73 2b 2b 3b 0a 09 20 20 6c  {..  lns++;..  l
793b0 6e 20 3d 20 6c 6e 2d 3e 4e 65 78 74 3b 0a 20 20  n = ln->Next;.  
793c0 20 20 20 20 7d 0a 20 20 20 20 70 67 20 3d 20 67      }.    pg = g
793d0 65 6f 6d 2d 3e 46 69 72 73 74 50 6f 6c 79 67 6f  eom->FirstPolygo
793e0 6e 3b 0a 20 20 20 20 77 68 69 6c 65 20 28 70 67  n;.    while (pg
793f0 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 70 67 73  ).      {..  pgs
79400 2b 2b 3b 0a 09 20 20 70 67 20 3d 20 70 67 2d 3e  ++;..  pg = pg->
79410 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
79420 20 20 69 66 20 28 70 74 73 20 3e 20 30 20 7c 7c    if (pts > 0 ||
79430 20 6c 6e 73 20 3e 20 30 20 7c 7c 20 70 67 73 20   lns > 0 || pgs 
79440 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 09 20  == 0).      {.. 
79450 20 2f 2a 20 6e 6f 74 20 50 6f 6c 79 67 6f 6e 2f   /* not Polygon/
79460 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 20 2a 2f 0a  MultiPolygon */.
79470 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43  .  gaiaFreeGeomC
79480 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a 09 20 20 73  oll (geom);..  s
79490 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
794a0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
794b0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
794c0 0a 0a 2f 2a 20 74 72 61 6e 73 66 6f 72 6d 69 6e  ../* transformin
794d0 67 20 52 69 6e 67 73 20 69 6e 74 6f 20 4c 69 6e  g Rings into Lin
794e0 65 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20  estrings */.    
794f0 67 65 6f 6d 31 20 3d 20 67 61 69 61 4c 69 6e 65  geom1 = gaiaLine
79500 61 72 69 7a 65 20 28 67 65 6f 6d 2c 20 31 29 3b  arize (geom, 1);
79510 0a 20 20 20 20 69 66 20 28 67 65 6f 6d 31 20 3d  .    if (geom1 =
79520 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a  = NULL).      {.
79530 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43  .  gaiaFreeGeomC
79540 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a 09 20 20 73  oll (geom);..  s
79550 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
79560 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
79570 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
79580 0a 0a 2f 2a 20 69 64 65 6e 74 69 66 79 69 6e 67  ../* identifying
79590 20 74 68 65 20 4e 6f 64 65 73 20 2a 2f 0a 20 20   the Nodes */.  
795a0 20 20 67 65 6f 6d 32 20 3d 20 61 75 78 50 6f 6c    geom2 = auxPol
795b0 79 67 4e 6f 64 65 73 20 28 67 65 6f 6d 29 3b 0a  ygNodes (geom);.
795c0 20 20 20 20 69 66 20 28 67 65 6f 6d 32 20 3d 3d      if (geom2 ==
795d0 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09   NULL).      {..
795e0 20 20 2f 2a 20 74 68 65 72 65 20 69 73 20 6e 6f    /* there is no
795f0 20 6e 65 65 64 20 74 6f 20 63 75 74 20 61 6e 79   need to cut any
79600 20 52 69 6e 67 20 5b 6e 6f 20 4e 6f 64 65 73 5d   Ring [no Nodes]
79610 20 2a 2f 0a 09 20 20 69 6e 74 20 6c 65 6e 3b 0a   */..  int len;.
79620 09 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
79630 20 2a 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c   *p_result = NUL
79640 4c 3b 0a 09 20 20 67 65 6f 6d 31 2d 3e 53 72 69  L;..  geom1->Sri
79650 64 20 3d 20 67 65 6f 6d 2d 3e 53 72 69 64 3b 0a  d = geom->Srid;.
79660 09 20 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c  .  gaiaToSpatiaL
79670 69 74 65 42 6c 6f 62 57 6b 62 20 28 67 65 6f 6d  iteBlobWkb (geom
79680 31 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c  1, &p_result, &l
79690 65 6e 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  en);..  sqlite3_
796a0 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e  result_blob (con
796b0 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20  text, p_result, 
796c0 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 67  len, free);..  g
796d0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
796e0 28 67 65 6f 6d 29 3b 0a 09 20 20 67 61 69 61 46  (geom);..  gaiaF
796f0 72 65 65 47 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f  reeGeomColl (geo
79700 6d 31 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  m1);..  return;.
79710 20 20 20 20 20 20 7d 0a 0a 2f 2a 20 61 74 74 65        }../* atte
79720 6d 70 74 69 6e 67 20 74 6f 20 63 75 74 20 52 69  mpting to cut Ri
79730 6e 67 73 20 2a 2f 0a 20 20 20 20 72 65 73 75 6c  ngs */.    resul
79740 74 20 3d 20 67 61 69 61 4c 69 6e 65 73 43 75 74  t = gaiaLinesCut
79750 41 74 4e 6f 64 65 73 20 28 67 65 6f 6d 31 2c 20  AtNodes (geom1, 
79760 67 65 6f 6d 32 29 3b 0a 20 20 20 20 69 66 20 28  geom2);.    if (
79770 72 65 73 75 6c 74 20 3d 3d 20 4e 55 4c 4c 29 0a  result == NULL).
79780 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
79790 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
797a0 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b      else.      {
797b0 0a 09 20 20 2f 2a 20 62 75 69 6c 64 73 20 74 68  ..  /* builds th
797c0 65 20 42 4c 4f 42 20 67 65 6f 6d 65 74 72 79 20  e BLOB geometry 
797d0 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
797e0 2f 0a 09 20 20 69 6e 74 20 6c 65 6e 3b 0a 09 20  /..  int len;.. 
797f0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
79800 70 5f 72 65 73 75 6c 74 20 3d 20 4e 55 4c 4c 3b  p_result = NULL;
79810 0a 09 20 20 72 65 73 75 6c 74 2d 3e 53 72 69 64  ..  result->Srid
79820 20 3d 20 67 65 6f 6d 2d 3e 53 72 69 64 3b 0a 09   = geom->Srid;..
79830 20 20 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69    gaiaToSpatiaLi
79840 74 65 42 6c 6f 62 57 6b 62 20 28 72 65 73 75 6c  teBlobWkb (resul
79850 74 2c 20 26 70 5f 72 65 73 75 6c 74 2c 20 26 6c  t, &p_result, &l
79860 65 6e 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  en);..  sqlite3_
79870 72 65 73 75 6c 74 5f 62 6c 6f 62 20 28 63 6f 6e  result_blob (con
79880 74 65 78 74 2c 20 70 5f 72 65 73 75 6c 74 2c 20  text, p_result, 
79890 6c 65 6e 2c 20 66 72 65 65 29 3b 0a 09 20 20 67  len, free);..  g
798a0 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c 6c 20  aiaFreeGeomColl 
798b0 28 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20  (result);.      
798c0 7d 0a 20 20 20 20 67 61 69 61 46 72 65 65 47 65  }.    gaiaFreeGe
798d0 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a 20  omColl (geom);. 
798e0 20 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43     gaiaFreeGeomC
798f0 6f 6c 6c 20 28 67 65 6f 6d 31 29 3b 0a 20 20 20  oll (geom1);.   
79900 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43 6f 6c   gaiaFreeGeomCol
79910 6c 20 28 67 65 6f 6d 32 29 3b 0a 7d 0a 0a 23 65  l (geom2);.}..#e
79920 6e 64 69 66 20 2f 2a 20 65 6e 64 20 47 45 4f 53  ndif /* end GEOS
79930 20 61 64 76 61 6e 63 65 64 20 61 6e 64 20 65 78   advanced and ex
79940 70 65 72 69 6d 65 6e 74 61 6c 20 66 65 61 74 75  perimental featu
79950 72 65 73 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f  res */..#endif /
79960 2a 20 65 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20  * end including 
79970 47 45 4f 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GEOS */..#ifndef
79980 20 4f 4d 49 54 5f 4d 41 54 48 53 51 4c 09 09 2f   OMIT_MATHSQL../
79990 2a 20 73 75 70 70 6f 72 74 69 6e 67 20 53 51 4c  * supporting SQL
799a0 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 20   math functions 
799b0 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 74  */..static int.t
799c0 65 73 74 49 6e 76 61 6c 69 64 46 50 20 28 64 6f  estInvalidFP (do
799d0 75 62 6c 65 20 78 29 0a 7b 0a 2f 2a 20 74 65 73  uble x).{./* tes
799e0 74 69 6e 67 20 69 66 20 74 68 69 73 20 6f 6e 65  ting if this one
799f0 20 69 73 20 61 6e 20 69 6e 76 61 6c 69 64 20 46   is an invalid F
79a00 6c 6f 61 74 69 6e 67 20 50 6f 69 6e 74 20 2a 2f  loating Point */
79a10 0a 23 69 66 64 65 66 20 5f 57 49 4e 33 32 0a 20  .#ifdef _WIN32. 
79a20 20 20 20 69 66 20 28 5f 66 70 63 6c 61 73 73 20     if (_fpclass 
79a30 28 78 29 20 3d 3d 20 5f 46 50 43 4c 41 53 53 5f  (x) == _FPCLASS_
79a40 4e 4e 20 7c 7c 20 5f 66 70 63 6c 61 73 73 20 28  NN || _fpclass (
79a50 78 29 20 3d 3d 20 5f 46 50 43 4c 41 53 53 5f 50  x) == _FPCLASS_P
79a60 4e 20 7c 7c 0a 09 5f 66 70 63 6c 61 73 73 20 28  N ||.._fpclass (
79a70 78 29 20 3d 3d 20 5f 46 50 43 4c 41 53 53 5f 4e  x) == _FPCLASS_N
79a80 5a 20 7c 7c 20 5f 66 70 63 6c 61 73 73 20 28 78  Z || _fpclass (x
79a90 29 20 3d 3d 20 5f 46 50 43 4c 41 53 53 5f 50 5a  ) == _FPCLASS_PZ
79aa0 29 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a 09 72  )..;.    else..r
79ab0 65 74 75 72 6e 20 31 3b 0a 23 65 6c 73 65 0a 20  eturn 1;.#else. 
79ac0 20 20 20 69 66 20 28 66 70 63 6c 61 73 73 69 66     if (fpclassif
79ad0 79 20 28 78 29 20 3d 3d 20 46 50 5f 4e 4f 52 4d  y (x) == FP_NORM
79ae0 41 4c 20 7c 7c 20 66 70 63 6c 61 73 73 69 66 79  AL || fpclassify
79af0 20 28 78 29 20 3d 3d 20 46 50 5f 5a 45 52 4f 29   (x) == FP_ZERO)
79b00 0a 09 3b 0a 20 20 20 20 65 6c 73 65 0a 09 72 65  ..;.    else..re
79b10 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20  turn 1;.#endif. 
79b20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a     return 0;.}..
79b30 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
79b40 5f 6d 61 74 68 5f 61 63 6f 73 20 28 73 71 6c 69  _math_acos (sqli
79b50 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
79b60 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
79b70 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
79b80 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
79b90 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 61 63 6f   function:./ aco
79ba0 73 28 64 6f 75 62 6c 65 20 58 29 0a 2f 0a 2f 20  s(double X)././ 
79bb0 52 65 74 75 72 6e 73 20 74 68 65 20 61 72 63 20  Returns the arc 
79bc0 63 6f 73 69 6e 65 20 6f 66 20 58 2c 20 74 68 61  cosine of X, tha
79bd0 74 20 69 73 2c 20 74 68 65 20 76 61 6c 75 65 20  t is, the value 
79be0 77 68 6f 73 65 20 63 6f 73 69 6e 65 20 69 73 20  whose cosine is 
79bf0 58 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61  X./ or NULL if a
79c00 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
79c10 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69  untered.*/.    i
79c20 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20  nt int_value;.  
79c30 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20    double x;.    
79c40 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
79c50 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
79c60 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
79c70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
79c80 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
79c90 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 20 20 20 20  LITE_FLOAT).    
79ca0 20 20 7b 0a 09 20 20 78 20 3d 20 61 63 6f 73 20    {..  x = acos 
79cb0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64  (sqlite3_value_d
79cc0 6f 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 29  ouble (argv[0]))
79cd0 3b 0a 09 20 20 69 66 20 28 74 65 73 74 49 6e 76  ;..  if (testInv
79ce0 61 6c 69 64 46 50 20 28 78 29 29 0a 09 20 20 20  alidFP (x))..   
79cf0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
79d00 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
79d10 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20  ;..  else..     
79d20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
79d30 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c  double (context,
79d40 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   x);.      }.   
79d50 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
79d60 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
79d70 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[0]) == SQLITE
79d80 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
79d90 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
79da0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
79db0 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20  nt (argv[0]);.. 
79dc0 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a   x = int_value;.
79dd0 09 20 20 78 20 3d 20 61 63 6f 73 20 28 78 29 3b  .  x = acos (x);
79de0 0a 09 20 20 69 66 20 28 74 65 73 74 49 6e 76 61  ..  if (testInva
79df0 6c 69 64 46 50 20 28 78 29 29 0a 09 20 20 20 20  lidFP (x))..    
79e00 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
79e10 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
79e20 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20  ..  else..      
79e30 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
79e40 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20  ouble (context, 
79e50 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  x);.      }.    
79e60 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65  else..sqlite3_re
79e70 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
79e80 78 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  xt);.}..static v
79e90 6f 69 64 0a 66 6e 63 74 5f 6d 61 74 68 5f 61 73  oid.fnct_math_as
79ea0 69 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  in (sqlite3_cont
79eb0 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
79ec0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
79ed0 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
79ee0 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
79ef0 6e 3a 0a 2f 20 61 73 69 6e 28 64 6f 75 62 6c 65  n:./ asin(double
79f00 20 58 29 0a 2f 0a 2f 20 52 65 74 75 72 6e 73 20   X)././ Returns 
79f10 74 68 65 20 61 72 63 20 73 69 6e 65 20 6f 66 20  the arc sine of 
79f20 58 2c 20 74 68 61 74 20 69 73 2c 20 74 68 65 20  X, that is, the 
79f30 76 61 6c 75 65 20 77 68 6f 73 65 20 73 69 6e 65  value whose sine
79f40 20 69 73 20 58 0a 2f 20 6f 72 20 4e 55 4c 4c 20   is X./ or NULL 
79f50 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20  if any error is 
79f60 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20  encountered.*/. 
79f70 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65     int int_value
79f80 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a  ;.    double x;.
79f90 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
79fa0 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
79fb0 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
79fc0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
79fd0 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d  type (argv[0]) =
79fe0 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a  = SQLITE_FLOAT).
79ff0 20 20 20 20 20 20 7b 0a 09 20 20 78 20 3d 20 61        {..  x = a
7a000 73 69 6e 20 28 73 71 6c 69 74 65 33 5f 76 61 6c  sin (sqlite3_val
7a010 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b  ue_double (argv[
7a020 30 5d 29 29 3b 0a 09 20 20 69 66 20 28 74 65 73  0]));..  if (tes
7a030 74 49 6e 76 61 6c 69 64 46 50 20 28 78 29 29 0a  tInvalidFP (x)).
7a040 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
7a050 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
7a060 65 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20  ext);..  else.. 
7a070 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
7a080 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74  ult_double (cont
7a090 65 78 74 2c 20 78 29 3b 0a 20 20 20 20 20 20 7d  ext, x);.      }
7a0a0 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71  .    else if (sq
7a0b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
7a0c0 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
7a0d0 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
7a0e0 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c      {..  int_val
7a0f0 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
7a100 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29  ue_int (argv[0])
7a110 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c  ;..  x = int_val
7a120 75 65 3b 0a 09 20 20 78 20 3d 20 61 73 69 6e 20  ue;..  x = asin 
7a130 28 78 29 3b 0a 09 20 20 69 66 20 28 74 65 73 74  (x);..  if (test
7a140 49 6e 76 61 6c 69 64 46 50 20 28 78 29 29 0a 09  InvalidFP (x))..
7a150 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
7a160 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
7a170 78 74 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20  xt);..  else..  
7a180 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
7a190 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65  lt_double (conte
7a1a0 78 74 2c 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a  xt, x);.      }.
7a1b0 20 20 20 20 65 6c 73 65 0a 09 73 71 6c 69 74 65      else..sqlite
7a1c0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
7a1d0 6f 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73 74 61 74  ontext);.}..stat
7a1e0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74  ic void.fnct_mat
7a1f0 68 5f 61 74 61 6e 20 28 73 71 6c 69 74 65 33 5f  h_atan (sqlite3_
7a200 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
7a210 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
7a220 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
7a230 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
7a240 63 74 69 6f 6e 3a 0a 2f 20 61 74 61 6e 28 64 6f  ction:./ atan(do
7a250 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74 75  uble X)././ Retu
7a260 72 6e 73 20 74 68 65 20 61 72 63 20 74 61 6e 67  rns the arc tang
7a270 65 6e 74 20 6f 66 20 58 2c 20 74 68 61 74 20 69  ent of X, that i
7a280 73 2c 20 74 68 65 20 76 61 6c 75 65 20 77 68 6f  s, the value who
7a290 73 65 20 74 61 6e 67 65 6e 74 20 69 73 20 58 0a  se tangent is X.
7a2a0 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79  / or NULL if any
7a2b0 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
7a2c0 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74  tered.*/.    int
7a2d0 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20   int_value;.    
7a2e0 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 47 41  double x;.    GA
7a2f0 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
7a300 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
7a310 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
7a320 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
7a330 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49  argv[0]) == SQLI
7a340 54 45 5f 46 4c 4f 41 54 29 0a 20 20 20 20 20 20  TE_FLOAT).      
7a350 7b 0a 09 20 20 78 20 3d 20 61 74 61 6e 20 28 73  {..  x = atan (s
7a360 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
7a370 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 29 3b 0a  ble (argv[0]));.
7a380 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
7a390 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78  t_double (contex
7a3a0 74 2c 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20  t, x);.      }. 
7a3b0 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69     else if (sqli
7a3c0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
7a3d0 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49  argv[0]) == SQLI
7a3e0 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20  TE_INTEGER).    
7a3f0 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65    {..  int_value
7a400 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
7a410 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a  _int (argv[0]);.
7a420 09 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65  .  x = int_value
7a430 3b 0a 09 20 20 78 20 3d 20 61 74 61 6e 20 28 78  ;..  x = atan (x
7a440 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
7a450 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e  sult_double (con
7a460 74 65 78 74 2c 20 78 29 3b 0a 20 20 20 20 20 20  text, x);.      
7a470 7d 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c 69  }.    else..sqli
7a480 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7a490 28 63 6f 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73 74  (context);.}..st
7a4a0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d  atic void.fnct_m
7a4b0 61 74 68 5f 63 65 69 6c 20 28 73 71 6c 69 74 65  ath_ceil (sqlite
7a4c0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
7a4d0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
7a4e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
7a4f0 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
7a500 75 6e 63 74 69 6f 6e 3a 0a 2f 20 63 65 69 6c 28  unction:./ ceil(
7a510 64 6f 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65  double X)././ Re
7a520 74 75 72 6e 73 20 74 68 65 20 73 6d 61 6c 6c 65  turns the smalle
7a530 73 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  st integer value
7a540 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 58   not less than X
7a550 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
7a560 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
7a570 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e  ntered.*/.    in
7a580 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  t int_value;.   
7a590 20 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 47   double x;.    G
7a5a0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
7a5b0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
7a5c0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
7a5d0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
7a5e0 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c  (argv[0]) == SQL
7a5f0 49 54 45 5f 46 4c 4f 41 54 29 0a 20 20 20 20 20  ITE_FLOAT).     
7a600 20 7b 0a 09 20 20 78 20 3d 20 63 65 69 6c 20 28   {..  x = ceil (
7a610 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
7a620 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 29 3b  uble (argv[0]));
7a630 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
7a640 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65  lt_double (conte
7a650 78 74 2c 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a  xt, x);.      }.
7a660 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c      else if (sql
7a670 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
7a680 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c  (argv[0]) == SQL
7a690 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20  ITE_INTEGER).   
7a6a0 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75     {..  int_valu
7a6b0 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
7a6c0 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b  e_int (argv[0]);
7a6d0 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75  ..  x = int_valu
7a6e0 65 3b 0a 09 20 20 78 20 3d 20 63 65 69 6c 20 28  e;..  x = ceil (
7a6f0 78 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72  x);..  sqlite3_r
7a700 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f  esult_double (co
7a710 6e 74 65 78 74 2c 20 78 29 3b 0a 20 20 20 20 20  ntext, x);.     
7a720 20 7d 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c   }.    else..sql
7a730 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
7a740 20 28 63 6f 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73   (context);.}..s
7a750 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
7a760 6d 61 74 68 5f 63 6f 73 20 28 73 71 6c 69 74 65  math_cos (sqlite
7a770 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
7a780 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
7a790 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
7a7a0 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
7a7b0 75 6e 63 74 69 6f 6e 3a 0a 2f 20 63 6f 73 28 64  unction:./ cos(d
7a7c0 6f 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74  ouble X)././ Ret
7a7d0 75 72 6e 73 20 74 68 65 20 63 6f 73 69 6e 65 20  urns the cosine 
7a7e0 6f 66 20 58 2c 20 77 68 65 72 65 20 58 20 69 73  of X, where X is
7a7f0 20 67 69 76 65 6e 20 69 6e 20 72 61 64 69 61 6e   given in radian
7a800 73 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61  s./ or NULL if a
7a810 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
7a820 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69  untered.*/.    i
7a830 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20  nt int_value;.  
7a840 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20    double x;.    
7a850 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
7a860 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
7a870 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
7a880 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
7a890 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
7a8a0 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 20 20 20 20  LITE_FLOAT).    
7a8b0 20 20 7b 0a 09 20 20 78 20 3d 20 63 6f 73 20 28    {..  x = cos (
7a8c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
7a8d0 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 29 3b  uble (argv[0]));
7a8e0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
7a8f0 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65  lt_double (conte
7a900 78 74 2c 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a  xt, x);.      }.
7a910 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c      else if (sql
7a920 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
7a930 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c  (argv[0]) == SQL
7a940 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20  ITE_INTEGER).   
7a950 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75     {..  int_valu
7a960 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
7a970 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b  e_int (argv[0]);
7a980 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75  ..  x = int_valu
7a990 65 3b 0a 09 20 20 78 20 3d 20 63 6f 73 20 28 78  e;..  x = cos (x
7a9a0 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
7a9b0 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e  sult_double (con
7a9c0 74 65 78 74 2c 20 78 29 3b 0a 20 20 20 20 20 20  text, x);.      
7a9d0 7d 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c 69  }.    else..sqli
7a9e0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7a9f0 28 63 6f 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73 74  (context);.}..st
7aa00 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d  atic void.fnct_m
7aa10 61 74 68 5f 63 6f 74 20 28 73 71 6c 69 74 65 33  ath_cot (sqlite3
7aa20 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
7aa30 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
7aa40 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
7aa50 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
7aa60 6e 63 74 69 6f 6e 3a 0a 2f 20 63 6f 74 28 64 6f  nction:./ cot(do
7aa70 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74 75  uble X)././ Retu
7aa80 72 6e 73 20 74 68 65 20 63 6f 74 61 6e 67 65 6e  rns the cotangen
7aa90 74 20 6f 66 20 58 0a 2f 20 6f 72 20 4e 55 4c 4c  t of X./ or NULL
7aaa0 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73   if any error is
7aab0 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a   encountered.*/.
7aac0 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75      int int_valu
7aad0 65 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b  e;.    double x;
7aae0 0a 20 20 20 20 64 6f 75 62 6c 65 20 74 61 6e 67  .    double tang
7aaf0 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
7ab00 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
7ab10 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
7ab20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
7ab30 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
7ab40 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54   == SQLITE_FLOAT
7ab50 29 0a 09 78 20 3d 20 73 71 6c 69 74 65 33 5f 76  )..x = sqlite3_v
7ab60 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67  alue_double (arg
7ab70 76 5b 30 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20  v[0]);.    else 
7ab80 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
7ab90 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
7aba0 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   == SQLITE_INTEG
7abb0 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  ER).      {..  i
7abc0 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74  nt_value = sqlit
7abd0 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72  e3_value_int (ar
7abe0 67 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69  gv[0]);..  x = i
7abf0 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  nt_value;.      
7ac00 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20  }.    else.     
7ac10 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
7ac20 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
7ac30 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
7ac40 20 20 20 20 20 20 7d 0a 20 20 20 20 74 61 6e 67        }.    tang
7ac50 20 3d 20 74 61 6e 20 28 78 29 3b 0a 20 20 20 20   = tan (x);.    
7ac60 69 66 20 28 74 61 6e 67 20 3d 3d 20 30 2e 30 29  if (tang == 0.0)
7ac70 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
7ac80 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7ac90 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
7aca0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
7acb0 20 20 78 20 3d 20 31 2e 30 20 2f 20 74 61 6e 67    x = 1.0 / tang
7acc0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
7acd0 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e  sult_double (con
7ace0 74 65 78 74 2c 20 78 29 3b 0a 7d 0a 0a 73 74 61  text, x);.}..sta
7acf0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61  tic void.fnct_ma
7ad00 74 68 5f 64 65 67 72 65 65 73 20 28 73 71 6c 69  th_degrees (sqli
7ad10 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
7ad20 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
7ad30 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7ad40 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c  * argv).{./* SQL
7ad50 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 64 65 67   function:./ deg
7ad60 72 65 65 73 28 64 6f 75 62 6c 65 20 58 29 0a 2f  rees(double X)./
7ad70 0a 2f 20 52 65 74 75 72 6e 73 20 74 68 65 20 61  ./ Returns the a
7ad80 72 67 75 6d 65 6e 74 20 58 2c 20 63 6f 6e 76 65  rgument X, conve
7ad90 72 74 65 64 20 66 72 6f 6d 20 72 61 64 69 61 6e  rted from radian
7ada0 73 20 74 6f 20 64 65 67 72 65 65 73 0a 2f 20 6f  s to degrees./ o
7adb0 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72  r NULL if any er
7adc0 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
7add0 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 69 6e  ed.*/.    int in
7ade0 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64 6f 75  t_value;.    dou
7adf0 62 6c 65 20 78 3b 0a 20 20 20 20 47 41 49 41 5f  ble x;.    GAIA_
7ae00 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c  UNUSED ();../* L
7ae10 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f  COV_EXCL_LINE */
7ae20 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33  .    if (sqlite3
7ae30 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
7ae40 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[0]) == SQLITE_
7ae50 46 4c 4f 41 54 29 0a 09 78 20 3d 20 73 71 6c 69  FLOAT)..x = sqli
7ae60 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
7ae70 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20   (argv[0]);.    
7ae80 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33  else if (sqlite3
7ae90 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67  _value_type (arg
7aea0 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f  v[0]) == SQLITE_
7aeb0 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b  INTEGER).      {
7aec0 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20  ..  int_value = 
7aed0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
7aee0 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20  t (argv[0]);..  
7aef0 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20  x = int_value;. 
7af00 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
7af10 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
7af20 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
7af30 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
7af40 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
7af50 20 78 20 3d 20 78 20 2a 20 35 37 2e 32 39 35 37   x = x * 57.2957
7af60 37 39 35 31 33 30 38 32 33 32 3b 0a 20 20 20 20  7951308232;.    
7af70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
7af80 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20  ouble (context, 
7af90 78 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  x);.}..static vo
7afa0 69 64 0a 66 6e 63 74 5f 6d 61 74 68 5f 65 78 70  id.fnct_math_exp
7afb0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
7afc0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
7afd0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
7afe0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
7aff0 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
7b000 0a 2f 20 65 78 70 28 64 6f 75 62 6c 65 20 58 29  ./ exp(double X)
7b010 0a 2f 0a 2f 20 52 65 74 75 72 6e 73 20 74 68 65  ././ Returns the
7b020 20 76 61 6c 75 65 20 6f 66 20 65 20 28 74 68 65   value of e (the
7b030 20 62 61 73 65 20 6f 66 20 6e 61 74 75 72 61 6c   base of natural
7b040 20 6c 6f 67 61 72 69 74 68 6d 73 29 20 72 61 69   logarithms) rai
7b050 73 65 64 20 74 6f 20 74 68 65 20 70 6f 77 65 72  sed to the power
7b060 20 6f 66 20 58 0a 2f 20 6f 72 20 4e 55 4c 4c 20   of X./ or NULL 
7b070 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20  if any error is 
7b080 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20  encountered.*/. 
7b090 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65     int int_value
7b0a0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a  ;.    double x;.
7b0b0 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20      GAIA_UNUSED 
7b0c0 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43  ();../* LCOV_EXC
7b0d0 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66  L_LINE */.    if
7b0e0 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   (sqlite3_value_
7b0f0 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d  type (argv[0]) =
7b100 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a  = SQLITE_FLOAT).
7b110 20 20 20 20 20 20 7b 0a 09 20 20 78 20 3d 20 65        {..  x = e
7b120 78 70 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  xp (sqlite3_valu
7b130 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 30  e_double (argv[0
7b140 5d 29 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f  ]));..  sqlite3_
7b150 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63  result_double (c
7b160 6f 6e 74 65 78 74 2c 20 78 29 3b 0a 20 20 20 20  ontext, x);.    
7b170 20 20 7d 0a 20 20 20 20 65 6c 73 65 20 69 66 20    }.    else if 
7b180 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
7b190 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
7b1a0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
7b1b0 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f  .      {..  int_
7b1c0 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  value = sqlite3_
7b1d0 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
7b1e0 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f  0]);..  x = int_
7b1f0 76 61 6c 75 65 3b 0a 09 20 20 78 20 3d 20 65 78  value;..  x = ex
7b200 70 20 28 78 29 3b 0a 09 20 20 73 71 6c 69 74 65  p (x);..  sqlite
7b210 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20  3_result_double 
7b220 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b 0a 20 20  (context, x);.  
7b230 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 09      }.    else..
7b240 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
7b250 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 7d  ull (context);.}
7b260 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
7b270 63 74 5f 6d 61 74 68 5f 66 6c 6f 6f 72 20 28 73  ct_math_floor (s
7b280 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7b290 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
7b2a0 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
7b2b0 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
7b2c0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
7b2d0 66 6c 6f 6f 72 28 64 6f 75 62 6c 65 20 58 29 0a  floor(double X).
7b2e0 2f 0a 2f 20 52 65 74 75 72 6e 73 20 74 68 65 20  /./ Returns the 
7b2f0 6c 61 72 67 65 73 74 20 69 6e 74 65 67 65 72 20  largest integer 
7b300 76 61 6c 75 65 20 6e 6f 74 20 67 72 65 61 74 65  value not greate
7b310 72 20 74 68 61 6e 20 58 0a 2f 20 6f 72 20 4e 55  r than X./ or NU
7b320 4c 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20  LL if any error 
7b330 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
7b340 2f 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61  /.    int int_va
7b350 6c 75 65 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  lue;.    double 
7b360 78 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  x;.    GAIA_UNUS
7b370 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
7b380 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
7b390 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c   if (sqlite3_val
7b3a0 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d  ue_type (argv[0]
7b3b0 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41  ) == SQLITE_FLOA
7b3c0 54 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 78 20  T).      {..  x 
7b3d0 3d 20 66 6c 6f 6f 72 20 28 73 71 6c 69 74 65 33  = floor (sqlite3
7b3e0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61  _value_double (a
7b3f0 72 67 76 5b 30 5d 29 29 3b 0a 09 20 20 73 71 6c  rgv[0]));..  sql
7b400 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
7b410 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b  le (context, x);
7b420 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
7b430 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
7b440 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
7b450 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
7b460 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
7b470 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
7b480 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
7b490 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d  argv[0]);..  x =
7b4a0 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 09 20 20 78   int_value;..  x
7b4b0 20 3d 20 66 6c 6f 6f 72 20 28 78 29 3b 0a 09 20   = floor (x);.. 
7b4c0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
7b4d0 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c  double (context,
7b4e0 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   x);.      }.   
7b4f0 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72   else..sqlite3_r
7b500 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
7b510 65 78 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ext);.}..static 
7b520 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74 68 5f 6c  void.fnct_math_l
7b530 6f 67 6e 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ogn (sqlite3_con
7b540 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
7b550 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
7b560 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
7b570 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69  .{./* SQL functi
7b580 6f 6e 3a 0a 2f 20 6c 6f 67 28 64 6f 75 62 6c 65  on:./ log(double
7b590 20 58 29 0a 2f 0a 2f 20 52 65 74 75 72 6e 73 20   X)././ Returns 
7b5a0 74 68 65 20 6e 61 74 75 72 61 6c 20 6c 6f 67 61  the natural loga
7b5b0 72 69 74 68 6d 20 6f 66 20 58 3b 20 74 68 61 74  rithm of X; that
7b5c0 20 69 73 2c 20 74 68 65 20 62 61 73 65 2d 65 20   is, the base-e 
7b5d0 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20 58 0a 2f  logarithm of X./
7b5e0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
7b5f0 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
7b600 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20  ered.*/.    int 
7b610 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64  int_value;.    d
7b620 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 47 41 49  ouble x;.    GAI
7b630 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
7b640 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
7b650 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
7b660 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
7b670 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[0]) == SQLIT
7b680 45 5f 46 4c 4f 41 54 29 0a 20 20 20 20 20 20 7b  E_FLOAT).      {
7b690 0a 09 20 20 78 20 3d 20 6c 6f 67 20 28 73 71 6c  ..  x = log (sql
7b6a0 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
7b6b0 65 20 28 61 72 67 76 5b 30 5d 29 29 3b 0a 09 20  e (argv[0]));.. 
7b6c0 20 69 66 20 28 74 65 73 74 49 6e 76 61 6c 69 64   if (testInvalid
7b6d0 46 50 20 28 78 29 29 0a 09 20 20 20 20 20 20 73  FP (x))..      s
7b6e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
7b6f0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
7b700 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
7b710 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
7b720 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b  le (context, x);
7b730 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
7b740 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
7b750 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
7b760 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
7b770 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
7b780 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
7b790 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
7b7a0 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d  argv[0]);..  x =
7b7b0 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 09 20 20 78   int_value;..  x
7b7c0 20 3d 20 6c 6f 67 20 28 78 29 3b 0a 09 20 20 69   = log (x);..  i
7b7d0 66 20 28 74 65 73 74 49 6e 76 61 6c 69 64 46 50  f (testInvalidFP
7b7e0 20 28 78 29 29 0a 09 20 20 20 20 20 20 73 71 6c   (x))..      sql
7b7f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
7b800 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 65   (context);..  e
7b810 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74  lse..      sqlit
7b820 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
7b830 20 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b 0a 20   (context, x);. 
7b840 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a       }.    else.
7b850 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  .sqlite3_result_
7b860 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
7b870 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
7b880 6e 63 74 5f 6d 61 74 68 5f 6c 6f 67 6e 32 20 28  nct_math_logn2 (
7b890 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
7b8a0 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
7b8b0 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
7b8c0 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a  ue ** argv).{./*
7b8d0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
7b8e0 20 6c 6f 67 28 64 6f 75 62 6c 65 20 42 2c 20 64   log(double B, d
7b8f0 6f 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74  ouble X)././ Ret
7b900 75 72 6e 73 20 74 68 65 20 6c 6f 67 61 72 69 74  urns the logarit
7b910 68 6d 20 6f 66 20 58 20 74 6f 20 74 68 65 20 62  hm of X to the b
7b920 61 73 65 20 42 0a 2f 20 6f 72 20 4e 55 4c 4c 20  ase B./ or NULL 
7b930 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20  if any error is 
7b940 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20  encountered.*/. 
7b950 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65     int int_value
7b960 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 20 3d  ;.    double x =
7b970 20 30 2e 30 3b 0a 20 20 20 20 64 6f 75 62 6c 65   0.0;.    double
7b980 20 62 20 3d 20 31 2e 30 3b 0a 20 20 20 20 64 6f   b = 1.0;.    do
7b990 75 62 6c 65 20 6c 6f 67 31 3b 0a 20 20 20 20 64  uble log1;.    d
7b9a0 6f 75 62 6c 65 20 6c 6f 67 32 3b 0a 20 20 20 20  ouble log2;.    
7b9b0 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
7b9c0 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
7b9d0 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
7b9e0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
7b9f0 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
7ba00 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 78 20 3d  LITE_FLOAT)..x =
7ba10 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
7ba20 6f 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 3b  ouble (argv[0]);
7ba30 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71  .    else if (sq
7ba40 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
7ba50 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
7ba60 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
7ba70 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c      {..  int_val
7ba80 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
7ba90 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29  ue_int (argv[0])
7baa0 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c  ;..  x = int_val
7bab0 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
7bac0 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
7bad0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
7bae0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
7baf0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
7bb00 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
7bb10 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
7bb20 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
7bb30 5f 46 4c 4f 41 54 29 0a 09 62 20 3d 20 73 71 6c  _FLOAT)..b = sql
7bb40 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
7bb50 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  e (argv[1]);.   
7bb60 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
7bb70 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
7bb80 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
7bb90 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
7bba0 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
7bbb0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
7bbc0 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20  nt (argv[1]);.. 
7bbd0 20 62 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a   b = int_value;.
7bbe0 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
7bbf0 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
7bc00 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7bc10 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
7bc20 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
7bc30 20 20 69 66 20 28 78 20 3c 3d 20 30 2e 30 20 7c    if (x <= 0.0 |
7bc40 7c 20 62 20 3c 3d 20 31 2e 30 29 0a 20 20 20 20  | b <= 1.0).    
7bc50 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72    {..  sqlite3_r
7bc60 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74  esult_null (cont
7bc70 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  ext);..  return;
7bc80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 6c 6f 67  .      }.    log
7bc90 31 20 3d 20 6c 6f 67 20 28 78 29 3b 0a 20 20 20  1 = log (x);.   
7bca0 20 69 66 20 28 74 65 73 74 49 6e 76 61 6c 69 64   if (testInvalid
7bcb0 46 50 20 28 6c 6f 67 31 29 29 0a 20 20 20 20 20  FP (log1)).     
7bcc0 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65   {..  sqlite3_re
7bcd0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
7bce0 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a  xt);..  return;.
7bcf0 20 20 20 20 20 20 7d 0a 20 20 20 20 6c 6f 67 32        }.    log2
7bd00 20 3d 20 6c 6f 67 20 28 62 29 3b 0a 20 20 20 20   = log (b);.    
7bd10 69 66 20 28 74 65 73 74 49 6e 76 61 6c 69 64 46  if (testInvalidF
7bd20 50 20 28 6c 6f 67 32 29 29 0a 20 20 20 20 20 20  P (log2)).      
7bd30 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
7bd40 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
7bd50 74 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  t);..  return;. 
7bd60 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74       }.    sqlit
7bd70 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
7bd80 20 28 63 6f 6e 74 65 78 74 2c 20 6c 6f 67 31 20   (context, log1 
7bd90 2f 20 6c 6f 67 32 29 3b 0a 7d 0a 0a 73 74 61 74  / log2);.}..stat
7bda0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74  ic void.fnct_mat
7bdb0 68 5f 6c 6f 67 5f 32 20 28 73 71 6c 69 74 65 33  h_log_2 (sqlite3
7bdc0 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
7bdd0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
7bde0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
7bdf0 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
7be00 6e 63 74 69 6f 6e 3a 0a 2f 20 6c 6f 67 32 28 64  nction:./ log2(d
7be10 6f 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74  ouble X)././ Ret
7be20 75 72 6e 73 20 74 68 65 20 62 61 73 65 2d 32 20  urns the base-2 
7be30 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20 58 0a 2f  logarithm of X./
7be40 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
7be50 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
7be60 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20  ered.*/.    int 
7be70 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64  int_value;.    d
7be80 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 64 6f 75  ouble x;.    dou
7be90 62 6c 65 20 6c 6f 67 31 3b 0a 20 20 20 20 64 6f  ble log1;.    do
7bea0 75 62 6c 65 20 6c 6f 67 32 3b 0a 20 20 20 20 47  uble log2;.    G
7beb0 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
7bec0 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
7bed0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
7bee0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
7bef0 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c  (argv[0]) == SQL
7bf00 49 54 45 5f 46 4c 4f 41 54 29 0a 09 78 20 3d 20  ITE_FLOAT)..x = 
7bf10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
7bf20 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 3b 0a  uble (argv[0]);.
7bf30 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71 6c      else if (sql
7bf40 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
7bf50 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c  (argv[0]) == SQL
7bf60 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20 20  ITE_INTEGER).   
7bf70 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75     {..  int_valu
7bf80 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
7bf90 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b  e_int (argv[0]);
7bfa0 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75  ..  x = int_valu
7bfb0 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65  e;.      }.    e
7bfc0 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73  lse.      {..  s
7bfd0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
7bfe0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
7bff0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
7c000 0a 20 20 20 20 6c 6f 67 31 20 3d 20 6c 6f 67 20  .    log1 = log 
7c010 28 78 29 3b 0a 20 20 20 20 69 66 20 28 74 65 73  (x);.    if (tes
7c020 74 49 6e 76 61 6c 69 64 46 50 20 28 6c 6f 67 31  tInvalidFP (log1
7c030 29 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  )).      {..  sq
7c040 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
7c050 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
7c060 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
7c070 20 20 20 20 6c 6f 67 32 20 3d 20 6c 6f 67 20 28      log2 = log (
7c080 32 2e 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  2.0);.    sqlite
7c090 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20  3_result_double 
7c0a0 28 63 6f 6e 74 65 78 74 2c 20 6c 6f 67 31 20 2f  (context, log1 /
7c0b0 20 6c 6f 67 32 29 3b 0a 7d 0a 0a 73 74 61 74 69   log2);.}..stati
7c0c0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74 68  c void.fnct_math
7c0d0 5f 6c 6f 67 5f 31 30 20 28 73 71 6c 69 74 65 33  _log_10 (sqlite3
7c0e0 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
7c0f0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
7c100 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
7c110 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
7c120 6e 63 74 69 6f 6e 3a 0a 2f 20 6c 6f 67 31 30 28  nction:./ log10(
7c130 64 6f 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65  double X)././ Re
7c140 74 75 72 6e 73 20 74 68 65 20 62 61 73 65 2d 31  turns the base-1
7c150 30 20 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20 58  0 logarithm of X
7c160 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e  ./ or NULL if an
7c170 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  y error is encou
7c180 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e  ntered.*/.    in
7c190 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20  t int_value;.   
7c1a0 20 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 64   double x;.    d
7c1b0 6f 75 62 6c 65 20 6c 6f 67 31 3b 0a 20 20 20 20  ouble log1;.    
7c1c0 64 6f 75 62 6c 65 20 6c 6f 67 32 3b 0a 20 20 20  double log2;.   
7c1d0 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
7c1e0 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
7c1f0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
7c200 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
7c210 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53  e (argv[0]) == S
7c220 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 78 20  QLITE_FLOAT)..x 
7c230 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
7c240 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29  double (argv[0])
7c250 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73  ;.    else if (s
7c260 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
7c270 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53  e (argv[0]) == S
7c280 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20  QLITE_INTEGER). 
7c290 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61       {..  int_va
7c2a0 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lue = sqlite3_va
7c2b0 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d  lue_int (argv[0]
7c2c0 29 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61  );..  x = int_va
7c2d0 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lue;.      }.   
7c2e0 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20   else.      {.. 
7c2f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
7c300 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
7c310 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
7c320 20 7d 0a 20 20 20 20 6c 6f 67 31 20 3d 20 6c 6f   }.    log1 = lo
7c330 67 20 28 78 29 3b 0a 20 20 20 20 69 66 20 28 74  g (x);.    if (t
7c340 65 73 74 49 6e 76 61 6c 69 64 46 50 20 28 6c 6f  estInvalidFP (lo
7c350 67 31 29 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  g1)).      {..  
7c360 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
7c370 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
7c380 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
7c390 7d 0a 20 20 20 20 6c 6f 67 32 20 3d 20 6c 6f 67  }.    log2 = log
7c3a0 20 28 31 30 2e 30 29 3b 0a 20 20 20 20 73 71 6c   (10.0);.    sql
7c3b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
7c3c0 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 6c 6f 67  le (context, log
7c3d0 31 20 2f 20 6c 6f 67 32 29 3b 0a 7d 0a 0a 73 74  1 / log2);.}..st
7c3e0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d  atic void.fnct_m
7c3f0 61 74 68 5f 70 69 20 28 73 71 6c 69 74 65 33 5f  ath_pi (sqlite3_
7c400 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
7c410 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
7c420 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
7c430 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
7c440 63 74 69 6f 6e 3a 0a 2f 20 70 69 28 76 6f 69 64  ction:./ pi(void
7c450 29 0a 2f 0a 2f 20 52 65 74 75 72 6e 73 20 74 68  )././ Returns th
7c460 65 20 76 61 6c 75 65 20 6f 66 20 28 70 69 29 0a  e value of (pi).
7c470 2a 2f 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53  */.    GAIA_UNUS
7c480 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f  ED ();../* LCOV_
7c490 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20  EXCL_LINE */.   
7c4a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
7c4b0 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c  double (context,
7c4c0 20 33 2e 31 34 31 35 39 32 36 35 33 35 38 39 37   3.1415926535897
7c4d0 39 33 32 33 38 34 36 29 3b 0a 7d 0a 0a 73 74 61  9323846);.}..sta
7c4e0 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61  tic void.fnct_ma
7c4f0 74 68 5f 70 6f 77 20 28 73 71 6c 69 74 65 33 5f  th_pow (sqlite3_
7c500 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
7c510 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
7c520 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
7c530 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  gv).{./* SQL fun
7c540 63 74 69 6f 6e 3a 0a 2f 20 70 6f 77 28 64 6f 75  ction:./ pow(dou
7c550 62 6c 65 20 58 2c 20 64 6f 75 62 6c 65 20 59 29  ble X, double Y)
7c560 0a 2f 0a 2f 20 52 65 74 75 72 6e 73 20 74 68 65  ././ Returns the
7c570 20 76 61 6c 75 65 20 6f 66 20 58 20 72 61 69 73   value of X rais
7c580 65 64 20 74 6f 20 74 68 65 20 70 6f 77 65 72 20  ed to the power 
7c590 6f 66 20 59 2e 0a 2f 20 6f 72 20 4e 55 4c 4c 20  of Y../ or NULL 
7c5a0 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20  if any error is 
7c5b0 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20  encountered.*/. 
7c5c0 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65     int int_value
7c5d0 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a  ;.    double x;.
7c5e0 20 20 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20      double y;.  
7c5f0 20 20 64 6f 75 62 6c 65 20 70 3b 0a 20 20 20 20    double p;.    
7c600 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09  GAIA_UNUSED ();.
7c610 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49  ./* LCOV_EXCL_LI
7c620 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71  NE */.    if (sq
7c630 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
7c640 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
7c650 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09 78 20 3d  LITE_FLOAT)..x =
7c660 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
7c670 6f 75 62 6c 65 20 28 61 72 67 76 5b 30 5d 29 3b  ouble (argv[0]);
7c680 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 73 71  .    else if (sq
7c690 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
7c6a0 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51   (argv[0]) == SQ
7c6b0 4c 49 54 45 5f 49 4e 54 45 47 45 52 29 0a 20 20  LITE_INTEGER).  
7c6c0 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f 76 61 6c      {..  int_val
7c6d0 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
7c6e0 75 65 5f 69 6e 74 20 28 61 72 67 76 5b 30 5d 29  ue_int (argv[0])
7c6f0 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f 76 61 6c  ;..  x = int_val
7c700 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
7c710 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
7c720 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
7c730 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
7c740 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
7c750 7d 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  }.    if (sqlite
7c760 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
7c770 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
7c780 5f 46 4c 4f 41 54 29 0a 09 79 20 3d 20 73 71 6c  _FLOAT)..y = sql
7c790 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
7c7a0 65 20 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  e (argv[1]);.   
7c7b0 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
7c7c0 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
7c7d0 67 76 5b 31 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[1]) == SQLITE
7c7e0 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
7c7f0 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
7c800 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
7c810 6e 74 20 28 61 72 67 76 5b 31 5d 29 3b 0a 09 20  nt (argv[1]);.. 
7c820 20 79 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a   y = int_value;.
7c830 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
7c840 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69  .      {..  sqli
7c850 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7c860 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65  (context);..  re
7c870 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
7c880 20 20 70 20 3d 20 70 6f 77 20 28 78 2c 20 79 29    p = pow (x, y)
7c890 3b 0a 20 20 20 20 69 66 20 28 74 65 73 74 49 6e  ;.    if (testIn
7c8a0 76 61 6c 69 64 46 50 20 28 70 29 29 0a 09 73 71  validFP (p))..sq
7c8b0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
7c8c0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
7c8d0 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72   else..sqlite3_r
7c8e0 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f  esult_double (co
7c8f0 6e 74 65 78 74 2c 20 70 29 3b 0a 7d 0a 0a 73 74  ntext, p);.}..st
7c900 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d  atic void.fnct_m
7c910 61 74 68 5f 73 74 64 64 65 76 5f 73 74 65 70 20  ath_stddev_step 
7c920 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
7c930 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
7c940 61 72 67 63 2c 0a 09 09 20 20 20 20 20 20 20 73  argc,...       s
7c950 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
7c960 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
7c970 75 6e 63 74 69 6f 6e 3a 0a 2f 20 73 74 64 64 65  unction:./ stdde
7c980 76 5f 70 6f 70 28 64 6f 75 62 6c 65 20 58 29 0a  v_pop(double X).
7c990 2f 20 73 74 64 64 65 76 5f 73 61 6d 70 28 64 6f  / stddev_samp(do
7c9a0 75 62 6c 65 20 58 29 0a 2f 20 76 61 72 5f 70 6f  uble X)./ var_po
7c9b0 70 28 64 6f 75 62 6c 65 20 58 29 0a 2f 20 76 61  p(double X)./ va
7c9c0 72 5f 73 61 6d 70 28 64 6f 75 62 6c 65 20 58 29  r_samp(double X)
7c9d0 0a 2f 0a 2f 20 61 67 67 72 65 67 61 74 65 20 66  ././ aggregate f
7c9e0 75 6e 63 74 69 6f 6e 20 2d 20 53 54 45 50 0a 2f  unction - STEP./
7c9f0 0a 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 73  .*/.    struct s
7ca00 74 64 64 65 76 5f 73 74 72 20 2a 70 3b 0a 20 20  tddev_str *p;.  
7ca10 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b    int int_value;
7ca20 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20  .    double x;. 
7ca30 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
7ca40 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
7ca50 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
7ca60 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
7ca70 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
7ca80 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09   SQLITE_FLOAT)..
7ca90 78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  x = sqlite3_valu
7caa0 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 30  e_double (argv[0
7cab0 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20  ]);.    else if 
7cac0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
7cad0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
7cae0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
7caf0 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f  .      {..  int_
7cb00 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  value = sqlite3_
7cb10 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
7cb20 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f  0]);..  x = int_
7cb30 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  value;.      }. 
7cb40 20 20 20 65 6c 73 65 0a 09 72 65 74 75 72 6e 3b     else..return;
7cb50 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
7cb60 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
7cb70 78 74 20 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a  xt (context, siz
7cb80 65 6f 66 20 28 73 74 72 75 63 74 20 73 74 64 64  eof (struct stdd
7cb90 65 76 5f 73 74 72 29 29 3b 0a 20 20 20 20 69 66  ev_str));.    if
7cba0 20 28 21 28 70 2d 3e 63 6c 65 61 6e 65 64 29 29   (!(p->cleaned))
7cbb0 0a 20 20 20 20 20 20 7b 0a 09 20 20 70 2d 3e 63  .      {..  p->c
7cbc0 6c 65 61 6e 65 64 20 3d 20 31 3b 0a 09 20 20 70  leaned = 1;..  p
7cbd0 2d 3e 6d 65 61 6e 20 3d 20 78 3b 0a 09 20 20 70  ->mean = x;..  p
7cbe0 2d 3e 71 75 6f 74 20 3d 20 30 2e 30 3b 0a 09 20  ->quot = 0.0;.. 
7cbf0 20 70 2d 3e 63 6f 75 6e 74 20 3d 20 30 2e 30 3b   p->count = 0.0;
7cc00 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  .      }.    p->
7cc10 63 6f 75 6e 74 20 2b 3d 20 31 2e 30 3b 0a 20 20  count += 1.0;.  
7cc20 20 20 70 2d 3e 71 75 6f 74 20 3d 0a 09 70 2d 3e    p->quot =..p->
7cc30 71 75 6f 74 20 2b 0a 09 28 28 28 70 2d 3e 63 6f  quot +..(((p->co
7cc40 75 6e 74 20 2d 20 31 2e 30 29 20 2a 20 28 28 78  unt - 1.0) * ((x
7cc50 20 2d 20 70 2d 3e 6d 65 61 6e 29 20 2a 20 28 78   - p->mean) * (x
7cc60 20 2d 20 70 2d 3e 6d 65 61 6e 29 29 29 20 2f 20   - p->mean))) / 
7cc70 70 2d 3e 63 6f 75 6e 74 29 3b 0a 20 20 20 20 70  p->count);.    p
7cc80 2d 3e 6d 65 61 6e 20 3d 20 70 2d 3e 6d 65 61 6e  ->mean = p->mean
7cc90 20 2b 20 28 28 78 20 2d 20 70 2d 3e 6d 65 61 6e   + ((x - p->mean
7cca0 29 20 2f 20 70 2d 3e 63 6f 75 6e 74 29 3b 0a 7d  ) / p->count);.}
7ccb0 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
7ccc0 63 74 5f 6d 61 74 68 5f 73 74 64 64 65 76 5f 70  ct_math_stddev_p
7ccd0 6f 70 5f 66 69 6e 61 6c 20 28 73 71 6c 69 74 65  op_final (sqlite
7cce0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
7ccf0 65 78 74 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  ext).{./* SQL fu
7cd00 6e 63 74 69 6f 6e 3a 0a 2f 20 73 74 64 64 65 76  nction:./ stddev
7cd10 5f 70 6f 70 28 64 6f 75 62 6c 65 20 58 29 0a 2f  _pop(double X)./
7cd20 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
7cd30 69 6f 6e 20 2d 20 20 46 49 4e 41 4c 0a 2f 0a 2a  ion -  FINAL./.*
7cd40 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a  /.    double x;.
7cd50 20 20 20 20 73 74 72 75 63 74 20 73 74 64 64 65      struct stdde
7cd60 76 5f 73 74 72 20 2a 70 20 3d 20 73 71 6c 69 74  v_str *p = sqlit
7cd70 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
7cd80 74 65 78 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  text (context, 0
7cd90 29 3b 0a 20 20 20 20 69 66 20 28 21 70 29 0a 20  );.    if (!p). 
7cda0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
7cdb0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
7cdc0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
7cdd0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
7cde0 78 20 3d 20 73 71 72 74 20 28 70 2d 3e 71 75 6f  x = sqrt (p->quo
7cdf0 74 20 2f 20 70 2d 3e 63 6f 75 6e 74 29 3b 0a 20  t / p->count);. 
7ce00 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
7ce10 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78  t_double (contex
7ce20 74 2c 20 78 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  t, x);.}..static
7ce30 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74 68 5f   void.fnct_math_
7ce40 73 74 64 64 65 76 5f 73 61 6d 70 5f 66 69 6e 61  stddev_samp_fina
7ce50 6c 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l (sqlite3_conte
7ce60 78 74 20 2a 20 63 6f 6e 74 65 78 74 29 0a 7b 0a  xt * context).{.
7ce70 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
7ce80 0a 2f 20 73 74 64 64 65 76 5f 73 61 6d 70 28 64  ./ stddev_samp(d
7ce90 6f 75 62 6c 65 20 58 29 0a 2f 20 61 67 67 72 65  ouble X)./ aggre
7cea0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2d 20  gate function - 
7ceb0 20 46 49 4e 41 4c 0a 2f 0a 2a 2f 0a 20 20 20 20   FINAL./.*/.    
7cec0 64 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 73 74  double x;.    st
7ced0 72 75 63 74 20 73 74 64 64 65 76 5f 73 74 72 20  ruct stddev_str 
7cee0 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67  *p = sqlite3_agg
7cef0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 28  regate_context (
7cf00 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20  context, 0);.   
7cf10 20 69 66 20 28 21 70 29 0a 20 20 20 20 20 20 7b   if (!p).      {
7cf20 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
7cf30 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
7cf40 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
7cf50 20 20 20 20 7d 0a 20 20 20 20 78 20 3d 20 73 71      }.    x = sq
7cf60 72 74 20 28 70 2d 3e 71 75 6f 74 20 2f 20 28 70  rt (p->quot / (p
7cf70 2d 3e 63 6f 75 6e 74 20 2d 20 31 2e 30 29 29 3b  ->count - 1.0));
7cf80 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
7cf90 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74  ult_double (cont
7cfa0 65 78 74 2c 20 78 29 3b 0a 7d 0a 0a 73 74 61 74  ext, x);.}..stat
7cfb0 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74  ic void.fnct_mat
7cfc0 68 5f 76 61 72 5f 70 6f 70 5f 66 69 6e 61 6c 20  h_var_pop_final 
7cfd0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
7cfe0 20 2a 20 63 6f 6e 74 65 78 74 29 0a 7b 0a 2f 2a   * context).{./*
7cff0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f   SQL function:./
7d000 20 76 61 72 5f 70 6f 70 28 64 6f 75 62 6c 65 20   var_pop(double 
7d010 58 29 0a 2f 20 61 67 67 72 65 67 61 74 65 20 66  X)./ aggregate f
7d020 75 6e 63 74 69 6f 6e 20 2d 20 20 46 49 4e 41 4c  unction -  FINAL
7d030 0a 2f 0a 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65  ./.*/.    double
7d040 20 78 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73   x;.    struct s
7d050 74 64 64 65 76 5f 73 74 72 20 2a 70 20 3d 20 73  tddev_str *p = s
7d060 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
7d070 5f 63 6f 6e 74 65 78 74 20 28 63 6f 6e 74 65 78  _context (contex
7d080 74 2c 20 30 29 3b 0a 20 20 20 20 69 66 20 28 21  t, 0);.    if (!
7d090 70 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  p).      {..  sq
7d0a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
7d0b0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
7d0c0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
7d0d0 20 20 20 20 78 20 3d 20 70 2d 3e 71 75 6f 74 20      x = p->quot 
7d0e0 2f 20 70 2d 3e 63 6f 75 6e 74 3b 0a 20 20 20 20  / p->count;.    
7d0f0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
7d100 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20  ouble (context, 
7d110 78 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  x);.}..static vo
7d120 69 64 0a 66 6e 63 74 5f 6d 61 74 68 5f 76 61 72  id.fnct_math_var
7d130 5f 73 61 6d 70 5f 66 69 6e 61 6c 20 28 73 71 6c  _samp_final (sql
7d140 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
7d150 6f 6e 74 65 78 74 29 0a 7b 0a 2f 2a 20 53 51 4c  ontext).{./* SQL
7d160 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 76 61 72   function:./ var
7d170 5f 73 61 6d 70 28 64 6f 75 62 6c 65 20 58 29 0a  _samp(double X).
7d180 2f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  / aggregate func
7d190 74 69 6f 6e 20 2d 20 20 46 49 4e 41 4c 0a 2f 0a  tion -  FINAL./.
7d1a0 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b  */.    double x;
7d1b0 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 64 64  .    struct stdd
7d1c0 65 76 5f 73 74 72 20 2a 70 20 3d 20 73 71 6c 69  ev_str *p = sqli
7d1d0 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
7d1e0 6e 74 65 78 74 20 28 63 6f 6e 74 65 78 74 2c 20  ntext (context, 
7d1f0 30 29 3b 0a 20 20 20 20 69 66 20 28 21 70 29 0a  0);.    if (!p).
7d200 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74        {..  sqlit
7d210 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28  e3_result_null (
7d220 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74  context);..  ret
7d230 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
7d240 20 78 20 3d 20 70 2d 3e 71 75 6f 74 20 2f 20 28   x = p->quot / (
7d250 70 2d 3e 63 6f 75 6e 74 20 2d 20 31 2e 30 29 3b  p->count - 1.0);
7d260 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
7d270 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74  ult_double (cont
7d280 65 78 74 2c 20 78 29 3b 0a 7d 0a 0a 73 74 61 74  ext, x);.}..stat
7d290 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74  ic void.fnct_mat
7d2a0 68 5f 72 61 64 69 61 6e 73 20 28 73 71 6c 69 74  h_radians (sqlit
7d2b0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
7d2c0 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
7d2d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
7d2e0 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20   argv).{./* SQL 
7d2f0 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 72 61 64 69  function:./ radi
7d300 61 6e 73 28 64 6f 75 62 6c 65 20 58 29 0a 2f 0a  ans(double X)./.
7d310 2f 20 52 65 74 75 72 6e 73 20 74 68 65 20 61 72  / Returns the ar
7d320 67 75 6d 65 6e 74 20 58 2c 20 63 6f 6e 76 65 72  gument X, conver
7d330 74 65 64 20 66 72 6f 6d 20 64 65 67 72 65 65 73  ted from degrees
7d340 20 74 6f 20 72 61 64 69 61 6e 73 0a 2f 20 6f 72   to radians./ or
7d350 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65 72 72   NULL if any err
7d360 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
7d370 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 69 6e 74  d.*/.    int int
7d380 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64 6f 75 62  _value;.    doub
7d390 6c 65 20 78 3b 0a 20 20 20 20 47 41 49 41 5f 55  le x;.    GAIA_U
7d3a0 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43  NUSED ();../* LC
7d3b0 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a  OV_EXCL_LINE */.
7d3c0 20 20 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f      if (sqlite3_
7d3d0 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
7d3e0 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 46  [0]) == SQLITE_F
7d3f0 4c 4f 41 54 29 0a 09 78 20 3d 20 73 71 6c 69 74  LOAT)..x = sqlit
7d400 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20  e3_value_double 
7d410 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 65  (argv[0]);.    e
7d420 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f  lse if (sqlite3_
7d430 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76  value_type (argv
7d440 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  [0]) == SQLITE_I
7d450 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a  NTEGER).      {.
7d460 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73  .  int_value = s
7d470 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
7d480 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20 78   (argv[0]);..  x
7d490 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20   = int_value;.  
7d4a0 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20      }.    else. 
7d4b0 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
7d4c0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
7d4d0 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
7d4e0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
7d4f0 78 20 3d 20 78 20 2a 20 2e 30 31 37 34 35 33 32  x = x * .0174532
7d500 39 32 35 31 39 39 34 33 32 39 35 38 3b 0a 20 20  925199432958;.  
7d510 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
7d520 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74  _double (context
7d530 2c 20 78 29 3b 0a 7d 0a 0a 0a 73 74 61 74 69 63  , x);.}...static
7d540 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74 68 5f   void.fnct_math_
7d550 72 6f 75 6e 64 20 28 73 71 6c 69 74 65 33 5f 63  round (sqlite3_c
7d560 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
7d570 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
7d580 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
7d590 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
7d5a0 74 69 6f 6e 3a 0a 2f 20 72 6f 75 6e 64 28 64 6f  tion:./ round(do
7d5b0 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74 75  uble X)././ Retu
7d5c0 72 6e 73 20 74 68 65 20 74 68 65 20 6e 65 61 72  rns the the near
7d5d0 65 73 74 20 69 6e 74 65 67 65 72 2c 20 62 75 74  est integer, but
7d5e0 20 72 6f 75 6e 64 20 68 61 6c 66 77 61 79 20 63   round halfway c
7d5f0 61 73 65 73 20 61 77 61 79 20 66 72 6f 6d 20 7a  ases away from z
7d600 65 72 6f 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66  ero./ or NULL if
7d610 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e   any error is en
7d620 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20  countered.*/.   
7d630 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b 0a   int int_value;.
7d640 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20 20      double x;.  
7d650 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
7d660 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
7d670 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
7d680 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
7d690 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20  pe (argv[0]) == 
7d6a0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 20 20  SQLITE_FLOAT).  
7d6b0 20 20 20 20 7b 0a 09 20 20 78 20 3d 20 6d 61 74      {..  x = mat
7d6c0 68 5f 72 6f 75 6e 64 20 28 73 71 6c 69 74 65 33  h_round (sqlite3
7d6d0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61  _value_double (a
7d6e0 72 67 76 5b 30 5d 29 29 3b 0a 09 20 20 73 71 6c  rgv[0]));..  sql
7d6f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
7d700 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b  le (context, x);
7d710 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
7d720 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
7d730 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
7d740 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
7d750 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
7d760 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
7d770 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
7d780 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d  argv[0]);..  x =
7d790 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 09 20 20 78   int_value;..  x
7d7a0 20 3d 20 6d 61 74 68 5f 72 6f 75 6e 64 20 28 78   = math_round (x
7d7b0 29 3b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
7d7c0 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e  sult_double (con
7d7d0 74 65 78 74 2c 20 78 29 3b 0a 20 20 20 20 20 20  text, x);.      
7d7e0 7d 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c 69  }.    else..sqli
7d7f0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7d800 28 63 6f 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73 74  (context);.}..st
7d810 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d  atic void.fnct_m
7d820 61 74 68 5f 73 69 67 6e 20 28 73 71 6c 69 74 65  ath_sign (sqlite
7d830 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
7d840 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
7d850 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
7d860 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66  argv).{./* SQL f
7d870 75 6e 63 74 69 6f 6e 3a 0a 2f 20 73 69 67 6e 28  unction:./ sign(
7d880 64 6f 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65  double X)././ Re
7d890 74 75 72 6e 73 20 74 68 65 20 73 69 67 6e 20 6f  turns the sign o
7d8a0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 61  f the argument a
7d8b0 73 20 2d 31 2c 20 30 2c 20 6f 72 20 31 2c 20 64  s -1, 0, or 1, d
7d8c0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74  epending on whet
7d8d0 68 65 72 20 58 20 69 73 20 6e 65 67 61 74 69 76  her X is negativ
7d8e0 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69  e, zero, or posi
7d8f0 74 69 76 65 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69  tive./ or NULL i
7d900 66 20 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65  f any error is e
7d910 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20  ncountered.*/.  
7d920 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b    int int_value;
7d930 0a 20 20 20 20 64 6f 75 62 6c 65 20 78 3b 0a 20  .    double x;. 
7d940 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
7d950 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
7d960 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
7d970 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
7d980 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
7d990 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29 0a 09   SQLITE_FLOAT)..
7d9a0 78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  x = sqlite3_valu
7d9b0 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76 5b 30  e_double (argv[0
7d9c0 5d 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 20  ]);.    else if 
7d9d0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
7d9e0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d  ype (argv[0]) ==
7d9f0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 29   SQLITE_INTEGER)
7da00 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e 74 5f  .      {..  int_
7da10 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  value = sqlite3_
7da20 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67 76 5b  value_int (argv[
7da30 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e 74 5f  0]);..  x = int_
7da40 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  value;.      }. 
7da50 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
7da60 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
7da70 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29  t_null (context)
7da80 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20  ;..  return;.   
7da90 20 20 20 7d 0a 20 20 20 20 69 66 20 28 78 20 3e     }.    if (x >
7daa0 20 30 2e 30 29 0a 09 73 71 6c 69 74 65 33 5f 72   0.0)..sqlite3_r
7dab0 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f  esult_double (co
7dac0 6e 74 65 78 74 2c 20 31 2e 30 29 3b 0a 20 20 20  ntext, 1.0);.   
7dad0 20 65 6c 73 65 20 69 66 20 28 78 20 3c 20 30 2e   else if (x < 0.
7dae0 30 29 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75  0)..sqlite3_resu
7daf0 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65  lt_double (conte
7db00 78 74 2c 20 2d 31 2e 30 29 3b 0a 20 20 20 20 65  xt, -1.0);.    e
7db10 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72 65 73  lse..sqlite3_res
7db20 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74  ult_double (cont
7db30 65 78 74 2c 20 30 2e 30 29 3b 0a 7d 0a 0a 73 74  ext, 0.0);.}..st
7db40 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d  atic void.fnct_m
7db50 61 74 68 5f 73 69 6e 20 28 73 71 6c 69 74 65 33  ath_sin (sqlite3
7db60 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
7db70 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
7db80 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
7db90 72 67 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75  rgv).{./* SQL fu
7dba0 6e 63 74 69 6f 6e 3a 0a 2f 20 73 69 6e 28 64 6f  nction:./ sin(do
7dbb0 75 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74 75  uble X)././ Retu
7dbc0 72 6e 73 20 74 68 65 20 73 69 6e 65 20 6f 66 20  rns the sine of 
7dbd0 58 2c 20 77 68 65 72 65 20 58 20 69 73 20 67 69  X, where X is gi
7dbe0 76 65 6e 20 69 6e 20 72 61 64 69 61 6e 73 0a 2f  ven in radians./
7dbf0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20   or NULL if any 
7dc00 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
7dc10 65 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20  ered.*/.    int 
7dc20 69 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64  int_value;.    d
7dc30 6f 75 62 6c 65 20 78 3b 0a 20 20 20 20 47 41 49  ouble x;.    GAI
7dc40 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a  A_UNUSED ();../*
7dc50 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20   LCOV_EXCL_LINE 
7dc60 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74  */.    if (sqlit
7dc70 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61  e3_value_type (a
7dc80 72 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54  rgv[0]) == SQLIT
7dc90 45 5f 46 4c 4f 41 54 29 0a 20 20 20 20 20 20 7b  E_FLOAT).      {
7dca0 0a 09 20 20 78 20 3d 20 73 69 6e 20 28 73 71 6c  ..  x = sin (sql
7dcb0 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
7dcc0 65 20 28 61 72 67 76 5b 30 5d 29 29 3b 0a 09 20  e (argv[0]));.. 
7dcd0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
7dce0 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74 2c  double (context,
7dcf0 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   x);.      }.   
7dd00 20 65 6c 73 65 20 69 66 20 28 73 71 6c 69 74 65   else if (sqlite
7dd10 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
7dd20 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[0]) == SQLITE
7dd30 5f 49 4e 54 45 47 45 52 29 0a 20 20 20 20 20 20  _INTEGER).      
7dd40 7b 0a 09 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d  {..  int_value =
7dd50 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
7dd60 6e 74 20 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20  nt (argv[0]);.. 
7dd70 20 78 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b 0a   x = int_value;.
7dd80 09 20 20 78 20 3d 20 73 69 6e 20 28 78 29 3b 0a  .  x = sin (x);.
7dd90 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
7dda0 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78  t_double (contex
7ddb0 74 2c 20 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20  t, x);.      }. 
7ddc0 20 20 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33     else..sqlite3
7ddd0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
7dde0 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  ntext);.}..stati
7ddf0 63 20 76 6f 69 64 0a 66 6e 63 74 5f 6d 61 74 68  c void.fnct_math
7de00 5f 73 71 72 74 20 28 73 71 6c 69 74 65 33 5f 63  _sqrt (sqlite3_c
7de10 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
7de20 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
7de30 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
7de40 76 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63  v).{./* SQL func
7de50 74 69 6f 6e 3a 0a 2f 20 73 71 72 74 28 64 6f 75  tion:./ sqrt(dou
7de60 62 6c 65 20 58 29 0a 2f 0a 2f 20 52 65 74 75 72  ble X)././ Retur
7de70 6e 73 20 74 68 65 20 73 71 75 61 72 65 20 72 6f  ns the square ro
7de80 6f 74 20 6f 66 20 61 20 6e 6f 6e 2d 6e 65 67 61  ot of a non-nega
7de90 74 69 76 65 20 6e 75 6d 62 65 72 20 58 0a 2f 20  tive number X./ 
7dea0 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79 20 65  or NULL if any e
7deb0 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
7dec0 72 65 64 0a 2a 2f 0a 20 20 20 20 69 6e 74 20 69  red.*/.    int i
7ded0 6e 74 5f 76 61 6c 75 65 3b 0a 20 20 20 20 64 6f  nt_value;.    do
7dee0 75 62 6c 65 20 78 3b 0a 20 20 20 20 47 41 49 41  uble x;.    GAIA
7def0 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f 2a 20  _UNUSED ();../* 
7df00 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a  LCOV_EXCL_LINE *
7df10 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69 74 65  /.    if (sqlite
7df20 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28 61 72  3_value_type (ar
7df30 67 76 5b 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45  gv[0]) == SQLITE
7df40 5f 46 4c 4f 41 54 29 0a 20 20 20 20 20 20 7b 0a  _FLOAT).      {.
7df50 09 20 20 78 20 3d 20 73 71 72 74 20 28 73 71 6c  .  x = sqrt (sql
7df60 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
7df70 65 20 28 61 72 67 76 5b 30 5d 29 29 3b 0a 09 20  e (argv[0]));.. 
7df80 20 69 66 20 28 74 65 73 74 49 6e 76 61 6c 69 64   if (testInvalid
7df90 46 50 20 28 78 29 29 0a 09 20 20 20 20 20 20 73  FP (x))..      s
7dfa0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
7dfb0 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20  ll (context);.. 
7dfc0 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
7dfd0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
7dfe0 6c 65 20 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b  le (context, x);
7dff0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
7e000 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61  e if (sqlite3_va
7e010 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
7e020 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) == SQLITE_INT
7e030 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09 20  EGER).      {.. 
7e040 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c   int_value = sql
7e050 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28  ite3_value_int (
7e060 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d  argv[0]);..  x =
7e070 20 69 6e 74 5f 76 61 6c 75 65 3b 0a 09 20 20 78   int_value;..  x
7e080 20 3d 20 73 71 72 74 20 28 78 29 3b 0a 09 20 20   = sqrt (x);..  
7e090 69 66 20 28 74 65 73 74 49 6e 76 61 6c 69 64 46  if (testInvalidF
7e0a0 50 20 28 78 29 29 0a 09 20 20 20 20 20 20 73 71  P (x))..      sq
7e0b0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
7e0c0 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20  l (context);..  
7e0d0 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69  else..      sqli
7e0e0 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
7e0f0 65 20 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b 0a  e (context, x);.
7e100 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
7e110 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
7e120 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
7e130 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
7e140 66 6e 63 74 5f 6d 61 74 68 5f 74 61 6e 20 28 73  fnct_math_tan (s
7e150 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7e160 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
7e170 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
7e180 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
7e190 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
7e1a0 74 61 6e 28 64 6f 75 62 6c 65 20 58 29 0a 2f 0a  tan(double X)./.
7e1b0 2f 20 52 65 74 75 72 6e 73 20 74 68 65 20 74 61  / Returns the ta
7e1c0 6e 67 65 6e 74 20 6f 66 20 58 2c 20 77 68 65 72  ngent of X, wher
7e1d0 65 20 58 20 69 73 20 67 69 76 65 6e 20 69 6e 20  e X is given in 
7e1e0 72 61 64 69 61 6e 73 0a 2f 20 6f 72 20 4e 55 4c  radians./ or NUL
7e1f0 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69  L if any error i
7e200 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f  s encountered.*/
7e210 0a 20 20 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c  .    int int_val
7e220 75 65 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 78  ue;.    double x
7e230 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45  ;.    GAIA_UNUSE
7e240 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45  D ();../* LCOV_E
7e250 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20  XCL_LINE */.    
7e260 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  if (sqlite3_valu
7e270 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29  e_type (argv[0])
7e280 20 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54   == SQLITE_FLOAT
7e290 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 78 20 3d  ).      {..  x =
7e2a0 20 74 61 6e 20 28 73 71 6c 69 74 65 33 5f 76 61   tan (sqlite3_va
7e2b0 6c 75 65 5f 64 6f 75 62 6c 65 20 28 61 72 67 76  lue_double (argv
7e2c0 5b 30 5d 29 29 3b 0a 09 20 20 73 71 6c 69 74 65  [0]));..  sqlite
7e2d0 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20  3_result_double 
7e2e0 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b 0a 20 20  (context, x);.  
7e2f0 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 20 69      }.    else i
7e300 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
7e310 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
7e320 3d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  == SQLITE_INTEGE
7e330 52 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 6e  R).      {..  in
7e340 74 5f 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  t_value = sqlite
7e350 33 5f 76 61 6c 75 65 5f 69 6e 74 20 28 61 72 67  3_value_int (arg
7e360 76 5b 30 5d 29 3b 0a 09 20 20 78 20 3d 20 69 6e  v[0]);..  x = in
7e370 74 5f 76 61 6c 75 65 3b 0a 09 20 20 78 20 3d 20  t_value;..  x = 
7e380 74 61 6e 20 28 78 29 3b 0a 09 20 20 73 71 6c 69  tan (x);..  sqli
7e390 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
7e3a0 65 20 28 63 6f 6e 74 65 78 74 2c 20 78 29 3b 0a  e (context, x);.
7e3b0 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65        }.    else
7e3c0 0a 09 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ..sqlite3_result
7e3d0 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
7e3e0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 65 6e  .}..#endif /* en
7e3f0 64 20 73 75 70 70 6f 72 74 69 6e 67 20 53 51 4c  d supporting SQL
7e400 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 20   math functions 
7e410 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  */..static void.
7e420 66 6e 63 74 5f 47 65 6f 6d 46 72 6f 6d 45 78 69  fnct_GeomFromExi
7e430 66 47 70 73 42 6c 6f 62 20 28 73 71 6c 69 74 65  fGpsBlob (sqlite
7e440 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
7e450 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09  ext, int argc,..
7e460 09 09 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ..  sqlite3_valu
7e470 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20  e ** argv).{./* 
7e480 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20  SQL function:./ 
7e490 47 65 6f 6d 46 72 6f 6d 45 78 69 66 47 70 73 42  GeomFromExifGpsB
7e4a0 6c 6f 62 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64  lob(BLOB encoded
7e4b0 20 69 6d 61 67 65 29 0a 2f 0a 2f 20 72 65 74 75   image)././ retu
7e4c0 72 6e 73 3a 0a 2f 20 61 20 50 4f 49 4e 54 20 67  rns:./ a POINT g
7e4d0 65 6f 6d 65 74 72 79 0a 2f 20 6f 72 20 4e 55 4c  eometry./ or NUL
7e4e0 4c 20 69 66 20 61 6e 79 20 65 72 72 6f 72 20 69  L if any error i
7e4f0 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a 2f  s encountered.*/
7e500 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
7e510 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a 20 20 20 20  ar *p_blob;.    
7e520 69 6e 74 20 6e 5f 62 79 74 65 73 3b 0a 20 20 20  int n_bytes;.   
7e530 20 67 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72   gaiaGeomCollPtr
7e540 20 67 65 6f 6d 3b 0a 20 20 20 20 75 6e 73 69 67   geom;.    unsig
7e550 6e 65 64 20 63 68 61 72 20 2a 67 65 6f 62 6c 6f  ned char *geoblo
7e560 62 3b 0a 20 20 20 20 69 6e 74 20 67 65 6f 73 69  b;.    int geosi
7e570 7a 65 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6c  ze;.    double l
7e580 6f 6e 67 69 74 75 64 65 3b 0a 20 20 20 20 64 6f  ongitude;.    do
7e590 75 62 6c 65 20 6c 61 74 69 74 75 64 65 3b 0a 20  uble latitude;. 
7e5a0 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28     GAIA_UNUSED (
7e5b0 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c  );../* LCOV_EXCL
7e5c0 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20  _LINE */.    if 
7e5d0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
7e5e0 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d  ype (argv[0]) !=
7e5f0 20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20   SQLITE_BLOB).  
7e600 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33      {..  sqlite3
7e610 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f  _result_null (co
7e620 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72  ntext);..  retur
7e630 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70  n;.      }.    p
7e640 5f 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65  _blob = (unsigne
7e650 64 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65  d char *) sqlite
7e660 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72  3_value_blob (ar
7e670 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79  gv[0]);.    n_by
7e680 74 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  tes = sqlite3_va
7e690 6c 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b  lue_bytes (argv[
7e6a0 30 5d 29 3b 0a 20 20 20 20 69 66 20 28 67 61 69  0]);.    if (gai
7e6b0 61 47 65 74 47 70 73 43 6f 6f 72 64 73 20 28 70  aGetGpsCoords (p
7e6c0 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 2c 20  _blob, n_bytes, 
7e6d0 26 6c 6f 6e 67 69 74 75 64 65 2c 20 26 6c 61 74  &longitude, &lat
7e6e0 69 74 75 64 65 29 29 0a 20 20 20 20 20 20 7b 0a  itude)).      {.
7e6f0 09 20 20 67 65 6f 6d 20 3d 20 67 61 69 61 41 6c  .  geom = gaiaAl
7e700 6c 6f 63 47 65 6f 6d 43 6f 6c 6c 20 28 29 3b 0a  locGeomColl ();.
7e710 09 20 20 67 65 6f 6d 2d 3e 53 72 69 64 20 3d 20  .  geom->Srid = 
7e720 34 33 32 36 3b 0a 09 20 20 67 61 69 61 41 64 64  4326;..  gaiaAdd
7e730 50 6f 69 6e 74 54 6f 47 65 6f 6d 43 6f 6c 6c 20  PointToGeomColl 
7e740 28 67 65 6f 6d 2c 20 6c 6f 6e 67 69 74 75 64 65  (geom, longitude
7e750 2c 20 6c 61 74 69 74 75 64 65 29 3b 0a 09 20 20  , latitude);..  
7e760 67 61 69 61 54 6f 53 70 61 74 69 61 4c 69 74 65  gaiaToSpatiaLite
7e770 42 6c 6f 62 57 6b 62 20 28 67 65 6f 6d 2c 20 26  BlobWkb (geom, &
7e780 67 65 6f 62 6c 6f 62 2c 20 26 67 65 6f 73 69 7a  geoblob, &geosiz
7e790 65 29 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47  e);..  gaiaFreeG
7e7a0 65 6f 6d 43 6f 6c 6c 20 28 67 65 6f 6d 29 3b 0a  eomColl (geom);.
7e7b0 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
7e7c0 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
7e7d0 20 67 65 6f 62 6c 6f 62 2c 20 67 65 6f 73 69 7a   geoblob, geosiz
7e7e0 65 2c 20 66 72 65 65 29 3b 0a 20 20 20 20 20 20  e, free);.      
7e7f0 7d 0a 20 20 20 20 65 6c 73 65 0a 09 73 71 6c 69  }.    else..sqli
7e800 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7e810 28 63 6f 6e 74 65 78 74 29 3b 0a 7d 0a 0a 73 74  (context);.}..st
7e820 61 74 69 63 20 76 6f 69 64 0a 62 6c 6f 62 5f 67  atic void.blob_g
7e830 75 65 73 73 20 28 73 71 6c 69 74 65 33 5f 63 6f  uess (sqlite3_co
7e840 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
7e850 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
7e860 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
7e870 2c 0a 09 20 20 20 20 69 6e 74 20 72 65 71 75 65  ,..    int reque
7e880 73 74 29 0a 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e  st).{./* SQL fun
7e890 63 74 69 6f 6e 3a 0a 2f 20 49 73 47 69 66 42 6c  ction:./ IsGifBl
7e8a0 6f 62 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20  ob(BLOB encoded 
7e8b0 69 6d 61 67 65 29 0a 2f 20 49 73 50 6e 67 42 6c  image)./ IsPngBl
7e8c0 6f 62 2c 20 49 73 4a 70 65 67 42 6c 6f 62 2c 20  ob, IsJpegBlob, 
7e8d0 49 73 45 78 69 66 42 6c 6f 62 2c 20 49 73 45 78  IsExifBlob, IsEx
7e8e0 69 66 47 70 73 42 6c 6f 62 2c 20 49 73 54 69 66  ifGpsBlob, IsTif
7e8f0 66 42 6c 6f 62 2c 0a 2f 20 49 73 5a 69 70 42 6c  fBlob,./ IsZipBl
7e900 6f 62 2c 20 49 73 50 64 66 42 6c 6f 62 2c 49 73  ob, IsPdfBlob,Is
7e910 47 65 6f 6d 65 74 72 79 42 6c 6f 62 0a 2f 0a 2f  GeometryBlob././
7e920 20 72 65 74 75 72 6e 73 3a 0a 2f 20 31 20 69 66   returns:./ 1 if
7e930 20 74 68 65 20 72 65 71 75 69 72 65 64 20 42 4c   the required BL
7e940 4f 42 5f 54 59 50 45 20 69 73 20 54 52 55 45 0a  OB_TYPE is TRUE.
7e950 2f 20 30 20 6f 74 68 65 72 77 69 73 65 0a 2f 20  / 0 otherwise./ 
7e960 6f 72 20 2d 31 20 69 66 20 61 6e 79 20 65 72 72  or -1 if any err
7e970 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
7e980 64 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  d.*/.    unsigne
7e990 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b 0a  d char *p_blob;.
7e9a0 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73 3b      int n_bytes;
7e9b0 0a 20 20 20 20 69 6e 74 20 62 6c 6f 62 5f 74 79  .    int blob_ty
7e9c0 70 65 3b 0a 20 20 20 20 47 41 49 41 5f 55 4e 55  pe;.    GAIA_UNU
7e9d0 53 45 44 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56  SED ();../* LCOV
7e9e0 5f 45 58 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20  _EXCL_LINE */.  
7e9f0 20 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76 61    if (sqlite3_va
7ea00 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b 30  lue_type (argv[0
7ea10 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ]) != SQLITE_BLO
7ea20 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71  B).      {..  sq
7ea30 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
7ea40 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29 3b 0a   (context, -1);.
7ea50 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  .  return;.     
7ea60 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d 20   }.    p_blob = 
7ea70 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
7ea80 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ) sqlite3_value_
7ea90 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b 0a  blob (argv[0]);.
7eaa0 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73 71      n_bytes = sq
7eab0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
7eac0 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  s (argv[0]);.   
7ead0 20 62 6c 6f 62 5f 74 79 70 65 20 3d 20 67 61 69   blob_type = gai
7eae0 61 47 75 65 73 73 42 6c 6f 62 54 79 70 65 20 28  aGuessBlobType (
7eaf0 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73 29  p_blob, n_bytes)
7eb00 3b 0a 20 20 20 20 69 66 20 28 72 65 71 75 65 73  ;.    if (reques
7eb10 74 20 3d 3d 20 47 41 49 41 5f 47 45 4f 4d 45 54  t == GAIA_GEOMET
7eb20 52 59 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  RY_BLOB).      {
7eb30 0a 09 20 20 69 66 20 28 62 6c 6f 62 5f 74 79 70  ..  if (blob_typ
7eb40 65 20 3d 3d 20 47 41 49 41 5f 47 45 4f 4d 45 54  e == GAIA_GEOMET
7eb50 52 59 5f 42 4c 4f 42 29 0a 09 20 20 20 20 20 20  RY_BLOB)..      
7eb60 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
7eb70 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b  nt (context, 1);
7eb80 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20  ..  else..      
7eb90 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
7eba0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  nt (context, 0);
7ebb0 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
7ebc0 20 20 7d 0a 20 20 20 20 69 66 20 28 72 65 71 75    }.    if (requ
7ebd0 65 73 74 20 3d 3d 20 47 41 49 41 5f 5a 49 50 5f  est == GAIA_ZIP_
7ebe0 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20  BLOB).      {.. 
7ebf0 20 69 66 20 28 62 6c 6f 62 5f 74 79 70 65 20 3d   if (blob_type =
7ec00 3d 20 47 41 49 41 5f 5a 49 50 5f 42 4c 4f 42 29  = GAIA_ZIP_BLOB)
7ec10 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
7ec20 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
7ec30 65 78 74 2c 20 31 29 3b 0a 09 20 20 65 6c 73 65  ext, 1);..  else
7ec40 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
7ec50 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
7ec60 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75  ext, 0);..  retu
7ec70 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
7ec80 69 66 20 28 72 65 71 75 65 73 74 20 3d 3d 20 47  if (request == G
7ec90 41 49 41 5f 50 44 46 5f 42 4c 4f 42 29 0a 20 20  AIA_PDF_BLOB).  
7eca0 20 20 20 20 7b 0a 09 20 20 69 66 20 28 62 6c 6f      {..  if (blo
7ecb0 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 50  b_type == GAIA_P
7ecc0 44 46 5f 42 4c 4f 42 29 0a 09 20 20 20 20 20 20  DF_BLOB)..      
7ecd0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
7ece0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b  nt (context, 1);
7ecf0 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20  ..  else..      
7ed00 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
7ed10 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  nt (context, 0);
7ed20 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
7ed30 20 20 7d 0a 20 20 20 20 69 66 20 28 72 65 71 75    }.    if (requ
7ed40 65 73 74 20 3d 3d 20 47 41 49 41 5f 54 49 46 46  est == GAIA_TIFF
7ed50 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09  _BLOB).      {..
7ed60 20 20 69 66 20 28 62 6c 6f 62 5f 74 79 70 65 20    if (blob_type 
7ed70 3d 3d 20 47 41 49 41 5f 54 49 46 46 5f 42 4c 4f  == GAIA_TIFF_BLO
7ed80 42 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  B)..      sqlite
7ed90 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
7eda0 6e 74 65 78 74 2c 20 31 29 3b 0a 09 20 20 65 6c  ntext, 1);..  el
7edb0 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  se..      sqlite
7edc0 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
7edd0 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
7ede0 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
7edf0 20 20 69 66 20 28 72 65 71 75 65 73 74 20 3d 3d    if (request ==
7ee00 20 47 41 49 41 5f 47 49 46 5f 42 4c 4f 42 29 0a   GAIA_GIF_BLOB).
7ee10 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20 28 62        {..  if (b
7ee20 6c 6f 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41  lob_type == GAIA
7ee30 5f 47 49 46 5f 42 4c 4f 42 29 0a 09 20 20 20 20  _GIF_BLOB)..    
7ee40 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
7ee50 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 31  _int (context, 1
7ee60 29 3b 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20  );..  else..    
7ee70 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
7ee80 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 30  _int (context, 0
7ee90 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
7eea0 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 72 65      }.    if (re
7eeb0 71 75 65 73 74 20 3d 3d 20 47 41 49 41 5f 50 4e  quest == GAIA_PN
7eec0 47 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a  G_BLOB).      {.
7eed0 09 20 20 69 66 20 28 62 6c 6f 62 5f 74 79 70 65  .  if (blob_type
7eee0 20 3d 3d 20 47 41 49 41 5f 50 4e 47 5f 42 4c 4f   == GAIA_PNG_BLO
7eef0 42 29 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  B)..      sqlite
7ef00 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
7ef10 6e 74 65 78 74 2c 20 31 29 3b 0a 09 20 20 65 6c  ntext, 1);..  el
7ef20 73 65 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65  se..      sqlite
7ef30 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f  3_result_int (co
7ef40 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65  ntext, 0);..  re
7ef50 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
7ef60 20 20 69 66 20 28 72 65 71 75 65 73 74 20 3d 3d    if (request ==
7ef70 20 47 41 49 41 5f 4a 50 45 47 5f 42 4c 4f 42 29   GAIA_JPEG_BLOB)
7ef80 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20 28  .      {..  if (
7ef90 62 6c 6f 62 5f 74 79 70 65 20 3d 3d 20 47 41 49  blob_type == GAI
7efa0 41 5f 4a 50 45 47 5f 42 4c 4f 42 20 7c 7c 20 62  A_JPEG_BLOB || b
7efb0 6c 6f 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41  lob_type == GAIA
7efc0 5f 45 58 49 46 5f 42 4c 4f 42 0a 09 20 20 20 20  _EXIF_BLOB..    
7efd0 20 20 7c 7c 20 62 6c 6f 62 5f 74 79 70 65 20 3d    || blob_type =
7efe0 3d 20 47 41 49 41 5f 45 58 49 46 5f 47 50 53 5f  = GAIA_EXIF_GPS_
7eff0 42 4c 4f 42 29 0a 09 20 20 20 20 20 20 73 71 6c  BLOB)..      sql
7f000 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
7f010 28 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 09 20  (context, 1);.. 
7f020 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71 6c   else..      sql
7f030 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
7f040 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09 20  (context, 0);.. 
7f050 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
7f060 0a 20 20 20 20 69 66 20 28 72 65 71 75 65 73 74  .    if (request
7f070 20 3d 3d 20 47 41 49 41 5f 45 58 49 46 5f 42 4c   == GAIA_EXIF_BL
7f080 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20 69  OB).      {..  i
7f090 66 20 28 62 6c 6f 62 5f 74 79 70 65 20 3d 3d 20  f (blob_type == 
7f0a0 47 41 49 41 5f 45 58 49 46 5f 42 4c 4f 42 20 7c  GAIA_EXIF_BLOB |
7f0b0 7c 20 62 6c 6f 62 5f 74 79 70 65 20 3d 3d 20 47  | blob_type == G
7f0c0 41 49 41 5f 45 58 49 46 5f 47 50 53 5f 42 4c 4f  AIA_EXIF_GPS_BLO
7f0d0 42 29 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c 69  B)..    {...sqli
7f0e0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28  te3_result_int (
7f0f0 63 6f 6e 74 65 78 74 2c 20 31 29 3b 0a 09 20 20  context, 1);..  
7f100 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20    }..  else..   
7f110 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
7f120 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20  t_int (context, 
7f130 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20  0);..  return;. 
7f140 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 72       }.    if (r
7f150 65 71 75 65 73 74 20 3d 3d 20 47 41 49 41 5f 45  equest == GAIA_E
7f160 58 49 46 5f 47 50 53 5f 42 4c 4f 42 29 0a 20 20  XIF_GPS_BLOB).  
7f170 20 20 20 20 7b 0a 09 20 20 69 66 20 28 62 6c 6f      {..  if (blo
7f180 62 5f 74 79 70 65 20 3d 3d 20 47 41 49 41 5f 45  b_type == GAIA_E
7f190 58 49 46 5f 47 50 53 5f 42 4c 4f 42 29 0a 09 20  XIF_GPS_BLOB).. 
7f1a0 20 20 20 7b 0a 09 09 73 71 6c 69 74 65 33 5f 72     {...sqlite3_r
7f1b0 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65  esult_int (conte
7f1c0 78 74 2c 20 31 29 3b 0a 09 20 20 20 20 7d 0a 09  xt, 1);..    }..
7f1d0 20 20 65 6c 73 65 0a 09 20 20 20 20 20 20 73 71    else..      sq
7f1e0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
7f1f0 20 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 09   (context, 0);..
7f200 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
7f210 7d 0a 20 20 20 20 69 66 20 28 72 65 71 75 65 73  }.    if (reques
7f220 74 20 3d 3d 20 47 41 49 41 5f 57 45 42 50 5f 42  t == GAIA_WEBP_B
7f230 4c 4f 42 29 0a 20 20 20 20 20 20 7b 0a 09 20 20  LOB).      {..  
7f240 69 66 20 28 62 6c 6f 62 5f 74 79 70 65 20 3d 3d  if (blob_type ==
7f250 20 47 41 49 41 5f 57 45 42 50 5f 42 4c 4f 42 29   GAIA_WEBP_BLOB)
7f260 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
7f270 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
7f280 65 78 74 2c 20 31 29 3b 0a 09 20 20 65 6c 73 65  ext, 1);..  else
7f290 0a 09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
7f2a0 72 65 73 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74  result_int (cont
7f2b0 65 78 74 2c 20 30 29 3b 0a 09 20 20 72 65 74 75  ext, 0);..  retu
7f2c0 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
7f2d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
7f2e0 6e 74 20 28 63 6f 6e 74 65 78 74 2c 20 2d 31 29  nt (context, -1)
7f2f0 3b 0a 7d 0a 0a 2f 2a 0a 2f 20 74 68 65 20 66 6f  ;.}../*./ the fo
7f300 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llowing function
7f310 73 20 73 69 6d 70 6c 79 20 72 65 61 64 64 72 65  s simply readdre
7f320 73 73 20 74 68 65 20 62 6c 6f 62 5f 67 75 65 73  ss the blob_gues
7f330 73 28 29 0a 2f 20 73 65 74 74 69 6e 67 20 74 68  s()./ setting th
7f340 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65  e appropriate re
7f350 71 75 65 73 74 20 6d 6f 64 65 0a 2a 2f 0a 0a 73  quest mode.*/..s
7f360 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
7f370 49 73 47 65 6f 6d 65 74 72 79 42 6c 6f 62 20 28  IsGeometryBlob (
7f380 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
7f390 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
7f3a0 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
7f3b0 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
7f3c0 20 20 62 6c 6f 62 5f 67 75 65 73 73 20 28 63 6f    blob_guess (co
7f3d0 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
7f3e0 76 2c 20 47 41 49 41 5f 47 45 4f 4d 45 54 52 59  v, GAIA_GEOMETRY
7f3f0 5f 42 4c 4f 42 29 3b 0a 7d 0a 0a 73 74 61 74 69  _BLOB);.}..stati
7f400 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49 73 5a 69  c void.fnct_IsZi
7f410 70 42 6c 6f 62 20 28 73 71 6c 69 74 65 33 5f 63  pBlob (sqlite3_c
7f420 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
7f430 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
7f440 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
7f450 76 29 0a 7b 0a 20 20 20 20 62 6c 6f 62 5f 67 75  v).{.    blob_gu
7f460 65 73 73 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  ess (context, ar
7f470 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 5a  gc, argv, GAIA_Z
7f480 49 50 5f 42 4c 4f 42 29 3b 0a 7d 0a 0a 73 74 61  IP_BLOB);.}..sta
7f490 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49 73  tic void.fnct_Is
7f4a0 50 64 66 42 6c 6f 62 20 28 73 71 6c 69 74 65 33  PdfBlob (sqlite3
7f4b0 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
7f4c0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
7f4d0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
7f4e0 72 67 76 29 0a 7b 0a 20 20 20 20 62 6c 6f 62 5f  rgv).{.    blob_
7f4f0 67 75 65 73 73 20 28 63 6f 6e 74 65 78 74 2c 20  guess (context, 
7f500 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41  argc, argv, GAIA
7f510 5f 50 44 46 5f 42 4c 4f 42 29 3b 0a 7d 0a 0a 73  _PDF_BLOB);.}..s
7f520 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
7f530 49 73 54 69 66 66 42 6c 6f 62 20 28 73 71 6c 69  IsTiffBlob (sqli
7f540 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
7f550 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
7f560 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7f570 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 62 6c  * argv).{.    bl
7f580 6f 62 5f 67 75 65 73 73 20 28 63 6f 6e 74 65 78  ob_guess (contex
7f590 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47  t, argc, argv, G
7f5a0 41 49 41 5f 54 49 46 46 5f 42 4c 4f 42 29 3b 0a  AIA_TIFF_BLOB);.
7f5b0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
7f5c0 6e 63 74 5f 49 73 47 69 66 42 6c 6f 62 20 28 73  nct_IsGifBlob (s
7f5d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7f5e0 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
7f5f0 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
7f600 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20  e ** argv).{.   
7f610 20 62 6c 6f 62 5f 67 75 65 73 73 20 28 63 6f 6e   blob_guess (con
7f620 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
7f630 2c 20 47 41 49 41 5f 47 49 46 5f 42 4c 4f 42 29  , GAIA_GIF_BLOB)
7f640 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
7f650 0a 66 6e 63 74 5f 49 73 50 6e 67 42 6c 6f 62 20  .fnct_IsPngBlob 
7f660 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
7f670 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
7f680 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
7f690 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20  lue ** argv).{. 
7f6a0 20 20 20 62 6c 6f 62 5f 67 75 65 73 73 20 28 63     blob_guess (c
7f6b0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
7f6c0 67 76 2c 20 47 41 49 41 5f 50 4e 47 5f 42 4c 4f  gv, GAIA_PNG_BLO
7f6d0 42 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  B);.}..static vo
7f6e0 69 64 0a 66 6e 63 74 5f 49 73 4a 70 65 67 42 6c  id.fnct_IsJpegBl
7f6f0 6f 62 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ob (sqlite3_cont
7f700 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
7f710 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
7f720 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
7f730 7b 0a 20 20 20 20 62 6c 6f 62 5f 67 75 65 73 73  {.    blob_guess
7f740 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c   (context, argc,
7f750 20 61 72 67 76 2c 20 47 41 49 41 5f 4a 50 45 47   argv, GAIA_JPEG
7f760 5f 42 4c 4f 42 29 3b 0a 7d 0a 0a 73 74 61 74 69  _BLOB);.}..stati
7f770 63 20 76 6f 69 64 0a 66 6e 63 74 5f 49 73 45 78  c void.fnct_IsEx
7f780 69 66 42 6c 6f 62 20 28 73 71 6c 69 74 65 33 5f  ifBlob (sqlite3_
7f790 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
7f7a0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
7f7b0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
7f7c0 67 76 29 0a 7b 0a 20 20 20 20 62 6c 6f 62 5f 67  gv).{.    blob_g
7f7d0 75 65 73 73 20 28 63 6f 6e 74 65 78 74 2c 20 61  uess (context, a
7f7e0 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f  rgc, argv, GAIA_
7f7f0 45 58 49 46 5f 42 4c 4f 42 29 3b 0a 7d 0a 0a 73  EXIF_BLOB);.}..s
7f800 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
7f810 49 73 45 78 69 66 47 70 73 42 6c 6f 62 20 28 73  IsExifGpsBlob (s
7f820 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7f830 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
7f840 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
7f850 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20  e ** argv).{.   
7f860 20 62 6c 6f 62 5f 67 75 65 73 73 20 28 63 6f 6e   blob_guess (con
7f870 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
7f880 2c 20 47 41 49 41 5f 45 58 49 46 5f 47 50 53 5f  , GAIA_EXIF_GPS_
7f890 42 4c 4f 42 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  BLOB);.}..static
7f8a0 20 76 6f 69 64 0a 66 6e 63 74 5f 49 73 57 65 62   void.fnct_IsWeb
7f8b0 50 42 6c 6f 62 20 28 73 71 6c 69 74 65 33 5f 63  PBlob (sqlite3_c
7f8c0 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
7f8d0 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
7f8e0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
7f8f0 76 29 0a 7b 0a 20 20 20 20 62 6c 6f 62 5f 67 75  v).{.    blob_gu
7f900 65 73 73 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  ess (context, ar
7f910 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 57  gc, argv, GAIA_W
7f920 45 42 50 5f 42 4c 4f 42 29 3b 0a 7d 0a 0a 73 74  EBP_BLOB);.}..st
7f930 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 42  atic void.fnct_B
7f940 6c 6f 62 46 72 6f 6d 46 69 6c 65 20 28 73 71 6c  lobFromFile (sql
7f950 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
7f960 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
7f970 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
7f980 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
7f990 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 42 6c  L function:./ Bl
7f9a0 6f 62 46 72 6f 6d 46 69 6c 65 28 54 45 58 54 20  obFromFile(TEXT 
7f9b0 66 69 6c 65 70 61 74 68 29 0a 2f 0a 2f 20 72 65  filepath)././ re
7f9c0 74 75 72 6e 73 3a 0a 2f 20 73 6f 6d 65 20 42 4c  turns:./ some BL
7f9d0 4f 42 20 6f 6e 20 73 75 63 63 65 73 73 0a 2f 20  OB on success./ 
7f9e0 6f 72 20 4e 55 4c 4c 20 6f 6e 20 66 61 69 6c 75  or NULL on failu
7f9f0 72 65 0a 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  re.*/.    unsign
7fa00 65 64 20 63 68 61 72 20 2a 70 5f 62 6c 6f 62 3b  ed char *p_blob;
7fa10 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79 74 65 73  .    int n_bytes
7fa20 3b 0a 20 20 20 20 69 6e 74 20 6d 61 78 5f 62 6c  ;.    int max_bl
7fa30 6f 62 3b 0a 20 20 20 20 69 6e 74 20 72 64 3b 0a  ob;.    int rd;.
7fa40 20 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c      sqlite3 *sql
7fa50 69 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ite = sqlite3_co
7fa60 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20  ntext_db_handle 
7fa70 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 63  (context);.    c
7fa80 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 20  onst char *path 
7fa90 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 46 49 4c 45  = NULL;.    FILE
7faa0 20 2a 69 6e 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20   *in = NULL;.   
7fab0 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b   GAIA_UNUSED ();
7fac0 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c  ../* LCOV_EXCL_L
7fad0 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73  INE */.    if (s
7fae0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
7faf0 65 20 28 61 72 67 76 5b 30 5d 29 20 3d 3d 20 53  e (argv[0]) == S
7fb00 51 4c 49 54 45 5f 54 45 58 54 29 0a 09 70 61 74  QLITE_TEXT)..pat
7fb10 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  h = (const char 
7fb20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
7fb30 5f 74 65 78 74 20 28 61 72 67 76 5b 30 5d 29 3b  _text (argv[0]);
7fb40 0a 20 20 20 20 69 66 20 28 70 61 74 68 20 3d 3d  .    if (path ==
7fb50 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b 0a 09   NULL).      {..
7fb60 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
7fb70 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b  _null (context);
7fb80 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ..  return;.    
7fb90 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62 20 3d    }.    p_blob =
7fba0 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
7fbb0 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *) sqlite3_value
7fbc0 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d 29 3b  _blob (argv[0]);
7fbd0 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d 20 73  .    n_bytes = s
7fbe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
7fbf0 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  es (argv[0]);.  
7fc00 20 20 69 6e 20 3d 20 66 6f 70 65 6e 20 28 70 61    in = fopen (pa
7fc10 74 68 2c 20 22 72 62 22 29 3b 0a 20 20 20 20 69  th, "rb");.    i
7fc20 66 20 28 69 6e 20 3d 3d 20 4e 55 4c 4c 29 0a 20  f (in == NULL). 
7fc30 20 20 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65       {..  sqlite
7fc40 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
7fc50 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75  ontext);..  retu
7fc60 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
7fc70 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a 09 20 20  else.      {..  
7fc80 2f 2a 20 71 75 65 72 79 69 6e 67 20 74 68 65 20  /* querying the 
7fc90 66 69 6c 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 09  file length */..
7fca0 20 20 69 66 20 28 66 73 65 65 6b 20 28 69 6e 2c    if (fseek (in,
7fcb0 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 20 3c 20   0, SEEK_END) < 
7fcc0 30 29 0a 09 20 20 20 20 7b 0a 09 09 73 71 6c 69  0)..    {...sqli
7fcd0 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
7fce0 28 63 6f 6e 74 65 78 74 29 3b 0a 09 09 66 63 6c  (context);...fcl
7fcf0 6f 73 65 20 28 69 6e 29 3b 0a 09 09 72 65 74 75  ose (in);...retu
7fd00 72 6e 3b 0a 09 20 20 20 20 7d 0a 09 20 20 6e 5f  rn;..    }..  n_
7fd10 62 79 74 65 73 20 3d 20 66 74 65 6c 6c 20 28 69  bytes = ftell (i
7fd20 6e 29 3b 0a 09 20 20 6d 61 78 5f 62 6c 6f 62 20  n);..  max_blob 
7fd30 3d 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20  = sqlite3_limit 
7fd40 28 73 71 6c 69 74 65 2c 20 53 51 4c 49 54 45 5f  (sqlite, SQLITE_
7fd50 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 2c 20 2d 31  LIMIT_LENGTH, -1
7fd60 29 3b 0a 09 20 20 69 66 20 28 6e 5f 62 79 74 65  );..  if (n_byte
7fd70 73 20 3e 20 6d 61 78 5f 62 6c 6f 62 29 0a 09 20  s > max_blob).. 
7fd80 20 20 20 7b 0a 09 09 2f 2a 20 74 6f 6f 20 62 69     {.../* too bi
7fd90 67 3b 20 63 61 6e 6e 6f 74 20 62 65 20 73 74 6f  g; cannot be sto
7fda0 72 65 64 20 69 6e 74 6f 20 61 20 42 4c 4f 42 20  red into a BLOB 
7fdb0 2a 2f 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73  */...sqlite3_res
7fdc0 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78  ult_null (contex
7fdd0 74 29 3b 0a 09 09 66 63 6c 6f 73 65 20 28 69 6e  t);...fclose (in
7fde0 29 3b 0a 09 09 72 65 74 75 72 6e 3b 0a 09 20 20  );...return;..  
7fdf0 20 20 7d 0a 09 20 20 72 65 77 69 6e 64 20 28 69    }..  rewind (i
7fe00 6e 29 3b 0a 09 20 20 70 5f 62 6c 6f 62 20 3d 20  n);..  p_blob = 
7fe10 6d 61 6c 6c 6f 63 20 28 6e 5f 62 79 74 65 73 29  malloc (n_bytes)
7fe20 3b 0a 09 20 20 2f 2a 20 61 74 74 65 6d 70 74 69  ;..  /* attempti
7fe30 6e 67 20 74 6f 20 6c 6f 61 64 20 74 68 65 20 42  ng to load the B
7fe40 4c 4f 42 20 66 72 6f 6d 20 74 68 65 20 66 69 6c  LOB from the fil
7fe50 65 20 2a 2f 0a 09 20 20 72 64 20 3d 20 66 72 65  e */..  rd = fre
7fe60 61 64 20 28 70 5f 62 6c 6f 62 2c 20 31 2c 20 6e  ad (p_blob, 1, n
7fe70 5f 62 79 74 65 73 2c 20 69 6e 29 3b 0a 09 20 20  _bytes, in);..  
7fe80 66 63 6c 6f 73 65 20 28 69 6e 29 3b 0a 09 20 20  fclose (in);..  
7fe90 69 66 20 28 72 64 20 21 3d 20 6e 5f 62 79 74 65  if (rd != n_byte
7fea0 73 29 0a 09 20 20 20 20 7b 0a 09 09 2f 2a 20 72  s)..    {.../* r
7feb0 65 61 64 20 65 72 72 6f 72 20 2a 2f 0a 09 09 66  ead error */...f
7fec0 72 65 65 20 28 70 5f 62 6c 6f 62 29 3b 0a 09 09  ree (p_blob);...
7fed0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
7fee0 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09  ull (context);..
7fef0 09 72 65 74 75 72 6e 3b 0a 09 20 20 20 20 7d 0a  .return;..    }.
7ff00 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
7ff10 74 5f 62 6c 6f 62 20 28 63 6f 6e 74 65 78 74 2c  t_blob (context,
7ff20 20 70 5f 62 6c 6f 62 2c 20 6e 5f 62 79 74 65 73   p_blob, n_bytes
7ff30 2c 20 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d  , free);.      }
7ff40 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
7ff50 66 6e 63 74 5f 42 6c 6f 62 54 6f 46 69 6c 65 20  fnct_BlobToFile 
7ff60 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
7ff70 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
7ff80 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
7ff90 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f  lue ** argv).{./
7ffa0 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a  * SQL function:.
7ffb0 2f 20 42 6c 6f 62 54 6f 46 69 6c 65 28 42 4c 4f  / BlobToFile(BLO
7ffc0 42 20 70 61 79 6c 6f 61 64 2c 20 54 45 58 54 20  B payload, TEXT 
7ffd0 66 69 6c 65 70 61 74 68 29 0a 2f 0a 2f 20 72 65  filepath)././ re
7ffe0 74 75 72 6e 73 3a 0a 2f 20 31 20 6f 6e 20 73 75  turns:./ 1 on su
7fff0 63 63 65 73 73 0a 2f 20 6f 72 20 30 20 6f 6e 20  ccess./ or 0 on 
80000 66 61 69 6c 75 72 65 0a 2a 2f 0a 20 20 20 20 75  failure.*/.    u
80010 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
80020 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
80030 62 79 74 65 73 3b 0a 20 20 20 20 63 6f 6e 73 74  bytes;.    const
80040 20 63 68 61 72 20 2a 70 61 74 68 20 3d 20 4e 55   char *path = NU
80050 4c 4c 3b 0a 20 20 20 20 46 49 4c 45 20 2a 6f 75  LL;.    FILE *ou
80060 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  t = NULL;.    in
80070 74 20 72 65 74 20 3d 20 31 3b 0a 20 20 20 20 47  t ret = 1;.    G
80080 41 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09  AIA_UNUSED ();..
80090 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e  /* LCOV_EXCL_LIN
800a0 45 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c  E */.    if (sql
800b0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20  ite3_value_type 
800c0 28 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c  (argv[0]) != SQL
800d0 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20  ITE_BLOB).      
800e0 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73  {..  sqlite3_res
800f0 75 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74  ult_int (context
80100 2c 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b  , 0);..  return;
80110 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 69 66 20  .      }.    if 
80120 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  (sqlite3_value_t
80130 79 70 65 20 28 61 72 67 76 5b 31 5d 29 20 3d 3d  ype (argv[1]) ==
80140 20 53 51 4c 49 54 45 5f 54 45 58 54 29 0a 09 70   SQLITE_TEXT)..p
80150 61 74 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ath = (const cha
80160 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
80170 75 65 5f 74 65 78 74 20 28 61 72 67 76 5b 31 5d  ue_text (argv[1]
80180 29 3b 0a 20 20 20 20 69 66 20 28 70 61 74 68 20  );.    if (path 
80190 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 20 20 7b  == NULL).      {
801a0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
801b0 6c 74 5f 69 6e 74 20 28 63 6f 6e 74 65 78 74 2c  lt_int (context,
801c0 20 30 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a   0);..  return;.
801d0 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c        }.    p_bl
801e0 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ob = (unsigned c
801f0 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76  har *) sqlite3_v
80200 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b  alue_blob (argv[
80210 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73  0]);.    n_bytes
80220 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
80230 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29  _bytes (argv[0])
80240 3b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65  ;.    out = fope
80250 6e 20 28 70 61 74 68 2c 20 22 77 62 22 29 3b 0a  n (path, "wb");.
80260 20 20 20 20 69 66 20 28 6f 75 74 20 3d 3d 20 4e      if (out == N
80270 55 4c 4c 29 0a 09 72 65 74 20 3d 20 30 3b 0a 20  ULL)..ret = 0;. 
80280 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 7b 0a     else.      {.
80290 09 20 20 2f 2a 20 65 78 70 6f 72 74 69 6e 67 20  .  /* exporting 
802a0 74 68 65 20 42 4c 4f 42 20 69 6e 74 6f 20 74 68  the BLOB into th
802b0 65 20 66 69 6c 65 20 2a 2f 0a 09 20 20 69 6e 74  e file */..  int
802c0 20 77 72 20 3d 20 66 77 72 69 74 65 20 28 70 5f   wr = fwrite (p_
802d0 62 6c 6f 62 2c 20 31 2c 20 6e 5f 62 79 74 65 73  blob, 1, n_bytes
802e0 2c 20 6f 75 74 29 3b 0a 09 20 20 69 66 20 28 77  , out);..  if (w
802f0 72 20 21 3d 20 6e 5f 62 79 74 65 73 29 0a 09 20  r != n_bytes).. 
80300 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 09 20       ret = 0;.. 
80310 20 66 63 6c 6f 73 65 20 28 6f 75 74 29 3b 0a 20   fclose (out);. 
80320 20 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74       }.    sqlit
80330 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 28 63  e3_result_int (c
80340 6f 6e 74 65 78 74 2c 20 72 65 74 29 3b 0a 7d 0a  ontext, ret);.}.
80350 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
80360 74 5f 47 65 6f 64 65 73 69 63 4c 65 6e 67 74 68  t_GeodesicLength
80370 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
80380 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
80390 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
803a0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
803b0 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 3a  /* SQL function:
803c0 0a 2f 20 47 65 6f 64 65 73 69 63 4c 65 6e 67 74  ./ GeodesicLengt
803d0 68 28 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 47  h(BLOB encoded G
803e0 45 4f 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f  EOMETRYCOLLECTIO
803f0 4e 29 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 20  N)././ returns  
80400 74 68 65 20 74 6f 74 61 6c 20 47 65 6f 64 65 73  the total Geodes
80410 69 63 20 6c 65 6e 67 74 68 20 66 6f 72 20 63 75  ic length for cu
80420 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79 20 0a  rrent geometry .
80430 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61 6e 79  / or NULL if any
80440 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
80450 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75 6e 73  tered.*/.    uns
80460 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f 62 6c  igned char *p_bl
80470 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f 62 79  ob;.    int n_by
80480 74 65 73 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  tes;.    double 
80490 6c 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 6c 65  l;.    double le
804a0 6e 67 74 68 20 3d 20 30 2e 30 3b 0a 20 20 20 20  ngth = 0.0;.    
804b0 64 6f 75 62 6c 65 20 61 3b 0a 20 20 20 20 64 6f  double a;.    do
804c0 75 62 6c 65 20 62 3b 0a 20 20 20 20 64 6f 75 62  uble b;.    doub
804d0 6c 65 20 72 66 3b 0a 20 20 20 20 67 61 69 61 47  le rf;.    gaiaG
804e0 65 6f 6d 43 6f 6c 6c 50 74 72 20 67 65 6f 20 3d  eomCollPtr geo =
804f0 20 4e 55 4c 4c 3b 0a 20 20 20 20 67 61 69 61 4c   NULL;.    gaiaL
80500 69 6e 65 73 74 72 69 6e 67 50 74 72 20 6c 69 6e  inestringPtr lin
80510 65 3b 0a 20 20 20 20 67 61 69 61 50 6f 6c 79 67  e;.    gaiaPolyg
80520 6f 6e 50 74 72 20 70 6f 6c 79 67 3b 0a 20 20 20  onPtr polyg;.   
80530 20 67 61 69 61 52 69 6e 67 50 74 72 20 72 69 6e   gaiaRingPtr rin
80540 67 3b 0a 20 20 20 20 69 6e 74 20 69 62 3b 0a 20  g;.    int ib;. 
80550 20 20 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69     sqlite3 *sqli
80560 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  te = sqlite3_con
80570 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 28  text_db_handle (
80580 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 47 41  context);.    GA
80590 49 41 5f 55 4e 55 53 45 44 20 28 29 3b 09 09 2f  IA_UNUSED ();../
805a0 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f 4c 49 4e 45  * LCOV_EXCL_LINE
805b0 20 2a 2f 0a 20 20 20 20 69 66 20 28 73 71 6c 69   */.    if (sqli
805c0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 20 28  te3_value_type (
805d0 61 72 67 76 5b 30 5d 29 20 21 3d 20 53 51 4c 49  argv[0]) != SQLI
805e0 54 45 5f 42 4c 4f 42 29 0a 20 20 20 20 20 20 7b  TE_BLOB).      {
805f0 0a 09 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
80600 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
80610 29 3b 0a 09 20 20 72 65 74 75 72 6e 3b 0a 20 20  );..  return;.  
80620 20 20 20 20 7d 0a 20 20 20 20 70 5f 62 6c 6f 62      }.    p_blob
80630 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
80640 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 76 61 6c  r *) sqlite3_val
80650 75 65 5f 62 6c 6f 62 20 28 61 72 67 76 5b 30 5d  ue_blob (argv[0]
80660 29 3b 0a 20 20 20 20 6e 5f 62 79 74 65 73 20 3d  );.    n_bytes =
80670 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
80680 79 74 65 73 20 28 61 72 67 76 5b 30 5d 29 3b 0a  ytes (argv[0]);.
80690 20 20 20 20 67 65 6f 20 3d 20 67 61 69 61 46 72      geo = gaiaFr
806a0 6f 6d 53 70 61 74 69 61 4c 69 74 65 42 6c 6f 62  omSpatiaLiteBlob
806b0 57 6b 62 20 28 70 5f 62 6c 6f 62 2c 20 6e 5f 62  Wkb (p_blob, n_b
806c0 79 74 65 73 29 3b 0a 20 20 20 20 69 66 20 28 21  ytes);.    if (!
806d0 67 65 6f 29 0a 09 73 71 6c 69 74 65 33 5f 72 65  geo)..sqlite3_re
806e0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
806f0 78 74 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20  xt);.    else.  
80700 20 20 20 20 7b 0a 09 20 20 69 66 20 28 67 65 74      {..  if (get
80710 5f 65 6c 6c 69 70 73 65 5f 70 61 72 61 6d 73 20  _ellipse_params 
80720 28 73 71 6c 69 74 65 2c 20 67 65 6f 2d 3e 53 72  (sqlite, geo->Sr
80730 69 64 2c 20 26 61 2c 20 26 62 2c 20 26 72 66 29  id, &a, &b, &rf)
80740 29 0a 09 20 20 20 20 7b 0a 09 09 6c 69 6e 65 20  )..    {...line 
80750 3d 20 67 65 6f 2d 3e 46 69 72 73 74 4c 69 6e 65  = geo->FirstLine
80760 73 74 72 69 6e 67 3b 0a 09 09 77 68 69 6c 65 20  string;...while 
80770 28 6c 69 6e 65 29 0a 09 09 20 20 7b 0a 09 09 20  (line)...  {... 
80780 20 20 20 20 20 2f 2a 20 4c 69 6e 65 73 74 72 69       /* Linestri
80790 6e 67 73 20 2a 2f 0a 09 09 20 20 20 20 20 20 6c  ngs */...      l
807a0 20 3d 20 67 61 69 61 47 65 6f 64 65 73 69 63 54   = gaiaGeodesicT
807b0 6f 74 61 6c 4c 65 6e 67 74 68 20 28 61 2c 20 62  otalLength (a, b
807c0 2c 20 72 66 2c 0a 09 09 09 09 09 09 20 20 20 6c  , rf,.......   l
807d0 69 6e 65 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d 6f  ine->DimensionMo
807e0 64 65 6c 2c 0a 09 09 09 09 09 09 20 20 20 6c 69  del,.......   li
807f0 6e 65 2d 3e 43 6f 6f 72 64 73 2c 20 6c 69 6e 65  ne->Coords, line
80800 2d 3e 50 6f 69 6e 74 73 29 3b 0a 09 09 20 20 20  ->Points);...   
80810 20 20 20 69 66 20 28 6c 20 3c 20 30 2e 30 29 0a     if (l < 0.0).
80820 09 09 09 7b 0a 09 09 09 20 20 20 20 6c 65 6e 67  ...{....    leng
80830 74 68 20 3d 20 2d 31 2e 30 3b 0a 09 09 09 20 20  th = -1.0;....  
80840 20 20 62 72 65 61 6b 3b 0a 09 09 09 7d 0a 09 09    break;....}...
80850 20 20 20 20 20 20 6c 65 6e 67 74 68 20 2b 3d 20        length += 
80860 6c 3b 0a 09 09 20 20 20 20 20 20 6c 69 6e 65 20  l;...      line 
80870 3d 20 6c 69 6e 65 2d 3e 4e 65 78 74 3b 0a 09 09  = line->Next;...
80880 20 20 7d 0a 09 09 69 66 20 28 6c 65 6e 67 74 68    }...if (length
80890 20 3e 3d 20 30 29 0a 09 09 20 20 7b 0a 09 09 20   >= 0)...  {... 
808a0 20 20 20 20 20 2f 2a 20 50 6f 6c 79 67 6f 6e 73       /* Polygons
808b0 20 2a 2f 0a 09 09 20 20 20 20 20 20 70 6f 6c 79   */...      poly
808c0 67 20 3d 20 67 65 6f 2d 3e 46 69 72 73 74 50 6f  g = geo->FirstPo
808d0 6c 79 67 6f 6e 3b 0a 09 09 20 20 20 20 20 20 77  lygon;...      w
808e0 68 69 6c 65 20 28 70 6f 6c 79 67 29 0a 09 09 09  hile (polyg)....
808f0 7b 0a 09 09 09 20 20 20 20 2f 2a 20 65 78 74 65  {....    /* exte
80900 72 69 6f 72 20 52 69 6e 67 20 2a 2f 0a 09 09 09  rior Ring */....
80910 20 20 20 20 72 69 6e 67 20 3d 20 70 6f 6c 79 67      ring = polyg
80920 2d 3e 45 78 74 65 72 69 6f 72 3b 0a 09 09 09 20  ->Exterior;.... 
80930 20 20 20 6c 20 3d 20 67 61 69 61 47 65 6f 64 65     l = gaiaGeode
80940 73 69 63 54 6f 74 61 6c 4c 65 6e 67 74 68 20 28  sicTotalLength (
80950 61 2c 20 62 2c 20 72 66 2c 0a 09 09 09 09 09 09  a, b, rf,.......
80960 09 20 72 69 6e 67 2d 3e 44 69 6d 65 6e 73 69 6f  . ring->Dimensio
80970 6e 4d 6f 64 65 6c 2c 0a 09 09 09 09 09 09 09 20  nModel,........ 
80980 72 69 6e 67 2d 3e 43 6f 6f 72 64 73 2c 0a 09 09  ring->Coords,...
80990 09 09 09 09 09 20 72 69 6e 67 2d 3e 50 6f 69 6e  ..... ring->Poin
809a0 74 73 29 3b 0a 09 09 09 20 20 20 20 69 66 20 28  ts);....    if (
809b0 6c 20 3c 20 30 2e 30 29 0a 09 09 09 20 20 20 20  l < 0.0)....    
809c0 20 20 7b 0a 09 09 09 09 20 20 6c 65 6e 67 74 68    {.....  length
809d0 20 3d 20 2d 31 2e 30 3b 0a 09 09 09 09 20 20 62   = -1.0;.....  b
809e0 72 65 61 6b 3b 0a 09 09 09 20 20 20 20 20 20 7d  reak;....      }
809f0 0a 09 09 09 20 20 20 20 6c 65 6e 67 74 68 20 2b  ....    length +
80a00 3d 20 6c 3b 0a 09 09 09 20 20 20 20 66 6f 72 20  = l;....    for 
80a10 28 69 62 20 3d 20 30 3b 20 69 62 20 3c 20 70 6f  (ib = 0; ib < po
80a20 6c 79 67 2d 3e 4e 75 6d 49 6e 74 65 72 69 6f 72  lyg->NumInterior
80a30 73 3b 20 69 62 2b 2b 29 0a 09 09 09 20 20 20 20  s; ib++)....    
80a40 20 20 7b 0a 09 09 09 09 20 20 2f 2a 20 69 6e 74    {.....  /* int
80a50 65 72 69 6f 72 20 52 69 6e 67 73 20 2a 2f 0a 09  erior Rings */..
80a60 09 09 09 20 20 72 69 6e 67 20 3d 20 70 6f 6c 79  ...  ring = poly
80a70 67 2d 3e 49 6e 74 65 72 69 6f 72 73 20 2b 20 69  g->Interiors + i
80a80 62 3b 0a 09 09 09 09 20 20 6c 20 3d 20 67 61 69  b;.....  l = gai
80a90 61 47 65 6f 64 65 73 69 63 54 6f 74 61 6c 4c 65  aGeodesicTotalLe
80aa0 6e 67 74 68 20 28 61 2c 20 62 2c 20 72 66 2c 0a  ngth (a, b, rf,.
80ab0 09 09 09 09 09 09 09 20 20 20 20 20 20 20 72 69  .......       ri
80ac0 6e 67 2d 3e 0a 09 09 09 09 09 09 09 20 20 20 20  ng->........    
80ad0 20 20 20 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65     DimensionMode
80ae0 6c 2c 0a 09 09 09 09 09 09 09 20 20 20 20 20 20  l,........      
80af0 20 72 69 6e 67 2d 3e 43 6f 6f 72 64 73 2c 0a 09   ring->Coords,..
80b00 09 09 09 09 09 09 20 20 20 20 20 20 20 72 69 6e  ......       rin
80b10 67 2d 3e 50 6f 69 6e 74 73 29 3b 0a 09 09 09 09  g->Points);.....
80b20 20 20 69 66 20 28 6c 20 3c 20 30 2e 30 29 0a 09    if (l < 0.0)..
80b30 09 09 09 20 20 20 20 7b 0a 09 09 09 09 09 6c 65  ...    {......le
80b40 6e 67 74 68 20 3d 20 2d 31 2e 30 3b 0a 09 09 09  ngth = -1.0;....
80b50 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 20 20 20  ..break;.....   
80b60 20 7d 0a 09 09 09 09 20 20 6c 65 6e 67 74 68 20   }.....  length 
80b70 2b 3d 20 6c 3b 0a 09 09 09 20 20 20 20 20 20 7d  += l;....      }
80b80 0a 09 09 09 20 20 20 20 69 66 20 28 6c 65 6e 67  ....    if (leng
80b90 74 68 20 3c 20 30 2e 30 29 0a 09 09 09 09 62 72  th < 0.0).....br
80ba0 65 61 6b 3b 0a 09 09 09 20 20 20 20 70 6f 6c 79  eak;....    poly
80bb0 67 20 3d 20 70 6f 6c 79 67 2d 3e 4e 65 78 74 3b  g = polyg->Next;
80bc0 0a 09 09 09 7d 0a 09 09 20 20 7d 0a 09 09 69 66  ....}...  }...if
80bd0 20 28 6c 65 6e 67 74 68 20 3c 20 30 2e 30 29 0a   (length < 0.0).
80be0 09 09 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ..    sqlite3_re
80bf0 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65  sult_null (conte
80c00 78 74 29 3b 0a 09 09 65 6c 73 65 0a 09 09 20 20  xt);...else...  
80c10 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
80c20 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74 65 78 74  _double (context
80c30 2c 20 6c 65 6e 67 74 68 29 3b 0a 09 20 20 20 20  , length);..    
80c40 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20 20 20 20  }..  else..     
80c50 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
80c60 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a  null (context);.
80c70 09 20 20 67 61 69 61 46 72 65 65 47 65 6f 6d 43  .  gaiaFreeGeomC
80c80 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20 20 20 20  oll (geo);.     
80c90 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69   }.}..static voi
80ca0 64 0a 66 6e 63 74 5f 47 72 65 61 74 43 69 72 63  d.fnct_GreatCirc
80cb0 6c 65 4c 65 6e 67 74 68 20 28 73 71 6c 69 74 65  leLength (sqlite
80cc0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
80cd0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 0a 09  ext, int argc,..
80ce0 09 09 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  ..sqlite3_value 
80cf0 2a 2a 20 61 72 67 76 29 0a 7b 0a 2f 2a 20 53 51  ** argv).{./* SQ
80d00 4c 20 66 75 6e 63 74 69 6f 6e 3a 0a 2f 20 47 72  L function:./ Gr
80d10 65 61 74 43 69 72 63 6c 65 4c 65 6e 67 74 68 28  eatCircleLength(
80d20 42 4c 4f 42 20 65 6e 63 6f 64 65 64 20 47 45 4f  BLOB encoded GEO
80d30 4d 45 54 52 59 43 4f 4c 4c 45 43 54 49 4f 4e 29  METRYCOLLECTION)
80d40 0a 2f 0a 2f 20 72 65 74 75 72 6e 73 20 20 74 68  ././ returns  th
80d50 65 20 74 6f 74 61 6c 20 47 72 65 61 74 20 43 69  e total Great Ci
80d60 72 63 6c 65 20 6c 65 6e 67 74 68 20 66 6f 72 20  rcle length for 
80d70 63 75 72 72 65 6e 74 20 67 65 6f 6d 65 74 72 79  current geometry
80d80 20 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20 61   ./ or NULL if a
80d90 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  ny error is enco
80da0 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20 75  untered.*/.    u
80db0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 5f  nsigned char *p_
80dc0 62 6c 6f 62 3b 0a 20 20 20 20 69 6e 74 20 6e 5f  blob;.    int n_
80dd0 62 79 74 65 73 3b 0a 20 20 20 20 64 6f 75 62 6c  bytes;.    doubl
80de0 65 20 6c 65 6e 67 74 68 20 3d 20 30 2e 30 3b 0a  e length = 0.0;.
80df0 20 20 20 20 64 6f 75 62 6c 65 20 61 3b 0a 20 20      double a;.  
80e00 20 20 64 6f 75 62 6c 65 20 62 3b 0a 20 20 20 20    double b;.    
80e10 64 6f 75 62 6c 65 20 72 66 3b 0a 20 20 20 20 67  double rf;.    g
80e20 61 69 61 47 65 6f 6d 43 6f 6c 6c 50 74 72 20 67  aiaGeomCollPtr g
80e30 65 6f 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 67  eo = NULL;.    g
80e40 61 69 61 4c 69 6e 65 73 74 72 69 6e 67 50 74 72  aiaLinestringPtr
80e50 20 6c 69 6e 65 3b 0a 20 20 20 20 67 61 69 61 50   line;.    gaiaP
80e60 6f 6c 79 67 6f 6e 50 74 72 20 70 6f 6c 79 67 3b  olygonPtr polyg;
80e70 0a 20 20 20 20 67 61 69 61 52 69 6e 67 50 74 72  .    gaiaRingPtr
80e80 20 72 69 6e 67 3b 0a 20 20 20 20 69 6e 74 20 69   ring;.    int i
80e90 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  b;.    sqlite3 *
80ea0 73 71 6c 69 74 65 20 3d 20 73 71 6c 69 74 65 33  sqlite = sqlite3
80eb0 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
80ec0 6c 65 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  le (context);.  
80ed0 20 20 47 41 49 41 5f 55 4e 55 53 45 44 20 28 29    GAIA_UNUSED ()
80ee0 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58 43 4c 5f  ;../* LCOV_EXCL_
80ef0 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69 66 20 28  LINE */.    if (
80f00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
80f10 70 65 20 28 61 72 67 76 5b 30 5d 29 20 21 3d 20  pe (argv[0]) != 
80f20 53 51 4c 49 54 45 5f 42 4c 4f 42 29 0a 20 20 20  SQLITE_BLOB).   
80f30 20 20 20 7b 0a 09 20 20 73 71 6c 69 74 65 33 5f     {..  sqlite3_
80f40 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e  result_null (con
80f50 74 65 78 74 29 3b 0a 09 20 20 72 65 74 75 72 6e  text);..  return
80f60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 70 5f  ;.      }.    p_
80f70 62 6c 6f 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  blob = (unsigned
80f80 20 63 68 61 72 20 2a 29 20 73 71 6c 69 74 65 33   char *) sqlite3
80f90 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 28 61 72 67  _value_blob (arg
80fa0 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 5f 62 79 74  v[0]);.    n_byt
80fb0 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  es = sqlite3_val
80fc0 75 65 5f 62 79 74 65 73 20 28 61 72 67 76 5b 30  ue_bytes (argv[0
80fd0 5d 29 3b 0a 20 20 20 20 67 65 6f 20 3d 20 67 61  ]);.    geo = ga
80fe0 69 61 46 72 6f 6d 53 70 61 74 69 61 4c 69 74 65  iaFromSpatiaLite
80ff0 42 6c 6f 62 57 6b 62 20 28 70 5f 62 6c 6f 62 2c  BlobWkb (p_blob,
81000 20 6e 5f 62 79 74 65 73 29 3b 0a 20 20 20 20 69   n_bytes);.    i
81010 66 20 28 21 67 65 6f 29 0a 09 73 71 6c 69 74 65  f (!geo)..sqlite
81020 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 28 63  3_result_null (c
81030 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 65 6c 73  ontext);.    els
81040 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 69 66 20  e.      {..  if 
81050 28 67 65 74 5f 65 6c 6c 69 70 73 65 5f 70 61 72  (get_ellipse_par
81060 61 6d 73 20 28 73 71 6c 69 74 65 2c 20 67 65 6f  ams (sqlite, geo
81070 2d 3e 53 72 69 64 2c 20 26 61 2c 20 26 62 2c 20  ->Srid, &a, &b, 
81080 26 72 66 29 29 0a 09 20 20 20 20 7b 0a 09 09 6c  &rf))..    {...l
81090 69 6e 65 20 3d 20 67 65 6f 2d 3e 46 69 72 73 74  ine = geo->First
810a0 4c 69 6e 65 73 74 72 69 6e 67 3b 0a 09 09 77 68  Linestring;...wh
810b0 69 6c 65 20 28 6c 69 6e 65 29 0a 09 09 20 20 7b  ile (line)...  {
810c0 0a 09 09 20 20 20 20 20 20 2f 2a 20 4c 69 6e 65  ...      /* Line
810d0 73 74 72 69 6e 67 73 20 2a 2f 0a 09 09 20 20 20  strings */...   
810e0 20 20 20 6c 65 6e 67 74 68 20 2b 3d 0a 09 09 09     length +=....
810f0 20 20 67 61 69 61 47 72 65 61 74 43 69 72 63 6c    gaiaGreatCircl
81100 65 54 6f 74 61 6c 4c 65 6e 67 74 68 20 28 61 2c  eTotalLength (a,
81110 20 62 2c 0a 09 09 09 09 09 09 20 20 20 20 20 20   b,.......      
81120 6c 69 6e 65 2d 3e 44 69 6d 65 6e 73 69 6f 6e 4d  line->DimensionM
81130 6f 64 65 6c 2c 0a 09 09 09 09 09 09 20 20 20 20  odel,.......    
81140 20 20 6c 69 6e 65 2d 3e 43 6f 6f 72 64 73 2c 0a    line->Coords,.
81150 09 09 09 09 09 09 20 20 20 20 20 20 6c 69 6e 65  ......      line
81160 2d 3e 50 6f 69 6e 74 73 29 3b 0a 09 09 20 20 20  ->Points);...   
81170 20 20 20 6c 69 6e 65 20 3d 20 6c 69 6e 65 2d 3e     line = line->
81180 4e 65 78 74 3b 0a 09 09 20 20 7d 0a 09 09 69 66  Next;...  }...if
81190 20 28 6c 65 6e 67 74 68 20 3e 3d 20 30 29 0a 09   (length >= 0)..
811a0 09 20 20 7b 0a 09 09 20 20 20 20 20 20 2f 2a 20  .  {...      /* 
811b0 50 6f 6c 79 67 6f 6e 73 20 2a 2f 0a 09 09 20 20  Polygons */...  
811c0 20 20 20 20 70 6f 6c 79 67 20 3d 20 67 65 6f 2d      polyg = geo-
811d0 3e 46 69 72 73 74 50 6f 6c 79 67 6f 6e 3b 0a 09  >FirstPolygon;..
811e0 09 20 20 20 20 20 20 77 68 69 6c 65 20 28 70 6f  .      while (po
811f0 6c 79 67 29 0a 09 09 09 7b 0a 09 09 09 20 20 20  lyg)....{....   
81200 20 2f 2a 20 65 78 74 65 72 69 6f 72 20 52 69 6e   /* exterior Rin
81210 67 20 2a 2f 0a 09 09 09 20 20 20 20 72 69 6e 67  g */....    ring
81220 20 3d 20 70 6f 6c 79 67 2d 3e 45 78 74 65 72 69   = polyg->Exteri
81230 6f 72 3b 0a 09 09 09 20 20 20 20 6c 65 6e 67 74  or;....    lengt
81240 68 20 2b 3d 0a 09 09 09 09 67 61 69 61 47 72 65  h +=.....gaiaGre
81250 61 74 43 69 72 63 6c 65 54 6f 74 61 6c 4c 65 6e  atCircleTotalLen
81260 67 74 68 20 28 61 2c 20 62 2c 0a 09 09 09 09 09  gth (a, b,......
81270 09 09 20 20 20 20 72 69 6e 67 2d 3e 0a 09 09 09  ..    ring->....
81280 09 09 09 09 20 20 20 20 44 69 6d 65 6e 73 69 6f  ....    Dimensio
81290 6e 4d 6f 64 65 6c 2c 0a 09 09 09 09 09 09 09 20  nModel,........ 
812a0 20 20 20 72 69 6e 67 2d 3e 43 6f 6f 72 64 73 2c     ring->Coords,
812b0 0a 09 09 09 09 09 09 09 20 20 20 20 72 69 6e 67  ........    ring
812c0 2d 3e 50 6f 69 6e 74 73 29 3b 0a 09 09 09 20 20  ->Points);....  
812d0 20 20 66 6f 72 20 28 69 62 20 3d 20 30 3b 20 69    for (ib = 0; i
812e0 62 20 3c 20 70 6f 6c 79 67 2d 3e 4e 75 6d 49 6e  b < polyg->NumIn
812f0 74 65 72 69 6f 72 73 3b 20 69 62 2b 2b 29 0a 09  teriors; ib++)..
81300 09 09 20 20 20 20 20 20 7b 0a 09 09 09 09 20 20  ..      {.....  
81310 2f 2a 20 69 6e 74 65 72 69 6f 72 20 52 69 6e 67  /* interior Ring
81320 73 20 2a 2f 0a 09 09 09 09 20 20 72 69 6e 67 20  s */.....  ring 
81330 3d 20 70 6f 6c 79 67 2d 3e 49 6e 74 65 72 69 6f  = polyg->Interio
81340 72 73 20 2b 20 69 62 3b 0a 09 09 09 09 20 20 6c  rs + ib;.....  l
81350 65 6e 67 74 68 20 2b 3d 0a 09 09 09 09 20 20 20  ength +=.....   
81360 20 20 20 67 61 69 61 47 72 65 61 74 43 69 72 63     gaiaGreatCirc
81370 6c 65 54 6f 74 61 6c 4c 65 6e 67 74 68 20 28 61  leTotalLength (a
81380 2c 20 62 2c 0a 09 09 09 09 09 09 09 09 20 20 72  , b,.........  r
81390 69 6e 67 2d 3e 0a 09 09 09 09 09 09 09 09 20 20  ing->.........  
813a0 44 69 6d 65 6e 73 69 6f 6e 4d 6f 64 65 6c 2c 0a  DimensionModel,.
813b0 09 09 09 09 09 09 09 09 20 20 72 69 6e 67 2d 3e  ........  ring->
813c0 43 6f 6f 72 64 73 2c 0a 09 09 09 09 09 09 09 09  Coords,.........
813d0 20 20 72 69 6e 67 2d 3e 50 6f 69 6e 74 73 29 3b    ring->Points);
813e0 0a 09 09 09 20 20 20 20 20 20 7d 0a 09 09 09 20  ....      }.... 
813f0 20 20 20 70 6f 6c 79 67 20 3d 20 70 6f 6c 79 67     polyg = polyg
81400 2d 3e 4e 65 78 74 3b 0a 09 09 09 7d 0a 09 09 20  ->Next;....}... 
81410 20 7d 0a 09 09 73 71 6c 69 74 65 33 5f 72 65 73   }...sqlite3_res
81420 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f 6e 74  ult_double (cont
81430 65 78 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 09 20  ext, length);.. 
81440 20 20 20 7d 0a 09 20 20 65 6c 73 65 0a 09 20 20     }..  else..  
81450 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
81460 6c 74 5f 6e 75 6c 6c 20 28 63 6f 6e 74 65 78 74  lt_null (context
81470 29 3b 0a 09 20 20 67 61 69 61 46 72 65 65 47 65  );..  gaiaFreeGe
81480 6f 6d 43 6f 6c 6c 20 28 67 65 6f 29 3b 0a 20 20  omColl (geo);.  
81490 20 20 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20      }.}..static 
814a0 76 6f 69 64 0a 63 6f 6e 76 65 72 74 55 6e 69 74  void.convertUnit
814b0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
814c0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
814d0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
814e0 61 6c 75 65 20 2a 2a 20 61 72 67 76 2c 0a 09 20  alue ** argv,.. 
814f0 20 20 20 20 69 6e 74 20 75 6e 69 74 5f 66 72 6f      int unit_fro
81500 6d 2c 20 69 6e 74 20 75 6e 69 74 5f 74 6f 29 0a  m, int unit_to).
81510 7b 0a 2f 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  {./* SQL functio
81520 6e 73 3a 0a 2f 20 43 76 74 54 6f 4b 6d 28 29 2c  ns:./ CvtToKm(),
81530 20 43 76 74 54 6f 44 6d 28 29 2c 20 43 76 74 54   CvtToDm(), CvtT
81540 6f 43 6d 28 29 2c 20 43 76 74 54 6f 4d 6d 28 29  oCm(), CvtToMm()
81550 2c 20 43 76 74 54 6f 4b 6d 69 28 29 2c 20 43 76  , CvtToKmi(), Cv
81560 74 54 6f 49 6e 28 29 2c 20 43 76 74 54 6f 46 74  tToIn(), CvtToFt
81570 28 29 2c 0a 2f 20 43 76 74 54 6f 59 64 28 29 2c  (),./ CvtToYd(),
81580 20 43 76 74 54 6f 4d 69 28 29 2c 20 43 76 74 54   CvtToMi(), CvtT
81590 6f 46 61 74 68 28 29 2c 20 43 76 74 54 6f 43 68  oFath(), CvtToCh
815a0 28 29 2c 20 43 76 74 54 6f 4c 69 6e 6b 28 29 2c  (), CvtToLink(),
815b0 20 43 76 74 54 6f 55 73 49 6e 28 29 2c 20 0a 2f   CvtToUsIn(), ./
815c0 20 43 76 74 54 6f 55 73 46 74 28 29 2c 20 43 76   CvtToUsFt(), Cv
815d0 74 54 6f 55 73 59 64 28 29 2c 20 43 76 74 54 6f  tToUsYd(), CvtTo
815e0 55 73 43 68 28 29 2c 20 43 76 74 54 6f 55 73 4d  UsCh(), CvtToUsM
815f0 69 28 29 2c 20 43 76 74 54 6f 49 6e 64 46 74 28  i(), CvtToIndFt(
81600 29 2c 20 0a 2f 20 43 76 74 54 6f 49 6e 64 59 64  ), ./ CvtToIndYd
81610 28 29 2c 20 43 76 74 54 6f 49 6e 64 43 68 28 29  (), CvtToIndCh()
81620 2c 20 0a 2f 20 43 76 74 46 72 6f 6d 4b 6d 28 29  , ./ CvtFromKm()
81630 2c 20 43 76 74 46 72 6f 6d 44 6d 28 29 2c 20 43  , CvtFromDm(), C
81640 76 74 46 72 6f 6d 43 6d 28 29 2c 20 43 76 74 46  vtFromCm(), CvtF
81650 72 6f 6d 4d 6d 28 29 2c 20 43 76 74 46 72 6f 6d  romMm(), CvtFrom
81660 4b 6d 69 28 29 2c 20 0a 2f 20 43 76 74 46 72 6f  Kmi(), ./ CvtFro
81670 6d 49 6e 28 29 2c 20 43 76 74 46 72 6f 6d 46 74  mIn(), CvtFromFt
81680 28 29 2c 20 43 76 74 46 72 6f 6d 59 64 28 29 2c  (), CvtFromYd(),
81690 20 43 76 74 46 72 6f 6d 4d 69 28 29 2c 20 43 76   CvtFromMi(), Cv
816a0 74 46 72 6f 6d 46 61 74 68 28 29 2c 20 0a 2f 20  tFromFath(), ./ 
816b0 43 76 74 46 72 6f 6d 43 68 28 29 2c 20 43 76 74  CvtFromCh(), Cvt
816c0 46 72 6f 6d 4c 69 6e 6b 28 29 2c 20 43 76 74 46  FromLink(), CvtF
816d0 72 6f 6d 55 73 49 6e 28 29 2c 20 43 76 74 46 72  romUsIn(), CvtFr
816e0 6f 6d 55 73 46 74 28 29 2c 20 43 76 74 46 72 6f  omUsFt(), CvtFro
816f0 6d 55 73 59 64 28 29 2c 20 0a 2f 20 43 76 74 46  mUsYd(), ./ CvtF
81700 72 6f 6d 55 73 43 68 28 29 2c 20 43 76 74 46 72  romUsCh(), CvtFr
81710 6f 6d 55 73 4d 69 28 29 2c 20 43 76 74 46 72 6f  omUsMi(), CvtFro
81720 6d 49 6e 64 46 74 28 29 2c 20 43 76 74 46 72 6f  mIndFt(), CvtFro
81730 6d 49 6e 64 59 64 28 29 2c 20 0a 2f 20 43 76 74  mIndYd(), ./ Cvt
81740 46 72 6f 6d 49 6e 64 43 68 28 29 0a 2f 0a 2f 20  FromIndCh()././ 
81750 63 6f 6e 76 65 72 74 73 20 61 20 4c 65 6e 67 74  converts a Lengt
81760 68 20 66 72 6f 6d 20 6f 6e 65 20 75 6e 69 74 20  h from one unit 
81770 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 6f  to a different o
81780 6e 65 0a 2f 20 6f 72 20 4e 55 4c 4c 20 69 66 20  ne./ or NULL if 
81790 61 6e 79 20 65 72 72 6f 72 20 69 73 20 65 6e 63  any error is enc
817a0 6f 75 6e 74 65 72 65 64 0a 2a 2f 0a 20 20 20 20  ountered.*/.    
817b0 64 6f 75 62 6c 65 20 63 76 74 3b 0a 20 20 20 20  double cvt;.    
817c0 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 20  double value;.  
817d0 20 20 69 6e 74 20 69 6e 74 5f 76 61 6c 75 65 3b    int int_value;
817e0 0a 20 20 20 20 47 41 49 41 5f 55 4e 55 53 45 44  .    GAIA_UNUSED
817f0 20 28 29 3b 09 09 2f 2a 20 4c 43 4f 56 5f 45 58   ();../* LCOV_EX
81800 43 4c 5f 4c 49 4e 45 20 2a 2f 0a 20 20 20 20 69  CL_LINE */.    i
81810 66 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f (sqlite3_value
81820 5f 74 79 70 65 20 28 61 72 67 76 5b 30 5d 29 20  _type (argv[0]) 
81830 3d 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 29  == SQLITE_FLOAT)
81840 0a 09 76 61 6c 75 65 20 3d 20 73 71 6c 69 74 65  ..value = sqlite
81850 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 28  3_value_double (
81860 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 65 6c  argv[0]);.    el
81870 73 65 20 69 66 20 28 73 71 6c 69 74 65 33 5f 76  se if (sqlite3_v
81880 61 6c 75 65 5f 74 79 70 65 20 28 61 72 67 76 5b  alue_type (argv[
81890 30 5d 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4e  0]) == SQLITE_IN
818a0 54 45 47 45 52 29 0a 20 20 20 20 20 20 7b 0a 09  TEGER).      {..
818b0 20 20 69 6e 74 5f 76 61 6c 75 65 20 3d 20 73 71    int_value = sq
818c0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
818d0 28 61 72 67 76 5b 30 5d 29 3b 0a 09 20 20 76 61  (argv[0]);..  va
818e0 6c 75 65 20 3d 20 69 6e 74 5f 76 61 6c 75 65 3b  lue = int_value;
818f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 65 6c 73  .      }.    els
81900 65 0a 20 20 20 20 20 20 7b 0a 09 20 20 73 71 6c  e.      {..  sql
81910 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
81920 20 28 63 6f 6e 74 65 78 74 29 3b 0a 09 20 20 72   (context);..  r
81930 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
81940 20 20 20 69 66 20 28 21 67 61 69 61 43 6f 6e 76     if (!gaiaConv
81950 65 72 74 4c 65 6e 67 74 68 20 28 76 61 6c 75 65  ertLength (value
81960 2c 20 75 6e 69 74 5f 66 72 6f 6d 2c 20 75 6e 69  , unit_from, uni
81970 74 5f 74 6f 2c 20 26 63 76 74 29 29 0a 09 73 71  t_to, &cvt))..sq
81980 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
81990 6c 20 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  l (context);.   
819a0 20 65 6c 73 65 0a 09 73 71 6c 69 74 65 33 5f 72   else..sqlite3_r
819b0 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 28 63 6f  esult_double (co
819c0 6e 74 65 78 74 2c 20 63 76 74 29 3b 0a 7d 0a 0a  ntext, cvt);.}..
819d0 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
819e0 5f 63 76 74 54 6f 4b 6d 20 28 73 71 6c 69 74 65  _cvtToKm (sqlite
819f0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
81a00 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
81a10 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
81a20 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76  argv).{.    conv
81a30 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74  ertUnit (context
81a40 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
81a50 49 41 5f 4d 2c 20 47 41 49 41 5f 4b 4d 29 3b 0a  IA_M, GAIA_KM);.
81a60 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
81a70 6e 63 74 5f 63 76 74 54 6f 44 6d 20 28 73 71 6c  nct_cvtToDm (sql
81a80 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
81a90 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
81aa0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
81ab0 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 63  ** argv).{.    c
81ac0 6f 6e 76 65 72 74 55 6e 69 74 20 28 63 6f 6e 74  onvertUnit (cont
81ad0 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  ext, argc, argv,
81ae0 20 47 41 49 41 5f 4d 2c 20 47 41 49 41 5f 44 4d   GAIA_M, GAIA_DM
81af0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
81b00 64 0a 66 6e 63 74 5f 63 76 74 54 6f 43 6d 20 28  d.fnct_cvtToCm (
81b10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
81b20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
81b30 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
81b40 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
81b50 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63    convertUnit (c
81b60 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
81b70 67 76 2c 20 47 41 49 41 5f 4d 2c 20 47 41 49 41  gv, GAIA_M, GAIA
81b80 5f 43 4d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  _CM);.}..static 
81b90 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74 54 6f 4d  void.fnct_cvtToM
81ba0 6d 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  m (sqlite3_conte
81bb0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
81bc0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
81bd0 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
81be0 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74  .    convertUnit
81bf0 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c   (context, argc,
81c00 20 61 72 67 76 2c 20 47 41 49 41 5f 4d 2c 20 47   argv, GAIA_M, G
81c10 41 49 41 5f 4d 4d 29 3b 0a 7d 0a 0a 73 74 61 74  AIA_MM);.}..stat
81c20 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74  ic void.fnct_cvt
81c30 54 6f 4b 6d 69 20 28 73 71 6c 69 74 65 33 5f 63  ToKmi (sqlite3_c
81c40 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
81c50 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
81c60 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
81c70 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74  v).{.    convert
81c80 55 6e 69 74 20 28 63 6f 6e 74 65 78 74 2c 20 61  Unit (context, a
81c90 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f  rgc, argv, GAIA_
81ca0 4d 2c 20 47 41 49 41 5f 4b 4d 49 29 3b 0a 7d 0a  M, GAIA_KMI);.}.
81cb0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
81cc0 74 5f 63 76 74 54 6f 49 6e 20 28 73 71 6c 69 74  t_cvtToIn (sqlit
81cd0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
81ce0 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
81cf0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
81d00 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e   argv).{.    con
81d10 76 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78  vertUnit (contex
81d20 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47  t, argc, argv, G
81d30 41 49 41 5f 4d 2c 20 47 41 49 41 5f 49 4e 29 3b  AIA_M, GAIA_IN);
81d40 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
81d50 66 6e 63 74 5f 63 76 74 54 6f 46 74 20 28 73 71  fnct_cvtToFt (sq
81d60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
81d70 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
81d80 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
81d90 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20   ** argv).{.    
81da0 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63 6f 6e  convertUnit (con
81db0 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
81dc0 2c 20 47 41 49 41 5f 4d 2c 20 47 41 49 41 5f 46  , GAIA_M, GAIA_F
81dd0 54 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  T);.}..static vo
81de0 69 64 0a 66 6e 63 74 5f 63 76 74 54 6f 59 64 20  id.fnct_cvtToYd 
81df0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
81e00 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
81e10 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
81e20 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20  lue ** argv).{. 
81e30 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28     convertUnit (
81e40 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61  context, argc, a
81e50 72 67 76 2c 20 47 41 49 41 5f 4d 2c 20 47 41 49  rgv, GAIA_M, GAI
81e60 41 5f 59 44 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  A_YD);.}..static
81e70 20 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74 54 6f   void.fnct_cvtTo
81e80 4d 69 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  Mi (sqlite3_cont
81e90 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69  ext * context, i
81ea0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
81eb0 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a  _value ** argv).
81ec0 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69  {.    convertUni
81ed0 74 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  t (context, argc
81ee0 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 4d 2c 20  , argv, GAIA_M, 
81ef0 47 41 49 41 5f 4d 49 29 3b 0a 7d 0a 0a 73 74 61  GAIA_MI);.}..sta
81f00 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63 76  tic void.fnct_cv
81f10 74 54 6f 46 61 74 68 20 28 73 71 6c 69 74 65 33  tToFath (sqlite3
81f20 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65  _context * conte
81f30 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
81f40 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61  lite3_value ** a
81f50 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76 65  rgv).{.    conve
81f60 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74 2c  rtUnit (context,
81f70 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49   argc, argv, GAI
81f80 41 5f 4d 2c 20 47 41 49 41 5f 46 41 54 48 29 3b  A_M, GAIA_FATH);
81f90 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
81fa0 66 6e 63 74 5f 63 76 74 54 6f 43 68 20 28 73 71  fnct_cvtToCh (sq
81fb0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20  lite3_context * 
81fc0 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
81fd0 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
81fe0 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20   ** argv).{.    
81ff0 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63 6f 6e  convertUnit (con
82000 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
82010 2c 20 47 41 49 41 5f 4d 2c 20 47 41 49 41 5f 43  , GAIA_M, GAIA_C
82020 48 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  H);.}..static vo
82030 69 64 0a 66 6e 63 74 5f 63 76 74 54 6f 4c 69 6e  id.fnct_cvtToLin
82040 6b 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  k (sqlite3_conte
82050 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
82060 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
82070 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
82080 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74  .    convertUnit
82090 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c   (context, argc,
820a0 20 61 72 67 76 2c 20 47 41 49 41 5f 4d 2c 20 47   argv, GAIA_M, G
820b0 41 49 41 5f 4c 49 4e 4b 29 3b 0a 7d 0a 0a 73 74  AIA_LINK);.}..st
820c0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63  atic void.fnct_c
820d0 76 74 54 6f 55 73 49 6e 20 28 73 71 6c 69 74 65  vtToUsIn (sqlite
820e0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
820f0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
82100 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
82110 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76  argv).{.    conv
82120 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74  ertUnit (context
82130 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
82140 49 41 5f 4d 2c 20 47 41 49 41 5f 55 53 5f 49 4e  IA_M, GAIA_US_IN
82150 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
82160 64 0a 66 6e 63 74 5f 63 76 74 54 6f 55 73 46 74  d.fnct_cvtToUsFt
82170 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
82180 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
82190 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
821a0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
821b0 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20      convertUnit 
821c0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
821d0 61 72 67 76 2c 20 47 41 49 41 5f 4d 2c 20 47 41  argv, GAIA_M, GA
821e0 49 41 5f 55 53 5f 46 54 29 3b 0a 7d 0a 0a 73 74  IA_US_FT);.}..st
821f0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63  atic void.fnct_c
82200 76 74 54 6f 55 73 59 64 20 28 73 71 6c 69 74 65  vtToUsYd (sqlite
82210 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
82220 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
82230 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
82240 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76  argv).{.    conv
82250 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74  ertUnit (context
82260 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
82270 49 41 5f 4d 2c 20 47 41 49 41 5f 55 53 5f 59 44  IA_M, GAIA_US_YD
82280 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
82290 64 0a 66 6e 63 74 5f 63 76 74 54 6f 55 73 43 68  d.fnct_cvtToUsCh
822a0 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
822b0 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
822c0 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
822d0 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
822e0 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20      convertUnit 
822f0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
82300 61 72 67 76 2c 20 47 41 49 41 5f 4d 2c 20 47 41  argv, GAIA_M, GA
82310 49 41 5f 55 53 5f 43 48 29 3b 0a 7d 0a 0a 73 74  IA_US_CH);.}..st
82320 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63  atic void.fnct_c
82330 76 74 54 6f 55 73 4d 69 20 28 73 71 6c 69 74 65  vtToUsMi (sqlite
82340 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
82350 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
82360 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
82370 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76  argv).{.    conv
82380 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74  ertUnit (context
82390 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
823a0 49 41 5f 4d 2c 20 47 41 49 41 5f 55 53 5f 4d 49  IA_M, GAIA_US_MI
823b0 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
823c0 64 0a 66 6e 63 74 5f 63 76 74 54 6f 49 6e 64 46  d.fnct_cvtToIndF
823d0 74 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t (sqlite3_conte
823e0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
823f0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
82400 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
82410 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74  .    convertUnit
82420 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c   (context, argc,
82430 20 61 72 67 76 2c 20 47 41 49 41 5f 4d 2c 20 47   argv, GAIA_M, G
82440 41 49 41 5f 49 4e 44 5f 46 54 29 3b 0a 7d 0a 0a  AIA_IND_FT);.}..
82450 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74  static void.fnct
82460 5f 63 76 74 54 6f 49 6e 64 59 64 20 28 73 71 6c  _cvtToIndYd (sql
82470 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63  ite3_context * c
82480 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
82490 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
824a0 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 63  ** argv).{.    c
824b0 6f 6e 76 65 72 74 55 6e 69 74 20 28 63 6f 6e 74  onvertUnit (cont
824c0 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  ext, argc, argv,
824d0 20 47 41 49 41 5f 4d 2c 20 47 41 49 41 5f 49 4e   GAIA_M, GAIA_IN
824e0 44 5f 59 44 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  D_YD);.}..static
824f0 20 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74 54 6f   void.fnct_cvtTo
82500 49 6e 64 43 68 20 28 73 71 6c 69 74 65 33 5f 63  IndCh (sqlite3_c
82510 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
82520 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
82530 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
82540 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74  v).{.    convert
82550 55 6e 69 74 20 28 63 6f 6e 74 65 78 74 2c 20 61  Unit (context, a
82560 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f  rgc, argv, GAIA_
82570 4d 2c 20 47 41 49 41 5f 49 4e 44 5f 43 48 29 3b  M, GAIA_IND_CH);
82580 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
82590 66 6e 63 74 5f 63 76 74 46 72 6f 6d 4b 6d 20 28  fnct_cvtFromKm (
825a0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
825b0 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
825c0 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
825d0 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
825e0 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63    convertUnit (c
825f0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
82600 67 76 2c 20 47 41 49 41 5f 4b 4d 2c 20 47 41 49  gv, GAIA_KM, GAI
82610 41 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  A_M);.}..static 
82620 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74 46 72 6f  void.fnct_cvtFro
82630 6d 44 6d 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  mDm (sqlite3_con
82640 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
82650 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
82660 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
82670 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e  .{.    convertUn
82680 69 74 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  it (context, arg
82690 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 44 4d  c, argv, GAIA_DM
826a0 2c 20 47 41 49 41 5f 4d 29 3b 0a 7d 0a 0a 73 74  , GAIA_M);.}..st
826b0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63  atic void.fnct_c
826c0 76 74 46 72 6f 6d 43 6d 20 28 73 71 6c 69 74 65  vtFromCm (sqlite
826d0 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
826e0 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
826f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
82700 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76  argv).{.    conv
82710 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74  ertUnit (context
82720 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
82730 49 41 5f 43 4d 2c 20 47 41 49 41 5f 4d 29 3b 0a  IA_CM, GAIA_M);.
82740 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
82750 6e 63 74 5f 63 76 74 46 72 6f 6d 4d 6d 20 28 73  nct_cvtFromMm (s
82760 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
82770 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
82780 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
82790 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20  e ** argv).{.   
827a0 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63 6f   convertUnit (co
827b0 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
827c0 76 2c 20 47 41 49 41 5f 4d 4d 2c 20 47 41 49 41  v, GAIA_MM, GAIA
827d0 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  _M);.}..static v
827e0 6f 69 64 0a 66 6e 63 74 5f 63 76 74 46 72 6f 6d  oid.fnct_cvtFrom
827f0 4b 6d 69 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Kmi (sqlite3_con
82800 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
82810 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
82820 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
82830 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e  .{.    convertUn
82840 69 74 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  it (context, arg
82850 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 4b 4d  c, argv, GAIA_KM
82860 49 2c 20 47 41 49 41 5f 4d 29 3b 0a 7d 0a 0a 73  I, GAIA_M);.}..s
82870 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f  tatic void.fnct_
82880 63 76 74 46 72 6f 6d 49 6e 20 28 73 71 6c 69 74  cvtFromIn (sqlit
82890 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
828a0 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
828b0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
828c0 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e   argv).{.    con
828d0 76 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78  vertUnit (contex
828e0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47  t, argc, argv, G
828f0 41 49 41 5f 49 4e 2c 20 47 41 49 41 5f 4d 29 3b  AIA_IN, GAIA_M);
82900 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
82910 66 6e 63 74 5f 63 76 74 46 72 6f 6d 46 74 20 28  fnct_cvtFromFt (
82920 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
82930 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
82940 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
82950 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
82960 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63    convertUnit (c
82970 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
82980 67 76 2c 20 47 41 49 41 5f 46 54 2c 20 47 41 49  gv, GAIA_FT, GAI
82990 41 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  A_M);.}..static 
829a0 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74 46 72 6f  void.fnct_cvtFro
829b0 6d 59 64 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e  mYd (sqlite3_con
829c0 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20  text * context, 
829d0 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
829e0 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29  3_value ** argv)
829f0 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e  .{.    convertUn
82a00 69 74 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  it (context, arg
82a10 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 59 44  c, argv, GAIA_YD
82a20 2c 20 47 41 49 41 5f 4d 29 3b 0a 7d 0a 0a 73 74  , GAIA_M);.}..st
82a30 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63  atic void.fnct_c
82a40 76 74 46 72 6f 6d 4d 69 20 28 73 71 6c 69 74 65  vtFromMi (sqlite
82a50 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74  3_context * cont
82a60 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
82a70 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20  qlite3_value ** 
82a80 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76  argv).{.    conv
82a90 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74  ertUnit (context
82aa0 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41  , argc, argv, GA
82ab0 49 41 5f 4d 49 2c 20 47 41 49 41 5f 4d 29 3b 0a  IA_MI, GAIA_M);.
82ac0 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66  }..static void.f
82ad0 6e 63 74 5f 63 76 74 46 72 6f 6d 46 61 74 68 20  nct_cvtFromFath 
82ae0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
82af0 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20   * context, int 
82b00 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
82b10 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20  lue ** argv).{. 
82b20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28     convertUnit (
82b30 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61  context, argc, a
82b40 72 67 76 2c 20 47 41 49 41 5f 46 41 54 48 2c 20  rgv, GAIA_FATH, 
82b50 47 41 49 41 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74  GAIA_M);.}..stat
82b60 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74  ic void.fnct_cvt
82b70 46 72 6f 6d 43 68 20 28 73 71 6c 69 74 65 33 5f  FromCh (sqlite3_
82b80 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
82b90 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
82ba0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
82bb0 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72  gv).{.    conver
82bc0 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74 2c 20  tUnit (context, 
82bd0 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41  argc, argv, GAIA
82be0 5f 43 48 2c 20 47 41 49 41 5f 4d 29 3b 0a 7d 0a  _CH, GAIA_M);.}.
82bf0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e 63  .static void.fnc
82c00 74 5f 63 76 74 46 72 6f 6d 4c 69 6e 6b 20 28 73  t_cvtFromLink (s
82c10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
82c20 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72   context, int ar
82c30 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
82c40 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20  e ** argv).{.   
82c50 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63 6f   convertUnit (co
82c60 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
82c70 76 2c 20 47 41 49 41 5f 4c 49 4e 4b 2c 20 47 41  v, GAIA_LINK, GA
82c80 49 41 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  IA_M);.}..static
82c90 20 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74 46 72   void.fnct_cvtFr
82ca0 6f 6d 55 73 49 6e 20 28 73 71 6c 69 74 65 33 5f  omUsIn (sqlite3_
82cb0 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78  context * contex
82cc0 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
82cd0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72  ite3_value ** ar
82ce0 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72  gv).{.    conver
82cf0 74 55 6e 69 74 20 28 63 6f 6e 74 65 78 74 2c 20  tUnit (context, 
82d00 61 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41  argc, argv, GAIA
82d10 5f 55 53 5f 49 4e 2c 20 47 41 49 41 5f 4d 29 3b  _US_IN, GAIA_M);
82d20 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
82d30 66 6e 63 74 5f 63 76 74 46 72 6f 6d 55 73 46 74  fnct_cvtFromUsFt
82d40 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   (sqlite3_contex
82d50 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74  t * context, int
82d60 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
82d70 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a  alue ** argv).{.
82d80 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20      convertUnit 
82d90 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
82da0 61 72 67 76 2c 20 47 41 49 41 5f 55 53 5f 46 54  argv, GAIA_US_FT
82db0 2c 20 47 41 49 41 5f 4d 29 3b 0a 7d 0a 0a 73 74  , GAIA_M);.}..st
82dc0 61 74 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63  atic void.fnct_c
82dd0 76 74 46 72 6f 6d 55 73 59 64 20 28 73 71 6c 69  vtFromUsYd (sqli
82de0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f  te3_context * co
82df0 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
82e00 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
82e10 2a 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f  * argv).{.    co
82e20 6e 76 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65  nvertUnit (conte
82e30 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20  xt, argc, argv, 
82e40 47 41 49 41 5f 55 53 5f 59 44 2c 20 47 41 49 41  GAIA_US_YD, GAIA
82e50 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  _M);.}..static v
82e60 6f 69 64 0a 66 6e 63 74 5f 63 76 74 46 72 6f 6d  oid.fnct_cvtFrom
82e70 55 73 43 68 20 28 73 71 6c 69 74 65 33 5f 63 6f  UsCh (sqlite3_co
82e80 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c  ntext * context,
82e90 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
82ea0 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67 76  e3_value ** argv
82eb0 29 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74 55  ).{.    convertU
82ec0 6e 69 74 20 28 63 6f 6e 74 65 78 74 2c 20 61 72  nit (context, ar
82ed0 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f 55  gc, argv, GAIA_U
82ee0 53 5f 43 48 2c 20 47 41 49 41 5f 4d 29 3b 0a 7d  S_CH, GAIA_M);.}
82ef0 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 66 6e  ..static void.fn
82f00 63 74 5f 63 76 74 46 72 6f 6d 55 73 4d 69 20 28  ct_cvtFromUsMi (
82f10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
82f20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61  * context, int a
82f30 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
82f40 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b 0a 20 20  ue ** argv).{.  
82f50 20 20 63 6f 6e 76 65 72 74 55 6e 69 74 20 28 63    convertUnit (c
82f60 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
82f70 67 76 2c 20 47 41 49 41 5f 55 53 5f 4d 49 2c 20  gv, GAIA_US_MI, 
82f80 47 41 49 41 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74  GAIA_M);.}..stat
82f90 69 63 20 76 6f 69 64 0a 66 6e 63 74 5f 63 76 74  ic void.fnct_cvt
82fa0 46 72 6f 6d 49 6e 64 46 74 20 28 73 71 6c 69 74  FromIndFt (sqlit
82fb0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 20 63 6f 6e  e3_context * con
82fc0 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
82fd0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
82fe0 20 61 72 67 76 29 0a 7b 0a 20 20 20 20 63 6f 6e   argv).{.    con
82ff0 76 65 72 74 55 6e 69 74 20 28 63 6f 6e 74 65 78  vertUnit (contex
83000 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 47  t, argc, argv, G
83010 41 49 41 5f 49 4e 44 5f 46 54 2c 20 47 41 49 41  AIA_IND_FT, GAIA
83020 5f 4d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  _M);.}..static v
83030 6f 69 64 0a 66 6e 63 74 5f 63 76 74 46 72 6f 6d  oid.fnct_cvtFrom
83040 49 6e 64 59 64 20 28 73 71 6c 69 74 65 33 5f 63  IndYd (sqlite3_c
83050 6f 6e 74 65 78 74 20 2a 20 63 6f 6e 74 65 78 74  ontext * context
83060 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
83070 74 65 33 5f 76 61 6c 75 65 20 2a 2a 20 61 72 67  te3_value ** arg
83080 76 29 0a 7b 0a 20 20 20 20 63 6f 6e 76 65 72 74  v).{.    convert
83090 55 6e 69 74 20 28 63 6f 6e 74 65 78 74 2c 20 61  Unit (context, a
830a0 72 67 63 2c 20 61 72 67 76 2c 20 47 41 49 41 5f  rgc, argv, GAIA_
830b0 49 4e 44 5f 59 44 2c 20 47 41 49 41 5f 4d 29 3b  IND_YD, GAIA_M);
830c0 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 0a  .}..static void.
830d0 66 6e 63 74 5f 63 76 74 46 72 6f 6d 49 6e 64 43  fnct_cvtFromIndC
830e0 68 20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  h (sqlite3_conte
830f0 78 74 20 2a 20 63 6f 6e 74 65 78 74 2c 20 69 6e  xt * context, in
83100 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
83110 76 61 6c 75 65 20 2a 2a 20 61 72 67 76 29 0a 7b  value ** argv).{
83120 0a 20 20 20 20 63 6f 6e 76 65 72 74 55 6e 69 74  .    convertUnit
83130 20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c   (context, argc,
83140 20 61 72 67 76 2c 20 47 41 49 41 5f 49 4e 44 5f   argv, GAIA_IND_
83150 43 48 2c 20 47 41 49 41 5f 4d 29 3b 0a 7d 0a 0a  CH, GAIA_M);.}..
83160 73 74 61 74 69 63 20 76 6f 69 64 0a 72 65 67 69  static void.regi
83170 73 74 65 72 5f 73 70 61 74 69 61 6c 69 74 65 5f  ster_spatialite_
83180 73 71 6c 5f 66 75 6e 63 74 69 6f 6e 73 20 28 73  sql_functions (s
83190 71 6c 69 74 65 33 20 2a 20 64 62 29 0a 7b 0a 20  qlite3 * db).{. 
831a0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
831b0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
831c0 22 73 70 61 74 69 61 6c 69 74 65 5f 76 65 72 73  "spatialite_vers
831d0 69 6f 6e 22 2c 20 30 2c 20 53 51 4c 49 54 45 5f  ion", 0, SQLITE_
831e0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
831f0 66 6e 63 74 5f 73 70 61 74 69 61 6c 69 74 65 5f  fnct_spatialite_
83200 76 65 72 73 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a  version, 0, 0);.
83210 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
83220 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
83230 20 22 70 72 6f 6a 34 5f 76 65 72 73 69 6f 6e 22   "proj4_version"
83240 2c 20 30 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 0, SQLITE_ANY,
83250 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
83260 5f 70 72 6f 6a 34 5f 76 65 72 73 69 6f 6e 2c 20  _proj4_version, 
83270 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
83280 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
83290 6f 6e 20 28 64 62 2c 20 22 67 65 6f 73 5f 76 65  on (db, "geos_ve
832a0 72 73 69 6f 6e 22 2c 20 30 2c 20 53 51 4c 49 54  rsion", 0, SQLIT
832b0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
832c0 20 20 66 6e 63 74 5f 67 65 6f 73 5f 76 65 72 73    fnct_geos_vers
832d0 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ion, 0, 0);.    
832e0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
832f0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 48 61  unction (db, "Ha
83300 73 50 72 6f 6a 22 2c 20 30 2c 20 53 51 4c 49 54  sProj", 0, SQLIT
83310 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
83320 20 20 66 6e 63 74 5f 68 61 73 5f 70 72 6f 6a 2c    fnct_has_proj,
83330 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
83340 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
83350 69 6f 6e 20 28 64 62 2c 20 22 48 61 73 47 65 6f  ion (db, "HasGeo
83360 73 22 2c 20 30 2c 20 53 51 4c 49 54 45 5f 41 4e  s", 0, SQLITE_AN
83370 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
83380 63 74 5f 68 61 73 5f 67 65 6f 73 2c 20 30 2c 20  ct_has_geos, 0, 
83390 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
833a0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
833b0 28 64 62 2c 20 22 48 61 73 47 65 6f 73 41 64 76  (db, "HasGeosAdv
833c0 61 6e 63 65 64 22 2c 20 30 2c 20 53 51 4c 49 54  anced", 0, SQLIT
833d0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
833e0 20 20 66 6e 63 74 5f 68 61 73 5f 67 65 6f 73 5f    fnct_has_geos_
833f0 61 64 76 61 6e 63 65 64 2c 20 30 2c 20 30 29 3b  advanced, 0, 0);
83400 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
83410 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
83420 2c 20 22 48 61 73 4d 61 74 68 53 71 6c 22 2c 20  , "HasMathSql", 
83430 30 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  0, SQLITE_ANY, 0
83440 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 68  ,....     fnct_h
83450 61 73 5f 6d 61 74 68 5f 73 71 6c 2c 20 30 2c 20  as_math_sql, 0, 
83460 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
83470 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
83480 28 64 62 2c 20 22 48 61 73 47 65 6f 43 61 6c 6c  (db, "HasGeoCall
83490 62 61 63 6b 73 22 2c 20 30 2c 20 53 51 4c 49 54  backs", 0, SQLIT
834a0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
834b0 20 20 66 6e 63 74 5f 68 61 73 5f 67 65 6f 5f 63    fnct_has_geo_c
834c0 61 6c 6c 62 61 63 6b 73 2c 20 30 2c 20 30 29 3b  allbacks, 0, 0);
834d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
834e0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
834f0 2c 20 22 48 61 73 49 63 6f 6e 76 22 2c 20 30 2c  , "HasIconv", 0,
83500 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
83510 09 09 09 20 20 20 20 20 66 6e 63 74 5f 68 61 73  ...     fnct_has
83520 5f 69 63 6f 6e 76 2c 20 30 2c 20 30 29 3b 0a 20  _iconv, 0, 0);. 
83530 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
83540 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
83550 22 48 61 73 46 72 65 65 58 4c 22 2c 20 30 2c 20  "HasFreeXL", 0, 
83560 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
83570 09 09 20 20 20 20 20 66 6e 63 74 5f 68 61 73 5f  ..     fnct_has_
83580 66 72 65 65 58 4c 2c 20 30 2c 20 30 29 3b 0a 20  freeXL, 0, 0);. 
83590 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
835a0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
835b0 22 48 61 73 45 70 73 67 22 2c 20 30 2c 20 53 51  "HasEpsg", 0, SQ
835c0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
835d0 20 20 20 20 20 66 6e 63 74 5f 68 61 73 5f 65 70       fnct_has_ep
835e0 73 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  sg, 0, 0);.    s
835f0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
83600 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47 65 6f  nction (db, "Geo
83610 6d 65 74 72 79 43 6f 6e 73 74 72 61 69 6e 74 73  metryConstraints
83620 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 3, SQLITE_ANY
83630 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
83640 74 5f 47 65 6f 6d 65 74 72 79 43 6f 6e 73 74 72  t_GeometryConstr
83650 61 69 6e 74 73 2c 20 30 2c 20 30 29 3b 0a 20 20  aints, 0, 0);.  
83660 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
83670 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
83680 47 65 6f 6d 65 74 72 79 43 6f 6e 73 74 72 61 69  GeometryConstrai
83690 6e 74 73 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f  nts", 4, SQLITE_
836a0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
836b0 66 6e 63 74 5f 47 65 6f 6d 65 74 72 79 43 6f 6e  fnct_GeometryCon
836c0 73 74 72 61 69 6e 74 73 2c 20 30 2c 20 30 29 3b  straints, 0, 0);
836d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
836e0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
836f0 2c 20 22 52 54 72 65 65 41 6c 69 67 6e 22 2c 20  , "RTreeAlign", 
83700 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  3, SQLITE_ANY, 0
83710 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 52  ,....     fnct_R
83720 54 72 65 65 41 6c 69 67 6e 2c 20 30 2c 20 30 29  TreeAlign, 0, 0)
83730 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
83740 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
83750 62 2c 20 22 43 68 65 63 6b 53 70 61 74 69 61 6c  b, "CheckSpatial
83760 4d 65 74 61 44 61 74 61 22 2c 20 30 2c 20 53 51  MetaData", 0, SQ
83770 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
83780 20 20 20 20 20 66 6e 63 74 5f 43 68 65 63 6b 53       fnct_CheckS
83790 70 61 74 69 61 6c 4d 65 74 61 44 61 74 61 2c 20  patialMetaData, 
837a0 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
837b0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
837c0 6f 6e 20 28 64 62 2c 20 22 41 75 74 6f 46 44 4f  on (db, "AutoFDO
837d0 53 74 61 72 74 22 2c 20 30 2c 20 53 51 4c 49 54  Start", 0, SQLIT
837e0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
837f0 20 20 66 6e 63 74 5f 41 75 74 6f 46 44 4f 53 74    fnct_AutoFDOSt
83800 61 72 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  art, 0, 0);.    
83810 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
83820 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 41 75  unction (db, "Au
83830 74 6f 46 44 4f 53 74 6f 70 22 2c 20 30 2c 20 53  toFDOStop", 0, S
83840 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
83850 09 20 20 20 20 20 66 6e 63 74 5f 41 75 74 6f 46  .     fnct_AutoF
83860 44 4f 53 74 6f 70 2c 20 30 2c 20 30 29 3b 0a 20  DOStop, 0, 0);. 
83870 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
83880 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
83890 22 49 6e 69 74 46 44 4f 53 70 61 74 69 61 6c 4d  "InitFDOSpatialM
838a0 65 74 61 44 61 74 61 22 2c 20 30 2c 20 53 51 4c  etaData", 0, SQL
838b0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
838c0 20 20 20 20 66 6e 63 74 5f 49 6e 69 74 46 44 4f      fnct_InitFDO
838d0 53 70 61 74 69 61 6c 4d 65 74 61 44 61 74 61 2c  SpatialMetaData,
838e0 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
838f0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
83900 69 6f 6e 20 28 64 62 2c 20 22 41 64 64 46 44 4f  ion (db, "AddFDO
83910 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 22 2c  GeometryColumn",
83920 20 36 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   6, SQLITE_ANY, 
83930 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
83940 41 64 64 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f  AddFDOGeometryCo
83950 6c 75 6d 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20  lumn, 0, 0);.   
83960 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
83970 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 52  function (db, "R
83980 65 63 6f 76 65 72 46 44 4f 47 65 6f 6d 65 74 72  ecoverFDOGeometr
83990 79 43 6f 6c 75 6d 6e 22 2c 20 36 2c 20 53 51 4c  yColumn", 6, SQL
839a0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
839b0 20 20 20 20 66 6e 63 74 5f 52 65 63 6f 76 65 72      fnct_Recover
839c0 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d  FDOGeometryColum
839d0 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  n, 0, 0);.    sq
839e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
839f0 63 74 69 6f 6e 20 28 64 62 2c 20 22 44 69 73 63  ction (db, "Disc
83a00 61 72 64 46 44 4f 47 65 6f 6d 65 74 72 79 43 6f  ardFDOGeometryCo
83a10 6c 75 6d 6e 22 2c 20 32 2c 20 53 51 4c 49 54 45  lumn", 2, SQLITE
83a20 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
83a30 20 66 6e 63 74 5f 44 69 73 63 61 72 64 46 44 4f   fnct_DiscardFDO
83a40 47 65 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 2c 20  GeometryColumn, 
83a50 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
83a60 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
83a70 6f 6e 20 28 64 62 2c 20 22 49 6e 69 74 53 70 61  on (db, "InitSpa
83a80 74 69 61 6c 4d 65 74 61 44 61 74 61 22 2c 20 30  tialMetaData", 0
83a90 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
83aa0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 49 6e  ....     fnct_In
83ab0 69 74 53 70 61 74 69 61 6c 4d 65 74 61 44 61 74  itSpatialMetaDat
83ac0 61 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  a, 0, 0);.    sq
83ad0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
83ae0 63 74 69 6f 6e 20 28 64 62 2c 20 22 49 6e 69 74  ction (db, "Init
83af0 53 70 61 74 69 61 6c 4d 65 74 61 44 61 74 61 22  SpatialMetaData"
83b00 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
83b10 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
83b20 5f 49 6e 69 74 53 70 61 74 69 61 6c 4d 65 74 61  _InitSpatialMeta
83b30 44 61 74 61 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Data, 0, 0);.   
83b40 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
83b50 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 49  function (db, "I
83b60 6e 73 65 72 74 45 70 73 67 53 72 69 64 22 2c 20  nsertEpsgSrid", 
83b70 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
83b80 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 49  ,....     fnct_I
83b90 6e 73 65 72 74 45 70 73 67 53 72 69 64 2c 20 30  nsertEpsgSrid, 0
83ba0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
83bb0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
83bc0 6e 20 28 64 62 2c 20 22 41 64 64 47 65 6f 6d 65  n (db, "AddGeome
83bd0 74 72 79 43 6f 6c 75 6d 6e 22 2c 20 35 2c 20 53  tryColumn", 5, S
83be0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
83bf0 09 20 20 20 20 20 66 6e 63 74 5f 41 64 64 47 65  .     fnct_AddGe
83c00 6f 6d 65 74 72 79 43 6f 6c 75 6d 6e 2c 20 30 2c  ometryColumn, 0,
83c10 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
83c20 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
83c30 20 28 64 62 2c 20 22 41 64 64 47 65 6f 6d 65 74   (db, "AddGeomet
83c40 72 79 43 6f 6c 75 6d 6e 22 2c 20 36 2c 20 53 51  ryColumn", 6, SQ
83c50 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
83c60 20 20 20 20 20 66 6e 63 74 5f 41 64 64 47 65 6f       fnct_AddGeo
83c70 6d 65 74 72 79 43 6f 6c 75 6d 6e 2c 20 30 2c 20  metryColumn, 0, 
83c80 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
83c90 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
83ca0 28 64 62 2c 20 22 52 65 63 6f 76 65 72 47 65 6f  (db, "RecoverGeo
83cb0 6d 65 74 72 79 43 6f 6c 75 6d 6e 22 2c 20 35 2c  metryColumn", 5,
83cc0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
83cd0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 52 65 63  ...     fnct_Rec
83ce0 6f 76 65 72 47 65 6f 6d 65 74 72 79 43 6f 6c 75  overGeometryColu
83cf0 6d 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  mn, 0, 0);.    s
83d00 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
83d10 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 44 69 73  nction (db, "Dis
83d20 63 61 72 64 47 65 6f 6d 65 74 72 79 43 6f 6c 75  cardGeometryColu
83d30 6d 6e 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  mn", 2, SQLITE_A
83d40 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
83d50 6e 63 74 5f 44 69 73 63 61 72 64 47 65 6f 6d 65  nct_DiscardGeome
83d60 74 72 79 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 29  tryColumn, 0, 0)
83d70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
83d80 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
83d90 62 2c 20 22 52 65 67 69 73 74 65 72 56 69 72 74  b, "RegisterVirt
83da0 75 61 6c 47 65 6f 6d 65 74 72 79 22 2c 20 31 2c  ualGeometry", 1,
83db0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
83dc0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 52 65 67  ...     fnct_Reg
83dd0 69 73 74 65 72 56 69 72 74 75 61 6c 47 65 6f 6d  isterVirtualGeom
83de0 65 74 72 79 2c 20 30 2c 20 30 29 3b 0a 20 20 20  etry, 0, 0);.   
83df0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
83e00 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 44  function (db, "D
83e10 72 6f 70 56 69 72 74 75 61 6c 47 65 6f 6d 65 74  ropVirtualGeomet
83e20 72 79 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  ry", 1, SQLITE_A
83e30 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
83e40 6e 63 74 5f 44 72 6f 70 56 69 72 74 75 61 6c 47  nct_DropVirtualG
83e50 65 6f 6d 65 74 72 79 2c 20 30 2c 20 30 29 3b 0a  eometry, 0, 0);.
83e60 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
83e70 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
83e80 20 22 52 65 63 6f 76 65 72 53 70 61 74 69 61 6c   "RecoverSpatial
83e90 49 6e 64 65 78 22 2c 20 30 2c 20 53 51 4c 49 54  Index", 0, SQLIT
83ea0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
83eb0 20 20 66 6e 63 74 5f 52 65 63 6f 76 65 72 53 70    fnct_RecoverSp
83ec0 61 74 69 61 6c 49 6e 64 65 78 2c 20 30 2c 20 30  atialIndex, 0, 0
83ed0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
83ee0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
83ef0 64 62 2c 20 22 52 65 63 6f 76 65 72 53 70 61 74  db, "RecoverSpat
83f00 69 61 6c 49 6e 64 65 78 22 2c 20 31 2c 20 53 51  ialIndex", 1, SQ
83f10 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
83f20 20 20 20 20 20 66 6e 63 74 5f 52 65 63 6f 76 65       fnct_Recove
83f30 72 53 70 61 74 69 61 6c 49 6e 64 65 78 2c 20 30  rSpatialIndex, 0
83f40 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
83f50 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
83f60 6e 20 28 64 62 2c 20 22 52 65 63 6f 76 65 72 53  n (db, "RecoverS
83f70 70 61 74 69 61 6c 49 6e 64 65 78 22 2c 20 32 2c  patialIndex", 2,
83f80 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
83f90 09 09 09 20 20 20 20 20 66 6e 63 74 5f 52 65 63  ...     fnct_Rec
83fa0 6f 76 65 72 53 70 61 74 69 61 6c 49 6e 64 65 78  overSpatialIndex
83fb0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
83fc0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
83fd0 74 69 6f 6e 20 28 64 62 2c 20 22 52 65 63 6f 76  tion (db, "Recov
83fe0 65 72 53 70 61 74 69 61 6c 49 6e 64 65 78 22 2c  erSpatialIndex",
83ff0 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   3, SQLITE_ANY, 
84000 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
84010 52 65 63 6f 76 65 72 53 70 61 74 69 61 6c 49 6e  RecoverSpatialIn
84020 64 65 78 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  dex, 0, 0);.    
84030 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
84040 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 68  unction (db, "Ch
84050 65 63 6b 53 70 61 74 69 61 6c 49 6e 64 65 78 22  eckSpatialIndex"
84060 2c 20 30 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 0, SQLITE_ANY,
84070 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
84080 5f 43 68 65 63 6b 53 70 61 74 69 61 6c 49 6e 64  _CheckSpatialInd
84090 65 78 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ex, 0, 0);.    s
840a0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
840b0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 68 65  nction (db, "Che
840c0 63 6b 53 70 61 74 69 61 6c 49 6e 64 65 78 22 2c  ckSpatialIndex",
840d0 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
840e0 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
840f0 43 68 65 63 6b 53 70 61 74 69 61 6c 49 6e 64 65  CheckSpatialInde
84100 78 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  x, 0, 0);.    sq
84110 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
84120 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 72 65 61  ction (db, "Crea
84130 74 65 53 70 61 74 69 61 6c 49 6e 64 65 78 22 2c  teSpatialIndex",
84140 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
84150 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
84160 43 72 65 61 74 65 53 70 61 74 69 61 6c 49 6e 64  CreateSpatialInd
84170 65 78 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ex, 0, 0);.    s
84180 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
84190 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 72 65  nction (db, "Cre
841a0 61 74 65 4d 62 72 43 61 63 68 65 22 2c 20 32 2c  ateMbrCache", 2,
841b0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
841c0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43 72 65  ...     fnct_Cre
841d0 61 74 65 4d 62 72 43 61 63 68 65 2c 20 30 2c 20  ateMbrCache, 0, 
841e0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
841f0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
84200 28 64 62 2c 20 22 44 69 73 61 62 6c 65 53 70 61  (db, "DisableSpa
84210 74 69 61 6c 49 6e 64 65 78 22 2c 20 32 2c 20 53  tialIndex", 2, S
84220 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
84230 09 20 20 20 20 20 66 6e 63 74 5f 44 69 73 61 62  .     fnct_Disab
84240 6c 65 53 70 61 74 69 61 6c 49 6e 64 65 78 2c 20  leSpatialIndex, 
84250 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
84260 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
84270 6f 6e 20 28 64 62 2c 20 22 52 65 62 75 69 6c 64  on (db, "Rebuild
84280 47 65 6f 6d 65 74 72 79 54 72 69 67 67 65 72 73  GeometryTriggers
84290 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
842a0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
842b0 74 5f 52 65 62 75 69 6c 64 47 65 6f 6d 65 74 72  t_RebuildGeometr
842c0 79 54 72 69 67 67 65 72 73 2c 20 30 2c 20 30 29  yTriggers, 0, 0)
842d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
842e0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
842f0 62 2c 20 22 43 72 65 61 74 65 54 6f 70 6f 6c 6f  b, "CreateTopolo
84300 67 79 54 61 62 6c 65 73 22 2c 20 32 2c 20 53 51  gyTables", 2, SQ
84310 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
84320 20 20 20 20 20 66 6e 63 74 5f 43 72 65 61 74 65       fnct_Create
84330 54 6f 70 6f 6c 6f 67 79 54 61 62 6c 65 73 2c 20  TopologyTables, 
84340 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
84350 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
84360 6f 6e 20 28 64 62 2c 20 22 43 72 65 61 74 65 54  on (db, "CreateT
84370 6f 70 6f 6c 6f 67 79 54 61 62 6c 65 73 22 2c 20  opologyTables", 
84380 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  3, SQLITE_ANY, 0
84390 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43  ,....     fnct_C
843a0 72 65 61 74 65 54 6f 70 6f 6c 6f 67 79 54 61 62  reateTopologyTab
843b0 6c 65 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  les, 0, 0);.    
843c0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
843d0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 55 70  unction (db, "Up
843e0 64 61 74 65 4c 61 79 65 72 53 74 61 74 69 73 74  dateLayerStatist
843f0 69 63 73 22 2c 20 30 2c 20 53 51 4c 49 54 45 5f  ics", 0, SQLITE_
84400 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
84410 66 6e 63 74 5f 55 70 64 61 74 65 4c 61 79 65 72  fnct_UpdateLayer
84420 53 74 61 74 69 73 74 69 63 73 2c 20 30 2c 20 30  Statistics, 0, 0
84430 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
84440 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
84450 64 62 2c 20 22 55 70 64 61 74 65 4c 61 79 65 72  db, "UpdateLayer
84460 53 74 61 74 69 73 74 69 63 73 22 2c 20 31 2c 20  Statistics", 1, 
84470 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
84480 09 09 20 20 20 20 20 66 6e 63 74 5f 55 70 64 61  ..     fnct_Upda
84490 74 65 4c 61 79 65 72 53 74 61 74 69 73 74 69 63  teLayerStatistic
844a0 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  s, 0, 0);.    sq
844b0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
844c0 63 74 69 6f 6e 20 28 64 62 2c 20 22 55 70 64 61  ction (db, "Upda
844d0 74 65 4c 61 79 65 72 53 74 61 74 69 73 74 69 63  teLayerStatistic
844e0 73 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e  s", 2, SQLITE_AN
844f0 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
84500 63 74 5f 55 70 64 61 74 65 4c 61 79 65 72 53 74  ct_UpdateLayerSt
84510 61 74 69 73 74 69 63 73 2c 20 30 2c 20 30 29 3b  atistics, 0, 0);
84520 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
84530 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
84540 2c 20 22 41 73 54 65 78 74 22 2c 20 31 2c 20 53  , "AsText", 1, S
84550 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
84560 63 74 5f 41 73 54 65 78 74 2c 20 30 2c 20 30 29  ct_AsText, 0, 0)
84570 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
84580 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
84590 62 2c 20 22 53 54 5f 41 73 54 65 78 74 22 2c 20  b, "ST_AsText", 
845a0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
845b0 2c 20 66 6e 63 74 5f 41 73 54 65 78 74 2c 0a 09  , fnct_AsText,..
845c0 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20  ..     0, 0);.  
845d0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
845e0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
845f0 41 73 57 6b 74 22 2c 20 31 2c 20 53 51 4c 49 54  AsWkt", 1, SQLIT
84600 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41  E_ANY, 0, fnct_A
84610 73 57 6b 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  sWkt, 0, 0);.   
84620 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
84630 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 41  function (db, "A
84640 73 57 6b 74 22 2c 20 32 2c 20 53 51 4c 49 54 45  sWkt", 2, SQLITE
84650 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41 73  _ANY, 0, fnct_As
84660 57 6b 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Wkt, 0, 0);.    
84670 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
84680 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 41 73  unction (db, "As
84690 53 76 67 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  Svg", 1, SQLITE_
846a0 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41 73 53  ANY, 0, fnct_AsS
846b0 76 67 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  vg1, 0, 0);.    
846c0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
846d0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 41 73  unction (db, "As
846e0 53 76 67 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  Svg", 2, SQLITE_
846f0 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41 73 53  ANY, 0, fnct_AsS
84700 76 67 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  vg2, 0, 0);.    
84710 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
84720 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 41 73  unction (db, "As
84730 53 76 67 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f  Svg", 3, SQLITE_
84740 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41 73 53  ANY, 0, fnct_AsS
84750 76 67 33 2c 20 30 2c 20 30 29 3b 0a 0a 23 69 66  vg3, 0, 0);..#if
84760 6e 64 65 66 20 4f 4d 49 54 5f 50 52 4f 4a 09 09  ndef OMIT_PROJ..
84770 2f 2a 20 50 52 4f 4a 2e 34 20 69 73 20 73 74 72  /* PROJ.4 is str
84780 69 63 74 6c 79 20 72 65 71 75 69 72 65 64 20 74  ictly required t
84790 6f 20 73 75 70 70 6f 72 74 20 4b 4d 4c 20 2a 2f  o support KML */
847a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
847b0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
847c0 2c 20 22 41 73 4b 6d 6c 22 2c 20 31 2c 20 53 51  , "AsKml", 1, SQ
847d0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
847e0 74 5f 41 73 4b 6d 6c 2c 20 30 2c 20 30 29 3b 0a  t_AsKml, 0, 0);.
847f0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
84800 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
84810 20 22 41 73 4b 6d 6c 22 2c 20 32 2c 20 53 51 4c   "AsKml", 2, SQL
84820 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
84830 5f 41 73 4b 6d 6c 2c 20 30 2c 20 30 29 3b 0a 20  _AsKml, 0, 0);. 
84840 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
84850 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
84860 22 41 73 4b 6d 6c 22 2c 20 33 2c 20 53 51 4c 49  "AsKml", 3, SQLI
84870 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
84880 41 73 4b 6d 6c 2c 20 30 2c 20 30 29 3b 0a 20 20  AsKml, 0, 0);.  
84890 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
848a0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
848b0 41 73 4b 6d 6c 22 2c 20 34 2c 20 53 51 4c 49 54  AsKml", 4, SQLIT
848c0 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41  E_ANY, 0, fnct_A
848d0 73 4b 6d 6c 2c 20 30 2c 20 30 29 3b 0a 23 65 6e  sKml, 0, 0);.#en
848e0 64 69 66 20 2f 2a 20 65 6e 64 20 69 6e 63 6c 75  dif /* end inclu
848f0 64 69 6e 67 20 50 52 4f 4a 2e 34 20 2a 2f 0a 0a  ding PROJ.4 */..
84900 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
84910 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
84920 20 22 41 73 47 6d 6c 22 2c 20 31 2c 20 53 51 4c   "AsGml", 1, SQL
84930 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
84940 5f 41 73 47 6d 6c 2c 20 30 2c 20 30 29 3b 0a 20  _AsGml, 0, 0);. 
84950 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
84960 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
84970 22 41 73 47 6d 6c 22 2c 20 32 2c 20 53 51 4c 49  "AsGml", 2, SQLI
84980 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
84990 41 73 47 6d 6c 2c 20 30 2c 20 30 29 3b 0a 20 20  AsGml, 0, 0);.  
849a0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
849b0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
849c0 41 73 47 6d 6c 22 2c 20 33 2c 20 53 51 4c 49 54  AsGml", 3, SQLIT
849d0 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41  E_ANY, 0, fnct_A
849e0 73 47 6d 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20  sGml, 0, 0);.   
849f0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
84a00 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47  function (db, "G
84a10 65 6f 6d 46 72 6f 6d 47 6d 6c 22 2c 20 31 2c 20  eomFromGml", 1, 
84a20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
84a30 09 09 20 20 20 20 20 66 6e 63 74 5f 46 72 6f 6d  ..     fnct_From
84a40 47 6d 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Gml, 0, 0);.    
84a50 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
84a60 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 41 73  unction (db, "As
84a70 47 65 6f 4a 53 4f 4e 22 2c 20 31 2c 20 53 51 4c  GeoJSON", 1, SQL
84a80 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
84a90 5f 41 73 47 65 6f 4a 53 4f 4e 2c 0a 09 09 09 20  _AsGeoJSON,.... 
84aa0 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73      0, 0);.    s
84ab0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
84ac0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 41 73 47  nction (db, "AsG
84ad0 65 6f 4a 53 4f 4e 22 2c 20 32 2c 20 53 51 4c 49  eoJSON", 2, SQLI
84ae0 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
84af0 41 73 47 65 6f 4a 53 4f 4e 2c 0a 09 09 09 20 20  AsGeoJSON,....  
84b00 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71     0, 0);.    sq
84b10 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
84b20 63 74 69 6f 6e 20 28 64 62 2c 20 22 41 73 47 65  ction (db, "AsGe
84b30 6f 4a 53 4f 4e 22 2c 20 33 2c 20 53 51 4c 49 54  oJSON", 3, SQLIT
84b40 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 41  E_ANY, 0, fnct_A
84b50 73 47 65 6f 4a 53 4f 4e 2c 0a 09 09 09 20 20 20  sGeoJSON,....   
84b60 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c    0, 0);.    sql
84b70 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
84b80 74 69 6f 6e 20 28 64 62 2c 20 22 47 65 6f 6d 46  tion (db, "GeomF
84b90 72 6f 6d 47 65 6f 4a 53 4f 4e 22 2c 20 31 2c 20  romGeoJSON", 1, 
84ba0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
84bb0 09 09 20 20 20 20 20 66 6e 63 74 5f 46 72 6f 6d  ..     fnct_From
84bc0 47 65 6f 4a 53 4f 4e 2c 20 30 2c 20 30 29 3b 0a  GeoJSON, 0, 0);.
84bd0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
84be0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
84bf0 20 22 47 65 6f 6d 46 72 6f 6d 4b 6d 6c 22 2c 20   "GeomFromKml", 
84c00 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
84c10 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 46  ,....     fnct_F
84c20 72 6f 6d 4b 6d 6c 2c 20 30 2c 20 30 29 3b 0a 20  romKml, 0, 0);. 
84c30 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
84c40 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
84c50 22 41 73 46 47 46 22 2c 20 32 2c 20 53 51 4c 49  "AsFGF", 2, SQLI
84c60 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
84c70 41 73 46 47 46 2c 20 30 2c 20 30 29 3b 0a 20 20  AsFGF, 0, 0);.  
84c80 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
84c90 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
84ca0 47 65 6f 6d 46 72 6f 6d 45 57 4b 42 22 2c 20 31  GeomFromEWKB", 1
84cb0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
84cc0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 46 72  ....     fnct_Fr
84cd0 6f 6d 45 57 4b 42 2c 20 30 2c 20 30 29 3b 0a 20  omEWKB, 0, 0);. 
84ce0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
84cf0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
84d00 22 41 73 45 57 4b 42 22 2c 20 31 2c 20 53 51 4c  "AsEWKB", 1, SQL
84d10 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
84d20 5f 54 6f 45 57 4b 42 2c 20 30 2c 20 30 29 3b 0a  _ToEWKB, 0, 0);.
84d30 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
84d40 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
84d50 20 22 41 73 45 57 4b 54 22 2c 20 31 2c 20 53 51   "AsEWKT", 1, SQ
84d60 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
84d70 74 5f 54 6f 45 57 4b 54 2c 20 30 2c 20 30 29 3b  t_ToEWKT, 0, 0);
84d80 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
84d90 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
84da0 2c 20 22 47 65 6f 6d 46 72 6f 6d 45 57 4b 54 22  , "GeomFromEWKT"
84db0 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
84dc0 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
84dd0 5f 46 72 6f 6d 45 57 4b 54 2c 20 30 2c 20 30 29  _FromEWKT, 0, 0)
84de0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
84df0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
84e00 62 2c 20 22 41 73 42 69 6e 61 72 79 22 2c 20 31  b, "AsBinary", 1
84e10 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
84e20 20 66 6e 63 74 5f 41 73 42 69 6e 61 72 79 2c 20   fnct_AsBinary, 
84e30 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20  0,....     0);. 
84e40 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
84e50 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
84e60 22 53 54 5f 41 73 42 69 6e 61 72 79 22 2c 20 31  "ST_AsBinary", 1
84e70 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
84e80 20 66 6e 63 74 5f 41 73 42 69 6e 61 72 79 2c 0a   fnct_AsBinary,.
84e90 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20  ...     0, 0);. 
84ea0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
84eb0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
84ec0 22 47 65 6f 6d 46 72 6f 6d 54 65 78 74 22 2c 20  "GeomFromText", 
84ed0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
84ee0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 47  ,....     fnct_G
84ef0 65 6f 6d 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c  eomFromText1, 0,
84f00 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
84f10 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
84f20 20 28 64 62 2c 20 22 47 65 6f 6d 46 72 6f 6d 54   (db, "GeomFromT
84f30 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  ext", 2, SQLITE_
84f40 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
84f50 66 6e 63 74 5f 47 65 6f 6d 46 72 6f 6d 54 65 78  fnct_GeomFromTex
84f60 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  t2, 0, 0);.    s
84f70 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
84f80 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47 65 6f  nction (db, "Geo
84f90 6d 65 74 72 79 46 72 6f 6d 54 65 78 74 22 2c 20  metryFromText", 
84fa0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
84fb0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 47  ,....     fnct_G
84fc0 65 6f 6d 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c  eomFromText1, 0,
84fd0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
84fe0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
84ff0 20 28 64 62 2c 20 22 47 65 6f 6d 65 74 72 79 46   (db, "GeometryF
85000 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c  romText", 2, SQL
85010 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
85020 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 46 72 6f      fnct_GeomFro
85030 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20  mText2, 0, 0);. 
85040 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
85050 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
85060 22 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54 65 78  "GeomCollFromTex
85070 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  t", 1, SQLITE_AN
85080 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
85090 63 74 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54  ct_GeomCollFromT
850a0 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext1, 0, 0);.   
850b0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
850c0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47  function (db, "G
850d0 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54 65 78 74 22  eomCollFromText"
850e0 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
850f0 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
85100 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54 65 78  _GeomCollFromTex
85110 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  t2, 0, 0);.    s
85120 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
85130 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47 65 6f  nction (db, "Geo
85140 6d 65 74 72 79 43 6f 6c 6c 65 63 74 69 6f 6e 46  metryCollectionF
85150 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c  romText", 1, SQL
85160 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
85170 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c      fnct_GeomCol
85180 6c 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30  lFromText1, 0, 0
85190 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
851a0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
851b0 64 62 2c 20 22 47 65 6f 6d 65 74 72 79 43 6f 6c  db, "GeometryCol
851c0 6c 65 63 74 69 6f 6e 46 72 6f 6d 54 65 78 74 22  lectionFromText"
851d0 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
851e0 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
851f0 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54 65 78  _GeomCollFromTex
85200 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  t2, 0, 0);.    s
85210 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
85220 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 50 6f 69  nction (db, "Poi
85230 6e 74 46 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20  ntFromText", 1, 
85240 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
85250 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 69 6e  ..     fnct_Poin
85260 74 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30  tFromText1, 0, 0
85270 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
85280 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
85290 64 62 2c 20 22 50 6f 69 6e 74 46 72 6f 6d 54 65  db, "PointFromTe
852a0 78 74 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  xt", 2, SQLITE_A
852b0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
852c0 6e 63 74 5f 50 6f 69 6e 74 46 72 6f 6d 54 65 78  nct_PointFromTex
852d0 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  t2, 0, 0);.    s
852e0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
852f0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4c 69 6e  nction (db, "Lin
85300 65 46 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20 53  eFromText", 1, S
85310 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
85320 09 20 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 46  .     fnct_LineF
85330 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30 29 3b  romText1, 0, 0);
85340 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
85350 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
85360 2c 20 22 4c 69 6e 65 46 72 6f 6d 54 65 78 74 22  , "LineFromText"
85370 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
85380 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
85390 5f 4c 69 6e 65 46 72 6f 6d 54 65 78 74 32 2c 20  _LineFromText2, 
853a0 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
853b0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
853c0 6f 6e 20 28 64 62 2c 20 22 4c 69 6e 65 53 74 72  on (db, "LineStr
853d0 69 6e 67 46 72 6f 6d 54 65 78 74 22 2c 20 31 2c  ingFromText", 1,
853e0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
853f0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4c 69 6e  ...     fnct_Lin
85400 65 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30  eFromText1, 0, 0
85410 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
85420 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
85430 64 62 2c 20 22 4c 69 6e 65 53 74 72 69 6e 67 46  db, "LineStringF
85440 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c  romText", 2, SQL
85450 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
85460 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 46 72 6f      fnct_LineFro
85470 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20  mText2, 0, 0);. 
85480 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
85490 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
854a0 22 50 6f 6c 79 46 72 6f 6d 54 65 78 74 22 2c 20  "PolyFromText", 
854b0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
854c0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 50  ,....     fnct_P
854d0 6f 6c 79 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c  olyFromText1, 0,
854e0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
854f0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
85500 20 28 64 62 2c 20 22 50 6f 6c 79 46 72 6f 6d 54   (db, "PolyFromT
85510 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  ext", 2, SQLITE_
85520 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
85530 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 54 65 78  fnct_PolyFromTex
85540 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  t2, 0, 0);.    s
85550 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
85560 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 50 6f 6c  nction (db, "Pol
85570 79 67 6f 6e 46 72 6f 6d 54 65 78 74 22 2c 20 31  ygonFromText", 1
85580 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
85590 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f  ....     fnct_Po
855a0 6c 79 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20  lyFromText1, 0, 
855b0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
855c0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
855d0 28 64 62 2c 20 22 50 6f 6c 79 67 6f 6e 46 72 6f  (db, "PolygonFro
855e0 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54  mText", 2, SQLIT
855f0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
85600 20 20 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 54    fnct_PolyFromT
85610 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext2, 0, 0);.   
85620 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
85630 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d  function (db, "M
85640 50 6f 69 6e 74 46 72 6f 6d 54 65 78 74 22 2c 20  PointFromText", 
85650 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
85660 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d  ,....     fnct_M
85670 50 6f 69 6e 74 46 72 6f 6d 54 65 78 74 31 2c 20  PointFromText1, 
85680 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
85690 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
856a0 6f 6e 20 28 64 62 2c 20 22 4d 50 6f 69 6e 74 46  on (db, "MPointF
856b0 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c  romText", 2, SQL
856c0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
856d0 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e 74 46      fnct_MPointF
856e0 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b  romText2, 0, 0);
856f0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
85700 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
85710 2c 20 22 4d 75 6c 74 69 50 6f 69 6e 74 46 72 6f  , "MultiPointFro
85720 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54  mText", 1, SQLIT
85730 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
85740 20 20 66 6e 63 74 5f 4d 50 6f 69 6e 74 46 72 6f    fnct_MPointFro
85750 6d 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20  mText1, 0, 0);. 
85760 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
85770 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
85780 22 4d 75 6c 74 69 50 6f 69 6e 74 46 72 6f 6d 54  "MultiPointFromT
85790 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  ext", 2, SQLITE_
857a0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
857b0 66 6e 63 74 5f 4d 50 6f 69 6e 74 46 72 6f 6d 54  fnct_MPointFromT
857c0 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext2, 0, 0);.   
857d0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
857e0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d  function (db, "M
857f0 4c 69 6e 65 46 72 6f 6d 54 65 78 74 22 2c 20 31  LineFromText", 1
85800 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
85810 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 4c  ....     fnct_ML
85820 69 6e 65 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c  ineFromText1, 0,
85830 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
85840 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
85850 20 28 64 62 2c 20 22 4d 4c 69 6e 65 46 72 6f 6d   (db, "MLineFrom
85860 54 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54 45  Text", 2, SQLITE
85870 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
85880 20 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d 54   fnct_MLineFromT
85890 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext2, 0, 0);.   
858a0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
858b0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d  function (db, "M
858c0 75 6c 74 69 4c 69 6e 65 53 74 72 69 6e 67 46 72  ultiLineStringFr
858d0 6f 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49  omText", 1, SQLI
858e0 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
858f0 20 20 20 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f     fnct_MLineFro
85900 6d 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20  mText1, 0, 0);. 
85910 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
85920 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
85930 22 4d 75 6c 74 69 4c 69 6e 65 53 74 72 69 6e 67  "MultiLineString
85940 46 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51  FromText", 2, SQ
85950 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
85960 20 20 20 20 20 66 6e 63 74 5f 4d 4c 69 6e 65 46       fnct_MLineF
85970 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b  romText2, 0, 0);
85980 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
85990 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
859a0 2c 20 22 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74  , "MPolyFromText
859b0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
859c0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
859d0 74 5f 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74 31  t_MPolyFromText1
859e0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
859f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
85a00 74 69 6f 6e 20 28 64 62 2c 20 22 4d 50 6f 6c 79  tion (db, "MPoly
85a10 46 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51  FromText", 2, SQ
85a20 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
85a30 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 6c 79 46       fnct_MPolyF
85a40 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b  romText2, 0, 0);
85a50 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
85a60 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
85a70 2c 20 22 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 46  , "MultiPolygonF
85a80 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c  romText", 1, SQL
85a90 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
85aa0 20 20 20 20 66 6e 63 74 5f 4d 50 6f 6c 79 46 72      fnct_MPolyFr
85ab0 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a  omText1, 0, 0);.
85ac0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
85ad0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
85ae0 20 22 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 46 72   "MultiPolygonFr
85af0 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c 49  omText", 2, SQLI
85b00 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
85b10 20 20 20 66 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f     fnct_MPolyFro
85b20 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20  mText2, 0, 0);. 
85b30 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
85b40 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
85b50 22 47 65 6f 6d 46 72 6f 6d 57 4b 42 22 2c 20 31  "GeomFromWKB", 1
85b60 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
85b70 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 47 65  ....     fnct_Ge
85b80 6f 6d 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30  omFromWkb1, 0, 0
85b90 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
85ba0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
85bb0 64 62 2c 20 22 47 65 6f 6d 46 72 6f 6d 57 4b 42  db, "GeomFromWKB
85bc0 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
85bd0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
85be0 74 5f 47 65 6f 6d 46 72 6f 6d 57 6b 62 32 2c 20  t_GeomFromWkb2, 
85bf0 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
85c00 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
85c10 6f 6e 20 28 64 62 2c 20 22 47 65 6f 6d 65 74 72  on (db, "Geometr
85c20 79 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20 53 51  yFromWKB", 1, SQ
85c30 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
85c40 20 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 46 72       fnct_GeomFr
85c50 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29 3b 0a 20  omWkb1, 0, 0);. 
85c60 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
85c70 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
85c80 22 47 65 6f 6d 65 74 72 79 46 72 6f 6d 57 4b 42  "GeometryFromWKB
85c90 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
85ca0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
85cb0 74 5f 47 65 6f 6d 46 72 6f 6d 57 6b 62 32 2c 20  t_GeomFromWkb2, 
85cc0 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
85cd0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
85ce0 6f 6e 20 28 64 62 2c 20 22 47 65 6f 6d 43 6f 6c  on (db, "GeomCol
85cf0 6c 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20 53 51  lFromWKB", 1, SQ
85d00 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
85d10 20 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 43 6f       fnct_GeomCo
85d20 6c 6c 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30  llFromWkb1, 0, 0
85d30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
85d40 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
85d50 64 62 2c 20 22 47 65 6f 6d 43 6f 6c 6c 46 72 6f  db, "GeomCollFro
85d60 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45  mWKB", 2, SQLITE
85d70 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
85d80 20 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c 6c 46 72   fnct_GeomCollFr
85d90 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b 0a 20  omWkb2, 0, 0);. 
85da0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
85db0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
85dc0 22 47 65 6f 6d 65 74 72 79 43 6f 6c 6c 65 63 74  "GeometryCollect
85dd0 69 6f 6e 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20  ionFromWKB", 1, 
85de0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
85df0 09 09 20 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d  ..     fnct_Geom
85e00 43 6f 6c 6c 46 72 6f 6d 57 6b 62 31 2c 20 30 2c  CollFromWkb1, 0,
85e10 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
85e20 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
85e30 20 28 64 62 2c 20 22 47 65 6f 6d 65 74 72 79 43   (db, "GeometryC
85e40 6f 6c 6c 65 63 74 69 6f 6e 46 72 6f 6d 57 4b 42  ollectionFromWKB
85e50 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
85e60 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
85e70 74 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 57 6b  t_GeomCollFromWk
85e80 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b2, 0, 0);.    s
85e90 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
85ea0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 50 6f 69  nction (db, "Poi
85eb0 6e 74 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20 53  ntFromWKB", 1, S
85ec0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
85ed0 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 69 6e 74  .     fnct_Point
85ee0 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29 3b  FromWkb1, 0, 0);
85ef0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
85f00 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
85f10 2c 20 22 50 6f 69 6e 74 46 72 6f 6d 57 4b 42 22  , "PointFromWKB"
85f20 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
85f30 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
85f40 5f 50 6f 69 6e 74 46 72 6f 6d 57 6b 62 32 2c 20  _PointFromWkb2, 
85f50 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
85f60 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
85f70 6f 6e 20 28 64 62 2c 20 22 4c 69 6e 65 46 72 6f  on (db, "LineFro
85f80 6d 57 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54 45  mWKB", 1, SQLITE
85f90 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
85fa0 20 66 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d 57 6b   fnct_LineFromWk
85fb0 62 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b1, 0, 0);.    s
85fc0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
85fd0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4c 69 6e  nction (db, "Lin
85fe0 65 46 72 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51  eFromWKB", 2, SQ
85ff0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
86000 20 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 46 72       fnct_LineFr
86010 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b 0a 20  omWkb2, 0, 0);. 
86020 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
86030 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
86040 22 4c 69 6e 65 53 74 72 69 6e 67 46 72 6f 6d 57  "LineStringFromW
86050 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  KB", 1, SQLITE_A
86060 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
86070 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d 57 6b 62 31  nct_LineFromWkb1
86080 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
86090 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
860a0 74 69 6f 6e 20 28 64 62 2c 20 22 4c 69 6e 65 53  tion (db, "LineS
860b0 74 72 69 6e 67 46 72 6f 6d 57 4b 42 22 2c 20 32  tringFromWKB", 2
860c0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
860d0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4c 69  ....     fnct_Li
860e0 6e 65 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30  neFromWkb2, 0, 0
860f0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
86100 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
86110 64 62 2c 20 22 50 6f 6c 79 46 72 6f 6d 57 4b 42  db, "PolyFromWKB
86120 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
86130 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
86140 74 5f 50 6f 6c 79 46 72 6f 6d 57 6b 62 31 2c 20  t_PolyFromWkb1, 
86150 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
86160 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
86170 6f 6e 20 28 64 62 2c 20 22 50 6f 6c 79 46 72 6f  on (db, "PolyFro
86180 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45  mWKB", 2, SQLITE
86190 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
861a0 20 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 57 6b   fnct_PolyFromWk
861b0 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b2, 0, 0);.    s
861c0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
861d0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 50 6f 6c  nction (db, "Pol
861e0 79 67 6f 6e 46 72 6f 6d 57 4b 42 22 2c 20 31 2c  ygonFromWKB", 1,
861f0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
86200 09 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 6c  ...     fnct_Pol
86210 79 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29  yFromWkb1, 0, 0)
86220 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
86230 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
86240 62 2c 20 22 50 6f 6c 79 67 6f 6e 46 72 6f 6d 57  b, "PolygonFromW
86250 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  KB", 2, SQLITE_A
86260 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
86270 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 57 6b 62 32  nct_PolyFromWkb2
86280 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
86290 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
862a0 74 69 6f 6e 20 28 64 62 2c 20 22 4d 50 6f 69 6e  tion (db, "MPoin
862b0 74 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20 53 51  tFromWKB", 1, SQ
862c0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
862d0 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e 74       fnct_MPoint
862e0 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29 3b  FromWkb1, 0, 0);
862f0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
86300 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
86310 2c 20 22 4d 50 6f 69 6e 74 46 72 6f 6d 57 4b 42  , "MPointFromWKB
86320 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
86330 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
86340 74 5f 4d 50 6f 69 6e 74 46 72 6f 6d 57 6b 62 32  t_MPointFromWkb2
86350 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
86360 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
86370 74 69 6f 6e 20 28 64 62 2c 20 22 4d 75 6c 74 69  tion (db, "Multi
86380 50 6f 69 6e 74 46 72 6f 6d 57 4b 42 22 2c 20 31  PointFromWKB", 1
86390 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
863a0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 50  ....     fnct_MP
863b0 6f 69 6e 74 46 72 6f 6d 57 6b 62 31 2c 20 30 2c  ointFromWkb1, 0,
863c0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
863d0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
863e0 20 28 64 62 2c 20 22 4d 75 6c 74 69 50 6f 69 6e   (db, "MultiPoin
863f0 74 46 72 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51  tFromWKB", 2, SQ
86400 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
86410 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e 74       fnct_MPoint
86420 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b  FromWkb2, 0, 0);
86430 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
86440 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
86450 2c 20 22 4d 4c 69 6e 65 46 72 6f 6d 57 4b 42 22  , "MLineFromWKB"
86460 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
86470 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
86480 5f 4d 4c 69 6e 65 46 72 6f 6d 57 6b 62 31 2c 20  _MLineFromWkb1, 
86490 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
864a0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
864b0 6f 6e 20 28 64 62 2c 20 22 4d 4c 69 6e 65 46 72  on (db, "MLineFr
864c0 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54  omWKB", 2, SQLIT
864d0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
864e0 20 20 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d    fnct_MLineFrom
864f0 57 6b 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Wkb2, 0, 0);.   
86500 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
86510 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d  function (db, "M
86520 75 6c 74 69 4c 69 6e 65 53 74 72 69 6e 67 46 72  ultiLineStringFr
86530 6f 6d 57 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54  omWKB", 1, SQLIT
86540 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
86550 20 20 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d    fnct_MLineFrom
86560 57 6b 62 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Wkb1, 0, 0);.   
86570 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
86580 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d  function (db, "M
86590 75 6c 74 69 4c 69 6e 65 53 74 72 69 6e 67 46 72  ultiLineStringFr
865a0 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54  omWKB", 2, SQLIT
865b0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
865c0 20 20 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d    fnct_MLineFrom
865d0 57 6b 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Wkb2, 0, 0);.   
865e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
865f0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d  function (db, "M
86600 50 6f 6c 79 46 72 6f 6d 57 4b 42 22 2c 20 31 2c  PolyFromWKB", 1,
86610 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
86620 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f  ...     fnct_MPo
86630 6c 79 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30  lyFromWkb1, 0, 0
86640 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
86650 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
86660 64 62 2c 20 22 4d 50 6f 6c 79 46 72 6f 6d 57 4b  db, "MPolyFromWK
86670 42 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e  B", 2, SQLITE_AN
86680 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
86690 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d 57 6b 62 32  ct_MPolyFromWkb2
866a0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
866b0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
866c0 74 69 6f 6e 20 28 64 62 2c 20 22 4d 75 6c 74 69  tion (db, "Multi
866d0 50 6f 6c 79 67 6f 6e 46 72 6f 6d 57 4b 42 22 2c  PolygonFromWKB",
866e0 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
866f0 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
86700 4d 50 6f 6c 79 46 72 6f 6d 57 6b 62 31 2c 20 30  MPolyFromWkb1, 0
86710 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
86720 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
86730 6e 20 28 64 62 2c 20 22 4d 75 6c 74 69 50 6f 6c  n (db, "MultiPol
86740 79 67 6f 6e 46 72 6f 6d 57 4b 42 22 2c 20 32 2c  ygonFromWKB", 2,
86750 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
86760 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f  ...     fnct_MPo
86770 6c 79 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30  lyFromWkb2, 0, 0
86780 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
86790 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
867a0 64 62 2c 20 22 53 54 5f 57 4b 54 54 6f 53 51 4c  db, "ST_WKTToSQL
867b0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
867c0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
867d0 74 5f 57 6b 74 54 6f 53 71 6c 2c 20 30 2c 20 30  t_WktToSql, 0, 0
867e0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
867f0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
86800 64 62 2c 20 22 53 54 5f 47 65 6f 6d 46 72 6f 6d  db, "ST_GeomFrom
86810 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54 45  Text", 1, SQLITE
86820 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
86830 20 66 6e 63 74 5f 47 65 6f 6d 46 72 6f 6d 54 65   fnct_GeomFromTe
86840 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xt1, 0, 0);.    
86850 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
86860 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
86870 5f 47 65 6f 6d 46 72 6f 6d 54 65 78 74 22 2c 20  _GeomFromText", 
86880 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
86890 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 47  ,....     fnct_G
868a0 65 6f 6d 46 72 6f 6d 54 65 78 74 32 2c 20 30 2c  eomFromText2, 0,
868b0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
868c0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
868d0 20 28 64 62 2c 20 22 53 54 5f 47 65 6f 6d 65 74   (db, "ST_Geomet
868e0 72 79 46 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20  ryFromText", 1, 
868f0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
86900 09 09 20 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d  ..     fnct_Geom
86910 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30 29  FromText1, 0, 0)
86920 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
86930 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
86940 62 2c 20 22 53 54 5f 47 65 6f 6d 65 74 72 79 46  b, "ST_GeometryF
86950 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c  romText", 2, SQL
86960 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
86970 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 46 72 6f      fnct_GeomFro
86980 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20  mText2, 0, 0);. 
86990 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
869a0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
869b0 22 53 54 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d  "ST_GeomCollFrom
869c0 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54 45  Text", 1, SQLITE
869d0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
869e0 20 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c 6c 46 72   fnct_GeomCollFr
869f0 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a  omText1, 0, 0);.
86a00 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
86a10 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
86a20 20 22 53 54 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f   "ST_GeomCollFro
86a30 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54  mText", 2, SQLIT
86a40 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
86a50 20 20 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c 6c 46    fnct_GeomCollF
86a60 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b  romText2, 0, 0);
86a70 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
86a80 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
86a90 2c 20 22 53 54 5f 47 65 6f 6d 65 74 72 79 43 6f  , "ST_GeometryCo
86aa0 6c 6c 65 63 74 69 6f 6e 46 72 6f 6d 54 65 78 74  llectionFromText
86ab0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
86ac0 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 66 6e 63  ,....     0, fnc
86ad0 74 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 54 65  t_GeomCollFromTe
86ae0 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xt1, 0, 0);.    
86af0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
86b00 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
86b10 5f 47 65 6f 6d 65 74 72 79 43 6f 6c 6c 65 63 74  _GeometryCollect
86b20 69 6f 6e 46 72 6f 6d 54 65 78 74 22 2c 20 32 2c  ionFromText", 2,
86b30 20 53 51 4c 49 54 45 5f 41 4e 59 2c 0a 09 09 09   SQLITE_ANY,....
86b40 20 20 20 20 20 30 2c 20 66 6e 63 74 5f 47 65 6f       0, fnct_Geo
86b50 6d 43 6f 6c 6c 46 72 6f 6d 54 65 78 74 32 2c 20  mCollFromText2, 
86b60 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
86b70 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
86b80 6f 6e 20 28 64 62 2c 20 22 53 54 5f 50 6f 69 6e  on (db, "ST_Poin
86b90 74 46 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20 53  tFromText", 1, S
86ba0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
86bb0 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 69 6e 74  .     fnct_Point
86bc0 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30 29  FromText1, 0, 0)
86bd0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
86be0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
86bf0 62 2c 20 22 53 54 5f 50 6f 69 6e 74 46 72 6f 6d  b, "ST_PointFrom
86c00 54 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54 45  Text", 2, SQLITE
86c10 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
86c20 20 66 6e 63 74 5f 50 6f 69 6e 74 46 72 6f 6d 54   fnct_PointFromT
86c30 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext2, 0, 0);.   
86c40 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
86c50 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
86c60 54 5f 4c 69 6e 65 46 72 6f 6d 54 65 78 74 22 2c  T_LineFromText",
86c70 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
86c80 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
86c90 4c 69 6e 65 46 72 6f 6d 54 65 78 74 31 2c 20 30  LineFromText1, 0
86ca0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
86cb0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
86cc0 6e 20 28 64 62 2c 20 22 53 54 5f 4c 69 6e 65 46  n (db, "ST_LineF
86cd0 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c  romText", 2, SQL
86ce0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
86cf0 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 46 72 6f      fnct_LineFro
86d00 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20  mText2, 0, 0);. 
86d10 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
86d20 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
86d30 22 53 54 5f 4c 69 6e 65 53 74 72 69 6e 67 46 72  "ST_LineStringFr
86d40 6f 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49  omText", 1, SQLI
86d50 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
86d60 20 20 20 66 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d     fnct_LineFrom
86d70 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20  Text1, 0, 0);.  
86d80 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
86d90 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
86da0 53 54 5f 4c 69 6e 65 53 74 72 69 6e 67 46 72 6f  ST_LineStringFro
86db0 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54  mText", 2, SQLIT
86dc0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
86dd0 20 20 66 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d 54    fnct_LineFromT
86de0 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext2, 0, 0);.   
86df0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
86e00 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
86e10 54 5f 50 6f 6c 79 46 72 6f 6d 54 65 78 74 22 2c  T_PolyFromText",
86e20 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
86e30 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
86e40 50 6f 6c 79 46 72 6f 6d 54 65 78 74 31 2c 20 30  PolyFromText1, 0
86e50 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
86e60 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
86e70 6e 20 28 64 62 2c 20 22 53 54 5f 50 6f 6c 79 46  n (db, "ST_PolyF
86e80 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c  romText", 2, SQL
86e90 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
86ea0 20 20 20 20 66 6e 63 74 5f 50 6f 6c 79 46 72 6f      fnct_PolyFro
86eb0 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20  mText2, 0, 0);. 
86ec0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
86ed0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
86ee0 22 53 54 5f 50 6f 6c 79 67 6f 6e 46 72 6f 6d 54  "ST_PolygonFromT
86ef0 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  ext", 1, SQLITE_
86f00 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
86f10 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 54 65 78  fnct_PolyFromTex
86f20 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  t1, 0, 0);.    s
86f30 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
86f40 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
86f50 50 6f 6c 79 67 6f 6e 46 72 6f 6d 54 65 78 74 22  PolygonFromText"
86f60 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
86f70 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
86f80 5f 50 6f 6c 79 46 72 6f 6d 54 65 78 74 32 2c 20  _PolyFromText2, 
86f90 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
86fa0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
86fb0 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d 50 6f 69  on (db, "ST_MPoi
86fc0 6e 74 46 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20  ntFromText", 1, 
86fd0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
86fe0 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69  ..     fnct_MPoi
86ff0 6e 74 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20  ntFromText1, 0, 
87000 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
87010 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
87020 28 64 62 2c 20 22 53 54 5f 4d 50 6f 69 6e 74 46  (db, "ST_MPointF
87030 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53 51 4c  romText", 2, SQL
87040 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
87050 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e 74 46      fnct_MPointF
87060 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20 30 29 3b  romText2, 0, 0);
87070 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
87080 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
87090 2c 20 22 53 54 5f 4d 75 6c 74 69 50 6f 69 6e 74  , "ST_MultiPoint
870a0 46 72 6f 6d 54 65 78 74 22 2c 20 31 2c 20 53 51  FromText", 1, SQ
870b0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
870c0 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e 74       fnct_MPoint
870d0 46 72 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30 29  FromText1, 0, 0)
870e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
870f0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
87100 62 2c 20 22 53 54 5f 4d 75 6c 74 69 50 6f 69 6e  b, "ST_MultiPoin
87110 74 46 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20 53  tFromText", 2, S
87120 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
87130 09 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e  .     fnct_MPoin
87140 74 46 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20 30  tFromText2, 0, 0
87150 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
87160 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
87170 64 62 2c 20 22 53 54 5f 4d 4c 69 6e 65 46 72 6f  db, "ST_MLineFro
87180 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54  mText", 1, SQLIT
87190 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
871a0 20 20 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d    fnct_MLineFrom
871b0 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20  Text1, 0, 0);.  
871c0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
871d0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
871e0 53 54 5f 4d 4c 69 6e 65 46 72 6f 6d 54 65 78 74  ST_MLineFromText
871f0 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
87200 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
87210 74 5f 4d 4c 69 6e 65 46 72 6f 6d 54 65 78 74 32  t_MLineFromText2
87220 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
87230 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
87240 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d 75  tion (db, "ST_Mu
87250 6c 74 69 4c 69 6e 65 53 74 72 69 6e 67 46 72 6f  ltiLineStringFro
87260 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54  mText", 1, SQLIT
87270 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
87280 20 20 66 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d    fnct_MLineFrom
87290 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20  Text1, 0, 0);.  
872a0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
872b0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
872c0 53 54 5f 4d 75 6c 74 69 4c 69 6e 65 53 74 72 69  ST_MultiLineStri
872d0 6e 67 46 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20  ngFromText", 2, 
872e0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
872f0 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 4c 69 6e  ..     fnct_MLin
87300 65 46 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20 30  eFromText2, 0, 0
87310 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
87320 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
87330 64 62 2c 20 22 53 54 5f 4d 50 6f 6c 79 46 72 6f  db, "ST_MPolyFro
87340 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54  mText", 1, SQLIT
87350 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
87360 20 20 66 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d    fnct_MPolyFrom
87370 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20  Text1, 0, 0);.  
87380 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
87390 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
873a0 53 54 5f 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74  ST_MPolyFromText
873b0 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
873c0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
873d0 74 5f 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74 32  t_MPolyFromText2
873e0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
873f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
87400 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d 75  tion (db, "ST_Mu
87410 6c 74 69 50 6f 6c 79 67 6f 6e 46 72 6f 6d 54 65  ltiPolygonFromTe
87420 78 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  xt", 1, SQLITE_A
87430 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
87440 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d 54 65 78  nct_MPolyFromTex
87450 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  t1, 0, 0);.    s
87460 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
87470 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
87480 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 46 72 6f 6d  MultiPolygonFrom
87490 54 65 78 74 22 2c 20 32 2c 20 53 51 4c 49 54 45  Text", 2, SQLITE
874a0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
874b0 20 66 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d 54   fnct_MPolyFromT
874c0 65 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext2, 0, 0);.   
874d0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
874e0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
874f0 54 5f 57 4b 42 54 6f 53 51 4c 22 2c 20 31 2c 20  T_WKBToSQL", 1, 
87500 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
87510 09 09 20 20 20 20 20 66 6e 63 74 5f 57 6b 62 54  ..     fnct_WkbT
87520 6f 53 71 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20  oSql, 0, 0);.   
87530 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
87540 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
87550 54 5f 47 65 6f 6d 46 72 6f 6d 57 4b 42 22 2c 20  T_GeomFromWKB", 
87560 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
87570 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 47  ,....     fnct_G
87580 65 6f 6d 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20  eomFromWkb1, 0, 
87590 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
875a0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
875b0 28 64 62 2c 20 22 53 54 5f 47 65 6f 6d 46 72 6f  (db, "ST_GeomFro
875c0 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45  mWKB", 2, SQLITE
875d0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
875e0 20 66 6e 63 74 5f 47 65 6f 6d 46 72 6f 6d 57 6b   fnct_GeomFromWk
875f0 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b2, 0, 0);.    s
87600 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
87610 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
87620 47 65 6f 6d 65 74 72 79 46 72 6f 6d 57 4b 42 22  GeometryFromWKB"
87630 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
87640 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
87650 5f 47 65 6f 6d 46 72 6f 6d 57 6b 62 31 2c 20 30  _GeomFromWkb1, 0
87660 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
87670 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
87680 6e 20 28 64 62 2c 20 22 53 54 5f 47 65 6f 6d 65  n (db, "ST_Geome
87690 74 72 79 46 72 6f 6d 57 4b 42 22 2c 20 32 2c 20  tryFromWKB", 2, 
876a0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
876b0 09 09 20 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d  ..     fnct_Geom
876c0 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b  FromWkb2, 0, 0);
876d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
876e0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
876f0 2c 20 22 53 54 5f 47 65 6f 6d 43 6f 6c 6c 46 72  , "ST_GeomCollFr
87700 6f 6d 57 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54  omWKB", 1, SQLIT
87710 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
87720 20 20 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c 6c 46    fnct_GeomCollF
87730 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29 3b 0a  romWkb1, 0, 0);.
87740 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
87750 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
87760 20 22 53 54 5f 47 65 6f 6d 43 6f 6c 6c 46 72 6f   "ST_GeomCollFro
87770 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45  mWKB", 2, SQLITE
87780 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
87790 20 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c 6c 46 72   fnct_GeomCollFr
877a0 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b 0a 20  omWkb2, 0, 0);. 
877b0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
877c0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
877d0 22 53 54 5f 47 65 6f 6d 65 74 72 79 43 6f 6c 6c  "ST_GeometryColl
877e0 65 63 74 69 6f 6e 46 72 6f 6d 57 4b 42 22 2c 20  ectionFromWKB", 
877f0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 0a 09  1, SQLITE_ANY,..
87800 09 09 20 20 20 20 20 30 2c 20 66 6e 63 74 5f 47  ..     0, fnct_G
87810 65 6f 6d 43 6f 6c 6c 46 72 6f 6d 57 6b 62 31 2c  eomCollFromWkb1,
87820 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
87830 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
87840 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 47 65 6f  ion (db, "ST_Geo
87850 6d 65 74 72 79 43 6f 6c 6c 65 63 74 69 6f 6e 46  metryCollectionF
87860 72 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49  romWKB", 2, SQLI
87870 54 45 5f 41 4e 59 2c 0a 09 09 09 20 20 20 20 20  TE_ANY,....     
87880 30 2c 20 66 6e 63 74 5f 47 65 6f 6d 43 6f 6c 6c  0, fnct_GeomColl
87890 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b  FromWkb2, 0, 0);
878a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
878b0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
878c0 2c 20 22 53 54 5f 50 6f 69 6e 74 46 72 6f 6d 57  , "ST_PointFromW
878d0 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  KB", 1, SQLITE_A
878e0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
878f0 6e 63 74 5f 50 6f 69 6e 74 46 72 6f 6d 57 6b 62  nct_PointFromWkb
87900 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  1, 0, 0);.    sq
87910 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
87920 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 50  ction (db, "ST_P
87930 6f 69 6e 74 46 72 6f 6d 57 4b 42 22 2c 20 32 2c  ointFromWKB", 2,
87940 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
87950 09 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 69  ...     fnct_Poi
87960 6e 74 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30  ntFromWkb2, 0, 0
87970 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
87980 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
87990 64 62 2c 20 22 53 54 5f 4c 69 6e 65 46 72 6f 6d  db, "ST_LineFrom
879a0 57 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  WKB", 1, SQLITE_
879b0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
879c0 66 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d 57 6b 62  fnct_LineFromWkb
879d0 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  1, 0, 0);.    sq
879e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
879f0 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4c  ction (db, "ST_L
87a00 69 6e 65 46 72 6f 6d 57 4b 42 22 2c 20 32 2c 20  ineFromWKB", 2, 
87a10 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
87a20 09 09 20 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65  ..     fnct_Line
87a30 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b  FromWkb2, 0, 0);
87a40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
87a50 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
87a60 2c 20 22 53 54 5f 4c 69 6e 65 53 74 72 69 6e 67  , "ST_LineString
87a70 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20 53 51 4c  FromWKB", 1, SQL
87a80 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
87a90 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 46 72 6f      fnct_LineFro
87aa0 6d 57 6b 62 31 2c 20 30 2c 20 30 29 3b 0a 20 20  mWkb1, 0, 0);.  
87ab0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
87ac0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
87ad0 53 54 5f 4c 69 6e 65 53 74 72 69 6e 67 46 72 6f  ST_LineStringFro
87ae0 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45  mWKB", 2, SQLITE
87af0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
87b00 20 66 6e 63 74 5f 4c 69 6e 65 46 72 6f 6d 57 6b   fnct_LineFromWk
87b10 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b2, 0, 0);.    s
87b20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
87b30 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
87b40 50 6f 6c 79 46 72 6f 6d 57 4b 42 22 2c 20 31 2c  PolyFromWKB", 1,
87b50 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
87b60 09 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 6c  ...     fnct_Pol
87b70 79 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29  yFromWkb1, 0, 0)
87b80 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
87b90 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
87ba0 62 2c 20 22 53 54 5f 50 6f 6c 79 46 72 6f 6d 57  b, "ST_PolyFromW
87bb0 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  KB", 2, SQLITE_A
87bc0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
87bd0 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d 57 6b 62 32  nct_PolyFromWkb2
87be0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
87bf0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
87c00 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 50 6f  tion (db, "ST_Po
87c10 6c 79 67 6f 6e 46 72 6f 6d 57 4b 42 22 2c 20 31  lygonFromWKB", 1
87c20 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
87c30 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f  ....     fnct_Po
87c40 6c 79 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30  lyFromWkb1, 0, 0
87c50 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
87c60 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
87c70 64 62 2c 20 22 53 54 5f 50 6f 6c 79 67 6f 6e 46  db, "ST_PolygonF
87c80 72 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49  romWKB", 2, SQLI
87c90 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
87ca0 20 20 20 66 6e 63 74 5f 50 6f 6c 79 46 72 6f 6d     fnct_PolyFrom
87cb0 57 6b 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Wkb2, 0, 0);.   
87cc0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
87cd0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
87ce0 54 5f 4d 50 6f 69 6e 74 46 72 6f 6d 57 4b 42 22  T_MPointFromWKB"
87cf0 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
87d00 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
87d10 5f 4d 50 6f 69 6e 74 46 72 6f 6d 57 6b 62 31 2c  _MPointFromWkb1,
87d20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
87d30 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
87d40 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d 50 6f  ion (db, "ST_MPo
87d50 69 6e 74 46 72 6f 6d 57 4b 42 22 2c 20 32 2c 20  intFromWKB", 2, 
87d60 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
87d70 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69  ..     fnct_MPoi
87d80 6e 74 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30  ntFromWkb2, 0, 0
87d90 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
87da0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
87db0 64 62 2c 20 22 53 54 5f 4d 75 6c 74 69 50 6f 69  db, "ST_MultiPoi
87dc0 6e 74 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20 53  ntFromWKB", 1, S
87dd0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
87de0 09 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e  .     fnct_MPoin
87df0 74 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29  tFromWkb1, 0, 0)
87e00 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
87e10 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
87e20 62 2c 20 22 53 54 5f 4d 75 6c 74 69 50 6f 69 6e  b, "ST_MultiPoin
87e30 74 46 72 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51  tFromWKB", 2, SQ
87e40 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
87e50 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 69 6e 74       fnct_MPoint
87e60 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30 29 3b  FromWkb2, 0, 0);
87e70 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
87e80 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
87e90 2c 20 22 53 54 5f 4d 4c 69 6e 65 46 72 6f 6d 57  , "ST_MLineFromW
87ea0 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  KB", 1, SQLITE_A
87eb0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
87ec0 6e 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d 57 6b 62  nct_MLineFromWkb
87ed0 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  1, 0, 0);.    sq
87ee0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
87ef0 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d  ction (db, "ST_M
87f00 4c 69 6e 65 46 72 6f 6d 57 4b 42 22 2c 20 32 2c  LineFromWKB", 2,
87f10 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
87f20 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 4c 69  ...     fnct_MLi
87f30 6e 65 46 72 6f 6d 57 6b 62 32 2c 20 30 2c 20 30  neFromWkb2, 0, 0
87f40 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
87f50 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
87f60 64 62 2c 20 22 53 54 5f 4d 75 6c 74 69 4c 69 6e  db, "ST_MultiLin
87f70 65 53 74 72 69 6e 67 46 72 6f 6d 57 4b 42 22 2c  eStringFromWKB",
87f80 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
87f90 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
87fa0 4d 4c 69 6e 65 46 72 6f 6d 57 6b 62 31 2c 20 30  MLineFromWkb1, 0
87fb0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
87fc0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
87fd0 6e 20 28 64 62 2c 20 22 53 54 5f 4d 75 6c 74 69  n (db, "ST_Multi
87fe0 4c 69 6e 65 53 74 72 69 6e 67 46 72 6f 6d 57 4b  LineStringFromWK
87ff0 42 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e  B", 2, SQLITE_AN
88000 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
88010 63 74 5f 4d 4c 69 6e 65 46 72 6f 6d 57 6b 62 32  ct_MLineFromWkb2
88020 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
88030 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
88040 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d 50  tion (db, "ST_MP
88050 6f 6c 79 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20  olyFromWKB", 1, 
88060 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
88070 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 50 6f 6c  ..     fnct_MPol
88080 79 46 72 6f 6d 57 6b 62 31 2c 20 30 2c 20 30 29  yFromWkb1, 0, 0)
88090 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
880a0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
880b0 62 2c 20 22 53 54 5f 4d 50 6f 6c 79 46 72 6f 6d  b, "ST_MPolyFrom
880c0 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  WKB", 2, SQLITE_
880d0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
880e0 66 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d 57 6b  fnct_MPolyFromWk
880f0 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b2, 0, 0);.    s
88100 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
88110 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
88120 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 46 72 6f 6d  MultiPolygonFrom
88130 57 4b 42 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  WKB", 1, SQLITE_
88140 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
88150 66 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d 57 6b  fnct_MPolyFromWk
88160 62 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b1, 0, 0);.    s
88170 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
88180 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
88190 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e 46 72 6f 6d  MultiPolygonFrom
881a0 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  WKB", 2, SQLITE_
881b0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
881c0 66 6e 63 74 5f 4d 50 6f 6c 79 46 72 6f 6d 57 6b  fnct_MPolyFromWk
881d0 62 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  b2, 0, 0);.    s
881e0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
881f0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47 65 6f  nction (db, "Geo
88200 6d 46 72 6f 6d 46 47 46 22 2c 20 31 2c 20 53 51  mFromFGF", 1, SQ
88210 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
88220 20 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 65 74       fnct_Geomet
88230 72 79 46 72 6f 6d 46 47 46 31 2c 20 30 2c 20 30  ryFromFGF1, 0, 0
88240 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
88250 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
88260 64 62 2c 20 22 47 65 6f 6d 46 72 6f 6d 46 47 46  db, "GeomFromFGF
88270 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
88280 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
88290 74 5f 47 65 6f 6d 65 74 72 79 46 72 6f 6d 46 47  t_GeometryFromFG
882a0 46 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  F2, 0, 0);.    s
882b0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
882c0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 6f 6d  nction (db, "Com
882d0 70 72 65 73 73 47 65 6f 6d 65 74 72 79 22 2c 20  pressGeometry", 
882e0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
882f0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43  ,....     fnct_C
88300 6f 6d 70 72 65 73 73 47 65 6f 6d 65 74 72 79 2c  ompressGeometry,
88310 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
88320 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
88330 69 6f 6e 20 28 64 62 2c 20 22 55 6e 63 6f 6d 70  ion (db, "Uncomp
88340 72 65 73 73 47 65 6f 6d 65 74 72 79 22 2c 20 31  ressGeometry", 1
88350 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
88360 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 55 6e  ....     fnct_Un
88370 63 6f 6d 70 72 65 73 73 47 65 6f 6d 65 74 72 79  compressGeometry
88380 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
88390 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
883a0 74 69 6f 6e 20 28 64 62 2c 20 22 53 61 6e 69 74  tion (db, "Sanit
883b0 69 7a 65 47 65 6f 6d 65 74 72 79 22 2c 20 31 2c  izeGeometry", 1,
883c0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
883d0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53 61 6e  ...     fnct_San
883e0 69 74 69 7a 65 47 65 6f 6d 65 74 72 79 2c 20 30  itizeGeometry, 0
883f0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
88400 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
88410 6e 20 28 64 62 2c 20 22 43 61 73 74 54 6f 50 6f  n (db, "CastToPo
88420 69 6e 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  int", 1, SQLITE_
88430 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
88440 66 6e 63 74 5f 43 61 73 74 54 6f 50 6f 69 6e 74  fnct_CastToPoint
88450 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
88460 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
88470 74 69 6f 6e 20 28 64 62 2c 20 22 43 61 73 74 54  tion (db, "CastT
88480 6f 4c 69 6e 65 73 74 72 69 6e 67 22 2c 20 31 2c  oLinestring", 1,
88490 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
884a0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43 61 73  ...     fnct_Cas
884b0 74 54 6f 4c 69 6e 65 73 74 72 69 6e 67 2c 20 30  tToLinestring, 0
884c0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
884d0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
884e0 6e 20 28 64 62 2c 20 22 43 61 73 74 54 6f 50 6f  n (db, "CastToPo
884f0 6c 79 67 6f 6e 22 2c 20 31 2c 20 53 51 4c 49 54  lygon", 1, SQLIT
88500 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
88510 20 20 66 6e 63 74 5f 43 61 73 74 54 6f 50 6f 6c    fnct_CastToPol
88520 79 67 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ygon, 0, 0);.   
88530 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
88540 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
88550 61 73 74 54 6f 4d 75 6c 74 69 50 6f 69 6e 74 22  astToMultiPoint"
88560 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
88570 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
88580 5f 43 61 73 74 54 6f 4d 75 6c 74 69 50 6f 69 6e  _CastToMultiPoin
88590 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  t, 0, 0);.    sq
885a0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
885b0 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 61 73 74  ction (db, "Cast
885c0 54 6f 4d 75 6c 74 69 4c 69 6e 65 73 74 72 69 6e  ToMultiLinestrin
885d0 67 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  g", 1, SQLITE_AN
885e0 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
885f0 63 74 5f 43 61 73 74 54 6f 4d 75 6c 74 69 4c 69  ct_CastToMultiLi
88600 6e 65 73 74 72 69 6e 67 2c 20 30 2c 20 30 29 3b  nestring, 0, 0);
88610 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
88620 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
88630 2c 20 22 43 61 73 74 54 6f 4d 75 6c 74 69 50 6f  , "CastToMultiPo
88640 6c 79 67 6f 6e 22 2c 20 31 2c 20 53 51 4c 49 54  lygon", 1, SQLIT
88650 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
88660 20 20 66 6e 63 74 5f 43 61 73 74 54 6f 4d 75 6c    fnct_CastToMul
88670 74 69 50 6f 6c 79 67 6f 6e 2c 20 30 2c 20 30 29  tiPolygon, 0, 0)
88680 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
88690 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
886a0 62 2c 20 22 43 61 73 74 54 6f 47 65 6f 6d 65 74  b, "CastToGeomet
886b0 72 79 43 6f 6c 6c 65 63 74 69 6f 6e 22 2c 20 31  ryCollection", 1
886c0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
886d0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43 61  ....     fnct_Ca
886e0 73 74 54 6f 47 65 6f 6d 65 74 72 79 43 6f 6c 6c  stToGeometryColl
886f0 65 63 74 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20  ection, 0, 0);. 
88700 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
88710 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
88720 22 43 61 73 74 54 6f 4d 75 6c 74 69 22 2c 20 31  "CastToMulti", 1
88730 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
88740 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43 61  ....     fnct_Ca
88750 73 74 54 6f 4d 75 6c 74 69 2c 20 30 2c 20 30 29  stToMulti, 0, 0)
88760 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
88770 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
88780 62 2c 20 22 53 54 5f 4d 75 6c 74 69 22 2c 20 31  b, "ST_Multi", 1
88790 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
887a0 20 66 6e 63 74 5f 43 61 73 74 54 6f 4d 75 6c 74   fnct_CastToMult
887b0 69 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29  i,....     0, 0)
887c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
887d0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
887e0 62 2c 20 22 43 61 73 74 54 6f 53 69 6e 67 6c 65  b, "CastToSingle
887f0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
88800 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
88810 74 5f 43 61 73 74 54 6f 53 69 6e 67 6c 65 2c 20  t_CastToSingle, 
88820 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
88830 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
88840 6f 6e 20 28 64 62 2c 20 22 43 61 73 74 54 6f 58  on (db, "CastToX
88850 59 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  Y", 1, SQLITE_AN
88860 59 2c 20 30 2c 20 66 6e 63 74 5f 43 61 73 74 54  Y, 0, fnct_CastT
88870 6f 58 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  oXY, 0,....     
88880 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
88890 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
888a0 28 64 62 2c 20 22 43 61 73 74 54 6f 58 59 5a 22  (db, "CastToXYZ"
888b0 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
888c0 20 30 2c 20 66 6e 63 74 5f 43 61 73 74 54 6f 58   0, fnct_CastToX
888d0 59 5a 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 30  YZ,....     0, 0
888e0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
888f0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
88900 64 62 2c 20 22 43 61 73 74 54 6f 58 59 4d 22 2c  db, "CastToXYM",
88910 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
88920 30 2c 20 66 6e 63 74 5f 43 61 73 74 54 6f 58 59  0, fnct_CastToXY
88930 4d 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29  M,....     0, 0)
88940 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
88950 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
88960 62 2c 20 22 43 61 73 74 54 6f 58 59 5a 4d 22 2c  b, "CastToXYZM",
88970 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
88980 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
88990 43 61 73 74 54 6f 58 59 5a 4d 2c 20 30 2c 20 30  CastToXYZM, 0, 0
889a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
889b0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
889c0 64 62 2c 20 22 45 78 74 72 61 63 74 4d 75 6c 74  db, "ExtractMult
889d0 69 50 6f 69 6e 74 22 2c 20 31 2c 20 53 51 4c 49  iPoint", 1, SQLI
889e0 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
889f0 20 20 20 66 6e 63 74 5f 45 78 74 72 61 63 74 4d     fnct_ExtractM
88a00 75 6c 74 69 50 6f 69 6e 74 2c 20 30 2c 20 30 29  ultiPoint, 0, 0)
88a10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
88a20 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
88a30 62 2c 20 22 45 78 74 72 61 63 74 4d 75 6c 74 69  b, "ExtractMulti
88a40 4c 69 6e 65 73 74 72 69 6e 67 22 2c 20 31 2c 20  Linestring", 1, 
88a50 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
88a60 09 09 20 20 20 20 20 66 6e 63 74 5f 45 78 74 72  ..     fnct_Extr
88a70 61 63 74 4d 75 6c 74 69 4c 69 6e 65 73 74 72 69  actMultiLinestri
88a80 6e 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ng, 0, 0);.    s
88a90 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
88aa0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 45 78 74  nction (db, "Ext
88ab0 72 61 63 74 4d 75 6c 74 69 50 6f 6c 79 67 6f 6e  ractMultiPolygon
88ac0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
88ad0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
88ae0 74 5f 45 78 74 72 61 63 74 4d 75 6c 74 69 50 6f  t_ExtractMultiPo
88af0 6c 79 67 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20  lygon, 0, 0);.  
88b00 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
88b10 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
88b20 53 54 5f 52 65 76 65 72 73 65 22 2c 20 31 2c 20  ST_Reverse", 1, 
88b30 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
88b40 09 09 20 20 20 20 20 66 6e 63 74 5f 52 65 76 65  ..     fnct_Reve
88b50 72 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  rse, 0, 0);.    
88b60 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
88b70 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
88b80 5f 46 6f 72 63 65 4c 48 52 22 2c 20 31 2c 20 53  _ForceLHR", 1, S
88b90 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
88ba0 09 20 20 20 20 20 66 6e 63 74 5f 46 6f 72 63 65  .     fnct_Force
88bb0 4c 48 52 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  LHR, 0, 0);.    
88bc0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
88bd0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 44 69  unction (db, "Di
88be0 6d 65 6e 73 69 6f 6e 22 2c 20 31 2c 20 53 51 4c  mension", 1, SQL
88bf0 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
88c00 5f 44 69 6d 65 6e 73 69 6f 6e 2c 0a 09 09 09 20  _Dimension,.... 
88c10 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73      0, 0);.    s
88c20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
88c30 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
88c40 44 69 6d 65 6e 73 69 6f 6e 22 2c 20 31 2c 20 53  Dimension", 1, S
88c50 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
88c60 09 20 20 20 20 20 66 6e 63 74 5f 44 69 6d 65 6e  .     fnct_Dimen
88c70 73 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20  sion, 0, 0);.   
88c80 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
88c90 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
88ca0 6f 6f 72 64 44 69 6d 65 6e 73 69 6f 6e 22 2c 20  oordDimension", 
88cb0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
88cc0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43  ,....     fnct_C
88cd0 6f 6f 72 64 44 69 6d 65 6e 73 69 6f 6e 2c 20 30  oordDimension, 0
88ce0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
88cf0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
88d00 6e 20 28 64 62 2c 20 22 53 54 5f 4e 44 69 6d 73  n (db, "ST_NDims
88d10 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
88d20 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
88d30 74 5f 4e 44 69 6d 73 2c 20 30 2c 20 30 29 3b 0a  t_NDims, 0, 0);.
88d40 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
88d50 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
88d60 20 22 47 65 6f 6d 65 74 72 79 54 79 70 65 22 2c   "GeometryType",
88d70 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
88d80 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
88d90 47 65 6f 6d 65 74 72 79 54 79 70 65 2c 20 30 2c  GeometryType, 0,
88da0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
88db0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
88dc0 20 28 64 62 2c 20 22 53 54 5f 47 65 6f 6d 65 74   (db, "ST_Geomet
88dd0 72 79 54 79 70 65 22 2c 20 31 2c 20 53 51 4c 49  ryType", 1, SQLI
88de0 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
88df0 20 20 20 66 6e 63 74 5f 47 65 6f 6d 65 74 72 79     fnct_Geometry
88e00 54 79 70 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Type, 0, 0);.   
88e10 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
88e20 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47  function (db, "G
88e30 65 6f 6d 65 74 72 79 41 6c 69 61 73 54 79 70 65  eometryAliasType
88e40 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
88e50 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
88e60 74 5f 47 65 6f 6d 65 74 72 79 41 6c 69 61 73 54  t_GeometryAliasT
88e70 79 70 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ype, 0, 0);.    
88e80 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
88e90 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 72  unction (db, "Sr
88ea0 69 64 46 72 6f 6d 41 75 74 68 43 52 53 22 2c 20  idFromAuthCRS", 
88eb0 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
88ec0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53  ,....     fnct_S
88ed0 72 69 64 46 72 6f 6d 41 75 74 68 43 52 53 2c 20  ridFromAuthCRS, 
88ee0 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
88ef0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
88f00 6f 6e 20 28 64 62 2c 20 22 53 52 49 44 22 2c 20  on (db, "SRID", 
88f10 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
88f20 2c 20 66 6e 63 74 5f 53 52 49 44 2c 20 30 2c 20  , fnct_SRID, 0, 
88f30 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
88f40 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
88f50 28 64 62 2c 20 22 53 54 5f 53 52 49 44 22 2c 20  (db, "ST_SRID", 
88f60 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
88f70 2c 20 66 6e 63 74 5f 53 52 49 44 2c 20 30 2c 20  , fnct_SRID, 0, 
88f80 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
88f90 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
88fa0 28 64 62 2c 20 22 53 65 74 53 52 49 44 22 2c 20  (db, "SetSRID", 
88fb0 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
88fc0 2c 20 66 6e 63 74 5f 53 65 74 53 52 49 44 2c 20  , fnct_SetSRID, 
88fd0 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20  0,....     0);. 
88fe0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
88ff0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
89000 22 49 73 45 6d 70 74 79 22 2c 20 31 2c 20 53 51  "IsEmpty", 1, SQ
89010 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
89020 74 5f 49 73 45 6d 70 74 79 2c 20 30 2c 0a 09 09  t_IsEmpty, 0,...
89030 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71  .     0);.    sq
89040 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
89050 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 49  ction (db, "ST_I
89060 73 45 6d 70 74 79 22 2c 20 31 2c 20 53 51 4c 49  sEmpty", 1, SQLI
89070 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
89080 49 73 45 6d 70 74 79 2c 0a 09 09 09 20 20 20 20  IsEmpty,....    
89090 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
890a0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
890b0 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 49 73 33  ion (db, "ST_Is3
890c0 44 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  D", 1, SQLITE_AN
890d0 59 2c 20 30 2c 20 66 6e 63 74 5f 49 73 33 44 2c  Y, 0, fnct_Is3D,
890e0 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
890f0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
89100 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 49 73 4d  ion (db, "ST_IsM
89110 65 61 73 75 72 65 64 22 2c 20 31 2c 20 53 51 4c  easured", 1, SQL
89120 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
89130 20 20 20 20 66 6e 63 74 5f 49 73 4d 65 61 73 75      fnct_IsMeasu
89140 72 65 64 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  red, 0, 0);.    
89150 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
89160 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 45 6e  unction (db, "En
89170 76 65 6c 6f 70 65 22 2c 20 31 2c 20 53 51 4c 49  velope", 1, SQLI
89180 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
89190 45 6e 76 65 6c 6f 70 65 2c 20 30 2c 0a 09 09 09  Envelope, 0,....
891a0 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71 6c       0);.    sql
891b0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
891c0 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 45 6e  tion (db, "ST_En
891d0 76 65 6c 6f 70 65 22 2c 20 31 2c 20 53 51 4c 49  velope", 1, SQLI
891e0 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
891f0 45 6e 76 65 6c 6f 70 65 2c 0a 09 09 09 20 20 20  Envelope,....   
89200 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c    0, 0);.    sql
89210 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
89220 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 45 78  tion (db, "ST_Ex
89230 70 61 6e 64 22 2c 20 32 2c 20 53 51 4c 49 54 45  pand", 2, SQLITE
89240 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 45 78  _ANY, 0, fnct_Ex
89250 70 61 6e 64 2c 20 30 2c 0a 09 09 09 20 20 20 20  pand, 0,....    
89260 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
89270 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
89280 20 28 64 62 2c 20 22 58 22 2c 20 31 2c 20 53 51   (db, "X", 1, SQ
89290 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
892a0 74 5f 58 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  t_X, 0, 0);.    
892b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
892c0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 59 22  unction (db, "Y"
892d0 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
892e0 20 30 2c 20 66 6e 63 74 5f 59 2c 20 30 2c 20 30   0, fnct_Y, 0, 0
892f0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
89300 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
89310 64 62 2c 20 22 5a 22 2c 20 31 2c 20 53 51 4c 49  db, "Z", 1, SQLI
89320 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
89330 5a 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  Z, 0, 0);.    sq
89340 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
89350 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d 22 2c 20  ction (db, "M", 
89360 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
89370 2c 20 66 6e 63 74 5f 4d 2c 20 30 2c 20 30 29 3b  , fnct_M, 0, 0);
89380 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
89390 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
893a0 2c 20 22 53 54 5f 58 22 2c 20 31 2c 20 53 51 4c  , "ST_X", 1, SQL
893b0 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
893c0 5f 58 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  _X, 0, 0);.    s
893d0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
893e0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
893f0 59 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  Y", 1, SQLITE_AN
89400 59 2c 20 30 2c 20 66 6e 63 74 5f 59 2c 20 30 2c  Y, 0, fnct_Y, 0,
89410 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
89420 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
89430 20 28 64 62 2c 20 22 53 54 5f 5a 22 2c 20 31 2c   (db, "ST_Z", 1,
89440 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20   SQLITE_ANY, 0, 
89450 66 6e 63 74 5f 5a 2c 20 30 2c 20 30 29 3b 0a 20  fnct_Z, 0, 0);. 
89460 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
89470 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
89480 22 53 54 5f 4d 22 2c 20 31 2c 20 53 51 4c 49 54  "ST_M", 1, SQLIT
89490 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 4d  E_ANY, 0, fnct_M
894a0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
894b0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
894c0 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d 69  tion (db, "ST_Mi
894d0 6e 58 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  nX", 1, SQLITE_A
894e0 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 4d 62 72 4d  NY, 0, fnct_MbrM
894f0 69 6e 58 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  inX, 0,....     
89500 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
89510 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
89520 28 64 62 2c 20 22 53 54 5f 4d 69 6e 59 22 2c 20  (db, "ST_MinY", 
89530 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
89540 2c 20 66 6e 63 74 5f 4d 62 72 4d 69 6e 59 2c 20  , fnct_MbrMinY, 
89550 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20  0,....     0);. 
89560 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
89570 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
89580 22 53 54 5f 4d 69 6e 5a 22 2c 20 31 2c 20 53 51  "ST_MinZ", 1, SQ
89590 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
895a0 74 5f 4d 69 6e 5a 2c 20 30 2c 20 30 29 3b 0a 20  t_MinZ, 0, 0);. 
895b0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
895c0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
895d0 22 53 54 5f 4d 69 6e 4d 22 2c 20 31 2c 20 53 51  "ST_MinM", 1, SQ
895e0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
895f0 74 5f 4d 69 6e 4d 2c 20 30 2c 20 30 29 3b 0a 20  t_MinM, 0, 0);. 
89600 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
89610 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
89620 22 53 54 5f 4d 61 78 58 22 2c 20 31 2c 20 53 51  "ST_MaxX", 1, SQ
89630 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
89640 74 5f 4d 62 72 4d 61 78 58 2c 20 30 2c 0a 09 09  t_MbrMaxX, 0,...
89650 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71  .     0);.    sq
89660 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
89670 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4d  ction (db, "ST_M
89680 61 78 59 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  axY", 1, SQLITE_
89690 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 4d 62 72  ANY, 0, fnct_Mbr
896a0 4d 61 78 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  MaxY, 0,....    
896b0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
896c0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
896d0 20 28 64 62 2c 20 22 53 54 5f 4d 61 78 5a 22 2c   (db, "ST_MaxZ",
896e0 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
896f0 30 2c 20 66 6e 63 74 5f 4d 61 78 5a 2c 20 30 2c  0, fnct_MaxZ, 0,
89700 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
89710 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
89720 20 28 64 62 2c 20 22 53 54 5f 4d 61 78 4d 22 2c   (db, "ST_MaxM",
89730 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
89740 30 2c 20 66 6e 63 74 5f 4d 61 78 4d 2c 20 30 2c  0, fnct_MaxM, 0,
89750 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
89760 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
89770 20 28 64 62 2c 20 22 4e 75 6d 50 6f 69 6e 74 73   (db, "NumPoints
89780 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
89790 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
897a0 74 5f 4e 75 6d 50 6f 69 6e 74 73 2c 20 30 2c 20  t_NumPoints, 0, 
897b0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
897c0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
897d0 28 64 62 2c 20 22 53 54 5f 4e 75 6d 50 6f 69 6e  (db, "ST_NumPoin
897e0 74 73 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  ts", 1, SQLITE_A
897f0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
89800 6e 63 74 5f 4e 75 6d 50 6f 69 6e 74 73 2c 20 30  nct_NumPoints, 0
89810 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
89820 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
89830 6e 20 28 64 62 2c 20 22 53 74 61 72 74 50 6f 69  n (db, "StartPoi
89840 6e 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  nt", 1, SQLITE_A
89850 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
89860 6e 63 74 5f 53 74 61 72 74 50 6f 69 6e 74 2c 20  nct_StartPoint, 
89870 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
89880 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
89890 6f 6e 20 28 64 62 2c 20 22 45 6e 64 50 6f 69 6e  on (db, "EndPoin
898a0 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  t", 1, SQLITE_AN
898b0 59 2c 20 30 2c 20 66 6e 63 74 5f 45 6e 64 50 6f  Y, 0, fnct_EndPo
898c0 69 6e 74 2c 0a 09 09 09 20 20 20 20 20 30 2c 20  int,....     0, 
898d0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
898e0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
898f0 28 64 62 2c 20 22 53 54 5f 53 74 61 72 74 50 6f  (db, "ST_StartPo
89900 69 6e 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  int", 1, SQLITE_
89910 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
89920 66 6e 63 74 5f 53 74 61 72 74 50 6f 69 6e 74 2c  fnct_StartPoint,
89930 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
89940 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
89950 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 45 6e 64  ion (db, "ST_End
89960 50 6f 69 6e 74 22 2c 20 31 2c 20 53 51 4c 49 54  Point", 1, SQLIT
89970 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
89980 20 20 66 6e 63 74 5f 45 6e 64 50 6f 69 6e 74 2c    fnct_EndPoint,
89990 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
899a0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
899b0 69 6f 6e 20 28 64 62 2c 20 22 50 6f 69 6e 74 4e  ion (db, "PointN
899c0 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
899d0 2c 20 30 2c 20 66 6e 63 74 5f 50 6f 69 6e 74 4e  , 0, fnct_PointN
899e0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
899f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
89a00 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 50 6f  tion (db, "ST_Po
89a10 69 6e 74 4e 22 2c 20 32 2c 20 53 51 4c 49 54 45  intN", 2, SQLITE
89a20 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 50 6f  _ANY, 0, fnct_Po
89a30 69 6e 74 4e 2c 0a 09 09 09 20 20 20 20 20 30 2c  intN,....     0,
89a40 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
89a50 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
89a60 20 28 64 62 2c 20 22 45 78 74 65 72 69 6f 72 52   (db, "ExteriorR
89a70 69 6e 67 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  ing", 1, SQLITE_
89a80 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
89a90 66 6e 63 74 5f 45 78 74 65 72 69 6f 72 52 69 6e  fnct_ExteriorRin
89aa0 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  g, 0, 0);.    sq
89ab0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
89ac0 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 45  ction (db, "ST_E
89ad0 78 74 65 72 69 6f 72 52 69 6e 67 22 2c 20 31 2c  xteriorRing", 1,
89ae0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
89af0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 45 78 74  ...     fnct_Ext
89b00 65 72 69 6f 72 52 69 6e 67 2c 20 30 2c 20 30 29  eriorRing, 0, 0)
89b10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
89b20 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
89b30 62 2c 20 22 4e 75 6d 49 6e 74 65 72 69 6f 72 52  b, "NumInteriorR
89b40 69 6e 67 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  ing", 1, SQLITE_
89b50 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
89b60 66 6e 63 74 5f 4e 75 6d 49 6e 74 65 72 69 6f 72  fnct_NumInterior
89b70 52 69 6e 67 73 2c 20 30 2c 20 30 29 3b 0a 20 20  Rings, 0, 0);.  
89b80 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
89b90 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
89ba0 4e 75 6d 49 6e 74 65 72 69 6f 72 52 69 6e 67 73  NumInteriorRings
89bb0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
89bc0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
89bd0 74 5f 4e 75 6d 49 6e 74 65 72 69 6f 72 52 69 6e  t_NumInteriorRin
89be0 67 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  gs, 0, 0);.    s
89bf0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
89c00 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
89c10 4e 75 6d 49 6e 74 65 72 69 6f 72 52 69 6e 67 22  NumInteriorRing"
89c20 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
89c30 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
89c40 5f 4e 75 6d 49 6e 74 65 72 69 6f 72 52 69 6e 67  _NumInteriorRing
89c50 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  s, 0, 0);.    sq
89c60 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
89c70 63 74 69 6f 6e 20 28 64 62 2c 20 22 49 6e 74 65  ction (db, "Inte
89c80 72 69 6f 72 52 69 6e 67 4e 22 2c 20 32 2c 20 53  riorRingN", 2, S
89c90 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
89ca0 09 20 20 20 20 20 66 6e 63 74 5f 49 6e 74 65 72  .     fnct_Inter
89cb0 69 6f 72 52 69 6e 67 4e 2c 20 30 2c 20 30 29 3b  iorRingN, 0, 0);
89cc0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
89cd0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
89ce0 2c 20 22 53 54 5f 49 6e 74 65 72 69 6f 72 52 69  , "ST_InteriorRi
89cf0 6e 67 4e 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  ngN", 2, SQLITE_
89d00 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
89d10 66 6e 63 74 5f 49 6e 74 65 72 69 6f 72 52 69 6e  fnct_InteriorRin
89d20 67 4e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  gN, 0, 0);.    s
89d30 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
89d40 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4e 75 6d  nction (db, "Num
89d50 47 65 6f 6d 65 74 72 69 65 73 22 2c 20 31 2c 20  Geometries", 1, 
89d60 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
89d70 09 09 20 20 20 20 20 66 6e 63 74 5f 4e 75 6d 47  ..     fnct_NumG
89d80 65 6f 6d 65 74 72 69 65 73 2c 20 30 2c 20 30 29  eometries, 0, 0)
89d90 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
89da0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
89db0 62 2c 20 22 53 54 5f 4e 75 6d 47 65 6f 6d 65 74  b, "ST_NumGeomet
89dc0 72 69 65 73 22 2c 20 31 2c 20 53 51 4c 49 54 45  ries", 1, SQLITE
89dd0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
89de0 20 66 6e 63 74 5f 4e 75 6d 47 65 6f 6d 65 74 72   fnct_NumGeometr
89df0 69 65 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ies, 0, 0);.    
89e00 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
89e10 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47 65  unction (db, "Ge
89e20 6f 6d 65 74 72 79 4e 22 2c 20 32 2c 20 53 51 4c  ometryN", 2, SQL
89e30 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
89e40 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 65 74 72      fnct_Geometr
89e50 79 4e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  yN, 0, 0);.    s
89e60 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
89e70 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
89e80 47 65 6f 6d 65 74 72 79 4e 22 2c 20 32 2c 20 53  GeometryN", 2, S
89e90 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
89ea0 09 20 20 20 20 20 66 6e 63 74 5f 47 65 6f 6d 65  .     fnct_Geome
89eb0 74 72 79 4e 2c 20 30 2c 20 30 29 3b 0a 20 20 20  tryN, 0, 0);.   
89ec0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
89ed0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d  function (db, "M
89ee0 42 52 43 6f 6e 74 61 69 6e 73 22 2c 20 32 2c 20  BRContains", 2, 
89ef0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
89f00 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 62 72 43  ..     fnct_MbrC
89f10 6f 6e 74 61 69 6e 73 2c 20 30 2c 20 30 29 3b 0a  ontains, 0, 0);.
89f20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
89f30 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
89f40 20 22 4d 62 72 44 69 73 6a 6f 69 6e 74 22 2c 20   "MbrDisjoint", 
89f50 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
89f60 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d  ,....     fnct_M
89f70 62 72 44 69 73 6a 6f 69 6e 74 2c 20 30 2c 20 30  brDisjoint, 0, 0
89f80 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
89f90 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
89fa0 64 62 2c 20 22 4d 42 52 45 71 75 61 6c 22 2c 20  db, "MBREqual", 
89fb0 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
89fc0 2c 20 66 6e 63 74 5f 4d 62 72 45 71 75 61 6c 2c  , fnct_MbrEqual,
89fd0 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a  ....     0, 0);.
89fe0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
89ff0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8a000 20 22 4d 62 72 49 6e 74 65 72 73 65 63 74 73 22   "MbrIntersects"
8a010 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
8a020 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
8a030 5f 4d 62 72 49 6e 74 65 72 73 65 63 74 73 2c 20  _MbrIntersects, 
8a040 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
8a050 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8a060 6f 6e 20 28 64 62 2c 20 22 53 54 5f 45 6e 76 49  on (db, "ST_EnvI
8a070 6e 74 65 72 73 65 63 74 73 22 2c 20 32 2c 20 53  ntersects", 2, S
8a080 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8a090 09 20 20 20 20 20 66 6e 63 74 5f 4d 62 72 49 6e  .     fnct_MbrIn
8a0a0 74 65 72 73 65 63 74 73 2c 20 30 2c 20 30 29 3b  tersects, 0, 0);
8a0b0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8a0c0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8a0d0 2c 20 22 53 54 5f 45 6e 76 49 6e 74 65 72 73 65  , "ST_EnvInterse
8a0e0 63 74 73 22 2c 20 35 2c 20 53 51 4c 49 54 45 5f  cts", 5, SQLITE_
8a0f0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8a100 66 6e 63 74 5f 45 6e 76 49 6e 74 65 72 73 65 63  fnct_EnvIntersec
8a110 74 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ts, 0, 0);.    s
8a120 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8a130 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
8a140 45 6e 76 65 6c 6f 70 65 73 49 6e 74 65 72 73 65  EnvelopesInterse
8a150 63 74 73 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  cts", 2, SQLITE_
8a160 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8a170 66 6e 63 74 5f 4d 62 72 49 6e 74 65 72 73 65 63  fnct_MbrIntersec
8a180 74 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ts, 0, 0);.    s
8a190 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8a1a0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
8a1b0 45 6e 76 65 6c 6f 70 65 73 49 6e 74 65 72 73 65  EnvelopesInterse
8a1c0 63 74 73 22 2c 20 35 2c 20 53 51 4c 49 54 45 5f  cts", 5, SQLITE_
8a1d0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8a1e0 66 6e 63 74 5f 45 6e 76 49 6e 74 65 72 73 65 63  fnct_EnvIntersec
8a1f0 74 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ts, 0, 0);.    s
8a200 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8a210 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d 42 52  nction (db, "MBR
8a220 4f 76 65 72 6c 61 70 73 22 2c 20 32 2c 20 53 51  Overlaps", 2, SQ
8a230 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8a240 20 20 20 20 20 66 6e 63 74 5f 4d 62 72 4f 76 65       fnct_MbrOve
8a250 72 6c 61 70 73 2c 20 30 2c 20 30 29 3b 0a 20 20  rlaps, 0, 0);.  
8a260 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8a270 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8a280 4d 62 72 54 6f 75 63 68 65 73 22 2c 20 32 2c 20  MbrTouches", 2, 
8a290 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8a2a0 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 62 72 54  ..     fnct_MbrT
8a2b0 6f 75 63 68 65 73 2c 20 30 2c 20 30 29 3b 0a 20  ouches, 0, 0);. 
8a2c0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8a2d0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8a2e0 22 4d 62 72 57 69 74 68 69 6e 22 2c 20 32 2c 20  "MbrWithin", 2, 
8a2f0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8a300 6e 63 74 5f 4d 62 72 57 69 74 68 69 6e 2c 0a 09  nct_MbrWithin,..
8a310 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20  ..     0, 0);.  
8a320 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8a330 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8a340 53 68 69 66 74 43 6f 6f 72 64 73 22 2c 20 33 2c  ShiftCoords", 3,
8a350 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8a360 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53 68 69  ...     fnct_Shi
8a370 66 74 43 6f 6f 72 64 73 2c 20 30 2c 20 30 29 3b  ftCoords, 0, 0);
8a380 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8a390 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8a3a0 2c 20 22 53 68 69 66 74 43 6f 6f 72 64 69 6e 61  , "ShiftCoordina
8a3b0 74 65 73 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f  tes", 3, SQLITE_
8a3c0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8a3d0 66 6e 63 74 5f 53 68 69 66 74 43 6f 6f 72 64 73  fnct_ShiftCoords
8a3e0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8a3f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8a400 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 54 72  tion (db, "ST_Tr
8a410 61 6e 73 6c 61 74 65 22 2c 20 34 2c 20 53 51 4c  anslate", 4, SQL
8a420 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8a430 20 20 20 20 66 6e 63 74 5f 54 72 61 6e 73 6c 61      fnct_Transla
8a440 74 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  te, 0, 0);.    s
8a450 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8a460 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
8a470 53 68 69 66 74 5f 4c 6f 6e 67 69 74 75 64 65 22  Shift_Longitude"
8a480 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
8a490 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
8a4a0 5f 53 68 69 66 74 4c 6f 6e 67 69 74 75 64 65 2c  _ShiftLongitude,
8a4b0 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8a4c0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8a4d0 69 6f 6e 20 28 64 62 2c 20 22 4e 6f 72 6d 61 6c  ion (db, "Normal
8a4e0 69 7a 65 4c 6f 6e 4c 61 74 22 2c 20 31 2c 20 53  izeLonLat", 1, S
8a4f0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8a500 09 20 20 20 20 20 66 6e 63 74 5f 4e 6f 72 6d 61  .     fnct_Norma
8a510 6c 69 7a 65 4c 6f 6e 4c 61 74 2c 20 30 2c 20 30  lizeLonLat, 0, 0
8a520 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8a530 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8a540 64 62 2c 20 22 53 63 61 6c 65 43 6f 6f 72 64 73  db, "ScaleCoords
8a550 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
8a560 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
8a570 74 5f 53 63 61 6c 65 43 6f 6f 72 64 73 2c 20 30  t_ScaleCoords, 0
8a580 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8a590 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8a5a0 6e 20 28 64 62 2c 20 22 53 63 61 6c 65 43 6f 6f  n (db, "ScaleCoo
8a5b0 72 64 69 6e 61 74 65 73 22 2c 20 32 2c 20 53 51  rdinates", 2, SQ
8a5c0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8a5d0 20 20 20 20 20 66 6e 63 74 5f 53 63 61 6c 65 43       fnct_ScaleC
8a5e0 6f 6f 72 64 73 2c 20 30 2c 20 30 29 3b 0a 20 20  oords, 0, 0);.  
8a5f0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8a600 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8a610 53 63 61 6c 65 43 6f 6f 72 64 73 22 2c 20 33 2c  ScaleCoords", 3,
8a620 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8a630 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53 63 61  ...     fnct_Sca
8a640 6c 65 43 6f 6f 72 64 73 2c 20 30 2c 20 30 29 3b  leCoords, 0, 0);
8a650 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8a660 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8a670 2c 20 22 53 63 61 6c 65 43 6f 6f 72 64 69 6e 61  , "ScaleCoordina
8a680 74 65 73 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f  tes", 3, SQLITE_
8a690 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8a6a0 66 6e 63 74 5f 53 63 61 6c 65 43 6f 6f 72 64 73  fnct_ScaleCoords
8a6b0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8a6c0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8a6d0 74 69 6f 6e 20 28 64 62 2c 20 22 52 6f 74 61 74  tion (db, "Rotat
8a6e0 65 43 6f 6f 72 64 73 22 2c 20 32 2c 20 53 51 4c  eCoords", 2, SQL
8a6f0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8a700 20 20 20 20 66 6e 63 74 5f 52 6f 74 61 74 65 43      fnct_RotateC
8a710 6f 6f 72 64 73 2c 20 30 2c 20 30 29 3b 0a 20 20  oords, 0, 0);.  
8a720 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8a730 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8a740 52 6f 74 61 74 65 43 6f 6f 72 64 69 6e 61 74 65  RotateCoordinate
8a750 73 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e  s", 2, SQLITE_AN
8a760 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8a770 63 74 5f 52 6f 74 61 74 65 43 6f 6f 72 64 73 2c  ct_RotateCoords,
8a780 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8a790 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8a7a0 69 6f 6e 20 28 64 62 2c 20 22 52 65 66 6c 65 63  ion (db, "Reflec
8a7b0 74 43 6f 6f 72 64 73 22 2c 20 33 2c 20 53 51 4c  tCoords", 3, SQL
8a7c0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8a7d0 20 20 20 20 66 6e 63 74 5f 52 65 66 6c 65 63 74      fnct_Reflect
8a7e0 43 6f 6f 72 64 73 2c 20 30 2c 20 30 29 3b 0a 20  Coords, 0, 0);. 
8a7f0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8a800 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8a810 22 52 65 66 6c 65 63 74 43 6f 6f 72 64 69 6e 61  "ReflectCoordina
8a820 74 65 73 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f  tes", 3, SQLITE_
8a830 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8a840 66 6e 63 74 5f 52 65 66 6c 65 63 74 43 6f 6f 72  fnct_ReflectCoor
8a850 64 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ds, 0, 0);.    s
8a860 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8a870 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 77 61  nction (db, "Swa
8a880 70 43 6f 6f 72 64 73 22 2c 20 31 2c 20 53 51 4c  pCoords", 1, SQL
8a890 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8a8a0 20 20 20 20 66 6e 63 74 5f 53 77 61 70 43 6f 6f      fnct_SwapCoo
8a8b0 72 64 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  rds, 0, 0);.    
8a8c0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8a8d0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 77  unction (db, "Sw
8a8e0 61 70 43 6f 6f 72 64 69 6e 61 74 65 73 22 2c 20  apCoordinates", 
8a8f0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8a900 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53  ,....     fnct_S
8a910 77 61 70 43 6f 6f 72 64 73 2c 20 30 2c 20 30 29  wapCoords, 0, 0)
8a920 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8a930 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8a940 62 2c 20 22 42 75 69 6c 64 4d 62 72 22 2c 20 34  b, "BuildMbr", 4
8a950 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8a960 20 66 6e 63 74 5f 42 75 69 6c 64 4d 62 72 31 2c   fnct_BuildMbr1,
8a970 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a  ....     0, 0);.
8a980 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8a990 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8a9a0 20 22 42 75 69 6c 64 4d 62 72 22 2c 20 35 2c 20   "BuildMbr", 5, 
8a9b0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8a9c0 6e 63 74 5f 42 75 69 6c 64 4d 62 72 32 2c 0a 09  nct_BuildMbr2,..
8a9d0 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20  ..     0, 0);.  
8a9e0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8a9f0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8aa00 42 75 69 6c 64 43 69 72 63 6c 65 4d 62 72 22 2c  BuildCircleMbr",
8aa10 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   3, SQLITE_ANY, 
8aa20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
8aa30 42 75 69 6c 64 43 69 72 63 6c 65 4d 62 72 31 2c  BuildCircleMbr1,
8aa40 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8aa50 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8aa60 69 6f 6e 20 28 64 62 2c 20 22 42 75 69 6c 64 43  ion (db, "BuildC
8aa70 69 72 63 6c 65 4d 62 72 22 2c 20 34 2c 20 53 51  ircleMbr", 4, SQ
8aa80 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8aa90 20 20 20 20 20 66 6e 63 74 5f 42 75 69 6c 64 43       fnct_BuildC
8aaa0 69 72 63 6c 65 4d 62 72 32 2c 20 30 2c 20 30 29  ircleMbr2, 0, 0)
8aab0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8aac0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8aad0 62 2c 20 22 45 78 74 65 6e 74 22 2c 20 31 2c 20  b, "Extent", 1, 
8aae0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 30  SQLITE_ANY, 0, 0
8aaf0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 45  ,....     fnct_E
8ab00 78 74 65 6e 74 5f 73 74 65 70 2c 20 66 6e 63 74  xtent_step, fnct
8ab10 5f 45 78 74 65 6e 74 5f 66 69 6e 61 6c 29 3b 0a  _Extent_final);.
8ab20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8ab30 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8ab40 20 22 4d 62 72 4d 69 6e 58 22 2c 20 31 2c 20 53   "MbrMinX", 1, S
8ab50 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8ab60 63 74 5f 4d 62 72 4d 69 6e 58 2c 20 30 2c 0a 09  ct_MbrMinX, 0,..
8ab70 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73  ..     0);.    s
8ab80 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8ab90 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d 62 72  nction (db, "Mbr
8aba0 4d 61 78 58 22 2c 20 31 2c 20 53 51 4c 49 54 45  MaxX", 1, SQLITE
8abb0 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 4d 62  _ANY, 0, fnct_Mb
8abc0 72 4d 61 78 58 2c 20 30 2c 0a 09 09 09 20 20 20  rMaxX, 0,....   
8abd0 20 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65    0);.    sqlite
8abe0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8abf0 6e 20 28 64 62 2c 20 22 4d 62 72 4d 69 6e 59 22  n (db, "MbrMinY"
8ac00 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
8ac10 20 30 2c 20 66 6e 63 74 5f 4d 62 72 4d 69 6e 59   0, fnct_MbrMinY
8ac20 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b  , 0,....     0);
8ac30 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8ac40 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8ac50 2c 20 22 4d 62 72 4d 61 78 59 22 2c 20 31 2c 20  , "MbrMaxY", 1, 
8ac60 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8ac70 6e 63 74 5f 4d 62 72 4d 61 78 59 2c 20 30 2c 0a  nct_MbrMaxY, 0,.
8ac80 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20  ...     0);.    
8ac90 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8aca0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d 61  unction (db, "Ma
8acb0 6b 65 50 6f 69 6e 74 22 2c 20 32 2c 20 53 51 4c  kePoint", 2, SQL
8acc0 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8acd0 5f 4d 61 6b 65 50 6f 69 6e 74 31 2c 0a 09 09 09  _MakePoint1,....
8ace0 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20       0, 0);.    
8acf0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8ad00 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d 61  unction (db, "Ma
8ad10 6b 65 50 6f 69 6e 74 22 2c 20 33 2c 20 53 51 4c  kePoint", 3, SQL
8ad20 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8ad30 5f 4d 61 6b 65 50 6f 69 6e 74 32 2c 0a 09 09 09  _MakePoint2,....
8ad40 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20       0, 0);.    
8ad50 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8ad60 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d 61  unction (db, "Ma
8ad70 6b 65 50 6f 69 6e 74 5a 22 2c 20 33 2c 20 53 51  kePointZ", 3, SQ
8ad80 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8ad90 20 20 20 20 20 66 6e 63 74 5f 4d 61 6b 65 50 6f       fnct_MakePo
8ada0 69 6e 74 5a 31 2c 20 30 2c 20 30 29 3b 0a 20 20  intZ1, 0, 0);.  
8adb0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8adc0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8add0 4d 61 6b 65 50 6f 69 6e 74 5a 22 2c 20 34 2c 20  MakePointZ", 4, 
8ade0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8adf0 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 61 6b 65  ..     fnct_Make
8ae00 50 6f 69 6e 74 5a 32 2c 20 30 2c 20 30 29 3b 0a  PointZ2, 0, 0);.
8ae10 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8ae20 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8ae30 20 22 4d 61 6b 65 50 6f 69 6e 74 4d 22 2c 20 33   "MakePointM", 3
8ae40 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8ae50 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4d 61  ....     fnct_Ma
8ae60 6b 65 50 6f 69 6e 74 4d 31 2c 20 30 2c 20 30 29  kePointM1, 0, 0)
8ae70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8ae80 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8ae90 62 2c 20 22 4d 61 6b 65 50 6f 69 6e 74 4d 22 2c  b, "MakePointM",
8aea0 20 34 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   4, SQLITE_ANY, 
8aeb0 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
8aec0 4d 61 6b 65 50 6f 69 6e 74 4d 32 2c 20 30 2c 20  MakePointM2, 0, 
8aed0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8aee0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8aef0 28 64 62 2c 20 22 4d 61 6b 65 50 6f 69 6e 74 5a  (db, "MakePointZ
8af00 4d 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 41 4e  M", 4, SQLITE_AN
8af10 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8af20 63 74 5f 4d 61 6b 65 50 6f 69 6e 74 5a 4d 31 2c  ct_MakePointZM1,
8af30 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8af40 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8af50 69 6f 6e 20 28 64 62 2c 20 22 4d 61 6b 65 50 6f  ion (db, "MakePo
8af60 69 6e 74 5a 4d 22 2c 20 35 2c 20 53 51 4c 49 54  intZM", 5, SQLIT
8af70 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8af80 20 20 66 6e 63 74 5f 4d 61 6b 65 50 6f 69 6e 74    fnct_MakePoint
8af90 5a 4d 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ZM2, 0, 0);.    
8afa0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8afb0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4d 61  unction (db, "Ma
8afc0 6b 65 4c 69 6e 65 22 2c 20 31 2c 20 53 51 4c 49  keLine", 1, SQLI
8afd0 54 45 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09 09  TE_ANY, 0, 0,...
8afe0 09 20 20 20 20 20 66 6e 63 74 5f 4d 61 6b 65 4c  .     fnct_MakeL
8aff0 69 6e 65 5f 73 74 65 70 2c 20 66 6e 63 74 5f 4d  ine_step, fnct_M
8b000 61 6b 65 4c 69 6e 65 5f 66 69 6e 61 6c 29 3b 0a  akeLine_final);.
8b010 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8b020 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8b030 20 22 4d 61 6b 65 4c 69 6e 65 22 2c 20 32 2c 20   "MakeLine", 2, 
8b040 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8b050 6e 63 74 5f 4d 61 6b 65 4c 69 6e 65 2c 20 30 2c  nct_MakeLine, 0,
8b060 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20  ....     0);.   
8b070 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8b080 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
8b090 6f 6c 6c 65 63 74 22 2c 20 31 2c 20 53 51 4c 49  ollect", 1, SQLI
8b0a0 54 45 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09 09  TE_ANY, 0, 0,...
8b0b0 09 20 20 20 20 20 66 6e 63 74 5f 43 6f 6c 6c 65  .     fnct_Colle
8b0c0 63 74 5f 73 74 65 70 2c 20 66 6e 63 74 5f 43 6f  ct_step, fnct_Co
8b0d0 6c 6c 65 63 74 5f 66 69 6e 61 6c 29 3b 0a 20 20  llect_final);.  
8b0e0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8b0f0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8b100 43 6f 6c 6c 65 63 74 22 2c 20 32 2c 20 53 51 4c  Collect", 2, SQL
8b110 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8b120 5f 43 6f 6c 6c 65 63 74 2c 20 30 2c 0a 09 09 09  _Collect, 0,....
8b130 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71 6c       0);.    sql
8b140 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8b150 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 43 6f  tion (db, "ST_Co
8b160 6c 6c 65 63 74 22 2c 20 31 2c 20 53 51 4c 49 54  llect", 1, SQLIT
8b170 45 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09 09 09  E_ANY, 0, 0,....
8b180 20 20 20 20 20 66 6e 63 74 5f 43 6f 6c 6c 65 63       fnct_Collec
8b190 74 5f 73 74 65 70 2c 20 66 6e 63 74 5f 43 6f 6c  t_step, fnct_Col
8b1a0 6c 65 63 74 5f 66 69 6e 61 6c 29 3b 0a 20 20 20  lect_final);.   
8b1b0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8b1c0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
8b1d0 54 5f 43 6f 6c 6c 65 63 74 22 2c 20 32 2c 20 53  T_Collect", 2, S
8b1e0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8b1f0 63 74 5f 43 6f 6c 6c 65 63 74 2c 0a 09 09 09 20  ct_Collect,.... 
8b200 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73      0, 0);.    s
8b210 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8b220 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 42 75 69  nction (db, "Bui
8b230 6c 64 4d 62 72 46 69 6c 74 65 72 22 2c 20 34 2c  ldMbrFilter", 4,
8b240 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8b250 09 09 09 20 20 20 20 20 66 6e 63 74 5f 42 75 69  ...     fnct_Bui
8b260 6c 64 4d 62 72 46 69 6c 74 65 72 2c 20 30 2c 20  ldMbrFilter, 0, 
8b270 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8b280 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8b290 28 64 62 2c 20 22 46 69 6c 74 65 72 4d 62 72 57  (db, "FilterMbrW
8b2a0 69 74 68 69 6e 22 2c 20 34 2c 20 53 51 4c 49 54  ithin", 4, SQLIT
8b2b0 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8b2c0 20 20 66 6e 63 74 5f 46 69 6c 74 65 72 4d 62 72    fnct_FilterMbr
8b2d0 57 69 74 68 69 6e 2c 20 30 2c 20 30 29 3b 0a 20  Within, 0, 0);. 
8b2e0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8b2f0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8b300 22 46 69 6c 74 65 72 4d 62 72 43 6f 6e 74 61 69  "FilterMbrContai
8b310 6e 73 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 41  ns", 4, SQLITE_A
8b320 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
8b330 6e 63 74 5f 46 69 6c 74 65 72 4d 62 72 43 6f 6e  nct_FilterMbrCon
8b340 74 61 69 6e 73 2c 20 30 2c 20 30 29 3b 0a 20 20  tains, 0, 0);.  
8b350 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8b360 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8b370 46 69 6c 74 65 72 4d 62 72 49 6e 74 65 72 73 65  FilterMbrInterse
8b380 63 74 73 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f  cts", 4, SQLITE_
8b390 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8b3a0 66 6e 63 74 5f 46 69 6c 74 65 72 4d 62 72 49 6e  fnct_FilterMbrIn
8b3b0 74 65 72 73 65 63 74 73 2c 20 30 2c 20 30 29 3b  tersects, 0, 0);
8b3c0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8b3d0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8b3e0 2c 20 22 4c 69 6e 65 73 46 72 6f 6d 52 69 6e 67  , "LinesFromRing
8b3f0 73 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  s", 1, SQLITE_AN
8b400 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8b410 63 74 5f 4c 69 6e 65 73 46 72 6f 6d 52 69 6e 67  ct_LinesFromRing
8b420 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  s, 0, 0);.    sq
8b430 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8b440 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4c  ction (db, "ST_L
8b450 69 6e 65 73 46 72 6f 6d 52 69 6e 67 73 22 2c 20  inesFromRings", 
8b460 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8b470 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4c  ,....     fnct_L
8b480 69 6e 65 73 46 72 6f 6d 52 69 6e 67 73 2c 20 30  inesFromRings, 0
8b490 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8b4a0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8b4b0 6e 20 28 64 62 2c 20 22 4c 69 6e 65 73 46 72 6f  n (db, "LinesFro
8b4c0 6d 52 69 6e 67 73 22 2c 20 32 2c 20 53 51 4c 49  mRings", 2, SQLI
8b4d0 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8b4e0 20 20 20 66 6e 63 74 5f 4c 69 6e 65 73 46 72 6f     fnct_LinesFro
8b4f0 6d 52 69 6e 67 73 2c 20 30 2c 20 30 29 3b 0a 20  mRings, 0, 0);. 
8b500 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8b510 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8b520 22 53 54 5f 4c 69 6e 65 73 46 72 6f 6d 52 69 6e  "ST_LinesFromRin
8b530 67 73 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  gs", 2, SQLITE_A
8b540 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
8b550 6e 63 74 5f 4c 69 6e 65 73 46 72 6f 6d 52 69 6e  nct_LinesFromRin
8b560 67 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  gs, 0, 0);.    s
8b570 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8b580 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
8b590 4e 50 6f 69 6e 74 73 22 2c 20 31 2c 20 53 51 4c  NPoints", 1, SQL
8b5a0 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8b5b0 5f 4e 50 6f 69 6e 74 73 2c 0a 09 09 09 20 20 20  _NPoints,....   
8b5c0 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c    0, 0);.    sql
8b5d0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8b5e0 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 6e 72  tion (db, "ST_nr
8b5f0 69 6e 67 73 22 2c 20 31 2c 20 53 51 4c 49 54 45  ings", 1, SQLITE
8b600 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 4e 52  _ANY, 0, fnct_NR
8b610 69 6e 67 73 2c 20 30 2c 0a 09 09 09 20 20 20 20  ings, 0,....    
8b620 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
8b630 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
8b640 20 28 64 62 2c 20 22 54 6f 47 41 52 53 22 2c 20   (db, "ToGARS", 
8b650 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8b660 2c 20 66 6e 63 74 5f 54 6f 47 41 52 53 2c 20 30  , fnct_ToGARS, 0
8b670 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8b680 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8b690 6e 20 28 64 62 2c 20 22 47 41 52 53 4d 62 72 22  n (db, "GARSMbr"
8b6a0 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
8b6b0 20 30 2c 20 66 6e 63 74 5f 47 41 52 53 4d 62 72   0, fnct_GARSMbr
8b6c0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b  , 0,....     0);
8b6d0 0a 0a 23 69 66 6e 64 65 66 20 4f 4d 49 54 5f 47  ..#ifndef OMIT_G
8b6e0 45 4f 53 09 09 2f 2a 20 69 6e 63 6c 75 64 69 6e  EOS../* includin
8b6f0 67 20 47 45 4f 53 20 2a 2f 0a 20 20 20 20 73 71  g GEOS */.    sq
8b700 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8b710 63 74 69 6f 6e 20 28 64 62 2c 20 22 42 75 69 6c  ction (db, "Buil
8b720 64 41 72 65 61 22 2c 20 31 2c 20 53 51 4c 49 54  dArea", 1, SQLIT
8b730 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 42  E_ANY, 0, fnct_B
8b740 75 69 6c 64 41 72 65 61 2c 0a 09 09 09 20 20 20  uildArea,....   
8b750 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c    0, 0);.    sql
8b760 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8b770 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 42 75  tion (db, "ST_Bu
8b780 69 6c 64 41 72 65 61 22 2c 20 31 2c 20 53 51 4c  ildArea", 1, SQL
8b790 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8b7a0 20 20 20 20 66 6e 63 74 5f 42 75 69 6c 64 41 72      fnct_BuildAr
8b7b0 65 61 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ea, 0, 0);.    s
8b7c0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8b7d0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 50 6f 6c  nction (db, "Pol
8b7e0 79 67 6f 6e 69 7a 65 22 2c 20 31 2c 20 53 51 4c  ygonize", 1, SQL
8b7f0 49 54 45 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09  ITE_ANY, 0, 0,..
8b800 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 6c 79  ..     fnct_Poly
8b810 67 6f 6e 69 7a 65 5f 73 74 65 70 2c 20 66 6e 63  gonize_step, fnc
8b820 74 5f 50 6f 6c 79 67 6f 6e 69 7a 65 5f 66 69 6e  t_Polygonize_fin
8b830 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  al);.    sqlite3
8b840 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
8b850 20 28 64 62 2c 20 22 53 54 5f 50 6f 6c 79 67 6f   (db, "ST_Polygo
8b860 6e 69 7a 65 22 2c 20 31 2c 20 53 51 4c 49 54 45  nize", 1, SQLITE
8b870 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09 09 09 20  _ANY, 0, 0,.... 
8b880 20 20 20 20 66 6e 63 74 5f 50 6f 6c 79 67 6f 6e      fnct_Polygon
8b890 69 7a 65 5f 73 74 65 70 2c 20 66 6e 63 74 5f 50  ize_step, fnct_P
8b8a0 6f 6c 79 67 6f 6e 69 7a 65 5f 66 69 6e 61 6c 29  olygonize_final)
8b8b0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 65 6e 64 20  ;.#endif /* end 
8b8c0 69 6e 63 6c 75 64 69 6e 67 20 47 45 4f 53 20 2a  including GEOS *
8b8d0 2f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  /..    sqlite3_c
8b8e0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8b8f0 64 62 2c 20 22 44 69 73 73 6f 6c 76 65 53 65 67  db, "DissolveSeg
8b900 6d 65 6e 74 73 22 2c 20 31 2c 20 53 51 4c 49 54  ments", 1, SQLIT
8b910 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8b920 20 20 66 6e 63 74 5f 44 69 73 73 6f 6c 76 65 53    fnct_DissolveS
8b930 65 67 6d 65 6e 74 73 2c 20 30 2c 20 30 29 3b 0a  egments, 0, 0);.
8b940 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8b950 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8b960 20 22 53 54 5f 44 69 73 73 6f 6c 76 65 53 65 67   "ST_DissolveSeg
8b970 6d 65 6e 74 73 22 2c 20 31 2c 20 53 51 4c 49 54  ments", 1, SQLIT
8b980 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8b990 20 20 66 6e 63 74 5f 44 69 73 73 6f 6c 76 65 53    fnct_DissolveS
8b9a0 65 67 6d 65 6e 74 73 2c 20 30 2c 20 30 29 3b 0a  egments, 0, 0);.
8b9b0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8b9c0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8b9d0 20 22 44 69 73 73 6f 6c 76 65 50 6f 69 6e 74 73   "DissolvePoints
8b9e0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
8b9f0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
8ba00 74 5f 44 69 73 73 6f 6c 76 65 50 6f 69 6e 74 73  t_DissolvePoints
8ba10 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8ba20 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8ba30 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 44 69  tion (db, "ST_Di
8ba40 73 73 6f 6c 76 65 50 6f 69 6e 74 73 22 2c 20 31  ssolvePoints", 1
8ba50 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8ba60 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 44 69  ....     fnct_Di
8ba70 73 73 6f 6c 76 65 50 6f 69 6e 74 73 2c 20 30 2c  ssolvePoints, 0,
8ba80 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
8ba90 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
8baa0 20 28 64 62 2c 20 22 43 6f 6c 6c 65 63 74 69 6f   (db, "Collectio
8bab0 6e 45 78 74 72 61 63 74 22 2c 20 32 2c 20 53 51  nExtract", 2, SQ
8bac0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8bad0 20 20 20 20 20 66 6e 63 74 5f 43 6f 6c 6c 65 63       fnct_Collec
8bae0 74 69 6f 6e 45 78 74 72 61 63 74 2c 20 30 2c 20  tionExtract, 0, 
8baf0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8bb00 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8bb10 28 64 62 2c 20 22 53 54 5f 43 6f 6c 6c 65 63 74  (db, "ST_Collect
8bb20 69 6f 6e 45 78 74 72 61 63 74 22 2c 20 32 2c 20  ionExtract", 2, 
8bb30 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8bb40 09 09 20 20 20 20 20 66 6e 63 74 5f 43 6f 6c 6c  ..     fnct_Coll
8bb50 65 63 74 69 6f 6e 45 78 74 72 61 63 74 2c 20 30  ectionExtract, 0
8bb60 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8bb70 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8bb80 6e 20 28 64 62 2c 20 22 53 54 5f 4c 6f 63 61 74  n (db, "ST_Locat
8bb90 65 5f 41 6c 6f 6e 67 5f 4d 65 61 73 75 72 65 22  e_Along_Measure"
8bba0 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
8bbb0 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
8bbc0 5f 4c 6f 63 61 74 65 42 65 74 77 65 65 6e 4d 65  _LocateBetweenMe
8bbd0 61 73 75 72 65 73 2c 20 30 2c 20 30 29 3b 0a 20  asures, 0, 0);. 
8bbe0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8bbf0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8bc00 22 53 54 5f 4c 6f 63 61 74 65 41 6c 6f 6e 67 22  "ST_LocateAlong"
8bc10 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
8bc20 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
8bc30 5f 4c 6f 63 61 74 65 42 65 74 77 65 65 6e 4d 65  _LocateBetweenMe
8bc40 61 73 75 72 65 73 2c 20 30 2c 20 30 29 3b 0a 20  asures, 0, 0);. 
8bc50 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8bc60 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8bc70 22 53 54 5f 4c 6f 63 61 74 65 5f 42 65 74 77 65  "ST_Locate_Betwe
8bc80 65 6e 5f 4d 65 61 73 75 72 65 73 22 2c 20 33 2c  en_Measures", 3,
8bc90 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8bca0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4c 6f 63  ...     fnct_Loc
8bcb0 61 74 65 42 65 74 77 65 65 6e 4d 65 61 73 75 72  ateBetweenMeasur
8bcc0 65 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  es, 0, 0);.    s
8bcd0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8bce0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
8bcf0 4c 6f 63 61 74 65 42 65 74 77 65 65 6e 22 2c 20  LocateBetween", 
8bd00 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  3, SQLITE_ANY, 0
8bd10 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4c  ,....     fnct_L
8bd20 6f 63 61 74 65 42 65 74 77 65 65 6e 4d 65 61 73  ocateBetweenMeas
8bd30 75 72 65 73 2c 20 30 2c 20 30 29 3b 0a 23 69 66  ures, 0, 0);.#if
8bd40 6e 64 65 66 20 4f 4d 49 54 5f 47 45 4f 43 41 4c  ndef OMIT_GEOCAL
8bd50 4c 42 41 43 4b 53 09 2f 2a 20 73 75 70 70 6f 72  LBACKS./* suppor
8bd60 74 69 6e 67 20 52 54 72 65 65 20 67 65 6f 6d 65  ting RTree geome
8bd70 74 72 79 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f  try callbacks */
8bd80 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 74 72  .    sqlite3_rtr
8bd90 65 65 5f 67 65 6f 6d 65 74 72 79 5f 63 61 6c 6c  ee_geometry_call
8bda0 62 61 63 6b 20 28 64 62 2c 20 22 52 54 72 65 65  back (db, "RTree
8bdb0 57 69 74 68 69 6e 22 2c 20 66 6e 63 74 5f 52 54  Within", fnct_RT
8bdc0 72 65 65 49 6e 74 65 72 73 65 63 74 73 2c 0a 09  reeIntersects,..
8bdd0 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20  ...     0);.    
8bde0 73 71 6c 69 74 65 33 5f 72 74 72 65 65 5f 67 65  sqlite3_rtree_ge
8bdf0 6f 6d 65 74 72 79 5f 63 61 6c 6c 62 61 63 6b 20  ometry_callback 
8be00 28 64 62 2c 20 22 52 54 72 65 65 43 6f 6e 74 61  (db, "RTreeConta
8be10 69 6e 73 22 2c 20 66 6e 63 74 5f 52 54 72 65 65  ins", fnct_RTree
8be20 49 6e 74 65 72 73 65 63 74 73 2c 0a 09 09 09 09  Intersects,.....
8be30 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71 6c       0);.    sql
8be40 69 74 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65  ite3_rtree_geome
8be50 74 72 79 5f 63 61 6c 6c 62 61 63 6b 20 28 64 62  try_callback (db
8be60 2c 20 22 52 54 72 65 65 49 6e 74 65 72 73 65 63  , "RTreeIntersec
8be70 74 73 22 2c 0a 09 09 09 09 20 20 20 20 20 66 6e  ts",.....     fn
8be80 63 74 5f 52 54 72 65 65 49 6e 74 65 72 73 65 63  ct_RTreeIntersec
8be90 74 73 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  ts, 0);.    sqli
8bea0 74 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74  te3_rtree_geomet
8beb0 72 79 5f 63 61 6c 6c 62 61 63 6b 20 28 64 62 2c  ry_callback (db,
8bec0 20 22 52 54 72 65 65 44 69 73 74 57 69 74 68 69   "RTreeDistWithi
8bed0 6e 22 2c 0a 09 09 09 09 20 20 20 20 20 66 6e 63  n",.....     fnc
8bee0 74 5f 52 54 72 65 65 44 69 73 74 57 69 74 68 69  t_RTreeDistWithi
8bef0 6e 2c 20 30 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  n, 0);.#endif /*
8bf00 20 65 6e 64 20 52 54 72 65 65 20 67 65 6f 6d 65   end RTree geome
8bf10 74 72 79 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f  try callbacks */
8bf20 0a 0a 2f 2a 20 73 6f 6d 65 20 42 4c 4f 42 2f 4a  ../* some BLOB/J
8bf30 50 45 47 2f 45 58 49 46 20 66 75 6e 63 74 69 6f  PEG/EXIF functio
8bf40 6e 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ns */.    sqlite
8bf50 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8bf60 6e 20 28 64 62 2c 20 22 49 73 47 65 6f 6d 65 74  n (db, "IsGeomet
8bf70 72 79 42 6c 6f 62 22 2c 20 31 2c 20 53 51 4c 49  ryBlob", 1, SQLI
8bf80 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8bf90 20 20 20 66 6e 63 74 5f 49 73 47 65 6f 6d 65 74     fnct_IsGeomet
8bfa0 72 79 42 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20  ryBlob, 0, 0);. 
8bfb0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8bfc0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8bfd0 22 49 73 5a 69 70 42 6c 6f 62 22 2c 20 31 2c 20  "IsZipBlob", 1, 
8bfe0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8bff0 09 09 20 20 20 20 20 66 6e 63 74 5f 49 73 5a 69  ..     fnct_IsZi
8c000 70 42 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20 20  pBlob, 0, 0);.  
8c010 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8c020 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8c030 49 73 50 64 66 42 6c 6f 62 22 2c 20 31 2c 20 53  IsPdfBlob", 1, S
8c040 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8c050 09 20 20 20 20 20 66 6e 63 74 5f 49 73 50 64 66  .     fnct_IsPdf
8c060 42 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Blob, 0, 0);.   
8c070 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8c080 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 49  function (db, "I
8c090 73 54 69 66 66 42 6c 6f 62 22 2c 20 31 2c 20 53  sTiffBlob", 1, S
8c0a0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8c0b0 09 20 20 20 20 20 66 6e 63 74 5f 49 73 54 69 66  .     fnct_IsTif
8c0c0 66 42 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20 20  fBlob, 0, 0);.  
8c0d0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8c0e0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8c0f0 49 73 47 69 66 42 6c 6f 62 22 2c 20 31 2c 20 53  IsGifBlob", 1, S
8c100 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8c110 09 20 20 20 20 20 66 6e 63 74 5f 49 73 47 69 66  .     fnct_IsGif
8c120 42 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Blob, 0, 0);.   
8c130 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8c140 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 49  function (db, "I
8c150 73 50 6e 67 42 6c 6f 62 22 2c 20 31 2c 20 53 51  sPngBlob", 1, SQ
8c160 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8c170 20 20 20 20 20 66 6e 63 74 5f 49 73 50 6e 67 42       fnct_IsPngB
8c180 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  lob, 0, 0);.    
8c190 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8c1a0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 49 73  unction (db, "Is
8c1b0 4a 70 65 67 42 6c 6f 62 22 2c 20 31 2c 20 53 51  JpegBlob", 1, SQ
8c1c0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8c1d0 20 20 20 20 20 66 6e 63 74 5f 49 73 4a 70 65 67       fnct_IsJpeg
8c1e0 42 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Blob, 0, 0);.   
8c1f0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8c200 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 49  function (db, "I
8c210 73 45 78 69 66 42 6c 6f 62 22 2c 20 31 2c 20 53  sExifBlob", 1, S
8c220 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8c230 09 20 20 20 20 20 66 6e 63 74 5f 49 73 45 78 69  .     fnct_IsExi
8c240 66 42 6c 6f 62 2c 20 30 2c 20 30 29 3b 0a 20 20  fBlob, 0, 0);.  
8c250 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8c260 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8c270 49 73 45 78 69 66 47 70 73 42 6c 6f 62 22 2c 20  IsExifGpsBlob", 
8c280 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8c290 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 49  ,....     fnct_I
8c2a0 73 45 78 69 66 47 70 73 42 6c 6f 62 2c 20 30 2c  sExifGpsBlob, 0,
8c2b0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
8c2c0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
8c2d0 20 28 64 62 2c 20 22 49 73 57 65 62 70 42 6c 6f   (db, "IsWebpBlo
8c2e0 62 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  b", 1, SQLITE_AN
8c2f0 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8c300 63 74 5f 49 73 57 65 62 50 42 6c 6f 62 2c 20 30  ct_IsWebPBlob, 0
8c310 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8c320 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8c330 6e 20 28 64 62 2c 20 22 47 65 6f 6d 46 72 6f 6d  n (db, "GeomFrom
8c340 45 78 69 66 47 70 73 42 6c 6f 62 22 2c 20 31 2c  ExifGpsBlob", 1,
8c350 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8c360 09 09 09 20 20 20 20 20 66 6e 63 74 5f 47 65 6f  ...     fnct_Geo
8c370 6d 46 72 6f 6d 45 78 69 66 47 70 73 42 6c 6f 62  mFromExifGpsBlob
8c380 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8c390 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8c3a0 74 69 6f 6e 20 28 64 62 2c 20 22 42 6c 6f 62 46  tion (db, "BlobF
8c3b0 72 6f 6d 46 69 6c 65 22 2c 20 31 2c 20 53 51 4c  romFile", 1, SQL
8c3c0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8c3d0 20 20 20 20 66 6e 63 74 5f 42 6c 6f 62 46 72 6f      fnct_BlobFro
8c3e0 6d 46 69 6c 65 2c 20 30 2c 20 30 29 3b 0a 20 20  mFile, 0, 0);.  
8c3f0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8c400 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8c410 42 6c 6f 62 54 6f 46 69 6c 65 22 2c 20 32 2c 20  BlobToFile", 2, 
8c420 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8c430 09 09 20 20 20 20 20 66 6e 63 74 5f 42 6c 6f 62  ..     fnct_Blob
8c440 54 6f 46 69 6c 65 2c 20 30 2c 20 30 29 3b 0a 0a  ToFile, 0, 0);..
8c450 2f 2a 20 73 6f 6d 65 20 47 65 6f 64 65 73 69 63  /* some Geodesic
8c460 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20   functions */.  
8c470 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8c480 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8c490 47 72 65 61 74 43 69 72 63 6c 65 4c 65 6e 67 74  GreatCircleLengt
8c4a0 68 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  h", 1, SQLITE_AN
8c4b0 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8c4c0 63 74 5f 47 72 65 61 74 43 69 72 63 6c 65 4c 65  ct_GreatCircleLe
8c4d0 6e 67 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ngth, 0, 0);.   
8c4e0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8c4f0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 47  function (db, "G
8c500 65 6f 64 65 73 69 63 4c 65 6e 67 74 68 22 2c 20  eodesicLength", 
8c510 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8c520 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 47  ,....     fnct_G
8c530 65 6f 64 65 73 69 63 4c 65 6e 67 74 68 2c 20 30  eodesicLength, 0
8c540 2c 20 30 29 3b 0a 0a 2f 2a 20 73 6f 6d 65 20 4c  , 0);../* some L
8c550 65 6e 67 74 68 20 55 6e 69 74 20 63 6f 6e 76 65  ength Unit conve
8c560 72 73 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rsion functions 
8c570 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  */.    sqlite3_c
8c580 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8c590 64 62 2c 20 22 43 76 74 54 6f 4b 6d 22 2c 20 31  db, "CvtToKm", 1
8c5a0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8c5b0 20 66 6e 63 74 5f 63 76 74 54 6f 4b 6d 2c 20 30   fnct_cvtToKm, 0
8c5c0 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20  ,....     0);.  
8c5d0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8c5e0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8c5f0 43 76 74 54 6f 44 6d 22 2c 20 31 2c 20 53 51 4c  CvtToDm", 1, SQL
8c600 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8c610 5f 63 76 74 54 6f 44 6d 2c 20 30 2c 0a 09 09 09  _cvtToDm, 0,....
8c620 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71 6c       0);.    sql
8c630 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8c640 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74 54 6f  tion (db, "CvtTo
8c650 43 6d 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  Cm", 1, SQLITE_A
8c660 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 63 76 74 54  NY, 0, fnct_cvtT
8c670 6f 43 6d 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  oCm, 0,....     
8c680 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8c690 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8c6a0 28 64 62 2c 20 22 43 76 74 54 6f 4d 6d 22 2c 20  (db, "CvtToMm", 
8c6b0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8c6c0 2c 20 66 6e 63 74 5f 63 76 74 54 6f 4d 6d 2c 20  , fnct_cvtToMm, 
8c6d0 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20  0,....     0);. 
8c6e0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8c6f0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8c700 22 43 76 74 54 6f 4b 6d 69 22 2c 20 31 2c 20 53  "CvtToKmi", 1, S
8c710 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8c720 63 74 5f 63 76 74 54 6f 4b 6d 69 2c 0a 09 09 09  ct_cvtToKmi,....
8c730 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20       0, 0);.    
8c740 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8c750 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76  unction (db, "Cv
8c760 74 54 6f 49 6e 22 2c 20 31 2c 20 53 51 4c 49 54  tToIn", 1, SQLIT
8c770 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 63  E_ANY, 0, fnct_c
8c780 76 74 54 6f 49 6e 2c 20 30 2c 0a 09 09 09 20 20  vtToIn, 0,....  
8c790 20 20 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74     0);.    sqlit
8c7a0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8c7b0 6f 6e 20 28 64 62 2c 20 22 43 76 74 54 6f 46 74  on (db, "CvtToFt
8c7c0 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
8c7d0 2c 20 30 2c 20 66 6e 63 74 5f 63 76 74 54 6f 46  , 0, fnct_cvtToF
8c7e0 74 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 30 29  t, 0,....     0)
8c7f0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8c800 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8c810 62 2c 20 22 43 76 74 54 6f 59 64 22 2c 20 31 2c  b, "CvtToYd", 1,
8c820 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20   SQLITE_ANY, 0, 
8c830 66 6e 63 74 5f 63 76 74 54 6f 59 64 2c 20 30 2c  fnct_cvtToYd, 0,
8c840 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20  ....     0);.   
8c850 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8c860 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
8c870 76 74 54 6f 4d 69 22 2c 20 31 2c 20 53 51 4c 49  vtToMi", 1, SQLI
8c880 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
8c890 63 76 74 54 6f 4d 69 2c 20 30 2c 0a 09 09 09 20  cvtToMi, 0,.... 
8c8a0 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71 6c 69      0);.    sqli
8c8b0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8c8c0 69 6f 6e 20 28 64 62 2c 20 22 43 76 74 54 6f 46  ion (db, "CvtToF
8c8d0 61 74 68 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  ath", 1, SQLITE_
8c8e0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8c8f0 66 6e 63 74 5f 63 76 74 54 6f 46 61 74 68 2c 20  fnct_cvtToFath, 
8c900 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
8c910 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8c920 6f 6e 20 28 64 62 2c 20 22 43 76 74 54 6f 43 68  on (db, "CvtToCh
8c930 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
8c940 2c 20 30 2c 20 66 6e 63 74 5f 63 76 74 54 6f 43  , 0, fnct_cvtToC
8c950 68 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 30 29  h, 0,....     0)
8c960 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8c970 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8c980 62 2c 20 22 43 76 74 54 6f 4c 69 6e 6b 22 2c 20  b, "CvtToLink", 
8c990 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8c9a0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 63  ,....     fnct_c
8c9b0 76 74 54 6f 4c 69 6e 6b 2c 20 30 2c 20 30 29 3b  vtToLink, 0, 0);
8c9c0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8c9d0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8c9e0 2c 20 22 43 76 74 54 6f 55 73 49 6e 22 2c 20 31  , "CvtToUsIn", 1
8c9f0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8ca00 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 63 76  ....     fnct_cv
8ca10 74 54 6f 55 73 49 6e 2c 20 30 2c 20 30 29 3b 0a  tToUsIn, 0, 0);.
8ca20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8ca30 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8ca40 20 22 43 76 74 54 6f 55 73 46 74 22 2c 20 31 2c   "CvtToUsFt", 1,
8ca50 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8ca60 09 09 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74  ...     fnct_cvt
8ca70 54 6f 55 73 46 74 2c 20 30 2c 20 30 29 3b 0a 20  ToUsFt, 0, 0);. 
8ca80 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8ca90 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8caa0 22 43 76 74 54 6f 55 73 59 64 22 2c 20 31 2c 20  "CvtToUsYd", 1, 
8cab0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8cac0 09 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 54  ..     fnct_cvtT
8cad0 6f 55 73 59 64 2c 20 30 2c 20 30 29 3b 0a 20 20  oUsYd, 0, 0);.  
8cae0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8caf0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8cb00 43 76 74 54 6f 55 73 43 68 22 2c 20 31 2c 20 53  CvtToUsCh", 1, S
8cb10 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8cb20 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 54 6f  .     fnct_cvtTo
8cb30 55 73 43 68 2c 20 30 2c 20 30 29 3b 0a 20 20 20  UsCh, 0, 0);.   
8cb40 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8cb50 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
8cb60 76 74 54 6f 55 73 4d 69 22 2c 20 31 2c 20 53 51  vtToUsMi", 1, SQ
8cb70 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8cb80 20 20 20 20 20 66 6e 63 74 5f 63 76 74 54 6f 55       fnct_cvtToU
8cb90 73 4d 69 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  sMi, 0, 0);.    
8cba0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8cbb0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76  unction (db, "Cv
8cbc0 74 54 6f 49 6e 64 46 74 22 2c 20 31 2c 20 53 51  tToIndFt", 1, SQ
8cbd0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8cbe0 20 20 20 20 20 66 6e 63 74 5f 63 76 74 54 6f 49       fnct_cvtToI
8cbf0 6e 64 46 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ndFt, 0, 0);.   
8cc00 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8cc10 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
8cc20 76 74 54 6f 49 6e 64 59 64 22 2c 20 31 2c 20 53  vtToIndYd", 1, S
8cc30 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8cc40 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 54 6f  .     fnct_cvtTo
8cc50 49 6e 64 59 64 2c 20 30 2c 20 30 29 3b 0a 20 20  IndYd, 0, 0);.  
8cc60 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8cc70 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8cc80 43 76 74 54 6f 49 6e 64 43 68 22 2c 20 31 2c 20  CvtToIndCh", 1, 
8cc90 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8cca0 09 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 54  ..     fnct_cvtT
8ccb0 6f 49 6e 64 43 68 2c 20 30 2c 20 30 29 3b 0a 20  oIndCh, 0, 0);. 
8ccc0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8ccd0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8cce0 22 43 76 74 46 72 6f 6d 4b 6d 22 2c 20 31 2c 20  "CvtFromKm", 1, 
8ccf0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8cd00 09 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 46  ..     fnct_cvtF
8cd10 72 6f 6d 4b 6d 2c 20 30 2c 20 30 29 3b 0a 20 20  romKm, 0, 0);.  
8cd20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8cd30 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8cd40 43 76 74 46 72 6f 6d 44 6d 22 2c 20 31 2c 20 53  CvtFromDm", 1, S
8cd50 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8cd60 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 46 72  .     fnct_cvtFr
8cd70 6f 6d 44 6d 2c 20 30 2c 20 30 29 3b 0a 20 20 20  omDm, 0, 0);.   
8cd80 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8cd90 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
8cda0 76 74 46 72 6f 6d 43 6d 22 2c 20 31 2c 20 53 51  vtFromCm", 1, SQ
8cdb0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8cdc0 20 20 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f       fnct_cvtFro
8cdd0 6d 43 6d 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  mCm, 0, 0);.    
8cde0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8cdf0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76  unction (db, "Cv
8ce00 74 46 72 6f 6d 4d 6d 22 2c 20 31 2c 20 53 51 4c  tFromMm", 1, SQL
8ce10 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8ce20 20 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d      fnct_cvtFrom
8ce30 4d 6d 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  Mm, 0, 0);.    s
8ce40 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8ce50 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74  nction (db, "Cvt
8ce60 46 72 6f 6d 4b 6d 69 22 2c 20 31 2c 20 53 51 4c  FromKmi", 1, SQL
8ce70 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8ce80 20 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d      fnct_cvtFrom
8ce90 4b 6d 69 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Kmi, 0, 0);.    
8cea0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8ceb0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76  unction (db, "Cv
8cec0 74 46 72 6f 6d 49 6e 22 2c 20 31 2c 20 53 51 4c  tFromIn", 1, SQL
8ced0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8cee0 20 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d      fnct_cvtFrom
8cef0 49 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  In, 0, 0);.    s
8cf00 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8cf10 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74  nction (db, "Cvt
8cf20 46 72 6f 6d 46 74 22 2c 20 31 2c 20 53 51 4c 49  FromFt", 1, SQLI
8cf30 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8cf40 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d 46     fnct_cvtFromF
8cf50 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  t, 0, 0);.    sq
8cf60 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8cf70 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74 46  ction (db, "CvtF
8cf80 72 6f 6d 59 64 22 2c 20 31 2c 20 53 51 4c 49 54  romYd", 1, SQLIT
8cf90 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8cfa0 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d 59 64    fnct_cvtFromYd
8cfb0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8cfc0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8cfd0 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74 46 72  tion (db, "CvtFr
8cfe0 6f 6d 4d 69 22 2c 20 31 2c 20 53 51 4c 49 54 45  omMi", 1, SQLITE
8cff0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
8d000 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d 4d 69 2c   fnct_cvtFromMi,
8d010 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8d020 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8d030 69 6f 6e 20 28 64 62 2c 20 22 43 76 74 46 72 6f  ion (db, "CvtFro
8d040 6d 46 61 74 68 22 2c 20 31 2c 20 53 51 4c 49 54  mFath", 1, SQLIT
8d050 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8d060 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d 46 61    fnct_cvtFromFa
8d070 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  th, 0, 0);.    s
8d080 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8d090 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74  nction (db, "Cvt
8d0a0 46 72 6f 6d 43 68 22 2c 20 31 2c 20 53 51 4c 49  FromCh", 1, SQLI
8d0b0 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8d0c0 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d 43     fnct_cvtFromC
8d0d0 68 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  h, 0, 0);.    sq
8d0e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8d0f0 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74 46  ction (db, "CvtF
8d100 72 6f 6d 4c 69 6e 6b 22 2c 20 31 2c 20 53 51 4c  romLink", 1, SQL
8d110 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8d120 20 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d      fnct_cvtFrom
8d130 4c 69 6e 6b 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Link, 0, 0);.   
8d140 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8d150 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
8d160 76 74 46 72 6f 6d 55 73 49 6e 22 2c 20 31 2c 20  vtFromUsIn", 1, 
8d170 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8d180 09 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 46  ..     fnct_cvtF
8d190 72 6f 6d 55 73 49 6e 2c 20 30 2c 20 30 29 3b 0a  romUsIn, 0, 0);.
8d1a0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8d1b0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8d1c0 20 22 43 76 74 46 72 6f 6d 55 73 46 74 22 2c 20   "CvtFromUsFt", 
8d1d0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8d1e0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 63  ,....     fnct_c
8d1f0 76 74 46 72 6f 6d 55 73 46 74 2c 20 30 2c 20 30  vtFromUsFt, 0, 0
8d200 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8d210 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8d220 64 62 2c 20 22 43 76 74 46 72 6f 6d 55 73 59 64  db, "CvtFromUsYd
8d230 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
8d240 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
8d250 74 5f 63 76 74 46 72 6f 6d 55 73 59 64 2c 20 30  t_cvtFromUsYd, 0
8d260 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8d270 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8d280 6e 20 28 64 62 2c 20 22 43 76 74 46 72 6f 6d 55  n (db, "CvtFromU
8d290 73 43 68 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  sCh", 1, SQLITE_
8d2a0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8d2b0 66 6e 63 74 5f 63 76 74 46 72 6f 6d 55 73 43 68  fnct_cvtFromUsCh
8d2c0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8d2d0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8d2e0 74 69 6f 6e 20 28 64 62 2c 20 22 43 76 74 46 72  tion (db, "CvtFr
8d2f0 6f 6d 55 73 4d 69 22 2c 20 31 2c 20 53 51 4c 49  omUsMi", 1, SQLI
8d300 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8d310 20 20 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d 55     fnct_cvtFromU
8d320 73 4d 69 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  sMi, 0, 0);.    
8d330 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8d340 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 76  unction (db, "Cv
8d350 74 46 72 6f 6d 49 6e 64 46 74 22 2c 20 31 2c 20  tFromIndFt", 1, 
8d360 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8d370 09 09 20 20 20 20 20 66 6e 63 74 5f 63 76 74 46  ..     fnct_cvtF
8d380 72 6f 6d 49 6e 64 46 74 2c 20 30 2c 20 30 29 3b  romIndFt, 0, 0);
8d390 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8d3a0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8d3b0 2c 20 22 43 76 74 46 72 6f 6d 49 6e 64 59 64 22  , "CvtFromIndYd"
8d3c0 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
8d3d0 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
8d3e0 5f 63 76 74 46 72 6f 6d 49 6e 64 59 64 2c 20 30  _cvtFromIndYd, 0
8d3f0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8d400 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8d410 6e 20 28 64 62 2c 20 22 43 76 74 46 72 6f 6d 49  n (db, "CvtFromI
8d420 6e 64 43 68 22 2c 20 31 2c 20 53 51 4c 49 54 45  ndCh", 1, SQLITE
8d430 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
8d440 20 66 6e 63 74 5f 63 76 74 46 72 6f 6d 49 6e 64   fnct_cvtFromInd
8d450 43 68 2c 20 30 2c 20 30 29 3b 0a 0a 23 69 66 6e  Ch, 0, 0);..#ifn
8d460 64 65 66 20 4f 4d 49 54 5f 4d 41 54 48 53 51 4c  def OMIT_MATHSQL
8d470 09 09 2f 2a 20 73 75 70 70 6f 72 74 69 6e 67 20  ../* supporting 
8d480 53 51 4c 20 6d 61 74 68 20 66 75 6e 63 74 69 6f  SQL math functio
8d490 6e 73 20 2a 2f 0a 0a 2f 2a 20 73 6f 6d 65 20 65  ns */../* some e
8d4a0 78 74 72 61 20 6d 61 74 68 20 66 75 6e 63 74 69  xtra math functi
8d4b0 6f 6e 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ons */.    sqlit
8d4c0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8d4d0 6f 6e 20 28 64 62 2c 20 22 61 63 6f 73 22 2c 20  on (db, "acos", 
8d4e0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8d4f0 2c 20 66 6e 63 74 5f 6d 61 74 68 5f 61 63 6f 73  , fnct_math_acos
8d500 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b  , 0,....     0);
8d510 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8d520 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8d530 2c 20 22 61 73 69 6e 22 2c 20 31 2c 20 53 51 4c  , "asin", 1, SQL
8d540 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8d550 5f 6d 61 74 68 5f 61 73 69 6e 2c 20 30 2c 0a 09  _math_asin, 0,..
8d560 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73  ..     0);.    s
8d570 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8d580 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 61 74 61  nction (db, "ata
8d590 6e 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  n", 1, SQLITE_AN
8d5a0 59 2c 20 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f  Y, 0, fnct_math_
8d5b0 61 74 61 6e 2c 20 30 2c 0a 09 09 09 20 20 20 20  atan, 0,....    
8d5c0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
8d5d0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
8d5e0 20 28 64 62 2c 20 22 63 65 69 6c 22 2c 20 31 2c   (db, "ceil", 1,
8d5f0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20   SQLITE_ANY, 0, 
8d600 66 6e 63 74 5f 6d 61 74 68 5f 63 65 69 6c 2c 20  fnct_math_ceil, 
8d610 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20  0,....     0);. 
8d620 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8d630 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8d640 22 63 65 69 6c 69 6e 67 22 2c 20 31 2c 20 53 51  "ceiling", 1, SQ
8d650 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
8d660 74 5f 6d 61 74 68 5f 63 65 69 6c 2c 0a 09 09 09  t_math_ceil,....
8d670 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20       0, 0);.    
8d680 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8d690 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 63 6f  unction (db, "co
8d6a0 73 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  s", 1, SQLITE_AN
8d6b0 59 2c 20 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f  Y, 0, fnct_math_
8d6c0 63 6f 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  cos, 0, 0);.    
8d6d0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8d6e0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 63 6f  unction (db, "co
8d6f0 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  t", 1, SQLITE_AN
8d700 59 2c 20 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f  Y, 0, fnct_math_
8d710 63 6f 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  cot, 0, 0);.    
8d720 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8d730 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 64 65  unction (db, "de
8d740 67 72 65 65 73 22 2c 20 31 2c 20 53 51 4c 49 54  grees", 1, SQLIT
8d750 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8d760 20 20 66 6e 63 74 5f 6d 61 74 68 5f 64 65 67 72    fnct_math_degr
8d770 65 65 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ees, 0, 0);.    
8d780 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8d790 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 65 78  unction (db, "ex
8d7a0 70 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  p", 1, SQLITE_AN
8d7b0 59 2c 20 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f  Y, 0, fnct_math_
8d7c0 65 78 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  exp, 0, 0);.    
8d7d0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8d7e0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 66 6c  unction (db, "fl
8d7f0 6f 6f 72 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  oor", 1, SQLITE_
8d800 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 6d 61 74  ANY, 0, fnct_mat
8d810 68 5f 66 6c 6f 6f 72 2c 0a 09 09 09 20 20 20 20  h_floor,....    
8d820 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8d830 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8d840 69 6f 6e 20 28 64 62 2c 20 22 6c 6e 22 2c 20 31  ion (db, "ln", 1
8d850 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8d860 20 66 6e 63 74 5f 6d 61 74 68 5f 6c 6f 67 6e 2c   fnct_math_logn,
8d870 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8d880 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8d890 69 6f 6e 20 28 64 62 2c 20 22 6c 6f 67 22 2c 20  ion (db, "log", 
8d8a0 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  1, SQLITE_ANY, 0
8d8b0 2c 20 66 6e 63 74 5f 6d 61 74 68 5f 6c 6f 67 6e  , fnct_math_logn
8d8c0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8d8d0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8d8e0 74 69 6f 6e 20 28 64 62 2c 20 22 6c 6f 67 22 2c  tion (db, "log",
8d8f0 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
8d900 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f 6c 6f 67  0, fnct_math_log
8d910 6e 32 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 30  n2, 0,....     0
8d920 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8d930 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8d940 64 62 2c 20 22 6c 6f 67 32 22 2c 20 31 2c 20 53  db, "log2", 1, S
8d950 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8d960 63 74 5f 6d 61 74 68 5f 6c 6f 67 5f 32 2c 20 30  ct_math_log_2, 0
8d970 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20  ,....     0);.  
8d980 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8d990 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8d9a0 6c 6f 67 31 30 22 2c 20 31 2c 20 53 51 4c 49 54  log10", 1, SQLIT
8d9b0 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 6d  E_ANY, 0, fnct_m
8d9c0 61 74 68 5f 6c 6f 67 5f 31 30 2c 0a 09 09 09 20  ath_log_10,.... 
8d9d0 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73      0, 0);.    s
8d9e0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8d9f0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 70 69 22  nction (db, "pi"
8da00 2c 20 30 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 0, SQLITE_ANY,
8da10 20 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f 70 69   0, fnct_math_pi
8da20 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8da30 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8da40 74 69 6f 6e 20 28 64 62 2c 20 22 70 6f 77 22 2c  tion (db, "pow",
8da50 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
8da60 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f 70 6f 77  0, fnct_math_pow
8da70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8da80 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8da90 74 69 6f 6e 20 28 64 62 2c 20 22 70 6f 77 65 72  tion (db, "power
8daa0 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
8dab0 2c 20 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f 70  , 0, fnct_math_p
8dac0 6f 77 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 30  ow, 0,....     0
8dad0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8dae0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8daf0 64 62 2c 20 22 72 61 64 69 61 6e 73 22 2c 20 31  db, "radians", 1
8db00 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8db10 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 6d 61  ....     fnct_ma
8db20 74 68 5f 72 61 64 69 61 6e 73 2c 20 30 2c 20 30  th_radians, 0, 0
8db30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8db40 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8db50 64 62 2c 20 22 72 6f 75 6e 64 22 2c 20 31 2c 20  db, "round", 1, 
8db60 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8db70 6e 63 74 5f 6d 61 74 68 5f 72 6f 75 6e 64 2c 0a  nct_math_round,.
8db80 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20  ...     0, 0);. 
8db90 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8dba0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8dbb0 22 73 69 67 6e 22 2c 20 31 2c 20 53 51 4c 49 54  "sign", 1, SQLIT
8dbc0 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 6d  E_ANY, 0, fnct_m
8dbd0 61 74 68 5f 73 69 67 6e 2c 20 30 2c 0a 09 09 09  ath_sign, 0,....
8dbe0 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73 71 6c       0);.    sql
8dbf0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8dc00 74 69 6f 6e 20 28 64 62 2c 20 22 73 69 6e 22 2c  tion (db, "sin",
8dc10 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
8dc20 30 2c 20 66 6e 63 74 5f 6d 61 74 68 5f 73 69 6e  0, fnct_math_sin
8dc30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8dc40 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8dc50 74 69 6f 6e 20 28 64 62 2c 20 22 73 74 64 64 65  tion (db, "stdde
8dc60 76 5f 70 6f 70 22 2c 20 31 2c 20 53 51 4c 49 54  v_pop", 1, SQLIT
8dc70 45 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09 09 09  E_ANY, 0, 0,....
8dc80 20 20 20 20 20 66 6e 63 74 5f 6d 61 74 68 5f 73       fnct_math_s
8dc90 74 64 64 65 76 5f 73 74 65 70 2c 20 66 6e 63 74  tddev_step, fnct
8dca0 5f 6d 61 74 68 5f 73 74 64 64 65 76 5f 70 6f 70  _math_stddev_pop
8dcb0 5f 66 69 6e 61 6c 29 3b 0a 20 20 20 20 73 71 6c  _final);.    sql
8dcc0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8dcd0 74 69 6f 6e 20 28 64 62 2c 20 22 73 74 64 64 65  tion (db, "stdde
8dce0 76 5f 73 61 6d 70 22 2c 20 31 2c 20 53 51 4c 49  v_samp", 1, SQLI
8dcf0 54 45 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09 09  TE_ANY, 0, 0,...
8dd00 09 20 20 20 20 20 66 6e 63 74 5f 6d 61 74 68 5f  .     fnct_math_
8dd10 73 74 64 64 65 76 5f 73 74 65 70 2c 0a 09 09 09  stddev_step,....
8dd20 20 20 20 20 20 66 6e 63 74 5f 6d 61 74 68 5f 73       fnct_math_s
8dd30 74 64 64 65 76 5f 73 61 6d 70 5f 66 69 6e 61 6c  tddev_samp_final
8dd40 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8dd50 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8dd60 64 62 2c 20 22 73 71 72 74 22 2c 20 31 2c 20 53  db, "sqrt", 1, S
8dd70 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8dd80 63 74 5f 6d 61 74 68 5f 73 71 72 74 2c 20 30 2c  ct_math_sqrt, 0,
8dd90 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20  ....     0);.   
8dda0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8ddb0 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 74  function (db, "t
8ddc0 61 6e 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  an", 1, SQLITE_A
8ddd0 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 6d 61 74 68  NY, 0, fnct_math
8dde0 5f 74 61 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20  _tan, 0, 0);.   
8ddf0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8de00 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 76  function (db, "v
8de10 61 72 5f 70 6f 70 22 2c 20 31 2c 20 53 51 4c 49  ar_pop", 1, SQLI
8de20 54 45 5f 41 4e 59 2c 20 30 2c 20 30 2c 0a 09 09  TE_ANY, 0, 0,...
8de30 09 20 20 20 20 20 66 6e 63 74 5f 6d 61 74 68 5f  .     fnct_math_
8de40 73 74 64 64 65 76 5f 73 74 65 70 2c 20 66 6e 63  stddev_step, fnc
8de50 74 5f 6d 61 74 68 5f 76 61 72 5f 70 6f 70 5f 66  t_math_var_pop_f
8de60 69 6e 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  inal);.    sqlit
8de70 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8de80 6f 6e 20 28 64 62 2c 20 22 76 61 72 5f 73 61 6d  on (db, "var_sam
8de90 70 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  p", 1, SQLITE_AN
8dea0 59 2c 20 30 2c 20 30 2c 0a 09 09 09 20 20 20 20  Y, 0, 0,....    
8deb0 20 66 6e 63 74 5f 6d 61 74 68 5f 73 74 64 64 65   fnct_math_stdde
8dec0 76 5f 73 74 65 70 2c 20 66 6e 63 74 5f 6d 61 74  v_step, fnct_mat
8ded0 68 5f 76 61 72 5f 73 61 6d 70 5f 66 69 6e 61 6c  h_var_samp_final
8dee0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 65 6e  );..#endif /* en
8def0 64 20 73 75 70 70 6f 72 74 69 6e 67 20 53 51 4c  d supporting SQL
8df00 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 20   math functions 
8df10 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4f 4d 49 54  */..#ifndef OMIT
8df20 5f 50 52 4f 4a 09 09 2f 2a 20 69 6e 63 6c 75 64  _PROJ../* includ
8df30 69 6e 67 20 50 52 4f 4a 2e 34 20 2a 2f 0a 0a 20  ing PROJ.4 */.. 
8df40 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8df50 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8df60 22 54 72 61 6e 73 66 6f 72 6d 22 2c 20 32 2c 20  "Transform", 2, 
8df70 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8df80 09 09 20 20 20 20 20 66 6e 63 74 5f 54 72 61 6e  ..     fnct_Tran
8df90 73 66 6f 72 6d 2c 20 30 2c 20 30 29 3b 0a 20 20  sform, 0, 0);.  
8dfa0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8dfb0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8dfc0 53 54 5f 54 72 61 6e 73 66 6f 72 6d 22 2c 20 32  ST_Transform", 2
8dfd0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8dfe0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 54 72  ....     fnct_Tr
8dff0 61 6e 73 66 6f 72 6d 2c 20 30 2c 20 30 29 3b 0a  ansform, 0, 0);.
8e000 0a 23 65 6e 64 69 66 20 2f 2a 20 65 6e 64 20 69  .#endif /* end i
8e010 6e 63 6c 75 64 69 6e 67 20 50 52 4f 4a 2e 34 20  ncluding PROJ.4 
8e020 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4f 4d 49 54  */..#ifndef OMIT
8e030 5f 47 45 4f 53 09 09 2f 2a 20 69 6e 63 6c 75 64  _GEOS../* includ
8e040 69 6e 67 20 47 45 4f 53 20 2a 2f 0a 0a 20 20 20  ing GEOS */..   
8e050 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8e060 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 42  function (db, "B
8e070 6f 75 6e 64 61 72 79 22 2c 20 31 2c 20 53 51 4c  oundary", 1, SQL
8e080 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8e090 5f 42 6f 75 6e 64 61 72 79 2c 0a 09 09 09 20 20  _Boundary,....  
8e0a0 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71     0, 0);.    sq
8e0b0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8e0c0 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 42  ction (db, "ST_B
8e0d0 6f 75 6e 64 61 72 79 22 2c 20 31 2c 20 53 51 4c  oundary", 1, SQL
8e0e0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8e0f0 20 20 20 20 66 6e 63 74 5f 42 6f 75 6e 64 61 72      fnct_Boundar
8e100 79 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  y, 0, 0);.    sq
8e110 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8e120 63 74 69 6f 6e 20 28 64 62 2c 20 22 49 73 43 6c  ction (db, "IsCl
8e130 6f 73 65 64 22 2c 20 31 2c 20 53 51 4c 49 54 45  osed", 1, SQLITE
8e140 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 49 73  _ANY, 0, fnct_Is
8e150 43 6c 6f 73 65 64 2c 0a 09 09 09 20 20 20 20 20  Closed,....     
8e160 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
8e170 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8e180 6f 6e 20 28 64 62 2c 20 22 53 54 5f 49 73 43 6c  on (db, "ST_IsCl
8e190 6f 73 65 64 22 2c 20 31 2c 20 53 51 4c 49 54 45  osed", 1, SQLITE
8e1a0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
8e1b0 20 66 6e 63 74 5f 49 73 43 6c 6f 73 65 64 2c 20   fnct_IsClosed, 
8e1c0 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
8e1d0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8e1e0 6f 6e 20 28 64 62 2c 20 22 49 73 53 69 6d 70 6c  on (db, "IsSimpl
8e1f0 65 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  e", 1, SQLITE_AN
8e200 59 2c 20 30 2c 20 66 6e 63 74 5f 49 73 53 69 6d  Y, 0, fnct_IsSim
8e210 70 6c 65 2c 0a 09 09 09 20 20 20 20 20 30 2c 20  ple,....     0, 
8e220 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8e230 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8e240 28 64 62 2c 20 22 53 54 5f 49 73 53 69 6d 70 6c  (db, "ST_IsSimpl
8e250 65 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  e", 1, SQLITE_AN
8e260 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8e270 63 74 5f 49 73 53 69 6d 70 6c 65 2c 20 30 2c 20  ct_IsSimple, 0, 
8e280 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8e290 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8e2a0 28 64 62 2c 20 22 49 73 52 69 6e 67 22 2c 20 31  (db, "IsRing", 1
8e2b0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8e2c0 20 66 6e 63 74 5f 49 73 52 69 6e 67 2c 20 30 2c   fnct_IsRing, 0,
8e2d0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
8e2e0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
8e2f0 20 28 64 62 2c 20 22 53 54 5f 49 73 52 69 6e 67   (db, "ST_IsRing
8e300 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
8e310 2c 20 30 2c 20 66 6e 63 74 5f 49 73 52 69 6e 67  , 0, fnct_IsRing
8e320 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b  ,....     0, 0);
8e330 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8e340 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8e350 2c 20 22 49 73 56 61 6c 69 64 22 2c 20 31 2c 20  , "IsValid", 1, 
8e360 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8e370 6e 63 74 5f 49 73 56 61 6c 69 64 2c 20 30 2c 0a  nct_IsValid, 0,.
8e380 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20  ...     0);.    
8e390 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8e3a0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
8e3b0 5f 49 73 56 61 6c 69 64 22 2c 20 31 2c 20 53 51  _IsValid", 1, SQ
8e3c0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
8e3d0 74 5f 49 73 56 61 6c 69 64 2c 0a 09 09 09 20 20  t_IsValid,....  
8e3e0 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71     0, 0);.    sq
8e3f0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8e400 63 74 69 6f 6e 20 28 64 62 2c 20 22 47 4c 65 6e  ction (db, "GLen
8e410 67 74 68 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  gth", 1, SQLITE_
8e420 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 4c 65 6e  ANY, 0, fnct_Len
8e430 67 74 68 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  gth, 0,....     
8e440 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8e450 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8e460 28 64 62 2c 20 22 53 54 5f 4c 65 6e 67 74 68 22  (db, "ST_Length"
8e470 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 1, SQLITE_ANY,
8e480 20 30 2c 20 66 6e 63 74 5f 4c 65 6e 67 74 68 2c   0, fnct_Length,
8e490 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a  ....     0, 0);.
8e4a0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8e4b0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8e4c0 20 22 41 72 65 61 22 2c 20 31 2c 20 53 51 4c 49   "Area", 1, SQLI
8e4d0 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
8e4e0 41 72 65 61 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Area, 0, 0);.   
8e4f0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8e500 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
8e510 54 5f 41 72 65 61 22 2c 20 31 2c 20 53 51 4c 49  T_Area", 1, SQLI
8e520 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f  TE_ANY, 0, fnct_
8e530 41 72 65 61 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Area, 0, 0);.   
8e540 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8e550 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43  function (db, "C
8e560 65 6e 74 72 6f 69 64 22 2c 20 31 2c 20 53 51 4c  entroid", 1, SQL
8e570 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8e580 5f 43 65 6e 74 72 6f 69 64 2c 0a 09 09 09 20 20  _Centroid,....  
8e590 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71     0, 0);.    sq
8e5a0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8e5b0 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 43  ction (db, "ST_C
8e5c0 65 6e 74 72 6f 69 64 22 2c 20 31 2c 20 53 51 4c  entroid", 1, SQL
8e5d0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8e5e0 20 20 20 20 66 6e 63 74 5f 43 65 6e 74 72 6f 69      fnct_Centroi
8e5f0 64 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  d, 0, 0);.    sq
8e600 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8e610 63 74 69 6f 6e 20 28 64 62 2c 20 22 50 6f 69 6e  ction (db, "Poin
8e620 74 4f 6e 53 75 72 66 61 63 65 22 2c 20 31 2c 20  tOnSurface", 1, 
8e630 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8e640 09 09 20 20 20 20 20 66 6e 63 74 5f 50 6f 69 6e  ..     fnct_Poin
8e650 74 4f 6e 53 75 72 66 61 63 65 2c 20 30 2c 20 30  tOnSurface, 0, 0
8e660 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8e670 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8e680 64 62 2c 20 22 53 54 5f 50 6f 69 6e 74 4f 6e 53  db, "ST_PointOnS
8e690 75 72 66 61 63 65 22 2c 20 31 2c 20 53 51 4c 49  urface", 1, SQLI
8e6a0 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8e6b0 20 20 20 66 6e 63 74 5f 50 6f 69 6e 74 4f 6e 53     fnct_PointOnS
8e6c0 75 72 66 61 63 65 2c 20 30 2c 20 30 29 3b 0a 20  urface, 0, 0);. 
8e6d0 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8e6e0 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8e6f0 22 53 69 6d 70 6c 69 66 79 22 2c 20 32 2c 20 53  "Simplify", 2, S
8e700 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8e710 63 74 5f 53 69 6d 70 6c 69 66 79 2c 0a 09 09 09  ct_Simplify,....
8e720 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20       0, 0);.    
8e730 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8e740 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
8e750 5f 47 65 6e 65 72 61 6c 69 7a 65 22 2c 20 32 2c  _Generalize", 2,
8e760 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8e770 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53 69 6d  ...     fnct_Sim
8e780 70 6c 69 66 79 2c 20 30 2c 20 30 29 3b 0a 20 20  plify, 0, 0);.  
8e790 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8e7a0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8e7b0 53 69 6d 70 6c 69 66 79 50 72 65 73 65 72 76 65  SimplifyPreserve
8e7c0 54 6f 70 6f 6c 6f 67 79 22 2c 20 32 2c 20 53 51  Topology", 2, SQ
8e7d0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
8e7e0 20 20 20 20 20 66 6e 63 74 5f 53 69 6d 70 6c 69       fnct_Simpli
8e7f0 66 79 50 72 65 73 65 72 76 65 54 6f 70 6f 6c 6f  fyPreserveTopolo
8e800 67 79 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  gy, 0, 0);.    s
8e810 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8e820 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 43 6f 6e  nction (db, "Con
8e830 76 65 78 48 75 6c 6c 22 2c 20 31 2c 20 53 51 4c  vexHull", 1, SQL
8e840 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
8e850 20 20 20 20 66 6e 63 74 5f 43 6f 6e 76 65 78 48      fnct_ConvexH
8e860 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ull, 0, 0);.    
8e870 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8e880 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
8e890 5f 43 6f 6e 76 65 78 48 75 6c 6c 22 2c 20 31 2c  _ConvexHull", 1,
8e8a0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8e8b0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43 6f 6e  ...     fnct_Con
8e8c0 76 65 78 48 75 6c 6c 2c 20 30 2c 20 30 29 3b 0a  vexHull, 0, 0);.
8e8d0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8e8e0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8e8f0 20 22 42 75 66 66 65 72 22 2c 20 32 2c 20 53 51   "Buffer", 2, SQ
8e900 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
8e910 74 5f 42 75 66 66 65 72 2c 20 30 2c 20 30 29 3b  t_Buffer, 0, 0);
8e920 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8e930 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8e940 2c 20 22 53 54 5f 42 75 66 66 65 72 22 2c 20 32  , "ST_Buffer", 2
8e950 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8e960 20 66 6e 63 74 5f 42 75 66 66 65 72 2c 0a 09 09   fnct_Buffer,...
8e970 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20  .     0, 0);.   
8e980 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8e990 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 49  function (db, "I
8e9a0 6e 74 65 72 73 65 63 74 69 6f 6e 22 2c 20 32 2c  ntersection", 2,
8e9b0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8e9c0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 49 6e 74  ...     fnct_Int
8e9d0 65 72 73 65 63 74 69 6f 6e 2c 20 30 2c 20 30 29  ersection, 0, 0)
8e9e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8e9f0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8ea00 62 2c 20 22 53 54 5f 49 6e 74 65 72 73 65 63 74  b, "ST_Intersect
8ea10 69 6f 6e 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  ion", 2, SQLITE_
8ea20 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8ea30 66 6e 63 74 5f 49 6e 74 65 72 73 65 63 74 69 6f  fnct_Intersectio
8ea40 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  n, 0, 0);.    sq
8ea50 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8ea60 63 74 69 6f 6e 20 28 64 62 2c 20 22 47 55 6e 69  ction (db, "GUni
8ea70 6f 6e 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41  on", 1, SQLITE_A
8ea80 4e 59 2c 20 30 2c 20 30 2c 0a 09 09 09 20 20 20  NY, 0, 0,....   
8ea90 20 20 66 6e 63 74 5f 55 6e 69 6f 6e 5f 73 74 65    fnct_Union_ste
8eaa0 70 2c 20 66 6e 63 74 5f 55 6e 69 6f 6e 5f 66 69  p, fnct_Union_fi
8eab0 6e 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  nal);.    sqlite
8eac0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8ead0 6e 20 28 64 62 2c 20 22 47 55 6e 69 6f 6e 22 2c  n (db, "GUnion",
8eae0 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
8eaf0 30 2c 20 66 6e 63 74 5f 55 6e 69 6f 6e 2c 20 30  0, fnct_Union, 0
8eb00 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8eb10 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8eb20 6e 20 28 64 62 2c 20 22 53 54 5f 55 6e 69 6f 6e  n (db, "ST_Union
8eb30 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
8eb40 2c 20 30 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  , 0, 0,....     
8eb50 66 6e 63 74 5f 55 6e 69 6f 6e 5f 73 74 65 70 2c  fnct_Union_step,
8eb60 20 66 6e 63 74 5f 55 6e 69 6f 6e 5f 66 69 6e 61   fnct_Union_fina
8eb70 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  l);.    sqlite3_
8eb80 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8eb90 28 64 62 2c 20 22 53 54 5f 55 6e 69 6f 6e 22 2c  (db, "ST_Union",
8eba0 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
8ebb0 30 2c 20 66 6e 63 74 5f 55 6e 69 6f 6e 2c 20 30  0, fnct_Union, 0
8ebc0 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20 20  ,....     0);.  
8ebd0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8ebe0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8ebf0 44 69 66 66 65 72 65 6e 63 65 22 2c 20 32 2c 20  Difference", 2, 
8ec00 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8ec10 09 09 20 20 20 20 20 66 6e 63 74 5f 44 69 66 66  ..     fnct_Diff
8ec20 65 72 65 6e 63 65 2c 20 30 2c 20 30 29 3b 0a 20  erence, 0, 0);. 
8ec30 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8ec40 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8ec50 22 53 54 5f 44 69 66 66 65 72 65 6e 63 65 22 2c  "ST_Difference",
8ec60 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
8ec70 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
8ec80 44 69 66 66 65 72 65 6e 63 65 2c 20 30 2c 20 30  Difference, 0, 0
8ec90 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8eca0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8ecb0 64 62 2c 20 22 53 79 6d 44 69 66 66 65 72 65 6e  db, "SymDifferen
8ecc0 63 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  ce", 2, SQLITE_A
8ecd0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
8ece0 6e 63 74 5f 53 79 6d 44 69 66 66 65 72 65 6e 63  nct_SymDifferenc
8ecf0 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  e, 0, 0);.    sq
8ed00 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8ed10 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 53  ction (db, "ST_S
8ed20 79 6d 44 69 66 66 65 72 65 6e 63 65 22 2c 20 32  ymDifference", 2
8ed30 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8ed40 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53 79  ....     fnct_Sy
8ed50 6d 44 69 66 66 65 72 65 6e 63 65 2c 20 30 2c 20  mDifference, 0, 
8ed60 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8ed70 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8ed80 28 64 62 2c 20 22 45 71 75 61 6c 73 22 2c 20 32  (db, "Equals", 2
8ed90 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8eda0 20 66 6e 63 74 5f 45 71 75 61 6c 73 2c 20 30 2c   fnct_Equals, 0,
8edb0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
8edc0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
8edd0 20 28 64 62 2c 20 22 53 54 5f 45 71 75 61 6c 73   (db, "ST_Equals
8ede0 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
8edf0 2c 20 30 2c 20 66 6e 63 74 5f 45 71 75 61 6c 73  , 0, fnct_Equals
8ee00 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b  ,....     0, 0);
8ee10 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8ee20 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8ee30 2c 20 22 49 6e 74 65 72 73 65 63 74 73 22 2c 20  , "Intersects", 
8ee40 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
8ee50 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 49  ,....     fnct_I
8ee60 6e 74 65 72 73 65 63 74 73 2c 20 30 2c 20 30 29  ntersects, 0, 0)
8ee70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8ee80 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8ee90 62 2c 20 22 53 54 5f 49 6e 74 65 72 73 65 63 74  b, "ST_Intersect
8eea0 73 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e  s", 2, SQLITE_AN
8eeb0 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8eec0 63 74 5f 49 6e 74 65 72 73 65 63 74 73 2c 20 30  ct_Intersects, 0
8eed0 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8eee0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8eef0 6e 20 28 64 62 2c 20 22 44 69 73 6a 6f 69 6e 74  n (db, "Disjoint
8ef00 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
8ef10 2c 20 30 2c 20 66 6e 63 74 5f 44 69 73 6a 6f 69  , 0, fnct_Disjoi
8ef20 6e 74 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 30  nt,....     0, 0
8ef30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8ef40 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8ef50 64 62 2c 20 22 53 54 5f 44 69 73 6a 6f 69 6e 74  db, "ST_Disjoint
8ef60 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
8ef70 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
8ef80 74 5f 44 69 73 6a 6f 69 6e 74 2c 20 30 2c 20 30  t_Disjoint, 0, 0
8ef90 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8efa0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8efb0 64 62 2c 20 22 4f 76 65 72 6c 61 70 73 22 2c 20  db, "Overlaps", 
8efc0 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
8efd0 2c 20 66 6e 63 74 5f 4f 76 65 72 6c 61 70 73 2c  , fnct_Overlaps,
8efe0 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a  ....     0, 0);.
8eff0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8f000 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8f010 20 22 53 54 5f 4f 76 65 72 6c 61 70 73 22 2c 20   "ST_Overlaps", 
8f020 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
8f030 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4f  ,....     fnct_O
8f040 76 65 72 6c 61 70 73 2c 20 30 2c 20 30 29 3b 0a  verlaps, 0, 0);.
8f050 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8f060 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8f070 20 22 43 72 6f 73 73 65 73 22 2c 20 32 2c 20 53   "Crosses", 2, S
8f080 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8f090 63 74 5f 43 72 6f 73 73 65 73 2c 20 30 2c 0a 09  ct_Crosses, 0,..
8f0a0 09 09 20 20 20 20 20 30 29 3b 0a 20 20 20 20 73  ..     0);.    s
8f0b0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8f0c0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
8f0d0 43 72 6f 73 73 65 73 22 2c 20 32 2c 20 53 51 4c  Crosses", 2, SQL
8f0e0 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74  ITE_ANY, 0, fnct
8f0f0 5f 43 72 6f 73 73 65 73 2c 0a 09 09 09 20 20 20  _Crosses,....   
8f100 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c    0, 0);.    sql
8f110 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8f120 74 69 6f 6e 20 28 64 62 2c 20 22 54 6f 75 63 68  tion (db, "Touch
8f130 65 73 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  es", 2, SQLITE_A
8f140 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 54 6f 75 63  NY, 0, fnct_Touc
8f150 68 65 73 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  hes, 0,....     
8f160 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8f170 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8f180 28 64 62 2c 20 22 53 54 5f 54 6f 75 63 68 65 73  (db, "ST_Touches
8f190 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
8f1a0 2c 20 30 2c 20 66 6e 63 74 5f 54 6f 75 63 68 65  , 0, fnct_Touche
8f1b0 73 2c 0a 09 09 09 20 20 20 20 20 30 2c 20 30 29  s,....     0, 0)
8f1c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8f1d0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8f1e0 62 2c 20 22 57 69 74 68 69 6e 22 2c 20 32 2c 20  b, "Within", 2, 
8f1f0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8f200 6e 63 74 5f 57 69 74 68 69 6e 2c 20 30 2c 20 30  nct_Within, 0, 0
8f210 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8f220 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8f230 64 62 2c 20 22 53 54 5f 57 69 74 68 69 6e 22 2c  db, "ST_Within",
8f240 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
8f250 30 2c 20 66 6e 63 74 5f 57 69 74 68 69 6e 2c 0a  0, fnct_Within,.
8f260 09 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20  ...     0, 0);. 
8f270 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
8f280 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
8f290 22 43 6f 6e 74 61 69 6e 73 22 2c 20 32 2c 20 53  "Contains", 2, S
8f2a0 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e  QLITE_ANY, 0, fn
8f2b0 63 74 5f 43 6f 6e 74 61 69 6e 73 2c 0a 09 09 09  ct_Contains,....
8f2c0 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 20 20       0, 0);.    
8f2d0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8f2e0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
8f2f0 5f 43 6f 6e 74 61 69 6e 73 22 2c 20 32 2c 20 53  _Contains", 2, S
8f300 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8f310 09 20 20 20 20 20 66 6e 63 74 5f 43 6f 6e 74 61  .     fnct_Conta
8f320 69 6e 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ins, 0, 0);.    
8f330 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8f340 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 52 65  unction (db, "Re
8f350 6c 61 74 65 22 2c 20 33 2c 20 53 51 4c 49 54 45  late", 3, SQLITE
8f360 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 52 65  _ANY, 0, fnct_Re
8f370 6c 61 74 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  late, 0, 0);.   
8f380 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8f390 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
8f3a0 54 5f 52 65 6c 61 74 65 22 2c 20 33 2c 20 53 51  T_Relate", 3, SQ
8f3b0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66 6e 63  LITE_ANY, 0, fnc
8f3c0 74 5f 52 65 6c 61 74 65 2c 0a 09 09 09 20 20 20  t_Relate,....   
8f3d0 20 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c    0, 0);.    sql
8f3e0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8f3f0 74 69 6f 6e 20 28 64 62 2c 20 22 44 69 73 74 61  tion (db, "Dista
8f400 6e 63 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  nce", 2, SQLITE_
8f410 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 44 69 73  ANY, 0, fnct_Dis
8f420 74 61 6e 63 65 2c 0a 09 09 09 20 20 20 20 20 30  tance,....     0
8f430 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8f440 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8f450 6e 20 28 64 62 2c 20 22 53 54 5f 44 69 73 74 61  n (db, "ST_Dista
8f460 6e 63 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  nce", 2, SQLITE_
8f470 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8f480 66 6e 63 74 5f 44 69 73 74 61 6e 63 65 2c 20 30  fnct_Distance, 0
8f490 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8f4a0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8f4b0 6e 20 28 64 62 2c 20 22 50 74 44 69 73 74 57 69  n (db, "PtDistWi
8f4c0 74 68 69 6e 22 2c 20 33 2c 20 53 51 4c 49 54 45  thin", 3, SQLITE
8f4d0 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20  _ANY, 0,....    
8f4e0 20 66 6e 63 74 5f 50 74 44 69 73 74 57 69 74 68   fnct_PtDistWith
8f4f0 69 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  in, 0, 0);.    s
8f500 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8f510 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 50 74 44  nction (db, "PtD
8f520 69 73 74 57 69 74 68 69 6e 22 2c 20 34 2c 20 53  istWithin", 4, S
8f530 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
8f540 09 20 20 20 20 20 66 6e 63 74 5f 50 74 44 69 73  .     fnct_PtDis
8f550 74 57 69 74 68 69 6e 2c 20 30 2c 20 30 29 3b 0a  tWithin, 0, 0);.
8f560 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8f570 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8f580 20 22 42 64 50 6f 6c 79 46 72 6f 6d 54 65 78 74   "BdPolyFromText
8f590 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 1, SQLITE_ANY
8f5a0 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
8f5b0 74 5f 42 64 50 6f 6c 79 46 72 6f 6d 54 65 78 74  t_BdPolyFromText
8f5c0 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  1, 0, 0);.    sq
8f5d0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8f5e0 63 74 69 6f 6e 20 28 64 62 2c 20 22 42 64 50 6f  ction (db, "BdPo
8f5f0 6c 79 46 72 6f 6d 54 65 78 74 22 2c 20 32 2c 20  lyFromText", 2, 
8f600 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8f610 09 09 20 20 20 20 20 66 6e 63 74 5f 42 64 50 6f  ..     fnct_BdPo
8f620 6c 79 46 72 6f 6d 54 65 78 74 32 2c 20 30 2c 20  lyFromText2, 0, 
8f630 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8f640 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8f650 28 64 62 2c 20 22 42 64 4d 50 6f 6c 79 46 72 6f  (db, "BdMPolyFro
8f660 6d 54 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54  mText", 1, SQLIT
8f670 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8f680 20 20 66 6e 63 74 5f 42 64 4d 50 6f 6c 79 46 72    fnct_BdMPolyFr
8f690 6f 6d 54 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a  omText1, 0, 0);.
8f6a0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8f6b0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8f6c0 20 22 42 64 4d 50 6f 6c 79 46 72 6f 6d 54 65 78   "BdMPolyFromTex
8f6d0 74 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e  t", 2, SQLITE_AN
8f6e0 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8f6f0 63 74 5f 42 64 4d 50 6f 6c 79 46 72 6f 6d 54 65  ct_BdMPolyFromTe
8f700 78 74 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xt2, 0, 0);.    
8f710 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8f720 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 42 64  unction (db, "Bd
8f730 50 6f 6c 79 46 72 6f 6d 57 4b 42 22 2c 20 31 2c  PolyFromWKB", 1,
8f740 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8f750 09 09 09 20 20 20 20 20 66 6e 63 74 5f 42 64 50  ...     fnct_BdP
8f760 6f 6c 79 46 72 6f 6d 57 4b 42 31 2c 20 30 2c 20  olyFromWKB1, 0, 
8f770 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
8f780 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
8f790 28 64 62 2c 20 22 42 64 50 6f 6c 79 46 72 6f 6d  (db, "BdPolyFrom
8f7a0 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  WKB", 2, SQLITE_
8f7b0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8f7c0 66 6e 63 74 5f 42 64 50 6f 6c 79 46 72 6f 6d 57  fnct_BdPolyFromW
8f7d0 4b 42 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  KB2, 0, 0);.    
8f7e0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8f7f0 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 42 64  unction (db, "Bd
8f800 4d 50 6f 6c 79 46 72 6f 6d 57 4b 42 22 2c 20 31  MPolyFromWKB", 1
8f810 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8f820 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 42 64  ....     fnct_Bd
8f830 4d 50 6f 6c 79 46 72 6f 6d 57 4b 42 31 2c 20 30  MPolyFromWKB1, 0
8f840 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8f850 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8f860 6e 20 28 64 62 2c 20 22 42 64 4d 50 6f 6c 79 46  n (db, "BdMPolyF
8f870 72 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49  romWKB", 2, SQLI
8f880 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8f890 20 20 20 66 6e 63 74 5f 42 64 4d 50 6f 6c 79 46     fnct_BdMPolyF
8f8a0 72 6f 6d 57 4b 42 32 2c 20 30 2c 20 30 29 3b 0a  romWKB2, 0, 0);.
8f8b0 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
8f8c0 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
8f8d0 20 22 53 54 5f 42 64 50 6f 6c 79 46 72 6f 6d 54   "ST_BdPolyFromT
8f8e0 65 78 74 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  ext", 1, SQLITE_
8f8f0 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20  ANY, 0,....     
8f900 66 6e 63 74 5f 42 64 50 6f 6c 79 46 72 6f 6d 54  fnct_BdPolyFromT
8f910 65 78 74 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ext1, 0, 0);.   
8f920 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
8f930 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
8f940 54 5f 42 64 50 6f 6c 79 46 72 6f 6d 54 65 78 74  T_BdPolyFromText
8f950 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
8f960 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
8f970 74 5f 42 64 50 6f 6c 79 46 72 6f 6d 54 65 78 74  t_BdPolyFromText
8f980 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  2, 0, 0);.    sq
8f990 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
8f9a0 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 42  ction (db, "ST_B
8f9b0 64 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74 22 2c  dMPolyFromText",
8f9c0 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   1, SQLITE_ANY, 
8f9d0 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
8f9e0 42 64 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74 31  BdMPolyFromText1
8f9f0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8fa00 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8fa10 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 42 64  tion (db, "ST_Bd
8fa20 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74 22 2c 20  MPolyFromText", 
8fa30 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
8fa40 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 42  ,....     fnct_B
8fa50 64 4d 50 6f 6c 79 46 72 6f 6d 54 65 78 74 32 2c  dMPolyFromText2,
8fa60 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8fa70 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8fa80 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 42 64 50  ion (db, "ST_BdP
8fa90 6f 6c 79 46 72 6f 6d 57 4b 42 22 2c 20 31 2c 20  olyFromWKB", 1, 
8faa0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8fab0 09 09 20 20 20 20 20 66 6e 63 74 5f 42 64 50 6f  ..     fnct_BdPo
8fac0 6c 79 46 72 6f 6d 57 4b 42 31 2c 20 30 2c 20 30  lyFromWKB1, 0, 0
8fad0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
8fae0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
8faf0 64 62 2c 20 22 53 54 5f 42 64 50 6f 6c 79 46 72  db, "ST_BdPolyFr
8fb00 6f 6d 57 4b 42 22 2c 20 32 2c 20 53 51 4c 49 54  omWKB", 2, SQLIT
8fb10 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20  E_ANY, 0,....   
8fb20 20 20 66 6e 63 74 5f 42 64 50 6f 6c 79 46 72 6f    fnct_BdPolyFro
8fb30 6d 57 4b 42 32 2c 20 30 2c 20 30 29 3b 0a 20 20  mWKB2, 0, 0);.  
8fb40 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8fb50 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8fb60 53 54 5f 42 64 4d 50 6f 6c 79 46 72 6f 6d 57 4b  ST_BdMPolyFromWK
8fb70 42 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f 41 4e  B", 1, SQLITE_AN
8fb80 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8fb90 63 74 5f 42 64 4d 50 6f 6c 79 46 72 6f 6d 57 4b  ct_BdMPolyFromWK
8fba0 42 31 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  B1, 0, 0);.    s
8fbb0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
8fbc0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
8fbd0 42 64 4d 50 6f 6c 79 46 72 6f 6d 57 4b 42 22 2c  BdMPolyFromWKB",
8fbe0 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
8fbf0 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
8fc00 42 64 4d 50 6f 6c 79 46 72 6f 6d 57 4b 42 32 2c  BdMPolyFromWKB2,
8fc10 20 30 2c 20 30 29 3b 0a 0a 23 69 66 64 65 66 20   0, 0);..#ifdef 
8fc20 47 45 4f 53 5f 41 44 56 41 4e 43 45 44 09 09 2f  GEOS_ADVANCED../
8fc30 2a 20 47 45 4f 53 20 61 64 76 61 6e 63 65 64 20  * GEOS advanced 
8fc40 61 6e 64 20 65 78 70 65 72 69 6d 65 6e 74 61 6c  and experimental
8fc50 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 20 20   features */..  
8fc60 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
8fc70 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
8fc80 4f 66 66 73 65 74 43 75 72 76 65 22 2c 20 33 2c  OffsetCurve", 3,
8fc90 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8fca0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4f 66 66  ...     fnct_Off
8fcb0 73 65 74 43 75 72 76 65 2c 20 30 2c 20 30 29 3b  setCurve, 0, 0);
8fcc0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
8fcd0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
8fce0 2c 20 22 53 54 5f 4f 66 66 73 65 74 43 75 72 76  , "ST_OffsetCurv
8fcf0 65 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e  e", 3, SQLITE_AN
8fd00 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
8fd10 63 74 5f 4f 66 66 73 65 74 43 75 72 76 65 2c 20  ct_OffsetCurve, 
8fd20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
8fd30 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8fd40 6f 6e 20 28 64 62 2c 20 22 53 69 6e 67 6c 65 53  on (db, "SingleS
8fd50 69 64 65 64 42 75 66 66 65 72 22 2c 20 33 2c 20  idedBuffer", 3, 
8fd60 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
8fd70 09 09 20 20 20 20 20 66 6e 63 74 5f 53 69 6e 67  ..     fnct_Sing
8fd80 6c 65 53 69 64 65 64 42 75 66 66 65 72 2c 20 30  leSidedBuffer, 0
8fd90 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
8fda0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
8fdb0 6e 20 28 64 62 2c 20 22 53 54 5f 53 69 6e 67 6c  n (db, "ST_Singl
8fdc0 65 53 69 64 65 64 42 75 66 66 65 72 22 2c 20 33  eSidedBuffer", 3
8fdd0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8fde0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53 69  ....     fnct_Si
8fdf0 6e 67 6c 65 53 69 64 65 64 42 75 66 66 65 72 2c  ngleSidedBuffer,
8fe00 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
8fe10 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
8fe20 69 6f 6e 20 28 64 62 2c 20 22 48 61 75 73 64 6f  ion (db, "Hausdo
8fe30 72 66 66 44 69 73 74 61 6e 63 65 22 2c 20 32 2c  rffDistance", 2,
8fe40 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
8fe50 09 09 09 20 20 20 20 20 66 6e 63 74 5f 48 61 75  ...     fnct_Hau
8fe60 73 64 6f 72 66 66 44 69 73 74 61 6e 63 65 2c 20  sdorffDistance, 
8fe70 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
8fe80 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
8fe90 6f 6e 20 28 64 62 2c 20 22 53 54 5f 48 61 75 73  on (db, "ST_Haus
8fea0 64 6f 72 66 66 44 69 73 74 61 6e 63 65 22 2c 20  dorffDistance", 
8feb0 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30  2, SQLITE_ANY, 0
8fec0 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 48  ,....     fnct_H
8fed0 61 75 73 64 6f 72 66 66 44 69 73 74 61 6e 63 65  ausdorffDistance
8fee0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
8fef0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
8ff00 74 69 6f 6e 20 28 64 62 2c 20 22 53 68 61 72 65  tion (db, "Share
8ff10 64 50 61 74 68 73 22 2c 20 32 2c 20 53 51 4c 49  dPaths", 2, SQLI
8ff20 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
8ff30 20 20 20 66 6e 63 74 5f 53 68 61 72 65 64 50 61     fnct_SharedPa
8ff40 74 68 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ths, 0, 0);.    
8ff50 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
8ff60 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
8ff70 5f 53 68 61 72 65 64 50 61 74 68 73 22 2c 20 32  _SharedPaths", 2
8ff80 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
8ff90 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 53 68  ....     fnct_Sh
8ffa0 61 72 65 64 50 61 74 68 73 2c 20 30 2c 20 30 29  aredPaths, 0, 0)
8ffb0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72  ;.    sqlite3_cr
8ffc0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64  eate_function (d
8ffd0 62 2c 20 22 43 6f 76 65 72 73 22 2c 20 32 2c 20  b, "Covers", 2, 
8ffe0 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
8fff0 6e 63 74 5f 43 6f 76 65 72 73 2c 20 30 2c 20 30  nct_Covers, 0, 0
90000 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  );.    sqlite3_c
90010 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28  reate_function (
90020 64 62 2c 20 22 53 54 5f 43 6f 76 65 72 73 22 2c  db, "ST_Covers",
90030 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   2, SQLITE_ANY, 
90040 30 2c 20 66 6e 63 74 5f 43 6f 76 65 72 73 2c 20  0, fnct_Covers, 
90050 30 2c 0a 09 09 09 20 20 20 20 20 30 29 3b 0a 20  0,....     0);. 
90060 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74     sqlite3_creat
90070 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20  e_function (db, 
90080 22 43 6f 76 65 72 65 64 42 79 22 2c 20 32 2c 20  "CoveredBy", 2, 
90090 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 20 66  SQLITE_ANY, 0, f
900a0 6e 63 74 5f 43 6f 76 65 72 65 64 42 79 2c 0a 09  nct_CoveredBy,..
900b0 09 09 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20  ..     0, 0);.  
900c0 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
900d0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
900e0 53 54 5f 43 6f 76 65 72 65 64 42 79 22 2c 20 32  ST_CoveredBy", 2
900f0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
90100 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 43 6f  ....     fnct_Co
90110 76 65 72 65 64 42 79 2c 20 30 2c 20 30 29 3b 0a  veredBy, 0, 0);.
90120 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61      sqlite3_crea
90130 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c  te_function (db,
90140 20 22 4c 69 6e 65 5f 49 6e 74 65 72 70 6f 6c 61   "Line_Interpola
90150 74 65 5f 50 6f 69 6e 74 22 2c 20 32 2c 20 53 51  te_Point", 2, SQ
90160 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
90170 20 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 49 6e       fnct_LineIn
90180 74 65 72 70 6f 6c 61 74 65 50 6f 69 6e 74 2c 20  terpolatePoint, 
90190 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
901a0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
901b0 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4c 69 6e 65  on (db, "ST_Line
901c0 5f 49 6e 74 65 72 70 6f 6c 61 74 65 5f 50 6f 69  _Interpolate_Poi
901d0 6e 74 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  nt", 2, SQLITE_A
901e0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
901f0 6e 63 74 5f 4c 69 6e 65 49 6e 74 65 72 70 6f 6c  nct_LineInterpol
90200 61 74 65 50 6f 69 6e 74 2c 20 30 2c 20 30 29 3b  atePoint, 0, 0);
90210 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
90220 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
90230 2c 20 22 4c 69 6e 65 5f 49 6e 74 65 72 70 6f 6c  , "Line_Interpol
90240 61 74 65 5f 45 71 75 69 64 69 73 74 61 6e 74 5f  ate_Equidistant_
90250 50 6f 69 6e 74 73 22 2c 20 32 2c 0a 09 09 09 20  Points", 2,.... 
90260 20 20 20 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20      SQLITE_ANY, 
90270 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f  0,....     fnct_
90280 4c 69 6e 65 49 6e 74 65 72 70 6f 6c 61 74 65 45  LineInterpolateE
90290 71 75 69 64 69 73 74 61 6e 74 50 6f 69 6e 74 73  quidistantPoints
902a0 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
902b0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
902c0 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4c 69  tion (db, "ST_Li
902d0 6e 65 5f 49 6e 74 65 72 70 6f 6c 61 74 65 5f 45  ne_Interpolate_E
902e0 71 75 69 64 69 73 74 61 6e 74 5f 50 6f 69 6e 74  quidistant_Point
902f0 73 22 2c 20 32 2c 0a 09 09 09 20 20 20 20 20 53  s", 2,....     S
90300 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
90310 09 20 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 49  .     fnct_LineI
90320 6e 74 65 72 70 6f 6c 61 74 65 45 71 75 69 64 69  nterpolateEquidi
90330 73 74 61 6e 74 50 6f 69 6e 74 73 2c 20 30 2c 20  stantPoints, 0, 
90340 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
90350 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
90360 28 64 62 2c 20 22 4c 69 6e 65 5f 4c 6f 63 61 74  (db, "Line_Locat
90370 65 5f 50 6f 69 6e 74 22 2c 20 32 2c 20 53 51 4c  e_Point", 2, SQL
90380 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
90390 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 4c 6f 63      fnct_LineLoc
903a0 61 74 65 50 6f 69 6e 74 2c 20 30 2c 20 30 29 3b  atePoint, 0, 0);
903b0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
903c0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
903d0 2c 20 22 53 54 5f 4c 69 6e 65 5f 4c 6f 63 61 74  , "ST_Line_Locat
903e0 65 5f 50 6f 69 6e 74 22 2c 20 32 2c 20 53 51 4c  e_Point", 2, SQL
903f0 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
90400 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 4c 6f 63      fnct_LineLoc
90410 61 74 65 50 6f 69 6e 74 2c 20 30 2c 20 30 29 3b  atePoint, 0, 0);
90420 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
90430 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
90440 2c 20 22 4c 69 6e 65 5f 53 75 62 73 74 72 69 6e  , "Line_Substrin
90450 67 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e  g", 3, SQLITE_AN
90460 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e  Y, 0,....     fn
90470 63 74 5f 4c 69 6e 65 53 75 62 73 74 72 69 6e 67  ct_LineSubstring
90480 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
90490 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
904a0 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4c 69  tion (db, "ST_Li
904b0 6e 65 5f 53 75 62 73 74 72 69 6e 67 22 2c 20 33  ne_Substring", 3
904c0 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
904d0 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 4c 69  ....     fnct_Li
904e0 6e 65 53 75 62 73 74 72 69 6e 67 2c 20 30 2c 20  neSubstring, 0, 
904f0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
90500 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20  create_function 
90510 28 64 62 2c 20 22 43 6c 6f 73 65 73 74 50 6f 69  (db, "ClosestPoi
90520 6e 74 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  nt", 2, SQLITE_A
90530 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
90540 6e 63 74 5f 43 6c 6f 73 65 73 74 50 6f 69 6e 74  nct_ClosestPoint
90550 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
90560 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
90570 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 43 6c  tion (db, "ST_Cl
90580 6f 73 65 73 74 50 6f 69 6e 74 22 2c 20 32 2c 20  osestPoint", 2, 
90590 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09  SQLITE_ANY, 0,..
905a0 09 09 20 20 20 20 20 66 6e 63 74 5f 43 6c 6f 73  ..     fnct_Clos
905b0 65 73 74 50 6f 69 6e 74 2c 20 30 2c 20 30 29 3b  estPoint, 0, 0);
905c0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
905d0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
905e0 2c 20 22 53 68 6f 72 74 65 73 74 4c 69 6e 65 22  , "ShortestLine"
905f0 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
90600 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63 74   0,....     fnct
90610 5f 53 68 6f 72 74 65 73 74 4c 69 6e 65 2c 20 30  _ShortestLine, 0
90620 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
90630 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
90640 6e 20 28 64 62 2c 20 22 53 54 5f 53 68 6f 72 74  n (db, "ST_Short
90650 65 73 74 4c 69 6e 65 22 2c 20 32 2c 20 53 51 4c  estLine", 2, SQL
90660 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20  ITE_ANY, 0,.... 
90670 20 20 20 20 66 6e 63 74 5f 53 68 6f 72 74 65 73      fnct_Shortes
90680 74 4c 69 6e 65 2c 20 30 2c 20 30 29 3b 0a 20 20  tLine, 0, 0);.  
90690 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
906a0 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22  _function (db, "
906b0 53 6e 61 70 22 2c 20 33 2c 20 53 51 4c 49 54 45  Snap", 3, SQLITE
906c0 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 53 6e  _ANY, 0, fnct_Sn
906d0 61 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ap, 0, 0);.    s
906e0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
906f0 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f  nction (db, "ST_
90700 53 6e 61 70 22 2c 20 33 2c 20 53 51 4c 49 54 45  Snap", 3, SQLITE
90710 5f 41 4e 59 2c 20 30 2c 20 66 6e 63 74 5f 53 6e  _ANY, 0, fnct_Sn
90720 61 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ap, 0, 0);.    s
90730 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
90740 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 4c 69 6e  nction (db, "Lin
90750 65 4d 65 72 67 65 22 2c 20 31 2c 20 53 51 4c 49  eMerge", 1, SQLI
90760 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09 20 20  TE_ANY, 0,....  
90770 20 20 20 66 6e 63 74 5f 4c 69 6e 65 4d 65 72 67     fnct_LineMerg
90780 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  e, 0, 0);.    sq
90790 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
907a0 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54 5f 4c  ction (db, "ST_L
907b0 69 6e 65 4d 65 72 67 65 22 2c 20 31 2c 20 53 51  ineMerge", 1, SQ
907c0 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
907d0 20 20 20 20 20 66 6e 63 74 5f 4c 69 6e 65 4d 65       fnct_LineMe
907e0 72 67 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  rge, 0, 0);.    
907f0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
90800 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 55 6e  unction (db, "Un
90810 61 72 79 55 6e 69 6f 6e 22 2c 20 31 2c 20 53 51  aryUnion", 1, SQ
90820 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09 09  LITE_ANY, 0,....
90830 20 20 20 20 20 66 6e 63 74 5f 55 6e 61 72 79 55       fnct_UnaryU
90840 6e 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20  nion, 0, 0);.   
90850 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
90860 66 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53  function (db, "S
90870 54 5f 55 6e 61 72 79 55 6e 69 6f 6e 22 2c 20 31  T_UnaryUnion", 1
90880 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c  , SQLITE_ANY, 0,
90890 0a 09 09 09 20 20 20 20 20 66 6e 63 74 5f 55 6e  ....     fnct_Un
908a0 61 72 79 55 6e 69 6f 6e 2c 20 30 2c 20 30 29 3b  aryUnion, 0, 0);
908b0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 72 65  .    sqlite3_cre
908c0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 28 64 62  ate_function (db
908d0 2c 20 22 4c 69 6e 65 73 43 75 74 41 74 4e 6f 64  , "LinesCutAtNod
908e0 65 73 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41  es", 2, SQLITE_A
908f0 4e 59 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66  NY, 0,....     f
90900 6e 63 74 5f 4c 69 6e 65 73 43 75 74 41 74 4e 6f  nct_LinesCutAtNo
90910 64 65 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  des, 0, 0);.    
90920 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
90930 75 6e 63 74 69 6f 6e 20 28 64 62 2c 20 22 53 54  unction (db, "ST
90940 5f 4c 69 6e 65 73 43 75 74 41 74 4e 6f 64 65 73  _LinesCutAtNodes
90950 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
90960 2c 20 30 2c 0a 09 09 09 20 20 20 20 20 66 6e 63  , 0,....     fnc
90970 74 5f 4c 69 6e 65 73 43 75 74 41 74 4e 6f 64 65  t_LinesCutAtNode
90980 73 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  s, 0, 0);.    sq
90990 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
909a0 63 74 69 6f 6e 20 28 64 62 2c 20 22 52 69 6e 67  ction (db, "Ring
909b0 73 43 75 74 41 74 4e 6f 64 65 73 22 2c 20 31 2c  sCutAtNodes", 1,
909c0 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a   SQLITE_ANY, 0,.
909d0 09 09 09 20 20 20 20 20 66 6e 63 74 5f 52 69 6e  ...     fnct_Rin
909e0 67 73 43 75 74 41 74 4e 6f 64 65 73 2c 20 30 2c  gsCutAtNodes, 0,
909f0 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
90a00 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
90a10 20 28 64 62 2c 20 22 53 54 5f 52 69 6e 67 73 43   (db, "ST_RingsC
90a20 75 74 41 74 4e 6f 64 65 73 22 2c 20 31 2c 20 53  utAtNodes", 1, S
90a30 51 4c 49 54 45 5f 41 4e 59 2c 20 30 2c 0a 09 09  QLITE_ANY, 0,...
90a40 09 20 20 20 20 20 66 6e 63 74 5f 52 69 6e 67 73  .     fnct_Rings
90a50 43 75 74 41 74 4e 6f 64 65 73 2c 20 30 2c 20 30  CutAtNodes, 0, 0
90a60 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 65 6e  );..#endif /* en
90a70 64 20 47 45 4f 53 20 61 64 76 61 6e 63 65 64 20  d GEOS advanced 
90a80 61 6e 64 20 65 78 70 65 72 69 6d 65 6e 74 61 6c  and experimental
90a90 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 23 65   features */..#e
90aa0 6e 64 69 66 20 2f 2a 20 65 6e 64 20 69 6e 63 6c  ndif /* end incl
90ab0 75 64 69 6e 67 20 47 45 4f 53 20 2a 2f 0a 7d 0a  uding GEOS */.}.
90ac0 0a 73 74 61 74 69 63 20 76 6f 69 64 0a 69 6e 69  .static void.ini
90ad0 74 5f 73 70 61 74 69 61 6c 69 74 65 5f 76 69 72  t_spatialite_vir
90ae0 74 75 61 6c 74 61 62 6c 65 73 20 28 73 71 6c 69  tualtables (sqli
90af0 74 65 33 20 2a 20 64 62 29 0a 7b 0a 23 69 66 6e  te3 * db).{.#ifn
90b00 64 65 66 20 4f 4d 49 54 5f 49 43 4f 4e 56 09 09  def OMIT_ICONV..
90b10 2f 2a 20 77 68 65 6e 20 49 43 4f 4e 56 20 69 73  /* when ICONV is
90b20 20 64 69 73 61 62 6c 65 64 20 53 48 50 2f 44 42   disabled SHP/DB
90b30 46 2f 54 58 54 20 63 61 6e 6e 6f 74 20 62 65 20  F/TXT cannot be 
90b40 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 2f 2a 20  supported */./* 
90b50 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
90b60 20 56 69 72 74 75 61 6c 53 68 61 70 65 20 20 65   VirtualShape  e
90b70 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  xtension */.    
90b80 76 69 72 74 75 61 6c 73 68 61 70 65 5f 65 78 74  virtualshape_ext
90b90 65 6e 73 69 6f 6e 5f 69 6e 69 74 20 28 64 62 29  ension_init (db)
90ba0 3b 0a 2f 2a 20 69 6e 69 74 69 61 6c 69 7a 69 6e  ;./* initializin
90bb0 67 20 74 68 65 20 56 69 72 74 75 61 6c 44 62 66  g the VirtualDbf
90bc0 20 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20    extension */. 
90bd0 20 20 20 76 69 72 74 75 61 6c 64 62 66 5f 65 78     virtualdbf_ex
90be0 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 20 28 64 62  tension_init (db
90bf0 29 3b 0a 2f 2a 20 69 6e 69 74 69 61 6c 69 7a 69  );./* initializi
90c00 6e 67 20 74 68 65 20 56 69 72 74 75 61 6c 54 65  ng the VirtualTe
90c10 78 74 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a  xt extension */.
90c20 20 20 20 20 76 69 72 74 75 61 6c 74 65 78 74 5f      virtualtext_
90c30 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 20 28  extension_init (
90c40 64 62 29 3b 0a 23 69 66 6e 64 65 66 20 4f 4d 49  db);.#ifndef OMI
90c50 54 5f 46 52 45 45 58 4c 0a 2f 2a 20 69 6e 69 74  T_FREEXL./* init
90c60 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 56 69 72  ializing the Vir
90c70 74 75 61 6c 58 4c 20 20 65 78 74 65 6e 73 69 6f  tualXL  extensio
90c80 6e 20 2a 2f 0a 20 20 20 20 76 69 72 74 75 61 6c  n */.    virtual
90c90 58 4c 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  XL_extension_ini
90ca0 74 20 28 64 62 29 3b 0a 23 65 6e 64 69 66 20 2f  t (db);.#endif /
90cb0 2a 20 46 72 65 65 58 4c 20 65 6e 61 62 6c 65 64  * FreeXL enabled
90cc0 2f 64 69 73 61 62 6c 65 20 2a 2f 0a 23 65 6e 64  /disable */.#end
90cd0 69 66 20 2f 2a 20 49 43 4f 4e 56 20 65 6e 61 62  if /* ICONV enab
90ce0 6c 65 64 2f 64 69 73 61 62 6c 65 64 20 2a 2f 0a  led/disabled */.
90cf0 0a 2f 2a 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  ./* initializing
90d00 20 74 68 65 20 56 69 72 74 75 61 6c 4e 65 74 77   the VirtualNetw
90d10 6f 72 6b 20 20 65 78 74 65 6e 73 69 6f 6e 20 2a  ork  extension *
90d20 2f 0a 20 20 20 20 76 69 72 74 75 61 6c 6e 65 74  /.    virtualnet
90d30 77 6f 72 6b 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  work_extension_i
90d40 6e 69 74 20 28 64 62 29 3b 0a 2f 2a 20 69 6e 69  nit (db);./* ini
90d50 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 4d 62  tializing the Mb
90d60 72 43 61 63 68 65 20 20 65 78 74 65 6e 73 69 6f  rCache  extensio
90d70 6e 20 2a 2f 0a 20 20 20 20 6d 62 72 63 61 63 68  n */.    mbrcach
90d80 65 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  e_extension_init
90d90 20 28 64 62 29 3b 0a 2f 2a 20 69 6e 69 74 69 61   (db);./* initia
90da0 6c 69 7a 69 6e 67 20 74 68 65 20 56 69 72 74 75  lizing the Virtu
90db0 61 6c 46 44 4f 20 20 65 78 74 65 6e 73 69 6f 6e  alFDO  extension
90dc0 20 2a 2f 0a 20 20 20 20 76 69 72 74 75 61 6c 66   */.    virtualf
90dd0 64 6f 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  do_extension_ini
90de0 74 20 28 64 62 29 3b 0a 2f 2a 20 69 6e 69 74 69  t (db);./* initi
90df0 61 6c 69 7a 69 6e 67 20 74 68 65 20 56 69 72 74  alizing the Virt
90e00 75 61 6c 53 70 61 74 69 61 6c 49 6e 64 65 78 20  ualSpatialIndex 
90e10 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20   extension */.  
90e20 20 20 76 69 72 74 75 61 6c 5f 73 70 61 74 69 61    virtual_spatia
90e30 6c 69 6e 64 65 78 5f 65 78 74 65 6e 73 69 6f 6e  lindex_extension
90e40 5f 69 6e 69 74 20 28 64 62 29 3b 0a 7d 0a 0a 73  _init (db);.}..s
90e50 74 61 74 69 63 20 69 6e 74 0a 69 6e 69 74 5f 73  tatic int.init_s
90e60 70 61 74 69 61 6c 69 74 65 5f 65 78 74 65 6e 73  patialite_extens
90e70 69 6f 6e 20 28 73 71 6c 69 74 65 33 20 2a 20 64  ion (sqlite3 * d
90e80 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  b, char **pzErrM
90e90 73 67 2c 0a 09 09 09 20 20 20 63 6f 6e 73 74 20  sg,....   const 
90ea0 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
90eb0 69 6e 65 73 20 2a 20 70 41 70 69 29 0a 7b 0a 20  ines * pApi).{. 
90ec0 20 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53     SQLITE_EXTENS
90ed0 49 4f 4e 5f 49 4e 49 54 32 20 28 70 41 70 69 29  ION_INIT2 (pApi)
90ee0 3b 0a 2f 2a 20 73 65 74 74 69 6e 67 20 74 68 65  ;./* setting the
90ef0 20 50 4f 53 49 58 20 6c 6f 63 61 6c 65 20 66 6f   POSIX locale fo
90f00 72 20 6e 75 6d 65 72 69 63 20 2a 2f 0a 20 20 20  r numeric */.   
90f10 20 73 65 74 6c 6f 63 61 6c 65 20 28 4c 43 5f 4e   setlocale (LC_N
90f20 55 4d 45 52 49 43 2c 20 22 50 4f 53 49 58 22 29  UMERIC, "POSIX")
90f30 3b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20  ;.    *pzErrMsg 
90f40 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20 72 65 67  = NULL;..    reg
90f50 69 73 74 65 72 5f 73 70 61 74 69 61 6c 69 74 65  ister_spatialite
90f60 5f 73 71 6c 5f 66 75 6e 63 74 69 6f 6e 73 20 28  _sql_functions (
90f70 64 62 29 3b 0a 0a 20 20 20 20 69 6e 69 74 5f 73  db);..    init_s
90f80 70 61 74 69 61 6c 69 74 65 5f 76 69 72 74 75 61  patialite_virtua
90f90 6c 74 61 62 6c 65 73 20 28 64 62 29 3b 0a 0a 2f  ltables (db);../
90fa0 2a 20 73 65 74 74 69 6e 67 20 61 20 74 69 6d 65  * setting a time
90fb0 6f 75 74 20 68 61 6e 64 6c 65 72 20 2a 2f 0a 20  out handler */. 
90fc0 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f     sqlite3_busy_
90fd0 74 69 6d 65 6f 75 74 20 28 64 62 2c 20 35 30 30  timeout (db, 500
90fe0 30 29 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20  0);..    return 
90ff0 30 3b 0a 7d 0a 0a 53 50 41 54 49 41 4c 49 54 45  0;.}..SPATIALITE
91000 5f 44 45 43 4c 41 52 45 20 76 6f 69 64 0a 73 70  _DECLARE void.sp
91010 61 74 69 61 6c 69 74 65 5f 69 6e 69 74 20 28 69  atialite_init (i
91020 6e 74 20 76 65 72 62 6f 73 65 29 0a 7b 0a 2f 2a  nt verbose).{./*
91030 20 75 73 65 64 20 77 68 65 6e 20 53 51 4c 69 74   used when SQLit
91040 65 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 53 70  e initializes Sp
91050 61 74 69 61 4c 69 74 65 20 76 69 61 20 73 74 61  atiaLite via sta
91060 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 6c  tically linked l
91070 69 62 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4f  ib */..#ifndef O
91080 4d 49 54 5f 47 45 4f 53 09 09 2f 2a 20 69 6e 69  MIT_GEOS../* ini
91090 74 69 61 6c 69 7a 69 6e 67 20 47 45 4f 53 20 2a  tializing GEOS *
910a0 2f 0a 20 20 20 20 69 6e 69 74 47 45 4f 53 20 28  /.    initGEOS (
910b0 67 65 6f 73 5f 77 61 72 6e 69 6e 67 2c 20 67 65  geos_warning, ge
910c0 6f 73 5f 65 72 72 6f 72 29 3b 0a 23 65 6e 64 69  os_error);.#endi
910d0 66 20 2f 2a 20 65 6e 64 20 47 45 4f 53 20 20 2a  f /* end GEOS  *
910e0 2f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 61  /..    sqlite3_a
910f0 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 20 28 28  uto_extension ((
91100 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 29 20  void (*)(void)) 
91110 69 6e 69 74 5f 73 70 61 74 69 61 6c 69 74 65 5f  init_spatialite_
91120 65 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 20 20  extension);.    
91130 69 66 20 28 69 73 61 74 74 79 20 28 31 29 29 0a  if (isatty (1)).
91140 20 20 20 20 20 20 7b 0a 09 20 20 2f 2a 20 70 72        {..  /* pr
91150 69 6e 74 69 6e 67 20 22 68 65 6c 6c 6f 22 20 6d  inting "hello" m
91160 65 73 73 61 67 65 20 6f 6e 6c 79 20 77 68 65 6e  essage only when
91170 20 73 74 64 6f 75 74 20 69 73 20 6f 6e 20 63 6f   stdout is on co
91180 6e 73 6f 6c 65 20 2a 2f 0a 09 20 20 69 66 20 28  nsole */..  if (
91190 76 65 72 62 6f 73 65 29 0a 09 20 20 20 20 7b 0a  verbose)..    {.
911a0 09 09 73 70 61 74 69 61 6c 69 74 65 5f 69 20 28  ..spatialite_i (
911b0 22 53 70 61 74 69 61 4c 69 74 65 20 76 65 72 73  "SpatiaLite vers
911c0 69 6f 6e 20 2e 2e 3a 20 25 73 22 2c 0a 09 09 09  ion ..: %s",....
911d0 20 20 20 20 20 20 73 70 61 74 69 61 6c 69 74 65        spatialite
911e0 5f 76 65 72 73 69 6f 6e 20 28 29 29 3b 0a 09 09  _version ());...
911f0 73 70 61 74 69 61 6c 69 74 65 5f 69 20 28 22 5c  spatialite_i ("\
91200 74 53 75 70 70 6f 72 74 65 64 20 45 78 74 65 6e  tSupported Exten
91210 73 69 6f 6e 73 3a 5c 6e 22 29 3b 0a 23 69 66 6e  sions:\n");.#ifn
91220 64 65 66 20 4f 4d 49 54 5f 49 43 4f 4e 56 09 09  def OMIT_ICONV..
91230 2f 2a 20 49 43 4f 4e 56 20 69 73 20 72 65 71 75  /* ICONV is requ
91240 69 72 65 64 20 62 79 20 53 48 50 2f 44 42 46 2f  ired by SHP/DBF/
91250 54 58 54 20 2a 2f 0a 09 09 73 70 61 74 69 61 6c  TXT */...spatial
91260 69 74 65 5f 69 0a 09 09 20 20 20 20 28 22 5c 74  ite_i...    ("\t
91270 2d 20 27 56 69 72 74 75 61 6c 53 68 61 70 65 27  - 'VirtualShape'
91280 5c 74 5b 64 69 72 65 63 74 20 53 68 61 70 65 66  \t[direct Shapef
91290 69 6c 65 20 61 63 63 65 73 73 5d 5c 6e 22 29 3b  ile access]\n");
912a0 0a 09 09 73 70 61 74 69 61 6c 69 74 65 5f 69 20  ...spatialite_i 
912b0 28 22 5c 74 2d 20 27 56 69 72 74 75 61 6c 44 62  ("\t- 'VirtualDb
912c0 66 27 5c 74 5c 74 5b 64 69 72 65 63 74 20 44 42  f'\t\t[direct DB
912d0 46 20 61 63 63 65 73 73 5d 5c 6e 22 29 3b 0a 23  F access]\n");.#
912e0 69 66 6e 64 65 66 20 4f 4d 49 54 5f 46 52 45 45  ifndef OMIT_FREE
912f0 58 4c 0a 09 09 73 70 61 74 69 61 6c 69 74 65 5f  XL...spatialite_
91300 69 20 28 22 5c 74 2d 20 27 56 69 72 74 75 61 6c  i ("\t- 'Virtual
91310 58 4c 27 5c 74 5c 74 5b 64 69 72 65 63 74 20 58  XL'\t\t[direct X
91320 4c 53 20 61 63 63 65 73 73 5d 5c 6e 22 29 3b 0a  LS access]\n");.
91330 23 65 6e 64 69 66 20 2f 2a 20 65 6e 64 20 46 72  #endif /* end Fr
91340 65 65 58 4c 20 63 6f 6e 64 69 74 69 6f 6e 61 6c  eeXL conditional
91350 20 2a 2f 0a 09 09 73 70 61 74 69 61 6c 69 74 65   */...spatialite
91360 5f 69 20 28 22 5c 74 2d 20 27 56 69 72 74 75 61  _i ("\t- 'Virtua
91370 6c 54 65 78 74 27 5c 74 5c 74 5b 64 69 72 65 63  lText'\t\t[direc
91380 74 20 43 53 56 2f 54 58 54 20 61 63 63 65 73 73  t CSV/TXT access
91390 5d 5c 6e 22 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  ]\n");.#endif /*
913a0 20 65 6e 64 20 49 43 4f 4e 56 20 63 6f 6e 64 69   end ICONV condi
913b0 74 69 6f 6e 61 6c 20 2a 2f 0a 09 09 73 70 61 74  tional */...spat
913c0 69 61 6c 69 74 65 5f 69 0a 09 09 20 20 20 20 28  ialite_i...    (
913d0 22 5c 74 2d 20 27 56 69 72 74 75 61 6c 4e 65 74  "\t- 'VirtualNet
913e0 77 6f 72 6b 27 5c 74 5b 44 69 6a 6b 73 74 72 61  work'\t[Dijkstra
913f0 20 73 68 6f 72 74 65 73 74 20 70 61 74 68 5d 5c   shortest path]\
91400 6e 22 29 3b 0a 09 09 73 70 61 74 69 61 6c 69 74  n");...spatialit
91410 65 5f 69 20 28 22 5c 74 2d 20 27 52 54 72 65 65  e_i ("\t- 'RTree
91420 27 5c 74 5c 74 5b 53 70 61 74 69 61 6c 20 49 6e  '\t\t[Spatial In
91430 64 65 78 20 2d 20 52 2a 54 72 65 65 5d 5c 6e 22  dex - R*Tree]\n"
91440 29 3b 0a 09 09 73 70 61 74 69 61 6c 69 74 65 5f  );...spatialite_
91450 69 0a 09 09 20 20 20 20 28 22 5c 74 2d 20 27 4d  i...    ("\t- 'M
91460 62 72 43 61 63 68 65 27 5c 74 5c 74 5b 53 70 61  brCache'\t\t[Spa
91470 74 69 61 6c 20 49 6e 64 65 78 20 2d 20 4d 42 52  tial Index - MBR
91480 20 63 61 63 68 65 5d 5c 6e 22 29 3b 0a 09 09 73   cache]\n");...s
91490 70 61 74 69 61 6c 69 74 65 5f 69 0a 09 09 20 20  patialite_i...  
914a0 20 20 28 22 5c 74 2d 20 27 56 69 72 74 75 61 6c    ("\t- 'Virtual
914b0 53 70 61 74 69 61 6c 49 6e 64 65 78 27 5c 74 5b  SpatialIndex'\t[
914c0 52 2a 54 72 65 65 20 6d 65 74 61 68 61 6e 64 6c  R*Tree metahandl
914d0 65 72 5d 5c 6e 22 29 3b 0a 09 09 73 70 61 74 69  er]\n");...spati
914e0 61 6c 69 74 65 5f 69 0a 09 09 20 20 20 20 28 22  alite_i...    ("
914f0 5c 74 2d 20 27 56 69 72 74 75 61 6c 46 44 4f 27  \t- 'VirtualFDO'
91500 5c 74 5c 74 5b 46 44 4f 2d 4f 47 52 20 69 6e 74  \t\t[FDO-OGR int
91510 65 72 6f 70 65 72 61 62 69 6c 69 74 79 5d 5c 6e  eroperability]\n
91520 22 29 3b 0a 09 09 73 70 61 74 69 61 6c 69 74 65  ");...spatialite
91530 5f 69 20 28 22 5c 74 2d 20 27 53 70 61 74 69 61  _i ("\t- 'Spatia
91540 4c 69 74 65 27 5c 74 5c 74 5b 53 70 61 74 69 61  Lite'\t\t[Spatia
91550 6c 20 53 51 4c 20 2d 20 4f 47 43 5d 5c 6e 22 29  l SQL - OGC]\n")
91560 3b 0a 09 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  ;..    }.#ifndef
91570 20 4f 4d 49 54 5f 50 52 4f 4a 09 09 2f 2a 20 50   OMIT_PROJ../* P
91580 52 4f 4a 2e 34 20 76 65 72 73 69 6f 6e 20 2a 2f  ROJ.4 version */
91590 0a 09 20 20 69 66 20 28 76 65 72 62 6f 73 65 29  ..  if (verbose)
915a0 0a 09 20 20 20 20 20 20 73 70 61 74 69 61 6c 69  ..      spatiali
915b0 74 65 5f 69 20 28 22 50 52 4f 4a 2e 34 20 76 65  te_i ("PROJ.4 ve
915c0 72 73 69 6f 6e 20 2e 2e 2e 2e 2e 2e 3a 20 25 73  rsion ......: %s
915d0 5c 6e 22 2c 20 70 6a 5f 67 65 74 5f 72 65 6c 65  \n", pj_get_rele
915e0 61 73 65 20 28 29 29 3b 0a 23 65 6e 64 69 66 20  ase ());.#endif 
915f0 2f 2a 20 65 6e 64 20 69 6e 63 6c 75 64 69 6e 67  /* end including
91600 20 50 52 4f 4a 2e 34 20 2a 2f 0a 23 69 66 6e 64   PROJ.4 */.#ifnd
91610 65 66 20 4f 4d 49 54 5f 47 45 4f 53 09 09 2f 2a  ef OMIT_GEOS../*
91620 20 47 45 4f 53 20 76 65 72 73 69 6f 6e 20 2a 2f   GEOS version */
91630 0a 09 20 20 69 66 20 28 76 65 72 62 6f 73 65 29  ..  if (verbose)
91640 0a 09 20 20 20 20 20 20 73 70 61 74 69 61 6c 69  ..      spatiali
91650 74 65 5f 69 20 28 22 47 45 4f 53 20 76 65 72 73  te_i ("GEOS vers
91660 69 6f 6e 20 2e 2e 2e 2e 2e 2e 2e 2e 3a 20 25 73  ion ........: %s
91670 5c 6e 22 2c 20 47 45 4f 53 76 65 72 73 69 6f 6e  \n", GEOSversion
91680 20 28 29 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   ());.#endif /* 
91690 65 6e 64 20 47 45 4f 53 20 76 65 72 73 69 6f 6e  end GEOS version
916a0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 7d 0a 0a 53   */.      }.}..S
916b0 50 41 54 49 41 4c 49 54 45 5f 44 45 43 4c 41 52  PATIALITE_DECLAR
916c0 45 20 76 6f 69 64 0a 73 70 61 74 69 61 6c 69 74  E void.spatialit
916d0 65 5f 63 6c 65 61 6e 75 70 20 28 29 0a 7b 0a 23  e_cleanup ().{.#
916e0 69 66 6e 64 65 66 20 4f 4d 49 54 5f 47 45 4f 53  ifndef OMIT_GEOS
916f0 0a 20 20 20 20 66 69 6e 69 73 68 47 45 4f 53 20  .    finishGEOS 
91700 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73  ();.#endif.    s
91710 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74  qlite3_reset_aut
91720 6f 5f 65 78 74 65 6e 73 69 6f 6e 20 28 29 3b 0a  o_extension ();.
91730 7d 0a 0a 23 69 66 20 21 28 64 65 66 69 6e 65 64  }..#if !(defined
91740 20 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69   _WIN32) || defi
91750 6e 65 64 28 5f 5f 4d 49 4e 47 57 5f 5f 29 0a 2f  ned(__MINGW__)./
91760 2a 20 4d 53 56 43 20 69 73 20 75 6e 61 62 6c 65  * MSVC is unable
91770 20 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 74   to understand t
91780 68 69 73 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  his declaration 
91790 2a 2f 0a 5f 5f 61 74 74 72 69 62 75 74 65 5f 5f  */.__attribute__
917a0 20 28 28 76 69 73 69 62 69 6c 69 74 79 20 28 22   ((visibility ("
917b0 64 65 66 61 75 6c 74 22 29 29 29 0a 23 65 6e 64  default"))).#end
917c0 69 66 0a 20 20 20 20 20 53 50 41 54 49 41 4c 49  if.     SPATIALI
917d0 54 45 5f 44 45 43 4c 41 52 45 20 69 6e 74 0a 09  TE_DECLARE int..
917e0 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69   sqlite3_extensi
917f0 6f 6e 5f 69 6e 69 74 20 28 73 71 6c 69 74 65 33  on_init (sqlite3
91800 20 2a 20 64 62 2c 20 63 68 61 72 20 2a 2a 70 7a   * db, char **pz
91810 45 72 72 4d 73 67 2c 0a 09 09 09 09 20 63 6f 6e  ErrMsg,..... con
91820 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
91830 6f 75 74 69 6e 65 73 20 2a 20 70 41 70 69 29 0a  outines * pApi).
91840 7b 0a 2f 2a 20 53 51 4c 69 74 65 20 69 6e 76 6f  {./* SQLite invo
91850 6b 65 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  kes this routine
91860 20 6f 6e 63 65 20 77 68 65 6e 20 69 74 20 64 79   once when it dy
91870 6e 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 73 20  namically loads 
91880 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 2a  the extension. *
91890 2f 0a 0a 23 69 66 6e 64 65 66 20 4f 4d 49 54 5f  /..#ifndef OMIT_
918a0 47 45 4f 53 09 09 2f 2a 20 69 6e 69 74 69 61 6c  GEOS../* initial
918b0 69 7a 69 6e 67 20 47 45 4f 53 20 2a 2f 0a 20 20  izing GEOS */.  
918c0 20 20 69 6e 69 74 47 45 4f 53 20 28 67 65 6f 73    initGEOS (geos
918d0 5f 77 61 72 6e 69 6e 67 2c 20 67 65 6f 73 5f 65  _warning, geos_e
918e0 72 72 6f 72 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  rror);.#endif /*
918f0 20 65 6e 64 20 47 45 4f 53 20 20 2a 2f 0a 0a 20   end GEOS  */.. 
91900 20 20 20 72 65 74 75 72 6e 20 69 6e 69 74 5f 73     return init_s
91910 70 61 74 69 61 6c 69 74 65 5f 65 78 74 65 6e 73  patialite_extens
91920 69 6f 6e 20 28 64 62 2c 20 70 7a 45 72 72 4d 73  ion (db, pzErrMs
91930 67 2c 20 70 41 70 69 29 3b 0a 7d 0a 0a 53 50 41  g, pApi);.}..SPA
91940 54 49 41 4c 49 54 45 5f 44 45 43 4c 41 52 45 20  TIALITE_DECLARE 
91950 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 0a 6d 61  sqlite3_int64.ma
91960 74 68 5f 6c 6c 61 62 73 20 28 73 71 6c 69 74 65  th_llabs (sqlite
91970 33 5f 69 6e 74 36 34 20 76 61 6c 75 65 29 0a 7b  3_int64 value).{
91980 0a 2f 2a 20 72 65 70 6c 61 63 69 6e 67 20 74 68  ./* replacing th
91990 65 20 43 39 39 20 6c 6c 61 62 73 28 29 20 66 75  e C99 llabs() fu
919a0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 72 65  nction */.    re
919b0 74 75 72 6e 20 76 61 6c 75 65 20 3c 20 30 20 3f  turn value < 0 ?
919c0 20 2d 76 61 6c 75 65 20 3a 20 76 61 6c 75 65 3b   -value : value;
919d0 0a 7d 0a 0a 53 50 41 54 49 41 4c 49 54 45 5f 44  .}..SPATIALITE_D
919e0 45 43 4c 41 52 45 20 64 6f 75 62 6c 65 0a 6d 61  ECLARE double.ma
919f0 74 68 5f 72 6f 75 6e 64 20 28 64 6f 75 62 6c 65  th_round (double
91a00 20 76 61 6c 75 65 29 0a 7b 0a 2f 2a 20 72 65 70   value).{./* rep
91a10 6c 61 63 69 6e 67 20 74 68 65 20 43 39 39 20 72  lacing the C99 r
91a20 6f 75 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e 20  ound() function 
91a30 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6d 69  */.    double mi
91a40 6e 20 3d 20 66 6c 6f 6f 72 20 28 76 61 6c 75 65  n = floor (value
91a50 29 3b 0a 20 20 20 20 69 66 20 28 66 61 62 73 20  );.    if (fabs 
91a60 28 76 61 6c 75 65 20 2d 20 6d 69 6e 29 20 3c 20  (value - min) < 
91a70 30 2e 35 29 0a 09 72 65 74 75 72 6e 20 6d 69 6e  0.5)..return min
91a80 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e  ;.    return min
91a90 20 2b 20 31 2e 30 3b 0a 7d 0a                     + 1.0;.}.