readOGR(rgdal)无法从XML获取多边形名称 [英] readOGR (rgdal) fails to fetch polygon names from XML

查看:116
本文介绍了readOGR(rgdal)无法从XML获取多边形名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入英格兰CCG边界的KML地图(在此处可用,使用包rgdal中的readOGR函数将200Kb 转换为R.我的最终目标是通过根据一些相关值为CCG着色来创建热图.我在一个数据帧中的CCG名称旁边有一个带有这些值的列表.我需要将该数据框中的CCG名称与导入的地图对象中的CCG名称进行匹配,并根据该值分配颜色.但是,我看不到在地图对象中导入的任何CCG名称,尽管它们存在于KML文件中.这就是我正在做的:

I am trying to import an KML map of CCG boundaries in England (Available here, 200Kb) into R using readOGR function from package rgdal. My end-goal is to create a heat-map by colouring CCGs according to some associated value. I have a list with those values next to CCG names in one data frame. I need to match CCG names in that data frame with CCG names in the imported map object, and assign colours based on the value. However, I cannot see any CCG names imported in the map object, although they are present in the KML file. This is what I am doing:

library(sp)
library(rgdal)
library(maps)
library(maptools)

假设KML文件位于工作目录中. 清单层:

Assuming the KML file is in the working directory. Listing layers:

ogrListLayers("Clinical_Commissioning_Groups_April_2016_Ultra_Generalised_Clipped_Boundaries_in_England.KML")

读取OGRGeoJSON层:

ccg_boundaries <- ReadOGR("Clinical_Commissioning_Groups_April_2016_Ultra_Generalised_Clipped_Boundaries_in_England.KML","OGRGeoJSON")

R Studio显示对象中有两个部分(对吗?).

R Studio shows there are two sections (right word?) in the object.

polygons,其中包含每个多边形的数据,例如对于第一个:

polygons, which contains data for each polygon, e.g. for the first one:

> ccg_boundaries@polygons[1]
[[1]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -2.104671 54.040320
Slot "area":
[1] 0.168067
...

data,带有两个变量(NameDescription),我希望它们包含CCG名称,但它为空:

And data, with two variables (Name and Description) which I would expect to contain CCG names, but it is empty:

> ccg_boundaries@data
    Name Description
0                   
1                   
2                   
3                   
4                   
5         

但是,KML文件中包含CCG名称,如果使用Word编辑器(例如,按字母顺序排列的第一个字母是"NHS Airedale,Wharfedale和Craven".

However, the CCG names are there in the KML file, which can be seen if opened with a Word editor, e.g. the first one in the alphabetic order is "NHS Airedale, Wharfedale and Craven".

<PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#OGRGeoJSON">
        <SimpleData name="objectid">1</SimpleData>
        <SimpleData name="ccg16cd">E38000001</SimpleData>
        <SimpleData name="ccg16nm">NHS Airedale, Wharfedale and Craven CCG</SimpleData>

是否可能有一个选项来读取OGR或其他一些选项来提取它们并将其包含在对象中?

Is there maybe an option to readOGR or some other option to extract them and include in the object?

推荐答案

好的,如果有人遇到相同的问题,这是我找到的解决方案.

OK, if anyone encounters the same problem, here is the solution I found.

网站以两种格式提供地图: KML和SHP .我选择了KML,因为在我遵循的工作示例中使用了KML.但是,这个特定的KML文件或其生成方式似乎存在问题.我改用Shapefile(SHP)尝试了该过程,它的工作原理像一个魅力.

The website provides the maps in two formats: KML and SHP. I chose KML, because this was used in a worked example that I was following. But there appears to be a problem with this particular KML file or how it was generated. I tried the procedure with a Shapefile (SHP) instead, and it worked like a charm.

可以通过相同的函数将形状文件读入R,但无需指定层:

Shapefiles can be read into R by the same function, but don't need specifying the layer:

ccg_boundaries <- ReadOGR("Clinical_Commissioning_Groups_April_2016_Ultra_Generalised_Clipped_Boundaries_in_England.SHP")

CCG名称现在在ccg16nm变量中:

CCG names are now there in the ccg16nm variable:

> head(ccg_boundaries@data)
  objectid   ccg16cd                                 ccg16nm st_areasha st_lengths
0        1 E38000001 NHS Airedale, Wharfedale and Craven CCG 1224636590  193149.74
1        2 E38000002                         NHS Ashford CCG  582174805  122841.19
2        3 E38000003                  NHS Aylesbury Vale CCG  984352696  229544.11
3        4 E38000004            NHS Barking and Dagenham CCG   36315011   31196.87
4        5 E38000005                          NHS Barnet CCG   86654018   41833.69
5        6 E38000006                        NHS Barnsley CCG  327520495  106476.52

这篇关于readOGR(rgdal)无法从XML获取多边形名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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