HOW TO USE SPATIALITE LIBRARY IN C++ PROJECT

1,633 views
Skip to first unread message

Alex Yin

unread,
Jan 23, 2013, 5:47:46 PM1/23/13
to spatiali...@googlegroups.com
Dear spatialite users:

I am developing a project in C++ and want to use some functionalities provided by the C API of spatialite library. We are using visual studio 2012. The inconsistency between C++ and C in the header files and various dependencies cause me a lot of headache. I am not a programming expert. Could you tell me whether there is a simple way to use the spatialite library without the need to figure out the function of each header file and source file?

 

I search on the web extensively for this. At first, it looks like the amalgamation is the solution. However, this has been deprecated for spatialite 4.0. I really could use some input as to how to make the functions exposed to my C++ project easily.

 

Any input is greatly appreciated. Thank you very much!! 



Brad Hards

unread,
Jan 24, 2013, 3:32:22 AM1/24/13
to spatiali...@googlegroups.com
On Thursday 24 January 2013 09:47:46 Alex Yin wrote:
> I am developing a project in C++ and want to use some functionalities
> provided by the C API of spatialite library. We are using visual studio
> 2012. The inconsistency between C++ and C in the header files and various
> dependencies cause me a lot of headache. I am not a programming expert.
> Could you tell me whether there is a simple way to use the spatialite
> library without the need to figure out the function of each header file and
> source file?
It would be easier to provide useful advice if you can tell us more about your
project. How is to be used? How is it to be deployed? Which API do you want to
use? What sort of problems are you having - "a lot of headache" isn't very
descriptive.

I would caution that C++, VS2012 and importing libraries may not be a good way
to start programming. It might help if we knew what real experience you have
(presumably some), even if it isn't directly relevant to this project.

Brad

Alex Yin

unread,
Jan 24, 2013, 9:58:28 AM1/24/13
to spatiali...@googlegroups.com
Sorry that I did not give more details in my post. 

We are working on an evacuation project. For now, there are several functionalities that SQLite/Spatialite provide are needed and probably we will use more in later stages. For example, we need to decide whether a certain household (point shapefile) is in the evacuation zone (polygon shapefile). We need to route them to the nearest shelter using probably Spatialite-network. In addition, we will need to select the households that meet certain criteria based on attributes and/or geography and do other operations with these households. Anyway, SQLite/Spatialite provide a lot of useful functionalities. W want to expose all the functionalities, if possible, to our own project since we possibly need them later. Let me emphasize our project is for National Science Foundation of U.S. We will not distribute the results or any product for commercial purposes. 

Since I am new to the Spatialite, I started with the demo1.c file posted on the website. I pasted the code in demo1.c into a new cpp file and added all the relevant headers as specified in the demo1.c. The compile was successful. However, the build was a failure. The following message was generated: 
"c:\users\weihao\documents\visual studio 2010\projects\test\test\spatialite.c(67): fatal error C1083: Cannot open include file: 'config-msvc.h': No such file or directory". I could add this header file to my project though I did not. I suspect that I am using the "libspatialite-4.0.0" the wrong way since each header file will reference other header files. I cannot figure out all the dependencies. 

I am not a programming expert. After an extensive search and trial and error in the last 3 days, I am wondering if there is a way to build the "libspatialite-4.0.0" into a usable library so that I can easily use the functions inside. I am not trying to modify the original code and I just want to use it. 

Our environment is Visual Studio 2010 professional and Windows 7. 

Brad Hards

unread,
Jan 25, 2013, 4:00:05 AM1/25/13
to spatiali...@googlegroups.com
On Friday 25 January 2013 01:58:28 Alex Yin wrote:
> 2010\projects\test\test\spatialite.c(67): fatal error C1083: Cannot open
> include file: 'config-msvc.h': No such file or directory". I could add this
> header file to my project though I did not. I suspect that I am using the
> "libspatialite-4.0.0" the wrong way since each header file will reference
> other header files. I cannot figure out all the dependencies.
I think there could be an easier way.

You can download the libspatialite DLL from the GAIA-SINS website:
http://www.gaia-gis.it/gaia-sins/windows-bin-x86/spatialite-4.0.0-DLL-win-
x86.zip.

There is also a 64 bit build available - start at http://www.gaia-gis.it/gaia-
sins/ and look for the "MS Windows binaries"


> I am not a programming expert. After an extensive search and trial and
> error in the last 3 days, I am wondering if there is a way to build the
> "libspatialite-4.0.0" into a usable library so that I can easily use the
> functions inside. I am not trying to modify the original code and I just
> want to use it.
I think you are approaching the problem in a not-as-easy-as-it-could-be way.

It is possible to use spatialite with just the C/C++ API. However much of the
power is best used through SQL. This also means that you can use the command
line tools and GUI tools to develop / test your queries before "burning" them
into the C++ application.

Conceptually, you are going to use C++ to make calls into sqlite, and embed
SQL commands into those calls. Some of those SQL commands might use
spatialite. You could implement the same queries in C# or java - pretty much
the only thing that changes is how you call into sqlite.

So lets take it step by step.

First find a tutorial on using VS2010, C++ native code with sqlite. Perhaps
something like http://dcravey.wordpress.com/2011/03/21/using-sqlite-in-a-
visual-c-application/, although it isn't the best coding style...

Now we need to figure out how to load the spatialite extension DLL. That
requires an sqlite that has extension loading enabled. You can do it in C++
(see http://www.sqlite.org/c3ref/load_extension.html), but its easier in SQL:
http://www.sqlite.org/lang_corefunc.html#load_extension

So the call is going to look something like (using the same demo style as
above):
cout << "Loading SpatiaLite extension ..." << endl;
const char *sqlLoad = "SELECT LOAD_EXTENSION("libspatialite-4.0.0.dll)";
rc = sqlite3_exec(db, sqlLoad, NULL, NULL, &error);
if (rc)
{
cerr << "Error loading spatialite: " << sqlite3_errmsg(db) << endl;
sqlite3_free(error);
}
else
{
cout << "Loaded spatialite" << endl;
}

[I didn't actually test this, and you might need to pass a full path to the
DLL, but hopefully you can figure it out from careful reading of the errors and
use of the debugger]

Now you can do more spatialite type operations (using spatial functions, the
virtual tables and so on) using the same SQL approach.

I would suggest you test this in the sqlite command shell or a GUI alternative
as a friendlier way to prototype and experiment.

HTH

Brad

Alex

unread,
Jan 27, 2013, 6:39:46 PM1/27/13
to spatiali...@googlegroups.com, br...@frogmouth.net
Dear Brad:

Thanks for your elaborated response. 

As you suggested, I downloaded the spatialite DLLs and now I am having some strange problems with loading the spatialite extension. I am using the test-2.3.sqlite sample database as the one in the spatialite tutorial. 

First, I tried to load the spatialite extension using the SQL approach. tHE error message read something like "error in the initialization". Then I use the function sqlite3_load_extension and error message turned out be "not an error", which was funny. And the console automatically printed out the spatialite version and the names of the functionalities such as "VirtualShape" etc. I guess that the extension loading was successful. Here comes the problem:

When I issue the sql command "SELECT spatialite_version()", the error message showed "No such function". Is there a special way to write the SQL query if I want to use the functions in the spatialite extension? 

I tested the opened database. A regular sqlite3 query returned results without any problem. In addition, I tested the spatialite_version() query in the spatialite GUI. It run perfectly. 

I am attaching my code. 

Thanks for your time and patience with me. I really appreciated your help. 





--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To post to this group, send email to spatiali...@googlegroups.com.
To unsubscribe from this group, send email to spatialite-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Yours sincerely
Weihao YIN
Ph.D Candidate
Department of Civil & Environmental Engineering
Virginia Tech
TestSpatialiteDLL_Brad.cpp

a.fu...@lqt.it

unread,
Jan 27, 2013, 7:25:01 PM1/27/13
to spatiali...@googlegroups.com
Hi Alex,

the attached sample (C lang) shows how to correctly loading
libspatialite as a dynamic extension to sqlite3.
you'll probably have to adapt the library path, or even better
to set the $PATH env variable accordingly to your local config.

please note: loading libspatialite as a dynamic extension make
very little sense if you are a C or C++ programmer; you can
directly link the library to your application (both dynamically
or statically, at your will), and then you'll be immediately
able to directly call the many APIs supported by the library.

You can eventually study the C-samples code to learn more:
http://www.gaia-gis.it/gaia-sins/splite-doxy-4.0.0/examples.html

bye Sandro




--
Il messaggio e' stato analizzato alla ricerca di virus o
contenuti pericolosi da MailScanner, ed e'
risultato non infetto.

load.c

Alex

unread,
Jan 27, 2013, 8:05:33 PM1/27/13
to spatialite-users, a.fu...@lqt.it
Dear Sandro:

Thanks for your response. 

I only tried to load spatialite as an extension to sqlite only because I do not know how to directly link the library to my application either statically or dynamically. I talked to someone who had experience working with libspatialite and he told me I need to build the libspatialite as a static library (.lib, I am using visual studio 2010) from the SOURCE code. So I spent an entire day in building libspatialite from scratch by starting with libiconv. After finally getting  all the lib files, I ran into some error related to geos I could not remember now. So I gave up on that since it was too complicated.

Please allow me to ask you this: could you give me a step-by-step guidance to link the libspatialite to my application ( a win32 console application in visual studio in C++)? Since building libspatialite from scratch does not seem to be the best way. I did an extensive search and came up empty. 

Brad Hards suggested that loading the DLL as an extension is relatively an easy way. So I tried. I did pretty much the same way as you. After I loaded the extension, the error message showed was "not an error". And the screen printed out information such as the spatialite 4.0 and virtualshape, virtual network etc. So I guess loading was successful. However, when I issued the command "SELECT spatialite_version()", the error message showed "No such a function". I attached my code and you can see it is the same as yours. Please let me know what I did wrong. 

Thanks for your time and patience. I really appreciate your help. 




--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To post to this group, send email to spatialite-users@googlegroups.com.
To unsubscribe from this group, send email to spatialite-users+unsubscribe@googlegroups.com.
TestSpatialiteDLL_Brad.cpp

Alex

unread,
Jan 27, 2013, 8:25:34 PM1/27/13
to spatialite-users, a.furieri
Dear Sandro:

I removed the entry point argument "spatialite_init" and it worked. But still, I use C++ and could you tell me how to link the spatialite library to my application? I did not find any pre-built library that I can download and only the source codes are available. 

a.fu...@lqt.it

unread,
Jan 27, 2013, 8:50:34 PM1/27/13
to spatiali...@googlegroups.com
Hi Alex,

> So I spent an entire day in building libspatialite from scratch
> by starting with libiconv. After finally getting  all the lib files,
> I ran into some error related to geos I could not remember now.
> So I gave up on that since it was too complicated.
>

I'm not at all a fan of Visual Studio: it could eventually
be a nice development suite if you are planning to use only
Microsoft components, but if you intend using some open source
library it surely will make your life harder.

A *strongly* suggested alternative is using the wonderfull
MinGW/MSYS development toolchain.
very shortly said: it simply is a porting to Windows of the
same identical toolchain we use everyday on Linux.
i.e. it's the "natural environment" for open source software;
building any open source library on MinGW/MSYS usually requires
just few minutes, and works in the most painless way.


> Please allow me to ask you this: could you give me a step-by-step
> guidance to link the libspatialite to my application ( a win32
> console
> application in visual studio in C++)?
>

using the damn C++ for a console app (no GUI) really seems an
overkill: the good old C really rockets in this specific environment,
and is by way simpler and easier than C++ (at least, IMHO).


> Since building libspatialite from scratch does not seem to be the
> best way. I did an extensive search and came up empty. 
>

if you are curious about MinGW you can read:
http://www.gaia-gis.it/gaia-sins/mingw_how_to.html
http://www.gaia-gis.it/gaia-sins/mingw64_how_to.html

if you absolutely insist working with Visual Studio:
http://www.gaia-gis.it/gaia-sins/msvc_how_to.html


> Brad Hards suggested that loading the DLL as an extension is
> relatively an easy way.
>

surely yes: this way you'll lost direct access to the C APIs,
but your life will probably be easier.

a.fu...@lqt.it

unread,
Jan 27, 2013, 9:01:49 PM1/27/13
to Alex, spatialite-users
> I removed the entry point argument "spatialite_init" and it worked.
>

the entry point name "spatialite_init" is wrong; a NULL is
expected in order to enable the standard default entry-point.


> But still, I use C++ and could you tell me how to link the spatialite
> library to my application?
>

I'm really sorry, but for sure I cannot teach you something I was
never able to understand in 32+ years of programming experience :-)
Each time I'm absolutely compelled using MSVC I never use the many
GUI oddities: I write an NMAKE file or I directly use the command
shell.

> I did not find any pre-built library that I can download and only the
> source codes are available. 
>

have a look at OSGeo4W: they distribute lots of pre-built libraries
for Windows MSVC (including GEOS, PROJ.4, ICONV and so on)
http://trac.osgeo.org/osgeo4w/
Reply all
Reply to author
Forward
0 new messages