A View Inside My Head

Jason's Random Thoughts of Interest

NAVIGATION - SEARCH

Migrating to BlogEngine.NET

I've been wanting to migrate this blog to a different blogging engine for a while now.  So, the other day, while recording an episode of The Tablet Show, Richard Campbell asked me "Did you know that your blog is down?"  

Well, it seems that a recent security patch broke something about SubText as I had it set up on my hosting provider. This became my opportunity to finally spend some time on this tired site!

A View Inside My Head | Entity Framework Spatial: A Real World Example

A View Inside My Head

Jason's Random Thoughts of Interest

NAVIGATION - SEARCH

Entity Framework Spatial: A Real World Example

Background

From the Wikipedia article, Leadership in Energy and Environmental Design (LEED) is described as “an internationally recognized green building certification system, providing third-party verification that a building or community was designed and built using strategies intended to improve performance in metrics such as energy savings, water efficiency, CO2 emissions reduction, improved indoor environmental quality, and stewardship of resources and sensitivity to their impacts.”

In my own words, LEED is a certification system that awards points for following certain environmentally-friendly practices when constructing a building. In the end, a building can be qualify for one of four different levels of certifications, based on the number of points: Certified, Silver, Gold, Platinum. There are often tax benefits associated with having a LEED certification, and many new government buildings (especially Federal) are required to be LEED certified.

Two points in particular (out of of 100, or so) from the LEED checklist are related to geospatial data. One point is awarded if at least 20% of the building materials (by cost) used in construction were manufactured within 500 miles of the job site. A second point is awarded if 10% of the raw materials of those building materials were extracted, harvested, or recovered within 500 miles of the job site.

As a window glass manufacturer, Tempuri Glass is often asked to provide data about its products that are being considered for use in construction. Tempuri Glass may have a certain advantage over its competitors if it can quickly show that its products would count towards these two points for any arbitrary job site.

Data

Tempuri is a simple organization, making only a single type of product (Soda Lime glass) that is then cut into different sizes per order. Therefore, regardless of how many different sized glass panes are produced by a given facility, the ingredients for that glass is the same. The formulas used will be different between facilities, though, since the raw ingredients will be sourced from different locations, and adjustments may need to be made to the ratios due to environmental factors (things like: elevation, temperature, humidity, etc).

So, for our data model, we just need to know where each facility is, and then the formula used to make the glass at that facility (including the ingredients of that formula and the location where they were harvested from).

a.EF_Diagram

Within the data store, the [Geocode] columns of the Facility and FormulaComponent tables use the SQL Server geography type. This is useful for the large-scale/real-world distance calculations that Tempuri Glass needs to perform, since the way that you calculate distance on an sphere or ellipsoid (like the Earth) is vastly different than on a flat map.

In the Entity Framework model (using the June 2011 CTP), the SQL Server geography types are mapped as the new System.Data.Spatial.DbGeography type. This makes the geospatial data a first class citizen of our data model, and not just a castable opaque BLOB, as was the case in the past.

Geospatial data can take on many forms, including Points, Line Strings, Polygons, and collections of these shapes. Even though it’s not apparent from the data model, our [Geocode] data will contain only Points (i.e., a single Latitude/Longitude pair). Likewise, a job site will be specified as a single Point, though there is no hard requirement for this because distance can still be calculated between a Polygon and a Point with no coding change required.

Facility Sample Data

FacilityID

FacilityName

City

State

Geocode

1

Greenfield, IA

Greenfield

IA

POINT (-94.4547843933106 41.3151755156904)

2

Spring Green, WI

Spring Green

WI

POINT (-90.053981 43.17431)

3

Tomah, WI

Tomah

WI

POINT (-90.477058 43.989319)

4

Fremont, IN

Fremont

IN

POINT (-84.9314403533936 41.7186070559443)

5

Fargo, ND

Fargo

ND

POINT (-96.8667125701904 46.8985894795683)

6

Waxahachie, TX

Waxahachie

TX

POINT (-96.8427014350891 32.4424403136322)

7

Hood River, OR

Hood River

OR

POINT (-121.51526927948 45.630620334868)

8

Vinton, VA

Vinton

VA

POINT (-79.863876 37.263329)

9

Casa Grande, AZ

Casa Grande

AZ

POINT (-111.78155422210693 32.882073958767954)

10

Mountain Top, PA

Mountain Top

PA

POINT (-75.896477 41.141327)

11

Winlock, WA

Winlock

WA

POINT (-122.926218509674 46.5449155194259)

12

Durant, OK

Durant

OK

POINT (-96.4133548736572 34.0001619910696)

13

Mooresville, NC

Mooresville

NC

POINT (-80.7865476608277 35.6316281732984)

 

FormulaComponent Sample Data

FormulaComponentID

Name

Percentage

SourceLocation

Geocode

14

Limestone

13

Genola, UT

POINT (-111.808204650879 40.0098667779887)

1

Silica Sand

75

Houck, AZ

POINT (-109.241695404053 35.2062151838369)

27

Soda Ash

12

Trona, CA

POINT (-117.311668395996 35.6955040738332)

15

Limestone

13

Genola, UT

POINT (-111.808204650879 40.0098667779887)

2

Silica Sand

75

Houck, AZ

POINT (-109.241695404053 35.2062151838369)

28

Soda Ash

12

Trona, CA

POINT (-117.311668395996 35.6955040738332)

16

Limestone

13

Chicago, IL

POINT (-87.6176834106445 41.5738476278005)

3

Silica Sand

75

Overton, NV

POINT (-114.4313621521 36.5146030619859)

29

Soda Ash

12

Green River, WY

POINT (-109.448783397675 41.5090754257687)

 

Spatial Querying Algorithm

Input: Job Site Latitude/Longitude

Steps:

A. Query for closest facility to Job Site within 500 miles:

  1. Calculate the distance between the job site and each facility.
  2. Filter the list of facilities to just those where distance < 500 miles.
  3. Order the list of facilities by distance in ascending order.
  4. The first element (if any) will be the closest facility, and also signifies that the product qualifies as being manufactured within 500 miles

B. If there is a facility within 500 miles, then sum the percentage of formula components that were sourced from within 500 miles of the Job Site:

  1. Calculate the distance between the job site and each of the facility’s formula components
  2. Filter the list of formula components to just those where distance < 500 miles
  3. Sum the Percentages

Output: Boolean of whether the product qualifies; Percentage of the product’s ingredients that qualifies.

Implementation

Before we can calculate distance using an instance method of the DbGeography type, we need to actually create an instance to represent the Job Site. DbGeography is immutable and does not have a constructor, so instead, a static method must be called to create a new object. There are a number of these factory methods available to create specific kinds of shapes (Point, Line String, Polygon, etc) given different kinds of input (text, byte arrays).

For simplicity, let’s use the .Parse() method, which accepts Well-Known Text (WKT) as input, and assumes a Spatial Reference ID of 4326 (the same coordinate system that GPS and internet mapping sites use).

Note: WKT uses a (Longitude, Latitude) ordering for points, which adheres to the same concept as (X, Y) ordering for Cartesian coordinates.

private static DbGeography CreatePoint(double latitude, double longitude)
{
return DbGeography.Parse(String.Format("POINT({1} {0})", latitude, longitude));
}



The first spatial query, written as a LINQ expression, finds the closest qualifying facility. Since SRID 4326 uses meters as the unit of measure, we need to convert 500 miles into meters within the predicate:

private Facility GetNearestFacilityToJobsite(DbGeography jobsite)
{
    var q1 = from f in context.Facilities
    let distance = f.Geocode.Distance(jobsite)
    where distance < 500 * 1609.344
    orderby distance
    select f;

return q1.FirstOrDefault();
}



Assuming that a facility was returned, a second LINQ expression can be used to find the sum of Percentage from qualifying Formula Components:

private decimal SumQualifyingPercentages(Facility nearestFacility, DbGeography jobsite)
{
var q2 = from fc in nearestFacility.Formula.FormulaComponents
where fc.Geocode.Distance(jobsite) < 500 * 1609.344
select fc;

return q2.Sum(c => c.Percentage.GetValueOrDefault(0));
}



Finally, putting all of the parts together (using a Tuple<> for the output):

private Tuple<bool, decimal> GetResults(double latitude, double longitude)
{
DbGeography jobsite = CreatePoint(latitude, longitude);
Facility nearestFacility = GetNearestFacilityToJobsite(jobsite);

if (nearestFacility != null)
{
return new Tuple<bool,decimal>(true, SumQualifyingPercentages(nearestFacility, jobsite));
}

return new Tuple<bool, decimal>(false, 0);
}

private void PerformQuery()
{
double latitude = 47.63752;
double longitude = -122.13343;

var results = GetResults(latitude, longitude);
}