简体   繁体   English

MySQL POLYGON ←→ Jts Polygon with JOOQ (as WKT)

[英]MySQL POLYGON ←→ Jts Polygon with JOOQ (as WKT)

I'm trying to get a seamlessly conversion between MySQL POLYGON type, and Jts Polygon while using jOOQ (3.9.x).我正在尝试在使用 jOOQ (3.9.x) 时在 MySQL POLYGON 类型和 Jts Polygon 之间进行无缝转换。 Ideally, I want to just parse the WKT (well known text) from the DB into the Jts type.理想情况下,我只想将数据库中的 WKT(众所周知的文本)解析为 Jts 类型。 However, the generated queries are not working, I see that they get simple quote marks around the function that converts to text, rendering it just text.但是,生成的查询不起作用,我看到它们在转换为文本的函数周围得到了简单的引号,将其呈现为文本。 Here's the converter and the binding that I'm using.这是我正在使用的转换器和绑定。 Am I doing this right?我这样做对吗? How should I approach the conversion via ST_AsWKT and ST_GeomFromText?我应该如何通过 ST_AsWKT 和 ST_GeomFromText 进行转换?

public class PolygonConverter implements Converter<Object, Polygon> {


    /**
     * Convert WK string into Polygon
     */

    @Override
    public Polygon from(Object databaseObject) {
        if (databaseObject == null) {
            return null;
        }
        String wkString = databaseObject.toString();
        WKTReader reader = JtsSpatialContext.GEO.getWktShapeParser();
        try {
            Polygon poly = (Polygon)reader.parse(wkString);
            return poly;
        } catch (java.text.ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    public Object to(Polygon userObject) {
        if (userObject == null) {
            return null;
        }
        return userObject.toString();
    }

    @Override
    public Class<Object> fromType() {
        return Object.class;
    }

    @Override
    public Class<Polygon> toType() {
        return Polygon.class;
    }
}

The Binding:绑定:

public class MySQLPolygonBinding implements Binding<Object, Polygon> {
    @Override
    public Converter<Object, Polygon> converter() {
        return new PolygonConverter();
    }

    @Override
    public void sql(BindingSQLContext<Polygon> ctx) throws SQLException {
        ctx.render().visit(DSL.sql("ST_AsWKT(?)"));
    }

    @Override
    public void register(BindingRegisterContext<Polygon> ctx) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public void set(BindingSetStatementContext<Polygon> ctx) throws SQLException {
        String resultStr = null;
        Object obj = ctx.convert(converter()).value();
        if (obj != null) {
            resultStr = String.format("ST_GeomFromText('%s')", obj,toString() );
        }
        ctx.statement().setObject(ctx.index(), resultStr);
    }

    @Override
    public void set(BindingSetSQLOutputContext<Polygon> ctx) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public void get(BindingGetResultSetContext<Polygon> ctx) throws SQLException {
        ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
    }

    @Override
    public void get(BindingGetStatementContext<Polygon> ctx) throws SQLException {
        ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
    }

    @Override
    public void get(BindingGetSQLInputContext<Polygon> ctx) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }
}

Note that starting from jOOQ 3.16 ( see #982 ), jOOQ supports various popular GIS implementations out of the box.请注意,从 jOOQ 3.16(见#982 )开始,jOOQ 支持各种流行的开箱即用的 GIS 实现。

From what I understand, you'd like two things to happen automatically:据我了解,您希望自动发生两件事:

String bind values should be wrapped with the ST_GeomFromText() function字符串绑定值应该用ST_GeomFromText()函数包装

This is the "easy" part and you almost got it right:这是“简单”的部分,你几乎做对了:

@Override
public void sql(BindingSQLContext<Polygon> ctx) throws SQLException {
    ctx.render()
       .sql("ST_GeomFromText(")
       // This will use your converter to convert from a Polygon to "Object"
       // prior to binding the variable
       .visit(DSL.val(ctx.convert(converter()).value()))
       .sql(")");
}


@Override
public void set(BindingSetStatementContext<Polygon> ctx) throws SQLException {
    // No wrapping of the bind variable in functions can be done here!
    ctx.statement().setString(ctx.index(), ctx.convert(converter()).value());
}

Polygon type columns should be formatted with the ST_AsWKT() function多边形类型列应使用ST_AsWKT()函数进行格式化

You (probably) don't have to do anything specific here inside of the binding, as your binding cannot influence any expression where no bind variable is involved (eg an ordinary column expression).您(可能)不必在绑定内部执行任何特定操作,因为您的绑定不会影响任何不涉及绑定变量的表达式(例如普通列表达式)。

Instead, you might need to adapt your converter (the one inside the binding) to be able to read any type that comes back from the JDBC driver and convert that into a Polygon相反,您可能需要调整您的转换器(绑定内部的转换器)才能读取从 JDBC 驱动程序返回的任何类型并将其转换为Polygon

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

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