Skip to content
Advertisement

Error – NetTopologySuite( When writing a SQL Server geography value, the shell of a polygon must be oriented counter-clockwise)

All, I’m using NetTopologySuite/ Entity Asp Core for my project. I want to create a radius of 1000 meter for the ‘searchArea ‘ When I’m running the the app it gives me an error : System.ArgumentException: When writing a SQL Server geography value, the shell of a polygon must be oriented counter-clockwise.

     public IActionResult Structures()
        {
             var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);

             var searchAreCoordinate = new NetTopologySuite.Geometries.Coordinate(-1.3190967394026893, 51.748851810406954); // Not sure if long-lat or lat-long.
            var searchArea = geometryFactory.CreatePoint(searchAreCoordinate).Buffer(1000); // To me, this is 1000 meters because the SRID is WGS84.

           

var nearestStructures = _context.Structure
                    

                  .OrderBy(s=>s.Coordinates.Distance(searchArea))
                 // .Where (s=>s.Coordinates.Intersects(searchArea)) // This is why i want to buffer the searchArea to get the result from this intersetion
                  .Take(10).ToList();       
        
        return View(nearestStructures);


            // return View(_context.Structure.ToList());
        }

My View

@model IEnumerable<Structures>




    <table class="table"> 

<thead>

    <tr>
        <th>
                    @Html.DisplayNameFor(Model=>Model.Id)
        </th>
        <th>
                  @Html.DisplayNameFor(Model=>Model.gml_id)
        </th>
        <th>
                  @Html.DisplayNameFor(Model=>Model.structure)
        </th>
       
    </tr>
</thead>
<tbody>

    @foreach (var item in Model)
    {
         <tr>
        <td>
          
                    @Html.DisplayFor(Model=>item.Id)
        </td>
        <td>
                  @Html.DisplayFor(Model=>item.gml_id)
        </td>
        <td>
                  @Html.DisplayFor(Model=>item.structure)
        </td>
      
    </tr>
    }
</tbody>


    </table>

ps: I could achieve this query using SQL Spatial as follow: I know different name of varabiels but same concept p2:2 the data that I have in my SQL table is geography

declare @meters int =   0.5/ 0.0006213712 select @meters as meters
declare @vehicle geography= geography::Point(51.748851810406954,-1.3190967394026893,4326).STBuffer(@meters) select @vehicle as Vehicle_Geo
select  Id,gml_id,Coordinates,Long,Lat,structure from [dbo].[Structure] where @vehicle.STIntersects(Coordinates) =1

Advertisement

Answer

var searchArea = geometryFactory.CreatePoint(searchAreCoordinate).Buffer(1000); // To me, this is 1000 meters because the SRID is WGS84.

This is fundamentally wrong. NTS does not care for any spatial reference system, it treats every coordinate as if it were planar.

If you have to work with geographic coordinates and want to buffer/measure with meters, you need to follow the instructions given in Srid ignored during client operations.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement