Reading geojson file to convert to sf

huangapple go评论78阅读模式
英文:

Reading geojson file to convert to sf

问题

I have a geojson file which i need to read/convert to sf object.

我有一个GeoJSON文件,我需要将其读取/转换为sf对象。

I read it with geojsonR package and it shows as lists of lists, but i need it to sf.

我使用geojsonR包读取了它,它显示为列表的列表,但我需要将其转换为sf对象。

I tried using st_read as suggested here but it gives me "Error: dsn must point to a source, not an empty string."

我尝试使用st_read,如这里建议的,但它给我返回了"Error: dsn must point to a source, not an empty string."

Also tried geojsonsf package and geojson_sf() and it gives me "Error in rcpp_geojson_to_sf(geojson, expand_geometries) : Invalid JSON"

我还尝试了geojsonsf包和geojson_sf(),但它给我返回了"Error in rcpp_geojson_to_sf(geojson, expand_geometries) : Invalid JSON"

how can i read it as sf and more importantly - i need sf geometry from there...

我该如何将其读取为sf对象,更重要的是 - 我需要从中获取sf的几何信息...

The file is here link

该文件在这里 链接

英文:

I have a geojson file which i need to read/convert to sf object

I read it with geojsonR package and it shows as lists of lists, but i need it to sf.
I tried using st_read as suggested here but it gives me "Error: dsn must point to a source, not an empty string."

Also tried geojsonsf package and geojson_sf() and it gives me "Error in rcpp_geojson_to_sf(geojson, expand_geometries) : Invalid JSON"

how can i read it as sf and more importantly - i need sf geometry from there...

The file is here https://vustaff-my.sharepoint.com/:u:/g/personal/e5028514_vu_edu_au/ERKzUBrFobBKrNe7ga-3arMB63ZuyD4eDbAtPQ5fzy2BbQ?e=m87Af9

答案1

得分: 4

以下是翻译好的部分:

如果您查看您的 'json'(它不是有效的 .geojson),您会发现它实际上是一组嵌套的 geojson 对象。

使用您的示例,我正在执行以下操作:

  1. 使用 readLines 将 .geojson(实际上是 .json)文件作为纯文本进行读取。
  2. 使用 jqr 检查一级属性。

请注意,有 8 个不同的 geojson 对象。

您可以通过在解析之前筛选 json 来读取它们,使用 geojsonsfsf

还请注意,.geojson 必须使用 CRS 4326 才能有效。看起来这个数据不是在这个 CRS 下的。

英文:

If you look at your 'json' (it's not valid .geojson) you'll see it's actually a set of nested geojson objects

Using your example, I'm

  1. reading the .geojson (which is actually .json) file as plain text using readLines.
  2. Inspecting the first-level properties using jqr
txt <- paste0(readLines("~/Downloads/test.geojson"), collapse = "")
library(jqr)
jq(txt, "keys")

# [
#   "grunnkretser.delomradegrense",
#   "grunnkretser.fylkesgrense",
#   "grunnkretser.grensesjo",
#   "grunnkretser.grunnkrets",
#   "grunnkretser.grunnkretsgrense",
#   "grunnkretser.kommunegrense",
#   "grunnkretser.riksgrense",
#   "grunnkretser.territorialgrense"
# ]

See there are 8 different geojson objects.

You can read each of them by filtering the json before parsing through geojsonsf or sf

library(geojsonsf)
library(sf)

sf_delomradegrense <- jq(txt, '.["grunnkretser.delomradegrense"]') %>% 
  geojsonsf::geojson_sf()

sf_delomradegrense
# Simple feature collection with 10284 features and 7 fields
# Geometry type: LINESTRING
# Dimension:     XY
# Bounding box:  xmin: 4010764 ymin: 3853260 xmax: 5097468 ymax: 5425159
# Geodetic CRS:  WGS 84
# First 10 features:
#   objid datafangstdato folgerterrengdetalj malemetode oppdateringsdato         objtype noyaktighet                       geometry
# 1      1           <NA>                             99   20160126000000 Delområdegrense        <NA> LINESTRING (4404964 4628261...
# 2      2           <NA>                             99   20131027000000 Delområdegrense        <NA> LINESTRING (4616126 5145684...
# 3      3           <NA>                             99   20131027000000 Delområdegrense        <NA> LINESTRING (4101761 4257071...
# 4      4           <NA>                             99   20131027000000 Delområdegrense        <NA> LINESTRING (4129652 4122672...
# 5      5           <NA>                             99   20170908000000 Delområdegrense        <NA> LINESTRING (4303917 4481500...
# 6      6           <NA>                             99   20131027000000 Delområdegrense        <NA> LINESTRING (4607806 5150974...
# 7      7           <NA>                             99   20131027000000 Delområdegrense        <NA> LINESTRING (4300691 4485384...
# 8      8           <NA>                             99   20170712000000 Delområdegrense        <NA> LINESTRING (4396353 4104326...
# 9      9           <NA>                             99   20170908000000 Delområdegrense        <NA> LINESTRING (4396353 4104326...
# 10    10           <NA>                             99   20131027000000 Delområdegrense        <NA> LINESTRING (4627750 5166350...

Also note that .geojson must be in CRS 4326 for it to be valid. It doesn't look like this data is.

huangapple
  • 本文由 发表于 2023年7月18日 07:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定