Check if a point (or many points) are located within a polygon?

I have a table with each row being a different polygon that were drawn in Google Maps. I'm trying to check if a point or multiple points are within a polygon.

// Helper function to check if a point is inside a polygon
function isPointInsidePolygon(point, polygon) {
    let isInside = false;
    for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
        const xi = polygon[i][0], yi = polygon[i][1];
        const xj = polygon[j][0], yj = polygon[j][1];
        const intersect = ((yi > point[1]) !== (yj > point[1])) &&
                          (point[0] < (xj - xi) * (point[1] - yi) / (yj - yi) + xi);
        if (intersect) isInside = !isInside;
    return isInside;

// Function to check if any coordinates in query7 are inside the polygons in query5
function checkCoordinatesInsidePolygons(query7Data, query5Data) {
    // Assuming the structures of query7Data and query5Data are as expected
    const filteredData = query7Data.filter(query7Item => {
        return query5Data.features.some(query5Item => {
            const point = [parseFloat(query7Item.geocoded_longitude), parseFloat(query7Item.geocoded_latitude)];
            return isPointInsidePolygon(point, query5Item.geometry.coordinates[0]);

    // Assuming table3 is an object that has a method to set its data = filteredData;

    return filteredData;

// Main execution
try {
    // Fetch data for table2 and table3
    const query7Data =;
    const query5Data =;

    if (!query7Data || !query5Data) {
        throw new Error('Data not found in query7 and query5');
    // Pass the fetched data to the check function
    return checkCoordinatesInsidePolygons(query7Data, query5Data);
} catch (error) {
    console.error('An error occurred:', error);
    // Take appropriate action here, such as setting an error state or message
    // Depending on your environment, you might need to actually return an error object or message
    return { error: error.message };

I believe I am getting the point's in all the polygon's in table 2, however, i want table3 to show points that are within the selected row's polygon from table2. The values in table also have their own coordinates (lat, long) so I'm trying to see with values fall within the selected rows polygon. Any help would be greatly appreciated. :slightly_smiling_face:

![Screenshot 2024-02-28 at 8.39.12 PM|690x387](upload://cwf7I9zAd2iBQam2ZxPX1EmN0ZF.png)

quick (probably unrelated?) question, are your polygons all convex? the math for concave is a bit different

Hi bob, thanks for the reply. I have both, convex and concave. Do I need to have a different function for the concave polygons?

ya, its been a while since my graphics class so there might be aetter way but we would 'cut' concave polys into smaller all convex ones then test each 9ne. if its inside 1 of them then its inside the concave polygon as a whole

this link might help.

1 Like