-
Notifications
You must be signed in to change notification settings - Fork 13
Assignment_07 #5
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
maxruizee
wants to merge
16
commits into
Geospatial-Python:master
Choose a base branch
from
maxruizee: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
16 commits
Select commit
Hold shift + click to select a range
002ca46
Create point.py
maxruizee 926d3aa
Create analytics.py
maxruizee be9850f
Create io_geojson.py
maxruizee 6a9f649
Create point_pattern.py
maxruizee f69832b
Create utils.py
maxruizee 6042b1f
Update point_pattern.py
maxruizee ec3884b
Update point.py
maxruizee 04a631c
Create test_point_pattern.py
maxruizee 23d440b
Update test_point_pattern.py
maxruizee a2bbca7
Update point_pattern.py
maxruizee b09d95d
Update point_pattern.py
maxruizee a6a1fd1
Update test_point_pattern.py
maxruizee 9b81340
Update point_pattern.py
maxruizee f25f173
Update test_point_pattern.py
maxruizee c8690b9
Update utils.py
maxruizee 39978f3
Update utils.py
maxruizee 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 |
---|---|---|
@@ -0,0 +1,249 @@ | ||
''' | ||
Created on Feb 23, 2016 | ||
|
||
@author: Max Ruiz | ||
''' | ||
|
||
import math | ||
|
||
''' | ||
Function List | ||
------------- | ||
find_largest_city(gj) | ||
mean_center(points) | ||
average_nearest_neighbor_distance(points) | ||
minimum_bounding_rectangle(points) | ||
mbr_area(mbr) | ||
expected_distance(area, n) | ||
euclidean_distance(a, b) # also in utils.py | ||
|
||
compute_critical(p) | ||
check_significant(lower,upper,observed) | ||
''' | ||
|
||
'''Assignment 5 functions''' | ||
|
||
def compute_critical(p): | ||
""" | ||
Given a list, p, of distances (constants), determine the upper and lower | ||
bound (or max and min value) of the set. The values in p are assumed floats. | ||
|
||
Parameter(s): list p | ||
|
||
Return(s): float lower, float upper | ||
""" | ||
lower = min(p) | ||
upper = max(p) | ||
return lower, upper | ||
|
||
def check_significant(lower, upper, observed): | ||
""" | ||
Check if given observed point is outside or within a given lower and upper | ||
bound. | ||
|
||
Parameter(s): float lower, float upper, float observed. | ||
|
||
Return(s): boolean | ||
""" | ||
return observed < lower or observed > upper | ||
|
||
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 | ||
""" | ||
|
||
max_population = 0 | ||
for feat in gj['features']: | ||
test_max_pop = feat['properties']['pop_max'] | ||
if test_max_pop > max_population: | ||
max_population = test_max_pop | ||
city = feat['properties']['name'] | ||
|
||
return city, max_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 | ||
""" | ||
sumx = 0.0 | ||
sumy = 0.0 | ||
for coord in points: | ||
sumx += coord[0] | ||
sumy += coord[1] | ||
x = sumx / len(points) | ||
y = sumy / len(points) | ||
|
||
return x, y | ||
|
||
|
||
def average_nearest_neighbor_distance_tuples(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. | ||
""" | ||
min_dist_sum = 0 | ||
for coord_n in points: | ||
first = True | ||
for coord_m in points: | ||
if coord_n == coord_m: | ||
continue | ||
else: | ||
d = euclidean_distance(coord_n, coord_m) | ||
if first: | ||
min_dist = d | ||
first = False | ||
else: | ||
if d < min_dist: | ||
min_dist = d | ||
min_dist_sum += min_dist | ||
|
||
mean_d = min_dist_sum / len(points) | ||
|
||
return mean_d | ||
|
||
def average_nearest_neighbor_distance(points, mark = None): | ||
if mark != None: | ||
pointsWithMark = list() | ||
for x in range(len(points)): | ||
if points[x].getMark() == mark: | ||
pointsWithMark.append(points[x].getPoint()) | ||
else: | ||
continue | ||
return average_nearest_neighbor_distance_tuples(pointsWithMark) | ||
else: | ||
allPoints = list(points[x].getPoint() for x in range(len(points))) | ||
return average_nearest_neighbor_distance_tuples(allPoints) | ||
|
||
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] | ||
""" | ||
xmin = 0 | ||
xmax = 0 | ||
ymin = 0 | ||
ymax = 0 | ||
for coord in points: | ||
if coord[0] < xmin: | ||
xmin = coord[0] | ||
elif coord[0] > xmax: | ||
xmax = coord[0] | ||
|
||
if coord[1] < ymin: | ||
ymin = coord[1] | ||
elif coord[1] > ymax: | ||
ymax = coord[1] | ||
|
||
xcorner = xmax - xmin | ||
ycorner = ymax - ymin | ||
mbr = [0,0,xcorner,ycorner] | ||
|
||
return mbr | ||
|
||
|
||
def mbr_area(mbr): | ||
""" | ||
Compute the area of a minimum bounding rectangle | ||
""" | ||
length = mbr[3] - mbr[1] | ||
width = mbr[2] - mbr[0] | ||
area = length * width | ||
|
||
return area | ||
|
||
|
||
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.5 * math.sqrt(area / n) | ||
return expected | ||
|
||
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 | ||
""" | ||
distance = math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2) | ||
return distance |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
''' | ||
Created on Feb 23, 2016 | ||
|
||
@author: Max Ruiz | ||
''' | ||
import json | ||
|
||
''' | ||
Function List | ||
------------- | ||
read_geojson(input_file) | ||
''' | ||
|
||
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. | ||
|
||
with open(input_file,'r') as f: | ||
gj = json.load(f) | ||
return gj |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
''' | ||
Created on Mar 15, 2016 | ||
|
||
@author: Max Ruiz | ||
''' | ||
|
||
|
||
class Point(object): | ||
|
||
def __init__(self, x, y, mark = None): | ||
self.x = x | ||
self.y = y | ||
self.mark = mark | ||
|
||
def __add__(self, other): | ||
return Point(self.x + other.x, self.y + other.y) | ||
|
||
def __eq__(self, other): | ||
return self.x == other.x and self.y == other.y | ||
|
||
def __neg__(self): | ||
return Point(-self.x, -self.y) | ||
|
||
def check_coincident(self, point): | ||
return (self.x == point[0] and self.y == point[1]) | ||
|
||
def shift_point(self, x_shift, y_shift): | ||
self.x += x_shift | ||
self.y += y_shift | ||
|
||
def getx(self): | ||
return self.x | ||
|
||
def gety(self): | ||
return self.y | ||
|
||
def getPoint(self): | ||
return (self.x, self.y) | ||
|
||
def getMark(self): | ||
return self.mark |
Oops, something went wrong.
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.
Is this the same as one of your magic methods?