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

import pandas as pd
import numpy as np
from scipy import spatial
import folium
from IPython.display import IFrame


# +
import math

def cartesian(latitude, longitude, elevation = 0):
    # Convert to radians
    latitude = latitude * (math.pi / 180)
    longitude = longitude * (math.pi / 180)

    R = 6371 # 6378137.0 + elevation  # relative to centre of the earth
    X = R * math.cos(latitude) * math.cos(longitude)
    Y = R * math.cos(latitude) * math.sin(longitude)
    Z = R * math.sin(latitude)
    return (X, Y, Z)



# -

# Put here the coordinates of the centre of the area of analysis
# For Sydney: 
c_point = (-33.882882592547034, 151.2063073174587)
# For Zurich:
#c_point = (47.372133, 8.516359)

# #### Import the dataframe with the POIs, including the calculated POI density
# Then, import the dataframe with the nodes

# Import the dataframe with the POIs
poi_df = pd.read_csv(r'C:\Users\demdr\Desktop\Testing the thesis functions\Project data\Spatial database\POI_df.csv')
# Import the dataframe with the nodes
node_csv = pd.read_csv(r'C:\Users\demdr\Desktop\Testing the thesis functions\Project data\Spatial database\Urban network analysis_nodes.csv')
node_csv

# #### Create arrays to store information regarding the distance from the closest poi and its POI density

Closest_Poi_Density_array = np.zeros(len(node_csv))
Closest_Poi_Distance_array = np.zeros(len(node_csv))
Closest_Poi_Index_array = np.zeros(len(node_csv))

# #### Construct a kd-tree for the poi data

places = []
for index, row in poi_df.iterrows():
    coordinates = [row['Latitude'], row['Longitude']]
    cartesian_coord = cartesian(*coordinates)
    places.append(cartesian_coord)
tree = spatial.KDTree(places)

# #### Query the kd-tree that has the spatial proximity information for the poi data
#  For each street network node, find the closest POI and store the distance from it,and the POI density

# +
node_csv['Closest Poi Density']=0
node_csv['Closest Poi Distance']=0
node_csv['Closest Poi Index']=0

for item in node_csv.index:
    this_pt = (node_csv.loc[item, 'y'],node_csv.loc[item, 'x'])
    closest = tree.query([cartesian(this_pt[0],this_pt[1])], p = 2)
    closest_distance, closest_poi_index = closest[0][0]*1000, closest[1][0]
    closest_poi = (poi_df.loc[poi_df.index[closest_poi_index], 'Latitude'],poi_df.loc[poi_df.index[closest_poi_index], 'Longitude'])
    closest_poi_density = poi_df.loc[poi_df.index[closest_poi_index], 'Density']
    
    node_density = closest_poi_density
    
    Closest_Poi_Density_array[item] = node_density
    Closest_Poi_Distance_array[item] = closest_distance
    Closest_Poi_Index_array[item] = closest_poi_index

# -

# #### Transform the POI density so that it takes into account the distance from the closest POI 
# The logic is that in some suburban areas the closest POI is far away
#
# So the nodes that are far away from the closest POI get a reduced POI density value instead of the initial one
#

# +
node_csv['Closest Poi Density']=Closest_Poi_Density_array 
node_csv['Closest Poi Distance']=Closest_Poi_Distance_array
node_csv['Closest Poi Index']=Closest_Poi_Index_array
node_csv['Closest Poi Density_with distance decay']=Closest_Poi_Distance_array

new_dens =(node_csv['Closest Poi Density']/np.log1p(node_csv['Closest Poi Distance']).apply(np.ceil)).apply(np.ceil).values**2
node_csv['Closest Poi Density_with distance decay']=new_dens
#this results in some cases where the new density is larger than the old one. So we use the following to replace it
max_density = node_csv['Closest Poi Density']
node_csv.loc[node_csv[node_csv['Closest Poi Density_with distance decay']>max_density].index, 'Closest Poi Density_with distance decay']=node_csv.loc[node_csv[node_csv['Closest Poi Density_with distance decay']>max_density].index, 'Closest Poi Density']
node_csv
# -

# #### Visualise POI density

# +
osm_and_poi = folium.Map(location = [c_point[0],c_point[1]], zoom_start = 20, tiles='CartoDB dark_matter' ) 
osm_and_poi.save("POI density projected on street network nodes.html" )



for item in node_csv.index:

    this_pt = (node_csv.loc[item, 'y'],node_csv.loc[item, 'x'])
    node_density = node_csv.loc[item,'Closest Poi Density_with distance decay']

    this_color_poi='green'
    this_color_node='green'
    
    # colour scheme for visualisation
    if node_density>=50:
        this_color_node='darkpurple'
    elif (node_density>40) & (node_density<=50):
        this_color_node='purple'
    elif (node_density>30) & (node_density<=40):
        this_color_node='darkred'
    elif (node_density>20) & (node_density<=30):
        this_color_node='red'
    elif (node_density>10) & (node_density<=20):
        this_color_node = 'orange'
    elif (node_density>5) & (node_density<=10):
        this_color_node = 'beige'
    else:
        this_color_node = 'green'
    
    folium.CircleMarker([this_pt[0],this_pt[1]], color=this_color_node, fill=True, radius=0.5, opacity=0.5,
                   popup = 'node ' + str(item)).add_to(osm_and_poi)                                          

osm_and_poi.save("POI density projected on street network nodes.html" )

IFrame(src='./POI density projected on street network nodes.html', width=1500, height=1500)

# -

# Replace the old 'Urban network analysis_nodes' csv with the modified one, which now has the POI density projected on the street network nodes
node_csv.to_csv(r'C:\Users\demdr\Desktop\Testing the thesis functions\Project data\Spatial database\Urban network analysis_nodes.csv')



StarcoderdataJupyterScriptsDedupFiltered