I'm writing a WinForms app in C#. I need to ensure no two Datarow
s in my Datatable
are more than 100 km apart. Each row has a UTM Zone, Easting, and Northing in separate DataColumn
s. All coordinates use the same datum, but some have different zones (otherwise, I would just use pythag math since UTMs are in metres). So far, I'm using the following code to do this, but it doesn't seem like my DbGeography.PointFromText
method is working quite right (see * in the code), as when the code reaches the line of code with the '**' at the start of it, I get an error saying "24201: Latitude values must be between -90 and 90 degrees". I've also tried:
dtTrap.Rows[i]["TrapGeog"] = DbGeography.PointFromText(pointWellKnownText: "POINT M(" + dtTrap.Rows[i][intEastingIndex].ToString() + " " + dtTrap.Rows[i][intNorthingIndex].ToString() + " " + dtTrap.Rows[i][Zone].ToString() + ")", coordinateSystemId: SRID);
Only to have it complain that "There's no column # 17" (17 is my UTM Zone).
I've found very little documentation for using this stuff... as far as I can tell, my SRIDs are correct (I pulled them from this site). I've done this kind of thing using Lat+Longs before, and it worked wonderfully. I just can't find proper syntax for the UTMs.
using System.Data.Entity.Spatial;
...
DataColumn dcGeog = new DataColumn("TrapGeog", typeof(DbGeography));
dtTrap.Columns.Add(dcGeog);
byte Zone;
Int16 SRID;
for (int i = 0; i < dtTrap.Rows.Count; ++i)
{
if (dtTrap.Rows[i][intZoneIndex] != null
&& dtTrap.Rows[i][intNorthingIndex] != null
&& dtTrap.Rows[i][intEastingIndex] != null
&& byte.TryParse(dtTrap.Rows[i][intZoneIndex].ToString(), out Zone) == true)
{
if (Zone == 15) { SRID = 26915; }
else if (Zone == 16) { SRID = 26916; }
else if (Zone == 17) { SRID = 26917; }
else { SRID = 26918; }
// shove it in:
try
{
*dtTrap.Rows[i]["TrapGeog"] = DbGeography.PointFromText(pointWellKnownText: "POINT(" + dtTrap.Rows[i][intEastingIndex].ToString() + " " + dtTrap.Rows[i][intNorthingIndex].ToString() + ")", coordinateSystemId: SRID);
}
catch (Exception ex)
{
if (ex.InnerException != null)
{
**MessageBox.Show(ex.InnerException.Message);
}
else
{
MessageBox.Show(ex.Message);
}
}
}
}
for (int i = 0; i < dtTrap.Rows.Count - 1; ++i)
{
for (int k = i + 1; k < dtTrap.Rows.Count; ++i)
{
DbGeography iTrap = (DbGeography)dtTrap.Rows[i]["TrapGeog"];
DbGeography kTrap = (DbGeography)dtTrap.Rows[k]["TrapGeog"];
if (iTrap.Distance(kTrap) > 100000)
{
sbErrorsAndWarningsLog.Append(@"Warning: Line number " + (i + 2).ToString() + " on the Trap spreadsheet has coordinates that are at least 100 km away from row " + (k + 2).ToString() + "'s point. Please check that these coordinates are correct.").AppendLine();
boolWarningsFound = true;
break;
}
}
}
double? d = Trap1.Distance(Trap2)
) when they have different SRIDs (due to a difference in UTM zones), Visual Studio throws the error "Data is Null. This method or property cannot be called on Null values." - despite both points being non-null. – Origami