If we have a table with a column with xy coordinates, for example:
We can get the difference between consecutive rows by using Pandas SHIFT function on columns.
".shift(-1)" will roll the rows 1 position backwards, and ".shift(1)" or simply ".shift()" will roll down your column by 1 position of the rows.
In our example, df1['x'].shift() will return:
0 NaN
1 455395.996360
2 527627.076641
3 536278.269190
4 553932.441097
5 568699.553239
6 569709.130272
7 573016.302437
8 575141.096777
9 580107.934566
if we want to calculate the euclidean distance between consecutive points, we can use the shift associated with numpy functions numpy.sqrt and numpy.power as following:
df1['diff']= np.sqrt(np.power(df1['x'].shift()-df1['x'],2)+
np.power(df1['y'].shift()-df1['y'],2))
Resulting in:
0 NaN
1 89911.101224
2 21323.016099
3 204394.524574
4 37767.197793
5 46692.771398
6 13246.254235
7 2641.201366
8 15153.187527
9 15853.974422
Python programming, with examples in hydraulic engineering and in hydrology.
Showing posts with label geometry. Show all posts
Showing posts with label geometry. Show all posts
Wednesday, May 30, 2018
Thursday, August 31, 2017
Calculate the centroid of a polygon with python
In this post I will show a way to calculate the centroid of a non-self-intersecting closed polygon.
I used the following formulas,
as shown in https://en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon .
In the following code, the function receives the coordinates as a list of lists or as a list of tuples.
from math import sqrt def centroid(lstP): sumCx = 0 sumCy = 0 sumAc= 0 for i in range(len(lstP)-1): cX = (lstP[i][0]+lstP[i+1][0])*(lstP[i][0]*lstP[i+1][1]-lstP[i+1][0]*lstP[i][1]) cY = (lstP[i][1]+lstP[i+1][1])*(lstP[i][0]*lstP[i+1][1]-lstP[i+1][0]*lstP[i][1]) pA = (lstP[i][0]*lstP[i+1][1])-(lstP[i+1][0]*lstP[i][1]) sumCx+=cX sumCy+=cY sumAc+=pA print cX,cY,pA ar = sumAc/2.0 print ar centr = ((1.0/(6.0*ar))*sumCx,(1.0/(6.0*ar))*sumCy) return centr
Subscribe to:
Posts (Atom)