# Files:  vds_config_d12.csv is the config file with VDS lat/long and other identifying data
#         5minagg_latest.txt is the 5 min aggregate real time data.  We need two fields: id and speed.
#
# FIRST: Remove first line of 5minagg (contains date). 
# tail -n+2 5minagg_latest.txt > 5min_latest_tail.txt
# SECOND: sort them
# sort 5min_latest_tail.txt > 5min_latest_sorted.txt
# sort vds_config_d12ML.csv > vds_config_d12ML_sorted.csv
# Join the two files on the ID field, appending speed as the last field
# join -t"," 5minagg_latest_sorted.txt  vds_config_d12ML_sorted.csv -o 1.1,2.2,2.6,2.7,2.9,2.11,2.12,1.4  > vds_speeds.csv 

#
#3.  Run this awk program to convert output csv to geojson
#
# Convert a csv with postmile and coordinates to a json file
# Note it switches lat/long to long/lat
# Sample invocation:  
# Determine how many lines in file
# eof=`wc vds_speeds.csv | cut -f3 -d" " `
# Pass that as parameter to awk program, who uses it to not print a comma after last record.
# awk -F',' -v eof="$eof" -f csvToGson.awk vds_speeds.csv

BEGIN { # The file header
print "{ \"type\": \"FeatureCollection\",  \"features\": ["
}
{
# assign color based on speed
if ($8 < 25) $8 = "red"
else if ($8 < 50) $8 = "yellow"
  else $8 = "lime" 
# output the formatted json record
printf "  {\n    \"type\": \"Feature\",\n    \"id\": \"%s\",\n    \"geometry\":\n      {\n      \"type\": \"Point\",\n      \"coordinates\": [%s,%s]\n      },\n      \"properties\": {\"street\":\"%s\", \"postmile\": \"%s %s %s\",\n   \"color\":\"%s\", \"perpx\":\"-0.7\", \"perpy\":\"0.7\"}\n  }", $1, $7,$6,$2,$3,$4,$5,$8,$1,$1 
# if it's not the last record, output the continuation symbol
if (NR != eof) print ","
}
END { # The file trailer
print "\n ]\n}"
}


