Search code examples
pythonleafletfoliumshapely

Polygons does not show up on folium map?


I am using an api to fetch coordinates that I creaty polygons with using shapely. I also have a manually set point that I want to check if it is in any of the areas(polygons). Which seems to work.

But when I try to use folium to display the point and the polygons it dosent wprk. The code prints: "The point is inside a area" as it should. But the map only display the point but no polygons(areas). I imagine the problem may have to do with the fact that Im using multiple different coordinate system. But I don't know. IS there anyone that could take a quick look and see if they spot something?

This is the code:

import requests
import folium
from shapely.geometry import Point, Polygon
from shapely.ops import transform
import pyproj

# Define the coordinate systems
wgs84 = pyproj.CRS('EPSG:4326')
sweref99 = pyproj.CRS('EPSG:3006')

# Fetch the areas from the API
url = 'examplecoordinates.com'
response = requests.get(url)
data = response.json()

# Extract the polygons from the response and store them in a list
polygons = []
for feature in data['features']:
    coords = feature['geometry']['coordinates'][0]
    polygon = Polygon(coords)
    polygons.append(polygon)

# Define the point to check in WGS84
point_wgs84 = Point(15.0560, 65.7346)

# Convert the point to SWEREF99
project = pyproj.Transformer.from_crs(wgs84, sweref99, always_xy=True).transform
point_sweref99 = transform(project, point_wgs84)

# Check if the point is inside any of the polygons
inside_risk_area = False
for polygon in polygons:
    if polygon.contains(point_sweref99):
        inside_risk_area = True
        print('The point is inside a area')
        break
else:
    print('The point is not inside any area')

# Create a map centered at the point of interest
m = folium.Map(location=[point_wgs84.y, point_wgs84.x], zoom_start=10)

# Add a marker for the point of interest
folium.Marker([point_wgs84.y, point_wgs84.x]).add_to(m)

# Add the areas as polygons to the map
for polygon in polygons:
    coords = [[p[1], p[0]] for p in polygon.exterior.coords]
    color = 'red' if inside_risk_area else 'green'
    folium.Polygon(locations=coords, color=color, fill_color=color, fill_opacity=0.3).add_to(m)

# Display the map
m.save('map.html')

Kind regards, Hugo


Solution

  • I solved it, if anyone ever have a similar problem to mine this was how I solved it.

    I changed this piece of code:

    polygons = []
    for feature in data['features']:
       coords = feature['geometry']['coordinates'][0]
       polygon = Polygon(coords)
       polygons.append(polygon)
    

    to this:

    polygons = []
    for feature in data['features']:
        coords = feature['geometry']['coordinates'][0]
        project = pyproj.Transformer.from_crs(sweref99, wgs84, always_xy=True).transform
        coords_wgs84 = [list(project(x, y)) for x, y in coords]
        polygon = Polygon(coords_wgs84)
        polygons.append(polygon)
    

    In order to properly display the polygons in the folium map, I needed to convert the polygon coordinates to WGS84, which is the CRS used by folium.