ropensci / rnaturalearth

An R package to hold and facilitate interaction with natural earth map data :earth_africa:
http://ropensci.github.io/rnaturalearth/
Other
214 stars 24 forks source link

'ne_download' fails to convert rivers to 'sp' #79

Closed AMBarbosa closed 2 weeks ago

AMBarbosa commented 1 year ago

ne_download is now returning (by default) a numeric vector instead of an 'sp' class object, apparently because there are unsupported empty geometries in the currently downloaded file:

rivers <- ne_download(scale = "medium", type = "rivers_lake_centerlines", category = "physical", destdir = ".")
# trying URL 'https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_rivers_lake_centerlines.zip'
# Content type 'application/zip' length 504454 bytes (492 KB)
# ==================================================
#   downloaded 492 KB
# 
# Error in h(simpleError(msg, call)) : 
#   error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': empty geometries are not supported by sp classes: conversion failed

class(rivers)
#  [1] "numeric"

If I may add a gentle reminder of a related enhancement suggestion: please add 'SpatVector' (sv) to the 'returnclass' options, as per https://github.com/ropensci/rnaturalearth/issues/49#issue-1092132517

PMassicotte commented 1 year ago

There was a recent update on sp, wonder if this is related.

PMassicotte commented 1 year ago

Interestingly, this is working:

rivers <- ne_download(scale = "small", type = "rivers_lake_centerlines", category = "physical")

Wonder if the source data has changed.

PMassicotte commented 1 year ago

Looks like is it because there are empty geometry in the data.

library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.7.0, PROJ 9.1.1; sf_use_s2() is TRUE

obj <- st_read("/vsizip/vsicurl/https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_rivers_lake_centerlines.zip")
#> Reading layer `ne_50m_rivers_lake_centerlines' from data source 
#>   `/vsizip/vsicurl/https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_rivers_lake_centerlines.zip' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 478 features and 36 fields (with 1 geometry empty)
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -165.2439 ymin: -50.24014 xmax: 176.3258 ymax: 73.3349
#> Geodetic CRS:  WGS 84

obj
#> Simple feature collection with 478 features and 36 fields (with 1 geometry empty)
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -165.2439 ymin: -50.24014 xmax: 176.3258 ymax: 73.3349
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    scalerank      featurecla        name note min_zoom name_alt min_label
#> 1          6 Lake Centerline        Kama <NA>      4.7     <NA>       6.0
#> 2          6           River        Kama <NA>      4.7     <NA>       6.0
#> 3          3 Lake Centerline        Abay <NA>      3.0     <NA>       4.0
#> 4          3 Lake Centerline    Al Furat <NA>      3.0     <NA>       4.0
#> 5          6 Lake Centerline     Alabama <NA>      5.0     <NA>       6.0
#> 6          6 Lake Centerline      Albany <NA>      5.0     <NA>       6.0
#> 7          1 Lake Centerline Albert Nile <NA>      2.0     <NA>       3.0
#> 8          3 Lake Centerline        Amur <NA>      3.0     <NA>       4.0
#> 9          2 Lake Centerline      Angara <NA>      2.1     <NA>       3.1
#> 10         6 Lake Centerline      Atbara <NA>      5.0     <NA>       6.0
#>        name_en       label wikidataid      name_ar     name_bn    name_de
#> 1         Kama        Kama     Q79082     نهر كاما        কামা       Kama
#> 2         Kama        Kama     Q79082     نهر كاما        কামা       Kama
#> 3  Lesser Abay        <NA>   Q3501984         أباي       অ্যাবে   Abajasee
#> 4    Euphrates    Al Furat     Q34589       الفرات       ফোরাত    Euphrat
#> 5      Alabama     Alabama    Q558643      ألاباما     আলাবামা    Alabama
#> 6       Albany      Albany   Q1241991      ألبانيا      আলবেনি     Albany
#> 7   White Nile Albert Nile   Q4814791 النيل الأبيض শ্বেত নীল নদ Weißer Nil
#> 8         Amur        Amur      Q6862     نهر آمور     আমুর নদী       Amur
#> 9       Angara      Angara    Q162737   نهر أنغارا  আঙ্গারা নদী     Angara
#> 10     Atbarah      Atbara    Q753897    نهر عطبرة     আতাবারা     Atbara
#>        name_es    name_fr       name_el   name_hi     name_hu   name_id
#> 1         Kama       Kama  Ποταμός Κάμα  कामा नदी        Káma      Kama
#> 2         Kama       Kama  Ποταμός Κάμα  कामा नदी        Káma      Kama
#> 3         Abay Abbey Lake         Άμπαι        एबे        Abay      Abay
#> 4     Éufrates   Euphrate      Ευφράτης  फ़रात नदी   Eufrátesz     Efrat
#> 5      Alabama    Alabama      Αλαμπάμα    अल्बामा     Alabama   Alabama
#> 6       Albany     Albany       Ώλμπανι     अल्बेनी      Albany    Albany
#> 7  Nilo Blanco  Nil Blanc Λευκός Νείλος   श्वेत नील Fehér-Nílus Nil Putih
#> 8         Amur      Amour         Αμούρ   अमूर नदी        Amur      Amur
#> 9       Angará     Angara       Αγγαράς अंगारा नदी      Angara    Angara
#> 10      Atbara     Atbara       Ατμπάρα     अतबरा      Atbara    Atbara
#>        name_it        name_ja      name_ko    name_nl   name_pl     name_pt
#> 1         Kama         カマ川       카마강       Kama      Kama        Kama
#> 2         Kama         カマ川       카마강       Kama      Kama        Kama
#> 3         Abay     小アバイ川       아바이       Abay      Abaj Lesser Abay
#> 4      Eufrate ユーフラテス川 유프라테스강    Eufraat    Eufrat    Eufrates
#> 5      Alabama     アラバマ川   앨라배마강    Alabama   Alabama     Alabama
#> 6       Albany オールバニー川       알바니     Albany    Albany      Albany
#> 7  Nilo Bianco     白ナイル川     백나일강 Witte Nijl Nil Biały Nilo Branco
#> 8         Amur     アムール川     아무르강      Amoer      Amur        Amur
#> 9       Angara     アンガラ川     안가라강     Angara    Angara      Angara
#> 10      Atbara     アトバラ川     아트바라    Atbarah    Atbara      Atbara
#>      name_ru     name_sv   name_tr   name_vi    name_zh      ne_id    name_fa
#> 1       Кама        Kama      Kama      Kama     卡馬河 1159125905   رود کاما
#> 2       Кама        Kama      Kama      Kama     卡馬河 1159125917   رود کاما
#> 3       Абая Lesser Abay      Abay      Abay     阿拜河 1159125319        ابی
#> 4     Евфрат      Eufrat     Fırat Euphrates 幼发拉底河 1159125935       فرات
#> 5    Алабама     Alabama   Alabama   Alabama 阿拉巴馬河 1159114961    آلاباما
#> 6     Олбани      Albany    Albany    Albany 奥尔巴尼河 1159114709 رود آلبانی
#> 7  Белый Нил  Vita Nilen Beyaz Nil Nin Trắng   白尼罗河 1159129205   نیل سفید
#> 8       Амур        Amur      Amur      Amur     黑龙江 1159126525       آمور
#> 9     Ангара      Angara    Angara    Angara   安加拉河 1159117253 رود آنگارا
#> 10    Атбара      Atbara    Atbara    Atbara 阿特巴拉河 1159115327     آتبارا
#>        name_he   name_uk       name_ur   name_zht
#> 1         קאמה      Кама   دریائے کاما     卡馬河
#> 2         קאמה      Кама   دریائے کاما     卡馬河
#> 3         אבאי      Абая           ابے       阿拜
#> 4          פרת    Євфрат   دریائے فرات 幼发拉底河
#> 5    נהר אלבמה   Алабама        البامہ 阿拉巴馬河
#> 6        אלבני    Олбані دریائے البانی     奧巴尼
#> 7  הנילוס הלבן Білий Ніл      نیل ابیض   白尼罗河
#> 8         אמור      Амур   دریائے آمور     黑龍江
#> 9        אנגרה    Ангара        انگارا   安加拉河
#> 10       עטברה    Атбара  دریائے عطبرہ 阿特巴拉河
#>                          geometry
#> 1  MULTILINESTRING ((51.93713 ...
#> 2  MULTILINESTRING ((53.69385 ...
#> 3  MULTILINESTRING ((37.11301 ...
#> 4  MULTILINESTRING ((38.56119 ...
#> 5  MULTILINESTRING ((-86.52177...
#> 6  MULTILINESTRING ((-91.31225...
#> 7  MULTILINESTRING ((31.3667 2...
#> 8  MULTILINESTRING ((137.0699 ...
#> 9  MULTILINESTRING ((106.4973 ...
#> 10 MULTILINESTRING ((35.92414 ...

obj[st_is_empty(obj), , drop = FALSE]
#> Simple feature collection with 1 feature and 36 fields (with 1 geometry empty)
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> Geodetic CRS:  WGS 84
#>     scalerank featurecla  name note min_zoom name_alt min_label name_en label
#> 461         5      River Loire <NA>      4.7     <NA>       5.7   Loire Loire
#>     wikidataid name_ar   name_bn name_de name_es name_fr name_el  name_hi
#> 461      Q1469    لوار লোয়ার নদী   Loire   Loira   Loire Λίγηρας लवार नदी
#>     name_hu name_id name_it    name_ja  name_ko name_nl name_pl name_pt name_ru
#> 461   Loire   Loire   Loira ロワール川 루아르강   Loire   Loara   Loire   Луара
#>     name_sv name_tr name_vi  name_zh      ne_id name_fa name_he name_uk name_ur
#> 461   Loire   Loire   Loire 卢瓦尔河 1159129677    لوار    לואר   Луара    لوار
#>     name_zht              geometry
#> 461 卢瓦尔河 MULTILINESTRING EMPTY

Created on 2023-06-05 with reprex v2.0.2

This makes as_Spatial(x) failing.

https://github.com/ropensci/rnaturalearth/blob/3b29b669ccd0e4f0cd7fc935faf8b6f4bea20faf/R/ne_as_sp.R#L16

PMassicotte commented 1 year ago
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.7.0, PROJ 9.1.1; sf_use_s2() is TRUE

obj <- st_read("/vsizip/vsicurl/https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_rivers_lake_centerlines.zip")
#> Reading layer `ne_50m_rivers_lake_centerlines' from data source 
#>   `/vsizip/vsicurl/https://naturalearth.s3.amazonaws.com/50m_physical/ne_50m_rivers_lake_centerlines.zip' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 478 features and 36 fields (with 1 geometry empty)
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -165.2439 ymin: -50.24014 xmax: 176.3258 ymax: 73.3349
#> Geodetic CRS:  WGS 84

obj
#> Simple feature collection with 478 features and 36 fields (with 1 geometry empty)
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -165.2439 ymin: -50.24014 xmax: 176.3258 ymax: 73.3349
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    scalerank      featurecla        name note min_zoom name_alt min_label
#> 1          6 Lake Centerline        Kama <NA>      4.7     <NA>       6.0
#> 2          6           River        Kama <NA>      4.7     <NA>       6.0
#> 3          3 Lake Centerline        Abay <NA>      3.0     <NA>       4.0
#> 4          3 Lake Centerline    Al Furat <NA>      3.0     <NA>       4.0
#> 5          6 Lake Centerline     Alabama <NA>      5.0     <NA>       6.0
#> 6          6 Lake Centerline      Albany <NA>      5.0     <NA>       6.0
#> 7          1 Lake Centerline Albert Nile <NA>      2.0     <NA>       3.0
#> 8          3 Lake Centerline        Amur <NA>      3.0     <NA>       4.0
#> 9          2 Lake Centerline      Angara <NA>      2.1     <NA>       3.1
#> 10         6 Lake Centerline      Atbara <NA>      5.0     <NA>       6.0
#>        name_en       label wikidataid      name_ar     name_bn    name_de
#> 1         Kama        Kama     Q79082     نهر كاما        কামা       Kama
#> 2         Kama        Kama     Q79082     نهر كاما        কামা       Kama
#> 3  Lesser Abay        <NA>   Q3501984         أباي       অ্যাবে   Abajasee
#> 4    Euphrates    Al Furat     Q34589       الفرات       ফোরাত    Euphrat
#> 5      Alabama     Alabama    Q558643      ألاباما     আলাবামা    Alabama
#> 6       Albany      Albany   Q1241991      ألبانيا      আলবেনি     Albany
#> 7   White Nile Albert Nile   Q4814791 النيل الأبيض শ্বেত নীল নদ Weißer Nil
#> 8         Amur        Amur      Q6862     نهر آمور     আমুর নদী       Amur
#> 9       Angara      Angara    Q162737   نهر أنغارا  আঙ্গারা নদী     Angara
#> 10     Atbarah      Atbara    Q753897    نهر عطبرة     আতাবারা     Atbara
#>        name_es    name_fr       name_el   name_hi     name_hu   name_id
#> 1         Kama       Kama  Ποταμός Κάμα  कामा नदी        Káma      Kama
#> 2         Kama       Kama  Ποταμός Κάμα  कामा नदी        Káma      Kama
#> 3         Abay Abbey Lake         Άμπαι        एबे        Abay      Abay
#> 4     Éufrates   Euphrate      Ευφράτης  फ़रात नदी   Eufrátesz     Efrat
#> 5      Alabama    Alabama      Αλαμπάμα    अल्बामा     Alabama   Alabama
#> 6       Albany     Albany       Ώλμπανι     अल्बेनी      Albany    Albany
#> 7  Nilo Blanco  Nil Blanc Λευκός Νείλος   श्वेत नील Fehér-Nílus Nil Putih
#> 8         Amur      Amour         Αμούρ   अमूर नदी        Amur      Amur
#> 9       Angará     Angara       Αγγαράς अंगारा नदी      Angara    Angara
#> 10      Atbara     Atbara       Ατμπάρα     अतबरा      Atbara    Atbara
#>        name_it        name_ja      name_ko    name_nl   name_pl     name_pt
#> 1         Kama         カマ川       카마강       Kama      Kama        Kama
#> 2         Kama         カマ川       카마강       Kama      Kama        Kama
#> 3         Abay     小アバイ川       아바이       Abay      Abaj Lesser Abay
#> 4      Eufrate ユーフラテス川 유프라테스강    Eufraat    Eufrat    Eufrates
#> 5      Alabama     アラバマ川   앨라배마강    Alabama   Alabama     Alabama
#> 6       Albany オールバニー川       알바니     Albany    Albany      Albany
#> 7  Nilo Bianco     白ナイル川     백나일강 Witte Nijl Nil Biały Nilo Branco
#> 8         Amur     アムール川     아무르강      Amoer      Amur        Amur
#> 9       Angara     アンガラ川     안가라강     Angara    Angara      Angara
#> 10      Atbara     アトバラ川     아트바라    Atbarah    Atbara      Atbara
#>      name_ru     name_sv   name_tr   name_vi    name_zh      ne_id    name_fa
#> 1       Кама        Kama      Kama      Kama     卡馬河 1159125905   رود کاما
#> 2       Кама        Kama      Kama      Kama     卡馬河 1159125917   رود کاما
#> 3       Абая Lesser Abay      Abay      Abay     阿拜河 1159125319        ابی
#> 4     Евфрат      Eufrat     Fırat Euphrates 幼发拉底河 1159125935       فرات
#> 5    Алабама     Alabama   Alabama   Alabama 阿拉巴馬河 1159114961    آلاباما
#> 6     Олбани      Albany    Albany    Albany 奥尔巴尼河 1159114709 رود آلبانی
#> 7  Белый Нил  Vita Nilen Beyaz Nil Nin Trắng   白尼罗河 1159129205   نیل سفید
#> 8       Амур        Amur      Amur      Amur     黑龙江 1159126525       آمور
#> 9     Ангара      Angara    Angara    Angara   安加拉河 1159117253 رود آنگارا
#> 10    Атбара      Atbara    Atbara    Atbara 阿特巴拉河 1159115327     آتبارا
#>        name_he   name_uk       name_ur   name_zht
#> 1         קאמה      Кама   دریائے کاما     卡馬河
#> 2         קאמה      Кама   دریائے کاما     卡馬河
#> 3         אבאי      Абая           ابے       阿拜
#> 4          פרת    Євфрат   دریائے فرات 幼发拉底河
#> 5    נהר אלבמה   Алабама        البامہ 阿拉巴馬河
#> 6        אלבני    Олбані دریائے البانی     奧巴尼
#> 7  הנילוס הלבן Білий Ніл      نیل ابیض   白尼罗河
#> 8         אמור      Амур   دریائے آمور     黑龍江
#> 9        אנגרה    Ангара        انگارا   安加拉河
#> 10       עטברה    Атбара  دریائے عطبرہ 阿特巴拉河
#>                          geometry
#> 1  MULTILINESTRING ((51.93713 ...
#> 2  MULTILINESTRING ((53.69385 ...
#> 3  MULTILINESTRING ((37.11301 ...
#> 4  MULTILINESTRING ((38.56119 ...
#> 5  MULTILINESTRING ((-86.52177...
#> 6  MULTILINESTRING ((-91.31225...
#> 7  MULTILINESTRING ((31.3667 2...
#> 8  MULTILINESTRING ((137.0699 ...
#> 9  MULTILINESTRING ((106.4973 ...
#> 10 MULTILINESTRING ((35.92414 ...

obj[st_is_empty(obj), , drop = FALSE]
#> Simple feature collection with 1 feature and 36 fields (with 1 geometry empty)
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> Geodetic CRS:  WGS 84
#>     scalerank featurecla  name note min_zoom name_alt min_label name_en label
#> 461         5      River Loire <NA>      4.7     <NA>       5.7   Loire Loire
#>     wikidataid name_ar   name_bn name_de name_es name_fr name_el  name_hi
#> 461      Q1469    لوار লোয়ার নদী   Loire   Loira   Loire Λίγηρας लवार नदी
#>     name_hu name_id name_it    name_ja  name_ko name_nl name_pl name_pt name_ru
#> 461   Loire   Loire   Loira ロワール川 루아르강   Loire   Loara   Loire   Луара
#>     name_sv name_tr name_vi  name_zh      ne_id name_fa name_he name_uk name_ur
#> 461   Loire   Loire   Loire 卢瓦尔河 1159129677    لوار    לואר   Луара    لوار
#>     name_zht              geometry
#> 461 卢瓦尔河 MULTILINESTRING EMPTY

One temporary way hack this, is to drop missing geom and convert it to sp.

sp::plot(as_Spatial(obj[!st_is_empty(obj), , drop = FALSE]))
#> The legacy packages maptools, rgdal, and rgeos, underpinning this package
#> will retire shortly. Please refer to R-spatial evolution reports on
#> https://r-spatial.org/r/2023/05/15/evolution4.html for details.
#> This package is now running under evolution status 0
#> Please note that rgdal will be retired during October 2023,
#> plan transition to sf/stars/terra functions using GDAL and PROJ
#> at your earliest convenience.
#> See https://r-spatial.org/r/2023/05/15/evolution4.html and https://github.com/r-spatial/evolution
#> rgdal: version: 1.6-7, (SVN revision 1203)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
#> Path to GDAL shared files: /usr/share/gdal
#> GDAL binary built with GEOS: TRUE 
#> Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
#> Path to PROJ shared files: /home/filoche/.local/share/proj:/usr/share/proj
#> PROJ CDN enabled: FALSE
#> Linking to sp version:1.6-1
#> To mute warnings of possible GDAL/OSR exportToProj4() degradation,
#> use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.

Created on 2023-06-05 with reprex v2.0.2