如何在 hibernate-spatial 中使用 PostgisFunctions 来转换 CRS 几何

How to use PostgisFunctions in hibernate-spatial to transform CRS geometry

几何字段由 org.locationtech.jts.geom 定义,因此关于空间方法,可以实现以下方法:covers, within, buffer, contains...(see Concrete Methods)
org.locationtech.jts.geom不提供转换 CRS 的方法,但是 hibernate-spatial 提供(see hibernate-spatial dialect function support)。

我们如何通过 hibernate-spatial 调用 Postgis 函数,例如 transform
正如 PostgisPG95Dialect class:
Extends the {@code PostgreSQL95Dialect} to add support for the Postgis spatial types, functions and operators 的评论中所述,但是使用 PostgisPG95DialectPostgisFunctions 或直接通过 geometry没找到。

PostgreSQL95Dialect.transform(geometry,4326)
SpatialFunction.transform(geometry,4326)
PostgisFunctions.transform(geometry,4326)
geometry.transform(4326)

如果必须使用 org.hibernate.spatial.GeolatteGeometryType (see hibernate-spatial types) 映射几何图形,则会出现以下错误(错误与 org.hibernate.spatial.JTSGeometryType 相同):

Caused by: org.hibernate.MappingException: Could not instantiate Type: org.hibernate.spatial.GeolatteGeometryType
at org.hibernate.type.TypeFactory.type(TypeFactory.java:113)
at org.hibernate.type.TypeFactory.byClass(TypeFactory.java:70)
at org.hibernate.type.TypeResolver.heuristicType(TypeResolver.java:126)
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:473)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:455)
at org.hibernate.mapping.Property.isValid(Property.java:227)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624)
at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:351)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:464)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.grails.orm.hibernate.cfg.HibernateMappingContextConfiguration.buildSessionFactory(HibernateMappingContextConfiguration.java:287)
at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.create(HibernateConnectionSourceFactory.java:86)
at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:39)
at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:212)
at jdk.internal.reflect.GeneratedConstructorAccessor83.newInstance(Unknown Source)
atjava.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)
... 127 common frames omitted
Caused by: java.lang.InstantiationException: org.hibernate.spatial.GeolatteGeometryType
at java.base/java.lang.Class.newInstance(Class.java:571)
at org.hibernate.type.TypeFactory.type(TypeFactory.java:105)
... 149 common frames omitted
Caused by: java.lang.NoSuchMethodException: org.hibernate.spatial.GeolatteGeometryType.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350)
at java.base/java.lang.Class.newInstance(Class.java:556)
... 150 common frames omitted

已找到直接通过休眠的解决方案sql。
Hibernate-Spatial PostGIS 例如 transform 函数可以使用 Hibernate SQL 语法查询在域 Class 及其在 Grails 中的几何字段上调用,如下例所示:

Geometry geometry4326 =  Parcel.executeQuery("select transform(geometry,4326) as geom from Parcel where id=1").first()

几何字段保留 locationtech.jts 定义。