Newer
Older
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Collections.Generic;
using System.Linq;
using GeoJSON.Net.Feature;
using GeoJSON.Net.Geometry;
namespace Mapbox.Vector.Tile
{
public static class VectorTileFeatureExtensions
{
private static List<Position> Project( List<Coordinate> coords, int x, int y, int z, uint extent )
{
return coords.Select( coord => coord.ToPosition( x, y, z, extent ) ).ToList();
}
private static LineString CreateLineString( List<Position> pos )
{
return new LineString( pos );
}
private static IGeometryObject GetPointGeometry( List<Position> pointList )
{
IGeometryObject geom;
if (pointList.Count == 1)
{
geom = new Point( pointList[0] );
}
else
{
var pnts = pointList.Select(p => new Point(p)).ToList();
geom = new MultiPoint( pnts );
}
return geom;
}
private static List<LineString> GetLineStringList( List<List<Position>> pointList )
{
return pointList.Select( part => CreateLineString( part ) ).ToList();
}
private static IGeometryObject GetLineGeometry( List<List<Position>> pointList )
{
IGeometryObject geom;
if (pointList.Count == 1)
{
geom = new LineString( pointList[0] );
}
else
{
geom = new MultiLineString( GetLineStringList( pointList ) );
}
return geom;
}
private static Polygon GetPolygon( List<List<Position>> lines )
{
var res = new List<LineString>();
foreach (var innerring in lines)
{
var line = new LineString(innerring);
if (line.IsLinearRing() && line.IsClosed())
{
res.Add( line );
}
}
var geom = new Polygon(res);
return geom;
}
private static IGeometryObject GetPolygonGeometry( List<List<List<Position>>> polygons )
{
{
IGeometryObject geom=null;
if (polygons.Count == 1)
{
geom = GetPolygon( polygons[0] );
}
else if (polygons.Count>0)
{
var multipolys = new List<Polygon>();
foreach (var poly in polygons)
{
var pl = GetPolygon(poly);
multipolys.Add( pl );
}
geom = new MultiPolygon( multipolys );
}
return geom;
}
}
public static List<Position> ProjectPoints( List<List<Coordinate>> Geometry, int x, int y, int z, uint extent )
{
var projectedCoords = new List<Position>();
var coords = new List<Coordinate>();
foreach (var g in Geometry)
{
coords.Add( g[0] );
projectedCoords = Project( coords, x, y, z, extent );
}
return projectedCoords;
}
public static List<List<Position>> ProjectLines( List<List<Coordinate>> Geometry, int x, int y, int z, uint extent )
{
var projectedCoords = new List<Position>();
var pointList = new List<List<Position>>();
foreach (var g in Geometry)
{
projectedCoords = Project( g, x, y, z, extent );
pointList.Add( projectedCoords );
}
return pointList;
}
public static List<List<List<Position>>> ProjectPolygons( List<List<List<Coordinate>>> Geometry, int x, int y, int z, uint extent )
{
var projectedCoords = new List<List<Position>>();
var result = new List<List<List<Position>>>();
foreach (var g in Geometry)
{
projectedCoords = ProjectLines( g, x, y, z, extent );
result.Add( projectedCoords );
}
return result;
}
public static Feature ToGeoJSON( this VectorTileFeature vectortileFeature, int x, int y, int z )
{
IGeometryObject geom = null;
switch (vectortileFeature.GeometryType)
{
case Tile.GeomType.Point:
var projectedPoints = ProjectPoints(vectortileFeature.Geometry, x, y, z, vectortileFeature.Extent);
geom = GetPointGeometry( projectedPoints );
break;
case Tile.GeomType.LineString:
var projectedLines = ProjectLines(vectortileFeature.Geometry, x, y, z, vectortileFeature.Extent);
geom = GetLineGeometry( projectedLines );
break;
case Tile.GeomType.Polygon:
var rings = ClassifyRings.Classify(vectortileFeature.Geometry);
var projectedPolygons = ProjectPolygons(rings, x, y, z, vectortileFeature.Extent);
geom = GetPolygonGeometry( projectedPolygons );
break;
}
var result = new Feature(geom, id: vectortileFeature.Id);
// add attributes
foreach (var item in vectortileFeature.Attributes)
{
result.Properties.Add( item.Key, item.Value );
}
return result;
}
}
}