Supported Data Formats

QGIS uses the OGR library to read and write vector data formats including ESRI Shapefiles, MapInfo, Microstation file formats and many more. GRASS vector, PostGIS, MSSQL Spatial and Oracle Spatial support is supplied by native QGIS data providers. Vector data can also be loaded in read mode from zip and gzip archives into QGIS. At the date of this document, 69 vector formats are supported by the OGR library (see OGR-SOFTWARE-SUITE Literature and Web References). The complete list of supported vector data formats by the OGR library (see OGR-SOFTWARE-SUITE Literature and Web References) is available at http://www.gdal.org/ogr/ogr_formats.html.

주석

Not all of the listed formats may work in QGIS for various reasons. For example, some require external commercial libraries or the GDAL/OGR installation of your OS was not build to support the format you want to use. Only those formats that have been well tested will appear in the list of file types when loading a vector into QGIS. Other untested formats can be loaded by selecting *.*.

Working with GRASS vector data is described in Section GRASS GIS Integration.

This section describes how to work with several common formats: ESRI Shapefiles, PostGIS layers, SpatiaLite layers, OpenStreetMap vectors etc. Many of the features available in QGIS work the same, regardless of the vector data source. This is by design and includes the identify, select, labeling and attributes functions.

ESRI Shapefiles

The standard vector file format used in QGIS is the ESRI Shapefile. Support is provided by the OGR Simple Feature Library ( http://www.gdal.org/ogr/ ).

A shapefile actually consists of several files. The following three are required:

  1. .shp file containing the feature geometries.
  2. .dbf file containing the attributes in dBase format.
  3. .shx index file.

Shapefiles also can include a file with a .prj suffix, which contains the projection information. While it is very useful to have a projection file, it is not mandatory. A shapefile dataset can contain additional files. For further details see the ESRI technical specification at: http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf.

Loading a Shapefile

To load a shapefile, start QGIS and click on the mActionAddOgrLayer Add Vector Layer toolbar button or simply type Ctrl+Shift+V. This will bring up a new window (see figure_vector_1).

Figure Vector 1:

../../../_images/addvectorlayerdialog.png

Add Vector Layer Dialog nix

From the available options check radiobuttonon File. Click on button [Browse]. That will bring up a standard open file dialog (see figure_vector_2) which allows you to navigate the file system and load a shapefile or other supported data source. The selection box Filter selectstring allows you to preselect some OGR supported file formats.

You can also select the Encoding type for the shapefile if desired.

Figure Vector 2:

../../../_images/shapefileopendialog.png

Open an OGR Supported Vector Layer Dialog nix

Selecting a shapefile from the list and clicking [Open] loads it into QGIS. Figure_vector_3 shows QGIS after loading the alaska.shp file.

Figure Vector 3:

../../../_images/shapefileloaded.png

QGIS with Shapefile of Alaska loaded nix

참고

Layer Colors

When you add a layer to the map, it is assigned a random color. When adding more than one layer at a time, different colors are assigned to each layer.

Once loaded, you can zoom around the shapefile using the map navigation tools. To change the style of a layer, open the Layer Properties dialog by double clicking on the layer name or by right-clicking on the name in the legend and choosing Properties from the popup menu. See Section Style Menu for more information on setting symbology of vector layers.

참고

Load layer and project from mounted external drives on OS X

On OS X, portable drives that are mounted besides the primary hard drive do not show up under File ‣ Open Project as expected. We are working on a more OSX-native open/save dialog to fix this. As a workaround you can type ‘/Volumes’ in the File name box and press return. Then you can navigate to external drives and network mounts.

Improving Performance for Shapefiles

To improve the performance of drawing a shapefile, you can create a spatial index. A spatial index will improve the speed of both zooming and panning. Spatial indexes used by QGIS have a .qix extension.

Use these steps to create the index:

  • Load a shapefile, clicking on the mActionAddOgrLayer Add Vector Layer toolbar button or type Ctrl+Shift+V.
  • Open the Layer Properties dialog by double-clicking on the shapefile name in the legend or by right-clicking and choosing Properties from the popup menu.
  • In the tab General click the [Create Spatial Index] button.

Problem loading a shape .prj file

If you load a shapefile with .prj file and QGIS is not able to read the coordinate reference system from that file, you have to define the proper projection manually within the General tab of the Layer Properties dialog of the layer by clicking the [Specify...] button. This is due to the fact, that .prj files often do not provide the complete projection parameters, as used in QGIS and listed in the CRS dialog.

For that reason, if you create a new shapefile with QGIS, two different projection files are created. A .prj file with limited projection parameters, compatible with ESRI software, and a .qpj file, providing the complete parameters of the used CRS. Whenever QGIS finds a .qpj file, it will be used instead of the .prj.

Loading a MapInfo Layer

mActionAddOgrLayer To load a MapInfo layer, click on the mActionAddOgrLayer Add Vector Layer toolbar button or type Ctrl+Shift+V, change the file type filter Filter selectstring: to ‘Mapinfo File [OGR]’ and select the MapInfo layer you want to load.

Loading an ArcInfo Binary Coverage

mActionAddOgrLayer To load an ArcInfo binary coverage, click on the mActionAddOgrLayer Add Vector Layer toolbar button or press Ctrl+Shift+V to open the Add Vector Layer dialog. Select radiobuttonon Directory. Change to Filter selectstring to ‘Arc/Info Binary Coverage’. Navigate to the directory that contains the coverage files and select it.

Similarly, you can load directory based vector files in the UK National Transfer Format as well as the raw TIGER Format of the US Census Bureau.

Loading OpenStreetMap Vectors

QGIS integrates Openstreetmap import as a core functionnality.

  • To connect to the OSM server and download data, open the menu Vector ‣ Openstreetmap ‣ Load data. You can skip this step if you already got a .osm XML file using josm or overpass or any other source.
  • The menu Vector ‣ Openstreetmap ‣ Import topology from an XML file will convert your .osm file into a spatialite database, and create a db connection.
  • The menu Vector ‣ Openstreetmap ‣ Export topology to Spatialite then allows you to open the database connection, select the type of data you want (points, lines, or polygons) and choose tags to import. This creates a spatialite geometry layer that you can then add to your project by clicking on the mActionAddSpatiaLiteLayer Add SpatiaLite Layer toolbar button or by selecting the mActionAddSpatiaLiteLayer Add SpatiaLite Layer... option from the Layer menu, see Section SpatiaLite Layers.

PostGIS Layers

PostGIS layers are stored in a PostgreSQL database. The advantages of PostGIS are the spatial indexing, filtering and query capabilities it provides. Using PostGIS, vector functions such as select and identify work more accurately than with OGR layers in QGIS.

Creating a stored Connection

mActionAddPostgisLayer The first time you use a PostGIS data source, you must create a connection to the PostgreSQL database that contains the data. Begin by clicking on the mActionAddPostgisLayer Add PostGIS Layer toolbar button, selecting the mActionAddPostgisLayer Add PostGIS Layer... option from the Layer menu or typing Ctrl+Shift+D. You can also open the Add Vector Layer dialog and select radiobuttonon Database. The Add PostGIS Table(s) dialog will be displayed. To access the connection manager, click on the [New] button to display the Create a New PostGIS Connection dialog. The parameters required for a connection are:

  • Name: A name for this connection. Can be the same as Database
  • Service: Service parameter to be used alternatively to hostname/port (and potentially database). This can be defined in pg_service.conf
  • Host: Name of the database host. This must be a resolvable host name the same as would be used to open a telnet connection or ping the host. If the database is on the same computer as QGIS, simply enter ‘localhost’ here.
  • Port: Port number the PostgreSQL database server listens on. The default port is 5432.
  • Database: Name of the database.
  • SSL mode: How the SSL connection will be negotiated with the server. Note that massive speedups in PostGIS layer rendering can be achieved by disabling SSL in the connection editor. Following options are available:
    • disable: only try an unencrypted SSL connection
    • allow: try a non-SSL connection, if that fails, try an SSL connection
    • prefer (the default): try an SSL connection, if that fails, try a non-SSL connection;
    • require: only try an SSL connection.
  • Username: User name used to login to the database.
  • Password: Password used with Username to connect to the database.

Optional you can activate following checkboxes:

  • checkbox Save Username
  • checkbox Save Password
  • checkbox Only look in the geometry_columns table
  • checkbox Don’t resolve type of unrestricted columns (GEOMETRY)
  • checkbox Only look in the ‘public’ schema
  • checkbox Also list tables with no geometry
  • checkbox Use estimated table metadata

Once all parameters and options are set, you can test the connection by clicking on the [Test Connect] button.

참고

QGIS User Settings and Security

Depending on your computing environment, storing passwords in your QGIS settings may be a security risk. Your customized settings for QGIS are stored based on the operating system:

  • nix, the settings are stored in your home directory in .qgis2/.
  • win, the settings are stored in the registry.

Loading a PostGIS Layer

mActionAddPostgisLayer Once you have one or more connections defined, you can load layers from the PostgreSQL database. Of course this requires having data in PostgreSQL. See Section Importing Data into PostgreSQL for a discussion on importing data into the database.

To load a layer from PostGIS, perform the following steps:

  • If the Add PostGIS layers dialog is not already open, selecting the mActionAddPostgisLayer Add PostGIS Layer... option from the Layer menu or typing Ctrl+Shift+D opens the dialog.
  • Choose the connection from the drop-down list and click [Connect].
  • Select or unselect checkbox Also list tables with no geometry
  • Optionally use some checkbox Search Options to define which features to load from the layer or use the [Build query] button to start the Query builder dialog.
  • Find the layer(s) you wish to add in the list of available layers.
  • Select it by clicking on it. You can select multiple layers by holding down the Shift key while clicking. See Section Query Builder for information on using the PostgreSQL Query Builder to further define the layer.
  • Click on the [Add] button to add the layer to the map.

참고

PostGIS Layers

Normally a PostGIS layer is defined by an entry in the geometry_columns table. From version 0.9.0 on, QGIS can load layers that do not have an entry in the geometry_columns table. This includes both tables and views. Defining a spatial view provides a powerful means to visualize your data. Refer to your PostgreSQL manual for information on creating views.

Some details about PostgreSQL layers

This section contains some details on how QGIS accesses PostgreSQL layers. Most of the time QGIS should simply provide you with a list of database tables that can be loaded, and load them on request. However, if you have trouble loading a PostgreSQL table into QGIS, the information below may help you understand any QGIS messages and give you direction on changing the PostgreSQL table or view definition to allow QGIS to load it.

QGIS requires that PostgreSQL layers contain a column that can be used as a unique key for the layer. For tables this usually means that the table needs a primary key, or a column with a unique constraint on it. In QGIS, this column needs to be of type int4 (an integer of size 4 bytes). Alternatively the ctid column can be used as primary key. If a table lacks these items, the oid column will be used instead. Performance will be improved if the column is indexed (note that primary keys are automatically indexed in PostgreSQL).

If the PostgreSQL layer is a view, the same requirement exists, but views do not have primary keys or columns with unique constraints on them. You have to define a primary key field (has to be integer) in the QGIS dialog before you can load the view. If a suitable column cannot does not exist in the view, QGIS will not load the layer. If this occurs, the solution is to alter the view so that it does include a suitable column (a type of integer and either a primary key or with a unique constraint, preferably indexed).

QGIS offers a checkbox Select at id that is activated by default. This option gets the ids without the attributes which is faster in most cases. It can make sense to disable this option when you use expensive views.

Importing Data into PostgreSQL

Data can be imported into PostgreSQL/PostGIS using several tools, such as the SPIT plugin or the command line tools shp2pgsql or ogr2ogr.

DB Manager

QGIS comes with a core plugin named icon_dbmanager DB Manager. It can be used to load shapefiles and other data formats and includes support for schemas. See Section DB Manager Plugin for more information.

shp2pgsql

PostGIS includes an utility called shp2pgsql that can be used to import shapefiles into a PostGIS enabled database. For example, to import a shapefile named lakes.shp into a PostgreSQL database named gis_data, use the following command:

shp2pgsql -s 2964 lakes.shp lakes_new | psql gis_data

This creates a new layer named lakes_new in the gis_data database. The new layer will have a spatial reference identifier (SRID) of 2964. See Section 투영법 다루기 for more information on spatial reference systems and projections.

참고

Exporting datasets from PostGIS

Like the import-tool shp2pgsql there is also a tool to export PostGIS-datasets as shapefiles: pgsql2shp. This is shipped within your PostGIS distribution.

ogr2ogr

Beside shp2pgsql and DB Manager there is another tool for feeding geodata in PostGIS: ogr2ogr. This is part of your GDAL installation.

To import a shapefile into PostGIS, do the following:

ogr2ogr -f "PostgreSQL" PG:"dbname=postgis host=myhost.de user=postgres \
password=topsecret" alaska.shp

This will import the shapefile alaska.shp into the PostGIS-database postgis using the user postgres with the password topsecret on host server myhost.de.

Note that OGR must be built with PostgreSQL to support PostGIS. You can see this by typing

ogrinfo --formats | grep -i post

If you like to use PostgreSQL’s COPY -command instead of the default INSERT INTO method you can export the following environment-variable (at least available on nix and osx):

export PG_USE_COPY=YES

ogr2ogr does not create spatial indexes like shp2pgsl does. You need to create them manually using the normal SQL-command CREATE INDEX afterwards as an extra step (as described in the next section Improving Performance).

Improving Performance

Retrieving features from a PostgreSQL database can be time consuming, especially over a network. You can improve the drawing performance of PostgreSQL layers by ensuring that a PostGIS spatial index exists on each layer in the database. PostGIS supports creation of a GiST (Generalized Search Tree) index to speed up spatial searches of the data (GiST index information is taken from the PostGIS documentation available at http://postgis.refractions.net).

The syntax for creating a GiST index is:

CREATE INDEX [indexname] ON [tablename]
  USING GIST ( [geometryfield] GIST_GEOMETRY_OPS );

Note that for large tables, creating the index can take a long time. Once the index is created, you should perform a VACUUM ANALYZE. See the PostGIS documentation (POSTGIS-PROJECT Literature and Web References) for more information.

The following is an example of creating a GiST index:

gsherman@madison:~/current$ psql gis_data
Welcome to psql 8.3.0, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

gis_data=# CREATE INDEX sidx_alaska_lakes ON alaska_lakes
gis_data-# USING GIST (the_geom GIST_GEOMETRY_OPS);
CREATE INDEX
gis_data=# VACUUM ANALYZE alaska_lakes;
VACUUM
gis_data=# \q
gsherman@madison:~/current$

Vector layers crossing 180° longitude

Many GIS packages don’t wrap vector maps, with a geographic reference system (lat/lon), crossing the 180 degrees longitude line (http://postgis.refractions.net/documentation/manual-1.4/ST_Shift_Longitude.html). As result, if we open such map in QGIS, we will see two far, distinct locations, that should show near each other. In Figure_vector_4 the tiny point on the far left of the map canvas (Chatham Islands), should be within the grid, right of New Zealand main islands.

Figure Vector 4:

../../../_images/vectorNotWrapping.png

Map in lat/lon crossing the 180° longitude line nix

A workaround is to transform the longitude values using PostGIS and the ST_Shift_Longitude function This function reads every point/vertex in every component of every feature in a geometry, and if the longitude coordinate is < 0° adds 360° to it. The result would be a 0° - 360° version of the data to be plotted in a 180° centric map.

Figure Vector 5:

../../../_images/vectorWrapping.png

Crossing 180° longitude applying the ST_Shift_Longitude function

Usage

  • Import data to PostGIS (Importing Data into PostgreSQL) using for example the DB Manager plugin.

  • Use the PostGIS command line interface to issue the following command (this is an example where “TABLE” is the actual name of your PostGIS table)

    gis_data=# update TABLE set the_geom=ST_Shift_Longitude(the_geom);

  • If everything went right you should receive a confirmation about the number of features that were updated, then you’ll be able to load the map and see the difference (Figure_vector_5)

SpatiaLite Layers

mActionAddSpatiaLiteLayer The first time you load data from a SpatiaLite database, begin by clicking on the mActionAddSpatiaLiteLayer Add SpatiaLite Layer toolbar button or by selecting the mActionAddSpatiaLiteLayer Add SpatiaLite Layer... option from the Layer menu or by typing Ctrl+Shift+L. This will bring up a window, which will allow you to either connect to a SpatiaLite database already known to QGIS, which you can choose from the dropdown menu or to define a new connection to a new database. To define a new connection, click on [New] and use the file browser to point to your SpatiaLite database, which is a file with a .sqlite extension.

If you want to save a vector layer to SpatiaLite format you can do this by right clicking the layer in the legend. Then click on Save as.., define the name of the output file, select ‘SpatiaLite’ as format and the CRS. Also you can select ‘SQLite’ as format, and then add SPATIALITE=YES in the OGR data source creation option field. This tells OGR to create a SpatiaLite database. See also http://www.gdal.org/ogr/drv_sqlite.html.

QGIS also supports editable views in SpatiaLite.

Creating a new SpatiaLite layer

If you want to create a new SpatiaLite layer, please refer to section Creating a new SpatiaLite layer.

참고

SpatiaLite data management Plugins

For SpatiaLite data management you can also use several Python plugins: QSpatiaLite, SpatiaLite Manager or DB Manager (core plugin, recommended). They can be downloaded and installed with the Plugin Installer.

MSSQL Spatial Layers

mActionAddMssqlLayer QGIS also provides native MS SQL 2008 support. The mActionAddMssqlLayer Add MSSQL Spatial Layer is part of the new toolbar button or available in the MS SQL node in the QBrowser tree, providing drag and drop import support.

ORACLE Spatial Layers

mActionAddOracleLayer QGIS also provides native ORACLE Locator/Spatial support. The mActionAddOracleLayer Add ORACLE Spatial Layer is part of the new toolbar button or available in the ORACLE node in the QBrowser tree, providing drag and drop import support. ORACLE Spatial layers are stored in an ORACLE database.

Creating a stored Connection

mActionAddOracleLayer The first time you use a ORACLE Spatial data source, you must create a connection to the database that contains the data. Begin by clicking on the mActionAddOracleLayer Add ORACLE Spatial Layer toolbar button, selecting the mActionAddOracleLayer Add ORACLE Spatial Layer... option from the Layer menu or typing Ctrl+Shift+O. To access the connection manager, click on the [New] button to display the Create a New ORACLE Spatial Connection dialog. The parameters required for a connection are:

  • Name: A name for this connection. Can be the same as Database
  • Database SID or SERVICE_NAME of the Oracle instance.
  • Host: Name of the database host. This must be a resolvable host name the same as would be used to open a telnet connection or ping the host. If the database is on the same computer as QGIS, simply enter ‘localhost’ here.
  • Port: Port number the PostgreSQL database server listens on. The default port is 1521.
  • Username: User name used to login to the database.
  • Password: Password used with Username to connect to the database.

Optional you can activate following checkboxes:

  • checkbox Save Username Indicates whether to save the database user name in the connection configuration.
  • checkbox Save Password Indicates whether to save the database password in the connection settings. Passwords are saved in clear text in the system configuration and in the project files!
  • checkbox Only look in meta data table Restricts the displayed tables to those that are in the all_sdo_geom_metadata view. This can speed up the initial display of spatial tables.
  • checkbox Only look for user’s tables When searching for spatial tables restrict the search to tables that are owner by the user.
  • checkbox Also list tables with no geometry Indicates that tables without geometry should also be listed by default.
  • checkbox Use estimated table statistics for the layer metadata When the layer is setup various metadata is required for the Oracle table. This includes information such as the table row count, geometry type and spatial extents of the data in the geometry column. If the table contains a large number of rows determining this metadata is time consuming. By activating this option the following fast table metadata operations are done: Row count is determined from all_tables.num_rows. Table extents are always determined with the SDO_TUNE.EXTENTS_OF function even if a layer filter is applied. The table geometry is determined from the first 100 non-null geometry rows in the table.
  • checkbox Only existing geometry types Only list the existing geometry types and don’t offer to add others.

Once all parameters and options are set, you can test the connection by clicking on the [Test Connect] button.

참고

QGIS User Settings and Security

Depending on your computing environment, storing passwords in your QGIS settings may be a security risk. Passwords are saved in clear text in the system configuration and in the project files! Your customized settings for QGIS are stored based on the operating system:

  • nix, the settings are stored in your home directory in .config/QGIS/QGIS2.conf.
  • win, the settings are stored in the registry.

Loading a ORACLE Spatial Layer

mActionAddOracleLayer Once you have one or more connections defined, you can load layers from the ORACLE database. Of course this requires having data in ORACLE.

To load a layer from ORACLE Spatial, perform the following steps:

  • If the Add ORACLE Spatial layers dialog is not already open, click on the mActionAddOracleLayer Add ORACLE Spatial Layer toolbar button.
  • Choose the connection from the drop-down list and click [Connect].
  • Select or unselect checkbox Also list tables with no geometry
  • Optionally use some checkbox Search Options to define which features to load from the layer or use the [Build query] button to start the Query builder dialog.
  • Find the layer(s) you wish to add in the list of available layers.
  • Select it by clicking on it. You can select multiple layers by holding down the Shift key while clicking. See Section Query Builder for information on using the ORACLE Query Builder to further define the layer.
  • Click on the [Add] button to add the layer to the map.

참고

ORACLE Spatial Layers

Normally an ORACLE Spatial layer is defined by an entry in the USER_SDO_METADATA table.