Top 5 Recent Articles
ARTICLES CATEGORIES
- Algorithms (22)
- All (399)
- Biography (1)
- Blog (44)
- Business Requirements (1)
- Commentary (1)
- Conversion (2)
- Customers (2)
- Data Models (1)
- Education (2)
- GeoRaptor (13)
- GPS (1)
- Image Processing (2)
- Import Export (8)
- Licensing (2)
- LiDAR (1)
- Linear Referencing (4)
- Manifold GIS (3)
- Mapping (1)
- MySQL Spatial (7)
- Networking and Routing (including Optimization) (5)
- Open Source (18)
- Oracle Spatial and Locator (194)
- Partitioning (1)
- PostGIS (36)
- Projections (1)
- Published Articles (1)
- qGIS (1)
- Recommendations (1)
- Services (1)
- Software Change Log (1)
- Source Code (37)
- Space Curves (9)
- Spatial Database Functions (109)
- Spatial DB comparison (1)
- Spatial XML Processing (11)
- SQL Server Spatial (92)
- Standards (3)
- Stored Procedure (17)
- Tessellation or Gridding (10)
- Tools (2)
- Topological Relationships (1)
- Training (2)
- Triangulation (2)
Loading Point Data from a CSV File in PostGIS
I recently wrote an article on the use of External File with Oracle tables to load point data into Oracle.
I thought I would have a look on how to do this in PostGIS.
A bit of research came up with the PostgreSQL COPY SQL command.
There appears to be no equivalent to Oracle’s Organisation External for a table. In PostgreSQL you simply create your table and then insert data into it from an external file. You don’t seem to need to concern yourself with special permissions to do this: just get in and do it.
Which is what we will do right now.
-- First off let's create our target table DROP TABLE lidar; Query returned successfully with no result in 15 ms. CREATE TABLE lidar( easting float, northing float, height float ); Query returned successfully with no result in 16 ms. -- Now we can execute the COPY command to load our data (PostgreSQL or perhaps PGAdminIII) gets -- a little upset if we use Windows backslashes between the folders in the filename. -- Yes, you can use a double backslash which will work but PostgreSQL will continue to complain. -- Solution: simply use Linux/Unix forward slash filenames. -- COPY lidar ( easting, northing, height ) FROM 'c:/temp/lidar/ground.csv' WITH DELIMITER AS ',' CSV HEADER ; Query returned successfully: 40401 rows affected, 297 ms execution time. -- Now let's query the loaded data. SELECT count(*) FROM lidar l WHERE l.easting BETWEEN 478040 AND 478060 AND l.northing BETWEEN 5228050 AND 5228070 AND l.height BETWEEN 200 AND 600; count bigint ------ 301 -- Now let's add a 3D point geometry column to the table... SELECT AddGeometryColumn('public', 'lidar', 'geom', 28355, 'POINT', 3); addgeometrycolumn text ----------------- "public.lidar.geom SRID:28355 TYPE:POINT DIMS:3" -- Now populate the added column update lidar set geom = ST_SetSRID(ST_MakePoint(easting,northing,height),28355); Query returned successfully: 40401 rows affected, 359 ms execution time. -- Create a spatial index for faster querying CREATE INDEX lidar_geom ON lidar USING GIST ( geom ); Query returned successfully with no result in 1344 ms. -- Now, use the newly indexed spatial column in the spatial equivalent of the above query SELECT count(*) FROM lidar WHERE geom && SetSRID('BOX3D(478040 5228050,478060 5228070)'::box3d,28355) AND height BETWEEN 200 AND 600; count bigint ------ 301
Now, that was pretty simple. There are certainly advantages to the PostgreSQL way of loading CSV organised data. I am not sure about the lack of security with regards to external file access though. One thing in Oracle’s favour is that externally organised tables can be used without having to load the data which can be useful in many situations.
Anyway, I hope this article is of use to someone.
Documentation
- MySQL Spatial General Functions
- Oracle LRS Objects
- Oracle Spatial Exporter (Java + pl/SQL)
- Oracle Spatial Object Functions
- Oracle Spatial Object Functions (Multi Page)
- PostGIS pl/pgSQL Functions
- SC4O Oracle Java Topology Suite (Java + pl/SQL)
- SQL Server Spatial General TSQL Functions
- SQL Server Spatial LRS TSQL Functions