wiki:LibKML

LibKML

Windows

Build with libkml

The LIBKML driver must be compiled against the latest version of LibKML (*not* version 1.2). The latest source code of libkml is at https://github.com/google/libkml.

The LibKML source code comes with two Visual Studio 2005 project files - Use the one at the "msvc" folder.

.\libkml\msvc\libkml.sln

That solution does not include all the needed project, so you should add it after loading the solution on VS 2005.

.\libkml\msvc\libkmlxsd.vcproj
.\libkml\third_party\zlib-1.2.3\contrib\minizip\minizip_static.vcproj

Note that from the SVN checkout there are more than source code but also some pre-build third part libraries. Those libraries are not build by the existing VS projects but they are going to be needed to build the LibKML driver.

.\libkml\third_party\expat.win32\libexpat.lib
.\libkml\third_party\googletest-r108.win32\release\gtest.lib
.\libkml\third_party\uriparser-0.7.5.win32\release\uriparser.lib
.\libkml\third_party\zlib-1.2.3.win32\lib\minizip.lib
.\libkml\third_party\zlib-1.2.3.win32\lib\zdll.lib
.\libkml\third_party\zlib-1.2.3.win32\lib\zlib.lib

You should make sure that you have build the whole solution, all the projects, in Release mode preferably.

Now, you should update the file "nmake.opt" at GDAL root folder, uncommenting the lines related to libkml and changing LIBKML_DIR to reflect where LibKML is in your system.

# Uncomment out the following lines to enable LibKML support.
LIBKML_DIR = C:/Folder Path/libkml
LIBKML_INCLUDE = -I$(LIBKML_DIR)/src -I$(LIBKML_DIR)/third_party/boost_1_34_1
LIBKML_LIBRARY = $(LIBKML_DIR)/msvc/Release
LIBKML_LIBS =	$(LIBKML_LIBRARY)/libkmlbase.lib \
		$(LIBKML_LIBRARY)/libkmlconvenience.lib \
		$(LIBKML_LIBRARY)/libkmldom.lib \
		$(LIBKML_LIBRARY)/libkmlengine.lib \
		$(LIBKML_LIBRARY)/libkmlregionator.lib \
		$(LIBKML_LIBRARY)/libkmlxsd.lib \
		$(LIBKML_LIBRARY)/minizip_static.lib \
		$(LIBKML_DIR)/third_party\expat.win32/libexpat.lib \
		$(LIBKML_DIR)/third_party\uriparser-0.7.5.win32/release/uriparser.lib \
		$(LIBKML_DIR)/third_party\zlib-1.2.3.win32/lib/minizip.lib \
		$(LIBKML_DIR)/third_party\zlib-1.2.3.win32/lib/zlib.lib

Now you can either rebuild, clean and build, all the GDAL project.

nmake -f makefile.vc clean
nmake -f makefile.vc 
nmake -f makefile.vc install

Or you can move to the driver folder and build the driver as a OGR plugin and copy the dll to gdalplugins folder.

.\gdal> cd ogr\ogrsf_frtms\libkml
.\gdal\ogr\ogrsf_frtms\libkml> nmake -f makefile.vc ogr_LIBKML.dll
.\gdal\ogr\ogrsf_frtms\libkml> copy ogr_LIBKML.dll <gdal path>\bin\gdalplugins

Visual Studio 2015 notes::

You may get an error of "c2137 empty character constant"; edit the file: src\kml\base\file_win32.cc and change line #43 to:

  std::wstring wstr(str.length(), L'\0');

Testing the driver

To test if the driver is been loaded:

.\> ogrinfo --formats
Supported Formats:
  -> "ESRI Shapefile" (read/write)
  -> "MapInfo File" (read/write)
  -> "UK .NTF" (readonly)
  -> "SDTS" (readonly)
  -> "TIGER" (read/write)
  -> "S57" (read/write)
  -> "DGN" (read/write)
  -> "VRT" (readonly)
  -> "REC" (readonly)
  -> "Memory" (read/write)
  -> "BNA" (read/write)
  -> "CSV" (read/write)
  -> "GML" (read/write)
  -> "GPX" (read/write)
  -> "LIBKML" (read/write)

And if you have a sample file:

.\> ogrinfo sample.kmz
INFO: Open of `sample.kmz'
      using driver `LIBKML' successful.
1: 0
Last modified 12 months ago Last modified on Apr 19, 2016 4:29:10 AM