| 1 | import collections,math,sys |
|---|
| 2 | # Scan the cctv file to find the nearest vds |
|---|
| 3 | # This is a one-time job to create the static file of cctv locations and nearest vds |
|---|
| 4 | # The output of this program needs to be manually modified before input to cptms_map.html |
|---|
| 5 | # 1. The vds column is appended to cctvFile (yes, adding a new column to the original input). |
|---|
| 6 | # 2. Convert this csv file to json (using cctvToJson.awk) |
|---|
| 7 | # 3. Remove tabs from json. Trim any blanks from nearVDS field. |
|---|
| 8 | # jdalbey Mar 2019 |
|---|
| 9 | vdsFile = "vds_fromhighwaysmap.csv" |
|---|
| 10 | cctvFile = "cctv_locations_D12.csv" |
|---|
| 11 | orientationLookup = {'N':0,'S':1,'E':0,'W':1} |
|---|
| 12 | |
|---|
| 13 | def loadHighways(): |
|---|
| 14 | |
|---|
| 15 | f = open(vdsFile,'r') |
|---|
| 16 | lines = [line.split(',') for line in f.readlines()] |
|---|
| 17 | # 5,N,0.65,4 S. LUIS REY |
|---|
| 18 | # Create a set containing just the route numbers |
|---|
| 19 | routeNums = set() |
|---|
| 20 | for item in lines: |
|---|
| 21 | routeNums.add(int(item[0])) |
|---|
| 22 | # put the route numbers in order |
|---|
| 23 | sortedRoutes = sorted (routeNums) |
|---|
| 24 | # Create the empty postmile collections |
|---|
| 25 | for route in sortedRoutes: |
|---|
| 26 | #print route, |
|---|
| 27 | highways[str(route)]=[collections.OrderedDict(),collections.OrderedDict()] |
|---|
| 28 | #print |
|---|
| 29 | # Process all the data, placing it in proper route and collection |
|---|
| 30 | for item in lines: |
|---|
| 31 | route = item[0] |
|---|
| 32 | orientation = orientationLookup[item[1]] |
|---|
| 33 | postmileItem = item[2] |
|---|
| 34 | highways[route][orientation][postmileItem]=item |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | def dumpHighways(): |
|---|
| 38 | # Dump the highways data we've organized |
|---|
| 39 | for item in highways: |
|---|
| 40 | for cnt in [0,1]: |
|---|
| 41 | list1 = highways[item][cnt] |
|---|
| 42 | print "highway",item,list1 |
|---|
| 43 | # show fields for one entry |
|---|
| 44 | for pm_entry in list1: |
|---|
| 45 | print pm_entry,list1[pm_entry] |
|---|
| 46 | |
|---|
| 47 | def findNearest(): |
|---|
| 48 | |
|---|
| 49 | f = open(cctvFile,'r') |
|---|
| 50 | #5,S,42.11,-117.978854,33.856288,SB 5 N/O MAGNOLIA AVE (S/O 91),12-005-CCTV-0001 |
|---|
| 51 | lines = [line.split(',') for line in f.readlines()] |
|---|
| 52 | |
|---|
| 53 | # Find he nearest VDS to each cctv |
|---|
| 54 | for item in lines: |
|---|
| 55 | route = item[0] |
|---|
| 56 | orientation = orientationLookup[item[1]] |
|---|
| 57 | postmileItem = item[2] |
|---|
| 58 | segment = highways[route][orientation] |
|---|
| 59 | |
|---|
| 60 | # Now we know which highway segment the cctv is on, look at all those VDS to find nearest |
|---|
| 61 | print route,item[1],postmileItem+",", |
|---|
| 62 | min = 9999 |
|---|
| 63 | nearVDS = 0 |
|---|
| 64 | for vds in segment: |
|---|
| 65 | diff = abs(float(vds) - float(postmileItem)) |
|---|
| 66 | #print vds + "("+str(diff)+")", |
|---|
| 67 | if (diff < min): |
|---|
| 68 | min = diff |
|---|
| 69 | nearVDS = vds |
|---|
| 70 | print route,item[1],nearVDS |
|---|
| 71 | |
|---|
| 72 | # ------------------------------------------------------------------------------------------ |
|---|
| 73 | |
|---|
| 74 | highways = collections.OrderedDict() |
|---|
| 75 | loadHighways() |
|---|
| 76 | dumpHighways() |
|---|
| 77 | print "CCTV\tnearestVDS" |
|---|
| 78 | findNearest() |
|---|
| 79 | |
|---|