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
:
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
:
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):
How can I query all cars within a radius of 40km? How do I get the correct coordinate system?
No comments:
Post a Comment