/// <summary> /// Converts a native Sql Server geometry (lat/lon) to GeoJSON geometry /// </summary> /// <param name="sqlGeometry">SQL Server geometry to convert</param> /// <param name="withBoundingBox">Value indicating whether the feature's BoundingBox should be set.</param> /// <returns>GeoJSON geometry</returns> public static T ToGeoJSONObject <T>(this SqlGeometry sqlGeometry, bool withBoundingBox = true) where T : GeoJSONObject { if (sqlGeometry == null || sqlGeometry.IsNull) { return(null); } // Make valid if necessary sqlGeometry = sqlGeometry.MakeValidIfInvalid(); if (sqlGeometry.STIsValid().IsFalse) { throw new Exception("Invalid geometry : " + sqlGeometry.IsValidDetailed()); } // Conversion using geometry sink T geoJSONobj = null; SqlGeometryGeoJsonSink sink = new SqlGeometryGeoJsonSink(); sqlGeometry.Populate(sink); geoJSONobj = sink.ConstructedGeometry as T; geoJSONobj.BoundingBoxes = withBoundingBox ? sink.BoundingBox : null; return(geoJSONobj); }
/// <summary> /// Converts a native Sql Server geometry (lat/lon) to GeoJSON geometry /// </summary> /// <param name="sqlGeometry">SQL Server geometry to convert</param> /// <param name="withBoundingBox">Value indicating whether the feature's BoundingBox should be set.</param> /// <returns>GeoJSON geometry</returns> public static IGeometryObject ToGeoJSONGeometry(this SqlGeometry sqlGeometry, bool withBoundingBox = true) { if (sqlGeometry == null || sqlGeometry.IsNull) { return(null); } // Make valid if necessary sqlGeometry = sqlGeometry.MakeValidIfInvalid(); if (sqlGeometry.STIsValid().IsFalse) { throw new Exception("Invalid geometry : " + sqlGeometry.IsValidDetailed()); } // Conversion using geometry sink SqlGeometryGeoJsonSink sink = new SqlGeometryGeoJsonSink(withBoundingBox); sqlGeometry.Populate(sink); return(sink.ConstructedGeometry); }