postgresql安裝、環境配置及導入shp


轉載自:

https://blog.csdn.net/zhangshuanlai/article/details/84100165

目錄

 

說明

開發這么久了一直沒時間記錄一下自己學習的點點滴滴。今天開始吧。

下載postgresql

網址:https://www.postgresql.org/
本次以下載10.6步驟講解
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

在這里插入圖片描述

安裝postgresql

安裝的步驟很簡單,一直下一步就行了,在安裝結束時會讓安裝擴展項,可以參照下圖安裝,也可以根據自己的需要安裝。
在這里插入圖片描述

手動添加環境變量

一般需要將postgresql的bin加入環境變量中,方便以后使用方便。在這里插入圖片描述

設置外網訪問

1、在data文件夾下找到pg_hba.conf

# TYPE DATABASE USER ADDRESS METHOD # IPv4 local connections: host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::1/128 md5 # Allow replication connections from localhost, by a user with the # replication privilege. #host replication postgres 127.0.0.1/32 md5 #host replication postgres ::1/128 md5 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

建立空間數據庫

1、 登錄postgres用戶,建立空的數據庫,指定數據的所有者是postgres用戶(或其他可用用戶)。

在這里插入圖片描述
創建數據庫 CREATE DATABASE shp2pgsqldemo WITH OWNER=postgres;

1、切換到shp2pgsqldemo數據庫,並安裝PostGIS相關擴展

\c shp2pgsqldemo
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;

導入shp

1、直接導入shp

使用bat導入shp,雙擊運行,如果不成功,請檢查是否環境變量加上。
shp2pgsql -s 4544 -c -W “GBK” DJQ5120812018.shp public.DJQ5120812018 | psql -d shp2pgsqldemo -U postgres -W
在這里插入圖片描述

2、shp轉換sql文件導入

2.1先將shp轉換為sql文件

依舊是利用bat轉換
shp2pgsql -s 4544 -c -W “GBK” DJQ5120812018.shp>DJQ5120812018.sql
在這里插入圖片描述

2.2先將sql導入

psql -d shp2pgsqldemo -U postgres -f DJQ5120812018.sql -W
在這里插入圖片描述

導入過程中的問題總結

出現亂碼
需要看編碼gbk是否有問題,更換其他編碼
操作失誤
坐標SRID需要自己找到對應的
常規可以在arcgis中查看,arcgis中的WKID及postgresql中的SRID。 在這里插入圖片描述

在postgresql中操作geometry方法

1.定義幾何對象格式:

POINT(0 0) ——點
LINESTRING(0 0,1 1,1 2) ——線
POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)) ——面
MULTIPOINT(0 0,1 2) ——多點
MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4)) ——多線
MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1))) ——多面
GEOMETRYCOLLECTION(POINT(2 3),LINESTRING((2 3,3 4))) ——幾何集合

2.常用函數:

wkt轉geometry st_geomfromtext(wkt,wkid)
geometry轉wkt st_astext(geom)
獲取點對象x、y坐標值 st_x(geom)、st_y(geom)
獲取線/面對象四至 st_xmin(geom)、st_ymin(geom)、st_xmax(geom)、st_ymax(geom)
計算兩點之間距離 st_distance(geom,geom) / st_distance(wkt,wkt)
計算線的長度 st_length(geom) / st_length(wkt)
計算面積 st_area(geom) / st_area(wkt)
緩沖區計算 st_buffer(geom,distance) / st_buffer(wkt,distance)

3.管理函數:

添加幾何字段 AddGeometryColumn(, , , , , )
刪除幾何字段 DropGeometryColumn(, , )
檢查數據庫幾何字段並在geometry_columns中歸檔 Probe_Geometry_Columns()
給幾何對象設置空間參考(在通過一個范圍做空間查詢時常用) ST_SetSRID(geometry, integer)

4.幾何對象關系函數 :

獲取兩個幾何對象間的距離 ST_Distance(geometry, geometry)
如果兩個幾何對象間距離在給定值范圍內,則返回TRUE ST_DWithin(geometry, geometry, float)
判斷兩個幾何對象是否相等(比如LINESTRING(0 0, 2 2)和LINESTRING(0 0, 1 1, 2 2)是相同的幾何對象) ST_Equals(geometry, geometry)
判斷兩個幾何對象是否分離 ST_Disjoint(geometry, geometry)
判斷兩個幾何對象是否相交 ST_Intersects(geometry, geometry)
判斷兩個幾何對象的邊緣是否接觸 ST_Touches(geometry, geometry)
判斷兩個幾何對象是否互相穿過 ST_Crosses(geometry, geometry)
判斷A是否被B包含 ST_Within(geometry A, geometry B)
判斷兩個幾何對象是否是重疊 ST_Overlaps(geometry, geometry)
判斷A是否包含B ST_Contains(geometry A, geometry B)
判斷A是否覆蓋 B ST_Covers(geometry A, geometry B)
判斷A是否被B所覆蓋 ST_CoveredBy(geometry A, geometry B)
通過DE-9IM 矩陣判斷兩個幾何對象的關系是否成立 ST_Relate(geometry, geometry, intersectionPatternMatrix)
獲得兩個幾何對象的關系(DE-9IM矩陣) ST_Relate(geometry, geometry)

5.幾何對象處理函數:

獲取幾何對象的中心 ST_Centroid(geometry)
面積量測 ST_Area(geometry)
長度量測 ST_Length(geometry)
返回曲面上的一個點 ST_PointOnSurface(geometry)
獲取邊界 ST_Boundary(geometry)
獲取緩沖后的幾何對象 ST_Buffer(geometry, double, [integer])
獲取多幾何對象的外接對象 ST_ConvexHull(geometry)
獲取兩個幾何對象相交的部分 ST_Intersection(geometry, geometry)
將經度小於0的值加360使所有經度值在0-360間 ST_Shift_Longitude(geometry)
獲取兩個幾何對象不相交的部分(A、B可互換) ST_SymDifference(geometry A, geometry B)
從A去除和B相交的部分后返回 ST_Difference(geometry A, geometry B)
返回兩個幾何對象的合並結果 ST_Union(geometry, geometry)
返回一系列幾何對象的合並結果 ST_Union(geometry set)
用較少的內存和較長的時間完成合並操作,結果和ST_Union相同 ST_MemUnion(geometry set)

6.幾何對象存取函數:

獲取幾何對象的WKT描述 ST_AsText(geometry)
獲取幾何對象的WKB描述 ST_AsBinary(geometry)
獲取幾何對象的空間參考ID ST_SRID(geometry)
獲取幾何對象的維數 ST_Dimension(geometry)
獲取幾何對象的邊界范圍 ST_Envelope(geometry)
判斷幾何對象是否為空 ST_IsEmpty(geometry)
判斷幾何對象是否不包含特殊點(比如自相交) ST_IsSimple(geometry)
判斷幾何對象是否閉合 ST_IsClosed(geometry)
判斷曲線是否閉合並且不包含特殊點 ST_IsRing(geometry)
獲取多幾何對象中的對象個數 ST_NumGeometries(geometry)
獲取多幾何對象中第N個對象 ST_GeometryN(geometry,int)
獲取幾何對象中的點個數 ST_NumPoints(geometry)
獲取幾何對象的第N個點 ST_PointN(geometry,integer)
獲取多邊形的外邊緣 ST_ExteriorRing(geometry)
獲取多邊形內邊界個數 ST_NumInteriorRings(geometry)
同上 ST_NumInteriorRing(geometry)
獲取多邊形的第N個內邊界 ST_InteriorRingN(geometry,integer)
獲取線的終點 ST_EndPoint(geometry)
獲取線的起始點 ST_StartPoint(geometry)
獲取幾何對象的類型 GeometryType(geometry)
類似上,但是不檢查M值,即POINTM對象會被判斷為point ST_GeometryType(geometry)
獲取點的X坐標 ST_X(geometry)
獲取點的Y坐標 ST_Y(geometry)
獲取點的Z坐標 ST_Z(geometry)
獲取點的M值 ST_M(geometry)

7.幾何對象構造函數 :

參考語義:
Text:WKT
WKB:WKB
Geom:Geometry
M:Multi
Bd:BuildArea
Coll:Collection ST_GeomFromText(text,[])

ST_PointFromText(text,[])
ST_LineFromText(text,[])
ST_LinestringFromText(text,[])
ST_PolyFromText(text,[])
ST_PolygonFromText(text,[])
ST_MPointFromText(text,[])
ST_MLineFromText(text,[])
ST_MPolyFromText(text,[])
ST_GeomCollFromText(text,[])
ST_GeomFromWKB(bytea,[])
ST_GeometryFromWKB(bytea,[])
ST_PointFromWKB(bytea,[])
ST_LineFromWKB(bytea,[])
ST_LinestringFromWKB(bytea,[])
ST_PolyFromWKB(bytea,[])
ST_PolygonFromWKB(bytea,[])
ST_MPointFromWKB(bytea,[])
ST_MLineFromWKB(bytea,[])
ST_MPolyFromWKB(bytea,[])
ST_GeomCollFromWKB(bytea,[])
ST_BdPolyFromText(text WKT, integer SRID)
ST_BdMPolyFromText(text WKT, integer SRID)

本文編寫過程中參考的文檔:
https://www.cnblogs.com/think8848/p/6929351.html
https://www.cnblogs.com/ytwy/p/6826549.html

 


免責聲明!

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



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