天天看點

PostgreSQL9.6, PostGIS2.3 常用函數

一、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 ));

– 傳回該幾何對象的中心點 傳回值為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 — 傳回一個幾何對象以某個點為中心點,逆時針旋轉指定弧度後的對象.

以上隻是一部分比較常用分析的函數,不是很全。官方文檔位址: