Skip to content

Commit cb5cd51

Browse files
committed
Add (optional) support for LZ4 PBF compression
1 parent 1cf497e commit cb5cd51

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1414
### Changed
1515

1616
- Various small fixes and cleanups.
17+
- Now depends on libosmium 2.16.0 or greater. This allows compiling with
18+
support for PBF lz4 compression which is enabled by default if the
19+
library is found. Disable by setting CMake option WITH_LZ4 to OFF.
1720

1821
### Fixed
1922

CMakeLists.txt

+18-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ set(AUTHOR "Jochen Topf <[email protected]>")
2929

3030
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3131

32+
option(WITH_LZ4 "Build with lz4 support for PBF files" ON)
33+
3234

3335
#-----------------------------------------------------------------------------
3436
#
@@ -38,9 +40,24 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3840

3941
include_directories(include)
4042

41-
find_package(Osmium 2.13.1 COMPONENTS io gdal)
43+
find_package(Osmium 2.16.0 COMPONENTS io gdal)
4244
include_directories(SYSTEM ${OSMIUM_INCLUDE_DIRS})
4345

46+
if(WITH_LZ4)
47+
find_package(LZ4)
48+
49+
if(LZ4_FOUND)
50+
message(STATUS "lz4 library found, compiling with it")
51+
add_definitions(-DOSMIUM_WITH_LZ4)
52+
include_directories(SYSTEM ${LZ4_INCLUDE_DIRS})
53+
list(APPEND OSMIUM_IO_LIBRARIES ${LZ4_LIBRARIES})
54+
else()
55+
message(WARNING "lz4 library not found, compiling without it")
56+
endif()
57+
else()
58+
message(STATUS "Building without lz4 support: Set WITH_LZ4=ON to change this")
59+
endif()
60+
4461
if(MSVC)
4562
find_path(GETOPT_INCLUDE_DIR getopt.h)
4663
find_library(GETOPT_LIBRARY NAMES wingetopt)

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ https://osmcode.org/osmcoastline
1414

1515
https://github.com/osmcode/libosmium
1616
https://osmcode.org/libosmium
17-
At least version 2.13.1 is needed.
17+
At least version 2.16.0 is needed.
1818

1919
### Protozero
2020

2121
https://github.com/mapbox/protozero
2222
Debian/Ubuntu: libprotozero-dev
2323
At least version 1.6.1 is needed.
2424

25+
### LZ4 (optional)
26+
27+
https://lz4.github.io/lz4/
28+
Debian/Ubuntu: liblz4-dev
29+
30+
Only needed for LZ4 PBF compression.
31+
2532
### zlib (for PBF support)
2633

2734
https://www.zlib.net/

cmake/FindLZ4.cmake

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
find_path(LZ4_INCLUDE_DIR
2+
NAMES lz4.h
3+
DOC "lz4 include directory")
4+
mark_as_advanced(LZ4_INCLUDE_DIR)
5+
find_library(LZ4_LIBRARY
6+
NAMES lz4 liblz4
7+
DOC "lz4 library")
8+
mark_as_advanced(LZ4_LIBRARY)
9+
10+
if (LZ4_INCLUDE_DIR)
11+
file(STRINGS "${LZ4_INCLUDE_DIR}/lz4.h" _lz4_version_lines
12+
REGEX "#define[ \t]+LZ4_VERSION_(MAJOR|MINOR|RELEASE)")
13+
string(REGEX REPLACE ".*LZ4_VERSION_MAJOR *\([0-9]*\).*" "\\1" _lz4_version_major "${_lz4_version_lines}")
14+
string(REGEX REPLACE ".*LZ4_VERSION_MINOR *\([0-9]*\).*" "\\1" _lz4_version_minor "${_lz4_version_lines}")
15+
string(REGEX REPLACE ".*LZ4_VERSION_RELEASE *\([0-9]*\).*" "\\1" _lz4_version_release "${_lz4_version_lines}")
16+
set(LZ4_VERSION "${_lz4_version_major}.${_lz4_version_minor}.${_lz4_version_release}")
17+
unset(_lz4_version_major)
18+
unset(_lz4_version_minor)
19+
unset(_lz4_version_release)
20+
unset(_lz4_version_lines)
21+
endif ()
22+
23+
include(FindPackageHandleStandardArgs)
24+
find_package_handle_standard_args(LZ4
25+
REQUIRED_VARS LZ4_LIBRARY LZ4_INCLUDE_DIR
26+
VERSION_VAR LZ4_VERSION)
27+
28+
if (LZ4_FOUND)
29+
set(LZ4_INCLUDE_DIRS "${LZ4_INCLUDE_DIR}")
30+
set(LZ4_LIBRARIES "${LZ4_LIBRARY}")
31+
32+
if (NOT TARGET LZ4::LZ4)
33+
add_library(LZ4::LZ4 UNKNOWN IMPORTED)
34+
set_target_properties(LZ4::LZ4 PROPERTIES
35+
IMPORTED_LOCATION "${LZ4_LIBRARY}"
36+
INTERFACE_INCLUDE_DIRECTORIES "${LZ4_INCLUDE_DIR}")
37+
endif ()
38+
endif ()

cmake/FindOsmium.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
# gdal - include if you want to use any of the OGR functions
3535
# proj - include if you want to use any of the Proj.4 functions
3636
# sparsehash - include if you use the sparsehash index
37+
# lz4 - include support for LZ4 compression of PBF files
3738
#
3839
# You can check for success with something like this:
3940
#
@@ -116,14 +117,21 @@ if(Osmium_USE_PBF)
116117
find_package(Threads)
117118
find_package(Protozero 1.6.3)
118119

120+
if(Osmium_USE_LZ4)
121+
find_package(LZ4 REQUIRED)
122+
add_definitions(-DOSMIUM_WITH_LZ4)
123+
endif()
124+
119125
list(APPEND OSMIUM_EXTRA_FIND_VARS ZLIB_FOUND Threads_FOUND PROTOZERO_INCLUDE_DIR)
120126
if(ZLIB_FOUND AND Threads_FOUND AND PROTOZERO_FOUND)
121127
list(APPEND OSMIUM_PBF_LIBRARIES
122128
${ZLIB_LIBRARIES}
129+
${LZ4_LIBRARIES}
123130
${CMAKE_THREAD_LIBS_INIT}
124131
)
125132
list(APPEND OSMIUM_INCLUDE_DIRS
126133
${ZLIB_INCLUDE_DIR}
134+
${LZ4_INCLUDE_DIRS}
127135
${PROTOZERO_INCLUDE_DIR}
128136
)
129137
else()

src/options.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#define strcasecmp _stricmp
3434
#endif
3535

36+
#include <osmium/io/pbf.hpp>
37+
3638
static void print_help() {
3739
std::cout << "Usage: osmcoastline [OPTIONS] OSMFILE\n"
3840
<< "\nOptions:\n"
@@ -171,7 +173,11 @@ Options::Options(int argc, char* argv[]) {
171173
break;
172174
case 'V':
173175
std::cout << "osmcoastline " << get_osmcoastline_long_version() << " / " << get_libosmium_version() << '\n'
174-
<< "Copyright (C) 2012-2021 Jochen Topf <[email protected]>\n"
176+
<< "PBF compression types:";
177+
for (const auto& type : osmium::io::supported_pbf_compression_types()) {
178+
std::cout << " " << type;
179+
}
180+
std::cout << "\n\nCopyright (C) 2012-2021 Jochen Topf <[email protected]>\n"
175181
<< "License: GNU GENERAL PUBLIC LICENSE Version 3 <https://gnu.org/licenses/gpl.html>.\n"
176182
<< "This is free software: you are free to change and redistribute it.\n"
177183
<< "There is NO WARRANTY, to the extent permitted by law.\n";

0 commit comments

Comments
 (0)