I want to extract the x, y and z from shapefile polygon vertices where the geometry is multipolygon. Once extracted would like to have them in a geopandas data frame. I have seen a couple of approaches however the problem seems to be that when the shapefile is imported I can't get it to a feature that has a Z attribute. For example, the following works to get the x and y only, using a shapefile that was generated from ArcGIS Pro by converting the multipolygon format to its single parts.

import geopandas as gpd

gdf = gpd.read_file("C:/FG3D_2019/code/Collada_Geospatial/Boston_Mesh_Evaluation/Boston_Bldg1_SingleParts.shp")
print('gdf type', type(gdf))
geom_uu = gdf.unary_union
# Extract the point values that define the perimeter of the polygon
x, y = geom_uu.exterior.coords.xy

What I would like is to get the z coordinates from the geometry field so essentially; x, y, z = geom_uu.exterior.coords.xyz.

Has anyone had luck with this?

1 Answer 1

from shapely.geometry import Polygon
polygon = Polygon([[0.0, 0.0, 2.0], [1.0, 0.0, 3.0], [1.0, 1.0, 4.O], [0.0, 1.0, 5.0]])
x,y,z = zip(*polygon.exterior.coords)
(0.0, 1.0, 1.0, 0.0, 0.0)
(0.0, 0.0, 1.0, 1.0, 0.0)
(2.0, 3.0, 4.0, 5.0, 2.0)
  • Thank you gene, that worked.
    – EWarner
    Commented Mar 23, 2021 at 22:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.