Awesome
SpatialiteForms
A nuget package that enables spatialite for Xamarin Forms. As an example this allows to do offline reverse geocoding on mobile devices (this barely scratches the surface of Spatialite, but it's a very common usecase for mobile dev)
It's entirely based on:
- sqlite-net-base and
- Spatialite and
- SQLitePCL.raw for SQLitePCLRaw.bundle_e_sqlite3 - which gives better iOS support for sqlite extensions
If you find this library useful, please support the above projects.
What this means is that it exposes a SQLiteConnection that supports spatial SQL capabilities.
To enable this functionality native libraries/framework is required (apart from the nuget package itself) :
- Get Android libraries from here and add under libs folder, with build action as "AndroidNativeLibrary". Folder structure under Android project:
.
├── Assets
├── ...
├── libs
│ ├── armeabi-v7a
│ ├── libspatialite.so
│ ├── x86
│ ├── libspatialite.so
└── ...
- Get iOS framework from here and add as Native Reference to iOS project
Platform | Arch | Comments |
---|---|---|
Android | x86 | will also run on x86_64 |
Android | armeabi-v7a | will also run on arm64-v8a |
iOS | x86_64 | for emulators |
iOS | arm64 | for devices starting with iPhone 5s |
Functionality | Android | iOS |
---|---|---|
HasEpsg | <ul><li> - [x] </li></ul> | <ul><li> - [x] </li></ul> |
HasFreeXL | <ul><li> - [ ] </li></ul> | <ul><li> - [ ] </li></ul> |
HasGeoCallbacks | <ul><li> - [ ] </li></ul> | <ul><li> - [ ] </li></ul> |
HasGeos | <ul><li> - [x] </li></ul> | <ul><li> - [x] </li></ul> |
HasGeosAdvanced | <ul><li> - [x] </li></ul> | <ul><li> - [x] </li></ul> |
HasGeosTrunk | <ul><li> - [ ] </li></ul> | <ul><li> - [ ] </li></ul> |
HasIconv | <ul><li> - [x] </li></ul> | <ul><li> - [x] </li></ul> |
HasLibxml2 | <ul><li> - [x] </li></ul> | <ul><li> - [x] </li></ul> |
HasMathSQL | <ul><li> - [x] </li></ul> | <ul><li> - [x] </li></ul> |
HasProj | <ul><li> - [x] </li></ul> | <ul><li> - [x] </li></ul> |
Component | Android version | iOS version |
---|---|---|
Geos | 3.5.0-CAPI-1.9.0 r4084 | 3.5.0-CAPI-1.9.0 r4084 |
Libxml2 | 2.9.1 | 2.9.4 |
Proj4 | Rel. 4.9.2, 08 September 2015 | Rel. 4.9.2, 08 September 2015 |
Spatialite | 4.4.0-topo-1 | 4.4.0-RC1 |
There's small version mismatches which shouldn't cause problems, but please raise an issue if anything occurs.
Current versioning/capabilities information can be accessed via SpatialiteInfo property, available in the SpatialiteConnection class.
SpatialiteForms also allows to use "pre-packaged" database files using Assets folder in Android and Resources folder for iOS.
<h2>Usage</h2>Example using countryData.db on how to prepackage a database with geometry data within the xamarin app and to get a Spatialite-capable SQLiteConnection.
Database countryData was created using Spatialite-GUI (very powerful tool) through which shape data was imported (shape data originated from here)
-
Add countryData.db under Assets folder on Android (build action - Android asset) and Resources folder on iOS (build action - Bundle resource)
-
Define a Region class
public class Region {
public string Name { get; set; }
}
- Get the Spatialite capable SQLiteConnection
var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "countryData.db");
SpatialiteConnection spatialite = CrossSpatialiteForms.Current.GetSpatialiteConnection(dbPath, "countryData.db", true);
var info = spatialite.SpatialiteInfo;
var region = spatialite.SQLiteConnection.Query<Region>("select name from region where within(Makepoint(-100.7594387, 46.77519), geometry);").FirstOrDefault();
the result region should have the name of North Dakota
Again this is only a small thing of what spatialite is capable of, please check official Spatialite documentation for more details.
<h2>Nuget package</h2>https://www.nuget.org/packages/Plugin.SpatialiteForms/
<h2>License</h2>This project is licensed under GPL3
Spatialite libraries have been compiled from original source code and no changes were made to it.
If you believe that I have infringed your copyright in any way please let me know and I will remove any code that is your copyright ASAP.