anboralabs / spatia-room

Implementation of spatia lite database to android component Room
MIT License
41 stars 4 forks source link

Add TypeConverters for Spatialite Geometries #24

Closed johan12345 closed 2 years ago

johan12345 commented 2 years ago

As discussed in https://github.com/johan12345/EVMap/issues/97, I have implemented TypeConverters for some basic geometry types in Spatialite based on the binary format documented here. Currently the 2D POINT, LINESTRING and POLYGON types are implemented - I think I will not need other types for my purposes anytime soon. But of course the other types could easily be added later.

I also added a simple example of how to create a geometry column in the database in the demo app. To work with geometry fields one still needs to manually call RecoverGeometryColumn (and, if desired, CreateSpatialIndex) in a callback after Room created the database, as Room will just initialize the geometry column as a simple BLOB column without the extra metadata. I'm not sure if there is a good way to automate this, it would probably require extending the Room compiler itself.

In contrast to the TypeConverters implemented by @mrclayman in #7 using the Well-Known-Text (WKT) representation as an intermediate step, this approach makes it possible to not only read, but also write geometry fields using Room's ORM. The performance may also be slightly better as we bypass the conversion to text.

The PR also contains an upgrade to the Kotlin version, which is necessary to use the minOf and maxOf extension functions on lists (added in Kotlin 1.4).

addresses #3, partially addresses #7

dalgarins commented 2 years ago

@johan12345 thanks a lot for this contribution.