Skip to content

Commit

Permalink
Make GEOS CPP optional
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro committed Sep 21, 2015
1 parent e9e638f commit 9fef5a1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Expand Up @@ -191,7 +191,6 @@ ENDIF(NOT WIN32 AND NOT ANDROID)
# required
FIND_PACKAGE(Proj)
FIND_PACKAGE(GEOS)
SET(GEOS_CPP_LIBRARY -lgeos CACHE STRING "Path to the geos CPP library")
FIND_PACKAGE(GDAL)
FIND_PACKAGE(Expat REQUIRED)
FIND_PACKAGE(Spatialindex REQUIRED)
Expand Down
14 changes: 13 additions & 1 deletion cmake/FindGEOS.cmake
Expand Up @@ -23,11 +23,16 @@ IF(WIN32)
IF (MINGW)
FIND_PATH(GEOS_INCLUDE_DIR geos_c.h /usr/local/include /usr/include c:/msys/local/include)
FIND_LIBRARY(GEOS_LIBRARY NAMES geos_c PATHS /usr/local/lib /usr/lib c:/msys/local/lib)
FIND_LIBRARY(GEOS_CPP_LIBRARY NAMES geos PATHS /usr/local/lib /usr/lib c:/msys/local/lib)
ENDIF (MINGW)

IF (MSVC)
FIND_PATH(GEOS_INCLUDE_DIR geos_c.h $ENV{LIB_DIR}/include $ENV{INCLUDE})
FIND_LIBRARY(GEOS_LIBRARY NAMES geos geos_c_i geos_c PATHS
FIND_LIBRARY(GEOS_LIBRARY NAMES geos_c_i geos_c PATHS
"$ENV{LIB_DIR}/lib"
$ENV{LIB}
)
FIND_LIBRARY(GEOS_CPP_LIBRARY NAMES geos PATHS
"$ENV{LIB_DIR}/lib"
$ENV{LIB}
)
Expand Down Expand Up @@ -67,6 +72,7 @@ ELSE(WIN32)

IF(CYGWIN)
FIND_LIBRARY(GEOS_LIBRARY NAMES geos_c PATHS /usr/lib /usr/local/lib)
FIND_LIBRARY(GEOS_CPP_LIBRARY NAMES geos PATHS /usr/lib /usr/local/lib)
ENDIF(CYGWIN)

IF (NOT GEOS_INCLUDE_DIR OR NOT GEOS_LIBRARY OR NOT GEOS_CONFIG)
Expand Down Expand Up @@ -132,12 +138,16 @@ ELSE(WIN32)
#MESSAGE("DBG GEOS_CONFIG_LIBS=${GEOS_CONFIG_LIBS}")
#MESSAGE("DBG GEOS_LIB_NAME_WITH_PREFIX=${GEOS_LIB_NAME_WITH_PREFIX}")
SET(GEOS_LIB_NAME_WITH_PREFIX -lgeos_c CACHE STRING INTERNAL)
SET(GEOS_CPP_LIB_NAME_WITH_PREFIX -lgeos CACHE STRING INTERNAL)

## remove prefix -l because we need the pure name

IF (GEOS_LIB_NAME_WITH_PREFIX)
STRING(REGEX REPLACE "[-][l]" "" GEOS_LIB_NAME ${GEOS_LIB_NAME_WITH_PREFIX} )
ENDIF (GEOS_LIB_NAME_WITH_PREFIX)
IF (GEOS_CPP_LIB_NAME_WITH_PREFIX)
STRING(REGEX REPLACE "[-][l]" "" GEOS_CPP_LIB_NAME ${GEOS_CPP_LIB_NAME_WITH_PREFIX} )
ENDIF (GEOS_CPP_LIB_NAME_WITH_PREFIX)
#MESSAGE("DBG GEOS_LIB_NAME=${GEOS_LIB_NAME}")

IF (APPLE)
Expand All @@ -146,9 +156,11 @@ ELSE(WIN32)
# while still preserving user setting if given
# ***FIXME*** need to improve framework check so below not needed
SET(GEOS_LIBRARY ${GEOS_LINK_DIRECTORIES}/lib${GEOS_LIB_NAME}.dylib CACHE STRING INTERNAL FORCE)
SET(GEOS_CPP_LIBRARY ${GEOS_LINK_DIRECTORIES}/lib${GEOS_CPP_LIB_NAME}.dylib CACHE STRING INTERNAL FORCE)
ENDIF (NOT GEOS_LIBRARY)
ELSE (APPLE)
SET(GEOS_LIBRARY ${GEOS_LINK_DIRECTORIES}/lib${GEOS_LIB_NAME}.so CACHE STRING INTERNAL)
SET(GEOS_CPP_LIBRARY ${GEOS_LINK_DIRECTORIES}/lib${GEOS_CPP_LIB_NAME}.so CACHE STRING INTERNAL)
ENDIF (APPLE)
#MESSAGE("DBG GEOS_LIBRARY=${GEOS_LIBRARY}")

Expand Down
8 changes: 6 additions & 2 deletions src/core/CMakeLists.txt
Expand Up @@ -523,8 +523,6 @@ SET(QGIS_CORE_HDRS
${CMAKE_BINARY_DIR}/qgsconfig.h
../plugins/qgisplugin.h

geosextra/geos_c_extra.h

qgis.h
qgsattributeaction.h
qgscachedfeatureiterator.h
Expand Down Expand Up @@ -737,6 +735,12 @@ SET(QGIS_CORE_HDRS
geometry/qgspointv2.h
)

IF(GEOS_CPP_LIBRARY)
SET(QGIS_CORE_SRCS ${QGIS_CORE_SRCS} geosextra/geos_c_extra.cpp)
SET(QGIS_CORE_HDRS ${QGIS_CORE_HDRS} geosextra/geos_c_extra.h)
ADD_DEFINITIONS("-DHAVE_GEOS_CPP")
ENDIF(GEOS_CPP_LIBRARY)

IF (QT_MOBILITY_LOCATION_FOUND OR Qt5Positioning_FOUND)
SET(QGIS_CORE_HDRS ${QGIS_CORE_HDRS}
gps/qgsqtlocationconnection.h
Expand Down
24 changes: 22 additions & 2 deletions src/core/geometry/qgsgeos.cpp
Expand Up @@ -117,18 +117,24 @@ class GEOSGeomScopedPtr

QgsGeos::QgsGeos( const QgsAbstractGeometryV2* geometry, int precision ): QgsGeometryEngine( geometry ), mGeos( 0 ), mGeosPrepared( 0 )
{
#ifdef HAVE_GEOS_CPP
double prec = qPow( 10, -precision );
mPrecisionModel = GEOSPrecisionModel_createFixed( 1.f / prec );
mPrecisionReducer = GEOSGeometryPrecisionReducer_create( mPrecisionModel );
#else
Q_UNUSED( precision )
#endif
cacheGeos();
}

QgsGeos::~QgsGeos()
{
#ifdef HAVE_GEOS_CPP
GEOSGeom_destroy_r( geosinit.ctxt, mGeos );
GEOSPreparedGeom_destroy_r( geosinit.ctxt, mGeosPrepared );
GEOSGeometryPrecisionReducer_destroy( mPrecisionReducer );
GEOSPrecisionModel_destroy( mPrecisionModel );
#endif
}

void QgsGeos::geometryChanged()
Expand Down Expand Up @@ -158,11 +164,15 @@ void QgsGeos::cacheGeos() const
}

GEOSGeometry* g = asGeos( mGeometry );
#ifdef HAVE_GEOS_CPP
if ( g )
{
mGeos = GEOSGeometryPrecisionReducer_reduce( mPrecisionReducer, g );
GEOSGeom_destroy_r( geosinit.ctxt, g );
}
#else
mGeos = g;
#endif
}

QgsAbstractGeometryV2* QgsGeos::intersection( const QgsAbstractGeometryV2& geom, QString* errorMsg ) const
Expand Down Expand Up @@ -1047,12 +1057,18 @@ QgsAbstractGeometryV2* QgsGeos::overlay( const QgsAbstractGeometryV2& geom, Over
{
return 0;
}

#ifdef HAVE_GEOS_CPP
GEOSGeomScopedPtr geosGeom;
geosGeom.reset( gG );

//reduce precision
GEOSGeomScopedPtr pg2;
pg2.reset( GEOSGeometryPrecisionReducer_reduce( mPrecisionReducer, geosGeom.get() ) );
#else
GEOSGeomScopedPtr pg2;
pg2.reset( gG );
#endif

try
{
Expand Down Expand Up @@ -1099,14 +1115,18 @@ bool QgsGeos::relation( const QgsAbstractGeometryV2& geom, Relation r, QString*
{
return false;
}
#ifdef HAVE_GEOS_CPP
GEOSGeomScopedPtr geosGeom;
geosGeom.reset( gG );

bool result = false;

GEOSGeomScopedPtr pg2;
pg2.reset( GEOSGeometryPrecisionReducer_reduce( mPrecisionReducer, geosGeom.get() ) );
#else
GEOSGeomScopedPtr pg2;
pg2.reset( gG );
#endif

bool result = false;
try
{
if ( mGeosPrepared ) //use faster version with prepared geometry
Expand Down
6 changes: 5 additions & 1 deletion src/core/geometry/qgsgeos.h
Expand Up @@ -18,8 +18,10 @@ email : marco.hugentobler at sourcepole dot com

#include "qgsgeometryengine.h"
#include "qgspointv2.h"
#include "geosextra/geos_c_extra.h"
#include <geos_c.h>
#ifdef HAVE_GEOS_CPP
#include "geosextra/geos_c_extra.h"
#endif

class QgsLineStringV2;
class QgsPolygonV2;
Expand Down Expand Up @@ -89,9 +91,11 @@ class CORE_EXPORT QgsGeos: public QgsGeometryEngine
private:
mutable GEOSGeometry* mGeos;
const GEOSPreparedGeometry* mGeosPrepared;
#ifdef HAVE_GEOS_CPP
//precision reducer
GEOSPrecisionModel* mPrecisionModel;
GEOSGeometryPrecisionReducer* mPrecisionReducer;
#endif

enum Overlay
{
Expand Down

0 comments on commit 9fef5a1

Please sign in to comment.