Forum

Author Topic: Get GeoPandas DataFrame from chunk shapes  (Read 496 times)

katemosolova

  • Newbie
  • *
  • Posts: 4
    • View Profile
Get GeoPandas DataFrame from chunk shapes
« on: October 13, 2024, 06:39:29 PM »
Good day,

Is there a way to obtain shape groups (chunk.shapes.groups[index]) in a format suitable for working with GeoPandas? Currently, I have to export them to GeoJSON and then read them using GeoPandas.

Is it possible to directly create a GeoDataFrame from Metashape shapes without the need for exporting to a file?

Thank you!

katemosolova

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Get GeoPandas DataFrame from chunk shapes
« Reply #1 on: October 16, 2024, 09:58:01 AM »

from shapely.geometry import Polygon
import geopandas as gpd

def extract(chunk):
   for shape in chunk.shapes:

        if shape.geometry.type != Geometry.Type.PolygonType:
            continue

       
        polygon = Polygon([(v.x, v.y) for v in shape.geometry.coordinates[0]])

        road_seg.append({
                          'geometry': polygon
                      })

    return gpd.GeoDataFrame(road_seg, crs=chunk.crs.wkt) if road_seg else None