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_10 #10

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
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Week 13 Deliverables (E9) - Due 4/19/16
For this week make sure that you have completed the following:
* Fork Assignment 10 to your own github repository.
* You can access assignment 10 [HERE](https://github.com/Geospatial-Python/assignment_10)
* Clone the repository locally
* Note that TravisCI is still turned off for this weel

## Deliverable
1. In the `io_geojson` module write a function that ingests the twitter data and returns a dictionary. Note that you could write this from scratch or, if a suitable library exists (maybe a built-in), utilize someone else's library.
1. Create a new Tweet class or extend the existing Point class to store:
* The tweet text
* The tweet spatial information, e.g. lat/lon coordinats (be careful with how twitter ships the data (lat/lon vs. lon/lat)
* A few (3-5) other interesting tweet attributes

Note: Here I leave it entirely to you to decide whether the Point class should be extended to include tweet information, a Tweet class should be created that inherets from the Point class, or a Tweet class should be created that contains (composition) a Point object.
* As a comment to your PR, please let me know how you implemented the Point - Tweet relationship and why. A sentene or two is plenty.
1. Ensure that the GUI that you created last week is taking the form of a class. This will ensure that you can track state more easily. See above or [here](http://zetcode.com/gui/pyqt4/firstprograms/) for an example of what I mean by, 'the form of a class' (under the heading 'An Application Icon', the `class Example()` code block).
1. Once your current GUI window is a class extend it to:
* Have a QWebView act as a the main, central widget.
* Embed a Folium map into the QWebView
* Add the tweet location markers to the Folium map
* As above, the bounding box tweets will need to be randomly located within the bounding box.
* Add an open button or file menu item. This will open a [`QtGui.QFileDialog`](http://zetcode.com/gui/pyqt4/dialogs/). Using this dialog, the user can supply a file of tweets and those tweets are drawn on the map. Ideally, your window loads a blank map centered on a default location. When the user loads a file of tweets, the markers are drawn and the window recenters to the mean center of the points. This will require reloading the HTML in the `QWebView`. If you struggle with this, you can simply not draw the map until the tweets are opened, and hard code the centering to Phoenix.

Hint: The folium map object has a `__repr__` magic method that returns the map as raw HTML. The `QWebView` has a method [`setHtml()`](http://pythoncentral.io/pyside-pyqt-tutorial-qwebview/), as well as a method [`reload()`](http://pyqt.sourceforge.net/Docs/PyQt4/qwebview.html#reload). I was not able to get `reload()` to properly redraw and instead had to make a second call to `setHtml()`.
1. Include a screen shot (png, jpg) of your GUI in the PR. Please keep these small (resize to 700px width would be ideal). Git does not like large, binary files.
# Week 13 Deliverables (E9) - Due 4/19/16
For this week make sure that you have completed the following:
* Fork Assignment 10 to your own github repository.
* You can access assignment 10 [HERE](https://github.com/Geospatial-Python/assignment_10)
* Clone the repository locally
* Note that TravisCI is still turned off for this weel
## Deliverable
1. In the `io_geojson` module write a function that ingests the twitter data and returns a dictionary. Note that you could write this from scratch or, if a suitable library exists (maybe a built-in), utilize someone else's library.
1. Create a new Tweet class or extend the existing Point class to store:
* The tweet text
* The tweet spatial information, e.g. lat/lon coordinats (be careful with how twitter ships the data (lat/lon vs. lon/lat)
* A few (3-5) other interesting tweet attributes
Note: Here I leave it entirely to you to decide whether the Point class should be extended to include tweet information, a Tweet class should be created that inherets from the Point class, or a Tweet class should be created that contains (composition) a Point object.
* As a comment to your PR, please let me know how you implemented the Point - Tweet relationship and why. A sentene or two is plenty.
1. Ensure that the GUI that you created last week is taking the form of a class. This will ensure that you can track state more easily. See above or [here](http://zetcode.com/gui/pyqt4/firstprograms/) for an example of what I mean by, 'the form of a class' (under the heading 'An Application Icon', the `class Example()` code block).
1. Once your current GUI window is a class extend it to:
* Have a QWebView act as a the main, central widget.
* Embed a Folium map into the QWebView
* Add the tweet location markers to the Folium map
* As above, the bounding box tweets will need to be randomly located within the bounding box.
* Add an open button or file menu item. This will open a [`QtGui.QFileDialog`](http://zetcode.com/gui/pyqt4/dialogs/). Using this dialog, the user can supply a file of tweets and those tweets are drawn on the map. Ideally, your window loads a blank map centered on a default location. When the user loads a file of tweets, the markers are drawn and the window recenters to the mean center of the points. This will require reloading the HTML in the `QWebView`. If you struggle with this, you can simply not draw the map until the tweets are opened, and hard code the centering to Phoenix.
Hint: The folium map object has a `__repr__` magic method that returns the map as raw HTML. The `QWebView` has a method [`setHtml()`](http://pythoncentral.io/pyside-pyqt-tutorial-qwebview/), as well as a method [`reload()`](http://pyqt.sourceforge.net/Docs/PyQt4/qwebview.html#reload). I was not able to get `reload()` to properly redraw and instead had to make a second call to `setHtml()`.
1. Include a screen shot (png, jpg) of your GUI in the PR. Please keep these small (resize to 700px width would be ideal). Git does not like large, binary files.
274 changes: 274 additions & 0 deletions analytics.py
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('..'))

import utils
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
27 changes: 27 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#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

def read_tweets(data):
with open(data, 'r') as data:
read = data.read()
return json.loads(read)
Loading