...
Numberedheadings | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||
Loading and Using PostGreSQL DataData can be loaded into Postgres in a number of ways - this module covers: The GDAL ogr2ogr tool from the command line
QGISIf you have QGIS installed, it provides
QGISIf you have QGIS installed, it provides a GUI-based means of loading data to a Posgres database. There are a few ways of getting data into Postgres using QGIS. Load data with DB ManagerThis approach gives the the greatest degree of control over the conversion from your source format to Postgres.
Load data using the BrowserThe QGIS Browser panel also contains functionality to load and manage data in the database.
Import CSV to PostgresThe Copy command can be used to import Comma Separated Value files (CSV) / text files to and from PostgreSQL.
ogr2ogrThe GDAL library contains an ogr2ogr executable which will convert between most spatial formats, including PosgresQL/PostGIS, Shapefile, MapInfo and GeoPackage. It can be run from the command line with appropriate parameters - see the GDAL site for full details. It’s also possible to use ogr2ogr from QGIS, so in this exercise we will use QGIS to create a command line, then run it in the command prompt. Load data with ogr2ogr
|
Numberedheadings | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||
GDAL/OGR og2ogrA versatile command line took which will translate many different formats, including shapefile, .TAB and GML. There are some GUI interfaces that support ogr2ogr (e.g. http://www.ogr2gui.ca), and QGIS uses it as its translation engine. A full list of supported formats is at http://www.gdal.org/ogr_formats.html. Desktop GISIf you are already using desktop GIS with support for PostGIS, it's usually easiest to use this, though bear in mind that some GIS products are more successful at doing this than others.
CopyThe Copy command is a command-line operated tool to import/export non-spatial data between PostgreSQL and CSV or other text formats.
Connecting to PostGIS from QGIS
The table will be added as a layer in the map. Spatial TablesThe specific characteristics of spatial tables are important. These include:
Geometry fieldsPostGIS expands PostgreSQL by introducing the datatype GEOMETRY, which holds spatial information in the OpenGIS standard: WKB (Well Known Binary) format. WKB is not easily interpreted by eye, so it can be converted to WKT (Well Known Text) to display the geometry in textual form, using the functions ST_AsText() or ST_AsEWKT(). WKB GEOMETRYWKT GEOMETRYPostGIS also supports 3D geometry using EWKB and EWKT. The name of the geometry field can vary, and different methods of loading data use different names - e.g. the_geom, geometry, wkb_geometry. It's recommended to use the same name for all tables, and the datatype must always be geometry. Type ModifierTypemod is a type modifier, used to modify data types - for example on variable length character columns, they specify the number of character available on the data type. description VARCHAR(255) For geometry columns, PostGIS uses type modifiers to define:
These are written as follows: Geometry_name GEOMETRY([datatype][(Z)], [SRID] Example 2D: wkb_geometry GEOMETRY(Point, 27700) Example 3D: wkb_geometry GEOMETRY(PointZ, 27700) These modifiers specify:
Only geometry that meets these criteria can be inserted or updated into the table. Type modifiersType modifiers replace constraints in PostGIS 1.5. Unique IdentifierEvery table should have a unique identifier field, with unique non-null values. It will normally be introduced by the importing tool as a serial or integer datatype, and be set as the primary key. However any unique value can also be set as a primary key. If a primary key does not exist, you will not be able edit data in pgAdmin, and there may be problems viewing and editing in GIS applications. Geometry TypesPostGIS geometry data types reflect OpenGIS standard definitions for spatial information. The three simplest types of geometry are points, lines and polygons. The diagram below shows how these are represented in WKT format: For example: POINT(2 3) LINESTRING(1 2, 3 3, 3 1) POLYGON((1 2, 3 3, 3 1, 1 2)) POLYGON((571178 6337246,571178 6402217,598061 6402217,598061 6337246,571178 6337246)) Collection GeometriesCollection geometries are groups of geometries that locally belong together. There is a collection type for each geometry type. GeometryCollections enable different geometry types to be stored together as one logical grouping.
Spatial Reference SystemsPostGIS follows EPSG (European Petroleum Survey Group) numberings for spatial reference system identification. Commonly used SRIDs (Spatial Reference IDs) used in the UK are:
Geometry ColumnsPostGIS uses the geometry_columns view in the public schema to list metadata, based on and associated with the geometry field in the spatial tables. This view is automatically created when the PostGIS extension is added to the database. When a spatial table is created using a standard process (for example one of the ones listed above), it will automatically appear in the geometry_columns view. The geometry_columns view is used in clients such as desktop GIS to generate a list of spatial tables without having to examine all tables for geometry types etc, so it acts as a catalogue of spatial data. Some clients will not open a spatial table if it does not appear in the view. geometry_columns fields
Geography DatatypeThe geography datatype provides native support for spatial features represented on geographic coordinates - i.e. spherical coordinates expressed in angular units (degrees). The basis for the geometry type is a plane. On the plane the shortest path between two objects is a straight line. This means calculations on geometries can be calculated using cartesian maths and straight line vectors. The geography type is based on a sphere. In the sphere the shortest path between two objects is an arc. This means that calculations on geographies (e.g. areas, distances, lengths, intersections) must be calculated on the sphere, using more complex maths. For accurate measurements, the calculations must take the actual shape of the earth into account, and as this is more complex, there are few functions defined for the geography type, and only WGS 84 (SRID:4326) is supported. While it's useful to be aware of the geography type, this course will focus on geometry types. The table below compares the two.
IndexesA database index is a data structure that improves the speed of data retrieval operations on a database table, at the cost of slower writes and increased storage space. Any field in a database can be indexed and more than one field can be involved in a single index. While indexes improve efficiency in search, they slow down update operations. Normally there should be a spatial index on the geometry field to improve the efficiency of rendering, analysis and searching of spatial data. Most import tools provide an option to create an index when the data is loaded, and it is good practice to do so. The most common type of spatial index is a GIST index, but other indexes include B-Tree type. Loader for MasterMapAstun's Loader is a set of Python scripts which make use of OGR 1.9 to transform GML or KML data to other formats. It is principally use to load Ordnance Survey MasterMap data to PostGreSQL. The source data is pre-processed using Python to make it suitable for loading with OGR, or to add value by deriving attributes. Although Loader was originally written to load Ordnance Survey OS MasterMap Topographic Layer data in GML/GZ format, it has been used to load other GML and KML data. Loader is an open source tool, at https://github.com/AstunTechnology/Loader. |
...