# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: .venv
#     language: python
#     name: .venv
# ---

from nbdev import *

# %nbdev_default_export geog

# ## Mapping of Australian States
#
# https://data.gov.au/dataset/ds-dga-bdcf5b09-89bc-47ec-9281-6b8e9ee147aa/details?q=PSMA%20administrative%20boundaries
#
# https://psma.com.au/wp-content/uploads/2019/06/Administrative-Boundaries-Getting-Started-Guide-New.pdf

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import geopandas as gpd


# %matplotlib inline

# https://data.gov.au/geoserver/nsw-suburb-locality-boundaries-psma-administrative-boundaries/wfs?request=GetFeature&typeName=ckan_91e70237_d9d1_4719_a82f_e71b811154c6&outputFormat=json
#
# https://data.gov.au/dataset/ds-dga-91e70237-d9d1-4719-a82f-e71b811154c6/details
#
# https://data.gov.au/data/dataset/12eca357-6bad-4130-9c47-eaaf4c11e039/resource/0def58c2-343e-47b2-a270-a35637c2f7b9/download/ntlocalitypolygonshp.zip

# +
# # !gunzip datasets/geo-data/nsw_locality_polygon_shp/NSW_LOCALITY_POLYGON_shp.shp.gz
# -

# %nbdev_export
def extract_postcode(pc):
    try:
        postcode = np.int(pc[-4:])
    except:
        postcode = np.NaN
    return postcode


# %nbdev_export
def load_suburb_data(state_name='NSW'):
    MAP_DATA_PATH = '../data/datasets/geo-data/'
    STATE_SUBURBS_FILE = 'NSW_Suburbs.geojson.json'
    suburbs_df = gpd.read_file(MAP_DATA_PATH + STATE_SUBURBS_FILE.replace('NSW', state_name))
    suburbs_df['postcode'] = suburbs_df['loc_pid'].apply(lambda pc: extract_postcode(pc))
    suburbs_df['postcode'] = suburbs_df['postcode'].astype(np.int64, errors='ignore')
    suburbs_df.to_excel(MAP_DATA_PATH + STATE_SUBURBS_FILE.replace('NSW', state_name).replace('.geojson.json', '.xlsx'))
    return suburbs_df


suburbs_df = load_suburb_data()

suburbs_df.to_feather('tmp_nsw_suburbs.feather')

# +
#TODO: Get data for all states - check formats - maybe just pull from URL (in JSON) and cache?
# -

NSW_URL = 'https://data.gov.au/geoserver/nsw-suburb-locality-boundaries-psma-administrative-boundaries/wfs?request=GetFeature&typeName=ckan_91e70237_d9d1_4719_a82f_e71b811154c6&outputFormat=json'

nsw_df = gpd.read_file(NSW_URL)

tmp_df = gpd.read_file('../data/datasets/geo-data/NSW_Suburbs.geojson.json')

nsw_df.equals(tmp_df)

# +
## NOTE: The shape file approach didn't work initially - but the GDA2020 file format seems ok but has different/less? info
# than the geoJSON file - so using geoJSON data instead

# set the filepath and load in a shapefile

fp = '../data/datasets/geo-data/nsw_state_polygon_shp/NSW_STATE_POLYGON_shp.shp'

map_shp_df = gpd.read_file(fp)

# +
# Lord Howe Island - map_df.iloc[3477] - drop as not part of mainland NSW

try:
    map_df.drop(index=3477, inplace=True)
except Exception as e:
    print(e)

# +
# now let's preview what our map looks like with no data in it

fig, ax = plt.subplots(1, figsize=(20, 12))
ax.axis('off')

map_df.plot(ax=ax)
# -

fig.savefig('testmap.png', dpi=300)

# +
# Load in other data

# Synthetic example counts by post code

df = pd.read_excel('../data/numberbypostcode.xlsx')
# -

df = df[['postcode', 'n']]

df['n'] = df['n'].astype(np.float64)

# +
#merged = map_df.set_index('postcode').join(df.set_index('postcode'))
# -

merged = pd.merge(map_df, df, how='outer', on='postcode')

merged.rename(index=str, columns={'n': 'nComplaints', 'postcode': 'Post code'}, inplace=True)

merged['nComplaints'].fillna(0, inplace=True)

merged.head()

# +
# set a variable that will call whatever column we want to visualise on the map
variable = 'nComplaints'

# set the range for the choropleth
vmin, vmax = 100, 1000

# create figure and axes for Matplotlib
fig, ax = plt.subplots(1, figsize=(20, 12))

# create map
merged.plot(column=variable, cmap='Blues', linewidth=0.8, ax=ax, edgecolor='0.8')

# Now we can customise and add annotations

# remove the axis
ax.axis('off')

# add a title
ax.set_title('Number of complaints by post code (NSW)', \
              fontdict={'fontsize': '25',
                        'fontweight' : '3'})

# create an annotation for the  data source
ax.annotate('Source: AFCA',
           xy=(0.1, .08), xycoords='figure fraction',
           horizontalalignment='left', verticalalignment='top',
           fontsize=10, color='#555555')

# Create colorbar as a legend
sm = plt.cm.ScalarMappable(cmap='Blues', norm=plt.Normalize(vmin=vmin, vmax=vmax))
sm._A = []
cbar = fig.colorbar(sm)

# this will save the figure as a high-res png. you can also save as svg
# fig.savefig('testmap.png', dpi=300)
# -



StarcoderdataJupyterScriptsDedupFiltered