Hibernate Spatial Create Geometry object
Asked Answered
C

1

7

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?

Cordi answered 20/1, 2017 at 5:52 Comment(2)
Not an answer, but just something I want to point out: your SRID is set to 3785. This the global spherical mercator projection (as used by Google). But your coordinates look like WGS84 long/lat. The proper SRID for that is 4326.Longdrawnout
yes you re right my fail 3785. I changed it 8307 for long/lat wgs84Cordi
T
12

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)
Trout answered 23/1, 2017 at 10:58 Comment(1)
Thank for you answer i tried but your srid and mine is difference. i am using 3785 srid. I couldnt find this srid in CoordinateReferenceSystems is there this srid?Cordi

© 2022 - 2024 — McMap. All rights reserved.