Search code examples
iosxcodesqlitemakefilespatialite

ST_Within,ST_IsValid or any other geos or rtreetopo related function not working in spatialite built for IOS plateform


I have built spatialite for IOS using https://github.com/mrclayman/libspatialite-ios/blob/libspatialite-5/Makefile makefile.

Included libraries in my app as follow

enter image description here

Added these to the search paths in your Xcode project's "Build Settings":

Library Search Paths: $(PROJECT_DIR)/AppName/libspatialite
Header Search Paths: $(PROJECT_DIR)/AppName/libspatialite/include

And in the "Build Phases" window, add the following to the section "Link Binary With Libraries":

libiconv
libcharset.1.0.0
libc++
libxml2.2
libz

Also added following in "Other Linker Flags"

-lsqlite3 -lproj -lgeos -lspatialite -lgeos_c

in Swift, included the headers in bridging header file:

#include <sqlite3.h>
#include <spatialite/gaiageo.h>
#include <spatialite.h>

and in my code i accessed the geopackage file and spatialite is working fine.

    var db: OpaquePointer?
    let flags = SQLITE_OPEN_READWRITE
    if sqlite3_open_v2(databasePath, &db, flags, nil) == SQLITE_OK {
        print("Spatialite database opened successfully")
        
        var spconnect: OpaquePointer?
        print("Spatialite version: \(spatialite_version()!)")
    }

I have tested a few geometry related functions like AsText , MakePoint etc and all spatialite based functions are working fine but when i try to run function which require other module like geos or proj for example

ST_IsValid , IsValidReason , ST_Within , ST_Overlaps , 
Throws error.

ST_IsValid returns -1 



SELECT ST_IsValid(ST_GeomFromText('POINT(15.785 21.713)'))

IsValidReason always throws error and ST_Within doesn't filter any record.

I have also tested another build by manually disabling a few functions for integrating rttopo module and any function which requires rttopo like

MakeValid or AsTWKB doesn't work.

Spatialite is properly built for

armv7,armv7s,arm64,i386,x86_64 architectures 

but as per i understood it is not properly communicating with other modules. Any type of help would be appereciated.


Solution

  • I encountered the same issue. It took some time to find a solution, but it was worth it.

    I wrote an article with explanations to help developers like us avoid the same struggle.

    Long story short - you need to initialize SpatiaLite on the C level.