Check if a point is within a polygon


I am very new to using VIktor and Python and have a question. I was tinkering around with placing polygons on maps and positioning markers. I wanted to know how I can check if this marker falls within the bounds of the polygon. If this explanation isn’t clear enough or if you need more information, please let me know.


Hi @Floris1 ,

Welcome to the community! Here is a snippet of a working app. Hope it helps:

from viktor.parametrization import (
    GeoPolygonField, GeoPointField,
from viktor import ViktorController, Color
from viktor.views import (
    MapPoint, MapPolygon, MapAndDataView, MapAndDataResult, DataGroup, DataItem, DataStatus,
from shapely.geometry import Point, Polygon

def point_in_polygon(point_coord, polygon_coords):
    # Create a Polygon object
    polygon = Polygon(polygon_coords)

    # Sample point coordinates (longitude, latitude)
    point = Point(point_coord)

    # Check if the point is within the polygon
    return polygon.contains(point)

class Parametrization(ViktorParametrization):
    polygon_field = GeoPolygonField("Create polygon")
    location = GeoPointField("Location")

class Controller(ViktorController):
    label = "My Entity Type"
    parametrization = Parametrization

    @MapAndDataView('Map view', duration_guess=1)
    def get_map_view(self, params, **kwargs):
        # Create some points using coordinates
        features = []

        # Create a polygon
        if params.polygon_field:
            polygon = MapPolygon.from_geo_polygon(params.polygon_field)
        if params.location:
            location = MapPoint.from_geo_point(params.location,

        if params.polygon_field and params.location:
            point_coord = (location.lon,
            polygon_coords = [(pnt.lon, for pnt in polygon.points]
            in_polygon = point_in_polygon(point_coord, polygon_coords)
            data = DataGroup(
                DataItem('Is location in polygon?', 'Yes' if in_polygon else "No", status=DataStatus.SUCCESS if in_polygon else DataStatus.ERROR)
            data = DataGroup()

        return MapAndDataResult(features, data)

Make sure to add shapely to the requirements.txt