import folium
import h3
from shapely.geometry import Polygon, shape
import pandas as pd
import shapely
import numpy as np
import asyncio
import aiohttp
import nest_asyncio
nest_asyncio.apply()
import twint
import os
import time
# A GeoJSON object representing a bounding box for the region of interest
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
68.02734375,
23.725011735951796
],
[
70.048828125,
20.427012814257385
],
[
72.333984375,
20.879342971957897
],
[
72.6416015625,
18.979025953255267
],
[
73.36669921875,
16.024695711685315
],
[
74.55322265625,
12.833226023521243
],
[
76.1572265625,
9.167178732976677
],
[
77.32177734375,
7.863381805309173
],
[
78.31054687499999,
8.47237228290914
],
[
78.44238281249999,
8.841651120809145
],
[
79.03564453124999,
9.188870084473406
],
[
79.62890625,
8.86336203355168
],
[
79.69482421875,
7.36246686553575
],
[
79.73876953125,
6.206090498573885
],
[
80.15625,
5.68158368342113
],
[
81.73828125,
6.293458760393985
],
[
82.08984375,
7.536764322084078
],
[
81.123046875,
9.167178732976677
],
[
80.13427734374999,
10.120301632173907
],
[
79.98046875,
11.673755403433413
],
[
80.44189453125,
13.389619591747595
],
[
80.26611328125,
15.284185114076433
],
[
81.10107421874999,
15.749962572748768
],
[
82.3974609375,
16.615137799987075
],
[
84.92431640625,
19.103648251663646
],
[
86.59423828125,
20.05593126519445
],
[
87.14355468749999,
20.73556590521864
],
[
87.099609375,
21.3303150734318
],
[
91.34033203125,
21.94304553343818
],
[
92.3291015625,
21.963424936844223
],
[
92.92236328125,
21.94304553343818
],
[
94.50439453125,
24.367113562651262
],
[
95.29541015625,
26.194876675795218
],
[
97.3828125,
27.059125784374068
],
[
97.6025390625,
28.304380682962783
],
[
96.30615234375,
29.49698759653577
],
[
94.5703125,
29.439597566602902
],
[
91.845703125,
28.110748760633534
],
[
89.69238281249999,
28.420391085674304
],
[
87.16552734375,
28.05259082333983
],
[
85.20996093749999,
28.69058765425071
],
[
83.671875,
29.592565403314087
],
[
82.2216796875,
30.41078179084589
],
[
80.88134765625,
31.015278981711266
],
[
79.5849609375,
32.52828936482526
],
[
78.15673828125,
35.37113502280101
],
[
75.30029296875,
37.00255267215955
],
[
72.3779296875,
36.61552763134925
],
[
71.25732421875,
34.50655662164561
],
[
68.9501953125,
31.297327991404266
],
[
65.93994140625,
27.391278222579277
],
[
65.72021484375,
25.46311452925943
],
[
68.02734375,
23.725011735951796
]
]
]
}
}
]
}
# Add a 1 degree buffer around this area
s = shape(geojson['features'][0]['geometry'])
s = s.buffer(1)
feature = {'type': 'Feature', 'properties': {}, 'geometry': shapely.geometry.mapping(s)}
feature['geometry']['coordinates'] = [[[v[0], v[1]] for v in feature['geometry']['coordinates'][0]]]
feature = feature['geometry']
feature['coordinates'][0] = [[v[1], v[0]] for v in feature['coordinates'][0]]
# map H3 hexagons (code taken from H3 example: https://github.com/uber/h3-py-notebooks/blob/master/notebooks/usage.ipynb)
def visualize_hexagons(hexagons, color="red", folium_map=None):
"""
hexagons is a list of hexcluster. Each hexcluster is a list of hexagons.
eg. [[hex1, hex2], [hex3, hex4]]
"""
polylines = []
lat = []
lng = []
for hex in hexagons:
polygons = h3.h3_set_to_multi_polygon([hex], geo_json=False)
# flatten polygons into loops.
outlines = [loop for polygon in polygons for loop in polygon]
polyline = [outline + [outline[0]] for outline in outlines][0]
lat.extend(map(lambda v:v[0],polyline))
lng.extend(map(lambda v:v[1],polyline))
polylines.append(polyline)
if folium_map is None:
m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=13, tiles='cartodbpositron')
else:
m = folium_map
for polyline in polylines:
my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color=color)
m.add_child(my_PolyLine)
return m
def visualize_polygon(polyline, color):
polyline.append(polyline[0])
lat = [p[0] for p in polyline]
lng = [p[1] for p in polyline]
m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=13, tiles='cartodbpositron')
my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color=color)
m.add_child(my_PolyLine)
return m
# find all hexagons with a center that falls within our buffered area of interest from above
polyline = feature['coordinates'][0]
polyline.append(polyline[0])
lat = [p[0] for p in polyline]
lng = [p[1] for p in polyline]
m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=6, tiles='cartodbpositron')
my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color="green")
m.add_child(my_PolyLine)
# make the list of hexagon IDs in our AOI
hexagons = list(h3.polyfill(feature, 3))
# map the hexagons
polylines = []
lat = []
lng = []
for hex in hexagons:
polygons = h3.h3_set_to_multi_polygon([hex], geo_json=False)
# flatten polygons into loops.
outlines = [loop for polygon in polygons for loop in polygon]
polyline = [outline + [outline[0]] for outline in outlines][0]
lat.extend(map(lambda v:v[0],polyline))
lng.extend(map(lambda v:v[1],polyline))
polylines.append(polyline)
for polyline in polylines:
my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color='red')
m.add_child(my_PolyLine)
display(m)