PyGIS—1.python地理空间分析指南第一章示例

PyGIS—1.python地理空间分析指南第一章示例

系列简介

本系列主要记录笔者在学习《python地理空间分析指南》一书中遇到的问题及收获。《python地理空间分析指南》电子书及其附带源码分享在:妙妙屋,提取码为:bzlx,以供有兴趣的朋友参考学习。

内容简介

本文主要记录《python地理空间分析指南》一书第一章的示例及本人的一点注记。

代码部分

"""SimpleGIS.py - A Simple GIS"""

import turtle as t

# DATA MODEL
# All layers will have a name, 1+ points, and population count
NAME = 0
POINTS = 1
POP = 2

# Create the state layer
state = ["COLORADO", [[-109, 37], [-109, 41], [-102, 41], [-102, 37]], 5187582]

# Cities layer list
# city = [name, [point], population]
cities = []

# Add Denver
cities.append(["DENVER", [-104.98, 39.74], 634265])
# Add Boulder
cities.append(["BOULDER", [-105.27, 40.02], 98889])
# Add Durango
cities.append(["DURANGO", [-107.88, 37.28], 17069])

# MAP GRAPHICS RENDERING
map_width = 800
map_height = 500

# State Bounding Box
# Use Python min/max function to get bounding box
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

# Get earth distance on each axis
dist_x = maxx - minx
dist_y = maxy - miny

# Scaling ratio each axis
# to map points from world to screen
x_ratio = map_width / dist_x
y_ratio = map_height / dist_y


def convert(point):
    """Convert lat/lon to screen coordinates"""
    lon = point[0]
    lat = point[1]
    x = map_width - ((maxx - lon) * x_ratio)
    # x = lon * x_ratio - minx * x_ratio
    y = map_height - ((maxy - lat) * y_ratio)
    # y = lat * y_ratio - miny * y_ratio
    # 相当于以左下角点为参考
    # Python turtle graphics start in the middle of the screen
    # so we must offset the points so they are centered
    x = x - (map_width/2)
    # x= (lon - (maxx - minx)/2)*x_ratio
    y = y - (map_height/2)
    # y= (lat - (maxy - miny)/2)*x_ratio
    # 结果为以最大值与最小值的中点为参考
    return [x, y]

# Add a title to the window
wn = t.Screen()
wn.title("Simple GIS")

# Draw the state
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()
# Go back to the first point
t.goto(first_pixel)
# Label the state
t.up()
t.goto([0, 0])
t.write(state[NAME], align="center", font=("Arial", 16, "bold"))

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

# Perform an attribute query
# Question: Which city has the largest population?
# Write the result but make sure it's under the map
biggest_city = max(cities, key=lambda city: city[POP])
t.goto(0, -1*((map_height/2)+20))
t.write("The highest-populated city is: " + biggest_city[NAME])

# Perform a spatial query
# Question: Which is the western most city?
# Write the result but make sure it's under the other question
western_city = min(cities, key=lambda city: city[POINTS])
t.goto(0, -1*((map_height/2)+40))
t.write("The western-most city is: " + western_city[NAME])

# Hide our map pen
t.pen(shown=False)
t.done()

注记

其中个人认为以下内容需要注意:

def convert(point):
    """Convert lat/lon to screen coordinates"""
    lon = point[0]
    lat = point[1]
    x = map_width - ((maxx - lon) * x_ratio)
    # x = lon * x_ratio - minx * x_ratio
    y = map_height - ((maxy - lat) * y_ratio)
    # y = lat * y_ratio - miny * y_ratio
    # 相当于以左下角点为参考
    # Python turtle graphics start in the middle of the screen
    # so we must offset the points so they are centered
    x = x - (map_width/2)
    # x= (lon - (maxx - minx)/2)*x_ratio
    y = y - (map_height/2)
    # y= (lat - (maxy - miny)/2)*x_ratio
    # 结果为以最大值与最小值的中点为参考
    return [x, y]

此函数实现将实际坐标转化为图上坐标,包括比例变化和参考点变化。

    x = map_width - ((maxx - lon) * x_ratio)
    # x = lon * x_ratio - minx * x_ratio
    y = map_height - ((maxy - lat) * y_ratio)
    # y = lat * y_ratio - miny * y_ratio
    # 相当于以左下角点为参考

上方代码中注释部分为原表达式变形后所得,可以看出此时参考原点为左下角点。

	x = x - (map_width/2)
    # x= (lon - *(maxx - minx)/2)*x_ratio
    y = y - (map_height/2)
    # y= (lat - *(maxy - miny)/2)*x_ratio
    # 结果为以最大值与最小值的中点为参考

上方代码中注释部分为原表达式变形后所得,可以看出此时参考原点变为为最大值与最小值的中点。

猜你喜欢

转载自blog.csdn.net/QBigBangQ/article/details/110175746