如何在R中读取.MAP文件扩展名? [英] How to read a .MAP file extension in R?
问题描述
是否有一种简单的方法来读取R中 .MAP
扩展名的文件?我在下面尝试了一些选项,但没有成功.
问题是:使用带有尾随nul字节的 readChar
-更改为 readBin()
; rawToChar()
不能接受的8位字符(在我的UTF-8系统上);一些文件中的多个碎片需要掉落;和其他一些.我将上面编辑过的 read.map()
函数添加到了 maptools 中,但是使用了不同的名称并且未导出.因此,现在(使用 https://r-forge.r-project.org/R/?group_id=943"rel =" nofollow noreferrer> https中的 maptools 版本370://r-forge.r-project.org/R/?group_id=943 构建完成后):
库(maptools)o<-maptools ::: readMAP2polylist("se_regsaud.MAP")oo<-maptools :::.makePolylistValid(o)ooo<-maptools :::.polylist2SpP(oo,tol = .Machine $ double.eps ^(1/4))rn<-row.names(ooo)df<-data.frame(ID = rn,row.names = rn,stringsAsFactors = FALSE)res<-SpatialPolygonsDataFrame(ooo,data = df)图书馆res_sf<-st_as_sf(res)res_sf情节(st_geometry(res_sf))
此方法重复使用了将近20年的 maptools 代码,并进行了少量修改以处理随后在读取二进制文件和固定条子方面的更改.
Is there a simple way to read a file of .MAP
extension in R? I have tried a few options below but had no success. Here is a .MAP
file for a reproducible example.
context: For some odd reason, the spatial regionalization used in health planning policies in Brazil is only available in this format. I would like to convert it to geopackage
so we can add it to the geobr package.
# none of these options work
mp <- sf::st_read("./se_mapas_2013/se_regsaud.MAP")
mp <- rgdal::readGDAL("./se_mapas_2013/se_regsaud.MAP")
mp <- rgdal::readOGR("./se_mapas_2013/se_regsaud.MAP")
mp <- raster::raster("./se_mapas_2013/se_regsaud.MAP")
mp <- stars::read_stars("./se_mapas_2013/se_regsaud.MAP")
ps. there is a similar question on SO focused on Python, unfortunately unanswered
UPDATE
We have found a publication that uses a custom function that reads the .MAP
file. See example below. However, it returns a "polylist"
object. Is there a simple way to convert it to a simple feature
?
original custom function
read.map = function(filename){
zz=file(filename,"rb")
#
# header of .map
#
versao = readBin(zz,"integer",1,size=2) # 100 = versao 1.00
#Bounding Box
Leste = readBin(zz,"numeric",1,size=4)
Norte = readBin(zz,"numeric",1,size=4)
Oeste = readBin(zz,"numeric",1,size=4)
Sul = readBin(zz,"numeric",1,size=4)
geocodigo = ""
nome = ""
xleg = 0
yleg = 0
sede = FALSE
poli = list()
i = 0
#
# repeat of each object in file
#
repeat{
tipoobj = readBin(zz,"integer",1,size=1) # 0=Poligono, 1=PoligonoComSede, 2=Linha, 3=Ponto
if (length(tipoobj) == 0) break
i = i + 1
Len = readBin(zz,"integer",1,size=1) # length byte da string Pascal
geocodigo[i] = readChar(zz,10)
Len = readBin(zz,"integer",1,size=1) # length byte da string Pascal
nome[i] = substr(readChar(zz,25),1,Len)
xleg[i] = readBin(zz,"numeric",1,size=4)
yleg[i] = readBin(zz,"numeric",1,size=4)
numpontos = readBin(zz,"integer",1,size=2)
sede = sede || (tipoobj = 1)
x=0
y=0
for (j in 1:numpontos){
x[j] = readBin(zz,"numeric",1,size=4)
y[j] = readBin(zz,"numeric",1,size=4)
}
# separate polygons
xInic = x[1]
yInic = y[1]
for (j in 2:numpontos){
if (x[j] == xInic & y[j] == yInic) {x[j]=NA; y[j] = NA}
}
poli[[i]] = c(x,y)
dim(poli[[i]]) = c(numpontos,2)
}
class(poli) = "polylist"
attr(poli,"region.id") = geocodigo
attr(poli,"region.name") = nome
attr(poli,"centroid") = list(x=xleg,y=yleg)
attr(poli,"sede") = sede
attr(poli,"maplim") = list(x=c(Oeste,Leste),y=c(Sul,Norte))
close(zz)
return(poli)
}
using original custom function
mp <- read.map("./se_mapas_2013/se_regsaud.MAP")
class(mp)
>[1] "polylist"
# plot
plot(attributes(mp)$maplim, type='n', asp=1, xlab=NA, ylab=NA)
title('Map')
lapply(mp, polygon, asp=T, col=3)
The problems were: use of readChar
with trailing nul bytes - changed to readBin()
; 8-bit characters that rawToChar()
would not accept (on my UTF-8 system); multiple slivers in some files that needed dropping; and some others. I added the edited read.map()
function above to maptools, but with a different name and not exported. So now (with maptools rev 370 from https://r-forge.r-project.org/R/?group_id=943 when build completes):
library(maptools)
o <- maptools:::readMAP2polylist("se_regsaud.MAP")
oo <- maptools:::.makePolylistValid(o)
ooo <- maptools:::.polylist2SpP(oo, tol=.Machine$double.eps^(1/4))
rn <- row.names(ooo)
df <- data.frame(ID=rn, row.names=rn, stringsAsFactors=FALSE)
res <- SpatialPolygonsDataFrame(ooo, data=df)
library(sf)
res_sf <- st_as_sf(res)
res_sf
plot(st_geometry(res_sf))
This approach re-uses the maptools code dating back almost twenty years, with minor edits to handle subsequent changes in reading binary files, and fixing slivers.
这篇关于如何在R中读取.MAP文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!