Skip to content

Instantly share code, notes, and snippets.

@kapadia
Last active August 29, 2015 14:03
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/0f81ea84bc176aef4516 to your computer and use it in GitHub Desktop.
Save kapadia/0f81ea84bc176aef4516 to your computer and use it in GitHub Desktop.
import sys
import numpy as np
import rasterio as rio
def copy(srcpath, dstpath):
with rio.drivers():
with rio.open(srcpath, 'r') as src:
metadata = src.meta
r, g, b = map(src.read_band, (1, 2, 3))
metadata.update(count=3)
with rio.open(dstpath, 'w', **metadata) as dst:
dst.transform = metadata["affine"]
dst.crs = metadata["crs"]
dst.write_band(1, r)
dst.write_band(2, g)
dst.write_band(3, b)
if __name__ == '__main__':
if len(sys.argv) != 3:
sys.exit()
copy(*sys.argv[1:3])
@kapadia
Copy link
Author

kapadia commented Jul 3, 2014

gdalinfo before

Driver: GTiff/GeoTIFF
Files: m_3907717_se_18_1_20130815-wm.tif
Size is 642, 792
Coordinate System is:
PROJCS["WGS 84 / Pseudo-Mercator",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0],
        UNIT["degree",0.0174532925199433],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Mercator_1SP"],
    PARAMETER["central_meridian",0],
    PARAMETER["scale_factor",1],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs"],
    AUTHORITY["EPSG","3857"]]
Origin = (-8676660.212174817919731,4821264.076014802791178)
Pixel Size = (13.006831336164968,-13.006831336164968)
Metadata:
  AREA_OR_POINT=Area
  TIFFTAG_DOCUMENTNAME=Clear Spring SE 3907717
  TIFFTAG_IMAGEDESCRIPTION=USDA-FSA-APFO National Agriculture Imagery Program
  TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
  TIFFTAG_XRESOLUTION=72
  TIFFTAG_YRESOLUTION=72
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (-8676660.212, 4821264.076) ( 77d56'37.55"W, 39d41'30.68"N)
Lower Left  (-8676660.212, 4810962.666) ( 77d56'37.55"W, 39d37'14.20"N)
Upper Right (-8668309.826, 4821264.076) ( 77d52' 7.51"W, 39d41'30.68"N)
Lower Right (-8668309.826, 4810962.666) ( 77d52' 7.51"W, 39d37'14.20"N)
Center      (-8672485.019, 4816113.371) ( 77d54'22.53"W, 39d39'22.47"N)
Band 1 Block=642x3 Type=Byte, ColorInterp=Red
  NoData Value=0
Band 2 Block=642x3 Type=Byte, ColorInterp=Green
  NoData Value=0
Band 3 Block=642x3 Type=Byte, ColorInterp=Blue
  NoData Value=0
Band 4 Block=642x3 Type=Byte, ColorInterp=Alpha
  NoData Value=0

gdalinfo after

Driver: GTiff/GeoTIFF
Files: m_3907717_se_18_1_20130815-wm-copy.tif
Size is 642, 792
Coordinate System is:
PROJCS["unnamed",
    GEOGCS["unnamed ellipse",
        DATUM["unknown",
            SPHEROID["unnamed",6378137,0]],
        PRIMEM["Greenwich",0],
        UNIT["degree",0.0174532925199433]],
    PROJECTION["Mercator_1SP"],
    PARAMETER["central_meridian",0],
    PARAMETER["scale_factor",1],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]]]
Origin = (-8676660.212174817919731,4821264.076014802791178)
Pixel Size = (13.006831336164968,-13.006831336164968)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (-8676660.212, 4821264.076) ( 77d56'37.55"W, 39d41'30.68"N)
Lower Left  (-8676660.212, 4810962.666) ( 77d56'37.55"W, 39d37'14.20"N)
Upper Right (-8668309.826, 4821264.076) ( 77d52' 7.51"W, 39d41'30.68"N)
Lower Right (-8668309.826, 4810962.666) ( 77d52' 7.51"W, 39d37'14.20"N)
Center      (-8672485.019, 4816113.371) ( 77d54'22.53"W, 39d39'22.47"N)
Band 1 Block=642x4 Type=Byte, ColorInterp=Red
  NoData Value=0
Band 2 Block=642x4 Type=Byte, ColorInterp=Green
  NoData Value=0
Band 3 Block=642x4 Type=Byte, ColorInterp=Blue
  NoData Value=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment