TransWikia.com

pgrouting/PostGIS Trying to set nearest node to airports in a region brings the same node for each airport

Geographic Information Systems Asked by marcbosch on May 18, 2021

I need to make a distance matrix from all the nodes in the Canary Islands to every airport in the region. I’m following the method set here but it brings the same node for all airports. Trying the method here has brought me similar problems. The query I’m running is as follows

create table aeropuertos_canarias as (SELECT * from rt_aerodromo_p WHERE cod_iata in ('LPA', 'VDE', 'FUE', 'GMZ', 'SPC', 'ACE', 'TFN', 'TFS'));
ALTER TABLE aeropuertos_canarias
   ADD COLUMN nearest_node integer;
select * from aeropuertos_canarias 


CREATE TABLE temp AS
   SELECT a.nombre, b.id, min(a.dist)
   FROM
     (SELECT airport.nombre, 
             min(ST_Distance(ST_SetSRID(airport.geom, 4326), ST_SetSRID(node.the_geom,4326))) AS dist
      FROM aeropuertos_canarias as airport, edges_can_noded_vertices_pgr as node
      GROUP BY airport.nombre) AS a,
     (SELECT airport.nombre, node.id, 
             ST_Distance(ST_SetSRID(airport.geom, 4326), ST_SetSRID(node.the_geom,4326)) AS dist
      FROM aeropuertos_canarias as airport, edges_can_noded_vertices_pgr as node) AS b
   WHERE a.dist = b. dist
         AND a.nombre = b.nombre
   GROUP BY a.nombre, b.id;
select * from temp;

However, this brings me the following result.

"Aeropuerto de El Hierro"   74411   3625064.97072886
"Aeropuerto de Fuerteventura"   74411   3625066.16827048
"Aeropuerto de Gran Canaria"    74411   3625065.96589645
"Aeropuerto de La Gomera"   74411   3625065.07375698
"Aeropuerto de La Palma"    74411   3625064.29973004
"Aeropuerto de Lanzarote - César Manrique"  74411   3625065.83955541
"Aeropuerto de Los Rodeos"  74411   3625065.05111873
"Aropuerto Reina Sofía" 74411   3625065.34297412

Which is pretty obviously not true. Any idea about what I’m doing wrong and where will be most welcome.

One Answer

ST_Distance returns distances in the geometries projection unit, so in degrees since you are explicitly using 4326. This is the issue, because the returned distances are much higher than 180.

It points to the fact that one, or both, of your coordinates are not in 4326 but in a different projected CRS. You must not use ST_SetSRID, which keeps the numerical values and assign the given projection, but instead use ST_Transform to change the numerical values so they fit the given projection.

Also, as a side note, use ST_DistanceSphere if you decide to use 4326, else the returned distance is meaningless.

Correct answer by JGH on May 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP