Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Spatial Create Geometry object

I am developing an location app with oracle spatial in Java. I am using hibernate 4.0 spatial for orm layer. I am new in spatial and i couldn't find best practise for hibernate spatial. My database model following;

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 statement is following;

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
        )
      );  

entity model is following i'm sharing just sdo_geometry object;

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;
    }

How can i fill this spahe object with dynamic parameters. For example;

  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
            )

How to pass sdo_gtype,sdo_srid,sdo_point_type parameters to geometry object dynamically?

like image 892
mertaksu Avatar asked Jan 20 '17 05:01

mertaksu


1 Answers

You can create as shown below.

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

In the GeonmetryFactory constructor you can pass precision model, srid and CoordinateReferenceSystem.

However there is spatial wrapper around JTS called geolatte, which is shipped with Hibernate Spatial(automatically downloads jars in maven). You can create Point using Geolatte as shown below.

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

There are utility class to tranform from JTS to Geolatte and vice-versa. And attaching output of the program also.

        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));

Output:

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

UPDATE

Projection you are looking for is supported by Geolatte, here are SRID supported by Geolatte, Since you are using Project CS, you need to use C2D(Cartesian 2D)

Here is code example to create

        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);

output:

Popular Visualisation CRS / Mercator (deprecated)
SRID=3785;POINT(12.34343 12.232424)
like image 137
Karthik Prasad Avatar answered Nov 07 '22 20:11

Karthik Prasad