Skip to content

Commit

Permalink
Add an answer to city boundaries lesson (#9427)
Browse files Browse the repository at this point in the history
  • Loading branch information
DelazJ authored Nov 29, 2024
2 parents 215f0fe + 00460f7 commit 7125fed
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions docs/training_manual/spatial_databases/geometry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,67 @@ Try Yourself: :abbr:`★★★ (Advanced level)`
Create city boundaries by computing the minimum convex hull of all addresses
for that city and computing a buffer around that area.

.. admonition:: Answer
:class: dropdown

- Add some people in 'Tokyo Outer Wards' city

.. code-block:: psql
INSERT INTO people (name, house_no, street_id, phone_no, city_id, geom)
VALUES ('Bad Aboum',
57,
2,
'073 712 31 21',
2,
'SRID=4326;POINT(22 18)');
INSERT INTO people (name, house_no, street_id, phone_no, city_id, geom)
VALUES ('Pat Atra',
59,
2,
'074 712 31 25',
2,
'SRID=4326;POINT(23 14)');
INSERT INTO people (name, house_no, street_id, phone_no, city_id, geom)
VALUES ('Kat Herin',
65,
2,
'074 722 31 28',
2,
'SRID=4326;POINT(29 18)');
- Create myPolygonTable table

.. code-block:: psql
CREATE TABLE myPolygonTable (
id serial NOT NULL PRIMARY KEY,
city_id int NOT NULL REFERENCES cities(id),
geom geometry NOT NULL
);
ALTER TABLE myPolygonTable
ADD CONSTRAINT myPolygonTable_geom_polygon_chk
CHECK (st_geometrytype(geometry) = 'ST_Polygon'::text );
- Create and load the convex hulls

.. code-block:: psql
INSERT INTO myPolygonTable (city_id, geometry)
SELECT * FROM
(
SELECT
ROW_NUMBER() over (order by city_id)::integer AS city_id,
ST_CONVEXHULL(ST_COLLECT(geom)) AS geom
FROM people
GROUP BY city_id
) convexHulls;
Access Sub-Objects
-------------------------------------------------------------------------------
Expand Down

0 comments on commit 7125fed

Please sign in to comment.