I want to construct a DbGeography point from latitude and longitude doubles.
I know I can convert my doubles to strings and use the DbGeography.FromText
method.
var latitude = 50.0d;
var longitude = 30.0d;
var pointString = string.Format(
"POINT({0} {1})",
longitude.ToString(),
latitude.ToString());
var point = DbGeography.FromText(pointString);
But it seems wasteful to convert my doubles to strings just so that DbGeography can parse them as doubles again.
I tried constructing a DbGeography directly like this:
var point = new DbGeography()
{
Latitude = 50,
Longitude = 30
};
but the Latitude and Longitude properties are read-only. (Which makes sense, because the DbGeography class handles much more than individual points)
The DbGeography
class also provides a FromBinary
method that takes a byte array. I'm not sure how to martial my latitude and longitude doubles into a correctly-formatted byte array.
Is there a simpler way to construct a DbGeography instance out of Latitude and Longitude doubles than the code at the top?
public static DbGeography CreatePoint(double, double)
which would wrap your code ? It won't avoid conversion, but at least makes your code clearer... – Raphaël AlthausDbGeography
create methods under one class. – jcarpenter2