
import numpy as np
import geopy
from StringIO import StringIO
from matplotlib import image as img
from matplotlib import pyplot as plt
import requests
from map import Map

class Greengraph(object):
    def __init__(self, start, end):
        self.start=start
        self.end=end
        self.geocoder=geopy.geocoders.GoogleV3(domain="maps.google.co.uk")

    def geolocate(self, place):
        return self.geocoder.geocode(place, exactly_one=False)[0][1]

    def location_sequence(self, start,end,steps):
      lats = np.linspace(start[0], end[0], steps)
      longs = np.linspace(start[1],end[1], steps)
      return np.vstack([lats, longs]).transpose()

    def green_between(self, steps):
        return [Map(*location).count_green()
                for location in self.location_sequence(
                    self.geolocate(self.start),
                    self.geolocate(self.end),
                    steps)]

def grapher(beginning, end, steps = 10, out = False):
    data = Greengraph(beginning, end)
    graph = data.green_between(steps)
    plt.plot(graph)
    if out == False:
        plt.show()
    else:
        plt.savefig(out)
