- QGIS Python Programming Cookbook
- Joel Lawhead
- 563字
- 2021-07-23 19:48:55
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:
- First, we build the base
URI
string with the filename:uri="""file:///qgis_data/ms/MS_Features.txt?"""
- Next, we tell QGIS that the file is a CSV file:
uri += """type=csv&"""
- Now, we specify our delimiter, which is a pipe ("|"), as a URL-encoded value:
uri += """delimiter=%7C&"""
- Next, we tell QGIS to trim any spaces at the ends of the fields:
uri += """trimFields=Yes&"""
- Now, the most important part, we specify the x field:
uri += """xField=PRIM_LONG_DEC&"""
- Then, we specify the y field:
uri += """yField=PRIM_LAT_DEC&"""
- 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 the
uri
with the CRS of the layer:uri += """crs=epsg:4326"""
- We load the layer using the
delimitedtext
data provider:layer=QgsVectorLayer(uri,"MS Features","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:

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.
- 數據庫系統原理及MySQL應用教程(第2版)
- Mastering Android Development with Kotlin
- Azure Serverless Computing Cookbook
- Building Serverless Web Applications
- .NET Standard 2.0 Cookbook
- 寫給大家看的Midjourney設計書
- Mudbox 2013 Cookbook
- SFML Game Development
- AngularJS by Example
- Java性能權威指南
- 機器學習開發者指南
- Microsoft Hyper-V PowerShell Automation
- jQuery Mobile從入門到精通
- Spring Boot實戰:從0開始動手搭建企業級項目
- C語言非常道