Skip to content
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
78 changes: 73 additions & 5 deletions point_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ 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 jFile:
gj = json.load(jFile)
return gj


Expand All @@ -56,9 +57,19 @@ def find_largest_city(gj):
population : int
The population of the largest city
"""
city = None

listOfFeatures = gj['features']

#city = None

max_population = 0

for i in listOfFeatures:
popMax = i['properties']['pop_max']
if popMax > max_population:
max_population = popMax
city = i['properties']['name']

return city, max_population


Expand All @@ -74,7 +85,16 @@ def write_your_own(gj):
Do not forget to write the accompanying test in
tests.py!
"""
return

listOfFeatures2 = gj['features']

#going through the list and print the last entry of the city, population, and coordinates
for i in listOfFeatures2:
city = i['properties']['name']
population = i['properties']['pop_max']
coordinates = i['geometry']['coordinates']

return city, population, coordinates

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

x = [i[0] for i in points]
y = [i[1] for i in points]

sumX = (sum(x) / len(points))
sumY = (sum(y) / len(points))

x = sumX
y = sumY

return x, y

Expand All @@ -121,6 +150,20 @@ def average_nearest_neighbor_distance(points):
"""
mean_d = 0

shortDistanceList = []

for firstPoint in points:
pointInList = 500
Copy link
Contributor

Choose a reason for hiding this comment

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

I would swap in math.inf here, so that you have a really big starting distance.

for secondPoint in points:
if firstPoint is not secondPoint:
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 points are coincident? Check out enumerate. It will let you check positional index, so you can check if you are at the same place in both lists.

distance = euclidean_distance(firstPoint, secondPoint)
if (pointInList > distance):
pointInList = distance

shortDistanceList.append(pointInList)

mean_d = sum(shortDistanceList) / len(points)

return mean_d


Expand All @@ -141,6 +184,24 @@ def minimum_bounding_rectangle(points):

mbr = [0,0,0,0]

xmin = 0
ymin = 0
xmax = 0
ymax = 0

for i in points:
if i[0] < xmin:
xmin = i[0]
if i[1] < ymin:
ymin = i[1]
if i[0] > xmax:
xmax = i[0]
if i[1] > ymax:
ymax = i[1]

mbr = [xmin,ymin,xmax,ymax]


return mbr


Expand All @@ -150,6 +211,10 @@ def mbr_area(mbr):
"""
area = 0

length = mbr[3] - mbr[1]
width = mbr[2] - mbr [0]
area = length * width

return area


Expand All @@ -174,6 +239,9 @@ def expected_distance(area, n):
"""

expected = 0

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

return expected


Expand Down
6 changes: 4 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ 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, population, coordinates = point_pattern.write_your_own(self.gj)
self.assertEqual(city, 'New York')
self.assertEqual(population, 19040000)
self.assertEqual(coordinates, [ -73.981962787406815, 40.75192492259464 ])

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