一、需求分析
有这么一个需求,就是给一个经纬度点。判断是否在海洋。如果在海洋中计算经纬度点到最近的海岸线或者海岛的距离。
之前是全球海岸线的shp文件,后来同事帮忙分离成了经纬度的点,我导入到mysql 中然后写了一个算法去计算。一般都需要10分钟吧。感觉效率很低。
后来天地图的哥们推荐说可以用postgresql,我就尝试了一下2秒就算出来了结果。惊呆了!!!故此记录一下使用过程。
二、安装postgresql
1、安装postgresql,这个就是按照文档来的https://www.runoob.com/postgresql/windows-install-postgresql.html
2、创建表格(测试)
CREATE TABLE weather (
city varchar(80),
temp_lo int, -- 最低温度
temp_hi int, -- 最高温度
prcp real, -- 湿度
date date
);
3、插入数据
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
4、查询数据
SELECT * FROM weather;
三、安装PostGIS
1、下载安装包然后安装就行,参考这个资料https://www.cnblogs.com/zhaoyanhaoBlog/p/10611425.html
2、数据库开启PostGIS扩展
-- Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
-- Enable Topology
CREATE EXTENSION postgis_topology;
-- Enable PostGIS Advanced 3D
-- and other geoprocessing algorithms
-- sfcgal not available with all distributions
CREATE EXTENSION postgis_sfcg