Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a table for Polygon values in Postgis and inserting

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?

like image 385
ckp Avatar asked Aug 13 '16 22:08

ckp


People also ask

How do I add a polygon in PostGIS?

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.

What is wkt in PostGIS?

Well-Known Text (WKT)

What is the difference between geometry and geography data in PostGIS?

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.

What is Srid in PostGIS?

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';


1 Answers

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)
like image 144
icuken Avatar answered Sep 30 '22 12:09

icuken