SELECT valid, reason, ST_AsText(location) AS location
FROM ST_IsValidDetail('POLYGON ((20 20, 120 190, 50 190, 170 50, 20 20))') AS t;
valid | reason | location
-------+-------------------+---------------------------------------------
f | Self-intersection | POINT(91.51162790697674 141.56976744186045)
修复图形
ST_MakeValid 修复图形
几何图形列信息存储在geometry_columns视图中,地理图形列信息存储在geography_columns视图中
EPSG database存储了常用的SRS系统,postgis使用srid来定义坐标系统,几何通过srid值与空间参考系相关联;可以使用ST_SRID函数查看表的srid值;空间参考信息存储在spatial_ref_sys表中
常用srid 4490 GCS2000地理坐标系
4528 GCS2000 Zone40坐标系
postgis官方加载空间数据分两种方式:
8.1 使用SQL加载数据
BEGIN;
INSERT INTO roads (road_id, roads_geom, road_name)
VALUES (1,'LINESTRING(191232 243118,191108 243242)','Jeff Rd');
INSERT INTO roads (road_id, roads_geom, road_name)
VALUES (2,'LINESTRING(189141 244158,189265 244817)','Geordie Rd');
INSERT INTO roads (road_id, roads_geom, road_name)
VALUES (3,'LINESTRING(192783 228138,192612 229814)','Paul St');
INSERT INTO roads (road_id, roads_geom, road_name)
VALUES (4,'LINESTRING(189412 252431,189631 259122)','Graeme Ave');
INSERT INTO roads (road_id, roads_geom, road_name)
VALUES (5,'LINESTRING(190131 224148,190871 228134)','Phil Tce');
INSERT INTO roads (road_id, roads_geom, road_name)
VALUES (6,'LINESTRING(198231 263418,198213 268322)','Dave Cres');
COMMIT;
8.2 使用形状文件加载器shp2pgsql
(1)
# shp2pgsql -c -D -s 4269 -i -I shaperoads.shp myschema.roadstable > roads.sql
# psql -d roadsdb -f roads.sql
(2)PgAdminIII shp2pgsql-gui工具
CREATE INDEX [indexname] ON [tablename] USING GIST ( [geometryfield] );
上述语法将始终构建 2D 索引。若要获取几何类型的 n 维索引,可以使用以下语法创建一个索引:
CREATE INDEX [indexname] ON [tablename] USING GIST ([geometryfield] gist_geometry_ops_nd);
构建空间索引是一项计算密集型工作。它还会在创建表时阻止对表的写入访问,因此在生产系统上,您可能希望以较慢的并发感知方式执行:
CREATE INDEX CONCURRENTLY [indexname] ON [tablename] USING GIST ( [geometryfield] );
构建索引后,有时强制 PostgreSQL 收集 表统计信息,用于优化查询计划:
VACUUM ANALYZE [table_name] [(column_name)];