Monday 11 March 2019

Loading point data into PostGIS: significant points example

In the previous post I created table and populated it with data from CSV file, but with no spatial 'part'. This time I going to load from CSV source points into database. CSV files are very common and there are plenty of various kind of data stored in this format.
In AIP, ENR part you can find list of  'Name-code designators for significant points'.
If we have this data into CSV forma, we can do following steps to create spatial data:

Create temporary waypoint table:


CREATE TABLE tmp_waypoint (
 rec_id serial PRIMARY KEY,
 ctry_icao_code char(2),
 wpt_ident varchar(5),
 wpt_type varchar(30),
 lat_src varchar(20),
 lon_src varchar(20),
 lat_dd float,
 lon_dd float,
 wpt_location geography(POINT, 4326)
);

Import data from CSV into temporary table, with 'empty' spatial column:

COPY tmp_waypoint(ctry_icao_code, wpt_ident, wpt_type, lat_src, lon_src, lat_dd, lon_dd) 
FROM 'C:\significant_points.csv' DELIMITER ';' CSV HEADER;

Update wpt_location column with geometry infromation based on lat_dd and lot_dd columns:

UPDATE tmp_waypoint
SET wpt_location = ST_GeomFromText('POINT(' || lon_dd || ' ' || lat_dd || ')',4326);

Now we got points in tmp_waypoint table and we are able to visualize and manipulate it using GIS software or spatial queries in PostGIS.  For example, we have following result so far:

But, we want waypoints in waypoint table. So we need to import data from tmp_waypoint into waypoint able without lat_dd and lon_dd columns. Following query will do this task:

INSERT INTO waypoint (rec_id, ctry_icao_code, wpt_ident, wpt_type, lat_src, lon_src, wpt_location)
SELECT  nextval('waypoint_rec_id_seq'), ctry_icao_code, wpt_ident, wpt_type, lat_src, lon_src, wpt_location FROM tmp_waypoint;

And final result is:

Notice, that I use nextval() function to get values of rec_id column.
At the and, we can delete table tmp_waypoint:

DROP TABLE tmp_waypoint


No comments:

Post a Comment