I have the following area "name" and "polygon" values for 10 different areas ('A',50.6373 3.0750,50.6374 3.0750,50.6374 3.0749,50.63 3.07491,50.6373 3.0750)
I want to create a table in postgres DB using POSTGIS
Later, I will have lan and lat values (e.g. 50.5465 3.0121) in a table to compare with the above table and pull out the area name
Can you help me with the code for both creating and inserting the polygon coordinates?
Create a Polygon from a 2D LineString. SELECT ST_MakePolygon( ST_GeomFromText('LINESTRING(75 29,77 29,77 29, 75 29)')); Create a Polygon from an open LineString, using ST_StartPoint and ST_AddPoint to close it.
Well-Known Text (WKT)
PostGIS gives you a choice of two different ways to store geospatial data: Geometry, where it assumes all of your data lives on a Cartesian plane (like a map projection); Geography, where it assumes that your data is made up of points on the earth's surface, as specified by latitudes and longitudes.
An SRID indicates coordinates of data. For example: Supported SRIDs can be queried from PostGIS, such as query coordinates related to Beijing and China. postgres=# select * from spatial_ref_sys where srtext ~* 'beijing'; postgres=# select * from spatial_ref_sys where srtext ~* 'china';
I don't have enough reputation to comment you question, there is a link you might find useful: SQL query for point-in-polygon using PostgreSQL
Adding extension for your database
CREATE EXTENSION postgis;
Creating table
CREATE TABLE areas (
id SERIAL PRIMARY KEY,
name VARCHAR(64),
polygon GEOMETRY
);
Creating index over polygon field
CREATE INDEX areas_polygon_idx ON areas USING GIST (polygon);
Inserting record
INSERT INTO areas (name, polygon) VALUES (
'A',
ST_GeometryFromText('POLYGON((50.6373 3.0750,50.6374 3.0750,50.6374 3.0749,50.63 3.07491,50.6373 3.0750))')
);
Querying
SELECT name FROM areas WHERE ST_Contains(polygon, ST_GeomFromText('POINT(50.637 3.074)'));
name
------
(0 rows)
SELECT name FROM areas WHERE ST_Contains(polygon, ST_GeomFromText('POINT(50.63735 3.07495)'));
name
------
A
(1 row)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With