File size: 3,022 Bytes
cfdf0a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98e1309
cfdf0a4
98e1309
cfdf0a4
 
 
 
 
 
 
 
 
 
 
 
fce81c6
cfdf0a4
 
 
 
 
 
 
 
45d34ef
cfdf0a4
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
"""borgarlina3_leaflet.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1gxzQen-P5wRoyODWRkWWGomLytSMWTsW
"""
"""
!pip install contextily
!pip install osmnx
!pip install ipyleaflet
!pip install geopandas matplotlib
!pip install folium
"""

from IPython import get_ipython
from IPython.display import display
#import contextily
#import osmnx
import ipyleaflet
import geopandas as gpd
import pandas as pd
import folium

def load_and_preprocess_data(geojson_file, pop_file, smallarea_file, dwellings_file):
    lina1 = gpd.read_file(geojson_file)
    pop = pd.read_csv(pop_file)
    smallarea = gpd.read_file(smallarea_file)
    dwellings = pd.read_csv(dwellings_file)

    # Pad 'smasvaedi' column with leading zeros
    pop['smasvaedi'] = pop['smasvaedi'].astype(str).str.zfill(4)

    # Transform to WGS84
    smallarea_wgs84 = smallarea.to_crs(epsg=4326)
    lina1_wgs84 = lina1.to_crs(epsg=4326)

    # Filter data
    pop2024 = pop[(pop['ar'] == 2024) & (pop['aldursflokkur'] == "0-4 ára") & (pop['kyn'] == 1)]
    all_dwellings = dwellings[dwellings['framvinda'] == "Fullbúið"].groupby('smasvaedi')['Fjöldi'].sum().reset_index()

    # Join dataframes
    pop2024_smallarea = smallarea_wgs84.merge(pop2024, left_on='smsv', right_on='smasvaedi', how='left')
    all_dwellings_smallarea = smallarea_wgs84.merge(all_dwellings, left_on='fid', right_on='smasvaedi', how='left')
    return lina1_wgs84, pop2024_smallarea, all_dwellings_smallarea

def create_map(lina1_wgs84, all_dwellings_smallarea):
    m = folium.Map()

    # The 'dagsetning' column does not exist in all_dwellings_smallarea
    #all_dwellings_smallarea['dagsetning'] = all_dwellings_smallarea['dagsetning'].astype(str)
    print(all_dwellings_smallarea.dtypes)
    # Add polygons to the map
    all_dwellings_smallarea['pubdate'] = all_dwellings_smallarea['pubdate'].astype(str)
    folium.Choropleth(
        geo_data=all_dwellings_smallarea[all_dwellings_smallarea['nuts3'] == '001'].to_json(),
        name="choropleth",
        data=all_dwellings_smallarea,
        columns=["smasvaedi", "Fjöldi"],
        key_on="feature.properties.smasvaedi",
        fill_color="YlGn",
        fill_opacity=0.7,
        line_opacity=0.2,
        legend_name="Dwellings"
    ).add_to(m)


    # Add markers for lina1_wgs84
    for _, r in lina1_wgs84.iterrows():
        folium.Marker(location=[r.geometry.y, r.geometry.x]).add_to(m)

    # Display the map
    return m

def main():
  geojson_file = "../given_data/cityline_geojson/cityline_2030.geojson"
  pop_file = "../given_data/ibuafjoldi.csv"
  smallarea_file = "../given_data/smasvaedi_2021.json"
  dwellings_file = "../given_data/ibudir.csv"

  lina1_wgs84, pop2024_smallarea, all_dwellings_smallarea = load_and_preprocess_data(geojson_file, pop_file, smallarea_file, dwellings_file)
  create_map(lina1_wgs84, all_dwellings_smallarea)  # Call the function to create the map

if __name__ == "__main__":
  main()