-1

当前输出的图像。我希望这将折线显示为每个对象在过去帧中的先前数据点。我正在使用库的 gmplot 和 geolib.Geodesic 来尝试跟踪 LIDAR 有效负载中的对象。目标是在有效载荷的每次迭代中获得一个输出,以获得一个谷歌地图图,该图显示有效载荷中的每个对象,其中最近的位置标有对象的 ID,并有一个标记显示它在纬度和经度坐标中的位置. 在另一个文件中,我有一个算法,它使用 LIDAR 中的 x 和 y 坐标来确定对象的正确纬度和经度。使标记正确是有效的。

我们在 JSON 文件中拥有所有数据,其中我们列出了纬度、经度和对象 ID,并按帧进行索引。使用嵌套字典来保存这些数据,从而使代码中的函数更易于阅读。所有函数都放在代码底部的函数中,称为 live_graph_coordinates。

当我尝试在对象的所有先前数据点后面跟踪折线时,问题就出现了。我使用字典来跟踪所有先前的纬度和经度点的 ID 值,但我无法正确显示这些点以及拆分为多条折线。有没有人有这个图书馆的经验或认识任何可以解决这个问题的人?

# Imports all modules needed for the code
import json
import gmplot
from geographiclib.geodesic import Geodesic
import gmaps
from math import atan2, degrees, sqrt
from pyproj import Proj, transform
import os
geod = Geodesic.WGS84

def parse_json(filename):
    # returns list of json objects
    # list to hold every string version of each JSON object in separate list within the larger list
    split_list = [[]]
    # for iterating which JSON object the string is currently being added to
    i = 0
    # opens JSON file with name file
    with open(filename, "r") as file:
        for line in file.readlines():
            # not the end of the JSON object
            if "}," in line:
                split_list[i].append(line)
                continue
            # is the end of the JSON object
            elif "}" in line:
                # adds the last line of the object to the string
                split_list[i].append(line)
                # goes to the next list
                i += 1
                split_list.append([])
                continue
            # just another part of the string
            else:
                split_list[i].append(line)
    # list of strings that're each JSON objects
    json_string_list = []
    # iterates over each list from above
    for i in split_list:
        # creates empty string to convert each to
        json_string = ""
        for j in i:
            json_string += j
        # appends the string once it's fully made to the list of JSON objects
        json_string_list.append(json_string)
    return json_string_list[-2:]


def get_JSON_dict(json_string_list, previous_lat_lng):
    # returns x, y lists
    json_string_list = json_string_list[0].split("}],")
    # adds the }]} back to the the string in each list
    for index, stripped_string in enumerate(json_string_list):
        add_back = stripped_string + "}]}"
        # adds the string back into the list
        json_string_list[index] = add_back
    # takes out the empty string at index -
    json_string_list.pop(-1)
    # iterates over the entire JSON string object
    for s in json_string_list:
        # loads the JSON string as a JSON object
        if "{" != s[0]:
            d = "{" + s
            json_s = json.loads(d)
        else:
            json_s = json.loads(s)
        # finds the frames as keys and iterates over them
        for key in json_s.keys():
            # iterates over the list within the keys variable
            for index, obj in enumerate(json_s[key]):
                # splits the string list into an actual list
                latitude = obj["latitude"]
                longitude = obj["longitude"]
                # checks whether or not the ID is already in the dictionary
                # if not then it doesn't add it to the dictionary again
                if obj["id"] in previous_lat_lng:
                    # ID is already in the dictionary (doesn't need to be added)
                    pass
                else:
                    # sets it equal to an empty list to hold latitude and longitude values respectively
                    previous_lat_lng[obj["id"]] = []
                # changes the dictionary to have floats instead of string values for the positions
                json_s[key][index]["latitude"] = latitude
                json_s[key][index]["longitude"] = longitude
    # returns the JSON dictionary
    return json_s, previous_lat_lng


def update_dict(lat, lon, previous_lat_lng, id):
    # checks whether or not the ID is already within the dictionary
    if id in previous_lat_lng.keys():
        # appends the current coordinates to the dictionary
        previous_lat_lng[id].append([lat, lon])
    else:
        # creates a 2D list with the new coordinates in the dictionary
        previous_lat_lng[id] = [[lat, lon]]
    return previous_lat_lng


def graph_data(json_dictionary, cam_lat, cam_lng, frame, previous_lat_lng, color="red", color2="cornflowerblue", edge_width=2.5):
    # creates the map plotter
    gmap3 = gmplot.GoogleMapPlotter(cam_lat, cam_lng, 20)
    for i in range(len(json_dictionary["Frame " + str(frame)])):
        # gets the current latitude and longitude
        latitude = float(json_dictionary["Frame " + str(frame)][i]["latitude"])
        longitude = float(json_dictionary["Frame " + str(frame)][i]["longitude"])
        # finds the current id to put into the previous_lat_lng dictionary
        current_id = json_dictionary["Frame " + str(frame)][i]["id"]
        # updates the previous_lat_lng dictionary to be able to be used in the graphing function
        previous_lat_lng = update_dict(latitude, longitude, previous_lat_lng, current_id)

    for id in previous_lat_lng.keys():
        # creates a list to be plotted on the map
        lat_list, lng_list = [], []
        for l in previous_lat_lng[id]: # l is a list of coordinates [latitude, longitude]
            # adds the latitude coordinate
            lat_list.append(l[0])
            # adds the longitude coordinate
            lng_list.append(l[1])
    # plots the line of where the person traveled
    gmap3.plot(lat_list, lng_list, color, edge_width, title=id)
    gmap3.marker(lat_list[-1], lng_list[-1], color2, title=id)

    # changes the directory to save the map to a separate folder within the project folder
    os.chdir("maps")
    # draws the google map and saves it to the map folder
    gmap3.draw("map" + str(frame) + ".html")
    # changes the directory back 1 folder to prevent errors
    os.chdir("..")
    # returns the previous dictionary in order to keep the previous data stored
    return previous_lat_lng


def live_graph_coordinates(latitude, longitude, json_file, frame, previous_lat_lng):
    # Uses api key to be able to make the coordinates
    gmaps.configure(**api key is here**)

    # shows the maps in satellite vision
    gmaps.figure('SATELLITE')

    # parses the json file to get a list of strings which are each a JSON object
    json_string_list = parse_json(json_file)

    # gets a list of x and y values for every object in the list
    json_dictionary, previous_lat_lng = get_JSON_dict(json_string_list, previous_lat_lng)

    if json_dictionary != None:
        # Graphs all the data points from the JSON file
        previous_lat_lng = graph_data(json_dictionary, latitude, longitude, frame, previous_lat_lng)
        return previous_lat_lng
    else:
        # no objects detected
        pass
{"Frame 1": [{"id": 533, "classification": "PERSON", "latitude": 33.48799308670221, "longitude": -111.90885153944437}, {"id": 549, "classification": "UNKNOWN", "latitude": 33.48801292738194, "longitude": -111.90885868765811}, {"id": 673, "classification": "PERSON", "latitude": 33.48804833314041, "longitude": -111.90882281530168}, {"id": 680, "classification": "UNKNOWN", "latitude": 33.4880037031338, "longitude": -111.90886720421587}, {"id": 682, "classification": "UNKNOWN", "latitude": 33.48801928916118, "longitude": -111.90877697851846}, {"id": 686, "classification": "PERSON", "latitude": 33.48804053863665, "longitude": -111.90881977666776}, {"id": 687, "classification": "PERSON", "latitude": 33.4880341240495, "longitude": -111.90881708982506}, {"id": 691, "classification": "PERSON", "latitude": 33.48803122508181, "longitude": -111.9088324527267}, {"id": 693, "classification": "UNKNOWN", "latitude": 33.48804587402998, "longitude": -111.90883551218909}, {"id": 694, "classification": "PERSON", "latitude": 33.48803580008242, "longitude": -111.90882961435683}, {"id": 696, "classification": "PERSON", "latitude": 33.48802004624938, "longitude": -111.90881496775653}, {"id": 697, "classification": "UNKNOWN", "latitude": 33.48802187886198, "longitude": -111.90882847417957}, {"id": 698, "classification": "PERSON", "latitude": 33.48801766538075, "longitude": -111.90882289307378}, {"id": 706, "classification": "UNKNOWN", "latitude": 33.4881289113697, "longitude": -111.908896469379}, {"id": 707, "classification": "PERSON", "latitude": 33.48806293756819, "longitude": -111.90880008535265}, {"id": 710, "classification": "UNKNOWN", "latitude": 33.4880314185434, "longitude": -111.90878311481961}, {"id": 711, "classification": "UNKNOWN", "latitude": 33.48804442683758, "longitude": -111.90879309453386}, {"id": 714, "classification": "PERSON", "latitude": 33.488061420726034, "longitude": -111.90879962024852}, {"id": 715, "classification": "PERSON", "latitude": 33.488055189955865, "longitude": -111.90879724325653}], 
"Frame 2": [{"id": 533, "classification": "PERSON", "latitude": 33.48799308670221, "longitude": -111.90885153944437}, {"id": 549, "classification": "UNKNOWN", "latitude": 33.48801292738194, "longitude": -111.90885868765811}, {"id": 673, "classification": "PERSON", "latitude": 33.48804833314041, "longitude": -111.90882281530168}, {"id": 680, "classification": "UNKNOWN", "latitude": 33.4880037031338, "longitude": -111.90886720421587}, {"id": 682, "classification": "UNKNOWN", "latitude": 33.48801928916118, "longitude": -111.90877697851846}, {"id": 686, "classification": "PERSON", "latitude": 33.48804053863665, "longitude": -111.90881977666776}, {"id": 687, "classification": "PERSON", "latitude": 33.4880341240495, "longitude": -111.90881708982506}, {"id": 691, "classification": "PERSON", "latitude": 33.48803122508181, "longitude": -111.9088324527267}, {"id": 693, "classification": "UNKNOWN", "latitude": 33.48804587402998, "longitude": -111.90883551218909}, {"id": 694, "classification": "PERSON", "latitude": 33.48803580008242, "longitude": -111.90882961435683}, {"id": 696, "classification": "PERSON", "latitude": 33.48802004624938, "longitude": -111.90881496775653}, {"id": 697, "classification": "UNKNOWN", "latitude": 33.48802187886198, "longitude": -111.90882847417957}, {"id": 698, "classification": "PERSON", "latitude": 33.48801766538075, "longitude": -111.90882289307378}, {"id": 706, "classification": "UNKNOWN", "latitude": 33.4881289113697, "longitude": -111.908896469379}, {"id": 707, "classification": "PERSON", "latitude": 33.48806293756819, "longitude": -111.90880008535265}, {"id": 710, "classification": "UNKNOWN", "latitude": 33.4880314185434, "longitude": -111.90878311481961}, {"id": 711, "classification": "UNKNOWN", "latitude": 33.48804442683758, "longitude": -111.90879309453386}, {"id": 714, "classification": "PERSON", "latitude": 33.488061420726034, "longitude": -111.90879962024852}, {"id": 715, "classification": "PERSON", "latitude": 33.488055189955865, "longitude": -111.90879724325653}],
        var latlng = new google.maps.LatLng(33.488199, -111.908902);
        var img = new google.maps.MarkerImage('/home/greg/.local/lib/python3.5/site-packages/gmplot/markers/6495ED.png');
        var marker = new google.maps.Marker({
        title: "3119",
        icon: img,
        position: latlng
        });
        marker.setMap(map);

        var latlng = new google.maps.LatLng(33.488194, -111.908912);
        var img = new google.maps.MarkerImage('/home/greg/.local/lib/python3.5/site-packages/gmplot/markers/6495ED.png');
        var marker = new google.maps.Marker({
        title: "3218",
        icon: img,
        position: latlng
        });
        marker.setMap(map);

        var latlng = new google.maps.LatLng(33.488194, -111.908912);
        var img = new google.maps.MarkerImage('/home/greg/.local/lib/python3.5/site-packages/gmplot/markers/6495ED.png');
        var marker = new google.maps.Marker({
        title: "3492",
        icon: img,
        position: latlng
        });
        marker.setMap(map);

        var latlng = new google.maps.LatLng(33.488194, -111.908912);
        var img = new google.maps.MarkerImage('/home/greg/.local/lib/python3.5/site-packages/gmplot/markers/6495ED.png');
        var marker = new google.maps.Marker({
        title: "3493",
        icon: img,
        position: latlng
        });
        marker.setMap(map);

        var latlng = new google.maps.LatLng(33.488194, -111.908912);
        var img = new google.maps.MarkerImage('/home/greg/.local/lib/python3.5/site-packages/gmplot/markers/6495ED.png');
        var marker = new google.maps.Marker({
        title: "3236",
        icon: img,
        position: latlng
        });
        marker.setMap(map);

        var latlng = new google.maps.LatLng(33.488194, -111.908912);
        var img = new google.maps.MarkerImage('/home/greg/.local/lib/python3.5/site-packages/gmplot/markers/6495ED.png');
        var marker = new google.maps.Marker({
        title: "3483",
        icon: img,
        position: latlng
        });
        marker.setMap(map);

        var latlng = new google.maps.LatLng(33.488194, -111.908912);
        var img = new google.maps.MarkerImage('/home/greg/.local/lib/python3.5/site-packages/gmplot/markers/6495ED.png');
        var marker = new google.maps.Marker({
        title: "3119",
        icon: img,
        position: latlng
        });
        marker.setMap(map);

var PolylineCoordinates = [
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
new google.maps.LatLng(33.487887, -111.909013),
new google.maps.LatLng(33.488226, -111.908717),
new google.maps.LatLng(33.487970, -111.908849),
new google.maps.LatLng(33.488199, -111.908902),
new google.maps.LatLng(33.488194, -111.908912),
new google.maps.LatLng(33.487842, -111.908994),
];
4

1 回答 1

0

据我所知,您混淆了两个库:

目前尚不清楚您要做什么,但这些软件包不可互操作。

使用gmaps,您应该能够更新地图上的折线。我建议查看以下页面: -关于绘制折线的这个 -关于更新符号的这个页面。您应该能够将其转换为折线。


作为旁注,现在,您的代码有些复杂,这使得这里的人们很难提供帮助。如果您尝试提出一个简单、可重复的示例,您将获得质量更高的答案。

另外,您绝对不应该手动滚动您自己的 JSON 解析。使用json图书馆。如果您只想对 JSON 进行一半解析(看起来这就是您正在做的事情),请解析所有内容,然后重新转储您需要的内容。解析很难。不要自己做。

于 2020-04-13T07:30:57.777 回答