CATALONIA

import spatialpixel.mapping.slippymapper as slippymapper
import json
def setup():
size(1000, 800, P2D)
# creating the zoom variable to be able to make the zoom interactive later 
global world, zoom
# setting up the initial zoom to 2
zoom = 2
# connecting the variable world with slippymapper and setting the location and zoom
world = slippymapper.SlippyMapper(0, 0, zoom, 'carto-dark', width, height)

withLocations = []

# opening json and making processing read the titter json file by rows
with open('CatalanReferendum_all.json') as file:
for row in file:
data=json.loads(row)
place=data['place']

# appending the tweets data in withLocations if the tweets have a location
if place:
withLocations.append(data)

# getting the latitude and langitude as separate values from the twitter file
for tweet in withLocations:
lon, lat = tuple(tweet['place']['bounding_box']['coordinates'][0][0])
print(lat,lon)
x=lat
y=lon

# getting the text of the tweet from the twitter file
TweetText=(tweet['text'])

# adding the Datamrker with the text embeded
world.addMarker(lat, lon, TweetMarker({'text':TweetText}))

world.render()

def draw():
background(255)
world.draw()

# writing the title
textSize(24)
text('HELP CATALONIA',50,50, color(255))
textSize(15)
text('#Catalonia, #CatalanReferendum, #Catalan Independence, Catalan, CatalanReferendum, Puigdemont, #10, #LlibertatJordis', 50,70, color(255))
textSize(12)
text('Gloria Serra Coch DATAMINING THE CITY I M.S. URBAN PLANNING. GSAPP. COLUMBIA UNIVERSITY IN THE CITY OF NEW YORK October 18th 2017', 50, 770)

# creating a class for the DataMarker with the text of the tweet as the data of the marker
class TweetMarker(slippymapper.DataMarker):
def drawMarker(self, x, y, marker):
txt = self.data['text']

# making the marker interactive. When you hover the market you get the text
if dist(x, y, mouseX, mouseY) <= 3:
# setting the marker text as the tweets text and its position and color
marker.fill(255,0,0)
marker.textSize(13)
marker.text(txt, 50, height - 200)

marker.stroke(255)
marker.ellipse(x, y, 3, 3)

# drawing lines from the center of BCN to the tweets
xBCN = world.lonToX(2.177053)
yBCN = world.latToY(41.382628)
strokeWeight(0.2)
fill(240,240,240)
line(x,y,xBCN,yBCN)

# creating the zoom interaction with the mouse
def mouseClicked():
global world, zoom
lon = world.xToLon(mouseX)
lat = world.yToLat(mouseY)

# making zoom in
if mouseButton == LEFT:
zoom += 1
# defining maximum zoom in
if zoom > 18:
zoom = 18
# making zoom out
elif mouseButton == RIGHT:
zoom -= 1
# defining maximum zoom out
if zoom < 2:
zoom = 2


world.setCenter(lat, lon)
world.setZoom(zoom)
world.render()