如何在不使用nHibernate空间的情况下使用nHibernate将PostGis几何列映射为wkt [英] How to map PostGis geometry column as wkt with nHibernate not using nHibernate spatial

查看:101
本文介绍了如何在不使用nHibernate空间的情况下使用nHibernate将PostGis几何列映射为wkt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用nHibernate将Postgis几何列读取为wkt.我知道我可以使用nHibernate Spatial,但这不是我的选择.

I need to read a postgis geometry column as wkt using nHibernate. I know I could use nHibernate Spatial, but that is not an option in my case.

我看过这篇文章:最佳方法在NHibernate(流利)中映射一个隐藏的属性,其中wkt存储在另一列中,他使用触发器来更新实际的几何形状.但是,项目所有者不想为此增加额外的列.

I have seen this post: Best way to map a hidden property in NHibernate (fluent) where the wkt is stored in another column and he uses triggers to update the actual geometry. However the project owners do not want to have an extra column for this purpose.

我也找到了该线程:在Hibernate中透明地使用PostGIS列 ,但没有提供答案.

I found this thread as well: Use PostGIS columns transparently in Hibernate, but no has provided an answer to that.

我相信我需要编写一个自定义类型,但是我确实需要有人为我指出正确的方向.我以前没有使用过Postgresql/postgis.

I believe I need to write a custom type, but I would really need someone to point me to right direction. I have not worked with Postgresql/postgis earlier.

Thnx!

推荐答案

我通过使用nHibernates sql-interceptor解决了该问题.不是最可靠的解决方案,但是通过这种方式,我可以为PostGis和Oracle使用不同的拦截器.

I solved the problem by using nHibernates sql-interceptor. Not the most robust solution but this way I can have different interceptors for PostGis and Oracle.

我还研究了nHibernate.Spatial及其几何类型.但是我不想使用NetTopologySuite,因此我应该需要编写更多代码.

I also looked into nHibernate.Spatial and their geometry types. But I did not want to use NetTopologySuite and therefore I should have needed to code a lot more.

以下是代码的一部分:

public class PostGisGeometrySqlInterceptor : IInterceptor
{
   ...

    /// <summary>
    /// Modifies all the select statements with geometry so that 
    /// xxx.geometry -> ST_AsText(xxx.geometry)
    /// </summary>
    /// <param name="sql">The original sql string.</param>
    /// <returns>The modified sql string.</returns>
    /// <remarks>
    /// All the geometry fields must be called 'geometry'. 
    /// Works only for one geometry in the sql string.
    /// </remarks>
    public virtual SqlString OnPrepareStatement(SqlString sql)
    {
        if (sql.StartsWithCaseInsensitive("SELECT") && sql.ToString().Contains("geometry"))
        {
            var indexOfGeometry = sql.IndexOfCaseInsensitive("geometry");
            var indexOfSpace = sql.Substring(0, indexOfGeometry).LastIndexOfCaseInsensitive(" ") + 1;

            var oldValue = sql.ToString(indexOfSpace, indexOfGeometry - indexOfSpace + "geometry".Length);
            var newValue = string.Format("ST_AsText({0})", oldValue);
            sql = sql.Replace(oldValue, newValue);
        }
        else if(...)

        ...

        return sql;   
    }

这篇关于如何在不使用nHibernate空间的情况下使用nHibernate将PostGis几何列映射为wkt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆