{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Distance wrapper script\n", "## Description\n", "This notebook computes the distance for the records with respect to the origin point for the selected trips of the route." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import json\n", "import math\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from pymongo import MongoClient\n", "con = MongoClient()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "def mydistance(a1,b1,a2,b2):\n", " '''\n", " input: location attributes corresponding to point 1 and 2. (lat1, lon1, lat2, lon2)\n", " output: distance between point 1 and point 2\n", " function: compute distance between two points using haversine formula\n", " ''' \n", " R=6371e3\n", " x1=math.radians(a1)\n", " y1=math.radians(b1)\n", " x2=math.radians(a2)\n", " y2=math.radians(b2)\n", " delx=x2-x1\n", " dely=y2-y1\n", " c=math.sin(delx/2)*math.sin(delx/2)+math.cos(x1)*math.cos(x2)*math.sin(dely/2)*math.sin(dely/2)\n", " d=2*math.atan2(math.sqrt(c),math.sqrt(1-c))\n", " e=R*d\n", " return(e)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nProjectDataUsed = True\\nUsedPreTrained = True\\nUseMongoDB = False\\n'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#'''\n", "ProjectDataUsed = True\n", "UsedPreTrained = False\n", "UseMongoDB = True\n", "#'''\n", "'''\n", "ProjectDataUsed = True\n", "UsedPreTrained = True\n", "UseMongoDB = False\n", "'''" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "RouteName = 'Git_ISCON_PDPU'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "TripsInfo=['20_12_2017__18_31_19', '18_01_2018__07_38_10']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "if UseMongoDB==True:\n", " '''Compute the distance for the trip records of the selected trips'''\n", " for TripIndex in range(len(TripsInfo)):\n", " LocationRecords = [lr for lr in con[RouteName][TripsInfo[TripIndex]+'.Filtered'].find().sort([('epoch',1)])]\n", " i=0\n", " #distanceFromOrigin=0.0\n", " LocationRecordsReference = [lr for lr in con[RouteName][TripsInfo[TripIndex]+'.Filtered'].find().sort([('epoch',1)])]\n", "\n", " distanceFromOrigin = mydistance(LocationRecordsReference[0][\"Latitude\"],LocationRecordsReference[0][\"Longitude\"],LocationRecords[0][\"Latitude\"],LocationRecords[0][\"Longitude\"])\n", " normalizedDistanceFromOrigin=0.0\n", " #print(distanceFromOrigin)\n", " #input()\n", " #totalDistance=0\n", " totalDistance=distanceFromOrigin\n", " for index in range(len(LocationRecords)-1):\n", " lt1=LocationRecords[index][\"Latitude\"]\n", " ln1=LocationRecords[index][\"Longitude\"]\n", " lt2=LocationRecords[index+1][\"Latitude\"]\n", " ln2=LocationRecords[index+1][\"Longitude\"]\n", " totalDistance += mydistance(lt1,ln1, lt2,ln2) \n", "\n", " normalizedDistanceFromOrigin = distanceFromOrigin/totalDistance\n", " LocationRecords[i]['distanceFromOrigin']=distanceFromOrigin\n", " LocationRecords[i]['normalizedDistanceFromOrigin']=normalizedDistanceFromOrigin\n", " for i in range(1, len(LocationRecords)):\n", " lt1=LocationRecords[i-1][\"Latitude\"]\n", " ln1=LocationRecords[i-1][\"Longitude\"]\n", " lt2=LocationRecords[i][\"Latitude\"]\n", " ln2=LocationRecords[i][\"Longitude\"]\n", "\n", " distanceFromOrigin += mydistance(lt1,ln1,lt2,ln2)\n", " normalizedDistanceFromOrigin = distanceFromOrigin/totalDistance\n", "\n", " LocationRecords[i]['distanceFromOrigin']=distanceFromOrigin\n", " LocationRecords[i]['normalizedDistanceFromOrigin']=normalizedDistanceFromOrigin\n", " #print(LocationRecords)\n", " #input()\n", " #con[RouteName].drop_collection(TripsInfo[TripIndex]+'.LocationRecordsWithDistanceFromOrigin')\n", " con[RouteName]['TripInfo'].update_one({'SingleTripInfo':TripsInfo[TripIndex]},{'$set':{'totalDistance':totalDistance}})\n", " con[RouteName][TripsInfo[TripIndex]+'.LocationRecordsWithDistanceFromOrigin'].insert_many(LocationRecords)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.9" } }, "nbformat": 4, "nbformat_minor": 2 }