I'm using nettopologysuite in c# to build a nettopology geometry object, and then save it as a geometry object in SQL. Generally this works fine, except i recent came up with (from a shapefile) the WKT:

POINT Z(3.59140026404647 50.7568317130073 0)

which broke everything. Both if i do it in code:

var tmp = SqlGeometry.Parse(new SqlString(geometryObject.AsText()));

or if i do it in the SQL procedure itself:

declare @tmpGeom as Geometry = null;
    if (@GeometryObj is not null)
            set @tmpGeom=geometry::STGeomFromText(@GeometryObj ,4326)
            if (@tmpGeom.STIsValid() = 0)
                set @tmpGeom = @tmpGeom.MakeValid()

neither work. I get an error similar to:

24142: Expected "(" at position 6. The input has "Z". System.FormatException Source = Microsoft.SqlServer.Types at Microsoft.SqlServer.Types.WellKnownTextReader.RecognizeToken(Char token) at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses) at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type) at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid) at Microsoft.SqlServer.Types.SqlGeometry.GeometryFromText(OpenGisType type, SqlChars text, Int32 srid) at Microsoft.SqlServer.Types.SqlGeometry.Parse(SqlString s)

Any ideas how to convert the POINT WKT (which does read as 'IsValid' in nettopology) into something SQL can use?


So it looks like i was able to get around my issue by replacing any occurance i had of




where 'ToWKT' is just an extension method i made below. keeping my sql/not sql options open with the second parameter too.:

public static string ToWKT(this NetTopologySuite.Geometries.Geometry geom, bool forSQL = true)
            if (geom != null && geom.IsValid)
                WKTWriter w;
                if (forSQL)
                    w = WKTWriter.ForMicrosoftSqlServer();
                    w = new WKTWriter();
                return w.Write(geom);
            return string.Empty;
| improve this answer | |

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.