This repo contains a Python and a MATLAB script for generating offset polygons from a set of 2D coordinates. The scripts were originally developed for geofencing purposes, but can be applied for other uses as well.
The script polygon_offset.py
for Python and polygon_offset.m
for MATLAB takes a set of 2D coordinates that make a polygon, an offseting distance, and a desired direction (inward or outward) to calculate new coordinates that make an offset polygon. The python script is more advanced than the one in MATLAB as it uses the utm
library for Python to convert latitude and longitude coordinates to UTM coordinates, and performs the offset calculation in UTM space. The script returns the offset polygon in lat/long and UTM coordinates. The script in MATLAB doesn't have the capability of working with latitude/longitude coordinates. However, one could use the matlab function utmconv
for that purpose.
polygon
: 2D array containing the coordinates of all vertices of a polygonoffset
: desired offset distancedirection
: direction at which the offset is desired ('i' for inward or 'o' for outward)zone
andletter
: UTM zone at which the lat/long coordinates were collected (python script only)
- 2D array containing the coordinates of all vertices of the offset polygon (lat/long and UTM)
The Python script has the following dependencies:
numpy
utm
import polygon_offset
geofence = [[latitude_coordinates],[longitude_coordinates]]
offset = 10
direction = 'o' # Outward (expansion)
zone = 10
letter = 'N'
offset_polygon = polygon_offset(geofence, offset, direction, zone, letter)
geofence = [[latitude_coordinates],[longitude_coordinates]]
offset = 10
direction = 'o' # Outward (expansion)
offset_polygon = polygon_offset(geofence, offset, direction)
- Script assumes that the input polygon is closed, i.e., the first and last vertex are the same.
- Script assumes that the input polygon is simple, i.e., it does not self-intersect
- The script only works with 2D polygons.
- The repository includes example excel files with latitude/longitude coordinates of buildings and a roundabout that can be used to demonstrate the usage of the python script. Below are a few screenshots from the outputs of the python script:
- In addition, this repository includes a MATLAB file (
Polygon_ofsett_demo.m
) that demonstrates the usage of the MATLAB script.Below is a screenshot from the output of the MATLAB demo file:
[1] https://stackoverflow.com/questions/54033808/how-to-offset-polygon-edges
If you would like to contribute to this project, please contact the author.
This project is licensed under the terms of the MIT license.
Please let me know if there's anything else I can help you with.