TransWikia.com

Python function which recognises when to convert coordinate system

Geographic Information Systems Asked on August 15, 2021

How do I write Python code for an AWS lambda function which will convert easting and northings to longitude and latitude?

For example, if a CSV is uploaded into an S3 bucket it may contain either eastings/northings or longitude/latitude columns for the location of different features. I’d like to create a Python code which recognises this and knows what to do… e.g.

if in long/lat format = leave and move on to next function
else:
if in easting/northing then convert to long/lat and move on to next function

So far, I have my working Python code which will convert the eastings and northings to longitude and latitude (see below). Next I just neeed to write a function that recognises when to do this conversion.

from pyproj import Proj, transform

inProj = Proj(init='epsg:27700') #British National Grid
outProj = Proj(init='epsg:4326') #WGS84
x1,y1 = 357551,259776 #(x,y coordinates)
x2,y2 = transform(inProj,outProj,x1,y1) # convert to long/lat
print (y2,x2)

edit
The CSV that is uploaded to the S3 bucket has the following headers for ease of reference.

Feature Easting Northing Length Date Comments
Feature1 181016.6 32794.39 6.5 metres 17/01/2020

2 Answers

If you only have two possible CRS inputs, and one is a projected system in meters whereas the other one is in decimal degrees, have you thought about using a regex pattern to determine whether the input is one or the other?

Here's how you would detect a decimal degree input. Feel free to update the pattern if your input is DMS or something else. Constructing a regex pattern for a metric system is considerably simpler, but depends on the min max bounds of that particular system.

import pandas as pd
import re


pattern = r'[-+]?([1-8]?d(.d+)?|90(.0+)?),s*[-+]?(180(.0+)?|((1[0-7]d)|([1-9]?d))(.d+)?)'
df = pd.read_csv('file.csv')

df['dd'] = df['lat'].astype(str) + ',' + df['lon'].astype(str)
for i in df['dd']:
    if re.match(pattern,str(i)):
        print('coordinate {} is in decimal degrees'.format(i))

Answered by Encomium on August 15, 2021

Use the WGS 84 bounding box to determined if WGS 84 or EPSG:27700. I got it from https://epsg.io/27700

# Considering easting = lon and northing = lat
if easting >= -8.82 and easting <= 1.92 and northing >= 49.79 and northing <= 60.94:
    print("Probably in lon, lat, don't do anything")
else:
    print("Probably in EPSG:27700, so do use your reprojection stuff")

I've also checked where are located the bounding box in WGS 84 to be sure you could not have EPSG:27700 points with these coordinates. Probably not except if your data are related to boats positions (corners are in sea area) as you can see if you follow below links

Answered by ThomasG77 on August 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP