1

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.

  • 1
    .qpj files are only created by qgis, not ogr2ogr. Please include your workflow for creating a single shape file. It seems you don't get the geometry information right at the moment. – AndreJ Mar 21 '18 at 10:55
  • You are right about the .qpj file. Thank you for pointing this out. Added single shapefile creation process to my first post. – Maicen19 Mar 21 '18 at 12:26
  • Do all your CSV files have similar structure? Could you add one line into your question? Have you read gdal.org/drv_csv.html and how to use X_POSSIBLE_NAMES and Y_POSSIBLE_NAMES open options? – user30184 Mar 21 '18 at 12:47
  • I don't see any reference to a vrt file in your batch command line. – AndreJ Mar 21 '18 at 15:56
5

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)

  • @user30184 Thank you for the link! @Spacedman Thanks to his comments I was able to find a command that works on my Windows: for /R %f in (*.csv) do ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:4326 -oo X_POSSIBLE_NAMES=Lon* -oo Y_POSSIBLE_NAMES=Lat* -f "ESRI Shapefile" "%~dpnf.shp" %f – Maicen19 Mar 21 '18 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.