Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a shapefile with several attributes, for example YEAR, COUNTY, and AREA. I also have a CSV file with more fields that I want in the shapefile, such as POPULATION. Both the shapefile and the CSV file have a field GISJOIN. I know how to do a join in QGIS. But how can I make a permanent join and write to a shapefile using ogr2ogr or one of the other tools in GDAL/OGR?

share|improve this question
    
Does your GDAL/OGR build have spatialite or sqlite support? –  BradHards May 13 at 0:30
    
@BradHards: It doesn't look like it. It's the packaged GDAL/OGR build from Ubuntu 14.04. –  Lincoln Mullen May 13 at 0:48

2 Answers 2

up vote 8 down vote accepted

The ogr2ogr utility supports a limited sql syntax. You can join your CSV to the shapefile using something like the following:

ogr2ogr -sql "select inshape.*, joincsv.* from inshape left join 'joincsv.csv'.joincsv on inshape.GISJOIN = joincsv.GISJOIN" shape_join.shp inshape.shp
share|improve this answer
1  
Oh gdal/ogr - you never cease to amaze me.. –  radek May 13 at 7:49

The accepted answer is really useful, but I found that it was slow with a large-ish database. I believe it also limits your options when joining the data.

My method now is to pull everything into SQLite (using a combination of csvkit and ogr2ogr):

csvsql --db sqlite:///myjoindb.db --insert myjoincsv.csv
ogr2ogr -f "SQLite" myjoindb.db myjoinshp.shp

Then join everything and create a shapefile out of it:

ogr2ogr -f "ESRI Shapefile" -sql "SELECT csv.*, shp.* FROM myjoinshp shp INNER JOIN myjoincsv csv ON csv.joinfield = shp.joinfield" joined_output.shp myjoindb.db
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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