试图在 python 中绘制英国的地理形状文件

Trying to plot a geographic shape file of the UK in python

我尝试在 3.7 上安装 Geopython,但 Fiona/GDAL 安装失败。接下来,我刚刚尝试查询形状为 reader 的文件并直接绘制点。

虽然我在 gis.Whosebug 上的示例取得了一些成功(归功于 user681),但我使用了来自英国政府网站的 shapefile。当我绘制边界时,我得到了可怕的点相关性。

我的代码在这里。

import matplotlib.pyplot as plt
import numpy as np
import shapefile   

ukmap = shapefile.Reader("./Archive/UK_map.shp")

txt_shapes = []
for ukmapshape in ukmap.shapes(): 
    listx=[]
    listy=[]   
    for x,y in ukmapshape.points:
        listx.append(x)
        listy.append(y)
    txt_shapes.append([listx,listy])

for zone in txt_shapes:
    x,y = zone
    plt.plot(x,y)
plt.axis('equal')
plt.show()

问题是,这可能是点顺序,a.k.a。我的文件或我的方法有误吗?

http://geoportal.statistics.gov.uk/datasets/nuts-level-1-january-2018-ultra-generalised-clipped-boundaries-in-the-united-kingdom

您必须考虑形状的各个部分,其中包含以下部分的每个 part/start 索引的结束索引。 这是对我有用的:

import matplotlib.pyplot as plt
import numpy as np
import shapefile   

ukmap = shapefile.Reader("./Archive/UK_map.shp")

txt_shapes = []
for ukmapshape in ukmap.shapeRecords(): 
    listx=[]
    listy=[]
    # parts contains end index of each shape part
    parts_endidx = ukmapshape.shape.parts.tolist()
    parts_endidx.append(len(ukmapshape.shape.points) - 1)
    for i in range(len(ukmapshape.shape.points)):
        x, y = ukmapshape.shape.points[i]
        if i in parts_endidx:
            # we reached end of part/start new part
            txt_shapes.append([listx,listy])
            listx = [x]
            listy = [y]
        else:
            # not end of part
            listx.append(x)
            listy.append(y)

for zone in txt_shapes:
    x,y = zone
    plt.plot(x,y)
plt.axis('equal')
plt.show()