Skip to content

Instantly share code, notes, and snippets.

@justintoth
Created June 24, 2020 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justintoth/c3c375012272ac5533e0f77eae41d734 to your computer and use it in GitHub Desktop.
Save justintoth/c3c375012272ac5533e0f77eae41d734 to your computer and use it in GitHub Desktop.
Import MapBox Tiles
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Newtonsoft.Json;
using Rpr.Common;
namespace Rpr.ImportMapBoxTiles
{
public static class Program
{
private static string mapBoxDomain;
private static string mapBoxUsername;
private static string mapBoxAccessToken;
private static string geoJsonFilePath;
static void Main(string[] args)
{
try
{
Console.WriteLine("Step 1 of 5: Reading job configuration...");
CommonCore.Startup.ConfigureToRunInNonHostedEnvironment();
mapBoxDomain = RprConfigurationManager.GetAppSetting("MapBox:Domain");
mapBoxUsername = RprConfigurationManager.GetAppSetting("MapBox:Username");
mapBoxAccessToken = RprConfigurationManager.GetAppSetting("MapBox:AccessToken");
geoJsonFilePath = RprConfigurationManager.GetAppSetting("GeoJsonFilePath");
Console.WriteLine($"Job configuration read, will use geo json file at path {geoJsonFilePath}");
// Get AWS credentials...
Console.WriteLine("Step 2 of 5: Getting AWS credentials...");
var awsCredentials = GetAWSCredentials();
Console.WriteLine($"Retrieved AWS credentials with session token {awsCredentials.SessionToken}...");
// Upload geo json file to AWS S3...
Console.WriteLine($"Step 3 of 5: Uploading geo json file {geoJsonFilePath} to AWS S3 at url {awsCredentials.Url}");
var putObjectResponse = UploadFileToS3(awsCredentials, geoJsonFilePath);
Console.WriteLine("Uploaded geo json file to AWS S3 successfully!");
// Create a MapBox upload...
Console.WriteLine($"Step 4 of 5: Creating a MapBox upload for AWS S3 url {awsCredentials.Url}...");
var upload = CreateUpload(awsCredentials);
Console.WriteLine($"Created a MapBox upload with id {upload.Id}!");
// Check status of MapBox upload...
var maxStatusChecks = 100;
for (var k = 0; k < maxStatusChecks; k++)
{
Thread.Sleep(1000);
Console.WriteLine($"Step 5 of 5: Checking status of MapBox upload with id {upload.Id}...");
upload = GetUploadStatus(upload);
if (upload.Complete)
{
Console.WriteLine("Upload completed successfully!");
break;
}
if (!String.IsNullOrEmpty(upload.Error))
{
Console.Error.WriteLine($"Upload failed! {upload.Error}");
break;
}
Console.WriteLine($"{upload.Progress * 100.0}% complete with MapBox upload.");
}
}
catch (Exception exc)
{
Console.Error.WriteLine($"Error running job: {exc.ToString()}");
Console.ReadLine();
}
}
private static AWSCredentials GetAWSCredentials()
{
using (var client = new WebClient())
{
var url = $"{mapBoxDomain}/uploads/v1/{mapBoxUsername}/credentials?access_token={mapBoxAccessToken}";
var responseJson = client.DownloadString(url);
return JsonConvert.DeserializeObject<AWSCredentials>(responseJson);
}
}
private static PutObjectResponse UploadFileToS3(AWSCredentials awsCredentials, string geoJsonFilePath)
{
var s3Config = new AmazonS3Config
{
ServiceURL = awsCredentials.Url,
UseHttp = false
};
using (var s3Client = new AmazonS3Client(awsCredentials.AccessKeyId, awsCredentials.SecretAccessKey, awsCredentials.SessionToken, s3Config))
{
using (var fileStream = File.OpenRead(geoJsonFilePath))
{
var s3Request = new PutObjectRequest
{
BucketName = awsCredentials.Bucket,
Key = awsCredentials.Key,
InputStream = fileStream
};
return s3Client.PutObjectAsync(s3Request).ResultInAnyContext();
}
}
}
private static CreateUploadResponse CreateUpload(AWSCredentials awsCredentials)
{
using (var client = new WebClient())
{
var url = $"{mapBoxDomain}/uploads/v1/{mapBoxUsername}";
var request = new CreateUploadRequest
{
Tileset = "janinesieja.censustractswinew",
Url = awsCredentials.Url,
Name = "CensusTractsWINew",
Private = false
};
var requestJson = JsonConvert.SerializeObject(request);
var requestBytes = Encoding.ASCII.GetBytes(requestJson);
var responseBytes = client.UploadData(url, requestBytes);
var responseJson = Encoding.ASCII.GetString(responseBytes);
return JsonConvert.DeserializeObject<CreateUploadResponse>(responseJson);
}
}
private static CreateUploadResponse GetUploadStatus(CreateUploadResponse upload)
{
using (var client = new WebClient())
{
var url = $"{mapBoxDomain}/uploads/v1/{mapBoxUsername}/{upload.Id}";
var responseJson = client.DownloadString(url);
return JsonConvert.DeserializeObject<CreateUploadResponse>(responseJson);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment