0

I'm working on a project that needs one set of coordinates converted to another(from CRS 229 to CRS 3857), and I have a function that works when you put the former coordinates in, but I would like to make it so that I can just run the function over the entire column(s)(there are X, Y, and a XY column).

This is what the code looks like

from pyproj import Proj, transform, CRS,  Transformer
import pandas as pd
import geopandas as gpd
crs_2229 = CRS.from_epsg(2229)
crs_3857 = CRS.from_epsg(3857)
transformer = Transformer.from_crs(crs_2229, crs_3857)

data = {'X': [6481427.400, 6485873.280],
        'Y': [1797333.200, 1796834.811],
        'XY':[(6481427.400000006,1797333.200000003),(6485873.279835222,1796834.811359349)]}

df = pd.DataFrame(data)

Now if I run this

transformer.transform(6481427.400000006,1797333.200000003)

It comes with the correct transformation of (-13165144.971136427, 4019524.5726486626). But how do I make it a little more automated?

Tried a lambda function, but it kept on rising as a key value error. Tried to make a for loop, but that didn't give me anything. Searched online for functions, but they didn't work on it, and now with the google colabs AI tool, it gave me the same code I would try as suggestions but they never worked.

2
  • Have you tried this? If you get an error, you should post the error here, so people can see it to know what you're doing wrong. Commented Dec 20, 2023 at 22:13
  • @Joe I tried that and it worked. I should've kept the errors, but deleted them. Thank you for that! Not sure what I had messed up before but now it is working Commented Dec 21, 2023 at 1:32

2 Answers 2

1

You could use:

def func(r):
    return transformer.transform(r[0], r[1])

df['XY_t'] = df['XY'].apply(func)

print(df['XY_t'])

which gives for new column:

0     (-13165144.971136427, 4019524.572648665)
1    (-13163513.038113914, 4019345.0284381276)
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that, but when you get passed 2 rows(was more for example than anything) it runs into an error: ValueError: Length of values (2) does not match length of index (39)[the data frame I testing this on is only 39 rows, but there are a couple hundred]
See edited answer which now gives correct answer for multiple rows.
0

If you're already using geopandas, could you not use a GeoDataFrame and use .to_crs.

from pyproj import CRS
import geopandas as gpd


crs_2229 = CRS.from_epsg(2229) # Can be replaced with a string 'epsg:2229'
crs_3857 = CRS.from_epsg(3857) # Can be replaced with a string 'epsg:3857'

data = {'geometry': gpd.points_from_xy(
(6481427.400000006,1797333.200000003),
(6485873.279835222,1796834.811359349))}

gdf = gpd.GeoDataFrame(data, crs=crs_2229).to_crs(crs_3857)

if you need to get coordinates independently later you could access the x and y as a property or using a function, eg:

gdf.get_coordinates()
gdf['geometry'].x
gdf['geometry'].y

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.