spatialite c samples 解析(2)


spatialite c samples 解析(2)


1.在程序中使用spatialite数据类型。

gaiaAllocGeomColl函数可以分配一个空间数据类型.

1    gaiaGeomCollPtr geo;
2    geo=gaiaAllocGeomColl();

然后再将空间数据写入到该变量中,通过执行下边函数即可:

1    gaiaAddPointToGeomColl (geo, 1.5, 2.75);

当然也有gaiaAddLinestringToGeomColl,gaiaAddPolygonToGeomColl来分别对应点,线,面.
所不同的是线和面的分配方法稍微复杂些,函数的定义如下:

1    void gaiaAddPointToGeomColl( gaiaGeomCollPtr p , double x , double y );
2    gaiaLinestringPtr gaiaAddLinestringToGeomColl ( gaiaGeomCollPtr p , int points);
3    gaiaPolygonPtr gaiaAddPolygonToGeomColl (gaiaGeomCollPtr p, int points, int num_interiors);

后两者都有对应该类型的返回值.
所以线的操做如下:

1    gaiaLinestringPtr line;
2    line = gaiaAddLinestringToGeomColl (geo_ln, 5);
3    gaiaSetPoint (line->Coords, 0, 1.0, 1.0);
4    gaiaSetPoint (line->Coords, 1, 2.0, 1.0);
5    gaiaSetPoint (line->Coords, 2, 2.0, 2.0);
6    gaiaSetPoint (line->Coords, 3, 100.0, 2.0);
7    gaiaSetPoint (line->Coords, 4, 100.0, 100.0);

需要注意的是line,它是geoln的一个成员,可以指向geoln,从gaiaGeomCollPtr的定义可以看出:

 1    typedef struct gaiaGeomCollStruct {
 2        int Srid;
 3        gaiaPointPtr FirstPoint;
 4        gaiaLinestringPtr FirstLinestring;
 5        gaiaPolygonPtr FirstPolygon;
 6        double MinX;
 7        double MinY;
 8        double MaxX;
 9        double MaxY;
10    }gaiaGeomColl;
11    typedef gaiaGeomColl * gaiaGeomCollPtr;

真正的数据是存储在 FirstLinestring所指向的内存空间.

 1    typedef struct gaiaLinestringStruct {
 2        int Points;
 3        double * Coords;
 4        double MinX;
 5        double MinY;
 6        double MaxX;
 7        double MaxY;
 8        struct gaiaLinestringStruct * Next;
 9    }gaiaLinestring;
10    typedef gaiaLinestring * gaiaLinestringPtr;

面的操作与之类似.

Powered by Engin & toto

comments powered by Disqus