Skip to content

assignment 10 #5

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
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
127 changes: 127 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -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)

Binary file added screen_shot_of_GUI .jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions tweet.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to think about: Is it better to write a class with many arguments that essential parse an existing data structure or a class that can take said data structure and parse it? If you are coming from a language that supports multiple constructors, you can achieve something similar using classmethod decorators.


def get_spatial_information(self):
"""
Return the tweet spatial information.
:return:(lat,lon)
"""
return (self.point.x,self.point.y)
95 changes: 95 additions & 0 deletions view.py
Original file line number Diff line number Diff line change
@@ -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()