TransWikia.com

Create LineString from lat/lon coords form CSV. with beginning and ending lat/lon?

Geographic Information Systems Asked by Bryon Betts on February 13, 2021

I am iterating through a CSV file of tornadoes, and for each row getting the beginning lat, beginning long, ending lat, and ending long. and trying to plot their line geometry on a map. I got the points for the starting points but I am having troubles creating the lines to show the path of the tornadoes.

I have:

for i, row in dataframe.iterrows():
    lx = row[begin_long],[ending_long]
    ly = row[begin_lat],[ending_lat]
    line_geometry.append(LineString(lx,ly))

One Answer

The LineString object is initalised with a list of coordinate pairs, e.g.

line = LineString([(0, 1), (2, 3)])

It looks like your coordinates are in a DataFrame. In that case you can use the DataFrame.apply method to create a linestring:

df = DataFrame([[0, 1, 2, 3], [4, 5, 6, 7]], columns=["x0", "x1", "y0", "y1"])

def make_line(row):
    coordinates = [(row["x0"], row["y0"]), (row["x1"], row["y1"])]
    return LineString(coordinates)

df["line"] = df.apply(make_line, axis=1)

Answered by Snorfalorpagus on February 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP