Skip to content

Commit f90a2fa

Browse files
author
Jörg Battermann
committed
GeometryObjects are done, next up Feature(Collection)s and then finally converters
1 parent 57f541d commit f90a2fa

11 files changed

Lines changed: 97 additions & 302 deletions

doc/GeoJSON.shfbproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
<FeedbackEMailAddress>jb%40joergbattermann.com</FeedbackEMailAddress>
3030
<FeedbackEMailLinkText>Joerg Battermann</FeedbackEMailLinkText>
3131
<CopyrightHref>https://github.com/jbattermann/GeoJSON.Net/blob/master/LICENSE</CopyrightHref>
32-
<CopyrightText>Copyright %28c%29 2011, J&amp;#246%3brg Battermann</CopyrightText>
32+
<CopyrightText>Copyright %28c%29 2011, Joerg Battermann</CopyrightText>
3333
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, TypeParameter, AutoDocumentDispose</MissingTags>
3434
<KeepLogFile>False</KeepLogFile>
35-
<ProjectSummary>.Net library for GeoJSON types &amp;amp%3b corresponding Json.Net %28de%29serializers.</ProjectSummary>
35+
<ProjectSummary>.Net library for GeoJSON types and corresponding Json.Net %28de%29serializers.</ProjectSummary>
3636
</PropertyGroup>
3737
<!-- There are no properties for these groups. AnyCPU needs to appear in
3838
order for Visual Studio to perform the build. The others are optional

src/GeoJSON.Net/Converters/GeometryConverter.cs

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@
1010
namespace GeoJSON.Net.Converters
1111
{
1212
using System;
13-
using System.Collections.Generic;
14-
using System.Linq;
1513

16-
using GeoJSON.Net.Exceptions;
1714
using GeoJSON.Net.Geometry;
1815

1916
using Newtonsoft.Json;
20-
using Newtonsoft.Json.Linq;
21-
using Newtonsoft.Json.Serialization;
2217

2318
/// <summary>
2419
/// Defines the GeometryObject type. Converts to/from a SimpleGeo 'geometry' field
@@ -44,94 +39,8 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
4439
/// </returns>
4540
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
4641
{
47-
var geometry = serializer.Deserialize<Dictionary<string, object>>(reader);
48-
JArray coordinates;
49-
try
50-
{
51-
coordinates = (JArray)geometry["coordinates"];
52-
if (coordinates == null || !coordinates.HasValues)
53-
{
54-
throw new ParsingException("Could not Parse SimpleGeo Response. (Coordinates missing?)");
55-
}
56-
}
57-
catch (Exception ex)
58-
{
59-
throw new ParsingException("Could not Parse SimpleGeo Response. (Coordinates missing?)", ex);
60-
}
61-
62-
var parsingErrors = new List<Exception>();
63-
64-
// ToDo: tidy up redundancies
65-
var geometryType = (string)geometry["type"];
66-
switch (geometryType.Trim())
67-
{
68-
case null:
69-
case "":
70-
return null;
71-
case "Point":
72-
var pointDeserializerSettings = new JsonSerializerSettings
73-
{
74-
Error = delegate(object sender, ErrorEventArgs args)
75-
{
76-
parsingErrors.Add(args.ErrorContext.Error);
77-
args.ErrorContext.Handled = true;
78-
},
79-
Converters = { new PositionConverter() }
80-
};
81-
var point = JsonConvert.DeserializeObject<Point>(
82-
coordinates.ToString(),
83-
pointDeserializerSettings);
84-
85-
if (parsingErrors.Any())
86-
{
87-
throw new AggregateException("Error Parsing GeometryObject.", parsingErrors);
88-
}
89-
90-
return point;
91-
case "Polygon":
92-
var polygonDeserializerSettings = new JsonSerializerSettings
93-
{
94-
Error = delegate(object sender, ErrorEventArgs args)
95-
{
96-
parsingErrors.Add(args.ErrorContext.Error);
97-
args.ErrorContext.Handled = true;
98-
},
99-
Converters = { new PolygonConverter() }
100-
};
101-
var polygon = JsonConvert.DeserializeObject<Polygon>(
102-
coordinates.ToString(),
103-
polygonDeserializerSettings);
104-
105-
if (parsingErrors.Any())
106-
{
107-
throw new AggregateException("Error parsing GeometryObject.", parsingErrors);
108-
}
109-
110-
return polygon;
111-
112-
case "MultiPolygon":
113-
var multipolygonDeserializerSettings = new JsonSerializerSettings
114-
{
115-
Error = delegate(object sender, ErrorEventArgs args)
116-
{
117-
parsingErrors.Add(args.ErrorContext.Error);
118-
args.ErrorContext.Handled = true;
119-
},
120-
Converters = { new MultiPolygonConverter() }
121-
};
122-
var multiPolygon = JsonConvert.DeserializeObject<MultiPolygon>(
123-
coordinates.ToString(),
124-
multipolygonDeserializerSettings);
125-
126-
if (parsingErrors.Any())
127-
{
128-
throw new AggregateException("Error parsing GeometryObject.", parsingErrors);
129-
}
130-
131-
return multiPolygon;
132-
default:
133-
throw new ParsingException(string.Format("Unknown geometry type '{0}' cannot be parsed.", geometryType));
134-
}
42+
// ToDo: implement
43+
throw new NotImplementedException();
13544
}
13645

13746
/// <summary>

src/GeoJSON.Net/Converters/MultiPolygonConverter.cs

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/GeoJSON.Net/Converters/PointsConverter.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/GeoJSON.Net/Converters/PolygonConverter.cs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@
1010
namespace GeoJSON.Net.Converters
1111
{
1212
using System;
13-
using System.Collections.Generic;
14-
using System.Linq;
1513

16-
using GeoJSON.Net;
17-
using GeoJSON.Net.Exceptions;
1814
using GeoJSON.Net.Geometry;
1915

2016
using Newtonsoft.Json;
21-
using Newtonsoft.Json.Linq;
22-
using Newtonsoft.Json.Serialization;
2317

2418
/// <summary>
2519
/// Converter to read and write the <see cref="MultiPolygon" /> type.
@@ -45,35 +39,8 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
4539
/// </returns>
4640
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
4741
{
48-
var pointsArray = (JArray)serializer.Deserialize(reader);
49-
50-
if (pointsArray == null || pointsArray.Count != 1)
51-
{
52-
throw new ParsingException("Polygon geometry coordinates could not be parsed.");
53-
}
54-
55-
var points = new List<Point>();
56-
var parsingErrors = new List<Exception>();
57-
58-
var polygonDeserializerSettings = new JsonSerializerSettings
59-
{
60-
Error = delegate(object sender, ErrorEventArgs args)
61-
{
62-
parsingErrors.Add(args.ErrorContext.Error);
63-
args.ErrorContext.Handled = true;
64-
},
65-
Converters = { new PositionConverter() }
66-
};
67-
points.AddRange(JsonConvert.DeserializeObject<List<Point>>(
68-
pointsArray.First.ToString(),
69-
polygonDeserializerSettings));
70-
71-
if (parsingErrors.Any())
72-
{
73-
throw new AggregateException("Error parsing GeometryObject.", parsingErrors);
74-
}
75-
76-
return new Polygon(points);
42+
// ToDo: implement
43+
throw new NotImplementedException();
7744
}
7845

7946
/// <summary>

src/GeoJSON.Net/GeoJSON.Net.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@
4242
<Reference Include="Microsoft.CSharp" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Converters\PointsConverter.cs" />
4645
<Compile Include="Converters\GeometryConverter.cs" />
47-
<Compile Include="Converters\MultiPolygonConverter.cs" />
4846
<Compile Include="Converters\PositionConverter.cs" />
4947
<Compile Include="Converters\PolygonConverter.cs" />
5048
<Compile Include="CoordinateReferenceSystem\LinkedCRS.cs" />
@@ -53,6 +51,7 @@
5351
<Compile Include="CoordinateReferenceSystem\ICRSObject.cs" />
5452
<Compile Include="CoordinateReferenceSystem\NamedCRS.cs" />
5553
<Compile Include="GeoJSONObject.cs" />
54+
<Compile Include="Geometry\MultiLineString.cs" />
5655
<Compile Include="Geometry\GeometryType.cs" />
5756
<Compile Include="Geometry\LineString.cs" />
5857
<Compile Include="Geometry\MultiPoint.cs" />

src/GeoJSON.Net/Geometry/LineString.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace GeoJSON.Net.Geometry
1212
using System;
1313
using System.Collections.Generic;
1414

15+
using GeoJSON.Net.Converters;
16+
1517
using Newtonsoft.Json;
1618

1719
/// <summary>
@@ -46,6 +48,7 @@ public LineString(List<Position> coordinates)
4648
/// </summary>
4749
/// <value>The Positions.</value>
4850
[JsonProperty(PropertyName = "coordinates", Required = Required.Always)]
51+
[JsonConverter(typeof(PositionConverter))]
4952
public List<Position> Coordinates { get; private set; }
5053

5154
/// <summary>
@@ -56,7 +59,18 @@ public LineString(List<Position> coordinates)
5659
/// </returns>
5760
public bool IsLinearRing()
5861
{
59-
return this.Coordinates.Count >= 4 && this.Coordinates[0].Equals(this.Coordinates[this.Coordinates.Count - 1]);
62+
return this.Coordinates.Count >= 4 && this.IsClosed();
63+
}
64+
65+
/// <summary>
66+
/// Determines whether this instance has its first and last coordinate at the same position and thereby is closed.
67+
/// </summary>
68+
/// <returns>
69+
/// <c>true</c> if this instance is closed; otherwise, <c>false</c>.
70+
/// </returns>
71+
public bool IsClosed()
72+
{
73+
return this.Coordinates[0].Equals(this.Coordinates[this.Coordinates.Count - 1]);
6074
}
6175
}
6276
}

0 commit comments

Comments
 (0)