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

2024-10-03 19:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我曾尝试在3.7版本上安装Geopython,但Fiona/GDAL在安装过程中失败惨重。接下来,我尝试使用shape reader查询文件并直接绘制点

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

enter image description here

我的密码在这里

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

问题是,这可能是点顺序,也就是我的文件,还是我的方法错了

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


Tags: 文件inimporttxtforas绘制plt
1条回答
网友
1楼 · 发布于 2024-10-03 19:31:00

您必须考虑形状的部分,其中包含以下部分的每个部分/起始索引的结束索引。 以下是对我有效的方法:

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

enter image description here

相关问题 更多 >