Skip to content

Commit

Permalink
Download data automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Sep 6, 2022
1 parent d87fa78 commit 730d61e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
Binary file removed data/combined-shapefile.dbf
Binary file not shown.
1 change: 0 additions & 1 deletion data/combined-shapefile.prj

This file was deleted.

Binary file removed data/combined-shapefile.shp
Binary file not shown.
Binary file removed data/combined-shapefile.shx
Binary file not shown.
2 changes: 0 additions & 2 deletions src/GeoTimeZone.DataBuilder/ConsoleOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public static void Stop()
Console.WriteLine("Finished at: {0}", GetLocalDisplayTime());
Console.WriteLine("Total elapsed time: {0:hh\\:mm\\:ss\\.fff}", Stopwatch.Elapsed);
Console.WriteLine();
Console.WriteLine("Hit any key to exit.");
Console.ReadKey();
}

public static void WriteMessage(string message)
Expand Down
61 changes: 52 additions & 9 deletions src/GeoTimeZone.DataBuilder/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,66 @@
using System.IO.Compression;
using System.Net.Http;
using System.Reflection;

namespace GeoTimeZone.DataBuilder;

internal static class Program
{
private static void Main()
private static async Task Main()
{
ConsoleOutput.Start();

var projectPath = Path.GetFullPath(".");
while (!File.Exists(Path.Combine(projectPath, "GeoTimeZone.sln")))
{
projectPath = Path.GetFullPath(Path.Combine(projectPath, ".."));
}
ConsoleOutput.WriteMessage("Downloading Time Zone Boundaries Shapefile");
var tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()[..8]);
var filePath = await DownloadDataAsync("2021c", tempDir);

var shapeFile = Path.Combine(projectPath, "data", "combined-shapefile.shp");
var tzShapeReader = new TimeZoneShapeFileReader(shapeFile);
ConsoleOutput.WriteMessage("Extracting contents...");
ZipFile.ExtractToDirectory(filePath, tempDir);

var outputPath = Path.Combine(projectPath, "src", "GeoTimeZone");
ConsoleOutput.WriteMessage("Processing data...");
var shapeFile = Path.Combine(tempDir, "combined-shapefile.shp");
var outputPath = Path.Combine(GetSolutionDir(), "src", "GeoTimeZone");
var tzShapeReader = new TimeZoneShapeFileReader(shapeFile);
TimeZoneDataBuilder.CreateGeohashData(tzShapeReader, outputPath);

ConsoleOutput.WriteMessage("Done!");
ConsoleOutput.Stop();
}

private static async Task<string> DownloadDataAsync(string version, string path)
{
var url = $"https://github.com/evansiroky/timezone-boundary-builder/releases/download/{version}/timezones.shapefile.zip";

if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

var filename = url[(url.LastIndexOf('/') + 1)..];
var httpClient = new HttpClient();
using var result = await httpClient.GetAsync(url);
if (!result.IsSuccessStatusCode)
{
ConsoleOutput.WriteMessage($"Error downloading data file: {result.StatusCode}");
Environment.Exit(1);
return null;
}

var filePath = Path.Combine(path, filename);
await using var fs = File.Create(filePath);
await result.Content.CopyToAsync(fs);

return filePath;
}

private static string GetSolutionDir()
{
var solutionDir = Assembly.GetExecutingAssembly().Location;
while (!File.Exists(Path.Combine(solutionDir, "GeoTimeZone.sln")))
{
solutionDir = Path.GetFullPath(Path.Combine(solutionDir, ".."));
}

return solutionDir;
}
}
2 changes: 0 additions & 2 deletions src/GeoTimeZone.DataBuilder/TimeZoneDataBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ public static void CreateGeohashData(TimeZoneShapeFileReader inputShapefile, str

WriteLookup(outputPath);
ConsoleOutput.WriteMessage("Lookup file written.");

ConsoleOutput.WriteMessage("Done!");
}

private static void PreLoadTimeZones(IEnumerable<TimeZoneFeature> features)
Expand Down

0 comments on commit 730d61e

Please sign in to comment.