diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/exit.png b/exit.png new file mode 100644 index 0000000..a44566f Binary files /dev/null and b/exit.png differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..35cd178 --- /dev/null +++ b/index.html @@ -0,0 +1,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + \ No newline at end of file diff --git a/io_geojson.py b/io_geojson.py new file mode 100644 index 0000000..9870c67 --- /dev/null +++ b/io_geojson.py @@ -0,0 +1,28 @@ +import json +# from urllib.request import urlopen + +def read_tweets(input_file): + with open(input_file, 'r') as f: + tweets = json.load(f) + return tweets + +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 + """ + gj = None + + with open(input_file, 'r') as f: + gj = json.load(f) + + return gj diff --git a/open.png b/open.png new file mode 100644 index 0000000..a06a5f6 Binary files /dev/null and b/open.png differ diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..b6b2d1a Binary files /dev/null and b/screenshot.png differ diff --git a/tweet.py b/tweet.py new file mode 100644 index 0000000..418043d --- /dev/null +++ b/tweet.py @@ -0,0 +1,25 @@ +import random +import utils + +class Tweet(object): + + def __init__(self, tweet): + + self.tweet_id = tweet['id'] + self.date = tweet['created_at'] + self.tweet = tweet['text'] + self.username = tweet['user']['name'] + self.user_description = tweet['user']['description'] + self.followers_count = tweet['user']['followers_count'] + self.bounds = tweet['place']['bounding_box']['coordinates'][0] + + def find_lat(self, n): + return self.bounds[n][1] + + def find_lng(self, n): + return self.bounds[n][0] + + def bounds(self): + lat = random.uniform(self.find_lat(0), self.find_lat(1)) + lng = random.uniform(self.find_lng(0), self.find_lng(1)) + return lat, lng \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..03144e4 --- /dev/null +++ b/utils.py @@ -0,0 +1,159 @@ +import math +import random + +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 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) + >>> shift_point(point, 1, 2) + (1,2) + """ + x = getx(point) + y = gety(point) + + x += x_shift + y += y_shift + + return x, y + + +def check_coincident(a, b): + """ + Check whether two points are coincident + Parameters + ---------- + a : tuple + A point in the form (x,y) + + b : tuple + A point in the form (x,y) + + Returns + ------- + equal : bool + Whether the points are equal + """ + return 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] + """ + return point in point_list + + +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 + x coordinate + """ + + return point.x + + +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 + y coordinate + """ + + return point.y \ No newline at end of file diff --git a/view.py b/view.py new file mode 100644 index 0000000..f20743f --- /dev/null +++ b/view.py @@ -0,0 +1,74 @@ +import sys +from PyQt4 import QtGui, QtCore, QtWebKit +import tweet +import folium +import io_geojson +from nose.tools import set_trace + +class View(QtGui.QMainWindow): + + def __init__(self): + + super(View, self).__init__() + self.initUI() + + def initUI(self): + + map_ = folium.Map(location=[33.4484, -112.0740]) + map_.save(r"./index.html") + + exit = QtGui.QAction(QtGui.QIcon('exit.png'), 'Quit', self) + exit.setStatusTip('Exit') + exit.triggered.connect(self.close) + exit.setShortcut('Ctrl+Q') + + open_ = QtGui.QAction(QtGui.QIcon('open.png'), 'Open File', self) + open_.setShortcut('Ctrl+F') + open_.setStatusTip('Import JSON') + open_.triggered.connect(self.mapTweets) + + self.webView = QtWebKit.QWebView() + self.webView.setHtml(open(r"./index.html").read()) + self.setCentralWidget(self.webView) + + menubar = self.menuBar() + menu = menubar.addMenu('&File') + menu.addAction(exit) + menu.addAction(open_) + + toolbar = self.addToolBar('Quit') + toolbar.addAction(exit) + toolbar = self.addToolBar('Open File') + toolbar.addAction(open_) + + self.setGeometry(300, 400, 700, 500) + self.setWindowTitle('Map o Tweets') + self.show() + self.webView.show() + + + def mapTweets(self): + map_ = folium.Map(location=[33.4484, -112.0740]) + + file = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '~') + if file == '': + return + + tweets = io_geojson.read_tweets(file) + for t in tweets: + data = tweet.Tweet(t) + folium.Marker([data.bounds[0][1], data.bounds[0][0]], popup = data.text).add_to(map_) + + map_.save(r"./index.html") + self.webView.setHtml(open("./index.html").read()) + self.webView.show() + +def main(): + app = QtGui.QApplication(sys.argv) + + view = View() + + sys.exit(app.exec_()) + +if __name__ == '__main__': + main() \ No newline at end of file