|
| 1 | +# GeoJSON4EntityFramework   |
| 2 | +___ |
| 3 | + |
| 4 | +### What is GeoJSON? |
| 5 | +[GeoJSON](http://geojson.org/) is a format for encoding a variety of geographic data structures. A GeoJSON object may represent a geometry, a feature, or a collection of features. |
| 6 | + |
| 7 | +In 2015, the Internet Engineering Task Force (IETF), in conjunction with the original specification authors, formed a GeoJSON WG to standardize GeoJSON. [RFC 7946](https://tools.ietf.org/html/rfc7946) was published in August 2016 and is the new standard specification of the GeoJSON format, replacing the 2008 GeoJSON specification. |
| 8 | + |
| 9 | +GeoJSON supports _Point_, _LineString_, _Polygon_, _MultiPoint_, _MultiLineString_, _MultiPolygon_, and _GeometryCollection_ geometry types. |
| 10 | + |
| 11 | +_Feature_ contain a geometry object and additional properties, and a _FeatureCollection_ represents a list of features. |
| 12 | + |
| 13 | +For example, A house, a road and a bus stop represents three different _Feature_. All of them might have different type of geometries. House could be a _polygon_, road could be a _linestring_ and bus stop could be a _point_. All of them represents a neighbourhood and this called _FeatureCollection_ in GeoJSON. |
| 14 | + |
| 15 | +### What is EntityFramework? |
| 16 | +EntityFramework (EF) is an open source object-relational mapping [(ORM)](https://en.wikipedia.org/wiki/Object-relational_mapping) framework for Microsoft .net. It allows us to use database rows as class instances. |
| 17 | + |
| 18 | +### What is Well-known Text (WKT)? |
| 19 | +[Well-known Text (WKT)](https://en.wikipedia.org/wiki/Well-known_text) is a text markup language for representing vector geometry objects on a map, spatial reference systems of spatial objects and transformations between spatial reference systems. In summary, It's a text representations of geometrical objects. |
| 20 | + |
| 21 | +### So, What is GeoJSON4EntityFramework ?? |
| 22 | +_GeoJSON for EntityFramework_ is a .net library that allows you to create GeoJSON output from EntityFramework Spatial Data or WKT inputs. In other words, It serializes different type of geometry objects to GeoJSON. It's not limited to only EF entities but It can serialize WKT inputs as well. |
| 23 | + |
| 24 | +### Features |
| 25 | +- [x] Supports Entity Framework v6 (System.Data.Entity.Spatial namespace) and Entity Framework v5 (System.Data.Spatial namespace) objects |
| 26 | +- [x] Supports Well-known Text inputs |
| 27 | +- [x] Supports DbGeometry (*planar*) and DbGeography (*geodetic "round earth"*) objects |
| 28 | +- [x] Supports all types of features defined in geojson specs ([RFC 7946](https://tools.ietf.org/html/rfc7946)) |
| 29 | +- [x] Supports boundingbox property defined in geojson specs ([RFC 7946](https://tools.ietf.org/html/rfc7946)) |
| 30 | +- [x] Supports geometry transform |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +### Quick Start |
| 35 | + |
| 36 | +#### EntityFramework Example |
| 37 | + |
| 38 | +**Visual Basic** |
| 39 | + |
| 40 | +```vbnet |
| 41 | +Imports alatas.GeoJSON4EntityFramework |
| 42 | + |
| 43 | +Function GetGeoJSONFromDB() As String |
| 44 | + Using db As New SpatialExampleEntities |
| 45 | + Dim data = From row In db.SampleTables Select row.SpatialData |
| 46 | + |
| 47 | + Dim features as New FeatureCollection(data.ToArray) |
| 48 | + Return features.Serialize(prettyPrint:=True) |
| 49 | + End Using |
| 50 | +End Function |
| 51 | +``` |
| 52 | + |
| 53 | +**C#** |
| 54 | + |
| 55 | +```csharp |
| 56 | +using alatas.GeoJSON4EntityFramework; |
| 57 | + |
| 58 | +public string GetGeoJSONFromDB() |
| 59 | +{ |
| 60 | + using (Entities db = new Entities()) |
| 61 | + { |
| 62 | + |
| 63 | + DbGeometry[] data = (from row in db.SampleTables select row.SpatialData).ToArray(); |
| 64 | + |
| 65 | + FeatureCollection features = new FeatureCollection(data); |
| 66 | + return features.Serialize(prettyPrint: true); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +#### Well-Known Text (WKT) Example |
| 72 | + |
| 73 | +**Visual Basic** |
| 74 | + |
| 75 | +```vbnet |
| 76 | +Imports alatas.GeoJSON4EntityFramework |
| 77 | + |
| 78 | +Function GetGeoJSONFromWKT() As String |
| 79 | + Dim WKTs = {"POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))", |
| 80 | + "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))", |
| 81 | + "LINESTRING (1 1, 2 2)"} |
| 82 | + |
| 83 | + Dim features as New FeatureCollection(WKTs) |
| 84 | + Return features.Serialize(prettyPrint:=True) |
| 85 | +End Function |
| 86 | +``` |
| 87 | + |
| 88 | +**C#** |
| 89 | + |
| 90 | +```csharp |
| 91 | +using alatas.GeoJSON4EntityFramework; |
| 92 | + |
| 93 | +public string GetGeoJSONFromWKT() |
| 94 | +{ |
| 95 | + string[] WKTs = { |
| 96 | + "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))", |
| 97 | + "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))", |
| 98 | + "LINESTRING (1 1, 2 2)" |
| 99 | + }; |
| 100 | + |
| 101 | + FeatureCollection features = new FeatureCollection(WKTs); |
| 102 | + return features.Serialize(prettyPrint: true); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +### Install |
| 109 | + |
| 110 | +#### Install with Package Manager Console - Nuget |
| 111 | + |
| 112 | +To install GeoJSON for Entity Framework, run the following command in the Package Manager Console |
| 113 | + |
| 114 | +**Entity Framework 6** |
| 115 | + |
| 116 | +```powershell |
| 117 | +Install-Package GeoJSON4EntityFramework |
| 118 | +``` |
| 119 | + |
| 120 | +**Entity Framework 5** |
| 121 | + |
| 122 | +```powershell |
| 123 | +Install-Package GeoJSON4EntityFramework5 |
| 124 | +``` |
| 125 | + |
| 126 | +#### Manual Install |
| 127 | +Download the latest [release](https://github.com/alatas/GeoJSON4EntityFramework/releases) and add to your project references manually |
| 128 | + |
| 129 | +### Prerequisites |
| 130 | +* Microsoft® System CLR Types for Microsoft® SQL Server® (x86/x64) [(SQLSysClrTypes.msi)](https://www.microsoft.com/en-us/download/details.aspx?id=49999) |
| 131 | + |
| 132 | +### Tests and Validation |
| 133 | +You may validate outputs with http://geojson.io and http://geojsonlint.com |
| 134 | +___ |
| 135 | +Test data extracted from OpenStreetMap®. OpenStreetMap® is open data, licensed under the [Open Data Commons Open Database License](http://opendatacommons.org/licenses/odbl/) (ODbL) by the OpenStreetMap Foundation (OSMF) |
0 commit comments