import pandas as pd
import folium
from folium.plugins import HeatMap
import requests
import re
from geopy.geocoders import Nominatim
import time
# Load the modified DataFrame
survey = pd.read_pickle('data/tidied_survey.pkl')
# Rename and clean the data
survey.rename(columns={'R2.4_9': 'training_providers'}, inplace=True)
training_providers = survey['training_providers'].astype("object")
training_providers = training_providers.replace('Bangor', 'Centre for Mindfulness Research and Practice, Bangor University')
training_providers = training_providers[training_providers != '']
training_providers = training_providers[~training_providers.isin(['Not applicable', 'Other'])]
# Calculate frequency counts for training centres
frequency_counts = training_providers.value_counts()
# Addresses for the UK training centres
addresses = {
"British Mindfulness Institute": "145 – 147 St. John Street, London, EC1V 4PY, United Kingdom",
"Breathworks": "Breathworks CIC/Foundation, 16 - 20 Turner Street, Manchester, M4 1DZ",
"Centre for Mindfulness Research and Practice, Bangor University": "Brigantia Building, Bangor, Gwynedd, LL57 2AS",
"Exeter University": "Washington Singer Building, School of Psychology, University of Exeter, Exeter, EX4 4QG",
"Gaia House": "West Ogwell, Newton Abbot, Devon, TQ12 6EW, England",
"Integrated Mindfulness Training": "145 Radcliffe New Road, Whitefield, Manchester, M45 7RP, England",
"Mindfulness Association": "Boatleys Farmhouse, Kemnay, Inverurie, Aberdeenshire, AB51 5NA",
"Mindfulness in Schools Project": "Bank House, Bank Street, Tonbridge, Kent TN9 1BL",
"Mindfulness UK": "Churchinford, Taunton, Somerset, TA3 7QY",
"Nottingham Centre for Mindfulness": "St. Ann's House, 114 Thorneywood Mount, Nottingham, NG3 2PZ",
"Oxford Mindfulness Centre": "The Wheelhouse, Angel Court, 81 St Clements, Oxford, OX4 1AW",
"Sussex Mindfulness Centre": "Aldrington House, 35 New Church Road, Hove BN3 4AG",
"UMass Centre for Mindfulness": "306 Belmont St, Worcester, MA 01605",
"University of Aberdeen": "King's College, Aberdeen, AB24 3FX",
"Youth Mindfulness": "223, South Block, 60 Osborne St, Glasgow G1 5QH"
}
# Initialize a Geocoder instance for Nominatim
geolocator = Nominatim(user_agent="geoapiExercises")
# Regex pattern for UK postcodes
postcode_regex = r'\b[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}\b'
# Function to extract and clean postcode using regex
def extract_postcode(address):
matches = re.findall(postcode_regex, address, re.IGNORECASE)
if matches:
return matches[0].replace(" ", "").upper()
else:
return None
# Function to get geolocation data for a postcode using postcodes.io
def get_geolocation(postcode):
response = requests.get(f"http://api.postcodes.io/postcodes/{postcode}")
if response.status_code == 200:
return response.json()
else:
return None
# Initialize Map
map_combined = folium.Map(location=[54, -2], zoom_start=6)
training_layer_group = folium.FeatureGroup(name="Training Centres")
retreat_layer_group = folium.FeatureGroup(name="Retreat Centres")
# Calculate max frequency for scaling radius
max_training_frequency = frequency_counts.max()
max_retreat_frequency = combined_frequency_table.max()
radius_scale_training = 10 / max_training_frequency
radius_scale_retreat = 10 / max_retreat_frequency
# Plotting markers for training centres with corrected frequency match and radius
for name, address in addresses.items():
frequency = int(frequency_counts.get(name, 0))
if frequency > 0:
lat, lon = None, None
# Use the provided coordinates for UMass Centre for Mindfulness
if name == "UMass Centre for Mindfulness":
lat, lon = 42.272889, -71.767704
else:
postcode = extract_postcode(address)
if postcode:
geolocation = get_geolocation(postcode)
if geolocation:
lat = geolocation['result']['latitude']
lon = geolocation['result']['longitude']
if lat is not None and lon is not None:
folium.CircleMarker(
location=[lat, lon],
radius=frequency * radius_scale_training,
popup=f"Training Centre: {name}, Frequency: {frequency}",
color='red',
fill=True,
fill_opacity=0.7
).add_to(training_layer_group)
time.sleep(1)
# Assuming combined_frequency_table is defined somewhere in your code
# Function to add a marker to the map for retreat centres
def add_retreat_marker(lat, lon, name, frequency):
folium.CircleMarker(
location=[lat, lon],
radius=frequency * radius_scale_retreat,
popup=f"Retreat Centre: {name}, Frequency: {frequency}",
color='blue',
fill=True,
fill_opacity=0.7
).add_to(retreat_layer_group)
# Addresses for the UK centers
uk_retreat_addresses = {
"Amaravati Retreat Centre": "St Margarets, Great Gaddesden, Hertfordshire, HP1 3BZ, England, United Kingdom",
"Breathworks": "16 - 20 Turner Street, Manchester, M4 1DZ, UK",
"Gaia House": "West Ogwell, Newton Abbot, Devon, TQ12 6EW, England",
"Manjushri Kadampa Meditation Centre": "Conishead Priory, Priory Road (A5087 Coast Road), Ulverston, Cumbria, LA12 9QQ, UK",
"Oxford Mindfulness Centre": "The Wheelhouse, Angel Court, 81 St Clements, Oxford, OX4 1AW, UK",
"Trigonos": "Plas Baladeulyn, Nantlle, Caernarfon, Wales, LL54 6BW",
"Triratna Buddhist Community": "Vajraloka, Tyn-Y-Ddol, Corwen, Denbighshire, LL21 0EN, United Kingdom",
"Vipassana Trust, Dhamma Dīpa": "Pencoyd, St Owens Cross, Hereford HR2 8NG, United Kingdom"
}
# Additional centers with predefined coordinates
additional_retreat_addresses = {
"Holy Isle": (55.533467, -5.0874746),
"Plum Village": (44.750513, 0.34150910),
"Samye Ling": (55.287437, -3.1876922),
"Tara Rokpa Centre": (-25.743249, 26.349999),
"Western Chan Fellowship": (51.65083, -3.23167)
}
# Plot UK retreat addresses
for centre, address in uk_retreat_addresses.items():
frequency = int(combined_frequency_table.get(centre, 0))
if frequency > 0:
postcode = extract_postcode(address)
if postcode:
geolocation = get_geolocation(postcode)
if geolocation:
lat = geolocation['result']['latitude']
lon = geolocation['result']['longitude']
add_retreat_marker(lat, lon, centre, frequency)
# Plot additional retreat centre addresses
for name, coords in additional_retreat_addresses.items():
frequency = int(combined_frequency_table.get(name, 0))
if frequency > 0:
add_retreat_marker(coords[0], coords[1], name, frequency)
# Add the training and retreat layer groups to the map and enable layer control
training_layer_group.add_to(map_combined)
retreat_layer_group.add_to(map_combined)
folium.LayerControl().add_to(map_combined)
# Save the combined map to an HTML file
map_combined.save('combined_map.html')
map_combined