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 requests | |
import json | |
def geolocate_ip(ip): | |
'''Get location data based on ip. | |
Note: Limited (Free) to a 1000 requests per day. | |
TODO(Leonard): Handler for being behind a vpn | |
Inputs: |
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 parse_tuple(string): | |
'''Parse a string like '(23.0,11.3)' to tuple of floats.''' | |
try: | |
s = eval(str(string)) | |
if type(s) == tuple: | |
return s | |
return | |
except: | |
return |
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
from geopandas import GeoDataFrame | |
from shapely.geometry import Point | |
def to_gdf(df, lon, lat, crs={'init': 'epsg:4326'}): | |
'''Converts a pandas dataframe with coordinate columns to a geopandas dataframe. | |
Inputs: | |
df: pandas dataframe | |
lon: str; name of longitude column | |
lat: str; name of Latitude column |
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 overlapped_bar(df, show=False, width=0.9, alpha=.5, | |
title='', xlabel='', ylabel='', **plot_kwargs): | |
"""Like a stacked bar chart except bars on top of each other with transparency""" | |
xlabel = xlabel or df.index.name | |
N = len(df) | |
M = len(df.columns) | |
indices = np.arange(N) | |
colors = ['steelblue', 'firebrick', 'darksage', 'goldenrod', 'gray'] * int(M / 5. + 1) | |
for i, label, color in zip(range(M), df.columns, colors): | |
kwargs = plot_kwargs |
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
from shapely.geometry import * | |
def remove_third_dimension(geom): | |
if geom.is_empty: | |
return geom | |
if isinstance(geom, Polygon): | |
exterior = geom.exterior | |
new_exterior = remove_third_dimension(exterior) |
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 dualExplode(row): | |
"""Explode weights and category_ids list elements to separate rows. | |
Args: | |
row: Row | |
Yield: | |
Row(**newDict) | |
""" | |
rowDict = row.asDict() | |
xList = rowDict.pop('x') | |
yList = rowDict.pop('y') |