diff --git a/myproject/N29E079.hgt b/myproject/N29E079.hgt new file mode 100644 index 0000000..632b6a6 Binary files /dev/null and b/myproject/N29E079.hgt differ diff --git a/myproject/db.sqlite3 b/myproject/db.sqlite3 index 3ab243c..b7b7f02 100644 Binary files a/myproject/db.sqlite3 and b/myproject/db.sqlite3 differ diff --git a/myproject/myapp/N29E079.hgt b/myproject/myapp/N29E079.hgt new file mode 100644 index 0000000..632b6a6 Binary files /dev/null and b/myproject/myapp/N29E079.hgt differ diff --git a/myproject/myapp/__pycache__/views.cpython-36.pyc b/myproject/myapp/__pycache__/views.cpython-36.pyc index 4588a69..fe900e3 100644 Binary files a/myproject/myapp/__pycache__/views.cpython-36.pyc and b/myproject/myapp/__pycache__/views.cpython-36.pyc differ diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index 3335436..d165f75 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -4,6 +4,7 @@ import requests import xarray import pandas as pd +import geopy.distance from django.http import HttpResponse, JsonResponse from django.shortcuts import render,redirect @@ -235,32 +236,40 @@ def getIndices(request): @login_required def getElevations(request): + SAMPLES = 3601 if request.method == 'POST': print('request for terrain profile') reqObj = json.loads( request.body.decode('utf-8') ) latLngs = reqObj['latLngArray'] - queryParams = '' - for i in latLngs: - queryParams+=',' - queryParams+=str(i['lat']) - queryParams+=',' - queryParams+=str(i['lng']) - queryParams = queryParams[1:] - print(queryParams) - query = ('http://open.mapquestapi.com/elevation/v1/profile?key=xxJeGzM0qOcSKmEVmjPI0N09bPpiASzD&shapeFormat=raw&latLngCollection='+str(queryParams)) - print(query) - r = requests.get(query) - print('-----------------------------------------------------------------------------') - elevationAPIOutput = json.loads(r.content) - print(elevationAPIOutput) - print('-----------------------------------------------------------------------------') - res = {} - if(elevationAPIOutput['info']['statuscode'] == 0): - res['elevationProfile'] = elevationAPIOutput['elevationProfile'] + hgt_file = 'N29E079.hgt' + elevArr = [] + prevlat = 0 + prevlng = 0 + with open(hgt_file, 'rb') as hgt_data: + # Each data is 16bit signed integer(i2) - big endian(>) + elevations = np.fromfile(hgt_data, np.dtype('>i2'), SAMPLES*SAMPLES).reshape((SAMPLES, SAMPLES)) + for i in latLngs: + lat = i['lat'] + lon = i['lng'] + lat_row = int(round((lat - int(lat)) * (SAMPLES - 1), 0)) + lon_row = int(round((lon - int(lon)) * (SAMPLES - 1), 0)) + ans = elevations[SAMPLES - 1 - lat_row, lon_row].astype(int) + + coords1 = (prevlat,prevlng) + coords2 = (lat,lon) + dist = 0 + if(prevlat == 0 and prevlng == 0): + print('huh initial one') + dist = 0 + else: + dist = geopy.distance.vincenty(coords1,coords2).km + elevArr.append({'elevation':int(ans),'distance':dist}) + print(ans) + print(dist) + prevlat = i['lat'] + prevlng = i['lng'] + res = {} res['error'] = 'no error' - print(res) - return JsonResponse(res) - else: - res['error'] = 'error in processing' + res['elevationProfile'] = elevArr return JsonResponse(res) diff --git a/myproject/static/js/script.js b/myproject/static/js/script.js index 1486b11..a4daf05 100644 --- a/myproject/static/js/script.js +++ b/myproject/static/js/script.js @@ -1,5 +1,10 @@ /*jshint esversion: 6 */ -function getCookie(name) { +/** + * This function is used to get cookie so that csrf token can be set while sending request to backend + * @param {string} name - Name to get cookie by name + * @returns {string} cookieValue - Value of cookie corresponding to name + */ +function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== "") { let cookies = document.cookie.split(";"); @@ -19,6 +24,14 @@ function getCookie(name) { return cookieValue; } +/** + * This function is used to send XML request to backend + * It uses Promises to send asynchronus requests + * @param {string} url - url defined in urls.py to which request must be made + * @param {string} type - type of request {'POST' / 'GET'} + * @param {object} data - data to be sent to backend in case of POST request + */ + function sendRequest(url,type,data){ let request = new XMLHttpRequest(); let csrftoken = getCookie("csrftoken"); @@ -39,7 +52,7 @@ function sendRequest(url,type,data){ }; // Setup our HTTP request request.open(type || "GET", url, true); - + // Add csrf token request.setRequestHeader("X-CSRFToken", csrftoken); // Send the request request.send(JSON.stringify(data)); @@ -47,6 +60,11 @@ function sendRequest(url,type,data){ } +/** + * + * @param {object} latlng - dictionary corresponding to latitude longitude of location clicked + */ + function latlonToBackend(latlng){ console.log(latlng.lat); console.log(latlng.lng); @@ -151,7 +169,7 @@ map.on("pm:drawend", (e) => { for(let i of distElevationArray){ console.log(i); xArr.push(i.distance); - yArr.push(i.height); + yArr.push(i.elevation); } console.log(xArr); console.log(yArr); @@ -192,6 +210,7 @@ map.on("pm:drawend", (e) => { y: yArr, x: xArr, }], layout ); + console.log(distElevationArray); document.getElementById("loader").style.display="none"; }).catch( (e)=>{