gdal_calc.py是一個(gè)gdal用于進(jìn)行柵格計(jì)算的腳本,其功能類似于ArcGIS Desktop/Spatial Analyst Tools/Map Algebra/Raster Calculator工具。
conda install -c conda-forge gdal的方式安裝報(bào)錯(cuò):
Collecting package metadata (current_repodata.json): failed
>>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<
Traceback (most recent call last):
File "D:\software\Miniconda3\lib\site-packages\conda\exceptions.py", line 1079, in __call__
return func(*args, **kwargs)
File "D:\software\Miniconda3\lib\site-packages\conda\cli\main.py", line 84, in _main
exit_code = do_call(args, p)
File "D:\software\Miniconda3\lib\site-packages\conda\cli\conda_argparse.py", line 83, in do_call
return getattr(module, func_name)(args, parser)
File "D:\software\Miniconda3\lib\site-packages\conda\cli\main_install.py", line 20, in execute
install(args, parser, 'install')
File "D:\software\Miniconda3\lib\site-packages\conda\cli\install.py", line 261, in install
unlink_link_transaction = solver.solve_for_transaction(
File "D:\software\Miniconda3\lib\site-packages\conda\core\solve.py", line 114, in solve_for_transaction
unlink_precs, link_precs = self.solve_for_diff(update_modifier, deps_modifier,
File "D:\software\Miniconda3\lib\site-packages\conda\core\solve.py", line 157, in solve_for_diff
final_precs = self.solve_final_state(update_modifier, deps_modifier, prune, ignore_pinned,
File "D:\software\Miniconda3\lib\site-packages\conda\core\solve.py", line 262, in solve_final_state
ssc = self._collect_all_metadata(ssc)
File "D:\software\Miniconda3\lib\site-packages\conda\common\io.py", line 88, in decorated
return f(*args, **kwds)
File "D:\software\Miniconda3\lib\site-packages\conda\core\solve.py", line 425, in _collect_all_metadata
index, r = self._prepare(prepared_specs)
File "D:\software\Miniconda3\lib\site-packages\conda\core\solve.py", line 1020, in _prepare
reduced_index = get_reduced_index(self.prefix, self.channels,
File "D:\software\Miniconda3\lib\site-packages\conda\core\index.py", line 288, in get_reduced_index
new_records = SubdirData.query_all(spec, channels=channels, subdirs=subdirs,
File "D:\software\Miniconda3\lib\site-packages\conda\core\subdir_data.py", line 140, in query_all
result = tuple(concat(executor.map(subdir_query, channel_urls)))
File "D:\software\Miniconda3\lib\concurrent\futures\_base.py", line 608, in result_iterator
yield fs.pop().result()
File "D:\software\Miniconda3\lib\concurrent\futures\_base.py", line 445, in result
return self.__get_result()
File "D:\software\Miniconda3\lib\concurrent\futures\_base.py", line 390, in __get_result
raise self._exception
File "D:\software\Miniconda3\lib\concurrent\futures\thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "D:\software\Miniconda3\lib\site-packages\conda\core\subdir_data.py", line 132, in <lambda>
subdir_query = lambda url: tuple(SubdirData(Channel(url), repodata_fn=repodata_fn).query(
File "D:\software\Miniconda3\lib\site-packages\conda\core\subdir_data.py", line 145, in query
self.load()
File "D:\software\Miniconda3\lib\site-packages\conda\core\subdir_data.py", line 210, in load
_internal_state = self._load()
File "D:\software\Miniconda3\lib\site-packages\conda\core\subdir_data.py", line 375, in _load
raw_repodata_str = fetch_repodata_remote_request(
File "D:\software\Miniconda3\lib\site-packages\conda\core\subdir_data.py", line 701, in fetch_repodata_remote_request
resp = session.get(join_url(url, filename), headers=headers, proxies=session.proxies,
File "D:\software\Miniconda3\lib\site-packages\requests\sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "D:\software\Miniconda3\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "D:\software\Miniconda3\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "D:\software\Miniconda3\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "D:\software\Miniconda3\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "D:\software\Miniconda3\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "D:\software\Miniconda3\lib\site-packages\urllib3\connection.py", line 359, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "D:\software\Miniconda3\lib\site-packages\urllib3\connection.py", line 500, in _connect_tls_proxy
return ssl_wrap_socket(
File "D:\software\Miniconda3\lib\site-packages\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "D:\software\Miniconda3\lib\site-packages\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "D:\software\Miniconda3\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "D:\software\Miniconda3\lib\ssl.py", line 997, in _create
raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname
$ D:\software\Miniconda3\Scripts\conda-script.py install -c conda-forge gdal
environment variables:
CIO_TEST=<not set>
CONDA_DEFAULT_ENV=base
CONDA_EXE=D:\software\Miniconda3\condabin..\Scripts\conda.exe
CONDA_EXES="D:\software\Miniconda3\condabin..\Scripts\conda.exe"
CONDA_PREFIX=D:\software\Miniconda3
CONDA_PROMPT_MODIFIER=(base)
CONDA_PYTHON_EXE=D:\software\Miniconda3\python.exe
CONDA_ROOT=D:\software\Miniconda3
CONDA_SHLVL=1
CURL_CA_BUNDLE=<not set>
HOMEPATH=\Users\liu
PATH=D:\software\Miniconda3;D:\software\Miniconda3\Library\mingw-w64\bin;D:
\software\Miniconda3\Library\usr\bin;D:\software\Miniconda3\Library\bi
n;D:\software\Miniconda3\Scripts;D:\software\Miniconda3\bin;D:\softwar
e\Miniconda3;D:\software\Miniconda3\Library\mingw-w64\bin;D:\software
Miniconda3\Library\usr\bin;D:\software\Miniconda3\Library\bin;D:\softw
are\Miniconda3\Scripts;D:\software\Miniconda3\bin;D:\software\Minicond
a3\condabin;D:\software\python39\Scripts;D:\software\python39;C:\Pytho
n27;C:\Python27\Scripts;D:\software\vmware\bin;C:\Windows\system32;C:
Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell
\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA
Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA
NvDLISR;D:\software\git\Git\cmd;D:\software\nvm\nvm;C:\Program
Files\nodejs;D:\software\xftp;D:\software\xshell;C:\Program Files\Dock
er\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-
bin;C:\Program Files\Microsoft SQL Server\120\Tools\Binn;C:\Program
Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn;C:\Program
Files (x86)\Microsoft SQL Server\120\Tools\Binn;C:\Program
Files\Microsoft SQL Server\120\DTS\Binn;D:\software\CMake\bin;C:\Users
\liu\AppData\Local\Microsoft\WindowsApps;D:\software\vscode\Microsoft
VS Code\bin;D:\software\nvm\nvm;C:\Program
Files\nodejs;D:\software\fiddler
PSMODULEPATH=C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\Windows
PowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft SQL
Server\120\Tools\PowerShell\Modules
REQUESTS_CA_BUNDLE=<not set>
SSL_CERT_FILE=<not set>
active environment : base
active env location : D:\software\Miniconda3
shell level : 1
user config file : C:\Users\liu\.condarc
populated config files :
conda version : 4.10.3
conda-build version : not installed
python version : 3.9.5.final.0
virtual packages : __cuda=10.2=0
__win=0=0
__archspec=1=x86_64
base environment : D:\software\Miniconda3 (writable)
conda av data dir : D:\software\Miniconda3\etc\conda
conda av metadata url : None
channel URLs : https://conda.anaconda.org/conda-forge/win-64
https://conda.anaconda.org/conda-forge/noarch
https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : D:\software\Miniconda3\pkgs
C:\Users\liu.conda\pkgs
C:\Users\liu\AppData\Local\conda\conda\pkgs
envs directories : D:\software\Miniconda3\envs
C:\Users\liu.conda\envs
C:\Users\liu\AppData\Local\conda\conda\envs
platform : win-64
user-agent : conda/4.10.3 requests/2.25.1 CPython/3.9.5 Windows/10 Windows/10.0.19043
administrator : False
netrc file : None
offline mode : False
An unexpected error has occurred. Conda has prepared the above report.
If submitted, this report will be used by core maintainers to improve
future releases of conda.
Would you like conda to send this report to the core maintainers?
gdal的各個(gè)exe文件下載地址:
在壓縮包的:bin\gdal\apps\目錄下
gdal_calc.py下載地址:
https://svn.osgeo.org/gdal/trunk/gdal/swig/python/scripts/gdal_calc.py
github上的是這個(gè)地址檐迟,不過(guò)我用這個(gè)跑不起來(lái),還是依賴包的問(wèn)題
https://github.com/OSGeo/gdal/blob/master/gdal/swig/python/gdal-utils/osgeo_utils/gdal_calc.py
python版的gdal下載
https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal
選擇自己對(duì)應(yīng)的版本就行了,我這里下載的是:
GDAL-2.4.1-cp39-cp39-win_amd64.whl
安裝:
pip install whl文件完整路徑
檢驗(yàn):
命令行輸入python该窗,進(jìn)入python命令行盗迟,
輸入:
from osgeo import gdal
gdal.__version__
可查看gdal的版本,要能查看版本則gdal安裝成功惭蹂。
如果在命令行直接執(zhí)行:python gdal_calc.py
會(huì)報(bào)錯(cuò):
gdal_calc.py", line 51, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
此種情況下直接執(zhí)行:pip install numpy安裝numpy庫(kù)伞插,
如果有開(kāi)翻墻代理可能報(bào)如下錯(cuò)誤:
ERROR: Exception:
Traceback (most recent call last):
File "d:\software\python39\lib\site-packages\pip_internal\cli\base_command.py", line 173, in _main
status = self.run(options, args)
File "d:\software\python39\lib\site-packages\pip_internal\cli\req_command.py", line 203, in wrapper
return func(self, options, args)
File "d:\software\python39\lib\site-packages\pip_internal\commands\install.py", line 315, in run
requirement_set = resolver.resolve(
File "d:\software\python39\lib\site-packages\pip_internal\resolution\resolvelib\resolver.py", line 94, in resolve
result = self._result = resolver.resolve(
File "d:\software\python39\lib\site-packages\pip_vendor\resolvelib\resolvers.py", line 472, in resolve
state = resolution.resolve(requirements, max_rounds=max_rounds)
File "d:\software\python39\lib\site-packages\pip_vendor\resolvelib\resolvers.py", line 341, in resolve
self._add_to_criteria(self.state.criteria, r, parent=None)
File "d:\software\python39\lib\site-packages\pip_vendor\resolvelib\resolvers.py", line 172, in _add_to_criteria
if not criterion.candidates:
File "d:\software\python39\lib\site-packages\pip_vendor\resolvelib\structs.py", line 151, in bool
return bool(self._sequence)
File "d:\software\python39\lib\site-packages\pip_internal\resolution\resolvelib\found_candidates.py", line 140, in bool
return any(self)
File "d:\software\python39\lib\site-packages\pip_internal\resolution\resolvelib\found_candidates.py", line 128, in <genexpr>
return (c for c in iterator if id(c) not in self._incompatible_ids)
File "d:\software\python39\lib\site-packages\pip_internal\resolution\resolvelib\found_candidates.py", line 29, in _iter_built
for version, func in infos:
File "d:\software\python39\lib\site-packages\pip_internal\resolution\resolvelib\factory.py", line 272, in iter_index_candidate_infos
result = self._finder.find_best_candidate(
File "d:\software\python39\lib\site-packages\pip_internal\index\package_finder.py", line 851, in find_best_candidate
candidates = self.find_all_candidates(project_name)
File "d:\software\python39\lib\site-packages\pip_internal\index\package_finder.py", line 798, in find_all_candidates
page_candidates = list(page_candidates_it)
File "d:\software\python39\lib\site-packages\pip_internal\index\sources.py", line 134, in page_candidates
yield from self._candidates_from_page(self._link)
File "d:\software\python39\lib\site-packages\pip_internal\index\package_finder.py", line 758, in process_project_url
html_page = self._link_collector.fetch_page(project_url)
File "d:\software\python39\lib\site-packages\pip_internal\index\collector.py", line 490, in fetch_page
return _get_html_page(location, session=self.session)
File "d:\software\python39\lib\site-packages\pip_internal\index\collector.py", line 400, in _get_html_page
resp = _get_html_response(url, session=session)
File "d:\software\python39\lib\site-packages\pip_internal\index\collector.py", line 115, in _get_html_response
resp = session.get(
File "d:\software\python39\lib\site-packages\pip_vendor\requests\sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "d:\software\python39\lib\site-packages\pip_internal\network\session.py", line 454, in request
return super().request(method, url, *args, **kwargs)
File "d:\software\python39\lib\site-packages\pip_vendor\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "d:\software\python39\lib\site-packages\pip_vendor\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "d:\software\python39\lib\site-packages\pip_vendor\cachecontrol\adapter.py", line 53, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "d:\software\python39\lib\site-packages\pip_vendor\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "d:\software\python39\lib\site-packages\pip_vendor\urllib3\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "d:\software\python39\lib\site-packages\pip_vendor\urllib3\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "d:\software\python39\lib\site-packages\pip_vendor\urllib3\connection.py", line 359, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "d:\software\python39\lib\site-packages\pip_vendor\urllib3\connection.py", line 500, in connect_tls_proxy
return ssl_wrap_socket(
File "d:\software\python39\lib\site-packages\pip_vendor\urllib3\util\ssl.py", line 453, in ssl_wrap_socket
ssl_sock = ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "d:\software\python39\lib\site-packages\pip_vendor\urllib3\util\ssl.py", line 495, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "d:\software\python39\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "d:\software\python39\lib\ssl.py", line 997, in _create
raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname
關(guān)閉代理后重新執(zhí)行:pip install numpy即可安裝成功。
執(zhí)行測(cè)試命令:
python gdal_calc.py -A dem.tiff --outfile=result.tif --calc="A*(A>220)" --NoDataValue=0
-a_srs指定坐標(biāo)系
當(dāng)我添加-a_srs epsg:4490時(shí)盾碗,報(bào)錯(cuò):
ERROR 4: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
ERROR 1: Failed to process SRS definition: EPSG:4490
檢查發(fā)現(xiàn)電腦環(huán)境變量里面有pg設(shè)置的GDAL_DATA環(huán)境變量媚污。
GDAL自己也有一個(gè)通用的設(shè)置環(huán)境變量的方法,即--config,可設(shè)置為:--config GDAT_DATA D:/software/PostgreSQL/10/gdal-data廷雅,但是并不生效
好在GDAL支持7種坐標(biāo)系的設(shè)置方法:常用設(shè)置耗美、EPSG:n、PROJ.4 definition航缀、OpenGIS Well Known Text商架、ESRI Well Known Text、Spatial References from URLs芥玉、filename
最好用的當(dāng)然是可以直接設(shè)置常用的坐標(biāo)系蛇摸,如:NAD27|NAD83|WGS84|WGS72,設(shè)置為-a_srs WGS84就可以了
-a_srs是指assign SRS to output灿巧,即輸出的坐標(biāo)
-s_srs是指source SRS赶袄,即數(shù)據(jù)源的坐標(biāo)
-t_srs是指target SRS揽涮,中文解釋我暫時(shí)還理解不到,應(yīng)該是在使用gdalwarp這類坐標(biāo)轉(zhuǎn)換工具時(shí)指定輸出坐標(biāo)。
如果是中國(guó)境內(nèi)饿肺,需要設(shè)置為4490則需要設(shè)置為:-a_srs "+proj=longlat +ellps=GRS80 +no_defs"
若我們?cè)趃dal_grid中未用-a_srs指定輸出數(shù)據(jù)的坐標(biāo)系蒋困,在gdal_calc.py中輸出的tiff數(shù)據(jù)會(huì)報(bào)錯(cuò)未指定坐標(biāo)系,如圖: