An Objective-C lightweight spatial RDBMS (based on SQLite/SpatiaLite). The project manages a whole stack of technologies that allow combined together to perform spatial SQL queries (i.e. queries based on distance, intersection between geometries etc.) to obtain easy-to-use Cocoa objects.
While fully working, the project is in alpha stage, please be careful using this in production projects.
With SpatialDBKit you will be able to:
- Open a spatialite database:
// Open a spatialite database
SpatialDatabase *db = [SpatialDatabase databaseWithPath: [[NSBundle mainBundle] pathForResource:@"test" ofType:@"sqlite"]];
[db open];
- fetch geometries in form of Shapekit objects:
FMResultSet *rs = [db executeQuery:@"select Name, geometry FROM Regions"];
while ([rs next]) {
NSLog(@"%@", [resultSet resultDictionary]);
}
- or in textual form:
FMResultSet *rs = [db executeQuery:@"select Name, AsText(geometry) as geom_text FROM Regions"];
- calculating distance from a point if you need to:
FMResultSet *rs = [db executeQuery:@"select distance(geometry, MakePoint(45.694216,9.676909,4326)) AS text FROM Regions"];
- or filtering points by distance:
FMResultSet *resultSet= [db executeQuery:@"SELECT astext(geometry) as geometry, distance(geometry, MakePoint(45.694216,9.676909,4326)) as geometry, Name FROM Towns where distance(geometry, MakePoint(45.694216,9.676909,4326)) < 5000"];
This is just the point of the iceberg.. check spatialite SQL functions reference list to be sure you don't miss anything of this awesome library!
Since importing C libraries is a sort of nightmare, SpatialDBKit has been thought to be used with CocoaPods that should address all the boring stuff for you. But before you must install the GNU build system if you haven't already:
brew install automake autoconf libtool
Now you can create your Podfile as usual:
platform :ios, '5.0'
pod 'SpatialDBKit'
Of course now you should #import <SpatialDBKit/SpatialDatabase.h>
Now two extra step are required:
-
rename your main file main.m to main.mm The long story is that SpatialDBKit dependencies reside on C++ libraries, so we should tell the compiler that our project files should be treated as Objective-C++. The podspec correctly sets C++ Standard Library project's build setting to libstdc++ (GNU c++ standard library), but Xcode 5.1 can't find any Objective-C++ file in the main project, so ignores the setting and links with libc++. Renaming a file with .mm extension does the trick.
-
manually open
spatialite.c
and replace (line 87):
#include <spatialite/spatialite.h>
with:
#include <spatialite/spatialite/spatialite.h>
This is a workaround to fix the issue tracked here: #12 (while I figure out something better, or it is fixed in CocoaPods, or in the spatialite's pod specification).
Since SpatialDBKit actually sits on a stack of technology, you should refer to the submodules documentation. The most relevant are:
-
PROJ.4 is under the MIT license.
-
GEOS is available under the terms of GNU Lesser General Public License (LGPL), and is a project of OSGeo.
-
ShapeKit, being a GEOS wrapper, is under the terms of GNU Lesser General Public License.
-
spatialite is developed and maintained by Alessandro Furieri and are licensed under the MPL tri-license terms.
-
SpatialDBKit itself is licensed under the same MPL tri-license terms.
-
SQLite has been dedicated to the public domain by the authors (thanks!).
-
Its Objective-C wrapper, FMDB by Gus Mueller, is under the MIT License.
License note: Be aware that LGPL v2.1 (GEOS license) and Apple Store compatibility is at least controversial (search for "LGPL iOS" on Google to know why).