Created
November 1, 2015 11:55
-
-
Save bastibe/edba77f6d6d87b263f57 to your computer and use it in GitHub Desktop.
Scripts for automating G2XPL scenery downloads
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
C:\Users\Bastian\Miniconda3\python.exe download.py "poly files/na_ca_manitoba.poly" | |
C:\Users\Bastian\Miniconda3\python.exe download.py "poly files/na_ca_quebec.poly" | |
C:\Users\Bastian\Miniconda3\python.exe download.py "poly files/na_ca_newfoundland-and-labrador.poly" | |
pause |
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
import sys | |
import re | |
import os.path | |
from math import ceil, floor | |
import numpy as np | |
from matplotlib.path import Path | |
def main(filename): | |
# read polygon file | |
polygon = [] | |
for line in open(filename): | |
if len(line.split()) != 2: | |
# line does not contain coordinates | |
continue | |
# convert line into two floating point numbers | |
polygon.append([float(f) for f in line.split()]) | |
polygon = np.array(polygon) | |
polygon_path = Path(polygon) | |
# calculate all lat/lon centers inside the polygon. This tries all lat/lon | |
# combinations in a rectangle superset of the polygon coordinates. | |
coordinates = [] | |
for lat in np.arange(floor(min(polygon[:,0]))-0.5, | |
ceil(max(polygon[:,0]))+1.5): | |
for lon in np.arange(floor(min(polygon[:,1]))-0.5, | |
ceil(max(polygon[:,1]))+1.5): | |
if polygon_path.contains_point((lat, lon)): | |
coordinates.append((lon, lat)) | |
print("\n### polygon file covers", len(coordinates), "tiles. ###\n") | |
# exit program if the polygon file did not cover any tiles | |
if len(coordinates) == 0: | |
return | |
# the new scenery name will be "z_G2xpl_<polyfilename>" | |
scenery_name = 'z_G2xpl_' + os.path.splitext(os.path.basename(filename))[0] | |
# download the tiles using g2xpl | |
for idx, (lat, long) in enumerate(coordinates): | |
print("\n### downloading tile", idx+1, "of", len(coordinates), "###\n") | |
# read the g2xpl configuration file | |
with open('g2xpl.ini') as f: | |
ini = f.read() | |
# change lat/lon, and scenery name in configuration | |
ini = re.sub('plane_long\s*=\s*[-+0-9.]+', | |
'plane_long='+str(long), ini) | |
ini = re.sub('plane_lat\s*=\s*[-+0-9.]+', | |
'plane_lat='+str(lat), ini) | |
ini = re.sub('scenery_name\s*=\s*.+', | |
'scenery_name='+scenery_name, ini) | |
# write changed g2xpl configuration file | |
with open('g2xpl.ini', 'w') as f: | |
f.write(ini) | |
# run g2xpl (it will pick up the changes in the configuration file) | |
os.system('g2xpl.exe') | |
print("\n### all downloads done. ###\n") | |
if __name__ == "__main__": | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment