[GIS] Geometry Data in GeoDataFrame will not plot

geodataframegeopandas

I want to import a CSV to use as a geodataframe. When I plot it, nothing shows, which makes me think I'm doing something wrong. Here's my code.

import pandas as pd
from geopandas import GeoDataFrame
myfile = 'data.csv'
df = pd.read_csv(myfile)
crs = {'init': 'epsg:4269'}
gdf = GeoDataFrame(df, crs=crs)
gdf.plot()

The 'geometry' column in my CSV contains two rows, which contain LineStrings e.g.

  1. LINESTRING(-117.674411 33.561825,-117.61164 33.589388)
  2. LINESTRING(-117.687375 33.602688,-117.690752 33.604706)

Best Answer

You need to ensure you have actual geometry objects in the dataframe, and not only a string representation of it (which is likely since it is imported from csv). You can convert the column with strings (assuming the strings are in WKT or "Well Known Text" format) to actual geometries with:

from shapely import wkt
df['geometry'] = df['geometry'].apply(wkt.loads)

For a full example, see http://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html#from-wkt-format