# Tagging Two Locations on Map using Python and Finding Distance between Two Tagged Location in Python

# Tagging Two Locations on Map using Python and Finding Distance between Two Tagged Location in Python¶

`# To begin with :`

In our last blog we tried to visualize total sales of an organization geographical location wise on a geographical map. Please refer to below blog :

Here in this blog we will see how to tag two different locations on map. And will connect them using a custom arrow on the map itself. Adn then we will see how to compute the aerial distance between the two locations in Kilometers.

Two locations in considerations here are as follows : –

(1) storejockey.com, Noida

(2) Connaught Place, New Delhi

`# Python Code`

Python Libraries to be use :

(1) folium

(2) collections

(3) numpy

(4) math

Make sure that you have all these libraries pre installed on your system. Please rfer to below blog in case of any help :

https://storejockey.com/2019/06/04/image-processing-1-getting-started-with-opencv-for-python/

The first step is to declare the latitude and longitude of two locations in some variables

```
import folium
from collections import namedtuple
import numpy as np
import math
instrovate_latitude = 28.5626
instrovate_longitude = 77.4512
instroate = [instrovate_latitude,instrovate_longitude]
cp_delhi_latitude = 28.6304
cp_delhi_longitude = 77.2177
cp_delhi = [cp_delhi_latitude, cp_delhi_longitude]
```

`# Tagging Locations on Map`

Following are the steps to tag locations on Map :

(1) create a folium map with location as latitude and logitude of instrovate

(2) use folium.marker(…) function to tag the two loations on map one by one. Here we can define color of tagging and text to be displayed if someone clicks that tagged location on map by using argument popup.

(3) Next is to create the line or arrow from instrovate to connaught place delhi. For this use folium.PolyLine(….) method.

(4) TO create the arraow kne from instrovate to connaught place delhi we will use two user defined function get_arrows() and get_degree() whose details will be defined later in the blog.

(5) Once we have the arows we will add it to our existing map

(6) View the map

```
dist_map = folium.Map(location=instroate, zoom_start=12)
folium.Marker(location=instroate, icon=folium.Icon(color='green') , popup = ' storejockey.com ').add_to(dist_map)
folium.Marker(location=cp_delhi, icon=folium.Icon(color='blue'), popup = ' Connaught Place ').add_to(dist_map)
folium.PolyLine(locations=[instroate, cp_delhi], color='red').add_to(dist_map)
arrows = getArrows(locations=[instroate, cp_delhi], n_arrows=3)
for arrow in arrows:
arrow.add_to(dist_map)
dist_map
```

`# Computing Distance Between Two Tagged Location`

Below is the user defined function to calculate the distance between two locations when theor latitude and longitude are given

We can call this function with latitude and longitude values of both the locations and then print the returned value of distance in Kilometeres.

Please note that the distance computed in the aerial distance between the two locations.

```
def find_distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
distance_bw_ori_desti = radius * c
return distance_bw_ori_desti
dis = find_distance((instrovate_latitude, instrovate_longitude),(cp_delhi_latitude, cp_delhi_longitude))
print("Aerial Distance between Instrovate.com and Connaught Place Delhi is %d Km"%(dis))
```

`# User defined functions for getting arrows`

Below are the two user defined functions used to get arrows to connect from instrovate to connaugth place delhi.

Locations needs to be converted in to namedtuples.

get_degree method is used to calculate the angle by which the arrow needs to be rotated as originally the arrow would be towards right hand side. It must be appropriately rotated to connect the two locations.

Below are the codes for two user defined functions

Please note that the execution of the below two user defined fuctions nees to be performed first they are being called in above codes.

```
def getArrows(locations, color='blue', size=6, n_arrows=3):
'''
Get a list of placed and rotated arrows or markers to be plotted
Parameters
locations : list of lists of latitude longitude that represent the begining and end of Line.
this function Return list of arrows or the markers
'''
Point = namedtuple('Point', field_names=['lat', 'lon'])
# creating point from Point named tuple
point1 = Point(locations[0][0], locations[0][1])
point2 = Point(locations[1][0], locations[1][1])
# calculate the rotation required for the marker.
#Reducing 90 to account for the orientation of marker
# Get the degree of rotation
angle = get_angle(point1, point2) - 90
# get the evenly space list of latitudes and longitudes for the required arrows
arrow_latitude = np.linspace(point1.lat, point2.lat, n_arrows + 2)[1:n_arrows+1]
arrow_longitude = np.linspace(point1.lon, point2.lon, n_arrows + 2)[1:n_arrows+1]
final_arrows = []
#creating each "arrow" and appending them to our arrows list
for points in zip(arrow_latitude, arrow_longitude):
final_arrows.append(folium.RegularPolygonMarker(location=points,
fill_color=color, number_of_sides=3,
radius=size, rotation=angle))
return final_arrows
```

```
def get_angle(p1, p2):
'''
This function Returns angle value in degree from the location p1 to location p2
Parameters it accepts :
p1 : namedtuple with lat lon
p2 : namedtuple with lat lon
This function Return the vlaue of degree in the data type float
Pleae also refers to for better understanding : https://gist.github.com/jeromer/2005586
'''
longitude_diff = np.radians(p2.lon - p1.lon)
latitude1 = np.radians(p1.lat)
latitude2 = np.radians(p2.lat)
x_vector = np.sin(longitude_diff) * np.cos(latitude2)
y_vector = (np.cos(latitude1) * np.sin(latitude2)
- (np.sin(latitude1) * np.cos(latitude2)
* np.cos(longitude_diff)))
angle = np.degrees(np.arctan2(x_vector, y_vector))
# Checking and adjustring angle value on the scale of 360
if angle < 0:
return angle + 360
return angle
```