Search code examples
c#sql-servergeojsongeographywkt

Getting wrong spatial result in SQL server + management studio (2014) when parse from MultiPolygon WKT


SQL query for parsing WKT to DbGeography:

select geography::STMPolyFromText('MULTIPOLYGON (((-2.5591667 49.2208332, -2.4799491 49.2644641, -2.3891134 49.2959748, -2.2950459 49.325767, -2.2176605 49.3624676, -2.1335686 49.4074579, -2.0975001 49.4605, -1.9925 49.3646667, -1.8916667 49.3166667, -1.8333334 49.2508333, -1.8333333 49.1833333, -1.8591667 49.0658333, -1.9428333 48.9646666, -1.9833333 48.9416666, -1.9833333 48.9365843, -1.9833333 48.8833333, -2.0833333 48.8721666, -2.2416668 48.8721666, -2.5253334 48.9278333, -2.5253333 49.0595, -2.5591667 49.2208332)))',4326)

Its display spatial result as below image enter image description here

When i convert this WKT to GeoJson Using GeoJSON4EntityFramework, and load this geojson in google map by below code:

map.data.addGeoJson(geoJsonObject);

its draws below map enter image description here

Please help me find-out which is correct from both above images.

If SQL partial result of SQL Management Studio is wrong, then how can i correct this?


Solution

  • I have findout the answer. We can know SqlGeography is larger than a hemisphere or not by "m_isLargerThanAHemisphere" Boolean value from non-public property value.

    below code for to get "m_isLargerThanAHemisphere" value:

                    SqlGeography sqlGeography = SqlGeography.Parse(geoWKT);
    
                    object geoData = PropertyHelper.GetPrivatePropertyValue<object>(sqlGeography, "GeoData");
                    bool m_isLargerThanAHemisphere = PropertyHelper.GetPrivateFieldValue<bool>(geoData, "m_isLargerThanAHemisphere");
                    if (m_isLargerThanAHemisphere)
                    {
                        sqlGeography = sqlGeography.ReorientObject();
                    }
                    bool isValid = sqlGeography.STIsValid().Value;
                    if (!isValid)
                    {
                        sqlGeography = sqlGeography.MakeValid();
                        isValid = sqlGeography.STIsValid().Value;
                    }
                    DbGeography dbGeography = DbGeography.FromText(sqlGeography.ToString(), 4326);
    

    PropertyHelper class reference