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 CSV or comma-separated values 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 let's 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://geospatialpython.googlecode.com/svn/MS_Features.txt.

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:

  1. First, we build the base URI string with the filename:
    uri="""file:///qgis_data/ms/MS_Features.txt?"""
    
  2. Next, we tell QGIS that the file is a CSV file:
    uri += """type=csv&"""
    
  3. Now, we specify our delimiter, which is a pipe ("|"), as a URL-encoded value:
    uri += """delimiter=%7C&"""
    
  4. Next, we tell QGIS to trim any spaces at the ends of the fields:
    uri += """trimFields=Yes&"""
    
  5. Now, the most important part, we specify the x field:
    uri += """xField=PRIM_LONG_DEC&"""
    
  6. Then, we specify the y field:
    uri += """yField=PRIM_LAT_DEC&"""
    
  7. We decline the spatial index option:
    uri += """spatialIndex=no&"""
    
  8. We decline the subset option:
    uri += """subsetIndex=no&"""
    
  9. We tell QGIS not to watch the file for changes:
    uri += """watchFile=no&"""
    
  10. Finally, we complete the uri with the CRS of the layer:
    uri += """crs=epsg:4326"""
    
  11. We load the layer using the delimitedtext data provider:
    layer=QgsVectorLayer(uri,"MS Features","delimitedtext")
    
  12. 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:

How to do it...

How it works...

The URI is quite extensive, but necessary to give QGIS enough information 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset