2

I have a polygon in python shapely that I would like to buffer with a 100 foot buffer.

If the polygon is stored as a series of WGS84 lon/lat points how would I go about calculating the distance of the 100ft buffer?

I can't find anything searching google and seek guidance on the algorithm.

1

1 Answer 1

6

First, I will assume that your points are in WGS84 = EPSG: 4326. Convert the lon/lat points to UTM meters using pyproj lib. First we need to get the correct projection. The issue is that to convert to UTM you need to know your UTM zone. That is something you can look up separately; UTM zone will depend on the longitude of the initial shape.

import pyproj
import shapely.geometry as shpgeo

proj = pyproj.Proj(proj="utm", zone=???, ellps="WGS84", datum="WGS84")

I used a standard helper function to convert Polygons and MultiPolygons to and from UTM. The arguments are a shapely Polygon or MultiPolygon object and a PyProj Proj object.

def toFromUTM(shp, proj, inv=False):
    geoInterface = shp.__geo_interface__
    
    shpType = geoInterface['type']
    coords = geoInterface['coordinates']
    if shpType == 'Polygon':
        newCoord = [[proj(*point, inverse=inv) for point in linring] for linring in coords]
    elif shpType == 'MultiPolygon':
        newCoord = [[[proj(*point, inverse=inv) for point in linring] for linring in poly] for poly in coords]
        
    return shpgeo.shape({'type': shpType, 'coordinates': tuple(newCoord)})

Using this function, you can convert your shape (initial_polygon) to UTM, buffer it in meters (100 feet is 30.48 meters), then convert back to lon/lat if you so desire

init_shape_utm = toFromUTM(initial_polygon, proj)
buffer_shape_utm = init_shape_utm.buffer(30.48)
buffer_shape_lonlat = toFromUTM(buffer_shape_utm, proj, inv=True)

The output will be a shapely Polygon (or MultiPolygon) buffered to 100 feet, with coordinates in the WGS84 lon/lat system.

1
  • This is such a life-saver. Created the gist here for quick reference for Indian UTM zone: gist.github.com/harshithjv/bcd3fef5661ce0a2ec20224e8e4ac415 Simplified the code with transform_wkt_with_buffer function that can be called with just two parameters, after configuring appropriate params to to proj object. Commented Jul 8, 2022 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.