0

What will be the conditions if I want to check whether two geometries are same or not.

I have coordinates of two geometries. Both geometries are same. But First geometry is rotated and second geometry is not rotated, however, input order (coordinates numbers) of the geometry is now changed. How can I prove in python code that both geometries are same even after oriented in different way in 2D space.

#Coordinates for first geometry,

X1 = [0.0, 0.87, 1.37, 1.87, 2.73, 3.6, 4.46, 4.96, 5.46, 4.6, 3.73, 2.87, 2.0, 1.5, 1.0, 0.5, 2.37, 3.23, 4.1]
Y1 = [0.0, 0.5, -0.37, -1.23, -0.73, -0.23, 0.27, -0.6, -1.46, -1.96, -2.46, -2.96, -3.46, -2.6, -1.73, -0.87, -2.1, -1.6, -1.1]

#Coordinates for second geometry,
X2 = [2, 4, 4, 2, 3, 2, 4, 3, 1, 3, 4, 3, 1, 2, 0, 3, 4, 2, 0]
Y2 = [3, 4, 2, 1, 3, 2, 1, 0, 0, 2, 3, 4, 1, 4, 0, 1, 0, 0, 1]

What I have tried so far: I tried to verify using below conditions.

(1) Number of node in both geometries should be same in both geometries.

#---------------------------CONDITION: 1------------------------------------------------#

# Condition: Number of nodes in both geometries should be same
node_number1 = df_1['node_number'].tolist()                        # converting 'node number' column from first dataframe to a list 
node_number2 = df_2['node_number'].tolist()                        # converting 'X_coordinate' column from second dataframe to a list

(2) Distance between two consecutive nodes (such as Node 1 & node2, Node 2 & Node 3) should be same in both geometries.

#---------------------------CONDITION: 2------------------------------------------------#

# Condition: Distance between two successive nodes must be 1.
x_shift1 = df_1['New_X_coordinate_reformed'].shift(-1)                          # In first dataframe shift the index by (-1) from X_coordinate values
y_shift1 = df_1['New_Y_coordinate_reformed'].shift(-1)                          # In first dataframe shift the index by (-1) from Y_coordinate values

# Finding a distance between two coordinates (nodes)
# General formula for this: square root of [(x2 - x1)^2 - (y2 - y1)^2]
nodal_dist_df_1 = np.sqrt((x_shift1 - df_1['New_X_coordinate_reformed'])**2 + (y_shift1 - df_1['New_Y_coordinate_reformed'])**2).dropna().to_list()
nodal_dist_df_1 = [round(num,2) for num in nodal_dist_df_1]       # rounding a obtained values in 'nodal_dist_df1' by 2 decimals 
  
x_shift2 = df_2['New_X_coordinate_reformed'].shift(-1)                          # In second dataframe shift the index by (-1) from X_coordinate values
y_shift2 = df_2['New_Y_coordinate_reformed'].shift(-1)                          # In second dataframe shift the index by (-1) from Y_coordinate values

# Finding a distance between two coordinates (nodes)
# General formula for this: square root of [(x2 - x1)^2 - (y2 - y1)^2]
nodal_dist_df_2 = np.sqrt((x_shift2 - df_2['New_X_coordinate_reformed'])**2 + (y_shift2 - df_2['New_Y_coordinate_reformed'])**2).dropna().to_list()
nodal_dist_df_2 = [round(num,2) for num in nodal_dist_df_2]       # rounding a obtained values in 'nodal_dist_df1' by 2 decimals 

(3) Distance to every node of geometry from 1st node should be same in both geometries.

#---------------------------CONDITION: 3------------------------------------------------#

# Condition: Distances between first node to each node should be same in both geometries.

i=0
dist_from_N1_1 = []                                                # For first dataframe initializng a blank list
for i in range(len(df_1['New_X_coordinate_reformed'])-1): 
    
    # Finding a distance between two coordinates (nodes) in first dataframe
    # General formula for this: square root of [(x2 - x1)^2 - (y2 - y1)^2] where x1 and y1 will be constant and second coordinate will vary from x2 to x15
    d = np.sqrt((df_1['New_X_coordinate_reformed'][i+1]-df_1['New_X_coordinate_reformed'][0])**2 + (df_1['New_Y_coordinate_reformed'][i+1]-df_1['New_Y_coordinate_reformed'][0])**2)
    dist_from_N1_1.append(d)                                       # appending a distance value in a list initialized before 'for' loop
    i = i+1
dist_from_N1_1 = [round(num,2) for num in dist_from_N1_1]       # rounding a obtained values in 'nodal_dist_df1' by 2 decimals 

i=0
dist_from_N1_2 = []                                                # For second dataframe initializng a blank list
for i in range(len(df_2['New_X_coordinate_reformed'])-1):
    
    # Finding a distance between two coordinates (nodes) in second dataframe
    # General formula for this: square root of [(x2 - x1)^2 - (y2 - y1)^2] where x1 and y1 will be constant and second coordinate will vary from x2 to x15
    d_N2 = np.sqrt((df_2['New_X_coordinate_reformed'][i+1]-df_2['New_X_coordinate_reformed'][0])**2 + (df_2['New_Y_coordinate_reformed'][i+1]-df_2['New_Y_coordinate_reformed'][0])**2)
    dist_from_N1_2.append(d_N2)
    i = i+1
dist_from_N1_2 = [round(num,2) for num in dist_from_N1_2]       # rounding a obtained values in 'nodal_dist_df1' by 2 decimals 

Now after analysing three conditions, I have checked them whether they satisfy or not.

#---------------------------Checking of all three condition-----------------------------#

if (len(node_number2) == len(node_number1) and (nodal_dist_df_1 == nodal_dist_df_2) and dist_from_N1_1 == dist_from_N1_2):
    print('All three conditions are satisfied.')
    print('Hence Yes, two geometries are same.')
else:
    print('No, two geometries are not same.')

But this approach gives oncorrect result. Kindly help me by giving your suggestions.

Urvesh
  • 331
  • 4
  • 15
  • Can you show the code you've tried so far? Also, do you know in advance what is the type of transformation your object is undergoing? – WArnold Sep 09 '22 at 09:36
  • I have edited my post and added the code I have written. I want to verify whether two geometries are same or not. – Urvesh Sep 09 '22 at 10:26
  • see [How to compare two shapes?](https://stackoverflow.com/a/22166032/2521214) – Spektre Sep 09 '22 at 10:40
  • @Spektre, I have read the answer, but I could not understand how will that approach be useful for my task. WOuld you please explain me more about it? – Urvesh Sep 09 '22 at 11:11
  • @Urvesh very simply once you convert your geometries to relative polar coordinates (all lines are represented by `angle_change,line_length` then the two geometries will become the same regardless of their rotation ... – Spektre Sep 09 '22 at 11:48
  • @Spektre do you mean, I should convert every cartesian coordinate into (Theta and r). Then I will be having a list of Theta and r coordinates of both geometries. How should I check whether both sets of coordinates belong to same geometry or not? – Urvesh Sep 09 '22 at 15:03
  • @Spektre It would be nice if you give some hints of code and procedure. This is better than we are writing to each other, it will prevent my confusion and will save time also. – Urvesh Sep 09 '22 at 15:05
  • 2D polygons usually consist of lines `line(x0,y0,x1,y1)` in cartesian so in first step you convert each line to absolute angle relative polar: `ang=atan2(y1-y0,x1-x0); len=sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));` and in next step you convert angle to relative so substract first line angle from all angles. Now you will have angle and length per each line instead of 4 coordinates. to compare your 2 shapes you just check if they the same +/- some margin of error so simply check if all angles and lengths are close to each other. I do not code in Python so do not excpect code in it ... – Spektre Sep 09 '22 at 16:40

0 Answers0