Created
March 27, 2020 13:25
-
-
Save GerardoLopez/2f9ad4b22925aee9f9ee611891b1bd68 to your computer and use it in GitHub Desktop.
Transforms coordinates between different Spatial Reference System (SRS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Transforms from one Spatial Reference System (SRS) to another one | |
# the SRSs must be define from their PROJ4 or WKT strings | |
import osr | |
# Sinusoidal definition | |
# from https://spatialreference.org/ref/sr-org/6842/ | |
# It fully match with the metadata in the MODIS products | |
sinusoidal_srs = (f'+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 ' | |
f'+b=6371007.181 +units=m +no_defs ') | |
# British National Grid (BNG) definition | |
# from https://spatialreference.org/ref/epsg/osgb-1936-british-national-grid/ | |
# User should pass those parameters? | |
bng_srs = (f' +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 ' | |
f' +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 ' | |
f' +units=m +no_defs ') | |
# Source spatial reference system | |
src = osr.SpatialReference() | |
src.ImportFromProj4(sinusoidal_srs) | |
# Target | |
tgt = osr.SpatialReference() | |
tgt.ImportFromProj4(bng_srs) | |
# Transform | |
transform = osr.CoordinateTransformation(src, tgt) | |
coords = transform.TransformPoint(-317759.094, 6162636.771) | |
print('Sinusoidal coordinates: -317759.094, 6162636.771') | |
print(f'To BNG: {coords}') | |
# Target | |
tgt = osr.SpatialReference() | |
tgt.ImportFromEPSG(4326) | |
transform = osr.CoordinateTransformation(src, tgt) | |
coords = transform.TransformPoint(-317759.094, 6162636.771) | |
print(f'To Lat/Lon: {coords}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment