How to Import KML File with Custom Data to Postgres/PostGIS Database

kmlogr2ogrpostgis

By custom, I don't mean a different structure of KML file, but in <description></description> section, I have data with HTML tags of this kind:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
  <name>New York City</name>
  <description><![CDATA[
    <table>
      <tr><td colspan='2'>Attributes</td></tr>
      <tr><td>CITY_ID</td><td>150335</td></tr>
      <tr><td>POPULATION</td><td>8244910</td></tr>
      <tr><td>AREA</td><td>468.48</td></tr>
    </table>]]>
  </description>
  <Point>
    <coordinates>40.712964,-74.003886,0</coordinates>
  </Point>
</Placemark>
</Document>
</kml>

I want to easily import all this data, for each placemark, to a table with corresponding columns in the database, how to do this?

Best Answer

you can use ST_GeomFromKML as this page.

SELECT ST_GeomFromKML('
        <LineString>
            <coordinates>-71.1663,42.2614 
                -71.1667,42.2616</coordinates>
        </LineString>');

or you can use ogr2ogr as following to process entire KML files;

ogr2ogr -f "PostgreSQL" PG:"host=yourhost user=youruser dbname=yourdb 
                            password=yourpass" inputfilename.kml

i hope it helps you...

Related Question