Running Mapnik in Visual C++

588 views
Skip to first unread message

Teva Velu

unread,
Oct 22, 2015, 1:37:39 PM10/22/15
to mapnik

I'm pretty new to Mapnik libraries. I was able to follow the instructions to download the mapnik libraries on my Windows 7 computer and ran the python scripts to render shape files. It works just fine. 

Now, I need to use mapnik in Visual C++ and I've been using Microsoft Visual Studio 2010 to write my VC++ code. 

I did the following steps:

1. From http://mapnik.org downloaded win32 binaries

2. Installed Python 2.7.5 in directory C:\Python27

3. Perform installation in accordance with the instructions

4. "Installing Mapnik in Windows", in directory C:\mapnik-v2.2.0 
      => Downloaded the source, unpacked the archive and executed from the directory \demo\python file rundemo.py - worked without error (new files were created: demo.jpg, demo.png,
demo256.png, map.xml) 

5. Created in Visual C++ 2010 Professional Edition empty project (Win32 Console Application)

6. In the project settings added:

  a)  in “Include additional directories” add path C:\mapnik-v2.2.0\include


  b)  in “Additional libraries” add path C:\mapnik-v2.2.0\lib



  c)  in “additional dependence” add file mapnik.lib 



8. Build the “Release|Win32” - no errors!

9. The following is the code I tried to run. 

#include "stdafx.h"
#include <mapnik/map.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/image.hpp>
#include <mapnik/image_util.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
  mapnik::Map m(256,256);
  mapnik::load_map(m, 'C:\\mapnik-v2.2.0\\demo\\c++\\Demo1\\file.xml');
  m.zoom_all();
  mapnik::image_data_8 im(256,256);
  mapnik::agg_renderer<mapnik::image_data_8> ren(m, im);
  ren.apply();
  mapnik::save_to_file(im, 'the_image.png');
  return 0;
}


10. I got the following error. 



Would you please help me to solve this error? Or if you have any helpful tutorials of using mapnik in VC++, please share with me. 


Thanks a lot. 

Teva




Wilhelm Berg

unread,
Oct 23, 2015, 12:07:57 PM10/23/15
to mapnik
Teva,

seems the actual error message is missing.

Willy

Teva Velu

unread,
Oct 23, 2015, 4:43:34 PM10/23/15
to mapnik
Thanks Willy. The following is the result of the built from output window. 

Teva Velu

unread,
Oct 27, 2015, 4:47:06 PM10/27/15
to mapnik
Anyone could help to solve this error? I just need a simple example to see how to use the mapnik in visual c++ 2010.

Thanks.

Kevin Kenny

unread,
Oct 27, 2015, 10:44:48 PM10/27/15
to map...@googlegroups.com
> <https://lh3.googleusercontent.com/-l-4N9Z28FRg/VikYl2NS2nI/AAAAAAAAAKo/Mr4Mh3Kq6ow/s1600/MapnikInclude.png>
>
>
> b) in “Additional libraries” add path C:\mapnik-v2.2.0\lib
>
> <https://lh3.googleusercontent.com/-e0rAD_U8xZ8/VikZaNleS1I/AAAAAAAAAK4/wetj0uKljos/s1600/MapnikLib.png>
>
>
>
> c) in “additional dependence” add file mapnik.lib
>
> <https://lh3.googleusercontent.com/-mJbb3Q6pGMA/VikZnsM1ffI/AAAAAAAAALA/X7v0xsazl04/s1600/manik.png>
>
>
>
> 8. Build the “Release|Win32” - _no errors_!
>
> 9. The following is the code I tried to run.
>
> #include "stdafx.h"
> #include <mapnik/map.hpp>
> #include <mapnik/load_map.hpp>
> #include <mapnik/agg_renderer.hpp>
> #include <mapnik/image.hpp>
> #include <mapnik/image_util.hpp>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> mapnik::Map m(256,256);
> mapnik::load_map(m,
> 'C:\\mapnik-v2.2.0\\demo\\c++\\Demo1\\file.xml');
> m.zoom_all();
> mapnik::image_data_8 im(256,256);
> mapnik::agg_renderer<mapnik::image_data_8> ren(m, im);
> ren.apply();
> mapnik::save_to_file(im, 'the_image.png');
> return 0;
> }
>
>
> 10. I got the following error.
>
> <https://lh3.googleusercontent.com/-9MNf_WNUZrY/VikefGoEctI/AAAAAAAAALQ/Y4dqpiZAB8I/s1600/mapnikError.png>
>
>
>
> Would you please help me to solve this error? Or if you have any
> helpful tutorials of using mapnik in VC++, please share with me.
>
>
> Thanks a lot.
>
> Teva
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "mapnik" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mapnik+un...@googlegroups.com
> <mailto:mapnik+un...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
The errors with 'M_PI' and 'rint' should be fixed if you do

#include <cmath>

before bringing in all the Mapnik #include's.

There isn't any <mapnik/image.hpp> - the example at
https://gist.github.com/lwu/5009 has the following boilerplate:

// define before any includes
#define BOOST_SPIRIT_THREADSAFE

#include <mapnik/map.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/font_engine_freetype.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/filter_factory.hpp>
#include <mapnik/color_factory.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/config_error.hpp>
#include <iostream>

Kevin

Teva Velu

unread,
Oct 28, 2015, 1:34:15 PM10/28/15
to mapnik
Thanks Kevin for your comments and link. They are useful. 

Unfortunately I'm still having run time error. I made the following changes in the code. 

#include "stdafx.h"
#define  _USE_MATH_DEFINES
#define _WINDOWS
#define BOOST_SPIRIT_THREADSAFE 
#include <mapnik/map.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/graphics.hpp>
#include <mapnik/image_util.hpp>
#include <iostream> 

int _tmain(int argc, _TCHAR* argv[])
{
  mapnik::Map m(256,256);
  mapnik::load_map(m, "C:\mapnik-v2.2.0\demo\c++\file.xml");
  m.zoom_all();
  mapnik::image_32 im(256,256);
  mapnik::agg_renderer<mapnik::image_32> ren(m, im);
  ren.apply();
  mapnik::save_to_file(im, "C:\mapnik-v2.2.0\demo\c++\1.png");
}

I got the following error on the following line:
mapnik::load_map(m, "C:\mapnik-v2.2.0\demo\c++\file.xml");

This is the error I'm getting:

"Unhanded exception at 0x7642d in Demo1.exe: Microsoft C++ exception: mapnik::config_error at memory location 0x002ee790"




Could you please help me to solve this error? 

Thank you.

Teva

Kevin Kenny

unread,
Oct 28, 2015, 1:40:00 PM10/28/15
to map...@googlegroups.com

Config_error sounds as if the XML is malformed. If you wrap the mapnik calls in a try... catch (mapnik::config_error e) you should be able to print e.what () and get more information.

To unsubscribe from this group and stop receiving emails from it, send an email to mapnik+un...@googlegroups.com.

Teva Velu

unread,
Oct 28, 2015, 5:02:50 PM10/28/15
to mapnik
Thanks for your help Kevin. I am still in the error phase. 

I tried to use try catch as you mentioned and I got linking error related to mapnik::config_error.

The following is the changes I made. 

#include "stdafx.h"
#define  _USE_MATH_DEFINES
#define _WINDOWS
#define BOOST_SPIRIT_THREADSAFE 
#include <mapnik/map.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/graphics.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/config_error.hpp>
#include <iostream> 

int _tmain(int argc, _TCHAR* argv[])
{
  try
  {
     mapnik::Map m(256,256);    
     mapnik::load_map(m, "C:\\mapnik-v2.2.0\\demo\\c++\\file.xml");
     m.zoom_all();
     mapnik::image_32 im(256,256);
     mapnik::agg_renderer<mapnik::image_32> ren(m, im);
     ren.apply();
     mapnik::save_to_file(im, "C:\mapnik-v2.2.0\demo\c++\1.png");
    }
  catch (mapnik::config_error e)
  {
  std::cout<< e.what();
  }   
}

The following are the errors I got:
Error 13 error LNK1120: 1 unresolved externals C:\mapnik-v2.2.0\demo\c++\Demo1\Release\Demo1.exe
Error 12 error LNK2001: unresolved external symbol "public: virtual char const * __thiscall mapnik::config_error::what(void)const " (?what@config_error@mapnik@@UBEPBDXZ) C:\mapnik-v2.2.0\demo\c++\Demo1\Demo1\Demo1.obj


However, I used the following XML file from the mapnik website. 

<Map background-color="blue" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
  <Style name="My Style">
  <Rule>
  <PolygonSymbolizer fill="#f2eff9" />
  <LineSymbolizer stroke="rgb(50%,50%,50%)" stroke-width="0.1" />
  </Rule>
  </Style>
<Layer name="world" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
  <StyleName>My Style</StyleName>
<Datasource>
<Parameter name="file">ne_110m_countries.shp</Parameter>
  </Datasource>
</Layer>
</Map>

I have the ne_110m_countries.shp shapefile in same location as XML file. For your reference I have attached my shape files too. 

Do you think anything I missed in the XML files? 

Thanks again for your help.

Teva
datafile.zip

Kevin Kenny

unread,
Oct 28, 2015, 5:17:06 PM10/28/15
to map...@googlegroups.com
Hmm, it seems to be stumbling over trying to link directly to mapnik::config_error's 'what_' method (which isn't marked for export from the DLL).

Try replacing
#include <mapnik/config_error.hpp>
with
#include <exception>

and then replace
catch(mapnik::config_error e)
with
catch(std::exception& e)

and see if that gets you over the linkage error. We really need to see the message associated with the config_error.

Teva Velu

unread,
Oct 28, 2015, 7:03:49 PM10/28/15
to mapnik
Thanks Kevin. When I used #include <exception>, I was able to get the error message. 

The following is the error message that I got from exception.

"Could not create datasource for type: 'shape' encountered during parsing of layer 'world' in Layer at line 8 of 'C\Mapnik-v2.2.2\demo\c++\file.xml"

I have used the following XML file. Could you please tell me what do I need to modify? 

<Map background-color="blue" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
  <Style name="My Style">
  <Rule>
  <PolygonSymbolizer fill="#f2eff9" />
  <LineSymbolizer stroke="rgb(50%,50%,50%)" stroke-width="0.1" />
  </Rule>
  </Style>
<Layer name="world" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
  <StyleName>My Style</StyleName>
<Datasource>
<Parameter name="type">shape</Parameter>
<Parameter name="file">ne_110m_countries.shp</Parameter>
  </Datasource>
</Layer>
</Map>

Thanks for your help.

Teva

Kevin Kenny

unread,
Oct 28, 2015, 7:20:44 PM10/28/15
to map...@googlegroups.com
It looks as if the shapefile plugin didn't build and install correctly. I'm afraid that there, you're getting beyind my expertise. I've always used Mapnik 2.2 on Windows and got the SDK from http://mapnik.org/pages/downloads.html  - I've not tried building the whole package on VC++ myself.

Teva Velu

unread,
Oct 29, 2015, 12:39:51 PM10/29/15
to mapnik
I appreciate your help Kevin. I wasn't even know where I had error and at least now I know my error is related to shape file plugin. I'm planning to use mapnik lib and header on my VC++ project to read and render some shape files. I was able to do the same task using Python in windows. So, were you able to use the lib and headers in windows without building whole mapink?

Teva Velu

unread,
Oct 29, 2015, 4:08:46 PM10/29/15
to mapnik
I solved the problem. I needed to add the following line of code.
 datasource_cache::instance().register_datasources( mapnikDir + "\\lib\\mapnik\\input\\");
Thanks for your help Kevin. 

The following is the working source code. 

#include "stdafx.h"
#define  _USE_MATH_DEFINES
#define _WINDOWS
#define BOOST_SPIRIT_THREADSAFE 
#include <mapnik/map.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/graphics.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/datasource_cache.hpp>
#include <iostream> 
#include <exception>

using namespace std;
using namespace mapnik;

int _tmain(int argc, _TCHAR* argv[])
{
  try
  {
     Map m(256,256);   

     std::string mapnikDir = "C:\\mapnik-v2.2.0";
     datasource_cache::instance().register_datasources( mapnikDir + "\\lib\\mapnik\\input\\");
     mapnik::load_map(m, "C:\\mapnik-v2.2.0\\demo\\python\\Demo1\\file.xml");
     m.zoom_all();
     mapnik::image_32 im(256,256);
     mapnik::agg_renderer<mapnik::image_32> ren(m, im);
     ren.apply();
     mapnik::save_to_file(im, "C:\\mapnik-v2.2.0\\demo\\python\\Demo1\\1.png");
    }
  catch (std::exception& e)
  {
  cout << e.what();
    cout << " Error message";      
  } 
  
}

Reply all
Reply to author
Forward
0 new messages