Custom Layer and Feature Creation Samples¶
These functions allow you to add vector features (points, lines, and areas) to vector layers, as well as generate entirely new vector, raster, and elevation grid layers. Additionally, functions that allow you to combine areas and create buffer zones can be utilized to enhance feature geometry.
Create a Floorplan with Area Features¶
Note
Before running this sample, configure these variables:
export_dir
: the directory for resulting files to be exported to
This sample demonstrates how to create area features and add them to a custom vector layer to generate a simple floorplan. Area features are generated based on 4 coordinate-pairs that represent the 4 corners of a room. Once the area feature has been created, it is then added to a custom vector layer. This proccess repeats until all rooms in the floorplan have been placed. The floorplan is then exported as a jpg.
# Generates a projection to use when creating a custom vector layer
proj = gm.GM_Projection_t()
proj.mProjSys = gm.GM_PRJ_ATTR_STATE_PLANE_SCALE_FACTOR
proj.mDatum = gm.GM_DATUM_WGS_84
proj.mUnit = gm.GM_PRJ_UNIT_METERS
proj.mNumAttrs = 0
created_layer = gm.CreateCustomVectorLayer("Floorplan_areas", proj)
# Points that will be used to create area features
# that will generate a house floorplan (in meters)
# Dimensions for a floorplan could also be extracted from a CSV or other
# data format to be used in this sample
floorplan_pts = [
[0, 0, 0, 12.6, 7.8, 12.6, 7.8, 9.2, 16, 9.2, 16, 0],
[0, 9.2, 0, 12.6, 7.8, 12.6, 7.8, 9.2],
[0, 0, 6.2, 0, 6.2, 9.2, 0, 9.2],
[6.2, 0, 6.2, 4, 7.8, 4, 7.8, 0],
[7.8, 0, 7.8, 4, 11.4, 4, 11.4, 0],
[11.4, 0, 11.4, 4, 12.4, 4, 12.4, 5.6, 16, 5.6, 16, 0],
[6.2, 5.6, 6.2, 9.2, 7.8, 9.2, 7.8, 5.6],
[7.8, 5.6, 7.8, 9.2, 9.8, 9.2, 9.8, 5.6],
[9.8, 5.6, 9.8, 9.2, 11.4, 9.2, 11.4, 5.6],
[11.4, 5.6, 11.4, 9.2, 16, 9.2, 16, 5.6],
[0, 9.2, 1.6, 9.2, 1.6, 12.6, 0, 12.6],
[6.2, 4, 6.2, 5.6, 12.4, 5.6, 12.4, 4],
]
# Create area features using point-pairs
for i in range(len(floorplan_pts)):
pts_list = []
for j in range(0, len(floorplan_pts[i]), 2):
pt = gm.GM_Point_t(floorplan_pts[i][j], floorplan_pts[i][j + 1])
pts_list.append(pt)
# Convert a list of points into an array of points
# so that it can be assigned to the area feature
size = len(pts_list)
area = gm.GM_AreaFeature_t()
pts_array = gm.GM_Point_t_array(size)
for k in range(size):
pts_array[k] = pts_list[k]
# Setup style options
style = gm.GM_AreaStyle_t()
style.mBorderPenWidth = 5
style.mBrushColor = gm.toRGB(198, 224, 141)
style.mBrushStyle = 1
area.mAreaStyle = style
area.mPointList = pts_array
area.mNumPoints = size
# Add the area feature to the vector layer
err_AddAreaToVectorLayer = gm.AddAreaToVectorLayer(
created_layer, area, gm.GM_AddFeature_MoreAddsToFollow
)
# Get handle for the loaded vector layer
arr_ptr, arr_size = gm.GetLoadedLayerList()
layers_array = gm.GM_LayerHandle_array_frompointer(arr_ptr)
layers_list = gm.carray_to_list(layers_array, arr_size)
# Create a bounding rectangle
# This will add a whitespace border around the floorplan
rect = gm.GM_Rectangle_t(-2, -2, 18, 15)
err_ExportRaster = gm.ExportRaster(
export_dir + "floorplan_areas.jpg",
gm.GM_Export_JPG,
layers_list[0],
rect,
1920,
1512,
gm.GM_ExportFlags_HideProgress,
)
err_CloseLayer = gm.CloseLayer(created_layer)
Create a Floorplan with Line Features¶
Note
Before running this sample, configure these variables:
export_dir
: the directory for resulting files to be exported to
This sample demonstrates how to create line features and add them to a custom vector layer to generate a simple floorplan. The line features are generated based on coordinate-pairs that represent the two endpoints of a line. Once the line feature has been created, it is then added to a custom vector layer. This proccess repeats until all rooms/walls in the floorplan have been placed. The floorplan is then exported as a jpg.
# Generates a projection to use when creating a custom vector layer
proj = gm.GM_Projection_t()
proj.mProjSys = gm.GM_PRJ_ATTR_STATE_PLANE_SCALE_FACTOR
proj.mDatum = gm.GM_DATUM_WGS_84
proj.mUnit = gm.GM_PRJ_UNIT_METERS
proj.mNumAttrs = 0
created_layer = gm.CreateCustomVectorLayer("Floorplan_lines", proj)
# Points that will be used to create line features
# that will generate a house floorplan (in meters)
# Dimensions for a floorplan could also be extracted from a CSV or other
# data format to be used in this sample
floorplan_pts = [
[0, 0, 0, 12.6],
[0, 12.6, 7.8, 12.6],
[7.8, 12.6, 7.8, 9.2],
[7.8, 9.2, 16, 9.2],
[16, 9.2, 16, 0],
[0, 0, 16, 0],
[0, 9.2, 7.8, 9.2],
[16, 5.6, 6.2, 5.6],
[6.2, 5.6, 6.2, 9.2],
[7.8, 5.6, 7.8, 9.2],
[9.8, 5.6, 9.8, 9.2],
[11.4, 5.6, 11.4, 9.2],
[6.2, 0, 6.2, 4],
[7.8, 0, 7.8, 4],
[11.4, 0, 11.4, 4],
[7.8, 4, 12.4, 4],
[12.4, 4, 12.4, 5.6],
[1.6, 9.2, 1.6, 11.5],
]
# Create line features using point-pairs
for i in range(len(floorplan_pts)):
line = gm.GM_LineFeature_t()
line.mNumPoints = 2
# Each list inside of floorplan_pts has 4 values
# The first 2 are X,Y for point 1, and the last 2 are X,Y for point 2
pt_1 = gm.GM_Point_t(floorplan_pts[i][0], floorplan_pts[i][1])
pt_2 = gm.GM_Point_t(floorplan_pts[i][2], floorplan_pts[i][3])
# Convert a list of points into an array of points
# so that it can be assigned to line feature
pt_list = [pt_1, pt_2]
pt_array = gm.GM_Point_t_array(2)
pt_array[0] = pt_list[0]
pt_array[1] = pt_list[1]
line.mPointList = pt_array
# Make the lines thicker to improve visibility
style = gm.GM_LineStyle_t()
style.mPenWidth = 5
line.mLineStyle = style
# Add the created line feature to the vector layer
err_AddLineToVectorLayer = gm.AddLineToVectorLayer(
created_layer, line, gm.GM_AddFeature_MoreAddsToFollow
)
# Get handle for the loaded vector layer
arr_ptr, arr_size = gm.GetLoadedLayerList()
layers_array = gm.GM_LayerHandle_array_frompointer(arr_ptr)
layers_list = gm.carray_to_list(layers_array, arr_size)
# Create a bounding rectangle
# This will add a whitespace border around the floorplan
rect = gm.GM_Rectangle_t(-2, -2, 18, 15)
err_ExportRaster = gm.ExportRaster(
export_dir + "floorplan_lines.jpg",
gm.GM_Export_JPG,
layers_list[0],
rect,
1920,
1512,
gm.GM_ExportFlags_HideProgress,
)
err_CloseLayer = gm.CloseLayer(created_layer)