Files
mapping-data-starter/README.md
2026-05-29 13:56:25 +03:00

83 lines
2.4 KiB
Python

#!/bin/python3
from p5 import *
from regions import get_region_coords
region_list = []
colours = {}
map_image = None
def preload():
global map_image
map_image = load_image('mercator.jpeg')
def draw_pin(x, y, colour):
fill(colour)
ellipse(x, y, 10, 10) # x, y, width, height
def draw_data():
red_value = 255 # Set a starting value for red
for region in region_list:
region_name = region['region'] # Get the name of the region
try:
region_coords = get_region_coords(region_name) # Use the name to get coordinates
if region_coords:
region_x = region_coords['x'] # Get the x coordinate
region_y = region_coords['y'] # Get the y coordinate
region_colour = Color(red_value, 0, 0) # Set the pin colour
colours[region_colour.hex] = region
draw_pin(region_x, region_y, region_colour) # Draw the pin
red_value -= 1 # Change the red value
if red_value < 0:
red_value = 0
except Exception as e:
print(f"Error processing {region_name}: {e}")
# Put code to run once here
def setup():
size(991, 768)
load_data('gdp.csv')
no_loop() # Only draw once, not continuously
# Put code to run every frame here
def draw():
# Draw the background image
image(
map_image, # The image to draw
0, # The x of the top-left corner
0, # The y of the top-left corner
width, # The width of the image
height # The height of the image
)
# Draw all the data pins
draw_data()
# Put code to run when the mouse is pressed here
def mouse_pressed():
pixel_colour = Color(get(mouse_x, mouse_y)).hex
if pixel_colour in colours:
facts = colours[pixel_colour]
print(facts['region'])
print(facts['gdp'])
else:
print('Region not detected')
def load_data(file_name):
with open(file_name) as f:
for line in f:
line = line.strip() # Remove whitespace
if line: # Skip empty lines
info = line.split(',')
# Change the dictionary to match the data you're using
region_dict = {
'region': info[0],
'gdp': info[1]
}
region_list.append(region_dict)
run()