Search code examples
pythonpython-3.xpandasgislatitude-longitude

Python Geo-Spatial Coordinate Format Conversion


I have a dataframe containing 6 columns of coordinate pairs:

Degrees|Minutes|Seconds

(for both latitude and longitude). This is known as the NAD83 format.

I want to convert these into a new dataframe of only 2 columns in decimal format, known as NAD27.

The library I typically use, geopy supports virtually every format, so there actually isn't a dedicated conversion function. I went through the documentation here to be sure: https://geopy.readthedocs.io/en/1.10.0/

Does python have any other means to convert to NAD27?


Solution

  • Let's suppose your DataFrame df contains columns lonD, lonM, lonS, latD, latM and latS. Then the following should work, using geopandas, shapely and pyproj internally.

    import geopandas as gpd
    import numpy as np
    from shapely.geometry import Point
    
    def dms_to_dec(d, m, s):
        sign = 1 - 2 * np.signbit(d)
        return d + sign * m / 60 + sign * s / 3600
        
    points = df.apply(lambda row: Point(dms_to_dec(*row[['lonD', 'lonM', 'lonS']]), 
                                        dms_to_dec(*row[['latD', 'latM', 'latS']])),
                      axis=1)
    gdf_nad83 = gpd.GeoDataFrame(df, geometry=points, crs={'init': 'EPSG:4269'})
    gdf_nad27 = gdf_nad83.to_crs({'init': 'EPSG:4267'})