|
| 1 | +# Default Library |
| 2 | +import requests |
| 3 | +import json |
| 4 | +import os |
| 5 | + |
| 6 | +# Download with pip install pycountry |
| 7 | +import pycountry |
| 8 | + |
| 9 | +for country in list(pycountry.countries): |
| 10 | + |
| 11 | + # All Points from all Groups |
| 12 | + # used to analyze |
| 13 | + allPoints = [] |
| 14 | + |
| 15 | + # Countries that dont consist of one body |
| 16 | + # will have multiple groups of coordinates |
| 17 | + pointGroups = [] |
| 18 | + |
| 19 | + # Country Code with 3 letters |
| 20 | + countryCode = country.alpha_3 |
| 21 | + countryName = country.name |
| 22 | + |
| 23 | + # Check if the SVG file already Exists and skip if it does |
| 24 | + if os.path.exists(f'output/{countryName}.svg'): |
| 25 | + print(f'{countryName}.svg Already exists ... Skipping to next Country\n') |
| 26 | + continue |
| 27 | + |
| 28 | + print('Generating Map for: ', countryName) |
| 29 | + |
| 30 | + # Get the Data |
| 31 | + re = requests.get(f'https://geodata.ucdavis.edu/gadm/gadm4.1/json/gadm41_{countryCode}_0.json') |
| 32 | + |
| 33 | + # If the string cant be parsed an invalid country was requested |
| 34 | + try: |
| 35 | + data = json.loads(re.text) |
| 36 | + except json.decoder.JSONDecodeError: |
| 37 | + print('Could not decode ... Skipping to next Country\n') |
| 38 | + continue |
| 39 | + |
| 40 | + # Organise the Data |
| 41 | + # Get the groups and all coordinates |
| 42 | + for i in data['features'][0]['geometry']['coordinates']: |
| 43 | + for group in i: |
| 44 | + pointGroups.append(group) |
| 45 | + for coord in group: |
| 46 | + allPoints.append(coord) |
| 47 | + |
| 48 | + print(f'\n{len(allPoints)} Points') |
| 49 | + |
| 50 | + # Analyse Data |
| 51 | + # Use these Information to calculate |
| 52 | + # offset, height and width of the Country |
| 53 | + lowestX = 9999999999 |
| 54 | + highestX = -9999999999 |
| 55 | + |
| 56 | + lowestY = 9999999999 |
| 57 | + highestY = -9999999999 |
| 58 | + |
| 59 | + for x, y in allPoints: |
| 60 | + lowestX = x if x < lowestX else lowestX |
| 61 | + highestX = x if x > highestX else highestX |
| 62 | + |
| 63 | + lowestY = y if y < lowestY else lowestY |
| 64 | + highestY = y if y > highestY else highestY |
| 65 | + |
| 66 | + print('lowestX', lowestX) |
| 67 | + print('highestX', highestX) |
| 68 | + |
| 69 | + print('lowestY', lowestY) |
| 70 | + print('highestY', highestY) |
| 71 | + |
| 72 | + svgWidth = (highestX - lowestX) |
| 73 | + svgHeight = (highestY - lowestY) |
| 74 | + |
| 75 | + # Transfrom Points to Polygon Strings |
| 76 | + polygonString = '' |
| 77 | + for group in pointGroups: |
| 78 | + coordinateString = '' |
| 79 | + for x, y in group: |
| 80 | + x = (x - lowestX) |
| 81 | + y = (y - lowestY) |
| 82 | + |
| 83 | + coordinateString = coordinateString + f'{x},{y} ' |
| 84 | + |
| 85 | + polygonString += f'<polygon points="{coordinateString}"></polygon>' |
| 86 | + |
| 87 | + svgContent = f""" |
| 88 | + <svg width="{svgWidth}" height="{svgHeight}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="transform: scale(1, -1)"> |
| 89 | + {polygonString} |
| 90 | + </svg> |
| 91 | + """ |
| 92 | + |
| 93 | + # make the output folder |
| 94 | + if not os.path.isdir("output"): |
| 95 | + os.mkdir("output") |
| 96 | + # write the svg file |
| 97 | + with open(f'output/{countryName}.svg', 'w') as f: |
| 98 | + f.write(svgContent) |
| 99 | + # new line |
| 100 | + print('\n') |
0 commit comments