Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector serialization of Geopandas data #251

Open
1 task done
paddymul opened this issue Mar 8, 2024 · 1 comment
Open
1 task done

Vector serialization of Geopandas data #251

paddymul opened this issue Mar 8, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@paddymul
Copy link
Owner

paddymul commented Mar 8, 2024

Checks

  • 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

#224

@paddymul paddymul added the enhancement New feature or request label Mar 8, 2024
@m-richards
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants