6

我有一个充满点的文本文件。它们在每一行上由逗号限制的 (x,y) 对分隔。例如。

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

我现在需要在每个点周围创建一个多边形。多边形必须距离该点有 15 公里的缓冲区。我无权访问 ArcGIS 或任何其他为我提供此功能的 GIS,所以在这一点上,我想知道是否有人有数学知识可以帮助我入门?

4

1 回答 1

2

您想使用GDAL/OGR/OSR,它可以进行投影、缓冲,甚至可以为您编写 Shapefile。

为了将纬度/经度转换为公制缓冲区的米,您需要一个投影坐标系。在下面的示例中,我使用动态加载和缓存的 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())
于 2012-05-22T08:17:14.590 回答