Extract coordinates inside a polygon shape drawn with dcc.Graph dragmode

I have a dcc.graph that uses dragmodes with "drawclosedpath, and I would like to be able to extract all of the coordinates that are inside of an svg path object. When using this drawmode, the graoh layout returns an svg path:

“M291.94094488188983,299.9798228346457L314.61811023622056,277.30265748031496L351.468503937008,277.30265748031496L367.53149606299223,283.91683070866145L387.3740157480316,290.5310039370079L404.3818897637796,307.5388779527559Z”

Does anyone know how to be able to extract these coordinates, and then be able to subset an array to include all of the coordinates INSIDE of this shape?

Hey @matt.sd.watson welcome to the forums.

I extracted the coordinates like this in the past:

import re

def extract(closedpath: str) -> list[float]:
    path = re.split('[a-zA-Z]', closedpath)
    path = [point for point in path if point]

    # initiate variables
    x = []
    y = []

    for point in path:
        point_x, point_y = map(float, point.split(','))
        x.append(point_x)
        y.append(point_y)
    return x,y

usage:

string = "M291.94094488188983,299.9798228346457L314.61811023622056,277.30265748031496L351.468503937008,277.30265748031496L367.53149606299223,283.91683070866145L387.3740157480316,290.5310039370079L404.3818897637796,307.5388779527559Z"

x, y = extract(string)

As for the second part of your question, this might be helpful:

Search for the text passage:

we use the function skimage.draw.polygon to obtain the coordinates of pixels covered by the path
then we use the function scipy.ndimage.binary_fill_holes in order to set to True the pixels enclosed by the path.

EDIT: reading the linked example it sounds pretty similar to what you want to do…

This is an example I made of a free polygon stroke, maybe it will help a little so you can do what you need