2

I have this little issue that I can't really make a good solution for. I have this dataframe:

index                                    id  x_polygon  y_polygon  \
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  44.863280   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  44.863280   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  44.863280   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  44.863280   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  44.863280   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  44.863280   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  44.863280   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  44.863280   

                               edges.id  
0  ac249205-3b06-42b3-b6fb-85763fdfced3  
0  d2155659-56c0-4520-9330-f9b61a097ca7  
0  c4582dd7-2b7b-4453-8374-c2e3da335982  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d  
0  ac249205-3b06-42b3-b6fb-85763fdfced3  
0  d2155659-56c0-4520-9330-f9b61a097ca7  
0  c4582dd7-2b7b-4453-8374-c2e3da335982  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d  
0  ac249205-3b06-42b3-b6fb-85763fdfced3  
0  d2155659-56c0-4520-9330-f9b61a097ca7  
0  c4582dd7-2b7b-4453-8374-c2e3da335982  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d  
0  ac249205-3b06-42b3-b6fb-85763fdfced3  
0  d2155659-56c0-4520-9330-f9b61a097ca7  
0  c4582dd7-2b7b-4453-8374-c2e3da335982  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d  

which gives the coordinates of edges of polygons (in this particualr case, it is one single polygon). The id is the name of the polygon and edges.id is the id id of each edge in theis polygon. Now, I ususally only look att the polygon and I usually use geopandas to transform this data into POLYGON obeject to work with, doing this:

geostore_obstacles_geometry = gpd.GeoDataFrame(geometry=geostore_obstacles.groupby(['id']).apply(
    lambda g: Polygon(gpd.points_from_xy(g['x_polygon'], g['y_polygon']))))

But now, I realize that I actually would like to work with each vertice in this polygon as if they where independent polygons. This means that I no longer can groupby id as I do. So, I need to define a column giving a name to each vertice, preferably using the edge name as a base. My initial idea was to check the difference between x_polygon and y_polygon values between consecutive rows. For instance

 index                                    id  x_polygon  y_polygon  \
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  37.251873   

                               edges.id  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d  

would give me one of the lines since the values of x_polygon are different while the others are not. So, I thought of doing this

geostore_obstacles_0['Diff_x'] = np.abs(round(geostore_obstacles_0.groupby('id')['x_polygon'].diff().fillna(1),0))
geostore_obstacles_0 = geostore_obstacles_0.sort_values(['index','edges.id'])
geostore_obstacles_0['Diff_y'] = np.abs(round(geostore_obstacles_0.groupby('id')['y_polygon'].diff().fillna(1),0))

which gives

index                                    id  x_polygon  y_polygon  \
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  32.987843  37.251873   
0      0  168bb0ea-3748-42ad-84c3-aa20e62c73b3  33.567165  37.251873   

                               edges.id  Diff_x  Diff_y  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d     0.0     1.0  
0  ab68fa67-2f46-49cf-b319-8454d5820f9d     0.0     0.0  

There are two problems:

  1. I do not know what to do with it to give a name to that feature
  2. This fails if (an it might happen), any of the polygon aren't at 0 or 90 degrees angles.

One approach I would love to have is to pick each individual segment from the geopandas polygons.

I know there is a package called gemgis to explode polygons, but it has a dependency on rasterio which I fail to install.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.