Last week I returned from the amazing FOSS4G-NA conference in Raleigh, NC, but I just couldn’t get back to my pre-conference self! In the spirit of continuous learning, I’ve decided to try finding better ways to solve common problems, and to explore awesome free and open source geospatial technologies further.
This will be a series of reviews for various features and hacks that I come across while tackling daily GIS/coding tasks, small and big. Hopefully they may help you learn something new or remind you of just the missing piece in your puzzle.
Last week I had to work through several scenarios of getting client/coworker-supplied data into our PostGIS database. It may be surprising in how many ways this thing can be a non-straightforward process. Formats, projections, geometry settings… talk about herding cats… I turned to QGIS for data prep (2.12.3 Lyon).
1. QGIS: permanently define layer CRS
If you bring a data source into QGIS without a defined Coordinate Reference System (CRS), you are immediately asked to select one. If you don’t do it right away, or select a wrong one, you can always re-open the selector via right-click > Set Layer CRS. Or, if you like longer route: right-click > Properties> General > Coordinate reference system > Select CRS (little globe-in-cone-hat icon on the right side, or ‘Specify…’ in earlier versions).
This is great, but only a temporary, ‘on-the-fly’ solution – the projection is not saved with the layer for the future. The most common next step I found advised (1, 2), and the one I always used before is: right-click > Save As… Actually, even if CRS hasn’t been defined temporary yet, it can be specified for the new file at the time of saving.
This is also great, but creates a whole new copy of the dataset! It is not always what we need. I tried to overwrite this same existing file at Save As step and QGIS disagreed by throwing error and crashing. Luckily, if your goal is to save the CRS with the layer, there is another way to do it! Disclaimer – only tested with vector data, rasters seem to be less fortunate. So, to set the layer’s CRS permanently go to: Vector > Data Management Tools > Define Current Projection.
There you can select your layer, choose predefined spatial reference system or import it from another existing layer, and voila – the .prj rejoins the rest of its shapefile sibling files! So happy for them =)
2. QGIS: merge multiple layers
Next treat I got was a dozen of one-polygon shapefiles that in an ideal world would be a single multi-feature shapefile. As I mentioned, I ultimately needed to import them to PostGIS, so would of course prefer to do it only once.
Since there were just a handful of layers, my first and simplest approach would be just to create a new shapefile, toggle editing session, then select and copy the polygon from each shapefile and paste it into the combo one. In fact that’s exactly what I did two weeks ago on a similar occasion. But this time I’m back from FOSS4G, enlightened to do better, remember?
So, of course there is a faster and easier alternative if all you need is to combine existing shapefiles in full. (3, 4) You don’t even need to load the layers to the map, but need to make sure they are all in the same projection and of the same geometry type. Go to Vector > Data Management Tools > Merge Shapefiles to One. There you can pick a whole directory of them, or select specific layers in the folder, then specify output shapefile – and it is all done in one step. Thanks QGIS!
3. QGIS: open .gdb
Another case of input data that I had to attend to was an ESRI file geodatabase. First thought I had was that I would have to use ArcMap for that. I tried…
Happens to me all the time… QGIS to the rescue! Turns out (5), it opens .gdb through a standard dialog ‘Add vector layer’: just need to pick Directory for Source type and OpenFileGDB for Source… emmm… Type =)
That ended up to be pretty easy!
Next, I used shp2pgsql to load my prepped shapefiles to the PostGIS database, where I continued to run into unexpected challenges – and will discuss that in the next post.
If you have suggestions or questions on anything mentioned – please chime in! I would also like to learn about your daily FOSS4G adventures.