Advertisement
devinteske

geoloop.cgi

Oct 10th, 2018
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.32 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. ############################################################ IDENT(1)
  3. #
  4. # $Title: CGI to convert InfluxDB map data to GeoJSON via JSONP $
  5. # $Copyright: 2018 Devin Teske. All rights reserved. $
  6. # $FrauBSD$
  7. #
  8. ############################################################ INCLUDES
  9.  
  10. use strict;
  11. use warnings;
  12. use CGI;
  13.  
  14. ############################################################ CONFIGURATION
  15.  
  16. my $duration = "30m";
  17. my $series_geoid_tag = "id_tag";
  18.  
  19. #
  20. # Database and query to get location data
  21. # NB: This is converted to GeoJSON points
  22. #
  23. my $DBNAME = "mydb";
  24. my $DBHOST = "localhost";
  25. my $DBPORT = 8086;
  26. my $FIELDS = join ", ", map { "last($_)" } qw/latitude longitude/;
  27. my $QUERY = join " ",
  28.     "select $FIELDS from tail where time >= now() - 30m",
  29.     "group by $series_geoid_tag fill(none)";
  30.  
  31. ############################################################ GLOBALS
  32.  
  33. our $| = 1; # Disable output buffering
  34.  
  35. my $tag;
  36. my $lat;
  37. my $lng;
  38.  
  39. my $cgi = new CGI;
  40.  
  41. my $callback = "data";
  42.  
  43. ############################################################ MAIN
  44.  
  45. #
  46. # Print HTTP response header
  47. #
  48. print $cgi->header({ type => "application/json" });
  49.  
  50. #
  51. # Perform database query
  52. #
  53. open CMD, "-|", join " ", "influx",
  54.     qq/-host "$DBHOST" -port "$DBPORT" -database "$DBNAME"/,
  55.     qq/-execute "$QUERY"/ or die "influx: $!";
  56.  
  57. #
  58. # Print callback and GeoJSON preamble
  59. #
  60. $callback = $cgi->param("callback") || $callback;
  61. print qq/$callback({ "type": "FeatureCollection",
  62.   "features": [/;
  63.  
  64. #
  65. # Convert latitude/longitude into GeoJSON points
  66. #
  67. my $sep = 0;
  68. while (<CMD>) {
  69.     next if /^(name: tail)?$/;
  70.     $tag = $1 and next if /^tags: $series_geoid_tag=(\d+)$/;
  71.     next if /^(time|--)/;
  72.     next unless /^\d+\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)/
  73.         || /^\d+\s+(-?\d+\.\d+)\s+(-?\d+)/
  74.         || /^\d+\s+(-?\d+)\s+(-?\d+\.\d+)/
  75.         || /^\d+\s+(-?\d+)\s+(-?\d+)/;
  76.     ($lat, $lng) = ($2, $1);
  77.     print "," unless $sep++ == 0;
  78.     print qq/\n    { "type": "Feature", "id": $tag,/;
  79.     print qq/\n      "geometry": {"type": "Point", /;
  80.     print qq/"coordinates": [$lat, $lng]/;
  81.     print qq/}}/;
  82. }
  83.  
  84. #
  85. # Terminate GeoJSON[P] structure
  86. #
  87. print qq/
  88.   ]
  89. });\n/;
  90.  
  91. close CMD;
  92. exit 0;
  93.  
  94. ################################################################################
  95. # END
  96. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement