Skip to content

Commit aa0c9d7

Browse files
committed
add lab 10 postgis
1 parent cc8d555 commit aa0c9d7

File tree

1 file changed

+112
-0
lines changed
  • Advanced databases 2022/Lab 10 (PostGIS)

1 file changed

+112
-0
lines changed

Advanced databases 2022/Lab 10 (PostGIS)/lab10.md

+112
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,122 @@ QGIS aims to be a user-friendly GIS, providing common functions and features. Th
124124
4. After connection open PostGIS database layer in QGIS.
125125

126126

127+
## Create table with geometry columns
128+
129+
The PostGis support two standard ways of expressing spatial objects:
130+
- the Well-Known Text (WKT) form
131+
- the Well-Known Binary (WKB) form.
132+
133+
Both WKT and WKB include information about the type of the object and the coordinates which form the object.
134+
135+
Examples of the selected text representations of the spatial objects (WKT ) of the features are as follows:
136+
137+
- POINT(0 0) - describe point in 2D space
138+
139+
- POINT Z (0 0 0) - describe point in 3D space
140+
141+
- POINT ZM (0 0 0 0) - describe point in 4D space
142+
143+
- LINESTRING(0 0,1 1,1 2) - describe line in 2D space
144+
145+
- POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)) - describe poligon in 2D
146+
147+
- MULTIPOINT((0 0),(1 2)) - describe set of points in 2D space
148+
149+
- MULTIPOINT Z ((0 0 0),(1 2 3)) - describe set of points in 3D space
150+
151+
- MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4)) - describe set of lines in 2D space
152+
153+
- MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1))) - describe set of polygons in 2D space
154+
155+
- GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4)) - describe set of geometry object
156+
157+
In practice we can use this information to create simply table with geometry columns:
158+
159+
1. In pgAdmin, open database **lab10_nyc** and run SQL Editor.
160+
2. Create table geom_example:
161+
```sql
162+
CREATE TABLE geom_example (object_name varchar, geom geometry);
163+
```
164+
3. Because the creation of geometric objects is done by giving the constructor in text form. The commands for adding objects to table will take the form:
165+
```sql
166+
INSERT INTO geom_example VALUES
167+
('Point', 'POINT(0 0)'),
168+
('Linestring', 'LINESTRING(0 0, 1 1, 2 1, 2 2)'),
169+
('Polygon', 'POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'),
170+
('PolygonWithHole', 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1))'),
171+
('Collection', 'GEOMETRYCOLLECTION(POINT(2 0),POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))');
172+
```
173+
174+
4. To check the definition of graphical objects in the database, we can use the function *ST_AsText*:
175+
```sql
176+
SELECT object_name, ST_AsText(geom) FROM geom_example;
177+
```
178+
179+
## Spatial function
180+
Using geometric objects in the spatial database is done by manipulating them with the functions built into the database. We should remember that with PostGIS all functions assume the geometric type, but their operation differs depending on the represented object. Of course, this principle does not apply to other database engines of this type.
181+
182+
In this section, selected functions will be discussed along with their subcategories.
183+
184+
### Input/Output data
185+
- **ST_GeomFromText(text)** - returns geometry,
186+
- **ST_AsText(geometry)** - returns WKT text,
187+
- **ST_AsEWKT(geometry)** - returns EWKT text,
188+
- **ST_GeomFromWKB(bytea)** - returns geometry,
189+
- **ST_AsBinary(geometry)** - returns WKB bytea,
190+
- **ST_AsEWKB(geometry)** - returns EWKB bytea,
191+
- **ST_GeomFromGML(text)** - returns geometry,
192+
- **ST_AsGML(geometry)** - returns GML text,
193+
- **ST_GeomFromKML(text)** - returns geometry,
194+
- **ST_AsKML(geometry)** - returns KML text,
195+
- **ST_AsGeoJSON(geometry)** - returns JSON text,
196+
- **ST_AsSVG(geometry)** - returns SVG text,
197+
198+
### Data type exploration
199+
200+
- **ST_GeometryType(geometry)** - returns the type of the geometry,
201+
- **ST_NDims(geometry)** - returns the number of dimensions of the geometry,
202+
- **ST_NumGeometries(geometry)** - returns the number of parts in the collection,
203+
- **ST_GeometryN(geometry,int)** - returns the specified part of the collection,
204+
- **ST_NumGeometries(multi/geomcollection)** - returns the number of parts in the collection
205+
206+
### Geometry description
207+
- **ST_X(geometry)** - returns the X coordinate of point,
208+
- **ST_Y(geometry)** - returns the Y coordinate of point,
209+
- **ST_StartPoint(geometry)** - returns the first line string coordinate as a point,
210+
- **ST_EndPoint(geometry)** - returns the last line string coordinate as a point,
211+
- **ST_NPoints(geometry)** - returns the number of coordinates in the line string,
212+
- **ST_Length(geometry)** - returns the total length of all linear parts,
213+
- **ST_Area(geometry)** - returns the total area of all polygonal parts,
214+
- **ST_NRings(geometry)** - returns the number of rings (usually 1, more if there are holes) of polygonal,
215+
- **ST_ExteriorRing(geometry)** - returns the outer ring as a line string of polygonal,
216+
- **ST_InteriorRingN(geometry,n)** - returns a specified interior ring as a line string of polygonal,
217+
- **ST_Perimeter(geometry)** - returns the length of all the rings of polygonal,
218+
219+
### Geometry relation
220+
- **ST_Contains(geometry A, geometry B)** - returns true if geometry A contains geometry B
221+
- **ST_Crosses(geometry A, geometry B)** - returns true if geometry A crosses geometry B
222+
- **ST_Disjoint(geometry A , geometry B)** - returns true if the geometries do not “spatially intersect”
223+
- **ST_Distance(geometry A, geometry B)** - returns the minimum distance between geometry A and geometry B
224+
- **ST_DWithin(geometry A, geometry B, radius)** - returns true if geometry A is radius distance or less from geometry B
225+
- **ST_Equals(geometry A, geometry B)** - returns true if geometry A is the same as geometry B
226+
- **ST_Intersects(geometry A, geometry B)** - returns true if geometry A intersects geometry B
227+
- **ST_Overlaps(geometry A, geometry B)** - returns true if geometry A and geometry B share space, but are not completely contained by each other.
228+
- **ST_Touches(geometry A, geometry B)** - returns true if the boundary of geometry A touches geometry B
229+
- **ST_Within(geometry A, geometry B)** - returns true if geometry A is within geometry B
230+
127231
##Exercises:
128232

129233
1. How many records are in the nyc_streets table?
130234
2. How many streets in New York have names that start with ‘B’, 'Q' and 'M'?
131235
3. What is the population of New York city?
132236
4. What is the population of the Bronx, Manhattan and Queens?
133237
5. How many "neighborhoods" are in each borough?
238+
6. What is the area of the: West Village, Harlem, Great Kills neighborhood?
239+
7. Find all neighborhoods bordering on: Rossville, Queens Village and Midtown
240+
8. What is the area of: Staten Island, Manhattan, Brooklyn?
241+
9. How many census blocks in New York City have a hole in them?
242+
10. What is the summary area of 'a hole' of census blocks in New York City?
243+
11. What is the length of streets in New York City, summarized by type?
244+
12. What streets crossing with: Pacific St, E 9th St, Avenue K?
245+
13. Find ten closest stations from: Elder Ave, Castle Hill Ave, 4th Ave.

0 commit comments

Comments
 (0)