Polygons

A polygon (also referred to as a loop in some applications) is a closed line with multiple segments (edges) and at least three points.

Basic creation and manipulation examples are provided below. For examples that work on colouring and attribute operations against edges, see Edge Networks.

Polygon examples

Creating a polygon

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

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

with proj.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 will insert new points within the points array (from object created above), which will result in new segments in the polygon.

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

path = "/cad/rectangle"
if proj.find_object(path):
    with proj.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 will remove two points from the polygon created in the previous example. It demonstrates using theremove_points() function available on the objects with points.

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

path = "/cad/rectangle"
if proj.find_object(path):
    with proj.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

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

with proj.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)]