How to insert points in a map

Viewed 166

I have coordinates of some properties and I would like to insert the points of these properties in a map. I left an example below for you to see. Can you help me with this?

Executable code:

import pandas as pd

df = pd.DataFrame({'Properties':[1,2,3,4,5,6],
'Latitude':[-24.930473, -24.95575,-24.924161,-24.95579, -24.94557, -24.93267],
'Longitude':[-49.994889, -49.990162,-50.004343, -50.007371, -50.01542, -50.00702]})

print(df)

Properties Latitude Longitude 
0   1     -24.930473 -49.994889   
1   2     -24.955750 -49.990162   
2   3     -24.924161 -50.004343   
3   4     -24.955790 -50.007371   
4   5     -24.945570 -50.015420   
5   6     -24.932670 -50.007020   

Example:

enter image description here

2 Answers

One way of doing this is to use the Google Map API. However, it is not free. The othe way to do this is to use folium and OpenStreetMap.

import folium


df = pd.DataFrame({'Properties':[1,2,3,4,5,6],
'Latitude':[-24.930473, -24.95575,-24.924161,-24.95579, -24.94557, -24.93267],
'Longitude':[-49.994889, -49.990162,-50.004343, -50.007371, -50.01542, -50.00702]})

m = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=2)
m

The m will return a map of the world that is zoomable.

To add the markers to this:

for i in range(0,len(df)):
    folium.Marker(
      location=[df.iloc[i]['Latitude'], df.iloc[i]['Longitude']],
      popup=df.iloc[i]['Properties'],
   ).add_to(m)

m

will return:

enter image description here

enter image description here

You happened to put all your location near the Falkland Islands. You can zoom in as much as you want.

If you search for an open-source tool, have a look at EOmaps! (based on matplotlib/cartopy)

Creting a quick map is as simple as:

import pandas as pd
from eomaps import Maps

df = pd.DataFrame({'Properties':[1,2,3,4,5,6],
'Latitude':[-24.930473, -24.95575,-24.924161,-24.95579, -24.94557, -24.93267],
'Longitude':[-49.994889, -49.990162,-50.004343, -50.007371, -50.01542, -50.00702]})

m = Maps()
m.add_feature.preset.coastline()
m2.set_data(df, parameter="Properties", x="Latitude", y="Longitude", crs=4326)
m2.set_shape.geod_circles(radius=600) # use 600m circles to represent the data
m2.plot_map()

... or if you want to do more fancy stuff:

import pandas as pd
from eomaps import Maps

df = pd.DataFrame({'Properties':[1,2,3,4,5,6],
'Latitude':[-24.930473, -24.95575,-24.924161,-24.95579, -24.94557, -24.93267],
'Longitude':[-49.994889, -49.990162,-50.004343, -50.007371, -50.01542, -50.00702]})


clat, clon = df.Latitude.mean(), df.Longitude.mean()

# create a map
m = Maps(Maps.CRS.Orthographic(clat, clon), figsize=(7,4))
m.add_feature.preset.coastline()
m.add_feature.preset.ocean()

# create a zoomed-in inset-map
m2 = m.new_inset_map(xy=(clat, clon), 
                     radius=0.4, shape="rectangles", radius_crs=4326,
                     plot_position=(0.25, 0.5), plot_size=0.8)
# add some features and the data to the inset-plot
m2.add_wms.GEBCO.add_layer.GEBCO_LATEST()
m2.set_data(df, parameter="Properties", x="Latitude", y="Longitude", crs=4326)
m2.set_shape.geod_circles(radius=600)
m2.plot_map()

# adjust the subplot boundaries (e.g. to make the globe smaller)
m.subplots_adjust(left=0.5, right=1, top=0.8, bottom=0.2)

# add a basic annotation
m.add_annotation(xy=(clat, clon), 
                 xytext=(10,20),
                 horizontalalignment="center",
                 text="points are here", 
                 bbox=dict(boxstyle="round", fc="r", ec="k", alpha=0.5))

enter image description here

Related