Geodesic buffering in python

2024/9/16 23:19:27

Given land polygons as a Shapely MultiPolygon, I want to find the (Multi-)Polygon that represents the e.g. 12 nautical mile buffer around the coastlines.

Using the Shapely buffer method does not work since it uses euclidean calculations.

Can somebody tell me how to calculate geodesic buffers in python?

Answer

This is not a shapely problem, since shapely explicitly tells in its documentation that the library is for planar computation only. Nevertheless, in order to answer your question, you should specify the coordinate systems you are using for your multipolygons. Assuming you are using WGS84 projection (lat,lon), this is a recipe I found in another SO question (fix-up-shapely-polygon-object-when-discontinuous-after-map-projection). You will need pyproj library.

import pyproj
from shapely.geometry import MultiPolygon, Polygon
from shapely.ops import transform as sh_transform
from functools import partialwgs84_globe = pyproj.Proj(proj='latlong', ellps='WGS84')def pol_buff_on_globe(pol, radius):_lon, _lat = pol.centroid.coords[0]aeqd = pyproj.Proj(proj='aeqd', ellps='WGS84', datum='WGS84',lat_0=_lat, lon_0=_lon)project_pol = sh_transform(partial(pyproj.transform, wgs84_globe, aeqd), pol)return sh_transform( partial(pyproj.transform, aeqd, wgs84_globe),project_pol.buffer(radius))def multipol_buff_on_globe(multipol, radius):return MultiPolygon([pol_buff_on_globe(g, radius) for g in multipol])

pol_buff_on_globe function does the following. First, build an azimuthal equidistant projection centered in the polygon centroid. Then, change the coordinate system of the polygon to that projection. After that, builds the buffer there, and then change the coordinate system of the buffered polygon to WGS84 coordinate system.

Some special care is needed:

  • You will need to find out how to translate the distance you want to the distance used in aeqd projection.
  • Be careful of not buffering including the poles (see the mentioned SO question).
  • The fact that we are using the centroid of the polygon to center the projection should guaranty the answer is good enough, but if you have specif precision requirements you should NOT USE this solution, or at least make a characterization of the error for the typical polygon you are using.
https://en.xdnf.cn/q/72764.html

Related Q&A

jinja2 variables naming - Are variables naming restrictions the same as for Python variables?

I didt find it written explicitly in the docs.Are the naming rules the same as with Python variables?(eg: {{ a_variablelike_that }} doesnt work for example)

What does t /= d mean? Python and getting errors

// t: current time, b: begInnIng value, c: change In value, d: durationdef: easeOutQuad, swing: function (x, t, b, c, d) {//alert(jQuery.easing.default);return jQuery.easing[jQuery.easing.def](x, t, b,…

Python File Read + Write

I am working on porting over a database from a custom MSSQL CMS to MYSQL - Wordpress. I am using Python to read a txt file with \t delineated columns and one row per line.I am trying to write a Python …

Uploading a file via pyCurl

I am trying to convert the following curl code into pycurl. I do not want to use requests. I need to use pycurl because requests is not fully working in my old python version.curl -X POST -H "Acce…

Speed up computation for Distance Transform on Image in Python

I would like to find the find the distance transform of a binary image in the fastest way possible without using the scipy package distance_trnsform_edt(). The image is 256 by 256. The reason I dont wa…

Prevent Kivy leaving debug messages

I have a simple a Kivy interface that also uses the terminal. Example code:import kivykivy.require(1.0.6)from kivy.app import App from kivy.uix.label import Labelclass MyApp(App):def build(self):return…

django.db.utils.InternalError: (1050, Table django_content_type already exists)

django.db.utils.InternalError: (1050, "Table django_content_type already exists")I just copied a project from my friend, when I run makemirations it runs properly. But for - python3 manage.py…

Use SQLites backup API from Python/SQLAlchemy

Im using an SQLite database from python (with SQLAlchemy). For performance reasons, Id like to populate an in-memory database in the application, and then back up that database to disk.SQLite has a bac…

ABC for String?

I recently discovered abstract base classes (ABCs) in collections and like their clear, systematic approach and Mixins. Now I also want to create customs strings (*), but I couldnt find an ABC for stri…

Get the most relevant word (spell check) from enchant suggest() in Python

I want to get the most relevant word from enchant suggest(). Is there any better way to do that. I feel my function is not efficient when it comes to checking large set of words in the range of 100k or…