传单R衍生多边形缺失纬度缺失长期缺失 [英] Leaflet R derivePolygons missing lat missing long

查看:119
本文介绍了传单R衍生多边形缺失纬度缺失长期缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在地图上绘制一些疾病事件数据的位置.

I am trying to plot the site of some disease-events data on a map.

我用它来导入数据:

ByTown<-readOGR(dsn="C:/temp/lyme/Towns", layer="Towns", encoding = "UTF-8", verbose= FALSE)

检查课程:

class(ByTown)
#getting this result
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

然后,我将所有因子转换为字符数据,并再次使用class来检查是否还有SpatialPolygonsDataFrame,我这样做:

Then I convert all of the factors to character data and check to see that I still have a SpatialPolygonsDataFrame using class again, which I do:

然后,我将希望合并的数据格式化为与原始标题相同的标题:

Then I format the data I wish to merge into the same title case as the original:

townCount$City<-str_to_title(townCount$City)

然后我将计数数据geo_join到空间多边形数据框:

Then I geo_join the count data to the spatial polygon data frame:

ByTown<-geo_join(ByTown, townCount,"MCD_NAME", "City")

然后我设置调色板并运行映射:

Then I set the palette and run the mapping:

pal = colorQuantile("PuOr",ByTown$count, n=5 )
map<-leaflet(ByTown) %>%
  addProviderTiles("CartoDB.Positron")%>%
  addPolygons(fillColor = ~pal(count),
            color = "#000000",
            stroke = TRUE,
            weight = 1,
            smoothFactor = 0.5,
            options(viewer = NULL))
map

我收到此错误:

Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") : 
  addPolygons must be called with both lng and lat, or with neither.

我已经查看了坐标槽,并且那里有数据...我对错误感到困惑,没有在网上找到任何有用的答案.这是坐标槽中第一个多边形的头部:

I have looked in the coordinates slots and there is data in there...I am baffled by the error and not finding any useful answers online. Here is the head of the first polygon in coordinates slot:

head(nByTown@polygons[[1]]@Polygons[[1]]@coords )

           [,1]     [,2]
[1,] 1036519 916318.7
[2,] 1036039 916355.8
[3,] 1031757 916299.7
[4,] 1027474 916244.5
[5,] 1026709 916198.1
[6,] 1026826 916248.3

每个人都有这个问题,找出根本原因并解决吗?

Any one every have this issue, identify the root cause and fix it?

推荐答案

addPolygons函数要么要求您定义纬度&列,经度,否则它将尝试从您提供的数据中得出它们.

The addPolygons function either requires you to define the columns of latitude & longitude, OR it will try and derive them from the data you provide.

鉴于您尚未指定纬度/经度列,它将尝试找出它们是哪一个.在您的情况下,找不到它们.这主要是由于您的数据不是经/纬格式.

Given you haven't specified the lat/lon columns, it tries to work out which ones they are. In your case, it couldn't find them. This is mainly due to your data not being in lat/lon format.

因此,您需要将数据转换为使用经纬度投影,例如(未测试)

Therefore, you'll need to transform your data to use a lat/lon projection, something like (untested)

nByTown_latlon <- spTransform(nByTown, CRS("+proj=longlat +datum=WGS84"))

这篇关于传单R衍生多边形缺失纬度缺失长期缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆