如何使用python读取或转换.MAP文件扩展名文档?

2024-06-01 12:44:20 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种简单的方法(或可用的库)来读取“.map”文件扩展名文档中的数据,最好使用Python(或R语言)?在

我正在使用python(PCRaster)中的建模工具编写扩展名为.map的地图。然而有趣的是,我还没有找到一个python库来打开和研究这些文件。在

干杯

A similar question is made here, unfortunately unanswered

Here a .txt file is converted to .map, but can't see how to reverse this

Here is a list of uses cases from Wikipedia for the .map file format


Tags: 文件工具to数据方法文档语言map
1条回答
网友
1楼 · 发布于 2024-06-01 12:44:20

因此,我还没有找到一种从PCRaster读取.map文件并能够直接打印它们的方法,但是我发现了将.map文件转换为Numpy数组的能力,该数组可以用于在其他地方进行绘图。在

为此,需要安装Numpy、AguilaPCRaster。之后,可以从python脚本文件运行命令。在

从PCRaster maps到Numpy:

pcr2numpy(map, mv)

从Numpy到PCRaster

^{pr2}$

以下是the PCRasterPython documentation page中的一个示例:

import numpy
from pcraster import *
from pcraster.numpy import *

setclone("clone.map")

a = numpy.array([[12,5,21],[9,7,3],[20,8,2],[5,6,-3]])

# conver a to a PCRaster Python map
# with the value 20 indicating a 'missing value' cell
n2p = numpy2pcr(Nominal, a, 20)
print "cellvalue:", cellvalue(n2p, 2, 3)[0]

# write it to a PCRaster map
report(n2p, "n2p.map")
# read the PCRaster map back
p2n = readmap("n2p.map")
# print it as a numpy array
# missing value is replaced by 9999
print pcr2numpy(p2n, 9999)

上面印着:

cellvalue: 3
[[  12    5   21]
 [   9    7    3]
 [9999    8    2]
 [   5    6   -3]]

相关问题 更多 >