2

I am using the following PHP code in order to read data from PostGIS and save it in KML format as a file. When I load the page a document with ".PHP" extension is returned (not .kml) which inside has the data in KML format. The page returns no error, but the console of the developer tools gives the following note:

Resource interpreted as Document but transferred with MIME type application/vnd.google-earth.kml+xml: http://mydomain.com/....

Does anyone have any idea what the problem could be?

The PHP code:

<?php

$dbconn = pg_connect("dbname='nyc' user='postgres' password='****' host='localhost'");
if (!$dbconn) {
    echo "Not connected : " . pg_error();
    exit;
}

$sql = 'SELECT *, ST_AsKML(ST_Transform(geom,26918)) AS kmlgm FROM nyc_census_blocks LIMIT 5';

 # Try query or error
$query_result = pg_query($dbconn, $sql);
if (!$query_result) {
    echo "An SQL error occured.\n";
    exit;
}

$kml ="<kml><Document>";

for ($i = 0; $i < pg_numrows($query_result); $i++) {

    $townname = pg_result($query_result, $i, 0);
    $townkml =  pg_result($query_result, $i, 1);

    $kml .= "<Placemark><name>Town: ".$townname."</name><description>".$townname."</description>".$townkml."</Placemark>\n";
}

$kml .= "</Document></kml>";

header('Content-Type: application/vnd.google-earth.kml+xml');

echo $kml;

pg_close($dbconn);
?>

and the kml data returned inside a file with ".php" suffix!!:

<kml><Document><Placemark><name>Town: 1</name><description>1</description>360850009001000</Placemark>
<Placemark><name>Town: 2</name><description>2</description>360850020011000</Placemark>
<Placemark><name>Town: 3</name><description>3</description>360850040001000</Placemark>
<Placemark><name>Town: 4</name><description>4</description>360850074001000</Placemark>
<Placemark><name>Town: 5</name><description>5</description>360850096011000</Placemark>
</Document></kml>
4

If you want to make browser download the generated KML, an extra header is needed:

header("Content-Disposition: attachment; filename=MY_KML_FILENAME.kml");

Otherwise, just ignore the message, it's harmless. Chrome doesn't recognize the KML-specific Content-Type, and expects ordinary text/xml or application/xml, but since it's all XML, it should be parsed correctly. Do you have any trouble displaying the KML in map application or Google Earth?

  • Thanks for the tip. That actually worked, but now there is another problem with the code. when I check the generated kml file with geojson.io , gives me an error of 0(zero) features imported. Do you have any idea how to fix this? – Catlover Feb 25 '14 at 10:52
  • Now I see that the generated KML doesn't contain any geometry. First, remove the ST_Transform part (ST_AsKML will automagically transform the geometry to EPSG:4326) and run the query in some non-GIS software like pgAdmin or Postgres shell. Then inspect the results and post here. – Michal Mackiewicz Feb 25 '14 at 12:01
  • Just did what you said, and queried in pgAdmin, returns the rows with correct information. For example, the value for the first row for kmlgm (the geometry field) is: – Catlover Feb 25 '14 at 12:39
  • "<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>-74.079205770132376,40.6434307837456 -74.079135046476281,40.64333789954803 -74.079907504924435,40.642973348520506 -74.080594280570295,40.643841897111301 -74.080808877568955,40.644115449954064 -74.081942655209758,40.644028080539293 -74.082072684564423,40.644207605437828 -74.082371181489705,40.644645079984301 -74.080281982569403,40.644793215660968 -74.079205770132376,40.6434307837456</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>" – Catlover Feb 25 '14 at 12:40
  • I also made the changes in the php code, loaded it again, but the generated .kml file has the same problem as before... Any clue what's causing the problem? – Catlover Feb 25 '14 at 12:41

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.