Skip to content

Commit

Permalink
Add geography type converter
Browse files Browse the repository at this point in the history
  • Loading branch information
chinadragon0515 committed Aug 2, 2016
1 parent b83b399 commit 7fb6c6d
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Microsoft.Restier.Providers.EntityFramework")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Microsoft.Restier.Providers.EntityFramework.Model")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Microsoft.Restier.Providers.EntityFramework.Query")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Microsoft.Restier.Providers.EntityFramework.Spatial")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Microsoft.Restier.Providers.EntityFramework.Submit")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Microsoft.Restier.Publishers.OData")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Microsoft.Restier.Publishers.OData.Batch")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<HintPath>..\..\packages\Microsoft.OData.Edm.6.15.0\lib\portable-net45+win+wpa81\Microsoft.OData.Edm.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Spatial, Version=6.15.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Spatial.6.15.0\lib\portable-net45+win+wpa81\Microsoft.Spatial.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -77,6 +81,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="ServiceCollectionExtensions.cs" />
<Compile Include="Spatial\GeographyConverter.cs" />
<Compile Include="Submit\ChangeSetInitializer.cs" />
<Compile Include="Submit\SubmitExecutor.cs" />
</ItemGroup>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
<data name="DataModificationMustBeCUD" xml:space="preserve">
<value>A DataModificationEntry must be either New, Update or Delete.</value>
</data>
<data name="InvalidLineStringGeographyType" xml:space="preserve">
<value>Need 'LineString type', while input is {0}.</value>
</data>
<data name="InvalidPointGeographyType" xml:space="preserve">
<value>Need 'Point type', while input is {0}.</value>
</data>
<data name="PreconditionCheckFailed" xml:space="preserve">
<value>The precondition check for request {0} on resource {1} is failed.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Data.Entity.Spatial;
using System.Globalization;
using System.Text;
using Microsoft.Restier.Providers.EntityFramework.Properties;
using Microsoft.Spatial;

namespace Microsoft.Restier.Providers.EntityFramework.Spatial
{
/// <summary>
/// The class defined conversion between GeographyPoint and DbGeography,
/// and between GeographyLineString and DbGeography.
/// </summary>
public static class GeographyConverter
{
private static readonly CultureInfo DefaultCulture = CultureInfo.GetCultureInfo("En-Us");
private const string GeographyTypeNamePoint = "Point";
private const string GeographyTypeNameLineString = "LineString";

public static GeographyPoint ToGeographyPoint(this DbGeography geography)
{
if (geography == null)
{
return null;
}

if (geography.SpatialTypeName != GeographyTypeNamePoint)
{
throw new InvalidOperationException(string.Format(
CultureInfo.InvariantCulture,
Resources.InvalidPointGeographyType,
geography.SpatialTypeName));
}

double lat = geography.Latitude ?? 0;
double lon = geography.Longitude ?? 0;
double? alt = geography.Elevation;
double? m = geography.Measure;
return GeographyPoint.Create(lat, lon, alt, m);
}

public static DbGeography ToDbGeography(this GeographyPoint point)
{
if (point == null)
{
return null;
}

string text = "POINT(" + point.Latitude.ToString(DefaultCulture) + " " +
point.Longitude.ToString(DefaultCulture);

if (point.Z.HasValue)
{
text += " " + point.Z.Value;
}

if (point.M.HasValue)
{
text += " " + point.M.Value;
}

text += ")";

return DbGeography.FromText(text);
}

public static GeographyLineString ToGeographyLineString(this DbGeography geography)
{
if (geography == null)
{
return null;
}

if (geography.SpatialTypeName != GeographyTypeNameLineString)
{
throw new InvalidOperationException(string.Format(
CultureInfo.InvariantCulture,
Resources.InvalidLineStringGeographyType,
geography.SpatialTypeName));
}

SpatialBuilder builder = SpatialBuilder.Create();
GeographyPipeline pipleLine = builder.GeographyPipeline;
pipleLine.SetCoordinateSystem(CoordinateSystem.DefaultGeography);
pipleLine.BeginGeography(SpatialType.LineString);

int numPoints = geography.PointCount ?? 0;
if (numPoints > 0)
{
DbGeography point = geography.PointAt(1);
pipleLine.BeginFigure(new GeographyPosition(point.Latitude ?? 0, point.Latitude ?? 0, point.Elevation, point.Measure));

for (int n = 2; n <= numPoints; n++)
{
point = geography.PointAt(n);
pipleLine.LineTo(new GeographyPosition(point.Latitude ?? 0, point.Latitude ?? 0, point.Elevation, point.Measure));
}

pipleLine.EndFigure();
}

pipleLine.EndGeography();
GeographyLineString lineString = (GeographyLineString)builder.ConstructedGeography;
return lineString;
}

public static DbGeography ToDbGeography(this GeographyLineString lineString)
{
if (lineString == null)
{
return null;
}

StringBuilder sb = new StringBuilder("LINESTRING(");
int n = 0;
foreach (var pt in lineString.Points)
{
double lat = pt.Latitude;
double lon = pt.Longitude;
double? alt = pt.Z;
double? m = pt.M;

string pointStr = lat.ToString(DefaultCulture) + " " + lon.ToString(DefaultCulture);

if (alt != null)
{
pointStr += " " + alt.Value;
}

if (m != null)
{
pointStr += " " + m.Value;
}

sb.Append(pointStr);
n++;
if (n != lineString.Points.Count)
{
sb.Append(",");
}
}
sb.Append(")");

return DbGeography.FromText(sb.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.OData.Edm" version="6.15.0" targetFramework="net45" />
<package id="Microsoft.Spatial" version="6.15.0" targetFramework="net45" />
<package id="System.ComponentModel" version="4.0.1" targetFramework="net45" />
<package id="System.Diagnostics.Debug" version="4.0.11" targetFramework="net45" />
<package id="System.Globalization" version="4.0.11" targetFramework="net45" />
Expand Down
25 changes: 25 additions & 0 deletions test/Microsoft.Restier.TestCommon/PublicApi.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,31 @@ public class Microsoft.Restier.Publishers.OData.RestierPayloadValueConverter : M
public virtual object ConvertToPayloadValue (object value, Microsoft.OData.Edm.IEdmTypeReference edmTypeReference)
}

[
ExtensionAttribute(),
]
public sealed class Microsoft.Restier.Providers.EntityFramework.Spatial.GeographyConverter {
[
ExtensionAttribute(),
]
public static DbGeography ToDbGeography (Microsoft.Spatial.GeographyLineString lineString)

[
ExtensionAttribute(),
]
public static DbGeography ToDbGeography (Microsoft.Spatial.GeographyPoint point)

[
ExtensionAttribute(),
]
public static Microsoft.Spatial.GeographyLineString ToGeographyLineString (DbGeography geography)

[
ExtensionAttribute(),
]
public static Microsoft.Spatial.GeographyPoint ToGeographyPoint (DbGeography geography)
}

public class Microsoft.Restier.Publishers.OData.Batch.RestierBatchChangeSetRequestItem : System.Web.OData.Batch.ChangeSetRequestItem, IDisposable {
public RestierBatchChangeSetRequestItem (System.Collections.Generic.IEnumerable`1[[System.Net.Http.HttpRequestMessage]] requests, System.Func`1[[Microsoft.Restier.Core.ApiBase]] apiFactory)

Expand Down

0 comments on commit 7fb6c6d

Please sign in to comment.