Skip to content

Instantly share code, notes, and snippets.

@kapadia
Created April 29, 2016 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kapadia/57969d97d7ac50ac03a34aeae29abdc7 to your computer and use it in GitHub Desktop.
Save kapadia/57969d97d7ac50ac03a34aeae29abdc7 to your computer and use it in GitHub Desktop.
Transform from pixel-space to world-space.
#!/usr/bin/env python
import argparse
import rasterio as rio
import rasterio.warp
def get_coordinates(srcpath, pixel):
with rio.open(srcpath) as src:
print("Coordinate reference system %s" % src.crs['init'])
src_crs = src.crs
# Transform from pixel coordinates to world coordinates in the CRS of the image
xy = src.ul(*pixel)
# To transform to another coordinate reference system
# Note the need to use the image's CRS as an intermediary. There might
# be a way to skip this step.
dst_crs = {'init': 'epsg:4326'}
print rasterio.warp.transform(src_crs, dst_crs, *zip(xy))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("srcpath")
parser.add_argument("--pixel", nargs=2, type=int)
args = parser.parse_args()
get_coordinates(args.srcpath, args.pixel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment