Skip to content

Commit

Permalink
Added SRTM DEM script
Browse files Browse the repository at this point in the history
  • Loading branch information
SinghCoder committed Jun 17, 2019
1 parent a9b6aa5 commit b3e787c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 25 deletions.
Binary file added myproject/N29E079.hgt
Binary file not shown.
Binary file modified myproject/db.sqlite3
Binary file not shown.
Binary file added myproject/myapp/N29E079.hgt
Binary file not shown.
Binary file modified myproject/myapp/__pycache__/views.cpython-36.pyc
Binary file not shown.
53 changes: 31 additions & 22 deletions myproject/myapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

25 changes: 22 additions & 3 deletions myproject/static/js/script.js
Original file line number Diff line number Diff line change
@@ -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(";");
Expand All @@ -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");
Expand All @@ -39,14 +52,19 @@ 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));
});

}

/**
*
* @param {object} latlng - dictionary corresponding to latitude longitude of location clicked
*/

function latlonToBackend(latlng){
console.log(latlng.lat);
console.log(latlng.lng);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)=>{
Expand Down

0 comments on commit b3e787c

Please sign in to comment.