简体   繁体   中英

How to select polygon from table by Latitude and Longitude (Postgis)

There is polygon table. I need to find one record from table with some Point (Lat/Long) inside of area.

It's example of coordinates: 149.14668176, -35.32202098

Could you please help me to write select string to find area that contain my Point?

 SELECT PostGIS_full_version();
                                                                                             postgis_full_version                                                                                               
 POSTGIS="2.5.2 r17328" [EXTENSION] PGSQL="96" GEOS="3.5.1-CAPI-1.9.1 r4246" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.1.2, released 2016/10/24" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" RASTER

Something like that:

SELECT id,name FROM area_polygon WHERE ST_Within('149.14668176, -35.32202098', geog);
bounds=# \d bounds.area_polygon;
id           | integer                 |           | not null |
geog         | geography(Polygon,4283) |           |          |
name         | text                    |           |          |

I expected:

  id  | name
------+--------
    1 | Alabama

st_within only supports geometry types, which is why you get the error in the earlier answer, because you have a geography column type.

You can either cast to geometry:

SELECT id,name 
FROM area_polygon 
WHERE ST_Within(ST_SetSRID(ST_POINT(149.14668176,-35.32202098),4283), geog::geometry);

Or you can use st_dwithin , with distance set to zero:

SELECT id,name 
    FROM area_polygon 
    WHERE ST_DWithin(ST_SetSRID(ST_POINT(149.14668176,-35.32202098),4283)::geography, geog,0);

Note that the order of the coordinates must be lon/lat (and not lat/lon) and I am assuming those coordinates are in your SRID 4283. They have to either match the geog SRID or be transformed to it...

See here for a list of which functions support which arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM