[GIS] invalid KML representation on ST_GeomFromKML

kmlpostgis

I exported a table with a geography(MultiPolygon,4326) column using ogr2ogr to a KML file. When trying to re-import any of the shapes using the ST_GeomFromKML function I get the following error:

ERROR:  invalid KML representation

********** Error **********

ERROR: invalid KML representation
SQL state: XX000

A sample of the KML shape I'm passing as argument is:

<Polygon><outerBoundaryIs><LinearRing><coordinates>-3.106121,53.524247,0.0 -3.102785,53.522356,0.0 -3.099633,53.521059,0.0 -3.097637,53.519834,0.0 -3.096472,53.518869,0.0 -3.095176,53.518035,0.0 -3.092952,53.517169,0.0 -3.090586,53.516356,0.0 -3.089088,53.515628,0.0 -3.08863,53.515296,0.0 -3.088306,53.514773,0.0 -3.088044,53.513979,0.0 -3.088025,53.513251,0.0 -3.087312,53.51078,0.0 -3.085799,53.508947,0.0 -3.084285,53.506689,0.0 -3.083638,53.506559,0.0 -3.083456,53.506456,0.0 -3.082374,53.504838,0.0 -3.081294,53.50436,0.0 -3.080699,53.503849,0.0 -3.079781,53.50176,0.0 -3.080216,53.501094,0.0 -3.080577,53.500779,0.0 -3.080403,53.500497,0.0 -3.080611,53.500357,0.0 -3.080569,53.500262,0.0 -3.079844,53.499901,0.0 -3.078279,53.500012,0.0 -3.077074,53.499674,0.0 -3.077043,53.499568,0.0 -3.077502,53.499601,0.0 -3.077802,53.499724,0.0 -3.078244,53.499719,0.0 -3.078791,53.499417,0.0 -3.07865,53.499375,0.0 -3.078458,53.499047,0.0 -3.078545,53.498628,0.0 -3.078439,53.498512,0.0 -3.078565,53.4983,0.0 -3.07818,53.49812,0.0 -3.078418,53.497947,0.0 -3.078275,53.497887,0.0 -3.078502,53.497779,0.0 -3.078095,53.49778,0.0 -3.077909,53.497649,0.0 -3.076642,53.495559,0.0 -3.074094,53.49256,0.0 -3.073583,53.492224,0.0 -3.071519,53.491591,0.0 -3.069933,53.490344,0.0 -3.067873,53.487934,0.0 -3.066784,53.486822,0.0 -3.066341,53.485681,0.0 -3.064965,53.48452,0.0 -3.064071,53.483344,0.0 -3.063361,53.48261,0.0 -3.062358,53.481779,0.0 -3.060913,53.480844,0.0 -3.059644,53.479436,0.0 -3.058885,53.479084,0.0 -3.058551,53.478809,0.0 -3.057679,53.477685,0.0 -3.056766,53.477329,0.0 -3.056209,53.477321,0.0 -3.054533,53.476541,0.0 -3.054158,53.476071,0.0 -3.053637,53.475686,0.0 -3.050002,53.473287,0.0 -3.049804,53.473026,0.0 -3.049445,53.471779,0.0 -3.049255,53.471421,0.0 -3.048791,53.470924,0.0 -3.04766,53.470316,0.0 -3.046093,53.469765,0.0 -3.042068,53.466788,0.0 -3.041516,53.466488,0.0 -3.041086,53.466535,0.0 -3.041495,53.466257,0.0 -3.041753,53.465722,0.0 -3.041636,53.465445,0.0 -3.041106,53.464933,0.0 -3.028521,53.455075,0.0 -3.028182,53.454884,0.0 -3.027857,53.45489,0.0 -3.026988,53.455444,0.0 -3.025183,53.455136,0.0 -3.023849,53.454794,0.0 -3.034479,53.451557,0.0 -3.028883,53.44732,0.0 -3.028206,53.446716,0.0 -3.024574,53.443565,0.0 -3.02148,53.440243,0.0 -3.019852,53.437712,0.0 -3.019207,53.436152,0.0 -3.008783,53.438358,0.0 -3.008371,53.43831,0.0 -3.007707,53.438365,0.0 -3.00601,53.438769,0.0 -3.005977,53.438903,0.0 -3.00612,53.43908,0.0 -3.005502,53.439219,0.0 -3.005371,53.439045,0.0 -3.005054,53.439002,0.0 -3.000359,53.439912,0.0 -3.000583,53.440287,0.0 -2.997346,53.440855,0.0 -2.9974,53.440947,0.0 -2.993993,53.441549,0.0 -2.992722,53.440295,0.0 -2.991689,53.440711,0.0 -2.991084,53.440188,0.0 -2.990493,53.440356,0.0 -2.990529,53.440588,0.0 -2.979872,53.442502,0.0 -2.979102,53.443306,0.0 -2.977634,53.442823,0.0 -2.974937,53.443332,0.0 -2.976007,53.444715,0.0 -2.97629,53.445262,0.0 -2.974999,53.446484,0.0 -2.974857,53.447385,0.0 -2.973437,53.447316,0.0 -2.973428,53.447785,0.0 -2.973775,53.449643,0.0 -2.972951,53.449692,0.0 -2.97298,53.449867,0.0 -2.972666,53.450457,0.0 -2.971614,53.452187,0.0 -2.971732,53.456193,0.0 -2.973435,53.46015,0.0 -2.973839,53.462714,0.0 -2.969575,53.465769,0.0 -2.969663,53.465859,0.0 -2.969578,53.46592,0.0 -2.969853,53.465944,0.0 -2.967359,53.467739,0.0 -2.966448,53.467817,0.0 -2.966339,53.467743,0.0 -2.963667,53.467923,0.0 -2.962821,53.468017,0.0 -2.961919,53.468241,0.0 -2.961735,53.468119,0.0 -2.960256,53.467949,0.0 -2.960341,53.467635,0.0 -2.959328,53.467643,0.0 -2.958559,53.468798,0.0 -2.95817,53.468718,0.0 -2.9572,53.47075,0.0 -2.956333,53.472971,0.0 -2.955776,53.472797,0.0 -2.955724,53.472403,0.0 -2.955166,53.472404,0.0 -2.954904,53.472659,0.0 -2.953994,53.472542,0.0 -2.953036,53.472757,0.0 -2.947897,53.473048,0.0 -2.947174,53.472178,0.0 -2.946174,53.472959,0.0 -2.939856,53.473354,0.0 -2.939865,53.47356,0.0 -2.939051,53.473625,0.0 -2.938246,53.47359,0.0 -2.933399,53.473813,0.0 -2.933291,53.473095,0.0 -2.929877,53.473624,0.0 -2.927723,53.473805,0.0 -2.928387,53.473444,0.0 -2.92765,53.47332,0.0 -2.922593,53.474976,0.0 -2.921212,53.475508,0.0 -2.919153,53.476761,0.0 -2.918951,53.477114,0.0 -2.919146,53.477806,0.0 -2.919106,53.478046,0.0 -2.918442,53.478125,0.0 -2.917992,53.478305,0.0 -2.917247,53.479099,0.0 -2.916975,53.479146,0.0 -2.916838,53.479276,0.0 -2.917187,53.480115,0.0 -2.919585,53.48205,0.0 -2.917227,53.482278,0.0 -2.914885,53.482398,0.0 -2.913795,53.482649,0.0 -2.912705,53.483164,0.0 -2.912205,53.483657,0.0 -2.91225,53.484216,0.0 -2.911695,53.485177,0.0 -2.911211,53.485751,0.0 -2.910712,53.48663,0.0 -2.90895,53.488823,0.0 -2.907143,53.490033,0.0 -2.905172,53.492011,0.0 -2.903678,53.49169,0.0 -2.899819,53.491659,0.0 -2.899466,53.491739,0.0 -2.899473,53.492192,0.0 -2.899809,53.492365,0.0 -2.899347,53.493669,0.0 -2.898825,53.494125,0.0 -2.89797,53.494441,0.0 -2.897963,53.494837,0.0 -2.897303,53.494941,0.0 -2.896775,53.4954,0.0 -2.896588,53.495934,0.0 -2.896753,53.496265,0.0 -2.896411,53.496679,0.0 -2.897077,53.497553,0.0 -2.897848,53.498079,0.0 -2.897991,53.49851,0.0 -2.898604,53.499143,0.0 -2.898842,53.499649,0.0 -2.898815,53.499843,0.0 -2.898551,53.500151,0.0 -2.898488,53.500744,0.0 -2.897817,53.501163,0.0 -2.897766,53.50138,0.0 -2.89545,53.502327,0.0 -2.894304,53.502613,0.0 -2.893806,53.502916,0.0 -2.893012,53.502967,0.0 -2.892321,53.503149,0.0 -2.891336,53.502909,0.0 -2.889697,53.502816,0.0 -2.888014,53.50381,0.0 -2.888206,53.504217,0.0 -2.888924,53.505141,0.0 -2.889016,53.505735,0.0 -2.889729,53.507136,0.0 -2.892103,53.510805,0.0 -2.892387,53.510989,0.0 -2.892414,53.511124,0.0 -2.887744,53.514539,0.0 -2.885885,53.517333,0.0 -2.885558,53.518151,0.0 -2.884605,53.519129,0.0 -2.884081,53.519539,0.0 -2.883361,53.519554,0.0 -2.881233,53.519981,0.0 -2.881741,53.520522,0.0 -2.88203,53.520483,0.0 -2.882823,53.5206,0.0 -2.883971,53.52063,0.0 -2.886327,53.521058,0.0 -2.887464,53.521476,0.0 -2.887935,53.521739,0.0 -2.888207,53.521738,0.0 -2.888384,53.521876,0.0 -2.88898,53.522035,0.0 -2.889111,53.522189,0.0 -2.889475,53.522174,0.0 -2.889647,53.522267,0.0 -2.889868,53.522126,0.0 -2.89013,53.522158,0.0 -2.890382,53.521935,0.0 -2.890946,53.52195,0.0 -2.891189,53.522144,0.0 -2.891389,53.522182,0.0 -2.891688,53.522129,0.0 -2.892062,53.521928,0.0 -2.892502,53.522046,0.0 -2.893365,53.521789,0.0 -2.893989,53.52174,0.0 -2.894328,53.521587,0.0 -2.894696,53.521607,0.0 -2.894768,53.52144,0.0 -2.895137,53.521333,0.0 -2.895712,53.521565,0.0 -2.895746,53.521739,0.0 -2.896003,53.521832,0.0 -2.896163,53.52178,0.0 -2.896188,53.521531,0.0 -2.896511,53.521538,0.0 -2.896713,53.521737,0.0 -2.896699,53.52195,0.0 -2.896828,53.52197,0.0 -2.897153,53.521843,0.0 -2.897205,53.521667,0.0 -2.897472,53.521655,0.0 -2.897878,53.522095,0.0 -2.898161,53.522042,0.0 -2.898259,53.521892,0.0 -2.898534,53.521775,0.0 -2.898493,53.521585,0.0 -2.898596,53.521567,0.0 -2.899122,53.521828,0.0 -2.89916,53.521972,0.0 -2.899779,53.522045,0.0 -2.901564,53.521616,0.0 -2.906293,53.521708,0.0 -2.907245,53.521982,0.0 -2.907787,53.522417,0.0 -2.908567,53.522469,0.0 -2.908775,53.522351,0.0 -2.909224,53.522514,0.0 -2.909375,53.5227,0.0 -2.910289,53.522666,0.0 -2.910842,53.522775,0.0 -2.910843,53.522904,0.0 -2.910974,53.522957,0.0 -2.911315,53.522746,0.0 -2.911555,53.522776,0.0 -2.911736,53.522903,0.0 -2.911448,53.523031,0.0 -2.911344,53.523185,0.0 -2.91158,53.523321,0.0 -2.912084,53.523161,0.0 -2.912207,53.523256,0.0 -2.91221,53.523447,0.0 -2.912701,53.523369,0.0 -2.912653,53.523506,0.0 -2.91274,53.523537,0.0 -2.912991,53.523422,0.0 -2.913317,53.523603,0.0 -2.913476,53.523365,0.0 -2.913845,53.523476,0.0 -2.913907,53.523412,0.0 -2.914092,53.523556,0.0 -2.914376,53.523563,0.0 -2.914359,53.523368,0.0 -2.914468,53.523281,0.0 -2.914931,53.52353,0.0 -2.915257,53.523468,0.0 -2.915454,53.523638,0.0 -2.915397,53.523704,0.0 -2.915685,53.52368,0.0 -2.915904,53.523976,0.0 -2.916348,53.523853,0.0 -2.916361,53.523681,0.0 -2.916782,53.5235,0.0 -2.916742,53.523182,0.0 -2.916918,53.523145,0.0 -2.917163,53.523478,0.0 -2.916958,53.523817,0.0 -2.917012,53.523952,0.0 -2.919085,53.524357,0.0 -2.920792,53.52493,0.0 -2.921877,53.525451,0.0 -2.922459,53.525514,0.0 -2.922845,53.525376,0.0 -2.922996,53.525235,0.0 -2.92386,53.525569,0.0 -2.924108,53.525764,0.0 -2.924118,53.525919,0.0 -2.923966,53.526027,0.0 -2.924865,53.526221,0.0 -2.925641,53.526888,0.0 -2.925808,53.527293,0.0 -2.925899,53.528769,0.0 -2.926315,53.529478,0.0 -2.926962,53.529989,0.0 -2.928123,53.530485,0.0 -2.92905,53.530515,0.0 -2.929344,53.530596,0.0 -2.929931,53.531072,0.0 -2.930518,53.531263,0.0 -2.930855,53.531168,0.0 -2.931259,53.531225,0.0 -2.931357,53.531422,0.0 -2.932688,53.53245,0.0 -2.9329,53.532377,0.0 -2.933093,53.532427,0.0 -2.933222,53.532764,0.0 -2.933432,53.532891,0.0 -2.933813,53.533394,0.0 -2.933836,53.533586,0.0 -2.933692,53.533789,0.0 -2.933754,53.534075,0.0 -2.933611,53.534415,0.0 -2.934054,53.534883,0.0 -2.934576,53.535045,0.0 -2.934452,53.5355,0.0 -2.934659,53.53573,0.0 -2.93463,53.535837,0.0 -2.934841,53.535943,0.0 -2.935127,53.535921,0.0 -2.935247,53.536026,0.0 -2.935282,53.536166,0.0 -2.93509,53.536224,0.0 -2.935315,53.536563,0.0 -2.935081,53.536589,0.0 -2.934963,53.536489,0.0 -2.934775,53.536664,0.0 -2.935156,53.537025,0.0 -2.935404,53.537138,0.0 -2.935901,53.536999,0.0 -2.936029,53.537059,0.0 -2.936384,53.537029,0.0 -2.936162,53.537491,0.0 -2.936402,53.537538,0.0 -2.936412,53.537746,0.0 -2.93598,53.537887,0.0 -2.935957,53.537973,0.0 -2.93612,53.538238,0.0 -2.936081,53.538396,0.0 -2.936773,53.538697,0.0 -2.937655,53.538535,0.0 -2.937918,53.538841,0.0 -2.938232,53.538508,0.0 -2.938416,53.538714,0.0 -2.938607,53.538759,0.0 -2.938744,53.538502,0.0 -2.939063,53.538737,0.0 -2.939084,53.539092,0.0 -2.939651,53.538995,0.0 -2.939926,53.539304,0.0 -2.94041,53.5394,0.0 -2.940709,53.539608,0.0 -2.940873,53.539495,0.0 -2.941262,53.539877,0.0 -2.941567,53.539685,0.0 -2.94145,53.540326,0.0 -2.941606,53.540666,0.0 -2.941888,53.540679,0.0 -2.942013,53.540994,0.0 -2.943086,53.541068,0.0 -2.943685,53.541191,0.0 -2.943925,53.540939,0.0 -2.944725,53.54122,0.0 -2.9449,53.541339,0.0 -2.944613,53.541604,0.0 -2.944753,53.541723,0.0 -2.944743,53.541876,0.0 -2.945134,53.542046,0.0 -2.945223,53.542508,0.0 -2.945478,53.542808,0.0 -2.945925,53.542987,0.0 -2.94718,53.54376,0.0 -2.947876,53.544319,0.0 -2.948319,53.544356,0.0 -2.950904,53.544269,0.0 -2.951182,53.544502,0.0 -2.952022,53.544896,0.0 -2.953665,53.545277,0.0 -2.95408,53.545569,0.0 -2.954628,53.545665,0.0 -2.95504,53.546054,0.0 -2.956041,53.545892,0.0 -2.956182,53.545901,0.0 -2.95637,53.546072,0.0 -2.956607,53.546083,0.0 -2.9569,53.545839,0.0 -2.95747,53.54575,0.0 -2.957769,53.545604,0.0 -2.958192,53.545693,0.0 -2.958665,53.545571,0.0 -2.959714,53.545547,0.0 -2.96054,53.545948,0.0 -2.961249,53.546025,0.0 -2.963006,53.546393,0.0 -2.963226,53.546349,0.0 -2.963271,53.546705,0.0 -2.963343,53.546759,0.0 -2.963539,53.54663,0.0 -2.963623,53.546685,0.0 -2.963579,53.547005,0.0 -2.96381,53.547071,0.0 -2.964109,53.546999,0.0 -2.964372,53.547029,0.0 -2.965169,53.547648,0.0 -2.965653,53.547562,0.0 -2.965746,53.547607,0.0 -2.965873,53.547868,0.0 -2.966788,53.547542,0.0 -2.967211,53.547467,0.0 -2.967509,53.547607,0.0 -2.967918,53.5475,0.0 -2.967978,53.547339,0.0 -2.968453,53.547421,0.0 -2.968628,53.547314,0.0 -2.96912,53.547356,0.0 -2.969138,53.547252,0.0 -2.969943,53.54722,0.0 -2.970178,53.547074,0.0 -2.970416,53.547107,0.0 -2.970549,53.547242,0.0 -2.970206,53.547297,0.0 -2.970322,53.547503,0.0 -2.970612,53.54748,0.0 -2.970795,53.547718,0.0 -2.971294,53.54762,0.0 -2.971415,53.547706,0.0 -2.971427,53.547906,0.0 -2.971586,53.547986,0.0 -2.972061,53.547882,0.0 -2.972575,53.54794,0.0 -2.972958,53.547791,0.0 -2.973302,53.547869,0.0 -2.973263,53.547673,0.0 -2.974094,53.547551,0.0 -2.974178,53.547393,0.0 -2.974468,53.547363,0.0 -2.974569,53.547202,0.0 -2.974943,53.54727,0.0 -2.975188,53.547231,0.0 -2.975542,53.546973,0.0 -2.97558,53.54682,0.0 -2.976509,53.546612,0.0 -2.977229,53.546262,0.0 -2.977396,53.546018,0.0 -2.977188,53.545831,0.0 -2.977641,53.545902,0.0 -2.977794,53.545299,0.0 -2.978119,53.545004,0.0 -2.978487,53.543975,0.0 -2.978348,53.542732,0.0 -2.977547,53.542199,0.0 -2.977282,53.541717,0.0 -2.976838,53.541361,0.0 -2.976634,53.54079,0.0 -2.97612,53.540353,0.0 -2.975621,53.540257,0.0 -2.975523,53.540122,0.0 -2.974719,53.539817,0.0 -2.974391,53.539611,0.0 -2.974068,53.538664,0.0 -2.973702,53.538015,0.0 -2.97281,53.537102,0.0 -2.972846,53.536123,0.0 -2.971895,53.535162,0.0 -2.971278,53.534807,0.0 -2.970501,53.534131,0.0 -2.97033,53.533334,0.0 -2.970048,53.532851,0.0 -2.969679,53.532476,0.0 -2.969211,53.531515,0.0 -2.968591,53.530664,0.0 -2.968481,53.53002,0.0 -2.967938,53.529576,0.0 -2.967764,53.529299,0.0 -2.967134,53.528191,0.0 -2.966849,53.527372,0.0 -2.965091,53.526349,0.0 -2.96483,53.526007,0.0 -2.964184,53.525603,0.0 -2.96405,53.525381,0.0 -2.963275,53.524815,0.0 -2.963233,53.524316,0.0 -2.962991,53.523684,0.0 -2.963025,53.522881,0.0 -2.963464,53.521956,0.0 -2.96364,53.520497,0.0 -2.964197,53.51964,0.0 -2.964048,53.519186,0.0 -2.96417,53.518584,0.0 -2.964699,53.518508,0.0 -2.965234,53.518571,0.0 -2.96724,53.517954,0.0 -2.968171,53.517885,0.0 -2.968382,53.517688,0.0 -2.970205,53.51684,0.0 -2.97121,53.51628,0.0 -2.97169,53.516082,0.0 -2.972473,53.516032,0.0 -2.972971,53.515424,0.0 -2.973772,53.515505,0.0 -2.976101,53.51528,0.0 -2.976457,53.51547,0.0 -2.977009,53.516319,0.0 -2.980611,53.518771,0.0 -2.982029,53.51922,0.0 -2.982559,53.519523,0.0 -2.983437,53.520671,0.0 -2.984,53.521052,0.0 -2.984761,53.521371,0.0 -2.985724,53.52155,0.0 -2.986923,53.521547,0.0 -2.988192,53.521364,0.0 -2.989623,53.521625,0.0 -2.989813,53.521729,0.0 -2.990118,53.522532,0.0 -2.990351,53.522759,0.0 -2.99155,53.52297,0.0 -2.991738,53.52267,0.0 -2.995469,53.519657,0.0 -2.994792,53.519202,0.0 -2.994826,53.519062,0.0 -2.996748,53.518037,0.0 -2.99727,53.517587,0.0 -2.997622,53.517567,0.0 -2.99785,53.517475,0.0 -2.998144,53.516609,0.0 -2.999059,53.515647,0.0 -2.999584,53.514869,0.0 -3.001604,53.513293,0.0 -3.00351,53.512459,0.0 -3.003661,53.511795,0.0 -3.004208,53.511303,0.0 -3.004672,53.511159,0.0 -3.00497,53.510895,0.0 -3.005114,53.510539,0.0 -3.005069,53.510264,0.0 -3.005763,53.510426,0.0 -3.006054,53.510415,0.0 -3.006916,53.510222,0.0 -3.009225,53.510039,0.0 -3.009696,53.509752,0.0 -3.012328,53.508992,0.0 -3.013712,53.508723,0.0 -3.014604,53.509456,0.0 -3.019221,53.512277,0.0 -3.043811,53.525895,0.0 -3.045598,53.526426,0.0 -3.04884,53.528352,0.0 -3.052484,53.529852,0.0 -3.053508,53.530078,0.0 -3.054894,53.530191,0.0 -3.058178,53.530912,0.0 -3.058396,53.531036,0.0 -3.06001,53.530376,0.0 -3.0595,53.530035,0.0 -3.058535,53.528273,0.0 -3.058617,53.528126,0.0 -3.059851,53.527832,0.0 -3.062978,53.527274,0.0 -3.063148,53.526595,0.0 -3.063203,53.526485,0.0 -3.063385,53.526459,0.0 -3.108843,53.525486,0.0 -3.106121,53.524247,0.0</coordinates></LinearRing></outerBoundaryIs></Polygon>

Am I missing anything? The documentation for the ST_GeomFromKML is not very useful for troubleshooting this.

Note: The same happens when exporting to KML using ST_asKML,
i.e.: the following script also throws an "invalid KML representation" error

SELECT ST_GeomFromKML((SELECT ST_asKML((SELECT shape FROM geographies LIMIT 1))));

Edit: Added full KML shape

Best Answer

Hard to tell without the complete shape. I assume that "..." is not actual part of your coordinate list?

Are you sure that your linear ring starts and ends with the same coordinate? That is at least one of the reasons for PostGIS to err on the conversion.