PostGIS realizes circular search and rectangular search

The query to find the center of the circle POINT(1000 1000) within 100 meters is as follows:

SELECT * FROM geotable
WHERE ST_DWithin(geocolumn, ‘POINT(1000 1000)’, 100.0);

Or what you need is rectangle retrieval: ST_MakeBox2D(geometry pointLowLeft, geometry pointUpRight);

SELECT feature_id, feature_name, the_geom
FROM features
WHERE the_geom && ST_SetSRID(ST_MakeBox2D(ST_Point(-989502.1875, 528439.5625),
ST_Point(-987121.375 ,529933.1875)),2163)

 Here is a detailed explanation:

Click on an arbitrary latitude and longitude on the map. I want to find how many points of interest are contained within a specified radius from this point.

Check the postgis manual and find such a function ( ST_Distance (geometry g1, geometry g2);)

The official documentation says this:

ST_Distance — For geometry type Returns the 2-dimensional cartesian minimum distance (based on spatial ref) between two geometries in projected units. For geography type defaults to return spheroidal minimum distance between two geographies in meters.

That is, given two spatial points, calculate the distance between the two points. The unit of the calculation result is related to the reference system of your spatial data.

If you are using the 4326 (wgs84) coordinate system, it is in degrees. If you want to convert to meters, you have to do some conversion. The following will be mentioned:

GEOCS represents the geographic coordinate system, that is, the coordinate system represented by latitude and longitude, such as
the projected coordinate system represented by 4326 PROJCS, which converts the spherical coordinate system into a plane coordinate system through an algorithm for calculation, generally in meters. Unit representation, such as 26986.

Therefore, when calculating the distance between two points, since the stored data are all latitude and longitude, it refers to GEOCS. To get the result in meters, first convert it to PROJCS, you can Implemented by ST_Transform

 See postgis manual geometry  ST_Transform (geometry g1, integer srid);

The first parameter is the original geometric object. The second parameter is to convert it to the coordinate system represented by this projection.

At this time, we just need to find a projection with a unit of meters as the standard system and convert it to it.

example:

SELECT ST_Distance(
   ST_Transform(ST_GeomFromText('POINT(-87.734087560562 43.770129071141)',4326),26986),
   ST_Transform(ST_GeomFromText('POINT(-87.747382933006 43.759234252055)', 4326),26986)
  );

The result of this check is the distance between two points in meters.

Now we are talking about how to find points within a certain range

The first function in postgis is used here: boolean  ST_DWithin (geometry g1, geometry g2, double precision distance_of_srid);

The first parameter is the reference object. The second parameter is the target object. The third parameter is the distance (similarly, if it is a geographic coordinate system, the unit is degrees. The projection system unit is meters)

That is, with g1 as the center and the radius as distance_of_srid, the package does not contain g2 in this range. If it contains g2, it will return true, otherwise it will be false.

A complete example is given below. Find points of interest within a radius of 1516 meters with (-87.71 43.741) as the center, and then arrange the results in order from near to far from this center point

SELECT t.feat_id,astext(t.geometry) FROM gis_site t 
WHERE ST_DWithin(

ST_Transform(GeomFromText('POINT(-87.71 43.741)',4326),26986),

ST_Transform(t.geometry,26986),

1516) 
ORDER BY ST_Distance(GeomFromText('POINT(-87.71 43.741)',4326), t.geometry);

result:

24;"POINT(-87.718330082111 43.753078987035)"
17;"POINT(-87.726085716036 43.736952192682)"
18;"POINT(-87.726085716036 43.736952192682)"

found three points

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325407797&siteId=291194637