public static string UnitsToString(this DistanceUnits distanceUnit, string format) { switch (format.ToLower()) { case "a": // plural...i.e. x miles case "abbrv": switch (distanceUnit) { case DistanceUnits.Miles: return("miles"); case DistanceUnits.Kilometers: return("km"); } break; case "a.s": // singular...i.e. min/mile case "abbrv.s": switch (distanceUnit) { case DistanceUnits.Miles: return("mile"); case DistanceUnits.Kilometers: return("km"); } break; } return(distanceUnit.ToString()); }
public static String GetPreferedShortUnitString(DistanceUnits unit) { var info = from U in unitsTable where U.Unit == unit select U; var theInfo = info.FirstOrDefault(U => U.PreferedShortFormat) ?? info.First(); if (theInfo != null) { return(theInfo.UnitText); } else { WDAppLog.LogNeverSupposedToBeHere(); return("(unnamed unit: " + unit.ToString() + ")"); } }
/// <summary> /// Returns the distance between two points on a curved plane with an assumed mean radius of 6371.1 KM (Earth) /// </summary> /// <param name="fLat1">Y1</param> /// <param name="fLong1">X1</param> /// <param name="fLat2">Y2</param> /// <param name="fLong2">X2</param> /// <param name="units">Miles or Kilometres</param> /// <returns></returns> public static double GreatCircleArcLength(double fLat1, double fLong1, double fLat2, double fLong2, DistanceUnits units) { const double dfPi = Math.PI; double dfRadLat1 = fLat1 * (dfPi / 180); double dfRadLat2 = fLat2 * (dfPi / 180); double dfRadDeltaLong = (fLong2 - fLong1) * (dfPi / 180); double dfCosZ = Math.Sin(dfRadLat1) * Math.Sin(dfRadLat2) + Math.Cos(dfRadLat1) * Math.Cos(dfRadLat2) * Math.Cos(dfRadDeltaLong); if (Math.Abs(dfCosZ - 1.0) < 0.0000000001) { // The points were very close together return(0); } else { double dfDiv = Math.Sqrt((dfCosZ * -1) * dfCosZ + 1); if (dfDiv == 0) { return(0); } else { double dfACosZ = Math.Atan((dfCosZ * -1) / dfDiv) + 1.5708; switch (units) { case DistanceUnits.Kilometers: return(dfACosZ * (float)6371.1); // Mean radius of Earth is 6371.1 KM case DistanceUnits.Miles: return(dfACosZ * (float)3959); // Mean radius of Earth is 3959 Miles case DistanceUnits.None: return(dfACosZ); // Users can apply their own Mean Radius to get different units default: throw new Exception("Unhandled distance unit type: " + units.ToString()); } } } }
/// <summary> /// Converts from 1 distance to another. By default, converts to Meter and then back to the other measurements /// </summary> /// <param name="value">The value of the from unit</param> /// <param name="from">From unit can be anything listed in DistanceUnits</param> /// <param name="to">To unit can be anything listed in DistanceUnits</param> /// <returns></returns> public override decimal Convert(decimal value, Enum from, Enum to) { DistanceUnits fromDu = (DistanceUnits)from; DistanceUnits toDu = (DistanceUnits)to; decimal conversion; if (!conversionMap.TryGetValue(fromDu, out conversion)) { throw new InvalidUnitTypeException(fromDu.ToString()); } // Convert from to Meter value /= conversion; // Convert meter to to. if (!conversionMap.TryGetValue(toDu, out conversion)) { throw new InvalidUnitTypeException(toDu.ToString()); } return(value * conversion); }
private void map_MouseMove(object sender, MouseEventArgs e) { Map.Cursor = Cursors.Hand; if (originPoint != null && _isMeasuring) { _graphicCount = GraphicsLayer.Graphics.Count; int g = _graphicCount - 1; MapPoint p = Map.ScreenToMap(e.GetPosition(Map)); MapPoint midpoint = new MapPoint((p.X + originPoint.X) / 2, (p.Y + originPoint.Y) / 2); ESRI.ArcGIS.Client.Geometry.PointCollection polypoints = new ESRI.ArcGIS.Client.Geometry.PointCollection(); ESRI.ArcGIS.Client.Geometry.Polygon poly = new ESRI.ArcGIS.Client.Geometry.Polygon(); if (Type == MeasureType.Area && _points.Count > 2) { Graphic graphic = GraphicsLayer.Graphics[0]; poly = graphic.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon; polypoints = poly.Rings[0]; int lastPt = polypoints.Count - 1; polypoints[lastPt] = p; } GraphicsLayer.Graphics[g - 2].Geometry = midpoint; (GraphicsLayer.Graphics[g - 1].Geometry as ESRI.ArcGIS.Client.Geometry.Polyline).Paths[0][1] = p; GraphicsLayer.Graphics[g].Geometry = midpoint; double angle = Math.Atan2((p.X - originPoint.X), (p.Y - originPoint.Y)) / Math.PI * 180 - 90; if (angle > 90 || angle < -90) { angle -= 180; } RotatingTextSymbol symb = GraphicsLayer.Graphics[g].Symbol as RotatingTextSymbol; symb.Angle = angle; double dist = Geodesic.GetSphericalDistance(originPoint, p); double distRound = RoundToSignificantDigit(dist); symb.Text = Convert.ToString(RoundToSignificantDigit(ConvertDistance(dist, DistanceUnits))); GraphicsLayer.Graphics[g].Symbol = symb; _segmentLength = distRound; _tempTotalLength = totalLength + distRound; RotatingTextSymbol totSym; if (Type == MeasureType.Distance) { totSym = GraphicsLayer.Graphics[0].Symbol as RotatingTextSymbol; totSym.Text = string.Format("Length Total:\n{0} {1}", RoundToSignificantDigit(ConvertDistance(_tempTotalLength, DistanceUnits)), DistanceUnits.ToString()); GraphicsLayer.Graphics[0].Symbol = totSym; } else { totSym = GraphicsLayer.Graphics[1].Symbol as RotatingTextSymbol; if (polypoints != null && polypoints.Count > 2) { double lastLen = Geodesic.GetSphericalDistance(polypoints[0], polypoints[polypoints.Count - 1]); poly = GraphicsLayer.Graphics[0].Geometry as ESRI.ArcGIS.Client.Geometry.Polygon; MapPoint anchor = poly.Extent.GetCenter(); ESRI.ArcGIS.Client.Geometry.PointCollection temppoints = new ESRI.ArcGIS.Client.Geometry.PointCollection(); foreach (MapPoint point in poly.Rings[0]) { temppoints.Add(point); } temppoints.Add(poly.Rings[0][0]); ESRI.ArcGIS.Client.Geometry.PointCollection pts = ToKmMapPoint(temppoints); double area = GetArea(pts); //double area = GetArea(ToKMMapPoint(polypoints)); totSym.Text = string.Format("Area:\n{0} {1}", RoundToSignificantDigit(area), AreaUnits.ToString()); GraphicsLayer.Graphics[1].Geometry = anchor; GraphicsLayer.Graphics[1].Symbol = totSym; } } } }
private void ShowResult(string locationName, Location location) { Double radius = 200; if (Request["radius"] != null && Request["radius"] != string.Empty) { if (double.Parse(Request["radius"], Utility.GetNumberFormatInfo()) <= SliderExtender1.Maximum) { radius = double.Parse(Request["radius"], Utility.GetNumberFormatInfo()); } } DistanceUnits distanceunit = DistanceUnits.Miles; if (UnitInKm == true) { distanceunit = DistanceUnits.Kilometers; } XmlDocument content = new XmlDocument(); XmlElement root = content.CreateElement("root"); //search node XmlElement infoNode = content.CreateElement("location"); infoNode.InnerText = location.ToString().Replace(" ", ""); XmlAttribute nameAttribute = content.CreateAttribute("name"); nameAttribute.Value = locationName; infoNode.Attributes.Append(nameAttribute); XmlAttribute latitudeAttribute = content.CreateAttribute("latitude"); latitudeAttribute.Value = location.Latitude.ToString(); infoNode.Attributes.Append(latitudeAttribute); XmlAttribute longitudeAttribute = content.CreateAttribute("longitude"); longitudeAttribute.Value = location.Longitude.ToString(); infoNode.Attributes.Append(longitudeAttribute); XmlAttribute unitAttribute = content.CreateAttribute("unit"); unitAttribute.Value = distanceunit.ToString().ToLower(); infoNode.Attributes.Append(unitAttribute); XmlAttribute radiusAttribute = content.CreateAttribute("radius"); radiusAttribute.Value = radius.ToString(Utility.GetNumberFormatInfo()); infoNode.Attributes.Append(radiusAttribute); root.AppendChild(infoNode); string selectMembLoc = @" select m.nodeId, m.email, m.loginName, m.password, lat.dataNvarchar as Latitude, lon.dataNvarchar as Longitude, karma.dataInt as Karma, memberdata.text as MemberData, avatar.datanvarchar as Avatar from cmsMember m INNER JOIN cmsPropertyData lat on m.nodeId = lat.contentnodeid INNER JOIN cmsPropertyData lon on m.nodeId = lon.contentnodeid Inner Join cmsPropertyData karma on m.nodeId = karma.contentNodeId Inner Join umbracoNode memberdata on m.nodeId = memberdata.id Inner Join cmsPropertyData avatar on m.nodeId = avatar.contentnodeid where lat.propertytypeid = 43 and lat.dataNvarchar is not null and lat.dataNvarchar != '' and lon.propertytypeid = 34 and lon.dataNvarchar is not null and lon.dataNvarchar != '' and karma.propertytypeid = 32 and avatar.propertytypeid = 39 and CASE WHEN IsNumeric(lat.dataNvarchar) = 1 THEN Cast(lat.dataNvarchar as decimal(10)) ELSE -90 END > @minlat and CASE WHEN IsNumeric(lat.dataNvarchar) = 1 THEN Cast(lat.dataNvarchar as decimal(10)) ELSE 90 END < @maxlat and CASE WHEN IsNumeric(lon.dataNvarchar) = 1 THEN Cast(lon.dataNvarchar as decimal(10)) ELSE -180 END > @minlon and CASE WHEN IsNumeric(lon.dataNvarchar) = 1 THEN Cast(lon.dataNvarchar as decimal(10)) ELSE 180 END < @maxlon;"; SqlConnection conn = new SqlConnection(umbraco.GlobalSettings.DbDSN); SqlCommand commMembLoc = new SqlCommand(selectMembLoc, conn); commMembLoc.Parameters.AddWithValue("@minlat", location.Latitude - Math.Ceiling(radius / 85)); commMembLoc.Parameters.AddWithValue("@maxlat", location.Latitude + Math.Ceiling(radius / 85)); commMembLoc.Parameters.AddWithValue("@minlon", location.Longitude - Math.Ceiling(radius / 85)); commMembLoc.Parameters.AddWithValue("@maxlon", location.Longitude + Math.Ceiling(radius / 85)); conn.Open(); bool found = false; string memberIds = string.Empty; try { SqlDataReader reader = commMembLoc.ExecuteReader(); while (reader.Read()) { int id = reader.GetInt32(0); int karma = reader.IsDBNull(6) ? 0 : reader.GetInt32(6); memberIds += id.ToString() + ";"; Location memberLocation = new Location( double.Parse(reader.GetString(4), Utility.GetNumberFormatInfo()), double.Parse(reader.GetString(5), Utility.GetNumberFormatInfo())); double distance = location.DistanceBetween(memberLocation, distanceunit); //Response.Output.WriteLine("Member " + id.ToString() + "distance " + distance.ToString(Utility.GetNumberFormatInfo())); if (distance <= radius) { found = true; XmlElement memberNode = content.CreateElement("member"); XmlAttribute idAttribute = content.CreateAttribute("id"); idAttribute.Value = id.ToString(); memberNode.Attributes.Append(idAttribute); //name XmlAttribute membernameAttribute = content.CreateAttribute("name"); membernameAttribute.Value = reader.GetString(7); memberNode.Attributes.Append(membernameAttribute); //karma XmlAttribute karmaAttribute = content.CreateAttribute("karma"); karmaAttribute.Value = karma.ToString(); memberNode.Attributes.Append(karmaAttribute); XmlElement distanceNode = content.CreateElement("data"); distanceNode.InnerText = distance.ToString(Utility.GetNumberFormatInfo()); XmlAttribute aliasAttribute = content.CreateAttribute("alias"); aliasAttribute.Value = "distance"; distanceNode.Attributes.Append(aliasAttribute); memberNode.AppendChild(distanceNode); XmlElement avatarNode = content.CreateElement("data"); avatarNode.InnerText = reader.IsDBNull(8) ? string.Empty : reader.GetString(8); XmlAttribute avatarAliasAttribute = content.CreateAttribute("alias"); avatarAliasAttribute.Value = "avatar"; avatarNode.Attributes.Append(avatarAliasAttribute); memberNode.AppendChild(avatarNode); XmlElement locationNode = content.CreateElement("data"); locationNode.InnerText = memberLocation.ToString(); XmlAttribute locationAliasAttribute = content.CreateAttribute("alias"); locationAliasAttribute.Value = "location"; locationNode.Attributes.Append(locationAliasAttribute); memberNode.AppendChild(locationNode); root.AppendChild(memberNode); } } } finally { conn.Close(); } if (!found) { pnlCreateTopic.Visible = false; } ViewState["MemberLocatorMemberIds"] = memberIds.Length > 0 ? memberIds.Substring(0, memberIds.Length - 1) : ""; //litResults.Text = root.InnerXml.ToString() + ResultsXslt; content.AppendChild(root); litResults.Text = umbraco.macro.GetXsltTransformResult(content, umbraco.macro.getXslt("MemberLocator.xslt")); ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "MemberLocator", umbraco.macro.GetXsltTransformResult(content, umbraco.macro.getXslt("MemberLocatorScript.xslt")), true); if (Member.GetCurrentMember().Groups.ContainsKey(4686)) { ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "MemberLocatorTiny", "topictiny();", true); } }
/// <summary> /// Returns the distance between two points on a curved plane with an assumed mean radius of 6371.1 KM (Earth) /// </summary> /// <param name="fLat1">Y1</param> /// <param name="fLong1">X1</param> /// <param name="fLat2">Y2</param> /// <param name="fLong2">X2</param> /// <param name="units">Miles or Kilometres</param> /// <returns></returns> public static double GreatCircleArcLength(double fLat1, double fLong1, double fLat2, double fLong2, DistanceUnits units) { const double dfPi = Math.PI; double dfRadLat1 = fLat1 * (dfPi / 180); double dfRadLat2 = fLat2 * (dfPi / 180); double dfRadDeltaLong = (fLong2 - fLong1) * (dfPi / 180); double dfCosZ = Math.Sin(dfRadLat1) * Math.Sin(dfRadLat2) + Math.Cos(dfRadLat1) * Math.Cos(dfRadLat2) * Math.Cos(dfRadDeltaLong); if (Math.Abs(dfCosZ - 1.0) < 0.0000000001) { // The points were very close together return 0; } else { double dfDiv = Math.Sqrt((dfCosZ * -1) * dfCosZ + 1); if (dfDiv == 0) { return 0; } else { double dfACosZ = Math.Atan((dfCosZ * -1) / dfDiv) + 1.5708; switch (units) { case DistanceUnits.Kilometers: return dfACosZ * (float)6371.1; // Mean radius of Earth is 6371.1 KM case DistanceUnits.Miles: return dfACosZ * (float)3959; // Mean radius of Earth is 3959 Miles case DistanceUnits.None: return dfACosZ; // Users can apply their own Mean Radius to get different units default: throw new Exception("Unhandled distance unit type: " + units.ToString()); } } } }