Search code examples
sqlpostgresqlgispostgis

How to get attributes of Polygon by providing X and Y values of Clicking position in Postgis?


I am looking for query help. I want to give lat and long values to query and get in result all values of that polygon.

> Geometry is polygon layer (Grids of 10x10 m)
> 
> Select * from Geometry_table where ST_x= 32 and ST_Y = 72 ;

Solution

  • First you need ST_MakePoint to create a point based on x,y coordinates, and then ST_Contains to check if the given coordinates lie inside of any polygon (grid cell):

    SELECT * 
    FROM geometry_table
    WHERE ST_Contains(geom,ST_SetSRID(ST_MakePoint(32,72),4326))
    
    • This query assumes your grid has the SRID 4326 (WGS84). Change it to the right SRS in case it differs from your table.

    Demo: db<>fiddle