SQLServer SQLGeometry GeomFromGml not loading valid GML due to gml:ring interpreted as gml:LinearRing

michel van der breggen 96 Reputation points
2020-09-21T18:59:52.607+00:00

Dear all,
I am working on a project in which i parse large amounts of GML data into Geometries in an SQL2019 Database. I use the GeomFromGml function for this.

I have the following XML :

<gml:Polygon xmlns:gml=""http://www.opengis.net/gml"">
<gml:exterior>
<gml:Ring>
<gml:curveMember>
<gml:Curve>
<gml:segments>
<gml:LineStringSegment>
<gml:posList>111363.617 565733.045 111361.879 565735.244 111356.086 565740.735 111350.007 565747.617 111347.434 565751.342 111345.526 565753.871 111343.671 565756.136 111341.694 565758.057 111338.588 565760.219 111337.947 565761.002 111337.757 565761.686 111337.787 565762.771 111338.331 565763.885 111339.678 565765.223 111342.055 565766.893 111345.003 565767.978 111347.492 565768.263 111352.363 565768.241 111354.307 565769.264 111355.827 565770.681 111357.033 565773.427 111358.177 565776.890 111359.659 565780.704 111361.275 565783.827 111363.388 565786.534 111366.339 565789.440 111369.820 565792.273 111372.307 565793.876 111374.022 565795.914 111375.804 565798.366 111377.505 565801.398 111379.839 565805.912 111381.730 565808.961 111383.005 565810.772 111384.175 565812.110 111385.662 565813.352 111387.365 565814.072 111390.978 565814.899 111394.671 565815.539 111397.584 565816.884 111399.735 565818.740 111401.246 565821.131 111402.908 565824.277 111404.171 565827.501 111405.259 565829.501 111407.156 565832.035 111409.919 565834.767 111415.103 565840.649 111416.603 565843.023 111418.644 565846.515 111420.936 565850.706 111423.347 565854.623 111432.191 565868.073 111436.999 565876.478 111439.145 565879.454 111440.900 565881.170 111442.908 565883.324 111445.544 565886.415 111448.296 565889.130 111450.842 565891.531 111453.537 565893.321 111456.929 565895.350 111460.499 565896.959 111464.173 565898.366 111466.884 565899.965 111468.772 565901.428 111470.625 565904.174 111471.564 565906.943 111472.790 565909.248 111474.459 565911.618 111475.780 565913.136 111479.013 565914.754 111483.128 565916.627 111492.461 565920.292 111497.370 565922.078 111500.556 565923.436 111502.903 565925.403 111505.075 565927.630 111506.713 565929.875 111508.538 565933.283 111509.235 565936.263 111510.684 565940.163 111512.394 565941.368 111524.253 565942.258 111534.725 565943.340 111540.589 565944.910 111548.740 565948.580 111552.918 565951.538 111556.087 565952.965 111562.529 565953.139 111567.195 565953.592 111570.607 565954.740 111582.215 565961.325 111599.963 565971.071 111604.664 565973.268 111611.041 565975.247 111616.190 565977.944 111620.194 565982.896 111622.512 565986.597 111625.695 565991.251 111631.692 565997.100 111639.286 566001.830 111657.280 566007.054 111666.144 566015.877 111667.134 566022.367 111665.649 566029.791 111664.218 566040.131 111662.396 566059.315 111664.152 566066.337 111668.818 566071.110 111684.242 566077.583 111692.256 566082.356 111708.749 566097.042 111714.169 566101.147 111723.921 566109.478 111726.166 566113.582 111735.127 566125.269 111737.420 566127.458 111741.287 566129.511 111747.174 566133.990 111750.936 566138.011 111753.703 566140.532 111755.306 566142.424 111756.939 566143.653 111752.153 566147.908 111752.219 566145.298 111751.904 566144.100 111751.331 566141.926 111749.591 566139.938 111746.004 566135.359 111741.210 566132.874 111735.208 566128.899 111729.641 566124.242 111726.402 566119.245 111722.823 566113.566 111718.959 566108.910 111707.583 566099.850 111701.106 566094.228 111693.606 566087.357 111687.469 566082.870 111682.015 566080.201 111676.730 566077.816 111664.655 566072.389 111660.668 566067.778 111659.528 566062.654 111659.969 566053.221 111661.397 566044.318 111662.473 566039.183 111662.670 566036.706 111663.878 566027.938 111664.396 566023.638 111662.327 566015.381 111661.405 566013.680 111658.979 566011.724 111650.263 566008.155 111645.269 566006.704 111635.529 566003.608 111633.782 566002.080 111624.716 565993.425 111623.147 565990.908 111620.775 565987.371 111618.064 565983.542 111616.830 565981.577 111616.304 565980.715 111614.500 565979.297 111612.552 565978.373 111600.866 565973.899 111598.565 565972.800 111590.954 565968.180 111584.102 565964.594 111574.802 565959.404 111569.642 565956.880 111564.481 565955.545 111558.393 565955.396 111553.604 565953.895 111551.753 565952.730 111544.837 565948.835 111539.917 565946.807 111532.921 565945.502 111525.529 565944.523 111516.616 565943.979 111511.281 565943.345 111508.110 565941.779 111506.729 565937.715 111506.170 565935.433 111504.067 565928.481 111502.008 565926.369 111499.862 565924.570 111496.887 565923.302 111491.999 565921.524 111482.614 565917.838 111478.446 565915.941 111474.955 565914.194 111473.421 565912.429 111471.666 565909.938 111470.352 565907.466 111469.435 565904.763 111467.799 565902.338 111466.144 565901.056 111463.600 565899.555 111459.993 565898.174 111456.320 565896.518 111452.837 565894.435 111450.023 565892.567 111447.384 565890.078 111444.580 565887.312 111441.926 565884.200 111439.957 565882.088 111438.143 565880.314 111435.887 565877.186 111431.053 565868.733 111425.880 565860.605 111422.335 565855.271 111419.899 565851.313 111417.599 565847.109 111415.579 565843.651 111414.145 565841.381 111409.043 565835.590 111406.247 565832.826 111404.245 565830.152 111403.080 565828.010 111401.814 565824.778 111400.205 565821.732 111398.812 565819.528 111396.921 565817.901 111394.308 565816.696 111390.740 565816.078 111386.994 565815.220 111385.030 565814.390 111383.333 565812.973 111382.061 565811.517 111380.729 565809.626 111378.795 565806.508 111376.450 565801.973 111374.794 565799.020 111373.078 565796.659 111371.507 565794.790 111369.112 565793.246 111365.536 565790.335 111362.488 565787.334 111360.259 565784.477 111358.562 565781.198 111357.045 565777.293 111355.909 565773.854 111354.824 565771.388 111353.603 565770.251 111352.078 565769.450 111347.414 565769.465 111344.719 565769.155 111341.491 565767.966 111338.904 565766.147 111337.342 565764.597 111336.593 565763.067 111336.550 565761.541 111336.854 565760.438 111337.756 565759.336 111340.931 565757.121 111342.783 565755.321 111344.580 565753.127 111346.459 565750.637 111349.057 565746.875 111355.220 565739.898 111361.054 565734.368 111362.789 565732.459 111363.943 565730.307 111363.935 565728.183 111363.206 565725.003 111362.255 565722.222 111360.453 565717.792 111359.328 565714.810 111357.659 565711.520 111356.276 565708.994 111349.874 565700.098 111348.524 565698.550 111345.189 565694.463 111343.292 565691.349 111341.395 565686.624 111339.601 565679.855 111338.530 565676.034 111335.313 565670.879 111332.098 565665.845 111330.169 565660.596 111329.133 565657.131 111324.430 565648.599 111322.358 565643.611 111320.890 565641.183 111317.754 565638.252 111314.364 565637.221 111311.901 565635.669 111308.940 565633.001 111306.188 565630.161 111299.269 565623.280 111296.459 565620.855 111289.743 565612.042 111288.034 565609.262 111287.885 565607.183 111287.952 565605.350 111289.880 565600.003 111290.553 565594.525 111292.963 565579.194 111293.289 565571.647 111293.150 565566.063 111292.136 565560.798 111290.827 565557.319 111288.751 565552.505 111286.880 565546.881 111285.400 565541.963 111284.091 565538.800 111282.983 565537.459 111281.177 565535.597 111273.273 565532.316 111267.964 565530.955 111264.612 565529.127 111259.711 565526.718 111256.850 565525.182 111254.613 565522.599 111253.675 565521.515 111251.992 565519.377 111249.075 565516.780 111237.613 565510.395 111226.197 565505.486 111223.601 565504.460 111221.519 565504.260 111217.954 565503.120 111213.476 565500.897 111210.396 565498.730 111201.740 565491.688 111201.293 565491.055 111200.823 565490.391 111198.985 565487.372 111198.319 565485.286 111196.981 565478.222 111195.855 565473.893 111195.104 565471.931</gml:posList>
</gml:LineStringSegment>
<gml:Arc>
<gml:posList>111195.104 565471.931 111194.349 565471.308 111193.400 565471.072</gml:posList>
</gml:Arc>
<gml:LineStringSegment>
<gml:posList>111193.400 565471.072 111195.490 565469.211 111196.833 565467.733 111196.878 565469.465 111197.239 565472.033 111198.144 565475.632 111198.542 565478.868 111200.278 565485.914 111201.610 565488.402 111202.868 565490.267 111203.347 565490.977 111209.094 565495.731 111213.095 565498.272 111216.474 565500.707 111226.801 565504.000 111232.277 565506.338 111237.211 565508.276 111240.491 565509.987 111250.566 565515.501 111252.819 565517.439 111253.825 565518.168 111255.237 565520.026 111256.611 565521.825 111258.817 565523.818 111264.390 565526.818 111269.808 565529.391 111273.949 565531.055 111281.147 565533.996 111283.101 565534.964 111284.494 565536.260 111286.004 565537.963 111287.416 565540.827 111287.863 565542.180 111288.074 565542.820 111289.990 565550.134 111292.312 565557.042 111292.395 565557.317 111294.111 565562.983 111294.943 565571.090 111294.382 565578.559 111293.502 565585.284 111292.786 565589.347 111291.702 565596.168 111290.865 565600.233 111288.951 565605.550 111288.891 565607.164 111289.017 565608.944 111290.628 565611.464 111296.317 565617.733 111298.446 565620.559 111301.348 565623.074 111308.895 565629.963 111312.146 565633.020 111316.403 565635.419 111319.266 565637.354 111320.997 565639.480 111323.230 565642.130 111327.671 565650.163 111329.892 565654.602 111332.782 565661.764 111334.825 565666.424 111337.045 565670.463 111339.088 565672.194 111341.112 565677.660 111342.943 565685.111 111345.766 565691.196 111348.320 565695.027 111352.306 565700.561 111356.718 565706.714 111358.266 565709.462 111361.556 565717.318 111364.175 565724.735 111364.940 565728.076 111364.946 565730.565 111363.617 565733.045</gml:posList>
</gml:LineStringSegment>
</gml:segments>
</gml:Curve>
</gml:curveMember>
</gml:Ring>
</gml:exterior>
</gml:Polygon>

Which is a valid GML Polygon. But if I put it into GeomFromGml it returns an error stating it can't find the LinearRing element withing the http://www.opengis.net/gml namespace.
I checked (as far as .NET will allow me) what this function is doing and i noticed that the callstack is as follows :

This exception was originally thrown at this call stack:
System.Xml.XmlReader.ReadStartElement(string, string)
Microsoft.SqlServer.Types.GeographyMarkupLanguageReader.ReadStartOrEmptyElement(string)
Microsoft.SqlServer.Types.GeographyMarkupLanguageReader.ParseGmlLinearRingElement()
Microsoft.SqlServer.Types.GeographyMarkupLanguageReader.ParseGmlExteriorLinearRingElement()
Microsoft.SqlServer.Types.GeographyMarkupLanguageReader.ParseGmlPolygonShape()
Microsoft.SqlServer.Types.GeographyMarkupLanguageReader.ParseGmlGeometry()
Microsoft.SqlServer.Types.GeographyMarkupLanguageReader.Read(int)
Microsoft.SqlServer.Types.SqlGeometry.GeomFromGml(System.Data.SqlTypes.SqlXml, int)
DBDebugtest.Program.Main(string[]) in Program.cs

As you can see after it sees the polygonshape (first line of the GML) it reads a LinearRing, but it actually is a Ring element. These are not the same elements. But I can't figure out why this error is happening and most of all how I can get my polygones to parse correctly.

Can anybody shed some more light on the subject and how to either correct this or work around it?

With regards,
Michel van der Breggen

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,760 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,555 questions
0 comments No comments
{count} votes

Accepted answer
  1. michel van der breggen 96 Reputation points
    2020-09-21T21:19:01.92+00:00

    Ok all,
    I took a deep dive into the source of the GeographyMarkupLanguageReader. It seems that the programmer of this module simplified the GML model for himself a bit too much. Every parent type (Polygon, PolygonPatch, Point, LineString and so on) has only one path down. It seems that Polygon only allows LinearRing as its child element, PolygonPatch only allows Ring as its child element and so on.

    So I had to String.Replace my way through several scenarios (different exterior and interior elements, Polygons with Rings and PolygonPatches with LinearRings) to get SQLServer to accept my GML Polygones. For the people wanting to try it out themselves, this is what I replaced:

    string newXml = GMLXml.InnerXml;
    if ( newXml.IndexOf("gml:Ring>") != -1 )
    {
    newXml = newXml.Replace("<gml:Polygon", "<gml:PolygonPatch");
    newXml = newXml.Replace("</gml:Polygon>", "</gml:PolygonPatch>");
    newXml = newXml.Replace("gml:Curve>", "gml:CompositeCurve>");
    newXml = newXml.Replace("gml:segments>", "gml:curveMember>");
    newXml = newXml.Replace("gml:LineStringSegment>", "gml:LineString>");
    newXml = newXml.Replace("gml:Arc>", "gml:ArcString>");
    newXml = newXml.Replace("<gml:LinearRing>", "<gml:Ring><gml:curveMember><gml:LineString>");
    newXml = newXml.Replace("</gml:LinearRing>", "</gml:LineString></gml:curveMember></gml:Ring>");
    }


0 additional answers

Sort by: Most helpful