[GIS] Calculating distance in WGS84 (MySQL and Geoserver)

distancegeoreferencinggeoserverMySQL

Here is the environment in which I work:

  • An application using geo-referenced data
  • Database MySQL 5.5, storing georeferenced points
  • A Geoserver server, connected to the MySQL database

Here is the context (common to many people):
I have to calculate the distance between two points stored in my DB (and better, I want all the points within a known distance)

I have searched about the methods available to me on the net, and here are my results:

  • MySQL: I can use the method GLength of the LineString type , but the result is in degrees, which is useless. In addition, the SRID (mine is 4326) can not be used.
  • WFS protocol: I can connect to GeoServer WFS with a query, but currently there is an open issue with DWithin method (the one that interests me), indicating that the parameter "unit" cannot be used (http://jira.codehaus.org/browse/GEOS-937).
  • WPS protocol: I saw a method "Distance". But there is little information, and this method does not seem to use a calculation method that suits me (https://wiki.state.ma.us/confluence/display/massgis/GeoServer+-+WPS+-+Distance).

I have not found other potential solutions.

Are there errors in the results that I described?

I cannot install and use PostgreSQL. I know that it can be done with it.

Best Answer

The code above was tested in Postgre and PostGIS 2.1 :

I have a table named UNITS and need to search units near by 500 Kilometers to the "MYSERIAL" unit. So I make the query using two alias to same table UNITS, one to refer MYSERIAL (p1) and other to search near units and show the DISTANCE (p2). A nested query is necessary to order by DISTANCE.

SELECT * FROM ( 
  SELECT p2.*, 
    ST_X(p2.the_geom) AS longitude, 
    ST_Y(p2.the_geom) as latitude ,
    ST_Distance_Sphere(p1.the_geom,p2.the_geom) as distance 
  FROM units AS p1, units AS p2 where p2.serial<> 'MYSERIAL' and p1.serial = 'MYSERIAL' order by distance asc) as t1 
WHERE ( t1.distance / 1000 ) < 500 order by t1.distance asc

Note P1 refer to MYUNIT, so P2 must ignore the searched unit, so I make p2.serial <> 'MYSERIAL' to avoid include itself in result (distance = 0). I use ST_Distance_Sphere (PostGIS) to make sure the distance respects the used projection.

Related Question