Last active
March 27, 2024 23:07
-
-
Save xoryouyou/ad356263dbe124a3482fb6fd36e8e163 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
import pandas as pd | |
import cartopy | |
import cartopy.feature as cf | |
import cartopy.crs as ccrs | |
import matplotlib.pyplot as plt | |
import shapely.geometry as sgeom | |
import numpy as np | |
from cartopy.geodesic import Geodesic | |
# generate some fake data | |
def generate_data(count): | |
data ={'id':[],'lat':[],'lon':[],'timestamp':[],'radio_horizon':[]} | |
for i in range(count): | |
data["id"].append(i), | |
data["lat"].append(random.uniform(45.0,60.0)), | |
data["lon"].append(random.uniform(0.0,30.0)), | |
data["timestamp"].append(int(time.time())), | |
data["radio_horizon"].append(random.uniform(50000.0,100000.0)) | |
return pd.DataFrame(data) | |
def draw_map(): | |
gd = Geodesic() | |
# number of entries in data list | |
samples = 1000 | |
# alpha value for drawing the circles | |
alpha = max(1.0/samples,0.01) # seem like the alpha cant go below 0.01 | |
data = generate_data(samples) | |
# projection setup | |
# https://scitools.org.uk/cartopy/docs/latest/reference/projections.html#platecarree | |
src_crs = ccrs.PlateCarree() | |
lcc = ccrs.LambertConformal(central_longitude=data.lon.mean(), central_latitude=data.lat.mean()) | |
# figure setup | |
fig = plt.figure(figsize=(7,7)) | |
ax = fig.add_subplot(111, projection=lcc) | |
# add coasts and countries | |
ax.add_feature(cf.COASTLINE) | |
ax.add_feature(cf.BORDERS) | |
# create polygons for circles | |
circles = [] | |
for lon, lat,radio_horizon in zip(data.lon, data.lat, data.radio_horizon): | |
polygon = gd.circle(lon=lon, lat=lat, radius=radio_horizon) | |
circles.append(sgeom.Polygon(polygon)) | |
# add them to the map | |
ax.add_geometries(circles, crs=src_crs, alpha=alpha, color="000000") | |
# crop the map based on data | |
ax.set_extent([data.lon.min(), data.lon.max(), data.lat.min(), data.lat.max()]) | |
# save the map | |
plt.savefig('circles', dpi=300) | |
draw_map() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_data(count): | |
data ={'id':[],'lat':[],'lon':[],'timestamp':[],'radio_horizon':[]} | |
for i in range(count): | |
data["id"].append(i), | |
data["lat"].append(random.uniform(45.0,60.0)), | |
data["lon"].append(random.uniform(0.0,30.0)), | |
data["timestamp"].append(int(time.time())), | |
data["radio_horizon"].append(random.uniform(100000.0,200000.0)) | |
return pd.DataFrame(data) | |
import random | |
import time | |
import pandas as pd | |
import io | |
import folium | |
import folium.plugins | |
samples = 10000 | |
data = generate_data(samples) | |
map = folium.Map([data.lat.mean(), data.lon.mean()], zoom_start=4) | |
cluster = folium.plugins.MarkerCluster().add_to(map) | |
for lat, lon, radio_horizon in zip(data.lat, data.lon, data.radio_horizon): | |
print(lat, lon, radio_horizon) | |
# https://python-visualization.github.io/folium/latest/reference.html#folium.vector_layers.Circle | |
folium.vector_layers.Circle( | |
location=[lat, lon], | |
tooltip=str(radio_horizon), | |
radius=radio_horizon, | |
color='#3186cc', | |
fill=True, | |
fill_color='#3186cc', | |
opacity=1.0/samples | |
).add_to(cluster) | |
map.save("map.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment