简体   繁体   English

如何使用 jOOQ 在 PostGIS 中选择多边形内的点?

[英]How to select points within polygon in PostGIS using jOOQ?

I have a table sensor_location :我有一个表sensor_location

CREATE TABLE public.sensor_location (
  sensor_id INTEGER NOT NULL,
  location_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  location_point public.geometry NOT NULL,
  CONSTRAINT sensor_location_sensor_id_fkey FOREIGN KEY (sensor_id)
    REFERENCES public.sensor(id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
    NOT DEFERRABLE
) 

I want a query which will return sensor_id s of sensors and location_time s within selected polygon.我想要一个查询,它将在选定的多边形内返回传感器的sensor_idlocation_time

The query should look something like:查询应类似于:

SELECT 
  sensor_id,
  location_time,
FROM 
  public.sensor_location
WHERE
  ST_Within(location_point, ST_Polygon(ST_GeomFromText('LINESTRING(-71.050316 48.422044,-71.070316 48.422044,-71.070316 48.462044,-71.050316 48.462044,-71.050316 48.422044)'), 0));

How can I do that using jOOQ?我怎样才能使用 jOOQ 做到这一点? Is it even possible to use jOOQ with PostGIS?甚至可以将 jOOQ 与 PostGIS 一起使用吗? Do I have to write my own sql query and just execute it with jOOQ?我是否必须编写自己的 sql 查询并使用 jOOQ 执行它?

I found this but I have no idea how to use it.我找到了这个,但我不知道如何使用它。 I'm still a novice Java programmer.我仍然是一个新手 Java 程序员。

jOOQ currently (version 3.8) doesn't have out-of-the-box support for PostGIS, but you can easily add your own. jOOQ 当前(3.8 版)没有对 PostGIS 的开箱即用支持,但您可以轻松添加自己的支持。

If you're running only few GIS queries如果您只运行少数 GIS 查询

... then, using plain SQL will certainly do the trick. ...然后,使用 纯 SQL肯定会成功。 Here's one example, how to do that:这是一个例子,如何做到这一点:

ctx.select(SENSOR_LOCATION.SENSOR_ID, SENSOR_LOCATION.LOCATION_TIME)
   .from(SENSOR_LOCATION)
   .where("ST_WITHIN({0}, ST_Polygon(ST_GeomFromText('...'), 0))", 
          SENSOR_LOCATION.LOCATION_POINT)
   .fetch();

Note how you can still use some type safety by using the plain SQL templating mechanism as shown above请注意如何通过使用如上所示的纯 SQL 模板机制仍然使用某种类型安全

If you're running lots of GIS queries如果您正在运行大量 GIS 查询

In this case, you probably want to build your own API that encapsulates all the plain SQL usage.在这种情况下,您可能希望构建自己的 API 来封装所有普通的 SQL 用法。 Here's an idea how to get started with that:这是一个如何开始的想法:

public static Condition stWithin(Field<?> left, Field<?> right) {
    return DSL.condition("ST_WITHIN({0}, {1})", left, right);
}

public static Field<?> stPolygon(Field<?> geom, int value) {
    return DSL.field("ST_Polygon({0}, {1})", Object.class, geom, DSL.val(value));
}

If you also want to support binding GIS data types to the JDBC driver, then indeed, custom data type bindings will be the way to go:如果您还想支持将 GIS 数据类型绑定到 JDBC 驱动程序,那么确实,自定义数据类型绑定将是您要走的路:

http://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings http://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings

You will then use your custom data types rather than the above Object.class , and you can then use Field<YourType> rather than Field<?> for additional type safety.然后您将使用您的自定义数据类型而不是上面的Object.class ,然后您可以使用Field<YourType>而不是Field<?>以获得额外的类型安全。

I found jooq-postgis-spatial spatial support: https://github.com/dmitry-zhuravlev/jooq-postgis-spatial我找到了 jooq-postgis-spatial 空间支持: https : //github.com/dmitry-zhuravlev/jooq-postgis-spatial

It allows working with geometries either using jts or postgis types.它允许使用 jts 或 postgis 类型处理几何图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM