Python GDAL库 01构建Simple GIS

Python GDAL库 01构建Simple GIS

import turtle as t

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

定义城市的名称、坐标、人口数

NAME = 0
POINTS = 1
POP = 2

建立含有州名(0),坐标(1),人口数(3),坐标应嵌套在另外的列表里

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

建立各城市的城市名,坐标,人口数

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])
世界地理坐标的做大范围,自本初子午线即0°经线向西为负“-”,向东为正“+”,向东或向西移动180°,为位于太平洋上的 +-180° 经线上,即为世界日变更线。自赤道向北极为正“+;”向南极为负“-”,+90°为北极点,-90°为南极点。
minx = 180
maxx = -180
miny = 90
maxy = -90
确定州的地理坐标范围,州的最大尺寸。也就是按州的四至点,确定出一个矩形。
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
        

定义一个地图绘图板的尺寸,这个尺寸根据自己需要的实际情况制定

map_width = 400
map_height = 300

计算出地图绘图板的尺寸,与世界地理坐标最大范围的缩放比。

也就是将地图绘图板的尺寸/世界地理坐标的最大尺寸。

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

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

利用上一步得到的缩放比,将经纬度坐标转为屏幕坐标。

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")

运用turtle 绘地图边界

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"))

绘制城市的分布和属性

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()

利用max和min函数找出人口最多以及距离西部最远的城市

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()

*注意:

lambda 函数的使用*

发布了36 篇原创文章 · 获赞 0 · 访问量 620

猜你喜欢

转载自blog.csdn.net/Corollary/article/details/105478108
GIS