{"id":943471,"date":"2024-12-26T22:41:01","date_gmt":"2024-12-26T14:41:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/943471.html"},"modified":"2024-12-26T22:41:03","modified_gmt":"2024-12-26T14:41:03","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8gdalinfo","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/943471.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528gdalinfo"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080854\/6b837ab4-ded6-49cc-8d54-012bf646fd9e.webp\" alt=\"python\u5982\u4f55\u8c03\u7528gdalinfo\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528gdalinfo\u7684\u6838\u5fc3\u65b9\u6cd5\u662f\u4f7f\u7528gdal\u5e93\u63d0\u4f9b\u7684Python\u7ed1\u5b9a\u3001\u901a\u8fc7osgeo.gdal\u6a21\u5757\u83b7\u53d6\u5f71\u50cf\u4fe1\u606f\u3001\u89e3\u6790\u548c\u5904\u7406\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528gdalinfo\u7684\u6838\u5fc3\u6b65\u9aa4\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u70b9\u662f<strong>\u5229\u7528osgeo.gdal\u6a21\u5757\u6765\u52a0\u8f7d\u5f71\u50cf\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528\u5176\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u5f71\u50cf\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong>\u3002\u8fd9\u4e0d\u4ec5\u5305\u62ec\u57fa\u672c\u7684\u4fe1\u606f\u63d0\u53d6\uff0c\u8fd8\u5305\u62ec\u5982\u4f55\u89e3\u6790\u548c\u5904\u7406\u8fd9\u4e9b\u4fe1\u606f\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001GDAL\u53ca\u5176Python\u7ed1\u5b9a\u6982\u8ff0<\/p>\n<\/p>\n<p><p>GDAL\uff08Geospatial Data Abstraction Library\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6\u3001\u5199\u5165\u548c\u8f6c\u6362\u5730\u7406\u7a7a\u95f4\u6570\u636e\u683c\u5f0f\u7684\u5f00\u6e90\u5e93\u3002GDAL\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\uff0c\u5982GeoTIFF\u3001Shapefile\u3001NetCDF\u7b49\uff0c\u662f\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff08GIS\uff09\u548c\u9065\u611f\u5e94\u7528\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u5de5\u5177\u3002GDAL\u7684Python\u7ed1\u5b9a\u63d0\u4f9b\u4e86\u5bf9\u8fd9\u4e9b\u529f\u80fd\u7684\u8bbf\u95ee\u63a5\u53e3\uff0c\u4f7f\u5f97Python\u5f00\u53d1\u8005\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5730\u7406\u7a7a\u95f4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>Python\u7ed1\u5b9a\u7684GDAL\u4e3b\u8981\u901a\u8fc7osgeo\u5e93\u6765\u5b9e\u73b0\u3002\u5728osgeo\u5e93\u4e2d\uff0cgdal\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6805\u683c\u6570\u636e\u7684\u64cd\u4f5c\uff0c\u800cogr\u6a21\u5757\u5219\u7528\u4e8e\u77e2\u91cf\u6570\u636e\u3002\u4f7f\u7528Python\u7ed1\u5b9a\u7684GDAL\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528gdalinfo\u7684\u529f\u80fd\u6765\u83b7\u53d6\u6805\u683c\u6570\u636e\u96c6\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u5c3a\u5bf8\u3001\u6295\u5f71\u3001\u6ce2\u6bb5\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5GDAL\u53ca\u5176Python\u7ed1\u5b9a<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528GDAL\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5GDAL\u5e93\u53ca\u5176Python\u7ed1\u5b9a\u3002\u5b89\u88c5GDAL\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u5305\u7ba1\u7406\u5668\u3001\u6e90\u7801\u7f16\u8bd1\u6216Python\u5305\u7ba1\u7406\u5de5\u5177\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u7cfb\u7edf\u5305\u7ba1\u7406\u5668<\/li>\n<\/ol>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5982apt\u6216yum\u6765\u5b89\u88c5GDAL\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install gdal-bin<\/p>\n<p>sudo apt-get install python3-gdal<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528conda\u5b89\u88c5<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8eAnaconda\u7528\u6237\uff0c\u53ef\u4ee5\u901a\u8fc7conda\u547d\u4ee4\u6765\u5b89\u88c5GDAL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda install -c conda-forge gdal<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528pip\u5b89\u88c5<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u73af\u5883\u4e0b\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528pip\u6765\u5b89\u88c5GDAL\u7684Python\u7ed1\u5b9a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install gdal<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528pip\u5b89\u88c5\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u4f9d\u8d56\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528conda\u6216\u7cfb\u7edf\u5305\u7ba1\u7406\u5668\u6765\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8c03\u7528gdalinfo\u83b7\u53d6\u5f71\u50cf\u4fe1\u606f<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u597dGDAL\u53ca\u5176Python\u7ed1\u5b9a\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u811a\u672c\u6765\u8c03\u7528gdalinfo\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u5f71\u50cf\u6587\u4ef6\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from osgeo import gdal<\/p>\n<h2><strong>\u6253\u5f00\u5f71\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>dataset = gdal.Open(&#39;path\/to\/your\/image.tif&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f71\u50cf\u5c3a\u5bf8<\/strong><\/h2>\n<p>width = dataset.RasterXSize<\/p>\n<p>height = dataset.RasterYSize<\/p>\n<p>print(f&#39;Width: {width}, Height: {height}&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f71\u50cf\u6ce2\u6bb5\u6570<\/strong><\/h2>\n<p>band_count = dataset.RasterCount<\/p>\n<p>print(f&#39;Band count: {band_count}&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f71\u50cf\u6295\u5f71\u4fe1\u606f<\/strong><\/h2>\n<p>projection = dataset.GetProjection()<\/p>\n<p>print(f&#39;Projection: {projection}&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f71\u50cf\u5730\u7406\u53d8\u6362\u53c2\u6570<\/strong><\/h2>\n<p>geotransform = dataset.GetGeoTransform()<\/p>\n<p>print(f&#39;GeoTransform: {geotransform}&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u96c6<\/strong><\/h2>\n<p>dataset = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528gdal\u6a21\u5757\u7684Open\u65b9\u6cd5\u6253\u5f00\u5f71\u50cf\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7\u76f8\u5e94\u7684\u65b9\u6cd5\u83b7\u53d6\u5f71\u50cf\u7684\u57fa\u672c\u4fe1\u606f\u3002\u901a\u8fc7GetProjection\u548cGetGeoTransform\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u5f71\u50cf\u7684\u6295\u5f71\u4fe1\u606f\u548c\u5730\u7406\u53d8\u6362\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u89e3\u6790\u548c\u5904\u7406gdalinfo\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u4e86\u5f71\u50cf\u7684\u57fa\u672c\u4fe1\u606f\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u8fd9\u4e9b\u4fe1\u606f\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u89e3\u6790\u548c\u5904\u7406\uff0c\u4ee5\u6ee1\u8db3\u5177\u4f53\u7684\u5e94\u7528\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5f71\u50cf\u7684\u5730\u7406\u53d8\u6362\u53c2\u6570\u8f6c\u6362\u4e3a\u5730\u7406\u5750\u6807\uff0c\u6216\u8005\u6839\u636e\u5f71\u50cf\u7684\u6ce2\u6bb5\u4fe1\u606f\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\u89e3\u6790\u5730\u7406\u53d8\u6362\u53c2\u6570<\/li>\n<\/ol>\n<p><p>\u5730\u7406\u53d8\u6362\u53c2\u6570\u901a\u5e38\u5305\u542b\u516d\u4e2a\u5143\u7d20\uff0c\u5206\u522b\u8868\u793a\u5f71\u50cf\u7684\u539f\u70b9\u5750\u6807\u3001\u50cf\u7d20\u5927\u5c0f\u548c\u65cb\u8f6c\u89d2\u5ea6\u3002\u901a\u8fc7\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u5f71\u50cf\u5750\u6807\u8f6c\u6362\u4e3a\u5730\u7406\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def pixel_to_geo(geotransform, x, y):<\/p>\n<p>    &quot;&quot;&quot;\u5c06\u50cf\u7d20\u5750\u6807\u8f6c\u6362\u4e3a\u5730\u7406\u5750\u6807&quot;&quot;&quot;<\/p>\n<p>    x_geo = geotransform[0] + x * geotransform[1] + y * geotransform[2]<\/p>\n<p>    y_geo = geotransform[3] + x * geotransform[4] + y * geotransform[5]<\/p>\n<p>    return x_geo, y_geo<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u5c06(100, 100)\u50cf\u7d20\u5750\u6807\u8f6c\u6362\u4e3a\u5730\u7406\u5750\u6807<\/strong><\/h2>\n<p>x_geo, y_geo = pixel_to_geo(geotransform, 100, 100)<\/p>\n<p>print(f&#39;Geographic coordinates: ({x_geo}, {y_geo})&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5904\u7406\u6ce2\u6bb5\u4fe1\u606f<\/li>\n<\/ol>\n<p><p>\u5728\u83b7\u53d6\u5f71\u50cf\u7684\u6ce2\u6bb5\u4fe1\u606f\u540e\uff0c\u53ef\u4ee5\u5bf9\u6ce2\u6bb5\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5982\u8ba1\u7b97\u7edf\u8ba1\u4fe1\u606f\u3001\u56fe\u50cf\u589e\u5f3a\u7b49\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u6ce2\u6bb5\u6570\u636e\uff0c\u5e76\u8ba1\u7b97\u6ce2\u6bb5\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7b2c\u4e00\u6ce2\u6bb5<\/p>\n<p>band = dataset.GetRasterBand(1)<\/p>\n<h2><strong>\u8bfb\u53d6\u6ce2\u6bb5\u6570\u636e<\/strong><\/h2>\n<p>data = band.ReadAsArray()<\/p>\n<h2><strong>\u8ba1\u7b97\u6ce2\u6bb5\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c<\/strong><\/h2>\n<p>min_value = data.min()<\/p>\n<p>max_value = data.max()<\/p>\n<p>print(f&#39;Band 1: Min={min_value}, Max={max_value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u8c03\u7528gdalinfo\u7684\u529f\u80fd\u6765\u83b7\u53d6\u5f71\u50cf\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u4fe1\u606f\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u3002\u8fd9\u4e3a\u5730\u7406\u7a7a\u95f4\u6570\u636e\u7684\u5206\u6790\u548c\u5e94\u7528\u63d0\u4f9b\u4e86\u5f3a\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528gdalinfo\u547d\u4ee4\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u8c03\u7528gdalinfo\u547d\u4ee4\u3002\u901a\u8fc7<code>subprocess.run()<\/code>\u6216<code>subprocess.Popen()<\/code>\uff0c\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u5e76\u83b7\u53d6\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\n# \u5b9a\u4e49gdalinfo\u547d\u4ee4\u548c\u53c2\u6570\ncommand = [&#39;gdalinfo&#39;, &#39;your_file.tif&#39;]\n\n# \u8c03\u7528gdalinfo\nresult = subprocess.run(command, capture_output=True, text=True)\n\n# \u8f93\u51fa\u7ed3\u679c\nprint(result.stdout)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7075\u6d3b\u5730\u4f20\u9012\u53c2\u6570\u5e76\u5904\u7406\u8fd4\u56de\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528gdalinfo\u65f6\uff0c\u5982\u4f55\u5904\u7406\u8f93\u51fa\u4fe1\u606f\uff1f<\/strong><br \/>gdalinfo\u7684\u8f93\u51fa\u901a\u5e38\u662f\u5173\u4e8e\u6805\u683c\u6570\u636e\u96c6\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u5927\u5c0f\u3001\u6295\u5f71\u3001\u6ce2\u6bb5\u4fe1\u606f\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790<code>result.stdout<\/code>\u4e2d\u7684\u5185\u5bb9\u6765\u63d0\u53d6\u9700\u8981\u7684\u4fe1\u606f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u7b5b\u9009\u51fa\u7279\u5b9a\u4fe1\u606f\u3002<\/p>\n<p><strong>gdalinfo\u662f\u5426\u652f\u6301Python\u76f4\u63a5\u8c03\u7528\u800c\u65e0\u9700\u547d\u4ee4\u884c\uff1f<\/strong><br \/>gdalinfo\u662fGDAL\u5e93\u4e2d\u7684\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4f46GDAL\u4e5f\u63d0\u4f9b\u4e86Python API\u3002\u901a\u8fc7\u4f7f\u7528GDAL\u7684Python\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u5728Python\u4e2d\u83b7\u53d6\u4e0egdalinfo\u76f8\u540c\u7684\u4fe1\u606f\uff0c\u800c\u65e0\u9700\u8c03\u7528\u547d\u4ee4\u884c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>gdal.Open()<\/code>\u65b9\u6cd5\u52a0\u8f7d\u6570\u636e\u96c6\uff0c\u7136\u540e\u4f7f\u7528\u76f8\u5173\u65b9\u6cd5\u83b7\u53d6\u5176\u5c5e\u6027\u548c\u5143\u6570\u636e\u3002\u8fd9\u6837\u505a\u7684\u4f18\u70b9\u5728\u4e8e\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u66f4\u597d\u7684\u9519\u8bef\u5904\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528gdalinfo\u7684\u6838\u5fc3\u65b9\u6cd5\u662f\u4f7f\u7528gdal\u5e93\u63d0\u4f9b\u7684Python\u7ed1\u5b9a\u3001\u901a\u8fc7osgeo.gdal\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":943473,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943471"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=943471"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943471\/revisions"}],"predecessor-version":[{"id":943474,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943471\/revisions\/943474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/943473"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=943471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=943471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=943471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}