Search code examples
rkml

Corrupt KML file when converting shapefile to kml in R using kml function from plotKML


i have a shapefile and i am trying to convert it to a kml file so that i can use it in Qlik Sense. I would like to have the kml area and kml name (tagged as Sector_ID) stored in the kml. Below is the code i used

I have tried various packages but none seems to be working. I can only get the kml area without the right labels (Sector_ID) so far. One of the ways i have tried is the kml function in the plotKML package but it is corrupted when i tried to read them in Qlik.

mapping_sector <- readOGR("data/Sectors/Sectors.shp",
                          ogrListLayers("data/Sectors/Sectors.shp"))
crs(mapping_sector) <- "+init=epsg:3414"
mapping_sector <- spTransform(mapping_sector, "+init=epsg:4326")

kml(obj=mapping_sector, folder.name="", file.name="Test.kml", 
    kmz=FALSE,labels=Sector_ID)

Appreciate if any help can be given to my code above or if any other ways i can solve it using R. Thank you.


Solution

  • With sf :

    library(sf)
    library(dplyr)
    
    read_sf("data/Sectors/Sectors.shp") %>%
      st_set_crs(3414L) %>%
      st_transform(4326L) %>%
      write_sf("Test.kml", dataset_options = c("NameField=Sector_ID"))