{"id":576,"date":"2017-12-17T12:12:39","date_gmt":"2017-12-17T04:12:39","guid":{"rendered":"http:\/\/www.whudj.cn\/?p=576"},"modified":"2017-12-17T12:12:39","modified_gmt":"2017-12-17T04:12:39","slug":"windows%e4%b8%8bgdal-python%e5%ae%89%e8%a3%85%e4%b8%8e%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.whudj.cn\/?p=576","title":{"rendered":"windows\u4e0bgdal python\u5b89\u88c5\u4e0e\u4f7f\u7528"},"content":{"rendered":"<p>\u6211\u65e5\u5e38\u7684\u7f16\u7a0b\u8bed\u8a00\u662fC++,\u770b\u5230\u5468\u8fb9\u7684\u540c\u4e8b\u6709\u5b66\u4e60python,\u56f4\u89c2\u4e86\u51e0\u4e2a\u5c0f\u4f8b\u5b50\uff0c\u611f\u89c9python\u5f88\u4e0d\u9519\uff0c\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\u3002\u6240\u4ee5\u6211\u6253\u7b97\u5229\u7528\u4e1a\u4f59\u65f6\u95f4\u5b66\u4e60\u4e00\u4e9bpython\u7684\u77e5\u8bc6\uff0c\u5982\u679c\u5b66\u4f1a\u4e86\u7528GPIO \u5e93\u63a7\u5236\u6211\u7684\u6811\u8393\u6d3e\u505a\u4e00\u4e9b\u597d\u73a9\u7684\u4e8b\u60c5\u6700\u597d\u4e86\u3002\u8fd9\u7bc7\u6587\u7ae0\u91cc\u3002\u6211\u6253\u7b97\u5206\u4eab\u4e00\u4e0bgdal python\u5e93\u7684\u5b89\u88c5\u4e0epycharm\u4e0b\u7f16\u5199\u4e00\u4e2a\u8bfb\u53d6shapefile\u5c0f\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u3002<!--more--><\/p>\n<p><strong>1)\u7f16\u8bd1gdal<\/strong><\/p>\n<p>\u9996\u5148\u662f\u5230gdal\u7684\u5b98\u7f51\u4e0a\u4e0b\u8f7d gdal\u7684\u6e90\u7801\uff0c\u6211\u9009\u62e9\u7684\u7248\u672c\u662f2.2.3\u3002<a href=\"https:\/\/trac.osgeo.org\/gdal\/wiki\/DownloadSource\">\u6e90\u7801\u5730\u5740<\/a> \u3002\u56e0\u4e3agdal python\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f \u4f7f\u7528 swig\u5bf9c++\u5bf9\u8c61\u8fdb\u884c\u7684\u5c01\u88c5\uff08\u4ee5\u4e0a\u8bf4\u6cd5\u53ef\u80fd\u4e0d\u592a\u51c6\u786e\uff09\uff0c\u6240\u4ee5\u9700\u8981\u5148\u7528msvc \u7f16\u8bd1gdal\u7684c++\u6e90\u7801\u3002\u5982\u679c\u4f60\u7684\u7535\u8111\u4e5f\u662f64\u4f4d\u64cd\u4f5c\u7cfb\u7edf\uff0c\u8bf7\u5148\u4fee\u6539\u201cnmake.opt\u201d\u6587\u4ef6\u7b2c206\u884c\u5c06<span class=\"lang:vim decode:true  crayon-inline \">#WIN64=YES<\/span>\u00a0\u524d\u9762\u7684#\u53bb\u6389\u3002\u6211\u7535\u8111\u4e0a\u5b89\u88c5\u7684 visual studio 2017 community \u7248\u672c\uff0c\u6240\u4ee5\u6211\u6253\u5f00\u7684\u662f vs2017 \u7248\u672c\u7684\u201cx64 Native Tools Command Prompt for VS 2017\u201d\u547d\u4ee4\u884c\u7a97\u53e3\uff0ccd\u5230\u6e90\u7801\u6240\u5728\u76ee\u5f55\u3002\u6267\u884c\u547d\u4ee4<span class=\"lang:vim decode:true  crayon-inline \">nmake -f makefile.vc MSVC_VER=1910<\/span>\u00a0\u7b49\u5f85\u7a0b\u5e8f\u7f16\u8bd1\u6210\u529f\uff0c\u6267\u884c<span class=\"lang:vim decode:true  crayon-inline \">nmake -f makefile.vc install<\/span>\u00a0\uff0c\u7f16\u8bd1\u751f\u6210\u7684\u94fe\u63a5\u5e93\u3001exe\u7b49\u4f1a\u88ab\u62f7\u8d1d\u5230\u201cC:\\warmerda\\bld\\bin\u201d\uff08\u53ef\u4ee5\u5728nmake.opt\u4e2d\u4fee\u6539\uff09\u3002\u5c06\u4e0a\u9762\u8def\u5f84\u52a0\u5165\u7cfb\u7edf\u53d8\u91cf\u201cpath\u201d\u4e2d\u3002<\/p>\n<p><strong>2) \u5b89\u88c5 gdal python<\/strong><\/p>\n<p>\u5b89\u88c5 gdal python\u6a21\u5757\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u7535\u8111\u4e0a\u5b89\u88c5\u4e86python \u3002cd\u5230\u6e90\u7801\u6240\u5728\u8def\u5f84\u7684swig\/python\u4e0b\uff0c\u6267\u884c\u547d\u4ee4<span class=\"lang:vim decode:true  crayon-inline \">python setup.py build<\/span>\u00a0\uff0c<span class=\"lang:vim decode:true  crayon-inline \">python setup.py install<\/span>\u00a0\u3002\u7136\u540e\u65b0\u5f00\u4e00\u4e2apython \u7a0b\u5e8f\uff0c\u6267\u884c<span class=\"lang:python decode:true  crayon-inline \">import gdal<\/span>\u00a0\uff0c\u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u8bc1\u660egdal \u7684\u5b89\u88c5\u662f\u6210\u529f\u7684\u3002<\/p>\n<p><strong>3) pycharm \u5b89\u88c5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u7f16\u5199python\u9700\u8981\u4e00\u4e2a\u53cb\u597d\u7684IDE,\u540c\u4e8b\u63a8\u8350\u7684\u662fpycharm \u3002\u6211\u5b89\u88c5\u7684\u662fpycharm 2017 community\u7248\u672c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-579\" src=\"http:\/\/www.whudj.cn\/wp-content\/uploads\/2017\/12\/pycharm.png\" alt=\"\" width=\"400\" height=\"247\" \/><\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u65b0\u5efa\u4e00\u4e2a\u5de5\u7a0b\u65f6\uff0cpycharm \u5df2\u7ecf\u81ea\u5e26\u4e00\u4e2apython interpreter(\u89e3\u91ca\u5668)\u4e86,\u800c\u8fd9\u4e2a\u89e3\u91ca\u5668\u5e76\u4e0d\u662f\u521a\u624d\u6211\u4eec\u5b89\u88c5gdal\u7684\u89e3\u91ca\u5668\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u5de5\u7a0b\u7684\u89e3\u91ca\u5668\u6307\u5b9a\u4e3a\u7cfb\u7edf\u7684\u89e3\u91ca\u5668\u3002\u89e3\u51b3\u529e\u6cd5\uff1a\u201cFile-&gt;Settings-&gt;Project:xxx-&gt;Project Interpreter\u201d,\u5728\u5bf9\u8bdd\u6846\u4e2d\u9009\u62e9project interpreter \u7684\u201cshow all\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-580\" src=\"http:\/\/www.whudj.cn\/wp-content\/uploads\/2017\/12\/show_interpreter.png\" alt=\"\" width=\"700\" height=\"122\" \/><\/p>\n<p>\u5728\u5f39\u51fa\u7684\u7a97\u53e3\u4e2d\u70b9\u201c+\u201dadd local\u3002\u9009\u62e9 \u201csystem interpreter\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-581\" src=\"http:\/\/www.whudj.cn\/wp-content\/uploads\/2017\/12\/system_interpreter.png\" alt=\"\" width=\"800\" height=\"115\" \/><\/p>\n<p>4) gdal \u7a0b\u5e8f\u7f16\u5199<\/p>\n<p>\u6211\u6253\u7b97\u7f16\u5199\u4e00\u4e2a\u8bfb\u53d6shapefile\uff0c\u83b7\u5f97\u8868\u5b57\u6bb5\u540d\u79f0\uff0c\u83b7\u53d6\u8981\u7d20\u4e2a\u6570\uff0c\u5e76\u4e14\u6253\u5370\u6bcf\u4e00\u4e2a\u8981\u7d20\u7684wkt\u5b57\u7b26\uff0c\u5e76\u4e14\u6253\u5370\u5176\u5916\u63a5\u77e9\u5f62\u5750\u6807\u3002\u7a0b\u5e8f\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:python decode:true\">import sys\r\nfrom osgeo import gdal\r\nfrom osgeo import ogr\r\n\r\nfilename = \"test_data\/poly.shp\"\r\nds = gdal.OpenEx(filename, gdal.OF_VECTOR)\r\n\r\nif ds is None:\r\n    print(\"can't open \"+filename)\r\n    exit(1)\r\n\r\nlayer = ds.GetLayer(0)\r\nlayer.ResetReading()\r\n\r\n#\u6253\u5370 layer defination\r\nlyrDefn =layer.GetLayerDefn()\r\nfor i in range(lyrDefn.GetFieldCount()):\r\n    fieldDefn = lyrDefn.GetFieldDefn(i)\r\n    print(fieldDefn.GetNameRef())\r\n\r\nprint(\"there are total {0} features in layer\".format(layer.GetFeatureCount()))\r\n\r\ni =1\r\nfor feature in layer:\r\n    print(\"---------{0}th feature--------\".format(i))\r\n    geom = feature.GetGeometryRef()\r\n    print(\"points number {0}\".format(geom.GetPointCount()))\r\n\r\n    print(geom.ExportToWkt())\r\n\r\n    #print(geom.GetGeometryCount())\r\n    env = geom.GetEnvelope()\r\n    for point in env:\r\n        print(point)\r\n\r\n    i = i+1\r\n    #print(geom.Area())<\/pre>\n<p>\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u6211\u7684\u7591\u95ee\u5982\u4e0b\u3002\u6709\u7ecf\u9a8c\u7684\u540c\u5b66\u53ef\u4ee5\u6307\u70b9\u4e00\u4e0b\uff0c\u6211\u540e\u9762\u5b66\u4e60\u5230\u7b54\u6848\uff0c\u4f1a\u56de\u6765\u8865\u5145\u3002<\/p>\n<ol>\n<li>python \u4e2d\u7c7b\u578b\u8f6c\u6362\u662f\u5982\u4f55\u8868\u8fbe\u7684\uff0c\u6bd4\u5982\uff0c\u5728c++\u4e2d\uff0c\u6211\u901a\u8fc7\u5c06Geometry\u5bf9\u8c61\u8f6c\u6210Polygon\u5bf9\u8c61\u4ee5\u4f7f\u7528polygon\u7684\u4e13\u6709\u65b9\u6cd5\uff0c\u5728python \u4e2d\uff0c\u4e0d\u77e5\u9053\u8fd9\u4e2a\u8fc7\u7a0b\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u6216\u8005\uff0c\u6709\u6ca1\u6709\u8fd9\u4e2a\u6982\u5ff5\uff1f<\/li>\n<li>pycharm \u5982\u4f55\u652f\u6301\u7b2c\u4e09\u65b9\u5e93\u7684\u4ee3\u7801\u8865\u5168\uff1f\u7528\u8d77\u6765\u597d\u4e0d\u65b9\u4fbf\u3002<\/li>\n<li>gdal python\u7684<a href=\"http:\/\/gdal.org\/python\/\">API Reference<\/a> \u597d\u50cf\u4e0d\u592a\u5168\uff0c\u91cc\u9762\u597d\u591a\u4e1c\u897f\u90fd\u6ca1\u6709,\u6bd4\u5982\uff0c\u6709\u4e9b\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u5e76\u6ca1\u6709\u6807\u8bb0\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u65e5\u5e38\u7684\u7f16\u7a0b\u8bed\u8a00\u662fC++,\u770b\u5230\u5468\u8fb9\u7684\u540c\u4e8b\u6709\u5b66\u4e60python,\u56f4\u89c2\u4e86\u51e0\u4e2a\u5c0f\u4f8b\u5b50\uff0c\u611f &hellip; <a href=\"http:\/\/www.whudj.cn\/?p=576\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts\/576"}],"collection":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=576"}],"version-history":[{"count":5,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions"}],"predecessor-version":[{"id":584,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions\/584"}],"wp:attachment":[{"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.whudj.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}