Skip to content

Instantly share code, notes, and snippets.

@aSipiere
Created September 17, 2020 11:35
Show Gist options
  • Save aSipiere/e74c8b34f0503f37d99d4a91ab6a6989 to your computer and use it in GitHub Desktop.
Save aSipiere/e74c8b34f0503f37d99d4a91ab6a6989 to your computer and use it in GitHub Desktop.
import pandas as pd
from pyproj import Transformer
def towgs84(row, lat_col, lon_col):
"""An apply function for a pyproj transformer from 4326/WGS84 to 3857/web mercator
:param row: a row of a pandas dataframe as supplied by df.apply().
:param lat_col: str of the lat col
:param lon_col: str of the lon col
"""
transformer = Transformer.from_crs("epsg:4326", "epsg:3857")
row['lon'], row['lat'] = transformer.transform(row[lon_col], row[lat_col])
return row
df = df.apply(to_web_mercator, axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment