Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6d67a16

Browse files
committed
add generate svg country maps tutorial
1 parent9f5cfc1 commit6d67a16

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pycountry
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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')

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp