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:

<dependency>
  <groupId>com.mysema.querydsl</groupId>
  <artifactId>querydsl-spatial</artifactId>
  <version>${querydsl.version}</version>
</dependency>    

Additionally the following database specific extra dependencies:

    
<!-- for PostgreSQL usage -->
<dependency>
  <groupId>org.postgis</groupId>
  <artifactId>postgis-jdbc</artifactId>
  <version>1.3.3</version>
  <scope>provided</scope>
</dependency>

<!-- for Oracle usage -->
<dependency>
  <groupId>oracle</groupId>
  <artifactId>sdoapi</artifactId>
  <version>11.2.0</version>
  <scope>provided</scope>
</dependency>

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:

<project>
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-maven-plugin</artifactId>
        <version>${querydsl.version}</version>
        ...
        <configuration>
          ...
          <spatial>true</spatial>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
</project>

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.

2.4.4.1. Filter by Distance

Geometry point = Wkt.fromWkt("Point(2 2)");  
query.where(table.geo.distance(point).lt(5.0));      

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

2.4.4.2. Contains

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

2.4.4.3. Intersection

Geometry geo = query.uniqueResult(table.geo1.intersection(table.geo2));      

2.4.4.4. 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.from(spatialRefSys).list(spatialRefSys);

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