char_length(string) —— 返回字符串中的字符数
avg(expression) —— 返回一个数值列的平均值的PostgreSQL聚合函数
stddev(expression) —— 返回输入值的标准差的PostgreSQL聚合函数
count(expression) —— 返回一个列的记录数的PostgreSQL聚合函数
sum(expression) —— 返回一个数值列的和的PostgreSQL聚合函数
ST_GeometryType(geometry) —— 返回几何图形的类型
ST_NDims(geometry) —— 返回几何图形的维数
ST_SRID(geometry) —— 返回几何图形的空间参考标识码
针对点的一些特定空间函数包括:
ST_X(geometry) —— 返回X坐标
ST_Y(geometry) —— 返回Y坐标
ST_Z(geometry) —— 返回Z坐标
ST_M(geometry) —— 返回M信息
转换为字符串数据格式
ST_AsText(geometry)
用于处理线串的一些特定空间函数包括
ST_Length(geometry) —— 返回线串的长度
ST_StartPoint(geometry) —— 将线串的第一个坐标作为点返回
ST_EndPoint(geometry) —— 将线串的最后一个坐标作为点返回
ST_NPoints(geometry) —— 返回线串的坐标数量
对于geography类型,只有相关的少量空间函数:
ST_AsText(geography) returns text
ST_GeographyFromText(text) returns geography
ST_AsBinary(geography) returns bytea
ST_GeogFromWKB(bytea) returns geography
ST_AsSVG(geography) returns text
ST_AsGML(geography) returns text
ST_AsKML(geography) returns text
ST_AsGeoJson(geography) returns text
ST_Distance(geography, geography) returns double
ST_DWithin(geography, geography, float8) returns boolean
ST_Area(geography) returns double
ST_Length(geography) returns double
ST_Covers(geography, geography) returns boolean
ST_CoveredBy(geography, geography) returns boolean
ST_Intersects(geography, geography) returns boolean
ST_Buffer(geography, float8) returns geography
ST_Intersection(geography, geography) returns geography
将Point转换成geometry类型
字段::GEOMETRY
–转换成geojson
st_asgeojson
关于多边形图形的一些特定空间函数包括:
ST_Area(geometry) —— 返回多边形的面积
ST_NRings(geometry) —— 返回多边形中环的数量(通常为1个,其他是孔)
ST_ExteriorRing(geometry) —— 以线串的形式返回多边形最外面的环
ST_InteriorRingN(geometry, n) —— 以线串形式返回指定的内部环
ST_Perimeter(geometry) —— 返回所有环的长度
有四种图形集合(Collection)类型,它们将多个简单几何图形组合为图形集合:
MultiPoint —— 点集合
MultiLineString —— 线串集合
MultiPolygon —— 多边形集合
GeometryCollection —— 由任意几何图形(包括其他GeometryCollection)组成的异构集合
用于处理图形集合的一些特定空间函数:
ST_NumGeometries(geometry) —— 返回集合中的组成部分的数量
ST_GeometryN(geometry, n) —— 返回集合中指定的组成部分
ST_Area(geometry) —— 返回集合中所有多边形组成部分的总面积
ST_Length(geometry) —— 返回所有线段组成部分的总长度
几何图形输入和输出
①Well-known text(WKT)
ST_GeomFromText(text, srid) —— 返回geometry
ST_AsText(geometry) —— 返回text
ST_AsEWKT(geometry) —— 返回text
②Well-known binary(WKB)
ST_GeomFromWKB(bytea) —— 返回geometry
ST_AsBinary(geometry) —— 返回bytea
ST_AsEWKB(geometry) —— 返回bytea
③Geographic Mark-up Language(GML)
ST_GeomFromGML(text) —— 返回geometry
ST_ASGML(geometry) —— 返回text
④Keyhole Mark-up Language(KML)
ST_GeomFromKML(text) —— 返回geometry
ST_ASKML(geometry) —— 返回text
⑤GeoJson
ST_AsGeoJSON(geometry) —— 返回text
⑥Scalable Vector Graphics(SVG)
ST_AsSVG(geometry) —— 返回text
SELECT ST_Distance(ST_GeographyFromText(‘POINT(0 0)’,4326),
ST_GeographyFromText(ST_AsText(geom),4326)) FROM “geometries”
SELECT ST_Distance(
ST_GeometryFromText(‘POINT(0 0)’, 4326),
ST_GeometryFromText(ST_AsText(geom), 4326)
) FROM “geometries”;
SELECT streets.gid, streets.name
FROM
nyc_streets streets,
nyc_subway_stations subways
WHERE subways.name = ‘Broad St’
AND streets.geom && ST_Expand(subways.geom, 200) – Magic number: 200m
ORDER BY ST_Distance(streets.geom, subways.geom) ASC
LIMIT 1;
insert INTO geometries(name,geom) values(‘nanjing’, ‘SRID=4326;POINT(-95.363151 29.763374)’)
insert into “geometries”(name,geom) values(‘123’,ST_GeometryFromText ( ST_AsText ( ‘POINT(0 6)’ ), 4326 ))
insert into “geometries”(name,geom) values(‘1232’,ST_GeometryFromText ( ST_AsText ( ‘LINESTRING(0 0, 1 1, 2 1, 2 2)’ ), 4326 ))
insert into “geometries”(name,geom) values(‘1232’,ST_GeometryFromText ( ST_AsText ( ‘POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))’ ), 4326 ))
insert into “geometries”(name,geom) values(‘1232’,ST_GeometryFromText ( ST_AsText ( ‘POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))’ ), 4326 ))
insert into “geometries”(name,geom) values(‘1232’,ST_GeometryFromText ( ST_AsText ( ‘GEOMETRYCOLLECTION(POINT(2 0), POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))’ ), 4326 ))
insert into “geometries”(name,geom) values(‘123’,‘SRID=4326;POINT(0 6)’)
insert into “geometries”(name,geom) values(‘1232’,‘SRID=4326;LINESTRING(0 0, 1 1, 2 1, 2 2)’)
insert into “geometries”(name,geom) values(‘1232’,‘SRID=4326;POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))’)
insert into “geometries”(name,geom) values(‘1232’,‘SRID=4326;POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))’)
insert into “geometries”(name,geom) values(‘1232’,‘SRID=4326;GEOMETRYCOLLECTION(POINT(2 0), POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))’)
update “geometries” set line = GeomFromText(‘LINESTRING(0 0,0 0)’, 4326)
update geometries set geom =ST_SetSRID(geom, 4326)
– 创建几何对象的空间参考ID,更新成0
update t_noisemap set geometry =ST_SetSRID(geometry, 0)
– 查询几何对象的空间参考ID
select st_srid(geometry) from t_noisemap
– 获取几何对象的空间几何描述
select ST_Astext(geometry) from t_noisemap where id=2
–获得点的X坐标
select ST_X(ST_AsText(points::GEOMETRY)) from “geometries”
host:10.10.162.132
port:5432
initial database:postgres
initial database:postgres
password:Jsepc01!
insert into ftest(id,geom) values(20,
ST_GeomFromEWKT(‘SRID=4326;TRIANGLE ((42 36,56 56,22 22,42 36))’)
)
建立空间数据库索引
create index geom_index on ftest using gist (geom)
弧度
insert into ftest (id,geom) values (22,
ST_GeomFromEWKT(‘SRID=4326;CIRCULARSTRING(116 62,116 56,116 62)’)
)
SELECT st_srid(geom) FROM road limit 1;