-->

从使用Python点矩形(Rectangles from Points using Python)

2019-09-16 21:38发布

我有一个文本文件满点。 它们在由逗号限制的(X,Y)对每行分离。 例如。

-43.1234,40.1234\n
-43.1244,40.1244\n
etc.

我现在需要创建一个围绕这些点的多边形。 多边形必须有从点15公里长的缓冲。 我没有访问到ArcGIS或在这一点上,我提供了这个功能,因此任何其他GIS,我想知道如果任何人有这将有助于我上手的数学吗?

Answer 1:

你想用GDAL / OGR / OSR ,这确实预测,缓冲区,甚至可以写shape文件给你。

为了转换度纬度/长到米的度量缓冲器,你需要一个投影坐标系。 在下面的例子我用UTM区,这是动态加载并高速缓存。 这将计算的大地水准面15公里。

我也同时计算GIS缓冲区,这是一个圆形的多边形,以及根据计算出缓冲,这是你所寻求的长方形的信封。

from osgeo import ogr, osr

# EPSG:4326 : WGS84 lat/lon : http://spatialreference.org/ref/epsg/4326/
wgs = osr.SpatialReference()
wgs.ImportFromEPSG(4326)    
coord_trans_cache = {}
def utm_zone(lat, lon):
    """Args for osr.SpatialReference.SetUTM(int zone, int north = 1)"""
    return int(round(((float(lon) - 180)%360)/6)), int(lat > 0)

# Your data from a text file, i.e., fp.readlines()
lines = ['-43.1234,40.1234\n', '-43.1244,40.1244\n']
for ft, line in enumerate(lines):
    print("### Feature " + str(ft) + " ###")
    lat, lon = [float(x) for x in line.split(',')]
    # Get projections sorted out for that UTM zone
    cur_utm_zone = utm_zone(lat, lon)
    if cur_utm_zone in coord_trans_cache:
        wgs2utm, utm2wgs = coord_trans_cache[cur_utm_zone]
    else: # define new UTM Zone
        utm = osr.SpatialReference()
        utm.SetUTM(*cur_utm_zone)
        # Define spatial transformations to/from UTM and lat/lon
        wgs2utm = osr.CoordinateTransformation(wgs, utm)
        utm2wgs = osr.CoordinateTransformation(utm, wgs)
        coord_trans_cache[cur_utm_zone] = wgs2utm, utm2wgs
    # Create 2D point
    pt = ogr.Geometry(ogr.wkbPoint)
    pt.SetPoint_2D(0, lon, lat) # X, Y; in that order!
    orig_wkt = pt.ExportToWkt()
    # Project to UTM
    res = pt.Transform(wgs2utm)
    if res != 0:
        print("spatial transform failed with code " + str(res))
    print(orig_wkt + " -> " + pt.ExportToWkt())
    # Compute a 15 km buffer
    buff = pt.Buffer(15000)
    print("Area: " + str(buff.GetArea()/1e6) + " km^2")
    # Transform UTM buffer back to lat/long
    res = buff.Transform(utm2wgs)
    if res != 0:
        print("spatial transform failed with code " + str(res))
    print("Envelope: " + str(buff.GetEnvelope()))
    # print("WKT: " + buff.ExportToWkt())


文章来源: Rectangles from Points using Python