gdal_calc.py是一个gdal用于进行栅格计算的脚本,其功能类似于ArcGIS Desktop/Spatial Analyst Tools/Map Algebra/Raster Calculator工具。
conda install -c conda-forge gdal的方式安装报错:
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
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=
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=
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=
SSL_CERT_FILE=
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的各个exe文件下载地址:
https://www.gisinternals.com/query.html?content=filelist&file=release-1911-gdal-2-4-4-mapserver-7-4-3.zip
在压缩包的:bin\gdal\apps\目录下
gdal_calc.py下载地址:
https://svn.osgeo.org/gdal/trunk/gdal/swig/python/scripts/gdal_calc.py
github上的是这个地址,不过我用这个跑不起来,还是依赖包的问题
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
选择自己对应的版本就行了,我这里下载的是:
GDAL-2.4.1-cp39-cp39-win_amd64.whl
安装:
pip install whl文件完整路径
检验:
命令行输入python,进入python命令行,
输入:
from osgeo import gdal
gdal.__version__
可查看gdal的版本,要能查看版本则gdal安装成功。
如果在命令行直接执行:python gdal_calc.py
会报错:
gdal_calc.py", line 51, in
import numpy
ModuleNotFoundError: No module named 'numpy'
此种情况下直接执行:pip install numpy安装numpy库,
如果有开翻墙代理可能报如下错误:
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
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
关闭代理后重新执行:pip install numpy即可安装成功。
执行测试命令:
python gdal_calc.py -A dem.tiff --outfile=result.tif --calc="A*(A>220)" --NoDataValue=0
-a_srs指定坐标系
当我添加-a_srs epsg:4490时,报错:
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
检查发现电脑环境变量里面有pg设置的GDAL_DATA环境变量。
GDAL自己也有一个通用的设置环境变量的方法,即--config,可设置为:--config GDAT_DATA D:/software/PostgreSQL/10/gdal-data,但是并不生效
好在GDAL支持7种坐标系的设置方法:常用设置、EPSG:n、PROJ.4 definition、OpenGIS Well Known Text、ESRI Well Known Text、Spatial References from URLs、filename
最好用的当然是可以直接设置常用的坐标系,如:NAD27|NAD83|WGS84|WGS72,设置为-a_srs WGS84就可以了
-a_srs是指assign SRS to output,即输出的坐标
-s_srs是指source SRS,即数据源的坐标
-t_srs是指target SRS,中文解释我暂时还理解不到,应该是在使用gdalwarp这类坐标转换工具时指定输出坐标。
如果是中国境内,需要设置为4490则需要设置为:-a_srs "+proj=longlat +ellps=GRS80 +no_defs"
若我们在gdal_grid中未用-a_srs指定输出数据的坐标系,在gdal_calc.py中输出的tiff数据会报错未指定坐标系,如图: