I have a DataFrame where one column consists of lines stored like so:

'[[-7.61, 42.15],[-6.62, 43.16],[-8.9, 41.0],[-9.2, 40.5]]'

Is there an easy way to read these lines directly into shapely LineStrings so I can treat this as a GeoDataFrame?


Actually, I just sorted it out, though I am happy for suggested improvements:

line = '[[-7.61, 42.15],[-6.62, 43.16],[-8.9, 41.0],[-9.2, 40.5]]'

import ast
line = LineString(ast.literal_eval(line))
|improve this answer|||||
  • 1
    That looks JSON-y so you could also do line = LineString(json.loads(text)) – mikewatt Nov 29 '18 at 2:31
  • Great, thanks! Even better without another library. – atkat12 Nov 29 '18 at 5:18

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.