http://wiki.woodpecker.org.cn/moin/lilin/geos-introduce
介绍
GEOS是一个集合形状的拓扑关系操作实用库(可能这么说不太准确),简单得说,就是判断两个几何形状之间关系和对两个几何形状进行操作以形成新的几何形状的库。
其实学geos的好地方不是在GEOS的主站。而是在另一个地方,JTS Topology Suite (JTS)!
事实上,GEOS是仿照JTS做的。它其实是JTS的C++实现。
GEOS主要支持几种操作和计算:
空间关系计算,主要支持几种计算
另外还支持多边形化,连接有向线段,压出节点等等操作。
geos for python的安装
windows下的安装
纯粹的GEOS安装
纯粹的GEOS在windows下的安装挺复杂,因为geos本来是为unix类系统写得.作者好像在windows下花的时间不是很多.所以代码很多在windows下都会出错.不过我还是把它安装上了.
- 下载geos的geos-2.1.4版(更高的版本我没有试过)解压,假设解压路径下面都表示为$GEOSHOME
- 进入$GEOSHOME\source\headers\geos,把platform.h.vc改名为platform.h,把version.h.in改名为version.h
- 进入$GEOSHOME\source
- 登记vcvars32.bat,如"D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\vcvars32.bat",依你的vs安装路径和版本自己看着办.
运行nmake /f makefile.vc,注意,不要去$GEOSHOME\VisualStudio下开vc工程,那个工程如果没有经过修改是不会运行成功的.nmake运行成功后,就有了geos.lib了,很好,现在geos的C++版已经可以用了.
- 下面进入$GEOSHOME\swig\python目录
- 运行swig -c++ -python -modern -o geos_wrap.cxx ../geos.i (注意,这里的前提是你有swig,没有去下一个swig for windows,然后把swig.exe所在目录加入path环境变量
- 打开setup.py,去掉有stdc++.lib的那行,然后把libgeos.a改成geos.lib
- 运行python setup.py build
- 运行python setup.py install
ok,成了!
让geos作为ogr的辅助支持
上面的编译方法实在有些……,不过有一种简单的办法也可以用到geos的功能,就是让ogr添加geos支持。这种方法比较稳妥,也比较容易成功。
先参考上面的步骤编译出geos.lib静态库。
然后打开gdal主目录下的nmake.opt修改:
GEOS_CFLAGS = -IE:/clibs/geos-2.1.4/source/headers -DHAVE_GEOS
GEOS_LIB = E:/clibs/geos-2.1.4/source/geos.lib
这两行,把前面的“#”号去掉,然后把两个路径修改成geos下对应的目录。注意:第一个需要修改的是从-I开始到-D结束,写得是GEOS头文件所在的headers目录,而且-D前面有空格。第二个是从=开始,到行末,写的是geos.lib所处路径
然后重新编译gdal库。然后 nmake /f makefile.vc install安装后就可以用GEOS支持了。
其实geos支持本来就应该在编译gdal时直接添加了。到后面再加就有点浪费感情了。:-)
测试安装是否成功
做个例子试一下,看是否添加成功:
>>> import ogr
>>> wkt = "POINT (1 1)"
>>> geom = ogr.CreateGeometryFromWkt(wkt)
>>> buf = geom.Buffer(1)
>>> buf.ExportToWkt()
'POLYGON ((2 1,1.998629534754652 0.947664043758551,1.994521895368586 0.895471536
735323,1.98768834059584 0.843565534964204,1.97814760073505 0.792088309188096,1.9
65925826291005 0.741180954904707,1.951056516297928 0.690983005633592,1.933580426
500956 0.64163205046448,1.913545457647471 0.593263356935137,1.891006524194483 0.
546009500272454,1.866025403791921 0.500000000012961,1.83867056795439 0.455360964
998779,1.809016994385503 0.412214747722056,1.777145961469214 0.370679608965282,1
.743144825491414 0.330869393656712,1.707106781202421 0.292893218829326,1.6691306
06376652 0.256855174538628,1.629320391069609 0.22285403855904,1.587785252314266
0.190983005640886,1.544639035038874 0.161329432070062,1.500000000025921 0.133974
596230527,1.453990499767549 0.1089934758259,1.406736643105877 0.08645454237079,1
.358367949577434 0.066419573515133,1.309016994409106 0.048943483715945,1.2588190
45138659 0.034074173720615,1.207911690855819 0.021852399274284,1.15643446508014
0.012311659411183,1.104528463309327 0.005478104636107,1.052335956286284 0.001370
465247698,1.000000000044896 0.0,0.947664043803385 0.001370465242998,0.8954715367
79973 0.005478104626721,0.843565535008546 0.012311659397137,0.792088309232011 0.
021852399255615,0.741180954948073 0.034074173697375,0.69098300567629 0.048943483
688198,0.641632050506393 0.066419573482955,0.593263356976151 0.086454542334269,0
.546009500312456 0.108993475785135,0.500000000051841 0.133974596185631,0.4553609
65036432 0.161329432021158,0.412214747758377 0.190983005588108,0.370679609000173
0.222854038502531,0.330869393690077 0.256855174478545,0.292893218861073 0.29289
3218765832,0.25685517456867 0.330869393589983,0.222854038587295 0.37067960889549
9,0.190983005667276 0.412214747649411,0.161329432094515 0.455360964923472,0.1339
74596252976 0.499999999935197,0.108993475846283 0.546009500192447,0.086454542389
052 0.593263356853106,0.066419573531224 0.641632050380649,0.04894348372982 0.690
983005548192,0.034074173732236 0.741180954817972,0.021852399283619 0.79208830910
0263,0.012311659418207 0.843565534875514,0.0054781046408 0.89547153664602,0.0013
70465250047 0.947664043668878,0.0 0.999999999910204,0.001370465240648 1.05233595
6151776,0.005478104622028 1.104528463175373,0.012311659390113 1.156434464947106,
0.02185239924628 1.20791169072407,0.034074173685754 1.258819045008556,0.04894348
3674323 1.309016994281006,0.066419573466864 1.358367949451687,0.086454542316006
1.406736642982829,0.10899347576475 1.453990499647536,0.13397459616318 1.49999999
9909273,0.161329431996703 1.54463903492591,0.190983005561715 1.587785252205296,0
.222854038474274 1.629320390964932,0.256855174448499 1.669130606276555,0.2928932
18734082 1.707106781107177,0.330869393556614 1.743144825401285,0.370679608860604
1.777145961384448,0.412214747613085 1.809016994306332,0.455360964885815 1.83867
056788103,0.499999999896312 1.866025403724574,0.546009500152441 1.89100652413333
3,0.593263356812088 1.913545457592686,0.641632050338733 1.933580426452686,0.6909
83005505492 1.951056516256306,0.741180954774604 1.965925826256144,0.792088309056
347 1.978147600707046,0.843565534831169 1.98768834057477,0.895471536601369 1.994
521895354507,0.947664043624043 1.998629534747603,0.999999999865308 2.0,1.0523359
56106943 1.998629534761701,1.104528463130724 1.994521895382665,1.156434464902764
1.98768834061691,1.207911690680157 1.978147600763054,1.258819044965192 1.965925
826325865,1.30901699423831 1.95105651633955,1.358367949409777 1.933580426549225,
1.406736642941818 1.913545457702254,1.453990499607537 1.89100652425563,1.4999999
99870396 1.866025403859266,1.544639034888262 1.838670568027747,1.587785252168979
1.809016994464671,1.629320390930046 1.777145961553976,1.669130606243195 1.74314
4825581537,1.707106781075436 1.70710678129766,1.743144825371249 1.66913060647674
5,1.777145961356199 1.629320391174281,1.809016994279947 1.58778525242323,1.83867
0567856582 1.544639035151832,1.86602540370213 1.500000000142563,1.89100652411295
4 1.453990499887555,1.913545457574428 1.406736643228919,1.933580426436599 1.3583
67949703174,1.951056516242435 1.3090169945372,1.965925826244526 1.25881904526875
5,1.978147600697713 1.207911690987561,1.987688340567748 1.156434465213167,1.9945
21895349815 1.104528463443274,1.998629534745254 1.052335956420784,2 1))'
>>>
好大的一个缓冲区!不过这个多边形的圆也太恶心了吧!
Linux下的安装
这还要我说吗?
./configure
make
make install
自己去折腾吧!
要OGR支持,只要在GDAL编译的时候加入GEOS的编译开关(似乎只要在编译gdal前编译安装好GEOS,在configure GDAL的时候就会自动加入GEOS支持,当然各个系统不一定)重新编译GDAL就可以了。
不懂编译开关的设置?那就用./configure --help看看吧!一大堆?慢慢看。一堆英文看不懂?不是吧,我四级没过都看懂了。实在看不懂?那么你就apt yum urpmi 什么的搞个二进制包吧!
祝你好运!