Skip to content

Commit 6d67a16

Browse files
committed
add generate svg country maps tutorial
1 parent 9f5cfc1 commit 6d67a16

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
118118
- [How to Make a Text Adventure Game in Python](https://www.thepythoncode.com/article/make-a-text-adventure-game-with-python). ([code](general/text-adventure-game))
119119
- [Zipf's Word Frequency Plot with Python](https://www.thepythoncode.com/article/plot-zipfs-law-using-matplotlib-python). ([code](general/zipf-curve))
120120
- [How to Plot Weather Temperature in Python](https://www.thepythoncode.com/article/interactive-weather-plot-with-matplotlib-and-requests). ([code](general/interactive-weather-plot/))
121+
- [How to Generate SVG Country Maps in Python](https://www.thepythoncode.com/article/generate-svg-country-maps-python). ([code](general/generate-svg-country-map))
121122

122123

123124

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Generate SVG Country Maps in Python](https://www.thepythoncode.com/article/generate-svg-country-maps-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pycountry
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)