【发布时间】:2018-07-27 20:43:59
【问题描述】:
我正在用 C# 编写一个 WinForms 应用程序。我需要确保我的Datatable 中没有两个Datarows 相距超过100 公里。每行在单独的DataColumns 中都有一个 UTM 区域、东向和北向。所有坐标都使用相同的数据,但有些坐标有不同的区域(否则,我只会使用 pythag 数学,因为 UTM 以米为单位)。到目前为止,我正在使用以下代码来执行此操作,但我的 DbGeography.PointFromText 方法似乎不太正常(请参阅代码中的 *),因为当代码到达带有 ' 的代码行时**' 在它的开头,我收到一条错误消息“24201:纬度值必须在 -90 到 90 度之间”。我也试过:
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);
只是让它抱怨“没有第 17 列”(17 是我的 UTM 区域)。
我发现使用这些东西的文档非常少……据我所知,我的 SRID 是正确的(我从 this site 中提取了它们)。我以前用 Lat+Longs 做过这种事情,效果很好。我只是找不到适合 UTM 的语法。
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;
}
}
}
【问题讨论】: