12

I need to geocode a large number of addresses, using the Bing Map service, EF 5 and SQL Server 2008. I'm using the geography data type in SQL, which translates to a DbGeography type by the EF.

When I create a DbGeography object, like this

string point = string.Format("POINT({0} {1})",address.Longitude,address.Latitude);
address.Location = System.Data.Spatial.DbGeography.PointFromText(point, 4326);

The second parameter calls for a "coordinateSystemId". What exactly is this? A lot of the examples I see use 4326. I am new to spatial data, but I'm guessing there is a set of well defined coordinate systems? I can't seem to find a definition.

denvercoder9
  • 756
  • 6
  • 15

2 Answers2

14

There's a whole StackExchange for GIS, and if you, like me, bumble in there as a coder not knowing GIS, you are in for a ride:

https://gis.stackexchange.com/questions/48949/epsg-3857-or-4326-for-googlemaps-openstreetmap-and-leaflet

It turns out there's a standard in mapping ("GIS") called EPSG, and people seem to use "EPSG," "EPSG SRID," and "SRID" interchangeably when referring to it. In most GPS coords usage, if you're talking about the Earth (including LatLngs you get from Google Maps), you're using EPSG SRID 4326, and if you're talking about a flat 2D map (like a Google Custom Map), you're talking about EPSG SRID 3857.

There's also some malarkey about WGS you can ignore.

.Net is only looking for the actual numeric ID in the EPSG SRID. It would be super helpful if these were just constants on DbGeography like:

public class DbGeography
{    
    public const int SridGps = 4326;
    public const int Srid2dPlane = 3857;

But no such luck.

Anyway consider making them constants in your application rather than just throwing around an undocumented magic number. Our little helper class:

public class GeoHelper
{
    public const int SridGoogleMaps = 4326;
    public const int SridCustomMap = 3857;



    public static DbGeography FromLatLng(double lat, double lng)
    {
        // http://codepaste.net/73hssg
        return DbGeography.PointFromText(
            "POINT("
            + lng.ToString() + " "
            + lat.ToString() + ")",
            SridGoogleMaps);
    }
}
Community
  • 1
  • 1
Chris Moschini
  • 33,398
  • 18
  • 147
  • 176
  • 4
    There is a constant for SRID 4326: [DbGeography.DefaultCoordinateSystemId](https://docs.microsoft.com/en-us/dotnet/api/system.data.spatial.dbgeography.defaultcoordinatesystemid), documented as *The default coordinate system id (SRID) for geography values (WGS 84).* – 0xced Mar 20 '18 at 07:23
  • 1
    Nice, that was added in .Net 4.5 - definitely helpful. – Chris Moschini Mar 28 '18 at 13:52
  • `System.Data.Spatial.DbGeography` is not available in .NET Core. – Ian Kemp Sep 16 '19 at 13:43
4

It is a well defined spatial reference identifier - a number that uniquely identifies a certain configuration of various spatial projections, datum, units, facing, etc.

Jake H
  • 1,650
  • 1
  • 12
  • 13