Using PostGIS with QGIS

去年的一篇文章,作者看到大多数人还在使用shapefiles来存储GIS信息,觉得不够科学,为什么不将数据导入数据库,利用数据库的特性呢?

本文将介绍如何将地图数据导入PostgreSQL(假设已安装PostGIS插件),以及使用QGIS对地图数据进行编辑和查询。

首先要安装PostgreSQL以及PostGIS,

然后去找个地方下载地图数据,文中介绍OZCAM可以下载到澳大利亚西部的地图数据。

安装QGIS,打开QGIS界面,创建到PostgreSQL的连接,将地图数据导入,导入过程中要选择正确的源和目标SRID。

数据导入后,就可以尽情的玩耍了。

[参考]

1. http://plugins.qgis.org/plugins/tags/postgis/

2. http://postgis.net/

[原文]
1. http://www.gaiaresources.com.au/using-postgis-qgis/

After attending the FOSS4G conference (see my last blog post) a couple of weeks ago, I was inspired to take a fresh look at the technologies that were on display there.

We have been involved (via rollouts, training, and customisation) in the increase in the use of QGIS in the past year or so.  Interestingly, the majority of these users are still using shapefiles to store data rather than in a database. There are a number of advantages to using a database – and  there is an existing open source spatial database that works really well with QGIS – PostGIS, the spatial extension for the PostgreSQL open source database.

I thought it would be interesting to use the simple example of creating a dynamic convex hull around a set of points to show how powerful using a spatial database can be.

The first step is to install QGIS and PostGIS.  If you haven’t done that already, this is easy to do if you are on Windows using the installers here and here. Both of these open source software packages have great documentation if you get stuck, but a couple of tips are:

  • make sure to make a note of all your settings when doing the PostGIS install, and
  • make sure you run the StackBuilder to install PostGIS after doing the main PostgreSQL install.

The next step is to get some data, I used the OZCAM species occurrence records for Western Australia, but any point records would do the same job for this simple example.  This data represents the publicly available points from within the Museum community within Australia, so it’s essentially vouchered specimen data.

 

These raw species records were downloaded as a shapefile, so we now need to get them into PostGIS. There are a bunch of ways to do this but seeing we are using QGIS to view the data let’s also use it to load the data.

The first thing to do is make a connection to your PostGIS database using the settings from the installation (Layers > Add PostGIS Layers > New).

 

Once you have a database connection we can load to shapefile into the database using DB Manager (Database > DB Manager > DB Manager).

 

Projections in PostGIS and QGIS are defined by code that is somewhat interchangeably called a SRID, EPSG or CRS. To figure out what the code is for your data you can open it in QGIS, right-click on the layer in the table of contents,  choose Properties and the code will be found on the General tab, under the Coordinate Reference System component in the middle of the window.

 

Now that our data are loaded into the database we are also able to connect, view and edit it from QGIS (Layers > Add PostGIS Layers). More importantly, we can start to use some of the other features that make relational databases so powerful.

The first thing I am going to do is filter our existing data to a single species, again there are a few different ways to do this but I am going to keep it simple and use a filter in QGIS. Right click on the layer and select filter and then use the query builder to create a filter, I used scientific name = Tadarida australis. This species is an endemic bat that is found fairly commonly throughout most of Australia (and it’s one we’ve been recording in our bat survey of the Perth region, which is on hiatus while we deal with several years worth of data, as we alluded to in a blog post at the start of the year).

 

Using PostGIS, we can now create really simple approximation of the range of the species using a convex hull. To do this I used pgAdminIII which is a management tool for the PostgreSQL database that underlies PostGIS. pgAdminIII usually comes preinstalled with PostGIS – if you are using Windows you should be able to find it in the PostgreSQL folder under the start menu. When you start up the tool you will need to connect to your database in much the same way as in QGIS, once you have done that you can run SQL commands directly on the database.

I created a view on my species points using the SQL below. A view is essentially a dynamic table created from other data using a SQL query, similar to a definition query in ArcGIS or a filter in QGIS but much more powerful.

create or replace view species_range as select ’1′::integer as “id”, st_convexhull(st_collect(geom)) as geom from ozcam where “scientific name” = ‘Tadarida australis’;

QGIS can display a view in the same way as a table and if we do that the result looks something like this:

 

Now for the fun stuff – remember that the species range polygon is created dynamically, so every time we refresh the layer the SQL query underlying the view gets rerun and the data is recreated.  If we were to delete a point from the species records table and refresh QGIS you can see that the convex hull gets dynamically regenerated…

 

This is extremely powerful if you have a dataset that is frequently updated or if we were applying a more complex analysis to the underlying layer. PostgreSQL also allows for the creation of custom functions, so we could even use a more accurate species distribution algorithm if we wanted to.

Using a spatial database comes with some great benefits and is easy to get started, so why not give it a go on your next project. As always we are here to help, so drop me a comment below, or an email.  You can even contact us through FacebookTwitter or LinkedIn.

Andrew

时间: 2024-10-31 02:16:29

Using PostGIS with QGIS的相关文章

新零售空间数据库实践一例 - PostGIS 点面叠加视觉判断输出

标签 PostgreSQL , 点面视觉输出 , subquery , nestloop join , 空间索引 , gist 背景 在新零售.快递等行业,有大量的点数据(例如包裹位置.快递员位置.仓库位置等),同时有大量的面数据(如小区,商圈,写字楼等). 如何判断实时的正在配送的包裹落在哪个面呢?并且将之联系起来. 这个从视觉角度来思考,非常简单. 例如有一个地图,将其划分为若干个面(例如前面提到的小区). 然后有一些小点,这些是POINT数据. 我们从图上一眼就能看出每个点落在哪个小区(面

PostGIS 2.2.0dev(最新版)手册中文完整版

Hi,all     最近我翻译了PostGIS 2.2dev版本的开发手册    下载地址:http://pan.baidu.com/s/1eS8wyGE    有兴趣的可以下载看看.英文水平比较好的可以直接看英文版.     PostgreSQL和PostGIS都是开源的,所以这份文档也是"开源"的.但我不希望你拿它做商业用途.愿我们一起将PostgreSQL和PostGIS发展的更好!!    涉及到PostGIS的中文书籍目前没有找到,所以本次翻译希望能成为良好的开端,愿大家贡

mysql空间扩展 VS PostGIS

功能 Mysql spatial extension  PostGIS 空间索引 仅MyISAM支持R树索引,InnoDB不支持  GIST树索引(R树的变种) 支持的空间类型 仅二维数据 二维.三维以及曲线 空间操作函数 有限的空间函数 基本实现OGC标准定义的空间操作函数 例:想查找蓝色多边形内的点,mysql空间扩展仅能查出在最小外包矩形(红色框)内的点,而postgis能查出任意多边形内的点. 空间投影 不支持 支持多种常用投影坐标系 例:想查找两点间距离.MySQL Spatial仅能

PostgreSQL 10 + PostGIS + Sharding(pg_pathman) + MySQL(fdw外部表) on ECS 部署指南(适合新用户)

标签 PostgreSQL , Linux , CentOS 背景 新用户部署PostgreSQL以及空间数据库插件PostGIS的指南. 内网环境RPM打包(可选项) 如果是内网环境,可以将包全部下载到本地再用rpm安装. 安装epel和postgresql yum rpm两个包后再执行: 1.使用yum-utils的yumdownloader下载需要的安装包,包括依赖包. yum install -y yum-utils yumdownloader --resolve --destdir=/

postgresql-通过PostGIS导入postgreSQL的shapefile文件到哪里查看啊。。。新手,不懂

问题描述 通过PostGIS导入postgreSQL的shapefile文件到哪里查看啊...新手,不懂 解决方案 这里有视频: Postgresql : Create Postgis database and import shapefileswww.youtube.com/watch?v=UgZGvyy9zhk

无人驾驶背后的技术 - PostGIS点云(pointcloud)应用

标签 PostgreSQL , PostGIS , box , grid , pointcloud , pgpointcloud , point聚合 , KNN , 自动驾驶 , 自动配送 , 无人驾驶 , 机器人配送 , 物流 背景 科幻电影的场景随着技术的发展,正在一步步的从荧幕变成现实.从军用到民用,比如汽车厂商.科技公司在尝试的无人驾驶,无人飞行器. 无人驾驶应用非常广泛,比如快递行业,时机成熟以后,将来可能快递员这个职业也会逐渐从社会上消失(解放快递员的双手和创造力,让更多的人参与到科

视觉挖掘与PostGIS空间数据库的邂逅

标签 PostgreSQL , PostGIS , 视觉匹配 , 空间相交 , 圈人 背景 推荐系统是广告营销平台的奶牛,其核心是精准.实时.高效. 这么多广告平台,到底谁家强?谁的核心牛逼? 1. 精准,指对用户的描述精准,通常需要基于大量的用户行为数据,经历深度学习后形成的用户画像,或称之为标签系统. 标签的准确性关系到推荐的精准度,比如你可能不会对一个正常的年轻人推荐老花眼镜(当然如果有其他购买意向的标签来指出他有购买老花眼镜的欲望除外). 2. 实时,指标签的更新实时性,很多标签是具有非

PgSQL · 功能分析 · PostGIS 在 O2O应用中的优势

最近上线了一个O2O相关的应用,用到了PostgreSQL和非常著名的插件PostGIS,该应用把PostgreSQL和PostGIS的优势在O2O领域成功的发挥了出来.O2O业务分为线上和线下两部分,线下部分业务和位置距离等密切相关,不可避免的需要保存商户位置信息,进行距离的计算,范围覆盖计算等,这部分业务简称LBS(Location Based Service),即基于地理位置信息服务.使用PostgreSQL和PostGIS实现这些业务,具有天然的优势. 空间数据库扩展 由于空间数据具有空

无人驾驶背后的技术 - PostGIS点云(pointcloud)应用 - 2

标签 PostgreSQL , PostGIS , box , grid , pointcloud , pgpointcloud , point聚合 , KNN , 自动驾驶 , 自动配送 , 无人驾驶 , 机器人配送 , 物流 , 无用功 背景 无人驾驶.配送机器人的业务背景,方案设计请参考: <无人驾驶背后的技术 - PostGIS点云(pointcloud)应用> 本文针对以上文章,补充一些新鲜内容. 一.transfer table消除索引build.格式检查等无用功 在服务端存储了所