[GIS] Join CSV using gdal/ogr

csvgdalshapefile

I'm trying to use gdal/ogr to join a shapefile called myshape.shp with a csv called mycsv.csv.
The join field in the shape in joinshp and the join field in the csv is joincsv.

I've found this command on other post(link) :

ogr2ogr -sql "select inshape.*, joincsv.* from inshape left join 'joincsv.csv'.joincsv on inshape.GISJOIN = joincsv.GISJOIN" shape_join.shp inshape.shp

I tried this but I can not get it to work:

ogr2ogr -sql "select myshape.*, mycsv.* from myshape left join 'mycsv.csv'.mycsv on myshape.joinshp = mycsv.joincsv" shape_join.shp myshape.shp

Can someone help me write this command for my example ?

Best Answer

Seems this question can't be answered with gdal/ogr. Here is another method to join csv and shapefile from linux shell. This method uses a postgres/postgis database :

  • Import your shape in postgres database with shp2pgsql
  • Do the same for the csv (psql)
  • Make a sql join between the shape and the csv (use sql query with Inner join)
  • Export the result as a shapefile with pgsql2shp