次の方法で共有


Spatial Types in the Entity Framework

One of the highly-anticipated features coming in the next
version of Entity Framework is Spatial support. The
team has been hard at work designing a compelling story for Spatial, and we
would love to get your feedback on it. In this post we will cover:

  • The basics of SQL Server’s  spatial support
  • Design goals
  • Walkthrough of design: CLR types, Metadata, Usage,
    WCF Serialization, Limitations
  • Questions (we want to hear from you!)

This entry will not cover the tools experience as we want to
focus on what is happening under the hood. 
We will blog about the tools design at a later time. For now, be sure
that we plan on shipping Spatial with Tools support.

 

The Basics
of Spatial

For those of you that are new to Spatial, let’s briefly describe
what it’s all about: There are two basic spatial types called Geometry and
Geography which allow you to perform geospatial calculations. An example of a
geospatial calculation is figuring out the distance between Seattle and New
York, or calculating the size (i.e. area) of a state, where the state has been
described as a polygon.

Geometry and Geography are a bit different. Geometry deals with planar data.
Geometry is well documented under the Open Geospatial Consortium
(OGC) Specification
. Geography
deals with ellipsoidal data, taking in mind the curvature of the earth when
performing any calculations. SQL introduced spatial support for these two types
in SQL Server 2008. The SQL implementation supports all of the standard
functions outlined in the OGC spec.

Programming Experience
in SQL

The query below returns all the stores within half a mile of the address of
a person whose ID is equal to 2. In the where clause we multiply the result of STDistance by 0.00062 to convert meters to miles.

DECLARE @dist sys.geography

SET @dist = (SELECT p.Address

       FROM dbo.People as p

       WHERE p.PersonID = 2)

SELECT [Store].[Name],

       [Store].[Location].Lat,

       [Store].[Location].Long

FROM   [dbo].[Stores] AS [Store]

WHERE (([Store].[Location].STDistance(@dist)) * cast(0.00062 as float(53))) <= .5

In the sample below, we change the
person’s address to a different coordinate. Note that STGeomFromText
takes two parameters: the point, and a Spatial Reference Identifier (SRID). The value of 4326 maps to the WGS 84 which is the standard
coordinate system for the Earth.

update [dbo].[People]

set [Address] = geography::STGeomFromText('POINT(-122.206834 57.611421)',
4326)

where [PersonID] = 2

Note that when listing a point, the longitude is listed
before the latitude.

 

Design

Goals

The goals for spatial are the following:

  • To provide a first class support for spatial in
    EDM
  • Rich Programming experience in LINQ and Entity
    SQL against Spatial types
  • Code-first, Model-first, and Database-first
    support
  • Tooling (not covered in this blog)

We have introduced two new primitive EDM Types called Geometry and Geography. This allows us to have spatial-typed properties in our
Entities and Complex Types. As with every other primitive type in EDM, Geometry
and Geography will be associated with CLR types. In this case, we have created
two new types named DBGeometry
and DBGeographywhich allows us to provide a
first-class programming experience against these types in LINQ and Entity SQL.

One can
describe these types in the CSDL in
a straightforward fashion:

<EntityType Name="Store">

  <Key>

      <PropertyRef Name="StoreId" />

  </Key>

  <Property Name="StoreId" Type="Int32" Nullable="false" />

  <Property Name="Name" Type="String" Nullable="false" />

  <Property Name="Location" Type="Geometry" Nullable="false" />

</EntityType>

 

Representingin
SSDL is very simple as well:

<EntityType Name="Stores">

  <Key>

      <PropertyRef Name="StoreId" />

  </Key>

  <Property Name="StoreId" Type="int"
Nullable="false" />

  <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />

  <Property Name="Location" Type="geometry" Nullable="false" />

</EntityType>

Be aware that spatial types cannot
be used as entity keys, cannot be used in relationships, and cannot be used as
discriminators.

Usage
Here are some scenarios and corresponding queries showing how simple
it is to write spatial queries in LINQ:

 

Query Scenario

Example

Simple Select of Entities with Spatial columns

// Store is an entity type with Location as a Geography type

var stores = from s in db.Stores

             select s;

Simple Select of Spatial values

var storeLocations = from s in db.Stores

                     select s.Location;

Query with filter, using static constructors

var store = from s in db.Stores

            where s.Location ==

                DbGeography.Parse(”POINT(-122.206834 47.611421)”)

            select s;

Query with filter using local variable

var store = from s in db.Stores

            where s.Location == loc

            select s;

Query involving Spatial methods

var distances = from s in db.Stores

                select s.Location.Distance(anotherLocation);

Find all the stores within a mile of a given address

var person = db.People.Single(p => p.PersonID == 2);

var stores = from s in db.Stores

             where s.Location.Distance(person.Address) *

                                              .00062 <= 1

             select new Location

                        {

                            Name = s.Name,

                            Latitude = s.Location.Latitude,

                            Longitude = s.Location.Longitude

                        };

 

Have in mind that spatial types are immutable, so they can’t be modified after
creation. Here is how to create a new location of type DbGeography:

s.Location = DbGeography.Parse("POINT(-122.206834
47.611421)");

db.SaveChanges();

Spatial
Functions

Our Spatial implementation relies on the underlying database
implementation of any of the spatial functions such as Distance, Intersects,
and others. To make this work, we have created the most common functions as
canonical functions on EDM. As a result, Entity Framework will defer the
execution of the function to the server.

Client-side Behavior
DbGeometry and DbGeography
internally use one of two implementations of DbSpatialServices
for client side behavior which we will make available:

One implementation relies on Microsoft.SQLServer.Types.SQLGeography
and Microsoft.SQLServer.Types.SQLGeometry being
available to the client. If these two namespaces are available, then we
delegate all spatial operations down to the SQL assemblies. Note that this
implementation introduces a dependency.

Another implementation provides limited services such as
serialization and deserialization, but does not allow performing non-trivial
spatial operations. We create these whenever you explicitly create a spatial
value or deserialize one from a web service.

DataContract Serialization

Our implementation provides a simple wrapper that is serializable, which allows spatial types to be used in
multi-tier applications. To provide maximum interoperability, we have created a
class called DbWellKnownSpatialValue which contains
the SRID, WellKnownText (WKT), and WellKnownBinary (WKB) values. We will serialize SRID, WKT
and WKB.

 

Questions

We want to hear from you. As we work through this design, it
is vital to hear what you think about our decisions, and that you chime in with
your own ideas. Here are a few questions, please take some time to answer them
in the comments:

  1. In order to have client-side spatial functionality,
    EF relies on a spatial implementation supplied by the provider. As a default,
    EF uses the SQL Spatial implementation. Do you foresee this being a problematic
    for Hosted applications which may or may not have access to a spatial
    implementation?
  2. Do you feel as though you will need richer
    capabilities on the client side?
  3. In addition to Geometry and Geography types, do
    you need to have constrained types like Point, Polygon?
  4. Do you foresee using heterogeneous columns of
    spatial values in your application?

 

Spatial data is ubiquitous now thanks to the widespread use
of GPS-enabled mobile devices. We are very excited about bringing spatial type
support to the Entity Framework. We encourage you to leave your thoughts on
this design below.

 

Cheers,

Pedro Ardila

Program Manager – Entity Framework Team

Comments

  • Anonymous
    May 04, 2011
    EF Team, Here are my thoughts to your questions... Question 1: I feel as long as your implementation does not dictate the design of other DB vendor's implementation, its fine.   I don't see an issue there.  I also feel as long as this closely resembles the IEEE standards it shouldn't be a problem.  Which it seems to be the case. Question 2: My one concern here is that if we need to do a quick calculation, we have to do a round trip to the server for this; even if the data is already on the client.  I know that we are doing this to get data from the database in the first place, but sometimes a simple calculation is needed and not another data pull, which will require a database connection, etc. etc.  So in other words, would it be possible to have some of the most common functions for geospacial be available in the .net world for less db round trips?  Or functions that would make sense outside the database world.  Such as distance functions, etc., etc...  I guess what I'm trying to say is that it would make a lot of sense to have a .Net namespace that deals with geospacial data and functions thats not necesarily specific to SQL Server's geospacial data types. Question 3: In this case my gut is telling me that a Point class would be useful with mapping for Bing/Google maps, etc.  Not sure if this is actually the case.  I can't speak for polygon.

  • Anonymous
    May 04, 2011
    Ummm...what about support for enums first. For that matter, why not an extensible type system so I can build my own support in?

  • Anonymous
    May 04, 2011
    I have to say that I think you using the type=Geometry is a bad idea.   It is not a .Net Geometry object, it is a SQL Server Geometry object.  It should say so in the type.  SqlServer.Geometry.  What if a third party vendor implements their own Geometry type?  It is not part of ado.net, so how are you going to map it?

  • Anonymous
    May 04, 2011
    What about providing spatial functionality to the .NET Framework as first class citizen? I think you should implement OGC specification independently from EF and then provide mapping functionality in EF.

  • Anonymous
    May 04, 2011
    Hello, great to hear that you are bringing support for spatial. My answers:

  1. Don't foresee problems (but I use only SQL Spatial so...)
  2. A wrapper for the SqlTypes would be very useful. As Ivan said, it is useful to be able to do calculations on the client, and that can be done with the SqlTypes, but the current API is dreadful (I suppose it was dictated by SQL requirements). A more .NETish wrapper would enable various use cases that are too complex now.
  3. If I get 2, I probably could live without Point or Polygon.
  4. I don't get the question...you mean Geography or Geometry that has LINESTRING and POINT in them...that probably could be useful (and it's supported in SQL Server). Regards, Pablo
  • Anonymous
    May 04, 2011
    I don't use spatial types much, so I can't provide any worthwhile feedback on your questions.  However, I do have a couple of potential points of improvement for the API itself. It would be nice to not have to do the metric to standard units conversion manually (there's a lot of room for error there).  I would suggest either adding a second parameter to the Distance() method like so: var stores = from s in db.Stores             where s.Location.Distance(person.Address, DistanceUnit.Miles) <= 1             select s; Or (my preferred option) using an extension method to provide a more fluent experience like so: var stores = from s in db.Stores             where s.Location.Distance(person.Address).InMiles() <= 1             select s; I would also recommend renaming the Distance() method to DistanceTo().

  • Anonymous
    May 05, 2011
    YES! Support for ENUMS! oh wait... I mean SPATIALS!!! WOOT! I think?

  • Anonymous
    May 05, 2011
    The main point is that your implementation should allow third party EF-providers for other databases to use arbitrary end .NET types for working with spatial types. Otherwise, classes like DbGeometry and DbGeography should not reside in SQL Server-specific assemblies and namespaces - they should be available in base .NET/ADO.NET or EF assemblies.

  • Anonymous
    May 06, 2011
    Will DbGeography support OGC methods like STUnion?  I assume yes but thought I would ask the question.  We will want to manipulate shapes in our application before saving them to the database.  Right now, we are using Microsoft.SqlServer.Types to accomplish that.

  • Anonymous
    May 08, 2011
    When reading this, I was also thinking the same as Rob: "why not an extensible type system so I can build my own support in"... For example we use our own 'enum' types, looking a lot like this lostechies.com/.../enumeration-classes. So when a row (say Employee) would have an 'EmployeeType' column I would like to be able to map that to the correct EmployeeType class instance but also be able to query on it. The first is easy to do currenty with a partial class property exposing the EmployeeType column as a real EmployeeType, however that doesn't work in querying (from p in Persons where p.EmployeeType == EmployeeType.Manager...) So not to bring the focus away from spatial types (we definitly need support for that), but I hope you will be able to use spatial types support to enhance the typing system so it becomes extensible with our own types...

  • Anonymous
    May 08, 2011
    The comment has been removed

  • Anonymous
    May 09, 2011
    I am building applications that deal with vast amounts of spatial data (as in dense mapping of entire earth terrain and man made structures, etc.).   We have the following requirements:

  •  Ability to store all sorts of spatial data, such as points, lines, polygons, homogenous collections of each type and heterogenous collections as well.

  •  Ability to store spatial data in various database platforms via EF.  Currently targeting MSSQL 2008 R2 and Oracle 10g as our minimums.

  •  Must be able to edit spatial data.

  •  Need server side spatial filtering and indexing.

  •  Performance is critical.

  •  We need all of this yesterday.  :~) thanks

  • Anonymous
    May 09, 2011
    the comment does not work or you are hidding all comments!

  • Anonymous
    May 10, 2011
    The comment has been removed

  • Anonymous
    May 13, 2011
    The comment has been removed

  • Anonymous
    May 25, 2011
    What about the hierarchyid datatype? Looks like we need a general solution for .NET user defined types as the hierarchyid and spatial datatypes. And if you have something general you can even support .NET user defined types which I have created.

  • Anonymous
    May 31, 2011
    I second Jaap. What about hierarchyid data type? Is support for that on the roadmap?

  • Anonymous
    June 14, 2011
    When will this spatial support be available?

  • Anonymous
    June 15, 2011
    Like Larry asked - when will a beta or CTP be available?  Normally I never use pre-RTM libraries/updates for a production project, but in this case, lack of support for SqlGeography in EF4 is KILLING ME.   Any info on when something - anything - available for download would be great, so I can get rid of the assorted computed-columns, blobs with triggers, and so on that are standing in as work-arounds.

  • Anonymous
    June 30, 2011
    Great feature!  I can't wait to see that working with Azure!

  • Anonymous
    August 18, 2011
    I have been using Oracle Spatial/Locator for 5 years now. An excellent product. With Denali now providing curve support and EF providing CodeFirst support, Microsoft has come a long way. In my case the only element that is lacking is WCF RIA Services support. This could very well tip-the-scales fo me. It has been a battle shoe-horning NHibernate into my the GIS world.  

  1. No. Gee, I guess as long as I can get to some engine I will be fine.
  2. No. I only forsee the client as the means to add/update the specific geometry columns values. The occasional this/or/that trip to the server will not be significant for me.
  3. No. Why?
  4. Yes. Absolutely. Why not? Rumor has it even ESRI has/is fixing this shortsightedness. r,
  • Anonymous
    September 12, 2011
    Hey Using the latest release of EF with the Spatial support. I need to store Regions on a map in SQL Server and then query which Suppliers are in a given region.
  1. How would i store this shape in SQL using EF?
  2. How would I query all suppliers in a Given Region using EF and Linq? Lets assume a simple db structure with no relationships Region: regionID (bigint) Name (nvarchar255) Shape (not sure if I can use geography to store a multipoint shape like a geo-fence) Supplier: supplierID (bigint) Name (nvarchar255) GeoLocation (geography)
  • Anonymous
    May 10, 2012
    Any ETA On the release date?

  • Anonymous
    March 13, 2014
    Why this error coming {"Error getting value from 'WellKnownValue' on 'System.Data.Entity.Spatial.DbGeography"}