Search code examples
pythonrotationgeogeopandasturfjs

Cant correctly rotate point using GeoPandas


My Python code:

from geopandas import GeoSeries
from shapely.geometry import Point

WGS84 = {'init': 'epsg:4326'}
p1 = (41.8121843, 45.2178516,)
p2 = (41.8124878, 45.2177536,)

point2 = Point(p2)

s2 = GeoSeries(point2, crs=WGS84)
rotated = s2.rotate(90, p1)  # Try to rotate p2 around p1

print(rotated)  # 41.8122823 45.2181551

My JS code (Turf.js):

var p1 = [41.8121843, 45.2178516];
var p2 = [41.8124878, 45.2177536];

var l = turf.lineString( [p1, p2] );
L.geoJson(l, {style: {color: '#0000ff', fillOpacity: 1, fillColor: '#0000ff', opacity: 1}}).addTo(map);

// Rotate line around p1
var trufRotated = turf.transformRotate(l, -90, {pivot: p1});
L.geoJson(trufRotated, {style: {color: '#ff0000'}}).addTo(map);

Illustration. Blue is original line. Red is line rotated using Turf.js (ok). Green is line rotated using GeoPandas (not ok):

enter image description here

Green line rotated using geopandas have wrong offset. Maybe it is problem of projection but I cant figure out how to fix it.

My question is how to rotate correctly using GeoPandas?


Solution

  • Solved. Thanks to @PaulH. I need to make rotation in Mercator and translate it again in lon/lat:

    WGS84 = {'init': 'epsg:4326'}
    MERC = {'init': 'epsg:3857'}
    
    p1 = (41.8121843, 45.2178516,)
    p2 = (41.8124878, 45.2177536,)
    
    point1 = Point(p1)
    point2 = Point(p2)
    
    s1 = GeoSeries(point1, crs=WGS84)
    s2 = GeoSeries(point2, crs=WGS84)
    
    s11 = s1.to_crs(MERC)
    s22 = s2.to_crs(MERC)
    
    rotated = s22.rotate(90, s11[0])
    rotated = rotated.to_crs(WGS84)
    print(rotated)