Skip to content

Commit

Permalink
2.1.0
Browse files Browse the repository at this point in the history
-added dbgeography support
-changed bbox to optional
-removed unnecessary points array from multipoint
-added unit tests for the specific spatial features
-a few cleanup
  • Loading branch information
alatas committed Sep 19, 2015
1 parent 7cc0447 commit 852a728
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 177 deletions.
2 changes: 2 additions & 0 deletions GeoJSON4EntityFramework/Base/GeoJsonGeometry.vb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
<JsonProperty(PropertyName:="bbox", Order:=5, NullValueHandling:=NullValueHandling.Ignore)>
Public Property BoundingBox As Double()

<JsonIgnore>
Public Property WithBoundingBox As Boolean = False
End Class
11 changes: 8 additions & 3 deletions GeoJSON4EntityFramework/Base/GeoJsonGeometryEF6.vb
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
Partial MustInherit Class GeoJsonGeometry(Of T)
Inherits GeoJsonElement(Of T)

Public MustOverride Sub CreateFromDbGeometry(inp As System.Data.Entity.Spatial.DbGeometry)
Public MustOverride Sub CreateFromDbGeometry(inp As Entity.Spatial.DbGeometry)

Public Shared Function FromDbGeometry(inp As System.Data.Entity.Spatial.DbGeometry) As GeoJsonGeometry(Of T)
Public Shared Function FromDbGeometry(inp As Entity.Spatial.DbGeometry, Optional withBoundingBox As Boolean = True) As GeoJsonGeometry(Of T)
Dim obj As GeoJsonGeometry(Of T) = CTypeDynamic(Activator.CreateInstance(Of T)(), GetType(T))

obj.BoundingBox = New Double(3) {
If withBoundingBox Then
obj.WithBoundingBox = True

obj.BoundingBox = New Double() {
inp.Envelope.PointAt(1).YCoordinate,
inp.Envelope.PointAt(1).XCoordinate,
inp.Envelope.PointAt(3).YCoordinate,
inp.Envelope.PointAt(3).XCoordinate
}

End If

obj.CreateFromDbGeometry(inp)
Return obj
End Function
Expand Down
43 changes: 35 additions & 8 deletions GeoJSON4EntityFramework/Elements/FeatureEF6.vb
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
Partial Class Feature

Public Shared Function FromDbGeometry(inp As System.Data.Entity.Spatial.DbGeometry) As Feature
Public Shared Function FromDbGeometry(inp As Entity.Spatial.DbGeometry, Optional withBoundingBox As Boolean = False) As Feature
Dim f As New Feature

Select Case inp.SpatialTypeName
Case "MultiPolygon"
f.Geometry.Add(MultiPolygon.FromDbGeometry(inp))
f.Geometry.Add(MultiPolygon.FromDbGeometry(inp, withBoundingBox))
Case "Polygon"
f.Geometry.Add(Polygon.FromDbGeometry(inp))
f.Geometry.Add(Polygon.FromDbGeometry(inp, withBoundingBox))
Case "Point"
f.Geometry.Add(Point.FromDbGeometry(inp))
f.Geometry.Add(Point.FromDbGeometry(inp, withBoundingBox))
Case "MultiPoint"
f.Geometry.Add(MultiPoint.FromDbGeometry(inp))
f.Geometry.Add(MultiPoint.FromDbGeometry(inp, withBoundingBox))
Case "LineString"
f.Geometry.Add(LineString.FromDbGeometry(inp))
f.Geometry.Add(LineString.FromDbGeometry(inp, withBoundingBox))
Case "MultiLineString"
f.Geometry.Add(MultiLineString.FromDbGeometry(inp))
f.Geometry.Add(MultiLineString.FromDbGeometry(inp, withBoundingBox))
Case "GeometryCollection"
f.Geometry.Add(GeometryCollection.FromDbGeometry(inp))
f.Geometry.Add(GeometryCollection.FromDbGeometry(inp, withBoundingBox))
Case Else
Throw New NotImplementedException
End Select

Return f
End Function

Public Shared Function FromDbGeography(inp As Entity.Spatial.DbGeography, Optional withBoundingBox As Boolean = False) As Feature
Dim f As New Feature

Dim inpgeom = Entity.Spatial.DbSpatialServices.Default.GeometryFromBinary(inp.AsBinary, inp.CoordinateSystemId)

Select Case inpgeom.SpatialTypeName
Case "MultiPolygon"
f.Geometry.Add(MultiPolygon.FromDbGeometry(inpgeom, withBoundingBox))
Case "Polygon"
f.Geometry.Add(Polygon.FromDbGeometry(inpgeom, withBoundingBox))
Case "Point"
f.Geometry.Add(Point.FromDbGeometry(inpgeom, withBoundingBox))
Case "MultiPoint"
f.Geometry.Add(MultiPoint.FromDbGeometry(inpgeom, withBoundingBox))
Case "LineString"
f.Geometry.Add(LineString.FromDbGeometry(inpgeom, withBoundingBox))
Case "MultiLineString"
f.Geometry.Add(MultiLineString.FromDbGeometry(inpgeom, withBoundingBox))
Case "GeometryCollection"
f.Geometry.Add(GeometryCollection.FromDbGeometry(inpgeom, withBoundingBox))
Case Else
Throw New NotImplementedException
End Select
Expand Down
8 changes: 4 additions & 4 deletions GeoJSON4EntityFramework/Elements/GeometryCollectionEF6.vb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
Dim element = inp.ElementAt(i)
Select Case element.SpatialTypeName
Case "MultiPolygon"
Geometries.Add(MultiPolygon.FromDbGeometry(element))
Geometries.Add(MultiPolygon.FromDbGeometry(element, WithBoundingBox))
Case "Polygon"
Geometries.Add(Polygon.FromDbGeometry(element))
Geometries.Add(Polygon.FromDbGeometry(element, WithBoundingBox))
Case "Point"
Geometries.Add(Point.FromDbGeometry(element))
Geometries.Add(Point.FromDbGeometry(element, WithBoundingBox))
Case "MultiPoint"
Geometries.Add(MultiPoint.FromDbGeometry(element))
Geometries.Add(MultiPoint.FromDbGeometry(element, WithBoundingBox))
Case Else
Throw New NotImplementedException
End Select
Expand Down
1 change: 1 addition & 0 deletions GeoJSON4EntityFramework/Elements/MultiPoint.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Inherits GeoJsonGeometry(Of MultiPoint)
Implements IGeoJsonGeometry

<JsonIgnore>
Public Property Points As New List(Of Point)

Public Overrides ReadOnly Property Coordinates As Object
Expand Down
4 changes: 2 additions & 2 deletions GeoJSON4EntityFramework/My Project/AssemblyInfo.vb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("2.0.0.0")>
<Assembly: AssemblyFileVersion("2.0.0.0")>
<Assembly: AssemblyVersion("2.1.0.0")>
<Assembly: AssemblyFileVersion("2.1.0.0")>
10 changes: 7 additions & 3 deletions GeoJSON4EntityFramework5/Base/GeoJsonGeometryEF5.vb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
Partial MustInherit Class GeoJsonGeometry(Of T)
Inherits GeoJsonElement(Of T)

Public MustOverride Sub CreateFromDbGeometry(inp As System.Data.Spatial.DbGeometry)
Public MustOverride Sub CreateFromDbGeometry(inp As Spatial.DbGeometry)

Public Shared Function FromDbGeometry(inp As System.Data.Spatial.DbGeometry) As GeoJsonGeometry(Of T)
Public Shared Function FromDbGeometry(inp As Spatial.DbGeometry, Optional withBoundingBox As Boolean = True) As GeoJsonGeometry(Of T)
Dim obj As GeoJsonGeometry(Of T) = CTypeDynamic(Activator.CreateInstance(Of T)(), GetType(T))

obj.BoundingBox = New Double(3) {
If withBoundingBox Then
obj.WithBoundingBox = True
obj.BoundingBox = New Double() {
inp.Envelope.PointAt(1).YCoordinate,
inp.Envelope.PointAt(1).XCoordinate,
inp.Envelope.PointAt(3).YCoordinate,
inp.Envelope.PointAt(3).XCoordinate
}

End If

obj.CreateFromDbGeometry(inp)
Return obj
End Function
Expand Down
43 changes: 35 additions & 8 deletions GeoJSON4EntityFramework5/Elements/FeatureEF5.vb
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
Partial Class Feature

Public Shared Function FromDbGeometry(inp As System.Data.Spatial.DbGeometry) As Feature
Public Shared Function FromDbGeometry(inp As Spatial.DbGeometry, Optional withBoundingBox As Boolean = False) As Feature
Dim f As New Feature

Select Case inp.SpatialTypeName
Case "MultiPolygon"
f.Geometry.Add(MultiPolygon.FromDbGeometry(inp))
f.Geometry.Add(MultiPolygon.FromDbGeometry(inp, withBoundingBox))
Case "Polygon"
f.Geometry.Add(Polygon.FromDbGeometry(inp))
f.Geometry.Add(Polygon.FromDbGeometry(inp, withBoundingBox))
Case "Point"
f.Geometry.Add(Point.FromDbGeometry(inp))
f.Geometry.Add(Point.FromDbGeometry(inp, withBoundingBox))
Case "MultiPoint"
f.Geometry.Add(MultiPoint.FromDbGeometry(inp))
f.Geometry.Add(MultiPoint.FromDbGeometry(inp, withBoundingBox))
Case "LineString"
f.Geometry.Add(LineString.FromDbGeometry(inp))
f.Geometry.Add(LineString.FromDbGeometry(inp, withBoundingBox))
Case "MultiLineString"
f.Geometry.Add(MultiLineString.FromDbGeometry(inp))
f.Geometry.Add(MultiLineString.FromDbGeometry(inp, withBoundingBox))
Case "GeometryCollection"
f.Geometry.Add(GeometryCollection.FromDbGeometry(inp))
f.Geometry.Add(GeometryCollection.FromDbGeometry(inp, withBoundingBox))
Case Else
Throw New NotImplementedException
End Select

Return f
End Function

Public Shared Function FromDbGeography(inp As Spatial.DbGeography, Optional withBoundingBox As Boolean = False) As Feature
Dim f As New Feature

Dim inpgeom = Spatial.DbSpatialServices.Default.GeometryFromBinary(inp.AsBinary, inp.CoordinateSystemId)

Select Case inpgeom.SpatialTypeName
Case "MultiPolygon"
f.Geometry.Add(MultiPolygon.FromDbGeometry(inpgeom, withBoundingBox))
Case "Polygon"
f.Geometry.Add(Polygon.FromDbGeometry(inpgeom, withBoundingBox))
Case "Point"
f.Geometry.Add(Point.FromDbGeometry(inpgeom, withBoundingBox))
Case "MultiPoint"
f.Geometry.Add(MultiPoint.FromDbGeometry(inpgeom, withBoundingBox))
Case "LineString"
f.Geometry.Add(LineString.FromDbGeometry(inpgeom, withBoundingBox))
Case "MultiLineString"
f.Geometry.Add(MultiLineString.FromDbGeometry(inpgeom, withBoundingBox))
Case "GeometryCollection"
f.Geometry.Add(GeometryCollection.FromDbGeometry(inpgeom, withBoundingBox))
Case Else
Throw New NotImplementedException
End Select
Expand Down
8 changes: 4 additions & 4 deletions GeoJSON4EntityFramework5/Elements/GeometryCollectionEF5.vb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
Dim element = inp.ElementAt(i)
Select Case element.SpatialTypeName
Case "MultiPolygon"
Geometries.Add(MultiPolygon.FromDbGeometry(element))
Geometries.Add(MultiPolygon.FromDbGeometry(element, WithBoundingBox))
Case "Polygon"
Geometries.Add(Polygon.FromDbGeometry(element))
Geometries.Add(Polygon.FromDbGeometry(element, WithBoundingBox))
Case "Point"
Geometries.Add(Point.FromDbGeometry(element))
Geometries.Add(Point.FromDbGeometry(element, WithBoundingBox))
Case "MultiPoint"
Geometries.Add(MultiPoint.FromDbGeometry(element))
Geometries.Add(MultiPoint.FromDbGeometry(element, WithBoundingBox))
Case Else
Throw New NotImplementedException
End Select
Expand Down
4 changes: 2 additions & 2 deletions GeoJSON4EntityFramework5/My Project/AssemblyInfo.vb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("2.0.0.0")>
<Assembly: AssemblyFileVersion("2.0.0.0")>
<Assembly: AssemblyVersion("2.1.0.0")>
<Assembly: AssemblyFileVersion("2.1.0.0")>
116 changes: 89 additions & 27 deletions TestProject/Tests.vb
Original file line number Diff line number Diff line change
@@ -1,41 +1,103 @@
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports alatas.GeoJSON4EntityFramework
Imports System.Data.Entity.Spatial
Imports alatas.GeoJSON4EntityFramework

<TestClass()>
Public Class Tests
Private fc As FeatureCollection
Private ReadOnly Property GetFeatureCollection() As FeatureCollection
Get
If fc Is Nothing Then
fc = New FeatureCollection
TestFeatures.ForEach(Sub(c)
Dim geom As Entity.Spatial.DbGeometry = Entity.Spatial.DbGeometry.FromText(c.Geometry)
Dim f = Feature.FromDbGeometry(geom)
f.ID = c.ID
f.Properties.Add("Name", c.Name)
f.Properties.Add("Area", geom.Area)
f.Properties.Add("Type", c.ElementType.ToString)
fc.Features.Add(f)
End Sub)
Return fc
End If

Return fc
End Get
End Property

Function GetFeatureCollection(Optional elementType As String = "", Optional withBBox As Boolean = False) As FeatureCollection
Dim fc As New FeatureCollection
TestFeatures.ForEach(Sub(c)
If elementType = "" Or (elementType <> "" And elementType = c.ElementType.ToString) Then
Dim geom = Entity.Spatial.DbGeometry.FromText(c.Geometry)
Dim f = Feature.FromDbGeometry(geom, withBBox)
f.ID = c.ID
f.Properties.Add("Name", c.Name)
f.Properties.Add("Area", geom.Area)
f.Properties.Add("Type", c.ElementType.ToString)
fc.Features.Add(f)
End If
End Sub)
Return fc
End Function

<TestMethod()> Public Sub TestAll()
Dim json = GeoJsonSerializer.Serialize(Of FeatureCollection)(GetFeatureCollection, True)
Dim json = GeoJsonSerializer.Serialize(Of FeatureCollection)(GetFeatureCollection(withBBox:=True), True)
Assert.IsNotNull(json)
WriteOutput(json)
End Sub

<TestMethod()> Public Sub OnlineTestAll()
Dim json = GeoJsonSerializer.Serialize(Of FeatureCollection)(GetFeatureCollection(withBBox:=True), False)
Assert.IsNotNull(json)
WriteOutput(json)
SendOutput(json)
End Sub

Public Sub TestSpecificType(elementType As String)
Dim json = GeoJsonSerializer.Serialize(Of FeatureCollection)(GetFeatureCollection(elementType.ToUpperInvariant), True)
Assert.IsNotNull(json)
WriteOutput(json)
End Sub

<TestMethod()> Public Sub TestAllOnline()
Dim json = GeoJsonSerializer.Serialize(Of FeatureCollection)(GetFeatureCollection, False)
Public Sub TestSpecificTypeOnline(elementType As String)
Dim json = GeoJsonSerializer.Serialize(Of FeatureCollection)(GetFeatureCollection(elementType.ToUpperInvariant), False)
Assert.IsNotNull(json)
WriteOutput(json)
SendOutput(json)
End Sub
<TestMethod> Sub TestMultiPolygon()
TestSpecificType("MultiPolygon")
End Sub

<TestMethod> Sub TestPolygon()
TestSpecificType("Polygon")
End Sub

<TestMethod> Sub TestPoint()
TestSpecificType("Point")
End Sub

<TestMethod> Sub TestMultiPoint()
TestSpecificType("MultiPoint")
End Sub

<TestMethod> Sub TestLineString()
TestSpecificType("LineString")
End Sub

<TestMethod> Sub TestMultiLineString()
TestSpecificType("MultiLineString")
End Sub

<TestMethod> Sub TestGeometryCollection()
TestSpecificType("GeometryCollection")
End Sub

<TestMethod> Sub OnlineTestMultiPolygon()
TestSpecificTypeOnline("MultiPolygon")
End Sub

<TestMethod> Sub OnlineTestPolygon()
TestSpecificTypeOnline("Polygon")
End Sub

<TestMethod> Sub OnlineTestPoint()
TestSpecificTypeOnline("Point")
End Sub

<TestMethod> Sub OnlineTestMultiPoint()
TestSpecificTypeOnline("MultiPoint")
End Sub

<TestMethod> Sub OnlineTestLineString()
TestSpecificTypeOnline("LineString")
End Sub

<TestMethod> Sub OnlineTestMultiLineString()
TestSpecificTypeOnline("MultiLineString")
End Sub

<TestMethod> Sub OnlineTestGeometryCollection()
TestSpecificTypeOnline("GeometryCollection")
End Sub

End Class
Loading

0 comments on commit 852a728

Please sign in to comment.