-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01_convert.py
More file actions
40 lines (30 loc) · 1.04 KB
/
01_convert.py
File metadata and controls
40 lines (30 loc) · 1.04 KB
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
import h3
import json
import os
import pandas as pd
import geopandas as gpd
from kml2geojson import main as k2g
# Convert Boundary Data
zones = k2g.convert('boundaries/bbmp_zone_boundaries_243.kml')
with open('boundaries/zones.geojson', 'w') as f:
f.write(json.dumps(zones[0]))
wards = k2g.convert('boundaries/bbmp-final-new-wards.kml')
with open('boundaries/wards.geojson', 'w') as f:
f.write(json.dumps(wards[0]))
# Convert Census Data
# ___
def point_to_h3(obj):
idx = str(h3.latlng_to_cell(obj.y, obj.x, 8))
return idx
RAW_DATA_DIR = 'raw_census_data/'
gdfs = []
for filename in os.listdir(RAW_DATA_DIR):
print(f'READING {filename}')
ds = k2g.convert(RAW_DATA_DIR + filename)
zone_gdf = gpd.GeoDataFrame.from_features(ds[0]['features'])
zone_code = filename.split('blr_')[1].split("_zone")[0].lower()
zone_gdf['Zone'] = zone_code
zone_gdf['H3Index8'] = zone_gdf['geometry'].apply(point_to_h3)
gdfs.append(zone_gdf)
gdf = pd.concat(gdfs)
gdf.to_file('census_data_converted.geojson', driver="GeoJSON")