Irish Grid To Lat Long in Python
Recently I came across some datasets within the Open Data portal in Northern Ireland that required some manipulation of the data into something... a bit more usable.
The end goal was to map every single street light in NI, courtesy of this tweet from Wesley Johnston of NI road blog fame: https://twitter.com/niroads/status/1742885280569274471
Currently most of the datasets that you see listed will come with an Irish Grid reference system, which uses Eastings and Northings.
This is a different system to that in use elsewere in the UK, which requires some conversion. I initially had assumed that this webservice might do the job, but alas - no dice. The final Python code to take the dataset and perform the conversion is shown below.
Hopefully its of use to some of your poor souls currently having to wrangle the local Government Open Data datasets into something that you can plot on a Google or OpenStreetMap map.
Of note here, is the pyproj package, which takes the work out. The rest is reading the csv, and writing it back out.
https://epsg.io/29903 Irish Grid
https://epsg.io/4326 to WSG84
import pyproj
import csv
def ingrs_to_latlng(easting, northing):
crs_irish = pyproj.Proj(init="EPSG:29903")
crs_wgs84 = pyproj.Proj(init="EPSG:4326")
lng, lat = pyproj.transform(crs_irish, crs_wgs84, easting, northing)
return lat, lng
def process_csv_file(input_file_path, output_file_path):
with open(input_file_path, mode='r') as infile, open(output_file_path, mode='w', newline='') as outfile:
csv_reader = csv.DictReader(infile)
fieldnames = csv_reader.fieldnames + ['LATITUDE', 'LONGITUDE']
csv_writer = csv.DictWriter(outfile, fieldnames=fieldnames)
csv_writer.writeheader()
for row in csv_reader:
easting = float(row['EASTING'])
northing = float(row['NORTHING'])
lat, lng = ingrs_to_latlng(easting, northing)
row['LATITUDE'] = lat
row['LONGITUDE'] = lng
csv_writer.writerow(row)
# Replace these with your actual file paths
input_file_path = 'your_input_file.csv'
output_file_path = 'your_output_file.csv'
process_csv_file(input_file_path, output_file_path)