Created
February 23, 2018 05:03
-
-
Save sariths/1dc3cdebdfd136a2772bd0b1e132c7b3 to your computer and use it in GitHub Desktop.
Plot the unique download locations for the tutorial on the world map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from geoip import geolite2 | |
from collections import Counter | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.basemap import Basemap | |
with open(r'/home/sarith/Desktop/ips.txt') as somefile: | |
data = somefile.read().split() | |
uniqueAddresses = set(data) | |
counter =0 | |
countries = [] | |
latList = [] | |
longList = [] | |
for val in uniqueAddresses: | |
location = geolite2.lookup(val) | |
if location is not None and location.country is not None: | |
locData = str(location).split() | |
country,long,Lat = locData[2],locData[-2],locData[-1] | |
country = country.replace("country='",'').replace("'","") | |
long = float(long.replace("location=(","").replace(",",'').strip()) | |
Lat = float(Lat.replace(")>","").strip()) | |
latitude,longitude = long,Lat | |
countries.append(country) | |
latList.append(latitude) | |
longList.append(longitude) | |
print(Counter(countries)) | |
# determine range to print based on min, max lat and lon of the data | |
margin = 2 # buffer to add to the range | |
lat_min = min(latList) - margin | |
lat_max = max(latList) + margin | |
lon_min = min(longList) - margin | |
lon_max = max(longList) + 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='#add8e6') | |
m.fillcontinents(color = 'white',lake_color='#add8e6') | |
# convert lat and lon to map projection coordinates | |
lons, lats = m(longList, latList) | |
# plot points as red dots | |
m.scatter(lons, lats, marker = '.', color='r', zorder=5) | |
plt.savefig(r'/home/sarith/Desktop/downloads.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment