Search code examples
pythonprojectionfolium

The position does not match while overlapping the image on the map using folium


This code is draft

bounds_lat = np.array([34.884427, 35.389037])
bounds_lon = np.array([128.761718, 129.305605])


folium.raster_layers.ImageOverlay(image=image_path,
                                  bounds=[[bounds_lat[0], bounds_lon[0]], [bounds_lat[1], bounds_lon[1]]],                            
                                  mercator_project=True,
                                  opacity=0.3).add_to(map)

enter image description here My image was incorrectly overlaid in map, so add the mercator_project=True

change the code

bounds_lat = np.array([34.884427, 35.389037])
bounds_lon = np.array([128.761718, 129.305605])


folium.raster_layers.ImageOverlay(image=image_path,
                                  bounds=[[bounds_lat[0], bounds_lon[0]], [bounds_lat[1], bounds_lon[1]]],
                                  mercator_project=True,
                                  opacity=0.3).add_to(map)

error occurred enter image description here please help me


Solution

  • Reading the docs yielded the following:

    image (string, file or array-like object) – The data you want to draw on the map. * If string, it will be written directly in the output file. * If file, it’s content will be converted as embedded in the output file. * If array-like, it will be converted to PNG base64 string and embedded in the output.

    This means you can use both a path to an image or an image array:

    But for mercator_project the documentation says:

    mercator_project (bool, default False.) – Used only for array-like image. Transforms the data to project (longitude, latitude) coordinates to the Mercator projection. Beware that this will only work if image is an array-like object.

    So you must first load the image into an array:

    from PIL import Image
    
    image_array= Image.open(image_path)
    
    

    Or use any other image processing library (OpenCV eg.).

    Then you can do:

    
    folium.raster_layers.ImageOverlay(image=image_array,
                                      bounds=[[bounds_lat[0], bounds_lon[0]], [bounds_lat[1], bounds_lon[1]]],
                                      mercator_project=True,
                                      opacity=0.3).add_to(map)