diff --git a/point_pattern.py b/point_pattern.py index 1b0a5cb..533a70b 100644 --- a/point_pattern.py +++ b/point_pattern.py @@ -5,7 +5,6 @@ patterns. The readings focused on iteration, sequences, and conditional execution. We are going to use these concepts to write functions to: - 1. Read a geojson file 2. Parse a geojson file to find the largest city by population 3. Write your own code to do something interesting with the geojson @@ -17,15 +16,14 @@ """ + def read_geojson(input_file): """ Read a geojson file - Parameters ---------- input_file : str The PATH to the data to be read - Returns ------- gj : dict @@ -33,7 +31,9 @@ def read_geojson(input_file): """ # Please use the python json module (imported above) # to solve this one. - gj = None + with open ('data/us_cities.geojson', 'r') as f: + gj = json.load(f) + return gj @@ -42,22 +42,26 @@ def find_largest_city(gj): Iterate through a geojson feature collection and find the largest city. Assume that the key to access the maximum population is 'pop_max'. - Parameters ---------- gj : dict A GeoJSON file read in as a Python dictionary - Returns ------- city : str The largest city - population : int The population of the largest city """ - city = None + + temp = 0 + city = "" max_population = 0 + for i in gj['features']: + if i['properties']['pop_max'] > temp: + temp = i['properties']['pop_max'] + city = i['properties']['name'] + max_population = temp return city, max_population @@ -66,35 +70,46 @@ def write_your_own(gj): """ Here you will write your own code to find some attribute in the supplied geojson file. - Take a look at the attributes available and pick something interesting that you might like to find or summarize. This is totally up to you. - Do not forget to write the accompanying test in tests.py! """ - return + temp = 99999999999 + city = "" + min_population = 0 + for i in gj['features']: + if i['properties']['pop_min'] < temp: + temp = i['properties']['pop_min'] + city = i['properties']['name'] + min_population = temp + + return city, min_population def mean_center(points): """ Given a set of points, compute the mean center - Parameters ---------- points : list A list of points in the form (x,y) - Returns ------- x : float Mean x coordinate - y : float Mean y coordinate """ - x = None - y = None + + summation_x = 0 + summation_y = 0 + for i in points: + summation_x += i[0] + summation_y += i[1] + + x = float(summation_x / len(points)) + y = float(summation_y / len(points)) return x, y @@ -102,24 +117,41 @@ def mean_center(points): def average_nearest_neighbor_distance(points): """ Given a set of points, compute the average nearest neighbor. - Parameters ---------- points : list A list of points in the form (x,y) - Returns ------- mean_d : float Average nearest neighbor distance - References ---------- Clark and Evan (1954 Distance to Nearest Neighbor as a Measure of Spatial Relationships in Populations. Ecology. 35(4) p. 445-453. """ - mean_d = 0 + sum_nn_dis = 0 + + for point_1 in points: + first = True + for point_2 in points: + if point_1 == point_2: + continue + else: + distance = euclidean_distance(point_1, point_2) + if first: + nn_dis = distance + first = False + elif distance < nn_dis: + nn_dis = distance + + sum_nn_dis += nn_dis + + + + + mean_d = sum_nn_dis / len(points) return mean_d @@ -127,12 +159,10 @@ def average_nearest_neighbor_distance(points): def minimum_bounding_rectangle(points): """ Given a set of points, compute the minimum bounding rectangle. - Parameters ---------- points : list A list of points in the form (x,y) - Returns ------- : list @@ -140,7 +170,22 @@ def minimum_bounding_rectangle(points): """ mbr = [0,0,0,0] - + xmin = 999 + ymin = 999 + xmax = 0 + ymax = 0 + for i in points: + if i[0] < xmin: + xmin = i[0] + if i[0] > xmax: + xmax = i[0] + if i[1] < ymin: + ymin = i[1] + if i[1] > ymax: + ymax = i[1] + + mbr = [xmin, ymin, xmax, ymax] + return mbr @@ -148,7 +193,7 @@ def mbr_area(mbr): """ Compute the area of a minimum bounding rectangle """ - area = 0 + area = (mbr[2] - mbr[0]) * (mbr[3] - mbr[1]) return area @@ -157,23 +202,20 @@ def expected_distance(area, n): """ Compute the expected mean distance given some study area. - This makes lots of assumptions and is not necessarily how you would want to compute this. This is just an example of the full analysis pipe, e.g. compute the mean distance and the expected mean distance. - Parameters ---------- area : float The area of the study area - n : int The number of points """ - expected = 0 + expected = .5 / ((math.sqrt(n/area))) return expected @@ -188,15 +230,12 @@ def expected_distance(area, n): def manhattan_distance(a, b): """ Compute the Manhattan distance between two points - Parameters ---------- a : tuple A point in the form (x,y) - b : tuple A point in the form (x,y) - Returns ------- distance : float @@ -209,18 +248,14 @@ def manhattan_distance(a, b): def euclidean_distance(a, b): """ Compute the Euclidean distance between two points - Parameters ---------- a : tuple A point in the form (x,y) - b : tuple A point in the form (x,y) - Returns ------- - distance : float The Euclidean distance between the two points """ @@ -231,28 +266,21 @@ def euclidean_distance(a, b): def shift_point(point, x_shift, y_shift): """ Shift a point by some amount in the x and y directions - Parameters ---------- point : tuple in the form (x,y) - x_shift : int or float distance to shift in the x direction - y_shift : int or float distance to shift in the y direction - Returns ------- new_x : int or float shited x coordinate - new_y : int or float shifted y coordinate - Note that the new_x new_y elements are returned as a tuple - Example ------- >>> point = (0,0) @@ -275,10 +303,8 @@ def check_coincident(a, b): ---------- a : tuple A point in the form (x,y) - b : tuple A point in the form (x,y) - Returns ------- equal : bool @@ -290,12 +316,10 @@ def check_coincident(a, b): def check_in(point, point_list): """ Check whether point is in the point list - Parameters ---------- point : tuple In the form (x,y) - point_list : list in the form [point, point_1, point_2, ..., point_n] """ @@ -307,12 +331,10 @@ def getx(point): A simple method to return the x coordinate of an tuple in the form(x,y). We will look at sequences in a coming lesson. - Parameters ---------- point : tuple in the form (x,y) - Returns ------- : int or float @@ -326,12 +348,10 @@ def gety(point): A simple method to return the x coordinate of an tuple in the form(x,y). We will look at sequences in a coming lesson. - Parameters ---------- point : tuple in the form (x,y) - Returns ------- : int or float diff --git a/tests/tests.py b/tests/tests.py index 2518463..4d2fd93 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -32,8 +32,9 @@ def test_write_your_own(self): Here you will write a test for the code you write in point_pattern.py. """ - some_return = point_pattern.write_your_own(self.gj) - self.assertTrue(False) + city, pop = point_pattern.write_your_own(self.gj) + self.assertEqual(city, 'Montana') + self.assertEqual(pop, 10) class TestIterablePointPattern(unittest.TestCase): """