diff --git a/io_geojson.py b/io_geojson.py new file mode 100644 index 0000000..3203a16 --- /dev/null +++ b/io_geojson.py @@ -0,0 +1,127 @@ +import json +import random + +def read_tweet_json(input_file): + """ + Read a tweet json file + + Parameters + ---------- + input_file : str + The PATH to the data to be read + + Returns + ------- + gj : dict + An in memory version of the geojson + """ + with open(input_file, 'r') as fp: + tweets = json.loads(fp.read()) + return tweets + + +def ingest_twitter_data(twitter_data): + """ + Ingest a tweet data and return the dict of needed data. + + Parameters + ---------- + twitter_data : str + The tweet data to be ingest + + Returns + ------- + need_data : dict + + """ + + if twitter_data['geo']==None: + + x_list=[value[1] for value in (twitter_data["place"]["bounding_box"]["coordinates"][0])] + + y_list=[value[0] for value in (twitter_data["place"]["bounding_box"]["coordinates"][0])] + x=random.uniform(min(x_list),max(x_list)) + y=random.uniform(min(y_list),max(y_list)) + else: + x=twitter_data['geo']['coordinates'][0] + y=twitter_data['geo']['coordinates'][1] + + need_data={"point":(x,y),"text":twitter_data["text"],"id_str":twitter_data["id_str"],"lang":twitter_data["lang"],"source":twitter_data["source"],"created_time":twitter_data["created_at"]} + return need_data + +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. + gj = None + fp = open(input_file, 'r') + gj = json.loads(fp.read()) + fp.close() + 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 + """ + city = None + max_population = 0 + for feature in gj["features"]: + if feature["properties"]["pop_max"]>max_population: + max_population=feature["properties"]["pop_max"] + city=feature["properties"]["nameascii"] + + 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! + + To find the average of pop_max and pop_min. + + """ + + sum_pop_max=0 + sum_pop_min=0 + num=0 + for feature in gj["features"]: + sum_pop_max+=feature["properties"]["pop_max"] + sum_pop_min+=feature["properties"]["pop_min"] + num+=1 + + return float(sum_pop_max/num),float(sum_pop_min/num) diff --git a/point.py b/point.py new file mode 100644 index 0000000..ee6d743 --- /dev/null +++ b/point.py @@ -0,0 +1,29 @@ + + + +class Point(): + + def __init__(self,x,y,mark=None): + self.x=x + self.y=y + self.mark=mark + + + def check_coincident(self, peer_p): + + return (self.x == peer_p.x and self.y == peer_p.y and self.mark == peer_p.mark) + + def shift_point(self, x_shift, y_shift): + + self.x += x_shift + self.y += y_shift + + def __eq__(self, other): + return self.x == other.x and self.y == other.y and self.mark == other.mark + + def __str__(self): + return "x=%f,y=%f,mark=%s"%(self.x,self.y,self.mark) + + def __add__(self, other): + return Point(self.x+other.x,self.y+other.y,self.mark) + diff --git a/screen_shot_of_GUI .jpg b/screen_shot_of_GUI .jpg new file mode 100644 index 0000000..83a6f42 Binary files /dev/null and b/screen_shot_of_GUI .jpg differ diff --git a/tweet.py b/tweet.py new file mode 100644 index 0000000..75ebcb9 --- /dev/null +++ b/tweet.py @@ -0,0 +1,20 @@ +import point + +#Tweet class be created that contains (composition) a Point object. +class Tweet(): + + def __init__(self,point,text,source,id_str,lang,created_time): + + self.point=point #The tweet spatial information + self.text=text #The text + self.source=source #the source + self.id_str=id_str #the id_str + self.lang=lang #the language + self.created_time=created_time #the create time + + def get_spatial_information(self): + """ + Return the tweet spatial information. + :return:(lat,lon) + """ + return (self.point.x,self.point.y) \ No newline at end of file diff --git a/view.py b/view.py new file mode 100644 index 0000000..363412a --- /dev/null +++ b/view.py @@ -0,0 +1,95 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import sys +import folium +import io_geojson +from tweet import * +from point import * +from PyQt4 import QtGui, QtCore, QtWebKit + +class Example(QtGui.QMainWindow): + + def __init__(self): + super(Example, self).__init__() + + self.initUI() + + def initUI(self): + + #self.webView = QtWebKit.QWebView() + #first just show a map without marks in the map + #the data is the last time i compute the averge of the lat/lon + map_osm = folium.Map(location=[33.59359997467155,-111.94546800838894]) + map_osm.save(r"./map.html") + + self.webView = QtWebKit.QWebView() + self.webView.setHtml(open(r"./map.html").read()) + self.setCentralWidget(self.webView) + self.statusBar() + + openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self) + openFile.setShortcut('Ctrl+O') + openFile.setStatusTip('Open new File') + openFile.triggered.connect(self.showDialog) + + menubar = self.menuBar() + fileMenu = menubar.addMenu('&File') + fileMenu.addAction(openFile) + + self.setGeometry(300, 300, 750, 650) + self.setWindowTitle('WebView') + self.show() + self.webView.show() + + def showDialog(self): + + fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home') + if fname == '': + return + tweets=io_geojson.read_tweet_json(fname) + + tweets_data=[] + for tweet_data in tweets: + tweets_data.append(io_geojson.ingest_twitter_data(tweet_data)) + + self.show_map_into_webview(tweets_data) + + + def show_map_into_webview(self,tweets_data): + + #first map tweet class list + + tweets = [Tweet(Point(tweet["point"][0],tweet["point"][1]),tweet["text"],tweet["source"],tweet["id_str"],tweet["lang"],tweet["created_time"]) for tweet in tweets_data] + + #compute the mean center of the points. + lat_all=[] + lon_all=[] + for tweet in tweets: + lat_all.append(tweet.get_spatial_information()[0]) + lon_all.append(tweet.get_spatial_information()[1]) + + avg_lat=sum(lat_all)/len(lat_all) + avg_lon=sum(lon_all)/len(lon_all) + + #set a map with the avg_lat,avg_lon + map_1 = folium.Map(location=[avg_lat, avg_lon]) + + #set markers in the map + for tweet in tweets: + folium.Marker(list(tweet.get_spatial_information()), popup=tweet.id_str).add_to(map_1) + map_1.save(r"./map.html") + + #set the webView with the map html + self.webView.setHtml(open("./map.html").read()) + self.webView.show() + +def main(): + + app = QtGui.QApplication(sys.argv) + ex = Example() + sys.exit(app.exec_()) + + +if __name__ == '__main__': + main()