gpsPointInPolygon (Functionblock)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

MX2, CX1, SX1, MX2 turbo/encore/warp, NX-200, NX-400, NX-900, LX2, LX5

Firmware version:

1.00 / 1.00.00


This function block is used for calculating if a given point is within a defined polygon with up to 7 sides. The function block uses points that are given by DINTs. When using this function together with GPS positions, the latitude/longitude positions are normally converted to DINT values by the user's program. Typically, this is done in the following way:

The degrees, minutes, and decimal minutes are each multiplied by a factor, so that the resulting DINT is formatted as follows:

 

DDD_MM_CCCC

 

where DDD is degrees (to be multiplied by 1,000,000), MM is minutes (to be multiplied by 10,000) and CCCC is decimal minutes. This means that all possible latitudes and longitudes can be contained within a DINT.

 

Please note:

The polygon corners XY positions must be presented to the function block clockwise, and in order to close the polygon, the first non-used entry in the definition of the polygon must point to the first position in order to close the polygon. Please see the example below.

 

 

Input:

 

Position_X : DINT

X part of the point to check.

 

Position_Y : DINT

Y part of the point to check.

 

Polygon_X : ARRAY[1..8] OF DINT

X part of each corner of the polygon.

 

Polygon_Y : ARRAY[1..8] OF DINT

Y part of each corner of the polygon.

 

Corners : SINT

Number of corners that define the polygon.

 

Output:

 

Within : BOOL

TRUE if Position_X/Position_Y is within the defined polygon, FALSE if outside of polygon.

 

Declaration:

FUNCTION_block gpsPointInPolygon;
VAR_INPUT
  // Position 1 parameters:
  Position_X   : DINT;               | X part of the position to check
  Position_Y   : DINT;               | Y part of the position to check
 
  // Polygon parameters:
  Polygon_X   : ARRAY[1..8] OF DINT; | X part of the polygon
  Polygon_Y   : ARRAY[1..8] OF DINT; | Y part of the polygon
 
  Corners     : SINT;             | Number of corners defined in the polygon
END_VAR;
VAR_OUTPUT
  Within     : BOOL;               | True if Position_X/Position_Y is within the polygon
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
PROGRAM test;
VAR
  Poly_x     : ARRAY[1..8] OF DINT;
  Poly_y     : ARRAY[1..8] OF DINT;
  P_x       : DINT;
  P_y       : DINT;
  checkPoly : gpsPointInPolygon;
END_VAR;
 
  // Define a polygon with corners in (0,0) (0,1000000) (1000000,1000000) (0,1000000) starting with lower left corner
  // going clockwise: (X is the point to search for (500000, 500000)
  //   ------------------
  //   |                |
  //   |                |
  //   |                |
  //   |        X       |
  //   |                |
  //   |                |
  //   |                |
  //   ------------------
  //
  poly_y[1] := 0_00_0000;
  poly_x[1] := 0_00_0000;
 
  poly_y[2] := 1_00_0000;
  poly_x[2] := 0_00_0000;
 
  poly_y[3] := 1_00_0000;
  poly_x[3] := 1_00_0000;
 
  poly_y[4] := 0_00_0000;
  poly_x[4] := 1_00_0000;
 
  poly_y[5] := poly_y[1]; // The last entry MUST point to the first in order to "close" the polygon
  poly_x[5] := poly_x[1];
 
  P_y       := 0_50_0000; //   This point is within the polygon
  P_x       := 0_50_0000;
 
  checkPoly(Position_x := P_x, Position_y := P_y,
            Polygon_x[1] := poly_x[1], Polygon_y[1] := poly_y[1],
            Polygon_x[2] := poly_x[2], Polygon_y[2] := poly_y[2],
            Polygon_x[3] := poly_x[3], Polygon_y[3] := poly_y[3],
            Polygon_x[4] := poly_x[4], Polygon_y[4] := poly_y[4],
            Polygon_x[5] := poly_x[5], Polygon_y[5] := poly_y[5],
            Corners := 5 ); // Number of corners that defines the polygon.
 
  IF checkPoly.Within THEN
    DebugMsg(message:="Inside");
  ELSE
    DebugMsg(message:="Outside");
  END_IF;
 
END_PROGRAM;