from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

"""
Explanation: Basemap
Let's see the world from the eyes of Python
First: import the libraries
End of explanation
"""


my_map = Basemap(projection='ortho', lat_0=50, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()


"""
Explanation: Now to use the Basemap function to create a map with a set projection define the centre point using lat/lon, resolution: l=low, h=high, and area_threshold: only plots coastal feastures greater than 1000km2
End of explanation
"""


my_map = Basemap(projection='ortho', lat_0=50, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.etopo()  # bathymetry 


"""
Explanation: Now to add a little bit of detail. Draw country boundaries, fill in the continents with a colour, and add bathymetry.
End of explanation
"""


my_map = Basemap(projection='ortho', lat_0=50, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')

my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))



"""
Explanation: Having some gridlines always helps too! But let's drop the bathymetry for now.
End of explanation
"""


my_map = Basemap(projection='ortho', lat_0=0, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))


"""
Explanation: Want a different view? Try this...
Here we have set the central point to a latitude value of: 0
End of explanation
"""


my_map = Basemap(projection='robin', lat_0=0, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))

# Find more here:
# http://matplotlib.org/basemap/users/mapsetup.html 

"""
Explanation: Maybe we don't like this projection anymore. How about something else?
End of explanation
"""


my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'l', area_thresh = 1000,
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()

my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
print("Apparently Python stole it for there supersnake cave.")

"""
Explanation: Can you find the Isle of Wight?
End of explanation
"""


my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'l', area_thresh = 0.1,
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
print("I always thought people from the Isle of Wight were a little bit square...")

"""
Explanation: Maybe it is too big for our 'area threshold'
Tweak it down!
End of explanation
"""


my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
print("That's a bit better!")

"""
Explanation: Let's try and correct those squares by increasing the resolution.
End of explanation
"""


my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))

lon = -1.3126
lat = 50.7507
x,y = my_map(lon, lat)
my_map.plot(x, y, 'bo', markersize=12)


"""
Explanation: We'd like to know where Cowes is: the captial of the Isle of Wight
End of explanation
"""


my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
lons = [-1.3126, -1.1010, -1.4043]
lats = [50.7507, 50.8429, 50.9096]
x,y = my_map(lons, lats)
my_map.plot(x, y, 'bo', markersize=10)


"""
Explanation: How about Portsmouth and Southampton too?
End of explanation
"""


my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
lons = [-1.3126, -1.1010, -1.4043]
lats = [50.7507, 50.8429, 50.9096]
x,y = my_map(lons, lats)
my_map.plot(x, y, 'bo', markersize=10)

labels = ['Cowes', 'Portsmouth', 'Southampton']
for label, xpt, ypt in zip(labels, x, y):
    plt.text(xpt, ypt, label)

"""
Explanation: Labels always help
End of explanation
"""


my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
lons = [-1.3126, -1.1010, -1.4043]
lats = [50.7507, 50.8429, 50.9096]
x,y = my_map(lons, lats)
my_map.plot(x, y, 'bo', markersize=10)

labels = ['Cowes', 'Portsmouth', 'Southampton']
for label, xpt, ypt in zip(labels, x, y):
    plt.text(xpt-7000, ypt+3000, label)

"""
Explanation: Now to offset the labels slightly
End of explanation
"""


# source: http://introtopython.org/visualization_earthquakes.html

"""
Explanation: Can you create your own map and zoom into Norfolk, label Norwich, Lowestoft and Sherringham.
End of explanation
"""


