Skip to content

Commit

Permalink
Support GDAL 2.0+, improve GDAL configuration detection
Browse files Browse the repository at this point in the history
  • Loading branch information
hobu committed Dec 28, 2015
1 parent 93d8b85 commit 01d9e14
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmake/gdal.cmake
@@ -1,7 +1,7 @@
#
# GDAL/OGR support (required)
#
find_package(GDAL QUIET 1.9.0)
find_package(GDAL 1.9.0)
set_package_properties(GDAL PROPERTIES TYPE REQUIRED
PURPOSE "Provides general purpose raster, vector, and reference system support")
if (GDAL_FOUND)
Expand Down
40 changes: 24 additions & 16 deletions cmake/modules/FindGDAL.cmake
Expand Up @@ -4,7 +4,7 @@
# On success, the macro sets the following variables:
# GDAL_FOUND = if the library found
# GDAL_LIBRARY = full path to the library
# GDAL_INCLUDE_DIR = where to find the library headers
# GDAL_INCLUDE_DIR = where to find the library headers
#
# On Unix, macro sets also:
# GDAL_VERSION_STRING = human-readable string containing version of the library
Expand Down Expand Up @@ -89,6 +89,7 @@ set (GDAL_VERSION_COUNT 3)

SET(GDAL_NAMES gdal)

MESSAGE(STATUS "I'm here!!!!!!!!!!!!!!!")
IF(WIN32)

SET(OSGEO4W_IMPORT_LIBRARY gdal_i)
Expand Down Expand Up @@ -123,7 +124,7 @@ IF(WIN32)
IF(MSVC)

FIND_PATH(GDAL_INCLUDE_DIR
NAMES gdal.h
NAMES gdal.h
PATH_PREFIXES gdal gdal-1.6
PATHS
"${OSGEO4W_ROOT_DIR}/apps/gdal-dev/include"
Expand All @@ -138,14 +139,14 @@ IF(WIN32)
"$ENV{LIB_DIR}/lib"
/usr/lib
c:/msys/local/lib
"${OSGEO4W_ROOT_DIR}/apps/gdal-dev/lib"
"${OSGEO4W_ROOT_DIR}/apps/gdal-dev/lib"
${OSGEO4W_ROOT_DIR}/lib)

IF(GDAL_LIBRARY)
SET(GDAL_LIBRARY;odbc32;odbccp32 CACHE STRING INTERNAL)
ENDIF()
ENDIF(MSVC)

ELSEIF(UNIX)

# Try to use framework on Mac OS X
Expand All @@ -154,44 +155,49 @@ ELSEIF(UNIX)
ENDIF()

# Try to use GDAL_HOME location if specified
IF($ENV{GDAL_HOME})
IF(DEFINED ENV{GDAL_HOME})
SET(GDAL_CONFIG_PREFER_PATH
"$ENV{GDAL_HOME}/bin" CACHE STRING "Search for gdal-config program in preferred location")
ENDIF()

# Try to use OSGeo4W installation
IF($ENV{OSGEO4W_HOME})
IF(DEFINED ENV{OSGEO4W_HOME})
SET(GDAL_CONFIG_PREFER_OSGEO4W_PATH
"$ENV{OSGEO4W_HOME}/bin" CACHE STRING "Search for gdal-config program provided by OSGeo4W")
ENDIF()

# Try to use FWTools installation
IF($ENV{FWTOOLS_HOME})
IF(DEFINED ENV{FWTOOLS_HOME})
SET(GDAL_CONFIG_PREFER_FWTOOLS_PATH
"$ENV{FWTOOLS_HOME}/bin_safe" CACHE STRING "Search for gdal-config program provided by FWTools")
ENDIF()

FIND_PROGRAM(GDAL_CONFIG gdal-config
HINTS
${GDAL_CONFIG_PREFER_PATH}
${GDAL_CONFIG_PREFER_OSGEO4W_PATH}
${GDAL_CONFIG_PREFER_FWTOOLS_PATH}
${GDAL_MAC_PATH}
/usr/local/bin/
/usr/bin/)
IF(GDAL_CONFIG)

IF(GDAL_CONFIG)

# TODO: Replace the regex hacks with CMake version comparison feature:
# if(version1 VERSION_LESS version2)

# Extract GDAL version
MESSAGE(STATUS "GDAL_CONFIG: ${GDAL_CONFIG}")
MESSAGE(STATUS "ORACLE_HOME: $ENV{ORACLE_HOME}")
MESSAGE(STATUS "GDAL_HOME: $ENV{GDAL_HOME}")
MESSAGE(STATUS "GDAL_CONFIG_PREFER_PATH: ${GDAL_CONFIG_PREFER_PATH}")
EXEC_PROGRAM(${GDAL_CONFIG} ARGS --version OUTPUT_VARIABLE GDAL_VERSION_STATED)
SET(GDAL_VERSION_STRING "${GDAL_VERSION_STATED}" CACHE STRING "Version of GDAL package found")

STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\1" GDAL_VERSION_MAJOR "${GDAL_VERSION_STATED}")
STRING(REGEX REPLACE "([0-9]+)\\.(/^\\d{1,2}$/)\\.([0-9]+)" "\\2" GDAL_VERSION_MINOR "${GDAL_VERSION_STATED}")
STRING(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)" "\\3" GDAL_VERSION_PATCH "${GDAL_VERSION_STATED}")

# Check for GDAL version
if (GDAL_FIND_VERSION)
COMPARE_VERSION_STRINGS( "${GDAL_VERSION_STATED}" "${GDAL_FIND_VERSION}" version_result)
Expand All @@ -202,20 +208,22 @@ ELSEIF(UNIX)
set (GDAL_VERSION_COMPATIBLE true)
endif()

MESSAGE(STATUS "Found version: ${GDAL_VERSION_STRING}")
set (GDAL_FOUND TRUE)
# Set INCLUDE_DIR to prefix+include
EXEC_PROGRAM(${GDAL_CONFIG} ARGS --prefix OUTPUT_VARIABLE GDAL_PREFIX)

FIND_PATH(GDAL_INCLUDE_DIR
gdal.h
gdal.h
PATH_PREFIXES gdal
PATHS
HINTS
${GDAL_PREFIX}/include/gdal
${GDAL_PREFIX}/include
/usr/local/include
/usr/local/include
/usr/include)
MESSAGE(STATUS "Include directory version: ${GDAL_INCLUDE_DIR}")

# Extract link dirs for rpath
# Extract link dirs for rpath
EXEC_PROGRAM(${GDAL_CONFIG} ARGS --libs OUTPUT_VARIABLE GDAL_CONFIG_LIBS)

# Split off the link dirs (for rpath)
Expand Down Expand Up @@ -245,7 +253,7 @@ ELSEIF(UNIX)
ELSE()
SET(GDAL_LIBRARY ${GDAL_LINK_DIRECTORIES}/lib${GDAL_LIB_NAME}.so CACHE STRING INTERNAL)
ENDIF()

ELSE()
MESSAGE("FindGDAL.cmake: gdal-config not found. Please set it manually: GDAL_CONFIG=${GDAL_CONFIG}")
ENDIF(GDAL_CONFIG)
Expand Down
37 changes: 37 additions & 0 deletions io/derivative/DerivativeWriter.cpp
Expand Up @@ -45,6 +45,7 @@
#include <boost/filesystem.hpp>

#include "gdal_priv.h" // For File I/O
#include "gdal_version.h" // For version info
#include "ogr_spatialref.h" //For Geographic Information/Transformations

namespace pdal
Expand Down Expand Up @@ -1060,9 +1061,20 @@ void DerivativeWriter::writeSlope(Eigen::MatrixXd* tDemData,
tBand->SetNoDataValue((double)c_background);

if (m_GRID_SIZE_X > 0 && m_GRID_SIZE_Y > 0)
// #define STR_HELPER(x) #x
// #define STR(x) STR_HELPER(x)
//
// #pragma message "content of GDAL_VERSION_MAJOR:" STR(GDAL_VERSION_MAJOR)
#if GDAL_VERSION_MAJOR <= 1
tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0);
#else

int ret = tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0, 0);
#endif
}

GDALClose((GDALDatasetH) mpDstDS);
Expand Down Expand Up @@ -1134,9 +1146,15 @@ void DerivativeWriter::writeAspect(Eigen::MatrixXd* tDemData,
tBand->SetNoDataValue((double)c_background);

if (m_GRID_SIZE_X > 0 && m_GRID_SIZE_Y > 0)
#if GDAL_VERSION_MAJOR <= 1
tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0);
#else
int ret = tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0, 0);
#endif
}

GDALClose((GDALDatasetH) mpDstDS);
Expand Down Expand Up @@ -1214,9 +1232,15 @@ void DerivativeWriter::writeCatchmentArea(Eigen::MatrixXd* tDemData,
tBand->SetNoDataValue((double)c_background);

if (m_GRID_SIZE_X > 0 && m_GRID_SIZE_Y > 0)
#if GDAL_VERSION_MAJOR <= 1
tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0);
#else
int ret = tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0, 0);
#endif
}

GDALClose((GDALDatasetH) mpDstDS);
Expand Down Expand Up @@ -1358,9 +1382,16 @@ void DerivativeWriter::writeHillshade(Eigen::MatrixXd* tDemData,
tBand->SetNoDataValue((double)c_background);

if (m_GRID_SIZE_X > 0 && m_GRID_SIZE_Y > 0)
#if GDAL_VERSION_MAJOR <= 1
tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0);
#else

int ret = tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0, 0);
#endif
}

GDALClose((GDALDatasetH) mpDstDS);
Expand Down Expand Up @@ -1451,9 +1482,15 @@ void DerivativeWriter::writeCurvature(Eigen::MatrixXd* tDemData,
tBand->SetNoDataValue((double)c_background);

if (m_GRID_SIZE_X > 0 && m_GRID_SIZE_Y > 0)
#if GDAL_VERSION_MAJOR <= 1
tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0);
#else
int ret = tBand->RasterIO(GF_Write, 0, 0, m_GRID_SIZE_X, m_GRID_SIZE_Y,
poRasterData, m_GRID_SIZE_X, m_GRID_SIZE_Y,
GDT_Float32, 0, 0, 0);
#endif
}

GDALClose((GDALDatasetH) mpDstDS);
Expand Down
16 changes: 13 additions & 3 deletions test/unit/SpatialReferenceTest.cpp
Expand Up @@ -41,6 +41,8 @@
#include <LasWriter.hpp>
#include <LasReader.hpp>

#include <gdal_version.h>

#include "Support.hpp"

using namespace pdal;
Expand Down Expand Up @@ -74,9 +76,15 @@ TEST(SpatialReferenceTest, test_proj4_roundtrip)
std::string proj4 = "+proj=utm +zone=15 +datum=WGS84 +units=m +no_defs";
std::string proj4_ellps =
"+proj=utm +zone=15 +ellps=WGS84 +datum=WGS84 +units=m +no_defs";

#if GDAL_VERSION_MAJOR <=1
std::string proj4_out =
"+proj=utm +zone=15 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m "
"+no_defs";
#else
std::string proj4_out = "+proj=utm +zone=15 +datum=WGS84 +units=m +no_defs";

#endif

{
SpatialReference ref;
Expand All @@ -90,7 +98,6 @@ TEST(SpatialReferenceTest, test_proj4_roundtrip)
SpatialReference ref;
ref.setProj4(proj4_ellps);
const std::string ret = ref.getProj4();
//EXPECT_TRUE(ret == proj4);
EXPECT_TRUE(ret == proj4_out);
}

Expand Down Expand Up @@ -144,7 +151,7 @@ TEST(SpatialReferenceTest, calcZone)
{
int zone = 1;
for (double lon = -537.0; lon < 537.0; lon += 6.0)
{
{
EXPECT_EQ(zone, SpatialReference::calculateZone(lon, 25));
EXPECT_EQ(-zone, SpatialReference::calculateZone(lon, -25));
zone++;
Expand Down Expand Up @@ -177,8 +184,11 @@ TEST(SpatialReferenceTest, test_read_srs)
const std::string ret_wkt = ref.getWKT();
const std::string ret_proj4 = ref.getProj4();

#if GDAL_VERSION_MAJOR <=1
const std::string wkt = "PROJCS[\"WGS 84 / UTM zone 17N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-81],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AUTHORITY[\"EPSG\",\"32617\"]]";

#else
const std::string wkt = "PROJCS[\"WGS 84 / UTM zone 17N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-81],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32617\"]]";
#endif
EXPECT_TRUE(ret_wkt == wkt);

std::string proj4 = "+proj=utm +zone=17 +datum=WGS84 +units=m +no_defs";
Expand Down

0 comments on commit 01d9e14

Please sign in to comment.