Skip to content

Instantly share code, notes, and snippets.

@attrition
Last active December 15, 2015 20:59
Show Gist options
  • Save attrition/5322320 to your computer and use it in GitHub Desktop.
Save attrition/5322320 to your computer and use it in GitHub Desktop.
Read in ascii topology data and extract a square chunk centred around a given latitude and longitude (code provides ottawa and montreal targets). Expects 30 arc-second map of canada.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AscMapSubset
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var canLat = 41.004166667f;
var canLon = -147.995833333f;
var cellSize = 0.008333333f;
// montreal
var monLat = 45.4987368f;
var monLon = -73.5907014f;
// ottawa
var ottLat = 45.4240305f;
var ottLon = -75.6954888f;
var area = new List<List<string>>();
var areaRadius = 5;
var y = (int)Math.Round(Math.Abs(monLat - canLat) / cellSize);
var x = (int)Math.Round(Math.Abs(monLon - canLon) / cellSize);
var height = 5040;
var startX = x - areaRadius;
var startY = height - (y - areaRadius);
var endX = x + areaRadius;
var endY = height - (y + areaRadius);
var lines = File.ReadAllLines(@"can3d30.asc")
.Where((line, lineNum) => lineNum > 5)
.ToList();
for (int i = startY; i >= endY; i--)
{
var line = lines[i];
var cols = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
area.Add(cols.Where((col, idx) => idx >= startX && idx <= endX).ToList());
}
var output = new List<string>();
var currLine = area.Count - 1;
for (int lonStep = startX; lonStep <= endX; lonStep++)
{
var col = 0;
for (int latStep = startY; latStep >= endY; latStep--)
{
var currLat = canLat + ((height - latStep) * cellSize);
var currLon = canLon + (lonStep * cellSize);
output.Add(currLat + " " + currLon + " " + area[currLine][col]);
col++;
}
currLine--;
}
File.WriteAllLines(@"output.asc", output);
}
}
}
@attrition
Copy link
Author

Expected 30 arc-send map of Canada provided here: can3d30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment