2.4. Querydsl Spatial

Support for Spatial queries is available via the Querydsl Spatial module, which is an extension module to the SQL module. The Spatial module supports the object model of Simple Feature Access in queries and object binding.

The geolatte project is used for the object model.

2.4.1. Maven integration

Add the following dependency to your Maven project:


Additionally the following database specific extra dependencies:

<!-- for PostgreSQL usage -->

<!-- for Oracle usage -->

2.4.2. Code generation via Maven

The code generation for Querydsl SQL can be set to detect the usage of spatial types in database schemas and use geolatte types in these case via the spatial property:


2.4.3. Runtime configuration

The runtime configuration aspect of the spatial module is that instead of the normal SQLTemplates instances, spatial enabled instances are used. Below is a list of spatial enabled SQLTemplates classes.

  • GeoDBTemplates (for H2)

  • MySQLSpatialTemplates

  • OracleSpatialTemplates (alpha stage)

  • PostGISTemplates

  • SQLServer2008SpatialTemplates

  • TeradataSpatialTemplates

2.4.4. Querying

With code generation and runtime configuration set for spatial types we can now try queries with it. Filter by Distance

Geometry point = Wkt.fromWkt("Point(2 2)");

In addition to straight distance between geometries spherical and spheroidal distance are provided via distanceSphere and distanceSpheroid. Contains

Geometry point = Wkt.fromWkt("Point(2 2)");
query.where(table.geo.contains(point)); Intersection

Geometry geo = query.select(table.geo1.intersection(table.geo2)).fetchOne(); Access to the SPATIAL_REF_SYS table

Unified access to the SPATIAL_REF_SYS standard table is provided via the QSpatialRefSys and SpatialRefSys classes. SPATIAL_REF_SYS contains data about the supported spatial reference systems.

QSpatialRefSys spatialRefSys = QSpatialRefSys.spatialRefSys;
List<SpatialRefSys> referenceSystems = query.select(spatialRefSys).from(spatialRefSys).fetch();

2.4.5. Inheritance

In case you use only generic geometry types in your database schema you can use conversion methods in the object model to convert to more specific types.

GeometryPath<Geometry> geometry = shapes.geometry;
PointPath<Point> point = geometry.asPoint();
NumberExpression<Double> pointX = point.x(); // x() is not available on GeometryExpression/GeometryPath