[gnome-maps/wip/mlundblad/transit-plugin-gtfs-local: 14/16] WIP: Add custom SQLite geo util functions



commit c1f3222c8acda82bf1bf1f5eb74ffd6a9a3981ef
Author: Marcus Lundblad <ml update uu se>
Date:   Sun Mar 15 23:26:45 2020 +0100

    WIP: Add custom SQLite geo util functions

 lib/maps-gtfs-db-funcs.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/maps-gtfs-db-funcs.h | 25 +++++++++++++++++++++
 lib/meson.build          |  6 ++++++
 3 files changed, 87 insertions(+)
---
diff --git a/lib/maps-gtfs-db-funcs.c b/lib/maps-gtfs-db-funcs.c
new file mode 100644
index 00000000..272834b0
--- /dev/null
+++ b/lib/maps-gtfs-db-funcs.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2020 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+#include "maps-gtfs-db-funcs.h"
+
+#include <glib.h>
+#include <math.h>
+
+#define EARTH_RADIUS_M 6372795
+
+static void
+distance_func (sqlite3_context *ctx, gint argc, sqlite3_value **argv)
+{
+  gdouble lat_a = sqlite3_value_double (argv[0]);
+  gdouble lon_a = sqlite3_value_double (argv[1]);
+  gdouble lat_b = sqlite3_value_double (argv[2]);
+  gdouble lon_b = sqlite3_value_double (argv[3]);
+
+  gdouble dlat, dlon, lat1, lat2;
+  gdouble a, c;
+
+  dlat = (lat_b - lat_a) * M_PI / 180.0;
+  dlon = (lon_b - lon_a) * M_PI / 180.0;
+  lat1 = lat_a * M_PI / 180.0;
+  lat2 = lat_b * M_PI / 180.0;
+
+  a = sin (dlat / 2) * sin (dlat / 2) +
+      sin (dlon / 2) * sin (dlon / 2) * cos (lat1) * cos (lat2);
+  c = 2 * atan2 (sqrt (a), sqrt (1-a));
+
+  sqlite3_result_double (ctx, EARTH_RADIUS_M * c);
+}
+
+void
+maps_gtfs_db_funcs_init (sqlite3 *db)
+{
+  sqlite3_create_function_v2 (db, "DISTANCE", 4,
+                              SQLITE_UTF8  | SQLITE_DETERMINISTIC,
+                              NULL, distance_func, NULL, NULL, NULL);
+}
diff --git a/lib/maps-gtfs-db-funcs.h b/lib/maps-gtfs-db-funcs.h
new file mode 100644
index 00000000..52b799c9
--- /dev/null
+++ b/lib/maps-gtfs-db-funcs.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2020 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+#pragma once
+
+#include <sqlite3.h>
+
+void maps_gtfs_db_funcs_init (sqlite3 *db);
+
diff --git a/lib/meson.build b/lib/meson.build
index ba8a918c..a78c49d3 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -22,6 +22,7 @@ sources = files(
        'maps-contact-store.c',
        'maps-file-tile-source.c',
        'maps-gtfs-agency.c',
+       'maps-gtfs-db-funcs.c',
        'maps-gtfs-route.c',
        'maps-gtfs-stop.c',
        'maps-gtfs-trip.c',
@@ -45,6 +46,11 @@ maps_enums = gnome.mkenums(
        symbol_prefix: maps_ns.to_lower()
 )
 
+libm = cc.find_library('m', required: false)
+if libm.found()
+    libmaps_deps += [ libm ]
+endif
+
 cflags = [
        '-DG_DISABLE_DEPRECATED',
        '-DPREFIX="@0@"'.format(prefix),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]