def get_exposure(events, attack_type, p, freq): """ """ cells = [] for country in p.countries: cells += list(json_to_h3(country, p.h3_level)) cells_coord = np.array( [[h3.h3_to_geo(c)[0], h3.h3_to_geo(c)[1]] for c in cells], dtype='float64') events_of_type = events[events["type"] == attack_type] events_coord = events_of_type[["latitude", "longitude"]].to_numpy(dtype='float64') events_h3 = events_of_type["h3"].to_numpy() rings = np.array([list(h3.k_ring(str(cell), 2)) for cell in cells], dtype='object') is_in_ring = np.array([np.isin(events_h3, ring) for ring in rings]) date_range = pd.date_range(p.startdate, p.enddate, freq=freq) date_range = ((date_range - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s")).to_numpy() events_dates = ((events["date_start"] - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s")).to_numpy() distances = calc_distances(cells_coord, events_coord, is_in_ring) g = get_exposure_raw(events_dates, is_in_ring, distances, date_range) return pd.DataFrame(g, index=cells, columns=pd.to_datetime(date_range, unit="s"))
def test_validation_geo(): h = '8a28308280fffff' # invalid hex with pytest.raises(H3CellError): h3.h3_to_geo(h) with pytest.raises(H3ResolutionError): h3.geo_to_h3(0, 0, 17) with pytest.raises(H3CellError): h3.h3_to_geo_boundary(h) with pytest.raises(H3CellError): h3.h3_indexes_are_neighbors(h, h)
def main(h3_mapping: str, h3_level: int, folder_out: str): # Reading the input df = pd.read_csv(h3_mapping, names=['key', 'sid']) df.head() # Create the output folder - If needed create_folder(folder_out) # Preparing the dataframe lons = [] lats = [] for i, row in df.iterrows(): la, lo = h3.h3_to_geo(row.key) lons.append(lo) lats.append(la) df['lon'] = lons df['lat'] = lats df['nocc'] = 1. # Writing output for sid in df.sid.unique(): if isinstance(sid, str): tmps = '{:s}.csv'.format(sid[0:3]) else: tmps = '{:02d}.csv'.format(sid) fname_out = os.path.join(folder_out, tmps) print(fname_out) tdf = df.loc[df.sid == sid] tdf.to_csv(fname_out, columns=['lon', 'lat', 'nocc'], index=False)
def create(rs: Union[ResultSet, Query], geom_column: str = 'geom', name: Optional[str] = None, resolution: Optional[int] = 0, plot: str = "size", column: Optional[str] = None, group_by: Optional[str] = None): gdf = to_gdf(rs, geom_column) center = None zoom = None if len(gdf): if gdf.crs is None: gdf = gdf.set_crs(epsg=4326) if gdf.crs.to_epsg() != 4326: gdf = gdf.to_crs(epsg=4326) center = get_center(gdf) zoom = get_zoom(gdf) if len(gdf) and resolution: # Add H3 index hex_col = 'hex' + str(resolution) # H3 uses lat, lon gdf[hex_col] = gdf[GEOM_COL].apply( lambda geom: geo_to_h3(geom.y, geom.x, resolution), 1) if group_by: # Rows may be grouped by any field in a JSON data_to_group = QueryResult.flatten_dataframe(gdf, group_by) if "." in group_by: _, group_by = group_by.rsplit(".", 1) # Join rows with the same value in group_by field data_to_group = data_to_group.groupby([hex_col, group_by], sort=False, as_index=False).size() elif column and "." in column: # Flatten the dataframe so we may calculate means etc. for the desired column data_to_group = QueryResult.flatten_dataframe(gdf, column) _, column = column.rsplit(".", 1) else: data_to_group = gdf groupby = data_to_group.groupby(hex_col, sort=False, as_index=False) if column: groupby = groupby[column] # plot = size (or mean, or median, or max, or min) of rows within the same hex # Available functions: https://pandas.pydata.org/docs/reference/groupby.html results = getattr(groupby, plot)() # Add centroid geometry just in case. Of course, H3 has lat lon in the wrong order again centroid_lat_lon = results[hex_col].map(lambda hex: h3_to_geo(hex)) # Convert to GeoSeries first (https://github.com/Toblerity/Shapely/issues/1096#issuecomment-962988370) results[GEOM_COL] = gpd.GeoSeries( [Point(geom[1], geom[0]) for geom in centroid_lat_lon]) gdf = gpd.GeoDataFrame(results, geometry=GEOM_COL, crs=gdf.crs) gdf = gdf.set_index(hex_col) # retain hex index as column too, so it can be plotted gdf[hex_col] = gdf.index return QueryResult(gdf, center, zoom, name)
def placekey_to_geo(placekey): """ Convert a Placekey into a (latitude, longitude) tuple. :param placekey: Placekey (string) :return: (latitude, longitude) as a tuple of floats """ return h3.h3_to_geo(placekey_to_h3(placekey))
def plot(data, col_hex='hexset', col_color='color', cmap='YlOrRd', line_width=100, opacity=0.7): """ data is a list of dictionaries OR: if it is a dataframe, it is converted to a list of dictionaries !!! currently mutates input! (maybe no longer..) """ if isinstance(data, pd.DataFrame): data = data.to_dict('records') # just to make it so we don't mutate `data` data = [dict(row) for row in data] # if we don't find a color column, just add random values # todo: super hacky, gotta fix and make not mutate if col_color not in data[0].keys(): for row in data: row[col_color] = int(np.random.randint(len(data))) # sets don't parse to json, so make sure the hexset is a row for row in data: row[col_hex] = list(row[col_hex]) data = make_pdk_rows(data, col_color, cmap=cmap) layer = pdk.Layer( 'H3ClusterLayer', data, pickable=True, stroked=True, filled=True, extruded=False, get_hexagons=col_hex, get_fill_color='_pdk_fill_color', get_line_width=line_width, opacity=opacity, ) hexes = set.union(*[set(row[col_hex]) for row in data]) #todo: can we improve on this? we don't need to send the whole list, probably just the min/max in lat/lng right? view = pdk.data_utils.compute_view([h3.h3_to_geo(h)[::-1] for h in hexes]) deck = pdk.Deck( layers=[layer], initial_view_state=view, map_style='light', ) return deck
def create_missing(missing, h3_level, a_value, b_value): """ Create a dataframe with the same structure of the dataframe containing basic information on point sources but for the points requiring a baseline seismicity. """ coo = np.array([h3.h3_to_geo(idx) for idx in missing]) a = np.ones_like(coo[:, 0]) * a_value b = np.ones_like(coo[:, 0]) * b_value df = pd.DataFrame({'lon': coo[:, 0], 'lat': coo[:, 1], 'agr': a, 'bgr': b}) return df
def s_test_bin( sbin: SpacemagBin, test_cfg: dict, N_norm: float = 1.0, ): t_yrs = test_cfg["investigation_time"] like_fn = S_TEST_FN[test_cfg["likelihood_fn"]] not_modeled_likelihood = test_cfg["not_modeled_likelihood"] not_modeled_log_like = (-np.inf if not_modeled_likelihood == 0.0 else np.log(not_modeled_likelihood)) # calculate the rate rate_mfd = sbin.get_rupture_mfd() rate_mfd = {mag: t_yrs * rate * N_norm for mag, rate in rate_mfd.items()} # calculate the observed L obs_eqs = sbin.observed_earthquakes obs_L, likes = like_fn( rate_mfd, binned_events=obs_eqs, not_modeled_likelihood=not_modeled_likelihood, return_likes=True, ) # handle bins with eqs but no rups bad_bins = [] for like in likes: if like == not_modeled_log_like: bad_bins.append(sbin.bin_id) bin_ctr = h3.h3_to_geo(sbin.bin_id) bin_ctr = (round(bin_ctr[0], 3), round(bin_ctr[1], 3)) logging.warn(f"{sbin.bin_id} {bin_ctr} has zero likelihood") obs_mfd = sbin.get_empirical_mfd(cumulative=False) for mag, rate in rate_mfd.items(): if rate == 0.0 and obs_mfd[mag] > 0.0: logging.warn(f"mag bin {mag} has obs eqs but no ruptures") stoch_rup_counts = [ get_poisson_counts_from_mfd(rate_mfd).copy() for i in range(test_cfg["n_iters"]) ] # calculate L for iterated stochastic event sets stoch_Ls = np.array([ like_fn( rate_mfd, empirical_mfd=stoch_rup_counts[i], not_modeled_likelihood=not_modeled_likelihood, ) for i in range(test_cfg["n_iters"]) ]) return obs_L, stoch_Ls, bad_bins
def plot3d(data, col_hex='hexset', col_color='color', cmap='YlOrRd', opacity=0.7, elevation_scale=100): """ data is a list of dictionaries. !!! currently mutates input! """ if isinstance(data, pd.DataFrame): data = data.to_dict('records') # just to make it so we don't mutate `data` data = [dict(row) for row in data] # sets don't parse to json, so make sure the hexset is a row for row in data: row[col_hex] = list(row[col_hex]) data = make_pdk_rows(data, col_color, cmap=cmap) layer = pdk.Layer( 'H3ClusterLayer', data, pickable=True, stroked=True, filled=True, extruded=True, get_hexagons=col_hex, get_fill_color='_pdk_fill_color', opacity=opacity, elevation_scale=elevation_scale, get_elevation=col_color, ) hexes = set.union(*[set(row[col_hex]) for row in data]) #todo: can we improve on this? we don't need to send the whole list, probably just the min/max in lat/lng right? view = pdk.data_utils.compute_view([h3.h3_to_geo(h)[::-1] for h in hexes]) view.pitch = 45 deck = pdk.Deck( layers=[layer], initial_view_state=view, map_style='light', ) return deck
def fill_hex_grid(gdf: gpd.GeoDataFrame, geom_column: str = "geometry") -> gpd.GeoDataFrame: bbox = gdf.total_bounds # Pandas somehow mangles Geopandas geometry column types so that the types # become mixed after concatenation and may cause TypeErrors, i.e. some # Shapely geometries may be cast as strings in the process. We have to # concatenate regular dataframes instead and reconstruct a geodataframe # from the hex indices afterwards. Utterly stupid. df = gdf.drop(columns=['geometry']) bbox_polygon = box(*bbox) hex_column = next((col for col in df.columns if col.startswith("hex")), False) if not hex_column: raise AssertionError( "Cannot calculate clusters, hex column not found.") resolution = int(hex_column.replace("hex", "")) # H3 polyfill needs geojson-like stuff. geo_json_conformant switches coordinate order hexes_in_bbox = h3.polyfill(mapping(bbox_polygon), resolution, geo_json_conformant=True) # Add only missing hexes here missing_hexes = set(hexes_in_bbox).difference(df[hex_column]) missing_df = pd.DataFrame(list(missing_hexes), columns=[hex_column]).set_index(hex_column, drop=False) columns_to_add = df.columns.difference(missing_df.columns) for column in columns_to_add: # Just add zeroes for missing index values missing_df.insert(0, column, 0) combined_df = pd.concat((df, missing_df)) # Add centroid geometries and reconstruct the geodataframe centroid_lat_lon = [h3.h3_to_geo(hex) for hex in combined_df[hex_column]] centroids = [Point(geom[1], geom[0]) for geom in centroid_lat_lon] combined_gdf = gpd.GeoDataFrame(combined_df) combined_gdf = combined_gdf.set_geometry(centroids) return combined_gdf
def search_vaccine(self, index, location, radius, chat_id): nround = 0 try: while self.userThread[chat_id] == index: nround += 1 url = 'https://labtools.curativeinc.com/api/v1/testing_sites/get_by_geolocation?h3={}&radius={}'.format( location, radius) req = requests.request(method='GET', url=url) sites = req.json() self.monitor_vaccine_site(sites, chat_id) if nround == 1: lines = self.vaccine_log[chat_id].split('\n') if len(lines) == 2 and lines[1] == "": coordinates = h3.h3_to_geo(location) if len(coordinates) == 2: url = "https://curative.com/sites#10/{}/{}".format( coordinates[0], coordinates[1]) else: url = "https://curative.com/search" self.SendMessage( chat_id, "There is no vaccination sites in your area round {} miles" .format(radius)) self.SendMessage( chat_id, "Using a bigger radius may help, but it doesn't cover all sites in the area due to the maximum number of sites for each query" ) self.SendMessage( chat_id, "You may change to a nearby zip code where has a vaccination site(NOT TEST ONLY SITE), the information can be found at {}" .format(url)) self.userStatus[chat_id] = TelegHelper.StatusKill time.sleep(self.timeout[chat_id]) except Exception as e: self.logger.info( "{}: An unexpected error occur at search_vaccine(): {}".format( chat_id, e)) self.logger.info("{} exits".format(chat_id))
def _compute_view(hexes, tilt=False): """ Computes a view based on transforming hexagons to their lat/lng points. Parameters ---------- hexes : Iterable[str] An iterable of H3 hexagons in string representation. Set, list, tuple, pandas.Series, etc. tilt : bool Tilt view if True Returns ------- pydeck.View """ view = pdk.data_utils.compute_view([h3.h3_to_geo(h)[::-1] for h in hexes]) if tilt: view.pitch = 45 return view
def calc_event_distance(origin, row): cell_lat, cell_lng = h3.h3_to_geo(origin) ev_lat, ev_lng = row['latitude'], row['longitude'] return harvesine(cell_lng, cell_lat, ev_lng, ev_lat)
def test_h3_to_geo(): latlng = h3.h3_to_geo('85283473fffffff') assert latlng == approx((37.34579337536848, -121.97637597255124))
def poc_polar(hotspot, chals): H = Hotspots() haddr = hotspot['address'] hlat, hlng = hotspot['lat'], hotspot['lng'] hname = hotspot['name'] if os.path.exists(hname): files = glob(hname + '\\*') for file in files: os.remove(file) else: os.mkdir(hname) wl = {} #witnesslist rl = { } #received list of hotspots(hotspot of intereset has been witness to these or received from them) c = 299792458 for chal in chals: # loop through challenges for p in chal['path']: #path? if p['challengee'] == haddr: # handles cases where hotspot of interest is transmitting for w in p[ 'witnesses']: #loop through witnesses so we can get rssi at each location challenge received #print('witness',w) lat = H.get_hotspot_by_addr(w['gateway'])['lat'] lng = H.get_hotspot_by_addr(w['gateway'])['lng'] name = H.get_hotspot_by_addr(w['gateway'])['name'] dist_km, heading = utils.haversine_km(hlat, hlng, lat, lng, return_heading=True) fspl = 20 * log10((dist_km + 0.01) * 1000) + 20 * log10( 915000000) + 20 * log10(4 * pi / c) - 27 try: wl[w['gateway']]['lat'] = lat wl[w['gateway']]['lng'] = lng wl[w['gateway']]['rssi'].append(w['signal']) except KeyError: wl[w['gateway']] = { 'rssi': [ w['signal'], ], 'dist_km': dist_km, 'heading': heading, 'fspl': fspl, 'lat': lat, 'lng': lng, 'name': name } else: # hotspot of interest is not transmitting but may be a witness challengee = p['challengee'] name = H.get_hotspot_by_addr(challengee)['name'] for w in p['witnesses']: if w['gateway'] != haddr: continue #print('transmitter ', name) #print('witness ', H.get_hotspot_by_addr(w['gateway'])['name']) # hotspot of interest was a witness lat = H.get_hotspot_by_addr(challengee)['lat'] lng = H.get_hotspot_by_addr(challengee)['lng'] #name=H.get_hotspot_by_addr(w['gateway'])['name'] dist_km, heading = utils.haversine_km(hlat, hlng, lat, lng, return_heading=True) fspl = 20 * log10((dist_km + 0.01) * 1000) + 20 * log10( 915000000) + 20 * log10(4 * pi / c) - 27 try: rl[challengee]['lat'] = lat rl[challengee]['lng'] = lng rl[challengee]['rssi'].append(w['signal']) except KeyError: rl[challengee] = { 'rssi': [ w['signal'], ], 'dist_km': dist_km, 'heading': heading, 'fspl': fspl, 'lat': lat, 'lng': lng, 'name': name } #print('rl:',rl) ratios = [1.0] * 16 rratios = [1.0] * 16 N = len(ratios) - 1 angles = [] rangles = [] #angles = [n / float(N) *2 *pi for n in range(N+1)] angles = list(np.arange(0.0, 2 * np.pi + (2 * np.pi / N), 2 * np.pi / N)) rangles = list(np.arange(0.0, 2 * np.pi + (2 * np.pi / N), 2 * np.pi / N)) #print(angles,len(angles)) #print(ratios,len(ratios)) markers = [] encoded = {} rencoded = {} for w in wl: #for witness in witnesslist #print(wl[w]) mean_rssi = sum(wl[w]['rssi']) / len(wl[w]['rssi']) ratio = wl[w]['fspl'] / mean_rssi * (-1) if ratio > 3.0: ratio = 3.0 elif ratio < -3.0: ratio = -3.0 ratios.append(ratio) angles.append(wl[w]['heading'] * pi / 180) #markers.append(folium.Marker([wl[w]['lat'],wl[w]['lng']],popup=wl[w]['name'])) markers.append([[wl[w]['lat'], wl[w]['lng']], wl[w]['name']]) # the histogram of the data #unique=set(wl[w]['rssi']) #num_unique=len(unique) n, bins, patches = plt.hist( wl[w]['rssi'], 10) #, density=True, facecolor='g', alpha=0.75,) plt.xlabel('RSSI(dB)') plt.ylabel('Count(Number of Packets)') wit = str(wl[w]['name']) plt.title('Packets from ' + hname + ' measured at ' + wit) #plt.text(60, .025, r'$\mu=100,\ \sigma=15$') #plt.xlim(40, 160) #plt.ylim(0, 0.03) plt.grid(True) #plt.show() strFile = str(wl[w]['name']) + '.jpg' strWitness = str(wl[w]['name']) if os.path.isfile(strFile): #print('remove') os.remove(strFile) # Opt.: os.system("rm "+strFile) plt.savefig(hname + '//' + strFile) encoded[strWitness] = base64.b64encode( open(hname + '//' + strFile, 'rb').read()) plt.close() for w in rl: #for witness in witnesslist #print(rl[w]) mean_rssi = sum(rl[w]['rssi']) / len(rl[w]['rssi']) rratio = rl[w]['fspl'] / mean_rssi * (-1) if rratio > 3.0: rratio = 3.0 elif rratio < -3.0: rratio = -3.0 rratios.append(rratio) rangles.append(rl[w]['heading'] * pi / 180) #markers.append([[wl[w]['lat'],wl[w]['lng']],wl[w]['name']]) n, bins, patches = plt.hist( rl[w]['rssi'], 10) #, density=True, facecolor='g', alpha=0.75,) plt.xlabel('RSSI(dB)') plt.ylabel('Count(Number of Packets)') wit = str(rl[w]['name']) plt.title('Packets from ' + wit + ' measured at ' + hname) plt.grid(True) #plt.show() strFile = 'rrr' + str(rl[w]['name']) + '.jpg' strWitness = str(rl[w]['name']) if os.path.isfile(strFile): #print('remove') os.remove(strFile) # Opt.: os.system("rm "+strFile) plt.savefig(hname + '//' + strFile) rencoded[strWitness] = base64.b64encode( open(hname + '//' + strFile, 'rb').read()) plt.close() # create polar chart angles, ratios = zip(*sorted(zip(angles, ratios))) rangles, rratios = zip(*sorted(zip(rangles, rratios))) angles, ratios = (list(t) for t in zip(*sorted(zip(angles, ratios)))) rangles, rratios = (list(t) for t in zip(*sorted(zip(rangles, rratios)))) fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) ax.set_theta_zero_location("N") ax.set_theta_direction(-1) #ax.set_rmax(3) #ax.set_rmin(-3) ax.set_ylim(-3, 3) ax.plot(angles, ratios, marker='^', linestyle='solid', color='tomato', linewidth=2, markersize=5, label='Transmitting') #markerfacecolor='m', markeredgecolor='k', ax.plot(rangles, rratios, marker='v', linestyle='solid', color='dodgerblue', linewidth=1, markersize=5, label='Receiving') #, markerfacecolor='m', markeredgecolor='k' ax.legend(bbox_to_anchor=(0, 1), fancybox=True, framealpha=0, loc="lower left", facecolor='#000000') plt.xlabel('FSPL/RSSI') plt.savefig(hname + '//' + hname + '.png', transparent=True) #plt.show() # add polar chart as a custom icon in map m = folium.Map([hlat, hlng], tiles='stamentoner', zoom_start=18, control_scale=True, max_zoom=20) polargroup = folium.FeatureGroup(name='Polar Plot') icon = folium.features.CustomIcon(icon_image=hname + '//' + hotspot['name'] + '.png', icon_size=(640, 480)) marker = folium.Marker([hlat, hlng], popup=hotspot['name'], icon=icon) polargroup.add_child(marker) # add witness markers hsgroup = folium.FeatureGroup(name='Witnesses') hsgroup.add_child(folium.Marker([hlat, hlng], popup=hotspot['name'])) # add the witness markers for marker in markers: #html = '<img src="data:image/jpg;base64,{}">'.format html = '<p><img src="data:image/jpg;base64,{}" alt="" width=640 height=480 /></p> \ <p><img src="data:image/jpg;base64,{}" alt="" width=640 height=480 /></p>'.format #print('marker',marker) try: iframe = IFrame(html(encoded[marker[1]].decode('UTF-8'), rencoded[marker[1]].decode('UTF-8')), width=640 + 25, height=960 + 40) popup = folium.Popup(iframe, max_width=2650) mark = folium.Marker(marker[0], popup=popup) hsgroup.add_child(mark) except KeyError: # this means this witness never heard from us so there is no marker for it pass # not sure where to put the receive packet histogram so just ignore for now radius = 0.01 center = Point(hlat, hlng) circle = center.buffer(radius) # Degrees Radius gjcircle = shapely.geometry.mapping(circle) circle = center.buffer(radius * 25) # Degrees Radius gjcircle8 = shapely.geometry.mapping(circle) dcgroup = folium.FeatureGroup(name='Distance Circles', show=False) radius = 0.01 center = Point(hlat, hlng) circle = center.buffer(radius) # Degrees Radius gjcircle = shapely.geometry.mapping(circle) circle = gjcircle['coordinates'][0] my_Circle = folium.Circle(location=[hlat, hlng], radius=300, popup='300m', tooltip='300m') dcgroup.add_child(my_Circle) my_Circle = folium.Circle(location=[hlat, hlng], radius=1000, popup='1km', tooltip='1km') dcgroup.add_child(my_Circle) my_Circle = folium.Circle(location=[hlat, hlng], radius=2000, popup='2km', tooltip='2km') dcgroup.add_child(my_Circle) my_Circle = folium.Circle(location=[hlat, hlng], radius=3000, name='circles', popup='3km', tooltip='3km') dcgroup.add_child(my_Circle) my_Circle = folium.Circle(location=[hlat, hlng], radius=4000, popup='4km', tooltip='4km') dcgroup.add_child(my_Circle) my_Circle = folium.Circle(location=[hlat, hlng], radius=5000, popup='5km', tooltip='5km') dcgroup.add_child(my_Circle) my_Circle = folium.Circle(location=[hlat, hlng], radius=10000, popup='10km', tooltip='10km') dcgroup.add_child(my_Circle) h3colorgroup = folium.FeatureGroup(name='h3 Hexagon Grid Color Fill', show=False) style = {'fillColor': '#f5f5f5', 'lineColor': '#ffffbf'} #polygon = folium.GeoJson(gjson, style_function = lambda x: style).add_to(m) h3group = folium.FeatureGroup(name='h3 r11 Hex Grid', show=False) h3namegroup = folium.FeatureGroup(name='h3 r11 Hex Grid Names', show=False) h3fillgroup = folium.FeatureGroup(name='h3 r11 Hex Grid Color Fill', show=True) h3r8namegroup = folium.FeatureGroup(name='h3 r8 Hex Grid Names', show=False) h3r8group = folium.FeatureGroup(name='h3 r8 Hex Grid', show=False) hexagons = list(h3.polyfill(gjcircle, 11)) hexagons8 = list(h3.polyfill(gjcircle8, 8)) polylines = [] lat = [] lng = [] i = 0 #print('hexagon',hexagons[0]) #print(dir(h3)) home_hex = h3.geo_to_h3(hlat, hlng, 11) a = h3.k_ring(home_hex, 7) for h in a: gjhex = h3.h3_to_geo_boundary(h, geo_json=True) gjhex = geometry.Polygon(gjhex) mean_rsrp = -60 folium.GeoJson( gjhex, style_function=lambda x, mean_rsrp=mean_rsrp: { 'fillColor': map_color_rsrp(mean_rsrp), 'color': map_color_rsrp(mean_rsrp), 'weight': 1, 'fillOpacity': 0.5 }, #tooltip='tooltip' ).add_to(h3fillgroup) for hex in hexagons: p2 = h3.h3_to_geo(hex) #p2 = [45.3311, -121.7113] folium.Marker( p2, name='hex_names', icon=DivIcon( #icon_size=(150,36), #icon_anchor=(35,-45), icon_anchor=(35, 0), html='<div style="font-size: 6pt; color : black">' + str(hex) + '</div>', )).add_to(h3namegroup) #m.add_child(folium.CircleMarker(p2, radius=15)) 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=1, color='blue') h3group.add_child(my_PolyLine) polylines = [] lat = [] lng = [] #polylines8 = [] for hex in hexagons8: p2 = h3.h3_to_geo(hex) folium.Marker( p2, name='hex_names', icon=DivIcon( #icon_size=(150,36), #icon_anchor=(35,-45), icon_anchor=(35, 0), html='<div style="font-size: 8pt; color : black">' + str(hex) + '</div>', )).add_to(h3r8namegroup) 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=1, color='blue') h3r8group.add_child(my_PolyLine) # add possible tiles folium.TileLayer('cartodbpositron').add_to(m) folium.TileLayer('cartodbdark_matter').add_to(m) folium.TileLayer('openstreetmap').add_to(m) folium.TileLayer('Mapbox Bright').add_to(m) #folium.TileLayer('stamentoner').add_to(m) # add markers layer #marker_cluster = MarkerCluster().add_to(m) polargroup.add_to(m) #polar plot hsgroup.add_to(m) #hotspots dcgroup.add_to(m) #distance circles h3group.add_to(m) h3namegroup.add_to(m) h3fillgroup.add_to(m) m.keep_in_front(h3group) h3r8group.add_to(m) h3r8namegroup.add_to(m) # add the layer control folium.LayerControl(collapsed=False).add_to(m) m.save(hname + '//' + hname + '_map.html')
def test2(): h = '8928308280fffff' expected = (37.77670234943567, -122.41845932318311) assert h3.h3_to_geo(h) == pytest.approx(expected)
aperture_size = st.slider('Select the Grid Resolution', 0, 15, 7) hex_col = 'hex' + str(aperture_size) # find hexs containing the points incident_df[hex_col] = incident_df.apply( lambda x: h3.geo_to_h3(x.lat, x.lng, aperture_size), 1) # aggregate the points incident_g_df = incident_df.groupby(hex_col).size().to_frame( 'cnt').reset_index() # find center of hex for visualization incident_g_df['lat'] = incident_g_df[hex_col].apply( lambda x: h3.h3_to_geo(x)[0]) incident_g_df['lng'] = incident_g_df[hex_col].apply( lambda x: h3.h3_to_geo(x)[1]) # Functions def plot_scatter(df, metric_col, x='lng', y='lat', marker='.', alpha=1, figsize=(16, 12), colormap='viridis'): df.plot.scatter(x=x, y=y,
axis=1) Dest_CNT '''Dest Prob''' Dest_Prob = {g: {} for g in Grid_list} for idx, row in Dest_CNT.iterrows(): grid = row['Pickup_Grid'] Dest_Prob[grid][row['Dropoff_Grid']] = row['Prob'] np.save(os.path.join(Save_path, 'Dest_Prob'), Dest_Prob) '''Travel time''' print('Starting Travel time') Travel_time = {g: {g: 0 for g in Grid_list} for g in Grid_list} for g in Grid_list: for g_ in Grid_list: g = str(g) g_ = str(g_) Travel_time[g][g_] = int( (Point(h3.h3_to_geo(g)).distance(Point(h3.h3_to_geo(g_))) * 1.3 * 111) / 0.6) np.save(os.path.join(Save_path, 'Travel_time'), Travel_time)
def get_h3_centroids(hex_column: Series) -> List: centroid_lat_lon = hex_column.map(lambda hex: h3_to_geo(hex)) return [Point(geom[1], geom[0]) for geom in centroid_lat_lon]