R语言 将sf对象转换为topojson

icnyk63a  于 2023-05-20  发布在  其他
关注(0)|答案(2)|浏览(196)

我试图从一个sf对象创建一个正确格式化为topoJSON的列表。
当阅读有效的topoJSON文件时,可以执行以下操作:

sa <- jsonlite::read_json("https://raw.githubusercontent.com/pachadotdev/d3po/main/dev/south-america.topojson", simplifyVector = F)

我想做的是:

# install_github("pachadotdev/canadamaps")
library(canadamaps)

library(sf)
library(geojsonio)

provinces <- get_provinces()
provinces <- st_as_sf(provinces)

geojson_write(provinces, file = "provinces.geojson")

provinces2 <- geojson_read("provinces.geojson")

geo2topo(provinces)

Error: no 'geo2topo' method for sftbl_dftbldata.frame

有没有从R转换为topoJSON的选项?
我尝试保存为geoJSON,然后在QGIS中重新导出,但它不保存为topoJSON。

qrjkbowd

qrjkbowd1#

geo2topo适用于JSON字符串,而不是R对象。
从您在示例中编写的geoJSON文件开始。ogrinfo告诉我它的geoJSON

$ ogrinfo provinces.geojson 
INFO: Open of `provinces.geojson'
      using driver `GeoJSON' successful.
1: provinces (Multi Polygon)

将其作为文本读入R-这是纯文本geoJSON:

> s = readLines("provinces.geojson")
Warning message:
In readLines("provinces.geojson") :
  incomplete final line found on 'provinces.geojson'
> substr(s, 1, 100)
[1] "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"pruid\":46,\"prname\":\"Manitob"

现在转换为topoJSON字符串:

> tj = geojsonio::geo2topo(s)
> substr(tj, 1, 100)
{"type":"Topology","objects":{"foo":{"type":"GeometryCollection","geometries":[{"type":"MultiPolygon

并将文本写入文件:

> writeLines(tj, "tj.topojson")
>

gdal解释为topoJSON

$ ogrinfo tj.topojson 
INFO: Open of `tj.topojson'
      using driver `TopoJSON' successful.
1: foo (Multi Polygon)

我不知道它从哪里得到的foo层名称,这可能是设置为其他地方,除非它实际上在数据中...

bf1o4zei

bf1o4zei2#

除了Spacedman描述的geo2topo方法之外,您还可以考虑geojsonio::topojson_json()
一个可能的障碍可能是provinces对象位于一些本地CRS中,而当前的geojson标准只要求EPSG:4326。有一个很好的故事,可能是杜撰的,标准制定机构不相信主要用户(IT,而不是GIS人员)理解地球不平坦的问题,并删除了在EPSG数据库中使用CRS作为标准化的选项,这是原始标准所允许的。
geojson / topojson仍然很难使用CRS,即使根据标准保存并读回R,它也会与CRS斗争,这就是为什么我必须应用sf::st_set_crs(4326)

library(sf)
library(geojsonio)
library(canadamaps)

provinces <- get_provinces()
provinces <- st_as_sf(provinces)

provinces %>% 
  st_transform(4326) %>% # the one and only CRS that geojson standard allows
  dplyr::transmute(id = pruid, prname) %>% 
  geojsonio::topojson_json(object_name = "provinces", crs = 4326) %>% 
  geojsonio::topojson_write(file = "oh_canada.json", crs = 4326)

st_read("oh_canada.json") %>% 
  st_set_crs(4326)

# Reading layer `provinces' from data source `/home/jindra/Documents/SmallDev/oh_canada.json' using driver `TopoJSON'
# Simple feature collection with 13 features and 2 fields
# Geometry type: MULTIPOLYGON
# Dimension:     XY
# Bounding box:  xmin: -141.0181 ymin: 41.72967 xmax: -52.61941 ymax: 83.1355
# CRS:           NA
# Simple feature collection with 13 features and 2 fields
# Geometry type: MULTIPOLYGON
# Dimension:     XY
# Bounding box:  xmin: -141.0181 ymin: 41.72967 xmax: -52.61941 ymax: 83.1355
# Geodetic CRS:  WGS 84
# First 10 features:
#    id                                              prname                        geometry
# 1  46                                            Manitoba MULTIPOLYGON (((-101.3625 4...
# 2  47                                        Saskatchewan MULTIPOLYGON (((-102 60, -1...
# 3  48                                             Alberta MULTIPOLYGON (((-110 60, -1...
# 4  59             British Columbia / Colombie-Britannique MULTIPOLYGON (((-120 60, -1...
# 5  24                                     Quebec / Québec MULTIPOLYGON (((-69.05071 4...
# 6  35                                             Ontario MULTIPOLYGON (((-79.51585 5...
# 7  10 Newfoundland and Labrador / Terre-Neuve-et-Labrador MULTIPOLYGON (((-53.81009 4...
# 8  11        Prince Edward Island / Île-du-Prince-Édouard MULTIPOLYGON (((-62.85386 4...
# 9  12                       Nova Scotia / Nouvelle-Écosse MULTIPOLYGON (((-64.0426 45...
# 10 13                   New Brunswick / Nouveau-Brunswick MULTIPOLYGON # ##(((-64.27499 4...

相关问题