Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- ############################################################ IDENT(1)
- #
- # $Title: CGI to convert InfluxDB map data to GeoJSON via JSONP $
- # $Copyright: 2018 Devin Teske. All rights reserved. $
- # $FrauBSD$
- #
- ############################################################ INCLUDES
- use strict;
- use warnings;
- use CGI;
- ############################################################ CONFIGURATION
- my $duration = "30m";
- my $series_geoid_tag = "id_tag";
- #
- # Database and query to get location data
- # NB: This is converted to GeoJSON points
- #
- my $DBNAME = "mydb";
- my $DBHOST = "localhost";
- my $DBPORT = 8086;
- my $FIELDS = join ", ", map { "last($_)" } qw/latitude longitude/;
- my $QUERY = join " ",
- "select $FIELDS from tail where time >= now() - 30m",
- "group by $series_geoid_tag fill(none)";
- ############################################################ GLOBALS
- our $| = 1; # Disable output buffering
- my $tag;
- my $lat;
- my $lng;
- my $cgi = new CGI;
- my $callback = "data";
- ############################################################ MAIN
- #
- # Print HTTP response header
- #
- print $cgi->header({ type => "application/json" });
- #
- # Perform database query
- #
- open CMD, "-|", join " ", "influx",
- qq/-host "$DBHOST" -port "$DBPORT" -database "$DBNAME"/,
- qq/-execute "$QUERY"/ or die "influx: $!";
- #
- # Print callback and GeoJSON preamble
- #
- $callback = $cgi->param("callback") || $callback;
- print qq/$callback({ "type": "FeatureCollection",
- "features": [/;
- #
- # Convert latitude/longitude into GeoJSON points
- #
- my $sep = 0;
- while (<CMD>) {
- next if /^(name: tail)?$/;
- $tag = $1 and next if /^tags: $series_geoid_tag=(\d+)$/;
- next if /^(time|--)/;
- next unless /^\d+\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)/
- || /^\d+\s+(-?\d+\.\d+)\s+(-?\d+)/
- || /^\d+\s+(-?\d+)\s+(-?\d+\.\d+)/
- || /^\d+\s+(-?\d+)\s+(-?\d+)/;
- ($lat, $lng) = ($2, $1);
- print "," unless $sep++ == 0;
- print qq/\n { "type": "Feature", "id": $tag,/;
- print qq/\n "geometry": {"type": "Point", /;
- print qq/"coordinates": [$lat, $lng]/;
- print qq/}}/;
- }
- #
- # Terminate GeoJSON[P] structure
- #
- print qq/
- ]
- });\n/;
- close CMD;
- exit 0;
- ################################################################################
- # END
- ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement