6

I am new in working with python.

I have a question regarding How to plot a map in Python using Latitude and Longitude data ?

What i have done before :

Step 1 : I have a CSV file which contains Information about Vehicle Driving signal.
Step 2 : From the File I have extracted some Latitude and Longitude Data in the form of pandas Data frame.
Step 3 : Now i save that Data frame as CSV File.
Step 4 : After saving, I am giving that file to Online GPS Visualizer, so it generates a Open street map.

[![enter image description here][1]][1]

Requirement No :1

I just want the same map to be plotted in python instead of giving the Lat&Long csv file to Online GPS Visualizer. So which Library I should use ? Base map Library is suited for my work or should i go for another alternative ?

Requirement No : 2

After plotting the map in python i just want to Highlight few Latitude and Longitude points in the map. (As Pointed in the image)

Mari
  • 698
  • 1
  • 8
  • 27
  • try to take a look at folium: https://pythonhow.com/web-mapping-with-python-and-folium/ It should be quite easy if you follow the examples you find online – Federico Gentile Jan 31 '19 at 08:36

1 Answers1

7

As suggested by this link1 and link2 you can achieve some similar results. Here is what the code looks like for example.

import gpxpy
import gpxpy.gpx
import folium

gpx_file = open('path_to_gpx_file.gpx', 'r')

gpx = gpxpy.parse(gpx_file)
points = []
for track in gpx.tracks:
    for segment in track.segments:        
        for point in segment.points:
            points.append(tuple([point.latitude, point.longitude]))
print(points)
ave_lat = sum(p[0] for p in points)/len(points)
ave_lon = sum(p[1] for p in points)/len(points)

# Load map centred on average coordinates
my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=14)

#add a markers
for each in points:  
    folium.Marker(each).add_to(my_map)

#fadd lines
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)

# Save map
my_map.save("./gpx_berlin_withmarker.html")
Federico Gentile
  • 5,650
  • 10
  • 47
  • 102
  • nice answer! I added an example code in case you have csv with lat/long sorted by timestamp: https://gist.github.com/deparkes/9a0b45c69beb9a614f54d13bc7c551b5#gistcomment-3163422 – philshem Feb 01 '20 at 21:49