Polygons

A polygon (also referred to as a loop in some applications) is a closed line composed of multiple segments (edges) and a minimum of three points. A polygon is represented in the SDK with the Polygon class.

Polygon examples

The following examples demonstrate how to create and manipulate polygons, including inserting and removing points and defining specific shapes such as rectangles and circles. For examples involving edge operations, such as colouring and attribute management, see Edge Networks.

Creating a polygon

To demonstrate auto-closure of the polygon, this example creates a Polygon object with four points that form a rectangle.

from mapteksdk.project import Project
from mapteksdk.data import Polygon
import numpy as np
project = Project() # Connect to default project

with project.new("/cad/rectangle", Polygon, overwrite=True) as rectangle:
    # Create a green flat rectangle
    rectangle.points = np.array([[0, 0, 0], [0, 50, 0],
                              [100, 50, 0], [100, 0, 0],])
    rectangle.point_colours = [0,255,0,255]

Inserting new points into a polygon

This example demonstrates how to insert new points into the existing points array, thereby creating additional segments in the polygon.

from mapteksdk.project import Project
import numpy as np
project = Project() # Connect to default project

path = "/cad/rectangle"
if project.find_object(path):
    with project.edit(path) as rectangle:
        new_point_at_top = np.array([50, 100, 0])
        new_point_at_bottom = np.array([50, -50, 0])
        # The top edge is index 1 (insert at 2)
        rectangle.points = np.insert(rectangle.points, 2, new_point_at_top, axis=0)
        # The bottom edge is (now) index 4 (insert at 5)
        rectangle.points = np.insert(rectangle.points, 5, new_point_at_bottom, axis=0)
else:
    print("Create a Polygon in '{}' first.".format(path))

Removing points from a polygon

This example illustrates removing points from an existing polygon, using the Polygon.remove_points() method to delete points at specified indices.

from mapteksdk.project import Project
project = Project() # Connect to default project

path = "/cad/rectangle"
if project.find_object(path):
    with project.edit(path) as rectangle:
        # Remove points at index 0 and 4
        rectangle.remove_points([0,4])
else:
    print("Create a Polygon in '{}' first.".format(path))

Creating a circle

The following example shows how to create a circle by defining a series of points positioned in a circular pattern.

from mapteksdk.project import Project
from mapteksdk.data import Polygon
import math
pi = math.pi
project = Project() # Connect to default project

with project.new("/cad/circle", Polygon, overwrite=True) as circle:
    number_of_points = 100
    radius = 50
    circle.points = [(math.cos(2*pi/number_of_points*x)*radius,
                        math.sin(2*pi/number_of_points*x)*radius,
                        0) for x in range(0, number_of_points+1)]