Loading data from a spreadsheet
Spreadsheets are one of the most common methods used to collect and store simple geographic data. QGIS can work with text files called Comma Separated Value (CSV) files. Any spreadsheet can be converted to a CSV using the spreadsheet program. As long as the CSV data has a column representing x values, one column representing y values, and other columns representing data with the first row containing field names, QGIS can import it. Many organizations distribute geographic information as a CSV, so sooner or later, you will find yourself importing a CSV. Moreover, PyQGIS lets you do it programmatically. Note that a CSV can be delimited by any character as long as it is consistent. Also, the file extension of the CSV file doesn't matter as long as you specify the file type for QGIS.
Getting ready
We'll use a sample CSV file with point features representing points of interest in a region. You can download this sample from https://github.com/GeospatialPython/Learn/raw/master/tourism.csv.
Save this to your qgis_data/ms
directory in your root or home directory.
How to do it...
We will build a URI string to load the CSV as a vector layer. All of the parameters used to describe the structure of the CSV are included in the URI, as follows:
- First, we build the base URI string with the filename:
uri = "file:///qgis_data/ms/tourism.csv?"
- Next, we tell QGIS that the file is a CSV file:
uri += "type=csv&"
- Now, the most important part, we specify the
x
field:uri += "xField=X&"
- Then, we specify the
y
field:uri += "yField=Y&"
- We decline the spatial index option:
uri += "spatialIndex=no&"
- We decline the subset option:
uri += "subsetIndex=no&"
- We tell QGIS not to watch the file for changes:
uri += "watchFile=no&"
- Finally, we complete
uri
with the CRS of the layer:uri += "crs=epsg:4326"
- We load the layer using the
delimitedtext
data provider:layer=QgsVectorLayer(uri,"Tourism Sites","delimitedtext")
- Finally, we add it to the map:
QgsMapLayerRegistry.instance().addMapLayers([layer])
- Verify that your map looks similar to the map shown in the following screenshot, which also has an
OpenStreetMap
service added for reference:
How it works...
The URI is quite extensive but necessary to give QGIS enough information in order to properly load the layer. We used strings in this simple example, but using the QUrl
object is safer as it handles the encoding for you. The documentation for the QUrl
class is in the Qt documentation at http://qt-project.org/doc/qt-4.8/qurl.html.
Note that in the URI, we tell QGIS that the type is CSV, but when we load the layer, the type is delimitedtext
. QGIS will ignore empty fields as long as all of the columns are balanced.
There's more...
If you're having trouble loading a layer, you can use the QGIS Add Delimited Text Layer... dialog under the Layer menu to figure out the correct parameters. Once the layer is loaded, you can take a look at its metadata to see the URI QGIS constructed to load it. You can also get the correct parameters from a loaded delimited text layer using the layer.source()
method programmatically, and, of course, both of these methods work with any type of layer, not just delimited text. Unlike other layer types, however, you cannot edit delimited text layers in QGIS.