将 sf 对象转换为 topojson

问题描述 投票:0回答:2

我正在尝试从 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。

r geojson sf topojson
2个回答
1
投票

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
层名称,这可能会被设置为其他地方的东西,除非它实际上在数据中...


1
投票

除了 Spacedman 描述的

geo2topo
方法之外,您还可以考虑
geojsonio::topojson_json()
.

一个可能的障碍可能是省份对象在某些本地 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...
© www.soinside.com 2019 - 2024. All rights reserved.