SpatialiteForms

December 3, 2018 · View on GitHub

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:

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) :

  1. 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   
 └── ...
  1. Get iOS framework from here and add as Native Reference to iOS project

Targeted architecture (these architectures are compatible with most devices)

PlatformArchComments
Androidx86will also run on x86_64
Androidarmeabi-v7awill also run on arm64-v8a
iOSx86_64for emulators
iOSarm64for devices starting with iPhone 5s

Supported Spatialite functionality

FunctionalityAndroidiOS
HasEpsg
  • - [x]
  • - [x]
HasFreeXL
  • - [ ]
  • - [ ]
HasGeoCallbacks
  • - [ ]
  • - [ ]
HasGeos
  • - [x]
  • - [x]
HasGeosAdvanced
  • - [x]
  • - [x]
HasGeosTrunk
  • - [ ]
  • - [ ]
HasIconv
  • - [x]
  • - [x]
HasLibxml2
  • - [x]
  • - [x]
HasMathSQL
  • - [x]
  • - [x]
HasProj
  • - [x]
  • - [x]

Spatialite component versions

ComponentAndroid versioniOS version
Geos3.5.0-CAPI-1.9.0 r40843.5.0-CAPI-1.9.0 r4084
Libxml22.9.12.9.4
Proj4Rel. 4.9.2, 08 September 2015Rel. 4.9.2, 08 September 2015
Spatialite4.4.0-topo-14.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.

Usage

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)

  1. Add countryData.db under Assets folder on Android (build action - Android asset) and Resources folder on iOS (build action - Bundle resource)

  2. Define a Region class

public class Region {  
  public string Name { get; set; }  
}
  1. 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.

Nuget package

https://www.nuget.org/packages/Plugin.SpatialiteForms/

License

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.