5

I use hibernate spatial attach a geolocation to a car. My card domain class looks like this:

import com.vividsolutions.jts.geom.Point
class Card {
  String name
  Point location 
}

My program is in Grails so the samples I gave are in Groovy. I found a similar post here which does not really answer the most important question on how to specify the radius correctly to set n kilimeter for the radius.

Here is how I compute a circle Geometry:

  private static Geometry createCircle(double x, double y, final double RADIUS) {
    GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
    shapeFactory.setNumPoints(1000);
    shapeFactory.setCentre(new Coordinate(x, y))
    shapeFactory.setSize( (RADIUS * 2)/88.1)

    return shapeFactory.createCircle().getBoundary()
  }

The size of the circle is divided by 88.1 the is just a dirty fix to get an approximate dimension but it is still wrong.

My query is done like this:

double radius = 40
Geometry filter = createCircle(car.location.x, car.location.y, radius)
Session session = sessionFactory.currentSession
Query q = session.createQuery("select c from Car c where within(c.location, ?) = true")
q.setParameter(0, filter, GeometryUserType.TYPE) 
q.list()

This works not very accurate. Some of the cars which should be outside circle are returned from this query.

Here is an example. My center is Hamburg and the radius is 40km. I made a google maps visualization.

Here is when I set radius = 40:

enter image description here

You can see that at the top left one car which has a location outside of the circle is still drawn. This should not be the case. It appears to me that the circle I draw with google maps is not equal to the circle Geometry I draw in code for my query.

Here is when I set radius = 30:

enter image description here

You see that the cars at the bottom right disappear which is correct, but the car at the top left still remains in the query.

When I draw the circle I created with createCircle I get the following (using getCoordinates() to get the coordinates of the circle):

enter image description here

How can I query all cars within a radius of 40km?

Community
  • 1
  • 1
Michael
  • 32,527
  • 49
  • 210
  • 370

1 Answers1

1

Try to compute the distance between the geometries instead. You can use dwithin function (see Hibernate spatial documentation ):

select c from Car c where dwithin(c.location, :geom, :dist) = true

or just distance:

select c from Car c where distance(c.location, :geom) < :dist

Also, don't forget transform distance measure to degrees (for WGS84 SRS).

edited:

Use geom the centre of your imaginary circle, so you don't have to generate a geometry, just filter by distance.

P.D.: In this post you can found a description about the problem of transformation of degrees and meters

Good luck!. Chema.

jmvivo
  • 2,653
  • 1
  • 16
  • 20
  • Still don't get it. Can I use Hibernate Spatial to work with locations on earth? How do I transform distance to degrees – Michael Jul 28 '15 at 09:37
  • What is geom? Is it my circle? Isn't the problem that the points of the circle do not correct represent a circle on earth? How do I fix that? – Michael Jul 28 '15 at 09:39
  • As far as I understand hibernate spatial (I use it with MySQL) uses flat geometry. When dealing with coordinates on earth, which I do, you have to change it to some other coordinate system. Can I use hibernate spatial querier to deal with coordinates on earth correctly? Do I have to transform my coordinates to WGS84 or whatever to get the same results as on google maps. Please help me on that. I am lost: – Michael Jul 28 '15 at 11:36
  • Another problem is that MYSQL does not have ``dwithin``and ``distance`` according to this: http://www.hibernatespatial.org/documentation/03-dialects/01-overview/ – Michael Jul 28 '15 at 11:42
  • MySQL spatial support is just to store geometries. Operations over this type of data is too much limited. I found this recipe (https://gist.github.com/oschrenk/2787570) which uses geotools (I don't test it) that could help you. Good luck! – jmvivo Jul 28 '15 at 19:46
  • Do I have Transform the latitude and longitude coordinates I get from google maps somehow? What about WGS84? When do I have to use this? – Michael Jul 28 '15 at 20:52