Sedona fails to read Geoparquet with geometry stored as JTS geometry because it expects binary. Is there a workaround that does avoid the conversion to binary and back?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Workaround for Sedona Reading JTS Geometry in GeoParquet
Sedona expects geometries in GeoParquet to be stored in the binary format (WKB or WKB), but you're encountering issues when the data contains JTS geometry objects directly. Here are some potential workarounds:
Option 1: Pre-process the GeoParquet file
Convert the JTS geometries to WKB before Sedona reads them:
python
# Using PyArrow to convert JTS to WKB
import pyarrow.parquet as pq
import pyarrow as pa
from sedona.utils.geometry_serde import serialize_to_wkb
# Read the original file
table = pq.read_table('input.parquet')
# Convert JTS geometry column to WKB
geometry_col = table.column('geometry').to_pylist()
wkb_geometries = [serialize_to_wkb(geom) for geom in geometry_col]
# Create new table with WKB geometries
new_table = table.set_column(
table.schema.get_field_index('geometry'),
'geometry',
pa.array(wkb_geometries, type=pa.binary())
)
# Write new file
pq.write_table(new_table, 'output.parquet')
Option 2: Use a custom deserializer
If you can't modify the source file, you could create a custom DataFrame reader:
python
from pyspark.sql.functions import udf
from pyspark.sql.types import BinaryType
from sedona.utils.geometry_serde import serialize_to_wkb
# Register UDF to convert JTS to WKB
@udf(BinaryType())
def jts_to_wkb(geom):
return serialize_to_wkb(geom)
# Read the file then convert
df = spark.read.parquet('input.parquet')
df = df.withColumn('geometry', jts_to_wkb(df['geometry']))
Option 3: Use GeoPandas as an intermediate
python
import geopandas as gpd
# Read with GeoPandas which handles JTS
gdf = gpd.read_parquet('input.parquet')
# Convert to WKB
gdf['geometry'] = gdf['geometry'].apply(lambda g: g.wkb)
# Save back to Parquet
gdf.to_parquet('output.parquet')
Important Notes:
The most efficient long-term solution is to ensure your GeoParquet files are written with WKB geometry from the start.
The conversion step will have some performance overhead, but it's typically a one-time cost.
If you control the data pipeline, consider modifying the upstream process to write WKB directly rather than JTS objects.
Would you like more details on any of these approaches or have specific constraints on your workflow that might affect which solution is best?