官术网_书友最值得收藏!

Using geospatial views

Views in PostgreSQL allow for ad hoc representation of data and data relationships in alternate forms. In this recipe, we'll be using views to allow for the automatic creation of point data based on tabular inputs. We can imagine a case where the input stream of data is non-spatial, but includes longitude and latitude or some other coordinates. We would like to automatically show this data as points in space.

Getting ready

We can create a view as a representation of spatial data pretty easily. The syntax for creating a view is similar to creating a table; for example:

CREATE VIEW viewname AS
 SELECT...

In the preceding command line, our SELECT query manipulates the data for us. Let's start with a small dataset. In this case, we will start with some random points.

First, we create the table from which the view will be constructed as follows:

–- Drop the table in case it exists
DROP TABLE IF EXISTS chp02.xwhyzed CASCADE; 
CREATE TABLE chp02.xwhyzed
-- This table will contain numeric x, y, and z values
(
 x numeric,
 y numeric,
 z numeric
)
WITH (OIDS=FALSE);
ALTER TABLE chp02.xwhyzed OWNER TO postgres;
-- We will be disciplined and ensure we have a primary key
ALTER TABLE chp02.xwhyzed ADD COLUMN gid serial;
ALTER TABLE chp02.xwhyzed ADD PRIMARY KEY (gid);

Now let's populate this with data for testing using the following query:

INSERT INTO chp02.xwhyzed (x, y, z)
 VALUES (random()*5, random()*7, random()*106);
INSERT INTO chp02.xwhyzed (x, y, z)
 VALUES (random()*5, random()*7, random()*106);
INSERT INTO chp02.xwhyzed (x, y, z)
 VALUES (random()*5, random()*7, random()*106);
INSERT INTO chp02.xwhyzed (x, y, z)
 VALUES (random()*5, random()*7, random()*106);

How to do it...

Now to create the view, we will use the following query:

-- Ensure we don't try to duplicate the view
DROP VIEW IF EXISTS chp02.xbecausezed;
-- Retain original attributes, but also create a point attribute from x and y
CREATE VIEW chp02.xbecausezed AS
 SELECT x, y, z, ST_MakePoint(x,y)
 FROM chp02.xwhyzed;

How it works...

Our view is really a simple transformation of the existing data using PostGIS's ST_MakePoint function. The ST_MakePoint function takes the input of two numbers to create a PostGIS point; in this case, our view simply uses our x and y values to populate the data. Any time there is an update to the table to add a new record with x and y values, the view will populate a point, which is really useful for data that is constantly being updated.

There are two disadvantages to this approach. The first is that we have not declared our spatial reference system in the view, so any software consuming these points will not know the coordinate system we are using—that is, whether it is a geographic (latitude/longitude) or a planar coordinate system. We will address this problem shortly. The second problem is that many software systems accessing these points may not automatically detect and use the spatial information from the table. This problem is addressed in the Using triggers to populate a geometry column recipe.

Note

Spatial Reference ID (SRID) allows us to specify the coordinate system for a given dataset. The numbering system is a simple integer value to specify a given coordinate system. SRID is derived originally from the European Petroleum Survey Group (EPSG ) and now maintained by the Surveying & Positioning Committee of the International Association of Oil & Gas Producers (OGP ). Useful tools for SRIDs are Spatial Reference (http://spatialreference.org) and Prj2EPSG (http://prj2epsg.org/search).

There's more...

To address the first problem mentioned in the How it works... section, we can simply wrap our existing ST_MakePoint function in another function specifying the SRID using ST_SetSRID, as shown in the following query:

-- Ensure we don't try to duplicate the view
DROP VIEW IF EXISTS chp02.xbecausezed;
-- Retain original attributes, but also create a point attribute from x and y
CREATE VIEW chp02.xbecausezed AS
 SELECT x, y, z, ST_SetSRID(ST_MakePoint(x,y), 3734) -- Add ST_SetSRID
 FROM chp02.xwhyzed;

See also

  • The Using triggers to populate a geometry column recipe
主站蜘蛛池模板: 莲花县| 北海市| 广灵县| 黑龙江省| 兴业县| 东明县| 马山县| 民县| 镇江市| 中西区| 宕昌县| 洛阳市| 青河县| 扬州市| 咸丰县| 玉田县| 华蓥市| 乌拉特中旗| 石嘴山市| 晋江市| 两当县| 杭锦后旗| 丰顺县| 安宁市| 二连浩特市| 阆中市| 临西县| 德化县| 文登市| 上蔡县| 申扎县| 师宗县| 麻阳| 沁水县| 辉南县| 永安市| 松溪县| 镇江市| 广饶县| 清流县| 阿荣旗|