試用 SpatiaLite(一):安裝及測試

2024-01-21 05:48

本文主要是介绍試用 SpatiaLite(一):安裝及測試,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前安裝過 PostgreSQL + PostGIS,由於它是成熟的產品,所以有很多廠商或社群支援。而這次試用的 SpatiaLite,雖然比較年輕,但因為它建基於 Sqlite,繼承了其開發和使用的容易性,亦為部分產品所使用。

根據 維基百科 的介紹:
SpatiaLite is a spatial extension to SQLite, providing vector geodatabase functionality. It is similar to PostGIS, Oracle Spatial, and SQL Server with spatial extensions, although SQLite/SpatiaLite aren't based on client-server architecture: they adopt a simpler personal architecture. i.e. the whole SQL engine is directly embedded within the application itself: a complete database simply is an ordinary file which can be freely copied (or even deleted) and transferred from one computer/OS to a different one without any special precaution.

SpatiaLite extends SQLite's existing spatial support to cover the OGC's SFS specification. It isn't necessary to use SpatiaLite to manage spatial data in SQLite, which has its own implementation of R-tree indexes and geometry types, but in order to do advanced spatial queries and support multiple map projections, SpatiaLite is needed.

亦根據其網頁介紹:
SpatiaLite is an open source library intended to extend the SQLite core to support fully fledged Spatial SQL capabilities.
SQLite is intrinsically simple and lightweight:

- a single lightweight library implementing the full SQL engine
- standard SQL implementation: almost complete SQL-92
- no complex client/server architecture
- a whole database simply corresponds to a single monolithic file (no size limits)
- any DB-file can be safely exchanged across different platforms, because the internal architecture is universally portable
- no installation, no configuration

SpatiaLite is smoothly integrated into SQLite to provide a complete and powerful Spatial DBMS (mostly OGC-SFS compliant).
Using SQLite + SpatiaLite you can effectively deploy an alternative open source Spatial DBMS roughly equivalent to PostgreSQL + PostGIS.

安裝 SpatiaLite 前,需先安裝  SQLite 3 PROJ.4 GEOS FreeXL 。先下載它們的原始碼,解壓後在各自的資料夾執行以下指令,大致就可以了:
./configure
make
sudo make install
sudo ldconfig

然後再下載最新的  libspatialite 原始碼 ,解壓後在資料夾執行以下指令:
./configure --enable-geocallbacks --enable-lwgeom
make
sudo make install
sudo ldconfig

--enable-geocallbacks --enable-lwgeom 是為了使用 R* index 和 lwgeom 的一些功能。(--enable-lwgeom 只限於安裝 PostgreSQL 和 PostGIS 後才能提供, 相關資料

安裝後執行 sqlite,輸入以下指令便能初始化 Spatial 的資料。:
sqlite> .load libspatialite.so
sqlite> SELECT InitSpatialMetaData();

除了 sqlite,網站上亦提供了工具,例如  spatialite CLI tools ,可供直接使用,而不用手動載入 libspatialite.so 和初始化 Spatial 的資料。

以下指令可供查看安裝的版本,除了 GeosAdvanced 外,其他功能亦已開啟,可用的函數可在 此頁 查閱:
spatialite> select spatialite_version();
4.0.0
spatialite> select proj4_version();
Rel. 4.8.0, 6 March 2012
spatialite> select geos_version();
3.3.6-CAPI-1.7.6
spatialite> select lwgeom_version();
2.0.2
spatialite> select HasIconv();
1
spatialite> select HasMathSQL();
1
spatialite> select HasGeoCallbacks();
1
spatialite> select HasProj();
1
spatialite> select HasGeos();
1
spatialite> select HasGeosAdvanced();
1
spatialite> select HasGeosTrunk();
0
spatialite> select HasLwGeom();
1
spatialite> select HasEpsg();
1
spatialite> select HasFreeXL();
1

例子:
輸入由 WGS84 為基準的經緯度(4326 是 WGS84 2D 的 EPSG CRS SRID 編號):
spatialite> select AsText(MakePoint(114.1689,22.4518,4326));
POINT(114.1689 22.4518)

將它轉換為 WGS84 UTM 50N 格網座標(32650 是 WGS84 / UTM zone 50N 的 EPSG CRS SRID 編號):
spatialite> select AsText(ST_Transform(MakePoint(114.1689,22.4518,4326),32650));
POINT(208621.605201 2485587.067636)

將它轉換為 HK1980 格網座標(2326 是 Hong Kong 1980 Grid System 的 EPSG CRS SRID 編號):
spatialite> select AsText(ST_Transform(MakePoint(114.1689,22.4518,4326),2326));
POINT(835447.180293 834705.40192)

利用格網座標計算大埔中心至九龍坑山的距離:
spatialite> select ST_Distance(MakePoint(208838.738969, 2488246.942923), MakePoint(208621.605201, 2485587.067636));
2668.72321824495

利用經緯度計算大埔中心至九龍坑山的距離:
spatialite> select ST_Length(MakeLine(MakePoint(114.17052, 22.475837,4326), MakePoint(114.1689,22.4518,4326)), 1);
2666.99235712016

計算大埔中心至九龍坑山的方位角:
spatialite> select Degrees(ST_Azimuth(MakePoint(114.1689,22.4518,4326), MakePointZ(114.17052, 22.475837, 437.639187, 4326)));
3.85568120514838

輸出九龍坑山三角網測站的 KML:
spatialite> select AsKml("Cloudy Hill", "description", MakePointZ( 835614.056, 837367.172, 440.8, 2326));
<Placemark><name>Cloudy Hill</name><description>description</description><Point><coordinates>114.170519962613,22.47583709798935,437.6391865937039</coordinates></Point></Placemark>

點的 Union:
spatialite> select astext(ST_Union(MakePoint(114.17052, 22.475837,4326), MakePoint(114.1689,22.4518,4326)));
MULTIPOINT(114.17052 22.475837, 114.1689 22.4518)

創建有地理數據的表,先是一般格式的欄:
spatialite> CREATE TABLE TestTable(
id INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL);

創建該地理數據欄,儲存以 WGS84 為基準的點:
spatialite> SELECT AddGeometryColumn('TestTable', 'Geometry', 4326, 'POINT', 'XY');
1

加入 R* index,以加快檢索:
spatialite> SELECT CreateSpatialIndex('TestTable', 'Geometry');
1

加入點數據:
spatialite> insert into TestTable (Name, Geometry) VALUES ("a", MakePoint(114.1689,22.4518,4326));
spatialite> insert into TestTable (Name, Geometry) VALUES ("b", MakePoint(114.17052,22.475837,4326));

列出數據(Geometry 未能直接顯示):
spatialite> select * from TestTable;
1|a|
2|b|

列出數據:
spatialite> select id, Name, AsText(Geometry) from TestTable;
1|a|POINT(114.1689 22.4518)
2|b|POINT(114.17052 22.475837)

利用 R* index 查出在 22.4518N 114.1689E, 22.4520N 114.1690E 內的點:
spatialite> SELECT  id, Name, AsText(Geometry) FROM TestTable WHERE ROWID IN (SELECT pkid FROM idx_TestTable_Geometry WHERE pkid MATCH RTreeIntersects(114.1689,22.4518,114.1690,22.4520));
1|a|POINT(114.1689 22.4518)

將點數據轉成 GeoHash(可以文字方式儲存於一般資料庫,以方便查詢某一點附近的其他點):
spatialite> SELECT Name, GeoHash(Geometry) FROM TestTable;
a|wecptzpr0ny5c1eeemw3
b|wecpy587jztypffhy099

參考資料:
The Gaia-SINS federated projects home-page
SpatiaLite
spatialite-tools
SpatiaLite 4.0.0 SQL functions reference list (可查閱各樣函數的資料)
using SpatiaLite (Spatialite 教學)
EPSG SRID 資料庫
另一 EPSG SRID 資料庫
另一 EPSG SRID 資料庫
另一 EPSG SRID 資料庫
有地球座標系統、地圖投射等的教學資料
Wikipedia - GeoHash
Geohash.org
Javascript module for encoding and decoding geohashes - Demo

这篇关于試用 SpatiaLite(一):安裝及測試的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/628533

相关文章

WCF測試用戶端--sendTimeout錯誤

wcf測試用戶端 測試時,一直提示超時錯誤,需要設置sendtimeout 因為我的wcf中調用了另外一個webservice,有點分不清楚到底是哪個報的錯誤。   根據網上查找的結果,以為要在webconfig中去設置,最後還是不正確。最終測試得出的結論要在wcf測試用戶端這裡設置。     webconfig應該是作為服務器端去調用的(個人理解)

多線程抓取資料測試

select * into test_source_data from sys.all_parameters select * into test_dest_data from test_source_data where 1=2; 三個cs文件 DataCopy.cs using System; using System.Collections.Generic; using System

分享一个Flask+Vue+Leaflet+Pyinstaller+SpatiaLite的应用

说明 1、只在window 11上运行过,其他版本不敢确定。 2、笔者借助了Supermap Iserver的地图服务和数据服务,笔者用爬虫请求到所有数据并放在自己的SpatiaLite数据库,如有侵权,请联系笔者。 3、笔者先用前后端分别开发,然后前端打包,前端打的包成为flask的模板,再次用Pyinstaller打包,生成一个文件夹和一个app.exe。然后用nsis打包app.exe

試用 SpatiaLite(二):進一步測試

之前試過 SpatiaLite 的基本指令,現在我會創建兩個表,一個儲存一些點,另一個儲存多邊形,然後進行操作。在點的表裡面,有兩點在富亨邨裡面,另一點則是九龍坑山。在多邊形的表裡面,我儲存了富亨邨的範圍。它們皆以平面及 WGS 84 為基準儲存。 創建點表: spatialite> CREATE TABLE PointTable(id INTEGER PRIMARY KEY

代碼測試 Scriptmanager 控制的異步頁面

1. web 2.0, 測試建立在 aspx 上。 首先,確定 scriptManager 的 SupportsPartialRendering 屬性 為 true <asp:ScriptManager ID="ScriptManager1" runat="server" SupportsPartialRendering="true"></asp:ScriptManager> 2.

[前端]React入门指南安裝及依赖包管理

仓库管理工具nrm #安装nrmnpm i -g nrm#查看当前nrm ls#use 使用仓库nrm use npm 常见问题 ·npm安装失败 。切换为npm镜像为淘宝镜像 。使用yarn,如果本来使用yarn还要失败,还得把yarn的源切换到国内,如果还没有办法解决,请删除node_modules及package-lock.son然后重新执行 npm install命再不能