We can then load our data through PGAdminIII. You can just right click on a table and select import. Remember to not load the “uid” column, because it is our primary key which will be populated automatically and not in the original data. You will also want to define it as the primary key.
Now we need a geometry column in the airports dataset.
ALTER TABLE airports ADD COLUMN geom geometry(POINT,4326);
We can define our geometry in the airports dataset from the Latitude and Longitude columns.
UPDATE airports SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);
And create a spatial index.
CREATE INDEX idx_airports_geom ON airports USING GIST(geom);
Then we can create a flights table.
CREATE TABLE flights AS
SELECT
air1.geom AS source_geom,
air2.geom AS destination_geom,
airlines.name,
routes.equipment,
routes.destination_airport_id,
routes.source_airport_id,
routes.destination_airport,
routes.source_airport
FROM
public.routes,
public.airlines,
public.airports air1,
public.airports air2
WHERE
routes.airline_id = airlines.airline_id AND
routes.source_airport_id = air1.airport_id AND
routes.destination_airport_id = air2.airport_id;
This table will have a source geometry and a destination geometry along with a few other attributes. I added a primary key to this table as well.
To filter out a specific airport, for example Honolulu we use the “Airport ID”.
CREATE TABLE honolulu_flights AS
SELECT * FROM flights
WHERE destination_airport_id = 3728 OR source_airport_id = 3728;
Then we add in the actual line geometry column.
ALTER TABLE honolulu_flights ADD COLUMN line_geom geometry(LineString,4326);
And populating the great circle geomtrey:
UPDATE honolulu_flights
SET line_geom =
(ST_Segmentize(
(ST_MakeLine(source_geom, destination_geom)::geography)
,100000)::geometry)
;
This is works great to an extent, but QGIS still has some trouble with lines that cross the date-line.
We can fix this using a Pacific centered projection like EPSG:3832.
We can either set QGIS to the projection. Or we can set our geometry to this projection when creating the flight lines.
ALTER TABLE honolulu_flights ADD COLUMN line_geom geometry(LineString,3832);