# map.py # # This file creates a Map class. # Each Map is made up of a number of states. # Each state has a capital # Thepurpose of creating these abstractions i sto be able to draw # 2-D maps and visualize state-related data by coloring each state. # # Each state is represented as a set of polygons. # Each polygon is a sequence of x,y coordinates # This representation allows for disjoint landmasses that # constitute a state. # # The Map class is designed so that it is capable of providing # a general interface for maps of any country. # # Simple graphics primitives are used to represent the polygons. # from myro import * from random import * # The State class class State: def __init__(self, name, st, cap, capPt, stPoly): """Given a name and data about a state, create a state object.""" self.name = name self.abbrev = st self.capital = (cap, capPt) self.boundary = stPoly self.color = 'white' def __eq__(self, other): return self.name == other.name def getName(self): return self.name def getAbbrev(self): return self.abbrev def getCapital(self): return self.capital def getBoundary(self): return self.boundary def drawState(self, win, color = 'white'): """Draw the state's boundary in the window with color.""" self.color = color self.boundary.setFill(color) self.boundary.draw(win) def colorState(self, color): self.color = color self.boundary.setFill(color) # The Map class... class Map: """Represents the geometric map of a country.""" def __init__(self, name = "US"): """Creates a map data structure. """ # attributes self.name = name self.width = 1500 self.height = 1000 # structure/components... # A map is a dictionary with states as keys # a tuple of state data as values self.data = self.readMap(name+"MapData.txt") # Accessing properties... def getWidth(self): """Returns the horizontal width (in pixels) of the map.""" return self.width def getHeight(self): """Returns the height (in pixels) of the map.""" return self.height def getSize(self): """Returns the size (width, height) in pixels.""" return (self.width, self.height) def getName(self): """Returns the name of the map.""" return self.name def getStates(self): """Returns a list of all the states in the map.""" return self.data.keys() def hasState(self, state): """Checks to see if the map has the given state.""" return self.data.has_key(state) def getState(self, state): """Returns all the state data for the given state.""" return self.data[state] def drawMap(self, window, drCap = 0, drState = 0): # draw the map for state, data in self.data.items(): #st, cap, capPoint, poly = data poly = data.getBoundary() poly.draw(window) # see if state capitals need to be drawn (drCap = 1) # see if name of capitals needs to be drawn (drCap = 2) # see if names of state/abbrev def readMap(self, mapFile): F = open(mapFile) Fcontents = F.read().split('\n') data = {} for i in range(0,len(Fcontents)-1,2): # Extract the state, abbrev, capital, and x,y of capital state, st, capital, capX, capY = Fcontents[i].split(',') capPoint = Point(int(capX), int(capY)) # Extract polygon coordinates from the next line coords = Fcontents[i+1].split(',') # translate them into points # points is a list of points that make up the state's polygon points = [] for j in range(0, len(coords), 2): points.append(Point(int(coords[j]), int(coords[j+1]))) statePoly = Polygon(points) # Stuff that uses the State class... newState = State(state, st, capital, capPoint, statePoly) data[state] = newState #data[state] = (st, capital, capPoint, statePoly) F.close() return data def demo(): """Demo the US Map.""" colors = ["yellow", "magenta", "lightBlue", "lightGreen"] us = Map() print "The map of US has been created." print "It has", len(us.getStates()), "states." if askQuestion("wanna see the map?") == "Yes": win = GraphWin(us.getName(), 1000, 750) win.setCoords(0, us.getHeight()-1, us.getWidth()-1, 0) win.setBackground("white") #us.drawMap(win) for state in us.getStates(): us.getState(state).drawState(win, choice(colors)) print "Good Bye!" if __name__ == '__main__': demo()