对于GIS来说,GDAL几乎是万物之源!
QGIS,用了GDAL!
FME,用了GDAL!
ArcGIS,也用了GDAL!
……
具体有多少,可以去官网上去查看,就不再一一列举了!
某天,我们的白总在我的QQ群里问了个这样的问题:
并且还不想用商业软件来实现,还要求要用代码!
怎么整!
唉!
谁让他是白总呢!
白总有需求,咱当然要响应了!
于是,就想到了GDAL。
于是,就想:GDAL有没有类似的功能?
答案是:当然有!
所以就打算用GDAL来解决这个问题。
但别问我谁是‘于是’,我也不知道!
也许是我打开方式有问题吧,我搜索了接近两个小时,百度、必应、谷歌,都试了一遍,但没啥收获!
所以不要过于依赖搜索引擎!
搜索无果,我就放弃了搜索引擎。把主要精力放在了官方文档上。看了看文档,我发现了俩函数。经过测试,这俩函数都是可以的。
PointOnSurface(Geometry self) -> Geometry
OGRGeometryH
OGR_G_PointOnSurface(OGRGeometryH hGeom)
Returns a point guaranteed to lie on the surface.
This method relates to the SFCOM ISurface::get_PointOnSurface() method
however the current implementation based on GEOS can operate on other
geometry types than the types that are supported by SQL/MM-Part 3 :
surfaces (polygons) and multisurfaces (multipolygons).
This method is built on the GEOS library, check it for the definition
of the geometry operation. If OGR is built without the GEOS library,
this method will always fail, issuing a CPLE_NotSupported error.
Parameters:
-----------
hGeom: the geometry to operate on.
a point guaranteed to lie on the surface or NULL if an error occurred.
OGR 1.10
Centroid(Geometry self) -> Geometry
int OGR_G_Centroid(OGRGeometryH hGeom, OGRGeometryH hCentroidPoint)
Compute the geometry centroid.
The centroid location is applied to the passed in OGRPoint object. The centroid is not necessarily within the geometry.
This method relates to the SFCOM ISurface::get_Centroid() method however the current implementation based on GEOS can operate on other geometry types such as multipoint, linestring, geometrycollection such as multipolygons. OGC SF SQL 1.1 defines the operation for surfaces (polygons). SQL/MM-Part 3 defines the operation for surfaces and multisurfaces (multipolygons).
This function is the same as the C++ method OGRGeometry::Centroid().
This function is built on the GEOS library, check it for the definition of the geometry operation. If OGR is built without the GEOS library, this function will always fail, issuing a CPLE_NotSupported error.
OGRERR_NONE on success or OGRERR_FAILURE on error.
查完文档,后面的就很简单了,直接上代码吧!
import ogr
p = ogr.CreateGeometryFromWkt('POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 4,4 4,4 2,2 2)) ')
#生成内部点
#方式一
p.PointOnSurface().ExportToWkt()
#方式二
p.Centroid().ExportToWkt()
具体的区别,我还没来得及体会。只知道PointOnSurface生成的点,在面的内部,而Centorid生成的点,不一定在内部!具体的使用,还需日后细细品。