So I am have an issue with adding a new object to a List using the .Add() call.
private static List<PointObject> _rawCoordinates = new List<PointObject>();
private static void KMLParser(string fileLocation)
{
string coordinateString = "";
XDocument document = XDocument.Load(fileLocation);
XNamespace ns = "http://www.opengis.net/kml/2.2";
var placemarks = document.Descendants(ns + "LineString")
.Select(p => new
{
Coord = p.Element(ns + "coordinates")
})
.ToList();
foreach(var obj in placemarks)
{
coordinateString = Regex.Replace(obj.Coord.Value, @"\t|\n|\r| ", "");
coordinateString = Regex.Replace(coordinateString, ",0", ",0\r");
foreach (string substr in coordinateString.TrimEnd('\r').Split('\r'))
{
_rawCoordinates.Add(LineParser(substr));
}
}
}
private static PointObject LineParser(string Coordinates)
{
PointObject pt = new PointObject();
int remainder;
int index = 0;
foreach (string substr in Coordinates.TrimEnd('\n').Split(',', '\n'))
{
remainder = index % 3;
switch (remainder)
{
case 0:
{
pt.SetLongitude(Math.Round(Convert.ToDouble(substr), _round));
Debug.WriteLine("Longitude: {0}", pt.GetLongitude());
index++;
break;
}
case 1:
{
pt.SetLatitude(Math.Round(Convert.ToDouble(substr), _round));
Debug.WriteLine("Latitude: {0}", pt.GetLatitude());
index++;
break;
}
default:
{
pt.SetBearing(0);
pt.SetDistance(0);
pt.SetIsStop(false);
pt.SetLayover(0);
break;
}
}
}
return pt;
}
So as you can see, I am add the returned PointObject from the LineParser Method,to the _rawCoordinates List, which is defined as a List. I am initializing a new PointObject each time I call the LineParser method, and everything I found, that should have taken care of the issue. However, each time I get a returned value from that method, it overwrites all elements in my _rawCoordinates List. I am not quite sure what I am doing wrong here. Any help would be greatly appreciated.
Edit: Adding my definition of PointObject
class PointObject
{
public static double _bearing = 0;
public static double _distance = 0;
public static double _longitude = 0;
public static double _latitude = 0;
public static bool _isStop = false;
public static int _layoverLength = 0;
public PointObject()
{
_longitude = 0;
_latitude = 0;
_bearing = 0;
_distance = 0;
_isStop = false;
_layoverLength = 0;
}
public PointObject(double lon, double lat, double dist, double bear, bool stop, int layover)
{
_longitude = lon;
_latitude = lat;
_distance = dist;
_bearing = bear;
_isStop = stop;
_layoverLength = layover;
}
}
So I am assuming you are going to want me to change those from public static to just public, right? If so, I can give it a try.
This is in the comments above from D Standly and Johny Tee, but you shouldn't have properties that you want to have multiple copies of declared as static in your PointObject class. Static means there is one copy of the field shared by all instances of the class (loose definition, see msdn for more details. (https://msdn.microsoft.com/en-us/library/98f28cdx.aspx)
So, change your PointObject definition to be something like:
class PointObject
{
public double _bearing = 0;
public double _distance = 0;
public double _longitude = 0;
public double _latitude = 0;
public bool _isStop = false;
public int _layoverLength = 0;
public PointObject()
{
_longitude = 0;
_latitude = 0;
_bearing = 0;
_distance = 0;
_isStop = false;
_layoverLength = 0;
}
public PointObject(double lon, double lat, double dist, double bear, bool stop, int layover)
{
_longitude = lon;
_latitude = lat;
_distance = dist;
_bearing = bear;
_isStop = stop;
_layoverLength = layover;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments