9

I'm working on a system that stores latitude/longitude information of addresses in a PostGIS table. To store Lat/Long in geometry data type I'm using ST_GeometryFromText function.

For example the following function call gets the geometric value against the specified Long/Lat position:

 myPointGeo =  ST_GeometryFromText('POINT(40.758871 -73.985114)');

Similarly, I convert a Polygon into geometric representation as follows:

myPolygonGeo =  ST_GeometryFromText('POLYGON ((40.7566484549725 -73.9878561496734, 40.7556894646734 -73.9853026866913, 40.7545841705587 -73.9860537052154, 40.7548036054111 -73.9881458282471, 40.7559820394514 -73.9887895584106, 40.7566484549725 -73.9878561496734 ))')

I want to find out whether the above lat/long position resides within this Polygon or not. For this I'm using ST_Within function. But though 'myPointGeo' actually resides within the Polygon, ST_Within is returning false. I'm using ST_Within this way:

 St_Within(myPointGeo,myPolygonGeo)

What am I doing wrong here? Should I use some other function for this purpose?

2

1 Answer 1

18

I think ST_Intersects would work better, it returns a true or false on whether or not two geometries intersect. So you'd want to do something like:

SELECT ST_Intersects(myPointGeo, myPolygonGeo);

or you can do:

SELECT a.id, b.id 
FROM pointTableName a, polygonTableName b 
WHERE ST_Intersects(a.myPointGeo, b.myPolygonGeo);
1
  • Thank you for your answer Commented Mar 31, 2020 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.