Skip to content
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

Assignment_04 #22

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions data/geo.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} }, { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] }, "properties": { "prop0": "value0", "prop1": 0.0 } }, { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }, "properties": { "prop0": "value0", "prop1": {"this": "that"} } } ] }
90 changes: 79 additions & 11 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('data/us_cities.geojson') as f:
gj = json.load(f)
return gj


Expand All @@ -56,13 +57,18 @@ def find_largest_city(gj):
population : int
The population of the largest city
"""
city = None
max_population = 0
for feature in gj['features']:
test_max_population = feature['properties']['pop_max']
if test_max_population > max_population:
max_population = test_max_population
city = feature['properties']['name']


return city, max_population


def write_your_own(gj):
def highest_latitude(gj):
"""
Here you will write your own code to find
some attribute in the supplied geojson file.
Expand All @@ -74,7 +80,13 @@ def write_your_own(gj):
Do not forget to write the accompanying test in
tests.py!
"""
return
latHigh=0.0
for feature in gj['features']:
latitude = feature['properties']['latitude']
if latitude>latHigh:
latHigh=latitude
city = feature['properties']['name']
return city, latHigh

def mean_center(points):
"""
Expand All @@ -93,9 +105,17 @@ def mean_center(points):
y : float
Mean y coordinate
"""
x = None
y = None
x=0.0
y=0.0
i=0

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

x = x/len(points)
y = y/len(points)
return x, y


Expand All @@ -119,7 +139,39 @@ def average_nearest_neighbor_distance(points):
Measure of Spatial Relationships in Populations. Ecology. 35(4)
p. 445-453.
"""
mean_d = 0
total = 0.0
i = 0

for p in points:
j = 0
test = points[i]
Copy link
Contributor

Choose a reason for hiding this comment

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

Check out enumerate here, it will let you omit the i and j counters.


for p in points:


test2 = points[j]
dist = (((test[0]-test2[0])**2)+((test[1]-test2[1])**2))**0.5

if i == j:
j = j + 1
continue

if j==0 and i != 0:
nearest = dist
if j==1 and i==0:
nearest = dist

if nearest>dist:
nearest = dist



j = j + 1
i = i + 1

total = total + nearest
n = len(points)
mean_d = total/n

return mean_d

Expand All @@ -138,8 +190,23 @@ def minimum_bounding_rectangle(points):
: list
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

mbr = [0,0,0,0]
xmin = math.inf
xmax = 0.0
ymin = math.inf
ymax = 0.0
i=0
for p in points:
if points[i][0]<xmin:
xmin=points[i][0]
if points[i][0]>xmax:
xmax=points[i][0]
if points[i][1]<ymin:
ymin=points[i][1]
if points[i][1]>ymax:
ymax=points[i][1]
i=i+1

mbr = [xmin,ymin,xmax,ymax]

return mbr

Expand All @@ -148,7 +215,8 @@ def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""
area = 0
[xmin,ymin,xmax,ymax]=mbr
area = (xmax-xmin)*(ymax-ymin)

return area

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

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


Expand Down
7 changes: 4 additions & 3 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ def test_find_largest(self):
self.assertEqual(city, 'New York')
self.assertEqual(pop, 19040000)

def test_write_your_own(self):
def test_highest_latitude(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, latHigh = point_pattern.highest_latitude(self.gj)
self.assertEqual(city, 'Barrow')
self.assertEqual(latHigh, 71.29057)

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