Esempio n. 1
0
def test_request_call():
    with patch.object(requests, 'get') as mock_get:
        with patch.object(img, 'imread') as mock_img: # Ensure Map class does not try and create pixel map from mock
            sample_map = Greengraph(sample_start, sample_end, sample_delay)
            test_place = Map(*sample_map.geolocate(sample_start))
            print mock_get.mock_calls
            mock_get.assert_called_with('http://maps.googleapis.com/maps/api/staticmap?',params =  url)
Esempio n. 2
0
def process():
    parser = ArgumentParser(
        description="Produce graph quantifying the amount of green land between two locations")
    parser.add_argument("--start", nargs="+",
                        help="The starting location, defaults to London ")
    parser.add_argument("--end",  nargs="+",
                        help="The ending location, defaults to Durham")
    parser.add_argument("--steps", type=int,
                        help="An integer number of steps between the starting and ending locations, defaults to 10")
    parser.add_argument("--out",
                        help="The output filename file, which will be saved as file.png. This defaults to graph.png")
    arguments = parser.parse_args()

    if arguments.start and arguments.end:
        mygraph = Greengraph(arguments.start, arguments.end)
    else:
        mygraph = Greengraph("London", "Durham")
    if arguments.steps:
        data = mygraph.green_between(arguments.steps)
    else:
        data = mygraph.green_between(10)
    plt.plot(data)
    plt.xlabel("Step")
    plt.ylabel("Number of green pixels (Max 160000)")
    if arguments.start and arguments.end:
        plt.title("Graph of green land between " +
                  " ".join(arguments.start) + " and " + " ".join(arguments.end))
    else:
        plt.title("Graph of green land between London and Durham")
    if arguments.out:
        filename = arguments.out + ".png"
        plt.savefig(filename)
    else:
        plt.savefig("graph.png")
Esempio n. 3
0
def plotGraph(start, end, steps, out=None):
    """ Generate a greeting string for a person.
    Parameters
    ----------
    start: str
    Start city.
    end: str
    End city.
    steps: int
    Gives number of images to process between start and end.
    out: str
    Specifies output file for graph. If not given then graph will be displayed. 
    Returns
    -------
    Nothing
    """

    mygraph=Greengraph(start, end)
    data = mygraph.green_between(steps)
    plt.plot(data)
    plt.xlabel("steps")
    plt.ylabel("proportion of green pixels")
    plt.title("Greengraph from "+start+" to "+end)

    if out:
        plt.savefig(out)
    else:
        plt.show()
Esempio n. 4
0
def greengraph(fromLocation, toLocation, steps):
    greenGraphObject = Greengraph(arguments.fromLocation, arguments.toLocation)
    #print(greenGraphObject.geolocate(arguments.fromLocation))
    #print(arguments.fromLocation)
    #greenGraphObject = Greengraph(arguments)
    data = greenGraphObject.green_between(arguments.steps)
    return data
Esempio n. 5
0
def test_API_overload():
    test_list = [5000]*20 # Create test array
    test_list[2] = 323 # Create two points which contain the critical value
    test_list[15] = 323
    with patch.object(Greengraph, 'green_between') as mock:
        mock.return_value = test_list
        test_map = Greengraph(sample_start, sample_end, sample_delay)
        assert (test_map.api_overload(sample_steps))==True
Esempio n. 6
0
def test_API_overload():
    test_list = [5000] * 20  # Create test array
    test_list[2] = 323  # Create two points which contain the critical value
    test_list[15] = 323
    with patch.object(Greengraph, 'green_between') as mock:
        mock.return_value = test_list
        test_map = Greengraph(sample_start, sample_end, sample_delay)
        assert (test_map.api_overload(sample_steps)) == True
Esempio n. 7
0
def test_request_call():
    with patch.object(requests, 'get') as mock_get:
        with patch.object(
                img, 'imread'
        ) as mock_img:  # Ensure Map class does not try and create pixel map from mock
            sample_map = Greengraph(sample_start, sample_end, sample_delay)
            test_place = Map(*sample_map.geolocate(sample_start))
            print mock_get.mock_calls
            mock_get.assert_called_with(
                'http://maps.googleapis.com/maps/api/staticmap?', params=url)
Esempio n. 8
0
def green_plotter(arguments):
    this_graph = Greengraph(arguments.first_location, arguments.second_location)# create an instance of the Greengraph class object.
    green_count = this_graph.green_between(arguments.steps)
    plt.plot(green_count)
    
    plt.title('Number of green pixels in locations between ' + arguments.first_location + ' and ' + arguments.second_location) 
    plt.xlabel('Steps')
    plt.ylabel('Green pixels')
    
    plt.savefig(arguments.output + '.png')
    plt.show()
Esempio n. 9
0
def chart(start, end, steps, out):
   this_chart = Greengraph(start,end)
   this_data = this_chart.green_between(steps)
   this_plot.plot(this_data)
   #Add Annotations
   this_plot.xlabel("Steps")
   this_plot.ylabel("'Green Pixel Density'")
   this_plot.title("Green Land Density: " + start + " - " + end)
   #Save result and show user
   this_plot.savefig(out)
   this_plot.show()
Esempio n. 10
0
def main():
    parser = ArgumentParser(description = "Evaluate how 'green' the path between two locations is")
    parser.add_argument('--from', '-f', help = 'Start location', dest='fromLoc')
    parser.add_argument('--to', '-t', help = 'End location', dest='toLoc')
    parser.add_argument('--steps', '-s', type = int, help = 'How many steps to evaluate', default = 20)
    parser.add_argument('--out', '-o', help = 'Output file name. Default file type: PNG', default = 'output.png')
    arguments = parser.parse_args()

    mygraph=Greengraph(arguments.fromLoc,arguments.toLoc)
    data = mygraph.green_between(arguments.steps)
    plt.plot(data)
    plt.savefig(arguments.out)
Esempio n. 11
0
def plotGreenDistribution(startPos, endPos, steps, outFile):
    '''
    Generate a plot with the distribution of green pixels between two locations.

    :param startPos: A starting location, like 'London'
    :type startPos: string
    :param endPos: An ending location, like 'Cambdrige'
    :type endPos: string
    :param steps: A number of steps to discretise the space between the two locations, like 15
    :type steps: int
    :param outFile: A filename to be used for saving the plot, like 'outGraph.png'
    :type outFile: string

    :returns: A file with the graph representing the distribution of green pixels.

    '''

    # make sure the input parameters are valid
    if not isinstance(steps,int):
        raise ValueError("The steps value: "+ str(steps)+" is not an integer")
    if not isinstance(startPos,str):
        raise ValueError("The start position: "+ str(startPos)+" is not a string")
    if not isinstance(endPos,str):
        raise ValueError("The end position: "+ str(endPos)+" is not a string")
    if not isinstance(outFile,str):
        raise ValueError("The output filename: "+ str(outFile)+" is not a string")

    if steps<=0:
        raise ValueError("The steps value: "+ str(steps)+" is non-positive")

    if len(startPos)==0:
        raise ValueError("The starting position: "+ startPos+" is empty")
    if len(endPos)==0:
        raise ValueError("The ending position: "+ endPos+" is empty")
    if len(outFile)==0:
        raise ValueError("The output filename: "+ outFile+" is empty")

    if startPos == endPos:
        raise ValueError("The start and the end position are the same!")

    myGraph=Greengraph(startPos,endPos)
    data=myGraph.green_between(steps)
    titleString='Proportion of green pixels between ' + startPos+ ' and ' + endPos
    plt.plot(data)
    plt.xlabel('Distance steps')
    plt.ylabel('Number of green pixels')
    plt.title(titleString)
    plt.savefig(outFile)
Esempio n. 12
0
def runGreengraph():
    parser = ArgumentParser(description = "Generates a graph of the proportion of green pixels in a series of satellite images between two points")
    parser.add_argument('--from', dest='start', default='New York', help='Starting location, defaults to New York') # Python doesn't like 'arguments.from'
    parser.add_argument('--to', dest='end', default='Chicago', help='End location, defaults to Chicago')
    parser.add_argument('--steps', default=20, help='Number of steps between start and end locations, default 20')
    parser.add_argument('--out', help='Filename to save plot to. Displays plot on screen instead if not specified')
    parser.add_argument('--format', default=None, help='Optionally force an output format, overriding extension in --out')
    arguments=parser.parse_args()
    
    mygraph=Greengraph(arguments.start,arguments.end) 
    data = mygraph.green_between(arguments.steps)
    plt.plot(data)
    
    if arguments.out:
        plt.savefig(arguments.out,format=arguments.format)
    else:
        plt.show()
Esempio n. 13
0
def process():
    parser = ArgumentParser(description = 'Plot the propotion of green space between two locations')
    
    parser.add_argument('--start','-s',type=str,required=True,
        help='The starting location')
    parser.add_argument('--end','-e',type=str,required=True,
        help='Then end location')
    parser.add_argument('--steps','-n',default=10,type=int,
        help='Number of satalite images to inspect between start and end locations - default = 10')
    parser.add_argument('--out','-o',default='green_between.png',
        help='Name of the .png output file the calculated graph will be written to -  default = green_between.png')

    arguments = parser.parse_args()

    mygraph = Greengraph(arguments.start,arguments.end)
    data = mygraph.green_between(arguments.steps)
    plt.plot(data)
    plt.savefig(arguments.out)
Esempio n. 14
0
def process():
    parser = ArgumentParser(
        description="Generate graph of proportion of green pixel in a series of satellite images between two points:"
    )

    parser.add_argument("--from", "-f", dest="fromCity")
    parser.add_argument("--to")
    parser.add_argument("--steps")
    parser.add_argument("--out")
    arguments = parser.parse_args()

    print(arguments.to, arguments.fromCity, arguments.steps, arguments.out)

    mygraph = Greengraph(arguments.fromCity, arguments.to)
    data = mygraph.green_between(arguments.steps)

    plt.plot(data)
    plt.savefig(arguments.out)
Esempio n. 15
0
def process():
    #get_ipython().magic('matplotlib inline')
    parser = ArgumentParser(description="Generate appropriate green counts")
    #parser.add_argument('--title', '-t')
    #parser.add_argument('--polite', '-p', action="store_true")
    parser.add_argument('fromLocation')
    parser.add_argument('toLocation')
    parser.add_argument('steps')

    arguments = parser.parse_args()
    print(arguments.fromLocation)
    greenGraphObject = Greengraph(arguments.fromLocation, arguments.toLocation)
    #print(greenGraphObject.geolocate(arguments.fromLocation))
    #print(arguments.fromLocation)
    #greenGraphObject = Greengraph(arguments)
    data = greenGraphObject.green_between(arguments.steps)
    print(data)
    #print(Greengraph(arguments.fromLocation,arguments.toLocation))
    plt.show(data)
Esempio n. 16
0
def greengraph_parser():
    parser = argparse.ArgumentParser(description = "Finds the number of green pixels between two points.")
    parser.add_argument('--from', dest='start', default='New York', help='Starting point')
    parser.add_argument('--to', dest='end', default='Chicago', help='Ending point')
    parser.add_argument('--steps', default='20', help='Steps between points')
    parser.add_argument('--out', help='File to save to, else display plot')
    
    args=parser.parse_args()
    
    mygraph=Greengraph(args.start, args.end)
    data = mygraph.green_between(args.steps)
    plt.plot(data)
    plt.title("Green pixels from "+args.start+" to "+args.end)
    plt.xlabel("Number of steps")
    plt.ylabel("Amount of green pixels")
    
    if args.out:
        plt.savefig(args.out)
        plt.show()
    else:
        plt.show()
Esempio n. 17
0
def test_geolocate():
    with patch.object(geocoders, 'GoogleV3') as mock_get:
        mymap = Greengraph(sample_start, sample_end, sample_delay)
        mymap.geolocate(sample_start)
        print mock_get.mock_calls
        mock_get.assert_called_with(domain='maps.google.co.uk')
Esempio n. 18
0
def test_negative_steps():
    with assert_raises(ValueError) as exception:
        mygraph = Greengraph(sample_start, sample_end, sample_delay)
        data = mygraph.green_between(bad_sample_steps)
Esempio n. 19
0
def test_bad_image_extension():
    with assert_raises(ValueError) as exception:
        mygraph = Greengraph(sample_start, sample_end, sample_delay)
        data = mygraph.green_between(sample_steps)
        plt.savefig(bad_sample_filename)
Esempio n. 20
0
def test_negative_steps():
    with assert_raises(ValueError) as exception:
        mygraph = Greengraph(sample_start, sample_end, sample_delay)
        data = mygraph.green_between(bad_sample_steps)
Esempio n. 21
0
def test_negative_delay_input():
    with assert_raises(ValueError) as exception:
        Greengraph(sample_start, sample_end, bad_sample_delay)
Esempio n. 22
0
from map import Map
import yaml

# Import fixtures from file and dump into fixtures
yml_filename = os.path.join(os.path.join(os.path.dirname(__file__), 'fixtures/fixtures.yml'))
with open(yml_filename) as f:
    fixtures = yaml.load(f)


sample_start = fixtures[0]['sample_start']
sample_end = fixtures[0]['sample_end']
sample_steps = int(fixtures[0]['sample_steps']) # Make sure is integer
sample_filename = fixtures[0]['sample_filename']
sample_delay = int(fixtures[0]['sample_delay']) # Make sure is integer

some_map = Greengraph(sample_start, sample_end, sample_delay)
img_start = Image.open(StringIO(some_map.show_map(sample_start)))
img_end = Image.open(StringIO(some_map.show_map(sample_end)))
img_start.show()
img_end.show()

# Show green pixel maps

img_green_start = Image.open(StringIO(some_map.show_green_map(sample_start)))
img_green_end = Image.open(StringIO(some_map.show_green_map(sample_end)))
img_green_start.show()
img_green_end.show()



Esempio n. 23
0
def test_bad_end():
    with assert_raises(TypeError) as exception:
        mymap = Greengraph(sample_start, bad_sample_end, sample_delay)
        test = mymap.green_between(sample_steps)
Esempio n. 24
0
def test_bad_end():
    with assert_raises(TypeError) as exception:
        mymap = Greengraph(sample_start, bad_sample_end, sample_delay)
        test = mymap.green_between(sample_steps)
Esempio n. 25
0
    parser.add_argument('--steps',
                        '-s',
                        type=int,
                        help='Number of steps, default = 20')
    parser.add_argument('--plot',
                        '-p',
                        action='store_true',
                        help='Display plot')
    parser.add_argument('--delay',
                        '-d',
                        type=int,
                        help='API request delay in seconds')

    arguments = parser.parse_args()

    mygraph = Greengraph(arguments.start, arguments.end,
                         arguments.delay)  # Create Greengraph object
    data = mygraph.green_between(arguments.steps)

    # Catch API overload error
    # Google sends error picture with 325 green pixels.
    # This method should be in one of the classes
    if mygraph.api_overload(arguments.steps):
        print 'Warning: API overload'

    # Saving and plotting data
    fig = plt.figure()
    plt.plot(data)
    fig.suptitle('Greengraph - %s to %s' % (arguments.start, arguments.end),
                 fontsize=14,
                 fontweight='bold')
    ax = fig.add_subplot(111)
Esempio n. 26
0
# Create parser arguments
if __name__ == "__main__":
    parser = ArgumentParser(description = "Count the green space between two locations")
    parser.add_argument('start', help = 'Input start location')
    parser.add_argument('end', help = 'Input end location')
    parser.add_argument('filename', type=str, help ='image filename with extension')
    parser.add_argument('--steps', '-s', type=int, help = 'Number of steps, default = 20')
    parser.add_argument('--plot', '-p', action ='store_true', help ='Display plot')
    parser.add_argument('--delay', '-d', type=int, help = 'API request delay in seconds')

    arguments = parser.parse_args()



    mygraph = Greengraph(arguments.start, arguments.end, arguments.delay) # Create Greengraph object
    data = mygraph.green_between(arguments.steps)

    # Catch API overload error
    # Google sends error picture with 325 green pixels.
    # This method should be in one of the classes
    if mygraph.api_overload(arguments.steps):
        print 'Warning: API overload'


    # Saving and plotting data
    fig = plt.figure()
    plt.plot(data)
    fig.suptitle('Greengraph - %s to %s' %(arguments.start, arguments.end), fontsize=14, fontweight='bold')
    ax = fig.add_subplot(111)
    ax.set_xlabel('steps')
Esempio n. 27
0
def test_geolocate():
    with patch.object(geocoders, 'GoogleV3') as mock_get:
        mymap = Greengraph(sample_start, sample_end, sample_delay)
        mymap.geolocate(sample_start)
        print mock_get.mock_calls
        mock_get.assert_called_with(domain='maps.google.co.uk')
Esempio n. 28
0
    params=dict(
        sensor= str(sensor).lower(),
        zoom= zoom,
        size= "x".join(map(str, size)),
        center= ",".join(map(str, (lat, long) )),
        style="feature:all|element:labels|visibility:off"
     )
            
    if satellite: params["maptype"]="satellite"
    


    image = requests.get(base, params=params).content

test_values()

lls=Greengraph.location_sequence(Greengraph("",""), (40.7127,-74.0059),(41.8781, -87.6297),20)
print(lls)[0])
print(lls[1])

[ 40.7127 -74.0059]
[ 40.77403684 -74.72294211]








Esempio n. 29
0
def test_bad_image_extension():
    with assert_raises(ValueError) as exception:
        mygraph = Greengraph(sample_start, sample_end, sample_delay)
        data = mygraph.green_between(sample_steps)
        plt.savefig(bad_sample_filename)