我使用 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 ))
,我注意到在特定数量的字符之后有一个换行符
这就是它在记事本++中的样子。
我想删除换行符并在一个字符串中打印坐标列表。