Hibernate Spatial创建几何对象

时间:2017-01-20 05:52:20

标签: java hibernate oracle-spatial hibernate-spatial

我正在使用Java中的oracle Spatial开发一个位置应用程序。我正在为orm层使用hibernate 4.0空间。我是空间新手,我无法找到冬眠空间的最佳实践。我的数据库模型如下;

CREATE TABLE SYSTEM.POI  (
    POI_ID          INTEGER,
    SERVICE_ID      INTEGER,
    POI_NAME        VARCHAR2(255 CHAR),
    DESCRIPTION     VARCHAR2(1023 CHAR),
    CATEGORY        VARCHAR2(127 CHAR),
    ADDRESS         VARCHAR2(4000 CHAR),
    MOBILE_PHONE    VARCHAR2(15 CHAR),
    FIXED_PHONE     VARCHAR2(15 CHAR),
    BUSINESS_HOURS  VARCHAR2(1023 CHAR),
    SHAPE           SDO_GEOMETRY  
)

插入语句如下;

INSERT INTO SYSTEM.POI (SERVICE_ID, POI_NAME, DESCRIPTION, CATEGORY, ADDRESS, MOBILE_PHONE, FIXED_PHONE, BUSINESS_HOURS, SHAPE)
VALUES(
        '1320',
        'PO-Kral Petrol Ürünleri San ve Tic. Ltd. şti.',
        'Camilerimiz',
        'CAMI',
        'İSLİCE MAHALLESİ DERE SOKAK NO:5',
        '2762151093',
        '5552552343',
        'Hafta içi  09:00 - 17:00, haftasonu 09:00 - 13:00 açık',
        SDO_GEOMETRY(
            2001, 
            3785,
            SDO_POINT_TYPE(28.90762, 41.1521, NULL),
            NULL,
            NULL
        )
      );  

实体模型跟随我只是共享sdo_geometry对象;

import com.vividsolutions.jts.geom.Geometry;

...

    @Column(name="SHAPE")
    @Type(type = "org.hibernatespatial.GeometryUserType")
    private Geometry shape;

    public Geometry getShape() {
      return shape;
    }

    public void setShape(Geometry shape) {
      this.shape = shape;
    }

如何用动态参数填充此spahe对象。例如;

  SDO_GEOMETRY(
                2001,  --> sdo_gtype
                3785,  --> sdo_srid
                SDO_POINT_TYPE(28.90762, 41.1521, NULL),  --> sdo_point
                NULL,  --> sdo_elem_info
                NULL   --> sdo_ordinates
            )

如何动态地将sdo_gtype,sdo_srid,sdo_point_type参数传递给几何对象?

1 个答案:

答案 0 :(得分:7)

您可以创建如下所示。

com.vividsolutions.jts.geom.Point p = new GeometryFactory().createPoint(new Coordinate(12.34343, 12.232424));
System.out.println(p);

GeonmetryFactory构造函数中,您可以传递precision model,srid和CoordinateReferenceSystem。

然而JTS周围有geolatte的空间包装,它随Hibernate Spatial一起提供(在maven中自动下载jar)。您可以使用Point创建Geolatte,如下所示。

        org.geolatte.geom.Point<G2D> point = Geometries.mkPoint(new G2D(12.34343, 12.232424), CoordinateReferenceSystems.WGS84);
        System.out.println(point);

有实用工具类可以从JTS转换为Geolatte,反之亦然。并附上程序的输出。

        org.geolatte.geom.Point<G2D> point = Geometries.mkPoint(new G2D(12.34343, 12.232424), CoordinateReferenceSystems.WGS84);
        System.out.println(point);
        com.vividsolutions.jts.geom.Point p = new GeometryFactory(new PrecisionModel(), 4326).createPoint(new Coordinate(12.34343, 12.232424));
        System.out.println(p);
        System.out.println(JTS.to(point));
        System.out.println(JTS.from(p));

<强>输出:

SRID=4326;POINT(12.34343 12.232424)
POINT (12.34343 12.232424)
POINT (12.34343 12.232424)
SRID=4326;POINT(12.34343 12.232424)

<强>更新

Geolatte支持您正在寻找的投影,Geolatte支持here SRID,因为您使用的是Project CS,所以您需要使用C2D(笛卡尔2D)

这是创建

的代码示例
        System.out.println(CrsRegistry.getProjectedCoordinateReferenceSystemForEPSG(3785).getName());
        org.geolatte.geom.Point<C2D> point1 = Geometries.mkPoint(new C2D(12.34343, 12.232424), CrsRegistry.getProjectedCoordinateReferenceSystemForEPSG(3785));
        System.out.println(point1);

输出:

Popular Visualisation CRS / Mercator (deprecated)
SRID=3785;POINT(12.34343 12.232424)