Haktan Aydın
Haktan Aydın

Reputation: 591

how to save geojson(in javascript) to geometry(MultiPolygon,4326) in PostGIS with Hibernate

I'm working on a jsf project(version 2.2).I have a geoJson object in javascript.The other side I have a column with type of geommetry(MultiPolygon,4326) in PostGIS. I'm using Hibernate 4.3.8 and Postgresql 9.3.

Geometry column definition in model class

@Type(type="org.hibernate.spatial.GeometryType")
@Column(name="geom")
private Polygon geom;

I've also included Hibernate Spatial 4.x to my project and I can access geojson string in managed bean. But I don't know how I can insert this geojson to my database. Probably I have to parse geojson and create polygon object from geojson coordinates and pass to my database. But I didn't succeed. What is the way of doing this?

Thanks in advance.

Upvotes: 3

Views: 2391

Answers (3)

Mathias Vonende
Mathias Vonende

Reputation: 1380

Probably I have to parse geojson and create polygon object from geojson coordinates.

You dont have to do this, the Database can do it for you, and chances are high, that it will be faster than you. Just create a Geometry from GeoJSON. In your Database use the following function:

geometry ST_GeomFromGeoJSON(text geomjson);

This yields a EWKB-Geometry that can be saved in the DB, reprojected, reformatted, etc. The parameter is the GeoJSON as string.

I have no idea how you can trigger this Database-Function with hibernate-spatial, but there should be a functionality to execute sql-queries directly. Be careful though, this can lead to SQL-injection errors.

P.S.: I don't recommend using Object-Relational-Mappers in the context of spatial applications at all, preciseley because of inaccessibilities like this.

Upvotes: 0

Paul Verest II
Paul Verest II

Reputation: 41

You will unlike need db processing over that field, so why no just save inside db as String/BLOB.

Upvotes: 1

jmvivo
jmvivo

Reputation: 2663

Take a look to geojson-jackson project. I think that is what you are looking for (I don't test it yet but looks good).

Good luck!!

Upvotes: 1

Related Questions