|
| 1 | +# Default Library |
| 2 | +importrequests |
| 3 | +importjson |
| 4 | +importos |
| 5 | + |
| 6 | +# Download with pip install pycountry |
| 7 | +importpycountry |
| 8 | + |
| 9 | +forcountryinlist(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 | +ifos.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 | +exceptjson.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 | +foriindata['features'][0]['geometry']['coordinates']: |
| 43 | +forgroupini: |
| 44 | +pointGroups.append(group) |
| 45 | +forcoordingroup: |
| 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 | +forx,yinallPoints: |
| 60 | +lowestX=xifx<lowestXelselowestX |
| 61 | +highestX=xifx>highestXelsehighestX |
| 62 | + |
| 63 | +lowestY=yify<lowestYelselowestY |
| 64 | +highestY=yify>highestYelsehighestY |
| 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 | +forgroupinpointGroups: |
| 78 | +coordinateString='' |
| 79 | +forx,yingroup: |
| 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 | +ifnotos.path.isdir("output"): |
| 95 | +os.mkdir("output") |
| 96 | +# write the svg file |
| 97 | +withopen(f'output/{countryName}.svg','w')asf: |
| 98 | +f.write(svgContent) |
| 99 | +# new line |
| 100 | +print('\n') |