Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic feature #85

Merged
merged 8 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ lib/*

#OSX
.DS_Store
/.vs
1 change: 1 addition & 0 deletions src/GeoJSON.Net.Tests/Feature/FeatureTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GeoJSON.Net.Feature;
using GeoJSON.Net.Geometry;
using Newtonsoft.Json;
using NUnit.Framework;
Expand Down
160 changes: 102 additions & 58 deletions src/GeoJSON.Net.Tests/Feature/GenericFeatureTests.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,102 @@
using System.Linq;
using GeoJSON.Net.Geometry;
using Newtonsoft.Json;
using NUnit.Framework;

namespace GeoJSON.Net.Tests.Feature
{
[TestFixture]
internal class GenericFeatureTests : TestBase
{
[Test]
public void Can_Deserialize_Point_Feature()
{
var json = GetExpectedJson();

var feature = JsonConvert.DeserializeObject<Net.Feature.Feature<Point>>(json);

Assert.IsNotNull(feature);
Assert.IsNotNull(feature.Properties);
Assert.IsTrue(feature.Properties.Any());

Assert.IsTrue(feature.Properties.ContainsKey("name"));
Assert.AreEqual("Dinagat Islands", feature.Properties["name"]);

Assert.AreEqual("test-id", feature.Id);

Assert.AreEqual(GeoJSONObjectType.Point, feature.Geometry.Type);
Assert.AreEqual(125.6, feature.Geometry.Coordinates.Longitude);
Assert.AreEqual(10.1, feature.Geometry.Coordinates.Latitude);
Assert.AreEqual(456, feature.Geometry.Coordinates.Altitude);
}

[Test]
public void Can_Deserialize_LineString_Feature()
{
var json = GetExpectedJson();

var feature = JsonConvert.DeserializeObject<Net.Feature.Feature<LineString>>(json);

Assert.IsNotNull(feature);
Assert.IsNotNull(feature.Properties);
Assert.IsTrue(feature.Properties.Any());

Assert.IsTrue(feature.Properties.ContainsKey("name"));
Assert.AreEqual("Dinagat Islands", feature.Properties["name"]);

Assert.AreEqual("test-id", feature.Id);

Assert.AreEqual(GeoJSONObjectType.LineString, feature.Geometry.Type);

Assert.AreEqual(4, feature.Geometry.Coordinates.Count);

//Assert.AreEqual(125.6, feature.Geometry.Coordinates.Longitude);
//Assert.AreEqual(10.1, feature.Geometry.Coordinates.Latitude);
//Assert.AreEqual(456, feature.Geometry.Coordinates.Altitude);
}
}
}
using System.Linq;
using GeoJSON.Net.Feature;
using GeoJSON.Net.Geometry;
using Newtonsoft.Json;
using NUnit.Framework;

namespace GeoJSON.Net.Tests.Feature
{
[TestFixture]
internal class GenericFeatureTests : TestBase
{
[Test]
public void Can_Deserialize_Point_Feature()
{
var json = GetExpectedJson();

var feature = JsonConvert.DeserializeObject<Feature<Point>>(json);

Assert.IsNotNull(feature);
Assert.IsNotNull(feature.Properties);
Assert.IsTrue(feature.Properties.Any());

Assert.IsTrue(feature.Properties.ContainsKey("name"));
Assert.AreEqual("Dinagat Islands", feature.Properties["name"]);

Assert.AreEqual("test-id", feature.Id);

Assert.AreEqual(GeoJSONObjectType.Point, feature.Geometry.Type);
Assert.AreEqual(125.6, feature.Geometry.Coordinates.Longitude);
Assert.AreEqual(10.1, feature.Geometry.Coordinates.Latitude);
Assert.AreEqual(456, feature.Geometry.Coordinates.Altitude);
}

[Test]
public void Can_Deserialize_LineString_Feature()
{
var json = GetExpectedJson();

var feature = JsonConvert.DeserializeObject<Feature<LineString>>(json);

Assert.IsNotNull(feature);
Assert.IsNotNull(feature.Properties);
Assert.IsTrue(feature.Properties.Any());

Assert.IsTrue(feature.Properties.ContainsKey("name"));
Assert.AreEqual("Dinagat Islands", feature.Properties["name"]);

Assert.AreEqual("test-id", feature.Id);

Assert.AreEqual(GeoJSONObjectType.LineString, feature.Geometry.Type);

Assert.AreEqual(4, feature.Geometry.Coordinates.Count);

//Assert.AreEqual(125.6, feature.Geometry.Coordinates.Longitude);
//Assert.AreEqual(10.1, feature.Geometry.Coordinates.Latitude);
//Assert.AreEqual(456, feature.Geometry.Coordinates.Altitude);
}

private class TypedFeatureProps
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value")]
public double Value { get; set; }
}

[Test]
public void Can_Deserialize_Typed_Point_Feature()
{
var json = GetExpectedJson();
var feature = JsonConvert.DeserializeObject<Feature<Point, TypedFeatureProps>>(json);

Assert.IsNotNull(feature);

Assert.IsNotNull(feature.Properties);
Assert.AreEqual(feature.Properties.Name, "Dinagat Islands");
Assert.AreEqual(feature.Properties.Value, 4.2);

Assert.AreEqual(feature.Id, "test-id");

Assert.AreEqual(feature.Geometry.Type, GeoJSONObjectType.Point);
}


[Test]
public void Can_Serialize_Typed_Point_Feature()
{
var geometry = new Point(new Position(1, 2));
var props = new TypedFeatureProps
{
Name = "no name here",
Value = 1.337
};
var feature = new Feature<Point, TypedFeatureProps>(geometry, props, "no id there");

var expectedJson = GetExpectedJson();
var actualJson = JsonConvert.SerializeObject(feature);

JsonAssert.AreEqual(expectedJson, actualJson);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "Feature",
"id": "test-id",
"geometry": {
"type": "Point",
"coordinates": [ 125.6, 10.1 ]
},
"properties": {
"name": "Dinagat Islands",
"value": 4.2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"geometry": {
"coordinates": [ 2.0, 1.0 ],
"type": "Point"
},
"properties": {
"name": "no name here",
"value": 1.337
},
"id": "no id there",
"type": "Feature"
}
6 changes: 3 additions & 3 deletions src/GeoJSON.Net.Tests/GeoJSON.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\GeoJSON.Net\GeoJSON.Net.csproj" />
<PackageReference Include="NUnit" Version="3.7.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-alpha1" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
</ItemGroup>
<ItemGroup>
Expand All @@ -23,5 +23,5 @@
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</ItemGroup>
</Project>
9 changes: 5 additions & 4 deletions src/GeoJSON.Net.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
<package id="NUnit" version="3.7.0" targetFramework="net461" />
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
<package id="NUnit" version="3.7.0" targetFramework="net461" />
<package id="NUnit3TestAdapter" version="3.8.0-alpha1" targetFramework="net45" />
</packages>
86 changes: 85 additions & 1 deletion src/GeoJSON.Net/Feature/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,90 @@

namespace GeoJSON.Net.Feature
{
/// <summary>
/// A GeoJSON Feature Object; generic version for strongly typed <see cref="Geometry"/>
/// and <see cref="Properties"/>
/// </summary>
/// <remarks>
/// See https://tools.ietf.org/html/rfc7946#section-3.2
/// </remarks>
public class Feature<TGeometry, TProps> : GeoJSONObject, IEquatable<Feature<TGeometry, TProps>>
where TGeometry : IGeometryObject
{
[JsonConstructor]
public Feature(TGeometry geometry, TProps properties, string id = null)
{
Geometry = geometry;
Properties = properties;
Id = id;

Type = GeoJSONObjectType.Feature;
}

[JsonProperty(PropertyName = "id", NullValueHandling = NullValueHandling.Ignore)]
public string Id { get; }

[JsonProperty(PropertyName = "geometry", Required = Required.AllowNull)]
[JsonConverter(typeof(GeometryConverter))]
public TGeometry Geometry { get; }

[JsonProperty(PropertyName = "properties", Required = Required.AllowNull)]
public TProps Properties { get;}

/// <summary>
/// Equality comparer.
/// </summary>
/// <remarks>
/// In contrast to <see cref="Feature.Equals(Feature)"/>, this implementation returns true only
/// if <see cref="Id"/> and <see cref="Properties"/> are also equal. See
/// <a href="https://github.com/GeoJSON-Net/GeoJSON.Net/issues/80">#80</a> for discussion. The rationale
/// here is that a user explicitly specifying the property type most probably cares about the properties
/// equality.
/// </remarks>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(Feature<TGeometry, TProps> other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return base.Equals(other)
&& string.Equals(Id, other.Id)
&& EqualityComparer<TGeometry>.Default.Equals(Geometry, other.Geometry)
&& EqualityComparer<TProps>.Default.Equals(Properties, other.Properties);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Feature<TGeometry, TProps>) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Id != null ? Id.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ EqualityComparer<TGeometry>.Default.GetHashCode(Geometry);
hashCode = (hashCode * 397) ^ EqualityComparer<TProps>.Default.GetHashCode(Properties);
return hashCode;
}
}

public static bool operator ==(Feature<TGeometry, TProps> left, Feature<TGeometry, TProps> right)
{
return object.Equals(left, right);
}

public static bool operator !=(Feature<TGeometry, TProps> left, Feature<TGeometry, TProps> right)
{
return !object.Equals(left, right);
}
}


/// <summary>
/// A GeoJSON Feature Object.
/// </summary>
Expand Down Expand Up @@ -156,7 +240,7 @@ public bool Equals(Feature<TGeometry> left, Feature<TGeometry> right)
{
return true;
}
if (ReferenceEquals(null, right))
if (ReferenceEquals(null, left))
{
return false;
}
Expand Down