""" Cannabis Licenses | License Mao Copyright (c) 2022 Cannlytics Authors: Keegan Skeate Candace O'Sullivan-Sutherland Created: 9/22/2022 Updated: 10/8/2022 License: Description: Map the adult-use cannabis retailers permitted in the United States: ✓ Alaska ✓ Arizona ✓ California ✓ Colorado ✓ Connecticut ✓ Illinois ✓ Maine ✓ Massachusetts ✓ Michigan ✓ Montana ✓ Nevada ✓ New Jersey x New Mexico (FIXME) ✓ Oregon ✓ Rhode Island ✓ Vermont ✓ Washington """ # Standard imports. from datetime import datetime import json import os # External imports. import folium import pandas as pd # Specify where your data lives. DATA_DIR = '../' # Read data subsets. with open('../subsets.json', 'r') as f: SUBSETS = json.loads(f.read()) def aggregate_retailers( datafiles, index_col=0, lat='premise_latitude', long='premise_longitude', ): """Aggregate retailer license data files, keeping only those with latitude and longitude.""" data = [] for filename in datafiles: data.append(pd.read_csv(filename, index_col=index_col)) data = pd.concat(data) return data.loc[(~data[lat].isnull()) & (~data[long].isnull())] def create_retailer_map( df, color='crimson', filename=None, lat='premise_latitude', long='premise_longitude', ): """Create a map of licensed retailers.""" m = folium.Map( location=[39.8283, -98.5795], zoom_start=3, control_scale=True, ) for _, row in df.iterrows(): folium.Circle( radius=5, location=[row[lat], row[long]], color=color, ).add_to(m) if filename: m.save(filename) return m # === Test === if __name__ == '__main__': # Aggregate retailers. subsets = list(SUBSETS.values()) datafiles = [DATA_DIR + x['data_url'] for x in subsets] retailers = aggregate_retailers(datafiles) # Create the retailers map. map_file = '../analysis/figures/cannabis-licenses-map.html' m = create_retailer_map(retailers, filename=map_file) # Save all of the retailers. timestamp = datetime.now().isoformat()[:19].replace(':', '-') retailers.to_csv(f'{DATA_DIR}/data/all/licenses-{timestamp}.csv', index=False)