2

I've been developing code to take a .csv with lat-long coords and convert them to LineStrings before creating parallel offsets for them. This has been working so far, but yesterday after clearing my variables, I started running into an error when running my code.

from shapely.geometry import LineString, Point
import pandas as pd
import geopandas as gpd

df = pd.read_csv("***/Kingsboro Rd NE.csv")
df = df.drop_duplicates()

geometry = [Point(xy) for xy in zip(df.lng, df.lat)]
df['geometry'] = geometry
df['agg'] = 1
df_line = gpd.GeoDataFrame(df.groupby(['agg'])['geometry'].apply(lambda x: LineString(x.tolist())), geometry='geometry')
df_line = df_line.set_crs("EPSG:4326")
df_line = df_line.to_crs("EPSG:2240")

df_offset_l = pd.DataFrame(df_line.iloc[0].geometry.parallel_offset(25, 'left', 2, mitre_limit = 100.0))

Everything runs fine until the final line, for which I get the following error:

ValueError: DataFrame constructor not properly called!

As I mentioned before, I have not been getting this error until yesterday, and I need it to work properly to perform the other data manipulation steps following this line of code.

Here's a sample of the input file:

lat long
33.85556214 -84.35139656
33.85569133 -84.35140193
33.85556214 -84.35139656
33.85553986 -84.35136706
33.85550868 -84.35134828
1
  • You try to create DataFrame or GeoDataFrame? – Kadir Şahbaz Jan 5 at 17:19
2

DataFrame or GeoDataFrame needs an iterable parameter but you pass shapely object (LineString) to it. So you get that error.

Make offset line:

offset_line = df_line.iloc[0].geometry.parallel_offset(25, 'left', 2, mitre_limit = 100.0)

Then,

  • If you need a DataFrame use this way:

    df_offset_l = pd.DataFrame({"geometry": [offset_line]})
    
    # OUTPUT (DataFrame)
    #     geometry
    # 0   LINESTRING (2240477.867 1402553.588, 2240476.3...
    
  • If you need a GeoDataFrame use this way:

    df_offset_l = gpd.GeoDataFrame({"geometry": [offset_line]})
    
    # OUTPUT (GeoDataFrame)
    #     geometry
    # 0   LINESTRING (2240477.866711137 1402553.58829784...
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.