-
Notifications
You must be signed in to change notification settings - Fork 13
Initial push #2
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
smortime
wants to merge
10
commits into
Geospatial-Python:master
Choose a base branch
from
smortime: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
Initial push #2
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bd847e6
Initial push to make sure everything still works
344695d
Adding part one of deliverable
5d93a4a
Added deliverable #2 changes
a6468e2
added compute_g
53c26b7
Moving PointPattern.py from tests folder to ..
58579e2
Added test cases for PointPattern
e1e8623
Had some path issues with online
2b7be9a
Fixing file paths...
5a3e75a
More path issues
34af5ea
call issues
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,88 @@ | ||
from .point import Point | ||
from . import analytics | ||
import random | ||
import numpy as np | ||
|
||
def PointPattern(object): | ||
def __init__(self): | ||
self.points = [] | ||
|
||
def average_nearest_neighbor_distance(self, mark=None): | ||
return analytics.average_nearest_neighbor_distance(self.points, mark) | ||
|
||
def add_point(self, point): | ||
self.points.append(point) | ||
|
||
def remove_point(self, index): | ||
del(self.points[index]) | ||
|
||
def count_coincident_points(self): | ||
count = 0 | ||
coincidnet_list = [] | ||
|
||
for i, point in enumerate(self.points): | ||
for j, point2 in enumerate(self.points): | ||
if i is j: | ||
continue | ||
if j in coincident_list: | ||
continue | ||
#Should use the magic method in point class | ||
if point == point2: | ||
count += 1 | ||
coincident_list.append(j) | ||
return count | ||
|
||
def list_marks(self): | ||
marks = [] | ||
|
||
for point in self.points: | ||
if point.mark is not None and point.mark not in marks: | ||
marks.append(point.mark) | ||
|
||
def return_subset(self, mark): | ||
#creates a list of points that have the same mark as passed | ||
return [i for i in self.points if i == mark] | ||
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. For a really fun approach, you could use |
||
|
||
def create_random_points(self, n=None): | ||
rand_points = [] | ||
rand = random.Random() | ||
marks = ['North', 'East', 'South', 'West'] | ||
|
||
if n is None: | ||
n = len(self.points) | ||
|
||
for i in range(n): | ||
rand_points.append(point.Point(rand.randint(1,100), rand.randint(1,100), mark=rand.choice(marks))) | ||
|
||
return rand_points | ||
|
||
def create_realizations(self, k): | ||
return analytics.permutations(k) | ||
|
||
def critical_points(self): | ||
return analytics.find_criticals(self.create_realizations(99)) | ||
|
||
def compute_g(self, nsteps): | ||
ds = np.linspace(0, 100, nsteps) | ||
g_sum = 0 | ||
|
||
for step in range(nsteps): | ||
o_i = ds[step] | ||
min_dis = None | ||
for i, j in enumerate(ds): | ||
|
||
temp = abs(j - o_i) | ||
|
||
if i is step: | ||
continue | ||
if min_dis is None: | ||
min_dis = temp | ||
elif min_dis > temp: | ||
min_dis = temp | ||
else: | ||
continue | ||
g_sum += min_dis | ||
return g_sum / nsteps | ||
|
||
|
||
|
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,274 @@ | ||
#analytics | ||
import math | ||
import json | ||
import sys | ||
import os | ||
|
||
sys.path.insert(0, os.path.abspath('..')) | ||
|
||
from . import utils | ||
from . import point | ||
|
||
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 | ||
""" | ||
temp = gj['features'] | ||
city = "" | ||
max_population = 0 | ||
|
||
for i in temp: | ||
if (i['properties']['pop_max'] > max_population): | ||
max_population = i['properties']['pop_max'] | ||
city = i['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! | ||
""" | ||
#Finds how many megacities there are in the geoJSON | ||
temp = gj['features'] | ||
megacities = 0 | ||
|
||
for i in temp: | ||
if(i['properties']['megacity'] == 1): | ||
megacities += 1 | ||
|
||
|
||
return megacities | ||
|
||
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 = 0 | ||
y = 0 | ||
|
||
for point in points: | ||
x += point[0] | ||
y += point[1] | ||
|
||
x = x / len(points) | ||
y = y / len(points) | ||
|
||
return x, y | ||
|
||
def average_nearest_neighbor_distance(points, mark=None): | ||
""" | ||
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. | ||
""" | ||
|
||
temp_points = [] | ||
|
||
if mark is not None: | ||
for point in points: | ||
if point.mark is mark: | ||
temp_points.append(point) | ||
else: | ||
temp_points = points | ||
|
||
nearest = [] | ||
|
||
for i, point in enumerate(temp_points): | ||
nearest.append(None) | ||
for j, point2 in enumerate(temp_points): | ||
if i is not j: | ||
dist = euclidean_distance((point.x, point.y), (point2.x, point2.y)) | ||
if nearest[i] == None: | ||
nearest[i] = dist | ||
elif nearest[i] > dist: | ||
nearest[i] = dist | ||
|
||
mean_d = sum(nearest) / 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] | ||
""" | ||
|
||
first = True | ||
mbr = [0,0,0,0] | ||
|
||
for point in points: | ||
if first: | ||
first = False | ||
mbr[0] = point[0] | ||
mbr[1] = point[1] | ||
mbr[2] = point[0] | ||
mbr[3] = point[1] | ||
|
||
if point[0] < mbr[0]: | ||
mbr[0] = point[0] | ||
if point[1] < mbr[1]: | ||
mbr[1] = point[1] | ||
if point[0] > mbr[2]: | ||
mbr[2] = point[0] | ||
if point[1] > mbr[3]: | ||
mbr[3] = point[1] | ||
|
||
return mbr | ||
|
||
def mbr_area(mbr): | ||
""" | ||
Compute the area of a minimum bounding rectangle | ||
""" | ||
area = (mbr[1] - mbr[3]) * (mbr[0] - mbr[2]) | ||
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 * (area / n) ** 0.5 | ||
return expected | ||
|
||
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 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 | ||
|
||
|
||
def permutations(p=99, n=100, marks=None): | ||
perms = [] | ||
|
||
if marks is None: | ||
for i in range(p): | ||
perms.append(average_nearest_neighbor_distance(utils.create_random_marked_points(n, marks=None))) | ||
|
||
else: | ||
for i in range(p): | ||
perms.append(average_nearest_neighbor_distance(utils.create_random_marked_points(n, marks))) | ||
|
||
return perms | ||
|
||
def find_criticals(perms): | ||
lower = min(perms) | ||
upper = max(perms) | ||
return lower, upper | ||
|
||
def check_significance(lower, upper, observed): | ||
if observed > upper: | ||
return True | ||
elif observed < lower: | ||
return True | ||
else: | ||
return False |
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,22 @@ | ||
#io_geojson | ||
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 |
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.
Check out using a
set
here as well. https://docs.python.org/3.5/library/stdtypes.html?highlight=set#set