简体   繁体   English

如何使用python在单张地图上绘制轨迹?

[英]How to plot trajectories on a leaflet map using python?

I am dealing with a data frame as shown below. 我正在处理如下所示的数据框。 x and y are the Mercator x, y coordinates of trajectories with time. x和y是轨迹随时间的墨卡托x,y坐标 Rows with same VoyageIDs are the points belonging to the same trajectory. 具有相同VoyageID的行是属于同一轨迹的点。 As we can see the rows with all 0's are separating different trajectories. 如我们所见,全为0的行分隔不同的轨迹。

        VoyageID      X             Y             Time
     27 2             -7.35857534   2.09175178     1.29471228
     28 2             -7.35863779   2.09167080     1.29471234
     29 2             -7.35882203   2.09156224     1.29471240
     30 2             -7.35908808   2.09147633     1.29471246
     31 2             -7.35941313   2.09134900     1.29471252
     32 2             -7.35970112   2.09123810     1.29471258
     33 0             0.0000000     0.0000000      0.0000000
     34 3             -7.34769342   2.09628155     1.29498270
     35 3             -7.34811254   2.09626864     1.29498282
     36 3             -7.34853711   2.09625315     1.29498288
     37 3             -7.34889255   2.09622732     1.29498294
     38 0             0.0000000     0.0000000      0.0000000
     39 4             -7.35857089   2.09176469     1.29531606
     40 4             -7.35862989   2.09169697     1.29531612
     41 4             -7.35869312   2.09162679     1.29531618
     42 4             -7.35876692   2.09158959     1.29531624
     43 0             0.0000000     0.0000000      0.0000000

I would kindly request to suggest me the best way to visualize these trajectories in the following two ways: 我恳请建议我以以下两种方式可视化这些轨迹的最佳方法:

  1. How can I plot x, y coordinates on a simple 2d line plot ? 如何在简单的2d线图中绘制x,y坐标?

  2. How can I plot the trajectories with x,y coordinates on a leaflet map (Using Folium or any other real map) ? 如何在单张地图(使用Folium或任何其他真实地图)上以x,y坐标绘制轨迹?

Also, how do I manage the points of different trajectories (They are separated by 0's). 另外,我该如何管理不同轨迹的点(它们之间用0分隔)。 I am new to python as well as matplotlib, so please provide me with little detailed answers if possible. 我是python和matplotlib的新手,所以请尽可能提供一些详细的答案。 Thanks in advance. 提前致谢。

You could convert your dataframe to JSON format which is easily handled by any Javascript mapping library. 您可以将数据框转换为JSON格式,任何Javascript映射库均可轻松处理。

For instance using Pandas DataFrame.to_json method turns this dataframe: 例如,使用Pandas DataFrame.to_json方法可以打开以下数据DataFrame.to_json

voyage  id  x           y           time
27      2   -7.35857534 2.09175178  1.29471228
28      2   -7.35863779 2.09167080  1.29471234
29      2   -7.35882203 2.09156224  1.29471240

Into this JSON array: 放入此JSON数组中:

[{
    "voyage": 27,
    "id": 2,
    "x": -7.35857534,
    "y": 2.09175178,
    "time": 1.29471228
}, {
    "voyage": 28,
    "id": 2,
    "x": -7.35863779,
    "y": 2.09167080,
    "time": 1.29471234
}, {
    "voyage": 29,
    "id": 2,
    "x": -7.35882203,
    "y": 2.09156224,
    "time": 1.29471240
}]

The resulting file can be loaded by Javascript and used with your mapping library like Leaflet: 生成的文件可以由Javascript加载,并与您的映射库(如Leaflet)一起使用:

// Start a new map
var map = new L.Map('leaflet', {'center': [0, 0], 'zoom': 0});

// Load file 
fetch('data.json').then(function(response) {

    // Return JSON 
    return response.json().then(function (arr) {

        // Add a line
        var line = new L.Polyline([]).addTo(map);

        // Iterate the objects in the array
        arr.forEach(function (obj) {

            // If ID is not 0
            if (obj.id) {

                // Add point to line
                line.addLatLng(obj.y, obj.x);

            // ID is 0
            } else {

                // Start new line
                line = new L.Polyline([]).addTo(map);
            }
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM