geopandasshapelypyprojfiona

How to solve the lack of precision of the coordinates of the centroid points after a buffer?


When generating polygons by buffer (here squares), the geometric points used for generation have different coordinates than those taken by the .centroid method on the polygon after their generation.

Here is an example with just one point.

from shapely.ops import transform
import geopandas as gpd
import shapely.wkt
import pyproj
from math import sqrt

def edge_size(area): return sqrt(area)*1e3

point = "POINT (4379065.583907348 2872272.254645019)"
point = shapely.wkt.loads(point)

center = gpd.GeoSeries(point)

project = pyproj.Transformer.from_proj(
    pyproj.Proj('epsg:3395'), 
    pyproj.Proj('epsg:4326'),
    always_xy=True) 

center = center.apply(lambda p: transform(project.transform, p))

print(center.iloc[0])

square = point.buffer(
    edge_size(3), cap_style=3) #distance of 3km2

square = gpd.GeoSeries(square)
square = square.apply(lambda p: transform(project.transform, p))
square = square.apply(lambda p: p.centroid)

print(square.iloc[0])

#POINT (39.33781544185747 25.11929860805248)
#POINT (39.33781544185747 25.11929777802279)

This leads to processing errors afterwards.
First of all, is this normal? And how to solve this problem?

I also reported my problem here. Thank you for your attention.


Solution

  • Copying my answer from GitHub for posterity.

    This is not a bug but a misunderstanding of coordinate transformation. You have to keep in mind that what is square in one projection is not square in another.

    If you stick to the same CRS, the output of the centroid of a buffer equals the initial point. But the centroid of a reprojected polygon is slightly off, specifically because you did reprojection that skewed the geometry in one direction.

    How to overcome this problem?

    Do all your operations in one CRS and reproject once you are done.