ThinkGeo.com    |     Documentation    |     Premium Support

The specified input does not represent a valid geography

I have trouble with some shapes and inserting into a SQL server table with a geography field defined.


It tells me "The specified input does not represent a valid geography instance".  I know I can get around this by also having a geometry field defined and use that and SQL server to clean up the object before updating the geography field .


Is there any way to clean up the ring orientation, intersecting lines of a polygon, ect.. without pushing the feature to SQL Server and performing the cleaning there?  I tried to see if there was a MakeValid method on the shape or the feature and there doesn't seem to be.  I would rather take care of cleaning the shape client side than server side if I can.


 


Example of code where newFeature has invalid geometry.



sql2008Layer.EditTools.BeginTransaction();


sql2008Layer.FeatureSource.AddFeature(newFeature);


 



TransactionResult result = sql2008Layer.EditTools.CommitTransaction();



In case anyone is interested, this is how I solved my issue.  I have it in my c# code now but I will be moving it to a stored procedure where I can pass in WKT and get back WKT that is clean and ready to be used in a geography field.  I also included a sample of how I check to see if my shape is valid for a geography or not. (SQL 2008) 
  
 Sample of clean: 
  
  workingVersion = SqlGeometry.STGeomFromWKB(new SqlBytes(inputFeature.GetWellKnownBinary()), 4326); 
   finalVersion = SqlGeography.STGeomFromWKB(workingVersion.MakeValid().STUnion(workingVersion.MakeValid().STStartPoint()).Reduce(0.00001).STAsBinary(), 4326 ); 
  outputFeature = new Feature(finalVersion.STAsText().ToSqlString().ToString(), inputFeature.Id, inputFeature.ColumnValues); 
  
 Sample of check: 
 bool isShapeValid = false; 
             try 
             { 
                 SqlGeography testCopy = new SqlGeography(); 
                 testCopy = SqlGeography.STGeomFromText(new SqlChars(WKT.ToCharArray()), 4326); 
                 isShapeValid = true; 
                  
             } 
             catch (Exception e) 
             { 
  
                 isShapeValid = false; 
             }

Peter,


Thanks for your posts and sharing. I even did not work it out via many tests; you are more familiar than me in SQL:).
 
Thanks.
 
Yale