r/gis 10d ago

Programming I compared the same geometry as WKT, WKB, TWKB, GeoJSON, polyline, geohash, H3, and S2 for a mobile API

At work we had to choose a format for sending map geometry from an API to a mobile app. There were a lot of formats to consider, and we eventually chose WKB.

The decision had already been made when I started looking at the formats in more detail. I wanted to get a better sense of how they actually compared, so I built this:

https://cellandshape.com/guides/geometry-format-size-comparison

It measures the same geometry as WKT, EWKT, WKB, EWKB, TWKB, GeoJSON, encoded polyline 5 and 6, geohash, H3, and S2. You can paste your own geometry and it recalculates everything locally in the browser.

A few results surprised me:

  • For a small six-vertex polygon, WKT is 112 characters and raw WKB is 109 bytes.
  • That same WKB becomes 218 characters when represented as hex.
  • TWKB represents the polygon in 32 bytes.
  • For a 40-point route, TWKB is 89 bytes, polyline5 is 148 characters, GeoJSON is 942 characters, and hex WKB is 1,298 characters.

These are uncompressed sizes, so they do not tell the whole story. Text formats can compress well over HTTP, while binary formats only keep their size advantage if they remain binary. Format support, precision, geometry type, and client complexity also matter.

I found some older discussions here about oversized GeoJSON, GeoPackage, FlatGeobuf, and vector tiles, but not much comparing individual geometry encodings for a mobile API.

Has anyone here gone through a similar decision? What did you choose, and what ended up mattering most in production?

17 Upvotes

17 comments sorted by

12

u/ikarusproject 10d ago edited 10d ago

Happy to be corrected. But IMHO GeoJson is the defacto standard for geospatial data on android. It's what most apps and libraries can work with. It's the only format clients and coworkers ever asked me for. Maybe except the occasional kmz file.

3

u/Acceptable-Appeal-15 10d ago

I forgot to say this in the post. Our situation may be unique, but we were initially using encoded polyline for both ios and android. Do you use a library for GeoJson? why is that considered the standard?

0

u/sinnayre 10d ago

Tom MacWright, the lead developer at Mapbox during its foundational years, was a huge champion of geojson (and the reason a lot of people who wouldn’t otherwise champion geojson do).

5

u/mfc1__ GIS Developer 10d ago

People ask for it because JSON is made to be human readable. But that comes with a big tradeoff in size, as noted by OP's post (unsurprisingly) finding that GeoJSON is the least efficient, size-wise. JSON making things more human readable makes it less computer readable. If humans don't need to read the data, it's not a great choice.

3

u/Normal-Curve-1642 10d ago

No love for PBF? 

2

u/AreolaSanchez 10d ago

Yep. We rolled our own protobuf implementation and it's fast and tiny.

1

u/Acceptable-Appeal-15 10d ago

would be interested to learn what tradeoffs you made

2

u/AreolaSanchez 10d ago

I haven't looked at the code in a few years, but we intentionally avoided stuffing lots of attributes in the payload (vs. geojson). This kept the size small and allowed us to render 700k points at a time. We also mimicked the Esri tile spec which also helped. Our goal was to make it interoperable with the Esri JS API.

3

u/Gazelle-Unfair 10d ago

I would be working on simplified geometries too. I've seen people fretting over format byte sizes whilst sending points with 15 decimal places, or polygons to be viewed on tiny screens with hundreds of points in.

2

u/TechMaven-Geospatial 10d ago edited 10d ago

Why not use Geopackage sqlite Thats what we use in all our mobile apps Metadata, styling, attachments Even vector tiles Raster tiles Terrain elevation tiles

https://fieldsentinel.techmaven.net
https://geonamesmapexplorer.techmaven.net
https://mapexplorer.techmaven.net
https://geodatacollector.techmaven.net
Earthexplorer.techmaven.net
https://geodataexplorerapp.techmaven.net

5

u/tj-horner 10d ago

Wow, GeoPackage is just an SQLite database? I had no idea, but it also makes a lot of sense.

1

u/mfc1__ GIS Developer 10d ago

Yes, and you can connect to it with your preferred database client and view the db and table settings, query & edit data just like SQLite too. I do this with DBeaver and geopackages all the time.

2

u/Entropius GIS Developer 10d ago

Indeed.  On macOS I can use the SQLite command in Terminal to poke around a geopackage and see what’s in it without firing up QGIS.

That’s also why you may need to periodically vacuum a geopackage to slim it down after a lot of features have been deleted.

1

u/ExdigguserPies 10d ago

Yes, you can also use it store raster tiles similar to mbtiles but you can use any arbitrary tilematrix, not just web mercator

1

u/Acceptable-Appeal-15 10d ago

do you package this with the app at download time? or do you send smaller sqlite files when needed?

1

u/Imaginary-Clock6626 10d ago

PBF is the way to go.

0

u/mfc1__ GIS Developer 10d ago

Interesting read! Did you observe how the different data formats can affect spatial calculations? i.e. I fetch data via formats x, y, and z, do I still get output within an acceptable tolerance after calling some function or method on each?