Skip to content

assignment_04 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
120 changes: 70 additions & 50 deletions point_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,23 +16,24 @@
"""



def read_geojson(input_file):
"""
Read a geojson file

Parameters
----------
input_file : str
The PATH to the data to be read

Returns
-------
gj : dict
An in memory version of the geojson
"""
# 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


Expand All @@ -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

Expand All @@ -66,89 +70,130 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using math.inf for a really large number?

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


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of continue. Check out enumerate() as well. This logic works this week, but fails if the points are coincident. Since you know that the lists are in the same order, you could check to see if the positional indices are the same.

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


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
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

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


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

Expand All @@ -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


Expand All @@ -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
Expand All @@ -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
"""
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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]
"""
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down