buckaroo icon indicating copy to clipboard operation
buckaroo copied to clipboard

Vector serialization of Geopandas data

Open paddymul opened this issue 11 months ago • 1 comments

Checks

  • [X] I have checked that this enhancement has not already been requested

How would you categorize this request. You can select multiple if not sure

Display (is this related to visual display of a value), Performance, Other (please describe in Issue Description)

Enhancement Description

Buckaroo should serialize GeoPandas geometry columns for the frontend in a way that allows a specially crafted displayer to render vector graphics for that column.

Pseudo Code Implementation

Here is some raw code

Note that this should run in GeopandasBuckarooWidget._df_to_obj

def serialize_geom(geom):
    # Shapely interface to extract coordinates is... not uniform
    # For simplicity, implement it for Polygons and later for other objects...
    if geom.geom_type == 'Polygon':
      return numpy.array(geom.exterior.coords.xy).T.tolist()
    elif geom.geom_type == 'MultiPolygon':
      return [serialize_geom(g) for g in geom.geoms]
    else:
      raise NotImplementedError("No serialization for %s" % self.geom_type)

def serialize_to_dict(gdf):
  """
  Serialize GeoDataFrame
  """
  import numpy

  # Let's work with 'index' output structure, it's closer to what we want
  dct = gdf.to_dict(orient='index')

  items = []
  for indx, fields in dct.items():
    # Create feature, with "id" value from GDF index
    item = { 'id': indx }

    # Transform 'geometry' object into list of coordinates
    geom = fields.pop('geometry')
    item['geometry'] = serialize_geom(geom)

    # Add all the remaining fields to output feature/item
    item.update(fields)
    items.append(item)

  return items

Ideally the transform will be compatible with a future move to arrow/parquet serialization

Prior Art

https://github.com/paddymul/buckaroo/issues/224

paddymul avatar Mar 08 '24 17:03 paddymul

@paddymul you may already be aware of this but if not https://github.com/developmentseed/lonboard may be a useful reference here when looking to make this more performant - it exposes and interface to visualise GeoDataFrames in deck.gl where geoarrow is used as the transfer format betwen python and the browser (but you would then need a different way to render the geometries, rather than using the shapely svg (as I understand is what's currently happening from the linked issue above). I'm afraid I'm not across the finer details myself, but if you've got questions I'm happy to try and answer

m-richards avatar Apr 04 '24 10:04 m-richards