创建一个新的数据库并从 north_carolina 数据集读取两个 shp 文件。
user@osgeo-6:~$ spatialite test.sqlite
SpatiaLite version ..: 3.1.0-RC2 Supported Extensions:
- 'VirtualShape' [direct Shapefile access]
- 'VirtualDbf' [direct DBF access]
- 'VirtualXL' [direct XLS access]
- 'VirtualText' [direct CSV/TXT access]
- 'VirtualNetwork' [Dijkstra shortest path]
- 'RTree' [Spatial Index - R*Tree]
- 'MbrCache' [Spatial Index - MBR cache]
- 'VirtualSpatialIndex' [R*Tree metahandler]
- 'VirtualFDO' [FDO-OGR interoperability]
- 'SpatiaLite' [Spatial SQL - OGC]
PROJ.4 version ......: Rel. 4.8.0, 6 March 2012
GEOS version ........: 3.3.3-CAPI-1.7.4
SQLite version ......: 3.7.9
Enter ".help" for instructions
spatialite>
spatialite> .loadshp data/north_carolina/shape/schools_wake schools utf-8 3358
spatialite> .loadshp data/north_carolina/shape/roadsmajor roads utf-8 3358
在 .loadshp 命令中第一项为目标文件名(无 .shp 扩展名),第二项为目标表单,然后是字符编码和投影系统的 EPSG 代码。
查询 42 高速公路周围的学校:
spatialite> SELECT s.NAMESHORT, s.ADDRNUMBER, s.ADDRROOT
...> FROM schools AS s, roads AS r
...> WHERE r.ROAD_NAME = "NC-42" AND
...> ST_Distance(s.Geometry, r.Geometry) < 1000;
FUQUAY-VARINA|6600|Johnson Pond Rd
WILLOW SPRINGS|6800|Dwight Rowland Rd
FUQUAY-VARINA|109|N Ennis St
LINCOLN HEIGHTS|307|Bridge St
输出:
最后,我们可以对逗号分隔的 “schools_rt42.txt” 文本文件作查询:
spatialite> .mode csv
spatialite> .output "schools_rt42.txt"
spatialite> SELECT s.NAMESHORT, s.ADDRNUMBER, s.ADDRROOT
...> FROM schools AS s, roads AS r
...> WHERE r.ROAD_NAME = "NC-42" AND
...> ST_Distance(s.Geometry, r.Geometry) < 1000;
spatialite>.q