Assignment

midterm_irma.py

import turtle

defirma_setup():

“””Creates the Turtle and the Screen with the map background

and coordinate system set to match latitude and longitude.

:return: a tuple containing the Turtle and the Screen

DO NOT CHANGE THE CODE IN THIS FUNCTION!

“””

importtkinter

turtle.setup(1025, 600)  # set size of window to size of map

wn = turtle.Screen()

wn.title(“Hurricane Irma”)

# kludge to get the map shown as a background image,

# since wn.bgpic does not allow you to position the image

canvas = wn.getcanvas()

turtle.setworldcoordinates(-110, 0, 0, 50)  # set the coordinate system to match lat/long

map_bg_img = tkinter.PhotoImage(file=”atlantic-hurricane-tracking-map.gif”)

# additional kludge for positioning the background image

# whensetworldcoordinates is used

# for some reason I have to move it 15 less than than 1025 width

canvas.create_image(-1010, -600, anchor=tkinter.NW, image=map_bg_img)

t = turtle.Turtle()

wn.register_shape(“hurricane.gif”)

t.shape(“hurricane.gif”)

return (t, wn, map_bg_img)

defirma():

“””Animates the path of hurricane Irma

“””

(t, wn, map_bg_img) = irma_setup()

# your code to animate Irma goes here

# BEFORE the call to wn.exitonclick()

wn.exitonclick()

if __name__ == “__main__”:

irma()

Solution

 midterm_irma.py

 import turtle

defirma_setup():

“””Creates the Turtle and the Screen with the map background

and coordinate system set to match latitude and longitude.

:return: a tuple containing the Turtle and the Screen

DO NOT CHANGE THE CODE IN THIS FUNCTION!

“””

importtkinter

turtle.setup(1025, 600)  # set size of window to size of map

wn = turtle.Screen()

wn.title(“Hurricane Irma”)

# kludge to get the map shown as a background image,

# since wn.bgpic does not allow you to position the image

canvas = wn.getcanvas()

turtle.setworldcoordinates(-110, 0, 0, 50)  # set the coordinate system to match lat/long

map_bg_img = tkinter.PhotoImage(file=”atlantic-hurricane-tracking-map.gif”)

# additional kludge for positioning the background image

# whensetworldcoordinates is used

# for some reason I have to move it 15 less than than 1025 width

canvas.create_image(-1010, -600, anchor=tkinter.NW, image=map_bg_img)

t = turtle.Turtle()

wn.register_shape(“hurricane.gif”)

t.shape(“hurricane.gif”)

return (t, wn, map_bg_img)

def category(w):

“”” calculate the category of the wind and return the color.

reference: http://www.nhc.noaa.gov/aboutsshws.php

“””

if w >= 157:

return (5, ‘red’)

if w >= 130:

return (4, ‘orange’)

if w >= 111:

return (3, ‘yellow’)

if w >= 96:

return (2, ‘green’)

if w >= 74:

return (1, ‘blue’)

return (0, ‘black’) # no hurricane strength

def draw(t, s):

“”” draw the hurricane turtle at the given location “””

# set the color according to its category

cat, color = category(s[2])

t.pencolor(color)

t.pensize((cat + 1) * 2)

t.setpos(s[0], s[1])

# only write text when it has a category strength

if cat > 0:

t.write(‘{}’.format(cat))

defirma():

“””Animates the path of hurricane Irma

“””

(t, wn, map_bg_img) = irma_setup()

# your code to animate Irma goes here

# BEFORE the call to wn.exitonclick()

# load the location and wind from csv

status = []

importcsv

with open(‘irma.csv’, ‘r’) as data:

reader = csv.reader(data)

# get the column number of (Lat, Lon, Wind)

header = next(reader)

for i in range(len(header)):

if header[i] == ‘Lat’:

lat = i

if header[i] == ‘Lon’:

lon = i

if header[i] == ‘Wind’:

wind = i

# collect the data into the list

for line in reader:

status.append((float(line[lon]), float(line[lat]), float(line[wind])))

# now draw the track of hurricane

iflen(status) > 0:

t.up() # move to the first location without showing line and turtle

t.ht()

draw(t, status[0])

t.st()

t.down()

# draw the track of hurricane

for i in range(1, len(status)):

draw(t, status[i])

t.ht()

wn.exitonclick()

if __name__ == “__main__”:

irma()