-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JuPedSim] Added a tool for WKT polygon visualization.
- Loading branch information
1 parent
ba07e6b
commit 637b658
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
import sys | ||
from matplotlib.pyplot import figure, plot, gca, show | ||
from shapely import wkt | ||
from shapely.geometry import Polygon, MultiPolygon | ||
|
||
if 'SUMO_HOME' in os.environ: | ||
tools = os.path.join(os.environ['SUMO_HOME'], 'tools') | ||
sys.path.append(tools) | ||
else: | ||
sys.exit("Please set environment variable 'SUMO_HOME' to your SUMO binary directory.") | ||
|
||
import sumolib | ||
from sumolib.options import ArgumentParser | ||
|
||
def plotPolygonWithHoles(polygon, options): | ||
x, y = polygon.exterior.coords.xy | ||
plot(x, y, color=options.color) | ||
for hole in polygon.interiors: | ||
if Polygon(hole).area > options.area_threshold: | ||
x, y = hole.coords.xy | ||
plot(x, y, color=options.color) | ||
|
||
def plotMultiPolygonWithHoles(multipolygon, options): | ||
for polygon in multipolygon.geoms: | ||
plotPolygonWithHoles(polygon, options['color']) | ||
|
||
def main(args=None): | ||
ap = ArgumentParser(description="Plot a polygon, or the difference between two polygons, given in WKT format.") | ||
ap.add_argument('-f','--filename', category='input', type=ap.file, help='Name of a WKT file', required=True) | ||
ap.add_argument('-o','--other-filename', category='input', type=ap.file, help='Name of another WKT file') | ||
ap.add_argument('-a', '--area-threshold', category='processing', default=0.01, help='Area threshold used to filter small holes') | ||
ap.add_argument('-m', '--color', category='processing', default='blue', help='Color used to draw the polygons') | ||
options = ap.parse_args(args=args) | ||
|
||
with open(options.filename) as file: | ||
wktString = file.read() | ||
polygon = wkt.loads(wktString) | ||
if options.other_filename: | ||
with open(options.other_filename) as otherFile: | ||
otherWKTString = otherFile.read() | ||
otherPolygon = polygon.loads(otherWKTString) | ||
polygon = polygon.difference(otherPolygon) | ||
|
||
figure() | ||
if type(polygon) == Polygon: | ||
plotPolygonWithHoles(polygon, options) | ||
elif type(polygon) == MultiPolygon: | ||
plotMultiPolygonWithHoles(polygon, options) | ||
gca().set_aspect('equal') | ||
show() | ||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except ValueError as e: | ||
sys.exit(e) |