Thursday 27 July 2017

Reading KML Files in R?


I'm hoping to analyze google timeline data in R for a final project in a GIS class,


Google can output timeline data as geojson and KML but it looks like the KML file includes a lot more info (type of transportation distance traveled etc) than the JSON file. Additionally, JSON is an option for the entire time lines but to download a single day/week/month it looks like I have to use KML.


I understand from How to efficiently read a kml file into R and Importing KML files to R that I need to specify the layer info as well as the kml file name to readOGR(), what I'm a little confused about is exactly how the layer names are included in a kml file.


Looks like the tag is associated with the layer name, but there are 122 name tags in the file so its clearly not exclusive to layer. Fine.


using layers <- ogrinfo(data_source) gets me


[1] "INFO: Open of `C:\\Users\\Documents\\GIS_Coursework_3\\history-2018-09-28.kml'"
[2] " using driver `LIBKML' successful."
[3] "1: Location history from 2018-09-28 to 2018-09-28 "


then using Location_History <- readOGR(data_source, "Location history-2018-09-28 to 2018-09-28 ") gives:


Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : 
Multiple incompatible geometries: wkbPoint: 12; wkbLineString: 12

The problem is that there are multiple "sub" layers,


in QGIS i can see that when I open the layer, there is a points layer and a lines layer


sub layers


I dont' see either of these text strings in the KML files anywhere when I open it as a text file.


I could probably just copy those, but it's not particularly useful to me as a one off. I need to get the layer info programmatically, rather than opening QGIS every time.



Is it practical for me to start exploring xml parsing in R?


Is there a package I haven't been able to find that handles this stuff successfully?


Am I missing something obvious about how to read KML layer info?


This is the only feature I've found lacking in R compared to QGIS or ArcGIS. They've been pretty comparable so far which I've found impressive.



Answer



Functions in rgdal read data into sp class objects, which can only contain one type of spatial object - a set of points, or lines, or polygons. The sf package provides classes for geometry vectors that can have different dimensions geometries within.


Using sf::st_read("file.kml") should return an object with a geometry column, and you can filter lines or points or polygons from this object using the st_geometry_type function.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...