0

I'm very new to Python so apologies for my lack of understanding.

I need to read in 2 columns (latitude & longitude) of a 4 column CSV file. Example below.

ShopName Misc latitude longitude XXX 3 999999 999999

I then have to change the latitude and longitude using a pyproj transform scrypt that I have checked. I then need to save the tranformed latitude and longitude data into a new csv such that the column format is the same as the existing csv. Example below.

ShopName Misc latitude longitude XXX 3 49.12124 -2.32131

I'm a bit lost but this is where I got to. Thank you in advance

import csv
from pyproj import Transformer

#2.2 Define function
def transformer = Transformer.from_crs("epsg:12345", "epsg:9999")
    result = transformer.transform(old_longitude, old_latitude)
    return new_longitude, new latitude

#2.3 Set destination file to variable
with open('new.csv' ,'w') as csv_new2:

#2.4 Instruct write method to new file    
    fileWriter2 = csv.writer(csv_new2)
    
#2.5 Set existing file to variable
    with open('old.csv','r') as csv_old2:

#2.6 Instruct read method to new file
        fileReader2 = csv.reader(csv_old2)

        for row in fileReader2:

1 Answers1

0

Here are some options for you.

pandas + pyproj:

geopandas:

from pyproj import Transformer
import pandas

pdf = pandas.read_csv("old.csv")
transformer = Transformer.from_crs("epsg:12345", "epsg:9999", always_xy=True)
xx, yy = trans.transform(pdf["longitude"].values, pdf["latitude"].values)
pdf = pdf.assign(longitude=xx, latitude=yy)
pdf.to_csv("new.csv")
snowman2
  • 646
  • 4
  • 11
  • Thank you for your help. Your code includes "...trans.transformer..." I needed to amend this to "...transformer.transform..." to make it work. I am wondering also, some of the CSV data is blank and when I run the script it is adding 'inf' into the empty cells. Is there a way to make it not add anything? – Beaucoupnice Nov 06 '20 at 11:05
  • That sounds like a separate question. I recommend asking a new question with your current progress. – snowman2 Nov 06 '20 at 12:52
  • Thank you kindly for your help. – Beaucoupnice Nov 06 '20 at 13:17