[GIS] Bulk csv to shapefile (using ogr2ogr)

csvogr2ogrqgisshapefile

Is bulk conversion from csv to shapefile possible with ogr2ogr? I can do it file by file when I first create dbf file, then using vrt file I create shapefile. But using command

for /R %f in (*.csv) do ogr2ogr -t_srs EPSG:4326 -f "ESRI Shapefile" "%~dpnf.shp" "%f

I am able to get only the dbf and prj file, but not qpj, shp nor shx files. I have hundreds of csv files in one folder I would like to convert to shapefiles, but I'm on half way to achieve this.

Edit
For a single file I first created .dbf file: ogr2ogr -f "ESRI Shapefile" all8.dbf all8.csv

Then, I created all8.vrt file:

<OGRVRTDataSource>
<OGRVRTLayer name="all8">
<SrcDataSource>all8.csv</SrcDataSource>
<SrcLayer>all8</SrcLayer>
<GeometryType>wkbPoint</GeometryType>
<LayerSRS>WGS84</LayerSRS>
<GeometryField encoding="PointFromColumns" x="Longitude" y="Latitude"/>
</OGRVRTLayer>
</OGRVRTDataSource>

And finally run: ogr2ogr -f "ESRI Shapefile" all8 all8.vrt

I also tried to replace "all8" in .vrt file with "*" (to create multiple shapefiles using only one .vrt file), but that did'nt work.

Best Answer

A modern-enough GDAL can convert CSV files directly into shapefiles. For the test.csv file in the documentation, http://www.gdal.org/drv_csv.html - you can do:

ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:3857 -oo X_POSSIBLE_NAMES=Lon* -oo Y_POSSIBLE_NAMES=Lat*  -f "ESRI Shapefile" test.shp test.csv

to create test.shp (and .dbf et al) from test.csv, converting from EPSG:4326 to EPSG:3857 as it goes.

You seem to be using Windows command syntax to loop over your files, which I dont know, but all you need to change is the destination and source names for each shapefile.

In a unix shell the relevant loop syntax is:

for f in *.csv ; do echo $f ${f%csv}shp ; done

(replace echo with your command, and put "$f" and "${f%csv}shp" where you want the csv and shapefile in the command)