0

我使用 pyshp 库来检索形状的坐标。

sf = shapefile.Reader(r"{}".format(boundary_file))
shapes = sf.shapes()
fields = sf.fields
records = sf.records()
for record in records:
    if record['NAME'] in cities_list:
        city = record['NAME']
        s = sf.shape(record_id)
        geom = s.__geo_interface__
        geom_list = []
            for dict in geom:
                if dict == "coordinates":
                    coord_dict = geom[dict]
                    coords = coord_dict[0]
                        for pairs in coords:
                            geom_list.append(pairs)

然后我需要根据“geom”字典中的坐标创建一个字符串。我使用的功能:

def listToString(s):
    # initialize an empty string
    str1 = ""

    # traverse in the string
    for ele in s:
        str1 += "'{}',".format(ele)

    # return string
    return str1

我进一步处理字符串以删除除坐标之外的任何其他字符。我的问题是存在我无法删除的换行符。打印时print(listToString(geom_list )),我注意到在特定数量的字符之后有一个换行符在此处输入图像描述 这就是它在记事本++中的样子。

我想删除换行符并在一个字符串中打印坐标列表。

4

1 回答 1

0

尝试直接访问几何,而不是使用 geo_interface。

所以而不是:

geom = s.__geo_interface__

尝试:

geom = s.points

这将为您提供一个更易于操作的 python 元组列表。例如:

print(geom)

[(325596.92722995684, 1203960.6356796448), (325586.0020689714, 1203249.2743698577), (325583.88228647516, 1203111.9924421005), (325557.3214867797, 1203079.1636561346), (325492.1746068211, 1203066.062398915), (325493.67478956026, 1203251.0499563925), (325497.30834786664, 1203686.7895428804), (325500.8040477646, 1203877.5114580444)]

然后扩展您的几何列表:

geom_list.extend(geom)
于 2022-01-30T20:40:53.870 回答