[GIS] Import KML to PostGIS

googlehistorykmlpostgis

I am looking for a way to import my data from KML (example below) to PostGIS database.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Location history from 04/20/2011 to 05/20/2011</name>
<open>1</open>
<description/>
<StyleMap id="multiTrack">
<Pair>
<key>normal</key>
<styleUrl>#multiTrack_n</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#multiTrack_h</styleUrl>
</Pair>
</StyleMap>
<Style id="multiTrack_n">
<IconStyle>
<Icon>
<href>http://earth.google.com/images/kml-icons/track-directional/track-0.png</href>
</Icon>
</IconStyle>
<LineStyle>
<color>99ffac59</color>
<width>6</width>
</LineStyle>
</Style>
<Style id="multiTrack_h">
<IconStyle>
<scale>1.2</scale>
<Icon>
<href>http://earth.google.com/images/kml-icons/track-directional/track-0.png</href>
</Icon>
</IconStyle>
<LineStyle>
<color>99ffac59</color>
<width>8</width>
</LineStyle>
</Style>
<Placemark>
<name>Latitude User</name>
<description>Location history for Latitude User from 04/20/2011 to 05/20/2011</description>
<styleUrl>#multiTrack</styleUrl>
<gx:Track>
<altitudeMode>clampToGround</altitudeMode>
<when>2011-04-20T13:25:36.026-07:00</when>
<gx:coord>18.171574 53.391877 0</gx:coord>
<when>2011-04-20T13:30:31.488-07:00</when>
<gx:coord>18.171574 53.391877 0</gx:coord>
(...)
</gx:Track>
</Placemark>
</Document>
</kml>

Every time (in case of one file) I used Notepad++ (tools "Replace" to remove usless codes and organize to structure possible to import to Excel), Excel for preparing to import to ArcGIS, next used "Add XY Data" to create geometry and export to SHP, but currently I am looking for more automated solution. 🙂

Best Answer

Have you tried ogr2ogr? It can import KML to PostGIS. It's part of the GDAL suite of FOSS tools

Which have binaries here or you can compile your own http://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries

Example in this question: How to import KML file, with custom data, to Postgres/PostGIS database?