Skip to content

Assignment 4 #19

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 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 71 additions & 10 deletions point_pattern.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import math # I am guessing that you will need to use the math module
import json # I would like you to use the JSON module for reading geojson (for now)
from urllib.request import urlopen

"""
Like last assignment, we are going to be working with point
patterns. The readings focused on iteration, sequences, and
Expand Down Expand Up @@ -33,7 +35,13 @@ def read_geojson(input_file):
"""
# Please use the python json module (imported above)
# to solve this one.
gj = None
# with open(input_file, 'r') as f:
# gj = json.load(f)

response = urlopen("https://api.myjson.com/bins/4587l").read().decode('utf8')
gj = json.loads(response)
print(gj)

return gj


Expand All @@ -58,10 +66,14 @@ def find_largest_city(gj):
"""
city = None
max_population = 0
for n in gj["features"]:
properties = n["properties"]
if (properties["pop_max"] > max_population):
max_population = properties["pop_max"]
city = properties["adm1name"]

return city, max_population


def write_your_own(gj):
"""
Here you will write your own code to find
Expand All @@ -74,7 +86,17 @@ def write_your_own(gj):
Do not forget to write the accompanying test in
tests.py!
"""
return
# Find coordinates from Alaska
alaska_points = []
for n in gj["features"]:
properties = n["properties"]
state_name = properties["adm1name"]
if state_name == "Alaska":
alaska_points.append(n)
else:
continue

return alaska_points

def mean_center(points):
"""
Expand All @@ -93,8 +115,17 @@ def mean_center(points):
y : float
Mean y coordinate
"""
x = None
y = None
x = 0
y = 0
number_of_points = 0

for p in points:
x += p[0]
y += p[1]
number_of_points += 1

x = x / number_of_points
y = y / number_of_points

return x, y

Expand All @@ -119,9 +150,24 @@ def average_nearest_neighbor_distance(points):
Measure of Spatial Relationships in Populations. Ecology. 35(4)
p. 445-453.
"""
mean_d = 0

return mean_d
mean_d = 0
temp_p = None
for p in points:
for q in points:
if check_coincident(p, q):
continue
cached = euclidean_distance(p, q)
if temp_p is None:
temp_p = cached
elif temp_p > cached:
temp_p = cached

mean_d += temp_p
temp_p = None

return mean_d / len(points)



def minimum_bounding_rectangle(points):
Expand All @@ -139,7 +185,22 @@ def minimum_bounding_rectangle(points):
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

mbr = [0,0,0,0]
min_x = 1000000000
min_y = 1000000000
max_x = -1
max_y = -1

for n in points:
if n[0] < min_x:
min_x = n[0]
if n[0] > max_x:
max_x = n[0]
if n[1] < min_y:
min_y = n[1]
if n[1] > max_y:
max_y = n[1]

mbr = [min_x, min_y, max_x, max_y]

return mbr

Expand All @@ -148,7 +209,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

Expand All @@ -173,7 +234,7 @@ def expected_distance(area, n):
The number of points
"""

expected = 0
expected = (0.5 * (math.sqrt(area/n)))
return expected


Expand Down
10 changes: 9 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TestFilesAndDicts(unittest.TestCase):

@classmethod
def setUpClass(cls):
# REMINDER: Trying to finish my tests but the computer I'm on now can't find this file for some reason
# Why is that...
cls.gj = point_pattern.read_geojson('data/us_cities.geojson')

def test_read_geojson(self):
Expand All @@ -33,7 +35,7 @@ def test_write_your_own(self):
point_pattern.py.
"""
some_return = point_pattern.write_your_own(self.gj)
self.assertTrue(False)
self.assertEqual(some_return[0]["properties"]["adm1name"],"Alaska")

class TestIterablePointPattern(unittest.TestCase):
"""
Expand Down Expand Up @@ -216,3 +218,9 @@ def test_check_in(self):

inlist = point_pattern.check_in((6,4), point_list)
self.assertFalse(inlist)


def test_alaska_points(self):
gj = point_pattern.read_geojson('data/us_cities.geojson')
alaska_points = point_pattern.write_your_own(gj)
self.assertTrue(alaska_points[0]["properties"]["adm1name"] == 'Alaska')