public static IEnumerable ImportKml(
        SqlString kml,
        SqlBoolean makeValid)
    {
        if (kml == null || string.IsNullOrEmpty(kml.Value))
        {
            return(new Collection <GeographyContext>());
        }

        Collection <GeographyContext> geographies = new Collection <GeographyContext>();

        Microsoft.SqlServer.SpatialToolbox.KMLProcessor.KMLProcessor parser =
            new Microsoft.SqlServer.SpatialToolbox.KMLProcessor.KMLProcessor(kml.Value.Trim());

        foreach (Geography p in parser.Geographies)
        {
            GeographyContext ge = new GeographyContext();
            ge.Id      = p.Id;
            ge.Context = p.Context;
            ge.Shape   = p.ToSqlGeography(makeValid.Value);

            geographies.Add(ge);
        }

        return(geographies);
    }
    public static SqlGeography KmlToGeography(
        SqlString kml,
        SqlBoolean makeValid)
    {
        if (kml == null || string.IsNullOrEmpty(kml.Value))
        {
            return(SqlGeography.Null);
        }

        SqlGeographyBuilder constructed = new SqlGeographyBuilder();

        Microsoft.SqlServer.SpatialToolbox.KMLProcessor.KMLProcessor parser = new Microsoft.SqlServer.SpatialToolbox.KMLProcessor.KMLProcessor(kml.Value.Trim());
        parser.Populate(constructed, makeValid.Value);
        return(constructed.ConstructedGeography);
    }