[GIS] How to save json returned by gdalinfo in python variable

gdalosgeoosgeo4wpython

When I run the command in osgeo4W shell

gdalinfo -json C:\Users\Gurminder\Documents\Git\Dump\s1a-ew-grd-hh-20160725t043517-20160725t043617-012300-01322b-001.tiff

I get a large json of my Ground Control Points in my Geotiff on osgeo4W shell

        "id":"415",
        "info":"",
        "pixel":7860.0,
        "line":9500.0,
        "x":41.044310052891461,
        "y":80.384405675842217,
        "z":0.000080652534962
      },
      {
        "id":"416",
        "info":"",
        "pixel":8384.0,
        "line":9500.0,
        "x":39.992034664274136,
        "y":80.451811504231259,
        "z":0.000068210996687
      },
      {
        "id":"417",
        "info":"",
        "pixel":8908.0,
        "line":9500.0,
        "x":38.925295378731292,
        "y":80.516011620653984,
        "z":0.00005738902837
      },
      {
        "id":"418",
        "info":"",
        "pixel":9432.0,
        "line":9500.0,
        "x":37.844523013673438,
        "y":80.576940552941835,
        "z":0.000047997571528
      },
      {
        "id":"419",
        "info":"",
        "pixel":9956.0,
        "line":9500.0,
        "x":36.750199046384161,
        "y":80.634534507475493,
        "z":0.000039840117097
      },
      {
        "id":"420",
        "info":"",
        "pixel":10464.0,
        "line":9500.0,
        "x":35.67685495909776,
        "y":80.687127644427321,
        "z":0.000033005140722
      },
      {
        "id":"421",
        "info":"",
        "pixel":0.0,
        "line":9990.0,
        "x":54.402523858527992,
        "y":78.888970505105561,
        "z":0.00080771651119
      },
      {
        "id":"422",
        "info":"",
        "pixel":524.0,
        "line":9990.0,
        "x":53.594594754429728,
        "y":78.994877546416106,
        "z":0.00068476703018
      },
      {
        "id":"423",
        "info":"",
        "pixel":1048.0,
        "line":9990.0,
        "x":52.771326645764503,
        "y":79.09860793751308,
        "z":0.000583708286285
      },
      {
        "id":"424",
        "info":"",
        "pixel":1572.0,
        "line":9990.0,
        "x":51.932609464913519,
        "y":79.200099113399347,
        "z":0.000499621964991
      },
      {
        "id":"425",
        "info":"",
        "pixel":2096.0,
        "line":9990.0,
        "x":51.078355257846297,
        "y":79.299287448374685,
        "z":0.000428957864642
      },
      {
        "id":"426",
        "info":"",
        "pixel":2620.0,
        "line":9990.0,
        "x":50.208499784506166,
        "y":79.396108400008572,
        "z":0.000369090586901
      },
      {
        "id":"427",
        "info":"",
        "pixel":3144.0,
        "line":9990.0,
        "x":49.323004461292157,
        "y":79.490496622303397,
        "z":0.000318020582199
      },
      {
        "id":"428",
        "info":"",
        "pixel":3668.0,
        "line":9990.0,
        "x":48.421858453237064,
        "y":79.582386076574608,
        "z":0.000274220481515
      },
      {
        "id":"429",
        "info":"",
        "pixel":4192.0,
        "line":9990.0,
        "x":47.505080811550357,
        "y":79.671710152726646,
        "z":0.000236473977566
      },
      {
        "id":"430",
        "info":"",
        "pixel":4716.0,
        "line":9990.0,
        "x":46.572722598777816,
        "y":79.758401805672122,
        "z":0.000203838571906
      },
      {
        "id":"431",
        "info":"",
        "pixel":5240.0,
        "line":9990.0,
        "x":45.624868968480804,
        "y":79.8423937077754,
        "z":0.000175525434315
      },
      {
        "id":"432",
        "info":"",
        "pixel":5764.0,
        "line":9990.0,
        "x":44.661641178464464,
        "y":79.923618416502279,
        "z":0.000150921754539
      },
      {
        "id":"433",
        "info":"",
        "pixel":6288.0,
        "line":9990.0,
        "x":43.683198521075894,
        "y":80.002008555919033,
        "z":0.000129488296807
      },
      {
        "id":"434",
        "info":"",
        "pixel":6812.0,
        "line":9990.0,
        "x":42.689740153517683,
        "y":80.077497010754328,
        "z":0.000110816210508
      },
      {
        "id":"435",
        "info":"",
        "pixel":7336.0,
        "line":9990.0,
        "x":41.681506806995351,
        "y":80.150017132083008,
        "z":0.00009514670819
      },
      {
        "id":"436",
        "info":"",
        "pixel":7860.0,
        "line":9990.0,
        "x":40.658782346748879,
        "y":80.219502954099653,
        "z":0.00008085463196
      },
      {
        "id":"437",
        "info":"",
        "pixel":8384.0,
        "line":9990.0,
        "x":39.621895146303608,
        "y":80.285889421813877,
        "z":0.000068411231041
      },
      {
        "id":"438",
        "info":"",
        "pixel":8908.0,
        "line":9990.0,
        "x":38.571219229101906,
        "y":80.349112629643642,
        "z":0.000057565048337
      },
      {
        "id":"439",
        "info":"",
        "pixel":9432.0,
        "line":9990.0,
        "x":37.507175119584744,
        "y":80.409110070861843,
        "z":0.000048157759011
      },
      {
        "id":"440",
        "info":"",
        "pixel":9956.0,
        "line":9990.0,
        "x":36.43023033424695,
        "y":80.465820897486239,
        "z":0.000039997510612
      },
      {
        "id":"441",
        "info":"",
        "pixel":10464.0,
        "line":9990.0,
        "x":35.374339433997491,
        "y":80.517606789267248,
        "z":0.000033148564398
      }
    ]
  },
  "metadata":{
    "":{
      "AREA_OR_POINT":"Area",
      "TIFFTAG_DATETIME":"2016:07:25 04:52:55",
      "TIFFTAG_IMAGEDESCRIPTION":"Sentinel-1A EW GRD MR L1",
      "TIFFTAG_SOFTWARE":"Sentinel-1 IPF 002.71"
    },
    "IMAGE_STRUCTURE":{
      "INTERLEAVE":"BAND"
    }
  },
  "cornerCoordinates":{
    "upperLeft":[
      0.0,
      0.0
    ],
    "lowerLeft":[
      0.0,
      9991.0
    ],
    "upperRight":[
      10465.0,
      0.0
    ],
    "lowerRight":[
      10465.0,
      9991.0
    ],
    "center":[
      5232.5,
      4995.5
    ]
  },
  "wgs84Extent":{
    "type":"Polygon",
    "coordinates":[
      [
      ]
    ]
  },
  "bands":[
    {
      "band":1,
      "block":[
        10465,
        1
      ],
      "type":"UInt16",
      "colorInterpretation":"Gray",
      "metadata":{
      }
    }
  ]
}

I am writing a python script where with gdal, I am trying to transfer GCP's from one file to another, this file has 411 GCP's, for transferring this huge list I am planning to create a loop with gdal_tranfer and send iteratively all the GCP's to the new file.

From python by using os.system() I can run gdalinfo but I cannot save the returned result into a python variable, is there any way of saving the entire json returned by gdalinfo into python variable?

Best Answer

I've done this with calls to curl, here's a snippet. The trick is to use subprocess.check_output instead of os.system:

import subprocess
layer_name = subprocess.check_output("curl -s {0}/{1}?f=pjson | json -a name".format(service_url, layer_number), shell=True)

So in your case, perhaps something like (untested):

import subprocess
ret = subprocess.check_output("gdalinfo -json C:\Users\Gurminder\Documents\Git\Dump\s1a-ew-grd-hh-20160725t043517-20160725t043617-012300-01322b-001.tiff", shell=True)

Since you are getting json back, you might be able to do the following, otherwise you might just get a string back instead of json (untested):

import json
import subprocess
actual_json = json.loads(subprocess.check_output("gdalinfo -json C:\Users\Gurminder\Documents\Git\Dump\s1a-ew-grd-hh-20160725t043517-20160725t043617-012300-01322b-001.tiff", shell=True))
Related Question