Error when using Intersects

62 views
Skip to first unread message

Arnaud Geoffroy

unread,
Apr 21, 2020, 6:02:09 AM4/21/20
to NetTopologySuite
Hi all,

I'm new to using this library so I apologize in advance if the solution seems to be trivial but after 2 days of google searches and tests, I now turn to the community for help.

I'm trying to get all object that are withing a given distance radius.

But when executing the query, I get the following error : 
"SqlException: A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": System.FormatException: One of the identified items was in an invalid format. System.FormatException: ."

Our application is running on Microsoft Azure and here is the version of our sql server : 

Microsoft SQL Azure (RTM) - 12.0.2000.8   Feb 14 2020 18:30:14   Copyright (C) 2019 Microsoft Corporation 

Here is the code of the method in the repository that try to get the object from the database :
 
        public async Task<List<Tool>> GetToolsAsync(GetToolParams criteria)
        {
            IQueryable<Tool> tools = this.dbContext.Tools
                .Include(t => t.Type)
                .Include(t => t.ToolStatus)
                .Include(t => t.AssignedUsers)
                .Include(t => t.Tag)
                .Include(t => t.Inspections)
                .Include(t => t.Groups)
                    .ThenInclude(g => g.Group)
                        /*.Include(t => t.Sites)
                            .ThenInclude(s => s.Site)*/;

            if (criteria.Latitude != null && criteria.Longitude != null && criteria.Distance != null)
            {
                IGeometryFactory geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
                IPoint point = geometryFactory.CreatePoint(new Coordinate(criteria.Longitude.Value, criteria.Latitude.Value));
                Polygon surrounding = (Polygon)geometryFactory.CreatePolygon(point.Buffer(criteria.Distance.Value).Coordinates);
                if (!surrounding.Shell.IsCCW)
                {
                    surrounding = (Polygon)geometryFactory.CreatePolygon(surrounding.Shell.Reverse().Coordinates);
                }

                tools = tools.Where(t => t.CurrentPosition != null && (t.CurrentPosition.Intersects(surrounding) ? true : false));
            }

            // Other where clauses goes here.  I removed them because they're irrelevant to this problem

            List<Tool> toolsList = await tools.ToListAsync();

            return toolsList;
        }

I'm aware that my way to get a polygon with CCW shell my be optimized but that's not the core problem for now.

The property CurrentPosition in the Tool class is declared as followed : 
        [Column(TypeName = "geography")]
        public Point CurrentPosition { get; set; }

Do you have any idea on what I do wrong here ?

Thanks in advance.

Arnaud Geoffroy

unread,
Apr 22, 2020, 5:30:41 AM4/22/20
to NetTopologySuite
Hi,

I manage to get around my problem by comparing distance instead of using intersects.  Since my buffer is made from a point, the result is a circle.  So I could write
tools = tools.Where(t=> t.CurrentPosition != null && t.CurrentPosition.Distance(point) <=criteria.Distance.Value);

With that line instead, I get around the exception and the result seems to be ok.

FObermaier

unread,
Apr 27, 2020, 3:18:03 AM4/27/20
to NetTopologySuite
Could you provide sample values for `longitude` and `latitude`?

Arnaud Geoffroy

unread,
Apr 27, 2020, 4:26:49 AM4/27/20
to NetTopologySuite
Sure... I tried with those : 

Latitude : 50.55837
Longitude : 4.69014
Distnace : 500 (meters hopefully)

FObermaier

unread,
Apr 27, 2020, 7:38:10 AM4/27/20
to NetTopologySuite
NTS expects all units to be in the same dimension. As you have latitude and longitude in degrees, you need to convert your 500m radius into some degree value and pass that to the buffer function.
Or you use some other method than buffer to create the circle around your circle center.
Reply all
Reply to author
Forward
0 new messages