While we already know how to make trivial shapefiles with shapelib, sometimes that’s too tedious. Very frequently I get data in Comma Separated Value (CSV) format, and reliably importing/converting it can be a pain.
Here’s our sample CSV file,
"Easting","Northing","Library" 625539.6,4837170.9,"Dufferin St. Clair" 625862.0,4838241.1,"Oakwood Village" 626251.0,4835287.2,"Bloor Gladstone" 626671.7,4836922.6,"Davenport" 627227.2,4840006.4,"Forest Hill"
ogr has a CSV driver. In its documentation the Virtual Format driver is touched upon. This allows you to set up a data definition file, especially useful if you read the same format frequently.
Here’s the VRT file for that CSV:
&amp;lt;OGRVRTDataSource&amp;gt; &amp;lt;!-- note that OGRVRTLayer name must be basename of source file --&amp;gt; &amp;lt;OGRVRTLayer name=&amp;quot;library_test&amp;quot;&amp;gt; &amp;lt;SrcDataSource&amp;gt;library_test.csv&amp;lt;/SrcDataSource&amp;gt; &amp;lt;GeometryType&amp;gt;wkbPoint&amp;lt;/GeometryType&amp;gt; &amp;lt;!-- your SRS goes here; I used EPSG SRID --&amp;gt; &amp;lt;LayerSRS&amp;gt;EPSG:2958&amp;lt;/LayerSRS&amp;gt; &amp;lt;GeometryField encoding=&amp;quot;PointFromColumns&amp;quot; x=&amp;quot;Easting&amp;quot; y=&amp;quot;Northing&amp;quot;/&amp;gt; &amp;lt;/OGRVRTLayer&amp;gt; &amp;lt;/OGRVRTDataSource&amp;gt;
Your CSV file will now behave like a shapefile, or indeed any other geo-format that OGR understands. QGIS is a bit picky – it doesn’t seem to always work out the path of the source file.
To prove these are real coordinates, here’s what I did to make a Google Earth KML file:
ogr2ogr -f KML -t_srs EPSG:4326 library_test.kml library_test.vrt -dsco NameField=Library
Technically, you don’t need to specify the SRS for KML output as it only supports EPSG:4326, but I found you got trivially different results if it was omitted.
Try this in Google Earth: library_test.kml
One reply on “using CSV as a virtual data source”
I should really add something about the csvt column format files. If you can hint to OGR what type each column contains, the conversion results will be much more repeatable.