diff --git a/routemodelv2/elevations/get_elevation_data.py b/routemodelv2/elevations/get_elevation_data.py new file mode 100644 index 0000000..8aab7b8 --- /dev/null +++ b/routemodelv2/elevations/get_elevation_data.py @@ -0,0 +1,136 @@ +import requests, math, psycopg2, pandas, sys, time + +''' +API INFORMATION +Dataset Used: {srtm30m} https://www.opentopodata.org/datasets/srtm/ +Parameters: Dataset, Coordinate +Return: Information returned as a json object [Elevation is in metres above sea level] +''' + +''' +STEPS +1. Check if the “elevation” table exists, if it doesn't create it. If it does, move on. +2. Seed the database with a given CSV path of raw data (lats, and longs) +3. Once seeded, update the table for each lat and long entry with its: + - elevation (as you have already captured) + - degree of elevation +''' + +FILENAME = None +DB_HOST = None +DB_NAME = None +DB_USER = None +DB_PASSWORD = None +DATASET = 'srtm30m' +QUERIES = { + 'create_table' : "CREATE TABLE IF NOT EXISTS elevation (id SERIAL PRIMARY KEY, lat float8, lon float8, elevation float8, degree float8);", + 'clear_table' : "TRUNCATE TABLE elevation RESTART IDENTITY;", + 'add_coord' : "INSERT INTO elevation (lat, lon) VALUES (%s, %s)", + 'elevation_empty' : "SELECT id, lat, lon FROM elevation WHERE elevation IS NULL", + 'degree_empty' : "SELECT id, lat, lon FROM elevation WHERE degree IS NULL", + 'update_elevation' : "UPDATE elevation SET elevation = %s WHERE id = %s", + 'udpate_degree' : "UPDATE elevation SET degree = %s WHERE lat = %s AND lon = %s", +} + +def verify_inputs(): + global FILENAME, DB_HOST, DB_NAME, DB_USER, DB_PASSWORD + if len(sys.argv) != 6: + print("Usage: python elevation.py ") + sys.exit(1) + scriptname, filename, db_host, db_name, db_user, db_password = sys.argv + FILENAME = filename + DB_HOST = db_host + DB_NAME = db_name + DB_USER = db_user + DB_PASSWORD = db_password + + +def read_csv(): + df = pandas.read_csv(FILENAME) + return df + + +def connect_to_db(): + connection = psycopg2.connect( + host = DB_HOST, + database = DB_NAME, + user = DB_USER, + password = DB_PASSWORD + ) + return connection + + +def create_table(connection): + cursor = connection.cursor() + cursor.execute(QUERIES['create_table']) + connection.commit() + cursor.close() + print("table created!") + return + + +def seed_database(connection, df): + confirmation = input("Seeding the database will clear current values in the table. Would you like to continue? (Y/N): ").lower() + if confirmation == "y": + cursor = connection.cursor() + print("Clearing table...") + cursor.execute(QUERIES['clear_table']) + connection.commit() + print("Seeding Database...") + for row in df.itertuples(): + lat = row.latitude + lon = row.longitude + cursor.execute(QUERIES['add_coord'], (lat, lon)) + connection.commit() + cursor.close() + print("Database seeded.") + return True + else: + print("Operation canceled.") + return False + + +def get_elevation(lat, long): + parameter = f'{lat},{long}' + try: + response = requests.get(f'https://api.opentopodata.org/v1/{DATASET}?locations={parameter}&&interpolation=bilinear') + response.raise_for_status() + data = response.json() + elevation_value = (data['results'][0]['elevation']) + return elevation_value + except Exception as e: + print(repr(e)) + return + + +def calculate_degree(lat1, long1, elev1, lat2, long2, elev2): + pass + + +def update_table_entries(connection): + print("Updating table entries...") + cursor = connection.cursor() + # Check which rows have NULL elevation + cursor.execute(QUERIES['elevation_empty']) + rows_to_update = cursor.fetchall() + for row in rows_to_update: + id, lat, lon = row + elevation = get_elevation(lat, lon) + cursor.execute(QUERIES['update_elevation'], (elevation, id)) + connection.commit() + print(f'Elevation Added for ID[{id}]') + time.sleep(1) + #TODO Update Degree + cursor.close() + return + +# MAIN +if __name__ == "__main__": + verify_inputs() + df = read_csv() + connection = connect_to_db() + create_table(connection) + seed_database(connection, df) + update_table_entries(connection) + connection.close() + diff --git a/routemodelv2/elevations/sample_coordinates.csv b/routemodelv2/elevations/sample_coordinates.csv new file mode 100644 index 0000000..1e6cb11 --- /dev/null +++ b/routemodelv2/elevations/sample_coordinates.csv @@ -0,0 +1,208 @@ +,coordinates,latitude,longitude +0,"(43.467879339595996, -80.56616840836313)",43.467879339595996,-80.56616840836313 +1,"(43.46797804546268, -80.5658911293861)",43.46797804546268,-80.5658911293861 +2,"(43.46807675065529, -80.56561384950668)",43.46807675065529,-80.56561384950668 +3,"(43.46817545517384, -80.56533656872487)",43.46817545517384,-80.56533656872487 +4,"(43.46827415901829, -80.5650592870407)",43.46827415901829,-80.5650592870407 +5,"(43.46837286218867, -80.56478200445413)",43.46837286218867,-80.56478200445413 +6,"(43.468471564684975, -80.56450472096518)",43.468471564684975,-80.56450472096518 +7,"(43.46857026650717, -80.56422743657384)",43.46857026650717,-80.56422743657384 +8,"(43.46866896765528, -80.56395015128014)",43.46866896765528,-80.56395015128014 +9,"(43.46876766812929, -80.56367286508403)",43.46876766812929,-80.56367286508403 +10,"(43.4688663679292, -80.56339557798556)",43.4688663679292,-80.56339557798556 +11,"(43.46896506705499, -80.56311828998469)",43.46896506705499,-80.56311828998469 +12,"(43.469063765506675, -80.56284100108142)",43.469063765506675,-80.56284100108142 +13,"(43.46916246328424, -80.5625637112758)",43.46916246328424,-80.5625637112758 +14,"(43.46926116038769, -80.56228642056779)",43.46926116038769,-80.56228642056779 +15,"(43.46935985681701, -80.56200912895738)",43.46935985681701,-80.56200912895738 +16,"(43.4694585525722, -80.56173183644461)",43.4694585525722,-80.56173183644461 +17,"(43.46955724765325, -80.56145454302944)",43.46955724765325,-80.56145454302944 +18,"(43.469655942060164, -80.5611772487119)",43.469655942060164,-80.5611772487119 +19,"(43.46975463579293, -80.56089995349197)",43.46975463579293,-80.56089995349197 +20,"(43.469853328851556, -80.56062265736965)",43.469853328851556,-80.56062265736965 +21,"(43.46995202123602, -80.56034536034497)",43.46995202123602,-80.56034536034497 +22,"(43.47005071294634, -80.56006806241788)",43.47005071294634,-80.56006806241788 +23,"(43.47014940398248, -80.55979076358844)",43.47014940398248,-80.55979076358844 +24,"(43.47024809434446, -80.55951346385659)",43.47024809434446,-80.55951346385659 +25,"(43.47034678403228, -80.55923616322237)",43.47034678403228,-80.55923616322237 +26,"(43.470445473045906, -80.55895886168578)",43.470445473045906,-80.55895886168578 +27,"(43.47054416138536, -80.55868155924679)",43.47054416138536,-80.55868155924679 +28,"(43.470642849050634, -80.55840425590543)",43.470642849050634,-80.55840425590543 +29,"(43.47074153604173, -80.55812695166168)",43.47074153604173,-80.55812695166168 +30,"(43.47084022235862, -80.55784964651555)",43.47084022235862,-80.55784964651555 +31,"(43.47093890800131, -80.55757234046705)",43.47093890800131,-80.55757234046705 +32,"(43.47103759296979, -80.55729503351616)",43.47103759296979,-80.55729503351616 +33,"(43.47113627726409, -80.55701772566289)",43.47113627726409,-80.55701772566289 +34,"(43.471234960884146, -80.55674041690725)",43.471234960884146,-80.55674041690725 +35,"(43.47133364383001, -80.55646310724921)",43.47133364383001,-80.55646310724921 +36,"(43.47143232610165, -80.5561857966888)",43.47143232610165,-80.5561857966888 +37,"(43.471531007699056, -80.555908485226)",43.471531007699056,-80.555908485226 +38,"(43.47162968862224, -80.55563117286084)",43.47162968862224,-80.55563117286084 +39,"(43.47172836887118, -80.55535385959328)",43.47172836887118,-80.55535385959328 +40,"(43.4718270484459, -80.55507654542335)",43.4718270484459,-80.55507654542335 +41,"(43.471925727346374, -80.55479923035104)",43.471925727346374,-80.55479923035104 +42,"(43.4720244055726, -80.55452191437635)",43.4720244055726,-80.55452191437635 +43,"(43.47212308312456, -80.55424459749928)",43.47212308312456,-80.55424459749928 +44,"(43.47222176000228, -80.55396727971983)",43.47222176000228,-80.55396727971983 +45,"(43.47232043620573, -80.55368996103799)",43.47232043620573,-80.55368996103799 +46,"(43.472419111734915, -80.55341264145379)",43.472419111734915,-80.55341264145379 +47,"(43.47251778658985, -80.55313532096719)",43.47251778658985,-80.55313532096719 +48,"(43.47261646077049, -80.55285799957822)",43.47261646077049,-80.55285799957822 +49,"(43.47271513427686, -80.55258067728687)",43.47271513427686,-80.55258067728687 +50,"(43.47281380710894, -80.55230335409314)",43.47281380710894,-80.55230335409314 +51,"(43.47291247926674, -80.55202602999704)",43.47291247926674,-80.55202602999704 +52,"(43.47301115075025, -80.55174870499854)",43.47301115075025,-80.55174870499854 +53,"(43.47310982155946, -80.55147137909769)",43.47310982155946,-80.55147137909769 +54,"(43.47320849169437, -80.55119405229445)",43.47320849169437,-80.55119405229445 +55,"(43.473307161154985, -80.55091672458883)",43.473307161154985,-80.55091672458883 +56,"(43.47340582994128, -80.55063939598082)",43.47340582994128,-80.55063939598082 +57,"(43.473504498053266, -80.55036206647046)",43.473504498053266,-80.55036206647046 +58,"(43.47360316549092, -80.5500847360577)",43.47360316549092,-80.5500847360577 +59,"(43.47370183225427, -80.54980740474257)",43.47370183225427,-80.54980740474257 +60,"(43.47380049834328, -80.54953007252504)",43.47380049834328,-80.54953007252504 +61,"(43.47389916375796, -80.54925273940516)",43.47389916375796,-80.54925273940516 +62,"(43.473997828498305, -80.54897540538289)",43.473997828498305,-80.54897540538289 +63,"(43.474096492564314, -80.54869807045824)",43.474096492564314,-80.54869807045824 +64,"(43.474195155955975, -80.54842073463122)",43.474195155955975,-80.54842073463122 +65,"(43.47429381867329, -80.54814339790182)",43.47429381867329,-80.54814339790182 +66,"(43.47439248071624, -80.54786606027004)",43.47439248071624,-80.54786606027004 +67,"(43.47449114208484, -80.54758872173589)",43.47449114208484,-80.54758872173589 +68,"(43.47458980277908, -80.54731138229936)",43.47458980277908,-80.54731138229936 +69,"(43.474688462798944, -80.54703404196044)",43.474688462798944,-80.54703404196044 +70,"(43.474787122144434, -80.54675670071917)",43.474787122144434,-80.54675670071917 +71,"(43.474885780815555, -80.54647935857551)",43.474885780815555,-80.54647935857551 +72,"(43.47498443881229, -80.54620201552946)",43.47498443881229,-80.54620201552946 +73,"(43.47508309613465, -80.54592467158105)",43.47508309613465,-80.54592467158105 +74,"(43.47518175278261, -80.54564732673026)",43.47518175278261,-80.54564732673026 +75,"(43.47528040875618, -80.54536998097709)",43.47528040875618,-80.54536998097709 +76,"(43.475379064055346, -80.54509263432155)",43.475379064055346,-80.54509263432155 +77,"(43.47547771868012, -80.54481528676362)",43.47547771868012,-80.54481528676362 +78,"(43.47557637263047, -80.54453793830332)",43.47557637263047,-80.54453793830332 +79,"(43.47567502590641, -80.54426058894066)",43.47567502590641,-80.54426058894066 +80,"(43.47577367850795, -80.54398323867561)",43.47577367850795,-80.54398323867561 +81,"(43.47587233043505, -80.54370588750818)",43.47587233043505,-80.54370588750818 +82,"(43.47597098168773, -80.54342853543838)",43.47597098168773,-80.54342853543838 +83,"(43.47606963226598, -80.5431511824662)",43.47606963226598,-80.5431511824662 +84,"(43.4761682821698, -80.54287382859165)",43.4761682821698,-80.54287382859165 +85,"(43.47626693139918, -80.54259647381473)",43.47626693139918,-80.54259647381473 +86,"(43.47636557995411, -80.54231911813544)",43.47636557995411,-80.54231911813544 +87,"(43.476464227834605, -80.54204176155376)",43.476464227834605,-80.54204176155376 +88,"(43.47656287504064, -80.54176440406971)",43.47656287504064,-80.54176440406971 +89,"(43.47666152157222, -80.54148704568328)",43.47666152157222,-80.54148704568328 +90,"(43.476760167429326, -80.54120968639448)",43.476760167429326,-80.54120968639448 +91,"(43.47685881261198, -80.5409323262033)",43.47685881261198,-80.5409323262033 +92,"(43.47695745712016, -80.54065496510977)",43.47695745712016,-80.54065496510977 +93,"(43.47705610095387, -80.54037760311384)",43.47705610095387,-80.54037760311384 +94,"(43.47715474411309, -80.54010024021554)",43.47715474411309,-80.54010024021554 +95,"(43.47725338659784, -80.53982287641486)",43.47725338659784,-80.53982287641486 +96,"(43.477352028408085, -80.53954551171182)",43.477352028408085,-80.53954551171182 +97,"(43.47745066954385, -80.5392681461064)",43.47745066954385,-80.5392681461064 +98,"(43.47754931000511, -80.5389907795986)",43.47754931000511,-80.5389907795986 +99,"(43.47764794979187, -80.53871341218844)",43.47764794979187,-80.53871341218844 +100,"(43.47774658890413, -80.5384360438759)",43.47774658890413,-80.5384360438759 +101,"(43.477845227341874, -80.53815867466099)",43.477845227341874,-80.53815867466099 +102,"(43.477943865105104, -80.5378813045437)",43.477943865105104,-80.5378813045437 +103,"(43.478042502193816, -80.53760393352405)",43.478042502193816,-80.53760393352405 +104,"(43.478141138608, -80.53732656160201)",43.478141138608,-80.53732656160201 +105,"(43.47823977434766, -80.5370491887776)",43.47823977434766,-80.5370491887776 +106,"(43.478338409412785, -80.53677181505083)",43.478338409412785,-80.53677181505083 +107,"(43.47843704380337, -80.53649444042168)",43.47843704380337,-80.53649444042168 +108,"(43.47853567751941, -80.53621706489015)",43.47853567751941,-80.53621706489015 +109,"(43.47863431056091, -80.53593968845625)",43.47863431056091,-80.53593968845625 +110,"(43.47873294292786, -80.53566231111998)",43.47873294292786,-80.53566231111998 +111,"(43.47883157462026, -80.53538493288134)",43.47883157462026,-80.53538493288134 +112,"(43.47863923410959, -80.5352433259979)",43.47863923410959,-80.5352433259979 +113,"(43.47844689341706, -80.5351017200128)",43.47844689341706,-80.5351017200128 +114,"(43.47825455254268, -80.53496011492606)",43.47825455254268,-80.53496011492606 +115,"(43.478062211486446, -80.53481851073765)",43.478062211486446,-80.53481851073765 +116,"(43.477869870248355, -80.53467690744756)",43.477869870248355,-80.53467690744756 +117,"(43.47767752882842, -80.53453530505578)",43.47767752882842,-80.53453530505578 +118,"(43.47748518722664, -80.53439370356232)",43.47748518722664,-80.53439370356232 +119,"(43.47729284544301, -80.53425210296714)",43.47729284544301,-80.53425210296714 +120,"(43.477100503477544, -80.53411050327023)",43.477100503477544,-80.53411050327023 +121,"(43.476908161330236, -80.5339689044716)",43.476908161330236,-80.5339689044716 +122,"(43.47671581900108, -80.53382730657123)",43.47671581900108,-80.53382730657123 +123,"(43.476523476490094, -80.5336857095691)",43.476523476490094,-80.5336857095691 +124,"(43.47633113379728, -80.53354411346521)",43.47633113379728,-80.53354411346521 +125,"(43.47613879092263, -80.53340251825955)",43.47613879092263,-80.53340251825955 +126,"(43.47594644786616, -80.5332609239521)",43.47594644786616,-80.5332609239521 +127,"(43.475754104627846, -80.53311933054285)",43.475754104627846,-80.53311933054285 +128,"(43.47556176120772, -80.53297773803179)",43.47556176120772,-80.53297773803179 +129,"(43.47536941760577, -80.53283614641893)",43.47536941760577,-80.53283614641893 +130,"(43.47517707382198, -80.53269455570423)",43.47517707382198,-80.53269455570423 +131,"(43.474984729856395, -80.53255296588767)",43.474984729856395,-80.53255296588767 +132,"(43.474792385709, -80.53241137696929)",43.474792385709,-80.53241137696929 +133,"(43.47460004137977, -80.53226978894904)",43.47460004137977,-80.53226978894904 +134,"(43.474407696868745, -80.53212820182691)",43.474407696868745,-80.53212820182691 +135,"(43.474215352175904, -80.53198661560289)",43.474215352175904,-80.53198661560289 +136,"(43.474023007301255, -80.531845030277)",43.474023007301255,-80.531845030277 +137,"(43.473830662244815, -80.53170344584917)",43.473830662244815,-80.53170344584917 +138,"(43.47372587646144, -80.53196442068774)",43.47372587646144,-80.53196442068774 +139,"(43.473621090080506, -80.5322253946245)",43.473621090080506,-80.5322253946245 +140,"(43.47351630310205, -80.53248636765943)",43.47351630310205,-80.53248636765943 +141,"(43.47341151552607, -80.53274733979254)",43.47341151552607,-80.53274733979254 +142,"(43.47330672735254, -80.53300831102382)",43.47330672735254,-80.53300831102382 +143,"(43.4732019385815, -80.5332692813533)",43.4732019385815,-80.5332692813533 +144,"(43.47309714921293, -80.53353025078094)",43.47309714921293,-80.53353025078094 +145,"(43.472992359246845, -80.53379121930678)",43.472992359246845,-80.53379121930678 +146,"(43.472887568683255, -80.53405218693081)",43.472887568683255,-80.53405218693081 +147,"(43.47278277752214, -80.53431315365303)",43.47278277752214,-80.53431315365303 +148,"(43.47267798576353, -80.53457411947342)",43.47267798576353,-80.53457411947342 +149,"(43.47257319340743, -80.53483508439201)",43.47257319340743,-80.53483508439201 +150,"(43.47246840045383, -80.53509604840879)",43.47246840045383,-80.53509604840879 +151,"(43.472363606902725, -80.53535701152377)",43.472363606902725,-80.53535701152377 +152,"(43.472258812754134, -80.53561797373693)",43.472258812754134,-80.53561797373693 +153,"(43.47215401800807, -80.5358789350483)",43.47215401800807,-80.5358789350483 +154,"(43.47204922266452, -80.53613989545786)",43.47204922266452,-80.53613989545786 +155,"(43.4719444267235, -80.53640085496562)",43.4719444267235,-80.53640085496562 +156,"(43.47183963018501, -80.53666181357157)",43.47183963018501,-80.53666181357157 +157,"(43.471734833049055, -80.53692277127573)",43.471734833049055,-80.53692277127573 +158,"(43.47163003531563, -80.53718372807808)",43.47163003531563,-80.53718372807808 +159,"(43.471525236984746, -80.53744468397863)",43.471525236984746,-80.53744468397863 +160,"(43.47132285394585, -80.53756280284698)",43.47132285394585,-80.53756280284698 +161,"(43.471120470777755, -80.53768092092704)",43.471120470777755,-80.53768092092704 +162,"(43.47091808748045, -80.53779903821885)",43.47091808748045,-80.53779903821885 +163,"(43.470715704053944, -80.53791715472241)",43.470715704053944,-80.53791715472241 +164,"(43.470513320498256, -80.53803527043773)",43.470513320498256,-80.53803527043773 +165,"(43.470310936813355, -80.53815338536482)",43.470310936813355,-80.53815338536482 +166,"(43.470108552999264, -80.5382714995037)",43.470108552999264,-80.5382714995037 +167,"(43.46990616905598, -80.53838961285436)",43.46990616905598,-80.53838961285436 +168,"(43.4697037849835, -80.53850772541682)",43.4697037849835,-80.53850772541682 +169,"(43.46950140078184, -80.5386258371911)",43.46950140078184,-80.5386258371911 +170,"(43.46929901645099, -80.5387439481772)",43.46929901645099,-80.5387439481772 +171,"(43.46909663199095, -80.53886205837513)",43.46909663199095,-80.53886205837513 +172,"(43.46889424740172, -80.5389801677849)",43.46889424740172,-80.5389801677849 +173,"(43.468691862683315, -80.53909827640653)",43.468691862683315,-80.53909827640653 +174,"(43.46848947783573, -80.53921638424004)",43.46848947783573,-80.53921638424004 +175,"(43.468287092858965, -80.5393344912854)",43.468287092858965,-80.5393344912854 +176,"(43.468084707753015, -80.53945259754265)",43.468084707753015,-80.53945259754265 +177,"(43.46788232251789, -80.5395707030118)",43.46788232251789,-80.5395707030118 +178,"(43.467679937153584, -80.53968880769285)",43.467679937153584,-80.53968880769285 +179,"(43.46747755166012, -80.53980691158583)",43.46747755166012,-80.53980691158583 +180,"(43.467275166037474, -80.53992501469072)",43.467275166037474,-80.53992501469072 +181,"(43.46707278028566, -80.54004311700756)",43.46707278028566,-80.54004311700756 +182,"(43.46687039440469, -80.54016121853633)",43.46687039440469,-80.54016121853633 +183,"(43.466668008394535, -80.54027931927706)",43.466668008394535,-80.54027931927706 +184,"(43.46646562225523, -80.54039741922976)",43.46646562225523,-80.54039741922976 +185,"(43.466263235986744, -80.54051551839446)",43.466263235986744,-80.54051551839446 +186,"(43.46606084958911, -80.54063361677112)",43.46606084958911,-80.54063361677112 +187,"(43.46585846306231, -80.54075171435979)",43.46585846306231,-80.54075171435979 +188,"(43.46565607640636, -80.54086981116046)",43.46565607640636,-80.54086981116046 +189,"(43.46545368962124, -80.54098790717316)",43.46545368962124,-80.54098790717316 +190,"(43.46525130270698, -80.54110600239788)",43.46525130270698,-80.54110600239788 +191,"(43.46504891566356, -80.54122409683465)",43.46504891566356,-80.54122409683465 +192,"(43.46484652849099, -80.54134219048346)",43.46484652849099,-80.54134219048346 +193,"(43.46464414118927, -80.54146028334435)",43.46464414118927,-80.54146028334435 +194,"(43.4644417537584, -80.54157837541729)",43.4644417537584,-80.54157837541729 +195,"(43.464304877302574, -80.5413436835921)",43.464304877302574,-80.5413436835921 +196,"(43.46416800036178, -80.54110899282594)",43.46416800036178,-80.54110899282594 +197,"(43.46403112293602, -80.5408743031188)",43.46403112293602,-80.5408743031188 +198,"(43.46389424502531, -80.54063961447065)",43.46389424502531,-80.54063961447065 +199,"(43.463757366629636, -80.5404049268815)",43.463757366629636,-80.5404049268815 +200,"(43.46362048774902, -80.54017024035134)",43.46362048774902,-80.54017024035134 +201,"(43.46348360838345, -80.53993555488016)",43.46348360838345,-80.53993555488016 +202,"(43.46331969082911, -80.53976512159053)",43.46331969082911,-80.53976512159053 +203,"(43.46315577301604, -80.53959468922189)",43.46315577301604,-80.53959468922189 +204,"(43.462991854944235, -80.53942425777419)",43.462991854944235,-80.53942425777419 +205,"(43.46282793661371, -80.53925382724742)",43.46282793661371,-80.53925382724742 +206,"(43.46266401802443, -80.53908339764159)",43.46266401802443,-80.53908339764159