-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d87fa78
commit 730d61e
Showing
7 changed files
with
52 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters