gis:postgresql-postgis-notes
Differences
This shows you the differences between two versions of the page.
| Previous revision | |||
| — | gis:postgresql-postgis-notes [2024/11/22 12:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== PostgreSQL - PostGIS ====== | ||
| + | ===== Introduction ===== | ||
| + | This section contains several notes that can be helpful to people who work with GIS systems and/or analyze GIS data. | ||
| + | |||
| + | Although it is highly preferable that your GIS system performs directly on the geometry columns, there are several cases where you need to import numeric data from spreadsheets into latitude-longitude columns and subsequently update the geometry columns manually. These examples will help. | ||
| + | |||
| + | There are several cases when analyzing GIS data where the coordinates need to appear in a metric system, so transformations need to take place. In the following examples, I use the ubiquitous WGS84 EPSG:4326 as well as the metric GGRS87 EPSG: | ||
| + | |||
| + | Please note that the examples are not guaranteed to work in all versions/ | ||
| + | |||
| + | ===== Create and drop coordinate columns ===== | ||
| + | When you use database columns where geometry data will be stored, do not create those columns using the normal database commands. **Use the PostGIS commands in order to specify the geometry parameters.** | ||
| + | |||
| + | Examples: Create a 3D point column in EPSG:4326 and a 2D column in GGRS87. You need to specify the database schema '' | ||
| + | |||
| + | < | ||
| + | SELECT AddGeometryColumn (' | ||
| + | SELECT AddGeometryColumn (' | ||
| + | </ | ||
| + | |||
| + | Example: Create a 2D linestring column | ||
| + | |||
| + | < | ||
| + | SELECT AddGeometryColumn (' | ||
| + | </ | ||
| + | |||
| + | Example: Delete the geometry columns | ||
| + | |||
| + | < | ||
| + | SELECT DropGeometryColumn(' | ||
| + | SELECT DropGeometryColumn(' | ||
| + | </ | ||
| + | |||
| + | ===== Insert a point with geometry ===== | ||
| + | |||
| + | Example: Insert a point in a table with a primary key called '' | ||
| + | |||
| + | < | ||
| + | INSERT INTO nodes (id, geom) VALUES (' | ||
| + | </ | ||
| + | |||
| + | Note that the coordinates are in x y format and therefore the first number is '' | ||
| + | |||
| + | Example: same as above in 3D, where '' | ||
| + | |||
| + | < | ||
| + | INSERT INTO nodes (id, geom) VALUES (' | ||
| + | </ | ||
| + | |||
| + | ===== Update a WGS84 geom column with data from lat lon columns ===== | ||
| + | |||
| + | Suppose that you have in your table two numeric columns '' | ||
| + | |||
| + | < | ||
| + | UPDATE cities SET geom = ST_SetSRID(ST_MakePoint(lon, | ||
| + | </ | ||
| + | |||
| + | ===== Update a GGRS87 metric geom column with data from lat lon columns ===== | ||
| + | |||
| + | Suppose that you have in your table two numeric columns '' | ||
| + | |||
| + | < | ||
| + | UPDATE cities SET geom = ST_Transform(ST_SetSRID(ST_MakePoint(lon, | ||
| + | </ | ||
| + | |||
| + | ===== Update a geom column with data from metric x,y columns ===== | ||
| + | |||
| + | Suppose that you have in your table two numeric columns '' | ||
| + | |||
| + | < | ||
| + | UPDATE cities SET geom = ST_Transform(ST_SetSRID(ST_MakePoint(ggrs87_x, | ||
| + | </ | ||
| + | |||
| + | ===== Correct a geom after modifying lat, lon ===== | ||
| + | Suppose you made a correction to the numeric values of lat and lon of London and you want to update the geom column of this single database row. | ||
| + | |||
| + | < | ||
| + | UPDATE cities SET geom = ST_SetSRID(ST_MakePoint(lon, | ||
| + | </ | ||
| + | |||
| + | ===== Get the nearest neighbours using spatial indexing ===== | ||
| + | Suppose that you want to retrieve the 5 nearest neighbours of point (22.164885, | ||
| + | |||
| + | The data are in EPSG:4326 but it requires the '' | ||
| + | I set both sides to EPSG:2100 because I want to know the distance in meters. Otherwise, I could avoid ST_Transform. | ||
| + | |||
| + | This same query also provides the distance in meters between the provided coordinates and each layer point, as return query column '' | ||
| + | |||
| + | < | ||
| + | SELECT *, ST_Transform(ST_SetSRID(geom, | ||
| + | FROM cities | ||
| + | ORDER BY dist | ||
| + | LIMIT 5 | ||
| + | </ | ||
| + | |||
| + | ===== Identify the points of a Point layer that are outside polygons of a Polygon layer ===== | ||
| + | |||
| + | There are multiple queries that you can use to solve this problem. You can find an interesting discussion in [[https:// | ||
| + | |||
| + | < | ||
| + | SELECT polygons.id FROM polygons WHERE NOT EXISTS | ||
| + | (SELECT 1 FROM points WHERE ST_Intersects(polygons.geom, | ||
| + | </ | ||
| + | |||
| + | For comparison purposes, in a data set with 45.000 points and 45.000 buildings, this query took approximately 4 minutes, while other queries took more than 14 minutes! | ||
| + | |||
| + | **WARNING: | ||
| + | |||
| + | ===== Import an SHP file manually ===== | ||
| + | Example: | ||
| + | |||
| + | < | ||
| + | shp2pgsql -I -s < | ||
| + | </ | ||
| + | |||
| + | ===== PHP parameter binding with geometry values ===== | ||
| + | If you write PHP code and you want to use the geometry column in a parameter binding, you will need to transform the geometry value into text. | ||
| + | |||
| + | In the following example, I assume that I have read a value from column '' | ||
| + | |||
| + | < | ||
| + | $sql = " | ||
| + | $st = $dbh-> | ||
| + | $st-> | ||
| + | $st-> | ||
| + | </ | ||
| + | |||
| + | Similar when a CRS transformation is required: | ||
| + | |||
| + | < | ||
| + | $sql = " | ||
| + | $st = $dbh-> | ||
| + | $st-> | ||
| + | $st-> | ||
| + | </ | ||
| + | |||
| + | |||
| + | ~~DISQUS~~ | ||
