| 1 | # Files: vds_config_d12.csv is the config file with VDS lat/long and other identifying data |
|---|
| 2 | # 5minagg_latest.txt is the 5 min aggregate real time data. We need two fields: id and speed. |
|---|
| 3 | # |
|---|
| 4 | # FIRST: Remove first line of 5minagg (contains date). |
|---|
| 5 | # tail -n+2 5minagg_latest.txt > 5min_latest_tail.txt |
|---|
| 6 | # SECOND: sort them |
|---|
| 7 | # sort 5min_latest_tail.txt > 5min_latest_sorted.txt |
|---|
| 8 | # sort vds_config_d12ML.csv > vds_config_d12ML_sorted.csv |
|---|
| 9 | # Join the two files on the ID field, appending speed as the last field |
|---|
| 10 | # 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 |
|---|
| 11 | |
|---|
| 12 | # |
|---|
| 13 | #3. Run this awk program to convert output csv to geojson |
|---|
| 14 | # |
|---|
| 15 | # Convert a csv with postmile and coordinates to a json file |
|---|
| 16 | # Note it switches lat/long to long/lat |
|---|
| 17 | # Sample invocation: |
|---|
| 18 | # Determine how many lines in file |
|---|
| 19 | # eof=`wc vds_speeds.csv | cut -f3 -d" " ` |
|---|
| 20 | # Pass that as parameter to awk program, who uses it to not print a comma after last record. |
|---|
| 21 | # awk -F',' -v eof="$eof" -f csvToGson.awk vds_speeds.csv |
|---|
| 22 | |
|---|
| 23 | BEGIN { # The file header |
|---|
| 24 | print "{ \"type\": \"FeatureCollection\", \"features\": [" |
|---|
| 25 | } |
|---|
| 26 | { |
|---|
| 27 | # assign color based on speed |
|---|
| 28 | if ($8 < 25) $8 = "red" |
|---|
| 29 | else if ($8 < 50) $8 = "yellow" |
|---|
| 30 | else $8 = "lime" |
|---|
| 31 | # output the formatted json record |
|---|
| 32 | 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 |
|---|
| 33 | # if it's not the last record, output the continuation symbol |
|---|
| 34 | if (NR != eof) print "," |
|---|
| 35 | } |
|---|
| 36 | END { # The file trailer |
|---|
| 37 | print "\n ]\n}" |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|