【问题标题】:How to find correct projection of the shapefile data for leaflet map in r?如何在 r 中找到传单地图的 shapefile 数据的正确投影?
【发布时间】:2021-08-10 18:53:21
【问题描述】:

我是 geo spatial 数据的新手,正在尝试使用 leafletshapefile 创建 choropleth map

我试图在 传单地图 上绘制数据,但出现错误:错误:必须提供 x/y 属性,这似乎是由于一些投影问题 & 不确定如何根据 leaflet() 更正投影。

我的 shapefile 投影

ind_global$geometry %>% 
  st_crs()

输出:

  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
library(raster)

crs(ind_global$geometry)

CRS arguments: +proj=longlat +datum=WGS84 +no_defs

(更新:

链接到几何数据集ind_global: https://github.com/johnsnow09/covid19-df_stack-code/blob/main/ind_global_rds.rds )

我试过的代码:

library(tidyverse)
library(sf)
libraary(leaflet)
library(htmlwidgets)

pal <- colorBin(palette = "OrRd", 9, domain = ind_global$total_vaccinations)
ind_global %>% 
  st_as_sf() %>% 
  st_transform(crs = "+init=epsg:4326") %>% 
  leaflet() %>% 
  addProviderTiles(provider = "CartoDB.Positron") %>% 
  add_polygons(label = Country.Region,
               stroke = FALSE,
               smoothFactor = .5,
               opacity = 1,
               fillOpacity = 0.7,
               fillColor = ~ pal(total_vaccinations),
               highlightOptions = highlightOptions(weight = 5,
                                                   fillOpacity = 1,
                                                   bringToFront = TRUE)) %>% 
  
  addLegend("bottomright",
            pal = pal,
            values = ~ total_vaccinations,
            title = "total Vaaccinations",
            opacity = 0.7)

也尝试了以下预测,但出现错误:

st_transform(crs = "+proj=longlat +datum=WGS84 +no_defs")

st_transform(crs = "+proj=longlat +ellps=GRS80")


使用 ggplotgeom_sf()

绘制时的 Shape 文件

【问题讨论】:

    标签: r leaflet geospatial projection sf


    【解决方案1】:

    我认为这个问题至少在我运行您的代码时是 Country.Region 是一个因素而不是字符变量。我只对您的代码进行了一些编辑,并得到了我认为您所追求的:

    library(sf)
    library(leaflet)
    library(htmlwidgets)
    library(riskyr)
    
    ind_global<-readRDS("C:/Users/SCMCKENZIE/Downloads/ind_global_rds.rds")
    
    pal <- colorBin(palette = "OrRd", 9, domain = ind_global$total_vaccinations)
    
    tmp<-ind_global %>% 
      st_as_sf() %>% 
      st_transform(crs = "+init=epsg:4326")
    tmp %>% leaflet() %>% 
      addProviderTiles(provider = "CartoDB.Positron") %>% 
      addPolygons(label = as.character(tmp$Country.Region),
                   stroke = FALSE,
                   smoothFactor = .5,
                   opacity = 1,
                   fillOpacity = 0.7,
                   fillColor = ~ pal(total_vaccinations),
                   highlightOptions = highlightOptions(weight = 5,
                                                       fillOpacity = 1,
                                                       bringToFront = TRUE)) %>% 
      
      addLegend("bottomright",
                pal = pal,
                values = ~ total_vaccinations,
                title = "total Vaaccinations",
                opacity = 0.7)
    

    【讨论】:

    • 谢谢肖恩,但我不确定这是否是问题所在,因为在此处提出帖子之前,我通过评论 label = Country.Region 尝试了代码,但没有成功,现在我运行我的原始代码并编辑label = as.character(Country.Region) & 它仍然没有工作。但是,当我运行将数据保存为对象的代码时,首先可以完美运行。非常感谢您的帮助,因为您的代码运行良好!
    • 我认为将变量称为数据框的一部分是必要的:tmp$Country.Region 否则它会给出一个奇怪的错误,因为在此函数中使用了另一个变量 total_vaccination 而没有提及数据框$变种。有时我根本没有得到 r 函数,并且在那里也没有提到任何帮助
    • 我猜为什么使用tmp$Country.Region 是必要的,虽然我不确定这是否属实,但你不能使用%&gt;% 管道语法将变量强制转换为新的数据类型而不明确使用 $ 从数据框中定义它。我用 tidyverse 包遇到过这个问题。 Tidyverse 有很多很棒的功能,但我认为它假设每个使用它的人都是计算机程序员,其中一些怪癖是不言而喻的。我自己不是程序员,我经常遇到这些障碍。
    • 是的,我完全同意你的看法。我也不是程序员,所以我每隔一天就面临这些挑战!
    猜你喜欢
    • 1970-01-01
    • 2016-11-02
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多