您的位置:首页 > 数据库

PostgreSQL9.6+PostGIS2.3学习笔记(二)常用函数以及JDBC连接

2016-12-18 15:42 791 查看

一、PostGIS常用函数

/* 基础查询函数 */

– 查询PostGIS版本

select PostGIS_Full_Version();

– 获取几何对象的WKT描述

select ST_AsText(geom) from public.tra_ln where gid =1;

– 获取几何对象的WKB描述

select ST_AsBinary(geom) from public.tra_ln where gid =1;

– 获取几何对象的SRID

select ST_SRID(geom) from public.tra_ln where gid =1;

– 获取当前几何对象类型

select GeometryType(geom) from public.res_py where gid =1;

/* 几何空间数据关系函数 */

– 判断两个几何对象是否一样 一样返回true

select ST_Equals((select geom from public.res_py where gid =1 ),(select geom from public.res_py where gid =2 )) ;

– 判断两个几何对象是否相连 不相连返回true

select ST_Disjoint((select geom from public.tra_ln where gid =1 ),(select geom from public.tra_ln where gid =2 ));

– 判断两个几何对象是否相交 相交返回true

select ST_Intersects((select geom from public.tra_ln where gid =1 ),(select geom from public.tra_ln where gid =2 ));

– 判断两个几何对象是否交叉 交叉返回true

select ST_Crosses((select geom from public.res_py where gid =1 ),(select geom from public.res_py where gid =2 ));

– 判断两个几何对象是否接触 接触返回true

select ST_Touches((select geom from public.res_py where gid =1 ),(select geom from public.res_py where gid =2 ));

– 判断一个几何对象A是否存在另一个几何对象B中 ST_Within(A,B) 存在返回true

– 判断一个几何对象A是否包含另一个几何对象B中 ST_Contains(A,B) 与上面的ST_Withinleisi

select ST_Within((select geom from public.res_py where gid =1 ),(select geom from public.res_py where gid =2 ));

– 如果几何对象B的所有点都在几何对象A中 ST_Covers(A,B) 则返回true

– 如果几何对象A的所有点都在几何对象B中 ST_CoveredBy(A,B) 则返回true

select ST_Covers((select geom from public.res_py where gid =1 ),(select geom from public.res_py where gid =1 ));

/* 几何空间数据处理函数 */

/* 单位的换算关系如下:

1英里= 63360 米

1米=1/1852 海里

1海里= 1/60度

对于geometry类型的测量函数返回的是笛卡尔距离,这种计算方式是无意义的,

因为它计算的是两者之间的平面距离。实际上这两者之间的距离的单位也许叫做“度“,但这个结果并不对应两个点之间的

真实角度差,所以称为”度”甚至是不准确的。

对GEOGRAPHY类型的查询和测量函数使用的单位都是米,因此距离参数应该用米表示,返回值也应该是米(对于面积来说

是平方米)

*/

– 返回该几何对象的中心点 返回值为point

select ST_AsText(ST_Centroid( geom ))from public.tra_ln where gid = 1 ;

– 计算两个几何对象的距离

select ST_Distance(Geography((select geom from public.tra_ln where gid =1)),Geography((select geom from public.tra_ln where gid = 100)) );

– 计算一个几何对象长度

select ST_Length(Geography(geom)) from public.tra_ln where gid =1;

– 计算几何对象面积

select ST_Area(Geography(geom)) from public.res_py where gid = 1;

– 一定在几何空间线数据上的点,返回一个数据点

select ST_AsText(ST_PointOnSurface(geom)) from public.tra_ln where gid = 1;

– 根据原有几何对象以及参数形成一个新的几何对象数据,获取缓冲后的几何对象,第二个参数单位为度

select ST_AsText(ST_Buffer(geom,0.0044996400287977,7)) from public.tra_ln where gid = 1;

– 可以返回mbr(空间最小外包矩形),传入参数可以是point line polygon

select ST_AsText(ST_Envelope(geom)) from public.tra_ln where gid = 1;

– 返回一个合并的几何空间数据,将两个几何空间数据合并为一个几何空间数据

select ST_AsText(ST_Union((select geom from public.tra_ln where gid =1),(select geom from public.tra_ln where gid = 100)));

– 获取一个几何对象边界(传入参数为线返回端点,传入面返回边界线)

select ST_AsText(ST_Boundary(geom)) from public.res_py where gid =1 ;

– 创建表

create table public.test_table (

gid int4,

geom geometry

);

– ST_BdPolyFromText — 根据一个任意的封闭的WKT描述的MultiLineString几何类型对象创建一个Polygon对象

– ST_BdPolyFromText(text WKT, integer srid);

insert into public.test_table(gid,geom) values(1,ST_BdPolyFromText(‘MULTILINESTRING((120.239152589 30.3085777040001,120.239191934 30.3082752960001))’,4326));

– ST_BdMPolyFromText — 根据一个任意的封闭的WKT描述的MultiLineString几何类型对象创建一个MultiPolygon对象.

– ST_BdMPolyFromText(text WKT, integer srid);

– ST_GeogFromText /ST_GeographyFromText 从一个WKT规范描述的对象返回一个具体的geography对象

– ST_GeogFromText/ST_GeographyFromText (text EWKT);

select ST_GeogFromText(‘MULTILINESTRING((120.239152589 30.3085777040001,120.239191934 30.3082752960001))’)

– ST_GeogFromWKB —从一个WKB或EWKB规范描述的对象返回一个具体的geography对象

– ST_GeogFromWKB(bytea geom);

– ST_GeomFromEWKB —从一个EWKB描述的几何对象返回一个具体的ST_Geometry值.

– ST_GeomFromEWKB(bytea EWKB);

– ST_GeomFromEWKT — 从一个EWKT描述的几何对象返回一个具体的ST_Geometry值.

– ST_GeomFromEWKT(text EWKT);

– ST_GeometryFromText 根据WKT描述的对象返回一个具体的ST_Geometry 函数值(也是一个geometry对象),该函数是

– ST_GeomFromText的别名,即两者等价

– ST_GeomFromGeoJSON —该函数根据一个geojson描述的几何对象,生成一个PostGIS 的geometry对象

– ST_GeomFromGeoJSON(text geomjson);

– ST_GeomFromKML —该函数根据一个KML描述的几何对象,生成一个PostGIS 的geometry对象

– ST_GeomFromKML(text geomkml);

– ST_GeomFromText — 根据WKT描述返回一个具体的ST_Geometry 值

– ST_GeomFromText(text WKT);/ST_GeomFromText(text WKT, integer srid);

– ST_LineFromMultiPoint —从一个MultiPoint几何类型中返回一个LineString类型对象

– ST_LineFromMultiPoint(geometry aMultiPoint)

– ST_MakeLine — 根据point或line几何类型创建Linestring类型对象

– ST_MakeLine(geometry set geoms); 一种是聚集函数,它用一排point或line几何类型生成一个linestring几何对象

– ST_MakeLine(geometry geom1, geometry geom2); 一种是用一个数组的point或line生成一个linestring对象

– ST_MakeLine(geometry[] geoms_array); 一种函数是用两个point或linestring类型生成一个linestring几何类型对象

– ST_MakeEnvelope 根据给定的最小值范围和最大值范围生成一个矩形,输入值必须是SRS(spatial_reference_system表)规定的SRID值

– ST_MakeEnvelope(double precision xmin, double precision ymin, double precision xmax, double precision ymax,integer srid=unknown);

– ST_MakePolygon 根据给定的闭合的LineString类型生成一个多边形,输入的几何类型必须是封闭的曲线

– ST_MakePoint — 创建一个2D,3DZ or 4D point 几何类型. 注意x是经度,而y是维度

– 2D ST_MakePoint(double precision x, double precision y);

– 3D ST_MakePoint(double precision x, double precision y, double precision z);

– 4D ST_MakePoint(double precision x, double precision y, double precision z, double precision m);

– ST_MakePointM — 使用x,y,m坐标创建一个point 几何类型对象.

– ST_MakePointM(float x, float y, float m);

– ST_MLineFromText — 根据WKT表述的几何对象返回ST_MultiLineString值

– ST_MLineFromText(text WKT, integer srid);

– ST_Point — 根据给定的坐标值,返回ST_Point值对应的几何类型对象,这个函数是OGC 函数ST_MakePoint 的别名

– ST_Point(float x_lon, float y_lat);

– ST_PointFromText —根据WKT表述和给定的SRID创建一个geometry几何类型对象,如果SRID没有给定默认设置为0,即未知

– ST_PointFromText(text WKT, integer srid);

– ST_Polygon — 根据具体的linestring类型对象和SRID创建一个polygon对象

– ST_Polygon(geometry aLineString, integer srid);

– ST_ExteriorRing — 返回一个POLYGON 几何类型的外环,如果输入类型不是POLYGON类型,返回NULL值,该函数不支持MULTIPOLYGON

– ST_ExteriorRing(geometry a_polygon);

– ST_IsRing —如果LINESTRING是简单、闭合的,则返回TRUE

– ST_IsRing(geometry g);

– ST_NPoints — 返回geometry的顶点个数.

– ST_NPoints(geometry g1);

– ST_Summary —返回geometry对象的文本概要

– ST_Summary(geometry/geography g);

– ST_X — 返回点的X坐标,如果输入参数不是一个点,返回NULL,输入必须是一个点类型 ST_X(geometry a_point);

– ST_Y — 返回输入点的Y坐标,如果输入不是点,返回NULL,输入必须是点类型 ST_Y(geometry a_point);

– ST_AddPoint — 在LINESTRING对象的某个点的位置之前添加一个点(点的位置计数从0开始)

– ST_LineMerge — 把一些LineString对象组合在一起,形成一个MULTILINESTRING对象

– ST_LineMerge(geometry amultilinestring);

– ST_RemovePoint — 从一个LINESTRING对象中移除一个Point点,下标从0开始

– ST_RemovePoint(geometry linestring, integer offset);

– ST_Rotate — 返回一个几何对象以某个点为中心点,逆时针旋转指定弧度后的对象.

以上只是一部分比较常用分析的函数,不是很全。官方文档地址:

PostGIS官方文档

二、JDBC连接数据库

废话不多说,先上代码:

1.ConnUtil.java :获取数据库连接类

package com.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnUtil  {
final private static  String driverClass="org.postgresql.Driver";// 驱动类
final private static String url="jdbc:postgresql://localhost:5432/onlyTest";// 数据库地址
final private static String username="postgres";// 用户名
final private static String password="postgre";// 密码
public String getDriverClass() {
return driverClass;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public static Connection getConn() {
Connection conn = null ;
try {
Class.forName(driverClass);
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Connection conn = ConnUtil.getConn();
if(conn!=null){
System.out.println("数据库连接成功!");
}
}
}


2.TestJdbc.java :写SQL查询数据等操作

package com.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.postgresql.geometric.*;
public class TestJdbc {

/** 根据gid获取对应表中的geom 的WKT信息
* @param gid  主键
* @param tablename 表名
* @return WKT描述的几何对象
*/
public String getWKTByGid(int gid,String tablename){
Connection conn = ConnUtil.getConn();
Statement stmt = null;
ResultSet rs = null;
String wktString = null;
String sql = "select ST_AsText(geom) from public.res_ln where gid=1 ";
if(conn!=null){
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){
wktString =  rs.getString(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return wktString ;
}
public static void main(String[] args) {
TestJdbc tj = new TestJdbc();
System.out.println(tj.getWKTByGid(1, "public.res_ln"));
}
}


注意:目前官方最新的驱动是9.4版本的,目前连接9.6版本也是可以的。下载方式有两种:

官网下载:下载页面地址



根据自己对应JDK版本下载对应版本的驱动。

通过安装的Postgresql中的 “Application Stack Bulider ”进行下载:(以9.6版本为例)

2.1. 开始菜单的所有程序中找到Postgresql,展开后点击 Application Stack Bulider



2.2.



2.3.



2.4.



点击下一个后就会进入下载,等待下载完成后到对应文件夹中找到文件,即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  postgresql 函数 jdbc