Python GDAL library 01 to build Simple GIS

Python GDAL library 01 to build Simple GIS

import turtle as t

# myList[0]
# firstItem = 0
# myList[firstItem]

Define the name, coordinates and population of the city

NAME = 0
POINTS = 1
POP = 2

Create a state name (0), coordinates (1), population (3), coordinates should be nested in another list

state = ["COLORADO", [[-109, 37], [-109, 41], [-102, 41], [-102, 37]], 5187582]

Establish the city name, coordinates, and population of each city

cities = []
cities.append(["DENVER", [-104.98, 39.74], 634265])
cities.append(["BOULDER", [-105.27, 40.02], 98889])
cities.append(["DURANGO", [-107.88, 37.28], 17069])
The widening range of the world geographic coordinates, from the prime meridian, that is, the 0 ° meridian, is a negative "-" to the west, a positive "+" to the east, and a movement of 180 ° to the east or west is + -180 ° on the Pacific The meridian is the change of the world day. From the equator to the north pole, it is positive "+;" the south pole is negative "-", + 90 ° is the north pole, and -90 ° is the south pole.
minx = 180
maxx = -180
miny = 90
maxy = -90
Determine the geographic coordinate range of the state and the maximum size of the state. That is, according to the four points of the state, a rectangle is determined.
for x, y in state[POINTS]:
    if x < minx:
       minx = x
    elif x > maxx:
        maxx = x
    if y < miny:
        miny = y
    elif y > maxy:
        maxy = y
        

Define the size of a map drawing board, this size is based on the actual situation you need

map_width = 400
map_height = 300

Calculate the scale of the map drawing board and the maximum range of world geographic coordinates.

That is, the size of the map drawing board / the maximum size of the world geographic coordinates.

dist_x = maxx - minx   # 世界最地理坐标长度,也就是本初子午线向东西180°之和。
dist_y = maxy - miny   # 世界最大宽度,也就是北极点到南极点的地理坐标长度。

x_ratio = map_width / dist_x  # 地图绘图板的长度尺寸/世界最地理坐标长度
y_ratio = map_height / dist_y # 地图绘图板的高度,也就是宽度尺寸/世界最地理坐标宽度尺寸

Use the zoom ratio obtained in the previous step to convert the latitude and longitude coordinates to screen coordinates.

def convert(point):
  lon = point[0]
  lat = point[1]
  x = map_width - ((maxx - lon) * x_ratio)
  y = map_height - ((maxy - lat) * y_ratio)
  print(x,y)
  ## 将绘图图版沿x轴移动“-(map_width / 2)”,沿y轴移动-(map_height / 2),
  ##  从而将绘图图版放置于中心的位置
  x = x - (map_width / 2)
  y = y - (map_height / 2)
  return [x, y]

##wn = t.Screen()
##wn.title("Simple GIS")

Use turtle to draw map boundaries

t.up()
first_pixel = None
for point in state[POINTS]:
    pixel = convert(point)
    if not first_pixel:
           first_pixel = pixel    
    t.goto(pixel)
    t.down()
t.goto(first_pixel)
t.up()
t.goto([0, 0])
t.write(state[NAME], align = "center", font = ("Arial", 16, "bold"))

Map the distribution and attributes of cities

for city in cities:
    pixel = convert(city[POINTS])
    t.up()
    t.goto(pixel)
    t.dot(10)
    t.write(city[NAME] + ", Pop.:"  + str(city[POP]), align = "left")
    t.up()

Use the max and min functions to find the cities with the largest population and the furthest distance from the west

biggest_city = max(cities, key = lambda city: city[POP])
t.goto(0, -200)
t.write("The biggest city is :" + biggest_city[NAME])

western_city = min(cities, key = lambda city: city[POINTS])
t.goto(0, -220)
t.write("The western-most city is :" + western_city[NAME])


t.pen(shown = False)   # 隐藏turtle光标
t.done()

*note:

Use of lambda function *

Published 36 original articles · praised 0 · visits 620

Guess you like

Origin blog.csdn.net/Corollary/article/details/105478108