From 58dfbdcb6bee0b4151659aa177b1a45b55a83396 Mon Sep 17 00:00:00 2001
From: Oliver Jowett <oliver@mutability.co.uk>
Date: Sat, 27 Dec 2014 20:52:56 +0000
Subject: [PATCH] Add support for controlling the accuracy of the receiver
 location written in the JSON metadata used by the webmap.

---
 debian/changelog                     |  2 ++
 debian/config-template               |  3 +++
 debian/dump1090-mutability.config    |  2 ++
 debian/dump1090-mutability.init      |  6 ++++++
 debian/dump1090-mutability.postinst  |  1 +
 debian/dump1090-mutability.templates | 21 +++++++++++++++++++++
 dump1090.c                           |  4 ++++
 dump1090.h                           |  1 +
 net_io.c                             | 17 ++++++++++++-----
 9 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 8886b6b..00440f6 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -3,6 +3,8 @@ dump1090-mutability (1.08.2302.14+1mu-4) UNRELEASED; urgency=medium
   * Remove half-implemented --no-decode option.
   * Remove Windows support files that aren't needed for the Debian package.
   * Remove some legacy support scripts not needed by the Debian package.
+  * Add support for controlling the accuracy of the receiver location
+    written in the JSON metadata used by the webmap.
 
  -- Oliver Jowett <oliver@mutability.co.uk>  Sat, 27 Dec 2014 20:08:44 +0000
 
diff --git a/debian/config-template b/debian/config-template
index 4c723fc..615c6fa 100644
--- a/debian/config-template
+++ b/debian/config-template
@@ -112,5 +112,8 @@ JSON_DIR=
 # Interval between writing json state (in seconds). 0 disables.
 JSON_INTERVAL=
 
+# Accuracy of receiver location to write to json state, one of "exact" / "approximate" / "none"
+JSON_LOCATION_ACCURACY=
+
 # Additional options that are passed to the Daemon.
 EXTRA_ARGS=
diff --git a/debian/dump1090-mutability.config b/debian/dump1090-mutability.config
index 48df016..68bc998 100644
--- a/debian/dump1090-mutability.config
+++ b/debian/dump1090-mutability.config
@@ -47,6 +47,7 @@ if [ -e $CONFIGFILE ]; then
   db_set $NAME/stats-interval "$STATS_INTERVAL"
   db_set $NAME/json-dir "$JSON_DIR"
   db_set $NAME/json-interval "$JSON_INTERVAL"
+  db_set $NAME/json-location-accuracy "$JSON_LOCATION_ACCURACY"
   db_set $NAME/extra-args "$EXTRA_ARGS"
 fi
 
@@ -202,6 +203,7 @@ db_go || true; db_get $NAME/auto-start; if [ "$RET" = "true" ]; then
  db_go || true; db_get $NAME/json-interval; if [ -n "$RET" ] && [ "$RET" -gt 0 ]; then
   # only if json-interval was given and non-zero
   db_input low $NAME/json-dir || true
+  db_input low $NAME/json-location-accuracy || true
  fi
  
  db_input low $NAME/extra-args || true
diff --git a/debian/dump1090-mutability.init b/debian/dump1090-mutability.init
index 1c9d8d3..ed900eb 100644
--- a/debian/dump1090-mutability.init
+++ b/debian/dump1090-mutability.init
@@ -83,6 +83,12 @@ if [ -n "$NET_BIND_ADDRESS" ]; then ARGS="$ARGS --net-bind-address $NET_BIND_ADD
 if [ -n "$STATS_INTERVAL" ]; then ARGS="$ARGS --stats-every $STATS_INTERVAL"; fi
 if [ -n "$JSON_DIR" ]; then ARGS="$ARGS --write-json $JSON_DIR"; fi
 if [ -n "$JSON_INTERVAL" ]; then ARGS="$ARGS --write-json-every $JSON_INTERVAL"; fi
+case "x$JSON_LOCATION_ACCURACY" in
+  xexact) ARGS="$ARGS --json-location-accuracy 2" ;;
+  xapproximate) ARGS="$ARGS --json-location-accuracy 1" ;;
+  *) ARGS="$ARGS --json-location-accuracy 0" ;;
+esac
+
 if [ -n "$EXTRA_ARGS" ]; then ARGS="$ARGS $EXTRA_ARGS"; fi
 
 # Load the VERBOSE setting and other rcS variables
diff --git a/debian/dump1090-mutability.postinst b/debian/dump1090-mutability.postinst
index 2402a19..04fac1e 100644
--- a/debian/dump1090-mutability.postinst
+++ b/debian/dump1090-mutability.postinst
@@ -84,6 +84,7 @@ case "$1" in
         subvar stats-interval STATS_INTERVAL
         subvar json-dir JSON_DIR
         subvar json-interval JSON_INTERVAL
+        subvar json-location-accuracy JSON_LOCATION_ACCURACY
         subvar extra-args EXTRA_ARGS
 
         cp -a -f $CONFIGFILE $CONFIGFILE.tmp
diff --git a/debian/dump1090-mutability.templates b/debian/dump1090-mutability.templates
index 2e14b57..68b6a70 100644
--- a/debian/dump1090-mutability.templates
+++ b/debian/dump1090-mutability.templates
@@ -234,6 +234,27 @@ Description: Interval between writing JSON aircraft state, in seconds:
 Type: string
 Default: 1
 
+Template: dump1090-mutability/json-location-accuracy
+Description: Receiver location accuracy to show in the web interface:
+ dump1090 can provide the configured receiver location to the web map,
+ so that the map can show distances from the receiver.
+ .
+ For privacy reasons, if you are making the map publicly available you
+ may not want to show the exact location of the receiver. There are three
+ options available to control what is shown:
+ .
+ approximate: dump1090 will provide the receiver location rounded to the
+   nearest 0.01 degree of latitude and longitude. This gives a location
+   that is accurate to within about 0.5 - 1km.
+ .
+ exact: dump1090 will provide the exact receiver location.
+ .
+ none: dump1090 will not provide the receiver location at all; distance
+   display will be disabled.
+Type: select
+Choices: approximate, exact, none
+Default: approximate
+
 Template: dump1090-mutability/extra-args
 Description: Extra arguments to pass to dump1090:
  Here you can add any extra arguments you want to pass to dump1090.
diff --git a/dump1090.c b/dump1090.c
index db43499..249689b 100644
--- a/dump1090.c
+++ b/dump1090.c
@@ -83,6 +83,7 @@ void modesInitConfig(void) {
     Modes.fUserLat                = MODES_USER_LATITUDE_DFLT;
     Modes.fUserLon                = MODES_USER_LONGITUDE_DFLT;
     Modes.json_interval           = 1;
+    Modes.json_location_accuracy  = 1;
 }
 //
 //=========================================================================
@@ -448,6 +449,7 @@ void showHelp(void) {
 "--no-decode              Don't decode the message contents beyond the minimum necessary\n"
 "--write-json <dir>       Periodically write json output to <dir> (for serving by a separate webserver)\n"
 "--write-json-every <t>   Write json output every t seconds (default 1)\n"
+"--json-location-accuracy <n>  Accuracy of receiver location in json metadata: 0=no location, 1=approximate, 2=exact\n"
 "--help                   Show this help\n"
 "\n"
 "Debug mode flags: d = Log frames decoded with errors\n"
@@ -844,6 +846,8 @@ int main(int argc, char **argv) {
             strcat(Modes.json_metadata_path, "/receiver.json");
         } else if (!strcmp(argv[j], "--write-json-every") && more) {
             Modes.json_interval = atoi(argv[++j]);
+        } else if (!strcmp(argv[j], "--json-location-accuracy") && more) {
+            Modes.json_location_accuracy = atoi(argv[++j]);
 #endif
         } else {
             fprintf(stderr,
diff --git a/dump1090.h b/dump1090.h
index d6df114..8ea6a2a 100644
--- a/dump1090.h
+++ b/dump1090.h
@@ -371,6 +371,7 @@ struct {                             // Internal state
     char *json_aircraft_path;        // Path to json aircraft file to write, or NULL not to.
     char *json_metadata_path;        // Path to json metadata file to write, or NULL not to.
     int   json_interval;             // Interval between rewriting the json aircraft file
+    int   json_location_accuracy;    // Accuracy of location metadata: 0=none, 1=approx, 2=exact
 
     // User details
     double fUserLat;                // Users receiver/antenna lat/lon needed for initial surface location
diff --git a/net_io.c b/net_io.c
index a55b0e2..1d60c83 100644
--- a/net_io.c
+++ b/net_io.c
@@ -739,11 +739,18 @@ char *generateReceiverJson(int *len)
                  "\"refresh\" : %d",
                  MODES_DUMP1090_VERSION, Modes.json_interval * 1000);
 
-    if (Modes.fUserLat != 0.0 || Modes.fUserLon != 0.0) {
-        p += sprintf(p, ", "                    \
-                     "\"lat\" : %.2f, "
-                     "\"lon\" : %.2f",
-                     Modes.fUserLat, Modes.fUserLon);  // round to 2dp - about 0.5-1km accuracy - for privacy reasons
+    if (Modes.json_location_accuracy && (Modes.fUserLat != 0.0 || Modes.fUserLon != 0.0)) {
+        if (Modes.json_location_accuracy == 1) {
+            p += sprintf(p, ", "                \
+                         "\"lat\" : %.2f, "
+                         "\"lon\" : %.2f",
+                         Modes.fUserLat, Modes.fUserLon);  // round to 2dp - about 0.5-1km accuracy - for privacy reasons
+        } else {
+            p += sprintf(p, ", "                \
+                         "\"lat\" : %.6f, "
+                         "\"lon\" : %.6f",
+                         Modes.fUserLat, Modes.fUserLon);  // exact location
+        }
     }
 
     p += sprintf(p, " }\n");