Answers:
在Python的GDAL / OGR食谱有一些示例代码,以缓冲几何。
from osgeo import ogr
wkt = "POINT (1198054.34 648493.09)"
pt = ogr.CreateGeometryFromWkt(wkt)
bufferDistance = 500
poly = pt.Buffer(bufferDistance)
print "%s buffered by %d is %s" % (pt.ExportToWkt(), bufferDistance, poly.ExportToWkt())
from osgeo import ogr
wkt1 = "POLYGON ((1208064.271243039 624154.6783778917, 1208064.271243039 601260.9785661874, 1231345.9998651114 601260.9785661874, 1231345.9998651114 624154.6783778917, 1208064.271243039 624154.6783778917))"
wkt2 = "POLYGON ((1199915.6662253144 633079.3410163528, 1199915.6662253144 614453.958118695, 1219317.1067437078 614453.958118695, 1219317.1067437078 633079.3410163528, 1199915.6662253144 633079.3410163528)))"
poly1 = ogr.CreateGeometryFromWkt(wkt1)
poly2 = ogr.CreateGeometryFromWkt(wkt2)
intersection = poly1.Intersection(poly2)
print intersection.ExportToWkt()
可以读取几何图形并将其写入shapefile和其他各种格式。
为简化起见,Shapely:manual 允许使用Python对PostGIS进行所有几何处理。
Shapely的第一个前提是Python程序员应该能够在RDBMS之外执行PostGIS类型的几何操作...
PolyGeo的第一个示例
from shapely.geometry import Point, LineString, Polygon, mapping
from shapely.wkt import loads
pt = Point(1198054.34,648493.09)
# or
pt = loads("POINT (1198054.34 648493.09)")
bufferDistance = 500
poly = pt.buffer(bufferDistance)
print poly.wkt
'POLYGON ((1198554.3400000001000000 648493.0899999999700000, 1198551.9323633362000000
# GeoJSON
print mapping(poly)
{'type': 'Polygon', 'coordinates': (((1198554.34, 648493.09), (1198551.9323633362, 648444.0814298352), (1198544.7326402017, 648395.544838992), ....}
PolyGeo提供的多边形示例:
poly1 = Polygon([(1208064.271243039,624154.6783778917), (1208064.271243039,601260.9785661874), (1231345.9998651114,601260.9785661874),(1231345.9998651114,624154.6783778917),(1208064.271243039,624154.6783778917)])
poly2 = loads("POLYGON ((1199915.6662253144 633079.3410163528, 1199915.6662253144 614453.958118695, 1219317.1067437078 614453.958118695, 1219317.1067437078 633079.3410163528, 1199915.6662253144 633079.3410163528)))"
intersection = poly1.intersection(poly2)
print intersection.wkt
print mapping(intersection) -> GeoJSON
第二个前提是要素的持久性,序列化和地图投影很重要,但存在正交问题。您可能不需要一百个GIS格式的阅读器和编写器,也不需要大量的State Plane投影,Shapely不会给您带来负担。
因此,您可以将其与其他Python模块结合使用,以读取或编写shapefile并以osgeo.ogr,Fiona或PyShp的形式处理投影 。
在Gis StackExchange中进行搜索,您可以找到许多示例,但我还提供了另一个示例来说明shapely和Fiona的组合以及使用shapely函数的函数cross()和buffer()(这可以通过PyShp完成)。
给定两个折线shapefile:
计算交集(整形函数交集())
from shapely.geometry import Point, Polygon, MultiPolygon, MumtiPoint, MultiLineString,shape, mapping
import fiona
# read the shapefiles and transform to MultilineString shapely geometry (shape())
layer1 = MultiLineString([shape(line['geometry']) for line in fiona.open('polyline1.shp')])
layer2 = MultiLineString([shape(line['geometry']) for line in fiona.open('polyline2.shp')])
points_intersect = layer1.intersection(layer2)
将结果另存为新的shapefile
# schema of the new shapefile
schema = {'geometry': 'MultiPoint','properties': {'test': 'int'}}
# write the new shapefile (function mapping() of shapely)
with fiona.open('intersect.shp','w','ESRI Shapefile', schema) as e:
e.write({'geometry':mapping(points_intersect), 'properties':{'test':1}})
结果:
缓冲单个点(shape的函数buffer())
# new schema
schema = {'geometry': 'Polygon','properties': {'test': 'int'}}
with fiona.open('buffer.shp','w','ESRI Shapefile', schema) as e:
for point in points:
e.write({'geometry':mapping(point.buffer(300)), 'properties':{'test':1}})
结果
缓冲多点几何
schema = {'geometry': 'MultiPolygon','properties': {'test': 'int'}}
points.buffer(300)
with fiona.open('buffer2.shp','w','ESRI Shapefile', schema) as e:
e.write({'geometry':mapping(points.buffer(300)), 'properties':{'test':1}})
我的“去”地理处理库是“遥感和GIS库”(RSGISLib)。它易于安装和使用,文档非常好。它具有矢量和栅格处理功能-我很少需要接近gui。可以在这里找到:http://rsgislib.org。
在这种情况下的一个示例是:
rsgislib.vectorutils.buffervector(inputvector, outputvector, bufferDist, force)
将向量缓冲指定距离的命令。
哪里:
例:
from rsgislib import vectorutils
inputVector = './Vectors/injune_p142_stem_locations.shp'
outputVector = './TestOutputs/injune_p142_stem_locations_1mbuffer.shp'
bufferDist = 1
vectorutils.buffervector(inputVector, outputVector, bufferDist, True)