Skip to content

late #20

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 7 commits into
base: master
Choose a base branch
from
Open

late #20

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
65 changes: 55 additions & 10 deletions point_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ 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 file:
gj = json.load(file)
print(gj)
return gj


Expand All @@ -56,8 +58,13 @@ def find_largest_city(gj):
population : int
The population of the largest city
"""
city = None
FeaturesGJ = gj['features']
max_population = 0
for featureEntry in FeaturesGJ:
if featureEntry["properties"]["pop_max"] > max_population:
max_population = featureEntry["properties"]["pop_max"]
city = featureEntry["properties"]["nameascii"]


return city, max_population

Expand All @@ -74,7 +81,13 @@ def write_your_own(gj):
Do not forget to write the accompanying test in
tests.py!
"""
return
location = gj['features']
unitedstates = 0

for i in location:
if i['properties']['adm0name'] == 'United States of America':
unitedstates += 1
return unitedstates

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

for point in points:
x += point[0]
y += point[1]
x = x / len(points)
y = y / len(points)

return x, y

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

nearest = []
for i, point in enumerate(points):
nearest.append(None)
for point2 in points:
if point is not point2:
dist = euclidean_distance(point, point2)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the two points are coincident? Check out enumerate to track the position (index) in both lists. if the index is equal, then the points are identical. Otherwise, they might just be coincident.

if nearest[i] == None:
nearest[i] = dist
elif nearest[i] > dist:
nearest[i] = dist

mean_d = sum(nearest) / len(points)
return mean_d


def minimum_bounding_rectangle(points):
"""
Given a set of points, compute the minimum bounding rectangle.
Expand All @@ -139,7 +167,24 @@ def minimum_bounding_rectangle(points):
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

i = True
mbr = [0,0,0,0]
for point in points:
if i:
i = False
mbr[0] = point[0]
mbr[1] = point[1]
mbr[2] = point[0]
mbr[3] = point[1]

if point[0] < mbr[0]:
mbr[0] = point[0]
if point[1] < mbr[1]:
mbr[1] = point[1]
if point[0] > mbr[2]:
mbr[2] = point[0]
if point[1] > mbr[3]:
mbr[3] = point[1]

return mbr

Expand All @@ -148,7 +193,7 @@ def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""
area = 0
area = (mbr[1] - mbr[3]) * (mbr[0] - mbr[2])

return area

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

expected = 0
expected = 0.5 * (area / n) ** 0.5
return expected


Expand Down
2 changes: 1 addition & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_write_your_own(self):
point_pattern.py.
"""
some_return = point_pattern.write_your_own(self.gj)
self.assertTrue(False)
self.assertTrue(some_return, 70)

class TestIterablePointPattern(unittest.TestCase):
"""
Expand Down