-
Notifications
You must be signed in to change notification settings - Fork 18
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
thervig
wants to merge
14
commits into
Geospatial-Python:master
Choose a base branch
from
thervig:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
19b0a7f
Update tests.py
thervig 0ffddc0
Update point_pattern.py
thervig 560db09
Update point_pattern.py
thervig 265d3de
Update point_pattern.py
thervig f9320f6
Update point_pattern.py
thervig 1ba5ec6
Update point_pattern.py
thervig 67a9acd
Update point_pattern.py
thervig 30b8cfd
Update point_pattern.py
thervig c254e16
Update point_pattern.py
thervig 3d13369
Update point_pattern.py
thervig 5293987
Update point_pattern.py
thervig 2a35e8a
Update point_pattern.py
thervig 2075987
Update point_pattern.py
thervig 2bffe85
Update point_pattern.py
thervig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of continue. Check out |
||
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 | ||
|
||
|
@@ -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 | ||
|
||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
""" | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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] | ||
""" | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?