0

I'm trying to read a kml from this kmz I unzipped however, when I try to read a layer from it (as I've done it with previous kmzs) it says:

library(rgdal)
unzip("EstacionesClimatologicas.kmz")
ogr <- readOGR("doc.kml", "Puebla")

which gives the following output and error:

OGR data source with driver: KML 
Source: "doc.kml", layer: "Puebla"
with 117 features
It has 2 fields
Error in readOGR("doc.kml", "Puebla") : Feature mismatch

when I check

  ogrInfo("doc.kml", "Puebla") 

says it's wkbPoint with 3 dimensions, How can I get this to run?

1

Digging into the code for rgdal, it seems to read 117 feature IDs, but the call the the OGR C code only returns 99 features. I can't see anything in the KML that might explain why 18 features get dropped.

Try the sf package, which does work nicely, and reads all 117 features:

> library(sf)
Linking to GEOS 3.5.1, GDAL 2.1.2, proj.4 4.9.3, lwgeom 2.3.1 r15264
> p = st_read("./doc.kml","Puebla")
Reading layer `Puebla' from data source `/home/rowlings/Downloads/GE/doc.kml' using driver `LIBKML'
Simple feature collection with 117 features and 11 fields
geometry type:  POINT
dimension:      XYZ
bbox:           xmin: -98.8197 ymin: 17.9794 xmax: -96.7933 ymax: 20.7483
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
> class(p)
[1] "sf"         "data.frame"

Works well.

If you really need sp class objects, you can convert the above:

> ps = as(p,"Spatial")
> class(ps)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
> names(ps)
 [1] "Name"         "description"  "timestamp"    "begin"        "end"         
 [6] "altitudeMode" "tessellate"   "extrude"      "visibility"   "drawOrder"   
[11] "icon" 

There may be other solutions to reading geometries with Z dimensions directly with rgdal, but this works nicely!

Note you don't need to unzip the kmz, you can read directly from it:

> p = st_read("../EstacionesClimatologicas.kmz","Puebla")
Reading layer `Puebla' from data source `/home/rowlings/Downloads/EstacionesClimatologicas.kmz' using driver `LIBKML'
Simple feature collection with 117 features and 11 fields
geometry type:  POINT
  • How can you watch the C code? – Elio Diaz Nov 8 '17 at 22:01
  • I didn't step into the C code, its just the return from a .Call in the R code had fewer features. This line: github.com/cran/rgdal/blob/… eventually triggering the error on line 256. – Spacedman Nov 9 '17 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.