I have a Data frame of 4 columns(point names, latitude, longitude and angles). I am plotting all the points in basemap w.r.t lat & long values. I have written a function to calculate distance for a given point to all the points in the dataframe and then filter out cells within certain km radius and highlight them with different color. I have the following code.
import pandas as pd
import numpy as np
import os
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
margin=0.5
def draw_map(lat_centre, lon_centre,lat, lon, lat1,lon1, marker1, color1, marker2, color2, marker3, color3):
#margin = 2 # buffer to add to the range
lat_min = min(lat) - margin
lat_max = max(lat) + margin
lon_min = min(lon) - margin
lon_max = max(lon) + margin
# create map using BASEMAP
m = Basemap(llcrnrlon=lon_min,
llcrnrlat=lat_min,
urcrnrlon=lon_max,
urcrnrlat=lat_max,
lat_0=(lat_max - lat_min)/2,
lon_0=(lon_max-lon_min)/2,
projection='merc',
resolution = 'h',
area_thresh=10000.,)
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color = 'white',lake_color='#46bcec')
# convert lat and lon to map projection coordinates
lons, lats = m(lon, lat)
# plot points as red dots
m.scatter(lons, lats, marker = marker2, color=color2, zorder=5)
#print('hi')
#plt.show()
#print('hi1')
lons1, lats1 = m(lon1, lat1)
# plot filtered points as green dots
m.scatter(lons1, lats1, marker = marker3, color=color3, zorder=5)
lons, lats = m(lon_centre, lat_centre)
# plot centre point as blue dots
m.scatter(lons, lats, marker = marker1, color=color1, zorder=5)
plt.show()
rr=os.getcwd()
df1=pd.read_csv(rr+'\\siteDB.csv')
df=df1[['Parent_Fdn_EUtranCellFDD','Lat','Long']]
df=df.drop_duplicates()
df.index=df.Parent_Fdn_EUtranCellFDD
print(df.index)
import math
def distance_calc(lon1, lat1, lon2, lat2):
#print(lon1[0], lon2[0])
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat / 2.0) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2.0) ** 2
c = 2 * np.arcsin(np.sqrt(a))
km = 6367 * c
return km
def get_distance(lat_1, lng_1, lat_2, lng_2):
d_lat = lat_2 - lat_1
d_lng = lng_2 - lng_1
temp = (
math.sin(d_lat / 2) ** 2
+ math.cos(lat_1)
* math.cos(lat_2)
* math.sin(d_lng / 2) ** 2
)
return 6373.0 * (2 * math.atan2(math.sqrt(temp), math.sqrt(1 - temp)))
import time
lat = df['Lat'].values
lon = df['Long'].values
g=len(lat)
def dist(orig, dest=None, ftr_km=None, greater_than=False):
t1=time.time()
if dest==None:
d = distance_calc(np.full((g), df.loc[orig, 'Long']), np.full((g), df.loc[orig, 'Lat']), lon, lat)
if ftr_km != None:
df['d'] = pd.Series(d, index=df.index)
#print (df['Long'],df['Lat'],df['d'])
if greater_than:
df1=df[df['d'] > ftr_km]
t2=time.time()
print(t2-t1)
df1.to_excel(rr+'\\{}.xlsx'.format(orig), index=False)
else:
df1=df[df['d'] < ftr_km]
t2=time.time()
print(t2-t1)
draw_map(df.loc[orig, 'Lat'],df.loc[orig, 'Long'], df['Lat'].values, df['Long'].values,df1['Lat'].values, df1['Long'].values,
marker1 = 'o', color1='b', marker2 = '*', color2='y', marker3 = '*', color3='g')
df1.to_excel(rr+'\\{}.xlsx'.format(orig), index=False)
else:
df1=df.copy(deep=True)
df1['d'] = pd.Series(d, index=df.index)
#print (df1['Long'],df1['Lat'],df1['d'])
t2=time.time()
print(t2-t1)
df1.to_excel(rr+'\\{}.xlsx'.format(orig), index=False)
else:
print(df.loc[orig, 'Lat'])
d = get_distance(df.loc[orig, 'Lat'], df.loc[orig, 'Long'],df.loc[dest, 'Lat'], df.loc[dest, 'Long'])
t2=time.time()
print(t2-t1)
Now I want to add a cone shaped wedge at a certain angle taking the given point as centre point upto same km radius that I gave inside the function.
If I execute dist('POINTA', ftr_km=3), actual result that I get from the above code is
But I want to have a cone shaped wedge starting from blue point, at a certain angle, like
I am new to matplotlib & could find some way todo till this point and hereafter I am stuck to add wedge to my existing code. Can someone help me how to do this.
Or may be can anyone provide me solution for getting all the cells highlighting under wedge area from the input file based on lat long?