### C++ Polygon with Holes for Label Placement Source: https://context7.com/mapbox/polylabel/llms.txt This example demonstrates how to use the C++ Polylabel API with polygons that contain holes. The first ring defines the outer boundary, and subsequent rings define the holes within the polygon. ```cpp #include #include #include int main() { mapbox::geometry::polygon polygonWithHole; // Outer ring mapbox::geometry::linear_ring outer; outer.push_back({0.0, 0.0}); outer.push_back({20.0, 0.0}); outer.push_back({20.0, 20.0}); outer.push_back({0.0, 20.0}); outer.push_back({0.0, 0.0}); polygonWithHole.push_back(outer); // Inner hole mapbox::geometry::linear_ring hole; hole.push_back({5.0, 5.0}); hole.push_back({5.0, 15.0}); hole.push_back({15.0, 15.0}); hole.push_back({15.0, 5.0}); hole.push_back({5.0, 5.0}); polygonWithHole.push_back(hole); // Higher precision for better accuracy auto labelPoint = mapbox::polylabel(polygonWithHole, 0.1); std::cout << "Optimal label position: (" << labelPoint.x << ", " << labelPoint.y << ")" << std::endl; // Output: Optimal label position in the ring between outer and hole return 0; } ``` -------------------------------- ### Find Pole of Inaccessibility in C++ Polygon Source: https://context7.com/mapbox/polylabel/llms.txt Use this snippet to find the optimal label position within a simple polygon using the C++ Polylabel library. Ensure the mapbox/geometry library is installed and included. ```cpp #include #include #include int main() { // Create a polygon using mapbox geometry types mapbox::geometry::polygon polygon; // Outer ring mapbox::geometry::linear_ring outer; outer.push_back({0.0, 0.0}); outer.push_back({10.0, 0.0}); outer.push_back({10.0, 10.0}); outer.push_back({0.0, 10.0}); outer.push_back({0.0, 0.0}); polygon.push_back(outer); // Find pole of inaccessibility with precision 1.0 mapbox::geometry::point result = mapbox::polylabel(polygon, 1.0); std::cout << "Label position: (" << result.x << ", " << result.y << ")" << std::endl; // Output: Label position: (5, 5) // With debug output enabled auto debugResult = mapbox::polylabel(polygon, 0.5, true); // Console shows algorithm progress: // found best 5 after N probes // num probes: N // best distance: 5 return 0; } // Compile with: // g++ -std=c++14 -I/path/to/include main.cpp -o polylabel_example ``` -------------------------------- ### Integrate Polylabel with GeoJSON Data in JavaScript Source: https://context7.com/mapbox/polylabel/llms.txt Load GeoJSON polygon data, find the optimal label position using polylabel, and format the result as a GeoJSON Point Feature. ```javascript import polylabel from 'polylabel'; import { readFileSync } from 'fs'; // Load GeoJSON polygon data const geojson = JSON.parse(readFileSync('polygon.geojson')); const coordinates = geojson.geometry.coordinates; // Find optimal label position const labelPosition = polylabel(coordinates, 1.0); // Use with mapping library (e.g., Mapbox GL JS) const labelFeature = { type: 'Feature', geometry: { type: 'Point', coordinates: labelPosition }, properties: { name: 'Label', distance: labelPosition.distance } }; console.log(JSON.stringify(labelFeature, null, 2)); ``` -------------------------------- ### Working with GeoJSON Data Source: https://context7.com/mapbox/polylabel/llms.txt Demonstrates how to use the Polylabel JavaScript API with GeoJSON data, commonly used in mapping and geospatial applications. ```APIDOC ## Working with GeoJSON Data ### Description Polylabel accepts polygon coordinates in GeoJSON-like format, making it easy to integrate with mapping libraries and geospatial data processing pipelines. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import polylabel from 'polylabel'; import { readFileSync } from 'fs'; // Load GeoJSON polygon data const geojson = JSON.parse(readFileSync('polygon.geojson')); const coordinates = geojson.geometry.coordinates; // Find optimal label position const labelPosition = polylabel(coordinates, 1.0); // Use with mapping library (e.g., Mapbox GL JS) const labelFeature = { type: 'Feature', geometry: { type: 'Point', coordinates: labelPosition }, properties: { name: 'Label', distance: labelPosition.distance } }; console.log(JSON.stringify(labelFeature, null, 2)); // { // "type": "Feature", // "geometry": { // "type": "Point", // "coordinates": [x, y] // }, // "properties": { // "name": "Label", // "distance": 123.45 // } // } ``` ### Response #### Success Response (200) - **labelFeature** (Object) - A GeoJSON Feature object representing the optimal label point. - **type** (String) - Always 'Feature'. - **geometry** (Object) - **type** (String) - Always 'Point'. - **coordinates** (Array) - The [x, y] coordinates of the optimal point. - **properties** (Object) - **name** (String) - A placeholder name for the label. - **distance** (Number) - The distance from the point to the nearest polygon edge. #### Response Example ```json { "type": "Feature", "geometry": { "type": "Point", "coordinates": [-122.41415, 37.7799] }, "properties": { "name": "Label", "distance": 0.000005 } } ``` ``` -------------------------------- ### Handling Degenerate Polygons Source: https://context7.com/mapbox/polylabel/llms.txt Explains how the Polylabel algorithm handles degenerate polygon cases, such as zero-area polygons or polygons with collinear points. ```APIDOC ## Handling Degenerate Polygons ### Description The algorithm gracefully handles degenerate cases such as zero-area polygons (collinear points) by returning a point on the polygon with distance 0. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import polylabel from 'polylabel'; // Degenerate polygon - all points are collinear (line) const linePolygon = [[[0, 0], [1, 0], [2, 0], [0, 0]]]; const lineResult = polylabel(linePolygon); console.log(lineResult); // [0, 0] console.log(lineResult.distance); // 0 // Degenerate polygon with backtracking const backtrackPolygon = [[[0, 0], [1, 0], [1, 1], [1, 0], [0, 0]]]; const backtrackResult = polylabel(backtrackPolygon); console.log(backtrackResult); // [0, 0] console.log(backtrackResult.distance); // 0 ``` ### Response #### Success Response (200) - **[x, y]** (Array) - The optimal point coordinates, which will be on the polygon boundary for degenerate cases. - **distance** (Number) - Always 0 for degenerate polygons. ``` -------------------------------- ### Handle Degenerate Polygons with Polylabel in JavaScript Source: https://context7.com/mapbox/polylabel/llms.txt The polylabel function handles degenerate polygons (e.g., lines) by returning a point on the polygon with a distance of 0. ```javascript import polylabel from 'polylabel'; // Degenerate polygon - all points are collinear (line) const linePolygon = [[[0, 0], [1, 0], [2, 0], [0, 0]]]; const lineResult = polylabel(linePolygon); console.log(lineResult); // [0, 0] console.log(lineResult.distance); // 0 ``` ```javascript // Degenerate polygon with backtracking const backtrackPolygon = [[[0, 0], [1, 0], [1, 1], [1, 0], [0, 0]]]; const backtrackResult = polylabel(backtrackPolygon); console.log(backtrackResult); // [0, 0] console.log(backtrackResult.distance); // 0 ``` -------------------------------- ### Calculate Pole of Inaccessibility in JavaScript Source: https://context7.com/mapbox/polylabel/llms.txt Use the polylabel function with polygon coordinates, an optional precision, and a debug flag. Returns the optimal point and its distance to the nearest edge. ```javascript import polylabel from 'polylabel'; // Simple square polygon - finds the center const square = [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]]; const result = polylabel(square, 1.0); console.log(result); // [2, 2] console.log(result.distance); // 2 ``` ```javascript // Complex polygon with hole const polygonWithHole = [ // Outer ring (counterclockwise) [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], // Inner hole (clockwise) [[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]] ]; const labelPoint = polylabel(polygonWithHole, 0.5); console.log(labelPoint); // Optimal label position console.log(labelPoint.distance); // Distance to nearest edge ``` ```javascript // Geographic coordinates - use appropriate precision const geoPolygon = [[ [-122.4194, 37.7749], [-122.4089, 37.7749], [-122.4089, 37.7849], [-122.4194, 37.7849], [-122.4194, 37.7749] ]]; const geoLabel = polylabel(geoPolygon, 0.000001); console.log(geoLabel); // [-122.41415, 37.7799] ``` ```javascript // Enable debug mode to see algorithm progress const debugResult = polylabel(square, 1.0, true); // Console output: // found best 2 after 2 probes // num probes: 2 // best distance: 2 ``` -------------------------------- ### Calculate Pole of Inaccessibility in C++ Source: https://github.com/mapbox/polylabel/blob/master/README.md Requires mapbox/geometry.hpp and mapbox/variant dependencies. The function returns a point representing the pole of inaccessibility. ```C++ #include int main() { mapbox::geometry::polygon polygon = readPolygon(); // Get polygon data from somewhere. mapbox::geometry::point p = mapbox::polylabel(polygon, 1.0); return 0; } ``` -------------------------------- ### Calculate Pole of Inaccessibility in JavaScript Source: https://github.com/mapbox/polylabel/blob/master/README.md Uses a GeoJSON-like array of coordinates to find the pole of inaccessibility. Ensure the precision parameter is adjusted for the input coordinate units. ```js const p = polylabel([[[0, 0], [1, 0], ...]], 1.0); const distance = p.distance; ``` -------------------------------- ### JavaScript API: polylabel Function Source: https://context7.com/mapbox/polylabel/llms.txt The main JavaScript function to calculate the pole of inaccessibility for a polygon. It accepts the polygon coordinates, an optional precision, and an optional debug flag. It returns the optimal point with its distance to the nearest edge. ```APIDOC ## JavaScript API: polylabel(polygon, precision, debug) ### Description The main function that calculates the pole of inaccessibility for a polygon. It takes a polygon in GeoJSON-like format (array of rings, where each ring is an array of `[x, y]` coordinate pairs), an optional precision value (default 1.0), and an optional debug flag. Returns the optimal point as `[x, y]` with an additional `distance` property indicating the distance to the nearest polygon edge. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import polylabel from 'polylabel'; // Simple square polygon - finds the center const square = [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]]; const result = polylabel(square, 1.0); console.log(result); // [2, 2] console.log(result.distance); // 2 // Complex polygon with hole const polygonWithHole = [ // Outer ring (counterclockwise) [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], // Inner hole (clockwise) [[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]] ]; const labelPoint = polylabel(polygonWithHole, 0.5); console.log(labelPoint); // Optimal label position console.log(labelPoint.distance); // Distance to nearest edge // Geographic coordinates - use appropriate precision const geoPolygon = [[[-122.4194, 37.7749], [-122.4089, 37.7749], [-122.4089, 37.7849], [-122.4194, 37.7849], [-122.4194, 37.7749]]]; const geoLabel = polylabel(geoPolygon, 0.000001); console.log(geoLabel); // [-122.41415, 37.7799] // Enable debug mode to see algorithm progress const debugResult = polylabel(square, 1.0, true); // Console output: // found best 2 after 2 probes // num probes: 2 // best distance: 2 ``` ### Response #### Success Response (200) - **[x, y]** (Array) - The optimal point coordinates. - **distance** (Number) - The distance from the point to the nearest polygon edge. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.