SQLite号称全世界最小的数据库,在几乎绝大多数数据库都具有空间数据的存储和查询功能后,SQLite目前也有了空间数据支持的扩展,利用这个扩展,可以按照OGC的Simple Feature Access标准存取空间数据。这个项目名叫SpatiaLite,与其一同分发的还有一个VirtualShape。前者为SQLite增加空间数据支持,后者可以把一个Shapefile作为SQLite的数据库
SpatiaLite编译(Windows)
下载地址:https://www.gaia-gis.it/fossil/libspatialite/index
Windows编译参考地址:http://www.gaia-gis.it/gaia-sins/msvc_how_to.html
依赖第三方库 libproj、libgeos、libfreexl、libiconv、libsqlite3、libz、libxml2、librttopp、libexpatd、liblzma、libreadosm
SpatiaLite空间数据表结构
(基于BLOB类型的)Geometry: 将空间数据以WKB(Well-Known Binary Representation)形式存储在名为WKB_Geometry的BLOB类型的字段中, 不会出现折行存储Geometry的情况 一行存储一个Geometry对象 每行GID是该Geometry对象的唯一标识 YMIN, YMAX, XMIN, XMAX用于存储该对象的四至 该表的主码是GID
SpatiaLite 简单示例
GAIA_POINT类型:
int geoType = gaiaGeometryType(geo);
if (geoType == GAIA_POINT) // 读取pointz
{
gaiaPointPtr p = geo->FirstPoint;
while (p != nullptr)
{ PointD pt; pt.x = p->X; pt.y = p->Y; p = p->Next; }
}
GAIA_LINESTRING类型:
int geoType = gaiaGeometryType(geo);
if (geoType == GAIA_LINESTRING) // 读取linestring
{ gaiaLinestringPtr lp = geo->FirstLinestring;
while (lp != nullptr)
{ for (int i = 0; i < lp->Points; i++)
{ PointD pt; pt.x = (lp->Coords)[i * 2]; pt.y = (lp->Coords)[i * 2 + 1]; pt.z = 0; points.emplace_back(pt);
}
lp = lp->Next;
}
}
GAIA_POLYGON类型:
int geoType = gaiaGeometryType(geo);
if (geoType == GAIA_POLYGON) // 读取polygon
{ gaiaPolygonPtr poly = geo->FirstPolygon;
while (poly != nullptr)
{
gaiaRingPtr pRing = poly->Exterior; //外环
for (int i = 0; i < pRing->Points; i++)
{ PointD pt; pt.x = (pRing->Coords)[i * 2];
pt.y = (pRing->Coords)[i * 2 + 1];
points.emplace_back(pt);
}
pRing = poly->Interiors; //内环
while(pRing != nullptr)
{
for (int i = 0; i < pRing->Points; i++)
{ PointD pt; pt.x = (pRing->Coords)[i * 2];
pt.y = (pRing->Coords)[i * 2 + 1];
points.emplace_back(pt);
}
pRing->Next;
}
poly = poly->Next;
}
}