PostgreSQL 地图两点之间,经纬度距离计算, PostGIS方式计算, 电子围栏功能实现;PostgreSQL空间数据存储扩展 PostGIS

本文主要是介绍PostgreSQL 地图两点之间,经纬度距离计算, PostGIS方式计算, 电子围栏功能实现;PostgreSQL空间数据存储扩展 PostGIS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

PostgreSQL 地图两点之间,经纬度距离计算, PostGIS方式计算, 电子围栏功能实现

  • PostGIS介绍 - PostgreSQL空间数据存储扩展 PostGIS
  • 安装插件PostGIS
  • 在数据库中启用插件
  • 坐标点相关函数使用
      • 坐标系 AddGeometryColumn
      • 文本转换为几何 ST_GeomFromText
      • 转换坐标体系ST_Transform
      • 将几何类型转换为文本描述ST_AsText
      • 测试点保存
      • 距离计算函数 ST_Distance
      • 文本转换地理几何类型函数 ST_GeogFromText
  • 两点距离计算
    • 附近5公里内的前50个点
    • 最近的30个点店
    • 计算gps附近50m内的围栏

PostGIS在对象关系型数据库PostgreSQL上增加了存储管理空间数据的能力,相当于Oracle的spatial部分。PostGIS最大的特点是符合并且实现了OpenGIS的一些规范,是最著名的开源GIS数据库。

PostGIS的版权被纳入到GNU的GPL中,也就是说任何人可以自由得到PostGIS的源码并对其做研究和改进。正是由于这一点,PostGIS得到了迅速的发展,越来越多的爱好者和研究机构参与到PostGIS的应用开发和完善当中。

PostGIS是由Refractions Research
Inc开发的,Refractions是一家GIS和数据库咨询公司,Refraction公司最初是在PostgreSQL的基础上研究空间数据库的实
现,由于PostgreSQL所提供的空间数据类型和功能远远不能满足GIS的需求,研究工作经常陷入到进退维谷的境地,最终的结果往往是耗费了大量的人
力物力,而产品却极其复杂并且性能低下。这些原因直接或间接促成PostGIS项目的实施。

PostGIS介绍 - PostgreSQL空间数据存储扩展 PostGIS

PostGIS在对象关系型数据库PostgreSQL上增加了存储管理空间数据的能力,相当于Oracle的spatial部分。PostGIS最大的特点是符合并且实现了OpenGIS的一些规范,是最著名的开源GIS数据库。

PostGIS的版权被纳入到GNU的GPL中,也就是说任何人可以自由得到PostGIS的源码并对其做研究和改进。正是由于这一点,PostGIS得到了迅速的发展,越来越多的爱好者和研究机构参与到PostGIS的应用开发和完善当中。

PostGIS是由Refractions Research Inc开发的,Refractions是一家GIS和数据库咨询公司,Refraction公司最初是在PostgreSQL的基础上研究空间数据库的实 现,由于PostgreSQL所提供的空间数据类型和功能远远不能满足GIS的需求,研究工作经常陷入到进退维谷的境地,最终的结果往往是耗费了大量的人 力物力,而产品却极其复杂并且性能低下。这些原因直接或间接促成PostGIS项目的实施。

安装插件PostGIS

1、导入repo(导入过程省略)

2、安装pg-10(安装过程省略)

3、检查插件

yum search postgis

在这里插入图片描述

# yum search postgisLoaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
================================================ N/S matched: postgis =================================================
postgis-docs.x86_64 : Extra documentation for PostGIS
postgis-utils.x86_64 : The utils for PostGIS
postgis23_10-client.x86_64 : Client tools and their libraries of PostGIS
postgis23_10-devel.x86_64 : Development headers and libraries for PostGIS
postgis23_10-docs.x86_64 : Extra documentation for PostGIS
postgis23_10-utils.x86_64 : The utils for PostGIS
postgis24_10-client.x86_64 : Client tools and their libraries of PostGIS
postgis24_10-devel.x86_64 : Development headers and libraries for PostGIS
postgis24_10-docs.x86_64 : Extra documentation for PostGIS
postgis24_10-gui.x86_64 : GUI for PostGIS
postgis24_10-utils.x86_64 : The utils for PostGIS
postgis25_10-client.x86_64 : Client tools and their libraries of PostGIS
postgis25_10-devel.x86_64 : Development headers and libraries for PostGIS
postgis25_10-docs.x86_64 : Extra documentation for PostGIS
postgis25_10-gui.x86_64 : GUI for PostGIS
postgis25_10-utils.x86_64 : The utils for PostGIS
postgis25_12-client.x86_64 : Client tools and their libraries of PostGIS
postgis25_12-devel.x86_64 : Development headers and libraries for PostGIS
postgis25_12-docs.x86_64 : Extra documentation for PostGIS
postgis25_12-gui.x86_64 : GUI for PostGIS
postgis25_12-utils.x86_64 : The utils for PostGIS
postgis30_10-client.x86_64 : Client tools and their libraries of PostGIS
postgis30_10-devel.x86_64 : Development headers and libraries for PostGIS
postgis30_10-docs.x86_64 : Extra documentation for PostGIS
postgis30_10-gui.x86_64 : GUI for PostGIS
postgis30_10-utils.x86_64 : The utils for PostGIS
postgis30_12-client.x86_64 : Client tools and their libraries of PostGIS
postgis30_12-devel.x86_64 : Development headers and libraries for PostGIS
postgis30_12-docs.x86_64 : Extra documentation for PostGIS
postgis30_12-gui.x86_64 : GUI for PostGIS
postgis30_12-utils.x86_64 : The utils for PostGIS
SFCGAL.x86_64 : C++ wrapper library around CGAL for PostGIS
pgrouting_10.x86_64 : Routing functionality for PostGIS
pgrouting_12.x86_64 : Routing functionality for PostGIS
postgis.x86_64 : Geographic Information Systems Extensions to PostgreSQL
postgis23_10.x86_64 : Geographic Information Systems Extensions to PostgreSQL
postgis24_10.x86_64 : Geographic Information Systems Extensions to PostgreSQL
postgis25_10.x86_64 : Geographic Information Systems Extensions to PostgreSQL
postgis25_12.x86_64 : Geographic Information Systems Extensions to PostgreSQL
postgis30_10.x86_64 : Geographic Information Systems Extensions to PostgreSQL
postgis30_12.x86_64 : Geographic Information Systems Extensions to PostgreSQLName and summary matches only, use "search all" for everything.

4、安装插件

yum install postgis.x86_64 postgis24_10.x86_64 -y --skip-broken 

等待插件安装完成即可
在这里插入图片描述

在数据库中启用插件

-- 添加空间插件
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;

在这里插入图片描述

启用插件之后,public下会新增一个表spatial_ref_sys
在这里插入图片描述

坐标点相关函数使用

坐标系 AddGeometryColumn

POINT数据类型支持
AddGeometryColumn函数使用 https://postgis.net/docs/AddGeometryColumn.html

Synopsis
text AddGeometryColumn(varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);text AddGeometryColumn(varchar schema_name, varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);text AddGeometryColumn(varchar catalog_name, varchar schema_name, varchar table_name, varchar column_name, integer srid, varchar type, integer dimension, boolean use_typmod=true);

在这里插入图片描述
函数测试示例

-- Create schema to hold data
CREATE SCHEMA my_schema;
-- Create a new simple PostgreSQL table
CREATE TABLE my_schema.my_spatial_table (id serial);-- Describing the table shows a simple table with a single "id" column.
postgis=# \d my_schema.my_spatial_tableTable "my_schema.my_spatial_table"Column |  Type   |                                Modifiers
--------+---------+-------------------------------------------------------------------------id     | integer | not null default nextval('my_schema.my_spatial_table_id_seq'::regclass)-- Add a spatial column to the table
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom',4326,'POINT',2);-- Add a point using the old constraint based behavior
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom_c',4326,'POINT',2, false);--Add a curvepolygon using old constraint behavior
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geomcp_c',4326,'CURVEPOLYGON',2, false);-- Describe the table again reveals the addition of a new geometry columns.
\d my_schema.my_spatial_tableaddgeometrycolumn
-------------------------------------------------------------------------my_schema.my_spatial_table.geomcp_c SRID:4326 TYPE:CURVEPOLYGON DIMS:2
(1 row)Table "my_schema.my_spatial_table"Column  |         Type         |                                Modifiers
----------+----------------------+-------------------------------------------------------------------------id       | integer              | not null default nextval('my_schema.my_spatial_table_id_seq'::regclass)geom     | geometry(Point,4326) |geom_c   | geometry             |geomcp_c | geometry             |
Check constraints:"enforce_dims_geom_c" CHECK (st_ndims(geom_c) = 2)"enforce_dims_geomcp_c" CHECK (st_ndims(geomcp_c) = 2)"enforce_geotype_geom_c" CHECK (geometrytype(geom_c) = 'POINT'::text OR geom_c IS NULL)"enforce_geotype_geomcp_c" CHECK (geometrytype(geomcp_c) = 'CURVEPOLYGON'::text OR geomcp_c IS NULL)"enforce_srid_geom_c" CHECK (st_srid(geom_c) = 4326)"enforce_srid_geomcp_c" CHECK (st_srid(geomcp_c) = 4326)-- geometry_columns view also registers the new columns --
SELECT f_geometry_column As col_name, type, srid, coord_dimension As ndimsFROM geometry_columnsWHERE f_table_name = 'my_spatial_table' AND f_table_schema = 'my_schema';col_name |     type     | srid | ndims
----------+--------------+------+-------geom     | Point        | 4326 |     2geom_c   | Point        | 4326 |     2geomcp_c | CurvePolygon | 4326 |     2

空间坐标系

SELECT AddGeometryColumn ('poi', 'geom_point', 4326, 'POINT', 2);
SELECT AddGeometryColumn ('poi', 'geom_point_26986', 26986, 'POINT', 2);

两个重要的坐标体系

4326 \ GCS_WGS_1984 \ World Geodetic System (WGS)
26986 \ 美国马萨诸塞州地方坐标系(区域坐标系)\ 投影坐标, 平面坐标

增加坐标列

ALTER TABLE kx_store ADD COLUMN address_geom_p_alter geometry(POINT,4326);

文本转换为几何 ST_GeomFromText

文本转换为几何类型的函数 ST_GeomFromText
函数说明 https://postgis.net/docs/ST_GeomFromText.html

Synopsis
geometry ST_GeomFromText(text WKT);geometry ST_GeomFromText(text WKT, integer srid);

在这里插入图片描述

 SELECT ST_GeomFromText('POINT(123.259113 42.258729)', 4326);

测试数据示例

SELECT ST_GeomFromText('LINESTRING(-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932)');
SELECT ST_GeomFromText('LINESTRING(-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932)',4269);SELECT ST_GeomFromText('MULTILINESTRING((-71.160281 42.258729,-71.160837 42.259113,-71.161144 42.25932))');SELECT ST_GeomFromText('POINT(-71.064544 42.28787)');SELECT ST_GeomFromText('POLYGON((-71.1776585052917 42.3902909739571,-71.1776820268866 42.3903701743239,
-71.1776063012595 42.3903825660754,-71.1775826583081 42.3903033653531,-71.1776585052917 42.3902909739571))');SELECT ST_GeomFromText('MULTIPOLYGON(((-71.1031880899493 42.3152774590236,
-71.1031627617667 42.3152960829043,-71.102923838298 42.3149156848307,
-71.1023097974109 42.3151969047397,-71.1019285062273 42.3147384934248,
-71.102505233663 42.3144722937587,-71.10277487471 42.3141658254797,
-71.103113945163 42.3142739188902,-71.10324876416 42.31402489987,
-71.1033002961013 42.3140393340215,-71.1033488797549 42.3139495090772,
-71.103396240451 42.3138632439557,-71.1041521907712 42.3141153348029,
-71.1041411411543 42.3141545014533,-71.1041287795912 42.3142114839058,
-71.1041188134329 42.3142693656241,-71.1041112482575 42.3143272556118,
-71.1041072845732 42.3143851580048,-71.1041057218871 42.3144430686681,
-71.1041065602059 42.3145009876017,-71.1041097995362 42.3145589148055,
-71.1041166403905 42.3146168544148,-71.1041258822717 42.3146748022936,
-71.1041375307579 42.3147318674446,-71.1041492906949 42.3147711126569,
-71.1041598612795 42.314808571739,-71.1042515013869 42.3151287620809,
-71.1041173835118 42.3150739481917,-71.1040809891419 42.3151344119048,
-71.1040438678912 42.3151191367447,-71.1040194562988 42.3151832057859,
-71.1038734225584 42.3151140942995,-71.1038446938243 42.3151006300338,
-71.1038315271889 42.315094347535,-71.1037393329282 42.315054824985,
-71.1035447555574 42.3152608696313,-71.1033436658644 42.3151648370544,
-71.1032580383161 42.3152269126061,-71.103223066939 42.3152517403219,
-71.1031880899493 42.3152774590236)),
((-71.1043632495873 42.315113108546,-71.1043583974082 42.3151211109857,
-71.1043443253471 42.3150676015829,-71.1043850704575 42.3150793250568,-71.1043632495873 42.315113108546)))',4326);SELECT ST_GeomFromText('CIRCULARSTRING(220268 150415,220227 150505,220227 150406)');

转换坐标体系ST_Transform

函数说明 https://postgis.net/docs/ST_Transform.html

Synopsis
geometry ST_Transform(geometry g1, integer srid);geometry ST_Transform(geometry geom, text to_proj);geometry ST_Transform(geometry geom, text from_proj, text to_proj);geometry ST_Transform(geometry geom, text from_proj, integer to_srid);

在这里插入图片描述

SELECT ST_Transform(ST_GeomFromText('POINT(123.259113 42.258729)', 4326),26986)

测试数据示例

Change Massachusetts state plane US feet geometry to WGS 84 long latSELECT ST_AsText(ST_Transform(ST_GeomFromText('POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))',2249),4326)) As wgs_geom;wgs_geom
---------------------------POLYGON((-71.1776848522251 42.3902896512902,-71.1776843766326 42.3903829478009,
-71.1775844305465 42.3903826677917,-71.1775825927231 42.3902893647987,-71.177684
8522251 42.3902896512902));
(1 row)--3D Circular String example
SELECT ST_AsEWKT(ST_Transform(ST_GeomFromEWKT('SRID=2249;CIRCULARSTRING(743238 2967416 1,743238 2967450 2,743265 2967450 3,743265.625 2967416 3,743238 2967416 4)'),4326));st_asewkt
--------------------------------------------------------------------------------------SRID=4326;CIRCULARSTRING(-71.1776848522251 42.3902896512902 1,-71.1776843766326 42.3903829478009 2,-71.1775844305465 42.3903826677917 3,-71.1775825927231 42.3902893647987 3,-71.1776848522251 42.3902896512902 4)Example of creating a partial functional index. For tables where you are not sure all the geometries will be filled in, its best to use a partial index that leaves out null geometries which will both conserve space and make your index smaller and more efficient.CREATE INDEX idx_the_geom_26986_parcelsON parcelsUSING gist(ST_Transform(the_geom, 26986))WHERE the_geom IS NOT NULL;Examples of using PROJ.4 text to transform with custom spatial references.-- Find intersection of two polygons near the North pole, using a custom Gnomic projection
-- See http://boundlessgeo.com/2012/02/flattening-the-peel/WITH data AS (SELECTST_GeomFromText('POLYGON((170 50,170 72,-130 72,-130 50,170 50))', 4326) AS p1,ST_GeomFromText('POLYGON((-170 68,-170 90,-141 90,-141 68,-170 68))', 4326) AS p2,'+proj=gnom +ellps=WGS84 +lat_0=70 +lon_0=-160 +no_defs'::text AS gnom)SELECT ST_AsText(ST_Transform(ST_Intersection(ST_Transform(p1, gnom), ST_Transform(p2, gnom)),gnom, 4326))FROM data;st_astext--------------------------------------------------------------------------------POLYGON((-170 74.053793645338,-141 73.4268621378904,-141 68,-170 68,-170 74.053793645338))Configuring transformation behaviour
Sometimes coordinate transformation involving a grid-shift can fail, for example if PROJ.4 has not been built with grid-shift files or the coordinate does not lie within the range for which the grid shift is defined. By default, PostGIS will throw an error if a grid shift file is not present, but this behaviour can be configured on a per-SRID basis either by testing different to_proj values of PROJ.4 text, or altering the proj4text value within the spatial_ref_sys table.For example, the proj4text parameter +datum=NAD87 is a shorthand form for the following +nadgrids parameter:+nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat
The @ prefix means no error is reported if the files are not present, but if the end of the list is reached with no file having been appropriate (ie. found and overlapping) then an error is issued.If, conversely, you wanted to ensure that at least the standard files were present, but that if all files were scanned without a hit a null transformation is applied you could use:+nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat,null
The null grid shift file is a valid grid shift file covering the whole world and applying no shift. So for a complete example, if you wanted to alter PostGIS so that transformations to SRID 4267 that didn't lie within the correct range did not throw an ERROR, you would use the following:UPDATE spatial_ref_sys SET proj4text = '+proj=longlat +ellps=clrk66 +nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat,null +no_defs' WHERE srid = 4267;

将几何类型转换为文本描述ST_AsText

函数说明 https://postgis.net/docs/ST_AsText.html

Synopsis
text ST_AsText(geometry g1);text ST_AsText(geometry g1, integer maxdecimaldigits=15);text ST_AsText(geography g1);text ST_AsText(geography g1, integer maxdecimaldigits=15);

在这里插入图片描述

SELECT ST_AsText(ST_GeomFromText('POINT(123.259113 42.258729)', 4326));

测试示例数据

SELECT ST_AsText('01030000000100000005000000000000000000
000000000000000000000000000000000000000000000000
F03F000000000000F03F000000000000F03F000000000000F03
F000000000000000000000000000000000000000000000000');st_astext
--------------------------------POLYGON((0 0,0 1,1 1,1 0,0 0))
(1 row)
Providing the precision is optional.SELECT ST_AsText(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)'))st_astext
------------------------------POINT(111.1111111 1.1111111)
(1 row)
SELECT ST_AsText(GeomFromEWKT('SRID=4326;POINT(111.1111111 1.1111111)'),2)
st_astext
--------------------
POINT(111.11 1.11)
(1 row)

测试点保存

--更新坐标点数据
update kx_store set 
address_geom_point=ST_GeomFromText('POINT(123.259113 42.258729)', 4326), 
address_geom_point_26986=ST_Transform(ST_GeomFromText('POINT(123.259113 42.258729)', 4326),26986),
address_geom_p_alter=ST_GeomFromText('POINT(123.259113 42.258729)', 4326) 
WHERE usid='12698186578197259211269818657819725921';--查询原数据
SELECT address_geom_point,address_geom_point_26986,address_geom_p_alter from kx_store WHERE usid='12698186578197259211269818657819725921';--查询可视数据
SELECT ST_AsText(address_geom_point),ST_AsText(address_geom_point_26986),address_geom_p_alter from kx_store WHERE usid='12698186578197259211269818657819725921';

距离计算函数 ST_Distance

函数使用说明 https://postgis.net/docs/ST_Distance.html

Synopsis
float ST_Distance(geometry g1, geometry g2);float ST_Distance(geography geog1, geography geog2, >boolean use_spheroid=true);
Basic Geometry Examples
Geometry example - units in planar degrees 4326 is WGS 84 long lat, units are degrees.SELECT ST_Distance('SRID=4326;POINT(-72.1235 42.3521)'::geometry,'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry);
st_distance
-----------------
0.00150567726382282
Geometry example - units in meters (SRID: 3857, proportional to pixels on popular web maps). Although the value is off, nearby ones can be compared correctly, which makes it a good choice for algorithms like KNN or KMeans.SELECT ST_Distance(ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 3857),ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 3857));
st_distance
-----------------
167.441410065196
Geometry example - units in meters (SRID: 3857 as above, but corrected by cos(lat) to account for distortion)SELECT ST_Distance(ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 3857),ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 3857)) * cosd(42.3521);
st_distance
-----------------
123.742351254151
Geometry example - units in meters (SRID: 26986 Massachusetts state plane meters) (most accurate for Massachusetts)SELECT ST_Distance(ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 26986),ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 26986));
st_distance
-----------------
123.797937878454
Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (least accurate)SELECT ST_Distance(ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 2163),ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 2163));st_distance
------------------
126.664256056812
Geography Examples
Same as geometry example but note units in meters - use sphere for slightly faster and less accurate computation.SELECT ST_Distance(gg1, gg2) As spheroid_dist, ST_Distance(gg1, gg2, false) As sphere_dist
FROM (SELECT'SRID=4326;POINT(-72.1235 42.3521)'::geography as gg1,'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geography as gg2) As foo  ;spheroid_dist   |   sphere_dist
------------------+------------------123.802076746848 | 123.475736916397

文本转换地理几何类型函数 ST_GeogFromText

函数使用说明 https://postgis.net/docs/ST_GeogFromText.html

Synopsis
geography ST_GeogFromText(text EWKT);
--- converting lon lat coords to geography
ALTER TABLE sometable ADD COLUMN geog geography(POINT,4326);
UPDATE sometable SET geog = ST_GeogFromText('SRID=4326;POINT(' || lon || ' ' || lat || ')');--- specify a geography point using EPSG:4267, NAD27
SELECT ST_AsEWKT(ST_GeogFromText('SRID=4267;POINT(-77.0092 38.889588)'));

两点距离计算

距离计算函数 ST_Distance

范围计算函数 ST_DWithin

文本转换地理几何类型函数 ST_GeogFromText

文本转换为地理几何类型函数 ST_GeographyFromText

计算距离,单位是m的方法

-- 921.37629155
select ST_Distance(ST_GeographyFromText('SRID=4326;POINT(114.017299 22.537126)'), ST_GeographyFromText('SRID=4326;POINT(114.025919 22.534866)'));
-- 921.37629155
SELECT ST_Distance(ST_GeomFromText('POINT(114.017299 22.537126)',4326):: geography,ST_GeomFromText('POINT(114.025919 22.534866)', 4326):: geography);-- 920.28519
SELECT ST_DistanceSphere(ST_GeomFromText('POINT(114.017299 22.537126)',4326),ST_GeomFromText('POINT(114.025919 22.534866)', 4326));-- unit=m   26986  马萨诸塞州 投影平面坐标系 单位m  result=972.989337453172
SELECT ST_Distance(ST_Transform(ST_GeomFromText('POINT(114.017299 22.537126)',4326 ),26986),ST_Transform(ST_GeomFromText('POINT(114.025919 22.534866)', 4326 ),26986));

在这里插入图片描述

SELECT ST_Distance(ST_GeomFromText(‘POINT(114.017299 22.537126)’,4326),
ST_GeomFromText(‘POINT(114.025919 22.534866)’, 4326)
);

在这里插入图片描述

附近5公里内的前50个点

# 计算两个点是否在给定距离内# 单位米m
SELECT ST_DWithin(
ST_GeographyFromText('SRID=4326;POINT(114.017299 22.537126)'), 
ST_GeographyFromText('SRID=4326;POINT(114.025919 22.534866)'), 
1000);# 单位度degrees
SELECT ST_DWithin(
ST_GeomFromText('POINT(114.017299 22.537126)',4326), 
ST_GeomFromText('POINT(114.025919 22.534866)', 4326), 
0.00811134108875483);-- 查找给定经纬度5km以内的点
SELECT	usid,longitude,latitude,ST_DistanceSphere (address_geom_point,ST_GeomFromText('POINT(116.281524,39.957202)',4326 )) distance 
FROM kx_store WHERE	ST_DWithin ( geom_point :: geography, ST_GeomFromText ( 'POINT(116.281524,39.957202)', 4326 ) :: geography, 5000 ) IS TRUE order by distance descLIMIT 50;

通过指定类型geom_point :: geography,单位变成米, 否则默认距离单位是度
在这里插入图片描述

在这里插入图片描述

最近的30个点店

SELECT * FROM kx_store ORDER BY address_geom_point <-> ST_GeomFromText ( 'POINT(116.281524,39.957202)', 4326 )  LIMIT 30;

计算gps附近50m内的围栏

使用函数ST_DWithin 判断一个几何对象是否在另一个的r距离以内:

SELECTST_Distance(ST_GeomFromText('POINT(120.731069 30.758984)',4326):: geography,geom_fence :: geography) AS distance, id, name
FROM	kx_store 
WHEREST_DWithin (geom_fence :: geography,ST_GeomFromText ( 'POINT( 120.731069 30.758984)', 4326 ) :: geography,50 ) ORDER BY distance LIMIT 30;

使用函数boolean ST_Within(geometry A, geometry B); 判断A是否完全在B内部

SELECTid, nameFROMbasic_mall_v1WHEREST_Within (ST_GeomFromText('POINT('|| #{longitude} ||' '|| #{latitude} ||')',4326),geom_fence)

这篇关于PostgreSQL 地图两点之间,经纬度距离计算, PostGIS方式计算, 电子围栏功能实现;PostgreSQL空间数据存储扩展 PostGIS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/723913

相关文章

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

PyCharm接入DeepSeek实现AI编程的操作流程

《PyCharm接入DeepSeek实现AI编程的操作流程》DeepSeek是一家专注于人工智能技术研发的公司,致力于开发高性能、低成本的AI模型,接下来,我们把DeepSeek接入到PyCharm中... 目录引言效果演示创建API key在PyCharm中下载Continue插件配置Continue引言

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

基于Go语言实现一个压测工具

《基于Go语言实现一个压测工具》这篇文章主要为大家详细介绍了基于Go语言实现一个简单的压测工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录整体架构通用数据处理模块Http请求响应数据处理Curl参数解析处理客户端模块Http客户端处理Grpc客户端处理Websocket客户端

Java CompletableFuture如何实现超时功能

《JavaCompletableFuture如何实现超时功能》:本文主要介绍实现超时功能的基本思路以及CompletableFuture(之后简称CF)是如何通过代码实现超时功能的,需要的... 目录基本思路CompletableFuture 的实现1. 基本实现流程2. 静态条件分析3. 内存泄露 bug

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.