json 如何从文本文件中读取python中编码为[[x,y],[x1,y1],....[xn,yn]]的多边形坐标

2izufjch  于 2022-12-15  发布在  Python
关注(0)|答案(2)|浏览(138)

我有一个文本文件coordinates.txt,里面有如下格式的数据。我想把每一行的坐标读入浮点数列表的python对象列表中。每个边界只有5个纬度和经度对。

{"id": "1b2e1366-9ac7-4220-8dc2-c333d3da6e42", "bounds": "[[-121.02662668533802, 38.39556098724271], [-121.01552400710878, 38.39556098724271], [-121.01552400710878, 38.38638931377034], [-121.02662668533802, 38.38638931377034], [-121.02662668533802, 38.39556098724271]]"}
{"id": "e2c02621-6685-435c-b1f0-07e0bca49870", "bounds": "[[-121.71606277115228, 38.424526411333495], [-121.70496009292305, 38.424526411333495], [-121.70496009292305, 38.41535473786112], [-121.71606277115228, 38.41535473786112], [-121.71606277115228, 38.424526411333495]]"}
{"id": "14d29a1f-e014-43fc-bfcf-b380126e8270", "bounds": "[[-120.64683157721372, 38.77860318996463], [-120.63572889898448, 38.77860318996463], [-120.63572889898448, 38.769431516492254], [-120.64683157721372, 38.769431516492254], [-120.64683157721372, 38.77860318996463]]"}

我使用下面的代码来读取键“bounds”的值:

with open(file = coordinates_file_path) as f:
        for line in f:
            coordinates = json.loads(line)['bounds']
            print(coordinates[0]) # this prints only the char `[`
            coordinates_data.append(coordinates)

我想要的:坐标为大小为5的列表,每个元素为大小为2的float列表,这样我就可以示例化一个Polygon()对象,如下所示:

>>> from geojson import Polygon

>>> Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38,   57.322), (2.39, 56.45)]])  
{"coordinates": [[[2.3..., 57.32...], [23.19..., -20.2...], [-120.4..., 19.1...], [2.3..., 57.3..]]], "type": "Polygon"}

但是对于我上面使用的代码,坐标只是一个字符串[[-121.02662668533802,38.39556098724271],[-121.01552400710878,38.39556098724271],[-121.01552400710878,38.38638931377034],[第一百二十一条第二款第二项第三款第三项第三款第三项第三款第三项第三款第三项第三款第三项第三款第四项],[第一百二十一条第二款第二项第三款第三项第三款第二项第三款第四项第三款第二项第三款第三项第四项第三款第二项第三款第四项第三款]]。

sxpgvts3

sxpgvts31#

使用python模块ast对我来说很有用

import ast
  
# initializing string representation of a list
ini_list = "[1, 2, 3, 4, 5]"
  
# printing initialized string of list and its type
print ("initial string", ini_list)
print (type(ini_list))
  
# Converting string to list
res = ast.literal_eval(ini_list)

输出:

initial string [1, 2, 3, 4, 5]
<class 'str'>
final list [1, 2, 3, 4, 5]
<class 'list'>
h79rfbju

h79rfbju2#

你可以试试这个:

import json

coordinates_file_path = "<filename>.txt"
with open(coordinates_file_path) as file:
    json_content = file.read().split("\n")
    
    for json_element in json_content:
        if json_element:
            json_converted = json.loads(json_element)
            bounds = json_converted["bounds"]

            # Remove the outer list brackets and split the 5 coordinates
            for coordinate in bounds.strip("[]").split("]"):
                # Remove inner list starting brackets and convert str to float
                coordinate = coordinate.strip("[, ").split(",")
                coordinate = list(map(float, coordinate))
                print(coordinate)

不确定这是否是最好的方法,但它可以完成工作。coordinate变量包含两个浮点数的列表,您可以使用它们来创建Polygon。希望这对您有所帮助!
输出:

[-121.02662668533802, 38.39556098724271]
[-121.01552400710878, 38.39556098724271]
[-121.01552400710878, 38.38638931377034]
[-121.02662668533802, 38.38638931377034]
[-121.02662668533802, 38.39556098724271]
[-121.71606277115228, 38.424526411333495]
[-121.70496009292305, 38.424526411333495]
[-121.70496009292305, 38.41535473786112]
[-121.71606277115228, 38.41535473786112]
[-121.71606277115228, 38.424526411333495]
[-120.64683157721372, 38.77860318996463]
[-120.63572889898448, 38.77860318996463]
[-120.63572889898448, 38.769431516492254]
[-120.64683157721372, 38.769431516492254]
[-120.64683157721372, 38.77860318996463]

相关问题