I am using pyproj inverse transform to add azimuth and distance "info" to an ordered geodataframe.
I am trying to iterate over all the rows in the geodataframe to add the "info" as a tuple to a new column in the geodataframe, but there is something amiss with my lambda function.
Running the following:
Windows 10
conda 4.8.2
Python 3.8.3
shapely 1.7.0 py38hbf43935_3 conda-forge
pyproj 2.6.1.post1 py38h1dd9442_0 conda-forge
Example geodataframe:
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from shapely.geometry import LineString
import pyproj
myid = [1, 1, 1, 1, 1]
myorder = [1, 2, 3, 4, 5]
lat = [36.42, 36.4, 36.32, 36.28,36.08]
long = [-118.11, -118.12, -118.07, -117.95, -117.95]
df = pd.DataFrame(list(zip(myid, myorder, lat, long)), columns =['myid', 'myorder', 'lat', 'long'])
gdf_pt = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['long'], df['lat']))
gdf_pt = gdf_pt.set_crs(epsg=4326)
print(gdf_pt.crs)
display(gdf_pt)
ax = gdf_pt.plot();
ax.set_aspect('equal')
ax.set_xticklabels(ax.get_xticklabels(), rotation=90);
Expected output:
I am iterating over i
row number, starting at the second row, and then using that row & the previous to calc the azimuths & distances. The results are as expected.
g = pyproj.Geod(ellps='WGS84')
for i, r in gdf_pt.iloc[1:].iterrows():
myinfo = g.inv(gdf_pt.long[i], gdf_pt.lat[i], gdf_pt.long[i-1], gdf_pt.lat[i-1])
gdf_pt.loc[i, 'az_fwd'] = myinfo[0]
gdf_pt.loc[i, 'az_back'] = myinfo[1]
gdf_pt.loc[i, 'dist'] = myinfo[2]
display(gdf_pt)
Wrong output, need help here!
I am trying to iterate over i
row number, starting at the second row, and then using that row & the previous to calc the azimuths & distances. However, as seen in the table display, it is not passing through i
as I expected, when compared to the expected results above. It is just reading the last effort and/or over-writing
g = pyproj.Geod(ellps='WGS84')
for i, r in gdf_pt.iloc[1:].iterrows():
gdf_pt['mytuple'] = gdf_pt.apply(lambda x:
g.inv(gdf_pt.long[i], gdf_pt.lat[i],
gdf_pt.long[i-1], gdf_pt.lat[i-1]), axis=1)
display(gdf_pt)
I also tried the following, which threw the error shown:
g = pyproj.Geod(ellps='WGS84')
for i, r in gdf_pt.iloc[1:].iterrows():
gdf_pt.loc[i, 'mytuple'] = gdf_pt.apply(lambda x:
g.inv(gdf_pt.long[i], gdf_pt.lat[i],
gdf_pt.long[i-1], gdf_pt.lat[i-1]), axis=1)
display(gdf_pt)
ValueError: Incompatible indexer with Series
gdf.assign