Out of memory exception while the build of a GPS trace containing a lot of GPS data

73 Views Asked by At

I obtain GPS points of a vehicle in order to build its trace.

I develop in C# and use the SqlGeography type (LineString). I store it into a SQL Server database by using the geography type.

Each received GPS point is added with the AddLine method like this:

private SqlGeography AddPoint(SqlGeography existingLine, SqlGeography pointToAdd)
{
    SqlGeographyBuilder line = new SqlGeographyBuilder();
    line.SetSrid(4326);
    line.BeginGeography(OpenGisGeographyType.LineString);

    line.BeginFigure(existingLine.STStartPoint().Lat.Value, existingLine.STStartPoint().Long.Value);
    for (int i = 2; i <= existingLine.STNumPoints().Value; i++)
    {
        line.AddLine(existingLine.STPointN(i).Lat.Value, existingLine.STPointN(i).Long.Value);
    }

    // Add a new point.
    line.AddLine(pointToAdd.Lat.Value, pointToAdd.Long.Value);

    line.EndFigure();
    line.EndGeography();

    return line.ConstructedGeography;
}

I've been surprised when a out of memory exception occurred cause of a great collection of GPS points (about 825000 points). This occurs on the line code:

return line.ConstructedGeography;

I've changed the SqlGeography type to SqlGeometry type and the result is immediate and without exception.

Furthermore, my trace is invalid sometimes and I have to use the MakeValid method. This method takes a lot of time to be executed when the volume of GPS data increases for a trace.

Questions:

1) Is there another way to add a point to the existing trace ? Are we forced to loop on the points of the existing line in order to rebuild it with a SqlGeographyBuilder before to add the new point ?

2) Why the result by using the SqlGeometry type is immediate and without exception ? What is the cause ? How to work the ConstructedGeography method compared to the ConstructedGeometry method ? I would like to get a short explanation with eventual links if found ? I've seen on several topics that the SqlGeometry is less restrictive than the SqlGeography type but what are the associated constraints ?

3) In my case, on what should I pay care to build a valid linestring by avoiding to use the MakeValid method ? I think to something as compared to the polygons with their ring orientation.

4) Why the union of 3 valid linestring by using the method AddLine of a SqlGeographyBuilder gives me a linestring NOT valid ? If I use the method MakeValid, I obtain an object containing 130772 points while each of 3 linestrings contains 1000 points. Why don't I get a object with 3000 points ? Why do the size increase ?

0

There are 0 best solutions below