<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-AU" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’ve encountered a problem with the ST_GeomFromKML function. I’m not sure if it’s a bug or not. The problem description is:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I have an external data source that I’m loading into a PostGIS-enabled PostgreSQL instance. The data source is KML and needs to be parsed and loaded to the database using just INSERT’s (because of embedded content within it.)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m extracting the XML for the feature spatial description, and using the ST_GeomFromKML() function to convert that XML text to the geometry object for the INSERT statement. This has been working fine for other data sources I’ve got –
however I’ve got a data source, that doesn’t work – and checking it, I can see that it differs from the others because it is using “LinearRing” elements as the “top level” spatial element.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">As far as I understand – the KML XSD (v2.2) allows for a LinearRing to be a top level spatial object (“AbstractGeometryGroup”.) but it seems that the ST_GeomFromKML function won’t accept it and generates an error.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’ve tried wrapping the XML fragment in a “<Polygon><outerBoundaryIs>” element pair, and ST_GeomFromKML accepts that.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’ve reproduced the error on two different versions of PostGIS being v2.3 and 2.5. Details below:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">SQL Statement that produces error:<br>
<pre><o:p></o:p></p>
<p class="MsoNormal">select ST_GeomFromKML('<LinearRing><coordinates><o:p></o:p></p>
<p class="MsoNormal">148,-35<o:p></o:p></p>
<p class="MsoNormal">148,-36 <o:p></o:p></p>
<p class="MsoNormal">144,-36 <o:p></o:p></p>
<p class="MsoNormal">148,-36<o:p></o:p></p>
<p class="MsoNormal">148,-35<o:p></o:p></p>
<p class="MsoNormal"></coordinates></LinearRing>');<o:p></o:p></p>
<p class="MsoNormal"></pre><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">System #1:<o:p></o:p></p>
<p class="MsoNormal">postgis_version:<o:p></o:p></p>
<p class="MsoNormal">2.3 USE_GEOS=1 USE_PROJ=1 USE_STATS=1<o:p></o:p></p>
<p class="MsoNormal">(1 row)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">PostgreSQL version<o:p></o:p></p>
<p class="MsoNormal">PostgreSQL 9.2.18 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.8.5 20150<o:p></o:p></p>
<p class="MsoNormal">623 (Red Hat 4.8.5-11), 64-bit<o:p></o:p></p>
<p class="MsoNormal">(1 row)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Error Message:<o:p></o:p></p>
<p class="MsoNormal">ERROR: invalid KML representation<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">System #2:<o:p></o:p></p>
<p class="MsoNormal">postgis_version:<o:p></o:p></p>
<p class="MsoNormal">2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">PostgreSQL version<o:p></o:p></p>
<p class="MsoNormal">PostgreSQL 10.12, compiled by Visual C++ build 1800, 64-bit<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Error Message:<o:p></o:p></p>
<p class="MsoNormal">ERROR: invalid KML representation<o:p></o:p></p>
<p class="MsoNormal">SQL state: XX000<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Workaround, works successfully in both tested systems:<o:p></o:p></p>
<p class="MsoNormal"><pre><o:p></o:p></p>
<p class="MsoNormal">select ST_GeomFromKML('<Polygon><outerBoundaryIs><LinearRing><coordinates><o:p></o:p></p>
<p class="MsoNormal">148,-35<o:p></o:p></p>
<p class="MsoNormal">148,-36 <o:p></o:p></p>
<p class="MsoNormal">144,-36 <o:p></o:p></p>
<p class="MsoNormal">148,-36<o:p></o:p></p>
<p class="MsoNormal">148,-35<o:p></o:p></p>
<p class="MsoNormal"></coordinates></LinearRing></outerBoundaryIs></Polygon>');<o:p></o:p></p>
<p class="MsoNormal"></pre><o:p></o:p></p>
<p class="MsoNormal">Regards, Bill Teluk<o:p></o:p></p>
</div>
</body>
</html>