Skip to content

Commit

Permalink
GeometryObjects are done, next up Feature(Collection)s and then final…
Browse files Browse the repository at this point in the history
…ly converters
  • Loading branch information
joergbattermann committed Jan 16, 2011
1 parent 57f541d commit f90a2fa
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 302 deletions.
4 changes: 2 additions & 2 deletions doc/GeoJSON.shfbproj
Expand Up @@ -29,10 +29,10 @@
<FeedbackEMailAddress>jb%40joergbattermann.com</FeedbackEMailAddress>
<FeedbackEMailLinkText>Joerg Battermann</FeedbackEMailLinkText>
<CopyrightHref>https://github.com/jbattermann/GeoJSON.Net/blob/master/LICENSE</CopyrightHref>
<CopyrightText>Copyright %28c%29 2011, J&amp;#246%3brg Battermann</CopyrightText>
<CopyrightText>Copyright %28c%29 2011, Joerg Battermann</CopyrightText>
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, TypeParameter, AutoDocumentDispose</MissingTags>
<KeepLogFile>False</KeepLogFile>
<ProjectSummary>.Net library for GeoJSON types &amp;amp%3b corresponding Json.Net %28de%29serializers.</ProjectSummary>
<ProjectSummary>.Net library for GeoJSON types and corresponding Json.Net %28de%29serializers.</ProjectSummary>
</PropertyGroup>
<!-- There are no properties for these groups. AnyCPU needs to appear in
order for Visual Studio to perform the build. The others are optional
Expand Down
95 changes: 2 additions & 93 deletions src/GeoJSON.Net/Converters/GeometryConverter.cs
Expand Up @@ -10,15 +10,10 @@
namespace GeoJSON.Net.Converters
{
using System;
using System.Collections.Generic;
using System.Linq;

using GeoJSON.Net.Exceptions;
using GeoJSON.Net.Geometry;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

/// <summary>
/// Defines the GeometryObject type. Converts to/from a SimpleGeo 'geometry' field
Expand All @@ -44,94 +39,8 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
/// </returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var geometry = serializer.Deserialize<Dictionary<string, object>>(reader);
JArray coordinates;
try
{
coordinates = (JArray)geometry["coordinates"];
if (coordinates == null || !coordinates.HasValues)
{
throw new ParsingException("Could not Parse SimpleGeo Response. (Coordinates missing?)");
}
}
catch (Exception ex)
{
throw new ParsingException("Could not Parse SimpleGeo Response. (Coordinates missing?)", ex);
}

var parsingErrors = new List<Exception>();

// ToDo: tidy up redundancies
var geometryType = (string)geometry["type"];
switch (geometryType.Trim())
{
case null:
case "":
return null;
case "Point":
var pointDeserializerSettings = new JsonSerializerSettings
{
Error = delegate(object sender, ErrorEventArgs args)
{
parsingErrors.Add(args.ErrorContext.Error);
args.ErrorContext.Handled = true;
},
Converters = { new PositionConverter() }
};
var point = JsonConvert.DeserializeObject<Point>(
coordinates.ToString(),
pointDeserializerSettings);

if (parsingErrors.Any())
{
throw new AggregateException("Error Parsing GeometryObject.", parsingErrors);
}

return point;
case "Polygon":
var polygonDeserializerSettings = new JsonSerializerSettings
{
Error = delegate(object sender, ErrorEventArgs args)
{
parsingErrors.Add(args.ErrorContext.Error);
args.ErrorContext.Handled = true;
},
Converters = { new PolygonConverter() }
};
var polygon = JsonConvert.DeserializeObject<Polygon>(
coordinates.ToString(),
polygonDeserializerSettings);

if (parsingErrors.Any())
{
throw new AggregateException("Error parsing GeometryObject.", parsingErrors);
}

return polygon;

case "MultiPolygon":
var multipolygonDeserializerSettings = new JsonSerializerSettings
{
Error = delegate(object sender, ErrorEventArgs args)
{
parsingErrors.Add(args.ErrorContext.Error);
args.ErrorContext.Handled = true;
},
Converters = { new MultiPolygonConverter() }
};
var multiPolygon = JsonConvert.DeserializeObject<MultiPolygon>(
coordinates.ToString(),
multipolygonDeserializerSettings);

if (parsingErrors.Any())
{
throw new AggregateException("Error parsing GeometryObject.", parsingErrors);
}

return multiPolygon;
default:
throw new ParsingException(string.Format("Unknown geometry type '{0}' cannot be parsed.", geometryType));
}
// ToDo: implement
throw new NotImplementedException();
}

/// <summary>
Expand Down
89 changes: 0 additions & 89 deletions src/GeoJSON.Net/Converters/MultiPolygonConverter.cs

This file was deleted.

61 changes: 0 additions & 61 deletions src/GeoJSON.Net/Converters/PointsConverter.cs

This file was deleted.

37 changes: 2 additions & 35 deletions src/GeoJSON.Net/Converters/PolygonConverter.cs
Expand Up @@ -10,16 +10,10 @@
namespace GeoJSON.Net.Converters
{
using System;
using System.Collections.Generic;
using System.Linq;

using GeoJSON.Net;
using GeoJSON.Net.Exceptions;
using GeoJSON.Net.Geometry;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

/// <summary>
/// Converter to read and write the <see cref="MultiPolygon" /> type.
Expand All @@ -45,35 +39,8 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
/// </returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var pointsArray = (JArray)serializer.Deserialize(reader);

if (pointsArray == null || pointsArray.Count != 1)
{
throw new ParsingException("Polygon geometry coordinates could not be parsed.");
}

var points = new List<Point>();
var parsingErrors = new List<Exception>();

var polygonDeserializerSettings = new JsonSerializerSettings
{
Error = delegate(object sender, ErrorEventArgs args)
{
parsingErrors.Add(args.ErrorContext.Error);
args.ErrorContext.Handled = true;
},
Converters = { new PositionConverter() }
};
points.AddRange(JsonConvert.DeserializeObject<List<Point>>(
pointsArray.First.ToString(),
polygonDeserializerSettings));

if (parsingErrors.Any())
{
throw new AggregateException("Error parsing GeometryObject.", parsingErrors);
}

return new Polygon(points);
// ToDo: implement
throw new NotImplementedException();
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions src/GeoJSON.Net/GeoJSON.Net.csproj
Expand Up @@ -42,9 +42,7 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="Converters\PointsConverter.cs" />
<Compile Include="Converters\GeometryConverter.cs" />
<Compile Include="Converters\MultiPolygonConverter.cs" />
<Compile Include="Converters\PositionConverter.cs" />
<Compile Include="Converters\PolygonConverter.cs" />
<Compile Include="CoordinateReferenceSystem\LinkedCRS.cs" />
Expand All @@ -53,6 +51,7 @@
<Compile Include="CoordinateReferenceSystem\ICRSObject.cs" />
<Compile Include="CoordinateReferenceSystem\NamedCRS.cs" />
<Compile Include="GeoJSONObject.cs" />
<Compile Include="Geometry\MultiLineString.cs" />
<Compile Include="Geometry\GeometryType.cs" />
<Compile Include="Geometry\LineString.cs" />
<Compile Include="Geometry\MultiPoint.cs" />
Expand Down
16 changes: 15 additions & 1 deletion src/GeoJSON.Net/Geometry/LineString.cs
Expand Up @@ -12,6 +12,8 @@ namespace GeoJSON.Net.Geometry
using System;
using System.Collections.Generic;

using GeoJSON.Net.Converters;

using Newtonsoft.Json;

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

/// <summary>
Expand All @@ -56,7 +59,18 @@ public LineString(List<Position> coordinates)
/// </returns>
public bool IsLinearRing()
{
return this.Coordinates.Count >= 4 && this.Coordinates[0].Equals(this.Coordinates[this.Coordinates.Count - 1]);
return this.Coordinates.Count >= 4 && this.IsClosed();
}

/// <summary>
/// Determines whether this instance has its first and last coordinate at the same position and thereby is closed.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is closed; otherwise, <c>false</c>.
/// </returns>
public bool IsClosed()
{
return this.Coordinates[0].Equals(this.Coordinates[this.Coordinates.Count - 1]);
}
}
}
Expand Down

0 comments on commit f90a2fa

Please sign in to comment.