-
Notifications
You must be signed in to change notification settings - Fork 13
Completed assignment_07 PR #10
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
qstin
wants to merge
3
commits into
Geospatial-Python:master
Choose a base branch
from
qstin: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
3 commits
Select commit
Hold shift + click to select a range
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
Empty file.
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,176 @@ | ||
from point import Point | ||
import math | ||
import statistics | ||
import random | ||
|
||
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 | ||
""" | ||
total = len(points) | ||
y = 0 | ||
x = 0 | ||
for point in points: | ||
x += point[0] | ||
y += point[1] | ||
|
||
x = x/total | ||
y = y/total | ||
return x, y | ||
|
||
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] | ||
""" | ||
x_list = [] | ||
y_list = [] | ||
|
||
for p in points: | ||
x_list.append(p[0]) | ||
y_list.append(p[1]) | ||
|
||
mbr = [0,0,0,0] | ||
mbr[0] = min(x_list) | ||
mbr[1] = min(y_list) | ||
mbr[2] = max(x_list) | ||
mbr[3] = max(y_list) | ||
|
||
return mbr | ||
|
||
|
||
def mbr_area(mbr): | ||
""" | ||
Compute the area of a minimum bounding rectangle | ||
""" | ||
width = mbr[2] - mbr[0] | ||
length = mbr[3] - mbr[1] | ||
area = width * length | ||
|
||
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 | ||
|
||
|
||
""" | ||
Below are the functions that you created last week. | ||
Your syntax might have been different (which is awesome), | ||
but the functionality is identical. No need to touch | ||
these unless you are interested in another way of solving | ||
the assignment | ||
""" | ||
|
||
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 | ||
The Manhattan distance between the two points | ||
""" | ||
distance = abs(a[0] - b[0]) + abs(a[1] - b[1]) | ||
return distance | ||
def create_random_marked_points(n, marks = None): | ||
point_list = [] | ||
rand = random.Random() | ||
for i in range(n): | ||
rand_x = round(rand.uniform(0,1),2) | ||
rand_y = round(rand.uniform(0,1),2) | ||
if marks is None: | ||
point_list.append(Point(rand_x, rand_y)) | ||
else: | ||
rand_mark = random.choice(marks) | ||
point_list.append(Point(rand_x, rand_y, rand_mark)) | ||
return point_list | ||
|
||
def euclidean_distance(a, b): | ||
distance = math.sqrt((a.x - b.x)**2 + (a.y - b.y)**2) | ||
return distance | ||
|
||
def average_nearest_neighbor_distance(points, mark = None): | ||
new_points = [] | ||
if mark is None: | ||
new_points = points | ||
else: | ||
for point in points: | ||
if point.mark is mark: | ||
new_points.append(point) | ||
|
||
dists = [] | ||
for num1, point in enumerate(new_points): | ||
dists.append(None) | ||
for num2, point2 in enumerate(new_points): | ||
if num1 is not num2: | ||
new_dist = euclidean_distance(point, point2) | ||
if dists[num1] == None: | ||
dists[num1] = new_dist | ||
elif dists[num1] > new_dist: | ||
dists[num1] = new_dist | ||
|
||
return sum(dists)/len(points) | ||
|
||
def permutations(p=99, n=100, marks=None): | ||
neighbor_perms = [] | ||
for i in range(p): | ||
neighbor_perms.append(average_nearest_neighbor_distance(create_random_marked_points(n), | ||
marks)) | ||
return neighbor_perms | ||
|
||
def compute_critical(perms): | ||
return max(perms), min(perms) | ||
|
||
def check_significant(lower, upper, observed): | ||
return(lower <= observed or observed <= upper) |
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,78 @@ | ||
import json | ||
|
||
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 | ||
|
||
|
||
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 | ||
city = None | ||
features_list = gj.get('features') | ||
x = 0 | ||
|
||
for f in features_list: | ||
if f['properties']['pop_max'] > max_population: | ||
max_population = f['properties']['pop_max'] | ||
city = f['properties']['name'] | ||
|
||
return city, max_population | ||
|
||
|
||
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! | ||
""" | ||
#find the largest city west of the Mississippi River | ||
|
||
largest_western_city = None | ||
features_list = gj.get('features') | ||
for f in features_list: | ||
if f['properties']['longitude'] < -95.202: | ||
largest_western_city = f['properties']['longitude'] | ||
|
||
|
||
return largest_western_city |
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,111 @@ | ||
import analytics | ||
import math | ||
import random | ||
import random | ||
import numpy as np | ||
|
||
|
||
class Point: | ||
|
||
def __init__(self, x = 0, y = 0, mark = ''): | ||
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 __radd__(self, other): | ||
return Point(self.x + other, self.y + other) | ||
|
||
def check_coincident(self, b): | ||
|
||
if b.x == self.x and b.y == self.y: | ||
return True | ||
|
||
def shift_point(self, x_shift, y_shift): | ||
|
||
self.x += x_shift | ||
self.y += y_shift | ||
|
||
class PointPattern(object): | ||
|
||
def __init__(self): | ||
self.points = [] | ||
self.marks = [] | ||
self.length = len(self.points) | ||
|
||
def __len__(self): | ||
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 inclusion! |
||
count = 0 | ||
for item in self.points: | ||
count += 1 | ||
return count | ||
|
||
def add_point(self, point): | ||
self.points.append(point) | ||
|
||
def remove_point(self, index): | ||
del(self.points[index]) | ||
|
||
def average_nearest_neighor(self, mark=None): | ||
return analytics.average_nearest_neighbor_distance(self.points, | ||
mark) | ||
|
||
def count_coincident(self): | ||
counted = [] | ||
count = 0 | ||
for index, point in enumerate(self.points): | ||
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. Think about two things: (1) can you use |
||
for index2, point2 in enumerate(self.points): | ||
if index != index2: | ||
if point.check_coincident(point2) is True: | ||
count += 1 | ||
return count | ||
|
||
def list_marks(self): | ||
marks = [] | ||
for point in self.points: | ||
if point.mark not in marks and point.mark is not None: | ||
marks.append(point.mark) | ||
return marks | ||
|
||
def points_by_mark(self, mark): | ||
|
||
points_to_return = [] | ||
for point in self.points: | ||
if point.mark == mark: | ||
points_to_return.append(point) | ||
return points_to_return | ||
|
||
def generate_random_points(self, n = None, marks = None): | ||
|
||
if n is None: | ||
n = len(self.points) | ||
point_list = analytics.create_random_marked_points(n, marks) | ||
return point_list | ||
|
||
def generate_realizations(p = 99, marks = None): | ||
neighbor_perms = [] | ||
for i in range(p): | ||
neighbor_perms.append( | ||
analytics.average_nearest_neighbor( | ||
generate_random_points())) | ||
return neighbor_perms | ||
|
||
def get_critical(neighbor_perms): | ||
return max(perms), min(perms) | ||
|
||
def comupte_g(self, nsteps): | ||
ds = np.linspace(0, 1, nsteps) | ||
dist_counts = [] | ||
for i, d in enumerate(ds): | ||
min_dist = None | ||
for n in range(nsteps): | ||
if n != i: | ||
if min_dist is None or min_dist > d: | ||
min_dist = d | ||
dist_counts.append(min_dist) | ||
return sum(dist_counts)/nsteps | ||
|
Empty file.
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.
Does one of your magic methods already do this now?