pablo-az
pablo-az

Reputation: 447

How to read or convert a .MAP File Extension document with python?

Is there a simple way (or a library available) to read the data in a ".map" file extension document preferably with Python (or in R)?

I am working with a modelling tool in python (PCRaster) that writes maps with the .map file extension. Interestingly however, I have not found a python library that can open and study these files.

Cheers,

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

Upvotes: 2

Views: 3448

Answers (1)

pablo-az
pablo-az

Reputation: 447

So I have not yet found a way read the .map files from PCRaster and be able to plot them directly, but I did just come across the ability to convert a .map file to a Numpy Array, which can then be used to plot elsewhere.

For this, Numpy, Aguila and PCRaster need to be installed. After, the commands can be run from a python script file.

From PCRaster maps to Numpy:

pcr2numpy(map, mv)

From Numpy to PCRaster

numpy2pcr(dataType, array, mv)

The following is an example from 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)

The above which prints:

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

Upvotes: 1

Related Questions