Warm tip: This article is reproduced from stackoverflow.com, please click
coordinates dataframe pandas python geospatial

How to convert geospatial coordinates dataFrame to native x,y projection?

发布于 2020-03-27 15:46:53

I have a following dataframe, the lat and lon are the latitudes and longitudes in Geographic coordinates system. I am trying to convert these coordinate system into native (x, y) projection.

I have tried pyproj for single points, but how do I proceed for the whole dataframe with thousands of rows.

       time                 lat        lon 
     0 2011-01-31 02:41:00  18.504273  -66.009332
     1 2011-01-31 02:42:00  18.504673  -66.006225

I am trying to get something like this:

       time                 lat        lon        x_Projn    y_Projn
     0 2011-01-31 02:41:00  18.504273  -66.009332 resp_x_val resp_y_val
     1 2011-01-31 02:42:00  18.504673  -66.006225 resp_x_val resp_y_val
     and so on...

Following is the code I tried for lat/lon to x,y system:

      from pyproj import Proj, transform

      inProj = Proj(init='epsg:4326')
      outProj = Proj(init='epsg:3857')
      x1,y1 = -105.150271116, 39.7278572773
      x2,y2 = transform(inProj,outProj,x1,y1)
      print (x2,y2)

Output:

      -11705274.637407782 4826473.692203013

Thanks for any kind of help.

Questioner
Liza
Viewed
27
Marat 2017-03-14 12:57

Unfortunately, pyproj only converts point by point. I guess something like this should work:

import pandas as pd
from pyproj import Proj, transform

inProj = Proj(init='epsg:4326')
outProj = Proj(init='epsg:3857')

def towgs84(row):
    return pd.Series(transform(inProj, outProj, row["lat"], row["lon"]))

wsg84_df = df.apply(towgs84, axis=1)  # new coord dataframe with two columns