Created
August 7, 2018 12:30
-
-
Save LeonardAukea/2a908047894bfc5d1bef703daef4fd00 to your computer and use it in GitHub Desktop.
Converts pandas dataframe with coordinates to geopandas dataframe.
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 | |
crs: dict; projection type | |
Returns: | |
gdf: Geopandas dataframe | |
''' | |
geometry = [Point(xy) for xy in zip(df[f'{lon}'], df[f'{lat}'])] | |
df = df.drop([f'{lon}', f'{lat}'], axis=1) | |
crs = {'init': 'epsg:4326'} | |
gdf = GeoDataFrame(df, crs=crs, geometry=geometry) | |
return gdf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment