0
votes

I have points shapefile and polygons shapefile. I would like to find out the highest point within each polygon. I have done an intersection to find out which points belong to each polygon:

import geopandas as gpd

from geopandas.tools import sjoin

point = gpd.GeoDataFrame.from_file(pointSHP)
print("POINT", point)
poly = gpd.GeoDataFrame.from_file(polygonSHP)
print("POLY", poly)
points_within_poly = gpd.sjoin(point, poly, how="inner", op='intersects')
print(points_within_poly.head(10))

Now I would like to select the highest point for each index_right. I think is a matter of sorting by Z value in geometry column but I am having problems to do it. I don't know how to extract the Z coordinate from geometry using geopandas. Finally I would like to do a spatial join and populate the Z value to the nearest point (another shapefile).

Thank you

1
To get the value of z from 3D point in your case, just use point.geometry[0].z for the first item in the geo-dataframe. - swatchai
thank you swatchai, do you know how can I keep the point with the highest value for each polygon? After that, I just need to join that point with another set of points within the same polygon by using the polygon ID. Thanks :) - Javier

1 Answers

0
votes

I'll focus on the first question, use this to extract the z coordinate

import geopandas as gpd
gdf = gpd.read_file("file.shp")
gdf['z'] = None
gdf['z'] = gdf.geometry.apply(lambda x: list(x.coords)[0][2])

or alternatively,

z_values = [list(x.coords)[0][2] for x in gdf.geometry]
gdf['z'] = None
gdf['z'] = z_values

explanation: for each geometry, make a list of the coords from the geometry, take the first[0] of the coords list, and take the third [2] value which is the z value of the geometry (because the first [0] is the x, and the second [1] is the y value). the spatial join part from nearest neighbor is too long to be written. I suggest asking a new question for this topic.