forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlGeoJSONExtensions.cs
More file actions
52 lines (50 loc) · 2.08 KB
/
NpgsqlGeoJSONExtensions.cs
File metadata and controls
52 lines (50 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Data;
using GeoJSON.Net;
using GeoJSON.Net.Geometry;
using Npgsql.GeoJSON;
using Npgsql.TypeMapping;
using NpgsqlTypes;
namespace Npgsql
{
/// <summary>
/// Extension allowing adding the GeoJSON plugin to an Npgsql type mapper.
/// </summary>
public static class NpgsqlGeoJSONExtensions
{
static readonly Type[] ClrTypes = new[]
{
typeof(GeoJSONObject), typeof(IGeoJSONObject), typeof(IGeometryObject),
typeof(Point), typeof(LineString), typeof(Polygon),
typeof(MultiPoint), typeof(MultiLineString), typeof(MultiPolygon),
typeof(GeometryCollection)
};
/// <summary>
/// Sets up GeoJSON mappings for the PostGIS types.
/// </summary>
/// <param name="mapper">The type mapper to set up (global or connection-specific)</param>
/// <param name="options">Options to use when constructing objects.</param>
/// <param name="geographyAsDefault">Specifies that the geography type is used for mapping by default.</param>
public static INpgsqlTypeMapper UseGeoJson(this INpgsqlTypeMapper mapper, GeoJSONOptions options = GeoJSONOptions.None, bool geographyAsDefault = false)
{
var factory = new GeoJSONHandlerFactory(options);
return mapper
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "geometry",
NpgsqlDbType = NpgsqlDbType.Geometry,
ClrTypes = geographyAsDefault ? Type.EmptyTypes : ClrTypes,
InferredDbType = DbType.Object,
TypeHandlerFactory = factory
}.Build())
.AddMapping(new NpgsqlTypeMappingBuilder
{
PgTypeName = "geography",
NpgsqlDbType = NpgsqlDbType.Geography,
ClrTypes = geographyAsDefault ? ClrTypes : Type.EmptyTypes,
InferredDbType = DbType.Object,
TypeHandlerFactory = factory
}.Build());
}
}
}