Skip to content

Commit

Permalink
fixed issue itinero#33: distances in export are written in exponentia…
Browse files Browse the repository at this point in the history
…l notation instead of regular notation
  • Loading branch information
Ranko Orlic committed Feb 26, 2016
1 parent 0a028e4 commit 233f315
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions GTFS.Test/GTFS.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="GTFSAssert.cs" />
<Compile Include="GTFSFeedTests.cs" />
<Compile Include="FileNotDisposedTest.cs" />
<Compile Include="WriteShapesTests.cs" />
<Compile Include="ParseFeedTests.cs" />
<Compile Include="ParseAgenciesTests.cs" />
<Compile Include="ParseRoutesTests.cs" />
Expand Down
83 changes: 83 additions & 0 deletions GTFS.Test/WriteShapesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// The MIT License (MIT)

// Copyright (c) 2014 Ben Abelshausen

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using GTFS.IO;
using GTFS.IO.CSV;
using NUnit.Framework;
using System.IO;
using System.Linq;
using GTFS.Entities;

namespace GTFS.Test
{
/// <summary>
/// Contains tests for writing shape files.
/// </summary>
[TestFixture]
public class WriteShapeTests
{
/// <summary>
/// Tests writing shape files.
/// </summary>
[Test]
public void WriteShapeFilesDoesNotUseExponentialNotation()
{
const string ShapesFile = "shapes.txt";

var feed = new GTFSFeed();
var shape = new Shape { Id = "something", Sequence = 1, Latitude = 1e-3, Longitude = -1e-5, DistanceTravelled = 1e-7 };
feed.Shapes.Add(shape);

using (var stream = File.OpenWrite(ShapesFile))
{
var writer = new GTFSWriter<GTFSFeed>();
var targetFile = new GTFSTargetFileStream(stream, "shapes");
writer.Write(feed, new IGTFSTargetFile[] { targetFile });
}

var bytes = File.ReadAllBytes(ShapesFile);

using (var reader = new CSVStreamReader(new MemoryStream(bytes)))
{
reader.MoveNext();
var headerLine = reader.Current;
var indexedColumns = headerLine.Select((col, i) => new { col, i }).ToDictionary(c => c.col, c => c.i);

reader.MoveNext();
var shapeLine = reader.Current;

var latitude = shapeLine[indexedColumns["shape_pt_lat"]];
var longitude = shapeLine[indexedColumns["shape_pt_lon"]];
var distance = shapeLine[indexedColumns["shape_dist_traveled"]];

var expected = new[]
{
"0.001000000",
"-0.000010000",
"0.000000100"
};
var actual = new[] { latitude, longitude, distance };
CollectionAssert.AreEquivalent(expected, actual);
}
}
}
}
6 changes: 1 addition & 5 deletions GTFS/GTFSWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -808,11 +808,7 @@ private string WriteFieldRouteType(string name, string fieldName, RouteTypeExten
/// <returns></returns>
private string WriteFieldDouble(string name, string fieldName, double? value)
{
if(value.HasValue)
{
return value.Value.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
return string.Empty;
return value.HasValue ? value.Value.ToString("F9", System.Globalization.CultureInfo.InvariantCulture) : string.Empty;
}

/// <summary>
Expand Down

0 comments on commit 233f315

Please sign in to comment.