PostgreSQL && PostGIS


數據庫為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')
)
)
);

參考資料

ST_Envelope

獲得SRID

SELECT Find_SRID('table_schema', 'tableName', 'geom');

參考資料

Find_SRID

查詢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中查詢


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM