Skip to content

convert KML file into shape file or CSV file

5 messages · Alex Mandel, Barry Rowlingson, zhijie zhang

#
rusers.sh wrote:
The way to do it in R is with the rgdal library (install.packages("rgdal")

-- psuedo code --
library(rgdal)
readOGR("pathtofile",layer.kml)
writeOGR("pathtooutput",driver="ESRI Shapefile",layer=output.shp)
-- end of fake code example --

but if you have the rgdal library that means you also have GDAL/OGR
installed and could just us the command line on your system:
ogr2ogr -f "ESRI Shapefile" nameof.shp file.kml

In this particular case, I tried with the file you linked. Turns out
it's not a valid KML file, at least according to OGC standards.
After failing to get any of my GIS tools to read it I checked it using
http://kmlvalidator.com/home.htm
It returns several Errors.

There's a good chance the file still works in Google Earth but nothing
else seems to be able to figure it out.

Alex
#
On Wed, Dec 9, 2009 at 1:22 AM, rusers.sh <rusers.sh at gmail.com> wrote:
As well as the validation problems reported by Alex Mandel, the
structure of this KML file isn't appropriate for straight conversion
to a shape or CSV file.

 It has several parts, some "NetworkLink" structures without URLs that
just provide some HTML info boxes, and some network link structures
that point to other kmz files. You can download these kmz files, unzip
them, and possibly convert them to other formats. For example, one of
the links is to:

http://www.declanbutler.info/Flumaps1/4_Human_cases/Maps_of_human_cases_by
period/Cases_by_period1.kmz

which when unzipped produces doc.kml and some png files for markers.
ogrinfo tells us:

$ ogrinfo doc.kml
Had to open data source read-only.
INFO: Open of `doc.kml'
      using driver `KML' successful.
1: Data (3D Point)
2: Data (3D Point)
3: Data (3D Point)
4: Data (3D Point)
5: Data (3D Point)
6: Data (3D Point)
7: Data (3D Point)
8: Data (3D Point)

 - which is a bit weird, 8 layers all called 'Data'. That makes it
hard to extract individual layers by name. So I edited the file and
renamed them Data to Data8....

 I can then read them in R:
I can then use writeOGR to create shapefiles, but it's easier on the
command line:

$ mkdir shapefile
$ ogr2ogr -f "ESRI Shapefile" shapefile doc.kml

So there's umpteen things going on here, above and beyond any possible
structural problems with the KML in the first place:

1. The original KML file points to a bunch of KMZ files, and readOGR
isn't going to do all that work for you.
2. Even if it could do all that work, the KML is a complex structure
with all sorts of geographic and no-geographic entities and so it
can't bung it all into a simple shapefile
3. The individual KMZ files which do contain the data have multiple layers.
4. The layers within a KMZ file (or at least, the one I bothered to
look at) have identical names which breaks stuff.

Otherwise, it's easy! :)

Barry
#
Almost, in my sleepiness I left out an important part from that pseudo
code. You need to store the data in a variable/object and then write it out.
importeddata <- readOGR("pathtofile",layer.kml)
writeOGR(importeddata ,"pathtooutput",driver="ESRI
Shapefile",layer=output.shp)

Make sure to read the help for both of those commands to get them correct.

Also, unless you're going to do some manipulation in R between those
steps, it's really a indirect method compared to ogr2ogr.

Alex
rusers.sh wrote: