Posts

Showing posts with the label Geopandas

Converting Pandas DataFrame To GeoDataFrame

Answer : Convert the DataFrame's content (e.g. Lat and Lon columns) into appropriate Shapely geometries first and then use them together with the original DataFrame to create a GeoDataFrame. from geopandas import GeoDataFrame from shapely.geometry import Point geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)] df = df.drop(['Lon', 'Lat'], axis=1) gdf = GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) Result: Date/Time ID geometry 0 4/1/2014 0:11:00 140 POINT (-73.95489999999999 40.769) 1 4/1/2014 0:17:00 NaN POINT (-74.03449999999999 40.7267) Since the geometries often come in the WKT format, I thought I'd include an example for that case as well: import geopandas as gpd import shapely.wkt geometry = df['wktcolumn'].map(shapely.wkt.loads) df = df.drop('wktcolumn', axis=1) gdf = gpd.GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) Update 201912: The official documentation at h...

Buffering Line With Flat Cap Style Using GeoPandas?

Answer : GeoPandas isn't passing through all arguments to the shapely buffer method. Instead you can use the standard pandas apply method to call buffer on each geometry individually, e.g.: # Assumes that geometry will be the geometry column capped_lines = df.geometry.apply(lambda g: g.buffer(100, cap_style=2)) Also, not that this returns a GeoPandas GeoSeries object, so if you need the attributes (and projection for that matter, though that may be an outstanding issue) you'll need to overwite the geometry column in the original GeoDataFrame. GeoPandas now pass kwargs to shapely, so you can do below now: gdf.geometry.to_crs("epsg:3857").buffer(10, cap_style=2) PR: https://github.com/geopandas/geopandas/pull/535 Update: reason for change crs to 3857 is control on buffer radius in meter, else geopandas raise below warning: UserWarning: Geometry is in a geographic CRS. Results from 'buffer' are likely incorrect. Use 'GeoSeries.to_crs()' to ...