數據庫為PostGIS
,使用引擎為npgsql
官方資料
PostgreSQL
PostGIS
postgis的空間查詢文檔
npgsql
三者關系
PostGIS
是基於PostgreSQL
的空間數據管理的擴展,npgsql
是基於C#的PostgreSQL的數據引擎
查詢字段信息
numeric_scale是小數位
SELECT a.attnum,
a.attname AS field,
t.typname AS type,
a.attlen AS length,
a.atttypmod AS lengthvar,
a.attnotnull AS notnull,
b.description AS comment,
CASE atttypid
WHEN 21 /*int2*/ THEN 16
WHEN 23 /*int4*/ THEN 32
WHEN 20 /*int8*/ THEN 64
WHEN 1700 /*numeric*/ THEN
CASE WHEN atttypmod = -1
THEN null
ELSE((atttypmod - 4) >> 16) & 65535-- calculate the precision
END
WHEN 700 /*float4*/ THEN 24 /*FLT_MANT_DIG*/
WHEN 701 /*float8*/ THEN 53 /*DBL_MANT_DIG*/
ELSE null
END AS numeric_precision,
CASE
WHEN atttypid IN(21, 23, 20) THEN 0
WHEN atttypid IN(1700) THEN
CASE
WHEN atttypmod = -1 THEN null
ELSE(atttypmod - 4) & 65535-- calculate the scale
END
ELSE null
END AS numeric_scale
FROM pg_class c,
pg_attribute a
LEFT OUTER JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid,
pg_type t
WHERE c.relname = 'tableName'
and a.attnum > 0
and a.attrelid = c.oid
and a.atttypid = t.oid
ORDER BY a.attnum;
參考資料
PostgreSQL查詢表名稱及表結構
Where are NUMERIC precision and scale for a field found in the pg_catalog tables?
查詢空間表基本信息
select f_table_catalog,f_table_schema, f_table_name,f_geometry_column,"type" from geometry_columns
f_table_name是表名,f_geometry_column是空間字段名,type是空間類型
參考資料
Selecting only Spatial tables from PostgreSQL database?
查詢整表的范圍Extent
SELECT ST_Extent(geom)::varchar as table_extent FROM tableName;
返回結果
BOX(35307504.5269217 3330334.18584413,35490036.5589796 3480141.43829077)
這里將box2d
對象轉換為字符串后,自行處理(比如正則表達式)
如果想得到geometry
對象,則可以使用下面的語句
SELECT ST_AsText(ST_Extent(geom)) as table_extent FROM tableName;
SELECT ST_AsText(ST_SetSRID(ST_Extent(geom),srid)) as table_extent FROM tableName;
參考資料
Bounding box for PostGIS table
ST_Extent
box2d
查詢單條記錄的范圍Extent
SELECT ST_AsText(
ST_Envelope(
ST_GeomFromText(
(select ST_AsText(geom) as "wkt" from tableName where gid = '1')
)
)
);
參考資料
獲得SRID
SELECT Find_SRID('table_schema', 'tableName', 'geom');
參考資料
查詢geometry type
SELECT type
FROM geometry_columns
WHERE f_table_schema = 'tableSchema'
AND f_table_name = 'tableName'
and f_geometry_column = 'geom';
參考資料
ST_GeometryType
How to get the geometry type of an empty PostGIS table?
空間查詢
這里展示表空間字段和目標Geometry
求空間關系
select * from tableName where ST_Intersects(geom,'SRID=4523;POINT(0 0)'::geometry)=true
結果為true
表示查詢符合條件的數據,為false
為查詢不符合條件的數據
注意需保證二者SRID一致,否則無法查詢
空間關系列舉
ST_Contains
ST_Intersects
ST_Crosses
ST_Touches
ST_DWithin
ST_Intersection
注意
以上的geom
是指空間字段,不一定是"geom"這個字符串
空間字段的實際名稱可以在geometry_columns
中查詢