readOGR (rgdal) fails to fetch polygon names from XML

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

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) 

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

ogrListLayers("Clinical_Commissioning_Groups_April_2016_Ultra_Generalised_Clipped_Boundaries_in_England.KML") 

Reading OGRGeoJSON layer:

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

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

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 ... 

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          

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> 

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

回答1:

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

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.

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 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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!